src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java
author attila
Thu, 14 Dec 2017 13:42:59 +0100
changeset 48334 fdefa410d655
parent 47216 71c04702a3d5
child 48354 c96d4c720995
permissions -rw-r--r--
8193296: Parser should not eagerly transform delete expressions Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     1
/*
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     4
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    10
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    15
 * accompanied this code).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    16
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    20
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    23
 * questions.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    24
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    25
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    26
package jdk.nashorn.internal.codegen;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    27
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.ARGUMENTS;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.ARGUMENTS_VAR;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    30
import static jdk.nashorn.internal.codegen.CompilerConstants.CALLEE;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    31
import static jdk.nashorn.internal.codegen.CompilerConstants.EXCEPTION_PREFIX;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    32
import static jdk.nashorn.internal.codegen.CompilerConstants.ITERATOR_PREFIX;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    33
import static jdk.nashorn.internal.codegen.CompilerConstants.RETURN;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    34
import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    35
import static jdk.nashorn.internal.codegen.CompilerConstants.SWITCH_TAG_PREFIX;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    36
import static jdk.nashorn.internal.codegen.CompilerConstants.THIS;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    37
import static jdk.nashorn.internal.codegen.CompilerConstants.VARARGS;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    38
import static jdk.nashorn.internal.ir.Symbol.HAS_OBJECT_VALUE;
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    39
import static jdk.nashorn.internal.ir.Symbol.IS_CONST;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    40
import static jdk.nashorn.internal.ir.Symbol.IS_FUNCTION_SELF;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    41
import static jdk.nashorn.internal.ir.Symbol.IS_GLOBAL;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    42
import static jdk.nashorn.internal.ir.Symbol.IS_INTERNAL;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    43
import static jdk.nashorn.internal.ir.Symbol.IS_LET;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    44
import static jdk.nashorn.internal.ir.Symbol.IS_PARAM;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    45
import static jdk.nashorn.internal.ir.Symbol.IS_PROGRAM_LEVEL;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    46
import static jdk.nashorn.internal.ir.Symbol.IS_SCOPE;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    47
import static jdk.nashorn.internal.ir.Symbol.IS_THIS;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    48
import static jdk.nashorn.internal.ir.Symbol.IS_VAR;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    49
import static jdk.nashorn.internal.ir.Symbol.KINDMASK;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    50
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    51
import java.util.ArrayDeque;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    52
import java.util.ArrayList;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    53
import java.util.Deque;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    54
import java.util.HashMap;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    55
import java.util.HashSet;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    56
import java.util.Iterator;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    57
import java.util.List;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    58
import java.util.ListIterator;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    59
import java.util.Map;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    60
import java.util.Set;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    61
import jdk.nashorn.internal.ir.AccessNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    62
import jdk.nashorn.internal.ir.BinaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    63
import jdk.nashorn.internal.ir.Block;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    64
import jdk.nashorn.internal.ir.CatchNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    65
import jdk.nashorn.internal.ir.Expression;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    66
import jdk.nashorn.internal.ir.ForNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    67
import jdk.nashorn.internal.ir.FunctionNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    68
import jdk.nashorn.internal.ir.IdentNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    69
import jdk.nashorn.internal.ir.IndexNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    70
import jdk.nashorn.internal.ir.LexicalContextNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    71
import jdk.nashorn.internal.ir.LiteralNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    72
import jdk.nashorn.internal.ir.Node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    73
import jdk.nashorn.internal.ir.RuntimeNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    74
import jdk.nashorn.internal.ir.RuntimeNode.Request;
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 32692
diff changeset
    75
import jdk.nashorn.internal.ir.Splittable;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    76
import jdk.nashorn.internal.ir.Statement;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    77
import jdk.nashorn.internal.ir.SwitchNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    78
import jdk.nashorn.internal.ir.Symbol;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    79
import jdk.nashorn.internal.ir.TryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    80
import jdk.nashorn.internal.ir.UnaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    81
import jdk.nashorn.internal.ir.VarNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    82
import jdk.nashorn.internal.ir.WithNode;
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32781
diff changeset
    83
import jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor;
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29281
diff changeset
    84
import jdk.nashorn.internal.parser.TokenType;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    85
import jdk.nashorn.internal.runtime.Context;
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    86
import jdk.nashorn.internal.runtime.ECMAErrors;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    87
import jdk.nashorn.internal.runtime.ErrorManager;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    88
import jdk.nashorn.internal.runtime.JSErrorType;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    89
import jdk.nashorn.internal.runtime.ParserException;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
    90
import jdk.nashorn.internal.runtime.Source;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    91
import jdk.nashorn.internal.runtime.logging.DebugLogger;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    92
import jdk.nashorn.internal.runtime.logging.Loggable;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    93
import jdk.nashorn.internal.runtime.logging.Logger;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    94
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    95
/**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    96
 * This visitor assigns symbols to identifiers denoting variables. It does few more minor calculations that are only
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    97
 * possible after symbols have been assigned; such is the transformation of "delete" and "typeof" operators into runtime
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    98
 * nodes and counting of number of properties assigned to "this" in constructor functions. This visitor is also notable
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    99
 * for what it doesn't do, most significantly it does no type calculations as in JavaScript variables can change types
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   100
 * during runtime and as such symbols don't have types. Calculation of expression types is performed by a separate
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   101
 * visitor.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   102
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   103
@Logger(name="symbols")
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32781
diff changeset
   104
final class AssignSymbols extends SimpleNodeVisitor implements Loggable {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   105
    private final DebugLogger log;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   106
    private final boolean     debug;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   107
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   108
    private static boolean isParamOrVar(final IdentNode identNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   109
        final Symbol symbol = identNode.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   110
        return symbol.isParam() || symbol.isVar();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   111
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   112
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   113
    private static String name(final Node node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   114
        final String cn = node.getClass().getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   115
        final int lastDot = cn.lastIndexOf('.');
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   116
        if (lastDot == -1) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   117
            return cn;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   118
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   119
        return cn.substring(lastDot + 1);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   120
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   121
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   122
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   123
     * Checks if various symbols that were provisionally marked as needing a slot ended up unused, and marks them as not
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   124
     * needing a slot after all.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   125
     * @param functionNode the function node
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   126
     * @return the passed in node, for easy chaining
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   127
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   128
    private static FunctionNode removeUnusedSlots(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   129
        if (!functionNode.needsCallee()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   130
            functionNode.compilerConstant(CALLEE).setNeedsSlot(false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   131
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   132
        if (!(functionNode.hasScopeBlock() || functionNode.needsParentScope())) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   133
            functionNode.compilerConstant(SCOPE).setNeedsSlot(false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   134
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   135
        // Named function expressions that end up not referencing themselves won't need a local slot for the self symbol.
27822
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   136
        if(functionNode.isNamedFunctionExpression() && !functionNode.usesSelfSymbol()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   137
            final Symbol selfSymbol = functionNode.getBody().getExistingSymbol(functionNode.getIdent().getName());
27822
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   138
            if(selfSymbol != null && selfSymbol.isFunctionSelf()) {
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   139
                selfSymbol.setNeedsSlot(false);
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   140
                selfSymbol.clearFlag(Symbol.IS_VAR);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   141
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   142
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   143
        return functionNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   144
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   145
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   146
    private final Deque<Set<String>> thisProperties = new ArrayDeque<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   147
    private final Map<String, Symbol> globalSymbols = new HashMap<>(); //reuse the same global symbol
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   148
    private final Compiler compiler;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   149
    private final boolean isOnDemand;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   150
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   151
    public AssignSymbols(final Compiler compiler) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   152
        this.compiler = compiler;
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   153
        this.log   = initLogger(compiler.getContext());
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   154
        this.debug = log.isEnabled();
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   155
        this.isOnDemand = compiler.isOnDemandCompilation();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   156
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   157
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   158
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   159
    public DebugLogger getLogger() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   160
        return log;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   161
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   162
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   163
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   164
    public DebugLogger initLogger(final Context context) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   165
        return context.getLogger(this.getClass());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   166
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   167
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   168
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   169
     * Define symbols for all variable declarations at the top of the function scope. This way we can get around
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   170
     * problems like
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   171
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   172
     * while (true) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   173
     *   break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   174
     *   if (true) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   175
     *     var s;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   176
     *   }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   177
     * }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   178
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   179
     * to an arbitrary nesting depth.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   180
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   181
     * see NASHORN-73
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   182
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   183
     * @param functionNode the FunctionNode we are entering
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   184
     * @param body the body of the FunctionNode we are entering
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   185
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   186
    private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
27817
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   187
        // This visitor will assign symbol to all declared variables.
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32781
diff changeset
   188
        body.accept(new SimpleNodeVisitor() {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   189
            @Override
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   190
            protected boolean enterDefault(final Node node) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   191
                // Don't bother visiting expressions; var is a statement, it can't be inside an expression.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   192
                // This will also prevent visiting nested functions (as FunctionNode is an expression).
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   193
                return !(node instanceof Expression);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   194
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   195
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   196
            @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   197
            public Node leaveVarNode(final VarNode varNode) {
27817
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   198
                final IdentNode ident  = varNode.getName();
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   199
                final boolean blockScoped = varNode.isBlockScoped();
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   200
                if (blockScoped && lc.inUnprotectedSwitchContext()) {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   201
                    throwUnprotectedSwitchError(varNode);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   202
                }
27817
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   203
                final Block block = blockScoped ? lc.getCurrentBlock() : body;
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   204
                final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   205
                if (varNode.isFunctionDeclaration()) {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   206
                    symbol.setIsFunctionDeclaration();
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   207
                }
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
   208
                return varNode.setName(ident.setSymbol(symbol));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   209
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   210
        });
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   211
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   212
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   213
    private IdentNode compilerConstantIdentifier(final CompilerConstants cc) {
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25238
diff changeset
   214
        return createImplicitIdentifier(cc.symbolName()).setSymbol(lc.getCurrentFunction().compilerConstant(cc));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   215
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   216
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   217
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   218
     * Creates an ident node for an implicit identifier within the function (one not declared in the script source
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   219
     * code). These identifiers are defined with function's token and finish.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   220
     * @param name the name of the identifier
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   221
     * @return an ident node representing the implicit identifier.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   222
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   223
    private IdentNode createImplicitIdentifier(final String name) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   224
        final FunctionNode fn = lc.getCurrentFunction();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   225
        return new IdentNode(fn.getToken(), fn.getFinish(), name);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   226
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   227
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   228
    private Symbol createSymbol(final String name, final int flags) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   229
        if ((flags & Symbol.KINDMASK) == IS_GLOBAL) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   230
            //reuse global symbols so they can be hashed
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   231
            Symbol global = globalSymbols.get(name);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   232
            if (global == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   233
                global = new Symbol(name, flags);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   234
                globalSymbols.put(name, global);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   235
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   236
            return global;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   237
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   238
        return new Symbol(name, flags);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   239
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   240
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   241
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   242
     * Creates a synthetic initializer for a variable (a var statement that doesn't occur in the source code). Typically
32534
b3ec7f3b3c2a 8136349: Typos patch for nashorn sources submitted on Sep 10, 2015
sundar
parents: 32435
diff changeset
   243
     * used to create assignment of {@code :callee} to the function name symbol in self-referential function
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   244
     * expressions as well as for assignment of {@code :arguments} to {@code arguments}.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   245
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   246
     * @param name the ident node identifying the variable to initialize
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   247
     * @param initConstant the compiler constant it is initialized to
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   248
     * @param fn the function node the assignment is for
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   249
     * @return a var node with the appropriate assignment
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   250
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   251
    private VarNode createSyntheticInitializer(final IdentNode name, final CompilerConstants initConstant, final FunctionNode fn) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   252
        final IdentNode init = compilerConstantIdentifier(initConstant);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   253
        assert init.getSymbol() != null && init.getSymbol().isBytecodeLocal();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   254
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   255
        final VarNode synthVar = new VarNode(fn.getLineNumber(), fn.getToken(), fn.getFinish(), name, init);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   256
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   257
        final Symbol nameSymbol = fn.getBody().getExistingSymbol(name.getName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   258
        assert nameSymbol != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   259
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25238
diff changeset
   260
        return (VarNode)synthVar.setName(name.setSymbol(nameSymbol)).accept(this);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   261
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   262
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   263
    private FunctionNode createSyntheticInitializers(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   264
        final List<VarNode> syntheticInitializers = new ArrayList<>(2);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   265
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   266
        // Must visit the new var nodes in the context of the body. We could also just set the new statements into the
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   267
        // block and then revisit the entire block, but that seems to be too much double work.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   268
        final Block body = functionNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   269
        lc.push(body);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   270
        try {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   271
            if (functionNode.usesSelfSymbol()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   272
                // "var fn = :callee"
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   273
                syntheticInitializers.add(createSyntheticInitializer(functionNode.getIdent(), CALLEE, functionNode));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   274
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   275
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   276
            if (functionNode.needsArguments()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   277
                // "var arguments = :arguments"
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   278
                syntheticInitializers.add(createSyntheticInitializer(createImplicitIdentifier(ARGUMENTS_VAR.symbolName()),
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   279
                        ARGUMENTS, functionNode));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   280
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   281
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   282
            if (syntheticInitializers.isEmpty()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   283
                return functionNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   284
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   285
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   286
            for(final ListIterator<VarNode> it = syntheticInitializers.listIterator(); it.hasNext();) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   287
                it.set((VarNode)it.next().accept(this));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   288
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   289
        } finally {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   290
            lc.pop(body);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   291
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   292
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   293
        final List<Statement> stmts = body.getStatements();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   294
        final List<Statement> newStatements = new ArrayList<>(stmts.size() + syntheticInitializers.size());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   295
        newStatements.addAll(syntheticInitializers);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   296
        newStatements.addAll(stmts);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   297
        return functionNode.setBody(lc, body.setStatements(lc, newStatements));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   298
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   299
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   300
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   301
     * Defines a new symbol in the given block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   302
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   303
     * @param block        the block in which to define the symbol
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   304
     * @param name         name of symbol.
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   305
     * @param origin       origin node
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   306
     * @param symbolFlags  Symbol flags.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   307
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   308
     * @return Symbol for given name or null for redefinition.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   309
     */
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   310
    private Symbol defineSymbol(final Block block, final String name, final Node origin, final int symbolFlags) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   311
        int    flags  = symbolFlags;
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   312
        final boolean isBlockScope = (flags & IS_LET) != 0 || (flags & IS_CONST) != 0;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   313
        final boolean isGlobal     = (flags & KINDMASK) == IS_GLOBAL;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   314
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   315
        Symbol symbol;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   316
        final FunctionNode function;
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   317
        if (isBlockScope) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   318
            // block scoped variables always live in current block, no need to look for existing symbols in parent blocks.
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   319
            symbol = block.getExistingSymbol(name);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   320
            function = lc.getCurrentFunction();
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   321
        } else {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   322
            symbol = findSymbol(block, name);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   323
            function = lc.getFunction(block);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   324
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   325
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   326
        // Global variables are implicitly always scope variables too.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   327
        if (isGlobal) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   328
            flags |= IS_SCOPE;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   329
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   330
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   331
        if (lc.getCurrentFunction().isProgram()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   332
            flags |= IS_PROGRAM_LEVEL;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   333
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   334
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   335
        final boolean isParam = (flags & KINDMASK) == IS_PARAM;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   336
        final boolean isVar =   (flags & KINDMASK) == IS_VAR;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   337
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   338
        if (symbol != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   339
            // Symbol was already defined. Check if it needs to be redefined.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   340
            if (isParam) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   341
                if (!isLocal(function, symbol)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   342
                    // Not defined in this function. Create a new definition.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   343
                    symbol = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   344
                } else if (symbol.isParam()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   345
                    // Duplicate parameter. Null return will force an error.
41924
d55f24e8953e 8156619: Unimplemented ES6 features should result in clear Error being thrown
hannesw
parents: 36696
diff changeset
   346
                    throwParserException(ECMAErrors.getMessage("syntax.error.duplicate.parameter", name), origin);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   347
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   348
            } else if (isVar) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   349
                if (isBlockScope) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   350
                    // Check redeclaration in same block
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   351
                    if (symbol.hasBeenDeclared()) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   352
                        throwParserException(ECMAErrors.getMessage("syntax.error.redeclare.variable", name), origin);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   353
                    } else {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   354
                        symbol.setHasBeenDeclared();
27814
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27206
diff changeset
   355
                        // Set scope flag on top-level block scoped symbols
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27206
diff changeset
   356
                        if (function.isProgram() && function.getBody() == block) {
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27206
diff changeset
   357
                            symbol.setIsScope();
96427359f4fe 8057691: Nashorn: let & const declarations are not shared between scripts
hannesw
parents: 27206
diff changeset
   358
                        }
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   359
                    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   360
                } else if ((flags & IS_INTERNAL) != 0) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   361
                    // Always create a new definition.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   362
                    symbol = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   363
                } else {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   364
                    // Found LET or CONST in parent scope of same function - s SyntaxError
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   365
                    if (symbol.isBlockScoped() && isLocal(lc.getCurrentFunction(), symbol)) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   366
                        throwParserException(ECMAErrors.getMessage("syntax.error.redeclare.variable", name), origin);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   367
                    }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   368
                    // Not defined in this function. Create a new definition.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   369
                    if (!isLocal(function, symbol) || symbol.less(IS_VAR)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   370
                        symbol = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   371
                    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   372
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   373
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   374
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   375
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   376
        if (symbol == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   377
            // If not found, then create a new one.
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   378
            final Block symbolBlock;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   379
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   380
            // Determine where to create it.
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   381
            if (isVar && ((flags & IS_INTERNAL) != 0 || isBlockScope)) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   382
                symbolBlock = block; //internal vars are always defined in the block closest to them
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   383
            } else if (isGlobal) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   384
                symbolBlock = lc.getOutermostFunction().getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   385
            } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   386
                symbolBlock = lc.getFunctionBody(function);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   387
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   388
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   389
            // Create and add to appropriate block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   390
            symbol = createSymbol(name, flags);
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   391
            symbolBlock.putSymbol(symbol);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   392
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   393
            if ((flags & IS_SCOPE) == 0) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   394
                // Initial assumption; symbol can lose its slot later
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   395
                symbol.setNeedsSlot(true);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   396
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   397
        } else if (symbol.less(flags)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   398
            symbol.setFlags(flags);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   399
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   400
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   401
        return symbol;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   402
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   403
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   404
    private <T extends Node> T end(final T node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   405
        return end(node, true);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   406
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   407
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   408
    private <T extends Node> T end(final T node, final boolean printNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   409
        if (debug) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   410
            final StringBuilder sb = new StringBuilder();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   411
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   412
            sb.append("[LEAVE ").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   413
                append(name(node)).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   414
                append("] ").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   415
                append(printNode ? node.toString() : "").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   416
                append(" in '").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   417
                append(lc.getCurrentFunction().getName()).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   418
                append('\'');
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   419
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   420
            if (node instanceof IdentNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   421
                final Symbol symbol = ((IdentNode)node).getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   422
                if (symbol == null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   423
                    sb.append(" <NO SYMBOL>");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   424
                } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   425
                    sb.append(" <symbol=").append(symbol).append('>');
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   426
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   427
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   428
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   429
            log.unindent();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   430
            log.info(sb);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   431
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   432
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   433
        return node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   434
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   435
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   436
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   437
    public boolean enterBlock(final Block block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   438
        start(block);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   439
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   440
        if (lc.isFunctionBody()) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   441
            assert !block.hasSymbols();
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   442
            final FunctionNode fn = lc.getCurrentFunction();
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   443
            if (isUnparsedFunction(fn)) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   444
                // It's a skipped nested function. Just mark the symbols being used by it as being in use.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   445
                for(final String name: compiler.getScriptFunctionData(fn.getId()).getExternalSymbolNames()) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   446
                    nameIsUsed(name, null);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   447
                }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   448
                // Don't bother descending into it, it must be empty anyway.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   449
                assert block.getStatements().isEmpty();
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   450
                return false;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   451
            }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   452
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   453
            enterFunctionBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   454
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   455
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   456
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   457
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   458
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   459
    private boolean isUnparsedFunction(final FunctionNode fn) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   460
        return isOnDemand && fn != lc.getOutermostFunction();
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   461
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   462
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   463
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   464
    public boolean enterCatchNode(final CatchNode catchNode) {
41983
eb674141ab03 8156615: Catch parameter can be a BindingPattern in ES6 mode
sdama
parents: 41924
diff changeset
   465
        final IdentNode exception = catchNode.getExceptionIdentifier();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   466
        final Block     block     = lc.getCurrentBlock();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   467
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   468
        start(catchNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   469
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   470
        // define block-local exception variable
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   471
        final String exname = exception.getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   472
        // If the name of the exception starts with ":e", this is a synthetic catch block, likely a catch-all. Its
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   473
        // symbol is naturally internal, and should be treated as such.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   474
        final boolean isInternal = exname.startsWith(EXCEPTION_PREFIX.symbolName());
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   475
        // IS_LET flag is required to make sure symbol is not visible outside catch block. However, we need to
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   476
        // clear the IS_LET flag after creation to allow redefinition of symbol inside the catch block.
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   477
        final Symbol symbol = defineSymbol(block, exname, catchNode, IS_VAR | IS_LET | (isInternal ? IS_INTERNAL : 0) | HAS_OBJECT_VALUE);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   478
        symbol.clearFlag(IS_LET);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   479
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   480
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   481
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   482
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   483
    private void enterFunctionBody() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   484
        final FunctionNode functionNode = lc.getCurrentFunction();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   485
        final Block body = lc.getCurrentBlock();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   486
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   487
        initFunctionWideVariables(functionNode, body);
27822
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   488
        acceptDeclarations(functionNode, body);
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   489
        defineFunctionSelfSymbol(functionNode, body);
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   490
    }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   491
27822
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   492
    private void defineFunctionSelfSymbol(final FunctionNode functionNode, final Block body) {
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   493
        // Function self-symbol is only declared as a local variable for named function expressions. Declared functions
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   494
        // don't need it as they are local variables in their declaring scope.
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   495
        if (!functionNode.isNamedFunctionExpression()) {
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   496
            return;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   497
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   498
27822
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   499
        final String name = functionNode.getIdent().getName();
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   500
        assert name != null; // As it's a named function expression.
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   501
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   502
        if (body.getExistingSymbol(name) != null) {
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   503
            // Body already has a declaration for the name. It's either a parameter "function x(x)" or a
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   504
            // top-level variable "function x() { ... var x; ... }".
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   505
            return;
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   506
        }
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   507
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   508
        defineSymbol(body, name, functionNode, IS_VAR | IS_FUNCTION_SELF | HAS_OBJECT_VALUE);
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   509
        if(functionNode.allVarsInScope()) { // basically, has deep eval
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   510
            // We must conservatively presume that eval'd code can dynamically use the function symbol.
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   511
            lc.setFlag(functionNode, FunctionNode.USES_SELF_SYMBOL);
a3866b9fb44d 8066222: too strong assertion on function expression names
attila
parents: 27817
diff changeset
   512
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   513
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   514
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   515
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   516
    public boolean enterFunctionNode(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   517
        start(functionNode, false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   518
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   519
        thisProperties.push(new HashSet<String>());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   520
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   521
        // Every function has a body, even the ones skipped on reparse (they have an empty one). We're
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   522
        // asserting this as even for those, enterBlock() must be invoked to correctly process symbols that
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   523
        // are used in them.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   524
        assert functionNode.getBody() != null;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   525
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   526
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   527
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   528
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   529
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   530
    public boolean enterVarNode(final VarNode varNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   531
        start(varNode);
27100
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   532
        // Normally, a symbol assigned in a var statement is not live for its RHS. Since we also represent function
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   533
        // declarations as VarNodes, they are exception to the rule, as they need to have the symbol visible to the
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   534
        // body of the declared function for self-reference.
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   535
        if (varNode.isFunctionDeclaration()) {
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   536
            defineVarIdent(varNode);
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   537
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   538
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   539
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   540
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   541
    @Override
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   542
    public Node leaveVarNode(final VarNode varNode) {
27100
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   543
        if (!varNode.isFunctionDeclaration()) {
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   544
            defineVarIdent(varNode);
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   545
        }
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   546
        return super.leaveVarNode(varNode);
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   547
    }
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   548
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   549
    private void defineVarIdent(final VarNode varNode) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   550
        final IdentNode ident = varNode.getName();
27100
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   551
        final int flags;
31485
be1acaee8e9e 8114838: Anonymous functions escape to surrounding scope when defined under "with" statement
sundar
parents: 30392
diff changeset
   552
        if (!varNode.isBlockScoped() && lc.getCurrentFunction().isProgram()) {
27100
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   553
            flags = IS_SCOPE;
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   554
        } else {
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   555
            flags = 0;
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   556
        }
44341993b5c1 8059842: Creating symbols for declared functions shouldn't be a special case
attila
parents: 26503
diff changeset
   557
        defineSymbol(lc.getCurrentBlock(), ident.getName(), ident, varNode.getSymbolFlags() | flags);
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   558
    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   559
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   560
    private Symbol exceptionSymbol() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   561
        return newObjectInternal(EXCEPTION_PREFIX);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   562
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   563
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   564
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   565
     * This has to run before fix assignment types, store any type specializations for
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   566
     * parameters, then turn them into objects for the generic version of this method.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   567
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   568
     * @param functionNode functionNode
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   569
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   570
    private FunctionNode finalizeParameters(final FunctionNode functionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   571
        final List<IdentNode> newParams = new ArrayList<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   572
        final boolean isVarArg = functionNode.isVarArg();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   573
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   574
        final Block body = functionNode.getBody();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   575
        for (final IdentNode param : functionNode.getParameters()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   576
            final Symbol paramSymbol = body.getExistingSymbol(param.getName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   577
            assert paramSymbol != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   578
            assert paramSymbol.isParam() : paramSymbol + " " + paramSymbol.getFlags();
25244
627d7e86f3b5 8047357: More precise synthetic return + unreachable throw
attila
parents: 25238
diff changeset
   579
            newParams.add(param.setSymbol(paramSymbol));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   580
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   581
            // parameters should not be slots for a function that uses variable arity signature
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   582
            if (isVarArg) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   583
                paramSymbol.setNeedsSlot(false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   584
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   585
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   586
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   587
        return functionNode.setParameters(lc, newParams);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   588
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   589
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   590
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   591
     * Search for symbol in the lexical context starting from the given block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   592
     * @param name Symbol name.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   593
     * @return Found symbol or null if not found.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   594
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   595
    private Symbol findSymbol(final Block block, final String name) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   596
        for (final Iterator<Block> blocks = lc.getBlocks(block); blocks.hasNext();) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   597
            final Symbol symbol = blocks.next().getExistingSymbol(name);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   598
            if (symbol != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   599
                return symbol;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   600
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   601
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   602
        return null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   603
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   604
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   605
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   606
     * Marks the current function as one using any global symbol. The function and all its parent functions will all be
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   607
     * marked as needing parent scope.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   608
     * @see FunctionNode#needsParentScope()
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   609
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   610
    private void functionUsesGlobalSymbol() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   611
        for (final Iterator<FunctionNode> fns = lc.getFunctions(); fns.hasNext();) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   612
            lc.setFlag(fns.next(), FunctionNode.USES_ANCESTOR_SCOPE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   613
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   614
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   615
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   616
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   617
     * Marks the current function as one using a scoped symbol. The block defining the symbol will be marked as needing
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   618
     * its own scope to hold the variable. If the symbol is defined outside of the current function, it and all
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   619
     * functions up to (but not including) the function containing the defining block will be marked as needing parent
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   620
     * function scope.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   621
     * @see FunctionNode#needsParentScope()
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   622
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   623
    private void functionUsesScopeSymbol(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   624
        final String name = symbol.getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   625
        for (final Iterator<LexicalContextNode> contextNodeIter = lc.getAllNodes(); contextNodeIter.hasNext(); ) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   626
            final LexicalContextNode node = contextNodeIter.next();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   627
            if (node instanceof Block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   628
                final Block block = (Block)node;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   629
                if (block.getExistingSymbol(name) != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   630
                    assert lc.contains(block);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   631
                    lc.setBlockNeedsScope(block);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   632
                    break;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   633
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   634
            } else if (node instanceof FunctionNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   635
                lc.setFlag(node, FunctionNode.USES_ANCESTOR_SCOPE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   636
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   637
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   638
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   639
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   640
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   641
     * Declares that the current function is using the symbol.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   642
     * @param symbol the symbol used by the current function.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   643
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   644
    private void functionUsesSymbol(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   645
        assert symbol != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   646
        if (symbol.isScope()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   647
            if (symbol.isGlobal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   648
                functionUsesGlobalSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   649
            } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   650
                functionUsesScopeSymbol(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   651
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   652
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   653
            assert !symbol.isGlobal(); // Every global is also scope
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   654
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   655
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   656
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   657
    private void initCompileConstant(final CompilerConstants cc, final Block block, final int flags) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   658
        defineSymbol(block, cc.symbolName(), null, flags).setNeedsSlot(true);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   659
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   660
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   661
    private void initFunctionWideVariables(final FunctionNode functionNode, final Block body) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   662
        initCompileConstant(CALLEE, body, IS_PARAM | IS_INTERNAL | HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   663
        initCompileConstant(THIS, body, IS_PARAM | IS_THIS | HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   664
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   665
        if (functionNode.isVarArg()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   666
            initCompileConstant(VARARGS, body, IS_PARAM | IS_INTERNAL | HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   667
            if (functionNode.needsArguments()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   668
                initCompileConstant(ARGUMENTS, body, IS_VAR | IS_INTERNAL | HAS_OBJECT_VALUE);
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   669
                defineSymbol(body, ARGUMENTS_VAR.symbolName(), null, IS_VAR | HAS_OBJECT_VALUE);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   670
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   671
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   672
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   673
        initParameters(functionNode, body);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   674
        initCompileConstant(SCOPE, body, IS_VAR | IS_INTERNAL | HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   675
        initCompileConstant(RETURN, body, IS_VAR | IS_INTERNAL);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   676
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   677
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   678
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   679
     * Initialize parameters for function node.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   680
     * @param functionNode the function node
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   681
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   682
    private void initParameters(final FunctionNode functionNode, final Block body) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   683
        final boolean isVarArg = functionNode.isVarArg();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   684
        final boolean scopeParams = functionNode.allVarsInScope() || isVarArg;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   685
        for (final IdentNode param : functionNode.getParameters()) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   686
            final Symbol symbol = defineSymbol(body, param.getName(), param, IS_PARAM);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   687
            if(scopeParams) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   688
                // NOTE: this "set is scope" is a poor substitute for clear expression of where the symbol is stored.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   689
                // It will force creation of scopes where they would otherwise not necessarily be needed (functions
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   690
                // using arguments object and other variable arity functions). Tracked by JDK-8038942.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   691
                symbol.setIsScope();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   692
                assert symbol.hasSlot();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   693
                if(isVarArg) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   694
                    symbol.setNeedsSlot(false);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   695
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   696
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   697
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   698
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   699
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   700
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   701
     * Is the symbol local to (that is, defined in) the specified function?
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   702
     * @param function the function
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   703
     * @param symbol the symbol
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   704
     * @return true if the symbol is defined in the specified function
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   705
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   706
    private boolean isLocal(final FunctionNode function, final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   707
        final FunctionNode definingFn = lc.getDefiningFunction(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   708
        assert definingFn != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   709
        return definingFn == function;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   710
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   711
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   712
    @Override
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   713
    public Node leaveBinaryNode(final BinaryNode binaryNode) {
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29281
diff changeset
   714
        if (binaryNode.isTokenType(TokenType.ASSIGN)) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   715
            return leaveASSIGN(binaryNode);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   716
        }
30392
dc4a419b2982 8079362: Enforce best practices for Node token API usage
attila
parents: 29281
diff changeset
   717
        return super.leaveBinaryNode(binaryNode);
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   718
    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   719
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   720
    private Node leaveASSIGN(final BinaryNode binaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   721
        // If we're assigning a property of the this object ("this.foo = ..."), record it.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   722
        final Expression lhs = binaryNode.lhs();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   723
        if (lhs instanceof AccessNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   724
            final AccessNode accessNode = (AccessNode) lhs;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   725
            final Expression base = accessNode.getBase();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   726
            if (base instanceof IdentNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   727
                final Symbol symbol = ((IdentNode)base).getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   728
                if(symbol.isThis()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   729
                    thisProperties.peek().add(accessNode.getProperty());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   730
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   731
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   732
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   733
        return binaryNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   734
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   735
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   736
    @Override
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   737
    public Node leaveUnaryNode(final UnaryNode unaryNode) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   738
        switch (unaryNode.tokenType()) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   739
        case DELETE:
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   740
            return leaveDELETE(unaryNode);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   741
        case TYPEOF:
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   742
            return leaveTYPEOF(unaryNode);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   743
        default:
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   744
            return super.leaveUnaryNode(unaryNode);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   745
        }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   746
    }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   747
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   748
    private Node leaveDELETE(final UnaryNode unaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   749
        final FunctionNode currentFunctionNode = lc.getCurrentFunction();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   750
        final boolean      strictMode          = currentFunctionNode.isStrict();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   751
        final Expression   rhs                 = unaryNode.getExpression();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   752
        final Expression   strictFlagNode      = (Expression)LiteralNode.newInstance(unaryNode, strictMode).accept(this);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   753
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   754
        Request request = Request.DELETE;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   755
        final List<Expression> args = new ArrayList<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   756
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   757
        if (rhs instanceof IdentNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   758
            final IdentNode ident = (IdentNode)rhs;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   759
            // If this is a declared variable or a function parameter, delete always fails (except for globals).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   760
            final String name = ident.getName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   761
            final Symbol symbol = ident.getSymbol();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   762
31828
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31485
diff changeset
   763
            if (symbol.isThis()) {
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31485
diff changeset
   764
                // Can't delete "this", ignore and return true
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   765
                return LiteralNode.newInstance(unaryNode, true);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   766
            }
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   767
            final Expression literalNode = LiteralNode.newInstance(unaryNode, name);
31828
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31485
diff changeset
   768
            final boolean failDelete = strictMode || (!symbol.isScope() && (symbol.isParam() || (symbol.isVar() && !symbol.isProgramLevel())));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   769
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   770
            if (!failDelete) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   771
                args.add(compilerConstantIdentifier(SCOPE));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   772
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   773
            args.add(literalNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   774
            args.add(strictFlagNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   775
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   776
            if (failDelete) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   777
                request = Request.FAIL_DELETE;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   778
            } else if ((symbol.isGlobal() && !symbol.isFunctionDeclaration()) || symbol.isProgramLevel()) {
31828
50189d0dacb0 8131683: Delete fails over multiple scopes
hannesw
parents: 31485
diff changeset
   779
                request = Request.SLOW_DELETE;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   780
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   781
        } else if (rhs instanceof AccessNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   782
            final Expression base     = ((AccessNode)rhs).getBase();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   783
            final String     property = ((AccessNode)rhs).getProperty();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   784
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   785
            args.add(base);
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   786
            args.add(LiteralNode.newInstance(unaryNode, property));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   787
            args.add(strictFlagNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   788
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   789
        } else if (rhs instanceof IndexNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   790
            final IndexNode indexNode = (IndexNode)rhs;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   791
            final Expression base  = indexNode.getBase();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   792
            final Expression index = indexNode.getIndex();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   793
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   794
            args.add(base);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   795
            args.add(index);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   796
            args.add(strictFlagNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   797
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   798
        } else {
48334
fdefa410d655 8193296: Parser should not eagerly transform delete expressions
attila
parents: 47216
diff changeset
   799
            throw new AssertionError("Unexpected delete with " + rhs.getClass().getName() + " expression");
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   800
        }
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   801
        return new RuntimeNode(unaryNode, request, args);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   802
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   803
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   804
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   805
    public Node leaveForNode(final ForNode forNode) {
36696
39ff39c8e396 8151700: Add support for ES6 for-of
hannesw
parents: 32888
diff changeset
   806
        if (forNode.isForInOrOf()) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   807
            return forNode.setIterator(lc, newObjectInternal(ITERATOR_PREFIX)); //NASHORN-73
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   808
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   809
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   810
        return end(forNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   811
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   812
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   813
    @Override
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   814
    public Node leaveFunctionNode(final FunctionNode functionNode) {
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   815
        final FunctionNode finalizedFunction;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   816
        if (isUnparsedFunction(functionNode)) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   817
            finalizedFunction = functionNode;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   818
        } else {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   819
            finalizedFunction =
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   820
               markProgramBlock(
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   821
               removeUnusedSlots(
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   822
               createSyntheticInitializers(
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   823
               finalizeParameters(
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   824
                       lc.applyTopFlags(functionNode))))
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   825
                       .setThisProperties(lc, thisProperties.pop().size()));
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   826
        }
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32534
diff changeset
   827
        return finalizedFunction;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   828
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   829
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   830
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   831
    public Node leaveIdentNode(final IdentNode identNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   832
        if (identNode.isPropertyName()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   833
            return identNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   834
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   835
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   836
        final Symbol symbol = nameIsUsed(identNode.getName(), identNode);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   837
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   838
        if (!identNode.isInitializedHere()) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   839
            symbol.increaseUseCount();
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   840
        }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   841
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   842
        IdentNode newIdentNode = identNode.setSymbol(symbol);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   843
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   844
        // If a block-scoped var is used before its declaration mark it as dead.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   845
        // We can only statically detect this for local vars, cross-function symbols require runtime checks.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   846
        if (symbol.isBlockScoped() && !symbol.hasBeenDeclared() && !identNode.isDeclaredHere() && isLocal(lc.getCurrentFunction(), symbol)) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   847
            newIdentNode = newIdentNode.markDead();
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   848
        }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   849
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   850
        return end(newIdentNode);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   851
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   852
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   853
    private Symbol nameIsUsed(final String name, final IdentNode origin) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   854
        final Block block = lc.getCurrentBlock();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   855
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   856
        Symbol symbol = findSymbol(block, name);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   857
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   858
        //If an existing symbol with the name is found, use that otherwise, declare a new one
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   859
        if (symbol != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   860
            log.info("Existing symbol = ", symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   861
            if (symbol.isFunctionSelf()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   862
                final FunctionNode functionNode = lc.getDefiningFunction(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   863
                assert functionNode != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   864
                assert lc.getFunctionBody(functionNode).getExistingSymbol(CALLEE.symbolName()) != null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   865
                lc.setFlag(functionNode, FunctionNode.USES_SELF_SYMBOL);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   866
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   867
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   868
            // if symbol is non-local or we're in a with block, we need to put symbol in scope (if it isn't already)
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   869
            maybeForceScope(symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   870
        } else {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   871
            log.info("No symbol exists. Declare as global: ", name);
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   872
            symbol = defineSymbol(block, name, origin, IS_GLOBAL | IS_SCOPE);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   873
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   874
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   875
        functionUsesSymbol(symbol);
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   876
        return symbol;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   877
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   878
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   879
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   880
    public Node leaveSwitchNode(final SwitchNode switchNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   881
        // We only need a symbol for the tag if it's not an integer switch node
27970
7b0048b90967 8066225: NPE in MethodEmitter with duplicate integer switch cases
attila
parents: 27822
diff changeset
   882
        if(!switchNode.isUniqueInteger()) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   883
            return switchNode.setTag(lc, newObjectInternal(SWITCH_TAG_PREFIX));
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   884
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   885
        return switchNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   886
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   887
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   888
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   889
    public Node leaveTryNode(final TryNode tryNode) {
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27970
diff changeset
   890
        assert tryNode.getFinallyBody() == null;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   891
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   892
        end(tryNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   893
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   894
        return tryNode.setException(lc, exceptionSymbol());
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   895
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   896
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   897
    private Node leaveTYPEOF(final UnaryNode unaryNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   898
        final Expression rhs = unaryNode.getExpression();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   899
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   900
        final List<Expression> args = new ArrayList<>();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   901
        if (rhs instanceof IdentNode && !isParamOrVar((IdentNode)rhs)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   902
            args.add(compilerConstantIdentifier(SCOPE));
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   903
            args.add(LiteralNode.newInstance(rhs, ((IdentNode)rhs).getName())); //null
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   904
        } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   905
            args.add(rhs);
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   906
            args.add(LiteralNode.newInstance(unaryNode)); //null, do not reuse token of identifier rhs, it can be e.g. 'this'
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   907
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   908
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   909
        final Node runtimeNode = new RuntimeNode(unaryNode, Request.TYPEOF, args);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   910
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   911
        end(unaryNode);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   912
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   913
        return runtimeNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   914
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   915
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   916
    private FunctionNode markProgramBlock(final FunctionNode functionNode) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31828
diff changeset
   917
        if (isOnDemand || !functionNode.isProgram()) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   918
            return functionNode;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   919
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   920
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   921
        return functionNode.setBody(lc, functionNode.getBody().setFlag(lc, Block.IS_GLOBAL_SCOPE));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   922
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   923
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   924
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   925
     * If the symbol isn't already a scope symbol, but it needs to be (see {@link #symbolNeedsToBeScope(Symbol)}, it is
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   926
     * promoted to a scope symbol and its block marked as needing a scope.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   927
     * @param symbol the symbol that might be scoped
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   928
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   929
    private void maybeForceScope(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   930
        if (!symbol.isScope() && symbolNeedsToBeScope(symbol)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   931
            Symbol.setSymbolIsScope(lc, symbol);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   932
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   933
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   934
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   935
    private Symbol newInternal(final CompilerConstants cc, final int flags) {
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   936
        return defineSymbol(lc.getCurrentBlock(), lc.getCurrentFunction().uniqueName(cc.symbolName()), null, IS_VAR | IS_INTERNAL | flags); //NASHORN-73
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   937
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   938
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   939
    private Symbol newObjectInternal(final CompilerConstants cc) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   940
        return newInternal(cc, HAS_OBJECT_VALUE);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   941
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   942
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   943
    private boolean start(final Node node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   944
        return start(node, true);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   945
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   946
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   947
    private boolean start(final Node node, final boolean printNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   948
        if (debug) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   949
            final StringBuilder sb = new StringBuilder();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   950
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   951
            sb.append("[ENTER ").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   952
                append(name(node)).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   953
                append("] ").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   954
                append(printNode ? node.toString() : "").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   955
                append(" in '").
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   956
                append(lc.getCurrentFunction().getName()).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   957
                append("'");
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   958
            log.info(sb);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   959
            log.indent();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   960
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   961
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   962
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   963
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   964
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   965
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   966
     * Determines if the symbol has to be a scope symbol. In general terms, it has to be a scope symbol if it can only
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   967
     * be reached from the current block by traversing a function node, a split node, or a with node.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   968
     * @param symbol the symbol checked for needing to be a scope symbol
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   969
     * @return true if the symbol has to be a scope symbol.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   970
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   971
    private boolean symbolNeedsToBeScope(final Symbol symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   972
        if (symbol.isThis() || symbol.isInternal()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   973
            return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   974
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   975
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   976
        final FunctionNode func = lc.getCurrentFunction();
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
   977
        if ( func.allVarsInScope() || (!symbol.isBlockScoped() && func.isProgram())) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   978
            return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   979
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   980
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   981
        boolean previousWasBlock = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   982
        for (final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   983
            final LexicalContextNode node = it.next();
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 32692
diff changeset
   984
            if (node instanceof FunctionNode || isSplitLiteral(node)) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   985
                // We reached the function boundary or a splitting boundary without seeing a definition for the symbol.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   986
                // It needs to be in scope.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   987
                return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   988
            } else if (node instanceof WithNode) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   989
                if (previousWasBlock) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   990
                    // We reached a WithNode; the symbol must be scoped. Note that if the WithNode was not immediately
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   991
                    // preceded by a block, this means we're currently processing its expression, not its body,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   992
                    // therefore it doesn't count.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   993
                    return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   994
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   995
                previousWasBlock = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   996
            } else if (node instanceof Block) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   997
                if (((Block)node).getExistingSymbol(symbol.getName()) == symbol) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   998
                    // We reached the block that defines the symbol without reaching either the function boundary, or a
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   999
                    // WithNode. The symbol need not be scoped.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1000
                    return false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1001
                }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1002
                previousWasBlock = true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1003
            } else {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1004
                previousWasBlock = false;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1005
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1006
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1007
        throw new AssertionError();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1008
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1009
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 32692
diff changeset
  1010
    private static boolean isSplitLiteral(final LexicalContextNode expr) {
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 32692
diff changeset
  1011
        return expr instanceof Splittable && ((Splittable) expr).getSplitRanges() != null;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1012
    }
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1013
27817
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1014
    private void throwUnprotectedSwitchError(final VarNode varNode) {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1015
        // Block scoped declarations in switch statements without explicit blocks should be declared
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1016
        // in a common block that contains all the case clauses. We cannot support this without a
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1017
        // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1018
        // directly contained by switch node). As a temporary solution we throw a reference error here.
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1019
        final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1020
        throwParserException(msg, varNode);
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1021
    }
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27814
diff changeset
  1022
26377
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1023
    private void throwParserException(final String message, final Node origin) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1024
        if (origin == null) {
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1025
            throw new ParserException(message);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1026
        }
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1027
        final Source source = compiler.getSource();
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1028
        final long token = origin.getToken();
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1029
        final int line = source.getLine(origin.getStart());
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1030
        final int column = source.getColumn(origin.getStart());
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1031
        final String formatted = ErrorManager.format(message, source, line, column, token);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1032
        throw new ParserException(JSErrorType.SYNTAX_ERROR, formatted, source, line, column, token);
028dad61662f 8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents: 26252
diff changeset
  1033
    }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
  1034
}