source: src/AST/Decl.hpp@ 0e315a5

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 0e315a5 was d76c588, checked in by Aaron Moss <a3moss@…>, 7 years ago

Stubs for new resolver, implementation of new indexer, type environment

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