Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Expression.cc

    r0f79853 r487845d7  
    5050}
    5151
    52 void 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 
    5952Expression::~Expression() {
    6053        delete env;
     
    8881        constant.print( os );
    8982        Expression::print( os, indent );
    90 }
    91 
    92 long 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 " );
    10383}
    10484
     
    11595        //      assert( inst->baseEnum );
    11696        //      EnumDecl * decl = inst->baseEnum;
    117         //      long long int value;
    118         //      if ( decl->valueOf( var, value ) ) {
    119         //              type->set_lvalue( false );
     97        //      for ( Declaration * member : decl->members ) {
     98        //              if ( member == _var ) {
     99        //                      type->set_lvalue( false );
     100        //              }
    120101        //      }
    121102        // }
     
    278259}
    279260
    280 CastExpr::CastExpr( Expression *arg, Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
     261CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
    281262        set_result(toType);
    282263}
    283264
    284 CastExpr::CastExpr( Expression *arg, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
     265CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) {
    285266        set_result( new VoidType( Type::Qualifiers() ) );
    286267}
    287268
    288 CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) {
     269CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
    289270}
    290271
     
    306287}
    307288
    308 KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) {
    309 }
    310 
    311 KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
    312 }
    313 
    314 KeywordCastExpr::~KeywordCastExpr() {
    315         delete arg;
    316 }
    317 
    318 const 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 
    329 void 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 
    337289VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
    338290        set_result(toType);
     
    381333}
    382334
     335namespace {
     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
    383350MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) :
    384351                Expression(), member(member), aggregate(aggregate) {
    385352        assert( member );
    386353        assert( aggregate );
    387         assert( aggregate->result );
    388 
    389         TypeSubstitution sub = aggregate->result->genericSubstitution();
     354
     355        TypeSubstitution sub( makeSub( aggregate->get_result() ) );
    390356        Type * res = member->get_type()->clone();
    391357        sub.apply( res );
     
    437403                } else {
    438404                        // references have been removed, in which case dereference returns an lvalue of the base type.
    439                         ret->result->set_lvalue( true );
     405                        ret->get_result()->set_lvalue( true );
    440406                }
    441407        }
     
    619585
    620586StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
    621         computeResult();
     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        }
    622598}
    623599StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
     
    629605        deleteAll( dtors );
    630606        deleteAll( returnDecls );
    631 }
    632 void 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         }
    651607}
    652608void StmtExpr::print( std::ostream &os, Indenter indent ) const {
     
    732688}
    733689
    734 DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
    735         assert( expr->result );
    736         result = expr->result->clone();
    737 }
    738 DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
    739 DeletedExpr::~DeletedExpr() {
    740         delete expr;
    741 }
    742 
    743 void 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 
    751 DefaultArgExpr::DefaultArgExpr( Expression * expr ) : expr( expr ) {
    752         assert( expr->result );
    753         result = expr->result->clone();
    754 }
    755 DefaultArgExpr::DefaultArgExpr( const DefaultArgExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ) {}
    756 DefaultArgExpr::~DefaultArgExpr() {
    757         delete expr;
    758 }
    759 
    760 void 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 
    765 GenericExpr::Association::Association( Type * type, Expression * expr ) : type( type ), expr( expr ), isDefault( false ) {}
    766 GenericExpr::Association::Association( Expression * expr ) : type( nullptr ), expr( expr ), isDefault( true ) {}
    767 GenericExpr::Association::Association( const Association & other ) : type( maybeClone( other.type ) ), expr( maybeClone( other.expr ) ), isDefault( other.isDefault ) {}
    768 GenericExpr::Association::~Association() {
    769         delete type;
    770         delete expr;
    771 }
    772 
    773 GenericExpr::GenericExpr( Expression * control, const std::list<Association> & assoc ) : Expression(), control( control ), associations( assoc ) {}
    774 GenericExpr::GenericExpr( const GenericExpr & other ) : Expression(other), control( maybeClone( other.control ) ), associations( other.associations ) {
    775 }
    776 GenericExpr::~GenericExpr() {
    777         delete control;
    778 }
    779 
    780 void 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 
    800690// Local Variables: //
    801691// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.