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