source: src/AST/Decl.hpp@ 94b1f718

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 94b1f718 was 9d6e7fa9, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed missing or incorrect stubs in decl/InitTweak

  • Property mode set to 100644
File size: 11.3 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
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
35namespace ast {
36
37/// Base declaration class
38class Decl : public ParseNode {
39public:
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;
58private:
59 Decl * clone() const override = 0;
60 MUTATE_FRIEND
61};
62
63/// Typed declaration base class
64class DeclWithType : public Decl {
65public:
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;
91private:
92 DeclWithType * clone() const override = 0;
93 MUTATE_FRIEND
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, 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 ); }
113private:
114 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
115 MUTATE_FRIEND
116};
117
118/// Object declaration `int foo()`
119class FunctionDecl : public DeclWithType {
120public:
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 ); }
137private:
138 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
139 MUTATE_FRIEND
140};
141
142/// Base class for named type aliases
143class NamedTypeDecl : public Decl {
144public:
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
156private:
157 NamedTypeDecl* clone() const override = 0;
158 MUTATE_FRIEND
159};
160
161/// Cforall type variable: `dtype T`
162class TypeDecl final : public NamedTypeDecl {
163public:
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 ); }
198private:
199 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
200 MUTATE_FRIEND
201};
202
203/// C-style typedef `typedef Foo Bar`
204class TypedefDecl final : public NamedTypeDecl {
205public:
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 ); }
213private:
214 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
215 MUTATE_FRIEND
216};
217
218/// Aggregate type declaration base class
219class AggregateDecl : public Decl {
220public:
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 /// Produces a name for the kind of aggregate
235 virtual std::string typeString() const = 0;
236
237private:
238 AggregateDecl * clone() const override = 0;
239 MUTATE_FRIEND
240};
241
242/// struct declaration `struct Foo { ... };`
243class StructDecl final : public AggregateDecl {
244public:
245 DeclarationNode::Aggregate kind;
246
247 StructDecl( const CodeLocation& loc, const std::string& name,
248 DeclarationNode::Aggregate kind = DeclarationNode::Struct,
249 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
250 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
251
252 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
253 bool is_monitor() { return kind == DeclarationNode::Monitor; }
254 bool is_thread() { return kind == DeclarationNode::Thread; }
255
256 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
257
258 std::string typeString() const override { return "struct"; }
259
260private:
261 StructDecl * clone() const override { return new StructDecl{ *this }; }
262 MUTATE_FRIEND
263};
264
265/// union declaration `union Foo { ... };`
266class UnionDecl final : public AggregateDecl {
267public:
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
274 std::string typeString() const override { return "union"; }
275
276private:
277 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
278 MUTATE_FRIEND
279};
280
281/// enum declaration `enum Foo { ... };`
282class EnumDecl final : public AggregateDecl {
283public:
284 EnumDecl( const CodeLocation& loc, const std::string& name,
285 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
286 : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
287
288 /// gets the integer value for this enumerator, returning true iff value found
289 bool valueOf( const Decl * enumerator, long long& value ) const;
290
291 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
292
293 std::string typeString() const override { return "enum"; }
294
295private:
296 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
297 MUTATE_FRIEND
298
299 /// Map from names to enumerator values; kept private for lazy initialization
300 mutable std::unordered_map< std::string, long long > enumValues;
301};
302
303/// trait declaration `trait Foo( ... ) { ... };`
304class TraitDecl final : public AggregateDecl {
305public:
306 TraitDecl( const CodeLocation& loc, const std::string& name,
307 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
308 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
309
310 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
311
312 std::string typeString() const override { return "trait"; }
313
314private:
315 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
316 MUTATE_FRIEND
317};
318
319class AsmDecl : public Decl {
320public:
321 ptr<AsmStmt> stmt;
322
323 AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
324 : Decl( loc, "", {}, {} ), stmt(stmt) {}
325
326 const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
327private:
328 AsmDecl *clone() const override { return new AsmDecl( *this ); }
329 MUTATE_FRIEND
330};
331
332class StaticAssertDecl : public Decl {
333public:
334 ptr<Expr> cond;
335 ptr<ConstantExpr> msg; // string literal
336
337 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
338 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
339
340 const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
341private:
342 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
343 MUTATE_FRIEND
344};
345
346}
347
348#undef MUTATE_FRIEND
349
350// Local Variables: //
351// tab-width: 4 //
352// mode: c++ //
353// compile-command: "make install" //
354// End: //
Note: See TracBrowser for help on using the repository browser.