Skip to content

Commit 35470e8

Browse files
author
dlsmith
committed
DynamicJava: Adjusted constant field hack so that it doesn't allow non-primitive, non-String fields to be treated as constants. SourceInfos support ordering. SourceCheckerImprovements. Checking for multiple errors in a single AST doesn't break when an error occurs in a ConstructorCall (the constructor body was then skipped previously).
git-svn-id: file:///tmp/test-svn/trunk@5167 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 488e3f8 commit 35470e8

8 files changed

Lines changed: 439 additions & 140 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,20 @@ private class MemberBodyVisitor extends AbstractVisitor<Void> {
356356
TypeContext bodyContext = new FunctionContext(sigContext, k);
357357
ExpressionChecker callChecker = new ExpressionChecker(bodyContext, _opt);
358358
ConstructorCall call = node.getConstructorCall();
359-
if (call != null) { callChecker.checkConstructorCall(call); }
360-
new StatementChecker(bodyContext, _opt).checkList(node.getStatements());
361-
// if the call is implicit, check it *after* checking the body (better error messages this way)
362-
if (call == null) { callChecker.checkConstructorCall(new ConstructorCall(null, null, true)); }
359+
ExecutionError error = null;
360+
if (call != null) {
361+
try { callChecker.checkConstructorCall(call); }
362+
catch (ExecutionError e) { error = e; }
363+
}
364+
try { new StatementChecker(bodyContext, _opt).checkList(node.getStatements()); }
365+
catch (ExecutionError e) {
366+
if (error != null) { error = e; }
367+
}
368+
// if the call is implicit, only check it if there are no errors (avoids redundant errors)
369+
if (call == null && error == null) {
370+
callChecker.checkConstructorCall(new ConstructorCall(null, null, true));
371+
}
372+
if (error != null) { throw error; }
363373
return null;
364374
}
365375

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
import edu.rice.cs.dynamicjava.symbol.type.*;
8888

8989
import koala.dynamicjava.tree.*;
90-
import koala.dynamicjava.tree.tiger.*;
9190
import koala.dynamicjava.tree.visitor.*;
9291
import koala.dynamicjava.interpreter.error.ExecutionError;
9392
import koala.dynamicjava.interpreter.TypeUtil;

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

Lines changed: 390 additions & 59 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import edu.rice.cs.dynamicjava.symbol.type.Type;
44
import edu.rice.cs.dynamicjava.symbol.type.VariableType;
5-
import edu.rice.cs.plt.tuple.Option;
65

