Changeset 90152a4 for src/SynTree


Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

Location:
src/SynTree
Files:
2 added
4 deleted
24 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/AggregateDecl.cc

    rf9feab8 r90152a4  
    8686std::string TraitDecl::typeString() const { return "trait"; }
    8787
     88bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
     89        if ( enumValues.empty() ) {
     90                long long int currentValue = 0;
     91                for ( Declaration * member : members ) {
     92                        ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
     93                        if ( field->init ) {
     94                                SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
     95                                auto result = eval( init->value );
     96                                if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) );
     97                                currentValue = result.first;
     98                        }
     99                        assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
     100                        enumValues[ field->name ] = currentValue;
     101                        ++currentValue;
     102                }
     103        }
     104        if ( enumValues.count( enumerator->name ) ) {
     105                value = enumValues[ enumerator->name ];
     106                return true;
     107        }
     108        return false;
     109}
     110
    88111// Local Variables: //
    89112// tab-width: 4 //
  • src/SynTree/ApplicationExpr.cc

    rf9feab8 r90152a4  
    4949}
    5050
     51ParamEntry::ParamEntry( ParamEntry && other ) :
     52                decl( other.decl ), actualType( other.actualType ), formalType( other.formalType ), expr( other.expr ), inferParams( std::move( other.inferParams ) ) {
     53        other.actualType = nullptr;
     54        other.formalType = nullptr;
     55        other.expr = nullptr;
     56}
     57
     58ParamEntry & ParamEntry::operator=( ParamEntry && other ) {
     59        if ( &other == this ) return *this;
     60        delete actualType;
     61        delete formalType;
     62        delete expr;
     63        decl = other.decl;
     64        actualType = other.actualType;
     65        formalType = other.formalType;
     66        expr = other.expr;
     67        other.actualType = nullptr;
     68        other.formalType = nullptr;
     69        other.expr = nullptr;
     70        inferParams = std::move( other.inferParams );
     71        return *this;
     72}
     73
    5174ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list<Expression *> & args ) : function( funcExpr ), args( args ) {
    5275        PointerType *pointer = strict_dynamic_cast< PointerType* >( funcExpr->get_result() );
  • src/SynTree/Attribute.cc

    rf9feab8 r90152a4  
    1515
    1616#include <ostream>           // for operator<<, ostream, basic_ostream, endl
     17#include <set>
    1718
    1819#include "Attribute.h"
     
    2122
    2223Attribute::Attribute( const Attribute &other ) : name( other.name ) {
    23   cloneAll( other.parameters, parameters );
     24        cloneAll( other.parameters, parameters );
    2425}
    2526
    2627Attribute::~Attribute() {
    27   deleteAll( parameters );
     28        deleteAll( parameters );
     29}
     30
     31bool Attribute::isValidOnFuncParam() const {
     32        // attributes such as aligned, cleanup, etc. produce GCC errors when they appear
     33        // on function parameters. Maintain here a whitelist of attribute names that are
     34        // allowed to appear on parameters.
     35        static std::set< std::string > valid = {
     36                "noreturn", "unused"
     37        };
     38        return valid.count( normalizedName() );
     39}
     40
     41std::string Attribute::normalizedName() const {
     42        // trim beginning/ending _, convert to lowercase
     43        auto begin = name.find_first_not_of('_');
     44        auto end = name.find_last_not_of('_');
     45        if (begin == std::string::npos || end == std::string::npos) return "";
     46        std::string ret;
     47        ret.reserve( end-begin+1 );
     48        std::transform( &name[begin], &name[end+1], back_inserter( ret ), tolower );
     49        return ret;
    2850}
    2951
    3052void Attribute::print( std::ostream &os, Indenter indent ) const {
    31   using std::endl;
    32   using std::string;
     53        using std::endl;
     54        using std::string;
    3355
    34   if ( ! empty() ) {
    35     os << "Attribute with name: " << name;
    36     if ( ! parameters.empty() ) {
    37       os << " with parameters: " << endl;
    38       printAll( parameters, os, indent+1 );
    39     }
    40   }
     56        if ( ! empty() ) {
     57                os << "Attribute with name: " << name;
     58                if ( ! parameters.empty() ) {
     59                        os << " with parameters: " << endl;
     60                        printAll( parameters, os, indent+1 );
     61                }
     62        }
    4163}
    4264
  • src/SynTree/Attribute.h

    rf9feab8 r90152a4  
    4343        bool empty() const { return name == ""; }
    4444
     45        std::string normalizedName() const;
     46
     47        /// true if this attribute is allowed to appear attached to a function parameter
     48        bool isValidOnFuncParam() const;
     49
    4550        Attribute * clone() const override { return new Attribute( *this ); }
    4651        virtual void accept( Visitor & v ) override { v.visit( this ); }
  • src/SynTree/BasicType.cc

    rf9feab8 r90152a4  
    5555          case DoubleImaginary:
    5656          case LongDoubleImaginary:
     57          case Float80:
     58          case Float128:
    5759                return false;
    5860          case NUMBER_OF_BASIC_TYPES:
  • src/SynTree/CompoundStmt.cc

    rf9feab8 r90152a4  
    2323#include "Statement.h"                // for CompoundStmt, Statement, DeclStmt
    2424#include "SynTree/Label.h"            // for Label
    25 #include "SynTree/VarExprReplacer.h"  // for VarExprReplacer, VarExprReplace...
     25#include "SynTree/DeclReplacer.h"     // for DeclReplacer
    2626
    2727using std::string;
     
    4949        // recursively execute this routine. There may be more efficient ways of doing
    5050        // this.
    51         VarExprReplacer::DeclMap declMap;
     51        DeclReplacer::DeclMap declMap;
    5252        std::list< Statement * >::const_iterator origit = other.kids.begin();
    5353        for ( Statement * s : kids ) {
     
    5959                                DeclarationWithType * origdwt = strict_dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
    6060                                assert( dwt->get_name() == origdwt->get_name() );
    61                                 declMap[ origdwt ] = new VariableExpr( dwt );
     61                                declMap[ origdwt ] = dwt;
    6262                        } else assert( ! dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ) );
    6363                } else assert( ! dynamic_cast< DeclStmt * > ( s ) );
    6464        }
    6565        if ( ! declMap.empty() ) {
    66                 VarExprReplacer replacer( declMap );
    67                 acceptMutator( replacer );
     66                DeclReplacer::replace( this, declMap );
    6867        }
    6968}
  • src/SynTree/Declaration.cc

    rf9feab8 r90152a4  
    8181
    8282
     83StaticAssertDecl::StaticAssertDecl( Expression * condition, ConstantExpr * message ) : Declaration( "", Type::StorageClasses(), LinkageSpec::C ), condition( condition ), message( message )  {
     84}
     85
     86StaticAssertDecl::StaticAssertDecl( const StaticAssertDecl & other ) : Declaration( other ), condition( maybeClone( other.condition ) ), message( maybeClone( other.message ) )  {
     87}
     88
     89StaticAssertDecl::~StaticAssertDecl() {
     90        delete condition;
     91        delete message;
     92}
     93
     94void StaticAssertDecl::print( std::ostream &os, Indenter indent ) const {
     95        os << "Static Assert with condition: ";
     96        condition->print( os, indent+1 );
     97        os << std::endl << indent << "and message: ";
     98        message->print( os, indent+1 );
     99os << std::endl;
     100}
     101
     102void StaticAssertDecl::printShort( std::ostream &os, Indenter indent ) const {
     103        print( os, indent );
     104}
     105
     106
    83107// Local Variables: //
    84108// tab-width: 4 //
  • src/SynTree/Declaration.h

    rf9feab8 r90152a4  
    8484        Expression *asmName;
    8585        std::list< Attribute * > attributes;
     86        bool isDeleted = false;
    8687
    8788        DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
     
    151152        FunctionType *type;
    152153        CompoundStmt *statements;
     154        std::list< Expression * > withExprs;
    153155
    154156        FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
     
    200202        typedef NamedTypeDecl Parent;
    201203  public:
    202         enum Kind { Dtype, Ftype, Ttype };
     204        enum Kind { Dtype, Ftype, Ttype, NUMBER_OF_KINDS };
    203205
    204206        Type * init;
     
    244246        typedef NamedTypeDecl Parent;
    245247  public:
    246         TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
     248        TypedefDecl( const std::string &name, CodeLocation location, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
     249                : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
     250
    247251        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
    248252
     
    262266        bool body;
    263267        std::list< Attribute * > attributes;
     268        AggregateDecl * parent = nullptr;
    264269
    265270        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
     
    319324        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
    320325
     326        bool valueOf( Declaration * enumerator, long long int & value );
     327
    321328        virtual EnumDecl *clone() const override { return new EnumDecl( *this ); }
    322329        virtual void accept( Visitor &v ) override { v.visit( this ); }
    323330        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
    324331  private:
     332        std::map< std::string, long long int > enumValues;
    325333        virtual std::string typeString() const override;
    326334};
     
    355363        virtual void accept( Visitor &v ) override { v.visit( this ); }
    356364        virtual AsmDecl *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
     365        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
     366        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
     367};
     368
     369class StaticAssertDecl : public Declaration {
     370public:
     371        Expression * condition;
     372        ConstantExpr * message;   // string literal
     373
     374        StaticAssertDecl( Expression * condition, ConstantExpr * message );
     375        StaticAssertDecl( const StaticAssertDecl & other );
     376        virtual ~StaticAssertDecl();
     377
     378        virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
     379        virtual void accept( Visitor &v ) override { v.visit( this ); }
     380        virtual StaticAssertDecl * acceptMutator( Mutator &m )  override { return m.mutate( this ); }
    357381        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
    358382        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
  • src/SynTree/Expression.cc

    rf9feab8 r90152a4  
    5050}
    5151
     52void Expression::spliceInferParams( Expression * other ) {
     53        if ( ! other ) return;
     54        for ( auto p : other->inferParams ) {
     55                inferParams[p.first] = std::move( p.second );
     56        }
     57}
     58
    5259Expression::~Expression() {
    5360        delete env;
     
    8188        constant.print( os );
    8289        Expression::print( os, indent );
     90}
     91
     92long long int ConstantExpr::intValue() const {
     93        if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) {
     94                if ( basicType->isInteger() ) {
     95                        return get_constant()->get_ival();
     96                }
     97        } else if ( dynamic_cast< OneType * >( result ) ) {
     98                return 1;
     99        } else if ( dynamic_cast< ZeroType * >( result ) ) {
     100                return 0;
     101        }
     102        SemanticError( this, "Constant expression of non-integral type " );
    83103}
    84104
     
    95115        //      assert( inst->baseEnum );
    96116        //      EnumDecl * decl = inst->baseEnum;
    97         //      for ( Declaration * member : decl->members ) {
    98         //              if ( member == _var ) {
    99         //                      type->set_lvalue( false );
    100         //              }
     117        //      long long int value;
     118        //      if ( decl->valueOf( var, value ) ) {
     119        //              type->set_lvalue( false );
    101120        //      }
    102121        // }
     
    259278}
    260279
    261 CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
     280CastExpr::CastExpr( Expression *arg, Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
    262281        set_result(toType);
    263282}
    264283
    265 CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) {
     284CastExpr::CastExpr( Expression *arg, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
    266285        set_result( new VoidType( Type::Qualifiers() ) );
    267286}
    268287
    269 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
     288CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) {
    270289}
    271290
     
    287306}
    288307
     308KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) {
     309}
     310
     311KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
     312}
     313
     314KeywordCastExpr::~KeywordCastExpr() {
     315        delete arg;
     316}
     317
     318const std::string & KeywordCastExpr::targetString() const {
     319        static const std::string targetStrs[] = {
     320                "coroutine", "thread", "monitor"
     321        };
     322        static_assert(
     323                (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS),
     324                "Each KeywordCastExpr::Target should have a corresponding string representation"
     325        );
     326        return targetStrs[(unsigned long)target];
     327}
     328
     329void KeywordCastExpr::print( std::ostream &os, Indenter indent ) const {
     330        os << "Keyword Cast of:" << std::endl << indent+1;
     331        arg->print(os, indent+1);
     332        os << std::endl << indent << "... to: ";
     333        os << targetString();
     334        Expression::print( os, indent );
     335}
     336
    289337VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
    290338        set_result(toType);
     
    333381}
    334382
    335 namespace {
    336         TypeSubstitution makeSub( Type * t ) {
    337                 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {
    338                         return makeSub( refType->get_base() );
    339                 } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
    340                         return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );
    341                 } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {
    342                         return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->parameters.begin() );
    343                 } else {
    344                         assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() );
    345                 }
    346         }
    347 }
    348 
    349 
    350383MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) :
    351384                Expression(), member(member), aggregate(aggregate) {
    352385        assert( member );
    353386        assert( aggregate );
    354 
    355         TypeSubstitution sub( makeSub( aggregate->get_result() ) );
     387        assert( aggregate->result );
     388
     389        TypeSubstitution sub = aggregate->result->genericSubstitution();
    356390        Type * res = member->get_type()->clone();
    357391        sub.apply( res );
     
    403437                } else {
    404438                        // references have been removed, in which case dereference returns an lvalue of the base type.
    405                         ret->get_result()->set_lvalue( true );
     439                        ret->result->set_lvalue( true );
    406440                }
    407441        }
     
    585619
    586620StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
    587         assert( statements );
    588         std::list< Statement * > & body = statements->get_kids();
    589         if ( ! body.empty() ) {
    590                 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
    591                         set_result( maybeClone( exprStmt->get_expr()->get_result() ) );
    592                 }
    593         }
    594         // ensure that StmtExpr has a result type
    595         if ( ! result ) {
    596                 set_result( new VoidType( Type::Qualifiers() ) );
    597         }
     621        computeResult();
    598622}
    599623StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
     
    605629        deleteAll( dtors );
    606630        deleteAll( returnDecls );
     631}
     632void StmtExpr::computeResult() {
     633        assert( statements );
     634        std::list< Statement * > & body = statements->kids;
     635        delete result;
     636        result = nullptr;
     637        if ( ! returnDecls.empty() ) {
     638                // prioritize return decl for result type, since if a return decl exists, then
     639                // the StmtExpr is currently in an intermediate state where the body will always
     640                // give a void result type.
     641                result = returnDecls.front()->get_type()->clone();
     642        } else if ( ! body.empty() ) {
     643                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
     644                        result = maybeClone( exprStmt->expr->result );
     645                }
     646        }
     647        // ensure that StmtExpr has a result type
     648        if ( ! result ) {
     649                result = new VoidType( Type::Qualifiers() );
     650        }
    607651}
    608652void StmtExpr::print( std::ostream &os, Indenter indent ) const {
     
    688732}
    689733
     734DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
     735        assert( expr->result );
     736        result = expr->result->clone();
     737}
     738DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
     739DeletedExpr::~DeletedExpr() {
     740        delete expr;
     741}
     742
     743void DeletedExpr::print( std::ostream & os, Indenter indent ) const {
     744        os << "Deleted Expression" << std::endl << indent+1;
     745        expr->print( os, indent+1 );
     746        os << std::endl << indent+1 << "... deleted by: ";
     747        deleteStmt->print( os, indent+1 );
     748}
     749
     750
     751DefaultArgExpr::DefaultArgExpr( Expression * expr ) : expr( expr ) {
     752        assert( expr->result );
     753        result = expr->result->clone();
     754}
     755DefaultArgExpr::DefaultArgExpr( const DefaultArgExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ) {}
     756DefaultArgExpr::~DefaultArgExpr() {
     757        delete expr;
     758}
     759
     760void DefaultArgExpr::print( std::ostream & os, Indenter indent ) const {
     761        os << "Default Argument Expression" << std::endl << indent+1;
     762        expr->print( os, indent+1 );
     763}
     764
     765GenericExpr::Association::Association( Type * type, Expression * expr ) : type( type ), expr( expr ), isDefault( false ) {}
     766GenericExpr::Association::Association( Expression * expr ) : type( nullptr ), expr( expr ), isDefault( true ) {}
     767GenericExpr::Association::Association( const Association & other ) : type( maybeClone( other.type ) ), expr( maybeClone( other.expr ) ), isDefault( other.isDefault ) {}
     768GenericExpr::Association::~Association() {
     769        delete type;
     770        delete expr;
     771}
     772
     773GenericExpr::GenericExpr( Expression * control, const std::list<Association> & assoc ) : Expression(), control( control ), associations( assoc ) {}
     774GenericExpr::GenericExpr( const GenericExpr & other ) : Expression(other), control( maybeClone( other.control ) ), associations( other.associations ) {
     775}
     776GenericExpr::~GenericExpr() {
     777        delete control;
     778}
     779
     780void GenericExpr::print( std::ostream & os, Indenter indent ) const {
     781        os << "C11 _Generic Expression" << std::endl << indent+1;
     782        control->print( os, indent+1 );
     783        os << std::endl << indent+1 << "... with associations: " << std::endl;
     784        for ( const Association & assoc : associations ) {
     785                os << indent+1;
     786                if (assoc.isDefault) {
     787                        os << "... default: ";
     788                        assoc.expr->print( os, indent+1 );
     789                } else {
     790                        os << "... type: ";
     791                        assoc.type->print( os, indent+1 );
     792                        os << std::endl << indent+1 << "... expression: ";
     793                        assoc.expr->print( os, indent+1 );
     794                        os << std::endl;
     795                }
     796                os << std::endl;
     797        }
     798}
     799
    690800// Local Variables: //
    691801// tab-width: 4 //
  • src/SynTree/Expression.h

    rf9feab8 r90152a4  
    4141        ParamEntry( UniqueId decl, Type * actualType, Type * formalType, Expression* expr ): decl( decl ), actualType( actualType ), formalType( formalType ), expr( expr ), inferParams( new InferredParams ) {}
    4242        ParamEntry( const ParamEntry & other );
     43        ParamEntry( ParamEntry && other );
    4344        ~ParamEntry();
    4445        ParamEntry & operator=( const ParamEntry & other );
     46        ParamEntry & operator=( ParamEntry && other );
    4547
    4648        UniqueId decl;
     
    7476        InferredParams & get_inferParams() { return inferParams; }
    7577
     78        // move other's inferParams to this
     79        void spliceInferParams( Expression * other );
     80
    7681        virtual Expression * clone() const override = 0;
    7782        virtual void accept( Visitor & v ) override = 0;
     
    188193  public:
    189194        Expression * arg;
    190 
    191         CastExpr( Expression * arg );
    192         CastExpr( Expression * arg, Type * toType );
     195        bool isGenerated = true; // whether this cast appeared in the source program
     196
     197        CastExpr( Expression * arg, bool isGenerated = true );
     198        CastExpr( Expression * arg, Type * toType, bool isGenerated = true );
     199        CastExpr( Expression * arg, void * ) = delete; // prevent accidentally passing pointers for isGenerated in the first constructor
    193200        CastExpr( const CastExpr & other );
    194201        virtual ~CastExpr();
     
    198205
    199206        virtual CastExpr * clone() const { return new CastExpr( * this ); }
     207        virtual void accept( Visitor & v ) { v.visit( this ); }
     208        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     209        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     210};
     211
     212/// KeywordCastExpr represents a cast to 'keyword types', e.g. (thread &)t
     213class KeywordCastExpr : public Expression {
     214public:
     215        Expression * arg;
     216        enum Target {
     217                Coroutine, Thread, Monitor, NUMBER_OF_TARGETS
     218        } target;
     219
     220        KeywordCastExpr( Expression * arg, Target target );
     221        KeywordCastExpr( const KeywordCastExpr & other );
     222        virtual ~KeywordCastExpr();
     223
     224        const std::string & targetString() const;
     225
     226        virtual KeywordCastExpr * clone() const { return new KeywordCastExpr( * this ); }
    200227        virtual void accept( Visitor & v ) { v.visit( this ); }
    201228        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     
    295322
    296323        Constant * get_constant() { return & constant; }
     324        const Constant * get_constant() const { return & constant; }
    297325        void set_constant( const Constant & newValue ) { constant = newValue; }
     326
     327        long long int intValue() const;
    298328
    299329        virtual ConstantExpr * clone() const { return new ConstantExpr( * this ); }
     
    725755        StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; }
    726756
     757        // call to set the result type of this StmtExpr based on its body
     758        void computeResult();
     759
    727760        std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
    728761        std::list< Expression * > & get_dtors() { return dtors; }
     
    816849};
    817850
     851/// expression that contains a deleted identifier - should never make it past the resolver.
     852class DeletedExpr : public Expression {
     853public:
     854        Expression * expr;
     855        BaseSyntaxNode * deleteStmt;
     856
     857        DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt );
     858        DeletedExpr( const DeletedExpr & other );
     859        ~DeletedExpr();
     860
     861        virtual DeletedExpr * clone() const { return new DeletedExpr( * this ); }
     862        virtual void accept( Visitor & v ) { v.visit( this ); }
     863        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     864        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     865};
     866
     867/// expression wrapping the use of a default argument - should never make it past the resolver.
     868class DefaultArgExpr : public Expression {
     869public:
     870        Expression * expr;
     871
     872        DefaultArgExpr( Expression * expr );
     873        DefaultArgExpr( const DefaultArgExpr & other );
     874        ~DefaultArgExpr();
     875
     876        virtual DefaultArgExpr * clone() const { return new DefaultArgExpr( * this ); }
     877        virtual void accept( Visitor & v ) { v.visit( this ); }
     878        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     879        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     880};
     881
     882/// C11 _Generic expression
     883class GenericExpr : public Expression {
     884public:
     885        struct Association {
     886                Type * type = nullptr;
     887                Expression * expr = nullptr;
     888                bool isDefault = false;
     889
     890                Association( Type * type, Expression * expr );
     891                Association( Expression * expr );
     892                Association( const Association & other );
     893                Association & operator=( const Association & other ) = delete; // at the moment this isn't used, and I don't want to implement it
     894                ~Association();
     895        };
     896
     897        Expression * control;
     898        std::list<Association> associations;
     899
     900        GenericExpr( Expression * control, const std::list<Association> & assoc );
     901        GenericExpr( const GenericExpr & other );
     902        virtual ~GenericExpr();
     903
     904        virtual GenericExpr * clone() const { return new GenericExpr( * this ); }
     905        virtual void accept( Visitor & v ) { v.visit( this ); }
     906        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     907        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     908};
     909
    818910// Local Variables: //
    819911// tab-width: 4 //
  • src/SynTree/FunctionDecl.cc

    rf9feab8 r90152a4  
    2626#include "Statement.h"           // for CompoundStmt
    2727#include "Type.h"                // for Type, FunctionType, Type::FuncSpecif...
    28 #include "VarExprReplacer.h"
     28#include "DeclReplacer.h"
    2929
    3030extern bool translation_unit_nomain;
     
    4141                : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
    4242
    43         VarExprReplacer::DeclMap declMap;
     43        DeclReplacer::DeclMap declMap;
    4444        for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) {
    45                 declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p) );
     45                declMap[ std::get<0>(p) ] = std::get<1>(p);
    4646        }
    4747        for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) {
    48                 declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p) );
     48                declMap[ std::get<0>(p) ] = std::get<1>(p);
    4949        }
    5050        if ( ! declMap.empty() ) {
    51                 VarExprReplacer replacer( declMap );
    52                 acceptMutator( replacer );
     51                DeclReplacer::replace( this, declMap );
    5352        }
     53        cloneAll( other.withExprs, withExprs );
    5454}
    5555
     
    5757        delete type;
    5858        delete statements;
     59        deleteAll( withExprs );
    5960}
    6061
  • src/SynTree/Label.h

    rf9feab8 r90152a4  
    3333        std::list< Attribute * >& get_attributes() { return attributes; }
    3434
    35         operator std::string() { return name; }
     35        operator std::string() const { return name; }
    3636        bool empty() { return name.empty(); }
    3737  private:
  • src/SynTree/Mutator.h

    rf9feab8 r90152a4  
    2222class Mutator {
    2323  protected:
    24         Mutator();
    25         virtual ~Mutator();
     24        Mutator() = default;
     25        virtual ~Mutator() = default;
    2626  public:
    27         virtual DeclarationWithType * mutate( ObjectDecl * objectDecl );
    28         virtual DeclarationWithType * mutate( FunctionDecl * functionDecl );
    29         virtual Declaration * mutate( StructDecl * aggregateDecl );
    30         virtual Declaration * mutate( UnionDecl * aggregateDecl );
    31         virtual Declaration * mutate( EnumDecl * aggregateDecl );
    32         virtual Declaration * mutate( TraitDecl * aggregateDecl );
    33         virtual Declaration * mutate( TypeDecl * typeDecl );
    34         virtual Declaration * mutate( TypedefDecl * typeDecl );
    35         virtual AsmDecl * mutate( AsmDecl * asmDecl );
     27        virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) = 0;
     28        virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ) = 0;
     29        virtual Declaration * mutate( StructDecl * aggregateDecl ) = 0;
     30        virtual Declaration * mutate( UnionDecl * aggregateDecl ) = 0;
     31        virtual Declaration * mutate( EnumDecl * aggregateDecl ) = 0;
     32        virtual Declaration * mutate( TraitDecl * aggregateDecl ) = 0;
     33        virtual Declaration * mutate( TypeDecl * typeDecl ) = 0;
     34        virtual Declaration * mutate( TypedefDecl * typeDecl ) = 0;
     35        virtual AsmDecl * mutate( AsmDecl * asmDecl ) = 0;
     36        virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) = 0;
    3637
    37         virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
    38         virtual Statement * mutate( ExprStmt * exprStmt );
    39         virtual Statement * mutate( AsmStmt * asmStmt );
    40         virtual Statement * mutate( IfStmt * ifStmt );
    41         virtual Statement * mutate( WhileStmt * whileStmt );
    42         virtual Statement * mutate( ForStmt * forStmt );
    43         virtual Statement * mutate( SwitchStmt * switchStmt );
    44         virtual Statement * mutate( CaseStmt * caseStmt );
    45         virtual Statement * mutate( BranchStmt * branchStmt );
    46         virtual Statement * mutate( ReturnStmt * returnStmt );
    47         virtual Statement * mutate( ThrowStmt * throwStmt );
    48         virtual Statement * mutate( TryStmt * tryStmt );
    49         virtual Statement * mutate( CatchStmt * catchStmt );
    50         virtual Statement * mutate( FinallyStmt * catchStmt );
    51         virtual Statement * mutate( WaitForStmt * waitforStmt );
    52         virtual Statement * mutate( WithStmt * withStmt );
    53         virtual NullStmt * mutate( NullStmt * nullStmt );
    54         virtual Statement * mutate( DeclStmt * declStmt );
    55         virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt );
     38        virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) = 0;
     39        virtual Statement * mutate( ExprStmt * exprStmt ) = 0;
     40        virtual Statement * mutate( AsmStmt * asmStmt ) = 0;
     41        virtual Statement * mutate( DirectiveStmt * dirStmt ) = 0;
     42        virtual Statement * mutate( IfStmt * ifStmt ) = 0;
     43        virtual Statement * mutate( WhileStmt * whileStmt ) = 0;
     44        virtual Statement * mutate( ForStmt * forStmt ) = 0;
     45        virtual Statement * mutate( SwitchStmt * switchStmt ) = 0;
     46        virtual Statement * mutate( CaseStmt * caseStmt ) = 0;
     47        virtual Statement * mutate( BranchStmt * branchStmt ) = 0;
     48        virtual Statement * mutate( ReturnStmt * returnStmt ) = 0;
     49        virtual Statement * mutate( ThrowStmt * throwStmt ) = 0;
     50        virtual Statement * mutate( TryStmt * tryStmt ) = 0;
     51        virtual Statement * mutate( CatchStmt * catchStmt ) = 0;
     52        virtual Statement * mutate( FinallyStmt * catchStmt ) = 0;
     53        virtual Statement * mutate( WaitForStmt * waitforStmt ) = 0;
     54        virtual Statement * mutate( WithStmt * withStmt ) = 0;
     55        virtual NullStmt * mutate( NullStmt * nullStmt ) = 0;
     56        virtual Statement * mutate( DeclStmt * declStmt ) = 0;
     57        virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ) = 0;
    5658
    57         virtual Expression* mutate( ApplicationExpr * applicationExpr );
    58         virtual Expression* mutate( UntypedExpr * untypedExpr );
    59         virtual Expression* mutate( NameExpr * nameExpr );
    60         virtual Expression* mutate( AddressExpr * castExpr );
    61         virtual Expression* mutate( LabelAddressExpr * labAddressExpr );
    62         virtual Expression* mutate( CastExpr * castExpr );
    63         virtual Expression* mutate( VirtualCastExpr * castExpr );
    64         virtual Expression* mutate( UntypedMemberExpr * memberExpr );
    65         virtual Expression* mutate( MemberExpr * memberExpr );
    66         virtual Expression* mutate( VariableExpr * variableExpr );
    67         virtual Expression* mutate( ConstantExpr * constantExpr );
    68         virtual Expression* mutate( SizeofExpr * sizeofExpr );
    69         virtual Expression* mutate( AlignofExpr * alignofExpr );
    70         virtual Expression* mutate( UntypedOffsetofExpr * offsetofExpr );
    71         virtual Expression* mutate( OffsetofExpr * offsetofExpr );
    72         virtual Expression* mutate( OffsetPackExpr * offsetPackExpr );
    73         virtual Expression* mutate( AttrExpr * attrExpr );
    74         virtual Expression* mutate( LogicalExpr * logicalExpr );
    75         virtual Expression* mutate( ConditionalExpr * conditionalExpr );
    76         virtual Expression* mutate( CommaExpr * commaExpr );
    77         virtual Expression* mutate( TypeExpr * typeExpr );
    78         virtual Expression* mutate( AsmExpr * asmExpr );
    79         virtual Expression* mutate( ImplicitCopyCtorExpr * impCpCtorExpr );
    80         virtual Expression* mutate( ConstructorExpr * ctorExpr );
    81         virtual Expression* mutate( CompoundLiteralExpr * compLitExpr );
    82         virtual Expression* mutate( RangeExpr * rangeExpr );
    83         virtual Expression* mutate( UntypedTupleExpr * tupleExpr );
    84         virtual Expression* mutate( TupleExpr * tupleExpr );
    85         virtual Expression* mutate( TupleIndexExpr * tupleExpr );
    86         virtual Expression* mutate( TupleAssignExpr * assignExpr );
    87         virtual Expression* mutate( StmtExpr  * stmtExpr );
    88         virtual Expression* mutate( UniqueExpr  * uniqueExpr );
    89         virtual Expression* mutate( UntypedInitExpr  * initExpr );
    90         virtual Expression* mutate( InitExpr  * initExpr );
     59        virtual Expression * mutate( ApplicationExpr * applicationExpr ) = 0;
     60        virtual Expression * mutate( UntypedExpr * untypedExpr ) = 0;
     61        virtual Expression * mutate( NameExpr * nameExpr ) = 0;
     62        virtual Expression * mutate( AddressExpr * addrExpr ) = 0;
     63        virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) = 0;
     64        virtual Expression * mutate( CastExpr * castExpr ) = 0;
     65        virtual Expression * mutate( KeywordCastExpr * castExpr ) = 0;
     66        virtual Expression * mutate( VirtualCastExpr * castExpr ) = 0;
     67        virtual Expression * mutate( UntypedMemberExpr * memberExpr ) = 0;
     68        virtual Expression * mutate( MemberExpr * memberExpr ) = 0;
     69        virtual Expression * mutate( VariableExpr * variableExpr ) = 0;
     70        virtual Expression * mutate( ConstantExpr * constantExpr ) = 0;
     71        virtual Expression * mutate( SizeofExpr * sizeofExpr ) = 0;
     72        virtual Expression * mutate( AlignofExpr * alignofExpr ) = 0;
     73        virtual Expression * mutate( UntypedOffsetofExpr * offsetofExpr ) = 0;
     74        virtual Expression * mutate( OffsetofExpr * offsetofExpr ) = 0;
     75        virtual Expression * mutate( OffsetPackExpr * offsetPackExpr ) = 0;
     76        virtual Expression * mutate( AttrExpr * attrExpr ) = 0;
     77        virtual Expression * mutate( LogicalExpr * logicalExpr ) = 0;
     78        virtual Expression * mutate( ConditionalExpr * conditionalExpr ) = 0;
     79        virtual Expression * mutate( CommaExpr * commaExpr ) = 0;
     80        virtual Expression * mutate( TypeExpr * typeExpr ) = 0;
     81        virtual Expression * mutate( AsmExpr * asmExpr ) = 0;
     82        virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) = 0;
     83        virtual Expression * mutate( ConstructorExpr * ctorExpr ) = 0;
     84        virtual Expression * mutate( CompoundLiteralExpr * compLitExpr ) = 0;
     85        virtual Expression * mutate( RangeExpr * rangeExpr ) = 0;
     86        virtual Expression * mutate( UntypedTupleExpr * tupleExpr ) = 0;
     87        virtual Expression * mutate( TupleExpr * tupleExpr ) = 0;
     88        virtual Expression * mutate( TupleIndexExpr * tupleExpr ) = 0;
     89        virtual Expression * mutate( TupleAssignExpr * assignExpr ) = 0;
     90        virtual Expression * mutate( StmtExpr  * stmtExpr ) = 0;
     91        virtual Expression * mutate( UniqueExpr  * uniqueExpr ) = 0;
     92        virtual Expression * mutate( UntypedInitExpr  * initExpr ) = 0;
     93        virtual Expression * mutate( InitExpr  * initExpr ) = 0;
     94        virtual Expression * mutate( DeletedExpr * delExpr ) = 0;
     95        virtual Expression * mutate( DefaultArgExpr * argExpr ) = 0;
     96        virtual Expression * mutate( GenericExpr * genExpr ) = 0;
    9197
    92         virtual Type * mutate( VoidType * basicType );
    93         virtual Type * mutate( BasicType * basicType );
    94         virtual Type * mutate( PointerType * pointerType );
    95         virtual Type * mutate( ArrayType * arrayType );
    96         virtual Type * mutate( ReferenceType * refType );
    97         virtual Type * mutate( FunctionType * functionType );
    98         virtual Type * mutate( StructInstType * aggregateUseType );
    99         virtual Type * mutate( UnionInstType * aggregateUseType );
    100         virtual Type * mutate( EnumInstType * aggregateUseType );
    101         virtual Type * mutate( TraitInstType * aggregateUseType );
    102         virtual Type * mutate( TypeInstType * aggregateUseType );
    103         virtual Type * mutate( TupleType * tupleType );
    104         virtual Type * mutate( TypeofType * typeofType );
    105         virtual Type * mutate( AttrType * attrType );
    106         virtual Type * mutate( VarArgsType * varArgsType );
    107         virtual Type * mutate( ZeroType * zeroType );
    108         virtual Type * mutate( OneType * oneType );
     98        virtual Type * mutate( VoidType * basicType ) = 0;
     99        virtual Type * mutate( BasicType * basicType ) = 0;
     100        virtual Type * mutate( PointerType * pointerType ) = 0;
     101        virtual Type * mutate( ArrayType * arrayType ) = 0;
     102        virtual Type * mutate( ReferenceType * refType ) = 0;
     103        virtual Type * mutate( QualifiedType * qualType ) = 0;
     104        virtual Type * mutate( FunctionType * functionType ) = 0;
     105        virtual Type * mutate( StructInstType * aggregateUseType ) = 0;
     106        virtual Type * mutate( UnionInstType * aggregateUseType ) = 0;
     107        virtual Type * mutate( EnumInstType * aggregateUseType ) = 0;
     108        virtual Type * mutate( TraitInstType * aggregateUseType ) = 0;
     109        virtual Type * mutate( TypeInstType * aggregateUseType ) = 0;
     110        virtual Type * mutate( TupleType * tupleType ) = 0;
     111        virtual Type * mutate( TypeofType * typeofType ) = 0;
     112        virtual Type * mutate( AttrType * attrType ) = 0;
     113        virtual Type * mutate( VarArgsType * varArgsType ) = 0;
     114        virtual Type * mutate( ZeroType * zeroType ) = 0;
     115        virtual Type * mutate( OneType * oneType ) = 0;
     116        virtual Type * mutate( GlobalScopeType * globalType ) = 0;
    109117
    110         virtual Designation * mutate( Designation * designation );
    111         virtual Initializer * mutate( SingleInit * singleInit );
    112         virtual Initializer * mutate( ListInit * listInit );
    113         virtual Initializer * mutate( ConstructorInit * ctorInit );
     118        virtual Designation * mutate( Designation * designation ) = 0 ;
     119        virtual Initializer * mutate( SingleInit * singleInit ) = 0 ;
     120        virtual Initializer * mutate( ListInit * listInit ) = 0 ;
     121        virtual Initializer * mutate( ConstructorInit * ctorInit ) = 0 ;
    114122
    115         virtual Subrange * mutate( Subrange * subrange );
     123        virtual Subrange * mutate( Subrange * subrange ) = 0;
    116124
    117         virtual Constant * mutate( Constant * constant );
     125        virtual Constant * mutate( Constant * constant ) = 0;
    118126
    119         virtual Attribute * mutate( Attribute * attribute );
     127        virtual Attribute * mutate( Attribute * attribute ) = 0;
    120128
    121         virtual TypeSubstitution * mutate( TypeSubstitution * sub );
    122 
    123   private:
    124         virtual Declaration * handleAggregateDecl(AggregateDecl * aggregateDecl );
    125         virtual Declaration * handleNamedTypeDecl(NamedTypeDecl * typeDecl );
    126         virtual Type * handleReferenceToType(ReferenceToType * aggregateUseType );
     129        virtual TypeSubstitution * mutate( TypeSubstitution * sub ) = 0;
    127130};
    128131
     
    140143template< typename Container, typename MutatorType >
    141144inline void mutateAll( Container &container, MutatorType &mutator ) {
    142         SemanticError errors;
     145        SemanticErrorException errors;
    143146        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    144147                try {
     
    148151                                assert( *i );
    149152                        } // if
    150                 } catch( SemanticError &e ) {
    151                         e.set_location( (*i)->location );
     153                } catch( SemanticErrorException &e ) {
    152154                        errors.append( e );
    153155                } // try
  • src/SynTree/ReferenceToType.cc

    rf9feab8 r90152a4  
    1414//
    1515
    16 #include <cassert>           // for assert
    17 #include <list>              // for list, _List_const_iterator, list<>::cons...
    18 #include <ostream>           // for operator<<, basic_ostream, ostream, endl
    19 #include <string>            // for string, operator<<, char_traits, operator==
    20 
    21 #include "Common/utility.h"  // for printAll, cloneAll, deleteAll
    22 #include "Declaration.h"     // for StructDecl, UnionDecl, EnumDecl, Declara...
    23 #include "Expression.h"      // for Expression
    24 #include "Type.h"            // for TypeInstType, StructInstType, UnionInstType
     16#include <cassert>            // for assert
     17#include <list>               // for list, _List_const_iterator, list<>::cons...
     18#include <ostream>            // for operator<<, basic_ostream, ostream, endl
     19#include <string>             // for string, operator<<, char_traits, operator==
     20
     21#include "Common/utility.h"   // for printAll, cloneAll, deleteAll
     22#include "Declaration.h"      // for StructDecl, UnionDecl, EnumDecl, Declara...
     23#include "Expression.h"       // for Expression
     24#include "Type.h"             // for TypeInstType, StructInstType, UnionInstType
     25#include "TypeSubstitution.h" // for TypeSubstitution
    2526
    2627class Attribute;
     
    4950
    5051namespace {
    51         void doLookup( const std::list< Declaration* > &members, const std::string &name, std::list< Declaration* > &foundDecls ) {
    52                 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
    53                         if ( (*i)->get_name() == name ) {
    54                                 foundDecls.push_back( *i );
     52        void doLookup( const std::list< Declaration * > & members, const std::string & name, std::list< Declaration* > & foundDecls ) {
     53                for ( Declaration * decl : members ) {
     54                        if ( decl->name == name ) {
     55                                foundDecls.push_back( decl );
    5556                        } // if
    5657                } // for
     
    5960
    6061StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) :
    61                 Parent( tq, baseStruct->get_name(), attributes ), baseStruct( baseStruct ) {}
     62                Parent( tq, baseStruct->name, attributes ), baseStruct( baseStruct ) {}
    6263
    6364std::string StructInstType::typeString() const { return "struct"; }
     65
     66const std::list<TypeDecl*>* StructInstType::get_baseParameters() const {
     67        if ( ! baseStruct ) return nullptr;
     68        return &baseStruct->get_parameters();
     69}
    6470
    6571std::list<TypeDecl*>* StructInstType::get_baseParameters() {
     
    7076bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; }
    7177
    72 AggregateDecl * StructInstType::getAggr() { return baseStruct; }
     78AggregateDecl * StructInstType::getAggr() const { return baseStruct; }
     79
     80TypeSubstitution StructInstType::genericSubstitution() const {
     81        return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() );
     82}
    7383
    7484void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
    7585        assert( baseStruct );
    76         doLookup( baseStruct->get_members(), name, foundDecls );
     86        doLookup( baseStruct->members, name, foundDecls );
    7787}
    7888
     
    93103
    94104UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) :
    95                 Parent( tq, baseUnion->get_name(), attributes ), baseUnion( baseUnion ) {}
     105                Parent( tq, baseUnion->name, attributes ), baseUnion( baseUnion ) {}
    96106
    97107std::string UnionInstType::typeString() const { return "union"; }
     
    102112}
    103113
     114const std::list< TypeDecl * > * UnionInstType::get_baseParameters() const {
     115        if ( ! baseUnion ) return nullptr;
     116        return &baseUnion->get_parameters();
     117}
     118
    104119bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; }
    105120
    106 AggregateDecl * UnionInstType::getAggr() { return baseUnion; }
     121AggregateDecl * UnionInstType::getAggr() const { return baseUnion; }
     122
     123TypeSubstitution UnionInstType::genericSubstitution() const {
     124        return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() );
     125}
    107126
    108127void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const {
    109128        assert( baseUnion );
    110         doLookup( baseUnion->get_members(), name, foundDecls );
     129        doLookup( baseUnion->members, name, foundDecls );
    111130}
    112131
     
    133152bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; }
    134153
     154AggregateDecl * EnumInstType::getAggr() const { return baseEnum; }
     155
    135156void EnumInstType::print( std::ostream &os, Indenter indent ) const {
    136157        using std::endl;
  • src/SynTree/ReferenceType.cc

    rf9feab8 r90152a4  
    1616#include "Type.h"
    1717#include "Expression.h"
     18#include "TypeSubstitution.h"
    1819#include "Common/utility.h"
    1920
     
    3536}
    3637
     38TypeSubstitution ReferenceType::genericSubstitution() const { return base->genericSubstitution(); }
     39
    3740void ReferenceType::print( std::ostream &os, Indenter indent ) const {
    3841        Type::print( os, indent );
  • src/SynTree/Statement.cc

    rf9feab8 r90152a4  
    3434Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
    3535
    36 void Statement::print( std::ostream & os, Indenter ) const {
     36void Statement::print( std::ostream & os, Indenter indent ) const {
    3737        if ( ! labels.empty() ) {
    38                 os << "Labels: {";
     38                os << indent << "... Labels: {";
    3939                for ( const Label & l : labels ) {
    4040                        os << l << ",";
     
    9494
    9595
     96DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {}
     97
     98void DirectiveStmt::print( std::ostream &os, Indenter ) const {
     99        os << "GCC Directive:" << directive << endl;
     100}
     101
     102
    96103const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
    97104
    98 BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticError ) :
     105BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
    99106        Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
    100107        //actually this is a syntactic error signaled by the parser
    101108        if ( type == BranchStmt::Goto && target.empty() ) {
    102                 throw SemanticError("goto without target");
    103         }
    104 }
    105 
    106 BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticError ) :
     109                SemanticError( target.get_statement()->location, "goto without target");
     110        }
     111}
     112
     113BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
    107114        Statement(), computedTarget( computedTarget ), type( type ) {
    108115        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
    109                 throw SemanticError("Computed target not valid in branch statement");
     116                SemanticError( computedTarget->location, "Computed target not valid in branch statement");
    110117        }
    111118}
     
    201208}
    202209
    203 CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
     210CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
    204211        Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
    205         if ( isDefault() && condition != 0 ) throw SemanticError("default case with condition: ", condition);
     212        if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
    206213}
    207214
     
    223230
    224231void CaseStmt::print( std::ostream &os, Indenter indent ) const {
    225         if ( isDefault() ) os << "Default ";
     232        if ( isDefault() ) os << indent << "Default ";
    226233        else {
    227                 os << "Case ";
     234                os << indent << "Case ";
    228235                condition->print( os, indent );
    229236        } // if
     
    231238
    232239        for ( Statement * stmt : stmts ) {
     240                os << indent+1;
    233241                stmt->print( os, indent+1 );
    234242        }
    235243}
    236244
    237 WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
    238         Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
     245WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ):
     246        Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
    239247}
    240248
     
    452460void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
    453461        os << "Waitfor Statement" << endl;
    454         os << indent << "... with block:" << endl << indent+1;
    455         // block->print( os, indent + 4 );
     462        indent += 1;
     463        for( auto & clause : clauses ) {
     464                os << indent << "target function :";
     465                if(clause.target.function) { clause.target.function->print(os, indent + 1); }
     466                os << endl << indent << "with arguments :" << endl;
     467                for( auto & thing : clause.target.arguments) {
     468                        if(thing) { thing->print(os, indent + 1); }
     469                }
     470                os << indent << " with statment :" << endl;
     471                if(clause.statement) { clause.statement->print(os, indent + 1); }
     472
     473                os << indent << " with condition :" << endl;
     474                if(clause.condition) { clause.condition->print(os, indent + 1); }
     475        }
     476
     477        os << indent << " timeout of :" << endl;
     478        if(timeout.time) { timeout.time->print(os, indent + 1); }
     479
     480        os << indent << " with statment :" << endl;
     481        if(timeout.statement) { timeout.statement->print(os, indent + 1); }
     482
     483        os << indent << " with condition :" << endl;
     484        if(timeout.condition) { timeout.condition->print(os, indent + 1); }
     485
     486
     487        os << indent << " else :" << endl;
     488        if(orelse.statement) { orelse.statement->print(os, indent + 1); }
     489
     490        os << indent << " with condition :" << endl;
     491        if(orelse.condition) { orelse.condition->print(os, indent + 1); }
    456492}
    457493
     
    468504void WithStmt::print( std::ostream & os, Indenter indent ) const {
    469505        os << "With statement" << endl;
     506        os << indent << "... with expressions: " << endl;
     507        printAll( exprs, os, indent+1 );
    470508        os << indent << "... with statement:" << endl << indent+1;
    471509        stmt->print( os, indent+1 );
     
    476514}
    477515
    478 void NullStmt::print( std::ostream &os, Indenter ) const {
     516void NullStmt::print( std::ostream &os, Indenter indent ) const {
    479517        os << "Null Statement" << endl;
     518        Statement::print( os, indent );
    480519}
    481520
  • src/SynTree/Statement.h

    rf9feab8 r90152a4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep  3 20:46:46 2017
    13 // Update Count     : 77
     12// Last Modified On : Thu Mar  8 14:53:02 2018
     13// Update Count     : 78
    1414//
    1515
     
    126126};
    127127
     128class DirectiveStmt : public Statement {
     129        public:
     130        std::string directive;
     131
     132        DirectiveStmt( const std::string & );
     133        virtual ~DirectiveStmt(){}
     134
     135        virtual DirectiveStmt * clone() const { return new DirectiveStmt( *this ); }
     136        virtual void accept( Visitor & v ) { v.visit( this ); }
     137        virtual Statement * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     138        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     139};
     140
    128141class IfStmt : public Statement {
    129142  public:
     
    179192        std::list<Statement *> stmts;
    180193
    181         CaseStmt( Expression *conditions, const std::list<Statement *> &stmts, bool isdef = false ) throw(SemanticError);
     194        CaseStmt( Expression *conditions, const std::list<Statement *> &stmts, bool isdef = false ) throw (SemanticErrorException);
    182195        CaseStmt( const CaseStmt &other );
    183196        virtual ~CaseStmt();
     
    207220        Expression *condition;
    208221        Statement *body;
     222        std::list<Statement *> initialization;
    209223        bool isDoWhile;
    210224
    211225        WhileStmt( Expression *condition,
    212                Statement *body, bool isDoWhile = false );
     226               Statement *body, std::list<Statement *> & initialization, bool isDoWhile = false );
    213227        WhileStmt( const WhileStmt &other );
    214228        virtual ~WhileStmt();
     
    255269class BranchStmt : public Statement {
    256270  public:
    257         enum Type { Goto = 0, Break, Continue };
     271        enum Type { Goto = 0, Break, Continue, FallThrough, FallThroughDefault };
    258272
    259273        // originalTarget kept for error messages.
     
    263277        Type type;
    264278
    265         BranchStmt( Label target, Type ) throw (SemanticError);
    266         BranchStmt( Expression *computedTarget, Type ) throw (SemanticError);
     279        BranchStmt( Label target, Type ) throw (SemanticErrorException);
     280        BranchStmt( Expression *computedTarget, Type ) throw (SemanticErrorException);
    267281
    268282        Label get_originalTarget() { return originalTarget; }
  • src/SynTree/SynTree.h

    rf9feab8 r90152a4  
    3838class TypedefDecl;
    3939class AsmDecl;
     40class StaticAssertDecl;
    4041
    4142class Statement;
     
    4344class ExprStmt;
    4445class AsmStmt;
     46class DirectiveStmt;
    4547class IfStmt;
    4648class WhileStmt;
     
    6870class LabelAddressExpr;
    6971class CastExpr;
     72class KeywordCastExpr;
    7073class VirtualCastExpr;
    7174class MemberExpr;
     
    97100class UntypedInitExpr;
    98101class InitExpr;
     102class DeletedExpr;
     103class DefaultArgExpr;
     104class GenericExpr;
    99105
    100106class Type;
     
    104110class ArrayType;
    105111class ReferenceType;
     112class QualifiedType;
    106113class FunctionType;
    107114class ReferenceToType;
     
    117124class ZeroType;
    118125class OneType;
     126class GlobalScopeType;
    119127
    120128class Designation;
  • src/SynTree/Type.cc

    rf9feab8 r90152a4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep 25 15:16:32 2017
    13 // Update Count     : 38
     12// Last Modified On : Fri Jun 22 10:17:19 2018
     13// Update Count     : 39
    1414//
    1515#include "Type.h"
    1616
    17 #include "Attribute.h"               // for Attribute
    18 #include "Common/utility.h"          // for cloneAll, deleteAll, printAll
    19 #include "InitTweak/InitTweak.h"     // for getPointerBase
    20 #include "SynTree/BaseSyntaxNode.h"  // for BaseSyntaxNode
    21 #include "SynTree/Declaration.h"     // for TypeDecl
     17#include "Attribute.h"                // for Attribute
     18#include "Common/utility.h"           // for cloneAll, deleteAll, printAll
     19#include "InitTweak/InitTweak.h"      // for getPointerBase
     20#include "SynTree/BaseSyntaxNode.h"   // for BaseSyntaxNode
     21#include "SynTree/Declaration.h"      // for TypeDecl
     22#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
    2223
    2324using namespace std;
    2425
    25 const char *BasicType::typeNames[BasicType::NUMBER_OF_BASIC_TYPES] = {
     26const char *BasicType::typeNames[] = {
    2627        "_Bool",
    2728        "char",
     
    4748        "__int128",
    4849        "unsigned __int128",
     50        "__float80",
     51        "__float128"
    4952};
     53static_assert(
     54        sizeof(BasicType::typeNames)/sizeof(BasicType::typeNames[0]) == BasicType::NUMBER_OF_BASIC_TYPES,
     55        "Each basic type name should have a corresponding kind enum value"
     56);
    5057
    5158Type::Type( const Qualifiers &tq, const std::list< Attribute * > & attributes ) : tq( tq ), attributes( attributes ) {}
     
    6269
    6370// These must remain in the same order as the corresponding bit fields.
    64 const char * Type::FuncSpecifiersNames[] = { "inline", "fortran", "_Noreturn" };
     71const char * Type::FuncSpecifiersNames[] = { "inline", "_Noreturn", "fortran" };
    6572const char * Type::StorageClassesNames[] = { "extern", "static", "auto", "register", "_Thread_local" };
    6673const char * Type::QualifiersNames[] = { "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic" };
     
    8188int Type::referenceDepth() const { return 0; }
    8289
     90TypeSubstitution Type::genericSubstitution() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); }
     91
    8392void Type::print( std::ostream &os, Indenter indent ) const {
    8493        if ( ! forall.empty() ) {
     
    96105}
    97106
     107
     108QualifiedType::QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ) : Type( tq, {} ), parent( parent ), child( child ) {
     109}
     110
     111QualifiedType::QualifiedType( const QualifiedType & other ) : Type( other ), parent( maybeClone( other.parent ) ), child( maybeClone( other.child ) ) {
     112}
     113
     114QualifiedType::~QualifiedType() {
     115        delete parent;
     116        delete child;
     117}
     118
     119void QualifiedType::print( std::ostream & os, Indenter indent ) const {
     120        os << "Qualified Type: " << endl;
     121        os << indent+1;
     122        parent->print( os, indent+1 );
     123        os << endl << indent+1;
     124        child->print( os, indent+1 );
     125        os << endl;
     126        Type::print( os, indent+1 );
     127}
     128
     129GlobalScopeType::GlobalScopeType() : Type( Type::Qualifiers(), {} ) {}
     130
     131void GlobalScopeType::print( std::ostream & os, Indenter ) const {
     132        os << "Global Scope Type" << endl;
     133}
     134
     135
    98136// Empty Variable declarations:
    99137const Type::FuncSpecifiers noFuncSpecifiers;
  • src/SynTree/Type.h

    rf9feab8 r90152a4  
    178178        virtual bool isComplete() const { return true; }
    179179
    180         virtual AggregateDecl * getAggr() {     assertf( false, "Non-aggregate type: %s", toString( this ).c_str() ); }
     180        virtual AggregateDecl * getAggr() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); }
     181
     182        virtual TypeSubstitution genericSubstitution() const;
    181183
    182184        virtual Type *clone() const = 0;
     
    229231                SignedInt128,
    230232                UnsignedInt128,
     233                Float80,
     234                Float128,
    231235                NUMBER_OF_BASIC_TYPES
    232236        } kind;
     
    311315};
    312316
     317class QualifiedType : public Type {
     318public:
     319        Type * parent;
     320        Type * child;
     321
     322        QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child );
     323        QualifiedType( const QualifiedType & tq );
     324        virtual ~QualifiedType();
     325
     326        virtual QualifiedType *clone() const override { return new QualifiedType( *this ); }
     327        virtual void accept( Visitor & v ) override { v.visit( this ); }
     328        virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); }
     329        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
     330};
     331
    313332class ReferenceType : public Type {
    314333public:
     
    328347        // the number of values are disallowed.
    329348        virtual unsigned size() const override { return base->size(); }
     349
     350        virtual TypeSubstitution genericSubstitution() const override;
    330351
    331352        virtual ReferenceType *clone() const override { return new ReferenceType( *this ); }
     
    356377        bool isTtype() const;
    357378
     379        bool isUnprototyped() const { return isVarArgs && parameters.size() == 0; }
     380
    358381        virtual FunctionType *clone() const override { return new FunctionType( *this ); }
    359382        virtual void accept( Visitor & v ) override { v.visit( this ); }
     
    404427        /// Accesses generic parameters of base struct (NULL if none such)
    405428        std::list<TypeDecl*> * get_baseParameters();
     429        const std::list<TypeDecl*> * get_baseParameters() const;
    406430
    407431        virtual bool isComplete() const override;
    408432
    409         virtual AggregateDecl * getAggr() override;
     433        virtual AggregateDecl * getAggr() const override;
     434
     435        virtual TypeSubstitution genericSubstitution() const override;
    410436
    411437        /// Looks up the members of this struct named "name" and places them into "foundDecls".
     
    437463
    438464        /// Accesses generic parameters of base union (NULL if none such)
    439         std::list< TypeDecl * > * get_baseParameters();
     465        std::list<TypeDecl*> * get_baseParameters();
     466        const std::list<TypeDecl*> * get_baseParameters() const;
    440467
    441468        virtual bool isComplete() const override;
    442469
    443         virtual AggregateDecl * getAggr() override;
     470        virtual AggregateDecl * getAggr() const override;
     471
     472        virtual TypeSubstitution genericSubstitution() const override;
    444473
    445474        /// looks up the members of this union named "name" and places them into "foundDecls"
     
    471500
    472501        virtual bool isComplete() const override;
     502
     503        virtual AggregateDecl * getAggr() const override;
    473504
    474505        virtual EnumInstType *clone() const override { return new EnumInstType( *this ); }
     
    651682};
    652683
     684class GlobalScopeType : public Type {
     685  public:
     686        GlobalScopeType();
     687
     688        virtual GlobalScopeType *clone() const override { return new GlobalScopeType( *this ); }
     689        virtual void accept( Visitor & v ) override { v.visit( this ); }
     690        virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); }
     691        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
     692};
     693
    653694// Local Variables: //
    654695// tab-width: 4 //
  • src/SynTree/TypeSubstitution.cc

    rf9feab8 r90152a4  
    106106}
    107107
     108namespace {
     109        struct EnvTrimmer {
     110                TypeSubstitution * env, * newEnv;
     111                EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
     112                void previsit( TypeDecl * tyDecl ) {
     113                        // transfer known bindings for seen type variables
     114                        if ( Type * t = env->lookup( tyDecl->name ) ) {
     115                                newEnv->add( tyDecl->name, t );
     116                        }
     117                }
     118        };
     119} // namespace
     120
     121/// reduce environment to just the parts that are referenced in a given expression
     122TypeSubstitution * TypeSubstitution::newFromExpr( Expression * expr, TypeSubstitution * env ) {
     123        if ( env ) {
     124                TypeSubstitution * newEnv = new TypeSubstitution();
     125                PassVisitor<EnvTrimmer> trimmer( env, newEnv );
     126                expr->accept( trimmer );
     127                return newEnv;
     128        }
     129        return nullptr;
     130}
     131
    108132void TypeSubstitution::normalize() {
     133        PassVisitor<Substituter> sub( *this, true );
    109134        do {
    110                 subCount = 0;
    111                 freeOnly = true;
     135                sub.pass.subCount = 0;
     136                sub.pass.freeOnly = true;
    112137                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
    113                         i->second = i->second->acceptMutator( *this );
     138                        i->second = i->second->acceptMutator( sub );
    114139                }
    115         } while ( subCount );
    116 }
    117 
    118 Type * TypeSubstitution::mutate( TypeInstType *inst ) {
    119         BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() );
     140        } while ( sub.pass.subCount );
     141}
     142
     143Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) {
     144        BoundVarsType::const_iterator bound = boundVars.find( inst->name );
    120145        if ( bound != boundVars.end() ) return inst;
    121146
    122         TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );
    123         if ( i == typeEnv.end() ) {
     147        TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() );
     148        if ( i == sub.typeEnv.end() ) {
    124149                return inst;
    125150        } else {
    126 ///         std::cout << "found " << inst->get_name() << ", replacing with ";
    127 ///         i->second->print( std::cout );
    128 ///         std::cout << std::endl;
     151                // cut off infinite loop for the case where a type is bound to itself.
     152                // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here.
     153                // TODO: investigate preventing type variables from being bound to themselves in the first place.
     154                if ( TypeInstType * replacement = dynamic_cast< TypeInstType * >( i->second ) ) {
     155                        if ( inst->name == replacement->name ) {
     156                                return inst;
     157                        }
     158                }
     159                // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl;
    129160                subCount++;
    130                 Type *newtype = i->second->clone();
     161                Type * newtype = i->second->clone();
    131162                newtype->get_qualifiers() |= inst->get_qualifiers();
    132163                delete inst;
    133                 return newtype;
    134         } // if
    135 }
    136 
    137 Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) {
    138         VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() );
    139         if ( i == varEnv.end() ) {
     164                // Note: need to recursively apply substitution to the new type because normalize does not substitute bound vars, but bound vars must be substituted when not in freeOnly mode.
     165                return newtype->acceptMutator( *visitor );
     166        } // if
     167}
     168
     169Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) {
     170        VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
     171        if ( i == sub.varEnv.end() ) {
    140172                return nameExpr;
    141173        } else {
     
    146178}
    147179
    148 template< typename TypeClass >
    149 Type *TypeSubstitution::handleType( TypeClass *type ) {
    150         ValueGuard<BoundVarsType> oldBoundVars( boundVars );
     180void TypeSubstitution::Substituter::premutate( Type * type ) {
     181        GuardValue( boundVars );
    151182        // bind type variables from forall-qualifiers
    152183        if ( freeOnly ) {
    153                 for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
    154                         boundVars.insert( (*tyvar )->get_name() );
     184                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
     185                        boundVars.insert( (*tyvar)->name );
    155186                } // for
    156187        } // if
    157         Type *ret = Mutator::mutate( type );
    158         return ret;
    159188}
    160189
    161190template< typename TypeClass >
    162 Type *TypeSubstitution::handleAggregateType( TypeClass *type ) {
    163         ValueGuard<BoundVarsType> oldBoundVars( boundVars );
     191void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) {
     192        GuardValue( boundVars );
    164193        // bind type variables from forall-qualifiers
    165194        if ( freeOnly ) {
    166                 for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
    167                         boundVars.insert( (*tyvar )->get_name() );
     195                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
     196                        boundVars.insert( (*tyvar)->name );
    168197                } // for
    169198                // bind type variables from generic type instantiations
    170199                std::list< TypeDecl* > *baseParameters = type->get_baseParameters();
    171                 if ( baseParameters && ! type->get_parameters().empty() ) {
     200                if ( baseParameters && ! type->parameters.empty() ) {
    172201                        for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) {
    173                                 boundVars.insert( (*tyvar)->get_name() );
     202                                boundVars.insert( (*tyvar)->name );
    174203                        } // for
    175204                } // if
    176205        } // if
    177         Type *ret = Mutator::mutate( type );
    178         return ret;
    179 }
    180 
    181 Type * TypeSubstitution::mutate( VoidType *voidType ) {
    182         return handleType( voidType );
    183 }
    184 
    185 Type * TypeSubstitution::mutate( BasicType *basicType ) {
    186         return handleType( basicType );
    187 }
    188 
    189 Type * TypeSubstitution::mutate( PointerType *pointerType ) {
    190         return handleType( pointerType );
    191 }
    192 
    193 Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
    194         return handleType( arrayType );
    195 }
    196 
    197 Type * TypeSubstitution::mutate( FunctionType *functionType ) {
    198         return handleType( functionType );
    199 }
    200 
    201 Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
    202         return handleAggregateType( aggregateUseType );
    203 }
    204 
    205 Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
    206         return handleAggregateType( aggregateUseType );
    207 }
    208 
    209 Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
    210         return handleType( aggregateUseType );
    211 }
    212 
    213 Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
    214         return handleType( aggregateUseType );
    215 }
    216 
    217 Type * TypeSubstitution::mutate( TupleType *tupleType ) {
    218         return handleType( tupleType );
    219 }
    220 
    221 Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
    222         return handleType( varArgsType );
    223 }
    224 
    225 Type * TypeSubstitution::mutate( ZeroType *zeroType ) {
    226         return handleType( zeroType );
    227 }
    228 
    229 Type * TypeSubstitution::mutate( OneType *oneType ) {
    230         return handleType( oneType );
     206}
     207
     208void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) {
     209        handleAggregateType( aggregateUseType );
     210}
     211
     212void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) {
     213        handleAggregateType( aggregateUseType );
    231214}
    232215
  • src/SynTree/TypeSubstitution.h

    rf9feab8 r90152a4  
    2727#include "SynTree/Declaration.h"   // for TypeDecl, Declaration (ptr only)
    2828#include "SynTree/Expression.h"    // for Expression (ptr only), NameExpr (p...
    29 #include "SynTree/Mutator.h"       // for Mutator
    3029#include "SynTree/Type.h"          // for Type, ArrayType (ptr only), BasicT...
    3130
    32 class TypeSubstitution : public Mutator {
    33         typedef Mutator Parent;
     31class TypeSubstitution {
    3432  public:
    3533        TypeSubstitution();
     
    5755        void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
    5856
     57        /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr
     58        static TypeSubstitution * newFromExpr( Expression * expr, TypeSubstitution * env );
     59
    5960        void normalize();
    6061
     
    6465        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
    6566  private:
    66         virtual Type* mutate(TypeInstType *aggregateUseType);
    67         virtual Expression* mutate(NameExpr *nameExpr);
    68 
    69         /// Records type variable bindings from forall-statements
    70         template< typename TypeClass > Type *handleType( TypeClass *type );
    71         /// Records type variable bindings from forall-statements and instantiations of generic types
    72         template< typename TypeClass > Type *handleAggregateType( TypeClass *type );
    73 
    74         virtual Type* mutate(VoidType *basicType);
    75         virtual Type* mutate(BasicType *basicType);
    76         virtual Type* mutate(PointerType *pointerType);
    77         virtual Type* mutate(ArrayType *arrayType);
    78         virtual Type* mutate(FunctionType *functionType);
    79         virtual Type* mutate(StructInstType *aggregateUseType);
    80         virtual Type* mutate(UnionInstType *aggregateUseType);
    81         virtual Type* mutate(EnumInstType *aggregateUseType);
    82         virtual Type* mutate(TraitInstType *aggregateUseType);
    83         virtual Type* mutate(TupleType *tupleType);
    84         virtual Type* mutate(VarArgsType *varArgsType);
    85         virtual Type* mutate(ZeroType *zeroType);
    86         virtual Type* mutate(OneType *oneType);
     67
     68        // Mutator that performs the substitution
     69        struct Substituter;
    8770
    8871        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
     
    9780        typedef std::map< std::string, Type* > TypeEnvType;
    9881        typedef std::map< std::string, Expression* > VarEnvType;
    99         typedef std::set< std::string > BoundVarsType;
    10082        TypeEnvType typeEnv;
    10183        VarEnvType varEnv;
    102         BoundVarsType boundVars;
    103         int subCount;
    104         bool freeOnly;
     84
     85  public:
     86        // has to come after declaration of typeEnv
     87        auto begin()       -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
     88        auto   end()       -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
     89        auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
     90        auto   end() const -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
    10591};
    10692
     
    122108                                } // if
    123109                        } else {
    124                                 throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal );
     110                                SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) );
    125111                        } // if
    126112                } else {
     
    134120
    135121template< typename FormalIterator, typename ActualIterator >
    136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
    137 {
     122TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
    138123        add( formalBegin, formalEnd, actualBegin );
    139124}
     125
     126// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
     127// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
     128#include "Common/PassVisitor.h"
     129
     130// definitition must happen after PassVisitor is included so that WithGuards can be used
     131struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> {
     132                Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
     133
     134                Type * postmutate( TypeInstType * aggregateUseType );
     135                Expression * postmutate( NameExpr * nameExpr );
     136
     137                /// Records type variable bindings from forall-statements
     138                void premutate( Type * type );
     139                /// Records type variable bindings from forall-statements and instantiations of generic types
     140                template< typename TypeClass > void handleAggregateType( TypeClass * type );
     141
     142                void premutate( StructInstType * aggregateUseType );
     143                void premutate( UnionInstType * aggregateUseType );
     144
     145                TypeSubstitution & sub;
     146                int subCount = 0;
     147                bool freeOnly;
     148                typedef std::set< std::string > BoundVarsType;
     149                BoundVarsType boundVars;
     150};
    140151
    141152template< typename SynTreeClass >
    142153int TypeSubstitution::apply( SynTreeClass *&input ) {
    143154        assert( input );
    144         subCount = 0;
    145         freeOnly = false;
    146         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    147         assert( input );
    148 ///     std::cout << "substitution result is: ";
    149 ///     newType->print( std::cout );
    150 ///     std::cout << std::endl;
    151         return subCount;
     155        PassVisitor<Substituter> sub( *this, false );
     156        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
     157        assert( input );
     158///     std::cerr << "substitution result is: ";
     159///     newType->print( std::cerr );
     160///     std::cerr << std::endl;
     161        return sub.pass.subCount;
    152162}
    153163
     
    155165int TypeSubstitution::applyFree( SynTreeClass *&input ) {
    156166        assert( input );
    157         subCount = 0;
    158         freeOnly = true;
    159         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    160         assert( input );
    161 ///     std::cout << "substitution result is: ";
    162 ///     newType->print( std::cout );
    163 ///     std::cout << std::endl;
    164         return subCount;
     167        PassVisitor<Substituter> sub( *this, true );
     168        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
     169        assert( input );
     170///     std::cerr << "substitution result is: ";
     171///     newType->print( std::cerr );
     172///     std::cerr << std::endl;
     173        return sub.pass.subCount;
    165174}
    166175
  • src/SynTree/Visitor.h

    rf9feab8 r90152a4  
    2121class Visitor {
    2222  protected:
    23         Visitor();
    24         virtual ~Visitor();
     23        Visitor() = default;
     24        virtual ~Visitor() = default;
    2525  public:
    2626        // visit: Default implementation of all functions visits the children
    2727        // of the given syntax node, but performs no other action.
    2828
    29         virtual void visit( ObjectDecl * objectDecl );
    30         virtual void visit( FunctionDecl * functionDecl );
    31         virtual void visit( StructDecl * aggregateDecl );
    32         virtual void visit( UnionDecl * aggregateDecl );
    33         virtual void visit( EnumDecl * aggregateDecl );
    34         virtual void visit( TraitDecl * aggregateDecl );
    35         virtual void visit( TypeDecl * typeDecl );
    36         virtual void visit( TypedefDecl * typeDecl );
    37         virtual void visit( AsmDecl * asmDecl );
     29        virtual void visit( ObjectDecl * objectDecl ) = 0;
     30        virtual void visit( FunctionDecl * functionDecl ) = 0;
     31        virtual void visit( StructDecl * aggregateDecl ) = 0;
     32        virtual void visit( UnionDecl * aggregateDecl ) = 0;
     33        virtual void visit( EnumDecl * aggregateDecl ) = 0;
     34        virtual void visit( TraitDecl * aggregateDecl ) = 0;
     35        virtual void visit( TypeDecl * typeDecl ) = 0;
     36        virtual void visit( TypedefDecl * typeDecl ) = 0;
     37        virtual void visit( AsmDecl * asmDecl ) = 0;
     38        virtual void visit( StaticAssertDecl * assertDecl ) = 0;
    3839
    39         virtual void visit( CompoundStmt * compoundStmt );
    40         virtual void visit( ExprStmt * exprStmt );
    41         virtual void visit( AsmStmt * asmStmt );
    42         virtual void visit( IfStmt * ifStmt );
    43         virtual void visit( WhileStmt * whileStmt );
    44         virtual void visit( ForStmt * forStmt );
    45         virtual void visit( SwitchStmt * switchStmt );
    46         virtual void visit( CaseStmt * caseStmt );
    47         virtual void visit( BranchStmt * branchStmt );
    48         virtual void visit( ReturnStmt * returnStmt );
    49         virtual void visit( ThrowStmt * throwStmt );
    50         virtual void visit( TryStmt * tryStmt );
    51         virtual void visit( CatchStmt * catchStmt );
    52         virtual void visit( FinallyStmt * finallyStmt );
    53         virtual void visit( WaitForStmt * waitforStmt );
    54         virtual void visit( WithStmt * withStmt );
    55         virtual void visit( NullStmt * nullStmt );
    56         virtual void visit( DeclStmt * declStmt );
    57         virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt );
     40        virtual void visit( CompoundStmt * compoundStmt ) = 0;
     41        virtual void visit( ExprStmt * exprStmt ) = 0;
     42        virtual void visit( AsmStmt * asmStmt ) = 0;
     43        virtual void visit( DirectiveStmt * directiveStmt ) = 0;
     44        virtual void visit( IfStmt * ifStmt ) = 0;
     45        virtual void visit( WhileStmt * whileStmt ) = 0;
     46        virtual void visit( ForStmt * forStmt ) = 0;
     47        virtual void visit( SwitchStmt * switchStmt ) = 0;
     48        virtual void visit( CaseStmt * caseStmt ) = 0;
     49        virtual void visit( BranchStmt * branchStmt ) = 0;
     50        virtual void visit( ReturnStmt * returnStmt ) = 0;
     51        virtual void visit( ThrowStmt * throwStmt ) = 0;
     52        virtual void visit( TryStmt * tryStmt ) = 0;
     53        virtual void visit( CatchStmt * catchStmt ) = 0;
     54        virtual void visit( FinallyStmt * finallyStmt ) = 0;
     55        virtual void visit( WaitForStmt * waitforStmt ) = 0;
     56        virtual void visit( WithStmt * withStmt ) = 0;
     57        virtual void visit( NullStmt * nullStmt ) = 0;
     58        virtual void visit( DeclStmt * declStmt ) = 0;
     59        virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) = 0;
    5860
    59         virtual void visit( ApplicationExpr * applicationExpr );
    60         virtual void visit( UntypedExpr * untypedExpr );
    61         virtual void visit( NameExpr * nameExpr );
    62         virtual void visit( CastExpr * castExpr );
    63         virtual void visit( VirtualCastExpr * castExpr );
    64         virtual void visit( AddressExpr * addressExpr );
    65         virtual void visit( LabelAddressExpr * labAddressExpr );
    66         virtual void visit( UntypedMemberExpr * memberExpr );
    67         virtual void visit( MemberExpr * memberExpr );
    68         virtual void visit( VariableExpr * variableExpr );
    69         virtual void visit( ConstantExpr * constantExpr );
    70         virtual void visit( SizeofExpr * sizeofExpr );
    71         virtual void visit( AlignofExpr * alignofExpr );
    72         virtual void visit( UntypedOffsetofExpr * offsetofExpr );
    73         virtual void visit( OffsetofExpr * offsetofExpr );
    74         virtual void visit( OffsetPackExpr * offsetPackExpr );
    75         virtual void visit( AttrExpr * attrExpr );
    76         virtual void visit( LogicalExpr * logicalExpr );
    77         virtual void visit( ConditionalExpr * conditionalExpr );
    78         virtual void visit( CommaExpr * commaExpr );
    79         virtual void visit( TypeExpr * typeExpr );
    80         virtual void visit( AsmExpr * asmExpr );
    81         virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
    82         virtual void visit( ConstructorExpr *  ctorExpr );
    83         virtual void visit( CompoundLiteralExpr * compLitExpr );
    84         virtual void visit( RangeExpr * rangeExpr );
    85         virtual void visit( UntypedTupleExpr * tupleExpr );
    86         virtual void visit( TupleExpr * tupleExpr );
    87         virtual void visit( TupleIndexExpr * tupleExpr );
    88         virtual void visit( TupleAssignExpr * assignExpr );
    89         virtual void visit( StmtExpr *  stmtExpr );
    90         virtual void visit( UniqueExpr *  uniqueExpr );
    91         virtual void visit( UntypedInitExpr *  initExpr );
    92         virtual void visit( InitExpr *  initExpr );
     61        virtual void visit( ApplicationExpr * applicationExpr ) = 0;
     62        virtual void visit( UntypedExpr * untypedExpr ) = 0;
     63        virtual void visit( NameExpr * nameExpr ) = 0;
     64        virtual void visit( CastExpr * castExpr ) = 0;
     65        virtual void visit( KeywordCastExpr * castExpr ) = 0;
     66        virtual void visit( VirtualCastExpr * castExpr ) = 0;
     67        virtual void visit( AddressExpr * addressExpr ) = 0;
     68        virtual void visit( LabelAddressExpr * labAddressExpr ) = 0;
     69        virtual void visit( UntypedMemberExpr * memberExpr ) = 0;
     70        virtual void visit( MemberExpr * memberExpr ) = 0;
     71        virtual void visit( VariableExpr * variableExpr ) = 0;
     72        virtual void visit( ConstantExpr * constantExpr ) = 0;
     73        virtual void visit( SizeofExpr * sizeofExpr ) = 0;
     74        virtual void visit( AlignofExpr * alignofExpr ) = 0;
     75        virtual void visit( UntypedOffsetofExpr * offsetofExpr ) = 0;
     76        virtual void visit( OffsetofExpr * offsetofExpr ) = 0;
     77        virtual void visit( OffsetPackExpr * offsetPackExpr ) = 0;
     78        virtual void visit( AttrExpr * attrExpr ) = 0;
     79        virtual void visit( LogicalExpr * logicalExpr ) = 0;
     80        virtual void visit( ConditionalExpr * conditionalExpr ) = 0;
     81        virtual void visit( CommaExpr * commaExpr ) = 0;
     82        virtual void visit( TypeExpr * typeExpr ) = 0;
     83        virtual void visit( AsmExpr * asmExpr ) = 0;
     84        virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) = 0;
     85        virtual void visit( ConstructorExpr *  ctorExpr ) = 0;
     86        virtual void visit( CompoundLiteralExpr * compLitExpr ) = 0;
     87        virtual void visit( RangeExpr * rangeExpr ) = 0;
     88        virtual void visit( UntypedTupleExpr * tupleExpr ) = 0;
     89        virtual void visit( TupleExpr * tupleExpr ) = 0;
     90        virtual void visit( TupleIndexExpr * tupleExpr ) = 0;
     91        virtual void visit( TupleAssignExpr * assignExpr ) = 0;
     92        virtual void visit( StmtExpr *  stmtExpr ) = 0;
     93        virtual void visit( UniqueExpr *  uniqueExpr ) = 0;
     94        virtual void visit( UntypedInitExpr *  initExpr ) = 0;
     95        virtual void visit( InitExpr *  initExpr ) = 0;
     96        virtual void visit( DeletedExpr * delExpr ) = 0;
     97        virtual void visit( DefaultArgExpr * argExpr ) = 0;
     98        virtual void visit( GenericExpr * genExpr ) = 0;
    9399
    94         virtual void visit( VoidType * basicType );
    95         virtual void visit( BasicType * basicType );
    96         virtual void visit( PointerType * pointerType );
    97         virtual void visit( ArrayType * arrayType );
    98         virtual void visit( ReferenceType * refType );
    99         virtual void visit( FunctionType * functionType );
    100         virtual void visit( StructInstType * aggregateUseType );
    101         virtual void visit( UnionInstType * aggregateUseType );
    102         virtual void visit( EnumInstType * aggregateUseType );
    103         virtual void visit( TraitInstType * aggregateUseType );
    104         virtual void visit( TypeInstType * aggregateUseType );
    105         virtual void visit( TupleType * tupleType );
    106         virtual void visit( TypeofType * typeofType );
    107         virtual void visit( AttrType * attrType );
    108         virtual void visit( VarArgsType * varArgsType );
    109         virtual void visit( ZeroType * zeroType );
    110         virtual void visit( OneType * oneType );
     100        virtual void visit( VoidType * basicType ) = 0;
     101        virtual void visit( BasicType * basicType ) = 0;
     102        virtual void visit( PointerType * pointerType ) = 0;
     103        virtual void visit( ArrayType * arrayType ) = 0;
     104        virtual void visit( ReferenceType * refType ) = 0;
     105        virtual void visit( QualifiedType * qualType ) = 0;
     106        virtual void visit( FunctionType * functionType ) = 0;
     107        virtual void visit( StructInstType * aggregateUseType ) = 0;
     108        virtual void visit( UnionInstType * aggregateUseType ) = 0;
     109        virtual void visit( EnumInstType * aggregateUseType ) = 0;
     110        virtual void visit( TraitInstType * aggregateUseType ) = 0;
     111        virtual void visit( TypeInstType * aggregateUseType ) = 0;
     112        virtual void visit( TupleType * tupleType ) = 0;
     113        virtual void visit( TypeofType * typeofType ) = 0;
     114        virtual void visit( AttrType * attrType ) = 0;
     115        virtual void visit( VarArgsType * varArgsType ) = 0;
     116        virtual void visit( ZeroType * zeroType ) = 0;
     117        virtual void visit( OneType * oneType ) = 0;
     118        virtual void visit( GlobalScopeType * globalType ) = 0;
    111119
    112         virtual void visit( Designation * designation );
    113         virtual void visit( SingleInit * singleInit );
    114         virtual void visit( ListInit * listInit );
    115         virtual void visit( ConstructorInit * ctorInit );
     120        virtual void visit( Designation * designation ) = 0;
     121        virtual void visit( SingleInit * singleInit ) = 0;
     122        virtual void visit( ListInit * listInit ) = 0;
     123        virtual void visit( ConstructorInit * ctorInit ) = 0;
    116124
    117         virtual void visit( Subrange * subrange );
     125        virtual void visit( Subrange * subrange ) = 0;
    118126
    119         virtual void visit( Constant * constant );
     127        virtual void visit( Constant * constant ) = 0;
    120128
    121         virtual void visit( Attribute * attribute );
    122   private:
    123         virtual void handleAggregateDecl( AggregateDecl *aggregateDecl );
    124         virtual void handleNamedTypeDecl( NamedTypeDecl *typeDecl );
    125         virtual void handleReferenceToType( ReferenceToType *aggregateUseType );
     129        virtual void visit( Attribute * attribute ) = 0;
    126130};
    127131
     
    135139template< typename Container, typename VisitorType >
    136140inline void acceptAll( Container &container, VisitorType &visitor ) {
    137         SemanticError errors;
     141        SemanticErrorException errors;
    138142        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    139143                try {
     
    141145                                (*i)->accept( visitor );
    142146                        }
    143                 } catch( SemanticError &e ) {
    144                         e.set_location( (*i)->location );
     147                } catch( SemanticErrorException &e ) {
    145148                        errors.append( e );
    146149                }
  • src/SynTree/module.mk

    rf9feab8 r90152a4  
    4646       SynTree/TypeDecl.cc \
    4747       SynTree/Initializer.cc \
    48        SynTree/Visitor.cc \
    49        SynTree/Mutator.cc \
    5048       SynTree/TypeSubstitution.cc \
    5149       SynTree/Attribute.cc \
    52        SynTree/VarExprReplacer.cc
     50       SynTree/DeclReplacer.cc
    5351
Note: See TracChangeset for help on using the changeset viewer.