Add support for Enum type for mgc version and also add default constructor. Comment...
authorjzhou <jzhou>
Fri, 7 Jan 2011 01:48:43 +0000 (01:48 +0000)
committerjzhou <jzhou>
Fri, 7 Jan 2011 01:48:43 +0000 (01:48 +0000)
Robust/src/IR/FieldDescriptor.java
Robust/src/IR/Flat/BuildCode.java
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/IR/TypeUtil.java
Robust/src/Lex/Lexer.java
Robust/src/Parse/java14.cup
Robust/src/Tests/DoTests
Robust/src/Tests/EnumTest.java

index 4a6932287957a374db36fecf6bdc9b9899a5e2f6..a60a65d3234559efc82c70c87b68711c76385dab 100644 (file)
@@ -17,6 +17,8 @@ public class FieldDescriptor extends Descriptor {
   protected String identifier;
   protected ExpressionNode en;
   private boolean isglobal;
+  private boolean isenum;
+  private int enumvalue;
 
   public FieldDescriptor(Modifiers m, TypeDescriptor t, String identifier, ExpressionNode e, boolean isglobal) {
     super(identifier);
@@ -26,6 +28,24 @@ public class FieldDescriptor extends Descriptor {
     this.safename = "___" + name + "___";
     this.uniqueid=count++;
     this.isglobal=isglobal;
+    this.isenum = false;
+    this.enumvalue = -1;
+  }
+  
+  public boolean isEnum() {
+    return this.isenum;
+  }
+  
+  public int enumValue() {
+    return this.enumvalue;
+  }
+  
+  public void setAsEnum() {
+    this.isenum = true;
+  }
+  
+  public void setEnumValue(int value) {
+    this.enumvalue = value;
   }
 
   public ExpressionNode getExpressionNode(){
index 9d27c6db5e5eee59aac24aa1c61fcba3c488eeea..2fb5e3bff1c0ac0b358c3bc7506c5bdbbfc99a0b 100644 (file)
@@ -1618,7 +1618,10 @@ public class BuildCode {
 
     for(int i=0; i<fields.size(); i++) {
       FieldDescriptor fd=(FieldDescriptor)fields.get(i);
-      if (fd.getType().isClass()||fd.getType().isArray())
+      if (state.MGC && fd.getType().isClass()
+          && fd.getType().getClassDesc().isEnum()) {
+        classdefout.println("  int " + fd.getSafeSymbol() + ";");
+      } else if (fd.getType().isClass()||fd.getType().isArray())
        classdefout.println("  struct "+fd.getType().getSafeSymbol()+" * "+fd.getSafeSymbol()+";");
       else if ((state.MGC) && (fd.isStatic())) {
         // TODO add version for normal Java later
@@ -2058,7 +2061,9 @@ public class BuildCode {
       TypeDescriptor type=td.getType();
       if (type.isNull())
        output.println("   void * "+td.getSafeSymbol()+";");
-      else if (type.isClass()||type.isArray())
+      else if (state.MGC && type.isClass() && type.getClassDesc().isEnum()) {
+        output.println("   int " + td.getSafeSymbol() + ";");
+      } else if (type.isClass()||type.isArray())
        output.println("   struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
       else
        output.println("   "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
@@ -5280,7 +5285,10 @@ public class BuildCode {
           output.println(generateTemp(fm, ffn.getDst(),lb)+"=*"+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";");
         }
         //output.println(generateTemp(fm, ffn.getDst(),lb)+"=global_defs_p->"+ffn.getSrc().getType().getClassDesc().getSafeSymbol()+"->"+ ffn.getField().getSafeSymbol()+";");
-      } else {
+      } else if (ffn.getField().isEnum()) {
+          // an Enum value, directly replace the field access as int
+          output.println(generateTemp(fm, ffn.getDst(), lb) + "=" + ffn.getField().enumValue() + ";");
+         } else {
         output.println(generateTemp(fm, ffn.getDst(),lb)+"="+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";");
       } 
     } else {
index 96b2537edd7583e9c8cebb67b65ae176af331151..037232e00e796f25ff48437aa91bf3263d4a2034 100644 (file)
@@ -160,8 +160,8 @@ public class BuildIR {
     if(cn != null) {
       ecd.setSurroundingClass(cn.getSymbol());
       ecd.setSurrounding(cn);
+      cn.addEnum(ecd);
     }
-    cn.addEnum(ecd);
     if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
         ecd.getSymbol().equals(TypeUtil.TagClass))) {
       ecd.setSuper(TypeUtil.ObjectClass);
index a629acadc836801fe31bc92fbfb7b3e90bb91645..401b00520d213702fe755b8e04e2c471de9209bd 100644 (file)
@@ -58,9 +58,20 @@ public class SemanticCheck {
        checkField(cd,fd);
       }
       
+      boolean hasConstructor = false;
       for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
        MethodDescriptor md=(MethodDescriptor)method_it.next();
        checkMethod(cd,md);
+    hasConstructor |= md.isConstructor();
+      }
+      if(!hasConstructor) {
+        // add a default constructor for this class
+        MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
+            cd.getSymbol(), false);
+        BlockNode bn=new BlockNode();
+        state.addTreeCode(md,bn);
+        cd.addMethod(md);
+        checkMethod(cd,md);
       }
     }
   }
@@ -563,13 +574,22 @@ public class SemanticCheck {
     FieldDescriptor fd=null;
     if (ltd.isArray()&&fieldname.equals("length"))
       fd=FieldDescriptor.arrayLength;
-    else 
+    else
       fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
     if(state.MGC) {
       // TODO add version for normal Java later
     if(ltd.isStatic()) {
-      // check if this field is a static field
-      if(!fd.isStatic()) {
+      if(ltd.getClassDesc().isEnum()) {
+        int value = ltd.getClassDesc().getEnumConstant(fieldname);
+        if(-1 == value) {
+          // check if this field is an enum constant
+          throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
+        }
+        fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
+        fd.setAsEnum();
+        fd.setEnumValue(value);
+      } else if(!fd.isStatic()) {
+        // check if this field is a static field
         throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
       }
     } 
@@ -958,6 +978,9 @@ NextMethod:
       ExpressionNode en=min.getArg(i);
       checkExpressionNode(md,nametable,en,null);
       tdarray[i]=en.getType();
+      if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
+        tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
+      }
     }
     TypeDescriptor typetolookin=null;
     if (min.getExpression()!=null) {
index 72d8405105a3aab469b97530f4d4d5082c9bee7c..1eda9aa480a0967bafea88f0e3d86a2a529cb7b9 100644 (file)
@@ -296,6 +296,11 @@ NextMethod:
   }
 
   public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
+    if(state.MGC) {
+      if(possiblesuper.isClass() && possiblesuper.class_desc.isEnum() && cd2.isInt()) {
+        return true;
+      }
+    }
     if (possiblesuper.isOffset() || cd2.isOffset()) return true;
     //Matching type are always okay
     if (possiblesuper.equals(cd2))
index 14a25697c39a02e0e0f958b0c1e789160c07638f..6cb427f91b4015cbbdd7ada08dba20c5073b916c 100644 (file)
@@ -289,7 +289,7 @@ public class Lexer {
     if (s.equals("false")) return new BooleanLiteral(false);
     // Check against keywords.
     //  pre-java 1.5 compatibility:
-    if (!isJava15 && s.equals("enum")) return new Identifier(s);
+    //if (!isJava15 && s.equals("enum")) return new Identifier(s);
     //  pre-java 1.4 compatibility:
     if (!isJava14 && s.equals("assert")) return new Identifier(s);
     //  pre-java 1.2 compatibility:
index 6f129037654dbe40a3d4c2c3f79546001a93f8d0..2fd93f3f51d686020e65e2a1d416bb5eb048aedb 100644 (file)
@@ -869,7 +869,7 @@ enum_body ::=
                {: RESULT=eco; :}
        ;
 enum_constants_opt ::=
-   {: RESULT=new ParseNode("empty"); :}
+  {: RESULT=new ParseNode("empty"); :}
        |       enum_constants:ecs
        {: RESULT=ecs; :}
        ;
index 3ebdab0c8f65171969ef3e1a112cfeb3a2fdc8ac..b80b4c7238c562852720f3d81eeeb0dec094b55a 100755 (executable)
@@ -16,4 +16,4 @@ dotest WriteFile WriteFile.java
 dotest ReadFile ReadFile.java
 dotest FileLength FileLength.java
 dotest IntegerTest IntegerTest.java
-dotest InitializerTest InitializerTest.java
\ No newline at end of file
+#dotest InitializerTest InitializerTest.java
index c3777271a299ab1960d2f185480ee08af60c4a8f..aa9f50e80aa74f0148ab6b137a6a6b1f172ffd95 100644 (file)
@@ -4,11 +4,21 @@ public enum Spiciness {
 
 public class EnumTest {
   
-  public EnumTest(){}
+  public Spiciness1 howHot;
+  
+  public enum Spiciness1 {
+    NOT, FLAMING, MILD, MEDIUM, HOT
+  } ///:~
+  
+  //public EnumTest(){}
   
   public static void main(String[] args) {
     Spiciness howHot = Spiciness.MEDIUM;
     System.out.println(howHot);
+    
+    EnumTest et = new EnumTest();
+    et.howHot = Spiciness1.MEDIUM;
+    System.out.println(et.howHot);
   }
 } /* Output:
 MEDIUM