source: src/AST/Decl.hpp@ f90d10f

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 f90d10f was 427854b, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

First draft implementation of generators, still missing error checking, testing and clean-up

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