1 | #ifndef INITIALIZER_H |
---|
2 | #define INITIALIZER_H |
---|
3 | |
---|
4 | #include "SynTree.h" |
---|
5 | #include "Visitor.h" |
---|
6 | #include "Mutator.h" |
---|
7 | |
---|
8 | #include <cassert> |
---|
9 | |
---|
10 | |
---|
11 | // Initializer: base class for object initializers (provide default values) |
---|
12 | class Initializer { |
---|
13 | public: |
---|
14 | // Initializer( std::string _name = std::string(""), int _pos = 0 ); |
---|
15 | Initializer( ); |
---|
16 | virtual ~Initializer(); |
---|
17 | |
---|
18 | static std::string designator_name( Expression *designator ); |
---|
19 | |
---|
20 | // void set_name( std::string newValue ) { name = newValue; } |
---|
21 | // std::string get_name() const { return name; } |
---|
22 | |
---|
23 | // void set_pos( int newValue ) { pos = newValue; } |
---|
24 | // int get_pos() const { return pos; } |
---|
25 | virtual void set_designators( std::list<Expression *> & ) { assert(false); } |
---|
26 | virtual std::list<Expression *> &get_designators() { |
---|
27 | assert(false); |
---|
28 | std::list<Expression *> *ret = 0; return *ret; // never reached |
---|
29 | } |
---|
30 | |
---|
31 | virtual Initializer *clone() const = 0; |
---|
32 | virtual void accept( Visitor &v ) = 0; |
---|
33 | virtual Initializer *acceptMutator( Mutator &m ) = 0; |
---|
34 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
35 | private: |
---|
36 | // std::string name; |
---|
37 | // int pos; |
---|
38 | }; |
---|
39 | |
---|
40 | // SingleInit represents an initializer for a common object (e.g., int x = 4) |
---|
41 | class SingleInit : public Initializer { |
---|
42 | public: |
---|
43 | SingleInit( Expression *value, std::list< Expression *> &designators ); |
---|
44 | SingleInit( const SingleInit &other ); |
---|
45 | virtual ~SingleInit(); |
---|
46 | |
---|
47 | Expression *get_value() { return value; } |
---|
48 | void set_value( Expression *newValue ) { value = newValue; } |
---|
49 | |
---|
50 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
51 | std::list<Expression *> &get_designators() { return designators; } |
---|
52 | |
---|
53 | virtual SingleInit *clone() const; |
---|
54 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
55 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
56 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
57 | private: |
---|
58 | //Constant *value; |
---|
59 | Expression *value; // has to be a compile-time constant |
---|
60 | std::list< Expression * > designators; |
---|
61 | }; |
---|
62 | |
---|
63 | // MemberInit represents an initializer for a member of an aggregate object (e.g., struct q { int a; } x = { a : 4 } ) |
---|
64 | class MemberInit : public Initializer { |
---|
65 | public: |
---|
66 | MemberInit( Expression *value, std::string member = std::string("") ); |
---|
67 | virtual ~MemberInit(); |
---|
68 | |
---|
69 | std::string get_member() { return member; } |
---|
70 | void set_member( std::string newValue ) { member = newValue; } |
---|
71 | Expression *get_value() { return value; } |
---|
72 | void set_value( Expression *newValue ) { value = newValue; } |
---|
73 | |
---|
74 | virtual MemberInit *clone() const; |
---|
75 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
76 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
77 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
78 | private: |
---|
79 | std::string member; |
---|
80 | Expression *value; |
---|
81 | }; |
---|
82 | |
---|
83 | // ElementInit represents an initializer of an element of an array (e.g., [10] int x = { [7] : 4 } |
---|
84 | class ElementInit : public Initializer { |
---|
85 | public: |
---|
86 | ElementInit( Expression *value ); |
---|
87 | virtual ~ElementInit(); |
---|
88 | |
---|
89 | int get_index() { return index; } |
---|
90 | void set_index( int newValue ) { index = newValue; } |
---|
91 | Expression *get_value() { return value; } |
---|
92 | void set_value( Expression *newValue ) { value = newValue; } |
---|
93 | |
---|
94 | virtual ElementInit *clone() const; |
---|
95 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
96 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
97 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
98 | private: |
---|
99 | int index; |
---|
100 | Expression *value; |
---|
101 | }; |
---|
102 | |
---|
103 | // ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize |
---|
104 | // an array or aggregate |
---|
105 | class ListInit : public Initializer { |
---|
106 | public: |
---|
107 | ListInit( std::list<Initializer*> &, |
---|
108 | std::list<Expression *> &designators = *(new std::list<Expression *>()) ); |
---|
109 | virtual ~ListInit(); |
---|
110 | |
---|
111 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
112 | std::list<Expression *> &get_designators() { return designators; } |
---|
113 | void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; } |
---|
114 | std::list<Initializer*> &get_initializers() { return initializers; } |
---|
115 | |
---|
116 | std::list<Initializer*>::iterator begin_initializers() { return initializers.begin(); } |
---|
117 | std::list<Initializer*>::iterator end_initializers() { return initializers.end(); } |
---|
118 | |
---|
119 | virtual ListInit *clone() const; |
---|
120 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
121 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
122 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
123 | private: |
---|
124 | std::list<Initializer*> initializers; // order *is* important |
---|
125 | std::list<Expression *> designators; |
---|
126 | }; |
---|
127 | |
---|
128 | #endif // INITIALIZER_H |
---|