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 Dec 13 17:38:33 2019
|
---|
13 | // Update Count : 29
|
---|
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 |
|
---|
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::C, 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 | /// Object declaration `int foo()`
|
---|
124 | class FunctionDecl : public DeclWithType {
|
---|
125 | public:
|
---|
126 | std::vector<ptr<DeclWithType>> params;
|
---|
127 | std::vector<ptr<DeclWithType>> returns;
|
---|
128 | // declared type, derived from parameter declarations
|
---|
129 | ptr<FunctionType> type;
|
---|
130 | ptr<CompoundStmt> stmts;
|
---|
131 | std::vector< ptr<Expr> > withExprs;
|
---|
132 |
|
---|
133 | FunctionDecl( const CodeLocation & loc, const std::string & name,
|
---|
134 | std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
|
---|
135 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
|
---|
136 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
|
---|
137 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
|
---|
138 | stmts( stmts ) {}
|
---|
139 |
|
---|
140 | const Type * get_type() const override;
|
---|
141 | void set_type( const Type * t ) override;
|
---|
142 |
|
---|
143 | bool has_body() const { return stmts; }
|
---|
144 |
|
---|
145 | const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
146 | private:
|
---|
147 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
|
---|
148 | MUTATE_FRIEND
|
---|
149 | };
|
---|
150 |
|
---|
151 | /// Base class for named type aliases
|
---|
152 | class NamedTypeDecl : public Decl {
|
---|
153 | public:
|
---|
154 | ptr<Type> base;
|
---|
155 | std::vector<ptr<TypeDecl>> params;
|
---|
156 | std::vector<ptr<DeclWithType>> assertions;
|
---|
157 |
|
---|
158 | NamedTypeDecl(
|
---|
159 | const CodeLocation & loc, const std::string & name, Storage::Classes storage,
|
---|
160 | const Type * b, Linkage::Spec spec = Linkage::Cforall )
|
---|
161 | : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
|
---|
162 |
|
---|
163 | /// Produces a name for the kind of alias
|
---|
164 | virtual const char * typeString() const = 0;
|
---|
165 |
|
---|
166 | private:
|
---|
167 | NamedTypeDecl* clone() const override = 0;
|
---|
168 | MUTATE_FRIEND
|
---|
169 | };
|
---|
170 |
|
---|
171 | /// Cforall type variable: `dtype T`
|
---|
172 | class TypeDecl final : public NamedTypeDecl {
|
---|
173 | public:
|
---|
174 | enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
|
---|
175 |
|
---|
176 | Kind kind;
|
---|
177 | bool sized;
|
---|
178 | ptr<Type> init;
|
---|
179 |
|
---|
180 | /// Data extracted from a type decl
|
---|
181 | struct Data {
|
---|
182 | Kind kind;
|
---|
183 | bool isComplete;
|
---|
184 |
|
---|
185 | Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
|
---|
186 | Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
|
---|
187 | Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
|
---|
188 | Data( const Data & d1, const Data & d2 )
|
---|
189 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
|
---|
190 |
|
---|
191 | bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
|
---|
192 | bool operator!=( const Data & o ) const { return !(*this == o); }
|
---|
193 | };
|
---|
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() { 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 | std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
|
---|
215 |
|
---|
216 | /// C-style typedef `typedef Foo Bar`
|
---|
217 | class TypedefDecl final : public NamedTypeDecl {
|
---|
218 | public:
|
---|
219 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
|
---|
220 | Type* b, Linkage::Spec spec = Linkage::Cforall )
|
---|
221 | : NamedTypeDecl( loc, name, storage, b, spec ) {}
|
---|
222 |
|
---|
223 | const char * typeString() const override { return "typedef"; }
|
---|
224 |
|
---|
225 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
226 | private:
|
---|
227 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
|
---|
228 | MUTATE_FRIEND
|
---|
229 | };
|
---|
230 |
|
---|
231 | /// Aggregate type declaration base class
|
---|
232 | class AggregateDecl : public Decl {
|
---|
233 | public:
|
---|
234 | enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
|
---|
235 | static const char * aggrString( Aggregate aggr );
|
---|
236 |
|
---|
237 | std::vector<ptr<Decl>> members;
|
---|
238 | std::vector<ptr<TypeDecl>> params;
|
---|
239 | std::vector<ptr<Attribute>> attributes;
|
---|
240 | bool body = false;
|
---|
241 | readonly<AggregateDecl> parent = {};
|
---|
242 |
|
---|
243 | AggregateDecl( const CodeLocation& loc, const std::string& name,
|
---|
244 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
|
---|
245 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
|
---|
246 | attributes( std::move(attrs) ) {}
|
---|
247 |
|
---|
248 | AggregateDecl* set_body( bool b ) { body = b; return this; }
|
---|
249 |
|
---|
250 | /// Produces a name for the kind of aggregate
|
---|
251 | virtual const char * typeString() const = 0;
|
---|
252 |
|
---|
253 | private:
|
---|
254 | AggregateDecl * clone() const override = 0;
|
---|
255 | MUTATE_FRIEND
|
---|
256 | };
|
---|
257 |
|
---|
258 | /// struct declaration `struct Foo { ... };`
|
---|
259 | class StructDecl final : public AggregateDecl {
|
---|
260 | public:
|
---|
261 | Aggregate kind;
|
---|
262 |
|
---|
263 | StructDecl( const CodeLocation& loc, const std::string& name,
|
---|
264 | Aggregate kind = Struct,
|
---|
265 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
|
---|
266 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
|
---|
267 |
|
---|
268 | bool is_coroutine() { return kind == Coroutine; }
|
---|
269 | bool is_generator() { return kind == Generator; }
|
---|
270 | bool is_monitor () { return kind == Monitor ; }
|
---|
271 | bool is_thread () { return kind == Thread ; }
|
---|
272 |
|
---|
273 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
274 |
|
---|
275 | const char * typeString() const override { return aggrString( kind ); }
|
---|
276 |
|
---|
277 | private:
|
---|
278 | StructDecl * clone() const override { return new StructDecl{ *this }; }
|
---|
279 | MUTATE_FRIEND
|
---|
280 | };
|
---|
281 |
|
---|
282 | /// union declaration `union Foo { ... };`
|
---|
283 | class UnionDecl final : public AggregateDecl {
|
---|
284 | public:
|
---|
285 | UnionDecl( const CodeLocation& loc, const std::string& name,
|
---|
286 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
|
---|
287 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
|
---|
288 |
|
---|
289 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
|
---|
290 |
|
---|
291 | const char * typeString() const override { return aggrString( Union ); }
|
---|
292 |
|
---|
293 | private:
|
---|
294 | UnionDecl * clone() const override { return new UnionDecl{ *this }; }
|
---|
295 | MUTATE_FRIEND
|
---|
296 | };
|
---|
297 |
|
---|
298 | /// enum declaration `enum Foo { ... };`
|
---|
299 | class EnumDecl final : public AggregateDecl {
|
---|
300 | public:
|
---|
301 | EnumDecl( const CodeLocation& loc, const std::string& name,
|
---|
302 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
|
---|
303 | : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
|
---|
304 |
|
---|
305 | /// gets the integer value for this enumerator, returning true iff value found
|
---|
306 | bool valueOf( const Decl * enumerator, long long& value ) const;
|
---|
307 |
|
---|
308 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
309 |
|
---|
310 | const char * typeString() const override { return aggrString( Enum ); }
|
---|
311 |
|
---|
312 | private:
|
---|
313 | EnumDecl * clone() const override { return new EnumDecl{ *this }; }
|
---|
314 | MUTATE_FRIEND
|
---|
315 |
|
---|
316 | /// Map from names to enumerator values; kept private for lazy initialization
|
---|
317 | mutable std::unordered_map< std::string, long long > enumValues;
|
---|
318 | };
|
---|
319 |
|
---|
320 | /// trait declaration `trait Foo( ... ) { ... };`
|
---|
321 | class TraitDecl final : public AggregateDecl {
|
---|
322 | public:
|
---|
323 | TraitDecl( const CodeLocation& loc, const std::string& name,
|
---|
324 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
|
---|
325 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
|
---|
326 |
|
---|
327 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
328 |
|
---|
329 | const char * typeString() const override { return "trait"; }
|
---|
330 |
|
---|
331 | private:
|
---|
332 | TraitDecl * clone() const override { return new TraitDecl{ *this }; }
|
---|
333 | MUTATE_FRIEND
|
---|
334 | };
|
---|
335 |
|
---|
336 | /// With statement `with (...) ...`
|
---|
337 | class WithStmt final : public Decl {
|
---|
338 | public:
|
---|
339 | std::vector<ptr<Expr>> exprs;
|
---|
340 | ptr<Stmt> stmt;
|
---|
341 |
|
---|
342 | WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
|
---|
343 | : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
|
---|
344 |
|
---|
345 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
346 | private:
|
---|
347 | WithStmt * clone() const override { return new WithStmt{ *this }; }
|
---|
348 | MUTATE_FRIEND
|
---|
349 | };
|
---|
350 |
|
---|
351 | class AsmDecl : public Decl {
|
---|
352 | public:
|
---|
353 | ptr<AsmStmt> stmt;
|
---|
354 |
|
---|
355 | AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
|
---|
356 | : Decl( loc, "", {}, {} ), stmt(stmt) {}
|
---|
357 |
|
---|
358 | const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
359 | private:
|
---|
360 | AsmDecl * clone() const override { return new AsmDecl( *this ); }
|
---|
361 | MUTATE_FRIEND
|
---|
362 | };
|
---|
363 |
|
---|
364 | class StaticAssertDecl : public Decl {
|
---|
365 | public:
|
---|
366 | ptr<Expr> cond;
|
---|
367 | ptr<ConstantExpr> msg; // string literal
|
---|
368 |
|
---|
369 | StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
|
---|
370 | : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
|
---|
371 |
|
---|
372 | const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
373 | private:
|
---|
374 | StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
|
---|
375 | MUTATE_FRIEND
|
---|
376 | };
|
---|
377 |
|
---|
378 | }
|
---|
379 |
|
---|
380 | #undef MUTATE_FRIEND
|
---|
381 |
|
---|
382 | // Local Variables: //
|
---|
383 | // tab-width: 4 //
|
---|
384 | // mode: c++ //
|
---|
385 | // compile-command: "make install" //
|
---|
386 | // End: //
|
---|