aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/binder/bytecode/BytecodeBinder.java
diff options
context:
space:
mode:
authorLiam Miller-Cushon <cushon@google.com>2021-07-20 13:11:19 -0700
committerJavac Team <javac-team+copybara@google.com>2021-07-20 13:11:54 -0700
commitdaafa522180923c2becd6fe1a507e04d904db77f (patch)
tree2132e2277d197372d57378c61b9553a93b57633e /java/com/google/turbine/binder/bytecode/BytecodeBinder.java
parentbe693b07c4beaa19d48d311d53dda023f40a71b5 (diff)
downloadturbine-daafa522180923c2becd6fe1a507e04d904db77f.tar.gz
Report diagnostics for type conversions
When modeling primitive widening conversions, report diagnostics for invalid conversions instead of crashing. This rearranges the implementation to use switches on the primitive type kinds and casts instead of double-dispatch. The new approach makes it easier to access the position information needed to report a diagnostic, and also makes it more explicit which constants implement a particular conversion. PiperOrigin-RevId: 385854947
Diffstat (limited to 'java/com/google/turbine/binder/bytecode/BytecodeBinder.java')
-rw-r--r--java/com/google/turbine/binder/bytecode/BytecodeBinder.java13
1 files changed, 9 insertions, 4 deletions
diff --git a/java/com/google/turbine/binder/bytecode/BytecodeBinder.java b/java/com/google/turbine/binder/bytecode/BytecodeBinder.java
index 0f4bac1..d2383b6 100644
--- a/java/com/google/turbine/binder/bytecode/BytecodeBinder.java
+++ b/java/com/google/turbine/binder/bytecode/BytecodeBinder.java
@@ -40,6 +40,7 @@ import com.google.turbine.bytecode.sig.Sig.WildTySig;
import com.google.turbine.bytecode.sig.SigParser;
import com.google.turbine.model.Const;
import com.google.turbine.model.Const.ArrayInitValue;
+import com.google.turbine.model.Const.Value;
import com.google.turbine.type.AnnoInfo;
import com.google.turbine.type.Type;
import java.util.ArrayList;
@@ -175,19 +176,23 @@ public final class BytecodeBinder {
// TODO(b/32626659): this is not bug-compatible with javac
switch (((Type.PrimTy) type).primkind()) {
case CHAR:
- return new Const.CharValue(value.asChar().value());
+ return new Const.CharValue((char) asInt(value));
case SHORT:
- return new Const.ShortValue(value.asShort().value());
+ return new Const.ShortValue((short) asInt(value));
case BOOLEAN:
// boolean constants are encoded as integers
- return new Const.BooleanValue(value.asInteger().value() != 0);
+ return new Const.BooleanValue(asInt(value) != 0);
case BYTE:
- return new Const.ByteValue(value.asByte().value());
+ return new Const.ByteValue((byte) asInt(value));
default:
return value;
}
}
+ private static int asInt(Value value) {
+ return ((Const.IntValue) value).value();
+ }
+
private static Const bindEnumValue(EnumConstValue value) {
return new EnumConstantValue(
new FieldSymbol(asClassSymbol(value.typeName()), value.constName()));