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 | // Decl.hpp -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Thu May 9 10:00:00 2019 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed Apr 5 10:42:00 2023 |
---|
13 | // Update Count : 35 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <iosfwd> |
---|
19 | #include <string> // for string, to_string |
---|
20 | #include <unordered_map> |
---|
21 | #include <vector> |
---|
22 | #include <algorithm> |
---|
23 | |
---|
24 | #include "FunctionSpec.hpp" |
---|
25 | #include "Fwd.hpp" // for UniqueId |
---|
26 | #include "LinkageSpec.hpp" |
---|
27 | #include "Node.hpp" // for ptr, readonly |
---|
28 | #include "ParseNode.hpp" |
---|
29 | #include "StorageClasses.hpp" |
---|
30 | #include "Visitor.hpp" |
---|
31 | |
---|
32 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
33 | #define MUTATE_FRIEND \ |
---|
34 | template<typename node_t> friend node_t * mutate(const node_t * node); \ |
---|
35 | template<typename node_t> friend node_t * shallowCopy(const node_t * node); |
---|
36 | |
---|
37 | namespace ast { |
---|
38 | |
---|
39 | /// Base declaration class |
---|
40 | class Decl : public ParseNode { |
---|
41 | public: |
---|
42 | std::string name; |
---|
43 | Storage::Classes storage; |
---|
44 | Linkage::Spec linkage; |
---|
45 | UniqueId uniqueId = 0; |
---|
46 | bool extension = false; |
---|
47 | |
---|
48 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
49 | Linkage::Spec linkage ) |
---|
50 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
51 | |
---|
52 | /// Ensures this node has a unique ID |
---|
53 | void fixUniqueId(); |
---|
54 | |
---|
55 | const Decl * accept( Visitor & v ) const override = 0; |
---|
56 | private: |
---|
57 | Decl * clone() const override = 0; |
---|
58 | MUTATE_FRIEND |
---|
59 | }; |
---|
60 | |
---|
61 | /// Typed declaration base class |
---|
62 | class DeclWithType : public Decl { |
---|
63 | public: |
---|
64 | /// Represents the type with all types and typedefs expanded. |
---|
65 | std::string mangleName; |
---|
66 | /// Stores the scope level at which the variable was declared. |
---|
67 | /// Used to access shadowed identifiers. |
---|
68 | int scopeLevel = 0; |
---|
69 | |
---|
70 | std::vector<ptr<Attribute>> attributes; |
---|
71 | Function::Specs funcSpec; |
---|
72 | ptr<Expr> asmName; |
---|
73 | bool isDeleted = false; |
---|
74 | bool isTypeFixed = false; |
---|
75 | bool isHidden = false; |
---|
76 | |
---|
77 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
78 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
79 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
80 | funcSpec(fs), asmName() {} |
---|
81 | |
---|
82 | std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
83 | |
---|
84 | /// Get type of this declaration. May be generated by subclass |
---|
85 | virtual const Type * get_type() const = 0; |
---|
86 | /// Set type of this declaration. May be verified by subclass |
---|
87 | virtual void set_type( const Type * ) = 0; |
---|
88 | |
---|
89 | const DeclWithType * accept( Visitor & v ) const override = 0; |
---|
90 | private: |
---|
91 | DeclWithType * clone() const override = 0; |
---|
92 | MUTATE_FRIEND |
---|
93 | }; |
---|
94 | |
---|
95 | /// Object declaration `Foo foo = 42;` |
---|
96 | class ObjectDecl final : public DeclWithType { |
---|
97 | public: |
---|
98 | ptr<Type> type; |
---|
99 | ptr<Init> init; |
---|
100 | ptr<Expr> bitfieldWidth; |
---|
101 | |
---|
102 | ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, |
---|
103 | const Init * init = nullptr, Storage::Classes storage = {}, |
---|
104 | Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr, |
---|
105 | std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) |
---|
106 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
107 | init( init ), bitfieldWidth( bitWd ) {} |
---|
108 | |
---|
109 | const Type* get_type() const override { return type; } |
---|
110 | void set_type( const Type * ty ) override { type = ty; } |
---|
111 | |
---|
112 | const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
113 | private: |
---|
114 | ObjectDecl * clone() const override { return new ObjectDecl{ *this }; } |
---|
115 | MUTATE_FRIEND |
---|
116 | }; |
---|
117 | |
---|
118 | /// Function variable arguments flag |
---|
119 | enum ArgumentFlag { FixedArgs, VariableArgs }; |
---|
120 | |
---|
121 | /// Object declaration `int foo()` |
---|
122 | class FunctionDecl final : public DeclWithType { |
---|
123 | public: |
---|
124 | std::vector<ptr<TypeDecl>> type_params; |
---|
125 | std::vector<ptr<DeclWithType>> assertions; |
---|
126 | std::vector<ptr<DeclWithType>> params; |
---|
127 | std::vector<ptr<DeclWithType>> returns; |
---|
128 | // declared type, derived from parameter declarations |
---|
129 | ptr<FunctionType> type; |
---|
130 | /// Null for the forward declaration of a function. |
---|
131 | ptr<CompoundStmt> stmts; |
---|
132 | std::vector< ptr<Expr> > withExprs; |
---|
133 | |
---|
134 | /// Monomorphic Function Constructor: |
---|
135 | FunctionDecl( const CodeLocation & locaction, const std::string & name, |
---|
136 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, |
---|
137 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
138 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs ); |
---|
139 | |
---|
140 | /// Polymorphic Function Constructor: |
---|
141 | FunctionDecl( const CodeLocation & location, const std::string & name, |
---|
142 | std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions, |
---|
143 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, |
---|
144 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
145 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs ); |
---|
146 | |
---|
147 | const Type * get_type() const override; |
---|
148 | void set_type( const Type * t ) override; |
---|
149 | |
---|
150 | bool has_body() const { return stmts; } |
---|
151 | |
---|
152 | const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
153 | private: |
---|
154 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); } |
---|
155 | MUTATE_FRIEND |
---|
156 | }; |
---|
157 | |
---|
158 | /// Base class for named type aliases |
---|
159 | class NamedTypeDecl : public Decl { |
---|
160 | public: |
---|
161 | ptr<Type> base; |
---|
162 | std::vector<ptr<DeclWithType>> assertions; |
---|
163 | |
---|
164 | NamedTypeDecl( |
---|
165 | const CodeLocation & loc, const std::string & name, Storage::Classes storage, |
---|
166 | const Type * b, Linkage::Spec spec = Linkage::Cforall ) |
---|
167 | : Decl( loc, name, storage, spec ), base( b ), assertions() {} |
---|
168 | |
---|
169 | /// Produces a name for the kind of alias |
---|
170 | virtual const char * typeString() const = 0; |
---|
171 | |
---|
172 | private: |
---|
173 | NamedTypeDecl* clone() const override = 0; |
---|
174 | MUTATE_FRIEND |
---|
175 | }; |
---|
176 | |
---|
177 | /// Cforall type variable: `dtype T` |
---|
178 | class TypeDecl final : public NamedTypeDecl { |
---|
179 | public: |
---|
180 | enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS }; |
---|
181 | |
---|
182 | Kind kind; |
---|
183 | bool sized; |
---|
184 | ptr<Type> init; |
---|
185 | |
---|
186 | TypeDecl( |
---|
187 | const CodeLocation & loc, const std::string & name, Storage::Classes storage, |
---|
188 | const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr ) |
---|
189 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ), |
---|
190 | init( i ) {} |
---|
191 | |
---|
192 | const char * typeString() const override; |
---|
193 | /// Produces a name for generated code |
---|
194 | const char * genTypeString() const; |
---|
195 | |
---|
196 | /// convenience accessor to match Type::isComplete() |
---|
197 | bool isComplete() const { return sized; } |
---|
198 | |
---|
199 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
200 | private: |
---|
201 | TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
202 | MUTATE_FRIEND |
---|
203 | }; |
---|
204 | |
---|
205 | /// Data extracted from a TypeDecl. |
---|
206 | struct TypeData { |
---|
207 | TypeDecl::Kind kind; |
---|
208 | bool isComplete; |
---|
209 | |
---|
210 | TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {} |
---|
211 | TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
212 | TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
213 | TypeData( const TypeData & d1, const TypeData & d2 ) |
---|
214 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
215 | |
---|
216 | bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; } |
---|
217 | bool operator!=( const TypeData & o ) const { return !(*this == o); } |
---|
218 | }; |
---|
219 | |
---|
220 | std::ostream & operator<< ( std::ostream &, const TypeData & ); |
---|
221 | |
---|
222 | /// C-style typedef `typedef Foo Bar` |
---|
223 | class TypedefDecl final : public NamedTypeDecl { |
---|
224 | public: |
---|
225 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
226 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
227 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
228 | |
---|
229 | const char * typeString() const override { return "typedef"; } |
---|
230 | |
---|
231 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
232 | private: |
---|
233 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
234 | MUTATE_FRIEND |
---|
235 | }; |
---|
236 | |
---|
237 | /// Aggregate type declaration base class |
---|
238 | class AggregateDecl : public Decl { |
---|
239 | public: |
---|
240 | enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate }; |
---|
241 | static const char * aggrString( Aggregate aggr ); |
---|
242 | |
---|
243 | std::vector<ptr<Decl>> members; |
---|
244 | std::vector<ptr<TypeDecl>> params; |
---|
245 | std::vector<ptr<Attribute>> attributes; |
---|
246 | bool body = false; |
---|
247 | readonly<AggregateDecl> parent = {}; |
---|
248 | |
---|
249 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
250 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
251 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(), |
---|
252 | attributes( std::move(attrs) ) {} |
---|
253 | |
---|
254 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
255 | |
---|
256 | /// Produces a name for the kind of aggregate |
---|
257 | virtual const char * typeString() const = 0; |
---|
258 | |
---|
259 | private: |
---|
260 | AggregateDecl * clone() const override = 0; |
---|
261 | MUTATE_FRIEND |
---|
262 | }; |
---|
263 | |
---|
264 | /// struct declaration `struct Foo { ... };` |
---|
265 | class StructDecl final : public AggregateDecl { |
---|
266 | public: |
---|
267 | Aggregate kind; |
---|
268 | |
---|
269 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
270 | Aggregate kind = Struct, |
---|
271 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
272 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
273 | |
---|
274 | bool is_coroutine() const { return kind == Coroutine; } |
---|
275 | bool is_generator() const { return kind == Generator; } |
---|
276 | bool is_monitor () const { return kind == Monitor ; } |
---|
277 | bool is_thread () const { return kind == Thread ; } |
---|
278 | |
---|
279 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
280 | |
---|
281 | const char * typeString() const override { return aggrString( kind ); } |
---|
282 | |
---|
283 | private: |
---|
284 | StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
285 | MUTATE_FRIEND |
---|
286 | }; |
---|
287 | |
---|
288 | /// union declaration `union Foo { ... };` |
---|
289 | class UnionDecl final : public AggregateDecl { |
---|
290 | public: |
---|
291 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
292 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
293 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
294 | |
---|
295 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
296 | |
---|
297 | const char * typeString() const override { return aggrString( Union ); } |
---|
298 | |
---|
299 | private: |
---|
300 | UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
301 | MUTATE_FRIEND |
---|
302 | }; |
---|
303 | |
---|
304 | enum class EnumAttribute{ Value, Posn, Label }; |
---|
305 | /// enum declaration `enum Foo { ... };` |
---|
306 | class EnumDecl final : public AggregateDecl { |
---|
307 | public: |
---|
308 | // isCfa indicated if the enum has a declaration like: |
---|
309 | // enum (type_optional) Name {...} |
---|
310 | bool isCfa; |
---|
311 | // if isCfa == true && base.get() == nullptr, it is a "opaque" enum |
---|
312 | ptr<Type> base; |
---|
313 | enum class EnumHiding { Visible, Hide } hide; |
---|
314 | std::vector< ast::ptr<ast::EnumInstType>> inlinedDecl; // child enums |
---|
315 | |
---|
316 | EnumDecl( const CodeLocation& loc, const std::string& name, bool isCfa = false, |
---|
317 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
318 | Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide, |
---|
319 | std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() ) |
---|
320 | : AggregateDecl( loc, name, std::move(attrs), linkage ), isCfa(isCfa), base(base), hide(hide), enumValues(enumValues) {} |
---|
321 | |
---|
322 | /// gets the integer value for this enumerator, returning true iff value found |
---|
323 | // Maybe it is not used in producing the enum value |
---|
324 | bool valueOf( const Decl * enumerator, long long& value ) const; |
---|
325 | |
---|
326 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
327 | |
---|
328 | const char * typeString() const override { return aggrString( Enum ); } |
---|
329 | |
---|
330 | const std::string getUnmangeldArrayName( const EnumAttribute attr ) const; |
---|
331 | |
---|
332 | unsigned calChildOffset(const std::string & childEnum) const; |
---|
333 | unsigned calChildOffset(const ast::EnumInstType * childEnum) const; |
---|
334 | |
---|
335 | bool isSubTypeOf(const ast::EnumDecl *) const; |
---|
336 | bool isTyped() const; |
---|
337 | bool isOpaque() const; |
---|
338 | private: |
---|
339 | EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
340 | MUTATE_FRIEND |
---|
341 | |
---|
342 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
343 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
344 | }; |
---|
345 | |
---|
346 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
347 | class TraitDecl final : public AggregateDecl { |
---|
348 | public: |
---|
349 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
350 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
351 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
352 | |
---|
353 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
354 | |
---|
355 | const char * typeString() const override { return "trait"; } |
---|
356 | |
---|
357 | private: |
---|
358 | TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
359 | MUTATE_FRIEND |
---|
360 | }; |
---|
361 | |
---|
362 | /// With statement `with (...) ...` |
---|
363 | /// This is a statement lexically, but a Decl is needed for the SymbolTable. |
---|
364 | class WithStmt final : public Decl { |
---|
365 | public: |
---|
366 | std::vector<ptr<Expr>> exprs; |
---|
367 | ptr<Stmt> stmt; |
---|
368 | |
---|
369 | WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt ) |
---|
370 | : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {} |
---|
371 | |
---|
372 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
373 | private: |
---|
374 | WithStmt * clone() const override { return new WithStmt{ *this }; } |
---|
375 | MUTATE_FRIEND |
---|
376 | }; |
---|
377 | |
---|
378 | /// Assembly declaration: `asm ... ( "..." : ... )` |
---|
379 | class AsmDecl final : public Decl { |
---|
380 | public: |
---|
381 | ptr<AsmStmt> stmt; |
---|
382 | |
---|
383 | AsmDecl( const CodeLocation & loc, AsmStmt * stmt ) |
---|
384 | : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {} |
---|
385 | |
---|
386 | const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
387 | private: |
---|
388 | AsmDecl * clone() const override { return new AsmDecl( *this ); } |
---|
389 | MUTATE_FRIEND |
---|
390 | }; |
---|
391 | |
---|
392 | /// C-preprocessor directive `#...` |
---|
393 | class DirectiveDecl final : public Decl { |
---|
394 | public: |
---|
395 | ptr<DirectiveStmt> stmt; |
---|
396 | |
---|
397 | DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt ) |
---|
398 | : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {} |
---|
399 | |
---|
400 | const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
401 | private: |
---|
402 | DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); } |
---|
403 | MUTATE_FRIEND |
---|
404 | }; |
---|
405 | |
---|
406 | /// Static Assertion `_Static_assert( ... , ... );` |
---|
407 | class StaticAssertDecl final : public Decl { |
---|
408 | public: |
---|
409 | ptr<Expr> cond; |
---|
410 | ptr<ConstantExpr> msg; // string literal |
---|
411 | |
---|
412 | StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg ) |
---|
413 | : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {} |
---|
414 | |
---|
415 | const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
416 | private: |
---|
417 | StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); } |
---|
418 | MUTATE_FRIEND |
---|
419 | }; |
---|
420 | |
---|
421 | /// Inline Member Declaration `inline TypeName;` |
---|
422 | class InlineMemberDecl final : public DeclWithType { |
---|
423 | public: |
---|
424 | ptr<Type> type; |
---|
425 | |
---|
426 | InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type, |
---|
427 | Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
428 | std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) |
---|
429 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {} |
---|
430 | |
---|
431 | const Type * get_type() const override { return type; } |
---|
432 | void set_type( const Type * ty ) override { type = ty; } |
---|
433 | |
---|
434 | const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
435 | private: |
---|
436 | InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; } |
---|
437 | MUTATE_FRIEND |
---|
438 | }; |
---|
439 | |
---|
440 | } |
---|
441 | |
---|
442 | #undef MUTATE_FRIEND |
---|
443 | |
---|
444 | // Local Variables: // |
---|
445 | // tab-width: 4 // |
---|
446 | // mode: c++ // |
---|
447 | // compile-command: "make install" // |
---|
448 | // End: // |
---|