Skip to content

Commit b2bccf4

Browse files
author
dlsmith
committed
DynamicJava: Fixed bugs in varargs handling and in attempted unboxing of "null".
git-svn-id: file:///tmp/test-svn/trunk@4564 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent e7fa839 commit b2bccf4

File tree

8 files changed

+63
-48
lines changed

8 files changed

+63
-48
lines changed

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/ExpressionChecker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,9 @@ private Type handleIncrementExpression(UnaryExpression node) {
17661766
*/
17671767
@Override public Type visit(ArrayTypeName node) {
17681768
Type elementType = node.getElementType().acceptVisitor(this);
1769-
return setType(node, new SimpleArrayType(elementType));
1769+
Type arrayT = node.isVararg() ? new VarargArrayType(elementType) :
1770+
new SimpleArrayType(elementType);
1771+
return setType(node, arrayT);
17701772
}
17711773

17721774
}

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/ExtendedTypeSystem.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,14 @@ private boolean isConcreteClass(DJClass c) {
198198
* (in terms of {@link isEqual}), transitive relation.
199199
*/
200200
public boolean isSubtype(Type subT, Type superT) {
201-
return isSubtype(subT, superT, new RecursionStack2<Type, Type>());
201+
//debug.logStart(new String[]{"subT", "superT"}, subT, superT);
202+
boolean result = isSubtype(subT, superT, new RecursionStack2<Type, Type>());
203+
//debug.logEnd("result", result);
204+
return result;
202205
}
203206

204207
private boolean isSubtype(final Type subT, final Type superT, final RecursionStack2<Type, Type> stack) {
205-
//debug.logStart();
206-
//debug.logValues(new String[]{"subT", "superT"}, subT, superT); try {
208+
//debug.logStart(new String[]{"subT", "superT"}, subT, superT); try {
207209

208210
if (subT.equals(superT)) { return true; } // what follows assumes the types are not syntactically equal
209211

@@ -319,7 +321,10 @@ private Boolean handleArrayType(final ArrayType subT) {
319321
public Boolean defaultCase(Type superT) { return false; }
320322

321323
@Override public Boolean forArrayType(ArrayType superT) {
322-
if (isPrimitive(subT.ofType())) { return false; }
324+
if (isPrimitive(subT.ofType())) {
325+
// types may be inequal if one is vararg and the other is not
326+
return subT.ofType().equals(superT.ofType());
327+
}
323328
else { return isSubtype(subT.ofType(), superT.ofType(), stack); }
324329
}
325330

@@ -445,14 +450,15 @@ public boolean isAssignable(Type target, Type expT, Object expValue) {
445450

446451
public boolean isPrimitiveConvertible(Type t) {
447452
return isPrimitive(t) ||
448-
isSubtype(t, BOOLEAN_CLASS) ||
449-
isSubtype(t, CHARACTER_CLASS) ||
450-
isSubtype(t, BYTE_CLASS) ||
451-
isSubtype(t, SHORT_CLASS) ||
452-
isSubtype(t, INTEGER_CLASS) ||
453-
isSubtype(t, LONG_CLASS) ||
454-
isSubtype(t, FLOAT_CLASS) ||
455-
isSubtype(t, DOUBLE_CLASS);
453+
(!isSubtype(t, NULL) &&
454+
(isSubtype(t, BOOLEAN_CLASS) ||
455+
isSubtype(t, CHARACTER_CLASS) ||
456+
isSubtype(t, BYTE_CLASS) ||
457+
isSubtype(t, SHORT_CLASS) ||
458+
isSubtype(t, INTEGER_CLASS) ||
459+
isSubtype(t, LONG_CLASS) ||
460+
isSubtype(t, FLOAT_CLASS) ||
461+
isSubtype(t, DOUBLE_CLASS)));
456462
}
457463

458464

@@ -1121,7 +1127,9 @@ public Expression makePrimitive(Expression e) throws UnsupportedConversionExcept
11211127
Type t = NodeProperties.getType(e);
11221128
if (isPrimitive(t)) { return e; }
11231129
// Note: The spec is not clear about whether a *subtype* (such as a variable) can
1124-
// be unboxed. We allow it here, because that seems like the correct approach.
1130+
// be unboxed. We allow it here unless the type is null, because that seems
1131+
// like the correct approach.
1132+
else if (isSubtype(t, NULL)) { throw new UnsupportedConversionException(); }
11251133
else if (isSubtype(t, BOOLEAN_CLASS)) { return unbox(e, "booleanValue"); }
11261134
else if (isSubtype(t, CHARACTER_CLASS)) { return unbox(e, "charValue"); }
11271135
else if (isSubtype(t, BYTE_CLASS)) { return unbox(e, "byteValue"); }
@@ -2610,7 +2618,7 @@ public MultiVarargChecker(Iterable<? extends Type> params, Iterable<? extends Ex
26102618
@Override public boolean matchesWithVarargs() {
26112619
if (_varargParam instanceof VarargArrayType) {
26122620
boxArgs();
2613-
if (matches()) {
2621+
if (super.matches()) {
26142622
ArrayType arrayT = (ArrayType) substitute((ArrayType) _varargParam, _tparams, _targs);
26152623
Type elementT = arrayT.ofType();
26162624
Iterable<Expression> boxedVarargArgs = EMPTY_EXPRESSION_ITERABLE;

dynamicjava/src/koala/dynamicjava/parser/grammar.jj

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ public class Parser {
171171

172172
// If the field is an array, create an array type node
173173
if (dim > 0) {
174-
typ = new ArrayTypeName(typ, dim,
175-
filename,
176-
typ.getBeginLine(), typ.getBeginColumn(),
177-
typ.getEndLine(), typ.getEndColumn());
174+
typ = new ArrayTypeName(typ, dim, false,
175+
filename,
176+
typ.getBeginLine(), typ.getBeginColumn(),
177+
typ.getEndLine(), typ.getEndColumn());
178178
}
179179

180180
return new FieldDeclaration(flags, typ, name.image, exp,
@@ -212,10 +212,10 @@ public class Parser {
212212

213213
// If the variable contains an array, create an array type node
214214
if (dim > 0) {
215-
typ = new ArrayTypeName(typ, dim,
216-
filename,
217-
typ.getBeginLine(), typ.getBeginColumn(),
218-
typ.getEndLine(), typ.getEndColumn());
215+
typ = new ArrayTypeName(typ, dim, false,
216+
filename,
217+
typ.getBeginLine(), typ.getBeginColumn(),
218+
typ.getEndLine(), typ.getEndColumn());
219219
}
220220

221221
return new VariableDeclaration(isFinal, typ, name.image, exp,
@@ -1757,10 +1757,10 @@ PARSER_END(Parser)
17571757
bc = typ.getBeginColumn();
17581758
}
17591759
if (dim > 0) {
1760-
typ = new ArrayTypeName(typ, dim,
1761-
filename,
1762-
typ.getBeginLine(), typ.getBeginColumn(),
1763-
typ.getEndLine(), typ.getEndColumn());
1760+
typ = new ArrayTypeName(typ, dim, false,
1761+
filename,
1762+
typ.getBeginLine(), typ.getBeginColumn(),
1763+
typ.getEndLine(), typ.getEndColumn());
17641764
}
17651765
if(lastFormalParameterIsVarArgs) flags |= 0x00000080; // java.lang.reflect.Modifier.VARARGS == 0x00000080 /**/
17661766

@@ -1842,10 +1842,10 @@ PARSER_END(Parser)
18421842
[ f="final" ] typ=type() id=<IDENTIFIER> ( "[" "]" { dim++; } )*
18431843
{
18441844
if (dim > 0) {
1845-
typ = new ArrayTypeName(typ, dim,
1846-
filename,
1847-
typ.getBeginLine(), typ.getBeginColumn(),
1848-
typ.getEndLine(), typ.getEndColumn());
1845+
typ = new ArrayTypeName(typ, dim, false,
1846+
filename,
1847+
typ.getBeginLine(), typ.getBeginColumn(),
1848+
typ.getEndLine(), typ.getEndColumn());
18491849
}
18501850
return new FormalParameter(f != null, typ, id.image,
18511851
filename,
@@ -1875,10 +1875,10 @@ PARSER_END(Parser)
18751875
[ f="final" ] typ=type() <VAR_ARGS> id=<IDENTIFIER>
18761876
{
18771877
// convert the type of the formal parameter to be an array
1878-
typ = new ArrayTypeName(typ, 1,
1879-
filename,
1880-
typ.getBeginLine(), typ.getBeginColumn(),
1881-
typ.getEndLine(), typ.getEndColumn());
1878+
typ = new ArrayTypeName(typ, 1, true,
1879+
filename,
1880+
typ.getBeginLine(), typ.getBeginColumn(),
1881+
typ.getEndLine(), typ.getEndColumn());
18821882
return new FormalParameter(f != null, typ, id.image,
18831883
filename,
18841884
(f != null) ? f.beginLine : typ.getBeginLine(),
@@ -1887,7 +1887,7 @@ PARSER_END(Parser)
18871887
}
18881888
}
18891889
catch(ParseException pe) {
1890-
_throwParseException(pe, "Invalid variable arguments formal parameter");
1890+
_throwParseException(pe, "Invalid varargs formal parameter");
18911891
}
18921892
}
18931893
@@ -4110,10 +4110,10 @@ PARSER_END(Parser)
41104110
if (dim == 0) {
41114111
return rt;
41124112
} else {
4113-
return new ArrayTypeName(rt, dim,
4114-
filename,
4115-
rt.getBeginLine(), rt.getBeginColumn(),
4116-
t.endLine, t.endColumn);
4113+
return new ArrayTypeName(rt, dim, false,
4114+
filename,
4115+
rt.getBeginLine(), rt.getBeginColumn(),
4116+
t.endLine, t.endColumn);
41174117
}
41184118
}
41194119
}

dynamicjava/src/koala/dynamicjava/tree/ArrayAllocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void initialize(TypeName t) {
187187
if (initialization != null) {
188188
TypeName et;
189189
if (dimension > 1)
190-
et = new ArrayTypeName(t, dimension-1, t.getFilename(), t.getBeginLine(), t.getBeginColumn(), endLine, endColumn);
190+
et = new ArrayTypeName(t, dimension-1, false, t.getFilename(), t.getBeginLine(), t.getBeginColumn(), endLine, endColumn);
191191
else
192192
et = t;
193193

dynamicjava/src/koala/dynamicjava/tree/ArrayTypeName.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ public class ArrayTypeName extends TypeName {
4848
*/
4949
private TypeName elementType;
5050

51+
private boolean vararg;
52+
5153
/**
5254
* Initializes the type
5355
* @param et the element type
5456
* @param dim the dimension of the arrays represented by this type (> 0)
5557
* @exception IllegalArgumentException if et is null or dim < 1
5658
*/
57-
public ArrayTypeName(TypeName et, int dim) {
58-
this(et, dim, null, 0, 0, 0, 0);
59+
public ArrayTypeName(TypeName et, int dim, boolean varg) {
60+
this(et, dim, varg, null, 0, 0, 0, 0);
5961
}
6062

6163
/**
@@ -69,13 +71,14 @@ public ArrayTypeName(TypeName et, int dim) {
6971
* @param ec the end column
7072
* @exception IllegalArgumentException if et is null or dim < 1
7173
*/
72-
public ArrayTypeName(TypeName et, int dim, String fn, int bl, int bc, int el, int ec) {
74+
public ArrayTypeName(TypeName et, int dim, boolean varg, String fn, int bl, int bc, int el, int ec) {
7375
super(fn, bl, bc, el, ec);
7476

7577
if (et == null) throw new IllegalArgumentException("et == null");
7678
if (dim < 1) throw new IllegalArgumentException("dim < 1");
7779

78-
elementType = (dim > 1) ? new ArrayTypeName(et, dim - 1, fn, bl, bc, el, ec) : et;
80+
elementType = (dim > 1) ? new ArrayTypeName(et, dim - 1, false, fn, bl, bc, el, ec) : et;
81+
vararg = varg;
7982
}
8083

8184
/**
@@ -95,6 +98,8 @@ public void setElementType(TypeName t) {
9598
firePropertyChange(ELEMENT_TYPE, elementType, elementType = t);
9699
}
97100

101+
public boolean isVararg() { return vararg; }
102+
98103
/**
99104
* Allows a visitor to traverse the tree
100105
* @param visitor the visitor to accept

dynamicjava/src/koala/dynamicjava/tree/EnumDeclaration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static List<Node> AddValues(String enumTypeName, List<Node> body, List<EnumConst
114114
int accessFlags = java.lang.reflect.Modifier.PRIVATE | java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.FINAL;
115115

116116
ReferenceTypeName enumType = new ReferenceTypeName(enumTypeName);
117-
TypeName valuesType = new ArrayTypeName(enumType, 1);
117+
TypeName valuesType = new ArrayTypeName(enumType, 1, false);
118118
List<Expression> sizes = new LinkedList<Expression>();
119119
sizes.add(new IntegerLiteral(String.valueOf(consts_names.length)));
120120
List<Expression> cells = new LinkedList<Expression>();

dynamicjava/src/koala/dynamicjava/tree/TreeUtilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static TypeName classToTypeName(Class<?> c, String fn, int bl, int bc, in
7878
result = new VoidTypeName(fn, bl, bc, el, ec);
7979
} else if (c.isArray()) {
8080
result = new ArrayTypeName(classToTypeName(c.getComponentType(), fn, bl, bc, el, ec),
81-
1, fn, bl, bc, el, ec);
81+
1, false, fn, bl, bc, el, ec);
8282
} else {
8383
List<IdentifierToken> ids = new ArrayList<IdentifierToken>(1);
8484
ids.add(new Identifier(c.getName()));

dynamicjava/src/koala/dynamicjava/tree/TreeUtilitiesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testClassToType() {
7070
equals(TreeUtilities.classToTypeName(boolean.class)));
7171
assertEquals("",true,new VoidTypeName().
7272
equals(TreeUtilities.classToTypeName(void.class)));
73-
assertEquals("",true,new ArrayTypeName(new IntTypeName(),1).
73+
assertEquals("",true,new ArrayTypeName(new IntTypeName(),1, false).
7474
equals(TreeUtilities.classToTypeName(int[].class)));
7575
assertEquals("",true,new ReferenceTypeName(Integer.class.getName()).
7676
equals(TreeUtilities.classToTypeName(Integer.class)));

0 commit comments

Comments
 (0)