source: src/AST/Decl.hpp @ 9e1d485

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9e1d485 was 9e1d485, checked in by Aaron Moss <a3moss@…>, 5 years ago

First draft of ast::Type with subclasses

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