source: src/AST/Decl.hpp@ 1389810

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 1389810 was e5c3811, checked in by Fangren Yu <f37yu@…>, 5 years ago

create dedicated symbol tables for big 3 operators
note: arbitrary this param type is not supported; it is currently allowed although never used

  • Property mode set to 100644
File size: 12.6 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 : Peter A. Buhr
12// Last Modified On : Fri Dec 13 17:38:33 2019
13// Update Count : 29
14//
15
16#pragma once
17
18#include <iosfwd>
19#include <string> // for string, to_string
20#include <unordered_map>
21#include <vector>
22#include <algorithm>
23
24#include "FunctionSpec.hpp"
25#include "Fwd.hpp" // for UniqueId
26#include "LinkageSpec.hpp"
27#include "Node.hpp" // for ptr, readonly
28#include "ParseNode.hpp"
29#include "StorageClasses.hpp"
30#include "Visitor.hpp"
31#include "Common/utility.h"
32#include "Common/SemanticError.h" // error_str
33
34// Must be included in *all* AST classes; should be #undef'd at the end of the file
35#define MUTATE_FRIEND \
36 template<typename node_t> friend node_t * mutate(const node_t * node); \
37 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
38
39namespace ast {
40
41/// Base declaration class
42class Decl : public ParseNode {
43public:
44 std::string name;
45 Storage::Classes storage;
46 Linkage::Spec linkage;
47 UniqueId uniqueId = 0;
48 bool extension = false;
49
50 Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
51 Linkage::Spec linkage )
52 : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
53
54 Decl* set_extension( bool ex ) { extension = ex; return this; }
55
56 /// Ensures this node has a unique ID
57 void fixUniqueId();
58 /// Get canonical declaration for unique ID
59 static readonly<Decl> fromId( UniqueId id );
60
61 const Decl * accept( Visitor & v ) const override = 0;
62private:
63 Decl * clone() const override = 0;
64 MUTATE_FRIEND
65};
66
67/// Typed declaration base class
68class DeclWithType : public Decl {
69public:
70 /// Represents the type with all types and typedefs expanded.
71 /// This field is generated by SymTab::Validate::Pass2
72 std::string mangleName;
73 /// Stores the scope level at which the variable was declared.
74 /// Used to access shadowed identifiers.
75 int scopeLevel = 0;
76
77 /*
78 ForallDecl foralls {
79 list<TypeDecl> params
80 list<ObjectDecl> assns
81 }
82 */
83
84 std::vector<ptr<Attribute>> attributes;
85 Function::Specs funcSpec;
86 ptr<Expr> asmName;
87 bool isDeleted = false;
88 bool isTypeFixed = false;
89
90 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
91 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
92 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
93 funcSpec(fs), asmName() {}
94
95 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
96
97 /// Get type of this declaration. May be generated by subclass
98 virtual const Type * get_type() const = 0;
99 /// Set type of this declaration. May be verified by subclass
100 virtual void set_type( const Type * ) = 0;
101
102 const DeclWithType * accept( Visitor & v ) const override = 0;
103private:
104 DeclWithType * clone() const override = 0;
105 MUTATE_FRIEND
106};
107
108/// Object declaration `Foo foo = 42;`
109class ObjectDecl final : public DeclWithType {
110public:
111 ptr<Type> type;
112 ptr<Init> init;
113 ptr<Expr> bitfieldWidth;
114
115 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
116 const Init * init = nullptr, Storage::Classes storage = {},
117 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr,
118 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
119 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
120 init( init ), bitfieldWidth( bitWd ) {}
121
122 const Type* get_type() const override { return type; }
123 void set_type( const Type * ty ) override { type = ty; }
124
125 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
126private:
127 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
128 MUTATE_FRIEND
129};
130
131/// Object declaration `int foo()`
132class FunctionDecl : public DeclWithType {
133public:
134 std::vector<ptr<DeclWithType>> params;
135 std::vector<ptr<DeclWithType>> returns;
136 // declared type, derived from parameter declarations
137 ptr<FunctionType> type;
138 ptr<CompoundStmt> stmts;
139 std::vector< ptr<Expr> > withExprs;
140
141 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
142 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
143 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
144 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false);
145 // : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
146 // stmts( stmts ) {}
147
148 const Type * get_type() const override;
149 void set_type( const Type * t ) override;
150
151 bool has_body() const { return stmts; }
152
153 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
154private:
155 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
156 MUTATE_FRIEND
157};
158
159/// Base class for named type aliases
160class NamedTypeDecl : public Decl {
161public:
162 ptr<Type> base;
163 std::vector<ptr<TypeDecl>> params;
164 std::vector<ptr<DeclWithType>> assertions;
165
166 NamedTypeDecl(
167 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
168 const Type * b, Linkage::Spec spec = Linkage::Cforall )
169 : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
170
171 /// Produces a name for the kind of alias
172 virtual const char * typeString() const = 0;
173
174private:
175 NamedTypeDecl* clone() const override = 0;
176 MUTATE_FRIEND
177};
178
179/// Cforall type variable: `dtype T`
180class TypeDecl final : public NamedTypeDecl {
181 public:
182 enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
183
184 Kind kind;
185 bool sized;
186 ptr<Type> init;
187
188 /// Data extracted from a type decl
189 struct Data {
190 Kind kind;
191 bool isComplete;
192
193 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
194 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
195 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
196 Data( const Data & d1, const Data & d2 )
197 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
198
199 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
200 bool operator!=( const Data & o ) const { return !(*this == o); }
201 };
202
203 TypeDecl(
204 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
205 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
206 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
207 init( i ) {}
208
209 const char * typeString() const override;
210 /// Produces a name for generated code
211 const char * genTypeString() const;
212
213 /// convenience accessor to match Type::isComplete()
214 bool isComplete() { return sized; }
215
216 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
217 private:
218 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
219 MUTATE_FRIEND
220};
221
222std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
223
224/// C-style typedef `typedef Foo Bar`
225class TypedefDecl final : public NamedTypeDecl {
226public:
227 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
228 Type* b, Linkage::Spec spec = Linkage::Cforall )
229 : NamedTypeDecl( loc, name, storage, b, spec ) {}
230
231 const char * typeString() const override { return "typedef"; }
232
233 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
234private:
235 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
236 MUTATE_FRIEND
237};
238
239/// Aggregate type declaration base class
240class AggregateDecl : public Decl {
241public:
242 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
243 static const char * aggrString( Aggregate aggr );
244
245 std::vector<ptr<Decl>> members;
246 std::vector<ptr<TypeDecl>> params;
247 std::vector<ptr<Attribute>> attributes;
248 bool body = false;
249 readonly<AggregateDecl> parent = {};
250
251 AggregateDecl( const CodeLocation& loc, const std::string& name,
252 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
253 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
254 attributes( std::move(attrs) ) {}
255
256 AggregateDecl* set_body( bool b ) { body = b; return this; }
257
258 /// Produces a name for the kind of aggregate
259 virtual const char * typeString() const = 0;
260
261private:
262 AggregateDecl * clone() const override = 0;
263 MUTATE_FRIEND
264};
265
266/// struct declaration `struct Foo { ... };`
267class StructDecl final : public AggregateDecl {
268public:
269 Aggregate kind;
270
271 StructDecl( const CodeLocation& loc, const std::string& name,
272 Aggregate kind = Struct,
273 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
274 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
275
276 bool is_coroutine() { return kind == Coroutine; }
277 bool is_generator() { return kind == Generator; }
278 bool is_monitor () { return kind == Monitor ; }
279 bool is_thread () { return kind == Thread ; }
280
281 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
282
283 const char * typeString() const override { return aggrString( kind ); }
284
285private:
286 StructDecl * clone() const override { return new StructDecl{ *this }; }
287 MUTATE_FRIEND
288};
289
290/// union declaration `union Foo { ... };`
291class UnionDecl final : public AggregateDecl {
292public:
293 UnionDecl( const CodeLocation& loc, const std::string& name,
294 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
295 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
296
297 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
298
299 const char * typeString() const override { return aggrString( Union ); }
300
301private:
302 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
303 MUTATE_FRIEND
304};
305
306/// enum declaration `enum Foo { ... };`
307class EnumDecl final : public AggregateDecl {
308public:
309 EnumDecl( const CodeLocation& loc, const std::string& name,
310 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
311 : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
312
313 /// gets the integer value for this enumerator, returning true iff value found
314 bool valueOf( const Decl * enumerator, long long& value ) const;
315
316 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
317
318 const char * typeString() const override { return aggrString( Enum ); }
319
320private:
321 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
322 MUTATE_FRIEND
323
324 /// Map from names to enumerator values; kept private for lazy initialization
325 mutable std::unordered_map< std::string, long long > enumValues;
326};
327
328/// trait declaration `trait Foo( ... ) { ... };`
329class TraitDecl final : public AggregateDecl {
330public:
331 TraitDecl( const CodeLocation& loc, const std::string& name,
332 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
333 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
334
335 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
336
337 const char * typeString() const override { return "trait"; }
338
339private:
340 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
341 MUTATE_FRIEND
342};
343
344/// With statement `with (...) ...`
345class WithStmt final : public Decl {
346public:
347 std::vector<ptr<Expr>> exprs;
348 ptr<Stmt> stmt;
349
350 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
351 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
352
353 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
354private:
355 WithStmt * clone() const override { return new WithStmt{ *this }; }
356 MUTATE_FRIEND
357};
358
359class AsmDecl : public Decl {
360public:
361 ptr<AsmStmt> stmt;
362
363 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
364 : Decl( loc, "", {}, {} ), stmt(stmt) {}
365
366 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
367private:
368 AsmDecl * clone() const override { return new AsmDecl( *this ); }
369 MUTATE_FRIEND
370};
371
372class StaticAssertDecl : public Decl {
373public:
374 ptr<Expr> cond;
375 ptr<ConstantExpr> msg; // string literal
376
377 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
378 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
379
380 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
381private:
382 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
383 MUTATE_FRIEND
384};
385
386}
387
388#undef MUTATE_FRIEND
389
390// Local Variables: //
391// tab-width: 4 //
392// mode: c++ //
393// compile-command: "make install" //
394// End: //
Note: See TracBrowser for help on using the repository browser.