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 interface
Node.Child
Marks fields that represent child nodes of this node.static interface
Node.Children
Marks array fields that are children of this node.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Node()
protected
Node(SourceSection sourceSection)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
accept(NodeVisitor nodeVisitor)
Invokes theNodeVisitor.visit(Node)
method for this node and recursively also for all child nodes.void
adoptChildren()
void
assignSourceSection(SourceSection section)
Assigns a link to a guest language source section to this node.void
atomic(java.lang.Runnable closure)
<T> T
atomic(java.util.concurrent.Callable<T> closure)
void
clearSourceSection()
Clears any previously assigned guest language source code from this node.protected java.lang.Object
clone()
Deprecated.Node
copy()
Creates a shallow copy of this node.java.lang.Iterable<Node>
getChildren()
Iterator over the children of this node.NodeCost
getCost()
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.String
getDescription()
Returns a user-readable description of the purpose of the Node, or "" if no description is available.SourceSection
getEncapsulatingSourceSection()
Retrieves the guest language source code section that is currently assigned to this node.java.lang.String
getLanguage()
Returns a string representing the language this node has been implemented for.Node
getParent()
The current parent node of this node.RootNode
getRootNode()
Get the root node of the tree a node belongs to.SourceSection
getSourceSection()
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.boolean
isReplaceable()
Checks if this node is properly adopted by a parent and can be replaced.protected void
onAdopt()
Subclasses ofNode
can implement this method to execute extra functionality when a node is effectively inserted into the AST.protected void
onReplace(Node newNode, java.lang.CharSequence reason)
Intended to be implemented by subclasses ofNode
to 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.String
toString()
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 theNodeInfo
annotation declared at the subclass. If noNodeInfo
annotation is declared the method returnsNodeCost.MONOMORPHIC
as 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:
true
if it is safe to replace this node.
-
onReplace
protected void onReplace(Node newNode, java.lang.CharSequence reason)
Intended to be implemented by subclasses ofNode
to 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 ofNode
can implement this method to execute extra functionality when a node is effectively inserted into the AST. TheonAdopt
callback 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.CloneNotSupportedException
Deprecated.This method must never be called. It enforces thatObject.clone()
is not directly called by subclasses. Use thecopy()
method instead.- Overrides:
clone
in 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
RootNode
ornull
if there is none.
-
toString
public java.lang.String toString()
Converts this node to a textual representation useful for debugging.- Overrides:
toString
in 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 "".
-
-