Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.hpp

    r427854b r99da267  
    99// Author           : Aaron B. Moss
    1010// Created On       : Thu May 9 10:00:00 2019
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec 13 17:38:33 2019
    13 // Update Count     : 29
     11// Last Modified By : Aaron B. Moss
     12// Last Modified On : Thu May 9 10:00:00 2019
     13// Update Count     : 1
    1414//
    1515
     
    2020#include <unordered_map>
    2121#include <vector>
    22 #include <algorithm>
    2322
    2423#include "FunctionSpec.hpp"
     
    2827#include "ParseNode.hpp"
    2928#include "StorageClasses.hpp"
     29#include "TypeVar.hpp"
    3030#include "Visitor.hpp"
    31 #include "Common/utility.h"
    32 #include "Common/SemanticError.h"                                               // error_str
     31#include "Parser/ParseNode.h"  // for DeclarationNode::Aggregate
    3332
    3433// Must be included in *all* AST classes; should be #undef'd at the end of the file
    35 #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
     34#define MUTATE_FRIEND \
     35    template<typename node_t> friend node_t * mutate(const node_t * node); \
     36        template<typename node_t> friend node_t * shallowCopy(const node_t * node);
    3637
    3738namespace ast {
     
    8889        virtual const Type * get_type() const = 0;
    8990        /// Set type of this declaration. May be verified by subclass
    90         virtual void set_type(Type *) = 0;
     91        virtual void set_type( const Type * ) = 0;
    9192
    9293        const DeclWithType * accept( Visitor & v ) const override = 0;
     
    103104        ptr<Expr> bitfieldWidth;
    104105
    105         ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
    106                 const Init * init = nullptr, Storage::Classes storage = {},
    107                 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr,
     106        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 
     107                const Init * init = nullptr, Storage::Classes storage = {}, 
     108                Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 
    108109                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
    109110        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
     
    111112
    112113        const Type* get_type() const override { return type; }
    113         void set_type( Type * ty ) override { type = ty; }
     114        void set_type( const Type * ty ) override { type = ty; }
    114115
    115116        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
     
    126127        std::vector< ptr<Expr> > withExprs;
    127128
    128         FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type,
     129        FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type,
    129130                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
    130131                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
     
    133134
    134135        const Type * get_type() const override;
    135         void set_type(Type * t) override;
     136        void set_type( const Type * t ) override;
    136137
    137138        bool has_body() const { return stmts; }
    138139
    139         const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
     140        const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); }
    140141private:
    141142        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
     
    150151        std::vector<ptr<DeclWithType>> assertions;
    151152
    152         NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
    153                 Type* b, Linkage::Spec spec = Linkage::Cforall )
     153        NamedTypeDecl(
     154                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
     155                const Type * b, Linkage::Spec spec = Linkage::Cforall )
    154156        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
    155157
    156158        /// Produces a name for the kind of alias
    157         virtual const char * typeString() const = 0;
     159        virtual std::string typeString() const = 0;
    158160
    159161private:
     
    164166/// Cforall type variable: `dtype T`
    165167class TypeDecl final : public NamedTypeDecl {
    166   public:
    167         enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
    168 
    169         Kind kind;
     168public:
     169        TypeVar::Kind kind;
    170170        bool sized;
    171171        ptr<Type> init;
     
    173173        /// Data extracted from a type decl
    174174        struct Data {
    175                 Kind kind;
     175                TypeVar::Kind kind;
    176176                bool isComplete;
    177177
    178                 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
     178                Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {}
    179179                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
    180                 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
     180                Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {}
    181181                Data( const Data & d1, const Data & d2 )
    182                         : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
    183 
    184                 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
    185                 bool operator!=( const Data & o ) const { return !(*this == o); }
     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); }
    186188        };
    187189
    188         TypeDecl( const CodeLocation & loc, const std::string & name, Storage::Classes storage, Type * b,
    189                           Kind k, bool s, Type * i = nullptr )
    190                 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ),
    191                 init( i ) {}
    192 
    193         const char * typeString() const override;
     190        TypeDecl(
     191                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
     192                const Type * b, TypeVar::Kind k, bool s, const Type * i = nullptr )
     193        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ),
     194          init( i ) {}
     195
     196        std::string typeString() const override;
    194197        /// Produces a name for generated code
    195         const char * genTypeString() const;
     198        std::string genTypeString() const;
    196199
    197200        /// convenience accessor to match Type::isComplete()
     
    199202
    200203        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    201   private:
     204private:
    202205        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
    203206        MUTATE_FRIEND
     
    213216        : NamedTypeDecl( loc, name, storage, b, spec ) {}
    214217
    215         const char * typeString() const override { return "typedef"; }
     218        std::string typeString() const override { return "typedef"; }
    216219
    217220        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     
    224227class AggregateDecl : public Decl {
    225228public:
    226         enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
    227         static const char * aggrString( Aggregate aggr );
    228 
    229229        std::vector<ptr<Decl>> members;
    230230        std::vector<ptr<TypeDecl>> params;
     
    241241
    242242        /// Produces a name for the kind of aggregate
    243         virtual const char * typeString() const = 0;
     243        virtual std::string typeString() const = 0;
    244244
    245245private:
     
    251251class StructDecl final : public AggregateDecl {
    252252public:
    253         Aggregate kind;
     253        DeclarationNode::Aggregate kind;
    254254
    255255        StructDecl( const CodeLocation& loc, const std::string& name,
    256                 Aggregate kind = Struct,
     256                DeclarationNode::Aggregate kind = DeclarationNode::Struct,
    257257                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
    258258        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
    259259
    260         bool is_coroutine() { return kind == Coroutine; }
    261         bool is_generator() { return kind == Generator; }
    262         bool is_monitor  () { return kind == Monitor  ; }
    263         bool is_thread   () { return kind == Thread   ; }
    264 
    265         const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    266 
    267         const char * typeString() const override { return aggrString( kind ); }
     260        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
     261        bool is_monitor() { return kind == DeclarationNode::Monitor; }
     262        bool is_thread() { return kind == DeclarationNode::Thread; }
     263
     264        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     265
     266        std::string typeString() const override { return "struct"; }
    268267
    269268private:
     
    281280        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
    282281
    283         const char * typeString() const override { return aggrString( Union ); }
     282        std::string typeString() const override { return "union"; }
    284283
    285284private:
     
    300299        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    301300
    302         const char * typeString() const override { return aggrString( Enum ); }
     301        std::string typeString() const override { return "enum"; }
    303302
    304303private:
     
    319318        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    320319
    321         const char * typeString() const override { return "trait"; }
     320        std::string typeString() const override { return "trait"; }
    322321
    323322private:
     
    326325};
    327326
    328 /// With statement `with (...) ...`
    329 class WithStmt final : public Decl {
    330 public:
    331         std::vector<ptr<Expr>> exprs;
    332         ptr<Stmt> stmt;
    333 
    334         WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
    335         : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
    336 
    337         const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    338 private:
    339         WithStmt * clone() const override { return new WithStmt{ *this }; }
    340         MUTATE_FRIEND
    341 };
    342 
    343327class AsmDecl : public Decl {
    344328public:
    345329        ptr<AsmStmt> stmt;
    346330
    347         AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
     331        AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
    348332        : Decl( loc, "", {}, {} ), stmt(stmt) {}
    349333
    350         const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
    351 private:
    352         AsmDecl * clone() const override { return new AsmDecl( *this ); }
     334        const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
     335private:
     336        AsmDecl *clone() const override { return new AsmDecl( *this ); }
    353337        MUTATE_FRIEND
    354338};
     
    362346        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
    363347
    364         const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
     348        const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
    365349private:
    366350        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
Note: See TracChangeset for help on using the changeset viewer.