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