Snapshot of Neal's working state.
authorgafter
Sun Jul 27 17:17:12 2008 -0700 (16 months ago)
changeset 82faceb9041853
parent 818729136e75b8
child 839571449ceff9
Snapshot of Neal's working state.
src/share/classes/com/sun/tools/javac/code/FunctionTypes.java
src/share/classes/com/sun/tools/javac/code/Symtab.java
src/share/classes/com/sun/tools/javac/comp/DeClosure.java
src/share/classes/com/sun/tools/javac/parser/Parser.java
src/share/classes/com/sun/tools/javac/tree/TreeMaker.java
--- a/src/share/classes/com/sun/tools/javac/code/FunctionTypes.java Sat Jul 26 23:02:31 2008 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/FunctionTypes.java Sun Jul 27 17:17:12 2008 -0700
@@ -351,7 +351,7 @@ public class FunctionTypes implements Co
}
if (target.generateFunctionClasses()) {
- JCClassDecl tree = make.Interface(sym);
+ JCClassDecl tree = make.ClassDef(sym);
sym.members_field = null;
JCExpression packge = make.QualIdent(sym.packge());
List<JCAnnotation> emptyAnnotations = List.nil();
--- a/src/share/classes/com/sun/tools/javac/code/Symtab.java Sat Jul 26 23:02:31 2008 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/Symtab.java Sun Jul 27 17:17:12 2008 -0700
@@ -161,6 +161,7 @@ public class Symtab {
public final Type inheritedType;
public final Type proprietaryType;
public final Type restrictedFunctionType;
+ public final Type threadType;
/** The symbol representing the length field of an array.
*/
@@ -417,6 +418,7 @@ public class Symtab {
suppressWarningsType = enterClass("java.lang.SuppressWarnings");
inheritedType = enterClass("java.lang.annotation.Inherited");
restrictedFunctionType = enterClass("java.lang.RestrictedFunction");
+ threadType = enterClass("java.lang.Thread");
// Enter a synthetic class that is used to mark Sun
// proprietary classes in ct.sym. This class does not have a
--- a/src/share/classes/com/sun/tools/javac/comp/DeClosure.java Sat Jul 26 23:02:31 2008 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/DeClosure.java Sun Jul 27 17:17:12 2008 -0700
@@ -50,14 +50,12 @@ import com.sun.tools.javac.tree.TreeInfo
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeScanner;
-import com.sun.tools.javac.tree.TreeTranslator;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Name;
-import java.lang.ref.Reference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -119,7 +117,6 @@ public class DeClosure {
class TransferState {
// TODO(gafter)2: we currently only handle nonlocal return.
JCExpression thread; // the field of the current frame that holds the executing thread
- JCExpression dead; // the field of the frame that is set to true when the method completes
}
// We scan each method, identifying the "lambda depth" of each
@@ -520,7 +517,7 @@ public class DeClosure {
// to a field of a final object. Each captured control transfer
// is translated into an idiom using exceptions or exception-like
// transfers.
- private class Phase2 extends TreeTranslator {
+ private class Phase2 extends com.sun.tools.javac.tree.TreeTranslator {
Map<JCMethodDecl,MethodState> methodStates = new HashMap<JCMethodDecl,MethodState>();
Phase1 phase1;
Phase2(Phase1 phase1) {
@@ -625,32 +622,100 @@ public class DeClosure {
}
JCExpression captureToField(int pos, VarSymbol sym) {
- frameStack.reify(pos);
- VarSymbol var = new VarSymbol(
- Flags.SYNTHETIC, sym.name.append('$', names.fromString(""+frameStack.decl.defs.size())),
- sym.type, frameStack.clazz);
+ VarSymbol var = makeFrameVar(pos, sym.name, sym.type, null);
JCExpression replacementExpression = make.at(pos).Select(
make.Ident(frameStack.var), var);
frameStack.captures = frameStack.captures.prepend(sym);
- // TODO(gafter)4: appending is inefficient.
- frameStack.decl.defs = frameStack.decl.defs.append(make.VarDef(var, null));
- frameStack.clazz.members_field.enter(var);
captureMap.put(sym, replacementExpression);
return replacementExpression;
}
+ VarSymbol makeFrameVar(int pos, String name, Type type, JCExpression init) {
+ return makeFrameVar(pos, names.fromString(name), type, init);
+ }
+
+ VarSymbol makeFrameVar(int pos, Name name, Type type, JCExpression init) {
+ frameStack.reify(pos);
+ return addField(pos, name, type, init, frameStack.decl);
+ }
+
+ VarSymbol addField(int pos, Name name, Type type, JCExpression init, JCClassDecl owner) {
+ VarSymbol var = new VarSymbol(
+ Flags.SYNTHETIC, names.fromString(frameStack.decl.defs.size()+"$"+name),
+ type, owner.sym);
+ // TODO(gafter)4: appending is inefficient.
+ owner.defs = owner.defs.append(make.VarDef(var, init));
+ owner.sym.members_field.enter(var);
+ return var;
+ }
+
+ JCClassDecl makeFrameClass(int pos, ClassSymbol clazz) {
+ frameStack.reify(pos);
+ return addMemberClass(pos, clazz, frameStack.decl);
+ }
+
+ JCClassDecl addMemberClass(int pos, ClassSymbol clazz, JCClassDecl owner) {
+ // TODO(gafter)4: appending is inefficient.
+ JCClassDecl decl = make.ClassDef(clazz);
+ owner.defs = owner.defs.append(decl);
+ owner.sym.members_field.enter(clazz);
+ return decl;
+ }
+
class MethodState extends FrameState {
+ final JCMethodDecl meth;
+ MethodState(JCMethodDecl meth) {
+ this.meth = meth;
+ }
JCStatement rewriteReturn(JCReturn tree) {
log.rawError(tree.pos, "'return' out of a closure not implemented");
return tree;
}
+ MethodSymbol findMethod(Type container, Name name) {
+ // a hack
+ return (MethodSymbol)((ClassSymbol)container.tsym).members().lookup(name).sym;
+ }
+ void createSupportForReturn() {
+ // (1) Add a field to the frame state: Thread thread = Thread.currentThread();
+ MethodSymbol currentThread = findMethod(syms.threadType, names.fromString("currentThread"));
+ JCExpression currentThreadExpr = make.at(meth.pos).App(make.QualIdent(currentThread));
+ VarSymbol thread = makeFrameVar(meth.pos, "thread", syms.threadType, currentThreadExpr);
+
+ // (2) Add a new, local exception class for the return
+ ClassSymbol returnExClass = new ClassSymbol(
+ Flags.SYNTHETIC, names.fromString(frameStack.decl.defs.size()+"$Return"),
+ frameStack.clazz);
+ // TODO(gafter)0: fill in the superclass as Jump
+ JCClassDecl returnEx = makeFrameClass(meth.pos, returnExClass);
+ log.rawError(meth.pos, "need to implement support state for nonlocal return");
+ if (true) throw new UnsupportedOperationException();
+ // TODO(gafter)0: fill in required members in the symbol table:
+ // static class MyReturn extends Jump {
+ // final Frame$ frame = frame$;
+ // final int value;
+ // @Override public void thread() { return frame.thread; }
+ // MyReturn(int value) {
+ // super();
+ // this.value = value;
+ // }
+ // }
+ // TODO(gafter)0: create Frame$ frame field
+ // TODO(gafter)0: create optional return value field
+ // TODO(gafter)0: create thread() method overrider
+ // TODO(gafter)0: create constructor taking return values
+ }
+ JCBlock wrapBody(JCBlock body) {
+ log.rawError(meth.pos, "need to wrap body nonlocal return");
+ throw new UnsupportedOperationException();
+ }
}
@Override
public void visitMethodDef(final JCMethodDecl tree) {
- final MethodState frameState = new MethodState();
+ final MethodState frameState = new MethodState(tree);
methodStates.put(tree, frameState); // TODO(gafter)2: can we use the frame stack instead?
Symbol oldOwner = currentOwner;
+ final boolean requiresCapturedReturns = phase1.capturedTransfers.remove(tree);
try {
currentOwner = tree.sym;
final ListBuffer<JCStatement> assign = ListBuffer.<JCStatement>lb();
@@ -666,12 +731,11 @@ public class DeClosure {
assign.add(make.at(param.pos).Exec(make.Assign(replacementExpression, make.Ident(param.sym)).setType(param.sym.type)));
}
}
- if (phase1.capturedTransfers.remove(tree)) {
- System.err.println("need to implement support state for nonlocal return");
- // TODO(gafter)1: implement support state for nonlocal return.
- return;
- }
+ if (requiresCapturedReturns)
+ frameState.createSupportForReturn();
Phase2.super.visitMethodDef(tree);
+ if (requiresCapturedReturns)
+ tree.body = frameState.wrapBody(tree.body);
}
});
if (tree.body != null)
--- a/src/share/classes/com/sun/tools/javac/parser/Parser.java Sat Jul 26 23:02:31 2008 -0700
+++ b/src/share/classes/com/sun/tools/javac/parser/Parser.java Sun Jul 27 17:17:12 2008 -0700
@@ -1405,10 +1405,9 @@ public class Parser {
Trial<Boolean> findColon = new Trial<Boolean>() {
public Boolean trial(Commit _unused) {
accept(LPAREN);
- // TODO(gafter)0: rewrite in terms of skipType()
if (!skipParams()) return false;
return S.token() == COLON;
- }
+ }
};
return trial(findColon);
}
@@ -1458,7 +1457,6 @@ public class Parser {
boolean acceptGT() {
switch (S.token()) {
- // TODO(gafter)0: refactor this common code.
case GT:
S.nextToken();
return true;
@@ -3384,7 +3382,7 @@ public class Parser {
/** Skip tokens that could be in a parameter list, but return false if
* it could not be a parameter list.
*/
- // TODO(gafter)0: rewrite in terms of skipType().
+ // TODO(gafter)5: rewrite in terms of skipType().
boolean skipParams() {
Token prevToken = null;
while (true) {
--- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Sat Jul 26 23:02:31 2008 -0700
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Sun Jul 27 17:17:12 2008 -0700
@@ -826,12 +826,11 @@ public class TreeMaker implements JCTree
return buf.toList();
}
- /** Create an interface declaration from the interface type symbol. */
- public JCClassDecl Interface(TypeSymbol tsym) {
- assert (tsym.flags() & Flags.INTERFACE) != 0;
+ /** Create an interface or class declaration from the interface type symbol. */
+ public JCClassDecl ClassDef(TypeSymbol tsym) {
JCModifiers mods = Modifiers(tsym.flags(), Annotations(tsym.getAnnotationMirrors()));
List<JCTypeParameter> typeParams = TypeParams(tsym.type.getTypeArguments());
- JCTree extending = null;
+ JCTree extending = (tsym.flags() & Flags.INTERFACE) != 0 ? null : Type(types.supertype(tsym.type));
List<JCExpression> implementing = Types(types.interfaces(tsym.type));
List<JCTree> defs = Members(tsym.members());
return ClassDef(mods, tsym.name, typeParams, extending, implementing, defs);