Changeset e39aa0f
- Timestamp:
- Jun 28, 2016, 3:33:01 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 4dcea3f
- Parents:
- 888cbe4
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/MLEMutator.cc
r888cbe4 re39aa0f 27 27 #include "SynTree/Statement.h" 28 28 #include "SynTree/Expression.h" 29 #include "SynTree/Attribute.h" 29 30 30 31 namespace ControlStruct { … … 32 33 delete targetTable; 33 34 targetTable = 0; 35 } 36 namespace { 37 Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; } 34 38 } 35 39 … … 44 48 if ( ! get_breakLabel().empty() ) { 45 49 std::list< Statement * >::iterator next = k+1; 46 Statement * stmt = 0; 47 if ( next == kids.end() ) { 48 std::list<Label> ls; ls.push_back( get_breakLabel() ); 49 kids.push_back( stmt = new NullStmt( ls ) ); 50 } else { 51 (stmt = *next)->get_labels().push_back( get_breakLabel() ); 52 } 53 stmt->get_labels().front().set_statement( stmt ); 54 50 std::list<Label> ls; ls.push_back( get_breakLabel() ); 51 kids.insert( next, new NullStmt( ls ) ); 55 52 set_breakLabel(""); 56 53 } // if … … 62 59 if ( labeledBlock ) { 63 60 Label brkLabel = generator->newLabel("blockBreak"); 64 enclosing Blocks.push_back( Entry( cmpndStmt, brkLabel ) );61 enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); 65 62 } // if 66 63 … … 71 68 72 69 if ( labeledBlock ) { 73 assert( ! enclosing Blocks.empty() );74 if ( ! enclosing Blocks.back().useBreakExit().empty() ) {75 set_breakLabel( enclosing Blocks.back().useBreakExit() );76 } 77 enclosing Blocks.pop_back();70 assert( ! enclosingControlStructures.empty() ); 71 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { 72 set_breakLabel( enclosingControlStructures.back().useBreakExit() ); 73 } 74 enclosingControlStructures.pop_back(); 78 75 } // if 79 76 … … 89 86 Label brkLabel = generator->newLabel("loopBreak"); 90 87 Label contLabel = generator->newLabel("loopContinue"); 91 enclosing Loops.push_back( Entry( loopStmt, brkLabel, contLabel ) );88 enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) ); 92 89 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) ); 93 90 94 91 // sanity check that the enclosing loops have been popped correctly 95 Entry &e = enclosing Loops.back();92 Entry &e = enclosingControlStructures.back(); 96 93 assert ( e == loopStmt ); 97 94 … … 99 96 // two labels, if they are used. 100 97 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) ); 101 enclosing Loops.pop_back();98 enclosingControlStructures.pop_back(); 102 99 103 100 return loopStmt; … … 115 112 // generate a label for breaking out of a labeled switch 116 113 Label brkLabel = generator->newLabel("switchBreak"); 117 enclosing Switches.push_back( Entry(switchStmt, brkLabel) );114 enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) ); 118 115 mutateAll( switchStmt->get_branches(), *this ); 119 116 120 Entry &e = enclosing Switches.back();117 Entry &e = enclosingControlStructures.back(); 121 118 assert ( e == switchStmt ); 122 119 … … 133 130 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) { 134 131 std::list<Label> temp; temp.push_back( brkLabel ); 135 Statement * stmt = new BranchStmt( temp, Label(""), BranchStmt::Break ); 136 stmt->get_labels().front().set_statement( stmt ); 137 c->get_statements().push_back( stmt ); 132 c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) ); 138 133 } else assert(0); // as of this point, all branches of a switch are still CaseStmts 139 134 } 140 135 141 assert ( enclosing Switches.back() == switchStmt );142 enclosing Switches.pop_back();136 assert ( enclosingControlStructures.back() == switchStmt ); 137 enclosingControlStructures.pop_back(); 143 138 return switchStmt; 144 139 } … … 147 142 std::string originalTarget = branchStmt->get_originalTarget(); 148 143 149 if ( branchStmt->get_type() == BranchStmt::Goto ) 144 std::list< Entry >::reverse_iterator targetEntry; 145 if ( branchStmt->get_type() == BranchStmt::Goto ) { 150 146 return branchStmt; 151 152 // test if continue target is a loop 153 if ( branchStmt->get_type() == BranchStmt::Continue) { 154 if ( enclosingLoops.empty() ) { 155 throw SemanticError( "'continue' outside a loop" ); 156 } else if ( branchStmt->get_target() != "" && std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) { 157 throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget ); 158 } 159 } 160 161 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) ) 162 throw SemanticError( "'break' outside a loop or switch" ); 163 164 if ( branchStmt->get_target() == "" ) return branchStmt; 165 166 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() ) 147 } else if ( branchStmt->get_type() == BranchStmt::Continue) { 148 // continue target must be a loop 149 if ( branchStmt->get_target() == "" ) { 150 targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } ); 151 } else { 152 // labelled continue - lookup label in table ot find attached control structure 153 targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] ); 154 } 155 if ( targetEntry == enclosingControlStructures.rend() || ! isLoop( targetEntry->get_controlStructure() ) ) { 156 throw SemanticError( "'continue' target must be an enclosing loop: " + originalTarget ); 157 } 158 } else if ( branchStmt->get_type() == BranchStmt::Break ) { 159 if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" ); 160 targetEntry = enclosingControlStructures.rbegin(); 161 } else { 162 assert( false ); 163 } 164 165 if ( branchStmt->get_target() != "" && targetTable->find( branchStmt->get_target() ) == targetTable->end() ) { 167 166 throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked) 168 169 std::list< Entry >::iterator check; 170 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() ) 171 // not in loop, checking if in block 172 if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() ) 173 // neither in loop nor in block, checking if in switch/choose 174 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() ) 175 throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget ); 176 167 } 168 169 // xxx - possibly remove this 177 170 // what about exiting innermost block or switch??? 178 if ( enclosingLoops.back() == (*check) )179 return branchStmt; // exit the innermost loop (labels unnecessary)171 // if ( enclosingControlStructures.back() == (*targetEntry) ) 172 // return branchStmt; // exit the innermost loop (labels unnecessary) 180 173 181 174 // branch error checks, get the appropriate label name and create a goto … … 183 176 switch ( branchStmt->get_type() ) { 184 177 case BranchStmt::Break: 185 assert( check->useBreakExit() != "");186 exitLabel = check->useBreakExit();178 assert( targetEntry->useBreakExit() != ""); 179 exitLabel = targetEntry->useBreakExit(); 187 180 break; 188 181 case BranchStmt::Continue: 189 assert( check->useContExit() != "");190 exitLabel = check->useContExit();182 assert( targetEntry->useContExit() != ""); 183 exitLabel = targetEntry->useContExit(); 191 184 break; 192 185 default: … … 194 187 } // switch 195 188 196 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto ); 189 if ( branchStmt->get_target() == "" && branchStmt->get_type() != BranchStmt::Continue ) { 190 // unlabelled break/continue - can keep branch as break/continue for extra semantic information, but add 191 // exitLabel as its destination so that label passes can easily determine where the break/continue goes to 192 branchStmt->set_target( exitLabel ); 193 return branchStmt; 194 } else { 195 // labelled break/continue - can't easily emulate this with break and continue, so transform into a goto 196 delete branchStmt; 197 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto ); 198 } 197 199 } 198 200 … … 210 212 // continue label goes in the body as the last statement 211 213 std::list< Label > labels; labels.push_back( e.useContExit() ); 212 Statement * stmt = new NullStmt( labels ); 213 stmt->get_labels().front().set_statement( stmt ); 214 newBody->get_kids().push_back( stmt ); 214 newBody->get_kids().push_back( new NullStmt( labels ) ); 215 215 } 216 216 -
src/ControlStruct/MLEMutator.h
r888cbe4 re39aa0f 56 56 bool operator!=( const Statement *stmt ) { return ( loop != stmt ); } 57 57 58 bool operator==( const Entry &other ) { return ( loop == other.get_ loop() ); }58 bool operator==( const Entry &other ) { return ( loop == other.get_controlStructure() ); } 59 59 60 Statement *get_ loop() const { return loop; }60 Statement *get_controlStructure() const { return loop; } 61 61 62 62 Label useContExit() { contUsed = true; return contExit; } … … 73 73 74 74 std::map< Label, Statement * > *targetTable; 75 std::list< Entry > enclosing Blocks, enclosingLoops, enclosingSwitches;75 std::list< Entry > enclosingControlStructures; 76 76 Label breakLabel; 77 77 LabelGenerator *generator; -
src/InitTweak/FixInit.cc
r888cbe4 re39aa0f 35 35 bool ctorp = false; 36 36 bool cpctorp = false; 37 bool dtorp = true;37 bool dtorp = false; 38 38 #define PRINT( text ) if ( ctordtorp ) { text } 39 39 #define CP_CTOR_PRINT( text ) if ( ctordtorp || cpctorp ) { text } … … 499 499 curVars.insert( objDecl ); 500 500 } 501 returnParent::visit( stmt );501 Parent::visit( stmt ); 502 502 } 503 503 … … 563 563 } 564 564 565 // Handle break/continue/goto in the same manner as C++. 566 // Basic idea: any objects that are in scope at the BranchStmt 567 // but not at the labelled (target) statement must be destructed. 568 // If there are any objects in scope at the target location but 569 // not at the BranchStmt then those objects would be uninitialized 570 // so notify the user of the error. 571 // See C++ Reference 6.6 Jump Statements for details. 565 572 void InsertDtors::handleGoto( BranchStmt * stmt ) { 566 assert( stmt->get_t ype() == BranchStmt::Goto);573 assert( stmt->get_target() != "" && "BranchStmt missing a label" ); 567 574 // S_L = lvars = set of objects in scope at label definition 568 575 // S_G = curVars = set of objects in scope at goto statement … … 602 609 603 610 void InsertDtors::visit( BranchStmt * stmt ) { 604 // TODO: adding to the end of a block isn't sufficient, since605 // return/break/goto should trigger destructor when block is left.606 611 switch( stmt->get_type() ) { 607 612 case BranchStmt::Continue: 608 613 case BranchStmt::Break: 609 // xxx - easiest thing to do: generate a label for every break/continue 610 // this label is unused, so attach unused attribute to it 611 // finally, all of these cases can be the same (this is less efficient than it could be, 612 // because the S_L-S_G check is unnecessary [the set should always be empty], but this 613 // serves as a bit of a sanity check, so I'm okay with it.) 614 // xxx - this is insufficient, because multiple blocks can be opened in a switch or loop 615 insertDtors( reverseDeclOrder.front().begin(), reverseDeclOrder.front().end(), back_inserter( stmtsToAdd ) ); 616 break; 614 // could optimize the break/continue case, because the S_L-S_G check 615 // is unnecessary (this set should always be empty), but it serves 616 // as a small sanity check. 617 617 case BranchStmt::Goto: 618 618 handleGoto( stmt );
Note: See TracChangeset
for help on using the changeset viewer.