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