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