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 : Andrew Beach |
---|
12 | // Last Modified On : Fri Aug 11 10:20:00 2017 |
---|
13 | // Update Count : 127 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <string> |
---|
19 | |
---|
20 | #include "BaseSyntaxNode.h" |
---|
21 | #include "Mutator.h" |
---|
22 | #include "Visitor.h" |
---|
23 | #include "SynTree.h" |
---|
24 | #include "Parser/LinkageSpec.h" |
---|
25 | #include "Parser/ParseNode.h" |
---|
26 | |
---|
27 | class Declaration : public BaseSyntaxNode { |
---|
28 | public: |
---|
29 | std::string name; |
---|
30 | LinkageSpec::Spec linkage; |
---|
31 | bool extension = false; |
---|
32 | |
---|
33 | Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage ); |
---|
34 | Declaration( const Declaration &other ); |
---|
35 | virtual ~Declaration(); |
---|
36 | |
---|
37 | const std::string &get_name() const { return name; } |
---|
38 | void set_name( std::string newValue ) { name = newValue; } |
---|
39 | |
---|
40 | Type::StorageClasses get_storageClasses() const { return storageClasses; } |
---|
41 | |
---|
42 | LinkageSpec::Spec get_linkage() const { return linkage; } |
---|
43 | void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; } |
---|
44 | |
---|
45 | UniqueId get_uniqueId() const { return uniqueId; } |
---|
46 | |
---|
47 | bool get_extension() const { return extension; } |
---|
48 | Declaration *set_extension( bool exten ) { extension = exten; return this; } |
---|
49 | |
---|
50 | void fixUniqueId( void ); |
---|
51 | virtual Declaration *clone() const = 0; |
---|
52 | virtual void accept( Visitor &v ) = 0; |
---|
53 | virtual Declaration *acceptMutator( Mutator &m ) = 0; |
---|
54 | virtual void print( std::ostream &os, int indent = 0 ) const = 0; |
---|
55 | virtual void printShort( std::ostream &os, int indent = 0 ) const = 0; |
---|
56 | |
---|
57 | static void dumpIds( std::ostream &os ); |
---|
58 | static Declaration *declFromId( UniqueId id ); |
---|
59 | |
---|
60 | private: |
---|
61 | Type::StorageClasses storageClasses; |
---|
62 | UniqueId uniqueId; |
---|
63 | }; |
---|
64 | |
---|
65 | class DeclarationWithType : public Declaration { |
---|
66 | public: |
---|
67 | // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 |
---|
68 | std::string mangleName; |
---|
69 | // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed |
---|
70 | int scopeLevel = 0; |
---|
71 | |
---|
72 | ConstantExpr *asmName; |
---|
73 | std::list< Attribute * > attributes; |
---|
74 | |
---|
75 | DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs ); |
---|
76 | DeclarationWithType( const DeclarationWithType &other ); |
---|
77 | virtual ~DeclarationWithType(); |
---|
78 | |
---|
79 | std::string get_mangleName() const { return mangleName; } |
---|
80 | DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; } |
---|
81 | |
---|
82 | std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
83 | |
---|
84 | int get_scopeLevel() const { return scopeLevel; } |
---|
85 | DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; } |
---|
86 | |
---|
87 | ConstantExpr *get_asmName() const { return asmName; } |
---|
88 | DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; } |
---|
89 | |
---|
90 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
91 | const std::list< Attribute * >& get_attributes() const { return attributes; } |
---|
92 | |
---|
93 | Type::FuncSpecifiers get_funcSpec() const { return fs; } |
---|
94 | //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; } |
---|
95 | |
---|
96 | virtual DeclarationWithType *clone() const = 0; |
---|
97 | virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; |
---|
98 | |
---|
99 | virtual Type *get_type() const = 0; |
---|
100 | virtual void set_type(Type *) = 0; |
---|
101 | |
---|
102 | private: |
---|
103 | Type::FuncSpecifiers fs; |
---|
104 | }; |
---|
105 | |
---|
106 | class ObjectDecl : public DeclarationWithType { |
---|
107 | typedef DeclarationWithType Parent; |
---|
108 | public: |
---|
109 | Type *type; |
---|
110 | Initializer *init; |
---|
111 | Expression *bitfieldWidth; |
---|
112 | |
---|
113 | ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init, |
---|
114 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() ); |
---|
115 | ObjectDecl( const ObjectDecl &other ); |
---|
116 | virtual ~ObjectDecl(); |
---|
117 | |
---|
118 | virtual Type *get_type() const { return type; } |
---|
119 | virtual void set_type(Type *newType) { type = newType; } |
---|
120 | |
---|
121 | Initializer *get_init() const { return init; } |
---|
122 | void set_init( Initializer *newValue ) { init = newValue; } |
---|
123 | |
---|
124 | Expression *get_bitfieldWidth() const { return bitfieldWidth; } |
---|
125 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } |
---|
126 | |
---|
127 | virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); } |
---|
128 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
129 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
130 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
131 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
132 | }; |
---|
133 | |
---|
134 | class FunctionDecl : public DeclarationWithType { |
---|
135 | typedef DeclarationWithType Parent; |
---|
136 | public: |
---|
137 | FunctionType *type; |
---|
138 | CompoundStmt *statements; |
---|
139 | |
---|
140 | FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, |
---|
141 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() ); |
---|
142 | FunctionDecl( const FunctionDecl &other ); |
---|
143 | virtual ~FunctionDecl(); |
---|
144 | |
---|
145 | Type *get_type() const; |
---|
146 | virtual void set_type(Type *); |
---|
147 | |
---|
148 | FunctionType *get_functionType() const { return type; } |
---|
149 | void set_functionType( FunctionType *newValue ) { type = newValue; } |
---|
150 | CompoundStmt *get_statements() const { return statements; } |
---|
151 | void set_statements( CompoundStmt *newValue ) { statements = newValue; } |
---|
152 | |
---|
153 | virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); } |
---|
154 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
155 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
156 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
157 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
158 | }; |
---|
159 | |
---|
160 | class NamedTypeDecl : public Declaration { |
---|
161 | typedef Declaration Parent; |
---|
162 | public: |
---|
163 | Type *base; |
---|
164 | std::list< TypeDecl* > parameters; |
---|
165 | std::list< DeclarationWithType* > assertions; |
---|
166 | |
---|
167 | NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type ); |
---|
168 | NamedTypeDecl( const NamedTypeDecl &other ); |
---|
169 | virtual ~NamedTypeDecl(); |
---|
170 | |
---|
171 | Type *get_base() const { return base; } |
---|
172 | void set_base( Type *newValue ) { base = newValue; } |
---|
173 | std::list< TypeDecl* >& get_parameters() { return parameters; } |
---|
174 | std::list< DeclarationWithType* >& get_assertions() { return assertions; } |
---|
175 | |
---|
176 | virtual std::string typeString() const = 0; |
---|
177 | |
---|
178 | virtual NamedTypeDecl *clone() const = 0; |
---|
179 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
180 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
181 | }; |
---|
182 | |
---|
183 | class TypeDecl : public NamedTypeDecl { |
---|
184 | typedef NamedTypeDecl Parent; |
---|
185 | public: |
---|
186 | enum Kind { Any, Dtype, Ftype, Ttype }; |
---|
187 | |
---|
188 | Type * init; |
---|
189 | bool sized; |
---|
190 | |
---|
191 | /// Data extracted from a type decl |
---|
192 | struct Data { |
---|
193 | TypeDecl::Kind kind; |
---|
194 | bool isComplete; |
---|
195 | Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {} |
---|
196 | Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {} |
---|
197 | Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {} |
---|
198 | bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; } |
---|
199 | bool operator!=(const Data & other) const { return !(*this == other);} |
---|
200 | }; |
---|
201 | |
---|
202 | TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr ); |
---|
203 | TypeDecl( const TypeDecl &other ); |
---|
204 | virtual ~TypeDecl(); |
---|
205 | |
---|
206 | Kind get_kind() const { return kind; } |
---|
207 | |
---|
208 | Type * get_init() const { return init; } |
---|
209 | TypeDecl * set_init( Type * newValue ) { init = newValue; return this; } |
---|
210 | |
---|
211 | bool isComplete() const { return kind == Any || sized; } |
---|
212 | bool get_sized() const { return sized; } |
---|
213 | TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; } |
---|
214 | |
---|
215 | virtual std::string typeString() const; |
---|
216 | virtual std::string genTypeString() const; |
---|
217 | |
---|
218 | virtual TypeDecl *clone() const { return new TypeDecl( *this ); } |
---|
219 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
220 | virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
221 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
222 | |
---|
223 | private: |
---|
224 | Kind kind; |
---|
225 | }; |
---|
226 | |
---|
227 | class TypedefDecl : public NamedTypeDecl { |
---|
228 | typedef NamedTypeDecl Parent; |
---|
229 | public: |
---|
230 | TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); } |
---|
231 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} |
---|
232 | |
---|
233 | virtual std::string typeString() const; |
---|
234 | |
---|
235 | virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); } |
---|
236 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
237 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
238 | private: |
---|
239 | }; |
---|
240 | |
---|
241 | class AggregateDecl : public Declaration { |
---|
242 | typedef Declaration Parent; |
---|
243 | public: |
---|
244 | std::list<Declaration*> members; |
---|
245 | std::list<TypeDecl*> parameters; |
---|
246 | bool body; |
---|
247 | std::list< Attribute * > attributes; |
---|
248 | |
---|
249 | AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ); |
---|
250 | AggregateDecl( const AggregateDecl &other ); |
---|
251 | virtual ~AggregateDecl(); |
---|
252 | |
---|
253 | std::list<Declaration*>& get_members() { return members; } |
---|
254 | std::list<TypeDecl*>& get_parameters() { return parameters; } |
---|
255 | |
---|
256 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
257 | const std::list< Attribute * >& get_attributes() const { return attributes; } |
---|
258 | |
---|
259 | bool has_body() const { return body; } |
---|
260 | AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; } |
---|
261 | |
---|
262 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
263 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
264 | protected: |
---|
265 | virtual std::string typeString() const = 0; |
---|
266 | }; |
---|
267 | |
---|
268 | class StructDecl : public AggregateDecl { |
---|
269 | typedef AggregateDecl Parent; |
---|
270 | public: |
---|
271 | StructDecl( const std::string &name, DeclarationNode::Aggregate kind = DeclarationNode::Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ) {} |
---|
272 | StructDecl( const StructDecl &other ) : Parent( other ), kind( other.kind ) {} |
---|
273 | |
---|
274 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } |
---|
275 | bool is_monitor() { return kind == DeclarationNode::Monitor; } |
---|
276 | bool is_thread() { return kind == DeclarationNode::Thread; } |
---|
277 | |
---|
278 | virtual StructDecl *clone() const { return new StructDecl( *this ); } |
---|
279 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
280 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
281 | private: |
---|
282 | DeclarationNode::Aggregate kind; |
---|
283 | virtual std::string typeString() const; |
---|
284 | }; |
---|
285 | |
---|
286 | class UnionDecl : public AggregateDecl { |
---|
287 | typedef AggregateDecl Parent; |
---|
288 | public: |
---|
289 | UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {} |
---|
290 | UnionDecl( const UnionDecl &other ) : Parent( other ) {} |
---|
291 | |
---|
292 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); } |
---|
293 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
294 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
295 | private: |
---|
296 | virtual std::string typeString() const; |
---|
297 | }; |
---|
298 | |
---|
299 | class EnumDecl : public AggregateDecl { |
---|
300 | typedef AggregateDecl Parent; |
---|
301 | public: |
---|
302 | EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {} |
---|
303 | EnumDecl( const EnumDecl &other ) : Parent( other ) {} |
---|
304 | |
---|
305 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); } |
---|
306 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
307 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
308 | private: |
---|
309 | virtual std::string typeString() const; |
---|
310 | }; |
---|
311 | |
---|
312 | class TraitDecl : public AggregateDecl { |
---|
313 | typedef AggregateDecl Parent; |
---|
314 | public: |
---|
315 | TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) { |
---|
316 | assertf( attributes.empty(), "attribute unsupported for traits" ); |
---|
317 | } |
---|
318 | TraitDecl( const TraitDecl &other ) : Parent( other ) {} |
---|
319 | |
---|
320 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); } |
---|
321 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
322 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
323 | private: |
---|
324 | virtual std::string typeString() const; |
---|
325 | }; |
---|
326 | |
---|
327 | class AsmDecl : public Declaration { |
---|
328 | public: |
---|
329 | AsmStmt *stmt; |
---|
330 | |
---|
331 | AsmDecl( AsmStmt *stmt ); |
---|
332 | AsmDecl( const AsmDecl &other ); |
---|
333 | virtual ~AsmDecl(); |
---|
334 | |
---|
335 | AsmStmt *get_stmt() { return stmt; } |
---|
336 | void set_stmt( AsmStmt *newValue ) { stmt = newValue; } |
---|
337 | |
---|
338 | virtual AsmDecl *clone() const { return new AsmDecl( *this ); } |
---|
339 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
340 | virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
341 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
342 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
343 | }; |
---|
344 | |
---|
345 | std::ostream & operator<<( std::ostream & out, const Declaration * decl ); |
---|
346 | std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data ); |
---|
347 | |
---|
348 | // Local Variables: // |
---|
349 | // tab-width: 4 // |
---|
350 | // mode: c++ // |
---|
351 | // compile-command: "make install" // |
---|
352 | // End: // |
---|