source: src/AST/Decl.hpp @ 24afc53

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

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 15.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"
[9e1d485]28#include "TypeVar.hpp"
[2bb4a01]29#include "Visitor.hpp"
[a300e4a]30#include "Parser/ParseNode.h"  // for DeclarationNode::Aggregate
[2bb4a01]31
32namespace ast {
[a300e4a]33
[2bb4a01]34/// Base declaration class
35class Decl : public ParseNode {
36public:
37        std::string name;
38        Storage::Classes storage;
39        Linkage::Spec linkage;
40        UniqueId uniqueId = 0;
41        bool extension = false;
42
[14cebb7a]43        Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]44                Linkage::Spec linkage )
45        : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
46
47        Decl* set_extension( bool ex ) { extension = ex; return this; }
48
49        /// Ensures this node has a unique ID
50        void fixUniqueId();
51        /// Get canonical declaration for unique ID
52        static readonly<Decl> fromId( UniqueId id );
53
[23f99e1]54        const Decl * accept( Visitor & v ) const override = 0;
[2bb4a01]55private:
[23f99e1]56        Decl * clone() const override = 0;
[2bb4a01]57};
58
59/// Typed declaration base class
60class DeclWithType : public Decl {
61public:
62        /// Represents the type with all types and typedefs expanded.
63        /// This field is generated by SymTab::Validate::Pass2
64        std::string mangleName;
[14cebb7a]65        /// Stores the scope level at which the variable was declared.
[2bb4a01]66        /// Used to access shadowed identifiers.
67        int scopeLevel = 0;
68
69        std::vector<ptr<Attribute>> attributes;
[a300e4a]70        Function::Specs funcSpec;
[2bb4a01]71        ptr<Expr> asmName;
72        bool isDeleted = false;
73
[14cebb7a]74        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]75                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
[14cebb7a]76        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
[a300e4a]77                funcSpec(fs), asmName() {}
[14cebb7a]78
[a300e4a]79        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
80
81        /// Get type of this declaration. May be generated by subclass
[6d51bd7]82        virtual const Type * get_type() const = 0;
[a300e4a]83        /// Set type of this declaration. May be verified by subclass
[23f99e1]84        virtual void set_type(Type *) = 0;
[a300e4a]85
[23f99e1]86        const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]87private:
[23f99e1]88        DeclWithType * clone() const override = 0;
[a300e4a]89};
90
[77a3f41]91/// Object declaration `Foo foo = 42;`
92class ObjectDecl final : public DeclWithType {
93public:
94        ptr<Type> type;
95        ptr<Init> init;
96        ptr<Expr> bitfieldWidth;
97
98        ObjectDecl( const CodeLocation& loc, const std::string& name, Type* type, Init* init = nullptr,
[14cebb7a]99                Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, Expr* bitWd = nullptr,
[77a3f41]100                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
[14cebb7a]101        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
[77a3f41]102          init( init ), bitfieldWidth( bitWd ) {}
[14cebb7a]103
[77a3f41]104        const Type* get_type() const override { return type; }
[23f99e1]105        void set_type( Type * ty ) override { type = ty; }
[77a3f41]106
[23f99e1]107        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]108private:
[23f99e1]109        ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
110
111        /// Must be copied in ALL derived classes
112        template<typename node_t>
113        friend auto mutate(const node_t * node);
114};
115
[8a5530c]116/// Object declaration `int foo()`
[23f99e1]117class FunctionDecl : public DeclWithType {
118public:
119        ptr<FunctionType> type;
120        ptr<CompoundStmt> stmts;
121        std::list< ptr<Expr> > withExprs;
122
123        FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type,
124                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
125                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
126        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
127          stmts( stmts ) {}
128
[8a5530c]129        const Type * get_type() const override;
130        void set_type(Type * t) override;
[23f99e1]131
132        bool has_body() const { return stmts; }
133
134        const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); }
135private:
136        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[6d51bd7]137
138        /// Must be copied in ALL derived classes
139        template<typename node_t>
140        friend auto mutate(const node_t * node);
[77a3f41]141};
142
[360b2e13]143/// Base class for named type aliases
144class NamedTypeDecl : public Decl {
145public:
146        ptr<Type> base;
[54e41b3]147        std::vector<ptr<TypeDecl>> params;
[360b2e13]148        std::vector<ptr<DeclWithType>> assertions;
149
[e0115286]150        NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]151                Type* b, Linkage::Spec spec = Linkage::Cforall )
[54e41b3]152        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
[360b2e13]153
154        /// Produces a name for the kind of alias
155        virtual std::string typeString() const = 0;
156
157private:
158        NamedTypeDecl* clone() const override = 0;
159};
160
161/// Cforall type variable: `dtype T`
162class TypeDecl final : public NamedTypeDecl {
163public:
[9e1d485]164        TypeVar::Kind kind;
[360b2e13]165        bool sized;
166        ptr<Type> init;
167
168        /// Data extracted from a type decl
169        struct Data {
[9e1d485]170                TypeVar::Kind kind;
[360b2e13]171                bool isComplete;
172
[9e1d485]173                Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {}
[360b2e13]174                Data( TypeDecl* d ) : kind( d->kind ), isComplete( d->sized ) {}
[9e1d485]175                Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {}
[e0115286]176                Data( const Data& d1, const Data& d2 )
[360b2e13]177                : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
178
179                bool operator== ( const Data& o ) const {
180                        return kind == o.kind && isComplete == o.isComplete;
181                }
182                bool operator!= ( const Data& o ) const { return !(*this == o); }
183        };
184
[e0115286]185        TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b,
[9e1d485]186                TypeVar::Kind k, bool s, Type* i = nullptr )
[8a5530c]187        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ),
[9e1d485]188          init( i ) {}
[360b2e13]189
190        std::string typeString() const override;
191        /// Produces a name for generated code
192        std::string genTypeString() const;
193
[9e1d485]194        /// convenience accessor to match Type::isComplete()
195        bool isComplete() { return sized; }
196
[23f99e1]197        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]198private:
[23f99e1]199        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
200
201        /// Must be copied in ALL derived classes
202        template<typename node_t>
203        friend auto mutate(const node_t * node);
[360b2e13]204};
205
206/// C-style typedef `typedef Foo Bar`
207class TypedefDecl final : public NamedTypeDecl {
208public:
[e0115286]209        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]210                Type* b, Linkage::Spec spec = Linkage::Cforall )
211        : NamedTypeDecl( loc, name, storage, b, spec ) {}
212
213        std::string typeString() const override { return "typedef"; }
214
[23f99e1]215        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]216private:
[23f99e1]217        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
218
219        /// Must be copied in ALL derived classes
220        template<typename node_t>
221        friend auto mutate(const node_t * node);
[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
240protected:
241        /// Produces a name for the kind of aggregate
242        virtual std::string typeString() const = 0;
243};
244
245/// struct declaration `struct Foo { ... };`
246class StructDecl final : public AggregateDecl {
247public:
248        DeclarationNode::Aggregate kind;
249
[14cebb7a]250        StructDecl( const CodeLocation& loc, const std::string& name,
251                DeclarationNode::Aggregate kind = DeclarationNode::Struct,
[a300e4a]252                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
253        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
254
255        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
256        bool is_monitor() { return kind == DeclarationNode::Monitor; }
257        bool is_thread() { return kind == DeclarationNode::Thread; }
258
[23f99e1]259        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[a300e4a]260private:
[23f99e1]261        StructDecl * clone() const override { return new StructDecl{ *this }; }
262
263        /// Must be copied in ALL derived classes
264        template<typename node_t>
265        friend auto mutate(const node_t * node);
[a300e4a]266
267        std::string typeString() const override { return "struct"; }
268};
269
270/// union declaration `union Foo { ... };`
271class UnionDecl final : public AggregateDecl {
272public:
[14cebb7a]273        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]274                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
275        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
276
[23f99e1]277        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[a300e4a]278private:
[23f99e1]279        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
280
281        /// Must be copied in ALL derived classes
282        template<typename node_t>
283        friend auto mutate(const node_t * node);
[a300e4a]284
285        std::string typeString() const override { return "union"; }
286};
287
288/// enum declaration `enum Foo { ... };`
289class EnumDecl final : public AggregateDecl {
290public:
[14cebb7a]291        EnumDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]292                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
293        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
294
295        /// gets the integer value for this enumerator, returning true iff value found
296        bool valueOf( Decl* enumerator, long long& value ) const;
297
[23f99e1]298        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[a300e4a]299private:
[23f99e1]300        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
301
302        /// Must be copied in ALL derived classes
303        template<typename node_t>
304        friend auto mutate(const node_t * node);
[a300e4a]305
306        std::string typeString() const override { return "enum"; }
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 ); }
[a300e4a]320private:
[23f99e1]321        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
322
323        /// Must be copied in ALL derived classes
324        template<typename node_t>
325        friend auto mutate(const node_t * node);
[a300e4a]326
327        std::string typeString() const override { return "trait"; }
[2bb4a01]328};
329
[23f99e1]330class AsmDecl : public Decl {
331public:
332        ptr<AsmStmt> stmt;
333
334        AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
335        : Decl( loc, "", {}, {} ), stmt(stmt) {}
336
337        const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
338private:
339        AsmDecl *clone() const override { return new AsmDecl( *this ); }
340
341        /// Must be copied in ALL derived classes
342        template<typename node_t>
343        friend auto mutate(const node_t * node);
344};
345
346class StaticAssertDecl : public Decl {
347public:
348        ptr<Expr> condition;
349        ptr<ConstantExpr> msg;   // string literal
350
351        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
352        : Decl( loc, "", {}, {} ), condition( condition ), msg( msg ) {}
353
354        const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
355private:
356        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
357
358        /// Must be copied in ALL derived classes
359        template<typename node_t>
360        friend auto mutate(const node_t * node);
361};
[e0115286]362
363//=================================================================================================
364/// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency
365/// remove only if there is a better solution
366/// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with
367/// forward declarations
368inline void increment( const class Decl * node, Node::ref_type ref ) { node->increment(ref); }
369inline void decrement( const class Decl * node, Node::ref_type ref ) { node->decrement(ref); }
370inline void increment( const class DeclWithType * node, Node::ref_type ref ) { node->increment(ref); }
371inline void decrement( const class DeclWithType * node, Node::ref_type ref ) { node->decrement(ref); }
372inline void increment( const class ObjectDecl * node, Node::ref_type ref ) { node->increment(ref); }
373inline void decrement( const class ObjectDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[23f99e1]374inline void increment( const class FunctionDecl * node, Node::ref_type ref ) { node->increment(ref); }
375inline void decrement( const class FunctionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[e0115286]376inline void increment( const class AggregateDecl * node, Node::ref_type ref ) { node->increment(ref); }
377inline void decrement( const class AggregateDecl * node, Node::ref_type ref ) { node->decrement(ref); }
378inline void increment( const class StructDecl * node, Node::ref_type ref ) { node->increment(ref); }
379inline void decrement( const class StructDecl * node, Node::ref_type ref ) { node->decrement(ref); }
380inline void increment( const class UnionDecl * node, Node::ref_type ref ) { node->increment(ref); }
381inline void decrement( const class UnionDecl * node, Node::ref_type ref ) { node->decrement(ref); }
382inline void increment( const class EnumDecl * node, Node::ref_type ref ) { node->increment(ref); }
383inline void decrement( const class EnumDecl * node, Node::ref_type ref ) { node->decrement(ref); }
384inline void increment( const class TraitDecl * node, Node::ref_type ref ) { node->increment(ref); }
385inline void decrement( const class TraitDecl * node, Node::ref_type ref ) { node->decrement(ref); }
386inline void increment( const class NamedTypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
387inline void decrement( const class NamedTypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
388inline void increment( const class TypeDecl * node, Node::ref_type ref ) { node->increment(ref); }
389inline void decrement( const class TypeDecl * node, Node::ref_type ref ) { node->decrement(ref); }
390inline void increment( const class TypedefDecl * node, Node::ref_type ref ) { node->increment(ref); }
391inline void decrement( const class TypedefDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[23f99e1]392inline void increment( const class AsmDecl * node, Node::ref_type ref ) { node->increment(ref); }
393inline void decrement( const class AsmDecl * node, Node::ref_type ref ) { node->decrement(ref); }
394inline void increment( const class StaticAssertDecl * node, Node::ref_type ref ) { node->increment(ref); }
395inline void decrement( const class StaticAssertDecl * node, Node::ref_type ref ) { node->decrement(ref); }
[e0115286]396
[2bb4a01]397}
398
399// Local Variables: //
400// tab-width: 4 //
401// mode: c++ //
402// compile-command: "make install" //
403// End: //
Note: See TracBrowser for help on using the repository browser.