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 | namespace ast { |
---|
33 | |
---|
34 | /// Base declaration class |
---|
35 | class Decl : public ParseNode { |
---|
36 | public: |
---|
37 | std::string name; |
---|
38 | Storage::Classes storage; |
---|
39 | Linkage::Spec linkage; |
---|
40 | UniqueId uniqueId = 0; |
---|
41 | bool extension = false; |
---|
42 | |
---|
43 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
44 | Linkage::Spec linkage ) |
---|
45 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
46 | |
---|
47 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
48 | |
---|
49 | /// Ensures this node has a unique ID |
---|
50 | void fixUniqueId(); |
---|
51 | /// Get canonical declaration for unique ID |
---|
52 | static readonly<Decl> fromId( UniqueId id ); |
---|
53 | |
---|
54 | const Decl * accept( Visitor & v ) const override = 0; |
---|
55 | private: |
---|
56 | Decl * clone() const override = 0; |
---|
57 | }; |
---|
58 | |
---|
59 | /// Typed declaration base class |
---|
60 | class DeclWithType : public Decl { |
---|
61 | public: |
---|
62 | /// Represents the type with all types and typedefs expanded. |
---|
63 | /// This field is generated by SymTab::Validate::Pass2 |
---|
64 | std::string mangleName; |
---|
65 | /// Stores the scope level at which the variable was declared. |
---|
66 | /// Used to access shadowed identifiers. |
---|
67 | int scopeLevel = 0; |
---|
68 | |
---|
69 | std::vector<ptr<Attribute>> attributes; |
---|
70 | Function::Specs funcSpec; |
---|
71 | ptr<Expr> asmName; |
---|
72 | bool isDeleted = false; |
---|
73 | |
---|
74 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
75 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
76 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
77 | funcSpec(fs), asmName() {} |
---|
78 | |
---|
79 | std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
80 | |
---|
81 | /// Get type of this declaration. May be generated by subclass |
---|
82 | virtual const Type * get_type() const = 0; |
---|
83 | /// Set type of this declaration. May be verified by subclass |
---|
84 | virtual void set_type(Type *) = 0; |
---|
85 | |
---|
86 | const DeclWithType * accept( Visitor & v ) const override = 0; |
---|
87 | private: |
---|
88 | DeclWithType * clone() const override = 0; |
---|
89 | }; |
---|
90 | |
---|
91 | /// Object declaration `Foo foo = 42;` |
---|
92 | class ObjectDecl final : public DeclWithType { |
---|
93 | public: |
---|
94 | ptr<Type> type; |
---|
95 | ptr<Init> init; |
---|
96 | ptr<Expr> bitfieldWidth; |
---|
97 | |
---|
98 | ObjectDecl( const CodeLocation& loc, const std::string& name, Type* type, Init* init = nullptr, |
---|
99 | Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, Expr* bitWd = nullptr, |
---|
100 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) |
---|
101 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
102 | init( init ), bitfieldWidth( bitWd ) {} |
---|
103 | |
---|
104 | const Type* get_type() const override { return type; } |
---|
105 | void set_type( Type * ty ) override { type = ty; } |
---|
106 | |
---|
107 | const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
108 | private: |
---|
109 | ObjectDecl * clone() const override { return new ObjectDecl{ *this }; } |
---|
110 | |
---|
111 | /// Must be copied in ALL derived classes |
---|
112 | template<typename node_t> |
---|
113 | friend auto mutate(const node_t * node); |
---|
114 | }; |
---|
115 | |
---|
116 | class FunctionDecl : public DeclWithType { |
---|
117 | public: |
---|
118 | ptr<FunctionType> type; |
---|
119 | ptr<CompoundStmt> stmts; |
---|
120 | std::list< ptr<Expr> > withExprs; |
---|
121 | |
---|
122 | FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type, |
---|
123 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, |
---|
124 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) |
---|
125 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
126 | stmts( stmts ) {} |
---|
127 | |
---|
128 | const Type * get_type() const override { return type.get(); } |
---|
129 | void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); } |
---|
130 | |
---|
131 | bool has_body() const { return stmts; } |
---|
132 | |
---|
133 | const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
134 | private: |
---|
135 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); } |
---|
136 | |
---|
137 | /// Must be copied in ALL derived classes |
---|
138 | template<typename node_t> |
---|
139 | friend auto mutate(const node_t * node); |
---|
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 | }; |
---|
159 | |
---|
160 | /// Cforall type variable: `dtype T` |
---|
161 | class TypeDecl final : public NamedTypeDecl { |
---|
162 | public: |
---|
163 | TypeVar::Kind kind; |
---|
164 | bool sized; |
---|
165 | ptr<Type> init; |
---|
166 | |
---|
167 | /// Data extracted from a type decl |
---|
168 | struct Data { |
---|
169 | TypeVar::Kind kind; |
---|
170 | bool isComplete; |
---|
171 | |
---|
172 | Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {} |
---|
173 | Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
174 | Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
175 | Data( const Data& d1, const Data& d2 ) |
---|
176 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
177 | |
---|
178 | bool operator== ( const Data& o ) const { |
---|
179 | return kind == o.kind && isComplete == o.isComplete; |
---|
180 | } |
---|
181 | bool operator!= ( const Data& o ) const { return !(*this == o); } |
---|
182 | }; |
---|
183 | |
---|
184 | TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b, |
---|
185 | TypeVar::Kind k, bool s, Type* i = nullptr ) |
---|
186 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), |
---|
187 | init( i ) {} |
---|
188 | |
---|
189 | std::string typeString() const override; |
---|
190 | /// Produces a name for generated code |
---|
191 | std::string genTypeString() const; |
---|
192 | |
---|
193 | /// convenience accessor to match Type::isComplete() |
---|
194 | bool isComplete() { return sized; } |
---|
195 | |
---|
196 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
197 | private: |
---|
198 | TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
199 | |
---|
200 | /// Must be copied in ALL derived classes |
---|
201 | template<typename node_t> |
---|
202 | friend auto mutate(const node_t * node); |
---|
203 | }; |
---|
204 | |
---|
205 | /// C-style typedef `typedef Foo Bar` |
---|
206 | class TypedefDecl final : public NamedTypeDecl { |
---|
207 | public: |
---|
208 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
209 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
210 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
211 | |
---|
212 | std::string typeString() const override { return "typedef"; } |
---|
213 | |
---|
214 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
215 | private: |
---|
216 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
217 | |
---|
218 | /// Must be copied in ALL derived classes |
---|
219 | template<typename node_t> |
---|
220 | friend auto mutate(const node_t * node); |
---|
221 | }; |
---|
222 | |
---|
223 | /// Aggregate type declaration base class |
---|
224 | class AggregateDecl : public Decl { |
---|
225 | public: |
---|
226 | std::vector<ptr<Decl>> members; |
---|
227 | std::vector<ptr<TypeDecl>> params; |
---|
228 | std::vector<ptr<Attribute>> attributes; |
---|
229 | bool body = false; |
---|
230 | readonly<AggregateDecl> parent = {}; |
---|
231 | |
---|
232 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
233 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
234 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(), |
---|
235 | attributes( std::move(attrs) ) {} |
---|
236 | |
---|
237 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
238 | |
---|
239 | protected: |
---|
240 | /// Produces a name for the kind of aggregate |
---|
241 | virtual std::string typeString() const = 0; |
---|
242 | }; |
---|
243 | |
---|
244 | /// struct declaration `struct Foo { ... };` |
---|
245 | class StructDecl final : public AggregateDecl { |
---|
246 | public: |
---|
247 | DeclarationNode::Aggregate kind; |
---|
248 | |
---|
249 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
250 | DeclarationNode::Aggregate kind = DeclarationNode::Struct, |
---|
251 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
252 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
253 | |
---|
254 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } |
---|
255 | bool is_monitor() { return kind == DeclarationNode::Monitor; } |
---|
256 | bool is_thread() { return kind == DeclarationNode::Thread; } |
---|
257 | |
---|
258 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
259 | private: |
---|
260 | StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
261 | |
---|
262 | /// Must be copied in ALL derived classes |
---|
263 | template<typename node_t> |
---|
264 | friend auto mutate(const node_t * node); |
---|
265 | |
---|
266 | std::string typeString() const override { return "struct"; } |
---|
267 | }; |
---|
268 | |
---|
269 | /// union declaration `union Foo { ... };` |
---|
270 | class UnionDecl final : public AggregateDecl { |
---|
271 | public: |
---|
272 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
273 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
274 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
275 | |
---|
276 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
277 | private: |
---|
278 | UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
279 | |
---|
280 | /// Must be copied in ALL derived classes |
---|
281 | template<typename node_t> |
---|
282 | friend auto mutate(const node_t * node); |
---|
283 | |
---|
284 | std::string typeString() const override { return "union"; } |
---|
285 | }; |
---|
286 | |
---|
287 | /// enum declaration `enum Foo { ... };` |
---|
288 | class EnumDecl final : public AggregateDecl { |
---|
289 | public: |
---|
290 | EnumDecl( const CodeLocation& loc, const std::string& name, |
---|
291 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
292 | : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {} |
---|
293 | |
---|
294 | /// gets the integer value for this enumerator, returning true iff value found |
---|
295 | bool valueOf( Decl* enumerator, long long& value ) const; |
---|
296 | |
---|
297 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
298 | private: |
---|
299 | EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
300 | |
---|
301 | /// Must be copied in ALL derived classes |
---|
302 | template<typename node_t> |
---|
303 | friend auto mutate(const node_t * node); |
---|
304 | |
---|
305 | std::string typeString() const override { return "enum"; } |
---|
306 | |
---|
307 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
308 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
309 | }; |
---|
310 | |
---|
311 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
312 | class TraitDecl final : public AggregateDecl { |
---|
313 | public: |
---|
314 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
315 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
316 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
317 | |
---|
318 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
319 | private: |
---|
320 | TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
321 | |
---|
322 | /// Must be copied in ALL derived classes |
---|
323 | template<typename node_t> |
---|
324 | friend auto mutate(const node_t * node); |
---|
325 | |
---|
326 | std::string typeString() const override { return "trait"; } |
---|
327 | }; |
---|
328 | |
---|
329 | class AsmDecl : public Decl { |
---|
330 | public: |
---|
331 | ptr<AsmStmt> stmt; |
---|
332 | |
---|
333 | AsmDecl( const CodeLocation & loc, AsmStmt *stmt ) |
---|
334 | : Decl( loc, "", {}, {} ), stmt(stmt) {} |
---|
335 | |
---|
336 | const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
337 | private: |
---|
338 | AsmDecl *clone() const override { return new AsmDecl( *this ); } |
---|
339 | |
---|
340 | /// Must be copied in ALL derived classes |
---|
341 | template<typename node_t> |
---|
342 | friend auto mutate(const node_t * node); |
---|
343 | }; |
---|
344 | |
---|
345 | class StaticAssertDecl : public Decl { |
---|
346 | public: |
---|
347 | ptr<Expr> condition; |
---|
348 | ptr<ConstantExpr> msg; // string literal |
---|
349 | |
---|
350 | StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg ) |
---|
351 | : Decl( loc, "", {}, {} ), condition( condition ), msg( msg ) {} |
---|
352 | |
---|
353 | const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
354 | private: |
---|
355 | StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); } |
---|
356 | |
---|
357 | /// Must be copied in ALL derived classes |
---|
358 | template<typename node_t> |
---|
359 | friend auto mutate(const node_t * node); |
---|
360 | }; |
---|
361 | |
---|
362 | //================================================================================================= |
---|
363 | /// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency |
---|
364 | /// remove only if there is a better solution |
---|
365 | /// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with |
---|
366 | /// forward declarations |
---|
367 | inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
368 | inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
369 | inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); } |
---|
370 | inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
371 | inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
372 | inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
373 | inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
374 | inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
375 | inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
376 | inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
377 | inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
378 | inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
379 | inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
380 | inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
381 | inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
382 | inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
383 | inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
384 | inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
385 | inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
386 | inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
387 | inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
388 | inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
389 | inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
390 | inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
391 | inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
392 | inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
393 | inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
394 | inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
395 | |
---|
396 | } |
---|
397 | |
---|
398 | // Local Variables: // |
---|
399 | // tab-width: 4 // |
---|
400 | // mode: c++ // |
---|
401 | // compile-command: "make install" // |
---|
402 | // End: // |
---|