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 : Mon Aug 14 10:15:00 2017
|
---|
13 | // Update Count : 128
|
---|
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 = 0;
|
---|
64 | virtual void accept( Visitor &v ) = 0;
|
---|
65 | virtual Declaration *acceptMutator( Mutator &m ) = 0;
|
---|
66 | virtual void print( std::ostream &os, int indent = 0 ) const = 0;
|
---|
67 | virtual void printShort( std::ostream &os, int indent = 0 ) 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 | ConstantExpr *asmName;
|
---|
85 | std::list< Attribute * > attributes;
|
---|
86 |
|
---|
87 | DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
|
---|
88 | DeclarationWithType( const DeclarationWithType &other );
|
---|
89 | virtual ~DeclarationWithType();
|
---|
90 |
|
---|
91 | std::string get_mangleName() const { return mangleName; }
|
---|
92 | DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
|
---|
93 |
|
---|
94 | std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
|
---|
95 |
|
---|
96 | int get_scopeLevel() const { return scopeLevel; }
|
---|
97 | DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
|
---|
98 |
|
---|
99 | ConstantExpr *get_asmName() const { return asmName; }
|
---|
100 | DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
|
---|
101 |
|
---|
102 | std::list< Attribute * >& get_attributes() { return attributes; }
|
---|
103 | const std::list< Attribute * >& get_attributes() const { return attributes; }
|
---|
104 |
|
---|
105 | Type::FuncSpecifiers get_funcSpec() const { return fs; }
|
---|
106 | //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
|
---|
107 |
|
---|
108 | virtual DeclarationWithType *clone() const = 0;
|
---|
109 | virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
|
---|
110 |
|
---|
111 | virtual Type * get_type() const = 0;
|
---|
112 | virtual void set_type(Type *) = 0;
|
---|
113 |
|
---|
114 | private:
|
---|
115 | Type::FuncSpecifiers fs;
|
---|
116 | };
|
---|
117 |
|
---|
118 | class ObjectDecl : public DeclarationWithType {
|
---|
119 | typedef DeclarationWithType Parent;
|
---|
120 | public:
|
---|
121 | Type *type;
|
---|
122 | Initializer *init;
|
---|
123 | Expression *bitfieldWidth;
|
---|
124 |
|
---|
125 | ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
|
---|
126 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
|
---|
127 | ObjectDecl( const ObjectDecl &other );
|
---|
128 | virtual ~ObjectDecl();
|
---|
129 |
|
---|
130 | virtual Type * get_type() const { return type; }
|
---|
131 | virtual void set_type(Type *newType) { type = newType; }
|
---|
132 |
|
---|
133 | Initializer *get_init() const { return init; }
|
---|
134 | void set_init( Initializer *newValue ) { init = newValue; }
|
---|
135 |
|
---|
136 | Expression *get_bitfieldWidth() const { return bitfieldWidth; }
|
---|
137 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
|
---|
138 |
|
---|
139 | virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
|
---|
140 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
141 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
142 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
143 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
144 | };
|
---|
145 |
|
---|
146 | class FunctionDecl : public DeclarationWithType {
|
---|
147 | typedef DeclarationWithType Parent;
|
---|
148 | public:
|
---|
149 | FunctionType *type;
|
---|
150 | CompoundStmt *statements;
|
---|
151 |
|
---|
152 | FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
|
---|
153 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
|
---|
154 | FunctionDecl( const FunctionDecl &other );
|
---|
155 | virtual ~FunctionDecl();
|
---|
156 |
|
---|
157 | Type * get_type() const { return type; }
|
---|
158 | virtual void set_type(Type * t) { type = safe_dynamic_cast< FunctionType* >( t ); }
|
---|
159 |
|
---|
160 | FunctionType * get_functionType() const { return type; }
|
---|
161 | void set_functionType( FunctionType *newValue ) { type = newValue; }
|
---|
162 | CompoundStmt *get_statements() const { return statements; }
|
---|
163 | void set_statements( CompoundStmt *newValue ) { statements = newValue; }
|
---|
164 |
|
---|
165 | virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
|
---|
166 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
167 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
168 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
169 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
170 | };
|
---|
171 |
|
---|
172 | class NamedTypeDecl : public Declaration {
|
---|
173 | typedef Declaration Parent;
|
---|
174 | public:
|
---|
175 | Type *base;
|
---|
176 | std::list< TypeDecl* > parameters;
|
---|
177 | std::list< DeclarationWithType* > assertions;
|
---|
178 |
|
---|
179 | NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
|
---|
180 | NamedTypeDecl( const NamedTypeDecl &other );
|
---|
181 | virtual ~NamedTypeDecl();
|
---|
182 |
|
---|
183 | Type *get_base() const { return base; }
|
---|
184 | void set_base( Type *newValue ) { base = newValue; }
|
---|
185 | std::list< TypeDecl* >& get_parameters() { return parameters; }
|
---|
186 | std::list< DeclarationWithType* >& get_assertions() { return assertions; }
|
---|
187 |
|
---|
188 | virtual std::string typeString() const = 0;
|
---|
189 |
|
---|
190 | virtual NamedTypeDecl *clone() const = 0;
|
---|
191 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
192 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
193 | };
|
---|
194 |
|
---|
195 | class TypeDecl : public NamedTypeDecl {
|
---|
196 | typedef NamedTypeDecl Parent;
|
---|
197 | public:
|
---|
198 | enum Kind { Any, Dtype, Ftype, Ttype };
|
---|
199 |
|
---|
200 | Type * init;
|
---|
201 | bool sized;
|
---|
202 |
|
---|
203 | /// Data extracted from a type decl
|
---|
204 | struct Data {
|
---|
205 | TypeDecl::Kind kind;
|
---|
206 | bool isComplete;
|
---|
207 | Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
|
---|
208 | Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
|
---|
209 | Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
|
---|
210 | bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
|
---|
211 | bool operator!=(const Data & other) const { return !(*this == other);}
|
---|
212 | };
|
---|
213 |
|
---|
214 | TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr );
|
---|
215 | TypeDecl( const TypeDecl &other );
|
---|
216 | virtual ~TypeDecl();
|
---|
217 |
|
---|
218 | Kind get_kind() const { return kind; }
|
---|
219 |
|
---|
220 | Type * get_init() const { return init; }
|
---|
221 | TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
|
---|
222 |
|
---|
223 | bool isComplete() const { return kind == Any || sized; }
|
---|
224 | bool get_sized() const { return sized; }
|
---|
225 | TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
|
---|
226 |
|
---|
227 | virtual std::string typeString() const;
|
---|
228 | virtual std::string genTypeString() const;
|
---|
229 |
|
---|
230 | virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
|
---|
231 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
232 | virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
233 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
234 |
|
---|
235 | private:
|
---|
236 | Kind kind;
|
---|
237 | };
|
---|
238 |
|
---|
239 | class TypedefDecl : public NamedTypeDecl {
|
---|
240 | typedef NamedTypeDecl Parent;
|
---|
241 | public:
|
---|
242 | TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
|
---|
243 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
|
---|
244 |
|
---|
245 | virtual std::string typeString() const;
|
---|
246 |
|
---|
247 | virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
|
---|
248 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
249 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
250 | private:
|
---|
251 | };
|
---|
252 |
|
---|
253 | class AggregateDecl : public Declaration {
|
---|
254 | typedef Declaration Parent;
|
---|
255 | public:
|
---|
256 | std::list<Declaration*> members;
|
---|
257 | std::list<TypeDecl*> parameters;
|
---|
258 | bool body;
|
---|
259 | std::list< Attribute * > attributes;
|
---|
260 |
|
---|
261 | AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
|
---|
262 | AggregateDecl( const AggregateDecl &other );
|
---|
263 | virtual ~AggregateDecl();
|
---|
264 |
|
---|
265 | std::list<Declaration*>& get_members() { return members; }
|
---|
266 | std::list<TypeDecl*>& get_parameters() { return parameters; }
|
---|
267 |
|
---|
268 | std::list< Attribute * >& get_attributes() { return attributes; }
|
---|
269 | const std::list< Attribute * >& get_attributes() const { return attributes; }
|
---|
270 |
|
---|
271 | bool has_body() const { return body; }
|
---|
272 | AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
|
---|
273 |
|
---|
274 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
275 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
276 | protected:
|
---|
277 | virtual std::string typeString() const = 0;
|
---|
278 | };
|
---|
279 |
|
---|
280 | class StructDecl : public AggregateDecl {
|
---|
281 | typedef AggregateDecl Parent;
|
---|
282 | public:
|
---|
283 | 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 ) {}
|
---|
284 | StructDecl( const StructDecl &other ) : Parent( other ), kind( other.kind ) {}
|
---|
285 |
|
---|
286 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
|
---|
287 | bool is_monitor() { return kind == DeclarationNode::Monitor; }
|
---|
288 | bool is_thread() { return kind == DeclarationNode::Thread; }
|
---|
289 |
|
---|
290 | virtual StructDecl *clone() const { return new StructDecl( *this ); }
|
---|
291 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
292 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
293 | private:
|
---|
294 | DeclarationNode::Aggregate kind;
|
---|
295 | virtual std::string typeString() const;
|
---|
296 | };
|
---|
297 |
|
---|
298 | class UnionDecl : public AggregateDecl {
|
---|
299 | typedef AggregateDecl Parent;
|
---|
300 | public:
|
---|
301 | UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
|
---|
302 | UnionDecl( const UnionDecl &other ) : Parent( other ) {}
|
---|
303 |
|
---|
304 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
|
---|
305 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
306 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
307 | private:
|
---|
308 | virtual std::string typeString() const;
|
---|
309 | };
|
---|
310 |
|
---|
311 | class EnumDecl : public AggregateDecl {
|
---|
312 | typedef AggregateDecl Parent;
|
---|
313 | public:
|
---|
314 | EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
|
---|
315 | EnumDecl( const EnumDecl &other ) : Parent( other ) {}
|
---|
316 |
|
---|
317 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
|
---|
318 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
319 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
320 | private:
|
---|
321 | virtual std::string typeString() const;
|
---|
322 | };
|
---|
323 |
|
---|
324 | class TraitDecl : public AggregateDecl {
|
---|
325 | typedef AggregateDecl Parent;
|
---|
326 | public:
|
---|
327 | TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
|
---|
328 | assertf( attributes.empty(), "attribute unsupported for traits" );
|
---|
329 | }
|
---|
330 | TraitDecl( const TraitDecl &other ) : Parent( other ) {}
|
---|
331 |
|
---|
332 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
|
---|
333 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
334 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
335 | private:
|
---|
336 | virtual std::string typeString() const;
|
---|
337 | };
|
---|
338 |
|
---|
339 | class AsmDecl : public Declaration {
|
---|
340 | public:
|
---|
341 | AsmStmt *stmt;
|
---|
342 |
|
---|
343 | AsmDecl( AsmStmt *stmt );
|
---|
344 | AsmDecl( const AsmDecl &other );
|
---|
345 | virtual ~AsmDecl();
|
---|
346 |
|
---|
347 | AsmStmt *get_stmt() { return stmt; }
|
---|
348 | void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
|
---|
349 |
|
---|
350 | virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
|
---|
351 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
352 | virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
353 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
354 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
355 | };
|
---|
356 |
|
---|
357 | std::ostream & operator<<( std::ostream & out, const Declaration * decl );
|
---|
358 | std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
|
---|
359 |
|
---|
360 | // Local Variables: //
|
---|
361 | // tab-width: 4 //
|
---|
362 | // mode: c++ //
|
---|
363 | // compile-command: "make install" //
|
---|
364 | // End: //
|
---|