source: src/AST/Decl.hpp@ f2e482cb

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f2e482cb was 8a5530c, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Fixed FunctionType cast, fixed maybe_accept, implemented statement visitation and fixed several nodes

  • Property mode set to 100644
File size: 15.1 KB
Line 
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
32namespace ast {
33
34class Attribute;
35class Expr;
36class Init;
37class TypeDecl;
38
39/// Base declaration class
40class Decl : public ParseNode {
41public:
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 const Decl * accept( Visitor & v ) const override = 0;
60private:
61 Decl * clone() const override = 0;
62};
63
64/// Typed declaration base class
65class DeclWithType : public Decl {
66public:
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 const DeclWithType * accept( Visitor & v ) const override = 0;
92private:
93 DeclWithType * clone() const override = 0;
94};
95
96/// Object declaration `Foo foo = 42;`
97class ObjectDecl final : public DeclWithType {
98public:
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 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
113private:
114 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/// Object declaration `int foo()`
122class FunctionDecl : public DeclWithType {
123public:
124 ptr<FunctionType> type;
125 ptr<CompoundStmt> stmts;
126 std::list< ptr<Expr> > withExprs;
127
128 FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type,
129 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
130 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
131 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
132 stmts( stmts ) {}
133
134 const Type * get_type() const override;
135 void set_type(Type * t) override;
136
137 bool has_body() const { return stmts; }
138
139 const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); }
140private:
141 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
142
143 /// Must be copied in ALL derived classes
144 template<typename node_t>
145 friend auto mutate(const node_t * node);
146};
147
148/// Base class for named type aliases
149class NamedTypeDecl : public Decl {
150public:
151 ptr<Type> base;
152 std::vector<ptr<TypeDecl>> parameters;
153 std::vector<ptr<DeclWithType>> assertions;
154
155 NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
156 Type* b, Linkage::Spec spec = Linkage::Cforall )
157 : Decl( loc, name, storage, spec ), base( b ), parameters(), assertions() {}
158
159 /// Produces a name for the kind of alias
160 virtual std::string typeString() const = 0;
161
162private:
163 NamedTypeDecl* clone() const override = 0;
164};
165
166/// Cforall type variable: `dtype T`
167class TypeDecl final : public NamedTypeDecl {
168public:
169 TypeVar::Kind kind;
170 bool sized;
171 ptr<Type> init;
172
173 /// Data extracted from a type decl
174 struct Data {
175 TypeVar::Kind kind;
176 bool isComplete;
177
178 Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {}
179 Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {}
180 Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {}
181 Data( const Data& d1, const Data& d2 )
182 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
183
184 bool operator== ( const Data& o ) const {
185 return kind == o.kind && isComplete == o.isComplete;
186 }
187 bool operator!= ( const Data& o ) const { return !(*this == o); }
188 };
189
190 TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b,
191 TypeVar::Kind k, bool s, Type* i = nullptr )
192 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ),
193 init( i ) {}
194
195 std::string typeString() const override;
196 /// Produces a name for generated code
197 std::string genTypeString() const;
198
199 /// convenience accessor to match Type::isComplete()
200 bool isComplete() { return sized; }
201
202 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
203private:
204 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
205
206 /// Must be copied in ALL derived classes
207 template<typename node_t>
208 friend auto mutate(const node_t * node);
209};
210
211/// C-style typedef `typedef Foo Bar`
212class TypedefDecl final : public NamedTypeDecl {
213public:
214 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
215 Type* b, Linkage::Spec spec = Linkage::Cforall )
216 : NamedTypeDecl( loc, name, storage, b, spec ) {}
217
218 std::string typeString() const override { return "typedef"; }
219
220 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
221private:
222 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
223
224 /// Must be copied in ALL derived classes
225 template<typename node_t>
226 friend auto mutate(const node_t * node);
227};
228
229/// Aggregate type declaration base class
230class AggregateDecl : public Decl {
231public:
232 std::vector<ptr<Decl>> members;
233 std::vector<ptr<TypeDecl>> parameters;
234 std::vector<ptr<Attribute>> attributes;
235 bool body = false;
236 readonly<AggregateDecl> parent = {};
237
238 AggregateDecl( const CodeLocation& loc, const std::string& name,
239 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
240 : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(),
241 attributes( std::move(attrs) ) {}
242
243 AggregateDecl* set_body( bool b ) { body = b; return this; }
244
245protected:
246 /// Produces a name for the kind of aggregate
247 virtual std::string typeString() const = 0;
248};
249
250/// struct declaration `struct Foo { ... };`
251class StructDecl final : public AggregateDecl {
252public:
253 DeclarationNode::Aggregate kind;
254
255 StructDecl( const CodeLocation& loc, const std::string& name,
256 DeclarationNode::Aggregate kind = DeclarationNode::Struct,
257 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
258 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
259
260 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
261 bool is_monitor() { return kind == DeclarationNode::Monitor; }
262 bool is_thread() { return kind == DeclarationNode::Thread; }
263
264 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
265private:
266 StructDecl * clone() const override { return new StructDecl{ *this }; }
267
268 /// Must be copied in ALL derived classes
269 template<typename node_t>
270 friend auto mutate(const node_t * node);
271
272 std::string typeString() const override { return "struct"; }
273};
274
275/// union declaration `union Foo { ... };`
276class UnionDecl final : public AggregateDecl {
277public:
278 UnionDecl( const CodeLocation& loc, const std::string& name,
279 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
280 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
281
282 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
283private:
284 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
285
286 /// Must be copied in ALL derived classes
287 template<typename node_t>
288 friend auto mutate(const node_t * node);
289
290 std::string typeString() const override { return "union"; }
291};
292
293/// enum declaration `enum Foo { ... };`
294class EnumDecl final : public AggregateDecl {
295public:
296 EnumDecl( const CodeLocation& loc, const std::string& name,
297 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
298 : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
299
300 /// gets the integer value for this enumerator, returning true iff value found
301 bool valueOf( Decl* enumerator, long long& value ) const;
302
303 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
304private:
305 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
306
307 /// Must be copied in ALL derived classes
308 template<typename node_t>
309 friend auto mutate(const node_t * node);
310
311 std::string typeString() const override { return "enum"; }
312
313 /// Map from names to enumerator values; kept private for lazy initialization
314 mutable std::unordered_map< std::string, long long > enumValues;
315};
316
317/// trait declaration `trait Foo( ... ) { ... };`
318class TraitDecl final : public AggregateDecl {
319public:
320 TraitDecl( const CodeLocation& loc, const std::string& name,
321 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
322 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
323
324 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
325private:
326 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
327
328 /// Must be copied in ALL derived classes
329 template<typename node_t>
330 friend auto mutate(const node_t * node);
331
332 std::string typeString() const override { return "trait"; }
333};
334
335class AsmDecl : public Decl {
336public:
337 ptr<AsmStmt> stmt;
338
339 AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
340 : Decl( loc, "", {}, {} ), stmt(stmt) {}
341
342 const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
343private:
344 AsmDecl *clone() const override { return new AsmDecl( *this ); }
345
346 /// Must be copied in ALL derived classes
347 template<typename node_t>
348 friend auto mutate(const node_t * node);
349};
350
351class StaticAssertDecl : public Decl {
352public:
353 ptr<Expr> condition;
354 ptr<ConstantExpr> msg; // string literal
355
356 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
357 : Decl( loc, "", {}, {} ), condition( condition ), msg( msg ) {}
358
359 const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
360private:
361 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
362
363 /// Must be copied in ALL derived classes
364 template<typename node_t>
365 friend auto mutate(const node_t * node);
366};
367
368//=================================================================================================
369/// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency
370/// remove only if there is a better solution
371/// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with
372/// forward declarations
373inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); }
374inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); }
375inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); }
376inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); }
377inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); }
378inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); }
379inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); }
380inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
381inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); }
382inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); }
383inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); }
384inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); }
385inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); }
386inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
387inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); }
388inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); }
389inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); }
390inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); }
391inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
392inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
393inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
394inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
395inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); }
396inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); }
397inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); }
398inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); }
399inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); }
400inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); }
401
402}
403
404// Local Variables: //
405// tab-width: 4 //
406// mode: c++ //
407// compile-command: "make install" //
408// End: //
Note: See TracBrowser for help on using the repository browser.