|
47 | 47 |
|
48 | 48 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
49 | 49 | import com.oracle.graal.python.builtins.objects.range.RangeNodesFactory.LenOfRangeNodeFactory;
|
50 |
| -import com.oracle.graal.python.builtins.objects.slice.PObjectSlice.SliceObjectInfo; |
51 | 50 | import com.oracle.graal.python.builtins.objects.slice.PSlice.SliceInfo;
|
52 | 51 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
53 | 52 | import com.oracle.graal.python.nodes.PRaiseNode;
|
@@ -94,52 +93,55 @@ PBigRange createBigRange(Object start, Object stop, Object step, PythonObjectFac
|
94 | 93 |
|
95 | 94 | }
|
96 | 95 |
|
| 96 | + /** |
| 97 | + * Attempts to produce result of the same type as the arguments, otherwise throws |
| 98 | + * {@link ArithmeticException}. It is responsibility of the caller to widen the arguments' types |
| 99 | + * if necessary. |
| 100 | + */ |
97 | 101 | @GenerateNodeFactory
|
98 | 102 | @GenerateUncached
|
99 | 103 | @ImportStatic(SpecialMethodNames.class)
|
100 | 104 | public abstract static class LenOfRangeNode extends Node {
|
101 | 105 | public abstract Object execute(Object start, Object stop, Object step);
|
102 | 106 |
|
103 |
| - public int len(Object start, Object stop, Object step) throws ArithmeticException { |
104 |
| - return (int) execute(start, stop, step); |
105 |
| - } |
| 107 | + public abstract int executeInt(int start, int stop, int step); |
106 | 108 |
|
107 |
| - public int len(SliceInfo slice) throws ArithmeticException { |
108 |
| - return (int) execute(slice.start, slice.stop, slice.step); |
| 109 | + public int len(int start, int stop, int step) throws ArithmeticException { |
| 110 | + return executeInt(start, stop, step); |
109 | 111 | }
|
110 | 112 |
|
111 |
| - public Object len(SliceObjectInfo slice) throws ArithmeticException { |
112 |
| - return execute(slice.start, slice.stop, slice.step); |
| 113 | + public int len(SliceInfo slice) throws ArithmeticException { |
| 114 | + return executeInt(slice.start, slice.stop, slice.step); |
113 | 115 | }
|
114 | 116 |
|
115 | 117 | @Specialization(guards = {"step > 0", "lo > 0", "lo < hi"})
|
116 |
| - Object simple(int lo, int hi, int step) { |
| 118 | + int simple(int lo, int hi, int step) { |
117 | 119 | return 1 + ((hi - 1 - lo) / step);
|
118 | 120 | }
|
119 | 121 |
|
120 | 122 | @Specialization(guards = {"step > 0", "lo >= hi"})
|
121 |
| - Object zero1(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) { |
| 123 | + int zero1(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) { |
122 | 124 | return 0;
|
123 | 125 | }
|
124 | 126 |
|
125 | 127 | @Specialization(guards = {"step > 0", "lo < hi"})
|
126 |
| - Object mightBeBig1(int lo, int hi, int step) throws ArithmeticException { |
| 128 | + int mightBeBig1(int lo, int hi, int step) throws ArithmeticException { |
127 | 129 | long diff = Math.subtractExact(Math.subtractExact(hi, (long) lo), 1);
|
128 | 130 | return Math.toIntExact(Math.addExact(diff / step, 1));
|
129 | 131 | }
|
130 | 132 |
|
131 | 133 | @Specialization(guards = {"step < 0", "lo < 0", "lo > hi"})
|
132 |
| - Object simpleNegative(int lo, int hi, int step) { |
| 134 | + int simpleNegative(int lo, int hi, int step) { |
133 | 135 | return 1 + ((lo - 1 - hi) / -step);
|
134 | 136 | }
|
135 | 137 |
|
136 | 138 | @Specialization(guards = {"step < 0", "lo <= hi"})
|
137 |
| - Object zero2(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) { |
| 139 | + int zero2(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) { |
138 | 140 | return 0;
|
139 | 141 | }
|
140 | 142 |
|
141 | 143 | @Specialization(guards = {"step < 0", "lo > hi"})
|
142 |
| - Object mightBeBig2(int lo, int hi, int step) throws ArithmeticException { |
| 144 | + int mightBeBig2(int lo, int hi, int step) throws ArithmeticException { |
143 | 145 | long diff = Math.subtractExact(Math.subtractExact(lo, (long) hi), 1);
|
144 | 146 | return Math.toIntExact(Math.addExact(diff / -(long) step, 1));
|
145 | 147 | }
|
|
0 commit comments