Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r6180274 rde52331  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // StatementNode.cc -- Transform from parse data-structures to AST data-structures, usually deleting the parse
    8 //     data-structure after the transformation.
     7// StatementNode.cc --
    98//
    109// Author           : Rodolfo G. Esteves
    1110// Created On       : Sat May 16 14:59:41 2015
    1211// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Wed Feb  2 20:29:30 2022
    14 // Update Count     : 425
     12// Last Modified On : Sat Oct 24 04:20:55 2020
     13// Update Count     : 383
    1514//
    1615
     
    6463        // convert from StatementNode list to Statement list
    6564        StatementNode * node = dynamic_cast< StatementNode * >(prev);
    66         list< Statement * > stmts;
     65        std::list< Statement * > stmts;
    6766        buildMoveList( stmt, stmts );
    6867        // splice any new Statements to end of current Statements
     
    7978} // build_expr
    8079
    81 Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
     80Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) {
    8281        if ( ctl->init != 0 ) {
    8382                buildMoveList( ctl->init, init );
     
    101100} // build_if_control
    102101
    103 Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
    104         list< Statement * > astinit;                                            // maybe empty
    105         Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
    106 
    107         Statement * astthen, * astelse = nullptr;
    108         list< Statement * > aststmt;
    109         buildMoveList< Statement, StatementNode >( then, aststmt );
    110         assert( aststmt.size() == 1 );
    111         astthen = aststmt.front();
    112 
    113         if ( else_ ) {
    114                 list< Statement * > aststmt;
    115                 buildMoveList< Statement, StatementNode >( else_, aststmt );
    116                 assert( aststmt.size() == 1 );
    117                 astelse = aststmt.front();
    118         } // if
    119 
    120         return new IfStmt( astcond, astthen, astelse, astinit );
     102Statement * build_if( IfCtrl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
     103        Statement * thenb, * elseb = nullptr;
     104        std::list< Statement * > branches;
     105        buildMoveList< Statement, StatementNode >( then_stmt, branches );
     106        assert( branches.size() == 1 );
     107        thenb = branches.front();
     108
     109        if ( else_stmt ) {
     110                std::list< Statement * > branches;
     111                buildMoveList< Statement, StatementNode >( else_stmt, branches );
     112                assert( branches.size() == 1 );
     113                elseb = branches.front();
     114        } // if
     115
     116        std::list< Statement * > init;
     117        Expression * cond = build_if_control( ctl, init );
     118        return new IfStmt( cond, thenb, elseb, init );
    121119} // build_if
    122120
    123121Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
    124         list< Statement * > aststmt;
    125         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    126         if ( ! isSwitch ) {                                                                     // choose statement
    127                 for ( Statement * stmt : aststmt ) {
     122        std::list< Statement * > branches;
     123        buildMoveList< Statement, StatementNode >( stmt, branches );
     124        if ( ! isSwitch ) {                                                                             // choose statement
     125                for ( Statement * stmt : branches ) {
    128126                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
    129127                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
     
    133131                } // for
    134132        } // if
    135         // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
    136         return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
     133        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
     134        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    137135} // build_switch
    138136
    139137Statement * build_case( ExpressionNode * ctl ) {
    140         return new CaseStmt( maybeMoveBuild< Expression >(ctl), {} ); // stmt starts empty and then added to
     138        std::list< Statement * > branches;
     139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    141140} // build_case
    142141
    143142Statement * build_default() {
    144         return new CaseStmt( nullptr, {}, true );                       // stmt starts empty and then added to
     143        std::list< Statement * > branches;
     144        return new CaseStmt( nullptr, branches, true );
    145145} // build_default
    146146
    147 Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
    148         list< Statement * > astinit;                                            // maybe empty
    149         Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
    150 
    151         list< Statement * > aststmt;                                            // loop body, compound created if empty
    152         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    153         assert( aststmt.size() == 1 );
    154 
    155         list< Statement * > astelse;                                            // else clause, maybe empty
    156         buildMoveList< Statement, StatementNode >( else_, astelse );
    157 
    158         return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
     147Statement * build_while( IfCtrl * ctl, StatementNode * stmt ) {
     148        std::list< Statement * > branches;
     149        buildMoveList< Statement, StatementNode >( stmt, branches );
     150        assert( branches.size() == 1 );
     151
     152        std::list< Statement * > init;
     153        Expression * cond = build_if_control( ctl, init );
     154        return new WhileStmt( cond, branches.front(), init, false );
    159155} // build_while
    160156
    161 Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
    162         list< Statement * > aststmt;                                            // loop body, compound created if empty
    163         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    164         assert( aststmt.size() == 1 );                                          // compound created if empty
    165 
    166         list< Statement * > astelse;                                            // else clause, maybe empty
    167         buildMoveList< Statement, StatementNode >( else_, astelse );
    168 
    169         // do-while cannot have declarations in the contitional, so init is always empty
    170         return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), {}, true );
     157Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt ) {
     158        std::list< Statement * > branches;
     159        buildMoveList< Statement, StatementNode >( stmt, branches );
     160        assert( branches.size() == 1 );
     161
     162        std::list< Statement * > init;
     163        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, true );
    171164} // build_do_while
    172165
    173 Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
    174         list< Statement * > astinit;                                            // maybe empty
    175         buildMoveList( forctl->init, astinit );
    176 
    177         Expression * astcond = nullptr;                                         // maybe empty
    178         astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
    179 
    180         Expression * astincr = nullptr;                                         // maybe empty
    181         astincr = maybeMoveBuild< Expression >(forctl->change);
     166Statement * build_for( ForCtrl * forctl, StatementNode * stmt ) {
     167        std::list< Statement * > branches;
     168        buildMoveList< Statement, StatementNode >( stmt, branches );
     169        assert( branches.size() == 1 );
     170
     171        std::list< Statement * > init;
     172        if ( forctl->init != 0 ) {
     173                buildMoveList( forctl->init, init );
     174        } // if
     175
     176        Expression * cond = 0;
     177        if ( forctl->condition != 0 )
     178                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
     179
     180        Expression * incr = 0;
     181        if ( forctl->change != 0 )
     182                incr = maybeMoveBuild< Expression >(forctl->change);
     183
    182184        delete forctl;
    183 
    184         list< Statement * > aststmt;                                            // loop body, compound created if empty
    185         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    186         assert( aststmt.size() == 1 );
    187 
    188         list< Statement * > astelse;                                            // else clause, maybe empty
    189         buildMoveList< Statement, StatementNode >( else_, astelse );
    190 
    191         return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
     185        return new ForStmt( init, cond, incr, branches.front() );
    192186} // build_for
    193187
     
    197191} // build_branch
    198192
    199 Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
     193Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
    200194        Statement * ret = new BranchStmt( * identifier, kind );
    201195        delete identifier;                                                                      // allocated by lexer
     
    208202
    209203Statement * build_return( ExpressionNode * ctl ) {
    210         list< Expression * > exps;
     204        std::list< Expression * > exps;
    211205        buildMoveList( ctl, exps );
    212206        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
     
    214208
    215209Statement * build_throw( ExpressionNode * ctl ) {
    216         list< Expression * > exps;
     210        std::list< Expression * > exps;
    217211        buildMoveList( ctl, exps );
    218         assertf( exps.size() < 2, "CFA internal error: leaking memory" );
     212        assertf( exps.size() < 2, "This means we are leaking memory");
    219213        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    220214} // build_throw
    221215
    222216Statement * build_resume( ExpressionNode * ctl ) {
    223         list< Expression * > exps;
     217        std::list< Expression * > exps;
    224218        buildMoveList( ctl, exps );
    225         assertf( exps.size() < 2, "CFA internal error: leaking memory" );
     219        assertf( exps.size() < 2, "This means we are leaking memory");
    226220        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    227221} // build_resume
     
    233227} // build_resume_at
    234228
    235 Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
    236         list< CatchStmt * > aststmt;
    237         buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
    238         CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_));
    239         FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_) );
    240         return new TryStmt( tryBlock, aststmt, finallyBlock );
     229Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
     230        std::list< CatchStmt * > branches;
     231        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
     232        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
     233        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
     234        return new TryStmt( tryBlock, branches, finallyBlock );
    241235} // build_try
    242236
    243237Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    244         list< Statement * > aststmt;
    245         buildMoveList< Statement, StatementNode >( body, aststmt );
    246         assert( aststmt.size() == 1 );
    247         return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), aststmt.front() );
     238        std::list< Statement * > branches;
     239        buildMoveList< Statement, StatementNode >( body, branches );
     240        assert( branches.size() == 1 );
     241        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    248242} // build_catch
    249243
    250244Statement * build_finally( StatementNode * stmt ) {
    251         list< Statement * > aststmt;
    252         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    253         assert( aststmt.size() == 1 );
    254         return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
     245        std::list< Statement * > branches;
     246        buildMoveList< Statement, StatementNode >( stmt, branches );
     247        assert( branches.size() == 1 );
     248        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
    255249} // build_finally
    256250
     
    260254        node->type = type;
    261255
    262         list< Statement * > stmts;
     256        std::list< Statement * > stmts;
    263257        buildMoveList< Statement, StatementNode >( then, stmts );
    264258        if(!stmts.empty()) {
     
    325319} // build_waitfor_timeout
    326320
    327 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
     321WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
    328322        auto node = new WaitForStmt();
    329323
     
    332326        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    333327
    334         node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
     328        node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
    335329        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
    336330
     
    339333
    340334Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
    341         list< Expression * > e;
     335        std::list< Expression * > e;
    342336        buildMoveList( exprs, e );
    343337        Statement * s = maybeMoveBuild<Statement>( stmt );
     
    367361
    368362Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    369         list< Expression * > out, in;
    370         list< ConstantExpr * > clob;
     363        std::list< Expression * > out, in;
     364        std::list< ConstantExpr * > clob;
    371365
    372366        buildMoveList( output, out );
     
    381375
    382376Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
    383         list< Expression * > expList;
     377        std::list< Expression * > expList;
    384378        buildMoveList( exprs, expList );
    385379        Statement * body = maybeMoveBuild<Statement>( stmt );
Note: See TracChangeset for help on using the changeset viewer.