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 | #include "Common/utility.h" |
---|
32 | #include "Common/SemanticError.h" // error_str |
---|
33 | |
---|
34 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
35 | #define MUTATE_FRIEND \ |
---|
36 | template<typename node_t> friend node_t * mutate(const node_t * node); \ |
---|
37 | template<typename node_t> friend node_t * shallowCopy(const node_t * node); |
---|
38 | |
---|
39 | namespace ast { |
---|
40 | |
---|
41 | /// Base declaration class |
---|
42 | class Decl : public ParseNode { |
---|
43 | public: |
---|
44 | std::string name; |
---|
45 | Storage::Classes storage; |
---|
46 | Linkage::Spec linkage; |
---|
47 | UniqueId uniqueId = 0; |
---|
48 | bool extension = false; |
---|
49 | |
---|
50 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
51 | Linkage::Spec linkage ) |
---|
52 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
53 | |
---|
54 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
55 | |
---|
56 | /// Ensures this node has a unique ID |
---|
57 | void fixUniqueId(); |
---|
58 | /// Get canonical declaration for unique ID |
---|
59 | static readonly<Decl> fromId( UniqueId id ); |
---|
60 | |
---|
61 | const Decl * accept( Visitor & v ) const override = 0; |
---|
62 | private: |
---|
63 | Decl * clone() const override = 0; |
---|
64 | MUTATE_FRIEND |
---|
65 | }; |
---|
66 | |
---|
67 | /// Typed declaration base class |
---|
68 | class DeclWithType : public Decl { |
---|
69 | public: |
---|
70 | /// Represents the type with all types and typedefs expanded. |
---|
71 | std::string mangleName; |
---|
72 | /// Stores the scope level at which the variable was declared. |
---|
73 | /// Used to access shadowed identifiers. |
---|
74 | int scopeLevel = 0; |
---|
75 | |
---|
76 | std::vector<ptr<Attribute>> attributes; |
---|
77 | Function::Specs funcSpec; |
---|
78 | ptr<Expr> asmName; |
---|
79 | bool isDeleted = false; |
---|
80 | bool isTypeFixed = false; |
---|
81 | |
---|
82 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
83 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
84 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
85 | funcSpec(fs), asmName() {} |
---|
86 | |
---|
87 | std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
88 | |
---|
89 | /// Get type of this declaration. May be generated by subclass |
---|
90 | virtual const Type * get_type() const = 0; |
---|
91 | /// Set type of this declaration. May be verified by subclass |
---|
92 | virtual void set_type( const Type * ) = 0; |
---|
93 | |
---|
94 | const DeclWithType * accept( Visitor & v ) const override = 0; |
---|
95 | private: |
---|
96 | DeclWithType * clone() const override = 0; |
---|
97 | MUTATE_FRIEND |
---|
98 | }; |
---|
99 | |
---|
100 | /// Object declaration `Foo foo = 42;` |
---|
101 | class ObjectDecl final : public DeclWithType { |
---|
102 | public: |
---|
103 | ptr<Type> type; |
---|
104 | ptr<Init> init; |
---|
105 | ptr<Expr> bitfieldWidth; |
---|
106 | |
---|
107 | ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, |
---|
108 | const Init * init = nullptr, Storage::Classes storage = {}, |
---|
109 | Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr, |
---|
110 | std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) |
---|
111 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
112 | init( init ), bitfieldWidth( bitWd ) {} |
---|
113 | |
---|
114 | const Type* get_type() const override { return type; } |
---|
115 | void set_type( const Type * ty ) override { type = ty; } |
---|
116 | |
---|
117 | const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
118 | private: |
---|
119 | ObjectDecl * clone() const override { return new ObjectDecl{ *this }; } |
---|
120 | MUTATE_FRIEND |
---|
121 | }; |
---|
122 | |
---|
123 | /// Function variable arguments flag |
---|
124 | enum ArgumentFlag { FixedArgs, VariableArgs }; |
---|
125 | |
---|
126 | /// Object declaration `int foo()` |
---|
127 | class FunctionDecl : public DeclWithType { |
---|
128 | public: |
---|
129 | std::vector<ptr<TypeDecl>> type_params; |
---|
130 | std::vector<ptr<DeclWithType>> assertions; |
---|
131 | std::vector<ptr<DeclWithType>> params; |
---|
132 | std::vector<ptr<DeclWithType>> returns; |
---|
133 | // declared type, derived from parameter declarations |
---|
134 | ptr<FunctionType> type; |
---|
135 | /// Null for the forward declaration of a function. |
---|
136 | ptr<CompoundStmt> stmts; |
---|
137 | std::vector< ptr<Expr> > withExprs; |
---|
138 | |
---|
139 | // The difference between the two constructors is in how they handle |
---|
140 | // assertions. The first constructor uses the assertions from the type |
---|
141 | // parameters, in the style of the old ast, and puts them on the type. |
---|
142 | // The second takes an explicite list of assertions and builds a list of |
---|
143 | // references to them on the type. |
---|
144 | |
---|
145 | FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall, |
---|
146 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, |
---|
147 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
148 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs ); |
---|
149 | |
---|
150 | FunctionDecl( const CodeLocation & location, const std::string & name, |
---|
151 | std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions, |
---|
152 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, |
---|
153 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
154 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs ); |
---|
155 | |
---|
156 | const Type * get_type() const override; |
---|
157 | void set_type( const Type * t ) override; |
---|
158 | |
---|
159 | bool has_body() const { return stmts; } |
---|
160 | |
---|
161 | const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
162 | private: |
---|
163 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); } |
---|
164 | MUTATE_FRIEND |
---|
165 | }; |
---|
166 | |
---|
167 | /// Base class for named type aliases |
---|
168 | class NamedTypeDecl : public Decl { |
---|
169 | public: |
---|
170 | ptr<Type> base; |
---|
171 | std::vector<ptr<DeclWithType>> assertions; |
---|
172 | |
---|
173 | NamedTypeDecl( |
---|
174 | const CodeLocation & loc, const std::string & name, Storage::Classes storage, |
---|
175 | const Type * b, Linkage::Spec spec = Linkage::Cforall ) |
---|
176 | : Decl( loc, name, storage, spec ), base( b ), assertions() {} |
---|
177 | |
---|
178 | /// Produces a name for the kind of alias |
---|
179 | virtual const char * typeString() const = 0; |
---|
180 | |
---|
181 | private: |
---|
182 | NamedTypeDecl* clone() const override = 0; |
---|
183 | MUTATE_FRIEND |
---|
184 | }; |
---|
185 | |
---|
186 | /// Cforall type variable: `dtype T` |
---|
187 | class TypeDecl final : public NamedTypeDecl { |
---|
188 | public: |
---|
189 | enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS }; |
---|
190 | |
---|
191 | Kind kind; |
---|
192 | bool sized; |
---|
193 | ptr<Type> init; |
---|
194 | |
---|
195 | TypeDecl( |
---|
196 | const CodeLocation & loc, const std::string & name, Storage::Classes storage, |
---|
197 | const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr ) |
---|
198 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ), |
---|
199 | init( i ) {} |
---|
200 | |
---|
201 | const char * typeString() const override; |
---|
202 | /// Produces a name for generated code |
---|
203 | const char * genTypeString() const; |
---|
204 | |
---|
205 | /// convenience accessor to match Type::isComplete() |
---|
206 | bool isComplete() const { return sized; } |
---|
207 | |
---|
208 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
209 | private: |
---|
210 | TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
211 | MUTATE_FRIEND |
---|
212 | }; |
---|
213 | |
---|
214 | /// Data extracted from a TypeDecl. |
---|
215 | struct TypeData { |
---|
216 | TypeDecl::Kind kind; |
---|
217 | bool isComplete; |
---|
218 | |
---|
219 | TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {} |
---|
220 | TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
221 | TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
222 | TypeData( const TypeData & d1, const TypeData & d2 ) |
---|
223 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
224 | |
---|
225 | bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; } |
---|
226 | bool operator!=( const TypeData & o ) const { return !(*this == o); } |
---|
227 | }; |
---|
228 | |
---|
229 | std::ostream & operator<< ( std::ostream &, const TypeData & ); |
---|
230 | |
---|
231 | /// C-style typedef `typedef Foo Bar` |
---|
232 | class TypedefDecl final : public NamedTypeDecl { |
---|
233 | public: |
---|
234 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
235 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
236 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
237 | |
---|
238 | const char * typeString() const override { return "typedef"; } |
---|
239 | |
---|
240 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
241 | private: |
---|
242 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
243 | MUTATE_FRIEND |
---|
244 | }; |
---|
245 | |
---|
246 | /// Aggregate type declaration base class |
---|
247 | class AggregateDecl : public Decl { |
---|
248 | public: |
---|
249 | enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate }; |
---|
250 | static const char * aggrString( Aggregate aggr ); |
---|
251 | |
---|
252 | std::vector<ptr<Decl>> members; |
---|
253 | std::vector<ptr<TypeDecl>> params; |
---|
254 | std::vector<ptr<Attribute>> attributes; |
---|
255 | bool body = false; |
---|
256 | readonly<AggregateDecl> parent = {}; |
---|
257 | |
---|
258 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
259 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
260 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(), |
---|
261 | attributes( std::move(attrs) ) {} |
---|
262 | |
---|
263 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
264 | |
---|
265 | /// Produces a name for the kind of aggregate |
---|
266 | virtual const char * typeString() const = 0; |
---|
267 | |
---|
268 | private: |
---|
269 | AggregateDecl * clone() const override = 0; |
---|
270 | MUTATE_FRIEND |
---|
271 | }; |
---|
272 | |
---|
273 | /// struct declaration `struct Foo { ... };` |
---|
274 | class StructDecl final : public AggregateDecl { |
---|
275 | public: |
---|
276 | Aggregate kind; |
---|
277 | |
---|
278 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
279 | Aggregate kind = Struct, |
---|
280 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
281 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
282 | |
---|
283 | bool is_coroutine() const { return kind == Coroutine; } |
---|
284 | bool is_generator() const { return kind == Generator; } |
---|
285 | bool is_monitor () const { return kind == Monitor ; } |
---|
286 | bool is_thread () const { return kind == Thread ; } |
---|
287 | |
---|
288 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
289 | |
---|
290 | const char * typeString() const override { return aggrString( kind ); } |
---|
291 | |
---|
292 | private: |
---|
293 | StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
294 | MUTATE_FRIEND |
---|
295 | }; |
---|
296 | |
---|
297 | /// union declaration `union Foo { ... };` |
---|
298 | class UnionDecl final : public AggregateDecl { |
---|
299 | public: |
---|
300 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
301 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
302 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
303 | |
---|
304 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
305 | |
---|
306 | const char * typeString() const override { return aggrString( Union ); } |
---|
307 | |
---|
308 | private: |
---|
309 | UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
310 | MUTATE_FRIEND |
---|
311 | }; |
---|
312 | |
---|
313 | /// enum declaration `enum Foo { ... };` |
---|
314 | class EnumDecl final : public AggregateDecl { |
---|
315 | public: |
---|
316 | bool isTyped; // isTyped indicated if the enum has a declaration like: |
---|
317 | // enum (type_optional) Name {...} |
---|
318 | ptr<Type> base; // if isTyped == true && base.get() == nullptr, it is a "void" type enum |
---|
319 | enum class EnumHiding { Visible, Hide } hide; |
---|
320 | |
---|
321 | EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false, |
---|
322 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, |
---|
323 | Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide, |
---|
324 | std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() ) |
---|
325 | : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {} |
---|
326 | |
---|
327 | /// gets the integer value for this enumerator, returning true iff value found |
---|
328 | // Maybe it is not used in producing the enum value |
---|
329 | bool valueOf( const Decl * enumerator, long long& value ) const; |
---|
330 | |
---|
331 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
332 | |
---|
333 | const char * typeString() const override { return aggrString( Enum ); } |
---|
334 | |
---|
335 | |
---|
336 | private: |
---|
337 | EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
338 | MUTATE_FRIEND |
---|
339 | |
---|
340 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
341 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
342 | }; |
---|
343 | |
---|
344 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
345 | class TraitDecl final : public AggregateDecl { |
---|
346 | public: |
---|
347 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
348 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
349 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
350 | |
---|
351 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
352 | |
---|
353 | const char * typeString() const override { return "trait"; } |
---|
354 | |
---|
355 | private: |
---|
356 | TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
357 | MUTATE_FRIEND |
---|
358 | }; |
---|
359 | |
---|
360 | /// With statement `with (...) ...` |
---|
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 : public Decl { |
---|
377 | public: |
---|
378 | ptr<AsmStmt> stmt; |
---|
379 | |
---|
380 | AsmDecl( const CodeLocation & loc, AsmStmt * stmt ) |
---|
381 | : Decl( loc, "", {}, {} ), 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 : public Decl { |
---|
391 | public: |
---|
392 | ptr<DirectiveStmt> stmt; |
---|
393 | |
---|
394 | DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt ) |
---|
395 | : Decl( loc, "", {}, {} ), 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 : 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, "", {}, {} ), 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: // |
---|