source: src/AST/Decl.hpp@ 54db6ba

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 stuck-waitfor-destruct
Last change on this file since 54db6ba was 6d51bd7, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Fixes to the new templated pass and started on conversions

  • Property mode set to 100644
File size: 13.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
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"
[a300e4a]28#include "Type.hpp" // for Type, ptr<Type>
[2bb4a01]29#include "Visitor.hpp"
[a300e4a]30#include "Parser/ParseNode.h" // for DeclarationNode::Aggregate
[2bb4a01]31
32namespace ast {
[a300e4a]33
[2bb4a01]34class Attribute;
35class Expr;
[77a3f41]36class Init;
[a300e4a]37class TypeDecl;
[2bb4a01]38
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
[6d51bd7]59 virtual const Decl * accept( Visitor & v ) const override = 0;
[2bb4a01]60private:
[6d51bd7]61 virtual Decl * clone() const override = 0;
[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
89 virtual void set_type(Type*) = 0;
90
[6d51bd7]91 virtual const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]92private:
[6d51bd7]93 virtual DeclWithType * clone() const override = 0;
[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
103 ObjectDecl( const CodeLocation& loc, const std::string& name, Type* type, Init* init = nullptr,
[14cebb7a]104 Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, Expr* bitWd = nullptr,
[77a3f41]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; }
110 void set_type( Type* ty ) override { type = ty; }
111
[6d51bd7]112 virtual const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]113private:
[6d51bd7]114 virtual ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
115
116 /// Must be copied in ALL derived classes
117 template<typename node_t>
118 friend auto mutate(const node_t * node);
[77a3f41]119};
120
[360b2e13]121/// Base class for named type aliases
122class NamedTypeDecl : public Decl {
123public:
124 ptr<Type> base;
125 std::vector<ptr<TypeDecl>> parameters;
126 std::vector<ptr<DeclWithType>> assertions;
127
[e0115286]128 NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]129 Type* b, Linkage::Spec spec = Linkage::Cforall )
130 : Decl( loc, name, storage, spec ), base( b ), parameters(), assertions() {}
131
132 /// Produces a name for the kind of alias
133 virtual std::string typeString() const = 0;
134
135private:
136 NamedTypeDecl* clone() const override = 0;
137};
138
139/// Cforall type variable: `dtype T`
140class TypeDecl final : public NamedTypeDecl {
141public:
142 /// type variable variants. otype is a specialized dtype
143 enum Kind { Dtype, Ftype, Ttype, NUMBER_OF_KINDS } kind;
144 bool sized;
145 ptr<Type> init;
146
147 /// Data extracted from a type decl
148 struct Data {
149 Kind kind;
150 bool isComplete;
151
152 Data() : kind( (Kind)-1 ), isComplete( false ) {}
153 Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {}
154 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
[e0115286]155 Data( const Data& d1, const Data& d2 )
[360b2e13]156 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
157
158 bool operator== ( const Data& o ) const {
159 return kind == o.kind && isComplete == o.isComplete;
160 }
161 bool operator!= ( const Data& o ) const { return !(*this == o); }
162 };
163
[e0115286]164 TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b,
[360b2e13]165 Kind k, bool s, Type* i = nullptr )
166 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ), init( i ) {}
167
168 std::string typeString() const override;
169 /// Produces a name for generated code
170 std::string genTypeString() const;
171
[6d51bd7]172 virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]173private:
[6d51bd7]174 virtual TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[360b2e13]175};
176
177/// C-style typedef `typedef Foo Bar`
178class TypedefDecl final : public NamedTypeDecl {
179public:
[e0115286]180 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]181 Type* b, Linkage::Spec spec = Linkage::Cforall )
182 : NamedTypeDecl( loc, name, storage, b, spec ) {}
183
184 std::string typeString() const override { return "typedef"; }
185
[6d51bd7]186 virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]187private:
[6d51bd7]188 virtual TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[360b2e13]189};
190
[a300e4a]191/// Aggregate type declaration base class
192class AggregateDecl : public Decl {
193public:
194 std::vector<ptr<Decl>> members;
195 std::vector<ptr<TypeDecl>> parameters;
196 std::vector<ptr<Attribute>> attributes;
197 bool body = false;
198 readonly<AggregateDecl> parent = {};
199
[14cebb7a]200 AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]201 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[14cebb7a]202 : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(),
[a300e4a]203 attributes( std::move(attrs) ) {}
[14cebb7a]204
[a300e4a]205 AggregateDecl* set_body( bool b ) { body = b; return this; }
206
207protected:
208 /// Produces a name for the kind of aggregate
209 virtual std::string typeString() const = 0;
210};
211
212/// struct declaration `struct Foo { ... };`
213class StructDecl final : public AggregateDecl {
214public:
215 DeclarationNode::Aggregate kind;
216
[14cebb7a]217 StructDecl( const CodeLocation& loc, const std::string& name,
218 DeclarationNode::Aggregate kind = DeclarationNode::Struct,
[a300e4a]219 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
220 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
221
222 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
223 bool is_monitor() { return kind == DeclarationNode::Monitor; }
224 bool is_thread() { return kind == DeclarationNode::Thread; }
225
[6d51bd7]226 virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[a300e4a]227private:
[6d51bd7]228 virtual StructDecl * clone() const override { return new StructDecl{ *this }; }
[a300e4a]229
230 std::string typeString() const override { return "struct"; }
231};
232
233/// union declaration `union Foo { ... };`
234class UnionDecl final : public AggregateDecl {
235public:
[14cebb7a]236 UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]237 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
238 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
239
[6d51bd7]240 virtual const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[a300e4a]241private:
[6d51bd7]242 virtual UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[a300e4a]243
244 std::string typeString() const override { return "union"; }
245};
246
247/// enum declaration `enum Foo { ... };`
248class EnumDecl final : public AggregateDecl {
249public:
[14cebb7a]250 EnumDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]251 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
252 : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
253
254 /// gets the integer value for this enumerator, returning true iff value found
255 bool valueOf( Decl* enumerator, long long& value ) const;
256
[6d51bd7]257 virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[a300e4a]258private:
[6d51bd7]259 virtual EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[a300e4a]260
261 std::string typeString() const override { return "enum"; }
262
263 /// Map from names to enumerator values; kept private for lazy initialization
264 mutable std::unordered_map< std::string, long long > enumValues;
265};
266
267/// trait declaration `trait Foo( ... ) { ... };`
268class TraitDecl final : public AggregateDecl {
269public:
[14cebb7a]270 TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]271 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
272 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
273
[6d51bd7]274 virtual const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[a300e4a]275private:
[6d51bd7]276 virtual TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[a300e4a]277
278 std::string typeString() const override { return "trait"; }
[2bb4a01]279};
280
[e0115286]281
282//=================================================================================================
283/// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency
284/// remove only if there is a better solution
285/// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with
286/// forward declarations
287inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); }
288inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); }
289inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); }
290inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); }
291inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); }
292inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[6d51bd7]293// inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); }
294// inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[e0115286]295inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); }
296inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); }
297inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); }
298inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); }
299inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); }
300inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
301inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); }
302inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); }
303inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); }
304inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); }
305inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
306inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
307inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
308inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[6d51bd7]309// inline void increment( const class FtypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
310// inline void decrement( const class FtypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
311// inline void increment( const class DtypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
312// inline void decrement( const class DtypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[e0115286]313inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); }
314inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[6d51bd7]315// inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); }
316// inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); }
317// inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); }
318// inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[e0115286]319
[2bb4a01]320}
321
322// Local Variables: //
323// tab-width: 4 //
324// mode: c++ //
325// compile-command: "make install" //
326// End: //
Note: See TracBrowser for help on using the repository browser.