nashorn/src/jdk/nashorn/internal/codegen/Lower.java
changeset 17769 14ea7feaf658
parent 17752 9d2d0e74a833
child 17982 03696682b6e5
--- a/nashorn/src/jdk/nashorn/internal/codegen/Lower.java	Thu May 23 09:49:03 2013 -0300
+++ b/nashorn/src/jdk/nashorn/internal/codegen/Lower.java	Thu May 23 15:51:08 2013 +0200
@@ -80,7 +80,7 @@
  * finalized.
  */
 
-final class Lower extends NodeOperatorVisitor {
+final class Lower extends NodeOperatorVisitor<BlockLexicalContext> {
 
     private static final DebugLogger LOG = new DebugLogger("lower");
 
@@ -105,7 +105,7 @@
                             terminated = true;
                         }
                     } else {
-                        statement.accept(new NodeVisitor() {
+                        statement.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
                             @Override
                             public boolean enterVarNode(final VarNode varNode) {
                                 newStatements.add(varNode.setInit(null));
@@ -121,7 +121,6 @@
 
     @Override
     public boolean enterBlock(final Block block) {
-        final LexicalContext lc = getLexicalContext();
         final FunctionNode   function = lc.getCurrentFunction();
         if (lc.isFunctionBody() && function.isProgram() && !function.hasDeclaredFunctions()) {
             new ExecuteNode(block.getLineNumber(), block.getToken(), block.getFinish(), LiteralNode.newInstance(block, ScriptRuntime.UNDEFINED)).accept(this);
@@ -134,12 +133,10 @@
         //now we have committed the entire statement list to the block, but we need to truncate
         //whatever is after the last terminal. block append won't append past it
 
-        final BlockLexicalContext lc = (BlockLexicalContext)getLexicalContext();
-
         Statement last = lc.getLastStatement();
 
         if (lc.isFunctionBody()) {
-            final FunctionNode currentFunction = getLexicalContext().getCurrentFunction();
+            final FunctionNode currentFunction = lc.getCurrentFunction();
             final boolean isProgram = currentFunction.isProgram();
             final ReturnNode returnNode = new ReturnNode(
                 last == null ? block.getLineNumber() : last.getLineNumber(), //TODO?
@@ -191,7 +188,7 @@
         final Node expr = executeNode.getExpression();
         ExecuteNode node = executeNode;
 
-        final FunctionNode currentFunction = getLexicalContext().getCurrentFunction();
+        final FunctionNode currentFunction = lc.getCurrentFunction();
 
         if (currentFunction.isProgram()) {
             if (!(expr instanceof Block) || expr instanceof FunctionNode) { // it's not a block, but can be a function
@@ -216,7 +213,7 @@
 
         final Node  test = forNode.getTest();
         if (!forNode.isForIn() && conservativeAlwaysTrue(test)) {
-            newForNode = forNode.setTest(getLexicalContext(), null);
+            newForNode = forNode.setTest(lc, null);
         }
 
         return addStatement(checkEscape(newForNode));
@@ -230,7 +227,7 @@
     @Override
     public Node leaveFunctionNode(final FunctionNode functionNode) {
         LOG.info("END FunctionNode: ", functionNode.getName());
-        return functionNode.setState(getLexicalContext(), CompilationState.LOWERED);
+        return functionNode.setState(lc, CompilationState.LOWERED);
     }
 
     @Override
@@ -262,16 +259,16 @@
     }
 
     private static Node ensureUniqueNamesIn(final LexicalContext lc, final Node node) {
-        return node.accept(new NodeVisitor() {
+        return node.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
             @Override
             public Node leaveFunctionNode(final FunctionNode functionNode) {
                 final String name = functionNode.getName();
-                return functionNode.setName(getLexicalContext(), lc.getCurrentFunction().uniqueName(name));
+                return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
             }
 
             @Override
             public Node leaveDefault(final Node labelledNode) {
-                return labelledNode.ensureUniqueLabels(getLexicalContext());
+                return labelledNode.ensureUniqueLabels(lc);
             }
         });
     }
@@ -292,10 +289,10 @@
         final long token      = tryNode.getToken();
         final int  finish     = tryNode.getFinish();
 
-        final IdentNode exception = new IdentNode(token, finish, getLexicalContext().getCurrentFunction().uniqueName("catch_all"));
+        final IdentNode exception = new IdentNode(token, finish, lc.getCurrentFunction().uniqueName("catch_all"));
 
         final Block catchBody = new Block(lineNumber, token, finish, new ThrowNode(lineNumber, token, finish, new IdentNode(exception), ThrowNode.IS_SYNTHETIC_RETHROW)).
-                setIsTerminal(getLexicalContext(), true); //ends with throw, so terminal
+                setIsTerminal(lc, true); //ends with throw, so terminal
 
         final CatchNode catchAllNode  = new CatchNode(lineNumber, token, finish, new IdentNode(exception), null, catchBody, CatchNode.IS_SYNTHETIC_RETHROW);
         final Block     catchAllBlock = new Block(lineNumber, token, finish, catchAllNode);
@@ -306,7 +303,7 @@
     }
 
     private IdentNode compilerConstant(final CompilerConstants cc) {
-        final FunctionNode functionNode = getLexicalContext().getCurrentFunction();
+        final FunctionNode functionNode = lc.getCurrentFunction();
         return new IdentNode(functionNode.getToken(), functionNode.getFinish(), cc.symbolName());
     }
 
@@ -324,9 +321,8 @@
     private Node spliceFinally(final TryNode tryNode, final List<ThrowNode> rethrows, final Block finallyBody) {
         assert tryNode.getFinallyBody() == null;
         final int            finish = tryNode.getFinish();
-        final LexicalContext lc     = getLexicalContext();
 
-        final TryNode newTryNode = (TryNode)tryNode.accept(new NodeVisitor() {
+        final TryNode newTryNode = (TryNode)tryNode.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
             final List<Node> insideTry = new ArrayList<>();
 
             @Override
@@ -355,12 +351,12 @@
 
             @Override
             public Node leaveBreakNode(final BreakNode breakNode) {
-                return copy(breakNode, Lower.this.getLexicalContext().getBreakable(breakNode.getLabel()));
+                return copy(breakNode, Lower.this.lc.getBreakable(breakNode.getLabel()));
             }
 
             @Override
             public Node leaveContinueNode(final ContinueNode continueNode) {
-                return copy(continueNode, Lower.this.getLexicalContext().getContinueTo(continueNode.getLabel()));
+                return copy(continueNode, Lower.this.lc.getContinueTo(continueNode.getLabel()));
             }
 
             @Override
@@ -383,7 +379,7 @@
                     newStatements.add(expr == null ? returnNode : returnNode.setExpression(resultNode));
                 }
 
-                return new ExecuteNode(returnNode.getLineNumber(), returnNode.getToken(), returnNode.getFinish(), new Block(returnNode.getLineNumber(), returnNode.getToken(), getLexicalContext().getCurrentBlock().getFinish(), newStatements));
+                return new ExecuteNode(returnNode.getLineNumber(), returnNode.getToken(), returnNode.getFinish(), new Block(returnNode.getLineNumber(), returnNode.getToken(), lc.getCurrentBlock().getFinish(), newStatements));
             }
 
             private Node copy(final Statement endpoint, final Node targetNode) {
@@ -442,7 +438,7 @@
         final Block catchAll = catchAllBlock(tryNode);
 
         final List<ThrowNode> rethrows = new ArrayList<>();
-        catchAll.accept(new NodeVisitor() {
+        catchAll.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
             @Override
             public boolean enterThrowNode(final ThrowNode throwNode) {
                 rethrows.add(throwNode);
@@ -470,7 +466,7 @@
     @Override
     public Node leaveVarNode(final VarNode varNode) {
         addStatement(varNode);
-        if (varNode.getFlag(VarNode.IS_LAST_FUNCTION_DECLARATION) && getLexicalContext().getCurrentFunction().isProgram()) {
+        if (varNode.getFlag(VarNode.IS_LAST_FUNCTION_DECLARATION) && lc.getCurrentFunction().isProgram()) {
             new ExecuteNode(varNode.getLineNumber(), varNode.getToken(), varNode.getFinish(), new IdentNode(varNode.getName())).accept(this);
         }
         return varNode;
@@ -484,7 +480,7 @@
         if (conservativeAlwaysTrue(test)) {
             //turn it into a for node without a test.
             final ForNode forNode = (ForNode)new ForNode(whileNode.getLineNumber(), whileNode.getToken(), whileNode.getFinish(), null, null, body, null, ForNode.IS_FOR).accept(this);
-            getLexicalContext().replace(whileNode, forNode);
+            lc.replace(whileNode, forNode);
             return forNode;
         }
 
@@ -519,7 +515,7 @@
      * @return eval location
      */
     private String evalLocation(final IdentNode node) {
-        final Source source = getLexicalContext().getCurrentFunction().getSource();
+        final Source source = lc.getCurrentFunction().getSource();
         return new StringBuilder().
             append(source.getName()).
             append('#').
@@ -551,10 +547,10 @@
 
             // 'eval' call with at least one argument
             if (args.size() >= 1 && EVAL.symbolName().equals(callee.getName())) {
-                final FunctionNode currentFunction = getLexicalContext().getCurrentFunction();
+                final FunctionNode currentFunction = lc.getCurrentFunction();
                 return callNode.setEvalArgs(
                     new CallNode.EvalArgs(
-                        ensureUniqueNamesIn(getLexicalContext(), args.get(0)).accept(this),
+                        ensureUniqueNamesIn(lc, args.get(0)).accept(this),
                         compilerConstant(THIS),
                         evalLocation(callee),
                         currentFunction.isStrict()));
@@ -580,7 +576,7 @@
     private static boolean controlFlowEscapes(final LexicalContext lex, final Block loopBody) {
         final List<Node> escapes = new ArrayList<>();
 
-        loopBody.accept(new NodeVisitor() {
+        loopBody.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
             @Override
             public Node leaveBreakNode(final BreakNode node) {
                 escapes.add(node);
@@ -601,7 +597,6 @@
     }
 
     private LoopNode checkEscape(final LoopNode loopNode) {
-        final LexicalContext lc = getLexicalContext();
         final boolean escapes = controlFlowEscapes(lc, loopNode.getBody());
         if (escapes) {
             return loopNode.
@@ -613,7 +608,7 @@
 
 
     private Node addStatement(final Statement statement) {
-        ((BlockLexicalContext)getLexicalContext()).appendStatement(statement);
+        lc.appendStatement(statement);
         return statement;
     }