source: src/AST/Decl.hpp@ b2a11ba

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 b2a11ba was e3bc51c, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed bad merge

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