Skip to content

Commit 29f703b

Browse files
committed
Recursively apply 'CastToIndexNode'.
1 parent 29ab69b commit 29f703b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToIndexNode.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import com.oracle.truffle.api.CompilerDirectives;
5353
import com.oracle.truffle.api.dsl.Fallback;
5454
import com.oracle.truffle.api.dsl.Specialization;
55-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
56-
import com.oracle.truffle.api.profiles.BranchProfile;
5755

5856
/**
5957
* Converts an arbitrary object to an index-sized integer (which is a Java {@code int}).
@@ -63,12 +61,14 @@ public abstract class CastToIndexNode extends PBaseNode {
6361
private static final String ERROR_MESSAGE = "cannot fit 'int' into an index-sized integer";
6462

6563
@Child private LookupAndCallUnaryNode callIndexNode;
64+
@Child private CastToIndexNode recursiveNode;
6665

67-
private final BranchProfile errorProfile = BranchProfile.create();
6866
private final PythonErrorType errorType;
67+
private final boolean recursive;
6968

70-
public CastToIndexNode(PythonErrorType errorType) {
69+
public CastToIndexNode(PythonErrorType errorType, boolean recursive) {
7170
this.errorType = errorType;
71+
this.recursive = recursive;
7272
}
7373

7474
public abstract int execute(Object x);
@@ -119,23 +119,25 @@ int doPIntOvf(PInt x) {
119119

120120
@Fallback
121121
int doGeneric(Object x) {
122-
try {
122+
if (recursive) {
123123
if (callIndexNode == null) {
124124
CompilerDirectives.transferToInterpreterAndInvalidate();
125125
callIndexNode = insert(LookupAndCallUnaryNode.create(__INDEX__));
126126
}
127-
return callIndexNode.executeInt(x);
128-
} catch (UnexpectedResultException e) {
129-
errorProfile.enter();
130-
throw raise(TypeError, "__index__ returned non-int (type %p)", e.getResult());
127+
if (recursiveNode == null) {
128+
CompilerDirectives.transferToInterpreterAndInvalidate();
129+
recursiveNode = insert(CastToIndexNodeGen.create(errorType, false));
130+
}
131+
return recursiveNode.execute(callIndexNode.executeObject(x));
131132
}
133+
throw raise(TypeError, "__index__ returned non-int (type %p)", x);
132134
}
133135

134136
public static CastToIndexNode create() {
135-
return CastToIndexNodeGen.create(IndexError);
137+
return CastToIndexNodeGen.create(IndexError, true);
136138
}
137139

138140
public static CastToIndexNode createOverflow() {
139-
return CastToIndexNodeGen.create(OverflowError);
141+
return CastToIndexNodeGen.create(OverflowError, true);
140142
}
141143
}

0 commit comments

Comments
 (0)