From ebf3960a04c5a354ee19bc384176d5a1ae9fe3da Mon Sep 17 00:00:00 2001 From: jzhou Date: Thu, 27 Jan 2011 19:05:30 +0000 Subject: [PATCH] Enable field array initialization for MGC --- Robust/src/IR/Flat/BuildFlat.java | 2 +- Robust/src/IR/Tree/ArrayInitializerNode.java | 12 ++++++ Robust/src/IR/Tree/SemanticCheck.java | 42 +++++++++++++++++++- Robust/src/Tests/ArrayInitializerTest.java | 10 +++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index f5347897..d8e4442e 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -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; diff --git a/Robust/src/IR/Tree/ArrayInitializerNode.java b/Robust/src/IR/Tree/ArrayInitializerNode.java index 48dab8a7..e6bc7b33 100644 --- a/Robust/src/IR/Tree/ArrayInitializerNode.java +++ b/Robust/src/IR/Tree/ArrayInitializerNode.java @@ -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="{"; diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 4feeb962..ddc0dc5a 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -773,9 +773,49 @@ public class SemanticCheck { } void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) { + Vector vec_type = new Vector(); 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) { diff --git a/Robust/src/Tests/ArrayInitializerTest.java b/Robust/src/Tests/ArrayInitializerTest.java index 486be9c9..f7d664d6 100644 --- a/Robust/src/Tests/ArrayInitializerTest.java +++ b/Robust/src/Tests/ArrayInitializerTest.java @@ -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 -- 2.34.1