Changes in src/Parser/StatementNode.cc [8d7bef2:cc32d83]
- File:
-
- 1 edited
-
src/Parser/StatementNode.cc (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/StatementNode.cc
r8d7bef2 rcc32d83 10 10 // Created On : Sat May 16 14:59:41 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Sep 1 23:25:23 201713 // Update Count : 3 4612 // Last Modified On : Mon Apr 30 09:21:16 2018 13 // Update Count : 354 14 14 // 15 15 16 16 #include <cassert> // for assert, strict_dynamic_cast, assertf 17 17 #include <list> // for list 18 #include <memory> // for unique_ptr 18 19 #include <string> // for string 19 20 … … 32 33 33 34 34 StatementNode::StatementNode( DeclarationNode * decl ) {35 StatementNode::StatementNode( DeclarationNode * decl ) { 35 36 assert( decl ); 36 DeclarationNode * agg = decl->extractAggregate();37 DeclarationNode * agg = decl->extractAggregate(); 37 38 if ( agg ) { 38 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );39 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) ); 39 40 set_next( nextStmt ); 40 41 if ( decl->get_next() ) { … … 49 50 agg = decl; 50 51 } // if 51 stmt = new DeclStmt{ maybeMoveBuild< Declaration >(agg) };52 stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) ); 52 53 } // StatementNode::StatementNode 53 54 54 StatementNode * StatementNode::append_last_case( StatementNode *stmt ) {55 StatementNode * prev = this;55 StatementNode * StatementNode::append_last_case( StatementNode * stmt ) { 56 StatementNode * prev = this; 56 57 // find end of list and maintain previous pointer 57 58 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()) ); 60 61 prev = curr; 61 62 } // for 62 63 // convert from StatementNode list to Statement list 63 StatementNode * node = dynamic_cast< StatementNode * >(prev);64 StatementNode * node = dynamic_cast< StatementNode * >(prev); 64 65 std::list< Statement * > stmts; 65 66 buildMoveList( stmt, stmts ); 66 67 // 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()); 68 69 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); 69 70 return this; 70 71 } 71 72 72 Statement * build_expr( ExpressionNode *ctl ) {73 Expression * e = maybeMoveBuild< Expression >( ctl );73 Statement * build_expr( ExpressionNode * ctl ) { 74 Expression * e = maybeMoveBuild< Expression >( ctl ); 74 75 75 76 if ( e ) … … 79 80 } 80 81 81 Statement * build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {82 Statement * thenb, *elseb = 0;82 Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) { 83 Statement * thenb, * elseb = 0; 83 84 std::list< Statement * > branches; 84 85 buildMoveList< Statement, StatementNode >( then_stmt, branches ); … … 115 116 } 116 117 117 Statement * build_switch( ExpressionNode *ctl, StatementNode *stmt ) {118 Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) { 118 119 std::list< Statement * > branches; 119 120 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 120 130 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements 121 131 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches ); 122 132 } 123 Statement * build_case( ExpressionNode *ctl ) {133 Statement * build_case( ExpressionNode * ctl ) { 124 134 std::list< Statement * > branches; 125 135 return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches ); 126 136 } 127 Statement * build_default() {137 Statement * build_default() { 128 138 std::list< Statement * > branches; 129 139 return new CaseStmt( nullptr, branches, true ); 130 140 } 131 141 132 Statement * build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {142 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) { 133 143 std::list< Statement * > branches; 134 144 buildMoveList< Statement, StatementNode >( stmt, branches ); … … 137 147 } 138 148 139 Statement * build_for( ForCtl *forctl, StatementNode *stmt ) {149 Statement * build_for( ForCtl * forctl, StatementNode * stmt ) { 140 150 std::list< Statement * > branches; 141 151 buildMoveList< Statement, StatementNode >( stmt, branches ); … … 147 157 } // if 148 158 149 Expression * cond = 0;159 Expression * cond = 0; 150 160 if ( forctl->condition != 0 ) 151 161 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) ); 152 162 153 Expression * incr = 0;163 Expression * incr = 0; 154 164 if ( forctl->change != 0 ) 155 165 incr = maybeMoveBuild< Expression >(forctl->change); … … 159 169 } 160 170 161 Statement * build_branch( BranchStmt::Type kind ) {171 Statement * build_branch( BranchStmt::Type kind ) { 162 172 Statement * ret = new BranchStmt( "", kind ); 163 173 return ret; 164 174 } 165 Statement * build_branch( std::string *identifier, BranchStmt::Type kind ) {166 Statement * ret = new BranchStmt( * identifier, kind );175 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) { 176 Statement * ret = new BranchStmt( * identifier, kind ); 167 177 delete identifier; // allocated by lexer 168 178 return ret; 169 179 } 170 Statement * build_computedgoto( ExpressionNode *ctl ) {180 Statement * build_computedgoto( ExpressionNode * ctl ) { 171 181 return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); 172 182 } 173 183 174 Statement * build_return( ExpressionNode *ctl ) {184 Statement * build_return( ExpressionNode * ctl ) { 175 185 std::list< Expression * > exps; 176 186 buildMoveList( ctl, exps ); … … 178 188 } 179 189 180 Statement * build_throw( ExpressionNode *ctl ) {190 Statement * build_throw( ExpressionNode * ctl ) { 181 191 std::list< Expression * > exps; 182 192 buildMoveList( ctl, exps ); … … 185 195 } 186 196 187 Statement * build_resume( ExpressionNode *ctl ) {197 Statement * build_resume( ExpressionNode * ctl ) { 188 198 std::list< Expression * > exps; 189 199 buildMoveList( ctl, exps ); … … 192 202 } 193 203 194 Statement * build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {204 Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) { 195 205 (void)ctl; 196 206 (void)target; … … 198 208 } 199 209 200 Statement * build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {210 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) { 201 211 std::list< CatchStmt * > branches; 202 212 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) ); 205 215 return new TryStmt( tryBlock, branches, finallyBlock ); 206 216 } 207 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {217 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) { 208 218 std::list< Statement * > branches; 209 219 buildMoveList< Statement, StatementNode >( body, branches ); … … 211 221 return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); 212 222 } 213 Statement * build_finally( StatementNode *stmt ) {223 Statement * build_finally( StatementNode * stmt ) { 214 224 std::list< Statement * > branches; 215 225 buildMoveList< Statement, StatementNode >( stmt, branches ); … … 294 304 } 295 305 296 Statement * build_compound( StatementNode *first ) {297 CompoundStmt * cs = new CompoundStmt();306 Statement * build_compound( StatementNode * first ) { 307 CompoundStmt * cs = new CompoundStmt(); 298 308 buildMoveList( first, cs->get_kids() ); 299 309 return cs; 300 310 } 301 311 302 Statement * build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {312 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { 303 313 std::list< Expression * > out, in; 304 314 std::list< ConstantExpr * > clob; … … 308 318 buildMoveList( clobber, clob ); 309 319 return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); 320 } 321 322 Statement * build_directive( string * directive ) { 323 return new DirectiveStmt( *directive ); 310 324 } 311 325
Note:
See TracChangeset
for help on using the changeset viewer.