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