[b8ab91a] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[817bb3c] | 7 | // MultiLevelExit.cpp -- Replaces CFA's local control flow with C's versions. |
---|
[b8ab91a] | 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Mon Nov 1 13:48:00 2021 |
---|
[400b8be] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Mon Mar 28 9:42:00 2022 |
---|
| 13 | // Update Count : 34 |
---|
[b8ab91a] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "MultiLevelExit.hpp" |
---|
| 17 | |
---|
| 18 | #include "AST/Pass.hpp" |
---|
| 19 | #include "AST/Stmt.hpp" |
---|
[3f681b1] | 20 | #include "Common/CodeLocationTools.hpp" |
---|
[66daee4] | 21 | #include "LabelGeneratorNew.hpp" |
---|
[b8ab91a] | 22 | |
---|
| 23 | #include <set> |
---|
[66daee4] | 24 | using namespace std; |
---|
| 25 | using namespace ast; |
---|
[b8ab91a] | 26 | |
---|
| 27 | namespace ControlStruct { |
---|
| 28 | class Entry { |
---|
[66daee4] | 29 | public: |
---|
| 30 | const Stmt * stmt; |
---|
| 31 | private: |
---|
[b8ab91a] | 32 | // Organized like a manual ADT. Avoids creating a bunch of dead data. |
---|
| 33 | struct Target { |
---|
[66daee4] | 34 | Label label; |
---|
[b8ab91a] | 35 | bool used = false; |
---|
[66daee4] | 36 | Target( const Label & label ) : label( label ) {} |
---|
[b8ab91a] | 37 | Target() : label( CodeLocation() ) {} |
---|
| 38 | }; |
---|
| 39 | Target firstTarget; |
---|
| 40 | Target secondTarget; |
---|
| 41 | |
---|
| 42 | enum Kind { |
---|
[400b8be] | 43 | ForStmtK, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseClauseK, SwitchStmtK, TryStmtK |
---|
[b8ab91a] | 44 | } kind; |
---|
| 45 | |
---|
| 46 | bool fallDefaultValid = true; |
---|
| 47 | |
---|
[66daee4] | 48 | static Label & useTarget( Target & target ) { |
---|
[b8ab91a] | 49 | target.used = true; |
---|
| 50 | return target.label; |
---|
| 51 | } |
---|
[66daee4] | 52 | public: |
---|
| 53 | Entry( const ForStmt * stmt, Label breakExit, Label contExit ) : |
---|
| 54 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {} |
---|
[3b0bc16] | 55 | Entry( const WhileDoStmt * stmt, Label breakExit, Label contExit ) : |
---|
| 56 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileDoStmtK ) {} |
---|
[66daee4] | 57 | Entry( const CompoundStmt *stmt, Label breakExit ) : |
---|
| 58 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {} |
---|
| 59 | Entry( const IfStmt *stmt, Label breakExit ) : |
---|
| 60 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {} |
---|
[400b8be] | 61 | Entry( const CaseClause *, const CompoundStmt *stmt, Label fallExit ) : |
---|
| 62 | stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseClauseK ) {} |
---|
[66daee4] | 63 | Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) : |
---|
| 64 | stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {} |
---|
| 65 | Entry( const TryStmt *stmt, Label breakExit ) : |
---|
| 66 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {} |
---|
| 67 | |
---|
[3b0bc16] | 68 | bool isContTarget() const { return kind <= WhileDoStmtK; } |
---|
[400b8be] | 69 | bool isBreakTarget() const { return kind != CaseClauseK; } |
---|
| 70 | bool isFallTarget() const { return kind == CaseClauseK; } |
---|
[66daee4] | 71 | bool isFallDefaultTarget() const { return kind == SwitchStmtK; } |
---|
[b8ab91a] | 72 | |
---|
[3e5db5b4] | 73 | // These routines set a target as being "used" by a BranchStmt |
---|
[3b0bc16] | 74 | Label useContExit() { assert( kind <= WhileDoStmtK ); return useTarget(secondTarget); } |
---|
[400b8be] | 75 | Label useBreakExit() { assert( kind != CaseClauseK ); return useTarget(firstTarget); } |
---|
| 76 | Label useFallExit() { assert( kind == CaseClauseK ); return useTarget(firstTarget); } |
---|
[66daee4] | 77 | Label useFallDefaultExit() { assert( kind == SwitchStmtK ); return useTarget(secondTarget); } |
---|
[b8ab91a] | 78 | |
---|
[3e5db5b4] | 79 | // These routines check if a specific label for a statement is used by a BranchStmt |
---|
[3b0bc16] | 80 | bool isContUsed() const { assert( kind <= WhileDoStmtK ); return secondTarget.used; } |
---|
[400b8be] | 81 | bool isBreakUsed() const { assert( kind != CaseClauseK ); return firstTarget.used; } |
---|
| 82 | bool isFallUsed() const { assert( kind == CaseClauseK ); return firstTarget.used; } |
---|
[66daee4] | 83 | bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; } |
---|
[b8ab91a] | 84 | void seenDefault() { fallDefaultValid = false; } |
---|
| 85 | bool isFallDefaultValid() const { return fallDefaultValid; } |
---|
| 86 | }; |
---|
| 87 | |
---|
[66daee4] | 88 | // Helper predicates used in find_if calls (it doesn't take methods): |
---|
[b8ab91a] | 89 | bool isBreakTarget( const Entry & entry ) { |
---|
| 90 | return entry.isBreakTarget(); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | bool isContinueTarget( const Entry & entry ) { |
---|
| 94 | return entry.isContTarget(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | bool isFallthroughTarget( const Entry & entry ) { |
---|
| 98 | return entry.isFallTarget(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | bool isFallthroughDefaultTarget( const Entry & entry ) { |
---|
| 102 | return entry.isFallDefaultTarget(); |
---|
| 103 | } |
---|
| 104 | |
---|
[817bb3c] | 105 | struct MultiLevelExitCore final : |
---|
[66daee4] | 106 | public WithVisitorRef<MultiLevelExitCore>, |
---|
| 107 | public WithShortCircuiting, public WithGuards { |
---|
[cb921d4] | 108 | MultiLevelExitCore( const LabelToStmt & lt ); |
---|
[b8ab91a] | 109 | |
---|
[66daee4] | 110 | void previsit( const FunctionDecl * ); |
---|
| 111 | |
---|
| 112 | const CompoundStmt * previsit( const CompoundStmt * ); |
---|
| 113 | const BranchStmt * postvisit( const BranchStmt * ); |
---|
[3b0bc16] | 114 | void previsit( const WhileDoStmt * ); |
---|
| 115 | const WhileDoStmt * postvisit( const WhileDoStmt * ); |
---|
[66daee4] | 116 | void previsit( const ForStmt * ); |
---|
| 117 | const ForStmt * postvisit( const ForStmt * ); |
---|
[400b8be] | 118 | const CaseClause * previsit( const CaseClause * ); |
---|
[66daee4] | 119 | void previsit( const IfStmt * ); |
---|
| 120 | const IfStmt * postvisit( const IfStmt * ); |
---|
| 121 | void previsit( const SwitchStmt * ); |
---|
| 122 | const SwitchStmt * postvisit( const SwitchStmt * ); |
---|
| 123 | void previsit( const ReturnStmt * ); |
---|
| 124 | void previsit( const TryStmt * ); |
---|
| 125 | void postvisit( const TryStmt * ); |
---|
[400b8be] | 126 | void previsit( const FinallyClause * ); |
---|
[66daee4] | 127 | |
---|
| 128 | const Stmt * mutateLoop( const Stmt * body, Entry& ); |
---|
[b8ab91a] | 129 | |
---|
[817bb3c] | 130 | const LabelToStmt & target_table; |
---|
[66daee4] | 131 | set<Label> fallthrough_labels; |
---|
| 132 | vector<Entry> enclosing_control_structures; |
---|
| 133 | Label break_label; |
---|
[b8ab91a] | 134 | bool inFinally; |
---|
| 135 | |
---|
| 136 | template<typename LoopNode> |
---|
| 137 | void prehandleLoopStmt( const LoopNode * loopStmt ); |
---|
| 138 | template<typename LoopNode> |
---|
| 139 | const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt ); |
---|
| 140 | |
---|
[66daee4] | 141 | list<ptr<Stmt>> fixBlock( |
---|
| 142 | const list<ptr<Stmt>> & kids, bool caseClause ); |
---|
[b8ab91a] | 143 | |
---|
[817bb3c] | 144 | template<typename UnaryPredicate> |
---|
| 145 | auto findEnclosingControlStructure( UnaryPredicate pred ) { |
---|
[66daee4] | 146 | return find_if( enclosing_control_structures.rbegin(), |
---|
| 147 | enclosing_control_structures.rend(), pred ); |
---|
[817bb3c] | 148 | } |
---|
| 149 | }; |
---|
[b8ab91a] | 150 | |
---|
[66daee4] | 151 | NullStmt * labelledNullStmt( |
---|
| 152 | const CodeLocation & cl, const Label & label ) { |
---|
| 153 | return new NullStmt( cl, vector<Label>{ label } ); |
---|
[b8ab91a] | 154 | } |
---|
| 155 | |
---|
[cb921d4] | 156 | MultiLevelExitCore::MultiLevelExitCore( const LabelToStmt & lt ) : |
---|
| 157 | target_table( lt ), break_label( CodeLocation(), "" ), |
---|
[b8ab91a] | 158 | inFinally( false ) |
---|
| 159 | {} |
---|
| 160 | |
---|
[66daee4] | 161 | void MultiLevelExitCore::previsit( const FunctionDecl * ) { |
---|
[b8ab91a] | 162 | visit_children = false; |
---|
| 163 | } |
---|
| 164 | |
---|
[66daee4] | 165 | const CompoundStmt * MultiLevelExitCore::previsit( |
---|
| 166 | const CompoundStmt * stmt ) { |
---|
[b8ab91a] | 167 | visit_children = false; |
---|
[3e5db5b4] | 168 | |
---|
| 169 | // if the stmt is labelled then generate a label to check in postvisit if the label is used |
---|
[2f52b18] | 170 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 171 | if ( isLabeled ) { |
---|
[66daee4] | 172 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 173 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 174 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); |
---|
| 175 | } |
---|
| 176 | |
---|
[66daee4] | 177 | auto mutStmt = mutate( stmt ); |
---|
[b8ab91a] | 178 | // A child statement may set the break label. |
---|
[891f707] | 179 | mutStmt->kids = fixBlock( stmt->kids, false ); |
---|
[b8ab91a] | 180 | |
---|
| 181 | if ( isLabeled ) { |
---|
[2f52b18] | 182 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 183 | Entry & entry = enclosing_control_structures.back(); |
---|
[2f52b18] | 184 | if ( ! entry.useBreakExit().empty() ) { |
---|
[b8ab91a] | 185 | break_label = entry.useBreakExit(); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | return mutStmt; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | size_t getUnusedIndex( |
---|
[66daee4] | 192 | const Stmt * stmt, const Label & originalTarget ) { |
---|
[b8ab91a] | 193 | const size_t size = stmt->labels.size(); |
---|
| 194 | |
---|
[66daee4] | 195 | // If the label is empty, do not add unused attribute. |
---|
| 196 | if ( originalTarget.empty() ) return size; |
---|
[b8ab91a] | 197 | |
---|
| 198 | // Search for a label that matches the originalTarget. |
---|
| 199 | for ( size_t i = 0 ; i < size ; ++i ) { |
---|
[66daee4] | 200 | const Label & label = stmt->labels[i]; |
---|
[b8ab91a] | 201 | if ( label == originalTarget ) { |
---|
[66daee4] | 202 | for ( const Attribute * attr : label.attributes ) { |
---|
[b8ab91a] | 203 | if ( attr->name == "unused" ) return size; |
---|
| 204 | } |
---|
| 205 | return i; |
---|
| 206 | } |
---|
| 207 | } |
---|
[6180274] | 208 | assertf( false, "CFA internal error: could not find label '%s' on statement %s", |
---|
[66daee4] | 209 | originalTarget.name.c_str(), toString( stmt ).c_str() ); |
---|
[b8ab91a] | 210 | } |
---|
| 211 | |
---|
[66daee4] | 212 | const Stmt * addUnused( |
---|
| 213 | const Stmt * stmt, const Label & originalTarget ) { |
---|
[b8ab91a] | 214 | size_t i = getUnusedIndex( stmt, originalTarget ); |
---|
| 215 | if ( i == stmt->labels.size() ) { |
---|
| 216 | return stmt; |
---|
| 217 | } |
---|
[66daee4] | 218 | Stmt * mutStmt = mutate( stmt ); |
---|
| 219 | mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) ); |
---|
[b8ab91a] | 220 | return mutStmt; |
---|
| 221 | } |
---|
| 222 | |
---|
[3e5db5b4] | 223 | // This routine updates targets on enclosing control structures to indicate which |
---|
| 224 | // label is used by the BranchStmt that is passed |
---|
[66daee4] | 225 | const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) { |
---|
| 226 | vector<Entry>::reverse_iterator targetEntry = |
---|
[b8ab91a] | 227 | enclosing_control_structures.rend(); |
---|
[3e5db5b4] | 228 | |
---|
| 229 | // Labels on different stmts require different approaches to access |
---|
[b8ab91a] | 230 | switch ( stmt->kind ) { |
---|
[491bb81] | 231 | case BranchStmt::Goto: |
---|
[b8ab91a] | 232 | return stmt; |
---|
[491bb81] | 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() ) { |
---|
[66daee4] | 242 | SemanticError( stmt->location, |
---|
| 243 | "'break' outside a loop, 'switch', or labelled block" ); |
---|
[491bb81] | 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){ |
---|
[66daee4] | 252 | return entry.stmt == targetStmt; |
---|
[491bb81] | 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'"), |
---|
[66daee4] | 258 | " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), |
---|
| 259 | stmt->originalTarget ) ); |
---|
[491bb81] | 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: ", |
---|
[66daee4] | 274 | stmt->originalTarget ) ); |
---|
[491bb81] | 275 | } |
---|
| 276 | return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget ); |
---|
| 277 | } |
---|
| 278 | break; |
---|
| 279 | } |
---|
| 280 | case BranchStmt::FallThroughDefault: { |
---|
| 281 | targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); |
---|
| 282 | |
---|
| 283 | // Check if in switch or choose statement. |
---|
| 284 | if ( targetEntry == enclosing_control_structures.rend() ) { |
---|
| 285 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | // Check if switch or choose has default clause. |
---|
| 289 | auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); |
---|
| 290 | bool foundDefault = false; |
---|
| 291 | for ( auto caseStmt : switchStmt->cases ) { |
---|
| 292 | if ( caseStmt->isDefault() ) { |
---|
| 293 | foundDefault = true; |
---|
| 294 | break; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | if ( ! foundDefault ) { |
---|
| 298 | SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'" |
---|
| 299 | "control structure with a 'default' clause" ); |
---|
| 300 | } |
---|
| 301 | break; |
---|
| 302 | } |
---|
| 303 | default: |
---|
[b8ab91a] | 304 | assert( false ); |
---|
| 305 | } |
---|
| 306 | |
---|
[2f52b18] | 307 | // Branch error checks: get the appropriate label name, which is always replaced. |
---|
[66daee4] | 308 | Label exitLabel( CodeLocation(), "" ); |
---|
[b8ab91a] | 309 | switch ( stmt->kind ) { |
---|
[491bb81] | 310 | case BranchStmt::Break: |
---|
[2f52b18] | 311 | assert( ! targetEntry->useBreakExit().empty() ); |
---|
[b8ab91a] | 312 | exitLabel = targetEntry->useBreakExit(); |
---|
| 313 | break; |
---|
[491bb81] | 314 | case BranchStmt::Continue: |
---|
[2f52b18] | 315 | assert( ! targetEntry->useContExit().empty() ); |
---|
[b8ab91a] | 316 | exitLabel = targetEntry->useContExit(); |
---|
| 317 | break; |
---|
[491bb81] | 318 | case BranchStmt::FallThrough: |
---|
[2f52b18] | 319 | assert( ! targetEntry->useFallExit().empty() ); |
---|
[b8ab91a] | 320 | exitLabel = targetEntry->useFallExit(); |
---|
| 321 | break; |
---|
[491bb81] | 322 | case BranchStmt::FallThroughDefault: |
---|
[2f52b18] | 323 | assert( ! targetEntry->useFallDefaultExit().empty() ); |
---|
[b8ab91a] | 324 | exitLabel = targetEntry->useFallDefaultExit(); |
---|
| 325 | // Check that fallthrough default comes before the default clause. |
---|
[2f52b18] | 326 | if ( ! targetEntry->isFallDefaultValid() ) { |
---|
[66daee4] | 327 | SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); |
---|
[b8ab91a] | 328 | } |
---|
| 329 | break; |
---|
[491bb81] | 330 | default: |
---|
[b8ab91a] | 331 | assert(0); |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | // Add unused attribute to silence warnings. |
---|
| 335 | targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); |
---|
| 336 | |
---|
[66daee4] | 337 | // Replace with goto to make later passes more uniform. |
---|
| 338 | return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel ); |
---|
[b8ab91a] | 339 | } |
---|
| 340 | |
---|
[3b0bc16] | 341 | void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) { |
---|
[b8ab91a] | 342 | return prehandleLoopStmt( stmt ); |
---|
| 343 | } |
---|
| 344 | |
---|
[3b0bc16] | 345 | const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) { |
---|
[b8ab91a] | 346 | return posthandleLoopStmt( stmt ); |
---|
| 347 | } |
---|
| 348 | |
---|
[66daee4] | 349 | void MultiLevelExitCore::previsit( const ForStmt * stmt ) { |
---|
[b8ab91a] | 350 | return prehandleLoopStmt( stmt ); |
---|
| 351 | } |
---|
| 352 | |
---|
[66daee4] | 353 | const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) { |
---|
[b8ab91a] | 354 | return posthandleLoopStmt( stmt ); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | // Mimic what the built-in push_front would do anyways. It is O(n). |
---|
| 358 | void push_front( |
---|
[66daee4] | 359 | vector<ptr<Stmt>> & vec, const Stmt * element ) { |
---|
[b8ab91a] | 360 | vec.emplace_back( nullptr ); |
---|
| 361 | for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { |
---|
[66daee4] | 362 | vec[ i ] = move( vec[ i - 1 ] ); |
---|
[b8ab91a] | 363 | } |
---|
| 364 | vec[ 0 ] = element; |
---|
| 365 | } |
---|
| 366 | |
---|
[400b8be] | 367 | const CaseClause * MultiLevelExitCore::previsit( const CaseClause * stmt ) { |
---|
[b8ab91a] | 368 | visit_children = false; |
---|
| 369 | |
---|
[66daee4] | 370 | // If default, mark seen. |
---|
[b8ab91a] | 371 | if ( stmt->isDefault() ) { |
---|
[2f52b18] | 372 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 373 | enclosing_control_structures.back().seenDefault(); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | // The cond may not exist, but if it does update it now. |
---|
[400b8be] | 377 | visitor->maybe_accept( stmt, &CaseClause::cond ); |
---|
[b8ab91a] | 378 | |
---|
| 379 | // Just save the mutated node for simplicity. |
---|
[400b8be] | 380 | CaseClause * mutStmt = mutate( stmt ); |
---|
[b8ab91a] | 381 | |
---|
[400b8be] | 382 | Label fallLabel = newLabel( "fallThrough", stmt->location ); |
---|
[66daee4] | 383 | if ( ! mutStmt->stmts.empty() ) { |
---|
[400b8be] | 384 | // These should already be in a block. |
---|
| 385 | auto first = mutStmt->stmts.front().get_and_mutate(); |
---|
| 386 | auto block = strict_dynamic_cast<CompoundStmt *>( first ); |
---|
| 387 | |
---|
[b8ab91a] | 388 | // Ensure that the stack isn't corrupted by exceptions in fixBlock. |
---|
| 389 | auto guard = makeFuncGuard( |
---|
[400b8be] | 390 | [&](){ enclosing_control_structures.emplace_back( mutStmt, block, fallLabel ); }, |
---|
[b8ab91a] | 391 | [this](){ enclosing_control_structures.pop_back(); } |
---|
[66daee4] | 392 | ); |
---|
[b8ab91a] | 393 | |
---|
| 394 | block->kids = fixBlock( block->kids, true ); |
---|
| 395 | |
---|
| 396 | // Add fallthrough label if necessary. |
---|
[66daee4] | 397 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 398 | Entry & entry = enclosing_control_structures.back(); |
---|
| 399 | if ( entry.isFallUsed() ) { |
---|
[400b8be] | 400 | mutStmt->stmts.push_back( labelledNullStmt( block->location, entry.useFallExit() ) ); |
---|
[b8ab91a] | 401 | } |
---|
| 402 | } |
---|
[66daee4] | 403 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 404 | Entry & entry = enclosing_control_structures.back(); |
---|
[66daee4] | 405 | assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ), |
---|
[6180274] | 406 | "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s", |
---|
[66daee4] | 407 | toString( entry.stmt ).c_str() ); |
---|
[b8ab91a] | 408 | if ( mutStmt->isDefault() ) { |
---|
| 409 | if ( entry.isFallDefaultUsed() ) { |
---|
| 410 | // Add fallthrough default label if necessary. |
---|
[2f52b18] | 411 | push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) ); |
---|
[b8ab91a] | 412 | } |
---|
| 413 | } |
---|
| 414 | return mutStmt; |
---|
| 415 | } |
---|
| 416 | |
---|
[66daee4] | 417 | void MultiLevelExitCore::previsit( const IfStmt * stmt ) { |
---|
[2f52b18] | 418 | bool labeledBlock = ! stmt->labels.empty(); |
---|
[b8ab91a] | 419 | if ( labeledBlock ) { |
---|
[66daee4] | 420 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 421 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 422 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 423 | } |
---|
| 424 | } |
---|
| 425 | |
---|
[66daee4] | 426 | const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) { |
---|
[2f52b18] | 427 | bool labeledBlock = ! stmt->labels.empty(); |
---|
[b8ab91a] | 428 | if ( labeledBlock ) { |
---|
| 429 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
[2f52b18] | 430 | if ( ! this_label.empty() ) { |
---|
[b8ab91a] | 431 | break_label = this_label; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | return stmt; |
---|
| 435 | } |
---|
| 436 | |
---|
[400b8be] | 437 | static bool isDefaultCase( const ptr<CaseClause> & caseClause ) { |
---|
| 438 | return caseClause->isDefault(); |
---|
[b8ab91a] | 439 | } |
---|
| 440 | |
---|
[66daee4] | 441 | void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) { |
---|
| 442 | Label label = newLabel( "switchBreak", stmt ); |
---|
[400b8be] | 443 | auto it = find_if( stmt->cases.rbegin(), stmt->cases.rend(), isDefaultCase ); |
---|
[b8ab91a] | 444 | |
---|
[400b8be] | 445 | const CaseClause * defaultCase = it != stmt->cases.rend() ? (*it) : nullptr; |
---|
| 446 | Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase->location ) : Label( stmt->location, "" ); |
---|
[b8ab91a] | 447 | enclosing_control_structures.emplace_back( stmt, label, defaultLabel ); |
---|
| 448 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); |
---|
| 449 | |
---|
[2f52b18] | 450 | // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during |
---|
| 451 | // traversal. |
---|
[400b8be] | 452 | for ( const CaseClause * caseStmt : stmt->cases ) { |
---|
[b8ab91a] | 453 | if ( caseStmt->stmts.empty() ) continue; |
---|
[66daee4] | 454 | auto block = caseStmt->stmts.front().strict_as<CompoundStmt>(); |
---|
| 455 | for ( const Stmt * stmt : block->kids ) { |
---|
| 456 | for ( const Label & l : stmt->labels ) { |
---|
[b8ab91a] | 457 | fallthrough_labels.insert( l ); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
[66daee4] | 463 | const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) { |
---|
[2f52b18] | 464 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 465 | Entry & entry = enclosing_control_structures.back(); |
---|
| 466 | assert( entry.stmt == stmt ); |
---|
| 467 | |
---|
[66daee4] | 468 | // Only run to generate the break label. |
---|
[b8ab91a] | 469 | if ( entry.isBreakUsed() ) { |
---|
[2f52b18] | 470 | // To keep the switch statements uniform (all direct children of a SwitchStmt should be CastStmts), append the |
---|
| 471 | // exit label and break to the last case, create a default case if no cases. |
---|
[66daee4] | 472 | SwitchStmt * mutStmt = mutate( stmt ); |
---|
[400b8be] | 473 | if ( mutStmt->cases.empty() ) { |
---|
| 474 | mutStmt->cases.push_back( new CaseClause( mutStmt->location, nullptr, {} ) ); |
---|
[b8ab91a] | 475 | } |
---|
| 476 | |
---|
[400b8be] | 477 | auto caseStmt = mutStmt->cases.back().get(); |
---|
[66daee4] | 478 | auto mutCase = mutate( caseStmt ); |
---|
[400b8be] | 479 | mutStmt->cases.back() = mutCase; |
---|
[b8ab91a] | 480 | |
---|
[66daee4] | 481 | Label label( mutCase->location, "breakLabel" ); |
---|
| 482 | auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label ); |
---|
[b8ab91a] | 483 | branch->labels.push_back( entry.useBreakExit() ); |
---|
| 484 | mutCase->stmts.push_back( branch ); |
---|
| 485 | |
---|
| 486 | return mutStmt; |
---|
| 487 | } |
---|
| 488 | return stmt; |
---|
| 489 | } |
---|
| 490 | |
---|
[66daee4] | 491 | void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) { |
---|
[b8ab91a] | 492 | if ( inFinally ) { |
---|
| 493 | SemanticError( stmt->location, "'return' may not appear in a finally clause" ); |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[66daee4] | 497 | void MultiLevelExitCore::previsit( const TryStmt * stmt ) { |
---|
[2f52b18] | 498 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 499 | if ( isLabeled ) { |
---|
[66daee4] | 500 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 501 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 502 | GuardAction([this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
[66daee4] | 506 | void MultiLevelExitCore::postvisit( const TryStmt * stmt ) { |
---|
[2f52b18] | 507 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 508 | if ( isLabeled ) { |
---|
| 509 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
[2f52b18] | 510 | if ( ! this_label.empty() ) { |
---|
[b8ab91a] | 511 | break_label = this_label; |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
[400b8be] | 516 | void MultiLevelExitCore::previsit( const FinallyClause * ) { |
---|
[2f52b18] | 517 | GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); }); |
---|
[66daee4] | 518 | enclosing_control_structures = vector<Entry>(); |
---|
[b8ab91a] | 519 | GuardValue( inFinally ) = true; |
---|
| 520 | } |
---|
| 521 | |
---|
[66daee4] | 522 | const Stmt * MultiLevelExitCore::mutateLoop( |
---|
| 523 | const Stmt * body, Entry & entry ) { |
---|
[b8ab91a] | 524 | if ( entry.isBreakUsed() ) { |
---|
| 525 | break_label = entry.useBreakExit(); |
---|
| 526 | } |
---|
| 527 | |
---|
[3e5db5b4] | 528 | // if continue is used insert a continue label into the back of the body of the loop |
---|
[b8ab91a] | 529 | if ( entry.isContUsed() ) { |
---|
[66daee4] | 530 | CompoundStmt * new_body = new CompoundStmt( body->location ); |
---|
[3e5db5b4] | 531 | // {} |
---|
[b8ab91a] | 532 | new_body->kids.push_back( body ); |
---|
[3e5db5b4] | 533 | // { |
---|
| 534 | // body |
---|
| 535 | // } |
---|
[817bb3c] | 536 | new_body->kids.push_back( |
---|
| 537 | labelledNullStmt( body->location, entry.useContExit() ) ); |
---|
[3e5db5b4] | 538 | // { |
---|
| 539 | // body |
---|
| 540 | // ContinueLabel: {} |
---|
| 541 | // } |
---|
[b8ab91a] | 542 | return new_body; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | return body; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | template<typename LoopNode> |
---|
| 549 | void MultiLevelExitCore::prehandleLoopStmt( const LoopNode * loopStmt ) { |
---|
| 550 | // Remember is loop before going onto mutate the body. |
---|
| 551 | // The labels will be folded in if they are used. |
---|
[66daee4] | 552 | Label breakLabel = newLabel( "loopBreak", loopStmt ); |
---|
| 553 | Label contLabel = newLabel( "loopContinue", loopStmt ); |
---|
[b8ab91a] | 554 | enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); |
---|
[3e5db5b4] | 555 | // labels are added temporarily to see if they are used and then added permanently in postvisit if ther are used |
---|
| 556 | // children will tag labels as being used during their traversal which occurs before postvisit |
---|
| 557 | |
---|
| 558 | // GuardAction calls the lambda after the node is done being visited |
---|
[b8ab91a] | 559 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | template<typename LoopNode> |
---|
| 563 | const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) { |
---|
[2f52b18] | 564 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 565 | Entry & entry = enclosing_control_structures.back(); |
---|
| 566 | assert( entry.stmt == loopStmt ); |
---|
| 567 | |
---|
[66daee4] | 568 | // Now check if the labels are used and add them if so. |
---|
[2f52b18] | 569 | return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); |
---|
[3e5db5b4] | 570 | // this call to mutate_field compares loopStmt->body and the result of mutateLoop |
---|
| 571 | // if they are the same the node isn't mutated, if they differ then the new mutated node is returned |
---|
| 572 | // the stmts will only differ if a label is used |
---|
[b8ab91a] | 573 | } |
---|
| 574 | |
---|
[66daee4] | 575 | list<ptr<Stmt>> MultiLevelExitCore::fixBlock( |
---|
| 576 | const list<ptr<Stmt>> & kids, bool is_case_clause ) { |
---|
| 577 | // Unfortunately cannot use automatic error collection. |
---|
[b8ab91a] | 578 | SemanticErrorException errors; |
---|
| 579 | |
---|
[66daee4] | 580 | list<ptr<Stmt>> ret; |
---|
[b8ab91a] | 581 | |
---|
| 582 | // Manually visit each child. |
---|
[66daee4] | 583 | for ( const ptr<Stmt> & kid : kids ) { |
---|
[b8ab91a] | 584 | if ( is_case_clause ) { |
---|
| 585 | // Once a label is seen, it's no longer a valid for fallthrough. |
---|
[66daee4] | 586 | for ( const Label & l : kid->labels ) { |
---|
[b8ab91a] | 587 | fallthrough_labels.erase( l ); |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | |
---|
[f75e25b] | 591 | ptr<Stmt> else_stmt = nullptr; |
---|
| 592 | Stmt * loop_kid = nullptr; |
---|
| 593 | // check if loop node and if so add else clause if it exists |
---|
| 594 | const WhileDoStmt * whilePtr = dynamic_cast<const WhileDoStmt *>(kid.get()); |
---|
| 595 | if ( whilePtr && whilePtr->else_) { |
---|
| 596 | else_stmt = whilePtr->else_; |
---|
| 597 | WhileDoStmt * mutate_ptr = mutate(whilePtr); |
---|
| 598 | mutate_ptr->else_ = nullptr; |
---|
| 599 | loop_kid = mutate_ptr; |
---|
| 600 | } |
---|
| 601 | const ForStmt * forPtr = dynamic_cast<const ForStmt *>(kid.get()); |
---|
| 602 | if ( forPtr && forPtr->else_) { |
---|
| 603 | else_stmt = forPtr->else_; |
---|
| 604 | ForStmt * mutate_ptr = mutate(forPtr); |
---|
| 605 | mutate_ptr->else_ = nullptr; |
---|
| 606 | loop_kid = mutate_ptr; |
---|
| 607 | } |
---|
| 608 | |
---|
[b8ab91a] | 609 | try { |
---|
[f75e25b] | 610 | if (else_stmt) ret.push_back( loop_kid->accept( *visitor ) ); |
---|
| 611 | else ret.push_back( kid->accept( *visitor ) ); |
---|
[b8ab91a] | 612 | } catch ( SemanticErrorException & e ) { |
---|
| 613 | errors.append( e ); |
---|
| 614 | } |
---|
| 615 | |
---|
[f75e25b] | 616 | if (else_stmt) ret.push_back(else_stmt); |
---|
[7ad47df] | 617 | |
---|
[2f52b18] | 618 | if ( ! break_label.empty() ) { |
---|
| 619 | ret.push_back( labelledNullStmt( ret.back()->location, break_label ) ); |
---|
[66daee4] | 620 | break_label = Label( CodeLocation(), "" ); |
---|
[b8ab91a] | 621 | } |
---|
| 622 | } |
---|
| 623 | |
---|
[2f52b18] | 624 | if ( ! errors.isEmpty() ) { |
---|
[b8ab91a] | 625 | throw errors; |
---|
| 626 | } |
---|
| 627 | return ret; |
---|
| 628 | } |
---|
| 629 | |
---|
[66daee4] | 630 | const CompoundStmt * multiLevelExitUpdate( |
---|
| 631 | const CompoundStmt * stmt, |
---|
| 632 | const LabelToStmt & labelTable ) { |
---|
[b8ab91a] | 633 | // Must start in the body, so FunctionDecls can be a stopping point. |
---|
[66daee4] | 634 | Pass<MultiLevelExitCore> visitor( labelTable ); |
---|
| 635 | const CompoundStmt * ret = stmt->accept( visitor ); |
---|
[3f681b1] | 636 | // There are some unset code locations slipping in, possibly by Labels. |
---|
| 637 | const Node * node = localFillCodeLocations( ret->location, ret ); |
---|
| 638 | return strict_dynamic_cast<const CompoundStmt *>( node ); |
---|
[b8ab91a] | 639 | } |
---|
| 640 | } // namespace ControlStruct |
---|
| 641 | |
---|
| 642 | // Local Variables: // |
---|
| 643 | // tab-width: 4 // |
---|
| 644 | // mode: c++ // |
---|
| 645 | // compile-command: "make install" // |
---|
| 646 | // End: // |
---|