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