nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/LexicalContext.java
author attila
Wed, 28 Jan 2015 17:58:08 +0100
changeset 28690 78317797ab62
parent 27817 56f6161c3e55
child 30701 1a25e71187ff
permissions -rw-r--r--
8067139: Finally blocks inlined incorrectly Reviewed-by: hannesw, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     1
/*
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     4
 *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    10
 *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    15
 * accompanied this code).
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    16
 *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    20
 *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    23
 * questions.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    24
 */
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    25
package jdk.nashorn.internal.ir;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    26
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    27
import java.io.File;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    28
import java.util.Iterator;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    29
import java.util.NoSuchElementException;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    30
import jdk.nashorn.internal.runtime.Debug;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    31
import jdk.nashorn.internal.runtime.Source;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    32
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    33
/**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    34
 * A class that tracks the current lexical context of node visitation as a stack of {@link Block} nodes. Has special
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    35
 * methods to retrieve useful subsets of the context.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    36
 *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    37
 * This is implemented with a primitive array and a stack pointer, because it really makes a difference
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    38
 * performance wise. None of the collection classes were optimal
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    39
 */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    40
public class LexicalContext {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    41
    private LexicalContextNode[] stack;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    42
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    43
    private int[] flags;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    44
    private int sp;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    45
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    46
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    47
     * Creates a new empty lexical context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    48
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    49
    public LexicalContext() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    50
        stack = new LexicalContextNode[16];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    51
        flags = new int[16];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    52
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    53
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    54
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    55
     * Set the flags for a lexical context node on the stack. Does not
18853
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    56
     * replace the flags, but rather adds to them.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    57
     *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    58
     * @param node  node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    59
     * @param flag  new flag to set
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    60
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    61
    public void setFlag(final LexicalContextNode node, final int flag) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    62
        if (flag != 0) {
18853
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    63
            // Use setBlockNeedsScope() instead
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    64
            assert !(flag == Block.NEEDS_SCOPE && node instanceof Block);
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    65
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    66
            for (int i = sp - 1; i >= 0; i--) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    67
                if (stack[i] == node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    68
                    flags[i] |= flag;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    69
                    return;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    70
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    71
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    72
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
    73
        assert false;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    74
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    75
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
    76
    /**
18853
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    77
     * Marks the block as one that creates a scope. Note that this method must
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    78
     * be used instead of {@link #setFlag(LexicalContextNode, int)} with
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    79
     * {@link Block#NEEDS_SCOPE} because it atomically also sets the
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    80
     * {@link FunctionNode#HAS_SCOPE_BLOCK} flag on the block's containing
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    81
     * function.
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    82
     * @param block the block that needs to be marked as creating a scope.
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    83
     */
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    84
    public void setBlockNeedsScope(final Block block) {
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    85
        for (int i = sp - 1; i >= 0; i--) {
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    86
            if (stack[i] == block) {
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    87
                flags[i] |= Block.NEEDS_SCOPE;
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    88
                for(int j = i - 1; j >=0; j --) {
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    89
                    if(stack[j] instanceof FunctionNode) {
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    90
                        flags[j] |= FunctionNode.HAS_SCOPE_BLOCK;
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    91
                        return;
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    92
                    }
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    93
                }
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    94
            }
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    95
        }
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    96
        assert false;
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    97
    }
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    98
25ba8264b427 8019819: scope symbol didn't get a slot in certain cases
attila
parents: 17756
diff changeset
    99
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   100
     * Get the flags for a lexical context node on the stack
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   101
     * @param node node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   102
     * @return the flags for the node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   103
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   104
    public int getFlags(final LexicalContextNode node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   105
        for (int i = sp - 1; i >= 0; i--) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   106
            if (stack[i] == node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   107
                return flags[i];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   108
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   109
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   110
        throw new AssertionError("flag node not on context stack");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   111
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   112
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   113
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   114
     * Get the function body of a function node on the lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   115
     * stack. This will trigger an assertion if node isn't present
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   116
     * @param functionNode function node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   117
     * @return body of function node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   118
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   119
    public Block getFunctionBody(final FunctionNode functionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   120
        for (int i = sp - 1; i >= 0 ; i--) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   121
            if (stack[i] == functionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   122
                return (Block)stack[i + 1];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   123
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   124
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   125
        throw new AssertionError(functionNode.getName() + " not on context stack");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   126
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   127
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   128
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   129
     * Return all nodes in the LexicalContext
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   130
     * @return all nodes
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   131
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   132
    public Iterator<LexicalContextNode> getAllNodes() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   133
        return new NodeIterator<>(LexicalContextNode.class);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   134
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   135
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   136
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   137
     * Returns the outermost function in this context. It is either the program, or a lazily compiled function.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   138
     * @return the outermost function in this context.
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   139
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   140
    public FunctionNode getOutermostFunction() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   141
        return (FunctionNode)stack[0];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   142
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   143
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   144
    /**
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   145
     * Pushes a new block on top of the context, making it the innermost open block.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   146
     * @param node the new node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   147
     * @return the node that was pushed
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   148
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   149
    public <T extends LexicalContextNode> T push(final T node) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   150
        assert !contains(node);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   151
        if (sp == stack.length) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   152
            final LexicalContextNode[] newStack = new LexicalContextNode[sp * 2];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   153
            System.arraycopy(stack, 0, newStack, 0, sp);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   154
            stack = newStack;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   155
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   156
            final int[] newFlags = new int[sp * 2];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   157
            System.arraycopy(flags, 0, newFlags, 0, sp);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   158
            flags = newFlags;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   159
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   160
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   161
        stack[sp] = node;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   162
        flags[sp] = 0;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   163
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   164
        sp++;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   165
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   166
        return node;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   167
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   168
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   169
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   170
     * Is the context empty?
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   171
     * @return true if empty
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   172
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   173
    public boolean isEmpty() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   174
        return sp == 0;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   175
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   176
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   177
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   178
     * The depth of the lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   179
     * @return depth
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   180
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   181
    public int size() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   182
        return sp;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   183
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   184
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   185
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   186
     * Pops the innermost block off the context and all nodes that has been contributed
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   187
     * since it was put there
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   188
     *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   189
     * @param node the node expected to be popped, used to detect unbalanced pushes/pops
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   190
     * @return the node that was popped
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   191
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   192
    @SuppressWarnings("unchecked")
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   193
    public <T extends Node> T pop(final T node) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   194
        --sp;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   195
        final LexicalContextNode popped = stack[sp];
17249
a2014831ae7a 8013325: function named 'arguments' should set DEFINES_ARGUMENTS flag in its parent, not itself
attila
parents: 17233
diff changeset
   196
        stack[sp] = null;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   197
        if (popped instanceof Flags) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   198
            return (T)((Flags<?>)popped).setFlag(this, flags[sp]);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   199
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   200
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   201
        return (T)popped;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   202
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   203
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   204
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   205
     * Explicitly apply flags to the topmost element on the stack. This is only valid to use from a
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   206
     * {@code NodeVisitor.leaveXxx()} method and only on the node being exited at the time. It is not mandatory to use,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   207
     * as {@link #pop(LexicalContextNode)} will apply the flags automatically, but this method can be used to apply them
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   208
     * during the {@code leaveXxx()} method in case its logic depends on the value of the flags.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   209
     * @param node the node to apply the flags to. Must be the topmost node on the stack.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   210
     * @return the passed in node, or a modified node (if any flags were modified)
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   211
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   212
    public <T extends LexicalContextNode & Flags<T>> T applyTopFlags(final T node) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   213
        assert node == peek();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   214
        return node.setFlag(this, flags[sp - 1]);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   215
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   216
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   217
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   218
     * Return the top element in the context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   219
     * @return the node that was pushed last
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   220
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   221
    public LexicalContextNode peek() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   222
        return stack[sp - 1];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   223
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   224
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   225
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   226
     * Check if a node is in the lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   227
     * @param node node to check for
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   228
     * @return true if in the context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   229
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   230
    public boolean contains(final LexicalContextNode node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   231
        for (int i = 0; i < sp; i++) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   232
            if (stack[i] == node) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   233
                return true;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   234
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   235
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   236
        return false;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   237
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   238
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   239
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   240
     * Replace a node on the lexical context with a new one. Normally
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   241
     * you should try to engineer IR traversals so this isn't needed
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   242
     *
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   243
     * @param oldNode old node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   244
     * @param newNode new node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   245
     * @return the new node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   246
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   247
    public LexicalContextNode replace(final LexicalContextNode oldNode, final LexicalContextNode newNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   248
        for (int i = sp - 1; i >= 0; i--) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   249
            if (stack[i] == oldNode) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   250
                assert i == sp - 1 : "violation of contract - we always expect to find the replacement node on top of the lexical context stack: " + newNode + " has " + stack[i + 1].getClass() + " above it";
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   251
                stack[i] = newNode;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   252
                break;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   253
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   254
         }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   255
        return newNode;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   256
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   257
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   258
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   259
     * Returns an iterator over all blocks in the context, with the top block (innermost lexical context) first.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   260
     * @return an iterator over all blocks in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   261
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   262
    public Iterator<Block> getBlocks() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   263
        return new NodeIterator<>(Block.class);
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   264
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   265
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   266
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   267
     * Returns an iterator over all functions in the context, with the top (innermost open) function first.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   268
     * @return an iterator over all functions in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   269
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   270
    public Iterator<FunctionNode> getFunctions() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   271
        return new NodeIterator<>(FunctionNode.class);
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   272
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   273
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   274
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   275
     * Get the parent block for the current lexical context block
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   276
     * @return parent block
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   277
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   278
    public Block getParentBlock() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   279
        final Iterator<Block> iter = new NodeIterator<>(Block.class, getCurrentFunction());
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   280
        iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   281
        return iter.hasNext() ? iter.next() : null;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   282
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   283
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   284
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   285
     * Gets the label node of the current block.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   286
     * @return the label node of the current block, if it is labeled. Otherwise returns null.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   287
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   288
    public LabelNode getCurrentBlockLabelNode() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   289
        assert stack[sp - 1] instanceof Block;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   290
        if(sp < 2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   291
            return null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   292
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   293
        final LexicalContextNode parent = stack[sp - 2];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   294
        return parent instanceof LabelNode ? (LabelNode)parent : null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   295
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   296
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   297
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   298
    /*
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   299
    public FunctionNode getProgram() {
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   300
        final Iterator<FunctionNode> iter = getFunctions();
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   301
        FunctionNode last = null;
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   302
        while (iter.hasNext()) {
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   303
            last = iter.next();
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   304
        }
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   305
        assert last != null;
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   306
        return last;
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   307
    }*/
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   308
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   309
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   310
     * Returns an iterator over all ancestors block of the given block, with its parent block first.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   311
     * @param block the block whose ancestors are returned
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   312
     * @return an iterator over all ancestors block of the given block.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   313
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   314
    public Iterator<Block> getAncestorBlocks(final Block block) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   315
        final Iterator<Block> iter = getBlocks();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   316
        while (iter.hasNext()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   317
            final Block b = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   318
            if (block == b) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   319
                return iter;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   320
            }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   321
        }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   322
        throw new AssertionError("Block is not on the current lexical context stack");
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   323
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   324
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   325
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   326
     * Returns an iterator over a block and all its ancestors blocks, with the block first.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   327
     * @param block the block that is the starting point of the iteration.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   328
     * @return an iterator over a block and all its ancestors.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   329
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   330
    public Iterator<Block> getBlocks(final Block block) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   331
        final Iterator<Block> iter = getAncestorBlocks(block);
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   332
        return new Iterator<Block>() {
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   333
            boolean blockReturned = false;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   334
            @Override
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   335
            public boolean hasNext() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   336
                return iter.hasNext() || !blockReturned;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   337
            }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   338
            @Override
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   339
            public Block next() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   340
                if (blockReturned) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   341
                    return iter.next();
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   342
                }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   343
                blockReturned = true;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   344
                return block;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   345
            }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   346
            @Override
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   347
            public void remove() {
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   348
                throw new UnsupportedOperationException();
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   349
            }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   350
        };
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   351
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   352
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   353
    /**
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26068
diff changeset
   354
     * Get the function for this block.
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   355
     * @param block block for which to get function
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   356
     * @return function for block
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   357
     */
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   358
    public FunctionNode getFunction(final Block block) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   359
        final Iterator<LexicalContextNode> iter = new NodeIterator<>(LexicalContextNode.class);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   360
        while (iter.hasNext()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   361
            final LexicalContextNode next = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   362
            if (next == block) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   363
                while (iter.hasNext()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   364
                    final LexicalContextNode next2 = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   365
                    if (next2 instanceof FunctionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   366
                        return (FunctionNode)next2;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   367
                    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   368
                }
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   369
            }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   370
        }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   371
        assert false;
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   372
        return null;
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   373
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   374
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   375
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   376
     * Returns the innermost block in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   377
     * @return the innermost block in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   378
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   379
    public Block getCurrentBlock() {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   380
        return getBlocks().next();
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   381
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   382
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   383
    /**
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   384
     * Returns the innermost function in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   385
     * @return the innermost function in the context.
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   386
     */
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   387
    public FunctionNode getCurrentFunction() {
19084
daddbeee0058 8020356: ClassCastException Undefined->Scope on spiltter class generated for a large switch statement
hannesw
parents: 18867
diff changeset
   388
        for (int i = sp - 1; i >= 0; i--) {
daddbeee0058 8020356: ClassCastException Undefined->Scope on spiltter class generated for a large switch statement
hannesw
parents: 18867
diff changeset
   389
            if (stack[i] instanceof FunctionNode) {
daddbeee0058 8020356: ClassCastException Undefined->Scope on spiltter class generated for a large switch statement
hannesw
parents: 18867
diff changeset
   390
                return (FunctionNode) stack[i];
daddbeee0058 8020356: ClassCastException Undefined->Scope on spiltter class generated for a large switch statement
hannesw
parents: 18867
diff changeset
   391
            }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   392
        }
19084
daddbeee0058 8020356: ClassCastException Undefined->Scope on spiltter class generated for a large switch statement
hannesw
parents: 18867
diff changeset
   393
        return null;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   394
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   395
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   396
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   397
     * Get the block in which a symbol is defined
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   398
     * @param symbol symbol
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   399
     * @return block in which the symbol is defined, assert if no such block in context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   400
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   401
    public Block getDefiningBlock(final Symbol symbol) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   402
        final String name = symbol.getName();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   403
        for (final Iterator<Block> it = getBlocks(); it.hasNext();) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   404
            final Block next = it.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   405
            if (next.getExistingSymbol(name) == symbol) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   406
                return next;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   407
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   408
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   409
        throw new AssertionError("Couldn't find symbol " + name + " in the context");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   410
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   411
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   412
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   413
     * Get the function in which a symbol is defined
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   414
     * @param symbol symbol
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   415
     * @return function node in which this symbol is defined, assert if no such symbol exists in context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   416
     */
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   417
    public FunctionNode getDefiningFunction(final Symbol symbol) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   418
        final String name = symbol.getName();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   419
        for (final Iterator<LexicalContextNode> iter = new NodeIterator<>(LexicalContextNode.class); iter.hasNext();) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   420
            final LexicalContextNode next = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   421
            if (next instanceof Block && ((Block)next).getExistingSymbol(name) == symbol) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   422
                while (iter.hasNext()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   423
                    final LexicalContextNode next2 = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   424
                    if (next2 instanceof FunctionNode) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   425
                        return (FunctionNode)next2;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   426
                    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   427
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   428
                throw new AssertionError("Defining block for symbol " + name + " has no function in the context");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   429
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   430
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   431
        throw new AssertionError("Couldn't find symbol " + name + " in the context");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   432
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   433
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   434
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   435
     * Is the topmost lexical context element a function body?
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   436
     * @return true if function body
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   437
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   438
    public boolean isFunctionBody() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   439
        return getParentBlock() == null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   440
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   441
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   442
    /**
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   443
     * Is the topmost lexical context element body of a SplitNode?
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   444
     * @return true if it's the body of a split node.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   445
     */
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   446
    public boolean isSplitBody() {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   447
        return sp >= 2 && stack[sp - 1] instanceof Block && stack[sp - 2] instanceof SplitNode;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   448
    }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   449
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 26503
diff changeset
   450
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   451
     * Get the parent function for a function in the lexical context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   452
     * @param functionNode function for which to get parent
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   453
     * @return parent function of functionNode or null if none (e.g. if functionNode is the program)
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   454
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   455
    public FunctionNode getParentFunction(final FunctionNode functionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   456
        final Iterator<FunctionNode> iter = new NodeIterator<>(FunctionNode.class);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   457
        while (iter.hasNext()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   458
            final FunctionNode next = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   459
            if (next == functionNode) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   460
                return iter.hasNext() ? iter.next() : null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   461
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   462
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   463
        assert false;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   464
        return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   465
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   466
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   467
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   468
     * Count the number of scopes until a given node. Note that this method is solely used to figure out the number of
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   469
     * scopes that need to be explicitly popped in order to perform a break or continue jump within the current bytecode
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   470
     * method. For this reason, the method returns 0 if it encounters a {@code SplitNode} between the current location
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   471
     * and the break/continue target.
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   472
     * @param until node to stop counting at. Must be within the current function
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   473
     * @return number of with scopes encountered in the context
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   474
     */
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   475
    public int getScopeNestingLevelTo(final LexicalContextNode until) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   476
        assert until != null;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   477
        //count the number of with nodes until "until" is hit
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   478
        int n = 0;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   479
        for (final Iterator<LexicalContextNode> iter = getAllNodes(); iter.hasNext();) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   480
            final LexicalContextNode node = iter.next();
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   481
            if (node == until) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   482
                break;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   483
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   484
            assert !(node instanceof FunctionNode); // Can't go outside current function
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   485
            if (node instanceof WithNode || node instanceof Block && ((Block)node).needsScope()) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   486
                n++;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20559
diff changeset
   487
            }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   488
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   489
        return n;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   490
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   491
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   492
    private BreakableNode getBreakable() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   493
        for (final NodeIterator<BreakableNode> iter = new NodeIterator<>(BreakableNode.class, getCurrentFunction()); iter.hasNext(); ) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   494
            final BreakableNode next = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   495
            if (next.isBreakableWithoutLabel()) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   496
                return next;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   497
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   498
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   499
        return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   500
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   501
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   502
    /**
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   503
     * Check whether the lexical context is currently inside a loop
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   504
     * @return true if inside a loop
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   505
     */
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   506
    public boolean inLoop() {
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   507
        return getCurrentLoop() != null;
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   508
    }
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   509
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   510
    /**
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   511
     * Returns the loop header of the current loop, or null if not inside a loop
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   512
     * @return loop header
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   513
     */
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   514
    public LoopNode getCurrentLoop() {
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   515
        final Iterator<LoopNode> iter = new NodeIterator<>(LoopNode.class, getCurrentFunction());
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   516
        return iter.hasNext() ? iter.next() : null;
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   517
    }
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   518
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   519
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   520
     * Find the breakable node corresponding to this label.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   521
     * @param labelName name of the label to search for. If null, the closest breakable node will be returned
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   522
     * unconditionally, e.g. a while loop with no label
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   523
     * @return closest breakable node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   524
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   525
    public BreakableNode getBreakable(final String labelName) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   526
        if (labelName != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   527
            final LabelNode foundLabel = findLabel(labelName);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   528
            if (foundLabel != null) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   529
                // iterate to the nearest breakable to the foundLabel
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   530
                BreakableNode breakable = null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   531
                for (final NodeIterator<BreakableNode> iter = new NodeIterator<>(BreakableNode.class, foundLabel); iter.hasNext(); ) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   532
                    breakable = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   533
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   534
                return breakable;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   535
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   536
            return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   537
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   538
        return getBreakable();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   539
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   540
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   541
    private LoopNode getContinueTo() {
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   542
        return getCurrentLoop();
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   543
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   544
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   545
    /**
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   546
     * Find the continue target node corresponding to this label.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   547
     * @param labelName label name to search for. If null the closest loop node will be returned unconditionally, e.g. a
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   548
     * while loop with no label
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   549
     * @return closest continue target node
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   550
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   551
    public LoopNode getContinueTo(final String labelName) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   552
        if (labelName != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   553
            final LabelNode foundLabel = findLabel(labelName);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   554
            if (foundLabel != null) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   555
                // iterate to the nearest loop to the foundLabel
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   556
                LoopNode loop = null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   557
                for (final NodeIterator<LoopNode> iter = new NodeIterator<>(LoopNode.class, foundLabel); iter.hasNext(); ) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   558
                    loop = iter.next();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   559
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   560
                return loop;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   561
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   562
            return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   563
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   564
        return getContinueTo();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   565
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   566
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   567
    /**
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   568
     * Find the inlined finally block node corresponding to this label.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   569
     * @param labelName label name to search for. Must not be null.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   570
     * @return closest inlined finally block with the given label
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   571
     */
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   572
    public Block getInlinedFinally(final String labelName) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   573
        for (final NodeIterator<TryNode> iter = new NodeIterator<>(TryNode.class); iter.hasNext(); ) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   574
            final Block inlinedFinally = iter.next().getInlinedFinally(labelName);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   575
            if (inlinedFinally != null) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   576
                return inlinedFinally;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   577
            }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   578
        }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   579
        return null;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   580
    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   581
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   582
    /**
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   583
     * Find the try node for an inlined finally block corresponding to this label.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   584
     * @param labelName label name to search for. Must not be null.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   585
     * @return the try node to which the labelled inlined finally block belongs.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   586
     */
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   587
    public TryNode getTryNodeForInlinedFinally(final String labelName) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   588
        for (final NodeIterator<TryNode> iter = new NodeIterator<>(TryNode.class); iter.hasNext(); ) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   589
            final TryNode tryNode = iter.next();
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   590
            if (tryNode.getInlinedFinally(labelName) != null) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   591
                return tryNode;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   592
            }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   593
        }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   594
        return null;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   595
    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   596
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   597
    /**
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   598
     * Check the lexical context for a given label node by name
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   599
     * @param name name of the label
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   600
     * @return LabelNode if found, null otherwise
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   601
     */
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   602
    private LabelNode findLabel(final String name) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   603
        for (final Iterator<LabelNode> iter = new NodeIterator<>(LabelNode.class, getCurrentFunction()); iter.hasNext(); ) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   604
            final LabelNode next = iter.next();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   605
            if (next.getLabelName().equals(name)) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   606
                return next;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   607
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   608
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   609
        return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   610
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   611
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   612
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   613
     * Checks whether a given target is a jump destination that lies outside a given split node
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   614
     * @param splitNode the split node
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   615
     * @param target the target node
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   616
     * @return true if target resides outside the split node
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   617
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   618
    public boolean isExternalTarget(final SplitNode splitNode, final BreakableNode target) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   619
        for (int i = sp; i-- > 0;) {
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   620
            final LexicalContextNode next = stack[i];
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   621
            if (next == splitNode) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   622
                return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   623
            } else if (next == target) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   624
                return false;
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   625
            } else if (next instanceof TryNode) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   626
                for(final Block inlinedFinally: ((TryNode)next).getInlinedFinallies()) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   627
                    if (TryNode.getLabelledInlinedFinallyBlock(inlinedFinally) == target) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   628
                        return false;
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   629
                    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27817
diff changeset
   630
                }
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   631
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   632
        }
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   633
        throw new AssertionError(target + " was expected in lexical context " + LexicalContext.this + " but wasn't");
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   634
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   635
27817
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   636
    /**
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   637
     * Checks whether the current context is inside a switch statement without explicit blocks (curly braces).
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   638
     * @return true if in unprotected switch statement
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   639
     */
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   640
    public boolean inUnprotectedSwitchContext() {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   641
        for (int i = sp; i > 0; i--) {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   642
            final LexicalContextNode next = stack[i];
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   643
            if (next instanceof Block) {
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   644
                return stack[i - 1] instanceof SwitchNode;
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   645
            }
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   646
        }
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   647
        return false;
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   648
    }
56f6161c3e55 8057980: let & const: remaining issues with lexical scoping
hannesw
parents: 27206
diff changeset
   649
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   650
    @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   651
    public String toString() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   652
        final StringBuffer sb = new StringBuffer();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   653
        sb.append("[ ");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   654
        for (int i = 0; i < sp; i++) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18853
diff changeset
   655
            final Object node = stack[i];
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   656
            sb.append(node.getClass().getSimpleName());
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   657
            sb.append('@');
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   658
            sb.append(Debug.id(node));
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   659
            sb.append(':');
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   660
            if (node instanceof FunctionNode) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18853
diff changeset
   661
                final FunctionNode fn = (FunctionNode)node;
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18853
diff changeset
   662
                final Source source = fn.getSource();
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   663
                String src = source.toString();
20559
9244eb6d195b 8025515: Performance issues with Source.getLine()
hannesw
parents: 19084
diff changeset
   664
                if (src.contains(File.pathSeparator)) {
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   665
                    src = src.substring(src.lastIndexOf(File.pathSeparator));
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   666
                }
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   667
                src += ' ';
20559
9244eb6d195b 8025515: Performance issues with Source.getLine()
hannesw
parents: 19084
diff changeset
   668
                src += fn.getLineNumber();
17523
cb4a7c901e0d 8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents: 17518
diff changeset
   669
                sb.append(src);
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   670
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   671
            sb.append(' ');
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   672
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   673
        sb.append(" ==> ]");
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   674
        return sb.toString();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   675
    }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   676
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   677
    private class NodeIterator <T extends LexicalContextNode> implements Iterator<T> {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   678
        private int index;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   679
        private T next;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   680
        private final Class<T> clazz;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   681
        private LexicalContextNode until;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   682
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   683
        NodeIterator(final Class<T> clazz) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   684
            this(clazz, null);
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   685
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   686
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   687
        NodeIterator(final Class<T> clazz, final LexicalContextNode until) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   688
            this.index = sp - 1;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   689
            this.clazz = clazz;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   690
            this.until = until;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   691
            this.next  = findNext();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   692
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   693
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   694
        @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   695
        public boolean hasNext() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   696
            return next != null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   697
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   698
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   699
        @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   700
        public T next() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   701
            if (next == null) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   702
                throw new NoSuchElementException();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   703
            }
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   704
            final T lnext = next;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   705
            next = findNext();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   706
            return lnext;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   707
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   708
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 24751
diff changeset
   709
        @SuppressWarnings("unchecked")
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   710
        private T findNext() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   711
            for (int i = index; i >= 0; i--) {
18867
bc91e3fcc5ba 8013925: Remove symbol fields from nodes that don't need them
attila
parents: 18853
diff changeset
   712
                final Object node = stack[i];
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   713
                if (node == until) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   714
                    return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   715
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   716
                if (clazz.isAssignableFrom(node.getClass())) {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   717
                    index = i - 1;
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 24751
diff changeset
   718
                    return (T)node;
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   719
                }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   720
            }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   721
            return null;
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   722
        }
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   723
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   724
        @Override
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   725
        public void remove() {
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   726
            throw new UnsupportedOperationException();
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16530
diff changeset
   727
        }
16530
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   728
    }
201d682e75f4 8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
diff changeset
   729
}