Changeset 436bbe5 for src/Parser


Ignore:
Timestamp:
Feb 2, 2022, 10:14:37 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
ff3b0249
Parents:
fde0a58
Message:

remove unnecessary std:: qualification, clean up build_* functions, continue renaming to then/else_

Location:
src/Parser
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    rfde0a58 r436bbe5  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  1 11:06:18 2022
    13 // Update Count     : 903
     12// Last Modified On : Wed Feb  2 09:15:49 2022
     13// Update Count     : 905
    1414//
    1515
     
    410410
    411411Expression * build_if_control( CondCtl * ctl, std::list< Statement * > & init );
    412 Statement * build_if( CondCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
     412Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ );
    413413Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt );
    414414Statement * build_case( ExpressionNode * ctl );
    415415Statement * build_default();
    416 Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * els = nullptr );
    417 Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * els = nullptr );
    418 Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * els = nullptr );
     416Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ = nullptr );
     417Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ = nullptr );
     418Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ = nullptr );
    419419Statement * build_branch( BranchStmt::Type kind );
    420420Statement * build_branch( std::string * identifier, BranchStmt::Type kind );
     
    424424Statement * build_resume( ExpressionNode * ctl );
    425425Statement * build_resume_at( ExpressionNode * ctl , ExpressionNode * target );
    426 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt );
    427 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body );
     426Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ );
     427Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body );
    428428Statement * build_finally( StatementNode * stmt );
    429429Statement * build_compound( StatementNode * first );
  • src/Parser/StatementNode.cc

    rfde0a58 r436bbe5  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  1 18:39:00 2022
    13 // Update Count     : 395
     12// Last Modified On : Wed Feb  2 09:45:28 2022
     13// Update Count     : 415
    1414//
    1515
     
    6363        // convert from StatementNode list to Statement list
    6464        StatementNode * node = dynamic_cast< StatementNode * >(prev);
    65         std::list< Statement * > stmts;
     65        list< Statement * > stmts;
    6666        buildMoveList( stmt, stmts );
    6767        // splice any new Statements to end of current Statements
     
    7878} // build_expr
    7979
    80 Expression * build_if_control( CondCtl * ctl, std::list< Statement * > & init ) {
     80Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
    8181        if ( ctl->init != 0 ) {
    8282                buildMoveList( ctl->init, init );
     
    100100} // build_if_control
    101101
    102 Statement * build_if( CondCtl * 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 );
     102Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
     103        list< Statement * > astinit;                                            // maybe empty
     104        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
     105
     106        Statement * astthen, * astelse = nullptr;
     107        list< Statement * > aststmt;
     108        buildMoveList< Statement, StatementNode >( then, aststmt );
     109        assert( aststmt.size() == 1 );
     110        astthen = aststmt.front();
     111
     112        if ( else_ ) {
     113                list< Statement * > aststmt;
     114                buildMoveList< Statement, StatementNode >( else_, aststmt );
     115                assert( aststmt.size() == 1 );
     116                astelse = aststmt.front();
     117        } // if
     118
     119        return new IfStmt( astcond, astthen, astelse, astinit );
    119120} // build_if
    120121
    121122Statement * 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 ) {
     123        list< Statement * > aststmt;
     124        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     125        if ( ! isSwitch ) {                                                                     // choose statement
     126                for ( Statement * stmt : aststmt ) {
    126127                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
    127128                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
     
    131132                } // for
    132133        } // if
    133         // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    134         return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
     134        // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
     135        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
    135136} // build_switch
    136137
    137138Statement * build_case( ExpressionNode * ctl ) {
    138         std::list< Statement * > branches;
    139         return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
     139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), list< Statement * >{} );
    140140} // build_case
    141141
    142142Statement * build_default() {
    143         std::list< Statement * > branches;
    144         return new CaseStmt( nullptr, branches, true );
     143        return new CaseStmt( nullptr, list< Statement * >{}, true );
    145144} // build_default
    146145
    147146Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
    148         std::list< Statement * > init;
    149         Expression * cond = build_if_control( ctl, init );
    150 
    151         std::list< Statement * > aststmt;
    152         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    153         assert( aststmt.size() == 1 );
    154 
    155         std::list< Statement * > astelse;
     147        list< Statement * > astinit;                                            // maybe empty
     148        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
     149
     150        list< Statement * > aststmt;                                            // loop body, compound created if empty
     151        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     152        assert( aststmt.size() == 1 );
     153
     154        list< Statement * > astelse;                                            // else clause, maybe empty
    156155        buildMoveList< Statement, StatementNode >( else_, astelse );
    157156
    158         return new WhileDoStmt( cond, aststmt.front(), astelse.front(), init, false );
     157        return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
    159158} // build_while
    160159
    161160Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
    162         std::list< Statement * > aststmt;
    163         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    164         assert( aststmt.size() == 1 );
    165 
    166         std::list< Statement * > astelse;
     161        // do-while cannot have declarations in the contitional, so always empty
     162        list< Statement * > astinit;
     163
     164        list< Statement * > aststmt;                                            // loop body, compound created if empty
     165        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     166        assert( aststmt.size() == 1 );                                          // compound created if empty
     167
     168        list< Statement * > astelse;                                            // else clause, maybe empty
    167169        buildMoveList< Statement, StatementNode >( else_, astelse );
    168170
    169         std::list< Statement * > init;
    170         return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), init, true );
     171        return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), astinit, true );
    171172} // build_do_while
    172173
    173174Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
    174         std::list< Statement * > init;
    175         if ( forctl->init != nullptr ) {
    176                 buildMoveList( forctl->init, init );
    177         } // if
    178 
    179         Expression * cond = nullptr;
    180         if ( forctl->condition != nullptr )
    181                 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
    182 
    183         Expression * incr = nullptr;
    184         if ( forctl->change != nullptr )
    185                 incr = maybeMoveBuild< Expression >(forctl->change);
    186 
    187         std::list< Statement * > aststmt;
    188         buildMoveList< Statement, StatementNode >( stmt, aststmt );
    189         assert( aststmt.size() == 1 );
    190 
    191         std::list< Statement * > astelse;
     175        list< Statement * > astinit;                                            // maybe empty
     176        buildMoveList( forctl->init, astinit );
     177
     178        Expression * astcond = nullptr;                                         // maybe empty
     179        astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
     180
     181        Expression * astincr = nullptr;                                         // maybe empty
     182        astincr = maybeMoveBuild< Expression >(forctl->change);
     183        delete forctl;
     184
     185        list< Statement * > aststmt;                                            // loop body, compound created if empty
     186        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     187        assert( aststmt.size() == 1 );
     188
     189        list< Statement * > astelse;                                            // else clause, maybe empty
    192190        buildMoveList< Statement, StatementNode >( else_, astelse );
    193191
    194         delete forctl;
    195         return new ForStmt( init, cond, incr, aststmt.front(), astelse.front() );
     192        return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
    196193} // build_for
    197194
     
    201198} // build_branch
    202199
    203 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
     200Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
    204201        Statement * ret = new BranchStmt( * identifier, kind );
    205202        delete identifier;                                                                      // allocated by lexer
     
    212209
    213210Statement * build_return( ExpressionNode * ctl ) {
    214         std::list< Expression * > exps;
     211        list< Expression * > exps;
    215212        buildMoveList( ctl, exps );
    216213        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
     
    218215
    219216Statement * build_throw( ExpressionNode * ctl ) {
    220         std::list< Expression * > exps;
     217        list< Expression * > exps;
    221218        buildMoveList( ctl, exps );
    222         assertf( exps.size() < 2, "This means we are leaking memory");
     219        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
    223220        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    224221} // build_throw
    225222
    226223Statement * build_resume( ExpressionNode * ctl ) {
    227         std::list< Expression * > exps;
     224        list< Expression * > exps;
    228225        buildMoveList( ctl, exps );
    229         assertf( exps.size() < 2, "This means we are leaking memory");
     226        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
    230227        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    231228} // build_resume
     
    237234} // build_resume_at
    238235
    239 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
    240         std::list< CatchStmt * > branches;
    241         buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
    242         CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
    243         FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    244         return new TryStmt( tryBlock, branches, finallyBlock );
     236Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
     237        list< CatchStmt * > aststmt;
     238        buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
     239        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_));
     240        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_) );
     241        return new TryStmt( tryBlock, aststmt, finallyBlock );
    245242} // build_try
    246243
    247244Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    248         std::list< Statement * > branches;
    249         buildMoveList< Statement, StatementNode >( body, branches );
    250         assert( branches.size() == 1 );
    251         return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
     245        list< Statement * > aststmt;
     246        buildMoveList< Statement, StatementNode >( body, aststmt );
     247        assert( aststmt.size() == 1 );
     248        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), aststmt.front() );
    252249} // build_catch
    253250
    254251Statement * build_finally( StatementNode * stmt ) {
    255         std::list< Statement * > branches;
    256         buildMoveList< Statement, StatementNode >( stmt, branches );
    257         assert( branches.size() == 1 );
    258         return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
     252        list< Statement * > aststmt;
     253        buildMoveList< Statement, StatementNode >( stmt, aststmt );
     254        assert( aststmt.size() == 1 );
     255        return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
    259256} // build_finally
    260257
     
    264261        node->type = type;
    265262
    266         std::list< Statement * > stmts;
     263        list< Statement * > stmts;
    267264        buildMoveList< Statement, StatementNode >( then, stmts );
    268265        if(!stmts.empty()) {
     
    329326} // build_waitfor_timeout
    330327
    331 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     328WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
    332329        auto node = new WaitForStmt();
    333330
     
    336333        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    337334
    338         node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
     335        node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
    339336        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
    340337
     
    343340
    344341Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
    345         std::list< Expression * > e;
     342        list< Expression * > e;
    346343        buildMoveList( exprs, e );
    347344        Statement * s = maybeMoveBuild<Statement>( stmt );
     
    371368
    372369Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    373         std::list< Expression * > out, in;
    374         std::list< ConstantExpr * > clob;
     370        list< Expression * > out, in;
     371        list< ConstantExpr * > clob;
    375372
    376373        buildMoveList( output, out );
     
    385382
    386383Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
    387         std::list< Expression * > expList;
     384        list< Expression * > expList;
    388385        buildMoveList( exprs, expList );
    389386        Statement * body = maybeMoveBuild<Statement>( stmt );
Note: See TracChangeset for help on using the changeset viewer.