Enable field array initialization for MGC
authorjzhou <jzhou>
Thu, 27 Jan 2011 19:05:30 +0000 (19:05 +0000)
committerjzhou <jzhou>
Thu, 27 Jan 2011 19:05:30 +0000 (19:05 +0000)
Robust/src/IR/Flat/BuildFlat.java
Robust/src/IR/Tree/ArrayInitializerNode.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/Tests/ArrayInitializerTest.java

index f5347897aea1d504607285d37952c4d2f9aecee8..d8e4442efd4eec0694f04a17e2cb0f689124e56e 100644 (file)
@@ -1390,7 +1390,7 @@ public class BuildFlat {
     boolean isGlobal = false;
     String disjointId = null;
     // get the type the array to be initialized
-    TypeDescriptor td = out_temp.getType();
+    TypeDescriptor td = ain.getType();
 
     // create a new array of size equal to the array initializer
     FlatNode first=null;
index 48dab8a769524bbcd1ca71fe236fe7c28d4915fd..e6bc7b335e99b42419a2a9df63965cf2783c2800 100644 (file)
@@ -1,10 +1,14 @@
 package IR.Tree;
 import java.util.Vector;
 
+import IR.TypeDescriptor;
+
 public class ArrayInitializerNode extends ExpressionNode {
+  TypeDescriptor type;
   Vector varInitList;
 
   public ArrayInitializerNode(Vector vil) {
+    this.type = null;
     varInitList=vil;
   }
 
@@ -15,6 +19,14 @@ public class ArrayInitializerNode extends ExpressionNode {
   public ExpressionNode getVarInitializer(int i) {
     return (ExpressionNode) varInitList.get(i);
   }
+  
+  public void setType(TypeDescriptor type) {
+    this.type = type;
+  }
+  
+  public TypeDescriptor getType() {
+    return this.type;
+  }
 
   public String printNode(int indent) {
     String st="{";
index 4feeb962ab0306c394fb6442e69220f2526965a0..ddc0dc5a0d61525403f30423e09fca1516291fb7 100644 (file)
@@ -773,9 +773,49 @@ public class SemanticCheck {
   }
 
   void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
+    Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
     for( int i = 0; i < ain.numVarInitializers(); ++i ) {
-      checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference()); 
+      checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
+      vec_type.add(ain.getVarInitializer(i).getType());
     }
+    // descide the type of this variableInitializerNode
+    TypeDescriptor out_type = vec_type.elementAt(0);
+    for(int i = 1; i < vec_type.size(); i++) {
+      TypeDescriptor tmp_type = vec_type.elementAt(i);
+      if(out_type == null) {
+        if(tmp_type != null) {
+          out_type = tmp_type;
+        }
+      } else if(out_type.isNull()) {
+        if(!tmp_type.isNull() ) {
+          if(!tmp_type.isArray()) {
+            throw new Error("Error: mixed type in var initializer list");
+          } else {
+            out_type = tmp_type;
+          }
+        }
+      } else if(out_type.isArray()) {
+        if(tmp_type.isArray()) {
+          if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
+            out_type = tmp_type;
+          }
+        } else if((tmp_type != null) && (!tmp_type.isNull())) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      } else if(out_type.isInt()) {
+        if(!tmp_type.isInt()) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      } else if(out_type.isString()) {
+        if(!tmp_type.isString()) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      }
+    }
+    if(out_type != null) {
+      out_type = out_type.makeArray(state);
+    }
+    ain.setType(out_type);
   }
 
   void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
index 486be9c9ccebbe40473c8e599e2023648e2a2111..f7d664d6df012bc3fcf9db79acb1d697faf74090 100644 (file)
@@ -1,4 +1,9 @@
 public class ArrayInitializerTest {
+  
+  public String[] sa = {"hello ", "world ", "!"};
+  
+  public ArrayInitializerTest(){}
+  
   static public void main( String[] args ) {
     
     int[] a = { 1, 2, 3 };
@@ -15,5 +20,10 @@ public class ArrayInitializerTest {
         System.out.println("ia[" + i + "] is null");
       }
     }
+    
+    ArrayInitializerTest ait = new ArrayInitializerTest();
+    for(int i = 0; i < ait.sa.length; i++) {
+      System.out.println(ait.sa[i]);
+    }
   }
 }
\ No newline at end of file