Skip to content

Commit

Permalink
[Truffle] Better tracking of parentSourcePosition in the AST translat…
Browse files Browse the repository at this point in the history
…ors.
  • Loading branch information
nirvdrum committed Mar 9, 2015
1 parent 50506f1 commit f9df82c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public RubyNode visitBlockNode(org.jruby.ast.BlockNode node) {

for (org.jruby.ast.Node child : node.childNodes()) {
if (child.getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);
}

final RubyNode translatedChild;
Expand All @@ -330,7 +330,7 @@ public RubyNode visitBlockNode(org.jruby.ast.BlockNode node) {
translatedChild = child.accept(this);
} finally {
if (child.getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = null;
parentSourceSection.pop();
}
}

Expand Down Expand Up @@ -360,12 +360,12 @@ public RubyNode visitBreakNode(org.jruby.ast.BreakNode node) {
RubyNode resultNode;

if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);

try {
resultNode = node.getValueNode().accept(this);
} finally {
parentSourceSection = null;
parentSourceSection.pop();
}
} else {
resultNode = node.getValueNode().accept(this);
Expand Down Expand Up @@ -1742,14 +1742,14 @@ public RubyNode visitLocalAsgnNode(org.jruby.ast.LocalAsgnNode node) {
rhs = new DeadNode(context, sourceSection, "null RHS of local variable assignment");
} else {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);
}

try {
rhs = node.getValueNode().accept(this);
} finally {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = null;
parentSourceSection.pop();
}
}
}
Expand Down Expand Up @@ -2184,14 +2184,14 @@ public RubyNode visitNextNode(org.jruby.ast.NextNode node) {
translatingNextExpression = true;

if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);
}

try {
resultNode = node.getValueNode().accept(this);
} finally {
if (node.getValueNode().getPosition() == InvalidSourcePosition.INSTANCE) {
parentSourceSection = null;
parentSourceSection.pop();
}

translatingNextExpression = t;
Expand All @@ -2202,7 +2202,7 @@ public RubyNode visitNextNode(org.jruby.ast.NextNode node) {

@Override
public RubyNode visitNilNode(org.jruby.ast.NilNode node) {
if (node.getPosition() == InvalidSourcePosition.INSTANCE && parentSourceSection == null) {
if (node.getPosition() == InvalidSourcePosition.INSTANCE && parentSourceSection.peek() == null) {
return new DeadNode(context, null, "nil node with no invalid source position - assumed to be implicit null");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public RubyNode compileFunctionNode(SourceSection sourceSection, String methodNa
RubyNode body;

if (bodyNode != null) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);

try {
body = bodyNode.accept(this);
} finally {
parentSourceSection = null;
parentSourceSection.pop();
}
} else {
body = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getNilObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public MethodDefinitionNode compileClassNode(SourceSection sourceSection, String
RubyNode body;

if (bodyNode != null) {
parentSourceSection = sourceSection;
parentSourceSection.push(sourceSection);

try {
body = bodyNode.accept(this);
} finally {
parentSourceSection = null;
parentSourceSection.pop();
}
} else {
body = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getNilObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public abstract class Translator extends org.jruby.ast.visitor.AbstractNodeVisitor<RubyNode> {

Expand All @@ -32,7 +33,7 @@ public abstract class Translator extends org.jruby.ast.visitor.AbstractNodeVisit
protected final RubyContext context;
protected final Source source;

protected SourceSection parentSourceSection;
protected Stack<SourceSection> parentSourceSection = new Stack<>();

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Mar 9, 2015

Contributor

Stack is softly deprecated - it's a legacy class based on Vector and is fully synchronised. It's like the old StringBuffer class. You probably want Deque and ArrayDeque.

This comment has been minimized.

Copy link
@eregon

eregon Mar 10, 2015

Member

It's really unfortunate these other 2 don't have a clean simple interface.


public Translator(Node currentNode, RubyContext context, Source source) {
this.currentNode = currentNode;
Expand All @@ -46,10 +47,10 @@ protected SourceSection translate(org.jruby.lexer.yacc.ISourcePosition sourcePos

public SourceSection translate(Source source, org.jruby.lexer.yacc.ISourcePosition sourcePosition) {
if (sourcePosition == InvalidSourcePosition.INSTANCE) {
if (parentSourceSection == null) {
if (parentSourceSection.peek() == null) {
throw new UnsupportedOperationException("Truffle doesn't want invalid positions - find a way to give me a real position!");
} else {
return parentSourceSection;
return parentSourceSection.peek();
}
} else if (sourcePosition instanceof DetailedSourcePosition) {
final DetailedSourcePosition detailedSourcePosition = (DetailedSourcePosition) sourcePosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ public RubyRootNode parse(Node currentNode, RubyContext context, Source source,
RubyNode truffleNode;

if (rootNode.getBodyNode() == null || rootNode.getBodyNode() instanceof org.jruby.ast.NilNode) {
translator.parentSourceSection = sharedMethodInfo.getSourceSection();
translator.parentSourceSection.push(sharedMethodInfo.getSourceSection());

try {
truffleNode = new ObjectLiteralNode(context, null, context.getCoreLibrary().getNilObject());
} finally {
translator.parentSourceSection = null;
translator.parentSourceSection.pop();
}
} else {
truffleNode = rootNode.getBodyNode().accept(translator);
Expand Down

0 comments on commit f9df82c

Please sign in to comment.