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