source: src/AST/Decl.hpp@ f90d10f

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f90d10f was 427854b, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

First draft implementation of generators, still missing error checking, testing and clean-up

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