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