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 | |
---|
48 | const std::string &get_name() const { return name; } |
---|
49 | void set_name( std::string newValue ) { name = newValue; } |
---|
50 | |
---|
51 | Type::StorageClasses get_storageClasses() const { return storageClasses; } |
---|
52 | |
---|
53 | LinkageSpec::Spec get_linkage() const { return linkage; } |
---|
54 | void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; } |
---|
55 | |
---|
56 | UniqueId get_uniqueId() const { return uniqueId; } |
---|
57 | |
---|
58 | bool get_extension() const { return extension; } |
---|
59 | Declaration *set_extension( bool exten ) { extension = exten; return this; } |
---|
60 | |
---|
61 | void fixUniqueId( void ); |
---|
62 | virtual Declaration *clone() const override = 0; |
---|
63 | virtual void accept( Visitor &v ) override = 0; |
---|
64 | virtual Declaration *acceptMutator( Mutator &m ) override = 0; |
---|
65 | virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0; |
---|
66 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const = 0; |
---|
67 | |
---|
68 | static void dumpIds( std::ostream &os ); |
---|
69 | static Declaration *declFromId( UniqueId id ); |
---|
70 | |
---|
71 | private: |
---|
72 | Type::StorageClasses storageClasses; |
---|
73 | UniqueId uniqueId; |
---|
74 | }; |
---|
75 | |
---|
76 | class DeclarationWithType : public Declaration { |
---|
77 | public: |
---|
78 | // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 |
---|
79 | std::string mangleName; |
---|
80 | // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed |
---|
81 | int scopeLevel = 0; |
---|
82 | |
---|
83 | Expression *asmName; |
---|
84 | std::list< Attribute * > attributes; |
---|
85 | |
---|
86 | DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs ); |
---|
87 | DeclarationWithType( const DeclarationWithType &other ); |
---|
88 | |
---|
89 | std::string get_mangleName() const { return mangleName; } |
---|
90 | DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; } |
---|
91 | |
---|
92 | std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
93 | |
---|
94 | int get_scopeLevel() const { return scopeLevel; } |
---|
95 | DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; } |
---|
96 | |
---|
97 | Expression *get_asmName() const { return asmName; } |
---|
98 | DeclarationWithType * set_asmName( Expression *newValue ) { asmName = newValue; return this; } |
---|
99 | |
---|
100 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
101 | const std::list< Attribute * >& get_attributes() const { return attributes; } |
---|
102 | |
---|
103 | Type::FuncSpecifiers get_funcSpec() const { return fs; } |
---|
104 | //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; } |
---|
105 | |
---|
106 | virtual DeclarationWithType *clone() const override = 0; |
---|
107 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override = 0; |
---|
108 | |
---|
109 | virtual Type * get_type() const = 0; |
---|
110 | virtual void set_type(Type *) = 0; |
---|
111 | |
---|
112 | private: |
---|
113 | Type::FuncSpecifiers fs; |
---|
114 | }; |
---|
115 | |
---|
116 | class ObjectDecl : public DeclarationWithType { |
---|
117 | typedef DeclarationWithType Parent; |
---|
118 | public: |
---|
119 | Type *type; |
---|
120 | Initializer *init; |
---|
121 | Expression *bitfieldWidth; |
---|
122 | |
---|
123 | ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init, |
---|
124 | const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() ); |
---|
125 | ObjectDecl( const ObjectDecl &other ); |
---|
126 | |
---|
127 | virtual Type * get_type() const override { return type; } |
---|
128 | virtual void set_type(Type *newType) override { type = newType; } |
---|
129 | |
---|
130 | Initializer *get_init() const { return init; } |
---|
131 | void set_init( Initializer *newValue ) { init = newValue; } |
---|
132 | |
---|
133 | Expression *get_bitfieldWidth() const { return bitfieldWidth; } |
---|
134 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } |
---|
135 | |
---|
136 | static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init ); |
---|
137 | |
---|
138 | virtual ObjectDecl *clone() const override { return new ObjectDecl( *this ); } |
---|
139 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
140 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
141 | virtual void print( std::ostream &os, Indenter indent = {} ) const override; |
---|
142 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; |
---|
143 | }; |
---|
144 | |
---|
145 | class FunctionDecl : public DeclarationWithType { |
---|
146 | typedef DeclarationWithType Parent; |
---|
147 | public: |
---|
148 | FunctionType *type; |
---|
149 | CompoundStmt *statements; |
---|
150 | std::list< Expression * > withExprs; |
---|
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 | |
---|
156 | virtual Type * get_type() const override { return type; } |
---|
157 | virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); } |
---|
158 | |
---|
159 | FunctionType * get_functionType() const { return type; } |
---|
160 | void set_functionType( FunctionType *newValue ) { type = newValue; } |
---|
161 | CompoundStmt *get_statements() const { return statements; } |
---|
162 | void set_statements( CompoundStmt *newValue ) { statements = newValue; } |
---|
163 | |
---|
164 | static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements ); |
---|
165 | |
---|
166 | virtual FunctionDecl *clone() const override { return new FunctionDecl( *this ); } |
---|
167 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
168 | virtual DeclarationWithType *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
169 | virtual void print( std::ostream &os, Indenter indent = {} ) const override; |
---|
170 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; |
---|
171 | }; |
---|
172 | |
---|
173 | class NamedTypeDecl : public Declaration { |
---|
174 | typedef Declaration Parent; |
---|
175 | public: |
---|
176 | Type *base; |
---|
177 | std::list< TypeDecl* > parameters; |
---|
178 | std::list< DeclarationWithType* > assertions; |
---|
179 | |
---|
180 | NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type ); |
---|
181 | NamedTypeDecl( const NamedTypeDecl &other ); |
---|
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 override = 0; |
---|
191 | virtual void print( std::ostream &os, Indenter indent = {} ) const override; |
---|
192 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; |
---|
193 | }; |
---|
194 | |
---|
195 | class TypeDecl : public NamedTypeDecl { |
---|
196 | typedef NamedTypeDecl Parent; |
---|
197 | public: |
---|
198 | enum Kind { 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, bool sized, Type * init = nullptr ); |
---|
215 | TypeDecl( const TypeDecl &other ); |
---|
216 | |
---|
217 | Kind get_kind() const { return kind; } |
---|
218 | |
---|
219 | Type * get_init() const { return init; } |
---|
220 | TypeDecl * set_init( Type * newValue ) { init = newValue; return this; } |
---|
221 | |
---|
222 | bool isComplete() const { return sized; } |
---|
223 | bool get_sized() const { return sized; } |
---|
224 | TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; } |
---|
225 | |
---|
226 | virtual std::string typeString() const override; |
---|
227 | virtual std::string genTypeString() const; |
---|
228 | |
---|
229 | virtual TypeDecl *clone() const override { return new TypeDecl( *this ); } |
---|
230 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
231 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
232 | virtual void print( std::ostream &os, Indenter indent = {} ) const override; |
---|
233 | |
---|
234 | private: |
---|
235 | Kind kind; |
---|
236 | }; |
---|
237 | |
---|
238 | class TypedefDecl : public NamedTypeDecl { |
---|
239 | typedef NamedTypeDecl Parent; |
---|
240 | public: |
---|
241 | TypedefDecl( const std::string &name, CodeLocation location, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) |
---|
242 | : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; } |
---|
243 | |
---|
244 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} |
---|
245 | |
---|
246 | virtual std::string typeString() const override; |
---|
247 | |
---|
248 | virtual TypedefDecl *clone() const override { return new TypedefDecl( *this ); } |
---|
249 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
250 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
251 | private: |
---|
252 | }; |
---|
253 | |
---|
254 | class AggregateDecl : public Declaration { |
---|
255 | typedef Declaration Parent; |
---|
256 | public: |
---|
257 | std::list<Declaration*> members; |
---|
258 | std::list<TypeDecl*> parameters; |
---|
259 | bool body; |
---|
260 | std::list< Attribute * > attributes; |
---|
261 | |
---|
262 | AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ); |
---|
263 | AggregateDecl( const AggregateDecl &other ); |
---|
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, Indenter indent = {} ) const override; |
---|
275 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; |
---|
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 override { return new StructDecl( *this ); } |
---|
291 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
292 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
293 | private: |
---|
294 | DeclarationNode::Aggregate kind; |
---|
295 | virtual std::string typeString() const override; |
---|
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 override { return new UnionDecl( *this ); } |
---|
305 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
306 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
307 | private: |
---|
308 | virtual std::string typeString() const override; |
---|
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 | bool valueOf( Declaration * enumerator, long long int & value ); |
---|
318 | |
---|
319 | virtual EnumDecl *clone() const override { return new EnumDecl( *this ); } |
---|
320 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
321 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
322 | private: |
---|
323 | std::map< std::string, long long int > enumValues; |
---|
324 | virtual std::string typeString() const override; |
---|
325 | }; |
---|
326 | |
---|
327 | class TraitDecl : public AggregateDecl { |
---|
328 | typedef AggregateDecl Parent; |
---|
329 | public: |
---|
330 | TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) { |
---|
331 | assertf( attributes.empty(), "attribute unsupported for traits" ); |
---|
332 | } |
---|
333 | TraitDecl( const TraitDecl &other ) : Parent( other ) {} |
---|
334 | |
---|
335 | virtual TraitDecl *clone() const override { return new TraitDecl( *this ); } |
---|
336 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
337 | virtual Declaration *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
338 | private: |
---|
339 | virtual std::string typeString() const override; |
---|
340 | }; |
---|
341 | |
---|
342 | class AsmDecl : public Declaration { |
---|
343 | public: |
---|
344 | AsmStmt *stmt; |
---|
345 | |
---|
346 | AsmDecl( AsmStmt *stmt ); |
---|
347 | AsmDecl( const AsmDecl &other ); |
---|
348 | |
---|
349 | AsmStmt *get_stmt() { return stmt; } |
---|
350 | void set_stmt( AsmStmt *newValue ) { stmt = newValue; } |
---|
351 | |
---|
352 | virtual AsmDecl *clone() const override { return new AsmDecl( *this ); } |
---|
353 | virtual void accept( Visitor &v ) override { v.visit( this ); } |
---|
354 | virtual AsmDecl *acceptMutator( Mutator &m ) override { return m.mutate( this ); } |
---|
355 | virtual void print( std::ostream &os, Indenter indent = {} ) const override; |
---|
356 | virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; |
---|
357 | }; |
---|
358 | |
---|
359 | std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data ); |
---|
360 | |
---|
361 | // Local Variables: // |
---|
362 | // tab-width: 4 // |
---|
363 | // mode: c++ // |
---|
364 | // compile-command: "make install" // |
---|
365 | // End: // |
---|