Changeset d5556a3 for src


Ignore:
Timestamp:
Dec 13, 2016, 5:37:15 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
31f379c
Parents:
1d2b64f
Message:

change rework TupleAssignExpr? and StmtExpr?

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Indexer.cc

    r1d2b64f rd5556a3  
    4040
    4141namespace SymTab {
     42        struct NewScope {
     43                NewScope( SymTab::Indexer & indexer ) : indexer( indexer ) { indexer.enterScope(); }
     44                ~NewScope() { indexer.leaveScope(); }
     45                SymTab::Indexer & indexer;
     46        };
     47
    4248        template< typename TreeType, typename VisitorType >
    4349        inline void acceptNewScope( TreeType *tree, VisitorType &visitor ) {
     
    454460        void Indexer::visit( TupleAssignExpr *tupleExpr ) {
    455461                acceptNewScope( tupleExpr->get_result(), *this );
    456                 enterScope();
    457                 acceptAll( tupleExpr->get_tempDecls(), *this );
    458                 acceptAll( tupleExpr->get_assigns(), *this );
    459                 leaveScope();
     462                maybeAccept( tupleExpr->get_stmtExpr(), *this );
    460463        }
    461464
  • src/SynTree/Expression.cc

    r1d2b64f rd5556a3  
    524524
    525525ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
     526        set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
    526527        delete callExpr;
    527528        deleteAll( tempDecls );
     
    533534        os <<  "Implicit Copy Constructor Expression: " << std::endl;
    534535        assert( callExpr );
     536        os << std::string( indent+2, ' ' );
    535537        callExpr->print( os, indent + 2 );
    536538        os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl;
     
    584586        os << std::string( indent+2, ' ' );
    585587        initializer->print( os, indent + 2 );
     588        Expression::print( os, indent );
    586589}
    587590
     
    603606        os << " ... ";
    604607        high->print( os, indent );
     608        Expression::print( os, indent );
    605609}
    606610
     
    614618        }
    615619}
    616 StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {}
     620StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
     621        cloneAll( other.returnDecls, returnDecls );
     622        cloneAll( other.dtors, dtors );
     623}
    617624StmtExpr::~StmtExpr() {
    618625        delete statements;
     626        deleteAll( dtors );
     627        deleteAll( returnDecls );
    619628}
    620629void StmtExpr::print( std::ostream &os, int indent ) const {
    621630        os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
    622631        statements->print( os, indent+2 );
     632        if ( ! returnDecls.empty() ) {
     633                os << std::string( indent+2, ' ' ) << "with returnDecls: ";
     634                printAll( returnDecls, os, indent+2 );
     635        }
     636        if ( ! dtors.empty() ) {
     637                os << std::string( indent+2, ' ' ) << "with dtors: ";
     638                printAll( dtors, os, indent+2 );
     639        }
     640        Expression::print( os, indent );
    623641}
    624642
     
    644662        get_expr()->print( os, indent+2 );
    645663        if ( get_object() ) {
    646                 os << " with decl: ";
     664                os << std::string( indent+2, ' ' ) << "with decl: ";
    647665                get_object()->printShort( os, indent+2 );
    648666        }
     667        Expression::print( os, indent );
    649668}
    650669
  • src/SynTree/Expression.h

    r1d2b64f rd5556a3  
    543543
    544544        std::list< ObjectDecl * > & get_tempDecls() { return tempDecls; }
    545         void set_tempDecls( std::list< ObjectDecl * > newValue ) { tempDecls = newValue; }
    546 
    547545        std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
    548         void set_returnDecls( std::list< ObjectDecl * > newValue ) { returnDecls = newValue; }
    549 
    550546        std::list< Expression * > & get_dtors() { return dtors; }
    551         void set_dtors( std::list< Expression * > newValue ) { dtors = newValue; }
    552547
    553548        virtual ImplicitCopyCtorExpr *clone() const { return new ImplicitCopyCtorExpr( *this ); }
     
    706701        virtual ~TupleAssignExpr();
    707702
    708         std::list< Expression * > & get_assigns() { return assigns; }
    709         std::list< ObjectDecl * > & get_tempDecls() { return tempDecls; }
     703        TupleAssignExpr * set_stmtExpr( StmtExpr * newValue ) { stmtExpr = newValue; return this; }
     704        StmtExpr * get_stmtExpr() const { return stmtExpr; }
    710705
    711706        virtual TupleAssignExpr *clone() const { return new TupleAssignExpr( *this ); }
     
    714709        virtual void print( std::ostream &os, int indent = 0 ) const;
    715710  private:
    716         std::list< Expression * > assigns; // assignment expressions that use tempDecls
    717         std::list< ObjectDecl * > tempDecls; // temporaries for address of lhs exprs
     711        StmtExpr * stmtExpr = nullptr;
    718712};
    719713
     
    728722        StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; }
    729723
     724        std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
     725        std::list< Expression * > & get_dtors() { return dtors; }
     726
    730727        virtual StmtExpr *clone() const { return new StmtExpr( *this ); }
    731728        virtual void accept( Visitor &v ) { v.visit( this ); }
     
    734731private:
    735732        CompoundStmt * statements;
     733        std::list< ObjectDecl * > returnDecls; // return variable(s) for stmt expression
     734        std::list< Expression * > dtors; // destructor(s) for return variable(s)
    736735};
    737736
  • src/SynTree/Mutator.cc

    r1d2b64f rd5556a3  
    325325        mutateAll( impCpCtorExpr->get_tempDecls(), *this );
    326326        mutateAll( impCpCtorExpr->get_returnDecls(), *this );
     327        mutateAll( impCpCtorExpr->get_dtors(), *this );
    327328        return impCpCtorExpr;
    328329}
     
    373374Expression *Mutator::mutate( TupleAssignExpr *assignExpr ) {
    374375        assignExpr->set_result( maybeMutate( assignExpr->get_result(), *this ) );
    375         mutateAll( assignExpr->get_tempDecls(), *this );
    376         mutateAll( assignExpr->get_assigns(), *this );
     376        assignExpr->set_stmtExpr( maybeMutate( assignExpr->get_stmtExpr(), *this ) );
    377377        return assignExpr;
    378378}
     
    381381        stmtExpr->set_result( maybeMutate( stmtExpr->get_result(), *this ) );
    382382        stmtExpr->set_statements( maybeMutate( stmtExpr->get_statements(), *this ) );
     383        mutateAll( stmtExpr->get_returnDecls(), *this );
     384        mutateAll( stmtExpr->get_dtors(), *this );
    383385        return stmtExpr;
    384386}
     
    503505Initializer *Mutator::mutate( ConstructorInit *ctorInit ) {
    504506        ctorInit->set_ctor( maybeMutate( ctorInit->get_ctor(), *this ) );
     507        ctorInit->set_dtor( maybeMutate( ctorInit->get_dtor(), *this ) );
    505508        ctorInit->set_init( maybeMutate( ctorInit->get_init(), *this ) );
    506509        return ctorInit;
  • src/SynTree/TupleExpr.cc

    r1d2b64f rd5556a3  
    8787}
    8888
    89 
    90 TupleAssignExpr::TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname ) : Expression( _aname ), assigns( assigns ), tempDecls( tempDecls ) {
     89TupleAssignExpr::TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname ) : Expression( _aname ) {
     90        // convert internally into a StmtExpr which contains the declarations and produces the tuple of the assignments
    9191        set_result( Tuples::makeTupleType( assigns ) );
     92        CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
     93        std::list< Statement * > & stmts = compoundStmt->get_kids();
     94        for ( ObjectDecl * obj : tempDecls ) {
     95                stmts.push_back( new DeclStmt( noLabels, obj ) );
     96        }
     97        TupleExpr * tupleExpr = new TupleExpr( assigns );
     98        assert( tupleExpr->get_result() );
     99        stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
     100        stmtExpr = new StmtExpr( compoundStmt );
    92101}
    93102
    94103TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ) {
    95         cloneAll( other.assigns, assigns );
    96         cloneAll( other.tempDecls, tempDecls );
    97 
    98         // clone needs to go into assigns and replace tempDecls
    99         VarExprReplacer::DeclMap declMap;
    100         std::list< ObjectDecl * >::const_iterator origit = other.tempDecls.begin();
    101         for ( ObjectDecl * temp : tempDecls ) {
    102                 assert( origit != other.tempDecls.end() );
    103                 ObjectDecl * origTemp = *origit++;
    104                 assert( origTemp );
    105                 assert( temp->get_name() == origTemp->get_name() );
    106                 declMap[ origTemp ] = temp;
    107         }
    108         if ( ! declMap.empty() ) {
    109                 VarExprReplacer replacer( declMap );
    110                 for ( Expression * assn : assigns ) {
    111                         assn->accept( replacer );
    112                 }
    113         }
     104        assert( other.stmtExpr );
     105        stmtExpr = other.stmtExpr->clone();
    114106}
    115107
    116108TupleAssignExpr::~TupleAssignExpr() {
    117         deleteAll( assigns );
    118         // deleteAll( tempDecls );
     109        delete stmtExpr;
    119110}
    120111
    121112void TupleAssignExpr::print( std::ostream &os, int indent ) const {
    122         os << "Tuple Assignment Expression, with temporaries:" << std::endl;
    123         printAll( tempDecls, os, indent+4 );
    124         os << std::string( indent+2, ' ' ) << "with assignments: " << std::endl;
    125         printAll( assigns, os, indent+4 );
     113        os << "Tuple Assignment Expression, with stmt expr:" << std::endl;
     114        os << std::string( indent+2, ' ' );
     115        stmtExpr->print( os, indent+4 );
    126116        Expression::print( os, indent );
    127117}
  • src/SynTree/Visitor.cc

    r1d2b64f rd5556a3  
    276276        acceptAll( impCpCtorExpr->get_tempDecls(), *this );
    277277        acceptAll( impCpCtorExpr->get_returnDecls(), *this );
     278        acceptAll( impCpCtorExpr->get_dtors(), *this );
    278279}
    279280
     
    317318void Visitor::visit( TupleAssignExpr *assignExpr ) {
    318319        maybeAccept( assignExpr->get_result(), *this );
    319         acceptAll( assignExpr->get_tempDecls(), *this );
    320         acceptAll( assignExpr->get_assigns(), *this );
     320        maybeAccept( assignExpr->get_stmtExpr(), *this );
    321321}
    322322
     
    324324        maybeAccept( stmtExpr->get_result(), *this );
    325325        maybeAccept( stmtExpr->get_statements(), *this );
     326        acceptAll( stmtExpr->get_returnDecls(), *this );
     327        acceptAll( stmtExpr->get_dtors(), *this );
    326328}
    327329
     
    425427void Visitor::visit( ConstructorInit *ctorInit ) {
    426428        maybeAccept( ctorInit->get_ctor(), *this );
     429        maybeAccept( ctorInit->get_dtor(), *this );
    427430        maybeAccept( ctorInit->get_init(), *this );
    428431}
  • src/Tuples/TupleExpansion.cc

    r1d2b64f rd5556a3  
    194194                                new CommaExpr( new CommaExpr( assignUnq, assignFinished ), var->clone() ) );
    195195                        condExpr->set_result( var->get_result()->clone() );
     196                        condExpr->set_env( maybeClone( unqExpr->get_env() ) );
    196197                        decls[id] = condExpr;
    197198                }
     
    202203        Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
    203204                assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) );
    204                 CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
    205                 std::list< Statement * > & stmts = compoundStmt->get_kids();
    206                 for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
    207                         stmts.push_back( new DeclStmt( noLabels, obj ) );
    208                 }
    209                 TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
    210                 assert( tupleExpr->get_result() );
    211                 stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
    212                 assnExpr->get_tempDecls().clear();
    213                 assnExpr->get_assigns().clear();
     205                StmtExpr * ret = assnExpr->get_stmtExpr();
     206                assnExpr->set_stmtExpr( nullptr );
     207                // move env to StmtExpr
     208                ret->set_env( assnExpr->get_env() );
     209                assnExpr->set_env( nullptr );
    214210                delete assnExpr;
    215                 return new StmtExpr( compoundStmt );
     211                return ret;
    216212        }
    217213
     
    221217                if ( ! typeMap.count( mangleName ) ) {
    222218                        // generate struct type to replace tuple type
     219                        // xxx - should fix this to only generate one tuple struct for each number of type parameters
    223220                        StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
    224221                        decl->set_body( true );
     
    247244                tupleExpr->set_tuple( nullptr );
    248245                unsigned int idx = tupleExpr->get_index();
     246                TypeSubstitution * env = tupleExpr->get_env();
     247                tupleExpr->set_env( nullptr );
    249248                delete tupleExpr;
    250249
     
    253252                assert( structDecl->get_members().size() > idx );
    254253                Declaration * member = *std::next(structDecl->get_members().begin(), idx);
    255                 return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
    256         }
    257 
    258         Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs ) {
     254                MemberExpr * memExpr = new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
     255                memExpr->set_env( env );
     256                return memExpr;
     257        }
     258
     259        Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs, TypeSubstitution * env ) {
    259260                if ( result->isVoid() ) {
    260261                        // void result - don't need to produce a value for cascading - just output a chain of comma exprs
    261262                        assert( ! exprs.empty() );
    262263                        std::list< Expression * >::const_iterator iter = exprs.begin();
    263                         Expression * expr = *iter++;
     264                        Expression * expr = new CastExpr( *iter++ );
    264265                        for ( ; iter != exprs.end(); ++iter ) {
    265                                 expr = new CommaExpr( expr, *iter );
    266                         }
     266                                expr = new CommaExpr( expr, new CastExpr( *iter ) );
     267                        }
     268                        expr->set_env( env );
    267269                        return expr;
    268270                } else {
     
    274276                                inits.push_back( new SingleInit( expr ) );
    275277                        }
    276                         return new CompoundLiteralExpr( result, new ListInit( inits ) );
     278                        Expression * expr = new CompoundLiteralExpr( result, new ListInit( inits ) );
     279                        expr->set_env( env );
     280                        return expr;
    277281                }
    278282        }
     
    284288                std::list< Expression * > exprs = tupleExpr->get_exprs();
    285289                assert( result );
     290                TypeSubstitution * env = tupleExpr->get_env();
    286291
    287292                // remove data from shell and delete it
    288293                tupleExpr->set_result( nullptr );
    289294                tupleExpr->get_exprs().clear();
     295                tupleExpr->set_env( nullptr );
    290296                delete tupleExpr;
    291297
    292                 return replaceTupleExpr( result, exprs );
     298                return replaceTupleExpr( result, exprs, env );
    293299        }
    294300
Note: See TracChangeset for help on using the changeset viewer.