jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor Java Examples

The following examples show how to use jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FoldConstants.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #2
Source File: ApplySpecialization.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #3
Source File: Splitter.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #4
Source File: FoldConstants.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #5
Source File: ApplySpecialization.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #6
Source File: Splitter.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #7
Source File: FoldConstants.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #8
Source File: ApplySpecialization.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #9
Source File: Splitter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #10
Source File: FoldConstants.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #11
Source File: ApplySpecialization.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #12
Source File: Splitter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #13
Source File: FoldConstants.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #14
Source File: ApplySpecialization.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #15
Source File: Splitter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #16
Source File: Splitter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static List<FunctionNode> directChildren(final FunctionNode functionNode) {
    final List<FunctionNode> dc = new ArrayList<>();
    functionNode.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterFunctionNode(final FunctionNode child) {
            if (child == functionNode) {
                return true;
            }
            if (lc.getParentFunction(child) == functionNode) {
                dc.add(child);
            }
            return false;
        }
    });
    return dc;
}
 
Example #17
Source File: FoldConstants.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * When we eliminate dead code, we must preserve var declarations as they are scoped to the whole
 * function. This method gathers var nodes from code passed to it, removing their initializers.
 *
 * @param deadCodeRoot the root node of eliminated dead code
 * @param statements a list that will be receiving the var nodes from the dead code, with their
 * initializers removed.
 */
static void extractVarNodesFromDeadCode(final Node deadCodeRoot, final List<Statement> statements) {
    deadCodeRoot.accept(new SimpleNodeVisitor() {
        @Override
        public boolean enterVarNode(final VarNode varNode) {
            statements.add(varNode.setInit(null));
            return false;
        }

        @Override
        public boolean enterFunctionNode(final FunctionNode functionNode) {
            // Don't descend into nested functions
            return false;
        }
    });
}
 
Example #18
Source File: ApplySpecialization.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private boolean hasApplies(final FunctionNode functionNode) {
    try {
        functionNode.accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode fn) {
                return fn == functionNode;
            }

            @Override
            public boolean enterCallNode(final CallNode callNode) {
                if (isApply(callNode)) {
                    throw HAS_APPLIES;
                }
                return true;
            }
        });
    } catch (final AppliesFoundException e) {
        return true;
    }

    log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do.");
    return false; // no applies
}
 
Example #19
Source File: AssignSymbols.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Define symbols for all variable declarations at the top of the function scope. This way we can get around
 * problems like
 *
 * while (true) {
 *   break;
 *   if (true) {
 *     var s;
 *   }
 * }
 *
 * to an arbitrary nesting depth.
 *
 * see NASHORN-73
 *
 * @param functionNode the FunctionNode we are entering
 * @param body the body of the FunctionNode we are entering
 */
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
    // This visitor will assign symbol to all declared variables.
    body.accept(new SimpleNodeVisitor() {
        @Override
        protected boolean enterDefault(final Node node) {
            // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
            // This will also prevent visiting nested functions (as FunctionNode is an expression).
            return !(node instanceof Expression);
        }

        @Override
        public Node leaveVarNode(final VarNode varNode) {
            final IdentNode ident  = varNode.getName();
            final boolean blockScoped = varNode.isBlockScoped();
            if (blockScoped && lc.inUnprotectedSwitchContext()) {
                throwUnprotectedSwitchError(varNode);
            }
            final Block block = blockScoped ? lc.getCurrentBlock() : body;
            final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
            if (varNode.isFunctionDeclaration()) {
                symbol.setIsFunctionDeclaration();
            }
            return varNode.setName(ident.setSymbol(symbol));
        }
    });
}
 
Example #20
Source File: AssignSymbols.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Define symbols for all variable declarations at the top of the function scope. This way we can get around
 * problems like
 *
 * while (true) {
 *   break;
 *   if (true) {
 *     var s;
 *   }
 * }
 *
 * to an arbitrary nesting depth.
 *
 * see NASHORN-73
 *
 * @param functionNode the FunctionNode we are entering
 * @param body the body of the FunctionNode we are entering
 */
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
    // This visitor will assign symbol to all declared variables.
    body.accept(new SimpleNodeVisitor() {
        @Override
        protected boolean enterDefault(final Node node) {
            // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
            // This will also prevent visiting nested functions (as FunctionNode is an expression).
            return !(node instanceof Expression);
        }

        @Override
        public Node leaveVarNode(final VarNode varNode) {
            final IdentNode ident  = varNode.getName();
            final boolean blockScoped = varNode.isBlockScoped();
            if (blockScoped && lc.inUnprotectedSwitchContext()) {
                throwUnprotectedSwitchError(varNode);
            }
            final Block block = blockScoped ? lc.getCurrentBlock() : body;
            final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
            if (varNode.isFunctionDeclaration()) {
                symbol.setIsFunctionDeclaration();
            }
            return varNode.setName(ident.setSymbol(symbol));
        }
    });
}
 
