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