41
41
package com .oracle .graal .python .nodes .util ;
42
42
43
43
import static com .oracle .graal .python .nodes .SpecialMethodNames .__INDEX__ ;
44
+ import static com .oracle .graal .python .runtime .exception .PythonErrorType .IndexError ;
45
+ import static com .oracle .graal .python .runtime .exception .PythonErrorType .OverflowError ;
44
46
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
45
47
46
48
import com .oracle .graal .python .builtins .objects .ints .PInt ;
47
49
import com .oracle .graal .python .nodes .PBaseNode ;
48
50
import com .oracle .graal .python .nodes .call .special .LookupAndCallUnaryNode ;
49
- import com .oracle .graal .python .runtime .exception .PException ;
51
+ import com .oracle .graal .python .runtime .exception .PythonErrorType ;
50
52
import com .oracle .truffle .api .CompilerDirectives ;
51
53
import com .oracle .truffle .api .dsl .Fallback ;
52
54
import com .oracle .truffle .api .dsl .Specialization ;
58
60
*/
59
61
public abstract class CastToIndexNode extends PBaseNode {
60
62
63
+ private static final String ERROR_MESSAGE = "cannot fit 'int' into an index-sized integer" ;
64
+
61
65
@ Child private LookupAndCallUnaryNode callIndexNode ;
66
+
62
67
private final BranchProfile errorProfile = BranchProfile .create ();
68
+ private final PythonErrorType errorType ;
69
+
70
+ public CastToIndexNode (PythonErrorType errorType ) {
71
+ this .errorType = errorType ;
72
+ }
63
73
64
74
public abstract int execute (Object x );
65
75
@@ -89,7 +99,7 @@ int doLongOvf(long x) {
89
99
try {
90
100
return PInt .intValueExact (x );
91
101
} catch (ArithmeticException e ) {
92
- throw raiseIndexError ( );
102
+ throw raise ( errorType , ERROR_MESSAGE );
93
103
}
94
104
}
95
105
@@ -103,7 +113,7 @@ int doPIntOvf(PInt x) {
103
113
try {
104
114
return x .intValueExact ();
105
115
} catch (ArithmeticException e ) {
106
- throw raiseIndexError ( );
116
+ throw raise ( errorType , ERROR_MESSAGE );
107
117
}
108
118
}
109
119
@@ -122,6 +132,10 @@ int doGeneric(Object x) {
122
132
}
123
133
124
134
public static CastToIndexNode create () {
125
- return CastToIndexNodeGen .create ();
135
+ return CastToIndexNodeGen .create (IndexError );
136
+ }
137
+
138
+ public static CastToIndexNode createOverflow () {
139
+ return CastToIndexNodeGen .create (OverflowError );
126
140
}
127
141
}
0 commit comments