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 | class Attribute; |
---|
35 | class Expr; |
---|
36 | class Init; |
---|
37 | class TypeDecl; |
---|
38 | |
---|
39 | /// Base declaration class |
---|
40 | class Decl : public ParseNode { |
---|
41 | public: |
---|
42 | std::string name; |
---|
43 | Storage::Classes storage; |
---|
44 | Linkage::Spec linkage; |
---|
45 | UniqueId uniqueId = 0; |
---|
46 | bool extension = false; |
---|
47 | |
---|
48 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
49 | Linkage::Spec linkage ) |
---|
50 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
51 | |
---|
52 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
53 | |
---|
54 | /// Ensures this node has a unique ID |
---|
55 | void fixUniqueId(); |
---|
56 | /// Get canonical declaration for unique ID |
---|
57 | static readonly<Decl> fromId( UniqueId id ); |
---|
58 | |
---|
59 | virtual const Decl * accept( Visitor & v ) const override = 0; |
---|
60 | private: |
---|
61 | virtual Decl * clone() const override = 0; |
---|
62 | }; |
---|
63 | |
---|
64 | /// Typed declaration base class |
---|
65 | class DeclWithType : public Decl { |
---|
66 | public: |
---|
67 | /// Represents the type with all types and typedefs expanded. |
---|
68 | /// This field is generated by SymTab::Validate::Pass2 |
---|
69 | std::string mangleName; |
---|
70 | /// Stores the scope level at which the variable was declared. |
---|
71 | /// Used to access shadowed identifiers. |
---|
72 | int scopeLevel = 0; |
---|
73 | |
---|
74 | std::vector<ptr<Attribute>> attributes; |
---|
75 | Function::Specs funcSpec; |
---|
76 | ptr<Expr> asmName; |
---|
77 | bool isDeleted = false; |
---|
78 | |
---|
79 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
80 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
81 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
82 | funcSpec(fs), asmName() {} |
---|
83 | |
---|
84 | std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
85 | |
---|
86 | /// Get type of this declaration. May be generated by subclass |
---|
87 | virtual const Type * get_type() const = 0; |
---|
88 | /// Set type of this declaration. May be verified by subclass |
---|
89 | virtual void set_type(Type*) = 0; |
---|
90 | |
---|
91 | virtual const DeclWithType * accept( Visitor & v ) const override = 0; |
---|
92 | private: |
---|
93 | virtual DeclWithType * clone() const override = 0; |
---|
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, 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 | virtual const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
113 | private: |
---|
114 | virtual ObjectDecl * clone() const override { return new ObjectDecl{ *this }; } |
---|
115 | |
---|
116 | /// Must be copied in ALL derived classes |
---|
117 | template<typename node_t> |
---|
118 | friend auto mutate(const node_t * node); |
---|
119 | }; |
---|
120 | |
---|
121 | /// Base class for named type aliases |
---|
122 | class NamedTypeDecl : public Decl { |
---|
123 | public: |
---|
124 | ptr<Type> base; |
---|
125 | std::vector<ptr<TypeDecl>> parameters; |
---|
126 | std::vector<ptr<DeclWithType>> assertions; |
---|
127 | |
---|
128 | NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
129 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
130 | : Decl( loc, name, storage, spec ), base( b ), parameters(), assertions() {} |
---|
131 | |
---|
132 | /// Produces a name for the kind of alias |
---|
133 | virtual std::string typeString() const = 0; |
---|
134 | |
---|
135 | private: |
---|
136 | NamedTypeDecl* clone() const override = 0; |
---|
137 | }; |
---|
138 | |
---|
139 | /// Cforall type variable: `dtype T` |
---|
140 | class TypeDecl final : public NamedTypeDecl { |
---|
141 | public: |
---|
142 | TypeVar::Kind kind; |
---|
143 | bool sized; |
---|
144 | ptr<Type> init; |
---|
145 | |
---|
146 | /// Data extracted from a type decl |
---|
147 | struct Data { |
---|
148 | TypeVar::Kind kind; |
---|
149 | bool isComplete; |
---|
150 | |
---|
151 | Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {} |
---|
152 | Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
153 | Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
154 | Data( const Data& d1, const Data& d2 ) |
---|
155 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
156 | |
---|
157 | bool operator== ( const Data& o ) const { |
---|
158 | return kind == o.kind && isComplete == o.isComplete; |
---|
159 | } |
---|
160 | bool operator!= ( const Data& o ) const { return !(*this == o); } |
---|
161 | }; |
---|
162 | |
---|
163 | TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b, |
---|
164 | TypeVar::Kind k, bool s, Type* i = nullptr ) |
---|
165 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), |
---|
166 | init( i ) {} |
---|
167 | |
---|
168 | std::string typeString() const override; |
---|
169 | /// Produces a name for generated code |
---|
170 | std::string genTypeString() const; |
---|
171 | |
---|
172 | /// convenience accessor to match Type::isComplete() |
---|
173 | bool isComplete() { return sized; } |
---|
174 | |
---|
175 | virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
176 | private: |
---|
177 | virtual TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
178 | }; |
---|
179 | |
---|
180 | /// C-style typedef `typedef Foo Bar` |
---|
181 | class TypedefDecl final : public NamedTypeDecl { |
---|
182 | public: |
---|
183 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
184 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
185 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
186 | |
---|
187 | std::string typeString() const override { return "typedef"; } |
---|
188 | |
---|
189 | virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
190 | private: |
---|
191 | virtual TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
192 | }; |
---|
193 | |
---|
194 | /// Aggregate type declaration base class |
---|
195 | class AggregateDecl : public Decl { |
---|
196 | public: |
---|
197 | std::vector<ptr<Decl>> members; |
---|
198 | std::vector<ptr<TypeDecl>> parameters; |
---|
199 | std::vector<ptr<Attribute>> attributes; |
---|
200 | bool body = false; |
---|
201 | readonly<AggregateDecl> parent = {}; |
---|
202 | |
---|
203 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
204 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
205 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(), |
---|
206 | attributes( std::move(attrs) ) {} |
---|
207 | |
---|
208 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
209 | |
---|
210 | protected: |
---|
211 | /// Produces a name for the kind of aggregate |
---|
212 | virtual std::string typeString() const = 0; |
---|
213 | }; |
---|
214 | |
---|
215 | /// struct declaration `struct Foo { ... };` |
---|
216 | class StructDecl final : public AggregateDecl { |
---|
217 | public: |
---|
218 | DeclarationNode::Aggregate kind; |
---|
219 | |
---|
220 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
221 | DeclarationNode::Aggregate kind = DeclarationNode::Struct, |
---|
222 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
223 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
224 | |
---|
225 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } |
---|
226 | bool is_monitor() { return kind == DeclarationNode::Monitor; } |
---|
227 | bool is_thread() { return kind == DeclarationNode::Thread; } |
---|
228 | |
---|
229 | virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
230 | private: |
---|
231 | virtual StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
232 | |
---|
233 | std::string typeString() const override { return "struct"; } |
---|
234 | }; |
---|
235 | |
---|
236 | /// union declaration `union Foo { ... };` |
---|
237 | class UnionDecl final : public AggregateDecl { |
---|
238 | public: |
---|
239 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
240 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
241 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
242 | |
---|
243 | virtual const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
244 | private: |
---|
245 | virtual UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
246 | |
---|
247 | std::string typeString() const override { return "union"; } |
---|
248 | }; |
---|
249 | |
---|
250 | /// enum declaration `enum Foo { ... };` |
---|
251 | class EnumDecl final : public AggregateDecl { |
---|
252 | public: |
---|
253 | EnumDecl( const CodeLocation& loc, const std::string& name, |
---|
254 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
255 | : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {} |
---|
256 | |
---|
257 | /// gets the integer value for this enumerator, returning true iff value found |
---|
258 | bool valueOf( Decl* enumerator, long long& value ) const; |
---|
259 | |
---|
260 | virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
261 | private: |
---|
262 | virtual EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
263 | |
---|
264 | std::string typeString() const override { return "enum"; } |
---|
265 | |
---|
266 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
267 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
268 | }; |
---|
269 | |
---|
270 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
271 | class TraitDecl final : public AggregateDecl { |
---|
272 | public: |
---|
273 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
274 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
275 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
276 | |
---|
277 | virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
278 | private: |
---|
279 | virtual TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
280 | |
---|
281 | std::string typeString() const override { return "trait"; } |
---|
282 | }; |
---|
283 | |
---|
284 | |
---|
285 | //================================================================================================= |
---|
286 | /// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency |
---|
287 | /// remove only if there is a better solution |
---|
288 | /// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with |
---|
289 | /// forward declarations |
---|
290 | inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
291 | inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
292 | inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); } |
---|
293 | inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
294 | inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
295 | inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
296 | // inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
297 | // inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
298 | inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
299 | inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
300 | inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
301 | inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
302 | inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
303 | inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
304 | inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
305 | inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
306 | inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
307 | inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
308 | inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
309 | inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
310 | inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
311 | inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
312 | // inline void increment( const class FtypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
313 | // inline void decrement( const class FtypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
314 | // inline void increment( const class DtypeDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
315 | // inline void decrement( const class DtypeDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
316 | inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
317 | inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
318 | // inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
319 | // inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
320 | // inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); } |
---|
321 | // inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); } |
---|
322 | |
---|
323 | } |
---|
324 | |
---|
325 | // Local Variables: // |
---|
326 | // tab-width: 4 // |
---|
327 | // mode: c++ // |
---|
328 | // compile-command: "make install" // |
---|
329 | // End: // |
---|