Skip to content

Commit

Permalink
🦄 refactor: Revise type cast to exclude trivial
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Oct 30, 2024
1 parent 66c888e commit b183156
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Optional<TypeDescription> manipulate(JavaFunctionContext functionContext,
optionalFromType.ifPresent(fromType ->
JavaClassCast.upCast(fromType, toType, functionContext::addStackManipulation));
JavaLocalVariable localVariable = new JavaLocalVariable(name, toType);
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(localVariable.getType());
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(toType);
StackManipulation stackManipulation = methodVariableAccess.storeAt(functionContext.getNextOffset());
functionContext.addStackManipulation(stackManipulation);
functionContext.addLocalVariable(localVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ public static TypeDescription getUpCastTypeForMathOp(TypeDescription... types) {
TypeDescription returnType = types[0];
for (int i = 1; i < length; i++) {
TypeDescription type = types[i];
if (PrimitiveWideningDelegate.forPrimitive(returnType).widenTo(type).isValid()) {
StackManipulation stackManipulation = PrimitiveWideningDelegate.forPrimitive(returnType).widenTo(type);
if (stackManipulation.isValid() && stackManipulation != StackManipulation.Trivial.INSTANCE) {
returnType = type;
}
}
return returnType;
}

public static boolean upCast(
TypeDescription fromTypeDescription,
TypeDescription toTypeDescription,
TypeDescription fromType,
TypeDescription toType,
Consumer<StackManipulation> stackManipulationConsumer) {
StackManipulation stackManipulation =
PrimitiveWideningDelegate.forPrimitive(fromTypeDescription).widenTo(toTypeDescription);
StackManipulation stackManipulation = PrimitiveWideningDelegate.forPrimitive(fromType).widenTo(toType);
if (stackManipulation.isValid() && stackManipulation != StackManipulation.Trivial.INSTANCE) {
stackManipulationConsumer.accept(stackManipulation);
}
Expand Down

0 comments on commit b183156

Please sign in to comment.