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