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 | // Declaration.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 : Fri May 06 11:16:45 2016 |
---|
13 | // Update Count : 33 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef DECLARATION_H |
---|
17 | #define DECLARATION_H |
---|
18 | |
---|
19 | #include "SynTree.h" |
---|
20 | #include "Visitor.h" |
---|
21 | #include "Mutator.h" |
---|
22 | #include "Parser/LinkageSpec.h" |
---|
23 | #include "Parser/ParseNode.h" |
---|
24 | |
---|
25 | class Declaration { |
---|
26 | public: |
---|
27 | Declaration( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage ); |
---|
28 | Declaration( const Declaration &other ); |
---|
29 | virtual ~Declaration(); |
---|
30 | |
---|
31 | const std::string &get_name() const { return name; } |
---|
32 | void set_name( std::string newValue ) { name = newValue; } |
---|
33 | DeclarationNode::StorageClass get_storageClass() const { return storageClass; } |
---|
34 | void set_storageClass( DeclarationNode::StorageClass newValue ) { storageClass = newValue; } |
---|
35 | LinkageSpec::Type get_linkage() const { return linkage; } |
---|
36 | void set_linkage( LinkageSpec::Type newValue ) { linkage = newValue; } |
---|
37 | bool get_isInline() const { return isInline; } |
---|
38 | void set_isInline( bool newValue ) { isInline = newValue; } |
---|
39 | bool get_isNoreturn() const { return isNoreturn; } |
---|
40 | void set_isNoreturn( bool newValue ) { isNoreturn = newValue; } |
---|
41 | UniqueId get_uniqueId() const { return uniqueId; } |
---|
42 | |
---|
43 | void fixUniqueId( void ); |
---|
44 | virtual Declaration *clone() const = 0; |
---|
45 | virtual void accept( Visitor &v ) = 0; |
---|
46 | virtual Declaration *acceptMutator( Mutator &m ) = 0; |
---|
47 | virtual void print( std::ostream &os, int indent = 0 ) const = 0; |
---|
48 | virtual void printShort( std::ostream &os, int indent = 0 ) const = 0; |
---|
49 | |
---|
50 | static void dumpIds( std::ostream &os ); |
---|
51 | static Declaration *declFromId( UniqueId id ); |
---|
52 | private: |
---|
53 | std::string name; |
---|
54 | DeclarationNode::StorageClass storageClass; |
---|
55 | LinkageSpec::Type linkage; |
---|
56 | bool isInline, isNoreturn; |
---|
57 | UniqueId uniqueId; |
---|
58 | }; |
---|
59 | |
---|
60 | class DeclarationWithType : public Declaration { |
---|
61 | public: |
---|
62 | DeclarationWithType( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage ); |
---|
63 | DeclarationWithType( const DeclarationWithType &other ); |
---|
64 | virtual ~DeclarationWithType(); |
---|
65 | |
---|
66 | std::string get_mangleName() const { return mangleName; } |
---|
67 | void set_mangleName( std::string newValue ) { mangleName = newValue; } |
---|
68 | |
---|
69 | virtual DeclarationWithType *clone() const = 0; |
---|
70 | virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; |
---|
71 | |
---|
72 | virtual Type *get_type() const = 0; |
---|
73 | virtual void set_type(Type *) = 0; |
---|
74 | private: |
---|
75 | // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 |
---|
76 | std::string mangleName; |
---|
77 | }; |
---|
78 | |
---|
79 | class ObjectDecl : public DeclarationWithType { |
---|
80 | typedef DeclarationWithType Parent; |
---|
81 | public: |
---|
82 | ObjectDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, Expression *bitfieldWidth, Type *type, Initializer *init, bool isInline = false, bool isNoreturn = false ); |
---|
83 | ObjectDecl( const ObjectDecl &other ); |
---|
84 | virtual ~ObjectDecl(); |
---|
85 | |
---|
86 | virtual Type *get_type() const { return type; } |
---|
87 | virtual void set_type(Type *newType) { type = newType; } |
---|
88 | |
---|
89 | Initializer *get_init() const { return init; } |
---|
90 | void set_init( Initializer *newValue ) { init = newValue; } |
---|
91 | Expression *get_bitfieldWidth() const { return bitfieldWidth; } |
---|
92 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } |
---|
93 | |
---|
94 | virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); } |
---|
95 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
96 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
97 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
98 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
99 | private: |
---|
100 | Type *type; |
---|
101 | Initializer *init; |
---|
102 | Expression *bitfieldWidth; |
---|
103 | }; |
---|
104 | |
---|
105 | class FunctionDecl : public DeclarationWithType { |
---|
106 | typedef DeclarationWithType Parent; |
---|
107 | public: |
---|
108 | // temporary - merge this into general GCC attributes |
---|
109 | enum Attribute { |
---|
110 | NoAttribute, Constructor, Destructor, |
---|
111 | }; |
---|
112 | |
---|
113 | FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, Attribute attribute = NoAttribute ); |
---|
114 | FunctionDecl( const FunctionDecl &other ); |
---|
115 | virtual ~FunctionDecl(); |
---|
116 | |
---|
117 | Type *get_type() const; |
---|
118 | virtual void set_type(Type *); |
---|
119 | |
---|
120 | FunctionType *get_functionType() const { return type; } |
---|
121 | void set_functionType( FunctionType *newValue ) { type = newValue; } |
---|
122 | CompoundStmt *get_statements() const { return statements; } |
---|
123 | void set_statements( CompoundStmt *newValue ) { statements = newValue; } |
---|
124 | std::list< std::string >& get_oldIdents() { return oldIdents; } |
---|
125 | std::list< Declaration* >& get_oldDecls() { return oldDecls; } |
---|
126 | Attribute get_attribute() const { return attribute; } |
---|
127 | void set_attribute( Attribute newValue ) { attribute = newValue; } |
---|
128 | |
---|
129 | virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); } |
---|
130 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
131 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
132 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
133 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
134 | private: |
---|
135 | FunctionType *type; |
---|
136 | CompoundStmt *statements; |
---|
137 | std::list< std::string > oldIdents; |
---|
138 | std::list< Declaration* > oldDecls; |
---|
139 | Attribute attribute; |
---|
140 | }; |
---|
141 | |
---|
142 | class NamedTypeDecl : public Declaration { |
---|
143 | typedef Declaration Parent; |
---|
144 | public: |
---|
145 | NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ); |
---|
146 | NamedTypeDecl( const TypeDecl &other ); |
---|
147 | virtual ~NamedTypeDecl(); |
---|
148 | |
---|
149 | Type *get_base() const { return base; } |
---|
150 | void set_base( Type *newValue ) { base = newValue; } |
---|
151 | std::list< TypeDecl* >& get_parameters() { return parameters; } |
---|
152 | std::list< DeclarationWithType* >& get_assertions() { return assertions; } |
---|
153 | |
---|
154 | virtual NamedTypeDecl *clone() const = 0; |
---|
155 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
156 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
157 | protected: |
---|
158 | virtual std::string typeString() const = 0; |
---|
159 | private: |
---|
160 | Type *base; |
---|
161 | std::list< TypeDecl* > parameters; |
---|
162 | std::list< DeclarationWithType* > assertions; |
---|
163 | }; |
---|
164 | |
---|
165 | class TypeDecl : public NamedTypeDecl { |
---|
166 | typedef NamedTypeDecl Parent; |
---|
167 | public: |
---|
168 | enum Kind { Any, Dtype, Ftype }; |
---|
169 | |
---|
170 | TypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type, Kind kind ); |
---|
171 | TypeDecl( const TypeDecl &other ); |
---|
172 | |
---|
173 | Kind get_kind() const { return kind; } |
---|
174 | |
---|
175 | virtual TypeDecl *clone() const { return new TypeDecl( *this ); } |
---|
176 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
177 | virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
178 | private: |
---|
179 | virtual std::string typeString() const; |
---|
180 | Kind kind; |
---|
181 | }; |
---|
182 | |
---|
183 | class TypedefDecl : public NamedTypeDecl { |
---|
184 | typedef NamedTypeDecl Parent; |
---|
185 | public: |
---|
186 | TypedefDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ) : Parent( name, sc, type ) {} |
---|
187 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} |
---|
188 | |
---|
189 | virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); } |
---|
190 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
191 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
192 | private: |
---|
193 | virtual std::string typeString() const; |
---|
194 | }; |
---|
195 | |
---|
196 | class AggregateDecl : public Declaration { |
---|
197 | typedef Declaration Parent; |
---|
198 | public: |
---|
199 | AggregateDecl( const std::string &name ); |
---|
200 | AggregateDecl( const AggregateDecl &other ); |
---|
201 | virtual ~AggregateDecl(); |
---|
202 | |
---|
203 | std::list<Declaration*>& get_members() { return members; } |
---|
204 | std::list<TypeDecl*>& get_parameters() { return parameters; } |
---|
205 | |
---|
206 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
207 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
208 | protected: |
---|
209 | virtual std::string typeString() const = 0; |
---|
210 | |
---|
211 | private: |
---|
212 | std::list<Declaration*> members; |
---|
213 | std::list<TypeDecl*> parameters; |
---|
214 | }; |
---|
215 | |
---|
216 | class StructDecl : public AggregateDecl { |
---|
217 | typedef AggregateDecl Parent; |
---|
218 | public: |
---|
219 | StructDecl( const std::string &name ) : Parent( name ) {} |
---|
220 | StructDecl( const StructDecl &other ) : Parent( other ) {} |
---|
221 | |
---|
222 | virtual StructDecl *clone() const { return new StructDecl( *this ); } |
---|
223 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
224 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
225 | |
---|
226 | private: |
---|
227 | virtual std::string typeString() const; |
---|
228 | }; |
---|
229 | |
---|
230 | class UnionDecl : public AggregateDecl { |
---|
231 | typedef AggregateDecl Parent; |
---|
232 | public: |
---|
233 | UnionDecl( const std::string &name ) : Parent( name ) {} |
---|
234 | UnionDecl( const UnionDecl &other ) : Parent( other ) {} |
---|
235 | |
---|
236 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); } |
---|
237 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
238 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
239 | private: |
---|
240 | virtual std::string typeString() const; |
---|
241 | }; |
---|
242 | |
---|
243 | class EnumDecl : public AggregateDecl { |
---|
244 | typedef AggregateDecl Parent; |
---|
245 | public: |
---|
246 | EnumDecl( const std::string &name ) : Parent( name ) {} |
---|
247 | EnumDecl( const EnumDecl &other ) : Parent( other ) {} |
---|
248 | |
---|
249 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); } |
---|
250 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
251 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
252 | private: |
---|
253 | virtual std::string typeString() const; |
---|
254 | }; |
---|
255 | |
---|
256 | class TraitDecl : public AggregateDecl { |
---|
257 | typedef AggregateDecl Parent; |
---|
258 | public: |
---|
259 | TraitDecl( const std::string &name ) : Parent( name ) {} |
---|
260 | TraitDecl( const TraitDecl &other ) : Parent( other ) {} |
---|
261 | |
---|
262 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); } |
---|
263 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
264 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
265 | private: |
---|
266 | virtual std::string typeString() const; |
---|
267 | }; |
---|
268 | |
---|
269 | std::ostream & operator<<( std::ostream & out, Declaration * decl ); |
---|
270 | |
---|
271 | #endif // DECLARATION_H |
---|
272 | |
---|
273 | // Local Variables: // |
---|
274 | // tab-width: 4 // |
---|
275 | // mode: c++ // |
---|
276 | // compile-command: "make install" // |
---|
277 | // End: // |
---|