Spaces:
Running
Running
inlining | |
exitVar: exitVar label: exitLabel | |
"Replace each return statement in this method with an assignment to the exit variable followed by a goto to the given label. Return true if a goto was generated." | |
"Optimization: If exitVar is nil, the return value of the inlined method is not being used, so don't add the assignment statement." | |
| newStmts labelUsed | | |
labelUsed := false. | |
parseTree nodesDo: [ :node | | |
node isStmtList ifTrue: [ | |
newStmts := OrderedCollection new: 100. | |
node statements do: [ :stmt | | |
(stmt isReturn) ifTrue: [ | |
exitVar = nil ifTrue: [ | |
stmt expression isLeaf ifFalse: [ | |
"evaluate return expression even though value isn't used" | |
newStmts add: stmt expression. | |
]. | |
] ifFalse: [ | |
"assign return expression to exit variable" | |
newStmts add: | |
(TAssignmentNode new | |
setVariable: (TVariableNode new setName: exitVar) | |
expression: stmt expression). | |
]. | |
(stmt == parseTree statements last) ifFalse: [ | |
"generate a goto (this return is NOT the last statement in the method)" | |
newStmts add: (TGoToNode new setLabel: exitLabel). | |
labelUsed := true. | |
]. | |
] ifFalse: [ | |
newStmts addLast: stmt. | |
]. | |
]. | |
node setStatements: newStmts asArray. | |
]. | |
]. | |
^labelUsed |