lots of formatting, refactored out Type.fromArraySpec()
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
index 3d77ba3..cff9792 100644 (file)
@@ -57,11 +57,26 @@ public abstract class Type implements CGConst {
     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 //////////////////////////////////////////////////////////////////////////////
 
@@ -142,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);
@@ -189,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 {
@@ -204,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 {
@@ -227,7 +252,7 @@ public abstract class Type implements CGConst {
                     if (name.equals("<init>"))
                         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<argTypes.length; i++)
                         sb.append((i==0?"":", ")+argTypes[i].toString());
@@ -279,6 +304,21 @@ public abstract class Type implements CGConst {
                     }
                 }
             }
+            public int hashCode() {
+                int h = returnType.hashCode() ^ name.hashCode() ^ getDeclaringClass().hashCode();
+                for(int i=0;i<argTypes.length;i++) h ^= argTypes[i].hashCode();
+                return h;
+            }
+            public boolean equals(Object o_) {
+                if(o_ == this) return true;
+                if(!(o_ instanceof Method)) return false;
+                Method o = (Method) o_;
+                if(!(o.getDeclaringClass() == getDeclaringClass() && o.returnType == returnType && o.name.equals(name))) return false;
+                if(o.argTypes.length != argTypes.length) return false;
+                for(int i=0;i<argTypes.length;i++)
+                    if(o.argTypes[i] != argTypes[i]) return false;
+                return true;
+            }
         }
     }