Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.hpp

    r99da267 r427854b  
    99// Author           : Aaron B. Moss
    1010// 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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Fri Dec 13 17:38:33 2019
     13// Update Count     : 29
    1414//
    1515
     
    2020#include <unordered_map>
    2121#include <vector>
     22#include <algorithm>
    2223
    2324#include "FunctionSpec.hpp"
     
    2728#include "ParseNode.hpp"
    2829#include "StorageClasses.hpp"
    29 #include "TypeVar.hpp"
    3030#include "Visitor.hpp"
    31 #include "Parser/ParseNode.h"  // for DeclarationNode::Aggregate
     31#include "Common/utility.h"
     32#include "Common/SemanticError.h"                                               // error_str
    3233
    3334// Must be included in *all* AST classes; should be #undef'd at the end of the file
    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);
     35#define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
    3736
    3837namespace ast {
     
    8988        virtual const Type * get_type() const = 0;
    9089        /// Set type of this declaration. May be verified by subclass
    91         virtual void set_type( const Type * ) = 0;
     90        virtual void set_type(Type *) = 0;
    9291
    9392        const DeclWithType * accept( Visitor & v ) const override = 0;
     
    104103        ptr<Expr> bitfieldWidth;
    105104
    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, 
     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,
    109108                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
    110109        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
     
    112111
    113112        const Type* get_type() const override { return type; }
    114         void set_type( const Type * ty ) override { type = ty; }
     113        void set_type( Type * ty ) override { type = ty; }
    115114
    116115        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
     
    127126        std::vector< ptr<Expr> > withExprs;
    128127
    129         FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type,
     128        FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type,
    130129                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
    131130                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
     
    134133
    135134        const Type * get_type() const override;
    136         void set_type( const Type * t ) override;
     135        void set_type(Type * t) override;
    137136
    138137        bool has_body() const { return stmts; }
    139138
    140         const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); }
     139        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
    141140private:
    142141        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
     
    151150        std::vector<ptr<DeclWithType>> assertions;
    152151
    153         NamedTypeDecl(
    154                 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
    155                 const Type * b, Linkage::Spec spec = Linkage::Cforall )
     152        NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
     153                Type* b, Linkage::Spec spec = Linkage::Cforall )
    156154        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
    157155
    158156        /// Produces a name for the kind of alias
    159         virtual std::string typeString() const = 0;
     157        virtual const char * typeString() const = 0;
    160158
    161159private:
     
    166164/// Cforall type variable: `dtype T`
    167165class TypeDecl final : public NamedTypeDecl {
    168 public:
    169         TypeVar::Kind kind;
     166  public:
     167        enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
     168
     169        Kind kind;
    170170        bool sized;
    171171        ptr<Type> init;
     
    173173        /// Data extracted from a type decl
    174174        struct Data {
    175                 TypeVar::Kind kind;
     175                Kind kind;
    176176                bool isComplete;
    177177
    178                 Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {}
     178                Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
    179179                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
    180                 Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {}
     180                Data( 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 {
    185                         return kind == o.kind && isComplete == o.isComplete;
    186                 }
    187                 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 { return kind == o.kind && isComplete == o.isComplete; }
     185                bool operator!=( const Data & o ) const { return !(*this == o); }
    188186        };
    189187
    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;
     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;
    197194        /// Produces a name for generated code
    198         std::string genTypeString() const;
     195        const char * genTypeString() const;
    199196
    200197        /// convenience accessor to match Type::isComplete()
     
    202199
    203200        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    204 private:
     201  private:
    205202        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
    206203        MUTATE_FRIEND
     
    216213        : NamedTypeDecl( loc, name, storage, b, spec ) {}
    217214
    218         std::string typeString() const override { return "typedef"; }
     215        const char * typeString() const override { return "typedef"; }
    219216
    220217        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     
    227224class AggregateDecl : public Decl {
    228225public:
     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 std::string typeString() const = 0;
     243        virtual const char * typeString() const = 0;
    244244
    245245private:
     
    251251class StructDecl final : public AggregateDecl {
    252252public:
    253         DeclarationNode::Aggregate kind;
     253        Aggregate kind;
    254254
    255255        StructDecl( const CodeLocation& loc, const std::string& name,
    256                 DeclarationNode::Aggregate kind = DeclarationNode::Struct,
     256                Aggregate kind = 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 == 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"; }
     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 ); }
    267268
    268269private:
     
    280281        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
    281282
    282         std::string typeString() const override { return "union"; }
     283        const char * typeString() const override { return aggrString( Union ); }
    283284
    284285private:
     
    299300        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    300301
    301         std::string typeString() const override { return "enum"; }
     302        const char * typeString() const override { return aggrString( Enum ); }
    302303
    303304private:
     
    318319        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
    319320
    320         std::string typeString() const override { return "trait"; }
     321        const char * typeString() const override { return "trait"; }
    321322
    322323private:
     
    325326};
    326327
     328/// With statement `with (...) ...`
     329class WithStmt final : public Decl {
     330public:
     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 ); }
     338private:
     339        WithStmt * clone() const override { return new WithStmt{ *this }; }
     340        MUTATE_FRIEND
     341};
     342
    327343class AsmDecl : public Decl {
    328344public:
    329345        ptr<AsmStmt> stmt;
    330346
    331         AsmDecl( const CodeLocation & loc, AsmStmt *stmt )
     347        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
    332348        : Decl( loc, "", {}, {} ), stmt(stmt) {}
    333349
    334         const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); }
    335 private:
    336         AsmDecl *clone() const override { return new AsmDecl( *this ); }
     350        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
     351private:
     352        AsmDecl * clone() const override { return new AsmDecl( *this ); }
    337353        MUTATE_FRIEND
    338354};
     
    346362        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
    347363
    348         const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); }
     364        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
    349365private:
    350366        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
Note: See TracChangeset for help on using the changeset viewer.