Class FunctionNode

All Implemented Interfaces:
Flags<FunctionNode>, LexicalContextNode, Cloneable

public final class FunctionNode extends Expression implements Flags<FunctionNode>
IR representation for function (or script.)
  • Field Details

    • IS_ANONYMOUS

      public static final int IS_ANONYMOUS
      Is anonymous function flag.
      See Also:
    • IS_DECLARED

      public static final int IS_DECLARED
      Is the function created in a function declaration (as opposed to a function expression)
      See Also:
    • IS_STRICT

      public static final int IS_STRICT
      is this a strict mode function?
      See Also:
    • USES_ARGUMENTS

      public static final int USES_ARGUMENTS
      Does the function use the "arguments" identifier ?
      See Also:
    • IS_STATEMENT

      public static final int IS_STATEMENT
      Is it a statement?
      See Also:
    • HAS_EVAL

      public static final int HAS_EVAL
      Does the function call eval? If it does, then all variables in this function might be get/set by it and it can introduce new variables into this function's scope too.
      See Also:
    • HAS_NESTED_EVAL

      public static final int HAS_NESTED_EVAL
      Does a nested function contain eval? If it does, then all variables in this function might be get/set by it.
      See Also:
    • HAS_SCOPE_BLOCK

      public static final int HAS_SCOPE_BLOCK
      Does this function have any blocks that create a scope? This is used to determine if the function needs to have a local variable slot for the scope symbol.
      See Also:
    • DEFINES_ARGUMENTS

      public static final int DEFINES_ARGUMENTS
      Flag this function as one that defines the identifier "arguments" as a function parameter or nested function name. This precludes it from needing to have an Arguments object defined as "arguments" local variable. Note that defining a local variable named "arguments" still requires construction of the Arguments object (see ECMAScript 5.1 Chapter 10.5).
      See Also:
    • USES_ANCESTOR_SCOPE

      public static final int USES_ANCESTOR_SCOPE
      Does this function or any of its descendants use variables from an ancestor function's scope (incl. globals)?
      See Also:
    • IS_SCRIPT

      public static final int IS_SCRIPT
      A script function
      See Also:
    • IS_GETTER

      public static final int IS_GETTER
      A getter function
      See Also:
    • IS_SETTER

      public static final int IS_SETTER
      A setter function
      See Also:
    • IS_PROGRAM

      public static final int IS_PROGRAM
      Is this function the top-level program?
      See Also:
    • HAS_CLOSURES

      public static final int HAS_CLOSURES
      Does this function have closures?
      See Also:
    • USES_THIS

      public static final int USES_THIS
      Does this function use the "this" keyword?
      See Also:
    • NEEDS_PARENT_SCOPE

      public static final int NEEDS_PARENT_SCOPE
      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, or it's the program.
      See Also:
    • IS_ARROW

      public static final int IS_ARROW
      An arrow function
      See Also:
    • IS_MODULE

      public static final int IS_MODULE
      A module function
      See Also:
    • HAS_DIRECT_SUPER

      public static final int HAS_DIRECT_SUPER
      Does this function contain a super call? (cf. ES6 14.3.5 Static Semantics: HasDirectSuper)
      See Also:
    • USES_SUPER

      public static final int USES_SUPER
      Does this function use the super binding?
      See Also:
    • IS_METHOD

      public static final int IS_METHOD
      Is this function a (class or object) method?
      See Also:
    • NO_FUNCTION_SELF

      public static final int NO_FUNCTION_SELF
      If one of these flags are set, this function does not have a function self binding.
      See Also:
    • IS_CLASS_CONSTRUCTOR

      public static final int IS_CLASS_CONSTRUCTOR
      Is this the constructor method?
      See Also:
    • IS_DERIVED_CONSTRUCTOR

      public static final int IS_DERIVED_CONSTRUCTOR
      Is this the constructor of a subclass (i.e., a class with an extends declaration)?
      See Also:
    • USES_NEW_TARGET

      public static final int USES_NEW_TARGET
      Does this function use new.target?
      See Also:
    • IS_GENERATOR

      public static final int IS_GENERATOR
      A generator function
      See Also:
    • IS_ASYNC

      public static final int IS_ASYNC
      Is it an async function?
      See Also:
    • HAS_NON_SIMPLE_PARAMETER_LIST

      public static final int HAS_NON_SIMPLE_PARAMETER_LIST
      Flag indicating that this function has a non-simple parameter list.
      See Also:
    • HAS_ARROW_EVAL

      public static final int HAS_ARROW_EVAL
      Flag indicating that this function has an eval nested in an arrow function.
      See Also:
    • HAS_FUNCTION_DECLARATIONS

      public static final int HAS_FUNCTION_DECLARATIONS
      Does this function have nested declarations?
      See Also:
    • HAS_APPLY_ARGUMENTS_CALL

      public static final int HAS_APPLY_ARGUMENTS_CALL
      Does this function contain a fn.apply(_, arguments) call?
      See Also:
    • IS_CLASS_FIELD_INITIALIZER

      public static final int IS_CLASS_FIELD_INITIALIZER
      Is this function a class field/static initializer?
      See Also:
    • ARROW_HEAD_FLAGS

      public static final int ARROW_HEAD_FLAGS
      All flags that may be set during parsing of an arrow head cover grammar and that have to be propagated to the enclosing function if the expression ends up not being an arrow function.
      See Also:
  • Constructor Details

    • FunctionNode

      public FunctionNode(Source source, int lineNumber, long token, int finish, long firstToken, long lastToken, IdentNode ident, com.oracle.truffle.api.strings.TruffleString name, int length, int numOfParams, List<IdentNode> parameters, int flags, Block body, Object endParserState, Module module, com.oracle.truffle.api.strings.TruffleString internalName)
      Constructor
      Parameters:
      source - the source
      lineNumber - line number
      token - token
      finish - finish
      firstToken - first token of the function node (including the function declaration)
      lastToken - lastToken
      ident - the identifier
      name - the name of the function
      parameters - parameter list
      flags - initial flags
      body - body of the function
      endParserState - The parser state at the end of the parsing.
  • Method Details

    • accept

      public Node accept(LexicalContext lc, NodeVisitor<? extends LexicalContext> visitor)
      Description copied from interface: LexicalContextNode
      Accept function for the node given a lexical context. It must be prepared to replace itself if present in the lexical context
      Specified by:
      accept in interface LexicalContextNode
      Parameters:
      lc - lexical context
      visitor - node visitor
      Returns:
      new node or same node depending on state change
    • accept

      public <R> R accept(LexicalContext lc, TranslatorNodeVisitor<? extends LexicalContext,R> visitor)
      Specified by:
      accept in interface LexicalContextNode
    • getSource

      public Source getSource()
      Get the source for this function
      Returns:
      the source
    • getId

      public int getId()
      Get the unique ID for this function within the script file.
      Returns:
      the id
    • getSourceName

      public String getSourceName()
      get source name - sourceURL or name derived from Source.
      Returns:
      name for the script source
    • getSourceName

      public static String getSourceName(Source source)
      Static source name getter
      Parameters:
      source - the source
      Returns:
      source name
    • getLineNumber

      public int getLineNumber()
      Returns the line number.
      Returns:
      the line number.
    • toString

      public void toString(StringBuilder sb, boolean printTypes)
      Description copied from class: Node
      Print logic that decides whether to show the optimistic type or not - for example it should not be printed after just parse, when it hasn't been computed, or has been set to a trivially provable value
      Specified by:
      toString in class Node
      Parameters:
      sb - string builder
      printTypes - print type?
    • getFlags

      public int getFlags()
      Description copied from interface: Flags
      Get all flags of a LexicalContextNode
      Specified by:
      getFlags in interface Flags<FunctionNode>
      Returns:
      flags
    • getFlag

      public boolean getFlag(int flag)
      Description copied from interface: Flags
      Check if a flag is set in a lexical context node
      Specified by:
      getFlag in interface Flags<FunctionNode>
      Parameters:
      flag - flag to check
      Returns:
      flags
    • setFlags

      public FunctionNode setFlags(LexicalContext lc, int flags)
      Description copied from interface: Flags
      Set all flags of a LexicalContextNode, overwriting previous flags
      Specified by:
      setFlags in interface Flags<FunctionNode>
      Parameters:
      lc - lexical context
      flags - new flags value
      Returns:
      the new LexicalContext node if flags were changed, same otherwise
    • setFlag

      public FunctionNode setFlag(LexicalContext lc, int flag)
      Description copied from interface: Flags
      Set a flag of a LexicalContextNode
      Specified by:
      setFlag in interface Flags<FunctionNode>
      Parameters:
      lc - lexical context
      flag - flag to set
      Returns:
      the new LexicalContext node if flags were changed, same otherwise
    • isProgram

      public boolean isProgram()
      Returns true if the function is the top-level program.
      Returns:
      True if this function node represents the top-level program.
    • hasEval

      public boolean hasEval()
      Check if this function has a call expression for the identifier "eval" (that is, eval(...)).
      Returns:
      true if eval is called.
    • getFirstToken

      public long getFirstToken()
      Get the first token for this function
      Returns:
      the first token
    • usesThis

      public boolean usesThis()
      Return true if this function makes use of the this object.
      Returns:
      true if function uses this object
    • getIdent

      public IdentNode getIdent()
      Get the identifier for this function, this is its symbol.
      Returns:
      the identifier as an IdentityNode
    • getBody

      public Block getBody()
      Get the function body, i.e., the top-most block of the function.
      Returns:
      the function body
    • getVarDeclarationBlock

      public Block getVarDeclarationBlock()
      Get the var declaration block, i.e., the actual function body, which is either getBody() or the next block after skipping the parameter initialization block.
    • setBody

      public FunctionNode setBody(LexicalContext lc, Block body)
      Reset the function body
      Parameters:
      lc - lexical context
      body - new body
      Returns:
      new function node if body changed, same if not
    • needsDynamicScope

      public boolean needsDynamicScope()
      Check whether a function would need dynamic scope, which is does if it has evals and isn't strict.
      Returns:
      true if dynamic scope is needed
    • needsArguments

      public boolean needsArguments()
      Returns true if this function needs to have an Arguments object defined as a local variable named "arguments". Functions that use "arguments" as identifier and don't define it as a name of a parameter or a nested function (see ECMAScript 5.1 Chapter 10.5), as well as any function that uses eval or with, or has a nested function that does the same, will have an "arguments" object. Also, if this function is a script, it will not have an "arguments" object, because it does not have local variables; rather the Global object will have an explicit "arguments" property that provides command-line arguments for the script.
      Returns:
      true if this function needs an arguments object.
    • getLastToken

      public long getLastToken()
      Return the last token for this function's code
      Returns:
      last token
    • getEndParserState

      public Object getEndParserState()
      Returns the end parser state for this function.
      Returns:
      the end parser state for this function.
    • getName

      public String getName()
      Get the name of this function
      Returns:
      the name
    • getNameTS

      public com.oracle.truffle.api.strings.TruffleString getNameTS()
    • setName

      public FunctionNode setName(LexicalContext lc, com.oracle.truffle.api.strings.TruffleString name)
      Set the name of this function.
      Parameters:
      lc - lexical context
      name - new name
      Returns:
      new function node if changed, otherwise the same
    • getInternalName

      public String getInternalName()
    • getInternalNameTS

      public com.oracle.truffle.api.strings.TruffleString getInternalNameTS()
    • getParameters

      public List<IdentNode> getParameters()
      Get the parameters to this function
      Returns:
      a list of IdentNodes which represent the function parameters, in order
    • getNumOfParams

      public int getNumOfParams()
      Return the number of parameters to this function
      Returns:
      the number of parameters
    • getLength

      public int getLength()
      The value of the function's length property, i.e., the typical number of arguments expected by the function.
    • isDeclared

      public boolean isDeclared()
      Check if this function is created as a function declaration (as opposed to function expression)
      Returns:
      true if function is declared.
    • isAnonymous

      public boolean isAnonymous()
      Check if this function is anonymous
      Returns:
      true if function is anonymous
    • isNamedFunctionExpression

      public boolean isNamedFunctionExpression()
      Returns true if this is a named function expression (that is, it isn't a declared function, it isn't an anonymous function expression, it isn't a method, and it isn't a program).
      Returns:
      true if this is a named function expression
    • isStrict

      public boolean isStrict()
      Check if the function is generated in strict mode
      Returns:
      true if strict mode enabled for function
    • isMethod

      public boolean isMethod()
    • usesSuper

      public boolean usesSuper()
    • hasDirectSuper

      public boolean hasDirectSuper()
    • isClassConstructor

      public boolean isClassConstructor()
    • isDerivedConstructor

      public boolean isDerivedConstructor()
    • usesNewTarget

      public boolean usesNewTarget()
    • isScript

      public boolean isScript()
    • isGetter

      public boolean isGetter()
    • isSetter

      public boolean isSetter()
    • isArrow

      public boolean isArrow()
    • isGenerator

      public boolean isGenerator()
    • isModule

      public boolean isModule()
    • getModule

      public Module getModule()
    • isStatement

      public boolean isStatement()
    • isAsync

      public boolean isAsync()
    • hasSimpleParameterList

      public boolean hasSimpleParameterList()
    • usesAncestorScope

      public boolean usesAncestorScope()
    • setUsesAncestorScope

      public void setUsesAncestorScope(boolean usesAncestorScope)
    • isNormal

      public boolean isNormal()
    • hasApplyArgumentsCall

      public boolean hasApplyArgumentsCall()
    • hasArrowEval

      public boolean hasArrowEval()
    • needsThis

      public boolean needsThis()
    • needsNewTarget

      public boolean needsNewTarget()
    • needsSuper

      public boolean needsSuper()
    • isClassFieldInitializer

      public boolean isClassFieldInitializer()
    • hasClosures

      public boolean hasClosures()
    • accept

      public final Node accept(NodeVisitor<? extends LexicalContext> visitor)
      Description copied from class: Node
      Provides a means to navigate the IR.
      Specified by:
      accept in interface LexicalContextNode
      Specified by:
      accept in class Node
      Parameters:
      visitor - Node visitor.
      Returns:
      node the node or its replacement after visitation, null if no further visitations are required
    • accept

      public final <R> R accept(TranslatorNodeVisitor<? extends LexicalContext,R> visitor)
      Description copied from class: Node
      Provides a means to navigate the IR.
      Specified by:
      accept in interface LexicalContextNode
      Specified by:
      accept in class Node
      Parameters:
      visitor - Node visitor.
      Returns:
      node the node or its replacement after visitation, null if no further visitations are required