Skip to content

Commit 252b26c

Browse files
committed
[GR-17457] Remove CreateCast
PullRequest: truffleruby/3925
2 parents 222dcf3 + 72a0608 commit 252b26c

25 files changed

+267
-294
lines changed

src/main/.checkstyle_checks.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@
265265
<property name="format" value='isNativeAccessAllowed\(\)'/>
266266
<property name="message" value='Use context.getOptions().NATIVE_PLATFORM instead.'/>
267267
</module>
268+
<module name="RegexpSinglelineJava">
269+
<property name="format" value="@CreateCast"/>
270+
<property name="message" value="Do not use @CreateCast. Use DSL inlining nodes instead."/>
271+
</module>
268272
<module name="IllegalType">
269273
<!-- Use PrintStream instead of PrintWriter, PrintWriter does not consistently flush, even when writing \n.-->
270274
<property name="illegalClassNames" value="PrintWriter"/>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.truffleruby.core.module.ConstantLookupResult;
2828
import org.truffleruby.core.module.ModuleOperations;
2929
import org.truffleruby.core.module.RubyModule;
30-
import org.truffleruby.core.numeric.FixnumLowerNodeGen;
30+
import org.truffleruby.core.numeric.FixnumLowerNodeGen.FixnumLowerASTNodeGen;
3131
import org.truffleruby.core.string.StringUtils;
3232
import org.truffleruby.core.support.TypeNodes;
3333
import org.truffleruby.language.LexicalScope;
@@ -435,7 +435,7 @@ public static boolean needsSelf(CoreMethod method) {
435435

436436
private static RubyNode transformArgument(CoreMethod method, RubyNode argument, int n) {
437437
if (ArrayUtils.contains(method.lowerFixnum(), n)) {
438-
argument = FixnumLowerNodeGen.create(argument);
438+
argument = FixnumLowerASTNodeGen.create(argument);
439439
}
440440

441441
if (n == 0 && method.raiseIfFrozenSelf()) {

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/builtins/PrimitiveNodeConstructor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.truffleruby.RubyLanguage;
1313
import org.truffleruby.annotations.Primitive;
1414
import org.truffleruby.core.array.ArrayUtils;
15-
import org.truffleruby.core.numeric.FixnumLowerNodeGen;
15+
import org.truffleruby.core.numeric.FixnumLowerNodeGen.FixnumLowerASTNodeGen;
1616
import org.truffleruby.core.support.TypeNodes;
1717
import org.truffleruby.language.RubyBaseNode;
1818
import org.truffleruby.language.RubyNode;
@@ -49,7 +49,7 @@ public RubyNode createInvokePrimitiveNode(Source source, SourceIndexLength sourc
4949

5050
for (int n = 0; n < arguments.length; n++) {
5151
if (ArrayUtils.contains(annotation.lowerFixnum(), n)) {
52-
arguments[n] = FixnumLowerNodeGen.create(arguments[n]);
52+
arguments[n] = FixnumLowerASTNodeGen.create(arguments[n]);
5353
}
5454
if (ArrayUtils.contains(annotation.raiseIfFrozen(), n)) {
5555
arguments[n] = TypeNodes.TypeCheckFrozenNode.create(arguments[n]);

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ protected Object at(RubyArray array, long index) {
246246
}
247247

248248
@Specialization(guards = "!isImplicitLong(index)")
249-
protected Object at(RubyArray array, Object index,
249+
protected static Object at(RubyArray array, Object index,
250250
@Cached ToLongNode toLongNode,
251251
@Cached FixnumLowerNode lowerNode,
252-
@Cached AtNode atNode) {
253-
return atNode.executeAt(array, lowerNode.executeLower(toLongNode.execute(index)));
252+
@Cached AtNode atNode,
253+
@Bind("this") Node node) {
254+
return atNode.executeAt(array, lowerNode.execute(node, toLongNode.execute(node, index)));
254255
}
255256
}
256257

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
*/
1010
package org.truffleruby.core.cast;
1111

12+
import com.oracle.truffle.api.dsl.Bind;
1213
import com.oracle.truffle.api.dsl.Cached;
14+
import com.oracle.truffle.api.dsl.Cached.Shared;
1315
import com.oracle.truffle.api.dsl.Fallback;
16+
import com.oracle.truffle.api.nodes.Node;
17+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
1418
import org.truffleruby.language.RubyBaseNode;
1519
import org.truffleruby.language.NotProvided;
1620
import org.truffleruby.language.control.RaiseException;
1721

1822
import com.oracle.truffle.api.dsl.Specialization;
19-
import com.oracle.truffle.api.profiles.ConditionProfile;
2023
import org.truffleruby.language.dispatch.DispatchNode;
2124

2225
import java.util.concurrent.TimeUnit;
2326

2427
public abstract class DurationToNanoSecondsNode extends RubyBaseNode {
2528

26-
private final ConditionProfile durationLessThanZeroProfile = ConditionProfile.create();
27-
2829
public abstract long execute(Object duration);
2930

3031
@Specialization
@@ -33,29 +34,33 @@ protected long noDuration(NotProvided duration) {
3334
}
3435

3536
@Specialization
36-
protected long duration(long duration) {
37-
return validate(TimeUnit.SECONDS.toNanos(duration));
37+
protected long duration(long duration,
38+
@Cached @Shared InlinedConditionProfile durationLessThanZeroProfile) {
39+
return validate(this, TimeUnit.SECONDS.toNanos(duration), durationLessThanZeroProfile);
3840
}
3941

4042
@Specialization
41-
protected long duration(double duration) {
42-
return validate((long) (duration * 1e9));
43+
protected long duration(double duration,
44+
@Cached @Shared InlinedConditionProfile durationLessThanZeroProfile) {
45+
return validate(this, (long) (duration * 1e9), durationLessThanZeroProfile);
4346
}
4447

4548
@Fallback
46-
protected long duration(Object duration,
49+
protected static long duration(Object duration,
4750
@Cached DispatchNode durationToNanoSeconds,
48-
@Cached ToLongNode toLongNode) {
51+
@Cached @Shared InlinedConditionProfile durationLessThanZeroProfile,
52+
@Cached ToLongNode toLongNode,
53+
@Bind("this") Node node) {
4954
final Object nanoseconds = durationToNanoSeconds.call(
50-
coreLibrary().truffleKernelOperationsModule,
55+
coreLibrary(node).truffleKernelOperationsModule,
5156
"convert_duration_to_nanoseconds",
5257
duration);
53-
return validate(toLongNode.execute(nanoseconds));
58+
return validate(node, toLongNode.execute(node, nanoseconds), durationLessThanZeroProfile);
5459
}
5560

56-
private long validate(long durationInNanos) {
57-
if (durationLessThanZeroProfile.profile(durationInNanos < 0)) {
58-
throw new RaiseException(getContext(), coreExceptions().argumentErrorTimeIntervalPositive(this));
61+
private static long validate(Node node, long durationInNanos, InlinedConditionProfile durationLessThanZeroProfile) {
62+
if (durationLessThanZeroProfile.profile(node, durationInNanos < 0)) {
63+
throw new RaiseException(getContext(node), coreExceptions(node).argumentErrorTimeIntervalPositive(node));
5964
}
6065
return durationInNanos;
6166
}

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

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,89 +11,77 @@
1111

1212
import com.oracle.truffle.api.dsl.Cached;
1313
import com.oracle.truffle.api.dsl.Fallback;
14+
import com.oracle.truffle.api.dsl.GenerateInline;
1415
import com.oracle.truffle.api.dsl.GenerateUncached;
15-
import com.oracle.truffle.api.dsl.NeverDefault;
16-
import com.oracle.truffle.api.dsl.NodeChild;
1716
import com.oracle.truffle.api.dsl.Specialization;
17+
import com.oracle.truffle.api.nodes.Node;
1818
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
1919
import org.truffleruby.core.numeric.RubyBignum;
2020
import org.truffleruby.language.Nil;
21-
import org.truffleruby.language.RubyBaseNodeWithExecute;
21+
import org.truffleruby.language.RubyBaseNode;
2222
import org.truffleruby.language.control.RaiseException;
2323
import org.truffleruby.language.dispatch.DispatchNode;
2424
import org.truffleruby.utils.Utils;
2525

2626
/** See {@link ToIntNode} for a comparison of different integer conversion nodes. */
2727
@GenerateUncached
28-
@NodeChild(value = "childNode", type = RubyBaseNodeWithExecute.class)
29-
public abstract class ToLongNode extends RubyBaseNodeWithExecute {
28+
@GenerateInline(inlineByDefault = true)
29+
public abstract class ToLongNode extends RubyBaseNode {
3030

31-
@NeverDefault
32-
public static ToLongNode create() {
33-
return ToLongNodeGen.create(null);
34-
}
31+
public abstract long execute(Node node, Object object);
3532

36-
public static ToLongNode create(RubyBaseNodeWithExecute child) {
37-
return ToLongNodeGen.create(child);
33+
public final long executeCached(Object object) {
34+
return execute(this, object);
3835
}
3936

40-
public abstract long execute(Object object);
41-
42-
abstract RubyBaseNodeWithExecute getChildNode();
43-
4437
@Specialization
45-
protected long coerceInt(int value) {
38+
protected static long coerceInt(int value) {
4639
return value;
4740
}
4841

4942
@Specialization
50-
protected long coerceLong(long value) {
43+
protected static long coerceLong(long value) {
5144
return value;
5245
}
5346

5447
@Specialization
55-
protected long coerceRubyBignum(RubyBignum value) {
48+
protected static long coerceRubyBignum(Node node, RubyBignum value) {
5649
throw new RaiseException(
57-
getContext(),
58-
coreExceptions().rangeError("bignum too big to convert into `long'", this));
50+
getContext(node),
51+
coreExceptions(node).rangeError("bignum too big to convert into `long'", node));
5952
}
6053

6154
@Specialization
62-
protected long coerceDouble(double value,
55+
protected static long coerceDouble(Node node, double value,
6356
@Cached InlinedBranchProfile errorProfile) {
6457
// emulate MRI logic
6558
// We check for `value < MAX_VALUE` because casting Long.MAX_VALUE to double yields a double value of 2^63 which is >
6659
// Long.MAX_VALUE.
6760
if (Long.MIN_VALUE <= value && value < Long.MAX_VALUE) {
6861
return (long) value;
6962
} else {
70-
errorProfile.enter(this);
63+
errorProfile.enter(node);
7164
throw new RaiseException(
72-
getContext(),
73-
coreExceptions().rangeError(Utils.concat("float ", value, " out of range of integer"), this));
65+
getContext(node),
66+
coreExceptions(node).rangeError(Utils.concat("float ", value, " out of range of integer"), node));
7467
}
7568
}
7669

7770
@Specialization
78-
protected long coerceNil(Nil nil) {
71+
protected static long coerceNil(Node node, Nil nil) {
7972
// MRI hardcodes this specific error message, which is slightly different from the one we would get in the
8073
// catch-all case.
8174
throw new RaiseException(
82-
getContext(),
83-
coreExceptions().typeError("no implicit conversion from nil to integer", this));
75+
getContext(node),
76+
coreExceptions(node).typeError("no implicit conversion from nil to integer", node));
8477
}
8578

8679
@Fallback
87-
protected long coerceObject(Object object,
88-
@Cached DispatchNode toIntNode,
89-
@Cached ToLongNode fitNode) {
80+
protected static long coerceObject(Node node, Object object,
81+
@Cached(inline = false) DispatchNode toIntNode,
82+
@Cached(inline = false) ToLongNode fitNode) {
9083
final Object coerced = toIntNode
91-
.call(coreLibrary().truffleTypeModule, "rb_to_int_fallback", object);
92-
return fitNode.execute(coerced);
93-
}
94-
95-
@Override
96-
public RubyBaseNodeWithExecute cloneUninitialized() {
97-
return create(getChildNode().cloneUninitialized());
84+
.call(coreLibrary(node).truffleTypeModule, "rb_to_int_fallback", object);
85+
return fitNode.executeCached(coerced);
9886
}
9987
}

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

Lines changed: 16 additions & 30 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,
59-
@Cached DispatchNode toStrNode) {
49+
protected static Object coerceObject(Node node, Object object,
50+
@Cached(inline = false) 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/exception/CoreExceptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public RubyException argumentErrorTooFewArguments(Node currentNode) {
139139
null);
140140
}
141141

142-
public RubyException argumentErrorTimeIntervalPositive(RubyBaseNode currentNode) {
143-
return argumentError(coreStrings().TIME_INTERVAL_MUST_BE_POS.createInstance(currentNode.getContext()),
142+
public RubyException argumentErrorTimeIntervalPositive(Node currentNode) {
143+
return argumentError(coreStrings().TIME_INTERVAL_MUST_BE_POS.createInstance(RubyContext.get(currentNode)),
144144
currentNode, null);
145145
}
146146

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

0 commit comments

Comments
 (0)