Class Node
java.lang.Object
com.oracle.truffle.api.nodes.Node
- All Implemented Interfaces:
NodeInterface,Cloneable
- Direct Known Subclasses:
BlockNode,DirectCallNode,ExecutableNode,ExecutionEventNode,IndirectCallNode,Library,LoopNode,MutableTruffleString.AsManagedNode,MutableTruffleString.AsMutableTruffleStringNode,MutableTruffleString.ConcatNode,MutableTruffleString.ForceEncodingNode,MutableTruffleString.FromByteArrayNode,MutableTruffleString.FromNativePointerNode,MutableTruffleString.SubstringByteIndexNode,MutableTruffleString.SubstringNode,MutableTruffleString.SwitchEncodingNode,MutableTruffleString.WriteByteNode,ProbeNode,TruffleString.AsManagedNode,TruffleString.AsNativeNode,TruffleString.AsTruffleStringNode,TruffleString.ByteIndexOfAnyByteNode,TruffleString.ByteIndexOfCodePointNode,TruffleString.ByteIndexOfCodePointSetNode,TruffleString.ByteIndexOfStringNode,TruffleString.ByteIndexToCodePointIndexNode,TruffleString.ByteLengthOfCodePointNode,TruffleString.CharIndexOfAnyCharUTF16Node,TruffleString.CodePointAtByteIndexNode,TruffleString.CodePointAtIndexNode,TruffleString.CodePointIndexToByteIndexNode,TruffleString.CodePointLengthNode,TruffleString.CodeRangeEqualsNode,TruffleString.CompareBytesNode,TruffleString.CompareCharsUTF16Node,TruffleString.CompareIntsUTF32Node,TruffleString.ConcatNode,TruffleString.CopyToByteArrayNode,TruffleString.CopyToNativeMemoryNode,TruffleString.CreateBackwardCodePointIteratorNode,TruffleString.CreateCodePointIteratorNode,TruffleString.EqualNode,TruffleString.ForceEncodingNode,TruffleString.FromByteArrayNode,TruffleString.FromCharArrayUTF16Node,TruffleString.FromCodePointNode,TruffleString.FromIntArrayUTF32Node,TruffleString.FromJavaStringNode,TruffleString.FromLongNode,TruffleString.FromNativePointerNode,TruffleString.GetByteCodeRangeNode,TruffleString.GetCodeRangeImpreciseNode,TruffleString.GetCodeRangeNode,TruffleString.GetInternalByteArrayNode,TruffleString.GetInternalNativePointerNode,TruffleString.GetStringCompactionLevelNode,TruffleString.HashCodeNode,TruffleString.IndexOfCodePointNode,TruffleString.IndexOfStringNode,TruffleString.IntIndexOfAnyIntUTF32Node,TruffleString.IsValidNode,TruffleString.LastByteIndexOfCodePointNode,TruffleString.LastByteIndexOfStringNode,TruffleString.LastIndexOfCodePointNode,TruffleString.LastIndexOfStringNode,TruffleString.MaterializeNode,TruffleString.ParseDoubleNode,TruffleString.ParseIntNode,TruffleString.ParseLongNode,TruffleString.ReadByteNode,TruffleString.ReadCharUTF16Node,TruffleString.RegionEqualByteIndexNode,TruffleString.RegionEqualNode,TruffleString.RepeatNode,TruffleString.SubstringByteIndexNode,TruffleString.SubstringNode,TruffleString.SwitchEncodingNode,TruffleString.ToJavaStringNode,TruffleString.ToValidStringNode,TruffleString.WithMask.CreateNode,TruffleString.WithMask.CreateUTF16Node,TruffleString.WithMask.CreateUTF32Node,TruffleStringBuilder.AppendByteNode,TruffleStringBuilder.AppendCharUTF16Node,TruffleStringBuilder.AppendCodePointNode,TruffleStringBuilder.AppendIntNumberNode,TruffleStringBuilder.AppendJavaStringUTF16Node,TruffleStringBuilder.AppendLongNumberNode,TruffleStringBuilder.AppendStringNode,TruffleStringBuilder.AppendSubstringByteIndexNode,TruffleStringBuilder.ToStringNode,TruffleStringIterator.NextNode,TruffleStringIterator.PreviousNode
Abstract base class for all Truffle nodes.
- Since:
- 0.8 or earlier
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceMarks fields that represent child nodes of this node.static @interfaceMarks array fields that are children of this node. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaccept(NodeVisitor nodeVisitor) Invokes theNodeVisitor.visit(Node)method for this node and recursively also for all child nodes.final voidfinal voidfinal <T> Tcopy()Creates a shallow copy of this node.deepCopy()Creates a deep copy of this node.Iterator over the children of this node.getCost()Deprecated.in 24.1 without replacementReturns properties of this node interesting for debugging and can be overwritten by subclasses to add their own custom properties.Returns a user-readable description of the purpose of the Node, or "" if no description is available.Retrieves the segment of guest language source code that is represented by this Node, if present; otherwise retrieves the segment represented by the nearest AST ancestor that has this information.protected final LockgetLock()Returns a lock object that can be used to synchronize modifications to the AST.final NodeThe current parent node of this node.final RootNodeGet the root node of the tree a node belongs to.Retrieves the segment of guest language source code that is represented by this Node.final <T extends Node>
Tinsert(T newChild) final <T extends Node>
T[]insert(T[] newChildren) booleanReturnstrueif this node can be adopted by a parent.final booleanisSafelyReplaceableBy(Node newNode) Checks if this node can be replaced by another node: tree structure & type.protected final voidnotifyInserted(Node node) Notifies the framework about the insertion of one or more nodes during execution.protected voidonReplace(Node newNode, CharSequence reason) Intended to be implemented by subclasses ofNodeto receive a notification when the node is rewritten.final <T extends Node>
Treplace(T newNode) Replaces this node with another node.final <T extends Node>
Treplace(T newNode, CharSequence reason) Replaces this node with another node.final voidNotifies the runtime that this node specialized to a polymorphic state.toString()Converts this node to a textual representation useful for debugging.
-
Constructor Details
-
Node
protected Node()- Since:
- 0.8 or earlier
-
-
Method Details
-
getCost
Deprecated.in 24.1 without replacementReturns 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.- Since:
- 0.8 or earlier
-
getSourceSection
Retrieves the segment of guest language source code that is represented by this Node. The default implementation of this method returnsnull. If your node represents a segment of the source code, override this method and return a eagerly or lazily computed source section value. This method is not designed to be invoked on compiled code paths. May be called on any thread and without a language context being active.Simple example implementation using a simple implementation using a field:
abstract class SimpleNode extends Node { private SourceSection sourceSection; void setSourceSection(SourceSection sourceSection) { this.sourceSection = sourceSection; } @Override public SourceSection getSourceSection() { return sourceSection; } }Recommended implementation computing the source section lazily from primitive fields:
abstract class RecommendedNode extends Node { private static final int NO_SOURCE = -1; private int sourceCharIndex = NO_SOURCE; private int sourceLength; public abstract Object execute(VirtualFrame frame); // invoked by the parser to set the source void setSourceSection(int charIndex, int length) { assert sourceCharIndex == NO_SOURCE : "source should only be set once"; this.sourceCharIndex = charIndex; this.sourceLength = length; } @Override public final SourceSection getSourceSection() { if (sourceCharIndex == NO_SOURCE) { // AST node without source return null; } RootNode rootNode = getRootNode(); if (rootNode == null) { // not yet adopted yet return null; } Source source = rootNode.getSourceSection().getSource(); return source.createSection(sourceCharIndex, sourceLength); } }- Returns:
- the source code represented by this Node
- Since:
- 0.8 or earlier
-
getEncapsulatingSourceSection
Retrieves the segment of guest language source code that is represented by this Node, if present; otherwise retrieves the segment represented by the nearest AST ancestor that has this information. Can be called on any thread and without a language context.- Returns:
- an approximation of the source code represented by this Node
- Since:
- 0.8 or earlier
-
isAdoptable
public boolean isAdoptable()Returnstrueif this node can be adopted by a parent. This method is intended to be overriden by subclasses. If nodes need to be statically shared that they must not be adoptable, because otherwise the parent reference might cause a memory leak. If a node is not adoptable then then it is guaranteed that theparentpointer remainsnullat all times, even if the node is tried to be adopted by a parent.If the result of this method is statically known then it is recommended to make the node implement
UnadoptableNodeinstead.Implementations of
isAdoptable()are required to fold to a constant result when compiled with a constant receiver.- Since:
- 19.0
-
insert
-
insert
-
notifyInserted
Notifies the framework about the insertion of one or more nodes during execution. Otherwise, the framework assumes thatinstrumentablenodes remain unchanged after their root node is firstexecuted. Insertions don't need to be notified if it is known that none of the inserted nodes areinstrumentable.The provided
Nodeand its children must beadoptedin the AST before invoking this method. The caller must ensure that this method is invoked only once for a given node and its children.class MyRootNode extends RootNode { protected MyRootNode(MyLanguage language) { super(language); } @Child InstrumentableLanguageNode child; @Override public Object execute(VirtualFrame frame) { if (child == null) { CompilerDirectives.transferToInterpreterAndInvalidate(); child = insert(new InstrumentableLanguageNode()); notifyInserted(child); } return child.execute(frame); } }- Parameters:
node- the node tree that got inserted.- Since:
- 0.27
-
adoptChildren
public final void adoptChildren()- Since:
- 0.8 or earlier
-
getDebugProperties
-
getParent
The current parent node of this node.- Returns:
- the parent node
- Since:
- 0.8 or earlier
-
replace
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
- Since:
- 0.8 or earlier
-
replace
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
- Since:
- 0.8 or earlier
-
isSafelyReplaceableBy
Checks if this node can be replaced by another node: tree structure & type.- Since:
- 0.8 or earlier
-
onReplace
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- Since:
- 0.8 or earlier
-
accept
Invokes theNodeVisitor.visit(Node)method for this node and recursively also for all child nodes.- Parameters:
nodeVisitor- the visitor- Since:
- 0.8 or earlier
-
getChildren
-
copy
-
deepCopy
Creates a deep copy of this node.- Returns:
- the new deep copy
- Since:
- 0.8 or earlier
-
getRootNode
-
reportPolymorphicSpecialize
public final void reportPolymorphicSpecialize()Notifies the runtime that this node specialized to a polymorphic state. This includes specializations that increase "level" of polymorphism (e.g. Adding another element to an existing inline cache). The runtime can use this information to, ifallowed, create a deep copy of theRootNodehosting this node and gather context sensitive profiling feedback.- Since:
- 0.33
-
toString
-
atomic
- Since:
- 0.8 or earlier
-
atomic
- Since:
- 0.8 or earlier
-
getLock
Returns a lock object that can be used to synchronize modifications to the AST. Don't lock if you call into foreign code with potential recursions to avoid deadlocks. Use responsibly.- Since:
- 0.19
-
getDescription
Returns a user-readable description of the purpose of the Node, or "" if no description is available. Can be called on any thread and without a language context.- Since:
- 0.8 or earlier
-