Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r8d7bef2 rcc32d83  
    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 : Mon Apr 30 09:21:16 2018
     13// Update Count     : 354
    1414//
    1515
    1616#include <cassert>                 // for assert, strict_dynamic_cast, assertf
    1717#include <list>                    // for list
     18#include <memory>                  // for unique_ptr
    1819#include <string>                  // for string
    1920
     
    3233
    3334
    34 StatementNode::StatementNode( DeclarationNode *decl ) {
     35StatementNode::StatementNode( DeclarationNode * decl ) {
    3536        assert( decl );
    36         DeclarationNode *agg = decl->extractAggregate();
     37        DeclarationNode * agg = decl->extractAggregate();
    3738        if ( agg ) {
    38                 StatementNode *nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
     39                StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
    3940                set_next( nextStmt );
    4041                if ( decl->get_next() ) {
     
    4950                agg = decl;
    5051        } // if
    51         stmt = new DeclStmt{ maybeMoveBuild< Declaration >(agg) };
     52        stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) );
    5253} // StatementNode::StatementNode
    5354
    54 StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
    55         StatementNode *prev = this;
     55StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
     56        StatementNode * prev = this;
    5657        // find end of list and maintain previous pointer
    5758        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    58                 StatementNode *node = strict_dynamic_cast< StatementNode * >(curr);
    59                 assert( dynamic_cast< CaseStmt * >(node->stmt) );
     59                StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
     60                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
    6061                prev = curr;
    6162        } // for
    6263        // convert from StatementNode list to Statement list
    63         StatementNode *node = dynamic_cast< StatementNode * >(prev);
     64        StatementNode * node = dynamic_cast< StatementNode * >(prev);
    6465        std::list< Statement * > stmts;
    6566        buildMoveList( stmt, stmts );
    6667        // splice any new Statements to end of current Statements
    67         CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt);
     68        CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
    6869        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    6970        return this;
    7071}
    7172
    72 Statement *build_expr( ExpressionNode *ctl ) {
    73         Expression *e = maybeMoveBuild< Expression >( ctl );
     73Statement * build_expr( ExpressionNode * ctl ) {
     74        Expression * e = maybeMoveBuild< Expression >( ctl );
    7475
    7576        if ( e )
     
    7980}
    8081
    81 Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
    82         Statement *thenb, *elseb = 0;
     82Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
     83        Statement * thenb, * elseb = 0;
    8384        std::list< Statement * > branches;
    8485        buildMoveList< Statement, StatementNode >( then_stmt, branches );
     
    115116}
    116117
    117 Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
     118Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
    118119        std::list< Statement * > branches;
    119120        buildMoveList< Statement, StatementNode >( stmt, branches );
     121        if ( ! isSwitch ) {                                                                             // choose statement
     122                for ( Statement * stmt : branches ) {
     123                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
     124                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
     125                                CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
     126                                block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
     127                        } // if
     128                } // for
     129        } // if
    120130        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    121131        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    122132}
    123 Statement *build_case( ExpressionNode *ctl ) {
     133Statement * build_case( ExpressionNode * ctl ) {
    124134        std::list< Statement * > branches;
    125135        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    126136}
    127 Statement *build_default() {
     137Statement * build_default() {
    128138        std::list< Statement * > branches;
    129139        return new CaseStmt( nullptr, branches, true );
    130140}
    131141
    132 Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
     142Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) {
    133143        std::list< Statement * > branches;
    134144        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    137147}
    138148
    139 Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
     149Statement * build_for( ForCtl * forctl, StatementNode * stmt ) {
    140150        std::list< Statement * > branches;
    141151        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    147157        } // if
    148158
    149         Expression *cond = 0;
     159        Expression * cond = 0;
    150160        if ( forctl->condition != 0 )
    151161                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
    152162
    153         Expression *incr = 0;
     163        Expression * incr = 0;
    154164        if ( forctl->change != 0 )
    155165                incr = maybeMoveBuild< Expression >(forctl->change);
     
    159169}
    160170
    161 Statement *build_branch( BranchStmt::Type kind ) {
     171Statement * build_branch( BranchStmt::Type kind ) {
    162172        Statement * ret = new BranchStmt( "", kind );
    163173        return ret;
    164174}
    165 Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
    166         Statement * ret = new BranchStmt( *identifier, kind );
     175Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
     176        Statement * ret = new BranchStmt( * identifier, kind );
    167177        delete identifier;                                                                      // allocated by lexer
    168178        return ret;
    169179}
    170 Statement *build_computedgoto( ExpressionNode *ctl ) {
     180Statement * build_computedgoto( ExpressionNode * ctl ) {
    171181        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
    172182}
    173183
    174 Statement *build_return( ExpressionNode *ctl ) {
     184Statement * build_return( ExpressionNode * ctl ) {
    175185        std::list< Expression * > exps;
    176186        buildMoveList( ctl, exps );
     
    178188}
    179189
    180 Statement *build_throw( ExpressionNode *ctl ) {
     190Statement * build_throw( ExpressionNode * ctl ) {
    181191        std::list< Expression * > exps;
    182192        buildMoveList( ctl, exps );
     
    185195}
    186196
    187 Statement *build_resume( ExpressionNode *ctl ) {
     197Statement * build_resume( ExpressionNode * ctl ) {
    188198        std::list< Expression * > exps;
    189199        buildMoveList( ctl, exps );
     
    192202}
    193203
    194 Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
     204Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
    195205        (void)ctl;
    196206        (void)target;
     
    198208}
    199209
    200 Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
     210Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
    201211        std::list< CatchStmt * > branches;
    202212        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
    203         CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
    204         FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
     213        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
     214        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    205215        return new TryStmt( tryBlock, branches, finallyBlock );
    206216}
    207 Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
     217Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    208218        std::list< Statement * > branches;
    209219        buildMoveList< Statement, StatementNode >( body, branches );
     
    211221        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    212222}
    213 Statement *build_finally( StatementNode *stmt ) {
     223Statement * build_finally( StatementNode * stmt ) {
    214224        std::list< Statement * > branches;
    215225        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    294304}
    295305
    296 Statement *build_compound( StatementNode *first ) {
    297         CompoundStmt *cs = new CompoundStmt();
     306Statement * build_compound( StatementNode * first ) {
     307        CompoundStmt * cs = new CompoundStmt();
    298308        buildMoveList( first, cs->get_kids() );
    299309        return cs;
    300310}
    301311
    302 Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
     312Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
    303313        std::list< Expression * > out, in;
    304314        std::list< ConstantExpr * > clob;
     
    308318        buildMoveList( clobber, clob );
    309319        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
     320}
     321
     322Statement * build_directive( string * directive ) {
     323        return new DirectiveStmt( *directive );
    310324}
    311325
Note: See TracChangeset for help on using the changeset viewer.