source: src/AST/Decl.hpp@ 2c04369

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 2c04369 was 9d6e7fa9, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed missing or incorrect stubs in decl/InitTweak

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