forb
variable.hpp
1 #ifndef FORBCC_VARIABLE_H
2 #define FORBCC_VARIABLE_H
3 
4 #include <entity.hpp>
5 #include <types/type.hpp>
6 #include <templates/shareable.hpp>
7 
8 namespace forbcc {
9  class code_ostream;
10 
13  class variable : public entity, public shareable<variable> {
15  std::shared_ptr<const type> _var_type;
16 
17  public:
18  /* ********************************************** CONSTRUCTORS ********************************************** */
19 
21  variable() : entity(nullptr, ""), _var_type(nullptr) {};
22 
26  variable(const std::shared_ptr<const type> &var_type, const std::string &name)
27  : entity(nullptr, name),
28  _var_type(var_type) {};
29 
30  /**************************************************************************************************************/
31 
33  ~variable() override = default;
34 
36  variable(variable &&) = default;
37 
39  variable &operator=(variable &&) = default;
40 
42  variable(const variable &) = default;
43 
45  variable &operator=(const variable &) = default;
46 
47  /**************************************************************************************************************/
48 
50  const std::shared_ptr<const type> &var_type() const {
51  return _var_type;
52  };
53 
56  void print_declaration(code_ostream &out) const override {
57  print_declaration(out, true);
58  };
59 
61  void print_definition(code_ostream &) const override {};
62 
64  void print_declaration(code_ostream &out, bool force_stack) const {
65  _var_type->print_var_definition(out, name(), force_stack);
66  };
67 
69  void print_lvalue(code_ostream &out, bool force_stack = true) const {
70  _var_type->print_var_lvalue(out, name(), force_stack);
71  }
72 
75  void print_reference(code_ostream &out) const {
76  _var_type->print_var_reference(out, name());
77  };
78 
81  void print_marshal(code_ostream &out, const marshal &do_undo, bool force_stack = true) const {
82  _var_type->print_var_marshal(out, name(), do_undo, force_stack);
83  };
84 
87  void print_serialize(code_ostream &out, const serialize &do_undo, bool force_stack = true) const {
88  _var_type->print_var_serialize(out, name(), do_undo, force_stack);
89  };
90 
91  };
92 
93 } // namespace forbcc
94 
95 #endif //FORBCC_VARIABLE_H
void print_declaration(code_ostream &out, bool force_stack) const
Prints the declaration of a variable that might be allocated on the stack or on the heap...
Definition: variable.hpp:64
void print_marshal(code_ostream &out, const marshal &do_undo, bool force_stack=true) const
Prints the variable declaration, proxy to forbcc::type::print_var_marshal, which is a virtual functio...
Definition: variable.hpp:81
void print_serialize(code_ostream &out, const serialize &do_undo, bool force_stack=true) const
Prints the variable declaration, proxy to forbcc::type::print_var_serialize, which is a virtual funct...
Definition: variable.hpp:87
variable(const std::shared_ptr< const type > &var_type, const std::string &name)
Constructs a variable with a given type.
Definition: variable.hpp:26
void print_reference(code_ostream &out) const
Prints the variable declaration, proxy to forbcc::type::print_var_reference, which is a virtual funct...
Definition: variable.hpp:75
void print_declaration(code_ostream &out) const override
Prints the variable declaration, proxy to forbcc::type::print_var_declaration, which is a virtual fun...
Definition: variable.hpp:56
void print_lvalue(code_ostream &out, bool force_stack=true) const
Prints the lvalue of the given variable.
Definition: variable.hpp:69
A template that can be used to ease the declaration of a shared_pointer of a given type...
Definition: shareable.hpp:13
std::string name() const
Returns the name of the given entity.
Definition: entity.hpp:68
variable()
Empty variable, used to preallocate variables in arrays or to use later assignment operator...
Definition: variable.hpp:21
void print_definition(code_ostream &) const override
Does nothing, variables do not require definition.
Definition: variable.hpp:61
This output stream supports indentation of code lines, which are formatted at each std::endl...
Definition: code_ostream.hpp:14
~variable() override=default
This class is virtual, so it requires a virtual destructor.
Definition: code_ostream.hpp:11
const std::shared_ptr< const type > & var_type() const
Returns a pointer to the type of the variable.
Definition: variable.hpp:50
Any code entity that should be generated by the compiler: namespaces, classes, structures, methods, variables.
Definition: entity.hpp:18
Represents either an attribute of a forbcc::type_struct class or an automatic variable declared withi...
Definition: variable.hpp:13
variable & operator=(variable &&)=default
This class supports moving.