Ignore:
Timestamp:
Feb 4, 2022, 10:10:34 PM (4 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
f8143a6
Parents:
5f3ba11 (diff), 67e86ae6 (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r5f3ba11 rb56ad5e  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // StatementNode.cc --
     7// StatementNode.cc -- Transform from parse data-structures to AST data-structures, usually deleting the parse
     8//     data-structure after the transformation.
    89//
    910// Author           : Rodolfo G. Esteves
    1011// Created On       : Sat May 16 14:59:41 2015
    1112// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Oct 24 04:20:55 2020
    13 // Update Count     : 383
     13// Last Modified On : Wed Feb  2 20:29:30 2022
     14// Update Count     : 425
    1415//
    1516
     
    6364        // convert from StatementNode list to Statement list
    6465        StatementNode * node = dynamic_cast< StatementNode * >(prev);
    65         std::list< Statement * > stmts;
     66        list< Statement * > stmts;
    6667        buildMoveList( stmt, stmts );
    6768        // splice any new Statements to end of current Statements
     
    7879} // build_expr
    7980
    80 Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) {
     81Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
    8182        if ( ctl->init != 0 ) {
    8283                buildMoveList( ctl->init, init );
     
    100101} // build_if_control
    101102
    102 Statement * 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 );
     103Statement * 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 );
    119121} // build_if
    120122
    121123Statement * 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 ) {
     124        list< Statement * > aststmt;
     125        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     126        if ( ! isSwitch ) {                                                                     // choose statement
     127                for ( Statement * stmt : aststmt ) {
    126128                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
    127129                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
     
    131133                } // for
    132134        } // if
    133         // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    134         return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
     135        // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
     136        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
    135137} // build_switch
    136138
    137139Statement * build_case( ExpressionNode * ctl ) {
    138         std::list< Statement * > branches;
    139         return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
     140        return new CaseStmt( maybeMoveBuild< Expression >(ctl), {} ); // stmt starts empty and then added to
    140141} // build_case
    141142
    142143Statement * build_default() {
    143         std::list< Statement * > branches;
    144         return new CaseStmt( nullptr, branches, true );
     144        return new CaseStmt( nullptr, {}, true );                       // stmt starts empty and then added to
    145145} // build_default
    146146
    147 Statement * 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 );
     147Statement * 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 );
    155159} // build_while
    156160
    157 Statement * 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 );
     161Statement * 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 );
    164171} // build_do_while
    165172
    166 Statement * 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 
     173Statement * 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);
    184182        delete forctl;
    185         return new ForStmt( init, cond, incr, branches.front() );
     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() );
    186192} // build_for
    187193
     
    191197} // build_branch
    192198
    193 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
     199Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
    194200        Statement * ret = new BranchStmt( * identifier, kind );
    195201        delete identifier;                                                                      // allocated by lexer
     
    202208
    203209Statement * build_return( ExpressionNode * ctl ) {
    204         std::list< Expression * > exps;
     210        list< Expression * > exps;
    205211        buildMoveList( ctl, exps );
    206212        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
     
    208214
    209215Statement * build_throw( ExpressionNode * ctl ) {
    210         std::list< Expression * > exps;
     216        list< Expression * > exps;
    211217        buildMoveList( ctl, exps );
    212         assertf( exps.size() < 2, "This means we are leaking memory");
     218        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
    213219        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    214220} // build_throw
    215221
    216222Statement * build_resume( ExpressionNode * ctl ) {
    217         std::list< Expression * > exps;
     223        list< Expression * > exps;
    218224        buildMoveList( ctl, exps );
    219         assertf( exps.size() < 2, "This means we are leaking memory");
     225        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
    220226        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    221227} // build_resume
     
    227233} // build_resume_at
    228234
    229 Statement * 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 );
     235Statement * 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 );
    235241} // build_try
    236242
    237243Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    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() );
     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() );
    242248} // build_catch
    243249
    244250Statement * build_finally( StatementNode * stmt ) {
    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() ) );
     251        list< Statement * > aststmt;
     252        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     253        assert( aststmt.size() == 1 );
     254        return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
    249255} // build_finally
    250256
     
    254260        node->type = type;
    255261
    256         std::list< Statement * > stmts;
     262        list< Statement * > stmts;
    257263        buildMoveList< Statement, StatementNode >( then, stmts );
    258264        if(!stmts.empty()) {
     
    319325} // build_waitfor_timeout
    320326
    321 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     327WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
    322328        auto node = new WaitForStmt();
    323329
     
    326332        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    327333
    328         node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
     334        node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
    329335        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
    330336
     
    333339
    334340Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
    335         std::list< Expression * > e;
     341        list< Expression * > e;
    336342        buildMoveList( exprs, e );
    337343        Statement * s = maybeMoveBuild<Statement>( stmt );
     
    361367
    362368Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    363         std::list< Expression * > out, in;
    364         std::list< ConstantExpr * > clob;
     369        list< Expression * > out, in;
     370        list< ConstantExpr * > clob;
    365371
    366372        buildMoveList( output, out );
     
    375381
    376382Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
    377         std::list< Expression * > expList;
     383        list< Expression * > expList;
    378384        buildMoveList( exprs, expList );
    379385        Statement * body = maybeMoveBuild<Statement>( stmt );
Note: See TracChangeset for help on using the changeset viewer.