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 : Aaron B. Moss |
---|
12 | // Last Modified On : Thu May 9 10:00:00 2019 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <string> // for string, to_string |
---|
19 | #include <unordered_map> |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #include "FunctionSpec.hpp" |
---|
23 | #include "Fwd.hpp" // for UniqueId |
---|
24 | #include "LinkageSpec.hpp" |
---|
25 | #include "Node.hpp" // for ptr, readonly |
---|
26 | #include "ParseNode.hpp" |
---|
27 | #include "StorageClasses.hpp" |
---|
28 | #include "TypeVar.hpp" |
---|
29 | #include "Visitor.hpp" |
---|
30 | #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate |
---|
31 | |
---|
32 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
33 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); |
---|
34 | |
---|
35 | namespace ast { |
---|
36 | |
---|
37 | /// Base declaration class |
---|
38 | class Decl : public ParseNode { |
---|
39 | public: |
---|
40 | std::string name; |
---|
41 | Storage::Classes storage; |
---|
42 | Linkage::Spec linkage; |
---|
43 | UniqueId uniqueId = 0; |
---|
44 | bool extension = false; |
---|
45 | |
---|
46 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
47 | Linkage::Spec linkage ) |
---|
48 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
49 | |
---|
50 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
51 | |
---|
52 | /// Ensures this node has a unique ID |
---|
53 | void fixUniqueId(); |
---|
54 | /// Get canonical declaration for unique ID |
---|
55 | static readonly<Decl> fromId( UniqueId id ); |
---|
56 | |
---|
57 | const Decl * accept( Visitor & v ) const override = 0; |
---|
58 | private: |
---|
59 | Decl * clone() const override = 0; |
---|
60 | MUTATE_FRIEND |
---|
61 | }; |
---|
62 | |
---|
63 | /// Typed declaration base class |
---|
64 | class DeclWithType : public Decl { |
---|
65 | public: |
---|
66 | /// Represents the type with all types and typedefs expanded. |
---|
67 | /// This field is generated by SymTab::Validate::Pass2 |
---|
68 | std::string mangleName; |
---|
69 | /// Stores the scope level at which the variable was declared. |
---|
70 | /// Used to access shadowed identifiers. |
---|
71 | int scopeLevel = 0; |
---|
72 | |
---|
73 | std::vector<ptr<Attribute>> attributes; |
---|
74 | Function::Specs funcSpec; |
---|
75 | ptr<Expr> asmName; |
---|
76 | bool isDeleted = 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(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, Init * init = nullptr, |
---|
104 | Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 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( 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 | /// Object declaration `int foo()` |
---|
119 | class FunctionDecl : public DeclWithType { |
---|
120 | public: |
---|
121 | ptr<FunctionType> type; |
---|
122 | ptr<CompoundStmt> stmts; |
---|
123 | std::list< ptr<Expr> > withExprs; |
---|
124 | |
---|
125 | FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type, |
---|
126 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, |
---|
127 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) |
---|
128 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
129 | stmts( stmts ) {} |
---|
130 | |
---|
131 | const Type * get_type() const override; |
---|
132 | void set_type(Type * t) override; |
---|
133 | |
---|
134 | bool has_body() const { return stmts; } |
---|
135 | |
---|
136 | const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
137 | private: |
---|
138 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); } |
---|
139 | MUTATE_FRIEND |
---|
140 | }; |
---|
141 | |
---|
142 | /// Base class for named type aliases |
---|
143 | class NamedTypeDecl : public Decl { |
---|
144 | public: |
---|
145 | ptr<Type> base; |
---|
146 | std::vector<ptr<TypeDecl>> params; |
---|
147 | std::vector<ptr<DeclWithType>> assertions; |
---|
148 | |
---|
149 | NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
150 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
151 | : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {} |
---|
152 | |
---|
153 | /// Produces a name for the kind of alias |
---|
154 | virtual std::string typeString() const = 0; |
---|
155 | |
---|
156 | private: |
---|
157 | NamedTypeDecl* clone() const override = 0; |
---|
158 | MUTATE_FRIEND |
---|
159 | }; |
---|
160 | |
---|
161 | /// Cforall type variable: `dtype T` |
---|
162 | class TypeDecl final : public NamedTypeDecl { |
---|
163 | public: |
---|
164 | TypeVar::Kind kind; |
---|
165 | bool sized; |
---|
166 | ptr<Type> init; |
---|
167 | |
---|
168 | /// Data extracted from a type decl |
---|
169 | struct Data { |
---|
170 | TypeVar::Kind kind; |
---|
171 | bool isComplete; |
---|
172 | |
---|
173 | Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {} |
---|
174 | Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
175 | Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
176 | Data( const Data& d1, const Data& d2 ) |
---|
177 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
178 | |
---|
179 | bool operator== ( const Data& o ) const { |
---|
180 | return kind == o.kind && isComplete == o.isComplete; |
---|
181 | } |
---|
182 | bool operator!= ( const Data& o ) const { return !(*this == o); } |
---|
183 | }; |
---|
184 | |
---|
185 | TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b, |
---|
186 | TypeVar::Kind k, bool s, Type* i = nullptr ) |
---|
187 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), |
---|
188 | init( i ) {} |
---|
189 | |
---|
190 | std::string typeString() const override; |
---|
191 | /// Produces a name for generated code |
---|
192 | std::string genTypeString() const; |
---|
193 | |
---|
194 | /// convenience accessor to match Type::isComplete() |
---|
195 | bool isComplete() { return sized; } |
---|
196 | |
---|
197 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
198 | private: |
---|
199 | TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
200 | MUTATE_FRIEND |
---|
201 | }; |
---|
202 | |
---|
203 | /// C-style typedef `typedef Foo Bar` |
---|
204 | class TypedefDecl final : public NamedTypeDecl { |
---|
205 | public: |
---|
206 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
207 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
208 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
209 | |
---|
210 | std::string typeString() const override { return "typedef"; } |
---|
211 | |
---|
212 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
213 | private: |
---|
214 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
215 | MUTATE_FRIEND |
---|
216 | }; |
---|
217 | |
---|
218 | /// Aggregate type declaration base class |
---|
219 | class AggregateDecl : public Decl { |
---|
220 | public: |
---|
221 | std::vector<ptr<Decl>> members; |
---|
222 | std::vector<ptr<TypeDecl>> params; |
---|
223 | std::vector<ptr<Attribute>> attributes; |
---|
224 | bool body = false; |
---|
225 | readonly<AggregateDecl> parent = {}; |
---|
226 | |
---|
227 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
228 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
229 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(), |
---|
230 | attributes( std::move(attrs) ) {} |
---|
231 | |
---|
232 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
233 | |
---|
234 | private: |
---|
235 | AggregateDecl * clone() const override = 0; |
---|
236 | MUTATE_FRIEND |
---|
237 | |
---|
238 | protected: |
---|
239 | /// Produces a name for the kind of aggregate |
---|
240 | virtual std::string typeString() const = 0; |
---|
241 | }; |
---|
242 | |
---|
243 | /// struct declaration `struct Foo { ... };` |
---|
244 | class StructDecl final : public AggregateDecl { |
---|
245 | public: |
---|
246 | DeclarationNode::Aggregate kind; |
---|
247 | |
---|
248 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
249 | DeclarationNode::Aggregate kind = DeclarationNode::Struct, |
---|
250 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
251 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
252 | |
---|
253 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } |
---|
254 | bool is_monitor() { return kind == DeclarationNode::Monitor; } |
---|
255 | bool is_thread() { return kind == DeclarationNode::Thread; } |
---|
256 | |
---|
257 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
258 | private: |
---|
259 | StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
260 | MUTATE_FRIEND |
---|
261 | |
---|
262 | std::string typeString() const override { return "struct"; } |
---|
263 | }; |
---|
264 | |
---|
265 | /// union declaration `union Foo { ... };` |
---|
266 | class UnionDecl final : public AggregateDecl { |
---|
267 | public: |
---|
268 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
269 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
270 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
271 | |
---|
272 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
273 | private: |
---|
274 | UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
275 | MUTATE_FRIEND |
---|
276 | |
---|
277 | std::string typeString() const override { return "union"; } |
---|
278 | }; |
---|
279 | |
---|
280 | /// enum declaration `enum Foo { ... };` |
---|
281 | class EnumDecl final : public AggregateDecl { |
---|
282 | public: |
---|
283 | EnumDecl( const CodeLocation& loc, const std::string& name, |
---|
284 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
285 | : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {} |
---|
286 | |
---|
287 | /// gets the integer value for this enumerator, returning true iff value found |
---|
288 | bool valueOf( Decl* enumerator, long long& value ) const; |
---|
289 | |
---|
290 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
291 | private: |
---|
292 | EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
293 | MUTATE_FRIEND |
---|
294 | |
---|
295 | std::string typeString() const override { return "enum"; } |
---|
296 | |
---|
297 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
298 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
299 | }; |
---|
300 | |
---|
301 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
302 | class TraitDecl final : public AggregateDecl { |
---|
303 | public: |
---|
304 | TraitDecl( 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 ) {} |
---|
307 | |
---|
308 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
309 | private: |
---|
310 | TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
311 | MUTATE_FRIEND |
---|
312 | |
---|
313 | std::string typeString() const override { return "trait"; } |
---|
314 | }; |
---|
315 | |
---|
316 | class AsmDecl : public Decl { |
---|
317 | public: |
---|
318 | ptr<AsmStmt> stmt; |
---|
319 | |
---|
320 | AsmDecl( const CodeLocation & loc, AsmStmt *stmt ) |
---|
321 | : Decl( loc, "", {}, {} ), stmt(stmt) {} |
---|
322 | |
---|
323 | const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
324 | private: |
---|
325 | AsmDecl *clone() const override { return new AsmDecl( *this ); } |
---|
326 | MUTATE_FRIEND |
---|
327 | }; |
---|
328 | |
---|
329 | class StaticAssertDecl : public Decl { |
---|
330 | public: |
---|
331 | ptr<Expr> condition; |
---|
332 | ptr<ConstantExpr> msg; // string literal |
---|
333 | |
---|
334 | StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg ) |
---|
335 | : Decl( loc, "", {}, {} ), condition( condition ), msg( msg ) {} |
---|
336 | |
---|
337 | const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
338 | private: |
---|
339 | StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); } |
---|
340 | MUTATE_FRIEND |
---|
341 | }; |
---|
342 | |
---|
343 | } |
---|
344 | |
---|
345 | #undef MUTATE_FRIEND |
---|
346 | |
---|
347 | // Local Variables: // |
---|
348 | // tab-width: 4 // |
---|
349 | // mode: c++ // |
---|
350 | // compile-command: "make install" // |
---|
351 | // End: // |
---|