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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 //
Note: See TracChangeset for help on using the changeset viewer.