76
/** Represents a class declaration. */
87
public interface DJClass extends Access.Limited, Access.Module {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ protected class JavaField implements DJField {
170170

171171
public Option<Object> constantValue() {
172172
// Whether a field is declared as a constant is not available via the reflection API,
173-
// so we approximate by treating all static final fields as constants.
174-
// (Note that some code my execute here during the type checking phase, before "run time".
173+
// so we approximate by treating all static final fields with a primitive/String type as constants.
174+
// (Note that some code may execute here during the type checking phase, before "run time".
175175
// This seems to be unavoidable given the reflection-based design.)
176-
if (isStatic() && isFinal()) {
176+
if (isStatic() && isFinal() && (_f.getType().isPrimitive() || _f.getType().equals(String.class))) {
177177
try { return Option.some(boxForReceiver(null).value()); }
178178
catch (WrappedException e) { return Option.none(); }
179179
}
@@ -253,7 +253,7 @@ public JavaConstructor(Constructor<?> k) {
253253
_params = makeParamThunk(); /* allows overriding */
254254
}
255255

256-
public String declaredName() { return JavaClass.this.declaredName(); }
256+
public String declaredName() { return isAnonymous() ? "<anonymous>" : JavaClass.this.declaredName(); }
257257
public DJClass declaringClass() { return JavaClass.this; }
258258
public Access accessibility() { return extractAccessibility(_k.getModifiers()); }
259259
public Access.Module accessModule() { return JavaClass.this.accessModule(); }

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ private boolean validCheckedCast(Type target, final Type source) {
980980
@Override public Boolean forParameterizedClassType(ParameterizedClassType target) {
981981
if (isReifiable(target)) { return true; }
982982
else if (isSubtype(target, source)) {
983-
// Verify that that, given a value of type source & erase(target), it must have type target.
983+
// Verify that, given a value of type source & erase(target), it must have type target.
984984
// Must show, where target=Target<T1..Tn>, forall X1..Xn, Target<X1..Xn> <: source implies Xi=Ti.
985985
ParameterizedClassType wildCapt = capture(parameterize(new RawClassType(target.ofClass())));
986986
Iterable<VariableType> unboundArgs =
@@ -2264,6 +2264,10 @@ public InstantiatedMethod(DJMethod m, Iterable<VariableType> classTParams, Itera
22642264
public Iterable<? extends Type> parameterTypes() {
22652265
return substitute(SymbolUtil.parameterTypes(_delegate), _sigma);
22662266
}
2267+
public String toString() {
2268+
TypePrinter p = typePrinter();
2269+
return "InstantiatedMethod(" + p.print(this) + ")";
2270+
}
22672271
}
22682272

22692273
private static abstract class DelegatingConstructor extends DelegatingFunction<DJConstructor>
@@ -2287,6 +2291,7 @@ private class ErasedConstructor extends DelegatingConstructor {
22872291
protected Iterable<Type> parameterTypes() {
22882292
return IterUtil.mapSnapshot(SymbolUtil.parameterTypes(_delegate), ERASE);
22892293
}
2294+
public String toString() { return "ErasedConstructor(" + declaredName() + ")"; }
22902295
}
22912296

22922297
private class InstantiatedConstructor extends DelegatingConstructor {
@@ -2305,6 +2310,7 @@ public InstantiatedConstructor(DJConstructor k, Iterable<VariableType> classTPar
23052310
public Iterable<? extends Type> parameterTypes() {
23062311
return substitute(SymbolUtil.parameterTypes(_delegate), _sigma);
23072312
}
2313+
public String toString() { return "InstantiatedConstructor(" + declaredName() + ")"; }
23082314
}
23092315

23102316
/** Create new type parameters for function {@code f} with bounds instantiated by the given substitution. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public DJConstructor value() {
355355
_outerClass = SymbolUtil.dynamicOuterClass(TreeClass.this);
356356
}
357357

358-
public String declaredName() { return TreeClass.this.declaredName(); }
358+
public String declaredName() { return isAnonymous() ? "<anonymous>" : TreeClass.this.declaredName(); }
359359
public DJClass declaringClass() { return TreeClass.this; }
360360
public Access.Module accessModule() { return _accessModule; }
361361
public Type returnType() { return SymbolUtil.thisType(TreeClass.this); }

dynamicjava/src/koala/dynamicjava/tree/SourceInfo.java

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -62,72 +62,28 @@
6262
*
6363
*/
6464

65-
// Commented code below belonged to DynamicJava that got overriden by code
66-
// imported from the earlier DrJava's Interactions preprocessor
67-
68-
///**
69-
// * To represent the source code informations
70-
// */
71-
//public static class SourceInformation {
72-
// // The fields
73-
// private String filename;
74-
// private int line;
75-
// private int column;
76-
//
77-
// /**
78-
// * Creates a source information
79-
// */
80-
// public SourceInformation(String filename, int line, int column) {
81-
// this.filename = filename;
82-
// this.line = line;
83-
// this.column = column;
84-
// }
85-
//
86-
// /**
87-
// * Returns the filename
88-
// */
89-
// public String getFilename() {
90-
// return filename;
91-
// }
92-
//
93-
// /**
94-
// * Returns the line where the error occurs
95-
// */
96-
// public int getLine() {
97-
// return line;
98-
// }
99-
//
100-
// /**
101-
// * Returns the column where the error occurs
102-
// */
103-
// public int getColumn() {
104-
// return column;
105-
// }
106-
//}
107-
108-
109-
// Code below imported from the earlier DrJava's Interactions preprocessor
11065
package koala.dynamicjava.tree;
11166

11267
import java.io.*;
11368

11469
import edu.rice.cs.plt.object.ObjectUtil;
70+
import edu.rice.cs.plt.tuple.Option;
11571

11672
/** A simple tuple class to represent source location. */
117-
public final class SourceInfo {
73+
public final class SourceInfo implements Comparable<SourceInfo> {
11874

11975
public interface Wrapper {
12076
SourceInfo getSourceInfo();
12177
}
12278

123-
public static final SourceInfo NONE = new SourceInfo(null, 0, 0, 0, 0);
79+
public static final SourceInfo NONE = new SourceInfo(Option.<File>none(), 0, 0, 0, 0);
12480

12581
public static SourceInfo point(File f, int line, int column) {
126-
return new SourceInfo(f, line, column, line, column);
82+
return new SourceInfo(Option.wrap(f), line, column, line, column);
12783
}
12884

12985
public static SourceInfo range(File f, int startLine, int startColumn, int endLine, int endColumn) {
130-
return new SourceInfo(f, startLine, startColumn, endLine, endColumn);
86+
return new SourceInfo(Option.wrap(f), startLine, startColumn, endLine, endColumn);
13187
}
13288

13389
public static SourceInfo extend(SourceInfo si, int endLine, int endColumn) {
@@ -164,9 +120,9 @@ public static SourceInfo span(Wrapper first, Wrapper second) {
164120
}
165121

166122
/**
167-
* The source file. May be null.
123+
* The source file.
168124
*/
169-
private final File _file;
125+
private final Option<File> _file;
170126

171127
/**
172128
* The starting line of the source location
@@ -188,32 +144,19 @@ public static SourceInfo span(Wrapper first, Wrapper second) {
188144
*/
189145
private final int _endColumn;
190146

191-
/**
192-
* Constructs a SourceInfo.
193-
* @param file The source file. May be null, indicating no source file.
194-
* @param startLine Starting line
195-
* @param startColumn Starting column
196-
* @param endLine Ending line
197-
* @param endColumn Ending column
198-
*/
199-
private SourceInfo(File file,
200-
int startLine,
201-
int startColumn,
202-
int endLine,
203-
int endColumn)
204-
{
147+
private SourceInfo(Option<File> file, int startLine, int startColumn, int endLine, int endColumn) {
205148
_file = file;
206149
_startLine = startLine;
207150
_startColumn = startColumn;
208151
_endLine = endLine;
209152
_endColumn = endColumn;
210153
}
211154

212-
/** May be null, if the source file is unknown. */
213-
public File getFile() { return _file; }
155+
/** May be null, if the source file is unknown. TODO: Change this interface to Option<File>. */
156+
public File getFile() { return _file.unwrap(null); }
214157

215158
/** Get the file's name, or {@code "(no file)"}. */
216-
public String getFilename() { return _file == null ? "(no file)" : _file.getPath(); }
159+
public String getFilename() { return _file.isNone() ? "(no file)" : _file.unwrap().getPath(); }
217160

218161
public int getStartLine() { return _startLine; }
219162
public int getStartColumn() { return _startColumn; }
@@ -247,7 +190,7 @@ public String toString() {
247190
else {
248191
SourceInfo casted = (SourceInfo) obj;
249192
return
250-
ObjectUtil.equal(this._file, casted._file) &&
193+
this._file.equals(casted._file) &&
251194
this._startLine == casted._startLine &&
252195
this._startColumn == casted._startColumn &&
253196
this._endLine == casted._endLine &&
@@ -259,4 +202,15 @@ public String toString() {
259202
return ObjectUtil.hash(getClass(), _file, _startLine, _startColumn, _endLine, _endColumn);
260203
}
261204

205+
public int compareTo(SourceInfo that) {
206+
int result = Option.<File>comparator().compare(this._file, that._file);
207+
if (result == 0) {
208+
result = ObjectUtil.compare(this._startLine, that._startLine,
209+
this._startColumn, that._startColumn,
210+
this._endLine, that._endLine,
211+
this._endColumn, that._endColumn);
212+
}
213+
return result;
214+
}
215+
262216
}

0 commit comments

Comments
 (0)