Changeset 90152a4 for src/Parser/StatementNode.cc
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/StatementNode.cc
rf9feab8 r90152a4 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 : Sat Aug 4 09:39:25 2018 13 // Update Count : 363 14 14 // 15 15 … … 33 33 34 34 35 StatementNode::StatementNode( DeclarationNode * decl ) {35 StatementNode::StatementNode( DeclarationNode * decl ) { 36 36 assert( decl ); 37 DeclarationNode * agg = decl->extractAggregate();37 DeclarationNode * agg = decl->extractAggregate(); 38 38 if ( agg ) { 39 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );39 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) ); 40 40 set_next( nextStmt ); 41 41 if ( decl->get_next() ) { … … 53 53 } // StatementNode::StatementNode 54 54 55 StatementNode * StatementNode::append_last_case( StatementNode *stmt ) {56 StatementNode * prev = this;55 StatementNode * StatementNode::append_last_case( StatementNode * stmt ) { 56 StatementNode * prev = this; 57 57 // find end of list and maintain previous pointer 58 58 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) { 59 StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);59 StatementNode * node = strict_dynamic_cast< StatementNode * >(curr); 60 60 assert( dynamic_cast< CaseStmt * >(node->stmt.get()) ); 61 61 prev = curr; 62 62 } // for 63 63 // convert from StatementNode list to Statement list 64 StatementNode * node = dynamic_cast< StatementNode * >(prev);64 StatementNode * node = dynamic_cast< StatementNode * >(prev); 65 65 std::list< Statement * > stmts; 66 66 buildMoveList( stmt, stmts ); … … 69 69 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); 70 70 return this; 71 } 72 73 Statement *build_expr( ExpressionNode *ctl ) { 74 Expression *e = maybeMoveBuild< Expression >( ctl ); 75 76 if ( e ) 77 return new ExprStmt( e ); 78 else 79 return new NullStmt(); 80 } 81 82 Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) { 83 Statement *thenb, *elseb = 0; 84 std::list< Statement * > branches; 85 buildMoveList< Statement, StatementNode >( then_stmt, branches ); 86 assert( branches.size() == 1 ); 87 thenb = branches.front(); 88 89 if ( else_stmt ) { 90 std::list< Statement * > branches; 91 buildMoveList< Statement, StatementNode >( else_stmt, branches ); 92 assert( branches.size() == 1 ); 93 elseb = branches.front(); 94 } // if 95 96 std::list< Statement * > init; 71 } // StatementNode::append_last_case 72 73 Statement * build_expr( ExpressionNode * ctl ) { 74 Expression * e = maybeMoveBuild< Expression >( ctl ); 75 76 if ( e ) return new ExprStmt( e ); 77 else return new NullStmt(); 78 } // build_expr 79 80 Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) { 97 81 if ( ctl->init != 0 ) { 98 82 buildMoveList( ctl->init, init ); … … 102 86 if ( ctl->condition ) { 103 87 // compare the provided condition against 0 104 cond = 88 cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) ); 105 89 } else { 106 90 for ( Statement * stmt : init ) { … … 113 97 } 114 98 delete ctl; 99 return cond; 100 } // build_if_control 101 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 ); 115 118 return new IfStmt( cond, thenb, elseb, init ); 116 } 117 118 Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) { 119 std::list< Statement * > branches; 120 buildMoveList< Statement, StatementNode >( stmt, branches ); 119 } // build_if 120 121 Statement * 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 ) { 126 CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); 127 if ( ! caseStmt->stmts.empty() ) { // code after "case" => end of case list 128 CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() ); 129 block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) ); 130 } // if 131 } // for 132 } // if 121 133 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements 122 134 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches ); 123 } 124 Statement *build_case( ExpressionNode *ctl ) { 135 } // build_switch 136 137 Statement * build_case( ExpressionNode * ctl ) { 125 138 std::list< Statement * > branches; 126 139 return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches ); 127 } 128 Statement *build_default() { 140 } // build_case 141 142 Statement * build_default() { 129 143 std::list< Statement * > branches; 130 144 return new CaseStmt( nullptr, branches, true ); 131 } 132 133 Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) { 134 std::list< Statement * > branches; 135 buildMoveList< Statement, StatementNode >( stmt, branches ); 136 assert( branches.size() == 1 ); 137 return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind ); 138 } 139 140 Statement *build_for( ForCtl *forctl, StatementNode *stmt ) { 145 } // build_default 146 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 ); 155 } // build_while 156 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 ); 164 } // build_do_while 165 166 Statement * build_for( ForCtrl * forctl, StatementNode * stmt ) { 141 167 std::list< Statement * > branches; 142 168 buildMoveList< Statement, StatementNode >( stmt, branches ); … … 148 174 } // if 149 175 150 Expression * cond = 0;176 Expression * cond = 0; 151 177 if ( forctl->condition != 0 ) 152 178 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) ); 153 179 154 Expression * incr = 0;180 Expression * incr = 0; 155 181 if ( forctl->change != 0 ) 156 182 incr = maybeMoveBuild< Expression >(forctl->change); … … 158 184 delete forctl; 159 185 return new ForStmt( init, cond, incr, branches.front() ); 160 } 161 162 Statement * build_branch( BranchStmt::Type kind ) {186 } // build_for 187 188 Statement * build_branch( BranchStmt::Type kind ) { 163 189 Statement * ret = new BranchStmt( "", kind ); 164 190 return ret; 165 } 166 Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) { 167 Statement * ret = new BranchStmt( *identifier, kind ); 191 } // build_branch 192 193 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) { 194 Statement * ret = new BranchStmt( * identifier, kind ); 168 195 delete identifier; // allocated by lexer 169 196 return ret; 170 } 171 Statement *build_computedgoto( ExpressionNode *ctl ) { 197 } // build_branch 198 199 Statement * build_computedgoto( ExpressionNode * ctl ) { 172 200 return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); 173 } 174 175 Statement * build_return( ExpressionNode *ctl ) {201 } // build_computedgoto 202 203 Statement * build_return( ExpressionNode * ctl ) { 176 204 std::list< Expression * > exps; 177 205 buildMoveList( ctl, exps ); 178 206 return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr ); 179 } 180 181 Statement * build_throw( ExpressionNode *ctl ) {207 } // build_return 208 209 Statement * build_throw( ExpressionNode * ctl ) { 182 210 std::list< Expression * > exps; 183 211 buildMoveList( ctl, exps ); 184 212 assertf( exps.size() < 2, "This means we are leaking memory"); 185 213 return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); 186 } 187 188 Statement * build_resume( ExpressionNode *ctl ) {214 } // build_throw 215 216 Statement * build_resume( ExpressionNode * ctl ) { 189 217 std::list< Expression * > exps; 190 218 buildMoveList( ctl, exps ); 191 219 assertf( exps.size() < 2, "This means we are leaking memory"); 192 220 return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); 193 } 194 195 Statement * build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {221 } // build_resume 222 223 Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) { 196 224 (void)ctl; 197 225 (void)target; 198 226 assertf( false, "resume at (non-local throw) is not yet supported," ); 199 } 200 201 Statement * build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {227 } // build_resume_at 228 229 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) { 202 230 std::list< CatchStmt * > branches; 203 231 buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches ); 204 CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));205 FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );232 CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt)); 233 FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); 206 234 return new TryStmt( tryBlock, branches, finallyBlock ); 207 } 208 Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) { 235 } // build_try 236 237 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) { 209 238 std::list< Statement * > branches; 210 239 buildMoveList< Statement, StatementNode >( body, branches ); 211 240 assert( branches.size() == 1 ); 212 241 return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); 213 } 214 Statement *build_finally( StatementNode *stmt ) { 242 } // build_catch 243 244 Statement * build_finally( StatementNode * stmt ) { 215 245 std::list< Statement * > branches; 216 246 buildMoveList< Statement, StatementNode >( stmt, branches ); 217 247 assert( branches.size() == 1 ); 218 248 return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) ); 219 } 249 } // build_finally 220 250 221 251 WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) { … … 238 268 239 269 return node; 240 } 270 } // build_waitfor 241 271 242 272 WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) { … … 257 287 258 288 return node; 259 } 289 } // build_waitfor 260 290 261 291 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) { … … 266 296 node->timeout.statement = maybeMoveBuild<Statement >( stmt ); 267 297 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 268 } 269 else { 298 } else { 270 299 node->orelse.statement = maybeMoveBuild<Statement >( stmt ); 271 300 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); 272 } 301 } // if 273 302 274 303 return node; 275 } 304 } // build_waitfor_timeout 276 305 277 306 WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) { … … 286 315 287 316 return node; 288 } 317 } // build_waitfor_timeout 289 318 290 319 WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) { … … 293 322 Statement * s = maybeMoveBuild<Statement>( stmt ); 294 323 return new WithStmt( e, s ); 295 } 296 297 Statement * build_compound( StatementNode *first ) {298 CompoundStmt * cs = new CompoundStmt();324 } // build_with 325 326 Statement * build_compound( StatementNode * first ) { 327 CompoundStmt * cs = new CompoundStmt(); 299 328 buildMoveList( first, cs->get_kids() ); 300 329 return cs; 301 } 302 303 Statement * build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {330 } // build_compound 331 332 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { 304 333 std::list< Expression * > out, in; 305 334 std::list< ConstantExpr * > clob; … … 309 338 buildMoveList( clobber, clob ); 310 339 return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); 311 } 340 } // build_asm 341 342 Statement * build_directive( string * directive ) { 343 return new DirectiveStmt( *directive ); 344 } // build_directive 312 345 313 346 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.