X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FType.java;h=cff979291edb8e143f26ddbff57a7a9743f471b2;hp=9e30f196109fcdb677e5b5da216028045751eecb;hb=9271b834f0f1ca988f0374cad6a8cc0cce274ee0;hpb=f66abe529719f8fb7cc27c7ddbdbf7bad70c49c7 diff --git a/src/org/ibex/classgen/Type.java b/src/org/ibex/classgen/Type.java index 9e30f19..cff9792 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,30 @@ 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; + } + + public static Type fromArraySpec(int i) { + switch(i) { + case 4: return Type.BOOLEAN; + case 5: return Type.CHAR; + case 6: return Type.FLOAT; + case 7: return Type.DOUBLE; + case 8: return Type.BYTE; + case 9: return Type.SHORT; + case 10: return Type.INT; + case 11: return Type.LONG; + default: throw new IllegalStateException("invalid array type"); + } + } + // Protected/Private ////////////////////////////////////////////////////////////////////////////// protected final String descriptor; @@ -61,6 +86,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 +114,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 +144,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() { @@ -128,7 +157,6 @@ public abstract class Type implements CGConst { public Type.Class.Body getBody(Context cx) { return cx.resolve(this.getName()); } public abstract class Body extends HasAttributes { public abstract Type.Class.Method.Body[] methods(); - public abstract Type.Class.Field.Body addField(Type.Class.Field field, int flags); public abstract Type.Class.Field.Body[] fields(); public Body(int flags, ClassFile.AttrGen attrs) { super(flags, attrs); @@ -175,6 +203,8 @@ public abstract class Type implements CGConst { public String getName() { return name; } public abstract String getTypeDescriptor(); public abstract String toString(); + public abstract int hashCode(); + public abstract boolean equals(Object o); } public class Field extends Member { @@ -190,6 +220,15 @@ public abstract class Type implements CGConst { if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags"); } } + public int hashCode() { + return type.hashCode() ^ name.hashCode() ^ getDeclaringClass().hashCode(); + } + public boolean equals(Object o_) { + if(o_ == this) return true; + if(!(o_ instanceof Field)) return false; + Field o = (Field) o_; + return o.getDeclaringClass() == getDeclaringClass() && o.type == type && o.name.equals(name); + } } public class Method extends Member { @@ -205,6 +244,7 @@ public abstract class Type implements CGConst { } public boolean isConstructor() { return getName().equals(""); } public boolean isClassInitializer() { return getName().equals(""); } + public String toString() { StringBuffer sb = new StringBuffer(); if (name.equals("")) sb.append("static "); @@ -212,7 +252,7 @@ public abstract class Type implements CGConst { if (name.equals("")) sb.append(Class.this.getShortName()); else - sb.append(returnType.toString()).append(".").append(name); + sb.append(returnType.toString()).append(" ").append(name); sb.append("("); for(int i=0; i