-
Notifications
You must be signed in to change notification settings - Fork 28
Get rid of empty init functions #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package de.peeeq.wurstscript.translation.imoptimizer; | ||
|
||
import de.peeeq.wurstscript.jassIm.*; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class InitFunctionCleaner { | ||
|
||
public static void clean(ImProg prog) { | ||
List<ImFunction> initFuncs = prog.getFunctions().stream().filter(func -> func.getName().startsWith | ||
("init_") && func.getBody().size() == 1).collect(Collectors.toList()); | ||
|
||
initFuncs.forEach(func -> { | ||
ImVar globalBridge = null; | ||
for (ImVar global : prog.getGlobals()) { | ||
if (global.getName().equals("ref_function_" + func.getName())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like using the name here. Names should not matter in the intermediate language. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What to use then? |
||
globalBridge = global; | ||
break; | ||
} | ||
} | ||
if (globalBridge != null) { | ||
Collection<ImVarRead> imVarReads = globalBridge.attrReads(); | ||
if (imVarReads.size() == 1) { | ||
ImVarRead initRead = imVarReads.iterator().next(); | ||
ImVar var = initRead.getVar(); | ||
|
||
Optional<ImFunction> mainFunc = prog.getFunctions().stream().filter(f -> f.getName().equals("main")).findFirst(); | ||
if (mainFunc.isPresent()) { | ||
ImStmts body = mainFunc.get().getBody(); | ||
for (int i = 0; i < body.size(); i++) { | ||
ImStmt imStmt = body.get(i); | ||
if (imStmt instanceof ImFunctionCall) { | ||
ImFunction calledFunc = ((ImFunctionCall) imStmt).getFunc(); | ||
if (calledFunc.getName().equals("TriggerAddCondition")) { | ||
int finalI = i; | ||
((ImFunctionCall) imStmt).getArguments().forEach(arg -> { | ||
if ((arg instanceof ImFunctionCall) && ((ImFunctionCall) arg).getFunc().getName().equals("Condition")) { | ||
ImExpr next = ((ImFunctionCall) arg).getArguments().iterator().next(); | ||
if ((next instanceof ImVarAccess) && ((ImVarAccess) next).getVar().structuralEquals(var)) { | ||
imStmt.replaceBy(JassIm.ImNull()); | ||
if (body.get(finalI - 1) instanceof ImFunctionCall) { | ||
body.get(finalI - 1).replaceBy(JassIm.ImNull()); | ||
} | ||
body.get(finalI + 1).replaceBy(JassIm.ImNull()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the -1 and +1 doing here? Looks like you are assuming that there are always certain other statements around a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The init calls are always generated the same way. Unless someone names a function "ref_function_" or so (which is the name issue above) then it seems reliable to me. |
||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use normal for-loops -- better performance, better stack traces, better debugging, easier to read