Class Node
- Direct Known Subclasses:
AstNode
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassIterates over the children of this Node.private static class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intThese flags enumerate the possible ways a statement/function can terminate.static final intstatic final intprotected Nodestatic final intstatic final intstatic final intstatic final intstatic final intstatic final intprotected Nodestatic final intstatic final intprotected intstatic final intstatic final intstatic final intstatic final intprotected Nodestatic final intprivate static final Nodestatic final intstatic final intstatic final intstatic final intprotected Node.PropListItemLinked list of properties.static final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intprotected intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChildAfter(Node newChild, Node node) Add 'child' after 'node'.voidaddChildBefore(Node newChild, Node node) Add 'child' before 'node'.voidaddChildrenToBack(Node children) voidaddChildrenToFront(Node children) voidaddChildToBack(Node child) voidaddChildToFront(Node child) private static voidappendPrintId(Node n, ObjToIntMap printIds, StringBuilder sb) private intendCheck()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.private intA general block of code is examined statement by statement.private intWhen a break is encountered annotate the statement being broken out of by setting its CONTROL_BLOCK_PROP property.private intReturns in the then and else blocks must be consistent with each other.private intA labelled statement implies that there maybe a break to the label.private intReturn statement in the loop body must be consistent.private intConsistency of return statements is checked between the case statements.private intIf the block has a finally, return consistency is checked in the finally block.private Node.PropListItemensureProperty(int propType) private static voidgeneratePrintIds(Node n, ObjToIntMap map) Can only be called whengetType() == Token.BIGINTgetChildBefore(Node child) final doubleCan only be called whengetType() == Token.NUMBERintgetExistingIntProp(int propType) intgetIntProp(int propType, int defaultValue) getJsDoc()Gets the JsDoc comment string attached to this node.Gets the JsDoc Comment object attached to this node.intReturn the line number recorded for this node.getNext()getProp(int propType) getScope()Can only be called when node has String context.final StringCan only be called when node has String context.intgetType()booleanbooleanChecks that every return usage in a function body is consistent with the requirements of strict-mode.booleaniterator()Returns anIteratorover the node's children.final intlabelId()voidlabelId(int labelId) private Node.PropListItemlookupProperty(int propType) static NodenewNumber(double number) static Nodestatic Nodestatic Nodeprivate static final StringpropToString(int propType) voidputIntProp(int propType, int prop) voidvoidremoveChild(Node child) voidvoidremoveProp(int propType) voidreplaceChild(Node child, Node newChild) voidreplaceChildAfter(Node prevChild, Node newChild) voidRecursively unlabel every TARGET or YIELD node in the tree.private voidvoidsetBigInt(BigInteger bigInt) final voidsetDouble(double number) voidsetJsDocNode(Comment jsdocNode) Sets the JsDoc comment string attached to this node.voidsetLineno(int lineno) voidCan only be called when node has String context.final voidCan only be called when node has String context.setType(int type) Sets the node type and returns this node.toString()private voidtoString(ObjToIntMap printIds, StringBuilder sb) toStringTree(ScriptNode treeTop) private static voidtoStringTreeHelper(ScriptNode treeTop, Node n, ObjToIntMap printIds, int level, StringBuilder sb) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
FUNCTION_PROP
public static final int FUNCTION_PROP- See Also:
-
LOCAL_PROP
public static final int LOCAL_PROP- See Also:
-
LOCAL_BLOCK_PROP
public static final int LOCAL_BLOCK_PROP- See Also:
-
REGEXP_PROP
public static final int REGEXP_PROP- See Also:
-
CASEARRAY_PROP
public static final int CASEARRAY_PROP- See Also:
-
TARGETBLOCK_PROP
public static final int TARGETBLOCK_PROP- See Also:
-
VARIABLE_PROP
public static final int VARIABLE_PROP- See Also:
-
ISNUMBER_PROP
public static final int ISNUMBER_PROP- See Also:
-
DIRECTCALL_PROP
public static final int DIRECTCALL_PROP- See Also:
-
SPECIALCALL_PROP
public static final int SPECIALCALL_PROP- See Also:
-
SKIP_INDEXES_PROP
public static final int SKIP_INDEXES_PROP- See Also:
-
OBJECT_IDS_PROP
public static final int OBJECT_IDS_PROP- See Also:
-
INCRDECR_PROP
public static final int INCRDECR_PROP- See Also:
-
CATCH_SCOPE_PROP
public static final int CATCH_SCOPE_PROP- See Also:
-
LABEL_ID_PROP
public static final int LABEL_ID_PROP- See Also:
-
MEMBER_TYPE_PROP
public static final int MEMBER_TYPE_PROP- See Also:
-
NAME_PROP
public static final int NAME_PROP- See Also:
-
CONTROL_BLOCK_PROP
public static final int CONTROL_BLOCK_PROP- See Also:
-
PARENTHESIZED_PROP
public static final int PARENTHESIZED_PROP- See Also:
-
GENERATOR_END_PROP
public static final int GENERATOR_END_PROP- See Also:
-
DESTRUCTURING_ARRAY_LENGTH
public static final int DESTRUCTURING_ARRAY_LENGTH- See Also:
-
DESTRUCTURING_NAMES
public static final int DESTRUCTURING_NAMES- See Also:
-
DESTRUCTURING_PARAMS
public static final int DESTRUCTURING_PARAMS- See Also:
-
JSDOC_PROP
public static final int JSDOC_PROP- See Also:
-
EXPRESSION_CLOSURE_PROP
public static final int EXPRESSION_CLOSURE_PROP- See Also:
-
SHORTHAND_PROPERTY_NAME
public static final int SHORTHAND_PROPERTY_NAME- See Also:
-
ARROW_FUNCTION_PROP
public static final int ARROW_FUNCTION_PROP- See Also:
-
TEMPLATE_LITERAL_PROP
public static final int TEMPLATE_LITERAL_PROP- See Also:
-
TRAILING_COMMA
public static final int TRAILING_COMMA- See Also:
-
LAST_PROP
public static final int LAST_PROP- See Also:
-
BOTH
public static final int BOTH- See Also:
-
LEFT
public static final int LEFT- See Also:
-
RIGHT
public static final int RIGHT- See Also:
-
NON_SPECIALCALL
public static final int NON_SPECIALCALL- See Also:
-
SPECIALCALL_EVAL
public static final int SPECIALCALL_EVAL- See Also:
-
SPECIALCALL_WITH
public static final int SPECIALCALL_WITH- See Also:
-
DECR_FLAG
public static final int DECR_FLAG- See Also:
-
POST_FLAG
public static final int POST_FLAG- See Also:
-
PROPERTY_FLAG
public static final int PROPERTY_FLAG- See Also:
-
ATTRIBUTE_FLAG
public static final int ATTRIBUTE_FLAG- See Also:
-
DESCENDANTS_FLAG
public static final int DESCENDANTS_FLAG- See Also:
-
NOT_SET
-
END_UNREACHED
public static final int END_UNREACHEDThese 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:
-
END_DROPS_OFF
public static final int END_DROPS_OFF- See Also:
-
END_RETURNS
public static final int END_RETURNS- See Also:
-
END_RETURNS_VALUE
public static final int END_RETURNS_VALUE- See Also:
-
END_YIELDS
public static final int END_YIELDS- See Also:
-
type
protected int type -
next
-
first
-
last
-
lineno
protected int lineno -
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 Details
-
Node
public Node(int nodeType) -
Node
-
Node
-
Node
-
Node
public Node(int nodeType, int line) -
Node
-
Node
-
Node
-
-
Method Details
-
newNumber
-
newString
-
newString
-
getType
public int getType() -
setType
Sets the node type and returns this node. -
getJsDoc
Gets the JsDoc comment string attached to this node.- Returns:
- the comment string or
nullif no JsDoc is attached to this node
-
getJsDocNode
Gets the JsDoc Comment object attached to this node.- Returns:
- the Comment or
nullif no JsDoc is attached to this node
-
setJsDocNode
Sets the JsDoc comment string attached to this node. -
hasChildren
public boolean hasChildren() -
getFirstChild
-
getLastChild
-
getNext
-
getChildBefore
-
getLastSibling
-
addChildToFront
-
addChildToBack
-
addChildrenToFront
-
addChildrenToBack
-
addChildBefore
Add 'child' before 'node'. -
addChildAfter
Add 'child' after 'node'. -
removeChild
-
replaceChild
-
replaceChildAfter
-
removeChildren
public void removeChildren() -
iterator
Returns anIteratorover the node's children. -
propToString
-
lookupProperty
-
ensureProperty
-
removeProp
public void removeProp(int propType) -
getProp
-
getIntProp
public int getIntProp(int propType, int defaultValue) -
getExistingIntProp
public int getExistingIntProp(int propType) -
putProp
-
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 whengetType() == Token.NUMBER -
setDouble
public final void setDouble(double number) -
getBigInt
Can only be called whengetType() == Token.BIGINT -
setBigInt
-
getString
Can only be called when node has String context. -
setString
Can only be called when node has String context. -
getScope
Can only be called when node has String context. -
setScope
Can only be called when node has String context. -
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
-
toString
-
toStringTree
-
toStringTreeHelper
private static void toStringTreeHelper(ScriptNode treeTop, Node n, ObjToIntMap printIds, int level, StringBuilder sb) -
generatePrintIds
-
appendPrintId
-