Demonstration of a Visitor
and an Editor
.
It is sometimes desireable to rewrite a body of the form
if (condition) { return a; } else { return b; }
into a series of statements without intermediate returns, like this:
value ret; if (condition) { ret = a; } else { ret = b; }
However, this is not possible for every block:
if (condition) { return a; } return b;
If we replace return
s with ret
assignments here,
the second return would still be reached.
hasEarlyReturns()
detects if a body contains such an early return
by visiting the body;
RewriteReturnsEditor
rewrites a body as shown above.
This could be used for some compiler optimizations.
Classes | |
RewriteReturnsEditor | shared RewriteReturnsEditor Rewrites all returns in a block to specifications of the named value. For example, if (condition) { return a; } else { return b; } is rewritten to if (condition) { returnValue = a; } else { returnValue = b; } You should only use this if the block has no early returns, otherwise this changes the semantics of the block. Usage: rewrittenBlock= someBlock.transform(RewriteReturnsEditor(returnValueName)); |