Skip to content

Commit

Permalink
jep 455 unboxing reference in instanceof (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalat committed Aug 8, 2024
1 parent f761429 commit adb7def
Show file tree
Hide file tree
Showing 3 changed files with 671 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
}

BranchLabel internalFalseLabel = falseLabel != null ? falseLabel : this.pattern != null ? new BranchLabel(codeStream) : null;
generateTypeCheck(currentScope, codeStream);
generateTypeCheck(currentScope, codeStream, internalFalseLabel);

if (this.pattern != null) {
codeStream.ifeq(internalFalseLabel);
Expand Down Expand Up @@ -217,7 +217,7 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
internalFalseLabel.place();
}

private void generateTypeCheck(BlockScope scope, CodeStream codeStream) {
private void generateTypeCheck(BlockScope scope, CodeStream codeStream, BranchLabel internalFalseLabel) {
PrimitiveConversionRoute route = this.testContextRecord != null ?
this.testContextRecord.route() : PrimitiveConversionRoute.NO_CONVERSION_ROUTE;
switch (route) {
Expand All @@ -238,6 +238,16 @@ private void generateTypeCheck(BlockScope scope, CodeStream codeStream) {
codeStream.iconst_1();
setPatternIsTotalType();
break;
// TODO: case WIDENING_REFERENCE_AND_UNBOXING_COVERSION:
// TODO: case WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION:
// TODO: case NARROWING_AND_UNBOXING_CONVERSION:
case UNBOXING_CONVERSION:
// codeStream.load(this.secretExpressionValue);
codeStream.ifnull(internalFalseLabel);
codeStream.iconst_1();
setPatternIsTotalType();
break;
// TODO: case UNBOXING_AND_WIDENING_PRIMITIVE_CONVERSION:
case NO_CONVERSION_ROUTE:
default:
codeStream.instance_of(this.type, this.type.resolvedType);
Expand Down Expand Up @@ -283,6 +293,13 @@ private void generateTestingConversion(BlockScope scope, CodeStream codeStream)
TypeBinding unboxedType = scope.environment().computeBoxingType(TypeBinding.wellKnownBaseType(rightId));
this.expression.computeConversion(scope, this.testContextRecord.left(), unboxedType);
break;
// TODO: case WIDENING_REFERENCE_AND_UNBOXING_COVERSION:
// TODO: case WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION:
// TODO: case NARROWING_AND_UNBOXING_CONVERSION:
case UNBOXING_CONVERSION:
codeStream.generateUnboxingConversion(this.testContextRecord.left().id);
break;
// TODO: case UNBOXING_AND_WIDENING_PRIMITIVE_CONVERSION:
case NO_CONVERSION_ROUTE:
default:
break;
Expand Down Expand Up @@ -393,7 +410,8 @@ private void checkRefForPrimitivesAndAddSecretVariable(BlockScope scope, TypeBin
PrimitiveConversionRoute route = Pattern.findPrimitiveConversionRoute(checkedType, expressionType, scope);
this.testContextRecord = new TestContextRecord(checkedType, expressionType, route);
if (route == PrimitiveConversionRoute.BOXING_CONVERSION
|| route == PrimitiveConversionRoute.BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION) {
// || route == PrimitiveConversionRoute.BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION
) {
addSecretExpressionValue(scope, expressionType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;

Expand All @@ -46,6 +45,12 @@ public enum PrimitiveConversionRoute {
WIDENING_AND_NARROWING_PRIMITIVE_CONVERSION,
BOXING_CONVERSION,
BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION,
// following for reference
WIDENING_REFERENCE_AND_UNBOXING_COVERSION,
WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION,
NARROWING_AND_UNBOXING_CONVERSION,
UNBOXING_CONVERSION,
UNBOXING_AND_WIDENING_PRIMITIVE_CONVERSION,
NO_CONVERSION_ROUTE
}

Expand Down Expand Up @@ -183,17 +188,16 @@ public static PrimitiveConversionRoute findPrimitiveConversionRoute(TypeBinding
return PrimitiveConversionRoute.BOXING_CONVERSION;
if (scope.environment().computeBoxingType(expressionType).isCompatibleWith(destinationType))
return PrimitiveConversionRoute.BOXING_CONVERSION_AND_WIDENING_REFERENCE_CONVERSION;
} else if (destinationIsBaseType && expressionType instanceof ReferenceBinding) {
TypeBinding boxedVersionDest = scope.environment().computeBoxingType(destinationType);
if (boxedVersionDest != null) {
/*
* a widening reference conversion followed by an unboxing conversion
* a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
* a narrowing reference conversion that is checked followed by an unboxing conversion
* an unboxing conversion (5.1.8)
* an unboxing conversion followed by a widening primitive conversion
*/
}

} else if (expressionType.isBoxedPrimitiveType() && destinationIsBaseType) {
TypeBinding unboxedExpressionType = scope.environment().computeBoxingType(expressionType);
//TODO: a widening reference conversion followed by an unboxing conversion
//TODO: a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
//TODO: a narrowing reference conversion that is checked followed by an unboxing conversion
//TODO: an unboxing conversion (5.1.8)
if (TypeBinding.equalsEquals(destinationType, unboxedExpressionType))
return PrimitiveConversionRoute.UNBOXING_CONVERSION;
//TODO: an unboxing conversion followed by a widening primitive conversion
}
}
return PrimitiveConversionRoute.NO_CONVERSION_ROUTE;
Expand Down
Loading

0 comments on commit adb7def

Please sign in to comment.