Example #21
Source File: Lower.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Node> T ensureUniqueNamesIn(final T node) {
    return (T)node.accept(new SimpleNodeVisitor() {
        @Override
        public Node leaveFunctionNode(final FunctionNode functionNode) {
            final String name = functionNode.getName();
            return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
        }

        @Override
        public Node leaveDefault(final Node labelledNode) {
            return labelledNode.ensureUniqueLabels(lc);
        }
    });
}
 
Example #22
Source File: CompilationPhase.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
FunctionNode transform(final Compiler compiler, final CompilationPhases phases, final FunctionNode fn) {
    // It's not necessary to guard the marking of symbols as locals with this "if" condition for
    // correctness, it's just an optimization -- runtime type calculation is not used when the compilation
    // is not an on-demand optimistic compilation, so we can skip locals marking then.
    if (compiler.useOptimisticTypes() && compiler.isOnDemandCompilation()) {
        fn.getBody().accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode functionNode) {
                // OTOH, we must not declare symbols from nested functions to be locals. As we're doing on-demand
                // compilation, and we're skipping parsing the function bodies for nested functions, this
                // basically only means their parameters. It'd be enough to mistakenly declare to be a local a
                // symbol in the outer function named the same as one of the parameters, though.
                return false;
            };
            @Override
            public boolean enterBlock(final Block block) {
                for (final Symbol symbol: block.getSymbols()) {
                    if (!symbol.isScope()) {
                        compiler.declareLocalSymbol(symbol.getName());
                    }
                }
                return true;
            };
        });
    }
    return fn;
}
 
Example #23
Source File: Lower.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Node> T ensureUniqueNamesIn(final T node) {
    return (T)node.accept(new SimpleNodeVisitor() {
        @Override
        public Node leaveFunctionNode(final FunctionNode functionNode) {
            final String name = functionNode.getName();
            return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
        }

        @Override
        public Node leaveDefault(final Node labelledNode) {
            return labelledNode.ensureUniqueLabels(lc);
        }
    });
}
 
Example #24
Source File: CompilationPhase.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
FunctionNode transform(final Compiler compiler, final CompilationPhases phases, final FunctionNode fn) {
    // It's not necessary to guard the marking of symbols as locals with this "if" condition for
    // correctness, it's just an optimization -- runtime type calculation is not used when the compilation
    // is not an on-demand optimistic compilation, so we can skip locals marking then.
    if (compiler.useOptimisticTypes() && compiler.isOnDemandCompilation()) {
        fn.getBody().accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode functionNode) {
                // OTOH, we must not declare symbols from nested functions to be locals. As we're doing on-demand
                // compilation, and we're skipping parsing the function bodies for nested functions, this
                // basically only means their parameters. It'd be enough to mistakenly declare to be a local a
                // symbol in the outer function named the same as one of the parameters, though.
                return false;
            };
            @Override
            public boolean enterBlock(final Block block) {
                for (final Symbol symbol: block.getSymbols()) {
                    if (!symbol.isScope()) {
                        compiler.declareLocalSymbol(symbol.getName());
                    }
                }
                return true;
            };
        });
    }
    return fn;
}
 
Example #25
Source File: AssignSymbols.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Define symbols for all variable declarations at the top of the function scope. This way we can get around
 * problems like
 *
 * while (true) {
 *   break;
 *   if (true) {
 *     var s;
 *   }
 * }
 *
 * to an arbitrary nesting depth.
 *
 * see NASHORN-73
 *
 * @param functionNode the FunctionNode we are entering
 * @param body the body of the FunctionNode we are entering
 */
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
    // This visitor will assign symbol to all declared variables.
    body.accept(new SimpleNodeVisitor() {
        @Override
        protected boolean enterDefault(final Node node) {
            // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
            // This will also prevent visiting nested functions (as FunctionNode is an expression).
            return !(node instanceof Expression);
        }

        @Override
        public Node leaveVarNode(final VarNode varNode) {
            final IdentNode ident  = varNode.getName();
            final boolean blockScoped = varNode.isBlockScoped();
            if (blockScoped && lc.inUnprotectedSwitchContext()) {
                throwUnprotectedSwitchError(varNode);
            }
            final Block block = blockScoped ? lc.getCurrentBlock() : body;
            final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
            if (varNode.isFunctionDeclaration()) {
                symbol.setIsFunctionDeclaration();
            }
            return varNode.setName(ident.setSymbol(symbol));
        }
    });
}
 
