Skip to content

Commit 2691785

Browse files
committed
Remove ToStringOrSymbolNode usage in CreateCast
1 parent 1662114 commit 2691785

11 files changed

+108
-124
lines changed

src/main/java/org/truffleruby/builtins/PrimitiveNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public abstract class PrimitiveNode extends RubyContextSourceNode {
2222

2323
// The same as "undefined" in Ruby code
24-
protected static final Object FAILURE = NotProvided.INSTANCE;
24+
public static final Object FAILURE = NotProvided.INSTANCE;
2525

2626
@Override
2727
public RubyNode cloneUninitialized() {

src/main/java/org/truffleruby/core/cast/ToStringOrSymbolNode.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,50 @@
99
*/
1010
package org.truffleruby.core.cast;
1111

12+
import com.oracle.truffle.api.dsl.GenerateCached;
13+
import com.oracle.truffle.api.dsl.GenerateInline;
1214
import com.oracle.truffle.api.dsl.GenerateUncached;
13-
import com.oracle.truffle.api.dsl.NeverDefault;
15+
import com.oracle.truffle.api.nodes.Node;
1416
import org.truffleruby.core.string.RubyString;
1517
import org.truffleruby.core.symbol.RubySymbol;
1618
import org.truffleruby.core.string.ImmutableRubyString;
17-
import org.truffleruby.language.RubyBaseNodeWithExecute;
19+
import org.truffleruby.language.RubyBaseNode;
1820
import org.truffleruby.language.dispatch.DispatchNode;
1921

2022
import com.oracle.truffle.api.dsl.Cached;
21-
import com.oracle.truffle.api.dsl.NodeChild;
2223
import com.oracle.truffle.api.dsl.Specialization;
2324

2425
/** Convert objects to a String by calling #to_str, but leave existing Strings or Symbols as they are. */
2526
@GenerateUncached
26-
@NodeChild(value = "childNode", type = RubyBaseNodeWithExecute.class)
27-
public abstract class ToStringOrSymbolNode extends RubyBaseNodeWithExecute {
27+
@GenerateInline
28+
@GenerateCached(false)
29+
public abstract class ToStringOrSymbolNode extends RubyBaseNode {
2830

29-
@NeverDefault
30-
public static ToStringOrSymbolNode create() {
31-
return ToStringOrSymbolNodeGen.create(null);
32-
}
33-
34-
public static ToStringOrSymbolNode create(RubyBaseNodeWithExecute child) {
35-
return ToStringOrSymbolNodeGen.create(child);
36-
}
37-
38-
public abstract Object execute(Object value);
39-
40-
public abstract RubyBaseNodeWithExecute getChildNode();
31+
public abstract Object execute(Node node, Object value);
4132

4233
@Specialization
43-
protected RubySymbol coerceRubySymbol(RubySymbol symbol) {
34+
protected static RubySymbol coerceRubySymbol(RubySymbol symbol) {
4435
return symbol;
4536
}
4637

4738
@Specialization
48-
protected RubyString coerceRubyString(RubyString string) {
39+
protected static RubyString coerceRubyString(RubyString string) {
4940
return string;
5041
}
5142

5243
@Specialization
53-
protected ImmutableRubyString coerceRubyString(ImmutableRubyString string) {
44+
protected static ImmutableRubyString coerceRubyString(ImmutableRubyString string) {
5445
return string;
5546
}
5647

5748
@Specialization(guards = { "!isRubySymbol(object)", "isNotRubyString(object)" })
58-
protected Object coerceObject(Object object,
49+
protected static Object coerceObject(Node node, Object object,
5950
@Cached DispatchNode toStrNode) {
6051
return toStrNode.call(
61-
coreLibrary().truffleTypeModule,
52+
coreLibrary(node).truffleTypeModule,
6253
"rb_convert_type",
6354
object,
64-
coreLibrary().stringClass,
65-
coreSymbols().TO_STR);
66-
}
67-
68-
@Override
69-
public RubyBaseNodeWithExecute cloneUninitialized() {
70-
return create(getChildNode().cloneUninitialized());
55+
coreLibrary(node).stringClass,
56+
coreSymbols(node).TO_STR);
7157
}
7258
}

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ public abstract static class MethodNode extends AlwaysInlinedMethodNode {
12291229
protected RubyMethod method(Frame callerFrame, Object self, Object[] rubyArgs, RootCallTarget target,
12301230
@Cached ToStringOrSymbolNode toStringOrSymbolNode,
12311231
@Cached GetMethodObjectNode getMethodObjectNode) {
1232-
Object name = toStringOrSymbolNode.execute(RubyArguments.getArgument(rubyArgs, 0));
1232+
Object name = toStringOrSymbolNode.execute(this, RubyArguments.getArgument(rubyArgs, 0));
12331233
return getMethodObjectNode.execute(callerFrame, self, name, PRIVATE);
12341234
}
12351235

@@ -1359,7 +1359,7 @@ public abstract static class PublicMethodNode extends AlwaysInlinedMethodNode {
13591359
protected RubyMethod method(Frame callerFrame, Object self, Object[] rubyArgs, RootCallTarget target,
13601360
@Cached ToStringOrSymbolNode toStringOrSymbolNode,
13611361
@Cached GetMethodObjectNode getMethodObjectNode) {
1362-
Object name = toStringOrSymbolNode.execute(RubyArguments.getArgument(rubyArgs, 0));
1362+
Object name = toStringOrSymbolNode.execute(this, RubyArguments.getArgument(rubyArgs, 0));
13631363
return getMethodObjectNode.execute(callerFrame, self, name, PUBLIC);
13641364
}
13651365

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.truffleruby.annotations.CoreModule;
4141
import org.truffleruby.annotations.Primitive;
4242
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
43-
import org.truffleruby.builtins.PrimitiveNode;
4443
import org.truffleruby.builtins.ReRaiseInlinedExceptionNode;
4544
import org.truffleruby.collections.ConcurrentOperations;
4645
import org.truffleruby.core.CoreLibrary;
@@ -54,7 +53,6 @@
5453
import org.truffleruby.core.cast.ToPathNode;
5554
import org.truffleruby.core.cast.ToStrNode;
5655
import org.truffleruby.core.cast.ToStringOrSymbolNode;
57-
import org.truffleruby.core.cast.ToStringOrSymbolNodeGen;
5856
import org.truffleruby.core.cast.ToSymbolNode;
5957
import org.truffleruby.core.constant.WarnAlreadyInitializedNode;
6058
import org.truffleruby.core.encoding.Encodings;
@@ -78,7 +76,6 @@
7876
import org.truffleruby.language.LexicalScope;
7977
import org.truffleruby.language.Nil;
8078
import org.truffleruby.language.RubyBaseNode;
81-
import org.truffleruby.language.RubyBaseNodeWithExecute;
8279
import org.truffleruby.language.RubyConstant;
8380
import org.truffleruby.language.RubyContextSourceNode;
8481
import org.truffleruby.language.RubyDynamicObject;
@@ -131,9 +128,7 @@
131128
import com.oracle.truffle.api.dsl.Cached;
132129
import com.oracle.truffle.api.dsl.Cached.Shared;
133130
import com.oracle.truffle.api.dsl.Cached.Exclusive;
134-
import com.oracle.truffle.api.dsl.CreateCast;
135131
import com.oracle.truffle.api.dsl.ImportStatic;
136-
import com.oracle.truffle.api.dsl.NodeChild;
137132
import com.oracle.truffle.api.dsl.Specialization;
138133
import com.oracle.truffle.api.frame.Frame;
139134
import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
@@ -144,6 +139,7 @@
144139
import com.oracle.truffle.api.profiles.BranchProfile;
145140
import com.oracle.truffle.api.source.SourceSection;
146141

142+
import static org.truffleruby.builtins.PrimitiveNode.FAILURE;
147143
import static org.truffleruby.core.module.ModuleNodes.GenerateAccessorNode.Accessor.BOTH;
148144
import static org.truffleruby.core.module.ModuleNodes.GenerateAccessorNode.Accessor.READER;
149145
import static org.truffleruby.core.module.ModuleNodes.GenerateAccessorNode.Accessor.WRITER;
@@ -941,51 +937,41 @@ protected boolean isConstDefined(
941937
}
942938

943939
@Primitive(name = "module_const_get")
944-
@NodeChild(value = "moduleNode", type = RubyNode.class)
945-
@NodeChild(value = "nameNode", type = RubyBaseNodeWithExecute.class)
946-
@NodeChild(value = "inheritNode", type = RubyNode.class)
947-
@NodeChild(value = "lookInObjectNode", type = RubyNode.class)
948-
@NodeChild(value = "checkNameNode", type = RubyNode.class)
949-
public abstract static class ConstGetNode extends PrimitiveNode {
950-
951-
@Child private LookupConstantNode lookupConstantLookInObjectNode = LookupConstantNode.create(true, true);
952-
@Child private LookupConstantNode lookupConstantNode = LookupConstantNode.create(true, false);
953-
@Child private GetConstantNode getConstantNode = GetConstantNode.create();
954-
@Child private ByteIndexOfStringNode byteIndexOfStringNode;
955-
956-
public static ConstGetNode create(RubyNode module, RubyBaseNodeWithExecute name, RubyNode inherit,
957-
RubyNode lookInObject, RubyNode checkName) {
958-
return ModuleNodesFactory.ConstGetNodeFactory.create(module, name, inherit, lookInObject, checkName);
940+
public abstract static class ConstGetNodePrimitiveNode extends PrimitiveArrayArgumentsNode {
941+
@Specialization
942+
protected Object getConst(
943+
RubyModule module, Object nameObject, boolean inherit, boolean lookInObject, boolean checkName,
944+
@Cached ConstGetNode constGetNode,
945+
@Cached ToStringOrSymbolNode toStringOrSymbolNode) {
946+
var name = toStringOrSymbolNode.execute(this, nameObject);
947+
return constGetNode.execute(this, module, name, inherit, lookInObject, checkName);
959948
}
949+
}
960950

961-
abstract RubyNode getModuleNode();
962-
963-
abstract RubyBaseNodeWithExecute getNameNode();
964-
965-
abstract RubyNode getInheritNode();
966-
967-
abstract RubyNode getLookInObjectNode();
968-
969-
abstract RubyNode getCheckNameNode();
951+
@GenerateCached(false)
952+
@GenerateInline
953+
public abstract static class ConstGetNode extends RubyBaseNode {
970954

971-
@CreateCast("nameNode")
972-
protected RubyBaseNodeWithExecute coerceToSymbolOrString(RubyBaseNodeWithExecute name) {
973-
// We want to know if the name is a Symbol, as then scoped lookup is not tried
974-
return ToStringOrSymbolNodeGen.create(name);
975-
}
955+
public abstract Object execute(Node node, RubyModule module, Object name, boolean inherit,
956+
boolean lookInObject, boolean checkName);
976957

977958
// Symbol
978959

979960
@Specialization(guards = "inherit")
980-
protected Object getConstant(
981-
RubyModule module, RubySymbol name, boolean inherit, boolean lookInObject, boolean checkName) {
982-
return getConstant(module, name.getString(), checkName, lookInObject);
961+
protected static Object getConstant(
962+
Node node, RubyModule module, RubySymbol name, boolean inherit, boolean lookInObject, boolean checkName,
963+
@Cached @Shared GetConstantNode getConstantNode,
964+
@Cached("create(true, false)") @Shared LookupConstantNode lookupConstantNode,
965+
@Cached("create(true, true)") @Shared LookupConstantNode lookupConstantLookInObjectNode) {
966+
return getConstant(module, name.getString(), checkName, lookInObject, getConstantNode, lookupConstantNode,
967+
lookupConstantLookInObjectNode);
983968
}
984969

985970
@Specialization(guards = "!inherit")
986-
protected Object getConstantNoInherit(
987-
RubyModule module, RubySymbol name, boolean inherit, boolean lookInObject, boolean checkName) {
988-
return getConstantNoInherit(module, name.getString(), checkName);
971+
protected static Object getConstantNoInherit(
972+
RubyModule module, RubySymbol name, boolean inherit, boolean lookInObject, boolean checkName,
973+
@Cached @Shared GetConstantNode getConstantNode) {
974+
return getConstantNoInherit(module, name.getString(), checkName, getConstantNode);
989975
}
990976

991977
// String
@@ -998,46 +984,67 @@ protected Object getConstantNoInherit(
998984
"!scoped",
999985
"checkName == cachedCheckName" },
1000986
limit = "getLimit()")
1001-
protected Object getConstantStringCached(
987+
protected static Object getConstantStringCached(
1002988
RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
1003989
@Cached @Shared RubyStringLibrary stringsName,
990+
@Cached @Shared GetConstantNode getConstantNode,
991+
@Cached("create(true, false)") @Shared LookupConstantNode lookupConstantNode,
992+
@Cached("create(true, true)") @Shared LookupConstantNode lookupConstantLookInObjectNode,
1004993
@Cached("asTruffleStringUncached(name)") TruffleString cachedTString,
1005994
@Cached("stringsName.getEncoding(name)") RubyEncoding cachedEncoding,
1006995
@Cached("getJavaString(name)") String cachedString,
1007996
@Cached("checkName") boolean cachedCheckName,
1008997
@Cached StringHelperNodes.EqualNode equalNode,
1009998
@Cached("isScoped(cachedString)") boolean scoped) {
1010-
return getConstant(module, cachedString, checkName, lookInObject);
999+
return getConstant(module, cachedString, checkName, lookInObject, getConstantNode, lookupConstantNode,
1000+
lookupConstantLookInObjectNode);
10111001
}
10121002

10131003
@Specialization(
1014-
guards = { "stringsName.isRubyString(name)", "inherit", "!isScoped(stringsName, name)" },
1004+
guards = {
1005+
"stringsName.isRubyString(name)",
1006+
"inherit",
1007+
"!isScoped(stringsName, name, byteIndexOfStringNode)" },
10151008
replaces = "getConstantStringCached")
1016-
protected Object getConstantString(
1017-
RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
1009+
protected static Object getConstantString(
1010+
Node node, RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
1011+
@Cached @Shared GetConstantNode getConstantNode,
1012+
@Cached @Shared ByteIndexOfStringNode byteIndexOfStringNode,
1013+
@Cached("create(true, false)") @Shared LookupConstantNode lookupConstantNode,
1014+
@Cached("create(true, true)") @Shared LookupConstantNode lookupConstantLookInObjectNode,
10181015
@Cached @Shared RubyStringLibrary stringsName,
10191016
@Cached @Shared ToJavaStringNode toJavaStringNode) {
1020-
return getConstant(module, toJavaStringNode.execute(this, name), checkName, lookInObject);
1017+
return getConstant(module, toJavaStringNode.execute(node, name), checkName, lookInObject, getConstantNode,
1018+
lookupConstantNode, lookupConstantLookInObjectNode);
10211019
}
10221020

10231021
@Specialization(
1024-
guards = { "stringsName.isRubyString(name)", "!inherit", "!isScoped(stringsName, name)" })
1025-
protected Object getConstantNoInheritString(
1026-
RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
1022+
guards = {
1023+
"stringsName.isRubyString(name)",
1024+
"!inherit",
1025+
"!isScoped(stringsName, name, byteIndexOfStringNode)" })
1026+
protected static Object getConstantNoInheritString(
1027+
Node node, RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
10271028
@Cached @Shared RubyStringLibrary stringsName,
1029+
@Cached @Shared ByteIndexOfStringNode byteIndexOfStringNode,
1030+
@Cached @Shared GetConstantNode getConstantNode,
10281031
@Cached @Shared ToJavaStringNode toJavaStringNode) {
1029-
return getConstantNoInherit(module, toJavaStringNode.execute(this, name), checkName);
1032+
return getConstantNoInherit(module, toJavaStringNode.execute(node, name), checkName, getConstantNode);
10301033
}
10311034

10321035
// Scoped String
1033-
@Specialization(guards = { "stringsName.isRubyString(name)", "isScoped(stringsName, name)" })
1034-
protected Object getConstantScoped(
1036+
@Specialization(
1037+
guards = { "stringsName.isRubyString(name)", "isScoped(stringsName, name, byteIndexOfStringNode)" })
1038+
protected static Object getConstantScoped(
10351039
RubyModule module, Object name, boolean inherit, boolean lookInObject, boolean checkName,
1040+
@Cached @Shared ByteIndexOfStringNode byteIndexOfStringNode,
10361041
@Cached @Shared RubyStringLibrary stringsName) {
10371042
return FAILURE;
10381043
}
10391044

1040-
private Object getConstant(RubyModule module, String name, boolean checkName, boolean lookInObject) {
1045+
private static Object getConstant(RubyModule module, String name, boolean checkName, boolean lookInObject,
1046+
GetConstantNode getConstantNode, LookupConstantNode lookupConstantNode,
1047+
LookupConstantNode lookupConstantLookInObjectNode) {
10411048
CompilerAsserts.partialEvaluationConstant(lookInObject);
10421049
if (lookInObject) {
10431050
return getConstantNode
@@ -1050,55 +1057,36 @@ private Object getConstant(RubyModule module, String name, boolean checkName, bo
10501057
}
10511058
}
10521059

1053-
private Object getConstantNoInherit(RubyModule module, String name, boolean checkName) {
1054-
final LookupConstantInterface lookup = this::lookupConstantNoInherit;
1060+
private static Object getConstantNoInherit(RubyModule module, String name, boolean checkName,
1061+
GetConstantNode getConstantNode) {
1062+
final LookupConstantInterface lookup = ConstGetNode::lookupConstantNoInherit;
10551063
return getConstantNode.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, checkName, lookup, true);
10561064
}
10571065

10581066
@TruffleBoundary
1059-
private RubyConstant lookupConstantNoInherit(LexicalScope lexicalScope, RubyModule module, String name,
1060-
boolean checkName) {
1067+
private static RubyConstant lookupConstantNoInherit(Node node, LexicalScope lexicalScope, RubyModule module,
1068+
String name, boolean checkName) {
10611069
return ModuleOperations
1062-
.lookupConstantWithInherit(getContext(), module, name, false, this, checkName)
1070+
.lookupConstantWithInherit(getContext(node), module, name, false, node, checkName)
10631071
.getConstant();
10641072
}
10651073

1066-
boolean isScoped(RubyStringLibrary libString, Object string) {
1067-
if (byteIndexOfStringNode == null) {
1068-
CompilerDirectives.transferToInterpreterAndInvalidate();
1069-
byteIndexOfStringNode = insert(ByteIndexOfStringNode.create());
1070-
}
1071-
1074+
static boolean isScoped(RubyStringLibrary libString, Object string,
1075+
ByteIndexOfStringNode byteIndexOfStringNode) {
10721076
var tstring = libString.getTString(string);
10731077
var encoding = libString.getTEncoding(string);
10741078
int byteLength = tstring.byteLength(encoding);
10751079
return byteIndexOfStringNode.execute(tstring, TStringConstants.COLON_COLON, 0, byteLength, encoding) >= 0;
10761080
}
10771081

10781082
@TruffleBoundary
1079-
boolean isScoped(String name) {
1083+
static boolean isScoped(String name) {
10801084
return name.contains("::");
10811085
}
10821086

10831087
protected int getLimit() {
10841088
return getLanguage().options.CONSTANT_CACHE;
10851089
}
1086-
1087-
RubyBaseNodeWithExecute getNameBeforeCasting() {
1088-
return ((ToStringOrSymbolNode) getNameNode()).getChildNode();
1089-
}
1090-
1091-
@Override
1092-
public RubyNode cloneUninitialized() {
1093-
var copy = create(
1094-
getModuleNode().cloneUninitialized(),
1095-
getNameBeforeCasting().cloneUninitialized(),
1096-
getInheritNode().cloneUninitialized(),
1097-
getLookInObjectNode().cloneUninitialized(),
1098-
getCheckNameNode().cloneUninitialized());
1099-
return copy.copyFlags(this);
1100-
}
1101-
11021090
}
11031091

11041092
@CoreMethod(names = "const_missing", required = 1)
@@ -1122,7 +1110,7 @@ protected Object constSourceLocation(RubyModule module, Object nameObject, Objec
11221110
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
11231111
@Cached ToStringOrSymbolNode toStringOrSymbolNode) {
11241112
final boolean inherit = booleanCastWithDefaultNode.execute(this, maybeInherit, true);
1125-
final var name = toStringOrSymbolNode.execute(nameObject);
1113+
final var name = toStringOrSymbolNode.execute(this, nameObject);
11261114
return constSourceLocationNode.execute(this, module, name, inherit);
11271115
}
11281116
}

0 commit comments

Comments
 (0)