source: src/AST/Decl.hpp @ 23f99e1

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 23f99e1 was 23f99e1, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Finished implementing declarations

  • Property mode set to 100644
File size: 15.2 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 "Type.hpp"            // for Type, ptr<Type>
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        const Decl * accept( Visitor & v ) const override = 0;
60private:
61        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        const DeclWithType * accept( Visitor & v ) const override = 0;
92private:
93        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        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
113private:
114        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);
119};
120
121class FunctionDecl : public DeclWithType {
122public:
123        ptr<FunctionType> type;
124        ptr<CompoundStmt> stmts;
125        std::list< ptr<Expr> > withExprs;
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
133        const Type * get_type() const override { return type.get(); }
134        void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
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 ); }
141
142        /// Must be copied in ALL derived classes
143        template<typename node_t>
144        friend auto mutate(const node_t * node);
145};
146
147/// Base class for named type aliases
148class NamedTypeDecl : public Decl {
149public:
150        ptr<Type> base;
151        std::vector<ptr<TypeDecl>> parameters;
152        std::vector<ptr<DeclWithType>> assertions;
153
154        NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
155                Type* b, Linkage::Spec spec = Linkage::Cforall )
156        : Decl( loc, name, storage, spec ), base( b ), parameters(), assertions() {}
157
158        /// Produces a name for the kind of alias
159        virtual std::string typeString() const = 0;
160
161private:
162        NamedTypeDecl* clone() const override = 0;
163};
164
165/// Cforall type variable: `dtype T`
166class TypeDecl final : public NamedTypeDecl {
167public:
168        /// type variable variants. otype is a specialized dtype
169        enum Kind { Dtype, Ftype, Ttype, NUMBER_OF_KINDS } kind;
170        bool sized;
171        ptr<Type> init;
172
173        /// Data extracted from a type decl
174        struct Data {
175                Kind kind;
176                bool isComplete;
177
178                Data() : kind( (Kind)-1 ), isComplete( false ) {}
179                Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {}
180                Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
181                Data( const Data& d1, const Data& d2 )
182                : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
183
184                bool operator== ( const Data& o ) const {
185                        return kind == o.kind && isComplete == o.isComplete;
186                }
187                bool operator!= ( const Data& o ) const { return !(*this == o); }
188        };
189
190        TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b,
191                Kind k, bool s, Type* i = nullptr )
192        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ), init( i ) {}
193
194        std::string typeString() const override;
195        /// Produces a name for generated code
196        std::string genTypeString() const;
197
198        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
199private:
200        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
201
202        /// Must be copied in ALL derived classes
203        template<typename node_t>
204        friend auto mutate(const node_t * node);
205};
206
207/// C-style typedef `typedef Foo Bar`
208class TypedefDecl final : public NamedTypeDecl {
209public:
210        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
211                Type* b, Linkage::Spec spec = Linkage::Cforall )
212        : NamedTypeDecl( loc, name, storage, b, spec ) {}
213
214        std::string typeString() const override { return "typedef"; }
215
216        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
217private:
218        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
219
220        /// Must be copied in ALL derived classes
221        template<typename node_t>
222        friend auto mutate(const node_t * node);
223};
224
225/// Aggregate type declaration base class
226class AggregateDecl : public Decl {
227public:
228        std::vector<ptr<Decl>> members;
229        std::vector<ptr<TypeDecl>> parameters;
230        std::vector<ptr<Attribute>> attributes;
231        bool body = false;
232        readonly<AggregateDecl> parent = {};
233
234        AggregateDecl( const CodeLocation& loc, const std::string& name,
235                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
236        : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(),
237          attributes( std::move(attrs) ) {}
238
239        AggregateDecl* set_body( bool b ) { body = b; return this; }
240
241protected:
242        /// Produces a name for the kind of aggregate
243        virtual std::string typeString() const = 0;
244};
245
246/// struct declaration `struct Foo { ... };`
247class StructDecl final : public AggregateDecl {
248public:
249        DeclarationNode::Aggregate kind;
250
251        StructDecl( const CodeLocation& loc, const std::string& name,
252                DeclarationNode::Aggregate kind = DeclarationNode::Struct,
253                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
254        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
255
256        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
257        bool is_monitor() { return kind == DeclarationNode::Monitor; }
258        bool is_thread() { return kind == DeclarationNode::Thread; }
259
260        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
261private:
262        StructDecl * clone() const override { return new StructDecl{ *this }; }
263
264        /// Must be copied in ALL derived classes
265        template<typename node_t>
266        friend auto mutate(const node_t * node);
267
268        std::string typeString() const override { return "struct"; }
269};
270
271/// union declaration `union Foo { ... };`
272class UnionDecl final : public AggregateDecl {
273public:
274        UnionDecl( const CodeLocation& loc, const std::string& name,
275                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
276        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
277
278        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
279private:
280        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
281
282        /// Must be copied in ALL derived classes
283        template<typename node_t>
284        friend auto mutate(const node_t * node);
285
286        std::string typeString() const override { return "union"; }
287};
288
289/// enum declaration `enum Foo { ... };`
290class EnumDecl final : public AggregateDecl {
291public:
292        EnumDecl( const CodeLocation& loc, const std::string& name,
293                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
294        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
295
296        /// gets the integer value for this enumerator, returning true iff value found
297        bool valueOf( Decl* enumerator, long long& value ) const;
298
299        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
300private:
301        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
302
303        /// Must be copied in ALL derived classes
304        template<typename node_t>
305        friend auto mutate(const node_t * node);
306
307        std::string typeString() const override { return "enum"; }
308
309        /// Map from names to enumerator values; kept private for lazy initialization
310        mutable std::unordered_map< std::string, long long > enumValues;
311};
312
313/// trait declaration `trait Foo( ... ) { ... };`
314class TraitDecl final : public AggregateDecl {
315public:
316        TraitDecl( const CodeLocation& loc, const std::string& name,
317                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
318        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
319
320        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
321private:
322        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
323
324        /// Must be copied in ALL derived classes
325        template<typename node_t>
326        friend auto mutate(const node_t * node);
327
328        std::string typeString() const override { return "trait"; }
329};
330
331class AsmDecl : public Decl {
332public:
333        ptr<AsmStmt> stmt;
334
335        AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
336        : Decl( loc, "", {}, {} ), stmt(stmt) {}
337
338        const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
339private:
340        AsmDecl *clone() const override { return new AsmDecl( *this ); }
341
342        /// Must be copied in ALL derived classes
343        template<typename node_t>
344        friend auto mutate(const node_t * node);
345};
346
347class StaticAssertDecl : public Decl {
348public:
349        ptr<Expr> condition;
350        ptr<ConstantExpr> msg;   // string literal
351
352        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
353        : Decl( loc, "", {}, {} ), condition( condition ), msg( msg ) {}
354
355        const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
356private:
357        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
358
359        /// Must be copied in ALL derived classes
360        template<typename node_t>
361        friend auto mutate(const node_t * node);
362};
363
364//=================================================================================================
365/// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency
366/// remove only if there is a better solution
367/// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with
368/// forward declarations
369inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); }
370inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); }
371inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); }
372inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); }
373inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); }
374inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); }
375inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); }
376inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
377inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); }
378inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); }
379inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); }
380inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); }
381inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); }
382inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
383inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); }
384inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); }
385inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); }
386inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); }
387inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
388inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
389inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
390inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
391inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); }
392inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); }
393inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); }
394inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); }
395inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); }
396inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); }
397
398}
399
400// Local Variables: //
401// tab-width: 4 //
402// mode: c++ //
403// compile-command: "make install" //
404// End: //
Note: See TracBrowser for help on using the repository browser.