Skip to content

Commit 0d1ef69

Browse files
committed
Optimize __cast__()
- Mark `__cast__()` for constant and cached returns. - Remove nested casts where the second executed cast is removed if the first executed cast passing ensures that the second executed cast will pass.
1 parent 0f39b03 commit 0d1ef69

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/main/java/com/laytonsmith/core/functions/Compiler.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.laytonsmith.core.Optimizable;
1313
import com.laytonsmith.core.ParseTree;
1414
import com.laytonsmith.core.Script;
15+
import com.laytonsmith.core.Optimizable.OptimizationOption;
1516
import com.laytonsmith.core.compiler.CompilerEnvironment;
1617
import com.laytonsmith.core.compiler.CompilerWarning;
1718
import com.laytonsmith.core.compiler.FileOptions;
@@ -35,6 +36,7 @@
3536
import com.laytonsmith.core.constructs.Target;
3637
import com.laytonsmith.core.constructs.Token;
3738
import com.laytonsmith.core.environments.Environment;
39+
import com.laytonsmith.core.environments.Environment.EnvironmentImpl;
3840
import com.laytonsmith.core.exceptions.CRE.CRECastException;
3941
import com.laytonsmith.core.exceptions.CRE.CRENotFoundException;
4042
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
@@ -1206,7 +1208,7 @@ public ParseTree postParseRewrite(ParseTree ast, Environment env,
12061208
@api
12071209
@noprofile
12081210
@hide("This is only used internally by the compiler.")
1209-
public static class __cast__ extends DummyFunction {
1211+
public static class __cast__ extends DummyFunction implements Optimizable {
12101212

12111213
public static final String NAME = "__cast__";
12121214

@@ -1292,5 +1294,35 @@ public CClassType typecheck(StaticAnalysis analysis,
12921294
// Return type that is being cast to.
12931295
return castToType;
12941296
}
1297+
1298+
@Override
1299+
public Set<OptimizationOption> optimizationOptions() {
1300+
return EnumSet.of(
1301+
OptimizationOption.OPTIMIZE_DYNAMIC,
1302+
OptimizationOption.CONSTANT_OFFLINE,
1303+
OptimizationOption.CACHE_RETURN
1304+
);
1305+
}
1306+
1307+
@Override
1308+
public ParseTree optimizeDynamic(Target t, Environment env, Set<Class<? extends EnvironmentImpl>> envs,
1309+
List<ParseTree> children, FileOptions fileOptions)
1310+
throws ConfigCompileException, ConfigRuntimeException, ConfigCompileGroupException {
1311+
1312+
// Optimize __cast__(__cast__(val, type1), type2) to __cast__(val, type1) if the cast to type2 will always
1313+
// pass given that the cast to type1 has passed.
1314+
ParseTree valNode = children.get(0);
1315+
if(valNode.getData() instanceof CFunction cf && cf.getFunction() != null
1316+
&& cf.getFunction().getName().equals(__cast__.NAME) && valNode.numberOfChildren() == 2) {
1317+
ParseTree typeNode = children.get(1);
1318+
ParseTree childTypeNode = valNode.getChildAt(1);
1319+
if(typeNode.getData() instanceof CClassType type
1320+
&& childTypeNode.getData() instanceof CClassType childType
1321+
&& InstanceofUtil.isInstanceof(childType, type, env)) {
1322+
return valNode;
1323+
}
1324+
}
1325+
return null;
1326+
}
12951327
}
12961328
}

0 commit comments

Comments
 (0)