Skip to content

Commit a191320

Browse files
author
dlsmith
committed
DynamicJava: Refactoring of AST nodes, integrating type arguments with the appropriate nodes rather than having separate "Polymorphic..." subtypes.
git-svn-id: file:///tmp/test-svn/trunk@5110 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 39e2920 commit a191320

36 files changed

+537
-1478
lines changed

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.rice.cs.dynamicjava.interpreter;
22

3+
import java.util.Collections;
4+
35
import koala.dynamicjava.tree.*;
46
import koala.dynamicjava.tree.tiger.*;
57
import koala.dynamicjava.tree.visitor.*;
@@ -41,7 +43,10 @@ public void initializeClassSignatures(TypeDeclaration ast) {
4143
TypeContext sigContext = new ClassSignatureContext(_context, _c, _loader);
4244
TypeNameChecker sigChecker = new TypeNameChecker(sigContext, _opt);
4345
debug.logStart("Initializing type parameters", "class", ast.getName());
44-
try { sigChecker.checkStructureForTypeParameters(typeParameters(ast)); }
46+
try {
47+
Iterable<TypeParameter> tparams = ast.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
48+
sigChecker.checkStructureForTypeParameters(tparams);
49+
}
4550
finally { debug.logEnd(); }
4651
debug.logStart("Initializing supertypes", "class", ast.getName());
4752
try {
@@ -99,7 +104,10 @@ public void checkSignatures(TypeDeclaration ast) {
99104
TypeContext sigContext = new ClassSignatureContext(_context, _c, _loader);
100105
TypeNameChecker sigChecker = new TypeNameChecker(sigContext, _opt);
101106
debug.logStart("Check type parameters");
102-
try { sigChecker.ensureWellFormedTypeParameters(typeParameters(ast)); }
107+
try {
108+
Iterable<TypeParameter> tparams = ast.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
109+
sigChecker.ensureWellFormedTypeParameters(tparams);
110+
}
103111
finally { debug.logEnd(); }
104112
debug.logStart("Check supertypes");
105113
try {
@@ -185,17 +193,6 @@ private void checkBodies(Iterable<? extends Node> members) {
185193
visitMembers(members, new MemberBodyVisitor(bodyContext));
186194
}
187195

188-
private static TypeParameter[] typeParameters(TypeDeclaration ast) {
189-
if (ast instanceof GenericClassDeclaration) {
190-
return ((GenericClassDeclaration) ast).getTypeParameters();
191-
}
192-
else if (ast instanceof GenericInterfaceDeclaration) {
193-
return ((GenericInterfaceDeclaration) ast).getTypeParameters();
194-
}
195-
else { return new TypeParameter[0]; }
196-
}
197-
198-
199196
private abstract class MemberSignatureVisitor extends AbstractVisitor<Void> {
200197

201198
protected final TypeContext _bodyContext;
@@ -218,11 +215,7 @@ private abstract class MemberSignatureVisitor extends AbstractVisitor<Void> {
218215
TypeContext sigContext = new FunctionSignatureContext(_bodyContext, m);
219216
TypeNameChecker sigChecker = new TypeNameChecker(sigContext, _opt);
220217

221-
TypeParameter[] tparams;
222-
if (node instanceof PolymorphicMethodDeclaration) {
223-
tparams = ((PolymorphicMethodDeclaration) node).getTypeParameters();
224-
}
225-
else { tparams = new TypeParameter[0]; }
218+
Iterable<TypeParameter> tparams = node.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
226219
sigChecker.checkTypeParameters(tparams);
227220

228221
Type returnT = sigChecker.check(node.getReturnType());
@@ -275,11 +268,7 @@ else if (!mods.isAbstract() && node.getBody() == null) {
275268
TypeContext sigContext = new FunctionSignatureContext(_bodyContext, k);
276269
TypeNameChecker sigChecker = new TypeNameChecker(sigContext, _opt);
277270

278-
TypeParameter[] tparams;
279-
if (node instanceof PolymorphicConstructorDeclaration) {
280-
tparams = ((PolymorphicConstructorDeclaration) node).getTypeParameters();
281-
}
282-
else { tparams = new TypeParameter[0]; }
271+
Iterable<TypeParameter> tparams = node.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
283272
sigChecker.checkTypeParameters(tparams);
284273

285274
for (FormalParameter param : node.getParameters()) {

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

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -689,17 +689,9 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
689689
Node resolved = resolveAmbiguousName((AmbiguousName) receiver);
690690
if (resolved instanceof ReferenceTypeName) {
691691
// this is actually a StaticMethodCall
692-
Expression translation;
693-
if (node instanceof PolymorphicObjectMethodCall) {
694-
translation =
695-
new PolymorphicStaticMethodCall((ReferenceTypeName) resolved, node.getMethodName(), node.getArguments(),
696-
((PolymorphicObjectMethodCall) node).getTypeArguments(),
697-
node.getSourceInfo());
698-
}
699-
else {
700-
translation = new StaticMethodCall((ReferenceTypeName) resolved, node.getMethodName(),
701-
node.getArguments(), node.getSourceInfo());
702-
}
692+
Expression translation = new StaticMethodCall((ReferenceTypeName) resolved, node.getTypeArgs(),
693+
node.getMethodName(), node.getArguments(),
694+
node.getSourceInfo());
703695
setTranslation(node, translation);
704696
translation.acceptVisitor(this);
705697
return setType(node, getType(translation));
@@ -711,11 +703,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
711703

712704
Iterable<? extends Expression> args = node.getArguments();
713705
checkList(args);
714-
715-
Iterable<Type> targs = IterUtil.empty();
716-
if (node instanceof PolymorphicObjectMethodCall) {
717-
targs = checkTypeNameList(((PolymorphicObjectMethodCall) node).getTypeArguments());
718-
}
706+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
719707

720708
try {
721709
// Note: Changes made below may also need to be made in the TypeSystem's boxing & unboxing implementations
@@ -751,11 +739,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
751739

752740
Iterable<? extends Expression> args = node.getArguments();
753741
checkList(args);
754-
755-
Iterable<Type> targs = IterUtil.empty();
756-
if (node instanceof PolymorphicSuperMethodCall) {
757-
targs = checkTypeNameList(((PolymorphicSuperMethodCall) node).getTypeArguments());
758-
}
742+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
759743

760744
Expression obj = TypeUtil.makeEmptyExpression(node);
761745
setType(obj, t);
@@ -788,11 +772,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
788772

789773
Iterable<? extends Expression> args = node.getArguments();
790774
checkList(args);
791-
792-
Iterable<Type> targs = IterUtil.empty();
793-
if (node instanceof PolymorphicStaticMethodCall) {
794-
targs = checkTypeNameList(((PolymorphicStaticMethodCall) node).getTypeArguments());
795-
}
775+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
796776

797777
try {
798778
// Note: Changes made below may also need to be made in the TypeSystem's boxing & unboxing implementations
@@ -906,11 +886,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
906886

907887
Iterable<? extends Expression> args = node.getArguments();
908888
checkList(args);
909-
910-
Iterable<Type> targs = IterUtil.empty();
911-
if (node instanceof PolymorphicSimpleAllocation) {
912-
targs = checkTypeNameList(((PolymorphicSimpleAllocation) node).getTypeArguments());
913-
}
889+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
914890

915891
try {
916892
ConstructorInvocation inv = ts.lookupConstructor(t, targs, args, expected, context.accessModule());
@@ -951,11 +927,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
951927

952928
Iterable<? extends Expression> args = node.getArguments();
953929
checkList(args);
954-
955-
Iterable<Type> targs = IterUtil.empty();
956-
if (node instanceof PolymorphicAnonymousAllocation) {
957-
targs = checkTypeNameList(((PolymorphicAnonymousAllocation) node).getTypeArguments());
958-
}
930+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
959931

960932
if (!(IterUtil.isEmpty(args) && IterUtil.isEmpty(targs) && ts.isImplementable(t))) {
961933
// Super constructor invocation is something besides Object()
@@ -990,11 +962,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
990962
*/
991963
@Override public Type visit(InnerAllocation node) {
992964
Type enclosing = check(node.getExpression());
993-
994-
Iterable<Type> classTargs = IterUtil.empty();
995-
if (node.getClassTypeArguments() != null) {
996-
classTargs = checkTypeNameList(node.getClassTypeArguments());
997-
}
965+
Iterable<Type> classTargs = checkTypeNameList(node.getClassTypeArgs().unwrap(Collections.<TypeName>emptyList()));
998966

999967
try {
1000968
ClassType t = ts.lookupClass(node.getExpression(), node.getClassName(), classTargs, context.accessModule());
@@ -1009,11 +977,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
1009977

1010978
Iterable<? extends Expression> args = node.getArguments();
1011979
checkList(args);
1012-
1013-
Iterable<Type> targs = IterUtil.empty();
1014-
if (node instanceof PolymorphicInnerAllocation) {
1015-
targs = checkTypeNameList(((PolymorphicInnerAllocation) node).getTypeArguments());
1016-
}
980+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
1017981

1018982
try {
1019983
ConstructorInvocation inv = ts.lookupConstructor(t, targs, args, expected, context.accessModule());
@@ -1045,11 +1009,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
10451009
*/
10461010
@Override public Type visit(AnonymousInnerAllocation node) {
10471011
Type enclosing = check(node.getExpression());
1048-
1049-
Iterable<Type> classTargs = IterUtil.empty();
1050-
if (node.getClassTypeArguments() != null) {
1051-
classTargs = checkTypeNameList(node.getClassTypeArguments());
1052-
}
1012+
Iterable<Type> classTargs = checkTypeNameList(node.getClassTypeArgs().unwrap(Collections.<TypeName>emptyList()));
10531013

10541014
try {
10551015
ClassType t = ts.lookupClass(node.getExpression(), node.getClassName(), classTargs, context.accessModule());
@@ -1065,11 +1025,7 @@ private DJClass resolveThis(Option<String> outerName, Node node) {
10651025

10661026
Iterable<? extends Expression> args = node.getArguments();
10671027
checkList(args);
1068-
1069-
Iterable<Type> targs = IterUtil.empty();
1070-
if (node instanceof PolymorphicAnonymousInnerAllocation) {
1071-
targs = checkTypeNameList(((PolymorphicAnonymousInnerAllocation) node).getTypeArguments());
1072-
}
1028+
Iterable<Type> targs = checkTypeNameList(node.getTypeArgs().unwrap(Collections.<TypeName>emptyList()));
10731029

10741030
try {
10751031
ConstructorInvocation inv = ts.lookupConstructor(t, targs, args, expected, context.accessModule());

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
package edu.rice.cs.dynamicjava.interpreter;
7575

76+
import java.util.Collections;
7677
import java.util.Set;
7778
import java.util.HashSet;
7879
import java.util.List;
@@ -347,11 +348,7 @@ private TypeContext handleTypeDeclaration(TypeDeclaration node) {
347348
TypeContext sigContext = new FunctionSignatureContext(context, f);
348349
TypeNameChecker sigChecker = new TypeNameChecker(sigContext, opt);
349350

350-
TypeParameter[] tparams;
351-
if (node instanceof PolymorphicMethodDeclaration) {
352-
tparams = ((PolymorphicMethodDeclaration) node).getTypeParameters();
353-
}
354-
else { tparams = new TypeParameter[0]; }
351+
Iterable<TypeParameter> tparams = node.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
355352
sigChecker.checkTypeParameters(tparams);
356353

357354
Type returnT = sigChecker.check(node.getReturnType());

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,13 @@ else if (ast instanceof AnonymousInnerAllocation) {
219219

220220
String classSig = null;
221221
if (_java5) {
222-
TypeParameter[] paramAsts;
223-
if (ast instanceof GenericClassDeclaration) {
224-
paramAsts = ((GenericClassDeclaration) ast).getTypeParameters();
222+
List<TypeParameter> paramAsts = Collections.emptyList();
223+
if (ast instanceof TypeDeclaration) {
224+
paramAsts = ((TypeDeclaration) ast).getTypeParams().unwrap(paramAsts);
225225
}
226-
else if (ast instanceof GenericInterfaceDeclaration) {
227-
paramAsts = ((GenericInterfaceDeclaration) ast).getTypeParameters();
228-
}
229-
else { paramAsts = new TypeParameter[0]; }
230226

231227
StringBuilder sigBuilder = new StringBuilder();
232-
if (paramAsts.length > 0) { sigBuilder.append(typeParamListSignature(paramAsts)); }
228+
if (!paramAsts.isEmpty()) { sigBuilder.append(typeParamListSignature(paramAsts)); }
233229
sigBuilder.append(typeSignature(extendsT));
234230
for (ReferenceTypeName implementsT : implementsTs) {
235231
sigBuilder.append(typeSignature(NodeProperties.getType(implementsT)));
@@ -436,14 +432,9 @@ private void compileConstructor(ConstructorDeclaration ast, Type extendsT) {
436432

437433
String methodSig = null;
438434
if (_java5) {
439-
TypeParameter[] typeParamAsts;
440-
if (ast instanceof PolymorphicConstructorDeclaration) {
441-
typeParamAsts = ((PolymorphicConstructorDeclaration) ast).getTypeParameters();
442-
}
443-
else { typeParamAsts = new TypeParameter[0]; }
444-
435+
List<TypeParameter> typeParamAsts = ast.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
445436
StringBuilder sigBuilder = new StringBuilder();
446-
if (typeParamAsts.length > 0) { sigBuilder.append(typeParamListSignature(typeParamAsts)); }
437+
if (!typeParamAsts.isEmpty()) { sigBuilder.append(typeParamListSignature(typeParamAsts)); }
447438
String firstArgSig = (outerT == null) ? RUNTIME_BINDINGS_DESCRIPTOR : typeSignature(outerT);
448439
sigBuilder.append(paramListSignature(firstArgSig, extractVars(params)));
449440
sigBuilder.append("V");
@@ -620,14 +611,10 @@ private void compileMethod(MethodDeclaration ast, boolean isInterface) {
620611

621612
String methodSig = null;
622613
if (_java5) {
623-
TypeParameter[] typeParamAsts;
624-
if (ast instanceof PolymorphicMethodDeclaration) {
625-
typeParamAsts = ((PolymorphicMethodDeclaration) ast).getTypeParameters();
626-
}
627-
else { typeParamAsts = new TypeParameter[0]; }
614+
List<TypeParameter> typeParamAsts = ast.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
628615

629616
StringBuilder sigBuilder = new StringBuilder();
630-
if (typeParamAsts.length > 0) { sigBuilder.append(typeParamListSignature(typeParamAsts)); }
617+
if (!typeParamAsts.isEmpty()) { sigBuilder.append(typeParamListSignature(typeParamAsts)); }
631618
sigBuilder.append(paramListSignature(extraArg, extractVars(params)));
632619
sigBuilder.append(typeSignature(returnT));
633620
for (ReferenceTypeName tn : exceptions) {
@@ -1041,7 +1028,7 @@ private static String[] extractClassNames(List<? extends ReferenceTypeName> type
10411028
return result;
10421029
}
10431030

1044-
private static String typeParamListSignature(TypeParameter[] params) {
1031+
private static String typeParamListSignature(Iterable<? extends TypeParameter> params) {
10451032
StringBuilder result = new StringBuilder();
10461033
result.append('<');
10471034
for (TypeParameter p : params) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ public void ensureWellFormedList(Iterable<? extends TypeName> l) {
159159
* Tag the given type parameters with a new VariableType, and set the bounds appropriately;
160160
* verify that the results are well-formed.
161161
*/
162-
public void checkTypeParameters(TypeParameter[] tparams) {
162+
public void checkTypeParameters(Iterable<? extends TypeParameter> tparams) {
163163
checkStructureForTypeParameters(tparams);
164164
ensureWellFormedTypeParameters(tparams);
165165
}
166166

167167
/** Tag the given type parameters with a new VariableType, and set the bounds appropriately. */
168-
public void checkStructureForTypeParameters(TypeParameter[] tparams) {
168+
public void checkStructureForTypeParameters(Iterable<? extends TypeParameter> tparams) {
169169
for (TypeParameter tparam : tparams) {
170170
setTypeVariable(tparam, new VariableType(new BoundedSymbol(tparam, tparam.getRepresentation())));
171171
}
@@ -185,7 +185,7 @@ public void checkStructureForTypeParameters(TypeParameter[] tparams) {
185185
* Verify that the given type parameters (for which {@link #checkStructureForTypeParameters} has
186186
* already been invoked) are well-formed.
187187
*/
188-
public void ensureWellFormedTypeParameters(TypeParameter[] tparams) {
188+
public void ensureWellFormedTypeParameters(Iterable<? extends TypeParameter> tparams) {
189189
for (TypeParameter tparam : tparams) {
190190
if (!ts.isWellFormed(getTypeVariable(tparam))) {
191191
throw new ExecutionError("malformed.type", tparam);

dynamicjava/src/edu/rice/cs/dynamicjava/sourcechecker/SourceChecker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ public void run(Node node) {
223223
*/
224224
private static class PropertiesDepthFirstVisitor extends DepthFirstVisitor {
225225
public void run(Node node) {
226-
node.acceptVisitor(this);
226+
try { node.acceptVisitor(this); }
227+
catch (IllegalArgumentException e) { /* thrown by "empty" stub nodes -- ignore */ }
227228
if (NodeProperties.hasLeftExpression(node)) { recur(NodeProperties.getLeftExpression(node)); }
228229
if (NodeProperties.hasTranslation(node)) { recur(NodeProperties.getTranslation(node)); }
229230
if (NodeProperties.hasStatementTranslation(node)) { recur(NodeProperties.getTranslation(node)); }

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.rice.cs.dynamicjava.symbol;
22

3+
import java.util.Collections;
4+
35
import edu.rice.cs.plt.iter.IterUtil;
46
import edu.rice.cs.plt.lambda.WrappedException;
57

@@ -12,7 +14,6 @@
1214

1315
import koala.dynamicjava.tree.MethodDeclaration;
1416
import koala.dynamicjava.tree.tiger.TypeParameter;
15-
import koala.dynamicjava.tree.tiger.PolymorphicMethodDeclaration;
1617
import koala.dynamicjava.interpreter.NodeProperties;
1718

1819
/** Represents a local function declaration. */
@@ -29,11 +30,8 @@ public LocalFunction(MethodDeclaration ast) {
2930
public Type returnType() { return NodeProperties.getType(_ast.getReturnType()); }
3031

3132
public Iterable<VariableType> typeParameters() {
32-
if (_ast instanceof PolymorphicMethodDeclaration) {
33-
TypeParameter[] ps = ((PolymorphicMethodDeclaration)_ast).getTypeParameters();
34-
return IterUtil.mapSnapshot(IterUtil.asIterable(ps), NodeProperties.NODE_TYPE_VARIABLE);
35-
}
36-
else { return IterUtil.empty(); }
33+
Iterable<TypeParameter> tparams = _ast.getTypeParams().unwrap(Collections.<TypeParameter>emptyList());
34+
return IterUtil.mapSnapshot(tparams, NodeProperties.NODE_TYPE_VARIABLE);
3735
}
3836

3937
public Iterable<LocalVariable> parameters() {

0 commit comments

Comments
 (0)