source: src/AST/Decl.hpp@ 90ce35aa

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 90ce35aa was 4eb43fa, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'new-ast' of plg.uwaterloo.ca:software/cfa/cfa-cc into new-ast

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