Class Node

  • All Implemented Interfaces:
    java.lang.Iterable<Node>
    Direct Known Subclasses:
    AstNode

    public class Node
    extends java.lang.Object
    implements java.lang.Iterable<Node>
    This class implements the root of the intermediate representation.
    • Field Detail

      • DESTRUCTURING_ARRAY_LENGTH

        public static final int DESTRUCTURING_ARRAY_LENGTH
        See Also:
        Constant Field Values
      • EXPRESSION_CLOSURE_PROP

        public static final int EXPRESSION_CLOSURE_PROP
        See Also:
        Constant Field Values
      • DESTRUCTURING_SHORTHAND

        public static final int DESTRUCTURING_SHORTHAND
        See Also:
        Constant Field Values
      • NOT_SET

        private static final Node NOT_SET
      • END_UNREACHED

        public static final int END_UNREACHED
        These flags enumerate the possible ways a statement/function can terminate. These flags are used by endCheck() and by the Parser to detect inconsistent return usage. END_UNREACHED is reserved for code paths that are assumed to always be able to execute (example: throw, continue) END_DROPS_OFF indicates if the statement can transfer control to the next one. Statement such as return dont. A compound statement may have some branch that drops off control to the next statement. END_RETURNS indicates that the statement can return (without arguments) END_RETURNS_VALUE indicates that the statement can return a value. A compound statement such as if (condition) { return value; } Will be detected as (END_DROPS_OFF | END_RETURN_VALUE) by endCheck()
        See Also:
        Constant Field Values
      • type

        protected int type
      • next

        protected Node next
      • first

        protected Node first
      • last

        protected Node last
      • lineno

        protected int lineno
      • propListHead

        protected Node.PropListItem propListHead
        Linked list of properties. Since vast majority of nodes would have no more then 2 properties, linked list saves memory and provides fast lookup. If this does not holds, propListHead can be replaced by UintMap.
    • Constructor Detail

      • Node

        public Node​(int nodeType)
      • Node

        public Node​(int nodeType,
                    Node child)
      • Node

        public Node​(int nodeType,
                    Node left,
                    Node right)
      • Node

        public Node​(int nodeType,
                    Node left,
                    Node mid,
                    Node right)
      • Node

        public Node​(int nodeType,
                    int line)
      • Node

        public Node​(int nodeType,
                    Node child,
                    int line)
      • Node

        public Node​(int nodeType,
                    Node left,
                    Node right,
                    int line)
      • Node

        public Node​(int nodeType,
                    Node left,
                    Node mid,
                    Node right,
                    int line)
    • Method Detail

      • newNumber

        public static Node newNumber​(double number)
      • newString

        public static Node newString​(java.lang.String str)
      • newString

        public static Node newString​(int type,
                                     java.lang.String str)
      • getType

        public int getType()
      • setType

        public Node setType​(int type)
        Sets the node type and returns this node.
      • getJsDoc

        public java.lang.String getJsDoc()
        Gets the JsDoc comment string attached to this node.
        Returns:
        the comment string or null if no JsDoc is attached to this node
      • getJsDocNode

        public Comment getJsDocNode()
        Gets the JsDoc Comment object attached to this node.
        Returns:
        the Comment or null if no JsDoc is attached to this node
      • setJsDocNode

        public void setJsDocNode​(Comment jsdocNode)
        Sets the JsDoc comment string attached to this node.
      • hasChildren

        public boolean hasChildren()
      • getFirstChild

        public Node getFirstChild()
      • getLastChild

        public Node getLastChild()
      • getNext

        public Node getNext()
      • getChildBefore

        public Node getChildBefore​(Node child)
      • getLastSibling

        public Node getLastSibling()
      • addChildToFront

        public void addChildToFront​(Node child)
      • addChildToBack

        public void addChildToBack​(Node child)
      • addChildrenToFront

        public void addChildrenToFront​(Node children)
      • addChildrenToBack

        public void addChildrenToBack​(Node children)
      • addChildBefore

        public void addChildBefore​(Node newChild,
                                   Node node)
        Add 'child' before 'node'.
      • addChildAfter

        public void addChildAfter​(Node newChild,
                                  Node node)
        Add 'child' after 'node'.
      • removeChild

        public void removeChild​(Node child)
      • replaceChild

        public void replaceChild​(Node child,
                                 Node newChild)
      • replaceChildAfter

        public void replaceChildAfter​(Node prevChild,
                                      Node newChild)
      • removeChildren

        public void removeChildren()
      • iterator

        public java.util.Iterator<Node> iterator()
        Returns an Iterator over the node's children.
        Specified by:
        iterator in interface java.lang.Iterable<Node>
      • propToString

        private static final java.lang.String propToString​(int propType)
      • removeProp

        public void removeProp​(int propType)
      • getProp

        public java.lang.Object getProp​(int propType)
      • getIntProp

        public int getIntProp​(int propType,
                              int defaultValue)
      • getExistingIntProp

        public int getExistingIntProp​(int propType)
      • putProp

        public void putProp​(int propType,
                            java.lang.Object prop)
      • putIntProp

        public void putIntProp​(int propType,
                               int prop)
      • getLineno

        public int getLineno()
        Return the line number recorded for this node.
        Returns:
        the line number
      • setLineno

        public void setLineno​(int lineno)
      • getDouble

        public final double getDouble()
        Can only be called when getType() == Token.NUMBER
      • setDouble

        public final void setDouble​(double number)
      • getString

        public final java.lang.String getString()
        Can only be called when node has String context.
      • setString

        public final void setString​(java.lang.String s)
        Can only be called when node has String context.
      • getScope

        public Scope getScope()
        Can only be called when node has String context.
      • setScope

        public void setScope​(Scope s)
        Can only be called when node has String context.
      • newTarget

        public static Node newTarget()
      • labelId

        public final int labelId()
      • labelId

        public void labelId​(int labelId)
      • hasConsistentReturnUsage

        public boolean hasConsistentReturnUsage()
        Checks that every return usage in a function body is consistent with the requirements of strict-mode.
        Returns:
        true if the function satisfies strict mode requirement.
      • endCheckIf

        private int endCheckIf()
        Returns in the then and else blocks must be consistent with each other. If there is no else block, then the return statement can fall through.
        Returns:
        logical OR of END_* flags
      • endCheckSwitch

        private int endCheckSwitch()
        Consistency of return statements is checked between the case statements. If there is no default, then the switch can fall through. If there is a default,we check to see if all code paths in the default return or if there is a code path that can fall through.
        Returns:
        logical OR of END_* flags
      • endCheckTry

        private int endCheckTry()
        If the block has a finally, return consistency is checked in the finally block. If all code paths in the finally returns, then the returns in the try-catch blocks don't matter. If there is a code path that does not return or if there is no finally block, the returns of the try and catch blocks are checked for mismatch.
        Returns:
        logical OR of END_* flags
      • endCheckLoop

        private int endCheckLoop()
        Return statement in the loop body must be consistent. The default assumption for any kind of a loop is that it will eventually terminate. The only exception is a loop with a constant true condition. Code that follows such a loop is examined only if one can statically determine that there is a break out of the loop.
          for(<> ; <>; <>) {}
          for(<> in <> ) {}
          while(<>) { }
          do { } while(<>)
         
        Returns:
        logical OR of END_* flags
      • endCheckBlock

        private int endCheckBlock()
        A general block of code is examined statement by statement. If any statement (even compound ones) returns in all branches, then subsequent statements are not examined.
        Returns:
        logical OR of END_* flags
      • endCheckLabel

        private int endCheckLabel()
        A labelled statement implies that there maybe a break to the label. The function processes the labelled statement and then checks the CONTROL_BLOCK_PROP property to see if there is ever a break to the particular label.
        Returns:
        logical OR of END_* flags
      • endCheckBreak

        private int endCheckBreak()
        When a break is encountered annotate the statement being broken out of by setting its CONTROL_BLOCK_PROP property.
        Returns:
        logical OR of END_* flags
      • endCheck

        private int endCheck()
        endCheck() examines the body of a function, doing a basic reachability analysis and returns a combination of flags END_* flags that indicate how the function execution can terminate. These constitute only the pessimistic set of termination conditions. It is possible that at runtime certain code paths will never be actually taken. Hence this analysis will flag errors in cases where there may not be errors.
        Returns:
        logical OR of END_* flags
      • hasSideEffects

        public boolean hasSideEffects()
      • resetTargets

        public void resetTargets()
        Recursively unlabel every TARGET or YIELD node in the tree. This is used and should only be used for inlining finally blocks where jsr instructions used to be. It is somewhat hackish, but implementing a clone() operation would take much, much more effort. This solution works for inlining finally blocks because you should never be writing any given block to the class file simultaneously. Therefore, an unlabeling will never occur in the middle of a block.
      • resetTargets_r

        private void resetTargets_r()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        private void toString​(ObjToIntMap printIds,
                              java.lang.StringBuilder sb)
      • toStringTree

        public java.lang.String toStringTree​(ScriptNode treeTop)
      • toStringTreeHelper

        private static void toStringTreeHelper​(ScriptNode treeTop,
                                               Node n,
                                               ObjToIntMap printIds,
                                               int level,
                                               java.lang.StringBuilder sb)
      • generatePrintIds

        private static void generatePrintIds​(Node n,
                                             ObjToIntMap map)
      • appendPrintId

        private static void appendPrintId​(Node n,
                                          ObjToIntMap printIds,
                                          java.lang.StringBuilder sb)