41
41
package com .oracle .graal .python .builtins .modules .cext ;
42
42
43
43
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .IndexError ;
44
- import static com .oracle .graal .python .builtins .PythonBuiltinClassType .SystemError ;
45
44
import java .util .List ;
46
45
import com .oracle .graal .python .builtins .Builtin ;
47
46
import com .oracle .graal .python .builtins .CoreFunctions ;
48
47
import com .oracle .graal .python .builtins .Python3Core ;
48
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
49
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .SystemError ;
49
50
import com .oracle .graal .python .builtins .PythonBuiltins ;
51
+ import com .oracle .graal .python .builtins .modules .BuiltinConstructors .StrNode ;
52
+ import com .oracle .graal .python .builtins .objects .PNone ;
50
53
import com .oracle .graal .python .builtins .objects .cext .capi .CApiGuards ;
51
54
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .AsPythonObjectNode ;
52
55
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .AsPythonObjectStealingNode ;
56
+ import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .GetNativeNullNode ;
53
57
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PCallCapiFunction ;
58
+ import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PRaiseNativeNode ;
54
59
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .TransformExceptionToNativeNode ;
55
60
import com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol ;
56
61
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
57
62
import com .oracle .graal .python .builtins .objects .common .IndexNodes .NormalizeIndexNode ;
58
63
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
64
+ import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .LenNode ;
65
+ import com .oracle .graal .python .builtins .objects .ints .PInt ;
59
66
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
67
+ import com .oracle .graal .python .builtins .objects .tuple .TupleBuiltins .GetItemNode ;
60
68
import com .oracle .graal .python .lib .PyNumberAsSizeNode ;
61
69
import com .oracle .graal .python .nodes .ErrorMessages ;
70
+ import static com .oracle .graal .python .nodes .ErrorMessages .BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P ;
71
+ import static com .oracle .graal .python .nodes .ErrorMessages .NATIVE_S_SUBTYPES_NOT_IMPLEMENTED ;
62
72
import com .oracle .graal .python .nodes .PGuards ;
63
73
import com .oracle .graal .python .nodes .PRaiseNode ;
74
+ import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
64
75
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
65
76
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
66
77
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
67
78
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
79
+ import com .oracle .graal .python .nodes .object .GetClassNode ;
80
+ import com .oracle .graal .python .nodes .subscript .SliceLiteralNode ;
81
+ import com .oracle .graal .python .nodes .truffle .PythonTypes ;
68
82
import com .oracle .graal .python .runtime .exception .PException ;
69
83
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
70
84
import com .oracle .truffle .api .dsl .Cached ;
73
87
import com .oracle .truffle .api .dsl .ImportStatic ;
74
88
import com .oracle .truffle .api .dsl .NodeFactory ;
75
89
import com .oracle .truffle .api .dsl .Specialization ;
90
+ import com .oracle .truffle .api .dsl .TypeSystemReference ;
76
91
import com .oracle .truffle .api .frame .VirtualFrame ;
92
+ import com .oracle .truffle .api .profiles .BranchProfile ;
77
93
78
- @ CoreFunctions (defineModule = PythonCextTupleBuiltins . PYTHON_CEXT_TUPLE )
94
+ @ CoreFunctions (extendsModule = PythonCextBuiltins . PYTHON_CEXT )
79
95
@ GenerateNodeFactory
80
96
public class PythonCextTupleBuiltins extends PythonBuiltins {
81
97
82
- public static final String PYTHON_CEXT_TUPLE = "python_cext_tuple" ;
83
-
84
98
@ Override
85
99
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
86
- return PythonCextBuiltinsFactory .getFactories ();
100
+ return PythonCextTupleBuiltinsFactory .getFactories ();
87
101
}
88
102
89
103
@ Override
@@ -93,7 +107,7 @@ public void initialize(Python3Core core) {
93
107
94
108
@ Builtin (name = "PyTuple_New" , minNumOfPositionalArgs = 1 )
95
109
@ GenerateNodeFactory
96
- abstract static class PyTuple_New extends PythonUnaryBuiltinNode {
110
+ abstract static class PyTupleNew extends PythonUnaryBuiltinNode {
97
111
@ Specialization
98
112
PTuple doGeneric (VirtualFrame frame , Object size ,
99
113
@ Cached PyNumberAsSizeNode asSizeNode ) {
@@ -104,7 +118,7 @@ PTuple doGeneric(VirtualFrame frame, Object size,
104
118
@ Builtin (name = "PyTuple_SetItem" , minNumOfPositionalArgs = 3 )
105
119
@ GenerateNodeFactory
106
120
@ ImportStatic (CApiGuards .class )
107
- abstract static class PyTuple_SetItem extends PythonTernaryBuiltinNode {
121
+ abstract static class PyTupleSetItem extends PythonTernaryBuiltinNode {
108
122
@ Specialization
109
123
static int doManaged (VirtualFrame frame , PythonNativeWrapper selfWrapper , Object position , Object elementWrapper ,
110
124
@ Cached AsPythonObjectNode selfAsPythonObjectNode ,
@@ -143,7 +157,7 @@ protected static SequenceStorageNodes.SetItemNode createSetItem() {
143
157
144
158
@ Builtin (name = "PyTuple_GetItem" , minNumOfPositionalArgs = 2 )
145
159
@ GenerateNodeFactory
146
- abstract static class PyTuple_GetItem extends PythonBinaryBuiltinNode {
160
+ abstract static class PyTupleGetItem extends PythonBinaryBuiltinNode {
147
161
148
162
@ Specialization
149
163
Object doPTuple (VirtualFrame frame , PTuple tuple , long key ,
@@ -163,4 +177,105 @@ Object doPTuple(Object tuple, @SuppressWarnings("unused") Object key) {
163
177
throw raise (SystemError , ErrorMessages .BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P , tuple , tuple );
164
178
}
165
179
}
180
+
181
+ @ Builtin (name = "PyTuple_Size" , minNumOfPositionalArgs = 2 , maxNumOfPositionalArgs = 3 )
182
+ @ GenerateNodeFactory
183
+ public abstract static class PyTupleSizeNode extends PythonUnaryBuiltinNode {
184
+ @ Specialization
185
+ public int size (PTuple tuple ,
186
+ @ Cached LenNode lenNode ,
187
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
188
+ try {
189
+ return lenNode .execute (tuple .getSequenceStorage ());
190
+ } catch (PException e ) {
191
+ transformExceptionToNativeNode .execute (e );
192
+ return -1 ;
193
+ }
194
+ }
195
+
196
+ @ Specialization (guards = {"!isPTuple(obj)" , "isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)" })
197
+ public Object sizeNative (VirtualFrame frame , @ SuppressWarnings ("unused" ) Object obj ,
198
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
199
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
200
+ @ Cached PRaiseNativeNode raiseNativeNode ) {
201
+ return raiseNativeNode .raiseInt (frame , -1 , PythonBuiltinClassType .NotImplementedError , NATIVE_S_SUBTYPES_NOT_IMPLEMENTED , "tuple" );
202
+ }
203
+
204
+ @ SuppressWarnings ("unused" )
205
+ @ Specialization (guards = {"!isPTuple(obj)" , "!isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)" })
206
+ public Object size (VirtualFrame frame , Object obj ,
207
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
208
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
209
+ @ Cached StrNode strNode ,
210
+ @ Cached PRaiseNativeNode raiseNativeNode ) {
211
+ return raiseNativeNode .raiseInt (frame , -1 , SystemError , BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P , strNode .executeWith (frame , obj ), obj );
212
+ }
213
+
214
+ protected boolean isTupleSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
215
+ return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PTuple );
216
+ }
217
+ }
218
+
219
+ @ Builtin (name = "PyTuple_GetSlice" , minNumOfPositionalArgs = 3 )
220
+ @ TypeSystemReference (PythonTypes .class )
221
+ @ GenerateNodeFactory
222
+ abstract static class PyTupleGetSliceNode extends PythonTernaryBuiltinNode {
223
+
224
+ @ Specialization
225
+ Object getSlice (VirtualFrame frame , PTuple tuple , long iLow , long iHigh ,
226
+ @ Cached GetItemNode getItemNode ,
227
+ @ Cached SliceLiteralNode sliceNode ,
228
+ @ Cached BranchProfile isIntRangeProfile ,
229
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ,
230
+ @ Cached GetNativeNullNode getNativeNullNode ) {
231
+ try {
232
+ if (PInt .isIntRange (iLow ) && PInt .isIntRange (iHigh )) {
233
+ isIntRangeProfile .enter ();
234
+ return getItemNode .execute (frame , tuple , sliceNode .execute (frame , (int ) iLow , (int ) iHigh , PNone .NONE ));
235
+ }
236
+ return getItemNode .execute (frame , tuple , sliceNode .execute (frame , iLow , iHigh , PNone .NONE ));
237
+ } catch (PException e ) {
238
+ transformExceptionToNativeNode .execute (e );
239
+ return getNativeNullNode .execute ();
240
+ }
241
+ }
242
+
243
+ @ Specialization
244
+ Object getSlice (VirtualFrame frame , PTuple tuple , Object iLow , Object iHigh ,
245
+ @ Cached GetItemNode getItemNode ,
246
+ @ Cached SliceLiteralNode sliceNode ,
247
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ,
248
+ @ Cached GetNativeNullNode getNativeNullNode ) {
249
+ try {
250
+ return getItemNode .execute (frame , tuple , sliceNode .execute (frame , iLow , iHigh , PNone .NONE ));
251
+ } catch (PException e ) {
252
+ transformExceptionToNativeNode .execute (e );
253
+ return getNativeNullNode .execute ();
254
+ }
255
+ }
256
+
257
+ @ Specialization (guards = {"!isPTuple(obj)" , "isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)" })
258
+ public Object getSliceNative (VirtualFrame frame , @ SuppressWarnings ("unused" ) Object obj , @ SuppressWarnings ("unused" ) Object iLow , @ SuppressWarnings ("unused" ) Object iHigh ,
259
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
260
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
261
+ @ Cached PRaiseNativeNode raiseNativeNode ,
262
+ @ Cached GetNativeNullNode getNativeNullNode ) {
263
+ return raiseNativeNode .raise (frame , getNativeNullNode .execute (), PythonBuiltinClassType .NotImplementedError , NATIVE_S_SUBTYPES_NOT_IMPLEMENTED , "list" );
264
+ }
265
+
266
+ @ Specialization (guards = {"!isPTuple(obj)" , "!isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)" })
267
+ public Object getSlice (VirtualFrame frame , Object obj , @ SuppressWarnings ("unused" ) Object iLow , @ SuppressWarnings ("unused" ) Object iHigh ,
268
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
269
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
270
+ @ Cached StrNode strNode ,
271
+ @ Cached PRaiseNativeNode raiseNativeNode ,
272
+ @ Cached GetNativeNullNode getNativeNullNode ) {
273
+ return raiseNativeNode .raise (frame , getNativeNullNode .execute (), SystemError , BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P , strNode .executeWith (frame , obj ), obj );
274
+ }
275
+
276
+ protected boolean isTupleSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
277
+ return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PTuple );
278
+ }
279
+ }
280
+
166
281
}
0 commit comments