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.ir; 27 28 import java.util.Collections; 29 import java.util.EnumSet; 30 import java.util.HashSet; 31 import java.util.List; 32 import java.util.Objects; 33 import java.util.Set; 34 import jdk.nashorn.internal.codegen.CompileUnit; 35 import jdk.nashorn.internal.codegen.Compiler; 36 import jdk.nashorn.internal.codegen.CompilerConstants; 37 import jdk.nashorn.internal.codegen.Namespace; 38 import jdk.nashorn.internal.codegen.types.Type; 39 import jdk.nashorn.internal.ir.annotations.Ignore; 40 import jdk.nashorn.internal.ir.annotations.Immutable; 41 import jdk.nashorn.internal.ir.visitor.NodeVisitor; 42 import jdk.nashorn.internal.runtime.ScriptFunction; 43 import jdk.nashorn.internal.runtime.Source; 44 import jdk.nashorn.internal.runtime.UserAccessorProperty; 45 import jdk.nashorn.internal.runtime.linker.LinkerCallSite; 46 47 /** 48 * IR representation for function (or script.) 49 */ 50 @Immutable 51 public final class FunctionNode extends LexicalContextExpression implements Flags<FunctionNode> { 52 53 /** Type used for all FunctionNodes */ 54 public static final Type FUNCTION_TYPE = Type.typeFor(ScriptFunction.class); 55 56 /** Function kinds */ 57 public enum Kind { 58 /** a normal function - nothing special */ 59 NORMAL, 60 /** a script function */ 61 SCRIPT, 62 /** a getter, @see {@link UserAccessorProperty} */ 63 GETTER, 64 /** a setter, @see {@link UserAccessorProperty} */ 65 SETTER 66 } 67 68 /** Compilation states available */ 69 public enum CompilationState { 70 /** compiler is ready */ 71 INITIALIZED, 72 /** method has been parsed */ 73 PARSED, 74 /** method has been parsed */ 75 PARSE_ERROR, 76 /** constant folding pass */ 77 CONSTANT_FOLDED, 78 /** method has been lowered */ 79 LOWERED, 80 /** method hass been attributed */ 81 ATTR, 82 /** method has been split */ 83 SPLIT, 84 /** method has had its types finalized */ 85 FINALIZED, 86 /** method has been emitted to bytecode */ 87 EMITTED 88 } 89 /** Source of entity. */ 90 private final Source source; 91 92 /** External function identifier. */ 93 @Ignore 94 private final IdentNode ident; 95 96 /** Parsed version of functionNode */ 97 @Ignore 98 private final FunctionNode snapshot; 99 100 /** The body of the function node */ 101 private final Block body; 102 103 /** Internal function name. */ 104 private final String name; 105 106 /** Compilation unit. */ 107 private final CompileUnit compileUnit; 108 109 /** Function kind. */ 110 private final Kind kind; 111 112 /** List of parameters. */ 113 private final List<IdentNode> parameters; 114 115 /** First token of function. **/ 116 private final long firstToken; 117 118 /** Last token of function. **/ 119 private final long lastToken; 120 121 /** Declared symbols in this function node */ 122 @Ignore 123 private final Set<Symbol> declaredSymbols; 124 125 /** Method's namespace. */ 126 private final Namespace namespace; 127 128 /** Current compilation state */ 129 @Ignore 130 private final EnumSet<CompilationState> compilationState; 131 132 @Ignore 133 private final Compiler.Hints hints; 134 135 /** Properties of this object assigned in this function */ 136 @Ignore 137 private HashSet<String> thisProperties; 138 139 /** Function flags. */ 140 private final int flags; 141 142 /** //@ sourceURL or //# sourceURL for program function nodes */ 143 private final String sourceURL; 144 145 private final int lineNumber; 146 147 /** Is anonymous function flag. */ 148 public static final int IS_ANONYMOUS = 1 << 0; 149 150 /** Is the function created in a function declaration (as opposed to a function expression) */ 151 public static final int IS_DECLARED = 1 << 1; 152 153 /** is this a strict mode function? */ 154 public static final int IS_STRICT = 1 << 2; 155 156 /** Does the function use the "arguments" identifier ? */ 157 public static final int USES_ARGUMENTS = 1 << 3; 158 159 /** Has this node been split because it was too large? */ 160 public static final int IS_SPLIT = 1 << 4; 161 162 /** Does the function call eval? If it does, then all variables in this function might be get/set by it and it can 163 * introduce new variables into this function's scope too.*/ 164 public static final int HAS_EVAL = 1 << 5; 165 166 /** Does a nested function contain eval? If it does, then all variables in this function might be get/set by it. */ 167 public static final int HAS_NESTED_EVAL = 1 << 6; 168 169 /** Does this function have any blocks that create a scope? This is used to determine if the function needs to 170 * have a local variable slot for the scope symbol. */ 171 public static final int HAS_SCOPE_BLOCK = 1 << 7; 172 173 /** 174 * Flag this function as one that defines the identifier "arguments" as a function parameter or nested function 175 * name. This precludes it from needing to have an Arguments object defined as "arguments" local variable. Note that 176 * defining a local variable named "arguments" still requires construction of the Arguments object (see 177 * ECMAScript 5.1 Chapter 10.5). 178 * @see #needsArguments() 179 */ 180 public static final int DEFINES_ARGUMENTS = 1 << 8; 181 182 /** Does this function or any of its descendants use variables from an ancestor function's scope (incl. globals)? */ 183 public static final int USES_ANCESTOR_SCOPE = 1 << 9; 184 185 /** Is this function lazily compiled? */ 186 public static final int IS_LAZY = 1 << 10; 187 188 /** Does this function have lazy, yet uncompiled children */ 189 public static final int HAS_LAZY_CHILDREN = 1 << 11; 190 191 /** Does this function have lazy, yet uncompiled children */ 192 public static final int IS_PROGRAM = 1 << 12; 193 194 /** Does this function have nested declarations? */ 195 public static final int HAS_FUNCTION_DECLARATIONS = 1 << 13; 196 197 /** Can this function be specialized? */ 198 public static final int CAN_SPECIALIZE = 1 << 14; 199 200 /** Does this function use the "this" keyword? */ 201 public static final int USES_THIS = 1 << 15; 202 203 /** Does this function or any nested functions contain an eval? */ 204 private static final int HAS_DEEP_EVAL = HAS_EVAL | HAS_NESTED_EVAL; 205 206 /** Does this function need to store all its variables in scope? */ 207 private static final int HAS_ALL_VARS_IN_SCOPE = HAS_DEEP_EVAL | IS_SPLIT | HAS_LAZY_CHILDREN; 208 209 /** Does this function potentially need "arguments"? Note that this is not a full test, as further negative check of REDEFINES_ARGS is needed. */ 210 private static final int MAYBE_NEEDS_ARGUMENTS = USES_ARGUMENTS | HAS_EVAL; 211 212 /** Does this function need the parent scope? It needs it if either it or its descendants use variables from it, or have a deep eval. 213 * We also pessimistically need a parent scope if we have lazy children that have not yet been compiled */ 214 private static final int NEEDS_PARENT_SCOPE = USES_ANCESTOR_SCOPE | HAS_DEEP_EVAL | HAS_LAZY_CHILDREN; 215 216 /** What is the return type of this function? */ 217 private Type returnType = Type.UNKNOWN; 218 219 /** 220 * Constructor 221 * 222 * @param source the source 223 * @param lineNumber line number 224 * @param token token 225 * @param finish finish 226 * @param firstToken first token of the funtion node (including the function declaration) 227 * @param namespace the namespace 228 * @param ident the identifier 229 * @param name the name of the function 230 * @param parameters parameter list 231 * @param kind kind of function as in {@link FunctionNode.Kind} 232 * @param flags initial flags 233 * @param sourceURL sourceURL specified in script (optional) 234 */ 235 public FunctionNode( 236 final Source source, 237 final int lineNumber, 238 final long token, 239 final int finish, 240 final long firstToken, 241 final Namespace namespace, 242 final IdentNode ident, 243 final String name, 244 final List<IdentNode> parameters, 245 final FunctionNode.Kind kind, 246 final int flags, 247 final String sourceURL) { 248 super(token, finish); 249 250 this.source = source; 251 this.lineNumber = lineNumber; 252 this.ident = ident; 253 this.name = name; 254 this.kind = kind; 255 this.parameters = parameters; 256 this.firstToken = firstToken; 257 this.lastToken = token; 258 this.namespace = namespace; 259 this.compilationState = EnumSet.of(CompilationState.INITIALIZED); 260 this.declaredSymbols = new HashSet<>(); 261 this.flags = flags; 262 this.sourceURL = sourceURL; 263 this.compileUnit = null; 264 this.body = null; 265 this.snapshot = null; 266 this.hints = null; 267 } 268 269 private FunctionNode( 270 final FunctionNode functionNode, 271 final long lastToken, 272 final int flags, 273 final String sourceURL, 274 final String name, 275 final Type returnType, 276 final CompileUnit compileUnit, 277 final EnumSet<CompilationState> compilationState, 278 final Block body, 279 final List<IdentNode> parameters, 280 final FunctionNode snapshot, 281 final Compiler.Hints hints) { 282 super(functionNode); 283 this.lineNumber = functionNode.lineNumber; 284 this.flags = flags; 285 this.sourceURL = sourceURL; 286 this.name = name; 287 this.returnType = returnType; 288 this.compileUnit = compileUnit; 289 this.lastToken = lastToken; 290 this.compilationState = compilationState; 291 this.body = body; 292 this.parameters = parameters; 293 this.snapshot = snapshot; 294 this.hints = hints; 295 296 // the fields below never change - they are final and assigned in constructor 297 this.source = functionNode.source; 298 this.ident = functionNode.ident; 299 this.namespace = functionNode.namespace; 300 this.declaredSymbols = functionNode.declaredSymbols; 301 this.kind = functionNode.kind; 302 this.firstToken = functionNode.firstToken; 303 this.thisProperties = functionNode.thisProperties; 304 } 305 306 @Override 307 public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { 308 if (visitor.enterFunctionNode(this)) { 309 return visitor.leaveFunctionNode(setBody(lc, (Block)body.accept(visitor))); 310 } 311 return this; 312 } 313 314 /** 315 * Get the source for this function 316 * @return the source 317 */ 318 public Source getSource() { 319 return source; 320 } 321 322 /** 323 * get source name - sourceURL or name derived from Source. 324 * 325 * @return name for the script source 326 */ 327 public String getSourceName() { 328 return (sourceURL != null)? sourceURL : source.getName(); 329 } 330 331 /** 332 * get the sourceURL 333 * @return the sourceURL 334 */ 335 public String getSourceURL() { 336 return sourceURL; 337 } 338 339 /** 340 * Set the sourceURL 341 * 342 * @param lc lexical context 343 * @param newSourceURL source url string to set 344 * @return function node or a new one if state was changed 345 */ 346 public FunctionNode setSourceURL(final LexicalContext lc, final String newSourceURL) { 347 if (Objects.equals(sourceURL, newSourceURL)) { 348 return this; 349 } 350 351 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, newSourceURL, name, returnType, compileUnit, compilationState, body, parameters, null, hints)); 352 } 353 354 /** 355 * Returns the line number. 356 * @return the line number. 357 */ 358 public int getLineNumber() { 359 return lineNumber; 360 } 361 362 /** 363 * Get the version of this function node's code as it looked upon construction 364 * i.e typically parsed and nothing else 365 * @return initial version of function node 366 */ 367 public FunctionNode getSnapshot() { 368 return snapshot; 369 } 370 371 /** 372 * Throw away the snapshot, if any, to save memory. Used when heuristic 373 * determines that a method is not worth specializing 374 * 375 * @param lc lexical context 376 * @return new function node if a snapshot was present, now with snapsnot null 377 */ 378 public FunctionNode clearSnapshot(final LexicalContext lc) { 379 if (this.snapshot == null) { 380 return this; 381 } 382 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, null, hints)); 383 } 384 385 /** 386 * Take a snapshot of this function node at a given point in time 387 * and store it in the function node 388 * @param lc lexical context 389 * @return function node 390 */ 391 public FunctionNode snapshot(final LexicalContext lc) { 392 if (this.snapshot == this) { 393 return this; 394 } 395 if (isProgram() || parameters.isEmpty()) { 396 return this; //never specialize anything that won't be recompiled 397 } 398 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, this, hints)); 399 } 400 401 /** 402 * Can this function node be regenerated with more specific type args? 403 * @return true if specialization is possible 404 */ 405 public boolean canSpecialize() { 406 return snapshot != null && getFlag(CAN_SPECIALIZE); 407 } 408 409 /** 410 * Get the compilation state of this function 411 * @return the compilation state 412 */ 413 public EnumSet<CompilationState> getState() { 414 return compilationState; 415 } 416 417 /** 418 * Check whether this FunctionNode has reached a give CompilationState. 419 * 420 * @param state the state to check for 421 * @return true of the node is in the given state 422 */ 423 public boolean hasState(final EnumSet<CompilationState> state) { 424 return compilationState.equals(state); 425 } 426 427 /** 428 * Check whether the state of this FunctionNode contains a given compilation 429 * state. 430 * 431 * A node can be in many states at once, e.g. both lowered and initialized. 432 * To check for an exact state, use {FunctionNode{@link #hasState(EnumSet)} 433 * 434 * @param state state to check for 435 * @return true if state is present in the total compilation state of this FunctionNode 436 */ 437 public boolean hasState(final CompilationState state) { 438 return compilationState.contains(state); 439 } 440 441 /** 442 * Add a state to the total CompilationState of this node, e.g. if 443 * FunctionNode has been lowered, the compiler will add 444 * {@code CompilationState#LOWERED} to the state vector 445 * 446 * @param lc lexical context 447 * @param state {@link CompilationState} to add 448 * @return function node or a new one if state was changed 449 */ 450 public FunctionNode setState(final LexicalContext lc, final CompilationState state) { 451 if (this.compilationState.contains(state)) { 452 return this; 453 } 454 final EnumSet<CompilationState> newState = EnumSet.copyOf(this.compilationState); 455 newState.add(state); 456 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, newState, body, parameters, snapshot, hints)); 457 } 458 459 /** 460 * Get any compiler hints that may associated with the function 461 * @return compiler hints 462 */ 463 public Compiler.Hints getHints() { 464 return this.hints == null ? Compiler.Hints.EMPTY : hints; 465 } 466 467 /** 468 * Set compiler hints for this function 469 * @param lc lexical context 470 * @param hints compiler hints 471 * @return new function if hints changed 472 */ 473 public FunctionNode setHints(final LexicalContext lc, final Compiler.Hints hints) { 474 if (this.hints == hints) { 475 return this; 476 } 477 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 478 } 479 480 /** 481 * Create a unique name in the namespace of this FunctionNode 482 * @param base prefix for name 483 * @return base if no collision exists, otherwise a name prefix with base 484 */ 485 public String uniqueName(final String base) { 486 return namespace.uniqueName(base); 487 } 488 489 490 @Override 491 public void toString(final StringBuilder sb) { 492 sb.append('['); 493 sb.append(returnType); 494 sb.append(']'); 495 sb.append(' '); 496 497 sb.append("function"); 498 499 if (ident != null) { 500 sb.append(' '); 501 ident.toString(sb); 502 } 503 504 sb.append('('); 505 boolean first = true; 506 507 for (final IdentNode parameter : parameters) { 508 if (!first) { 509 sb.append(", "); 510 } else { 511 first = false; 512 } 513 514 parameter.toString(sb); 515 } 516 517 sb.append(')'); 518 } 519 520 @Override 521 public boolean getFlag(final int flag) { 522 return (flags & flag) != 0; 523 } 524 525 @Override 526 public FunctionNode setFlags(final LexicalContext lc, int flags) { 527 if (this.flags == flags) { 528 return this; 529 } 530 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 531 } 532 533 @Override 534 public FunctionNode clearFlag(final LexicalContext lc, final int flag) { 535 return setFlags(lc, flags & ~flag); 536 } 537 538 @Override 539 public FunctionNode setFlag(final LexicalContext lc, final int flag) { 540 return setFlags(lc, flags | flag); 541 } 542 543 /** 544 * Returns true if the function is the top-level program. 545 * @return True if this function node represents the top-level program. 546 */ 547 public boolean isProgram() { 548 return getFlag(IS_PROGRAM); 549 } 550 551 /** 552 * Should this function node be lazily code generated, i.e. first at link time 553 * @return true if lazy 554 */ 555 public boolean isLazy() { 556 return getFlag(IS_LAZY); 557 } 558 559 /** 560 * Check if the {@code eval} keyword is used in this function 561 * 562 * @return true if {@code eval} is used 563 */ 564 public boolean hasEval() { 565 return getFlag(HAS_EVAL); 566 } 567 568 /** 569 * Get the first token for this function 570 * @return the first token 571 */ 572 public long getFirstToken() { 573 return firstToken; 574 } 575 576 /** 577 * Check whether this function has nested function declarations 578 * @return true if nested function declarations exist 579 */ 580 public boolean hasDeclaredFunctions() { 581 return getFlag(HAS_FUNCTION_DECLARATIONS); 582 } 583 584 /** 585 * Check if this function's generated Java method needs a {@code callee} parameter. Functions that need access to 586 * their parent scope, functions that reference themselves, and non-strict functions that need an Arguments object 587 * (since it exposes {@code arguments.callee} property) will need to have a callee parameter. We also return true 588 * for split functions to make sure symbols slots are the same in the main and split methods. 589 * 590 * @return true if the function's generated Java method needs a {@code callee} parameter. 591 */ 592 public boolean needsCallee() { 593 return needsParentScope() || needsSelfSymbol() || isSplit() || (needsArguments() && !isStrict()); 594 } 595 596 /** 597 * Return {@code true} if this function makes use of the {@code this} object. 598 * 599 * @return true if function uses {@code this} object 600 */ 601 public boolean usesThis() { 602 return getFlag(USES_THIS); 603 } 604 605 /** 606 * Get the identifier for this function, this is its symbol. 607 * @return the identifier as an IdentityNode 608 */ 609 public IdentNode getIdent() { 610 return ident; 611 } 612 613 /** 614 * Return a set of symbols declared in this function node. This 615 * is only relevant after Attr, otherwise it will be an empty 616 * set as no symbols have been introduced 617 * @return set of declared symbols in function 618 */ 619 public Set<Symbol> getDeclaredSymbols() { 620 return Collections.unmodifiableSet(declaredSymbols); 621 } 622 623 /** 624 * Add a declared symbol to this function node 625 * @param symbol symbol that is declared 626 */ 627 public void addDeclaredSymbol(final Symbol symbol) { 628 declaredSymbols.add(symbol); 629 } 630 631 /** 632 * Get the function body 633 * @return the function body 634 */ 635 public Block getBody() { 636 return body; 637 } 638 639 /** 640 * Reset the function body 641 * @param lc lexical context 642 * @param body new body 643 * @return new function node if body changed, same if not 644 */ 645 public FunctionNode setBody(final LexicalContext lc, final Block body) { 646 if(this.body == body) { 647 return this; 648 } 649 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags | (body.needsScope() ? FunctionNode.HAS_SCOPE_BLOCK : 0), sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 650 } 651 652 /** 653 * Does this function's method needs to be variable arity (gather all script-declared parameters in a final 654 * {@code Object[]} parameter. Functions that need to have the "arguments" object as well as functions that simply 655 * declare too many arguments for JVM to handle with fixed arity will need to be variable arity. 656 * @return true if the Java method in the generated code that implements this function needs to be variable arity. 657 * @see #needsArguments() 658 * @see LinkerCallSite#ARGLIMIT 659 */ 660 public boolean isVarArg() { 661 return needsArguments() || parameters.size() > LinkerCallSite.ARGLIMIT; 662 } 663 664 /** 665 * Returns true if this function needs to have an Arguments object defined as a local variable named "arguments". 666 * Functions that use "arguments" as identifier and don't define it as a name of a parameter or a nested function 667 * (see ECMAScript 5.1 Chapter 10.5), as well as any function that uses eval or with, or has a nested function that 668 * does the same, will have an "arguments" object. Also, if this function is a script, it will not have an 669 * "arguments" object, because it does not have local variables; rather the Global object will have an explicit 670 * "arguments" property that provides command-line arguments for the script. 671 * @return true if this function needs an arguments object. 672 */ 673 public boolean needsArguments() { 674 // uses "arguments" or calls eval, but it does not redefine "arguments", and finally, it's not a script, since 675 // for top-level script, "arguments" is picked up from Context by Global.init() instead. 676 return getFlag(MAYBE_NEEDS_ARGUMENTS) && !getFlag(DEFINES_ARGUMENTS) && !isProgram(); 677 } 678 679 /** 680 * Returns true if this function needs access to its parent scope. Functions referencing variables outside their 681 * scope (including global variables), as well as functions that call eval or have a with block, or have nested 682 * functions that call eval or have a with block, will need a parent scope. Top-level script functions also need a 683 * parent scope since they might be used from within eval, and eval will need an externally passed scope. 684 * @return true if the function needs parent scope. 685 */ 686 public boolean needsParentScope() { 687 return getFlag(NEEDS_PARENT_SCOPE) || isProgram(); 688 } 689 690 /** 691 * Register a property assigned to the this object in this function. 692 * @param key the property name 693 */ 694 public void addThisProperty(final String key) { 695 if (thisProperties == null) { 696 thisProperties = new HashSet<>(); 697 } 698 thisProperties.add(key); 699 } 700 701 /** 702 * Get the number of properties assigned to the this object in this function. 703 * @return number of properties 704 */ 705 public int countThisProperties() { 706 return thisProperties == null ? 0 : thisProperties.size(); 707 } 708 709 /** 710 * Returns true if any of the blocks in this function create their own scope. 711 * @return true if any of the blocks in this function create their own scope. 712 */ 713 public boolean hasScopeBlock() { 714 return getFlag(HAS_SCOPE_BLOCK); 715 } 716 717 /** 718 * Return the kind of this function 719 * @see FunctionNode.Kind 720 * @return the kind 721 */ 722 public Kind getKind() { 723 return kind; 724 } 725 726 /** 727 * Return the last token for this function's code 728 * @return last token 729 */ 730 public long getLastToken() { 731 return lastToken; 732 } 733 734 /** 735 * Set the last token for this function's code 736 * @param lc lexical context 737 * @param lastToken the last token 738 * @return function node or a new one if state was changed 739 */ 740 public FunctionNode setLastToken(final LexicalContext lc, final long lastToken) { 741 if (this.lastToken == lastToken) { 742 return this; 743 } 744 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 745 } 746 747 /** 748 * Get the name of this function 749 * @return the name 750 */ 751 public String getName() { 752 return name; 753 } 754 755 756 /** 757 * Set the internal name for this function 758 * @param lc lexical context 759 * @param name new name 760 * @return new function node if changed, otherwise the same 761 */ 762 public FunctionNode setName(final LexicalContext lc, final String name) { 763 if (this.name.equals(name)) { 764 return this; 765 } 766 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 767 } 768 769 /** 770 * Check if this function should have all its variables in its own scope. Scripts, split sub-functions, and 771 * functions having with and/or eval blocks are such. 772 * 773 * @return true if all variables should be in scope 774 */ 775 public boolean allVarsInScope() { 776 return isProgram() || getFlag(HAS_ALL_VARS_IN_SCOPE); 777 } 778 779 /** 780 * Checks if this function is a sub-function generated by splitting a larger one 781 * 782 * @return true if this function is split from a larger one 783 */ 784 public boolean isSplit() { 785 return getFlag(IS_SPLIT); 786 } 787 788 /** 789 * Checks if this function has yet-to-be-generated child functions 790 * 791 * @return true if there are lazy child functions 792 */ 793 public boolean hasLazyChildren() { 794 return getFlag(HAS_LAZY_CHILDREN); 795 } 796 797 /** 798 * Get the parameters to this function 799 * @return a list of IdentNodes which represent the function parameters, in order 800 */ 801 public List<IdentNode> getParameters() { 802 return Collections.unmodifiableList(parameters); 803 } 804 805 /** 806 * Reset the compile unit used to compile this function 807 * @see Compiler 808 * @param lc lexical context 809 * @param parameters the compile unit 810 * @return function node or a new one if state was changed 811 */ 812 public FunctionNode setParameters(final LexicalContext lc, final List<IdentNode> parameters) { 813 if (this.parameters == parameters) { 814 return this; 815 } 816 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 817 } 818 819 /** 820 * Check if this function is created as a function declaration (as opposed to function expression) 821 * @return true if function is declared. 822 */ 823 public boolean isDeclared() { 824 return getFlag(IS_DECLARED); 825 } 826 827 /** 828 * Check if this function is anonymous 829 * @return true if function is anonymous 830 */ 831 public boolean isAnonymous() { 832 return getFlag(IS_ANONYMOUS); 833 } 834 835 /** 836 * Does this function need a self symbol - this is needed only for self 837 * referring functions 838 * @return true if function needs a symbol for self 839 */ 840 public boolean needsSelfSymbol() { 841 return body.getFlag(Block.NEEDS_SELF_SYMBOL); 842 } 843 844 @Override 845 public Type getType() { 846 return FUNCTION_TYPE; 847 } 848 849 /** 850 * Get the return type for this function. Return types can be specialized 851 * if the compiler knows them, but parameters cannot, as they need to go through 852 * appropriate object conversion 853 * 854 * @return the return type 855 */ 856 public Type getReturnType() { 857 return returnType; 858 } 859 860 /** 861 * Set the function return type 862 * @param lc lexical context 863 * @param returnType new return type 864 * @return function node or a new one if state was changed 865 */ 866 public FunctionNode setReturnType(final LexicalContext lc, final Type returnType) { 867 //we never bother with object types narrower than objects, that will lead to byte code verification errors 868 //as for instance even if we know we are returning a string from a method, the code generator will always 869 //treat it as an object, at least for now 870 if (this.returnType == returnType) { 871 return this; 872 } 873 final Type type = Type.widest(this.returnType, returnType.isObject() ? Type.OBJECT : returnType); 874 return Node.replaceInLexicalContext( 875 lc, 876 this, 877 new FunctionNode( 878 this, 879 lastToken, 880 flags, 881 sourceURL, 882 name, 883 type, 884 compileUnit, 885 compilationState, 886 body.setReturnType(type), 887 parameters, 888 snapshot, 889 hints)); 890 } 891 892 /** 893 * Check if the function is generated in strict mode 894 * @return true if strict mode enabled for function 895 */ 896 public boolean isStrict() { 897 return getFlag(IS_STRICT); 898 } 899 900 /** 901 * Get the compile unit used to compile this function 902 * @see Compiler 903 * @return the compile unit 904 */ 905 public CompileUnit getCompileUnit() { 906 return compileUnit; 907 } 908 909 /** 910 * Reset the compile unit used to compile this function 911 * @see Compiler 912 * @param lc lexical context 913 * @param compileUnit the compile unit 914 * @return function node or a new one if state was changed 915 */ 916 public FunctionNode setCompileUnit(final LexicalContext lc, final CompileUnit compileUnit) { 917 if (this.compileUnit == compileUnit) { 918 return this; 919 } 920 return Node.replaceInLexicalContext(lc, this, new FunctionNode(this, lastToken, flags, sourceURL, name, returnType, compileUnit, compilationState, body, parameters, snapshot, hints)); 921 } 922 923 /** 924 * Create a temporary variable to the current frame. 925 * 926 * @param block that needs the temporary 927 * @param type Strong type of symbol. 928 * @param node Primary node to use symbol. 929 * 930 * @return Symbol used. 931 */ 932 933 /** 934 * Get the symbol for a compiler constant, or null if not available (yet) 935 * @param cc compiler constant 936 * @return symbol for compiler constant, or null if not defined yet (for example in Lower) 937 */ 938 public Symbol compilerConstant(final CompilerConstants cc) { 939 return body.getExistingSymbol(cc.symbolName()); 940 } 941 }