X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fclassgen%2FType.java;h=7463f04a728910b4c80417556b19cae09ea44be7;hb=5f681f726cce9463d57cc5f9554fff2ef4c2c64c;hp=b5428ad54135ad9f8e46b9fe02ab2db47f8c8ce2;hpb=fca6ebc5ba340465405ef884bed208758e8abcdf;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/Type.java b/src/org/ibex/classgen/Type.java index b5428ad..7463f04 100644 --- a/src/org/ibex/classgen/Type.java +++ b/src/org/ibex/classgen/Type.java @@ -18,6 +18,7 @@ public abstract class Type implements CGConst { public static final Type BYTE = new Primitive("B", "byte"); public static final Type CHAR = new Primitive("C", "char"); public static final Type SHORT = new Primitive("S", "short"); + public static final Type NULL = new Null(); public static final Type.Class OBJECT = Type.Class.instance("java.lang.Object"); public static final Type.Class STRING = Type.Class.instance("java.lang.String"); @@ -53,6 +54,16 @@ public abstract class Type implements CGConst { public boolean isClass() { return false; } public boolean isArray() { return false; } + public static Type unify(Type t1, Type t2) { + if(t1 == Type.NULL) return t2; + if(t2 == Type.NULL) return t1; + if((t1 == Type.INT && t2 == Type.BOOLEAN) || (t2 == Type.INT & t1 == Type.BOOLEAN)) return Type.BOOLEAN; + if(t1 == t2) return t1; + // FIXME: This needs to do a lot more (subclasses, etc) + // it probably should be in Context.java + return null; + } + // Protected/Private ////////////////////////////////////////////////////////////////////////////// protected final String descriptor; @@ -61,6 +72,10 @@ public abstract class Type implements CGConst { this.descriptor = descriptor; instances.put(descriptor, this); } + + public static class Null extends Type { + protected Null() { super(""); } // not really correct.... + } public static class Primitive extends Type { private String humanReadable; @@ -85,7 +100,7 @@ public abstract class Type implements CGConst { public Type.Array asArray() { return this; } public boolean isArray() { return true; } public String toString() { return base.toString() + "[]"; } - public Type getElementType() { return Type.fromDescriptor(getDescriptor().substring(0, getDescriptor().length()-1)); } + public Type getElementType() { return base; } } public static class Class extends Type.Ref { @@ -115,7 +130,7 @@ public abstract class Type implements CGConst { return p == -1 ? descriptor.substring(1,descriptor.length()-1) : descriptor.substring(p+1,descriptor.length()-1); } private static String _initHelper(String s) { - if (!s.startsWith("L") || !s.endsWith(";")) throw new Error("invalid"); + if (!s.startsWith("L") || !s.endsWith(";")) throw new Error("invalid: " + s); return s; } String[] components() {