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 : Wed Feb 2 23:07:54 2022 |
---|
13 | // Update Count : 33 |
---|
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, WhileDoStmtK, 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 | public: |
---|
52 | Entry( const ForStmt * stmt, Label breakExit, Label contExit ) : |
---|
53 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {} |
---|
54 | Entry( const WhileDoStmt * stmt, Label breakExit, Label contExit ) : |
---|
55 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileDoStmtK ) {} |
---|
56 | Entry( const CompoundStmt *stmt, Label breakExit ) : |
---|
57 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {} |
---|
58 | Entry( const IfStmt *stmt, Label breakExit ) : |
---|
59 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {} |
---|
60 | Entry( const CaseStmt *stmt, Label fallExit ) : |
---|
61 | stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmtK ) {} |
---|
62 | Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) : |
---|
63 | stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {} |
---|
64 | Entry( const TryStmt *stmt, Label breakExit ) : |
---|
65 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {} |
---|
66 | |
---|
67 | bool isContTarget() const { return kind <= WhileDoStmtK; } |
---|
68 | bool isBreakTarget() const { return kind != CaseStmtK; } |
---|
69 | bool isFallTarget() const { return kind == CaseStmtK; } |
---|
70 | bool isFallDefaultTarget() const { return kind == SwitchStmtK; } |
---|
71 | |
---|
72 | // These routines set a target as being "used" by a BranchStmt |
---|
73 | Label useContExit() { assert( kind <= WhileDoStmtK ); 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 | // These routines check if a specific label for a statement is used by a BranchStmt |
---|
79 | bool isContUsed() const { assert( kind <= WhileDoStmtK ); return secondTarget.used; } |
---|
80 | bool isBreakUsed() const { assert( kind != CaseStmtK ); return firstTarget.used; } |
---|
81 | bool isFallUsed() const { assert( kind == CaseStmtK ); return firstTarget.used; } |
---|
82 | bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; } |
---|
83 | void seenDefault() { fallDefaultValid = false; } |
---|
84 | bool isFallDefaultValid() const { return fallDefaultValid; } |
---|
85 | }; |
---|
86 | |
---|
87 | // Helper predicates used in find_if calls (it doesn't take methods): |
---|
88 | bool isBreakTarget( const Entry & entry ) { |
---|
89 | return entry.isBreakTarget(); |
---|
90 | } |
---|
91 | |
---|
92 | bool isContinueTarget( const Entry & entry ) { |
---|
93 | return entry.isContTarget(); |
---|
94 | } |
---|
95 | |
---|
96 | bool isFallthroughTarget( const Entry & entry ) { |
---|
97 | return entry.isFallTarget(); |
---|
98 | } |
---|
99 | |
---|
100 | bool isFallthroughDefaultTarget( const Entry & entry ) { |
---|
101 | return entry.isFallDefaultTarget(); |
---|
102 | } |
---|
103 | |
---|
104 | struct MultiLevelExitCore final : |
---|
105 | public WithVisitorRef<MultiLevelExitCore>, |
---|
106 | public WithShortCircuiting, public WithGuards { |
---|
107 | MultiLevelExitCore( const LabelToStmt & lt ); |
---|
108 | |
---|
109 | void previsit( const FunctionDecl * ); |
---|
110 | |
---|
111 | const CompoundStmt * previsit( const CompoundStmt * ); |
---|
112 | const BranchStmt * postvisit( const BranchStmt * ); |
---|
113 | void previsit( const WhileDoStmt * ); |
---|
114 | const WhileDoStmt * postvisit( const WhileDoStmt * ); |
---|
115 | void previsit( const ForStmt * ); |
---|
116 | const ForStmt * postvisit( const ForStmt * ); |
---|
117 | const CaseStmt * previsit( const CaseStmt * ); |
---|
118 | void previsit( const IfStmt * ); |
---|
119 | const IfStmt * postvisit( const IfStmt * ); |
---|
120 | void previsit( const SwitchStmt * ); |
---|
121 | const SwitchStmt * postvisit( const SwitchStmt * ); |
---|
122 | void previsit( const ReturnStmt * ); |
---|
123 | void previsit( const TryStmt * ); |
---|
124 | void postvisit( const TryStmt * ); |
---|
125 | void previsit( const FinallyStmt * ); |
---|
126 | |
---|
127 | const Stmt * mutateLoop( const Stmt * body, Entry& ); |
---|
128 | |
---|
129 | const LabelToStmt & target_table; |
---|
130 | set<Label> fallthrough_labels; |
---|
131 | vector<Entry> enclosing_control_structures; |
---|
132 | Label break_label; |
---|
133 | bool inFinally; |
---|
134 | |
---|
135 | template<typename LoopNode> |
---|
136 | void prehandleLoopStmt( const LoopNode * loopStmt ); |
---|
137 | template<typename LoopNode> |
---|
138 | const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt ); |
---|
139 | |
---|
140 | list<ptr<Stmt>> fixBlock( |
---|
141 | const list<ptr<Stmt>> & kids, bool caseClause ); |
---|
142 | |
---|
143 | template<typename UnaryPredicate> |
---|
144 | auto findEnclosingControlStructure( UnaryPredicate pred ) { |
---|
145 | return find_if( enclosing_control_structures.rbegin(), |
---|
146 | enclosing_control_structures.rend(), pred ); |
---|
147 | } |
---|
148 | }; |
---|
149 | |
---|
150 | NullStmt * labelledNullStmt( |
---|
151 | const CodeLocation & cl, const Label & label ) { |
---|
152 | return new NullStmt( cl, vector<Label>{ label } ); |
---|
153 | } |
---|
154 | |
---|
155 | MultiLevelExitCore::MultiLevelExitCore( const LabelToStmt & lt ) : |
---|
156 | target_table( lt ), break_label( CodeLocation(), "" ), |
---|
157 | inFinally( false ) |
---|
158 | {} |
---|
159 | |
---|
160 | void MultiLevelExitCore::previsit( const FunctionDecl * ) { |
---|
161 | visit_children = false; |
---|
162 | } |
---|
163 | |
---|
164 | const CompoundStmt * MultiLevelExitCore::previsit( |
---|
165 | const CompoundStmt * stmt ) { |
---|
166 | visit_children = false; |
---|
167 | |
---|
168 | // if the stmt is labelled then generate a label to check in postvisit if the label is used |
---|
169 | bool isLabeled = ! stmt->labels.empty(); |
---|
170 | if ( isLabeled ) { |
---|
171 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
172 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
173 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); |
---|
174 | } |
---|
175 | |
---|
176 | auto mutStmt = mutate( stmt ); |
---|
177 | // A child statement may set the break label. |
---|
178 | mutStmt->kids = move( fixBlock( stmt->kids, false ) ); |
---|
179 | |
---|
180 | if ( isLabeled ) { |
---|
181 | assert( ! enclosing_control_structures.empty() ); |
---|
182 | Entry & entry = enclosing_control_structures.back(); |
---|
183 | if ( ! entry.useBreakExit().empty() ) { |
---|
184 | break_label = entry.useBreakExit(); |
---|
185 | } |
---|
186 | } |
---|
187 | return mutStmt; |
---|
188 | } |
---|
189 | |
---|
190 | size_t getUnusedIndex( |
---|
191 | const Stmt * stmt, const Label & originalTarget ) { |
---|
192 | const size_t size = stmt->labels.size(); |
---|
193 | |
---|
194 | // If the label is empty, do not add unused attribute. |
---|
195 | if ( originalTarget.empty() ) return size; |
---|
196 | |
---|
197 | // Search for a label that matches the originalTarget. |
---|
198 | for ( size_t i = 0 ; i < size ; ++i ) { |
---|
199 | const Label & label = stmt->labels[i]; |
---|
200 | if ( label == originalTarget ) { |
---|
201 | for ( const Attribute * attr : label.attributes ) { |
---|
202 | if ( attr->name == "unused" ) return size; |
---|
203 | } |
---|
204 | return i; |
---|
205 | } |
---|
206 | } |
---|
207 | assertf( false, "CFA internal error: could not find label '%s' on statement %s", |
---|
208 | originalTarget.name.c_str(), toString( stmt ).c_str() ); |
---|
209 | } |
---|
210 | |
---|
211 | const Stmt * addUnused( |
---|
212 | const Stmt * stmt, const Label & originalTarget ) { |
---|
213 | size_t i = getUnusedIndex( stmt, originalTarget ); |
---|
214 | if ( i == stmt->labels.size() ) { |
---|
215 | return stmt; |
---|
216 | } |
---|
217 | Stmt * mutStmt = mutate( stmt ); |
---|
218 | mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) ); |
---|
219 | return mutStmt; |
---|
220 | } |
---|
221 | |
---|
222 | // This routine updates targets on enclosing control structures to indicate which |
---|
223 | // label is used by the BranchStmt that is passed |
---|
224 | const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) { |
---|
225 | vector<Entry>::reverse_iterator targetEntry = |
---|
226 | enclosing_control_structures.rend(); |
---|
227 | |
---|
228 | // Labels on different stmts require different approaches to access |
---|
229 | switch ( stmt->kind ) { |
---|
230 | case BranchStmt::Goto: |
---|
231 | return stmt; |
---|
232 | case BranchStmt::Continue: |
---|
233 | case BranchStmt::Break: { |
---|
234 | bool isContinue = stmt->kind == BranchStmt::Continue; |
---|
235 | // Handle unlabeled break and continue. |
---|
236 | if ( stmt->target.empty() ) { |
---|
237 | if ( isContinue ) { |
---|
238 | targetEntry = findEnclosingControlStructure( isContinueTarget ); |
---|
239 | } else { |
---|
240 | if ( enclosing_control_structures.empty() ) { |
---|
241 | SemanticError( stmt->location, |
---|
242 | "'break' outside a loop, 'switch', or labelled block" ); |
---|
243 | } |
---|
244 | targetEntry = findEnclosingControlStructure( isBreakTarget ); |
---|
245 | } |
---|
246 | // Handle labeled break and continue. |
---|
247 | } else { |
---|
248 | // Lookup label in table to find attached control structure. |
---|
249 | targetEntry = findEnclosingControlStructure( |
---|
250 | [ targetStmt = target_table.at(stmt->target) ](auto entry){ |
---|
251 | return entry.stmt == targetStmt; |
---|
252 | } ); |
---|
253 | } |
---|
254 | // Ensure that selected target is valid. |
---|
255 | if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && ! isContinueTarget( *targetEntry ) ) ) { |
---|
256 | SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"), |
---|
257 | " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), |
---|
258 | stmt->originalTarget ) ); |
---|
259 | } |
---|
260 | break; |
---|
261 | } |
---|
262 | // handle fallthrough in case/switch stmts |
---|
263 | case BranchStmt::FallThrough: { |
---|
264 | targetEntry = findEnclosingControlStructure( isFallthroughTarget ); |
---|
265 | // Check that target is valid. |
---|
266 | if ( targetEntry == enclosing_control_structures.rend() ) { |
---|
267 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
268 | } |
---|
269 | if ( ! stmt->target.empty() ) { |
---|
270 | // Labelled fallthrough: target must be a valid fallthough label. |
---|
271 | if ( ! fallthrough_labels.count( stmt->target ) ) { |
---|
272 | SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", |
---|
273 | stmt->originalTarget ) ); |
---|
274 | } |
---|
275 | return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget ); |
---|
276 | } |
---|
277 | break; |
---|
278 | } |
---|
279 | case BranchStmt::FallThroughDefault: { |
---|
280 | targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); |
---|
281 | |
---|
282 | // Check if in switch or choose statement. |
---|
283 | if ( targetEntry == enclosing_control_structures.rend() ) { |
---|
284 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); |
---|
285 | } |
---|
286 | |
---|
287 | // Check if switch or choose has default clause. |
---|
288 | auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); |
---|
289 | bool foundDefault = false; |
---|
290 | for ( auto subStmt : switchStmt->stmts ) { |
---|
291 | const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>(); |
---|
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: |
---|
304 | assert( false ); |
---|
305 | } |
---|
306 | |
---|
307 | // Branch error checks: get the appropriate label name, which is always replaced. |
---|
308 | Label exitLabel( CodeLocation(), "" ); |
---|
309 | switch ( stmt->kind ) { |
---|
310 | case BranchStmt::Break: |
---|
311 | assert( ! targetEntry->useBreakExit().empty() ); |
---|
312 | exitLabel = targetEntry->useBreakExit(); |
---|
313 | break; |
---|
314 | case BranchStmt::Continue: |
---|
315 | assert( ! targetEntry->useContExit().empty() ); |
---|
316 | exitLabel = targetEntry->useContExit(); |
---|
317 | break; |
---|
318 | case BranchStmt::FallThrough: |
---|
319 | assert( ! targetEntry->useFallExit().empty() ); |
---|
320 | exitLabel = targetEntry->useFallExit(); |
---|
321 | break; |
---|
322 | case BranchStmt::FallThroughDefault: |
---|
323 | assert( ! targetEntry->useFallDefaultExit().empty() ); |
---|
324 | exitLabel = targetEntry->useFallDefaultExit(); |
---|
325 | // Check that fallthrough default comes before the default clause. |
---|
326 | if ( ! targetEntry->isFallDefaultValid() ) { |
---|
327 | SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); |
---|
328 | } |
---|
329 | break; |
---|
330 | default: |
---|
331 | assert(0); |
---|
332 | } |
---|
333 | |
---|
334 | // Add unused attribute to silence warnings. |
---|
335 | targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); |
---|
336 | |
---|
337 | // Replace with goto to make later passes more uniform. |
---|
338 | return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel ); |
---|
339 | } |
---|
340 | |
---|
341 | void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) { |
---|
342 | return prehandleLoopStmt( stmt ); |
---|
343 | } |
---|
344 | |
---|
345 | const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) { |
---|
346 | return posthandleLoopStmt( stmt ); |
---|
347 | } |
---|
348 | |
---|
349 | void MultiLevelExitCore::previsit( const ForStmt * stmt ) { |
---|
350 | return prehandleLoopStmt( stmt ); |
---|
351 | } |
---|
352 | |
---|
353 | const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) { |
---|
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( |
---|
359 | vector<ptr<Stmt>> & vec, const Stmt * element ) { |
---|
360 | vec.emplace_back( nullptr ); |
---|
361 | for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { |
---|
362 | vec[ i ] = move( vec[ i - 1 ] ); |
---|
363 | } |
---|
364 | vec[ 0 ] = element; |
---|
365 | } |
---|
366 | |
---|
367 | const CaseStmt * MultiLevelExitCore::previsit( const CaseStmt * stmt ) { |
---|
368 | visit_children = false; |
---|
369 | |
---|
370 | // If default, mark seen. |
---|
371 | if ( stmt->isDefault() ) { |
---|
372 | assert( ! enclosing_control_structures.empty() ); |
---|
373 | enclosing_control_structures.back().seenDefault(); |
---|
374 | } |
---|
375 | |
---|
376 | // The cond may not exist, but if it does update it now. |
---|
377 | visitor->maybe_accept( stmt, &CaseStmt::cond ); |
---|
378 | |
---|
379 | // Just save the mutated node for simplicity. |
---|
380 | CaseStmt * mutStmt = mutate( stmt ); |
---|
381 | |
---|
382 | Label fallLabel = newLabel( "fallThrough", stmt ); |
---|
383 | if ( ! mutStmt->stmts.empty() ) { |
---|
384 | // Ensure that the stack isn't corrupted by exceptions in fixBlock. |
---|
385 | auto guard = makeFuncGuard( |
---|
386 | [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); }, |
---|
387 | [this](){ enclosing_control_structures.pop_back(); } |
---|
388 | ); |
---|
389 | |
---|
390 | // These should already be in a block. |
---|
391 | auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() ); |
---|
392 | block->kids = fixBlock( block->kids, true ); |
---|
393 | |
---|
394 | // Add fallthrough label if necessary. |
---|
395 | assert( ! enclosing_control_structures.empty() ); |
---|
396 | Entry & entry = enclosing_control_structures.back(); |
---|
397 | if ( entry.isFallUsed() ) { |
---|
398 | mutStmt->stmts.push_back( labelledNullStmt( mutStmt->location, entry.useFallExit() ) ); |
---|
399 | } |
---|
400 | } |
---|
401 | assert( ! enclosing_control_structures.empty() ); |
---|
402 | Entry & entry = enclosing_control_structures.back(); |
---|
403 | assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ), |
---|
404 | "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s", |
---|
405 | toString( entry.stmt ).c_str() ); |
---|
406 | if ( mutStmt->isDefault() ) { |
---|
407 | if ( entry.isFallDefaultUsed() ) { |
---|
408 | // Add fallthrough default label if necessary. |
---|
409 | push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) ); |
---|
410 | } |
---|
411 | } |
---|
412 | return mutStmt; |
---|
413 | } |
---|
414 | |
---|
415 | void MultiLevelExitCore::previsit( const IfStmt * stmt ) { |
---|
416 | bool labeledBlock = ! stmt->labels.empty(); |
---|
417 | if ( labeledBlock ) { |
---|
418 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
419 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
420 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) { |
---|
425 | bool labeledBlock = ! stmt->labels.empty(); |
---|
426 | if ( labeledBlock ) { |
---|
427 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
428 | if ( ! this_label.empty() ) { |
---|
429 | break_label = this_label; |
---|
430 | } |
---|
431 | } |
---|
432 | return stmt; |
---|
433 | } |
---|
434 | |
---|
435 | bool isDefaultCase( const ptr<Stmt> & stmt ) { |
---|
436 | const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>(); |
---|
437 | return caseStmt->isDefault(); |
---|
438 | } |
---|
439 | |
---|
440 | void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) { |
---|
441 | Label label = newLabel( "switchBreak", stmt ); |
---|
442 | auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase ); |
---|
443 | |
---|
444 | const CaseStmt * defaultCase = it != stmt->stmts.rend() ? (it)->strict_as<CaseStmt>() : nullptr; |
---|
445 | Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase ) : 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 this level, then remove as each is seen during |
---|
450 | // 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 SwitchStmt should be CastStmts), append the |
---|
471 | // exit label and break to the last case, create a default case if no cases. |
---|
472 | SwitchStmt * mutStmt = mutate( stmt ); |
---|
473 | if ( mutStmt->stmts.empty() ) { |
---|
474 | mutStmt->stmts.push_back( new CaseStmt( mutStmt->location, nullptr, {} ) ); |
---|
475 | } |
---|
476 | |
---|
477 | auto caseStmt = mutStmt->stmts.back().strict_as<CaseStmt>(); |
---|
478 | auto mutCase = mutate( caseStmt ); |
---|
479 | mutStmt->stmts.back() = mutCase; |
---|
480 | |
---|
481 | Label label( mutCase->location, "breakLabel" ); |
---|
482 | auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label ); |
---|
483 | branch->labels.push_back( entry.useBreakExit() ); |
---|
484 | mutCase->stmts.push_back( branch ); |
---|
485 | |
---|
486 | return mutStmt; |
---|
487 | } |
---|
488 | return stmt; |
---|
489 | } |
---|
490 | |
---|
491 | void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) { |
---|
492 | if ( inFinally ) { |
---|
493 | SemanticError( stmt->location, "'return' may not appear in a finally clause" ); |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | void MultiLevelExitCore::previsit( const TryStmt * stmt ) { |
---|
498 | bool isLabeled = ! stmt->labels.empty(); |
---|
499 | if ( isLabeled ) { |
---|
500 | Label breakLabel = newLabel( "blockBreak", stmt ); |
---|
501 | enclosing_control_structures.emplace_back( stmt, breakLabel ); |
---|
502 | GuardAction([this](){ enclosing_control_structures.pop_back(); } ); |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | void MultiLevelExitCore::postvisit( const TryStmt * stmt ) { |
---|
507 | bool isLabeled = ! stmt->labels.empty(); |
---|
508 | if ( isLabeled ) { |
---|
509 | auto this_label = enclosing_control_structures.back().useBreakExit(); |
---|
510 | if ( ! this_label.empty() ) { |
---|
511 | break_label = this_label; |
---|
512 | } |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | void MultiLevelExitCore::previsit( const FinallyStmt * ) { |
---|
517 | GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); }); |
---|
518 | enclosing_control_structures = vector<Entry>(); |
---|
519 | GuardValue( inFinally ) = true; |
---|
520 | } |
---|
521 | |
---|
522 | const Stmt * MultiLevelExitCore::mutateLoop( |
---|
523 | const Stmt * body, Entry & entry ) { |
---|
524 | if ( entry.isBreakUsed() ) { |
---|
525 | break_label = entry.useBreakExit(); |
---|
526 | } |
---|
527 | |
---|
528 | // if continue is used insert a continue label into the back of the body of the loop |
---|
529 | if ( entry.isContUsed() ) { |
---|
530 | CompoundStmt * new_body = new CompoundStmt( body->location ); |
---|
531 | // {} |
---|
532 | new_body->kids.push_back( body ); |
---|
533 | // { |
---|
534 | // body |
---|
535 | // } |
---|
536 | new_body->kids.push_back( |
---|
537 | labelledNullStmt( body->location, entry.useContExit() ) ); |
---|
538 | // { |
---|
539 | // body |
---|
540 | // ContinueLabel: {} |
---|
541 | // } |
---|
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. |
---|
552 | Label breakLabel = newLabel( "loopBreak", loopStmt ); |
---|
553 | Label contLabel = newLabel( "loopContinue", loopStmt ); |
---|
554 | enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); |
---|
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 |
---|
559 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); |
---|
560 | } |
---|
561 | |
---|
562 | template<typename LoopNode> |
---|
563 | const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) { |
---|
564 | assert( ! enclosing_control_structures.empty() ); |
---|
565 | Entry & entry = enclosing_control_structures.back(); |
---|
566 | assert( entry.stmt == loopStmt ); |
---|
567 | |
---|
568 | // Now check if the labels are used and add them if so. |
---|
569 | return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); |
---|
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 |
---|
573 | } |
---|
574 | |
---|
575 | list<ptr<Stmt>> MultiLevelExitCore::fixBlock( |
---|
576 | const list<ptr<Stmt>> & kids, bool is_case_clause ) { |
---|
577 | // Unfortunately cannot use automatic error collection. |
---|
578 | SemanticErrorException errors; |
---|
579 | |
---|
580 | list<ptr<Stmt>> ret; |
---|
581 | |
---|
582 | // Manually visit each child. |
---|
583 | for ( const ptr<Stmt> & kid : kids ) { |
---|
584 | if ( is_case_clause ) { |
---|
585 | // Once a label is seen, it's no longer a valid for fallthrough. |
---|
586 | for ( const Label & l : kid->labels ) { |
---|
587 | fallthrough_labels.erase( l ); |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | try { |
---|
592 | ret.push_back( kid->accept( *visitor ) ); |
---|
593 | } catch ( SemanticErrorException & e ) { |
---|
594 | errors.append( e ); |
---|
595 | } |
---|
596 | |
---|
597 | if ( ! break_label.empty() ) { |
---|
598 | ret.push_back( labelledNullStmt( ret.back()->location, break_label ) ); |
---|
599 | break_label = Label( CodeLocation(), "" ); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | if ( ! errors.isEmpty() ) { |
---|
604 | throw errors; |
---|
605 | } |
---|
606 | return ret; |
---|
607 | } |
---|
608 | |
---|
609 | const CompoundStmt * multiLevelExitUpdate( |
---|
610 | const CompoundStmt * stmt, |
---|
611 | const LabelToStmt & labelTable ) { |
---|
612 | // Must start in the body, so FunctionDecls can be a stopping point. |
---|
613 | Pass<MultiLevelExitCore> visitor( labelTable ); |
---|
614 | const CompoundStmt * ret = stmt->accept( visitor ); |
---|
615 | return ret; |
---|
616 | } |
---|
617 | } // namespace ControlStruct |
---|
618 | |
---|
619 | // Local Variables: // |
---|
620 | // tab-width: 4 // |
---|
621 | // mode: c++ // |
---|
622 | // compile-command: "make install" // |
---|
623 | // End: // |
---|