Example #26
Source File: CompilationPhase.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
FunctionNode transform(final Compiler compiler, final CompilationPhases phases, final FunctionNode fn) {
    // It's not necessary to guard the marking of symbols as locals with this "if" condition for
    // correctness, it's just an optimization -- runtime type calculation is not used when the compilation
    // is not an on-demand optimistic compilation, so we can skip locals marking then.
    if (compiler.useOptimisticTypes() && compiler.isOnDemandCompilation()) {
        fn.getBody().accept(new SimpleNodeVisitor() {
            @Override
            public boolean enterFunctionNode(final FunctionNode functionNode) {
                // OTOH, we must not declare symbols from nested functions to be locals. As we're doing on-demand
                // compilation, and we're skipping parsing the function bodies for nested functions, this
                // basically only means their parameters. It'd be enough to mistakenly declare to be a local a
                // symbol in the outer function named the same as one of the parameters, though.
                return false;
            };
            @Override
            public boolean enterBlock(final Block block) {
                for (final Symbol symbol: block.getSymbols()) {
                    if (!symbol.isScope()) {
                        compiler.declareLocalSymbol(symbol.getName());
                    }
                }
                return true;
            };
        });
    }
    return fn;
}
 
Example #27
Source File: Lower.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Node> T ensureUniqueNamesIn(final T node) {
    return (T)node.accept(new SimpleNodeVisitor() {
        @Override
        public Node leaveFunctionNode(final FunctionNode functionNode) {
            final String name = functionNode.getName();
            return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
        }

        @Override
        public Node leaveDefault(final Node labelledNode) {
            return labelledNode.ensureUniqueLabels(lc);
        }
    });
}
 
Example #28
Source File: AssignSymbols.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Define symbols for all variable declarations at the top of the function scope. This way we can get around
 * problems like
 *
 * while (true) {
 *   break;
 *   if (true) {
 *     var s;
 *   }
 * }
 *
 * to an arbitrary nesting depth.
 *
 * see NASHORN-73
 *
 * @param functionNode the FunctionNode we are entering
 * @param body the body of the FunctionNode we are entering
 */
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
    // This visitor will assign symbol to all declared variables.
    body.accept(new SimpleNodeVisitor() {
        @Override
        protected boolean enterDefault(final Node node) {
            // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
            // This will also prevent visiting nested functions (as FunctionNode is an expression).
            return !(node instanceof Expression);
        }

        @Override
        public Node leaveVarNode(final VarNode varNode) {
            final IdentNode ident  = varNode.getName();
            final boolean blockScoped = varNode.isBlockScoped();
            if (blockScoped && lc.inUnprotectedSwitchContext()) {
                throwUnprotectedSwitchError(varNode);
            }
            final Block block = blockScoped ? lc.getCurrentBlock() : body;
            final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
            if (varNode.isFunctionDeclaration()) {
                symbol.setIsFunctionDeclaration();
            }
            return varNode.setName(ident.setSymbol(symbol));
        }
    });
}
 
Example #29
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Define symbols for all variable declarations at the top of the function scope. This way we can get around
 * problems like
 *
 * while (true) {
 *   break;
 *   if (true) {
 *     var s;
 *   }
 * }
 *
 * to an arbitrary nesting depth.
 *
 * see NASHORN-73
 *
 * @param functionNode the FunctionNode we are entering
 * @param body the body of the FunctionNode we are entering
 */
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
    // This visitor will assign symbol to all declared variables.
    body.accept(new SimpleNodeVisitor() {
        @Override
        protected boolean enterDefault(final Node node) {
            // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
            // This will also prevent visiting nested functions (as FunctionNode is an expression).
            return !(node instanceof Expression);
        }

        @Override
        public Node leaveVarNode(final VarNode varNode) {
            final IdentNode ident  = varNode.getName();
            final boolean blockScoped = varNode.isBlockScoped();
            if (blockScoped && lc.inUnprotectedSwitchContext()) {
                throwUnprotectedSwitchError(varNode);
            }
            final Block block = blockScoped ? lc.getCurrentBlock() : body;
            final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
            if (varNode.isFunctionDeclaration()) {
                symbol.setIsFunctionDeclaration();
            }
            return varNode.setName(ident.setSymbol(symbol));
        }
    });
}
 
Example #30
Source File: Lower.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Node> T ensureUniqueNamesIn(final T node) {
    return (T)node.accept(new SimpleNodeVisitor() {
        @Override
        public Node leaveFunctionNode(final FunctionNode functionNode) {
            final String name = functionNode.getName();
            return functionNode.setName(lc, lc.getCurrentFunction().uniqueName(name));
        }

        @Override
        public Node leaveDefault(final Node labelledNode) {
            return labelledNode.ensureUniqueLabels(lc);
        }
    });
}