Skip to content

Commit

Permalink
Painless: Clean up PainlessCast (#32754)
Browse files Browse the repository at this point in the history
Renames variables and methods in PainlessCast to go along with the new naming 
scheme. Mechanical change.
  • Loading branch information
jdconrad authored Aug 10, 2018
1 parent 1dcf807 commit 7a1bbbc
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 264 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -135,52 +135,52 @@ public void writeLoopCounter(int slot, int count, Location location) {

public void writeCast(PainlessCast cast) {
if (cast != null) {
if (cast.from == char.class && cast.to == String.class) {
if (cast.originalType == char.class && cast.targetType == String.class) {
invokeStatic(UTILITY_TYPE, CHAR_TO_STRING);
} else if (cast.from == String.class && cast.to == char.class) {
} else if (cast.originalType == String.class && cast.targetType == char.class) {
invokeStatic(UTILITY_TYPE, STRING_TO_CHAR);
} else if (cast.unboxFrom != null) {
unbox(getType(cast.unboxFrom));
writeCast(cast.from, cast.to);
} else if (cast.unboxTo != null) {
if (cast.from == def.class) {
if (cast.explicit) {
if (cast.to == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN);
else if (cast.to == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BYTE_EXPLICIT);
else if (cast.to == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_SHORT_EXPLICIT);
else if (cast.to == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_CHAR_EXPLICIT);
else if (cast.to == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_INT_EXPLICIT);
else if (cast.to == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_LONG_EXPLICIT);
else if (cast.to == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_FLOAT_EXPLICIT);
else if (cast.to == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_DOUBLE_EXPLICIT);
} else if (cast.unboxOriginalType != null) {
unbox(getType(cast.unboxOriginalType));
writeCast(cast.originalType, cast.targetType);
} else if (cast.unboxTargetType != null) {
if (cast.originalType == def.class) {
if (cast.explicitCast) {
if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN);
else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BYTE_EXPLICIT);
else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_SHORT_EXPLICIT);
else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_CHAR_EXPLICIT);
else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_INT_EXPLICIT);
else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_LONG_EXPLICIT);
else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_FLOAT_EXPLICIT);
else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_DOUBLE_EXPLICIT);
else {
throw new IllegalStateException("Illegal tree structure.");
}
} else {
if (cast.to == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN);
else if (cast.to == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BYTE_IMPLICIT);
else if (cast.to == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_SHORT_IMPLICIT);
else if (cast.to == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_CHAR_IMPLICIT);
else if (cast.to == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_INT_IMPLICIT);
else if (cast.to == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_LONG_IMPLICIT);
else if (cast.to == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_FLOAT_IMPLICIT);
else if (cast.to == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_DOUBLE_IMPLICIT);
if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN);
else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BYTE_IMPLICIT);
else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_SHORT_IMPLICIT);
else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_CHAR_IMPLICIT);
else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_INT_IMPLICIT);
else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_LONG_IMPLICIT);
else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_FLOAT_IMPLICIT);
else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_DOUBLE_IMPLICIT);
else {
throw new IllegalStateException("Illegal tree structure.");
}
}
} else {
writeCast(cast.from, cast.to);
unbox(getType(cast.unboxTo));
writeCast(cast.originalType, cast.targetType);
unbox(getType(cast.unboxTargetType));
}
} else if (cast.boxFrom != null) {
box(getType(cast.boxFrom));
writeCast(cast.from, cast.to);
} else if (cast.boxTo != null) {
writeCast(cast.from, cast.to);
box(getType(cast.boxTo));
} else if (cast.boxOriginalType != null) {
box(getType(cast.boxOriginalType));
writeCast(cast.originalType, cast.targetType);
} else if (cast.boxTargetType != null) {
writeCast(cast.originalType, cast.targetType);
box(getType(cast.boxTargetType));
} else {
writeCast(cast.from, cast.to);
writeCast(cast.originalType, cast.targetType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,55 @@
public class PainlessCast {

/** Create a standard cast with no boxing/unboxing. */
public static PainlessCast standard(Class<?> from, Class<?> to, boolean explicit) {
return new PainlessCast(from, to, explicit, null, null, null, null);
public static PainlessCast originalTypetoTargetType(Class<?> originalType, Class<?> targetType, boolean explicitCast) {
return new PainlessCast(originalType, targetType, explicitCast, null, null, null, null);
}

/** Create a cast where the from type will be unboxed, and then the cast will be performed. */
public static PainlessCast unboxFrom(Class<?> from, Class<?> to, boolean explicit, Class<?> unboxFrom) {
return new PainlessCast(from, to, explicit, unboxFrom, null, null, null);
/** Create a cast where the original type will be unboxed, and then the cast will be performed. */
public static PainlessCast unboxOriginalType(
Class<?> originalType, Class<?> targetType, boolean explicitCast, Class<?> unboxOriginalType) {

return new PainlessCast(originalType, targetType, explicitCast, unboxOriginalType, null, null, null);
}

/** Create a cast where the to type will be unboxed, and then the cast will be performed. */
public static PainlessCast unboxTo(Class<?> from, Class<?> to, boolean explicit, Class<?> unboxTo) {
return new PainlessCast(from, to, explicit, null, unboxTo, null, null);
/** Create a cast where the target type will be unboxed, and then the cast will be performed. */
public static PainlessCast unboxTargetType(
Class<?> originalType, Class<?> targetType, boolean explicitCast, Class<?> unboxTargetType) {

return new PainlessCast(originalType, targetType, explicitCast, null, unboxTargetType, null, null);
}

/** Create a cast where the from type will be boxed, and then the cast will be performed. */
public static PainlessCast boxFrom(Class<?> from, Class<?> to, boolean explicit, Class<?> boxFrom) {
return new PainlessCast(from, to, explicit, null, null, boxFrom, null);
/** Create a cast where the original type will be boxed, and then the cast will be performed. */
public static PainlessCast boxOriginalType(
Class<?> originalType, Class<?> targetType, boolean explicitCast, Class<?> boxOriginalType) {

return new PainlessCast(originalType, targetType, explicitCast, null, null, boxOriginalType, null);
}

/** Create a cast where the to type will be boxed, and then the cast will be performed. */
public static PainlessCast boxTo(Class<?> from, Class<?> to, boolean explicit, Class<?> boxTo) {
return new PainlessCast(from, to, explicit, null, null, null, boxTo);
/** Create a cast where the target type will be boxed, and then the cast will be performed. */
public static PainlessCast boxTargetType(
Class<?> originalType, Class<?> targetType, boolean explicitCast, Class<?> boxTargetType) {

return new PainlessCast(originalType, targetType, explicitCast, null, null, null, boxTargetType);
}

public final Class<?> from;
public final Class<?> to;
public final boolean explicit;
public final Class<?> unboxFrom;
public final Class<?> unboxTo;
public final Class<?> boxFrom;
public final Class<?> boxTo;
public final Class<?> originalType;
public final Class<?> targetType;
public final boolean explicitCast;
public final Class<?> unboxOriginalType;
public final Class<?> unboxTargetType;
public final Class<?> boxOriginalType;
public final Class<?> boxTargetType;

private PainlessCast(Class<?> originalType, Class<?> targetType, boolean explicitCast,
Class<?> unboxOriginalType, Class<?> unboxTargetType, Class<?> boxOriginalType, Class<?> boxTargetType) {

private PainlessCast(Class<?> from, Class<?> to, boolean explicit,
Class<?> unboxFrom, Class<?> unboxTo, Class<?> boxFrom, Class<?> boxTo) {
this.from = from;
this.to = to;
this.explicit = explicit;
this.unboxFrom = unboxFrom;
this.unboxTo = unboxTo;
this.boxFrom = boxFrom;
this.boxTo = boxTo;
this.originalType = originalType;
this.targetType = targetType;
this.explicitCast = explicitCast;
this.unboxOriginalType = unboxOriginalType;
this.unboxTargetType = unboxTargetType;
this.boxOriginalType = boxOriginalType;
this.boxTargetType = boxTargetType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ void write(MethodWriter writer, Globals globals) {

@Override
public String toString() {
return singleLineToString(PainlessLookupUtility.typeToCanonicalTypeName(cast.to), child);
return singleLineToString(PainlessLookupUtility.typeToCanonicalTypeName(cast.targetType), child);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ private static void assertCast(Class<?> actual, Class<?> expected, boolean mustB
}

PainlessCast cast = AnalyzerCaster.getLegalCast(location, actual, expected, true, false);
assertEquals(actual, cast.from);
assertEquals(expected, cast.to);
assertEquals(actual, cast.originalType);
assertEquals(expected, cast.targetType);

if (mustBeExplicit) {
ClassCastException error = expectThrows(ClassCastException.class,
() -> AnalyzerCaster.getLegalCast(location, actual, expected, false, false));
assertTrue(error.getMessage().startsWith("Cannot cast"));
} else {
cast = AnalyzerCaster.getLegalCast(location, actual, expected, false, false);
assertEquals(actual, cast.from);
assertEquals(expected, cast.to);
assertEquals(actual, cast.originalType);
assertEquals(expected, cast.targetType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ public void testECapturingFunctionRef() {
public void testECast() {
Location l = new Location(getTestName(), 0);
AExpression child = new EConstant(l, "test");
PainlessCast cast = PainlessCast.standard(String.class, Integer.class, true);
PainlessCast cast = PainlessCast.originalTypetoTargetType(String.class, Integer.class, true);
assertEquals("(ECast java.lang.Integer (EConstant String 'test'))", new ECast(l, child, cast).toString());

l = new Location(getTestName(), 1);
child = new EBinary(l, Operation.ADD, new EConstant(l, "test"), new EConstant(l, 12));
cast = PainlessCast.standard(Integer.class, Boolean.class, true);
cast = PainlessCast.originalTypetoTargetType(Integer.class, Boolean.class, true);
assertEquals("(ECast java.lang.Boolean (EBinary (EConstant String 'test') + (EConstant Integer 12)))",
new ECast(l, child, cast).toString());
}
Expand Down

0 comments on commit 7a1bbbc

Please sign in to comment.