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/Parser/StatementNode.cc

    rf9feab8 r90152a4  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Sep  1 23:25:23 2017
    13 // Update Count     : 346
     12// Last Modified On : Sat Aug  4 09:39:25 2018
     13// Update Count     : 363
    1414//
    1515
     
    3333
    3434
    35 StatementNode::StatementNode( DeclarationNode *decl ) {
     35StatementNode::StatementNode( DeclarationNode * decl ) {
    3636        assert( decl );
    37         DeclarationNode *agg = decl->extractAggregate();
     37        DeclarationNode * agg = decl->extractAggregate();
    3838        if ( agg ) {
    39                 StatementNode *nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
     39                StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
    4040                set_next( nextStmt );
    4141                if ( decl->get_next() ) {
     
    5353} // StatementNode::StatementNode
    5454
    55 StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
    56         StatementNode *prev = this;
     55StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
     56        StatementNode * prev = this;
    5757        // find end of list and maintain previous pointer
    5858        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    59                 StatementNode *node = strict_dynamic_cast< StatementNode * >(curr);
     59                StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
    6060                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
    6161                prev = curr;
    6262        } // for
    6363        // convert from StatementNode list to Statement list
    64         StatementNode *node = dynamic_cast< StatementNode * >(prev);
     64        StatementNode * node = dynamic_cast< StatementNode * >(prev);
    6565        std::list< Statement * > stmts;
    6666        buildMoveList( stmt, stmts );
     
    6969        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    7070        return this;
    71 }
    72 
    73 Statement *build_expr( ExpressionNode *ctl ) {
    74         Expression *e = maybeMoveBuild< Expression >( ctl );
    75 
    76         if ( e )
    77                 return new ExprStmt( e );
    78         else
    79                 return new NullStmt();
    80 }
    81 
    82 Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
    83         Statement *thenb, *elseb = 0;
    84         std::list< Statement * > branches;
    85         buildMoveList< Statement, StatementNode >( then_stmt, branches );
    86         assert( branches.size() == 1 );
    87         thenb = branches.front();
    88 
    89         if ( else_stmt ) {
    90                 std::list< Statement * > branches;
    91                 buildMoveList< Statement, StatementNode >( else_stmt, branches );
    92                 assert( branches.size() == 1 );
    93                 elseb = branches.front();
    94         } // if
    95 
    96         std::list< Statement * > init;
     71} // StatementNode::append_last_case
     72
     73Statement * build_expr( ExpressionNode * ctl ) {
     74        Expression * e = maybeMoveBuild< Expression >( ctl );
     75
     76        if ( e ) return new ExprStmt( e );
     77        else return new NullStmt();
     78} // build_expr
     79
     80Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) {
    9781        if ( ctl->init != 0 ) {
    9882                buildMoveList( ctl->init, init );
     
    10286        if ( ctl->condition ) {
    10387                // compare the provided condition against 0
    104                 cond =  notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
     88                cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
    10589        } else {
    10690                for ( Statement * stmt : init ) {
     
    11397        }
    11498        delete ctl;
     99        return cond;
     100} // build_if_control
     101
     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 );
    115118        return new IfStmt( cond, thenb, elseb, init );
    116 }
    117 
    118 Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
    119         std::list< Statement * > branches;
    120         buildMoveList< Statement, StatementNode >( stmt, branches );
     119} // build_if
     120
     121Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
     122        std::list< Statement * > branches;
     123        buildMoveList< Statement, StatementNode >( stmt, branches );
     124        if ( ! isSwitch ) {                                                                             // choose statement
     125                for ( Statement * stmt : branches ) {
     126                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
     127                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
     128                                CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
     129                                block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
     130                        } // if
     131                } // for
     132        } // if
    121133        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    122134        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    123 }
    124 Statement *build_case( ExpressionNode *ctl ) {
     135} // build_switch
     136
     137Statement * build_case( ExpressionNode * ctl ) {
    125138        std::list< Statement * > branches;
    126139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    127 }
    128 Statement *build_default() {
     140} // build_case
     141
     142Statement * build_default() {
    129143        std::list< Statement * > branches;
    130144        return new CaseStmt( nullptr, branches, true );
    131 }
    132 
    133 Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
    134         std::list< Statement * > branches;
    135         buildMoveList< Statement, StatementNode >( stmt, branches );
    136         assert( branches.size() == 1 );
    137         return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
    138 }
    139 
    140 Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
     145} // build_default
     146
     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 );
     155} // build_while
     156
     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 );
     164} // build_do_while
     165
     166Statement * build_for( ForCtrl * forctl, StatementNode * stmt ) {
    141167        std::list< Statement * > branches;
    142168        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    148174        } // if
    149175
    150         Expression *cond = 0;
     176        Expression * cond = 0;
    151177        if ( forctl->condition != 0 )
    152178                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
    153179
    154         Expression *incr = 0;
     180        Expression * incr = 0;
    155181        if ( forctl->change != 0 )
    156182                incr = maybeMoveBuild< Expression >(forctl->change);
     
    158184        delete forctl;
    159185        return new ForStmt( init, cond, incr, branches.front() );
    160 }
    161 
    162 Statement *build_branch( BranchStmt::Type kind ) {
     186} // build_for
     187
     188Statement * build_branch( BranchStmt::Type kind ) {
    163189        Statement * ret = new BranchStmt( "", kind );
    164190        return ret;
    165 }
    166 Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
    167         Statement * ret = new BranchStmt( *identifier, kind );
     191} // build_branch
     192
     193Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
     194        Statement * ret = new BranchStmt( * identifier, kind );
    168195        delete identifier;                                                                      // allocated by lexer
    169196        return ret;
    170 }
    171 Statement *build_computedgoto( ExpressionNode *ctl ) {
     197} // build_branch
     198
     199Statement * build_computedgoto( ExpressionNode * ctl ) {
    172200        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
    173 }
    174 
    175 Statement *build_return( ExpressionNode *ctl ) {
     201} // build_computedgoto
     202
     203Statement * build_return( ExpressionNode * ctl ) {
    176204        std::list< Expression * > exps;
    177205        buildMoveList( ctl, exps );
    178206        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
    179 }
    180 
    181 Statement *build_throw( ExpressionNode *ctl ) {
     207} // build_return
     208
     209Statement * build_throw( ExpressionNode * ctl ) {
    182210        std::list< Expression * > exps;
    183211        buildMoveList( ctl, exps );
    184212        assertf( exps.size() < 2, "This means we are leaking memory");
    185213        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    186 }
    187 
    188 Statement *build_resume( ExpressionNode *ctl ) {
     214} // build_throw
     215
     216Statement * build_resume( ExpressionNode * ctl ) {
    189217        std::list< Expression * > exps;
    190218        buildMoveList( ctl, exps );
    191219        assertf( exps.size() < 2, "This means we are leaking memory");
    192220        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    193 }
    194 
    195 Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
     221} // build_resume
     222
     223Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
    196224        (void)ctl;
    197225        (void)target;
    198226        assertf( false, "resume at (non-local throw) is not yet supported," );
    199 }
    200 
    201 Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
     227} // build_resume_at
     228
     229Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
    202230        std::list< CatchStmt * > branches;
    203231        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
    204         CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
    205         FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
     232        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
     233        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    206234        return new TryStmt( tryBlock, branches, finallyBlock );
    207 }
    208 Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
     235} // build_try
     236
     237Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    209238        std::list< Statement * > branches;
    210239        buildMoveList< Statement, StatementNode >( body, branches );
    211240        assert( branches.size() == 1 );
    212241        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    213 }
    214 Statement *build_finally( StatementNode *stmt ) {
     242} // build_catch
     243
     244Statement * build_finally( StatementNode * stmt ) {
    215245        std::list< Statement * > branches;
    216246        buildMoveList< Statement, StatementNode >( stmt, branches );
    217247        assert( branches.size() == 1 );
    218248        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
    219 }
     249} // build_finally
    220250
    221251WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
     
    238268
    239269        return node;
    240 }
     270} // build_waitfor
    241271
    242272WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
     
    257287
    258288        return node;
    259 }
     289} // build_waitfor
    260290
    261291WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
     
    266296                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
    267297                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    268         }
    269         else {
     298        } else {
    270299                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
    271300                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    272         }
     301        } // if
    273302
    274303        return node;
    275 }
     304} // build_waitfor_timeout
    276305
    277306WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     
    286315
    287316        return node;
    288 }
     317} // build_waitfor_timeout
    289318
    290319WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
     
    293322        Statement * s = maybeMoveBuild<Statement>( stmt );
    294323        return new WithStmt( e, s );
    295 }
    296 
    297 Statement *build_compound( StatementNode *first ) {
    298         CompoundStmt *cs = new CompoundStmt();
     324} // build_with
     325
     326Statement * build_compound( StatementNode * first ) {
     327        CompoundStmt * cs = new CompoundStmt();
    299328        buildMoveList( first, cs->get_kids() );
    300329        return cs;
    301 }
    302 
    303 Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
     330} // build_compound
     331
     332Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    304333        std::list< Expression * > out, in;
    305334        std::list< ConstantExpr * > clob;
     
    309338        buildMoveList( clobber, clob );
    310339        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    311 }
     340} // build_asm
     341
     342Statement * build_directive( string * directive ) {
     343        return new DirectiveStmt( *directive );
     344} // build_directive
    312345
    313346// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.