Changes in src/Parser/StatementNode.cc [6180274:de52331]
- File:
-
- 1 edited
-
src/Parser/StatementNode.cc (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/StatementNode.cc
r6180274 rde52331 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // StatementNode.cc -- Transform from parse data-structures to AST data-structures, usually deleting the parse 8 // data-structure after the transformation. 7 // StatementNode.cc -- 9 8 // 10 9 // Author : Rodolfo G. Esteves 11 10 // Created On : Sat May 16 14:59:41 2015 12 11 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Wed Feb 2 20:29:30 202214 // Update Count : 42512 // Last Modified On : Sat Oct 24 04:20:55 2020 13 // Update Count : 383 15 14 // 16 15 … … 64 63 // convert from StatementNode list to Statement list 65 64 StatementNode * node = dynamic_cast< StatementNode * >(prev); 66 list< Statement * > stmts;65 std::list< Statement * > stmts; 67 66 buildMoveList( stmt, stmts ); 68 67 // splice any new Statements to end of current Statements … … 79 78 } // build_expr 80 79 81 Expression * build_if_control( CondCtl * ctl,list< Statement * > & init ) {80 Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) { 82 81 if ( ctl->init != 0 ) { 83 82 buildMoveList( ctl->init, init ); … … 101 100 } // build_if_control 102 101 103 Statement * 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 ); 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 ); 121 119 } // build_if 122 120 123 121 Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) { 124 list< Statement * > aststmt;125 buildMoveList< Statement, StatementNode >( stmt, aststmt);126 if ( ! isSwitch ) { // choose statement127 for ( Statement * stmt : aststmt) {122 std::list< Statement * > branches; 123 buildMoveList< Statement, StatementNode >( stmt, branches ); 124 if ( ! isSwitch ) { // choose statement 125 for ( Statement * stmt : branches ) { 128 126 CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); 129 127 if ( ! caseStmt->stmts.empty() ) { // code after "case" => end of case list … … 133 131 } // for 134 132 } // if 135 // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements136 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt);133 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements 134 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches ); 137 135 } // build_switch 138 136 139 137 Statement * build_case( ExpressionNode * ctl ) { 140 return new CaseStmt( maybeMoveBuild< Expression >(ctl), {} ); // stmt starts empty and then added to 138 std::list< Statement * > branches; 139 return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches ); 141 140 } // build_case 142 141 143 142 Statement * build_default() { 144 return new CaseStmt( nullptr, {}, true ); // stmt starts empty and then added to 143 std::list< Statement * > branches; 144 return new CaseStmt( nullptr, branches, true ); 145 145 } // build_default 146 146 147 Statement * 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 ); 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 ); 159 155 } // build_while 160 156 161 Statement * 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 ); 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 ); 171 164 } // build_do_while 172 165 173 Statement * 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); 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 182 184 delete forctl; 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() ); 185 return new ForStmt( init, cond, incr, branches.front() ); 192 186 } // build_for 193 187 … … 197 191 } // build_branch 198 192 199 Statement * build_branch( st ring * identifier, BranchStmt::Type kind ) {193 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) { 200 194 Statement * ret = new BranchStmt( * identifier, kind ); 201 195 delete identifier; // allocated by lexer … … 208 202 209 203 Statement * build_return( ExpressionNode * ctl ) { 210 list< Expression * > exps;204 std::list< Expression * > exps; 211 205 buildMoveList( ctl, exps ); 212 206 return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr ); … … 214 208 215 209 Statement * build_throw( ExpressionNode * ctl ) { 216 list< Expression * > exps;210 std::list< Expression * > exps; 217 211 buildMoveList( ctl, exps ); 218 assertf( exps.size() < 2, " CFA internal error: leaking memory");212 assertf( exps.size() < 2, "This means we are leaking memory"); 219 213 return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); 220 214 } // build_throw 221 215 222 216 Statement * build_resume( ExpressionNode * ctl ) { 223 list< Expression * > exps;217 std::list< Expression * > exps; 224 218 buildMoveList( ctl, exps ); 225 assertf( exps.size() < 2, " CFA internal error: leaking memory");219 assertf( exps.size() < 2, "This means we are leaking memory"); 226 220 return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); 227 221 } // build_resume … … 233 227 } // build_resume_at 234 228 235 Statement * 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 );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 ); 241 235 } // build_try 242 236 243 237 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) { 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() );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() ); 248 242 } // build_catch 249 243 250 244 Statement * build_finally( StatementNode * stmt ) { 251 list< Statement * > aststmt;252 buildMoveList< Statement, StatementNode >( stmt, aststmt);253 assert( aststmt.size() == 1 );254 return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );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() ) ); 255 249 } // build_finally 256 250 … … 260 254 node->type = type; 261 255 262 list< Statement * > stmts;256 std::list< Statement * > stmts; 263 257 buildMoveList< Statement, StatementNode >( then, stmts ); 264 258 if(!stmts.empty()) { … … 325 319 } // build_waitfor_timeout 326 320 327 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_ , ExpressionNode * else_when ) {321 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) { 328 322 auto node = new WaitForStmt(); 329 323 … … 332 326 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 333 327 334 node->orelse.statement = maybeMoveBuild<Statement >( else_ );328 node->orelse.statement = maybeMoveBuild<Statement >( else_stmt ); 335 329 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) ); 336 330 … … 339 333 340 334 Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) { 341 list< Expression * > e;335 std::list< Expression * > e; 342 336 buildMoveList( exprs, e ); 343 337 Statement * s = maybeMoveBuild<Statement>( stmt ); … … 367 361 368 362 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { 369 list< Expression * > out, in;370 list< ConstantExpr * > clob;363 std::list< Expression * > out, in; 364 std::list< ConstantExpr * > clob; 371 365 372 366 buildMoveList( output, out ); … … 381 375 382 376 Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) { 383 list< Expression * > expList;377 std::list< Expression * > expList; 384 378 buildMoveList( exprs, expList ); 385 379 Statement * body = maybeMoveBuild<Statement>( stmt );
Note:
See TracChangeset
for help on using the changeset viewer.