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 |
|
---|
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 | Expression *get_asmName() const { return asmName; }
|
---|
100 | DeclarationWithType * set_asmName( Expression *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 override = 0;
|
---|
109 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override = 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 override { return type; }
|
---|
131 | virtual void set_type(Type *newType) override { 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 | static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
|
---|
140 |
|
---|
141 | virtual ObjectDecl *clone() const override { return new ObjectDecl( *this ); }
|
---|
142 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
143 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
144 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
145 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
|
---|
146 | };
|
---|
147 |
|
---|
148 | class FunctionDecl : public DeclarationWithType {
|
---|
149 | typedef DeclarationWithType Parent;
|
---|
150 | public:
|
---|
151 | FunctionType *type;
|
---|
152 | CompoundStmt *statements;
|
---|
153 | std::list< Expression * > withExprs;
|
---|
154 |
|
---|
155 | FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
|
---|
156 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
|
---|
157 | FunctionDecl( const FunctionDecl &other );
|
---|
158 | virtual ~FunctionDecl();
|
---|
159 |
|
---|
160 | virtual Type * get_type() const override { return type; }
|
---|
161 | virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
|
---|
162 |
|
---|
163 | FunctionType * get_functionType() const { return type; }
|
---|
164 | void set_functionType( FunctionType *newValue ) { type = newValue; }
|
---|
165 | CompoundStmt *get_statements() const { return statements; }
|
---|
166 | void set_statements( CompoundStmt *newValue ) { statements = newValue; }
|
---|
167 |
|
---|
168 | static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
|
---|
169 |
|
---|
170 | virtual FunctionDecl *clone() const override { return new FunctionDecl( *this ); }
|
---|
171 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
172 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
173 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
174 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
|
---|
175 | };
|
---|
176 |
|
---|
177 | class NamedTypeDecl : public Declaration {
|
---|
178 | typedef Declaration Parent;
|
---|
179 | public:
|
---|
180 | Type *base;
|
---|
181 | std::list< TypeDecl* > parameters;
|
---|
182 | std::list< DeclarationWithType* > assertions;
|
---|
183 |
|
---|
184 | NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
|
---|
185 | NamedTypeDecl( const NamedTypeDecl &other );
|
---|
186 | virtual ~NamedTypeDecl();
|
---|
187 |
|
---|
188 | Type *get_base() const { return base; }
|
---|
189 | void set_base( Type *newValue ) { base = newValue; }
|
---|
190 | std::list< TypeDecl* >& get_parameters() { return parameters; }
|
---|
191 | std::list< DeclarationWithType* >& get_assertions() { return assertions; }
|
---|
192 |
|
---|
193 | virtual std::string typeString() const = 0;
|
---|
194 |
|
---|
195 | virtual NamedTypeDecl *clone() const override = 0;
|
---|
196 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
197 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
|
---|
198 | };
|
---|
199 |
|
---|
200 | class TypeDecl : public NamedTypeDecl {
|
---|
201 | typedef NamedTypeDecl Parent;
|
---|
202 | public:
|
---|
203 | enum Kind { Dtype, Ftype, Ttype };
|
---|
204 |
|
---|
205 | Type * init;
|
---|
206 | bool sized;
|
---|
207 |
|
---|
208 | /// Data extracted from a type decl
|
---|
209 | struct Data {
|
---|
210 | TypeDecl::Kind kind;
|
---|
211 | bool isComplete;
|
---|
212 | Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
|
---|
213 | Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
|
---|
214 | Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
|
---|
215 | bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
|
---|
216 | bool operator!=(const Data & other) const { return !(*this == other);}
|
---|
217 | };
|
---|
218 |
|
---|
219 | TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, bool sized, Type * init = nullptr );
|
---|
220 | TypeDecl( const TypeDecl &other );
|
---|
221 | virtual ~TypeDecl();
|
---|
222 |
|
---|
223 | Kind get_kind() const { return kind; }
|
---|
224 |
|
---|
225 | Type * get_init() const { return init; }
|
---|
226 | TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
|
---|
227 |
|
---|
228 | bool isComplete() const { return sized; }
|
---|
229 | bool get_sized() const { return sized; }
|
---|
230 | TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
|
---|
231 |
|
---|
232 | virtual std::string typeString() const override;
|
---|
233 | virtual std::string genTypeString() const;
|
---|
234 |
|
---|
235 | virtual TypeDecl *clone() const override { return new TypeDecl( *this ); }
|
---|
236 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
237 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
238 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
239 |
|
---|
240 | private:
|
---|
241 | Kind kind;
|
---|
242 | };
|
---|
243 |
|
---|
244 | class TypedefDecl : public NamedTypeDecl {
|
---|
245 | typedef NamedTypeDecl Parent;
|
---|
246 | public:
|
---|
247 | TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
|
---|
248 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
|
---|
249 |
|
---|
250 | virtual std::string typeString() const override;
|
---|
251 |
|
---|
252 | virtual TypedefDecl *clone() const override { return new TypedefDecl( *this ); }
|
---|
253 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
254 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
255 | private:
|
---|
256 | };
|
---|
257 |
|
---|
258 | class AggregateDecl : public Declaration {
|
---|
259 | typedef Declaration Parent;
|
---|
260 | public:
|
---|
261 | std::list<Declaration*> members;
|
---|
262 | std::list<TypeDecl*> parameters;
|
---|
263 | bool body;
|
---|
264 | std::list< Attribute * > attributes;
|
---|
265 |
|
---|
266 | AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
|
---|
267 | AggregateDecl( const AggregateDecl &other );
|
---|
268 | virtual ~AggregateDecl();
|
---|
269 |
|
---|
270 | std::list<Declaration*>& get_members() { return members; }
|
---|
271 | std::list<TypeDecl*>& get_parameters() { return parameters; }
|
---|
272 |
|
---|
273 | std::list< Attribute * >& get_attributes() { return attributes; }
|
---|
274 | const std::list< Attribute * >& get_attributes() const { return attributes; }
|
---|
275 |
|
---|
276 | bool has_body() const { return body; }
|
---|
277 | AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
|
---|
278 |
|
---|
279 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
280 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
|
---|
281 | protected:
|
---|
282 | virtual std::string typeString() const = 0;
|
---|
283 | };
|
---|
284 |
|
---|
285 | class StructDecl : public AggregateDecl {
|
---|
286 | typedef AggregateDecl Parent;
|
---|
287 | public:
|
---|
288 | 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 ) {}
|
---|
289 | StructDecl( const StructDecl &other ) : Parent( other ), kind( other.kind ) {}
|
---|
290 |
|
---|
291 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
|
---|
292 | bool is_monitor() { return kind == DeclarationNode::Monitor; }
|
---|
293 | bool is_thread() { return kind == DeclarationNode::Thread; }
|
---|
294 |
|
---|
295 | virtual StructDecl *clone() const override { return new StructDecl( *this ); }
|
---|
296 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
297 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
298 | private:
|
---|
299 | DeclarationNode::Aggregate kind;
|
---|
300 | virtual std::string typeString() const override;
|
---|
301 | };
|
---|
302 |
|
---|
303 | class UnionDecl : public AggregateDecl {
|
---|
304 | typedef AggregateDecl Parent;
|
---|
305 | public:
|
---|
306 | UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
|
---|
307 | UnionDecl( const UnionDecl &other ) : Parent( other ) {}
|
---|
308 |
|
---|
309 | virtual UnionDecl *clone() const override { return new UnionDecl( *this ); }
|
---|
310 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
311 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
312 | private:
|
---|
313 | virtual std::string typeString() const override;
|
---|
314 | };
|
---|
315 |
|
---|
316 | class EnumDecl : public AggregateDecl {
|
---|
317 | typedef AggregateDecl Parent;
|
---|
318 | public:
|
---|
319 | EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
|
---|
320 | EnumDecl( const EnumDecl &other ) : Parent( other ) {}
|
---|
321 |
|
---|
322 | bool valueOf( Declaration * enumerator, long long int & value );
|
---|
323 |
|
---|
324 | virtual EnumDecl *clone() const override { return new EnumDecl( *this ); }
|
---|
325 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
326 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
327 | private:
|
---|
328 | std::map< std::string, long long int > enumValues;
|
---|
329 | virtual std::string typeString() const override;
|
---|
330 | };
|
---|
331 |
|
---|
332 | class TraitDecl : public AggregateDecl {
|
---|
333 | typedef AggregateDecl Parent;
|
---|
334 | public:
|
---|
335 | TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
|
---|
336 | assertf( attributes.empty(), "attribute unsupported for traits" );
|
---|
337 | }
|
---|
338 | TraitDecl( const TraitDecl &other ) : Parent( other ) {}
|
---|
339 |
|
---|
340 | virtual TraitDecl *clone() const override { return new TraitDecl( *this ); }
|
---|
341 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
342 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
343 | private:
|
---|
344 | virtual std::string typeString() const override;
|
---|
345 | };
|
---|
346 |
|
---|
347 | class AsmDecl : public Declaration {
|
---|
348 | public:
|
---|
349 | AsmStmt *stmt;
|
---|
350 |
|
---|
351 | AsmDecl( AsmStmt *stmt );
|
---|
352 | AsmDecl( const AsmDecl &other );
|
---|
353 | virtual ~AsmDecl();
|
---|
354 |
|
---|
355 | AsmStmt *get_stmt() { return stmt; }
|
---|
356 | void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
|
---|
357 |
|
---|
358 | virtual AsmDecl *clone() const override { return new AsmDecl( *this ); }
|
---|
359 | virtual void accept( Visitor &v ) override { v.visit( this ); }
|
---|
360 | virtual AsmDecl *acceptMutator( Mutator &m ) override { return m.mutate( this ); }
|
---|
361 | virtual void print( std::ostream &os, Indenter indent = {} ) const override;
|
---|
362 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
|
---|
363 | };
|
---|
364 |
|
---|
365 | std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
|
---|
366 |
|
---|
367 | // Local Variables: //
|
---|
368 | // tab-width: 4 //
|
---|
369 | // mode: c++ //
|
---|
370 | // compile-command: "make install" //
|
---|
371 | // End: //
|
---|