[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 | |
---|
[0577df2] | 151 | NullStmt * labelledNullStmt( const CodeLocation & cl, const Label & label ) { |
---|
[66daee4] | 152 | return new NullStmt( cl, vector<Label>{ label } ); |
---|
[b8ab91a] | 153 | } |
---|
| 154 | |
---|
[cb921d4] | 155 | MultiLevelExitCore::MultiLevelExitCore( const LabelToStmt & lt ) : |
---|
| 156 | target_table( lt ), break_label( CodeLocation(), "" ), |
---|
[b8ab91a] | 157 | inFinally( false ) |
---|
| 158 | {} |
---|
| 159 | |
---|
[66daee4] | 160 | void MultiLevelExitCore::previsit( const FunctionDecl * ) { |
---|
[b8ab91a] | 161 | visit_children = false; |
---|
| 162 | } |
---|
| 163 | |
---|
[66daee4] | 164 | const CompoundStmt * MultiLevelExitCore::previsit( |
---|
[0577df2] | 165 | const CompoundStmt * stmt ) { |
---|
[b8ab91a] | 166 | visit_children = false; |
---|
[3e5db5b4] | 167 | |
---|
| 168 | // if the stmt is labelled then generate a label to check in postvisit if the label is used |
---|
[2f52b18] | 169 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 170 | if ( isLabeled ) { |
---|
[66daee4] | 171 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 172 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 173 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); |
---|
| 174 | } |
---|
| 175 | |
---|
[66daee4] | 176 | auto mutStmt = mutate( stmt ); |
---|
[b8ab91a] | 177 | // A child statement may set the break label. |
---|
[891f707] | 178 | mutStmt->kids = fixBlock( stmt->kids, false ); |
---|
[b8ab91a] | 179 | |
---|
| 180 | if ( isLabeled ) { |
---|
[2f52b18] | 181 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 182 | Entry & entry = enclosing_control_structures.back(); |
---|
[2f52b18] | 183 | if ( ! entry.useBreakExit().empty() ) { |
---|
[b8ab91a] | 184 | break_label = entry.useBreakExit(); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | return mutStmt; |
---|
| 188 | } |
---|
| 189 | |
---|
[0577df2] | 190 | size_t getUnusedIndex( const Stmt * stmt, const Label & originalTarget ) { |
---|
[b8ab91a] | 191 | const size_t size = stmt->labels.size(); |
---|
| 192 | |
---|
[66daee4] | 193 | // If the label is empty, do not add unused attribute. |
---|
| 194 | if ( originalTarget.empty() ) return size; |
---|
[b8ab91a] | 195 | |
---|
| 196 | // Search for a label that matches the originalTarget. |
---|
| 197 | for ( size_t i = 0 ; i < size ; ++i ) { |
---|
[66daee4] | 198 | const Label & label = stmt->labels[i]; |
---|
[b8ab91a] | 199 | if ( label == originalTarget ) { |
---|
[66daee4] | 200 | for ( const Attribute * attr : label.attributes ) { |
---|
[b8ab91a] | 201 | if ( attr->name == "unused" ) return size; |
---|
| 202 | } |
---|
| 203 | return i; |
---|
| 204 | } |
---|
| 205 | } |
---|
[6180274] | 206 | assertf( false, "CFA internal error: could not find label '%s' on statement %s", |
---|
[66daee4] | 207 | originalTarget.name.c_str(), toString( stmt ).c_str() ); |
---|
[b8ab91a] | 208 | } |
---|
| 209 | |
---|
[0577df2] | 210 | const Stmt * addUnused( const Stmt * stmt, const Label & originalTarget ) { |
---|
[b8ab91a] | 211 | size_t i = getUnusedIndex( stmt, originalTarget ); |
---|
| 212 | if ( i == stmt->labels.size() ) { |
---|
| 213 | return stmt; |
---|
| 214 | } |
---|
[66daee4] | 215 | Stmt * mutStmt = mutate( stmt ); |
---|
| 216 | mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) ); |
---|
[b8ab91a] | 217 | return mutStmt; |
---|
| 218 | } |
---|
| 219 | |
---|
[3e5db5b4] | 220 | // This routine updates targets on enclosing control structures to indicate which |
---|
| 221 | // label is used by the BranchStmt that is passed |
---|
[66daee4] | 222 | const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) { |
---|
| 223 | vector<Entry>::reverse_iterator targetEntry = |
---|
[b8ab91a] | 224 | enclosing_control_structures.rend(); |
---|
[3e5db5b4] | 225 | |
---|
| 226 | // Labels on different stmts require different approaches to access |
---|
[b8ab91a] | 227 | switch ( stmt->kind ) { |
---|
[491bb81] | 228 | case BranchStmt::Goto: |
---|
[b8ab91a] | 229 | return stmt; |
---|
[491bb81] | 230 | case BranchStmt::Continue: |
---|
| 231 | case BranchStmt::Break: { |
---|
| 232 | bool isContinue = stmt->kind == BranchStmt::Continue; |
---|
| 233 | // Handle unlabeled break and continue. |
---|
| 234 | if ( stmt->target.empty() ) { |
---|
| 235 | if ( isContinue ) { |
---|
| 236 | targetEntry = findEnclosingControlStructure( isContinueTarget ); |
---|
| 237 | } else { |
---|
| 238 | if ( enclosing_control_structures.empty() ) { |
---|
[66daee4] | 239 | SemanticError( stmt->location, |
---|
| 240 | "'break' outside a loop, 'switch', or labelled block" ); |
---|
[491bb81] | 241 | } |
---|
| 242 | targetEntry = findEnclosingControlStructure( isBreakTarget ); |
---|
| 243 | } |
---|
| 244 | // Handle labeled break and continue. |
---|
| 245 | } else { |
---|
| 246 | // Lookup label in table to find attached control structure. |
---|
| 247 | targetEntry = findEnclosingControlStructure( |
---|
| 248 | [ targetStmt = target_table.at(stmt->target) ](auto entry){ |
---|
[66daee4] | 249 | return entry.stmt == targetStmt; |
---|
[491bb81] | 250 | } ); |
---|
| 251 | } |
---|
| 252 | // Ensure that selected target is valid. |
---|
| 253 | if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && ! isContinueTarget( *targetEntry ) ) ) { |
---|
| 254 | SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"), |
---|
[66daee4] | 255 | " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), |
---|
| 256 | stmt->originalTarget ) ); |
---|
[491bb81] | 257 | } |
---|
| 258 | break; |
---|
| 259 | } |
---|
| 260 | // handle fallthrough in case/switch stmts |
---|
| 261 | case BranchStmt::FallThrough: { |
---|
| 262 | targetEntry = findEnclosingControlStructure( isFallthroughTarget ); |
---|
| 263 | // Check that target is valid. |
---|
| 264 | if ( targetEntry == enclosing_control_structures.rend() ) { |
---|
| 265 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
| 266 | } |
---|
| 267 | if ( ! stmt->target.empty() ) { |
---|
| 268 | // Labelled fallthrough: target must be a valid fallthough label. |
---|
| 269 | if ( ! fallthrough_labels.count( stmt->target ) ) { |
---|
| 270 | SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", |
---|
[66daee4] | 271 | stmt->originalTarget ) ); |
---|
[491bb81] | 272 | } |
---|
| 273 | return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget ); |
---|
| 274 | } |
---|
| 275 | break; |
---|
| 276 | } |
---|
| 277 | case BranchStmt::FallThroughDefault: { |
---|
| 278 | targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); |
---|
| 279 | |
---|
| 280 | // Check if in switch or choose statement. |
---|
| 281 | if ( targetEntry == enclosing_control_structures.rend() ) { |
---|
| 282 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | // Check if switch or choose has default clause. |
---|
| 286 | auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); |
---|
| 287 | bool foundDefault = false; |
---|
| 288 | for ( auto caseStmt : switchStmt->cases ) { |
---|
| 289 | if ( caseStmt->isDefault() ) { |
---|
| 290 | foundDefault = true; |
---|
| 291 | break; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | if ( ! foundDefault ) { |
---|
| 295 | SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'" |
---|
| 296 | "control structure with a 'default' clause" ); |
---|
| 297 | } |
---|
| 298 | break; |
---|
| 299 | } |
---|
| 300 | default: |
---|
[b8ab91a] | 301 | assert( false ); |
---|
| 302 | } |
---|
| 303 | |
---|
[2f52b18] | 304 | // Branch error checks: get the appropriate label name, which is always replaced. |
---|
[66daee4] | 305 | Label exitLabel( CodeLocation(), "" ); |
---|
[b8ab91a] | 306 | switch ( stmt->kind ) { |
---|
[491bb81] | 307 | case BranchStmt::Break: |
---|
[2f52b18] | 308 | assert( ! targetEntry->useBreakExit().empty() ); |
---|
[b8ab91a] | 309 | exitLabel = targetEntry->useBreakExit(); |
---|
| 310 | break; |
---|
[491bb81] | 311 | case BranchStmt::Continue: |
---|
[2f52b18] | 312 | assert( ! targetEntry->useContExit().empty() ); |
---|
[b8ab91a] | 313 | exitLabel = targetEntry->useContExit(); |
---|
| 314 | break; |
---|
[491bb81] | 315 | case BranchStmt::FallThrough: |
---|
[2f52b18] | 316 | assert( ! targetEntry->useFallExit().empty() ); |
---|
[b8ab91a] | 317 | exitLabel = targetEntry->useFallExit(); |
---|
| 318 | break; |
---|
[491bb81] | 319 | case BranchStmt::FallThroughDefault: |
---|
[2f52b18] | 320 | assert( ! targetEntry->useFallDefaultExit().empty() ); |
---|
[b8ab91a] | 321 | exitLabel = targetEntry->useFallDefaultExit(); |
---|
| 322 | // Check that fallthrough default comes before the default clause. |
---|
[2f52b18] | 323 | if ( ! targetEntry->isFallDefaultValid() ) { |
---|
[66daee4] | 324 | SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); |
---|
[b8ab91a] | 325 | } |
---|
| 326 | break; |
---|
[491bb81] | 327 | default: |
---|
[b8ab91a] | 328 | assert(0); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | // Add unused attribute to silence warnings. |
---|
| 332 | targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); |
---|
| 333 | |
---|
[66daee4] | 334 | // Replace with goto to make later passes more uniform. |
---|
| 335 | return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel ); |
---|
[b8ab91a] | 336 | } |
---|
| 337 | |
---|
[3b0bc16] | 338 | void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) { |
---|
[b8ab91a] | 339 | return prehandleLoopStmt( stmt ); |
---|
| 340 | } |
---|
| 341 | |
---|
[3b0bc16] | 342 | const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) { |
---|
[b8ab91a] | 343 | return posthandleLoopStmt( stmt ); |
---|
| 344 | } |
---|
| 345 | |
---|
[66daee4] | 346 | void MultiLevelExitCore::previsit( const ForStmt * stmt ) { |
---|
[b8ab91a] | 347 | return prehandleLoopStmt( stmt ); |
---|
| 348 | } |
---|
| 349 | |
---|
[66daee4] | 350 | const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) { |
---|
[b8ab91a] | 351 | return posthandleLoopStmt( stmt ); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | // Mimic what the built-in push_front would do anyways. It is O(n). |
---|
[0577df2] | 355 | void push_front( vector<ptr<Stmt>> & vec, const Stmt * element ) { |
---|
[b8ab91a] | 356 | vec.emplace_back( nullptr ); |
---|
| 357 | for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { |
---|
[0bd46fd] | 358 | vec[ i ] = std::move( vec[ i - 1 ] ); |
---|
[b8ab91a] | 359 | } |
---|
| 360 | vec[ 0 ] = element; |
---|
| 361 | } |
---|
| 362 | |
---|
[400b8be] | 363 | const CaseClause * MultiLevelExitCore::previsit( const CaseClause * stmt ) { |
---|
[b8ab91a] | 364 | visit_children = false; |
---|
| 365 | |
---|
[66daee4] | 366 | // If default, mark seen. |
---|
[b8ab91a] | 367 | if ( stmt->isDefault() ) { |
---|
[2f52b18] | 368 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 369 | enclosing_control_structures.back().seenDefault(); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | // The cond may not exist, but if it does update it now. |
---|
[400b8be] | 373 | visitor->maybe_accept( stmt, &CaseClause::cond ); |
---|
[b8ab91a] | 374 | |
---|
| 375 | // Just save the mutated node for simplicity. |
---|
[400b8be] | 376 | CaseClause * mutStmt = mutate( stmt ); |
---|
[b8ab91a] | 377 | |
---|
[400b8be] | 378 | Label fallLabel = newLabel( "fallThrough", stmt->location ); |
---|
[66daee4] | 379 | if ( ! mutStmt->stmts.empty() ) { |
---|
[400b8be] | 380 | // These should already be in a block. |
---|
| 381 | auto first = mutStmt->stmts.front().get_and_mutate(); |
---|
| 382 | auto block = strict_dynamic_cast<CompoundStmt *>( first ); |
---|
| 383 | |
---|
[b8ab91a] | 384 | // Ensure that the stack isn't corrupted by exceptions in fixBlock. |
---|
| 385 | auto guard = makeFuncGuard( |
---|
[400b8be] | 386 | [&](){ enclosing_control_structures.emplace_back( mutStmt, block, fallLabel ); }, |
---|
[b8ab91a] | 387 | [this](){ enclosing_control_structures.pop_back(); } |
---|
[66daee4] | 388 | ); |
---|
[b8ab91a] | 389 | |
---|
| 390 | block->kids = fixBlock( block->kids, true ); |
---|
| 391 | |
---|
| 392 | // Add fallthrough label if necessary. |
---|
[66daee4] | 393 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 394 | Entry & entry = enclosing_control_structures.back(); |
---|
| 395 | if ( entry.isFallUsed() ) { |
---|
[400b8be] | 396 | mutStmt->stmts.push_back( labelledNullStmt( block->location, entry.useFallExit() ) ); |
---|
[b8ab91a] | 397 | } |
---|
| 398 | } |
---|
[66daee4] | 399 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 400 | Entry & entry = enclosing_control_structures.back(); |
---|
[66daee4] | 401 | assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ), |
---|
[6180274] | 402 | "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s", |
---|
[66daee4] | 403 | toString( entry.stmt ).c_str() ); |
---|
[b8ab91a] | 404 | if ( mutStmt->isDefault() ) { |
---|
| 405 | if ( entry.isFallDefaultUsed() ) { |
---|
| 406 | // Add fallthrough default label if necessary. |
---|
[2f52b18] | 407 | push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) ); |
---|
[b8ab91a] | 408 | } |
---|
| 409 | } |
---|
| 410 | return mutStmt; |
---|
| 411 | } |
---|
| 412 | |
---|
[66daee4] | 413 | void MultiLevelExitCore::previsit( const IfStmt * stmt ) { |
---|
[2f52b18] | 414 | bool labeledBlock = ! stmt->labels.empty(); |
---|
[b8ab91a] | 415 | if ( labeledBlock ) { |
---|
[66daee4] | 416 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 417 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 418 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
[66daee4] | 422 | const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) { |
---|
[2f52b18] | 423 | bool labeledBlock = ! stmt->labels.empty(); |
---|
[b8ab91a] | 424 | if ( labeledBlock ) { |
---|
| 425 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
[2f52b18] | 426 | if ( ! this_label.empty() ) { |
---|
[b8ab91a] | 427 | break_label = this_label; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | return stmt; |
---|
| 431 | } |
---|
| 432 | |
---|
[400b8be] | 433 | static bool isDefaultCase( const ptr<CaseClause> & caseClause ) { |
---|
| 434 | return caseClause->isDefault(); |
---|
[b8ab91a] | 435 | } |
---|
| 436 | |
---|
[66daee4] | 437 | void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) { |
---|
| 438 | Label label = newLabel( "switchBreak", stmt ); |
---|
[400b8be] | 439 | auto it = find_if( stmt->cases.rbegin(), stmt->cases.rend(), isDefaultCase ); |
---|
[b8ab91a] | 440 | |
---|
[400b8be] | 441 | const CaseClause * defaultCase = it != stmt->cases.rend() ? (*it) : nullptr; |
---|
| 442 | Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase->location ) : Label( stmt->location, "" ); |
---|
[b8ab91a] | 443 | enclosing_control_structures.emplace_back( stmt, label, defaultLabel ); |
---|
| 444 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); |
---|
| 445 | |
---|
[2f52b18] | 446 | // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during |
---|
| 447 | // traversal. |
---|
[400b8be] | 448 | for ( const CaseClause * caseStmt : stmt->cases ) { |
---|
[b8ab91a] | 449 | if ( caseStmt->stmts.empty() ) continue; |
---|
[66daee4] | 450 | auto block = caseStmt->stmts.front().strict_as<CompoundStmt>(); |
---|
| 451 | for ( const Stmt * stmt : block->kids ) { |
---|
| 452 | for ( const Label & l : stmt->labels ) { |
---|
[b8ab91a] | 453 | fallthrough_labels.insert( l ); |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
[66daee4] | 459 | const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) { |
---|
[2f52b18] | 460 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 461 | Entry & entry = enclosing_control_structures.back(); |
---|
| 462 | assert( entry.stmt == stmt ); |
---|
| 463 | |
---|
[66daee4] | 464 | // Only run to generate the break label. |
---|
[b8ab91a] | 465 | if ( entry.isBreakUsed() ) { |
---|
[2f52b18] | 466 | // To keep the switch statements uniform (all direct children of a SwitchStmt should be CastStmts), append the |
---|
| 467 | // exit label and break to the last case, create a default case if no cases. |
---|
[66daee4] | 468 | SwitchStmt * mutStmt = mutate( stmt ); |
---|
[400b8be] | 469 | if ( mutStmt->cases.empty() ) { |
---|
| 470 | mutStmt->cases.push_back( new CaseClause( mutStmt->location, nullptr, {} ) ); |
---|
[b8ab91a] | 471 | } |
---|
| 472 | |
---|
[400b8be] | 473 | auto caseStmt = mutStmt->cases.back().get(); |
---|
[66daee4] | 474 | auto mutCase = mutate( caseStmt ); |
---|
[400b8be] | 475 | mutStmt->cases.back() = mutCase; |
---|
[b8ab91a] | 476 | |
---|
[66daee4] | 477 | Label label( mutCase->location, "breakLabel" ); |
---|
| 478 | auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label ); |
---|
[b8ab91a] | 479 | branch->labels.push_back( entry.useBreakExit() ); |
---|
| 480 | mutCase->stmts.push_back( branch ); |
---|
| 481 | |
---|
| 482 | return mutStmt; |
---|
| 483 | } |
---|
| 484 | return stmt; |
---|
| 485 | } |
---|
| 486 | |
---|
[66daee4] | 487 | void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) { |
---|
[b8ab91a] | 488 | if ( inFinally ) { |
---|
| 489 | SemanticError( stmt->location, "'return' may not appear in a finally clause" ); |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | |
---|
[66daee4] | 493 | void MultiLevelExitCore::previsit( const TryStmt * stmt ) { |
---|
[2f52b18] | 494 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 495 | if ( isLabeled ) { |
---|
[66daee4] | 496 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
[b8ab91a] | 497 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
| 498 | GuardAction([this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | |
---|
[66daee4] | 502 | void MultiLevelExitCore::postvisit( const TryStmt * stmt ) { |
---|
[2f52b18] | 503 | bool isLabeled = ! stmt->labels.empty(); |
---|
[b8ab91a] | 504 | if ( isLabeled ) { |
---|
| 505 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
[2f52b18] | 506 | if ( ! this_label.empty() ) { |
---|
[b8ab91a] | 507 | break_label = this_label; |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|
[400b8be] | 512 | void MultiLevelExitCore::previsit( const FinallyClause * ) { |
---|
[0bd46fd] | 513 | GuardAction([this, old = std::move( enclosing_control_structures)](){ enclosing_control_structures = std::move(old); }); |
---|
[66daee4] | 514 | enclosing_control_structures = vector<Entry>(); |
---|
[b8ab91a] | 515 | GuardValue( inFinally ) = true; |
---|
| 516 | } |
---|
| 517 | |
---|
[66daee4] | 518 | const Stmt * MultiLevelExitCore::mutateLoop( |
---|
| 519 | const Stmt * body, Entry & entry ) { |
---|
[b8ab91a] | 520 | if ( entry.isBreakUsed() ) { |
---|
| 521 | break_label = entry.useBreakExit(); |
---|
| 522 | } |
---|
| 523 | |
---|
[3e5db5b4] | 524 | // if continue is used insert a continue label into the back of the body of the loop |
---|
[b8ab91a] | 525 | if ( entry.isContUsed() ) { |
---|
[66daee4] | 526 | CompoundStmt * new_body = new CompoundStmt( body->location ); |
---|
[3e5db5b4] | 527 | // {} |
---|
[b8ab91a] | 528 | new_body->kids.push_back( body ); |
---|
[3e5db5b4] | 529 | // { |
---|
| 530 | // body |
---|
| 531 | // } |
---|
[817bb3c] | 532 | new_body->kids.push_back( |
---|
| 533 | labelledNullStmt( body->location, entry.useContExit() ) ); |
---|
[3e5db5b4] | 534 | // { |
---|
| 535 | // body |
---|
| 536 | // ContinueLabel: {} |
---|
| 537 | // } |
---|
[b8ab91a] | 538 | return new_body; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | return body; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | template<typename LoopNode> |
---|
| 545 | void MultiLevelExitCore::prehandleLoopStmt( const LoopNode * loopStmt ) { |
---|
| 546 | // Remember is loop before going onto mutate the body. |
---|
| 547 | // The labels will be folded in if they are used. |
---|
[66daee4] | 548 | Label breakLabel = newLabel( "loopBreak", loopStmt ); |
---|
| 549 | Label contLabel = newLabel( "loopContinue", loopStmt ); |
---|
[b8ab91a] | 550 | enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); |
---|
[3e5db5b4] | 551 | // labels are added temporarily to see if they are used and then added permanently in postvisit if ther are used |
---|
| 552 | // children will tag labels as being used during their traversal which occurs before postvisit |
---|
| 553 | |
---|
| 554 | // GuardAction calls the lambda after the node is done being visited |
---|
[b8ab91a] | 555 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | template<typename LoopNode> |
---|
| 559 | const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) { |
---|
[2f52b18] | 560 | assert( ! enclosing_control_structures.empty() ); |
---|
[b8ab91a] | 561 | Entry & entry = enclosing_control_structures.back(); |
---|
| 562 | assert( entry.stmt == loopStmt ); |
---|
| 563 | |
---|
[66daee4] | 564 | // Now check if the labels are used and add them if so. |
---|
[2f52b18] | 565 | return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); |
---|
[3e5db5b4] | 566 | // this call to mutate_field compares loopStmt->body and the result of mutateLoop |
---|
| 567 | // if they are the same the node isn't mutated, if they differ then the new mutated node is returned |
---|
| 568 | // the stmts will only differ if a label is used |
---|
[b8ab91a] | 569 | } |
---|
| 570 | |
---|
[66daee4] | 571 | list<ptr<Stmt>> MultiLevelExitCore::fixBlock( |
---|
| 572 | const list<ptr<Stmt>> & kids, bool is_case_clause ) { |
---|
| 573 | // Unfortunately cannot use automatic error collection. |
---|
[b8ab91a] | 574 | SemanticErrorException errors; |
---|
| 575 | |
---|
[66daee4] | 576 | list<ptr<Stmt>> ret; |
---|
[b8ab91a] | 577 | |
---|
| 578 | // Manually visit each child. |
---|
[66daee4] | 579 | for ( const ptr<Stmt> & kid : kids ) { |
---|
[b8ab91a] | 580 | if ( is_case_clause ) { |
---|
| 581 | // Once a label is seen, it's no longer a valid for fallthrough. |
---|
[66daee4] | 582 | for ( const Label & l : kid->labels ) { |
---|
[b8ab91a] | 583 | fallthrough_labels.erase( l ); |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | |
---|
[f75e25b] | 587 | ptr<Stmt> else_stmt = nullptr; |
---|
[0577df2] | 588 | const Stmt * loop_kid = nullptr; |
---|
[f75e25b] | 589 | // check if loop node and if so add else clause if it exists |
---|
[0577df2] | 590 | const WhileDoStmt * whilePtr = kid.as<WhileDoStmt>(); |
---|
| 591 | if ( whilePtr && whilePtr->else_ ) { |
---|
[f75e25b] | 592 | else_stmt = whilePtr->else_; |
---|
[0577df2] | 593 | loop_kid = mutate_field( whilePtr, &WhileDoStmt::else_, nullptr ); |
---|
[f75e25b] | 594 | } |
---|
[0577df2] | 595 | const ForStmt * forPtr = kid.as<ForStmt>(); |
---|
| 596 | if ( forPtr && forPtr->else_ ) { |
---|
[f75e25b] | 597 | else_stmt = forPtr->else_; |
---|
[0577df2] | 598 | loop_kid = mutate_field( forPtr, &ForStmt::else_, nullptr ); |
---|
[f75e25b] | 599 | } |
---|
| 600 | |
---|
[b8ab91a] | 601 | try { |
---|
[f75e25b] | 602 | if (else_stmt) ret.push_back( loop_kid->accept( *visitor ) ); |
---|
| 603 | else ret.push_back( kid->accept( *visitor ) ); |
---|
[b8ab91a] | 604 | } catch ( SemanticErrorException & e ) { |
---|
| 605 | errors.append( e ); |
---|
| 606 | } |
---|
| 607 | |
---|
[f75e25b] | 608 | if (else_stmt) ret.push_back(else_stmt); |
---|
[7ad47df] | 609 | |
---|
[2f52b18] | 610 | if ( ! break_label.empty() ) { |
---|
| 611 | ret.push_back( labelledNullStmt( ret.back()->location, break_label ) ); |
---|
[66daee4] | 612 | break_label = Label( CodeLocation(), "" ); |
---|
[b8ab91a] | 613 | } |
---|
| 614 | } |
---|
| 615 | |
---|
[2f52b18] | 616 | if ( ! errors.isEmpty() ) { |
---|
[b8ab91a] | 617 | throw errors; |
---|
| 618 | } |
---|
| 619 | return ret; |
---|
| 620 | } |
---|
| 621 | |
---|
[66daee4] | 622 | const CompoundStmt * multiLevelExitUpdate( |
---|
| 623 | const CompoundStmt * stmt, |
---|
| 624 | const LabelToStmt & labelTable ) { |
---|
[b8ab91a] | 625 | // Must start in the body, so FunctionDecls can be a stopping point. |
---|
[66daee4] | 626 | Pass<MultiLevelExitCore> visitor( labelTable ); |
---|
| 627 | const CompoundStmt * ret = stmt->accept( visitor ); |
---|
[3f681b1] | 628 | // There are some unset code locations slipping in, possibly by Labels. |
---|
| 629 | const Node * node = localFillCodeLocations( ret->location, ret ); |
---|
| 630 | return strict_dynamic_cast<const CompoundStmt *>( node ); |
---|
[b8ab91a] | 631 | } |
---|
| 632 | } // namespace ControlStruct |
---|
| 633 | |
---|
| 634 | // Local Variables: // |
---|
| 635 | // tab-width: 4 // |
---|
| 636 | // mode: c++ // |
---|
| 637 | // compile-command: "make install" // |
---|
| 638 | // End: // |
---|