Bug correction
[IRC.git] / Robust / src / IR / TypeDescriptor.java
index 0114421dc81e3fd55a63c850d8a0a877f73abb1c..bcafc03b44c7bef67bdf512c53b03c7a4a532f44 100644 (file)
@@ -17,10 +17,11 @@ public class TypeDescriptor extends Descriptor {
     public static final int DOUBLE=8;
     public static final int VOID=9;
     public static final int NULL=10;
-    public static final int CLASS=11;
-
+    public static final int TAG=11;
+    public static final int CLASS=12;
 
 
+    int arraycount;
     int type;
     ClassDescriptor class_desc;
 
@@ -29,16 +30,63 @@ public class TypeDescriptor extends Descriptor {
            TypeDescriptor t=(TypeDescriptor)o;
            if (t.type!=type)
                return false;
-           if ((type==CLASS)&&(t.class_desc!=class_desc))
+           if ((type==CLASS)&&(!t.getSymbol().equals(getSymbol())))
+               return false;
+           if (t.arraycount!=arraycount)
                return false;
            return true;
        }
        return false;
     }
 
+    public boolean isString() {
+       if (type!=CLASS)
+           return false;
+       if (arraycount>0)
+           return false;
+       if (!getSymbol().equals(TypeUtil.StringClass))
+           return false;
+       return true;
+    }
+
+    public int hashCode() {
+       int hashcode=type^arraycount;
+       if (type==CLASS)
+           hashcode^=getSymbol().hashCode();
+       return hashcode;
+    }
+
+    public TypeDescriptor makeArray(State state) {
+       TypeDescriptor td=new TypeDescriptor(getSymbol());
+       td.arraycount=arraycount+1;
+       td.type=type;
+       td.class_desc=class_desc;
+       state.addArrayType(td);
+       return td;
+    }
+
+    public boolean isArray() {
+       return (arraycount>0);
+    }
+
+    public int getArrayCount() {
+       return arraycount;
+    }
+
+    public TypeDescriptor dereference() {
+       TypeDescriptor td=new TypeDescriptor(getSymbol());
+       if (arraycount==0)
+           throw new Error();
+       td.arraycount=arraycount-1;
+       td.type=type;
+       td.class_desc=class_desc;
+       return td;
+    }
 
     public String getSafeSymbol() {
-       if (isClass())
+       if (isArray()) 
+           return IR.Flat.BuildCode.arraytype;
+       else if (isClass())
            return class_desc.getSafeSymbol();
        else if (isByte())
            return "char";
@@ -61,11 +109,40 @@ public class TypeDescriptor extends Descriptor {
        else throw new Error("Error Type: "+type);
     }
 
+    public String getRepairSymbol() {
+       if (isArray())
+           return IR.Flat.BuildCode.arraytype;
+       else if (isClass())
+           return class_desc.getSymbol();
+       else if (isByte())
+           return "byte";
+       else if (isChar())
+           return "short";
+       else if (isShort())
+           return "short";
+       else if (isInt())
+           return "int";
+       else if (isBoolean()) //Booleans are ints in C
+           return "int";
+       else if (isLong())
+           return "long long";
+       else if (isVoid())
+           return "void";
+       else if (isDouble())
+           return "double";
+       else if (isFloat())
+           return "float";
+       else throw new Error("Error Type: "+type);
+    }
+
     public String getSafeDescriptor() {
-       if (isClass())
+       //Can't safely use [ in C
+       if (isArray()) 
+           return "_AR_"+this.dereference().getSafeDescriptor();
+       else if (isClass())
            return class_desc.getSafeDescriptor();
        else if (isByte())
-           return "C";
+           return "B";
        else if (isChar())
            return "C";
        else if (isShort())
@@ -80,6 +157,8 @@ public class TypeDescriptor extends Descriptor {
            return "D";
        else if (isFloat())
            return "F";
+       else if (isTag())
+           return "T";
        else throw new Error(); 
     }
 
@@ -119,7 +198,7 @@ public class TypeDescriptor extends Descriptor {
     }
 
     public boolean isPtr() {
-       return (isClass()||isNull());
+       return (isClass()||isNull()||isTag()||isArray());
     }
 
     public boolean isIntegerType() {
@@ -137,11 +216,23 @@ public class TypeDescriptor extends Descriptor {
     public boolean isClass() {
        return type==CLASS;
     }
+    
+    public boolean isTag() {
+       return type==TAG;
+    }
 
     public TypeDescriptor(NameDescriptor name) {
        super(name.toString());
        this.type=CLASS;
        this.class_desc=null;
+       this.arraycount=0;
+    }
+
+    public TypeDescriptor(String st) {
+       super(st);
+       this.type=CLASS;
+       this.class_desc=null;
+       this.arraycount=0;
     }
 
     public ClassDescriptor getClassDesc() {
@@ -152,17 +243,29 @@ public class TypeDescriptor extends Descriptor {
        super(cd.getSymbol());
        this.type=CLASS;
        this.class_desc=cd;
+       this.arraycount=0;
     }
 
     public TypeDescriptor(int t) {
        super(decodeInt(t));
        this.type=t;
+       this.arraycount=0;
     }
 
     public String toString() {
-       if (type==CLASS)
+       if (type==CLASS) {
            return name;
-       else 
+       } else 
+           return decodeInt(type);
+    }
+
+    public String toPrettyString() {
+       if (type==CLASS) {
+           String str=name;
+           for(int i=0;i<arraycount;i++)
+               str+="[]";
+           return str;
+       } else 
            return decodeInt(type);
     }
 
@@ -187,6 +290,8 @@ public class TypeDescriptor extends Descriptor {
            return "void";
        else if (type==NULL)
            return "null";
+       else if (type==TAG)
+           return TypeUtil.TagClass;
        else throw new Error();
     }
 }