source: src/AST/Decl.hpp@ 98168b9

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

factor out resolver calls in pre-resolution stage

  • 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 std::vector<ptr<Attribute>> attributes;
78 Function::Specs funcSpec;
79 ptr<Expr> asmName;
80 bool isDeleted = false;
81 bool isTypeFixed = false;
82
83 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
84 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
85 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
86 funcSpec(fs), asmName() {}
87
88 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
89
90 /// Get type of this declaration. May be generated by subclass
91 virtual const Type * get_type() const = 0;
92 /// Set type of this declaration. May be verified by subclass
93 virtual void set_type( const Type * ) = 0;
94
95 const DeclWithType * accept( Visitor & v ) const override = 0;
96private:
97 DeclWithType * clone() const override = 0;
98 MUTATE_FRIEND
99};
100
101/// Object declaration `Foo foo = 42;`
102class ObjectDecl final : public DeclWithType {
103public:
104 ptr<Type> type;
105 ptr<Init> init;
106 ptr<Expr> bitfieldWidth;
107
108 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
109 const Init * init = nullptr, Storage::Classes storage = {},
110 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr,
111 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
112 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
113 init( init ), bitfieldWidth( bitWd ) {}
114
115 const Type* get_type() const override { return type; }
116 void set_type( const Type * ty ) override { type = ty; }
117
118 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
119private:
120 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
121 MUTATE_FRIEND
122};
123
124/// Object declaration `int foo()`
125class FunctionDecl : public DeclWithType {
126public:
127 std::vector<ptr<DeclWithType>> params;
128 std::vector<ptr<DeclWithType>> returns;
129 // declared type, derived from parameter declarations
130 ptr<FunctionType> type;
131 ptr<CompoundStmt> stmts;
132 std::vector< ptr<Expr> > withExprs;
133
134 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
135 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
136 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
137 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false);
138 // : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
139 // stmts( stmts ) {}
140
141 const Type * get_type() const override;
142 void set_type( const Type * t ) override;
143
144 bool has_body() const { return stmts; }
145
146 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
147private:
148 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
149 MUTATE_FRIEND
150};
151
152/// Base class for named type aliases
153class NamedTypeDecl : public Decl {
154public:
155 ptr<Type> base;
156 std::vector<ptr<TypeDecl>> params;
157 std::vector<ptr<DeclWithType>> assertions;
158
159 NamedTypeDecl(
160 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
161 const Type * b, Linkage::Spec spec = Linkage::Cforall )
162 : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
163
164 /// Produces a name for the kind of alias
165 virtual const char * typeString() const = 0;
166
167private:
168 NamedTypeDecl* clone() const override = 0;
169 MUTATE_FRIEND
170};
171
172/// Cforall type variable: `dtype T`
173class TypeDecl final : public NamedTypeDecl {
174 public:
175 enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
176
177 Kind kind;
178 bool sized;
179 ptr<Type> init;
180
181 /// Data extracted from a type decl
182 struct Data {
183 Kind kind;
184 bool isComplete;
185
186 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
187 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
188 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
189 Data( const Data & d1, const Data & d2 )
190 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
191
192 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
193 bool operator!=( const Data & o ) const { return !(*this == o); }
194 };
195
196 TypeDecl(
197 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
198 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
199 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
200 init( i ) {}
201
202 const char * typeString() const override;
203 /// Produces a name for generated code
204 const char * genTypeString() const;
205
206 /// convenience accessor to match Type::isComplete()
207 bool isComplete() { return sized; }
208
209 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
210 private:
211 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
212 MUTATE_FRIEND
213};
214
215std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
216
217/// C-style typedef `typedef Foo Bar`
218class TypedefDecl final : public NamedTypeDecl {
219public:
220 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
221 Type* b, Linkage::Spec spec = Linkage::Cforall )
222 : NamedTypeDecl( loc, name, storage, b, spec ) {}
223
224 const char * typeString() const override { return "typedef"; }
225
226 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
227private:
228 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
229 MUTATE_FRIEND
230};
231
232/// Aggregate type declaration base class
233class AggregateDecl : public Decl {
234public:
235 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
236 static const char * aggrString( Aggregate aggr );
237
238 std::vector<ptr<Decl>> members;
239 std::vector<ptr<TypeDecl>> params;
240 std::vector<ptr<Attribute>> attributes;
241 bool body = false;
242 readonly<AggregateDecl> parent = {};
243
244 AggregateDecl( const CodeLocation& loc, const std::string& name,
245 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
246 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
247 attributes( std::move(attrs) ) {}
248
249 AggregateDecl* set_body( bool b ) { body = b; return this; }
250
251 /// Produces a name for the kind of aggregate
252 virtual const char * typeString() const = 0;
253
254private:
255 AggregateDecl * clone() const override = 0;
256 MUTATE_FRIEND
257};
258
259/// struct declaration `struct Foo { ... };`
260class StructDecl final : public AggregateDecl {
261public:
262 Aggregate kind;
263
264 StructDecl( const CodeLocation& loc, const std::string& name,
265 Aggregate kind = Struct,
266 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
267 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
268
269 bool is_coroutine() { return kind == Coroutine; }
270 bool is_generator() { return kind == Generator; }
271 bool is_monitor () { return kind == Monitor ; }
272 bool is_thread () { return kind == Thread ; }
273
274 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
275
276 const char * typeString() const override { return aggrString( kind ); }
277
278private:
279 StructDecl * clone() const override { return new StructDecl{ *this }; }
280 MUTATE_FRIEND
281};
282
283/// union declaration `union Foo { ... };`
284class UnionDecl final : public AggregateDecl {
285public:
286 UnionDecl( const CodeLocation& loc, const std::string& name,
287 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
288 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
289
290 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
291
292 const char * typeString() const override { return aggrString( Union ); }
293
294private:
295 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
296 MUTATE_FRIEND
297};
298
299/// enum declaration `enum Foo { ... };`
300class EnumDecl final : public AggregateDecl {
301public:
302 EnumDecl( const CodeLocation& loc, const std::string& name,
303 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
304 : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
305
306 /// gets the integer value for this enumerator, returning true iff value found
307 bool valueOf( const Decl * enumerator, long long& value ) const;
308
309 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
310
311 const char * typeString() const override { return aggrString( Enum ); }
312
313private:
314 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
315 MUTATE_FRIEND
316
317 /// Map from names to enumerator values; kept private for lazy initialization
318 mutable std::unordered_map< std::string, long long > enumValues;
319};
320
321/// trait declaration `trait Foo( ... ) { ... };`
322class TraitDecl final : public AggregateDecl {
323public:
324 TraitDecl( const CodeLocation& loc, const std::string& name,
325 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
326 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
327
328 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
329
330 const char * typeString() const override { return "trait"; }
331
332private:
333 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
334 MUTATE_FRIEND
335};
336
337/// With statement `with (...) ...`
338class WithStmt final : public Decl {
339public:
340 std::vector<ptr<Expr>> exprs;
341 ptr<Stmt> stmt;
342
343 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
344 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
345
346 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
347private:
348 WithStmt * clone() const override { return new WithStmt{ *this }; }
349 MUTATE_FRIEND
350};
351
352class AsmDecl : public Decl {
353public:
354 ptr<AsmStmt> stmt;
355
356 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
357 : Decl( loc, "", {}, {} ), stmt(stmt) {}
358
359 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
360private:
361 AsmDecl * clone() const override { return new AsmDecl( *this ); }
362 MUTATE_FRIEND
363};
364
365class StaticAssertDecl : public Decl {
366public:
367 ptr<Expr> cond;
368 ptr<ConstantExpr> msg; // string literal
369
370 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
371 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
372
373 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
374private:
375 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
376 MUTATE_FRIEND
377};
378
379}
380
381#undef MUTATE_FRIEND
382
383// Local Variables: //
384// tab-width: 4 //
385// mode: c++ //
386// compile-command: "make install" //
387// End: //
Note: See TracBrowser for help on using the repository browser.