Skip to content

Commit

Permalink
Adapt to YARP changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Sep 20, 2023
1 parent bb56d3b commit 3bb3466
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 86 deletions.
126 changes: 40 additions & 86 deletions src/main/java/org/truffleruby/parser/YARPTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,21 @@ public RubyRootNode translate(Nodes.Node node) {
Split.HEURISTIC, null, Arity.NO_ARGUMENTS);
}

public RubyNode visitAliasNode(Nodes.AliasNode node) {
RubyNode rubyNode;
@Override
public RubyNode visitAliasGlobalVariableNode(Nodes.AliasGlobalVariableNode node) {
RubyNode rubyNode = new AliasGlobalVarNode(toString(node.old_name), toString(node.new_name));

if (node.new_name instanceof Nodes.GlobalVariableReadNode &&
node.old_name instanceof Nodes.GlobalVariableReadNode) {
rubyNode = new AliasGlobalVarNode(
toString(node.old_name),
toString(node.new_name));
} else {
// expected InterpolatedSymbolNode (that should be evaluated in runtime)
// or SymbolNode
rubyNode = new ModuleNodes.AliasKeywordNode(
node.new_name.accept(this),
node.old_name.accept(this));
}
assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

@Override
public RubyNode visitAliasMethodNode(Nodes.AliasMethodNode node) {
// expected InterpolatedSymbolNode (that should be evaluated in runtime)
// or SymbolNode
RubyNode rubyNode = new ModuleNodes.AliasKeywordNode(
node.new_name.accept(this),
node.old_name.accept(this));

assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -452,14 +452,6 @@ public RubyNode visitCallNode(Nodes.CallNode node) {
return rubyCallNode;
}

public RubyNode visitCallOperatorAndWriteNode(Nodes.CallOperatorAndWriteNode node) {
return defaultVisit(node);
}

public RubyNode visitCallOperatorOrWriteNode(Nodes.CallOperatorOrWriteNode node) {
return defaultVisit(node);
}

public RubyNode visitCallOperatorWriteNode(Nodes.CallOperatorWriteNode node) {
return defaultVisit(node);
}
Expand All @@ -473,30 +465,21 @@ public RubyNode visitCaseNode(Nodes.CaseNode node) {
}

public RubyNode visitClassNode(Nodes.ClassNode node) {
final String name;
final RubyNode lexicalParent = translateCPath(node.constant_path);
final RubyNode superClass;

if (node.constant_path instanceof Nodes.ConstantReadNode constantNode) {
name = toString(constantNode);
} else if (node.constant_path instanceof Nodes.ConstantPathNode pathNode) {
name = toString(pathNode.child);
} else {
throw CompilerDirectives.shouldNotReachHere();
}

if (node.superclass != null) {
superClass = node.superclass.accept(this);
} else {
superClass = null;
}

final DefineClassNode defineOrGetClass = new DefineClassNode(name, lexicalParent, superClass);
final DefineClassNode defineOrGetClass = new DefineClassNode(node.name, lexicalParent, superClass);

final RubyNode rubyNode = openModule(
node,
defineOrGetClass,
name,
node.name,
node.body,
OpenModule.CLASS,
shouldUseDynamicConstantLookupForModuleBody(node));
Expand All @@ -508,7 +491,7 @@ public RubyNode visitClassNode(Nodes.ClassNode node) {
public RubyNode visitClassVariableReadNode(Nodes.ClassVariableReadNode node) {
final RubyNode rubyNode = new ReadClassVariableNode(
getLexicalScopeNode("class variable lookup", node),
toString(node));
node.name);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand All @@ -518,7 +501,7 @@ public RubyNode visitClassVariableWriteNode(Nodes.ClassVariableWriteNode node) {
final RubyNode rhs = node.value.accept(this);
final RubyNode rubyNode = new WriteClassVariableNode(
getLexicalScopeNode("set dynamic class variable", node),
toString(node.name),
node.name,
rhs);

assignNodePositionInSource(node, rubyNode);
Expand All @@ -528,7 +511,7 @@ public RubyNode visitClassVariableWriteNode(Nodes.ClassVariableWriteNode node) {
public RubyNode visitClassVariableTargetNode(Nodes.ClassVariableTargetNode node) {
final RubyNode rubyNode = new WriteClassVariableNode(
getLexicalScopeNode("set dynamic class variable", node),
toString(node.name),
node.name,
null);

assignNodePositionInSource(node, rubyNode);
Expand All @@ -538,7 +521,7 @@ public RubyNode visitClassVariableTargetNode(Nodes.ClassVariableTargetNode node)
public RubyNode visitConstantPathNode(Nodes.ConstantPathNode node) {
assert node.child instanceof Nodes.ConstantReadNode;

final String name = toString(node.child);
final String name = ((Nodes.ConstantReadNode) node.child).name;
final RubyNode moduleNode;

if (node.parent != null) {
Expand Down Expand Up @@ -567,7 +550,7 @@ public RubyNode visitConstantPathWriteNode(Nodes.ConstantPathWriteNode node) {
moduleNode = new ObjectClassLiteralNode();
}

final String name = toString(constantPathNode.child);
final String name = ((Nodes.ConstantReadNode) constantPathNode.child).name;
final RubyNode value = node.value.accept(this);
final RubyNode rubyNode = new WriteConstantNode(name, moduleNode, value);

Expand All @@ -585,7 +568,7 @@ public RubyNode visitConstantPathTargetNode(Nodes.ConstantPathTargetNode node) {
moduleNode = new ObjectClassLiteralNode();
}

final String name = toString(node.child);
final String name = ((Nodes.ConstantReadNode) node.child).name;
final RubyNode rubyNode = new WriteConstantNode(name, moduleNode, null);

assignNodePositionInSource(node, rubyNode);
Expand All @@ -594,38 +577,35 @@ public RubyNode visitConstantPathTargetNode(Nodes.ConstantPathTargetNode node) {

public RubyNode visitConstantReadNode(Nodes.ConstantReadNode node) {
final RubyNode rubyNode;
final String name = toString(node);

if (environment.isDynamicConstantLookup()) {
if (language.options.LOG_DYNAMIC_CONSTANT_LOOKUP) {
RubyLanguage.LOGGER.info(() -> "dynamic constant lookup at " +
RubyLanguage.getCurrentContext().fileLine(getSourceSection(node)));
}

rubyNode = new ReadConstantWithDynamicScopeNode(name);
rubyNode = new ReadConstantWithDynamicScopeNode(node.name);
} else {
final LexicalScope lexicalScope = environment.getStaticLexicalScope();
rubyNode = new ReadConstantWithLexicalScopeNode(lexicalScope, name);
rubyNode = new ReadConstantWithLexicalScopeNode(lexicalScope, node.name);
}

assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitConstantWriteNode(Nodes.ConstantWriteNode node) {
final String name = toString(node.name_loc);
final RubyNode value = node.value.accept(this);
final RubyNode moduleNode = getLexicalScopeModuleNode("set dynamic constant", node);
final RubyNode rubyNode = new WriteConstantNode(name, moduleNode, value);
final RubyNode rubyNode = new WriteConstantNode(node.name, moduleNode, value);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitConstantTargetNode(Nodes.ConstantTargetNode node) {
final String name = toString(node);
final RubyNode moduleNode = getLexicalScopeModuleNode("set dynamic constant", node);
final RubyNode rubyNode = new WriteConstantNode(name, moduleNode, null);
final RubyNode rubyNode = new WriteConstantNode(node.name, moduleNode, null);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -725,23 +705,21 @@ public RubyNode visitForwardingSuperNode(Nodes.ForwardingSuperNode node) {
}

public RubyNode visitGlobalVariableReadNode(Nodes.GlobalVariableReadNode node) {
final RubyNode rubyNode = ReadGlobalVariableNodeGen.create(toString(node));
final RubyNode rubyNode = ReadGlobalVariableNodeGen.create(node.name);
assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitGlobalVariableWriteNode(Nodes.GlobalVariableWriteNode node) {
final String name = toString(node.name);
final RubyNode value = node.value.accept(this);
final RubyNode rubyNode = WriteGlobalVariableNodeGen.create(name, value);
final RubyNode rubyNode = WriteGlobalVariableNodeGen.create(node.name, value);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitGlobalVariableTargetNode(Nodes.GlobalVariableTargetNode node) {
final String name = toString(node.name);
final RubyNode rubyNode = WriteGlobalVariableNodeGen.create(name, null);
final RubyNode rubyNode = WriteGlobalVariableNodeGen.create(node.name, null);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -848,25 +826,22 @@ public RubyNode visitInNode(Nodes.InNode node) {
}

public RubyNode visitInstanceVariableReadNode(Nodes.InstanceVariableReadNode node) {
final String name = toString(node);
final RubyNode rubyNode = new ReadInstanceVariableNode(name);
final RubyNode rubyNode = new ReadInstanceVariableNode(node.name);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitInstanceVariableWriteNode(Nodes.InstanceVariableWriteNode node) {
final String name = toString(node.name);
final RubyNode value = node.value.accept(this);
final RubyNode rubyNode = WriteInstanceVariableNodeGen.create(name, value);
final RubyNode rubyNode = WriteInstanceVariableNodeGen.create(node.name, value);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
}

public RubyNode visitInstanceVariableTargetNode(Nodes.InstanceVariableTargetNode node) {
final String name = toString(node.name);
final RubyNode rubyNode = WriteInstanceVariableNodeGen.create(name, null);
final RubyNode rubyNode = WriteInstanceVariableNodeGen.create(node.name, null);

assignNodePositionInSource(node, rubyNode);
return rubyNode;
Expand Down Expand Up @@ -954,8 +929,7 @@ public RubyNode visitInterpolatedSymbolNode(Nodes.InterpolatedSymbolNode node) {
}

public RubyNode visitInterpolatedXStringNode(Nodes.InterpolatedXStringNode node) {
final Nodes.InterpolatedStringNode stringNode = new Nodes.InterpolatedStringNode(
node.opening_loc, node.parts, node.closing_loc, node.startOffset, node.length);
var stringNode = new Nodes.InterpolatedStringNode(node.parts, node.startOffset, node.length);
final RubyNode string = stringNode.accept(this);
final RubyNode rubyNode = createCallNode(new SelfNode(), "`", string);

Expand All @@ -972,7 +946,7 @@ public RubyNode visitLambdaNode(Nodes.LambdaNode node) {
}

public RubyNode visitLocalVariableReadNode(Nodes.LocalVariableReadNode node) {
final String name = toString(node);
final String name = node.name;

final RubyNode rubyNode = environment.findLocalVarNode(name, null);
assert rubyNode != null : name;
Expand All @@ -982,7 +956,7 @@ public RubyNode visitLocalVariableReadNode(Nodes.LocalVariableReadNode node) {
}

public RubyNode visitLocalVariableWriteNode(Nodes.LocalVariableWriteNode node) {
final String name = toString(node.name);
final String name = node.name;

if (environment.getNeverAssignInParentScope()) {
environment.declareVar(name);
Expand Down Expand Up @@ -1016,8 +990,7 @@ public RubyNode visitLocalVariableWriteNode(Nodes.LocalVariableWriteNode node) {
public RubyNode visitLocalVariableTargetNode(Nodes.LocalVariableTargetNode node) {
// TODO: this could be done more directly but the logic of visitLocalVariableWriteNode() needs to be simpler first
return visitLocalVariableWriteNode(
new Nodes.LocalVariableWriteNode(node.name, node.depth, null, null, null, node.startOffset,
node.length));
new Nodes.LocalVariableWriteNode(node.name, node.depth, null, node.startOffset, node.length));
}

public RubyNode visitMatchPredicateNode(Nodes.MatchPredicateNode node) {
Expand All @@ -1033,23 +1006,14 @@ public RubyNode visitMissingNode(Nodes.MissingNode node) {
}

public RubyNode visitModuleNode(Nodes.ModuleNode node) {
final String name;
final RubyNode lexicalParent = translateCPath(node.constant_path);

if (node.constant_path instanceof Nodes.ConstantReadNode constantNode) {
name = toString(constantNode);
} else if (node.constant_path instanceof Nodes.ConstantPathNode pathNode) {
name = toString(pathNode.child);
} else {
throw CompilerDirectives.shouldNotReachHere();
}

final DefineModuleNode defineModuleNode = DefineModuleNodeGen.create(name, lexicalParent);
final DefineModuleNode defineModuleNode = DefineModuleNodeGen.create(node.name, lexicalParent);

final RubyNode rubyNode = openModule(
node,
defineModuleNode,
name,
node.name,
node.body,
OpenModule.MODULE,
shouldUseDynamicConstantLookupForModuleBody(node));
Expand Down Expand Up @@ -1348,8 +1312,8 @@ public RubyNode visitWhileNode(Nodes.WhileNode node) {
}

public RubyNode visitXStringNode(Nodes.XStringNode node) {
final Nodes.StringNode stringNode = new Nodes.StringNode(
node.opening_loc, node.content_loc, node.closing_loc, node.unescaped, node.startOffset, node.length);
// TODO: pass flags, needs https://github.com/ruby/yarp/issues/1567
var stringNode = new Nodes.StringNode((short) 0, null, null, node.unescaped, node.startOffset, node.length);
final RubyNode string = stringNode.accept(this);
final RubyNode rubyNode = createCallNode(new SelfNode(), "`", string);

Expand Down Expand Up @@ -1694,16 +1658,6 @@ protected boolean isSideEffectFreeRescueExpression(Nodes.Node node) {
node instanceof Nodes.NilNode;
}

private String toString(byte[] bytes) {
return TStringUtils.toJavaStringOrThrow(
TruffleString.fromByteArrayUncached(bytes, sourceEncoding.tencoding, false), sourceEncoding);
}

private String toString(Nodes.Location location) {
return TStringUtils.toJavaStringOrThrow(TruffleString.fromByteArrayUncached(sourceBytes, location.startOffset,
location.length, sourceEncoding.tencoding, false), sourceEncoding);
}

private String toString(Nodes.Node node) {
return TStringUtils.toJavaStringOrThrow(TruffleString.fromByteArrayUncached(sourceBytes, node.startOffset,
node.length, sourceEncoding.tencoding, false), sourceEncoding);
Expand Down
2 changes: 2 additions & 0 deletions tool/import-yarp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set -e

YARP=../../yarp

export YARP_SERIALIZE_ONLY_SEMANTICS_FIELDS=1

# Create generated files
pushd $YARP
bundle
Expand Down

0 comments on commit 3bb3466

Please sign in to comment.