Package com.oracle.truffle.api.nodes
Class Node
- java.lang.Object
-
- com.oracle.truffle.api.nodes.Node
-
- All Implemented Interfaces:
NodeInterface,java.lang.Cloneable
- Direct Known Subclasses:
DirectCallNode,IndirectCallNode,LoopNode,ProbeNode,RootNode
public abstract class Node extends java.lang.Object implements NodeInterface, java.lang.Cloneable
Abstract base class for all Truffle nodes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceNode.ChildMarks fields that represent child nodes of this node.static interfaceNode.ChildrenMarks array fields that are children of this node.
-
Constructor Summary
Constructors Modifier Constructor Description protectedNode()protectedNode(SourceSection sourceSection)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaccept(NodeVisitor nodeVisitor)Invokes theNodeVisitor.visit(Node)method for this node and recursively also for all child nodes.voidadoptChildren()voidassignSourceSection(SourceSection section)Assigns a link to a guest language source section to this node.voidatomic(java.lang.Runnable closure)<T> Tatomic(java.util.concurrent.Callable<T> closure)voidclearSourceSection()Clears any previously assigned guest language source code from this node.protected java.lang.Objectclone()Deprecated.Nodecopy()Creates a shallow copy of this node.java.lang.Iterable<Node>getChildren()Iterator over the children of this node.NodeCostgetCost()Returns a rough estimate for the cost of thisNode.java.util.Map<java.lang.String,java.lang.Object>getDebugProperties()Returns properties of this node interesting for debugging and can be overwritten by subclasses to add their own custom properties.java.lang.StringgetDescription()Returns a user-readable description of the purpose of the Node, or "" if no description is available.SourceSectiongetEncapsulatingSourceSection()Retrieves the guest language source code section that is currently assigned to this node.java.lang.StringgetLanguage()Returns a string representing the language this node has been implemented for.NodegetParent()The current parent node of this node.RootNodegetRootNode()Get the root node of the tree a node belongs to.SourceSectiongetSourceSection()Retrieves the guest language source code section that is currently assigned to this node.protected <T extends Node>
Tinsert(T newChild)Method that updates the link to the parent in the specified new child node to this node.protected <T extends Node>
T[]insert(T[] newChildren)Method that updates the link to the parent in the array of specified new child nodes to this node.booleanisReplaceable()Checks if this node is properly adopted by a parent and can be replaced.protected voidonAdopt()Subclasses ofNodecan implement this method to execute extra functionality when a node is effectively inserted into the AST.protected voidonReplace(Node newNode, java.lang.CharSequence reason)Intended to be implemented by subclasses ofNodeto receive a notification when the node is rewritten.<T extends Node>
Treplace(T newNode)Replaces this node with another node.<T extends Node>
Treplace(T newNode, java.lang.CharSequence reason)Replaces this node with another node.java.lang.StringtoString()Converts this node to a textual representation useful for debugging.
-
-
-
Constructor Detail
-
Node
protected Node()
-
Node
protected Node(SourceSection sourceSection)
-
-
Method Detail
-
assignSourceSection
public final void assignSourceSection(SourceSection section)
Assigns a link to a guest language source section to this node.- Parameters:
section- the object representing a section in guest language source code
-
getCost
public NodeCost getCost()
Returns a rough estimate for the cost of thisNode. This estimate can be used by runtime systems or guest languages to implement heuristics based on Truffle ASTs. This method is intended to be overridden by subclasses. The default implementation returns the value ofNodeInfo.cost()of theNodeInfoannotation declared at the subclass. If noNodeInfoannotation is declared the method returnsNodeCost.MONOMORPHICas a default value.
-
clearSourceSection
public final void clearSourceSection()
Clears any previously assigned guest language source code from this node.
-
getSourceSection
public final SourceSection getSourceSection()
Retrieves the guest language source code section that is currently assigned to this node.- Returns:
- the assigned source code section
-
getEncapsulatingSourceSection
public final SourceSection getEncapsulatingSourceSection()
Retrieves the guest language source code section that is currently assigned to this node.- Returns:
- the assigned source code section
-
insert
protected final <T extends Node> T[] insert(T[] newChildren)
Method that updates the link to the parent in the array of specified new child nodes to this node.- Parameters:
newChildren- the array of new children whose parent should be updated- Returns:
- the array of new children
-
insert
protected final <T extends Node> T insert(T newChild)
Method that updates the link to the parent in the specified new child node to this node.- Parameters:
newChild- the new child whose parent should be updated- Returns:
- the new child
-
adoptChildren
public final void adoptChildren()
-
getDebugProperties
public java.util.Map<java.lang.String,java.lang.Object> getDebugProperties()
Returns properties of this node interesting for debugging and can be overwritten by subclasses to add their own custom properties.- Returns:
- the properties as a key/value hash map
-
getParent
public final Node getParent()
The current parent node of this node.- Returns:
- the parent node
-
replace
public final <T extends Node> T replace(T newNode, java.lang.CharSequence reason)
Replaces this node with another node. If there is a source section (seegetSourceSection()) associated with this node, it is transferred to the new node.- Parameters:
newNode- the new node that is the replacementreason- a description of the reason for the replacement- Returns:
- the new node
-
replace
public final <T extends Node> T replace(T newNode)
Replaces this node with another node. If there is a source section (seegetSourceSection()) associated with this node, it is transferred to the new node.- Parameters:
newNode- the new node that is the replacement- Returns:
- the new node
-
isReplaceable
public final boolean isReplaceable()
Checks if this node is properly adopted by a parent and can be replaced.- Returns:
trueif it is safe to replace this node.
-
onReplace
protected void onReplace(Node newNode, java.lang.CharSequence reason)
Intended to be implemented by subclasses ofNodeto receive a notification when the node is rewritten. This method is invoked before the actual replace has happened.- Parameters:
newNode- the replacement nodereason- the reason the replace supplied
-
onAdopt
protected void onAdopt()
Subclasses ofNodecan implement this method to execute extra functionality when a node is effectively inserted into the AST. TheonAdoptcallback is called after the node has been effectively inserted, and it is guaranteed to be called only once for any given node.
-
accept
public final void accept(NodeVisitor nodeVisitor)
Invokes theNodeVisitor.visit(Node)method for this node and recursively also for all child nodes.- Parameters:
nodeVisitor- the visitor
-
getChildren
public final java.lang.Iterable<Node> getChildren()
Iterator over the children of this node.- Returns:
- the iterator
-
copy
public Node copy()
Creates a shallow copy of this node.- Returns:
- the new copy
-
clone
@Deprecated protected final java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionDeprecated.This method must never be called. It enforces thatObject.clone()is not directly called by subclasses. Use thecopy()method instead.- Overrides:
clonein classjava.lang.Object- Throws:
java.lang.CloneNotSupportedException
-
getRootNode
public final RootNode getRootNode()
Get the root node of the tree a node belongs to.- Returns:
- the
RootNodeornullif there is none.
-
toString
public java.lang.String toString()
Converts this node to a textual representation useful for debugging.- Overrides:
toStringin classjava.lang.Object
-
atomic
public final void atomic(java.lang.Runnable closure)
-
atomic
public final <T> T atomic(java.util.concurrent.Callable<T> closure)
-
getDescription
public java.lang.String getDescription()
Returns a user-readable description of the purpose of the Node, or "" if no description is available.
-
getLanguage
public java.lang.String getLanguage()
Returns a string representing the language this node has been implemented for. If the language is unknown, returns "".
-
-