Skip to content
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

Support conversions of primitives in testing context #265

Closed
Closed
Original file line number Diff line number Diff line change
@@ -3152,10 +3152,8 @@ static Block.Builder lowerTypePattern(Block.Builder endNoMatchBlock, Block.Build
TypePatternOp tpOp, Value target) {
TypeElement targetType = tpOp.targetType();

Block.Builder nextBlock = currentBlock.block();

// Check if instance of target type
Result p; // result of the type check
Op p = null; // op that perform type check
Op c; // op that perform conversion
TypeElement s = target.type();
TypeElement t = targetType;
@@ -3165,31 +3163,32 @@ static Block.Builder lowerTypePattern(Block.Builder endNoMatchBlock, Block.Build
ClassType box;
if (cs.unbox().isEmpty()) { // s not a boxed type
box = pt.box().orElseThrow();
p = currentBlock.op(CoreOp.instanceOf(box, target));
p = CoreOp.instanceOf(box, target);
} else {
box = cs;
p = currentBlock.op(constant(BOOLEAN, true));
}
c = invoke(MethodRef.method(box, t + "Value", t), target);
} else {
// primitive to primitive conversion
if (isNarrowingPrimitiveConv(s, t) || isWideningPrimitiveConvThatNeedCheck(s, t)
|| (BYTE.equals(s) && CHAR.equals(t))) {
PrimitiveType ps = ((PrimitiveType) s);
if (isNarrowingPrimitiveConv(ps, pt) || isWideningPrimitiveConvThatNeedCheck(s, t)
MethodRef mref = convMethodRef(s, t);
p = currentBlock.op(invoke(mref, target));
} else {
p = currentBlock.op(constant(BOOLEAN, true));
p = invoke(mref, target);
}
c = CoreOp.conv(targetType, target);
}
} else {
p = currentBlock.op(CoreOp.instanceOf(targetType, target));
p = CoreOp.instanceOf(targetType, target);
c = CoreOp.cast(targetType, target);
}

currentBlock.op(conditionalBranch(p, nextBlock.successor(), endNoMatchBlock.successor()));

currentBlock = nextBlock;
if (p != null) {
Block.Builder nextBlock = currentBlock.block();
currentBlock.op(conditionalBranch(currentBlock.op(p), nextBlock.successor(), endNoMatchBlock.successor()));
currentBlock = nextBlock;
}

target = currentBlock.op(c);

@@ -3204,14 +3203,18 @@ private static boolean isWideningPrimitiveConvThatNeedCheck(TypeElement s, TypeE
|| (LONG.equals(s) && DOUBLE.equals(t));
}

private static boolean isNarrowingPrimitiveConv(TypeElement s, TypeElement t) { // s -> t
if (!(s instanceof PrimitiveType sp) || !(t instanceof PrimitiveType tp)) {
return false;
}
List<PrimitiveType> l = List.of(BYTE, SHORT, CHAR, INT, LONG, FLOAT, DOUBLE);
int si = l.indexOf(s);
int ti = l.indexOf(t);
return ti < si || (SHORT.equals(s) && CHAR.equals(t));
// s -> t is narrowing if order(t) <= order(s)
private final static Map<PrimitiveType, Integer> narrowingOrder = Map.of(
BYTE, 1,
SHORT, 2,
CHAR, 2,
INT, 3,
LONG, 4,
FLOAT, 5,
DOUBLE, 6
);
private static boolean isNarrowingPrimitiveConv(PrimitiveType s, PrimitiveType t) {
return narrowingOrder.get(t) <= narrowingOrder.get(s);
}

private static MethodRef convMethodRef(TypeElement s, TypeElement t) {