source: src/AST/Decl.hpp@ 8ba363e

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 8ba363e was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 5 years ago

reimplement function type and eliminate deep copy

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