From cf64b66f770b5335e5fb5327f4d81d2db7f1b62c Mon Sep 17 00:00:00 2001 From: jzhou Date: Tue, 29 Nov 2011 20:47:41 +0000 Subject: [PATCH] Array field initialization was accidentally broken. Fix it and add ArrayInitializerTest into the unit test suit in case it is broken again --- Robust/src/IR/Tree/SemanticCheck.java | 44 ++++++++++++++++--- Robust/src/Tests/DoTests | 1 + .../output/ArrayInitializerTest.output.goal | 10 +++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 Robust/src/Tests/output/ArrayInitializerTest.output.goal diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 1fa4a7c9..4ada3509 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -1072,13 +1072,47 @@ 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()); } - if (td==null) - throw new Error(); - - ain.setType(td); + // descide the type of this variableInitializerNode + TypeDescriptor out_type = null; + for(int i = 0; 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/DoTests b/Robust/src/Tests/DoTests index 687668f1..1f270171 100755 --- a/Robust/src/Tests/DoTests +++ b/Robust/src/Tests/DoTests @@ -19,6 +19,7 @@ dotest IntegerTest IntegerTest.java dotest AbstractTest AbstractTest.java dotest EnumTest EnumTest.java dotest InitializerTest InitializerTest.java +dotest ArrayInitializerTest ArrayInitializerTest.java dotest InterfaceTest InterfaceTest.java dotest StaticInnerClassTest StaticInnerClassTest.java dotest StaticTest StaticTest.java diff --git a/Robust/src/Tests/output/ArrayInitializerTest.output.goal b/Robust/src/Tests/output/ArrayInitializerTest.output.goal new file mode 100644 index 00000000..2f37304a --- /dev/null +++ b/Robust/src/Tests/output/ArrayInitializerTest.output.goal @@ -0,0 +1,10 @@ +a[2] should be 3: 3 +1 +2 +ia[1] is null +hello +world +! +ja[0] is null +3 +4 -- 2.34.1