use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SourceSectionFilterTest method testColumnIn.
@Test
public void testColumnIn() {
Source sampleSource = Source.newBuilder("123456789\n123456789").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
Node root = createNode(sampleSource.createSection(0, 19));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 1).build(), root, createNode(sampleSource.createSection(1, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(1, 2).build(), root, createNode(sampleSource.createSection(0, 1), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(1, 1).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(4, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(1, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(0, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(0, 2), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(1, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(2, 2).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(1, 1).build(), root, createNode(sampleSource.createUnavailableSection())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(IndexRange.byLength(1, 1)).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().columnIn(IndexRange.byLength(1, 1), IndexRange.between(2, 3), IndexRange.byLength(3, 1)).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().columnIn(IndexRange.byLength(1, 1), IndexRange.byLength(3, 1)).build(), root, createNode(sampleSource.createSection(1, 1), tags())));
Assert.assertNotNull(SourceSectionFilter.newBuilder().columnIn(2, 2).build().toString());
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class DefaultNearestNodeSearch method findChildTaggedNode.
/**
* Finds the nearest tagged {@link Node node}. See the algorithm description at
* {@link InstrumentableNode#findNearestNodeAt(int, Set)}.
*/
private static Node findChildTaggedNode(Node node, int offset, Set<Class<? extends Tag>> tags, boolean haveOuterCandidate, boolean preferFirst) {
Node[] highestLowerNode = new Node[] { null };
Node[] highestLowerTaggedNode = new Node[] { null };
Node[] lowestHigherNode = new Node[] { null };
Node[] lowestHigherTaggedNode = new Node[] { null };
final Node[] foundNode = new Node[] { null };
NodeUtil.forEachChild(node, new NodeVisitor() {
int highestLowerNodeIndex = 0;
int highestLowerTaggedNodeIndex = 0;
int lowestHigherNodeIndex = 0;
int lowestHigherTaggedNodeIndex = 0;
@Override
public boolean visit(Node childNode) {
Node ch = childNode;
if (ch instanceof WrapperNode) {
ch = ((WrapperNode) ch).getDelegateNode();
}
SourceSection ss;
if (ch instanceof InstrumentableNode && ((InstrumentableNode) ch).isInstrumentable()) {
ch = (Node) ((InstrumentableNode) ch).materializeInstrumentableNodes(tags);
ss = ch.getSourceSection();
if (ss == null) {
return true;
}
} else {
// An unknown node, process its children on the same level.
NodeUtil.forEachChild(ch, this);
return foundNode[0] == null;
}
boolean isTagged = isTaggedWith((InstrumentableNode) ch, tags);
int i1 = ss.getCharIndex();
int i2 = getCharEndIndex(ss);
if (isTagged && offset == i1) {
// We're at it
foundNode[0] = ch;
return false;
}
if (i1 <= offset && offset <= i2) {
// In an encapsulating source section
Node taggedNode = findChildTaggedNode(ch, offset, tags, isTagged || haveOuterCandidate, preferFirst);
if (taggedNode != null) {
foundNode[0] = taggedNode;
return false;
}
if (isTagged) {
// If nothing in and is tagged, return it
foundNode[0] = ch;
return false;
}
}
if (offset < i1) {
// We're after the offset
if (lowestHigherNode[0] == null || lowestHigherNodeIndex > i1) {
lowestHigherNode[0] = ch;
lowestHigherNodeIndex = i1;
}
if (isTagged) {
if (lowestHigherTaggedNode[0] == null || lowestHigherTaggedNodeIndex > i1) {
lowestHigherTaggedNode[0] = ch;
lowestHigherTaggedNodeIndex = i1;
}
}
}
if (i2 < offset) {
// We're before the offset
if (highestLowerNode[0] == null || (preferFirst ? i1 < highestLowerNodeIndex : highestLowerNodeIndex < i1)) {
highestLowerNode[0] = ch;
highestLowerNodeIndex = i1;
}
if (isTagged) {
if (highestLowerTaggedNode[0] == null || (preferFirst ? i1 < highestLowerTaggedNodeIndex : highestLowerTaggedNodeIndex < i1)) {
highestLowerTaggedNode[0] = ch;
highestLowerTaggedNodeIndex = i1;
}
}
}
return true;
}
});
if (foundNode[0] != null) {
return foundNode[0];
}
Node primaryNode;
Node primaryTaggedNode;
Node secondaryNode;
Node secondaryTaggedNode;
if (preferFirst) {
// Prefer node before the offset:
primaryNode = highestLowerNode[0];
primaryTaggedNode = highestLowerTaggedNode[0];
secondaryNode = lowestHigherNode[0];
secondaryTaggedNode = lowestHigherTaggedNode[0];
} else {
// Prefer node after the offset:
primaryNode = lowestHigherNode[0];
primaryTaggedNode = lowestHigherTaggedNode[0];
secondaryNode = highestLowerNode[0];
secondaryTaggedNode = highestLowerTaggedNode[0];
}
if (isTaggedWith(primaryNode, tags)) {
return primaryNode;
}
if (isTaggedWith(secondaryNode, tags)) {
return secondaryNode;
}
// Try to go in the preferred node:
Node taggedNode = null;
if (!haveOuterCandidate) {
if (primaryNode != null) {
taggedNode = findChildTaggedNode(primaryNode, offset, tags, haveOuterCandidate, true);
}
}
if (taggedNode == null && primaryTaggedNode != null) {
return primaryTaggedNode;
}
// Try to go in a node before:
if (!haveOuterCandidate) {
if (taggedNode == null && secondaryNode != null) {
taggedNode = findChildTaggedNode(secondaryNode, offset, tags, haveOuterCandidate, true);
}
}
if (taggedNode == null && secondaryTaggedNode != null) {
return secondaryTaggedNode;
}
return taggedNode;
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class DefaultNearestNodeSearch method findLastNode.
private static Node findLastNode(Node contextNode, Set<Class<? extends Tag>> tags) {
if (isTaggedWith(contextNode, tags)) {
return contextNode;
}
List<Node> children = NodeUtil.findNodeChildren(contextNode);
for (int i = children.size() - 1; i >= 0; i--) {
Node ch = children.get(i);
if (ch instanceof WrapperNode) {
ch = ((WrapperNode) ch).getDelegateNode();
}
Node last = findLastNode(ch, tags);
if (last != null) {
return last;
}
}
return null;
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class UnwindInstrumentationReturnSnippets method isLanguageContextInitialized.
/**
* Test if language context of the source of the event is initialized.
*
* @since 0.26
*/
public boolean isLanguageContextInitialized() {
CompilerAsserts.neverPartOfCompilation();
Node node = getInstrumentedNode();
if (node == null) {
return true;
}
RootNode root = node.getRootNode();
if (root == null) {
return true;
}
LanguageInfo languageInfo = root.getLanguageInfo();
Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo);
return AccessorInstrumentHandler.langAccess().isContextInitialized(env);
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class UnwindInstrumentationReturnSnippets method validEventContext.
@SuppressWarnings("unchecked")
boolean validEventContext() {
Node node = getInstrumentedNode();
if (node instanceof RootNode) {
throw new IllegalStateException("Instrumentable node must not be a root node.");
}
Object object = null;
if (node instanceof InstrumentableNode) {
object = ((InstrumentableNode) node).getNodeObject();
} else {
// legacy support
return true;
}
if (object != null) {
assert AccessorInstrumentHandler.interopAccess().isValidNodeObject(object);
}
boolean foundStandardTag = false;
for (Class<?> clazz : StandardTags.ALL_TAGS) {
if (hasTag((Class<? extends Tag>) clazz)) {
foundStandardTag = true;
}
}
if (foundStandardTag) {
RootNode root = probeNode.getRootNode();
if (root != null && root.getSourceSection() != null) {
assert sourceSection != null : "All nodes tagged with a standard tag and with a root node that has a source section must also have a source section.";
}
}
return true;
}
Aggregations