forb
type_array.hpp
1 #ifndef FORBCC_ARRAY_TYPE_H
2 #define FORBCC_ARRAY_TYPE_H
3 
4 #include <string>
5 #include <algorithm>
6 #include <cassert>
7 
8 #include <types/type.hpp>
9 #include <templates/shareable.hpp>
10 
11 namespace forbcc {
12 
14  class type_array : public type, public shareable<type_array> {
15 
16  /* ************************************************* ALIAS ************************************************** */
17  public:
19  using length_t = long;
20 
21  /* *********************************************** ATTRIBUTES *********************************************** */
22  private:
24  std::shared_ptr<const type> _item_type;
25 
27  length_t _length = 0;
28 
30  size_t _size = 0;
31 
32  /* ************************************************* STATIC ************************************************* */
33  public:
36 
41 
43  static std::string to_identifier(const std::shared_ptr<const type> &item_type, const length_t length) {
44  // FIXME: This could probably lead to conflicts, but they would be extremely rare.
45  std::string codename = item_type->codename();
46  std::replace(codename.begin(), codename.end(), ':', '_');
47  return "_Forb_Array_" + item_type->codename() + "_D_" + std::to_string(length) + "_";
48  };
49 
50  /* ********************************************** CONSTRUCTORS ********************************************** */
51  public:
53  type_array() : type(), _item_type(nullptr) {};
54 
58  type_array(const std::shared_ptr<const type> &item_type, const length_t length)
59  : type(nullptr, to_identifier(item_type, length)),
60  _item_type(item_type),
61  _length(length),
62  _size(item_type->size_of() * length) {
63  assert((length > 0 && "The length of the array must be strictly positive!"));
64  };
65 
67  type_array(const type_array *item_type, const int length) = delete;
68 
69  /**************************************************************************************************************/
70 
72  ~type_array() override = default;
73 
75  type_array(type_array &&) = default;
76 
78  type_array &operator=(type_array &&) = default;
79 
81  type_array(const type_array &) = default;
82 
84  type_array &operator=(const type_array &) = default;
85 
86  /**************************************************************************************************************/
87 
89  void print_declaration(code_ostream &) const override {};
90 
92  void print_definition(code_ostream &) const override {};
93 
95  std::shared_ptr<const type> item_type() const {
96  return _item_type;
97  };
98 
100  length_t length() const {
101  return _length;
102  };
103 
105  void print_var_declaration(code_ostream &out, const std::string &var_name) const override;
106 
108  void print_var_definition(code_ostream &out, const std::string &var_name, bool force_stack) const override;
109 
111  void print_var_lvalue(code_ostream &out, const std::string &var_name, bool is_stack) const override;
112 
114  void print_var_reference(code_ostream &out, const std::string &var_name) const override;
115 
117  void print_var_marshal(code_ostream &out, const std::string &var_name, const marshal &do_undo,
118  bool force_stack) const override;
119 
121  void print_var_serialize(code_ostream &out, const std::string &var_name, const serialize &do_undo,
122  bool force_stack) const override;
123 
125  size_t size_of() const override {
126  return _size;
127  };
128 
130  size_t alignment() const override {
131  return item_type()->alignment();
132  };
133  };
134 
135 } // namespace forbcc
136 
137 #endif //FORBCC_ARRAY_TYPE_H
std::shared_ptr< const type > item_type() const
Returns the type of the items of the given array.
Definition: type_array.hpp:95
type_array()
Empty array type, used to preallocate variables in arrays or to use later assignment operator...
Definition: type_array.hpp:53
virtual std::string codename() const
Returns the codename of the given entity, which is the name of the entity as seen from the global sco...
Definition: entity.hpp:81
void print_var_serialize(code_ostream &out, const std::string &var_name, const serialize &do_undo, bool force_stack) const override
Prints the serialization/deserialization of a variable of the given type.
Definition: type_array.cpp:58
A template that can be used to declare a list of unique ordered items.
Definition: ordered_unique_list.hpp:16
void print_definition(code_ostream &) const override
Does nothing, primitive types do not need to be defined.
Definition: type_array.hpp:92
void print_var_declaration(code_ostream &out, const std::string &var_name) const override
Prints the declaration of a variable of the given type (no semicolon).
Definition: type_array.cpp:15
length_t length() const
Returns the length of the array.
Definition: type_array.hpp:100
size_t alignment() const override
Returns the alignment of the given type.
Definition: type_array.hpp:130
void print_var_lvalue(code_ostream &out, const std::string &var_name, bool is_stack) const override
Print the acquisition of the value of a variable of this type.
Definition: type_array.cpp:31
A template that can be used to ease the declaration of a shared_pointer of a given type...
Definition: shareable.hpp:13
size_t size_of() const override
Returns the size of the given type.
Definition: type_array.hpp:125
This output stream supports indentation of code lines, which are formatted at each std::endl...
Definition: code_ostream.hpp:14
void print_var_reference(code_ostream &out, const std::string &var_name) const override
Prints the declaration of a reference variable of the given type (no semicolon).
Definition: type_array.cpp:42
void print_var_definition(code_ostream &out, const std::string &var_name, bool force_stack) const override
Print the definition of a variable of this type.
Definition: type_array.cpp:19
Definition: code_ostream.hpp:11
static type_array_list arrays
The list of all known arrays.
Definition: type_array.hpp:40
~type_array() override=default
This class is virtual, so it requires a virtual destructor.
void print_declaration(code_ostream &) const override
Does nothing, primitive types do not need to be declared.
Definition: type_array.hpp:89
Identifies an array of a given type, to be used in forbcc::method or in forbcc::type_struct definitio...
Definition: type_array.hpp:14
void print_var_marshal(code_ostream &out, const std::string &var_name, const marshal &do_undo, bool force_stack) const override
Prints the marshalling/unmarshalling operation of a variable of the given type.
Definition: type_array.cpp:46
type_array & operator=(type_array &&)=default
This class supports moving.
Base class used to define primitive types, custom types (structures) and arrays.
Definition: type.hpp:22
type_array(const std::shared_ptr< const type > &item_type, const length_t length)
Any trivially copyable type can be accepted as an array item type (except another array_type)...
Definition: type_array.hpp:58
static std::string to_identifier(const std::shared_ptr< const type > &item_type, const length_t length)
Converts a pair of item_type and length to an unique identifier, to be used when storing known array ...
Definition: type_array.hpp:43
long length_t
The type of the length attribute (which is a signed type, but values less than zero are considered in...
Definition: type_array.hpp:19