1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Initializer.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Tue Feb 09 14:40:15 2016 |
---|
13 | // Update Count : 19 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef INITIALIZER_H |
---|
17 | #define INITIALIZER_H |
---|
18 | |
---|
19 | #include "SynTree.h" |
---|
20 | #include "Visitor.h" |
---|
21 | #include "Mutator.h" |
---|
22 | |
---|
23 | #include <cassert> |
---|
24 | |
---|
25 | // Initializer: base class for object initializers (provide default values) |
---|
26 | class Initializer { |
---|
27 | public: |
---|
28 | // Initializer( std::string _name = std::string(""), int _pos = 0 ); |
---|
29 | Initializer( bool maybeConstructed ); |
---|
30 | virtual ~Initializer(); |
---|
31 | |
---|
32 | static std::string designator_name( Expression *designator ); |
---|
33 | |
---|
34 | // void set_name( std::string newValue ) { name = newValue; } |
---|
35 | // std::string get_name() const { return name; } |
---|
36 | |
---|
37 | // void set_pos( int newValue ) { pos = newValue; } |
---|
38 | // int get_pos() const { return pos; } |
---|
39 | virtual void set_designators( std::list<Expression *> & ) { assert(false); } |
---|
40 | virtual std::list<Expression *> &get_designators() { |
---|
41 | assert(false); |
---|
42 | std::list<Expression *> *ret = 0; return *ret; // never reached |
---|
43 | } |
---|
44 | |
---|
45 | bool get_maybeConstructed() { return maybeConstructed; } |
---|
46 | |
---|
47 | virtual Initializer *clone() const = 0; |
---|
48 | virtual void accept( Visitor &v ) = 0; |
---|
49 | virtual Initializer *acceptMutator( Mutator &m ) = 0; |
---|
50 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
51 | private: |
---|
52 | // std::string name; |
---|
53 | // int pos; |
---|
54 | bool maybeConstructed; |
---|
55 | }; |
---|
56 | |
---|
57 | // SingleInit represents an initializer for a common object (e.g., int x = 4) |
---|
58 | class SingleInit : public Initializer { |
---|
59 | public: |
---|
60 | SingleInit( Expression *value, std::list< Expression *> &designators, bool maybeConstructed = false ); |
---|
61 | SingleInit( const SingleInit &other ); |
---|
62 | virtual ~SingleInit(); |
---|
63 | |
---|
64 | Expression *get_value() { return value; } |
---|
65 | void set_value( Expression *newValue ) { value = newValue; } |
---|
66 | |
---|
67 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
68 | std::list<Expression *> &get_designators() { return designators; } |
---|
69 | |
---|
70 | virtual SingleInit *clone() const; |
---|
71 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
72 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
73 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
74 | private: |
---|
75 | //Constant *value; |
---|
76 | Expression *value; // has to be a compile-time constant |
---|
77 | std::list< Expression * > designators; |
---|
78 | }; |
---|
79 | |
---|
80 | // ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize |
---|
81 | // an array or aggregate |
---|
82 | class ListInit : public Initializer { |
---|
83 | public: |
---|
84 | ListInit( std::list<Initializer*> &, |
---|
85 | std::list<Expression *> &designators, bool maybeConstructed = false ); |
---|
86 | virtual ~ListInit(); |
---|
87 | |
---|
88 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
89 | std::list<Expression *> &get_designators() { return designators; } |
---|
90 | void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; } |
---|
91 | std::list<Initializer*> &get_initializers() { return initializers; } |
---|
92 | |
---|
93 | std::list<Initializer*>::iterator begin_initializers() { return initializers.begin(); } |
---|
94 | std::list<Initializer*>::iterator end_initializers() { return initializers.end(); } |
---|
95 | |
---|
96 | virtual ListInit *clone() const; |
---|
97 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
98 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
99 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
100 | private: |
---|
101 | std::list<Initializer*> initializers; // order *is* important |
---|
102 | std::list<Expression *> designators; |
---|
103 | }; |
---|
104 | |
---|
105 | // ConstructorInit represents an initializer that is either a constructor expression or |
---|
106 | // a C-style initializer. |
---|
107 | class ConstructorInit : public Initializer { |
---|
108 | public: |
---|
109 | ConstructorInit( Expression * ctor, Initializer * init ); |
---|
110 | virtual ~ConstructorInit(); |
---|
111 | |
---|
112 | void set_ctor( Expression * newValue ) { ctor = newValue; } |
---|
113 | Expression * get_ctor() const { return ctor; } |
---|
114 | void set_init( Initializer * newValue ) { init = newValue; } |
---|
115 | Initializer * get_init() const { return init; } |
---|
116 | |
---|
117 | virtual ConstructorInit *clone() const; |
---|
118 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
119 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
120 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
121 | |
---|
122 | private: |
---|
123 | Expression * ctor; |
---|
124 | // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback |
---|
125 | // if an appropriate constructor definition is not found by the resolver |
---|
126 | Initializer * init; |
---|
127 | }; |
---|
128 | |
---|
129 | #endif // INITIALIZER_H |
---|
130 | |
---|
131 | // Local Variables: // |
---|
132 | // tab-width: 4 // |
---|
133 | // mode: c++ // |
---|
134 | // compile-command: "make install" // |
---|
135 | // End: // |
---|