Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Expression.cc

    rbf32bb8 rb6fe7e6  
    3131
    3232
    33 Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {}
    34 
    35 Expression::Expression( const Expression &other ) : result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) {
     33Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
     34
     35Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) {
     36        cloneAll( other.results, results );
    3637}
    3738
     
    3940        delete env;
    4041        delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
    41         delete result;
     42        deleteAll( results );
     43}
     44
     45void Expression::add_result( Type *t ) {
     46        if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) {
     47                std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) );
     48        } else {
     49                results.push_back(t);
     50        } // if
    4251}
    4352
     
    5968
    6069ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
    61         set_result( constant.get_type()->clone() );
     70        add_result( constant.get_type()->clone() );
    6271}
    6372
     
    7685        assert( var );
    7786        assert( var->get_type() );
    78         Type * type = var->get_type()->clone();
    79         type->set_isLvalue( true );
    80         set_result( type );
     87        add_result( var->get_type()->clone() );
     88        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
     89                (*i)->set_isLvalue( true );
     90        } // for
    8191}
    8292
     
    100110SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
    101111                Expression( _aname ), expr(expr_), type(0), isType(false) {
    102         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     112        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    103113}
    104114
    105115SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
    106116                Expression( _aname ), expr(0), type(type_), isType(true) {
    107         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     117        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    108118}
    109119
     
    131141AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
    132142                Expression( _aname ), expr(expr_), type(0), isType(false) {
    133         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     143        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    134144}
    135145
    136146AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
    137147                Expression( _aname ), expr(0), type(type_), isType(true) {
    138         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     148        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    139149}
    140150
     
    162172UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
    163173                Expression( _aname ), type(type_), member(member_) {
    164         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     174        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    165175}
    166176
     
    187197OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
    188198                Expression( _aname ), type(type_), member(member_) {
    189         set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
     199        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
    190200}
    191201
     
    219229
    220230OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
    221         set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
     231        add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
    222232}
    223233
     
    274284
    275285CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
    276         set_result(toType);
     286        add_result(toType);
    277287}
    278288
    279289CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
    280         set_result( new VoidType( Type::Qualifiers() ) );
    281290}
    282291
     
    294303        arg->print(os, indent+2);
    295304        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
    296         os << std::string( indent+2, ' ' );
    297         if ( result->isVoid() ) {
    298                 os << "nothing";
     305        if ( results.empty() ) {
     306                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
    299307        } else {
    300                 result->print( os, indent+2 );
     308                printAll(results, os, indent+2);
    301309        } // if
    302         os << std::endl;
    303         Expression::print( os, indent );
    304 }
    305 
    306 UntypedMemberExpr::UntypedMemberExpr( Expression * _member, Expression *_aggregate, Expression *_aname ) :
     310        Expression::print( os, indent );
     311}
     312
     313UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
    307314                Expression( _aname ), member(_member), aggregate(_aggregate) {}
    308315
    309316UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
    310                 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
     317                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
    311318}
    312319
    313320UntypedMemberExpr::~UntypedMemberExpr() {
    314321        delete aggregate;
    315         delete member;
    316322}
    317323
    318324void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
    319         os << "Untyped Member Expression, with field: " << std::endl;
    320         os << std::string( indent+2, ' ' );
    321         get_member()->print(os, indent+4);
    322         os << std::string( indent+2, ' ' );
     325        os << "Untyped Member Expression, with field: " << get_member();
    323326
    324327        Expression *agg = get_aggregate();
    325         os << "from aggregate: " << std::endl;
     328        os << ", from aggregate: ";
    326329        if (agg != 0) {
    327                 os << std::string( indent + 4, ' ' );
    328                 agg->print(os, indent + 4);
     330                os << std::string( indent + 2, ' ' );
     331                agg->print(os, indent + 2);
    329332        }
    330333        os << std::string( indent+2, ' ' );
     
    335338MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
    336339                Expression( _aname ), member(_member), aggregate(_aggregate) {
    337         set_result( member->get_type()->clone() );
    338         get_result()->set_isLvalue( true );
     340        add_result( member->get_type()->clone() );
     341        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
     342                (*i)->set_isLvalue( true );
     343        } // for
    339344}
    340345
     
    367372}
    368373
    369 UntypedExpr::UntypedExpr( Expression *_function, const std::list<Expression *> &_args, Expression *_aname ) :
    370                 Expression( _aname ), function(_function), args(_args) {}
     374
     375UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
    371376
    372377UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
     
    374379        cloneAll( other.args, args );
    375380}
     381
     382UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
     383                Expression( _aname ), function(_function), args(_args) {}
    376384
    377385UntypedExpr::~UntypedExpr() {
     
    411419LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
    412420                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
    413         set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
     421        add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
    414422}
    415423
     
    469477ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
    470478        assert( callExpr );
    471         assert( callExpr->has_result() );
    472         set_result( callExpr->get_result()->clone() );
     479        cloneAll( callExpr->get_results(), results );
    473480}
    474481
     
    503510        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
    504511        assert( arg );
    505         set_result( maybeClone( arg->get_result() ) );
     512        cloneAll( arg->get_results(), results );
    506513}
    507514
     
    523530
    524531CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
    525         assert( type && initializer );
    526         set_result( type->clone() );
    527 }
    528 
    529 CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( other.type->clone() ), initializer( other.initializer->clone() ) {}
     532        add_result( type->clone() );
     533}
     534
     535CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
    530536
    531537CompoundLiteralExpr::~CompoundLiteralExpr() {
     
    536542void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
    537543        os << "Compound Literal Expression: " << std::endl;
    538         os << std::string( indent+2, ' ' );
    539         type->print( os, indent + 2 );
    540         os << std::string( indent+2, ' ' );
    541         initializer->print( os, indent + 2 );
     544        if ( type ) type->print( os, indent + 2 );
     545        if ( initializer ) initializer->print( os, indent + 2 );
    542546}
    543547
     
    553557
    554558RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
    555 RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
     559RangeExpr::RangeExpr( const RangeExpr &other ) : low( other.low->clone() ), high( other.high->clone() ) {}
    556560void RangeExpr::print( std::ostream &os, int indent ) const {
    557         os << "Range Expression: ";
     561        os << std::string( indent, ' ' ) << "Range Expression: ";
    558562        low->print( os, indent );
    559563        os << " ... ";
    560564        high->print( os, indent );
    561 }
    562 
    563 StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
    564         assert( statements );
    565         std::list< Statement * > & body = statements->get_kids();
    566         if ( ! body.empty() ) {
    567                 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
    568                         set_result( maybeClone( exprStmt->get_expr()->get_result() ) );
    569                 }
    570         }
    571 }
    572 StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {}
    573 StmtExpr::~StmtExpr() {
    574         delete statements;
    575 }
    576 void StmtExpr::print( std::ostream &os, int indent ) const {
    577         os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
    578         statements->print( os, indent+2 );
    579 }
    580 
    581 
    582 long long UniqueExpr::count = 0;
    583 UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), id( idVal ) {
    584         assert( count != -1 );
    585         if ( id == -1 ) id = count++;
    586         set_expr( expr );
    587         assert( expr );
    588         if ( expr->get_result() ) {
    589                 set_result( expr->get_result()->clone() );
    590         }
    591 }
    592 UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), id( other.id ) {
    593 }
    594 UniqueExpr::~UniqueExpr() {
    595         if ( expr.unique() ) {
    596                 delete *expr;
    597         }
    598 }
    599 void UniqueExpr::print( std::ostream &os, int indent ) const {
    600         os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' );
    601         get_expr()->print( os, indent+2 );
    602565}
    603566
Note: See TracChangeset for help on using the changeset viewer.