source: src/AST/Decl.hpp@ 417117e

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 417117e was e0e9a0b, checked in by Aaron Moss <a3moss@…>, 6 years ago

Somewhat deeper clone for types with forall qualifiers.

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