- File:
-
- 1 edited
-
src/ControlStruct/MultiLevelExit.cpp (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/MultiLevelExit.cpp
r3b0bc16 r3e5db5b4 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 1 13:48:00 2021 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Feb 1 18:48:47 202213 // Update Count : 2 911 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Nov 8 10:56:00 2021 13 // Update Count : 2 14 14 // 15 15 … … 18 18 #include "AST/Pass.hpp" 19 19 #include "AST/Stmt.hpp" 20 #include " LabelGeneratorNew.hpp"20 #include "ControlStruct/LabelGenerator.h" 21 21 22 22 #include <set> 23 using namespace std;24 using namespace ast;25 23 26 24 namespace ControlStruct { 25 26 namespace { 27 27 28 class Entry { 28 public:29 const Stmt * stmt;30 private:29 public: 30 const ast::Stmt * stmt; 31 private: 31 32 // Organized like a manual ADT. Avoids creating a bunch of dead data. 32 33 struct Target { 33 Label label;34 ast::Label label; 34 35 bool used = false; 35 Target( const Label & label ) : label( label ) {}36 Target( const ast::Label & label ) : label( label ) {} 36 37 Target() : label( CodeLocation() ) {} 37 38 }; … … 40 41 41 42 enum Kind { 42 ForStmt K, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseStmtK, SwitchStmtK, TryStmtK43 ForStmt, WhileStmt, CompoundStmt, IfStmt, CaseStmt, SwitchStmt, TryStmt 43 44 } kind; 44 45 45 46 bool fallDefaultValid = true; 46 47 47 static Label & useTarget( Target & target ) {48 static ast::Label & useTarget( Target & target ) { 48 49 target.used = true; 49 50 return target.label; 50 51 } 51 52 52 public:53 Entry( const ForStmt * stmt, Label breakExit,Label contExit ) :54 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmt K) {}55 Entry( const WhileDoStmt * stmt, Label breakExit,Label contExit ) :56 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( While DoStmtK) {}57 Entry( const CompoundStmt *stmt,Label breakExit ) :58 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmt K) {}59 Entry( const IfStmt *stmt,Label breakExit ) :60 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmt K) {}61 Entry( const CaseStmt *stmt,Label fallExit ) :62 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmt K) {}63 Entry( const SwitchStmt *stmt, Label breakExit,Label fallDefaultExit ) :64 stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmt K) {}65 Entry( const TryStmt *stmt,Label breakExit ) :66 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmt K) {}67 68 bool isContTarget() const { return kind <= While DoStmtK; }69 bool isBreakTarget() const { return kind != CaseStmtK; }70 bool isFallTarget() const { return kind == CaseStmtK; }71 bool isFallDefaultTarget() const { return kind == SwitchStmtK; }53 public: 54 Entry( const ast::ForStmt * stmt, ast::Label breakExit, ast::Label contExit ) : 55 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmt ) {} 56 Entry( const ast::WhileStmt * stmt, ast::Label breakExit, ast::Label contExit ) : 57 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileStmt ) {} 58 Entry( const ast::CompoundStmt *stmt, ast::Label breakExit ) : 59 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmt ) {} 60 Entry( const ast::IfStmt *stmt, ast::Label breakExit ) : 61 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmt ) {} 62 Entry( const ast::CaseStmt *stmt, ast::Label fallExit ) : 63 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmt ) {} 64 Entry( const ast::SwitchStmt *stmt, ast::Label breakExit, ast::Label fallDefaultExit ) : 65 stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmt ) {} 66 Entry( const ast::TryStmt *stmt, ast::Label breakExit ) : 67 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmt ) {} 68 69 bool isContTarget() const { return kind <= WhileStmt; } 70 bool isBreakTarget() const { return CaseStmt != kind; } 71 bool isFallTarget() const { return CaseStmt == kind; } 72 bool isFallDefaultTarget() const { return SwitchStmt == kind; } 72 73 73 74 // These routines set a target as being "used" by a BranchStmt 74 Label useContExit() { assert( kind <= WhileDoStmtK); return useTarget(secondTarget); }75 Label useBreakExit() { assert( kind != CaseStmtK); return useTarget(firstTarget); }76 Label useFallExit() { assert( kind == CaseStmtK); return useTarget(firstTarget); }77 Label useFallDefaultExit() { assert( kind == SwitchStmtK); return useTarget(secondTarget); }75 ast::Label useContExit() { assert( kind <= WhileStmt ); return useTarget(secondTarget); } 76 ast::Label useBreakExit() { assert( CaseStmt != kind ); return useTarget(firstTarget); } 77 ast::Label useFallExit() { assert( CaseStmt == kind ); return useTarget(firstTarget); } 78 ast::Label useFallDefaultExit() { assert( SwitchStmt == kind ); return useTarget(secondTarget); } 78 79 79 80 // These routines check if a specific label for a statement is used by a BranchStmt 80 bool isContUsed() const { assert( kind <= While DoStmtK); return secondTarget.used; }81 bool isBreakUsed() const { assert( kind != CaseStmtK); return firstTarget.used; }82 bool isFallUsed() const { assert( kind == CaseStmtK); return firstTarget.used; }83 bool isFallDefaultUsed() const { assert( kind == SwitchStmtK); return secondTarget.used; }81 bool isContUsed() const { assert( kind <= WhileStmt ); return secondTarget.used; } 82 bool isBreakUsed() const { assert( CaseStmt != kind ); return firstTarget.used; } 83 bool isFallUsed() const { assert( CaseStmt == kind ); return firstTarget.used; } 84 bool isFallDefaultUsed() const { assert( SwitchStmt == kind ); return secondTarget.used; } 84 85 void seenDefault() { fallDefaultValid = false; } 85 86 bool isFallDefaultValid() const { return fallDefaultValid; } 86 87 }; 87 88 88 // Helper predicates used in find_if calls (it doesn't take methods):89 // Helper predicates used in std::find_if calls (it doesn't take methods): 89 90 bool isBreakTarget( const Entry & entry ) { 90 91 return entry.isBreakTarget(); … … 104 105 105 106 struct MultiLevelExitCore final : 106 publicWithVisitorRef<MultiLevelExitCore>,107 public WithShortCircuiting, publicWithGuards {107 public ast::WithVisitorRef<MultiLevelExitCore>, 108 public ast::WithShortCircuiting, public ast::WithGuards { 108 109 MultiLevelExitCore( const LabelToStmt & lt ); 109 110 110 void previsit( const FunctionDecl * );111 112 const CompoundStmt * previsit( constCompoundStmt * );113 const BranchStmt * postvisit( constBranchStmt * );114 void previsit( const WhileDoStmt * );115 const WhileDoStmt * postvisit( const WhileDoStmt * );116 void previsit( const ForStmt * );117 const ForStmt * postvisit( constForStmt * );118 const CaseStmt * previsit( constCaseStmt * );119 void previsit( const IfStmt * );120 const IfStmt * postvisit( constIfStmt * );121 void previsit( const SwitchStmt * );122 const SwitchStmt * postvisit( constSwitchStmt * );123 void previsit( const ReturnStmt * );124 void previsit( const TryStmt * );125 void postvisit( const TryStmt * );126 void previsit( const FinallyStmt * );127 128 const Stmt * mutateLoop( constStmt * body, Entry& );111 void previsit( const ast::FunctionDecl * ); 112 113 const ast::CompoundStmt * previsit( const ast::CompoundStmt * ); 114 const ast::BranchStmt * postvisit( const ast::BranchStmt * ); 115 void previsit( const ast::WhileStmt * ); 116 const ast::WhileStmt * postvisit( const ast::WhileStmt * ); 117 void previsit( const ast::ForStmt * ); 118 const ast::ForStmt * postvisit( const ast::ForStmt * ); 119 const ast::CaseStmt * previsit( const ast::CaseStmt * ); 120 void previsit( const ast::IfStmt * ); 121 const ast::IfStmt * postvisit( const ast::IfStmt * ); 122 void previsit( const ast::SwitchStmt * ); 123 const ast::SwitchStmt * postvisit( const ast::SwitchStmt * ); 124 void previsit( const ast::ReturnStmt * ); 125 void previsit( const ast::TryStmt * ); 126 void postvisit( const ast::TryStmt * ); 127 void previsit( const ast::FinallyStmt * ); 128 129 const ast::Stmt * mutateLoop( const ast::Stmt * body, Entry& ); 129 130 130 131 const LabelToStmt & target_table; 131 s et<Label> fallthrough_labels;132 vector<Entry> enclosing_control_structures;133 Label break_label;132 std::set<ast::Label> fallthrough_labels; 133 std::vector<Entry> enclosing_control_structures; 134 ast::Label break_label; 134 135 bool inFinally; 135 136 … … 139 140 const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt ); 140 141 141 list<ptr<Stmt>> fixBlock(142 const list<ptr<Stmt>> & kids, bool caseClause );142 std::list<ast::ptr<ast::Stmt>> fixBlock( 143 const std::list<ast::ptr<ast::Stmt>> & kids, bool caseClause ); 143 144 144 145 template<typename UnaryPredicate> 145 146 auto findEnclosingControlStructure( UnaryPredicate pred ) { 146 return find_if( enclosing_control_structures.rbegin(),147 enclosing_control_structures.rend(), pred );147 return std::find_if( enclosing_control_structures.rbegin(), 148 enclosing_control_structures.rend(), pred ); 148 149 } 149 150 }; 150 151 151 NullStmt * labelledNullStmt(152 const CodeLocation & cl, constLabel & label ) {153 return new NullStmt( cl, vector<Label>{ label } );152 ast::NullStmt * labelledNullStmt( 153 const CodeLocation & cl, const ast::Label & label ) { 154 return new ast::NullStmt( cl, std::vector<ast::Label>{ label } ); 154 155 } 155 156 … … 159 160 {} 160 161 161 void MultiLevelExitCore::previsit( const FunctionDecl * ) {162 void MultiLevelExitCore::previsit( const ast::FunctionDecl * ) { 162 163 visit_children = false; 163 164 } 164 165 165 const CompoundStmt * MultiLevelExitCore::previsit(166 constCompoundStmt * stmt ) {166 const ast::CompoundStmt * MultiLevelExitCore::previsit( 167 const ast::CompoundStmt * stmt ) { 167 168 visit_children = false; 168 169 … … 170 171 bool isLabeled = !stmt->labels.empty(); 171 172 if ( isLabeled ) { 172 Label breakLabel =newLabel( "blockBreak", stmt );173 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt ); 173 174 enclosing_control_structures.emplace_back( stmt, breakLabel ); 174 175 GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); 175 176 } 176 177 177 auto mutStmt = mutate( stmt );178 auto mutStmt = ast::mutate( stmt ); 178 179 // A child statement may set the break label. 179 mutStmt->kids = move( fixBlock( stmt->kids, false ) );180 mutStmt->kids = std::move( fixBlock( stmt->kids, false ) ); 180 181 181 182 if ( isLabeled ) { … … 190 191 191 192 size_t getUnusedIndex( 192 const Stmt * stmt, constLabel & originalTarget ) {193 const ast::Stmt * stmt, const ast::Label & originalTarget ) { 193 194 const size_t size = stmt->labels.size(); 194 195 195 // If the label is empty, do not add unused attribute.196 if ( originalTarget.empty() ) return size;196 // If the label is empty, we can skip adding the unused attribute: 197 if ( originalTarget.empty() ) return size; 197 198 198 199 // Search for a label that matches the originalTarget. 199 200 for ( size_t i = 0 ; i < size ; ++i ) { 200 const Label & label = stmt->labels[i];201 const ast::Label & label = stmt->labels[i]; 201 202 if ( label == originalTarget ) { 202 for ( const Attribute * attr : label.attributes ) {203 for ( const ast::Attribute * attr : label.attributes ) { 203 204 if ( attr->name == "unused" ) return size; 204 205 } … … 207 208 } 208 209 assertf( false, "Could not find label '%s' on statement %s", 209 originalTarget.name.c_str(), toString( stmt ).c_str() );210 } 211 212 const Stmt * addUnused(213 const Stmt * stmt, constLabel & originalTarget ) {210 originalTarget.name.c_str(), toString( stmt ).c_str() ); 211 } 212 213 const ast::Stmt * addUnused( 214 const ast::Stmt * stmt, const ast::Label & originalTarget ) { 214 215 size_t i = getUnusedIndex( stmt, originalTarget ); 215 216 if ( i == stmt->labels.size() ) { 216 217 return stmt; 217 218 } 218 Stmt * mutStmt =mutate( stmt );219 mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) );219 ast::Stmt * mutStmt = ast::mutate( stmt ); 220 mutStmt->labels[i].attributes.push_back( new ast::Attribute( "unused" ) ); 220 221 return mutStmt; 221 222 } … … 223 224 // This routine updates targets on enclosing control structures to indicate which 224 225 // label is used by the BranchStmt that is passed 225 const BranchStmt * MultiLevelExitCore::postvisit( constBranchStmt * stmt ) {226 vector<Entry>::reverse_iterator targetEntry =226 const ast::BranchStmt * MultiLevelExitCore::postvisit( const ast::BranchStmt * stmt ) { 227 std::vector<Entry>::reverse_iterator targetEntry = 227 228 enclosing_control_structures.rend(); 228 229 229 230 // Labels on different stmts require different approaches to access 230 231 switch ( stmt->kind ) { 231 caseBranchStmt::Goto:232 case ast::BranchStmt::Goto: 232 233 return stmt; 233 case BranchStmt::Continue: 234 case BranchStmt::Break: { 235 bool isContinue = stmt->kind == BranchStmt::Continue; 236 // Handle unlabeled break and continue. 237 if ( stmt->target.empty() ) { 238 if ( isContinue ) { 239 targetEntry = findEnclosingControlStructure( isContinueTarget ); 240 } else { 241 if ( enclosing_control_structures.empty() ) { 242 SemanticError( stmt->location, 243 "'break' outside a loop, 'switch', or labelled block" ); 244 } 245 targetEntry = findEnclosingControlStructure( isBreakTarget ); 246 } 247 // Handle labeled break and continue. 248 } else { 249 // Lookup label in table to find attached control structure. 250 targetEntry = findEnclosingControlStructure( 251 [ targetStmt = target_table.at(stmt->target) ](auto entry){ 252 return entry.stmt == targetStmt; 253 } ); 254 } 255 // Ensure that selected target is valid. 256 if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) { 257 SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"), 258 " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), 259 stmt->originalTarget ) ); 260 } 261 break; 262 } 263 // handle fallthrough in case/switch stmts 264 case BranchStmt::FallThrough: { 265 targetEntry = findEnclosingControlStructure( isFallthroughTarget ); 266 // Check that target is valid. 267 if ( targetEntry == enclosing_control_structures.rend() ) { 268 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 269 } 270 if ( !stmt->target.empty() ) { 271 // Labelled fallthrough: target must be a valid fallthough label. 272 if ( !fallthrough_labels.count( stmt->target ) ) { 273 SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", 274 stmt->originalTarget ) ); 275 } 276 return new BranchStmt( 277 stmt->location, BranchStmt::Goto, stmt->originalTarget ); 278 } 279 break; 280 } 281 case BranchStmt::FallThroughDefault: { 282 targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); 283 284 // Check if in switch or choose statement. 285 if ( targetEntry == enclosing_control_structures.rend() ) { 286 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 287 } 288 289 // Check if switch or choose has default clause. 290 auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); 291 bool foundDefault = false; 292 for ( auto subStmt : switchStmt->stmts ) { 293 const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>(); 294 if ( caseStmt->isDefault() ) { 295 foundDefault = true; 296 break; 297 } 298 } 299 if ( ! foundDefault ) { 300 SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'" 301 "control structure with a 'default' clause" ); 302 } 303 break; 304 } 305 default: 234 case ast::BranchStmt::Continue: 235 case ast::BranchStmt::Break: { 236 bool isContinue = stmt->kind == ast::BranchStmt::Continue; 237 // Handle unlabeled break and continue. 238 if ( stmt->target.empty() ) { 239 if ( isContinue ) { 240 targetEntry = findEnclosingControlStructure( isContinueTarget ); 241 } else { 242 if ( enclosing_control_structures.empty() ) { 243 SemanticError( stmt->location, 244 "'break' outside a loop, 'switch', or labelled block" ); 245 } 246 targetEntry = findEnclosingControlStructure( isBreakTarget ); 247 } 248 // Handle labeled break and continue. 249 } else { 250 // Lookup label in table to find attached control structure. 251 targetEntry = findEnclosingControlStructure( 252 [ targetStmt = target_table.at(stmt->target) ](auto entry){ 253 return entry.stmt == targetStmt; 254 } ); 255 } 256 // Ensure that selected target is valid. 257 if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) { 258 SemanticError( 259 stmt->location, 260 toString( (isContinue ? "'continue'" : "'break'"), 261 " target must be an enclosing ", 262 (isContinue ? "loop: " : "control structure: "), 263 stmt->originalTarget ) ); 264 } 265 break; 266 } 267 // handle fallthrough in case/switch stmts 268 case ast::BranchStmt::FallThrough: { 269 targetEntry = findEnclosingControlStructure( isFallthroughTarget ); 270 // Check that target is valid. 271 if ( targetEntry == enclosing_control_structures.rend() ) { 272 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 273 } 274 if ( !stmt->target.empty() ) { 275 // Labelled fallthrough: target must be a valid fallthough label. 276 if ( !fallthrough_labels.count( stmt->target ) ) { 277 SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", stmt->originalTarget ) ); 278 } 279 return new ast::BranchStmt( 280 stmt->location, ast::BranchStmt::Goto, stmt->originalTarget ); 281 } 282 break; 283 } 284 case ast::BranchStmt::FallThroughDefault: { 285 targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); 286 287 // Check that this is in a switch or choose statement. 288 if ( targetEntry == enclosing_control_structures.rend() ) { 289 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 290 } 291 292 // Check that the switch or choose has a default clause. 293 auto switchStmt = strict_dynamic_cast< const ast::SwitchStmt * >( 294 targetEntry->stmt ); 295 bool foundDefault = false; 296 for ( auto subStmt : switchStmt->stmts ) { 297 const ast::CaseStmt * caseStmt = subStmt.strict_as<ast::CaseStmt>(); 298 if ( caseStmt->isDefault() ) { 299 foundDefault = true; 300 break; 301 } 302 } 303 if ( !foundDefault ) { 304 SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose' control structure with a 'default' clause" ); 305 } 306 break; 307 } 308 default: 306 309 assert( false ); 307 310 } 308 311 309 312 // Branch error checks: get the appropriate label name: 310 // (This label is alwaysreplaced.)311 Label exitLabel( CodeLocation(), "" );313 // (This label will always be replaced.) 314 ast::Label exitLabel( CodeLocation(), "" ); 312 315 switch ( stmt->kind ) { 313 caseBranchStmt::Break:316 case ast::BranchStmt::Break: 314 317 assert( !targetEntry->useBreakExit().empty() ); 315 318 exitLabel = targetEntry->useBreakExit(); 316 319 break; 317 caseBranchStmt::Continue:320 case ast::BranchStmt::Continue: 318 321 assert( !targetEntry->useContExit().empty() ); 319 322 exitLabel = targetEntry->useContExit(); 320 323 break; 321 caseBranchStmt::FallThrough:324 case ast::BranchStmt::FallThrough: 322 325 assert( !targetEntry->useFallExit().empty() ); 323 326 exitLabel = targetEntry->useFallExit(); 324 327 break; 325 caseBranchStmt::FallThroughDefault:328 case ast::BranchStmt::FallThroughDefault: 326 329 assert( !targetEntry->useFallDefaultExit().empty() ); 327 330 exitLabel = targetEntry->useFallDefaultExit(); 328 331 // Check that fallthrough default comes before the default clause. 329 332 if ( !targetEntry->isFallDefaultValid() ) { 330 SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); 333 SemanticError( stmt->location, 334 "'fallthrough default' must precede the 'default' clause" ); 331 335 } 332 336 break; 333 default:337 default: 334 338 assert(0); 335 339 } … … 338 342 targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); 339 343 340 // Replace withgoto to make later passes more uniform.341 return new BranchStmt( stmt->location,BranchStmt::Goto, exitLabel );342 } 343 344 void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) {344 // Replace this with a goto to make later passes more uniform. 345 return new ast::BranchStmt( stmt->location, ast::BranchStmt::Goto, exitLabel ); 346 } 347 348 void MultiLevelExitCore::previsit( const ast::WhileStmt * stmt ) { 345 349 return prehandleLoopStmt( stmt ); 346 350 } 347 351 348 const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) {352 const ast::WhileStmt * MultiLevelExitCore::postvisit( const ast::WhileStmt * stmt ) { 349 353 return posthandleLoopStmt( stmt ); 350 354 } 351 355 352 void MultiLevelExitCore::previsit( const ForStmt * stmt ) {356 void MultiLevelExitCore::previsit( const ast::ForStmt * stmt ) { 353 357 return prehandleLoopStmt( stmt ); 354 358 } 355 359 356 const ForStmt * MultiLevelExitCore::postvisit( constForStmt * stmt ) {360 const ast::ForStmt * MultiLevelExitCore::postvisit( const ast::ForStmt * stmt ) { 357 361 return posthandleLoopStmt( stmt ); 358 362 } … … 360 364 // Mimic what the built-in push_front would do anyways. It is O(n). 361 365 void push_front( 362 vector<ptr<Stmt>> & vec, constStmt * element ) {366 std::vector<ast::ptr<ast::Stmt>> & vec, const ast::Stmt * element ) { 363 367 vec.emplace_back( nullptr ); 364 368 for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { 365 vec[ i ] = move( vec[ i - 1 ] );369 vec[ i ] = std::move( vec[ i - 1 ] ); 366 370 } 367 371 vec[ 0 ] = element; 368 372 } 369 373 370 const CaseStmt * MultiLevelExitCore::previsit( constCaseStmt * stmt ) {374 const ast::CaseStmt * MultiLevelExitCore::previsit( const ast::CaseStmt * stmt ) { 371 375 visit_children = false; 372 376 373 // If default, markseen.377 // If it is the default, mark the default as seen. 374 378 if ( stmt->isDefault() ) { 375 379 assert( !enclosing_control_structures.empty() ); … … 378 382 379 383 // The cond may not exist, but if it does update it now. 380 visitor->maybe_accept( stmt, & CaseStmt::cond );384 visitor->maybe_accept( stmt, &ast::CaseStmt::cond ); 381 385 382 386 // Just save the mutated node for simplicity. 383 CaseStmt * mutStmt =mutate( stmt );384 385 Label fallLabel =newLabel( "fallThrough", stmt );386 if ( ! mutStmt->stmts.empty() ) {387 ast::CaseStmt * mutStmt = ast::mutate( stmt ); 388 389 ast::Label fallLabel = LabelGenerator::newLabel( "fallThrough", stmt ); 390 if ( !mutStmt->stmts.empty() ) { 387 391 // Ensure that the stack isn't corrupted by exceptions in fixBlock. 388 392 auto guard = makeFuncGuard( 389 393 [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); }, 390 394 [this](){ enclosing_control_structures.pop_back(); } 391 );395 ); 392 396 393 397 // These should already be in a block. 394 auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() );398 auto block = ast::mutate( mutStmt->stmts.front().strict_as<ast::CompoundStmt>() ); 395 399 block->kids = fixBlock( block->kids, true ); 396 400 397 401 // Add fallthrough label if necessary. 398 assert( ! enclosing_control_structures.empty() );402 assert( !enclosing_control_structures.empty() ); 399 403 Entry & entry = enclosing_control_structures.back(); 400 404 if ( entry.isFallUsed() ) { … … 403 407 } 404 408 } 405 assert( ! enclosing_control_structures.empty() );409 assert( !enclosing_control_structures.empty() ); 406 410 Entry & entry = enclosing_control_structures.back(); 407 assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ),408 "Control structure enclosing a case clause must be a switch, but is: %s",409 toString( entry.stmt ).c_str() );411 assertf( dynamic_cast< const ast::SwitchStmt * >( entry.stmt ), 412 "Control structure enclosing a case clause must be a switch, but is: %s", 413 toString( entry.stmt ).c_str() ); 410 414 if ( mutStmt->isDefault() ) { 411 415 if ( entry.isFallDefaultUsed() ) { 412 416 // Add fallthrough default label if necessary. 413 417 push_front( mutStmt->stmts, labelledNullStmt( 414 stmt->location, entry.useFallDefaultExit()415 ) );418 stmt->location, entry.useFallDefaultExit() 419 ) ); 416 420 } 417 421 } … … 419 423 } 420 424 421 void MultiLevelExitCore::previsit( const IfStmt * stmt ) {425 void MultiLevelExitCore::previsit( const ast::IfStmt * stmt ) { 422 426 bool labeledBlock = !stmt->labels.empty(); 423 427 if ( labeledBlock ) { 424 Label breakLabel =newLabel( "blockBreak", stmt );428 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt ); 425 429 enclosing_control_structures.emplace_back( stmt, breakLabel ); 426 430 GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); … … 428 432 } 429 433 430 const IfStmt * MultiLevelExitCore::postvisit( constIfStmt * stmt ) {434 const ast::IfStmt * MultiLevelExitCore::postvisit( const ast::IfStmt * stmt ) { 431 435 bool labeledBlock = !stmt->labels.empty(); 432 436 if ( labeledBlock ) { … … 439 443 } 440 444 441 bool isDefaultCase( const ptr<Stmt> & stmt ) {442 const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>();445 bool isDefaultCase( const ast::ptr<ast::Stmt> & stmt ) { 446 const ast::CaseStmt * caseStmt = stmt.strict_as<ast::CaseStmt>(); 443 447 return caseStmt->isDefault(); 444 448 } 445 449 446 void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) {447 Label label =newLabel( "switchBreak", stmt );448 auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );449 450 const CaseStmt * defaultCase = it != stmt->stmts.rend()451 ? (it)->strict_as< CaseStmt>() : nullptr;452 Label defaultLabel = defaultCase453 ? newLabel( "fallThroughDefault", defaultCase )454 : Label( stmt->location, "" );450 void MultiLevelExitCore::previsit( const ast::SwitchStmt * stmt ) { 451 ast::Label label = LabelGenerator::newLabel( "switchBreak", stmt ); 452 auto it = std::find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase ); 453 454 const ast::CaseStmt * defaultCase = it != stmt->stmts.rend() 455 ? (it)->strict_as<ast::CaseStmt>() : nullptr; 456 ast::Label defaultLabel = defaultCase 457 ? LabelGenerator::newLabel( "fallThroughDefault", defaultCase ) 458 : ast::Label( stmt->location, "" ); 455 459 enclosing_control_structures.emplace_back( stmt, label, defaultLabel ); 456 460 GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); 457 461 458 462 // Collect valid labels for fallthrough. It starts with all labels at 459 // this level, then remove as each is seen duringtraversal.460 for ( const Stmt * stmt : stmt->stmts ) {461 auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt );463 // this level, then removed as we see them in traversal. 464 for ( const ast::Stmt * stmt : stmt->stmts ) { 465 auto * caseStmt = strict_dynamic_cast< const ast::CaseStmt * >( stmt ); 462 466 if ( caseStmt->stmts.empty() ) continue; 463 auto block = caseStmt->stmts.front().strict_as< CompoundStmt>();464 for ( const Stmt * stmt : block->kids ) {465 for ( const Label & l : stmt->labels ) {467 auto block = caseStmt->stmts.front().strict_as<ast::CompoundStmt>(); 468 for ( const ast::Stmt * stmt : block->kids ) { 469 for ( const ast::Label & l : stmt->labels ) { 466 470 fallthrough_labels.insert( l ); 467 471 } … … 470 474 } 471 475 472 const SwitchStmt * MultiLevelExitCore::postvisit( constSwitchStmt * stmt ) {476 const ast::SwitchStmt * MultiLevelExitCore::postvisit( const ast::SwitchStmt * stmt ) { 473 477 assert( !enclosing_control_structures.empty() ); 474 478 Entry & entry = enclosing_control_structures.back(); 475 479 assert( entry.stmt == stmt ); 476 480 477 // Only run to generate the break label.481 // Only run if we need to generate the break label. 478 482 if ( entry.isBreakUsed() ) { 479 483 // To keep the switch statements uniform (all direct children of a 480 484 // SwitchStmt should be CastStmts), append the exit label and break 481 485 // to the last case, create a default case is there are no cases. 482 SwitchStmt * mutStmt =mutate( stmt );486 ast::SwitchStmt * mutStmt = ast::mutate( stmt ); 483 487 if ( mutStmt->stmts.empty() ) { 484 mutStmt->stmts.push_back( new CaseStmt(485 mutStmt->location, nullptr, {} ));486 } 487 488 auto caseStmt = mutStmt->stmts.back().strict_as< CaseStmt>();489 auto mutCase = mutate( caseStmt );488 mutStmt->stmts.push_back( new ast::CaseStmt( 489 mutStmt->location, nullptr, {} )); 490 } 491 492 auto caseStmt = mutStmt->stmts.back().strict_as<ast::CaseStmt>(); 493 auto mutCase = ast::mutate( caseStmt ); 490 494 mutStmt->stmts.back() = mutCase; 491 495 492 Label label( mutCase->location, "breakLabel" );493 auto branch = new BranchStmt( mutCase->location,BranchStmt::Break, label );496 ast::Label label( mutCase->location, "breakLabel" ); 497 auto branch = new ast::BranchStmt( mutCase->location, ast::BranchStmt::Break, label ); 494 498 branch->labels.push_back( entry.useBreakExit() ); 495 499 mutCase->stmts.push_back( branch ); … … 500 504 } 501 505 502 void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) {506 void MultiLevelExitCore::previsit( const ast::ReturnStmt * stmt ) { 503 507 if ( inFinally ) { 504 508 SemanticError( stmt->location, "'return' may not appear in a finally clause" ); … … 506 510 } 507 511 508 void MultiLevelExitCore::previsit( const TryStmt * stmt ) {512 void MultiLevelExitCore::previsit( const ast::TryStmt * stmt ) { 509 513 bool isLabeled = !stmt->labels.empty(); 510 514 if ( isLabeled ) { 511 Label breakLabel =newLabel( "blockBreak", stmt );515 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt ); 512 516 enclosing_control_structures.emplace_back( stmt, breakLabel ); 513 517 GuardAction([this](){ enclosing_control_structures.pop_back(); } ); … … 515 519 } 516 520 517 void MultiLevelExitCore::postvisit( const TryStmt * stmt ) {521 void MultiLevelExitCore::postvisit( const ast::TryStmt * stmt ) { 518 522 bool isLabeled = !stmt->labels.empty(); 519 523 if ( isLabeled ) { … … 525 529 } 526 530 527 void MultiLevelExitCore::previsit( const FinallyStmt * ) {528 GuardAction([this, old = move(enclosing_control_structures)](){529 enclosing_control_structures =move(old);530 });531 enclosing_control_structures = vector<Entry>();531 void MultiLevelExitCore::previsit( const ast::FinallyStmt * ) { 532 GuardAction([this, old = std::move(enclosing_control_structures)](){ 533 enclosing_control_structures = std::move(old); 534 }); 535 enclosing_control_structures = std::vector<Entry>(); 532 536 GuardValue( inFinally ) = true; 533 537 } 534 538 535 const Stmt * MultiLevelExitCore::mutateLoop(536 constStmt * body, Entry & entry ) {539 const ast::Stmt * MultiLevelExitCore::mutateLoop( 540 const ast::Stmt * body, Entry & entry ) { 537 541 if ( entry.isBreakUsed() ) { 538 542 break_label = entry.useBreakExit(); … … 541 545 // if continue is used insert a continue label into the back of the body of the loop 542 546 if ( entry.isContUsed() ) { 543 CompoundStmt * new_body = newCompoundStmt( body->location );547 ast::CompoundStmt * new_body = new ast::CompoundStmt( body->location ); 544 548 // {} 545 549 new_body->kids.push_back( body ); … … 563 567 // Remember is loop before going onto mutate the body. 564 568 // The labels will be folded in if they are used. 565 Label breakLabel =newLabel( "loopBreak", loopStmt );566 Label contLabel =newLabel( "loopContinue", loopStmt );569 ast::Label breakLabel = LabelGenerator::newLabel( "loopBreak", loopStmt ); 570 ast::Label contLabel = LabelGenerator::newLabel( "loopContinue", loopStmt ); 567 571 enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); 568 572 // labels are added temporarily to see if they are used and then added permanently in postvisit if ther are used … … 579 583 assert( entry.stmt == loopStmt ); 580 584 581 // Now check if the labels are used and add them if so.582 return mutate_field(585 // Now we check if the labels are used and add them if so. 586 return ast::mutate_field( 583 587 loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); 584 588 // this call to mutate_field compares loopStmt->body and the result of mutateLoop … … 587 591 } 588 592 589 list<ptr<Stmt>> MultiLevelExitCore::fixBlock(590 const list<ptr<Stmt>> & kids, bool is_case_clause ) {591 // Unfortunately cannot use automatic error collection.593 std::list<ast::ptr<ast::Stmt>> MultiLevelExitCore::fixBlock( 594 const std::list<ast::ptr<ast::Stmt>> & kids, bool is_case_clause ) { 595 // Unfortunately we can't use the automatic error collection. 592 596 SemanticErrorException errors; 593 597 594 list<ptr<Stmt>> ret;598 std::list<ast::ptr<ast::Stmt>> ret; 595 599 596 600 // Manually visit each child. 597 for ( const ptr<Stmt> & kid : kids ) {601 for ( const ast::ptr<ast::Stmt> & kid : kids ) { 598 602 if ( is_case_clause ) { 599 603 // Once a label is seen, it's no longer a valid for fallthrough. 600 for ( const Label & l : kid->labels ) {604 for ( const ast::Label & l : kid->labels ) { 601 605 fallthrough_labels.erase( l ); 602 606 } … … 612 616 ret.push_back( 613 617 labelledNullStmt( ret.back()->location, break_label ) ); 614 break_label = Label( CodeLocation(), "" );618 break_label = ast::Label( CodeLocation(), "" ); 615 619 } 616 620 } … … 622 626 } 623 627 624 const CompoundStmt * multiLevelExitUpdate( 625 const CompoundStmt * stmt, 626 const LabelToStmt & labelTable ) { 628 } // namespace 629 630 const ast::CompoundStmt * multiLevelExitUpdate( 631 const ast::CompoundStmt * stmt, 632 const LabelToStmt & labelTable ) { 627 633 // Must start in the body, so FunctionDecls can be a stopping point. 628 Pass<MultiLevelExitCore> visitor( labelTable );629 const CompoundStmt * ret = stmt->accept( visitor );634 ast::Pass<MultiLevelExitCore> visitor( labelTable ); 635 const ast::CompoundStmt * ret = stmt->accept( visitor ); 630 636 return ret; 631 637 } 638 632 639 } // namespace ControlStruct 633 640
Note:
See TracChangeset
for help on using the changeset viewer.