90
90
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
91
91
import com .oracle .graal .python .builtins .PythonBuiltins ;
92
92
import com .oracle .graal .python .builtins .modules .BuiltinConstructors .BytesNode ;
93
+ import com .oracle .graal .python .builtins .modules .BuiltinConstructors .ComplexNode ;
93
94
import com .oracle .graal .python .builtins .modules .BuiltinConstructors .FrozenSetNode ;
94
95
import com .oracle .graal .python .builtins .modules .BuiltinConstructors .MappingproxyNode ;
95
96
import com .oracle .graal .python .builtins .modules .BuiltinConstructors .StrNode ;
204
205
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetItemNode ;
205
206
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetItemScalarNode ;
206
207
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .LenNode ;
208
+ import com .oracle .graal .python .builtins .objects .complex .PComplex ;
207
209
import com .oracle .graal .python .builtins .objects .dict .DictBuiltins ;
208
210
import com .oracle .graal .python .builtins .objects .dict .DictBuiltins .DelItemNode ;
209
211
import com .oracle .graal .python .builtins .objects .dict .DictBuiltins .ItemsNode ;
282
284
import com .oracle .graal .python .nodes .PRaiseNode ;
283
285
import com .oracle .graal .python .nodes .SpecialAttributeNames ;
284
286
import com .oracle .graal .python .nodes .SpecialMethodNames ;
287
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__FLOAT__ ;
285
288
import com .oracle .graal .python .nodes .WriteUnraisableNode ;
286
289
import com .oracle .graal .python .nodes .argument .CreateArgumentsNode .CreateAndCheckArgumentsNode ;
287
290
import com .oracle .graal .python .nodes .argument .keywords .ExpandKeywordStarargsNode ;
@@ -2219,7 +2222,7 @@ protected boolean isListSubtype(VirtualFrame frame, Object obj, GetClassNode get
2219
2222
return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PList );
2220
2223
}
2221
2224
}
2222
-
2225
+
2223
2226
///////////// long /////////////
2224
2227
2225
2228
@ Builtin (name = "_PyLong_Sign" , minNumOfPositionalArgs = 1 )
@@ -2333,7 +2336,7 @@ Object fromDouble(VirtualFrame frame, double d,
2333
2336
2334
2337
@ Builtin (name = "PyLong_FromString" , minNumOfPositionalArgs = 3 )
2335
2338
@ TypeSystemReference (PythonTypes .class )
2336
- @ GenerateNodeFactory
2339
+ @ GenerateNodeFactory
2337
2340
abstract static class PyLongFromStringNode extends PythonTernaryBuiltinNode {
2338
2341
2339
2342
@ Specialization (guards = "negative == 0" )
@@ -2363,7 +2366,7 @@ Object fromString(VirtualFrame frame, String s, long base, @SuppressWarnings("un
2363
2366
}
2364
2367
}
2365
2368
}
2366
-
2369
+
2367
2370
///////////// float /////////////
2368
2371
2369
2372
@ Builtin (name = "PyFloat_FromDouble" , minNumOfPositionalArgs = 1 )
@@ -2385,7 +2388,123 @@ public Object fromDouble(VirtualFrame frame, Object obj,
2385
2388
return raiseNativeNode .raiseInt (frame , -1 , SystemError , BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P , strNode .executeWith (frame , obj ), obj );
2386
2389
}
2387
2390
}
2388
-
2391
+
2392
+ ///////////// complex /////////////
2393
+
2394
+ @ Builtin (name = "PyComplex_AsCComplex" , minNumOfPositionalArgs = 1 )
2395
+ @ GenerateNodeFactory
2396
+ abstract static class PyComplexAsCComplexNode extends PythonUnaryBuiltinNode {
2397
+ @ Specialization
2398
+ PTuple asComplex (PComplex c ) {
2399
+ return factory ().createTuple (new Object []{c .getReal (), c .getImag ()});
2400
+ }
2401
+
2402
+ @ Specialization (guards = "!isPComplex(obj)" )
2403
+ Object asComplex (VirtualFrame frame , Object obj ,
2404
+ @ Cached ComplexNode complexNode ,
2405
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ,
2406
+ @ Cached GetNativeNullNode getNativeNullNode ) {
2407
+ try {
2408
+ PComplex c = (PComplex ) complexNode .execute (frame , PythonBuiltinClassType .PComplex , obj , PNone .NO_VALUE );
2409
+ return factory ().createTuple (new Object []{c .getReal (), c .getImag ()});
2410
+ } catch (PException e ) {
2411
+ transformExceptionToNativeNode .execute (e );
2412
+ return getNativeNullNode .execute ();
2413
+ }
2414
+ }
2415
+ }
2416
+
2417
+ @ Builtin (name = "PyComplex_RealAsDouble" , minNumOfPositionalArgs = 1 )
2418
+ @ GenerateNodeFactory
2419
+ abstract static class PyComplexRealAsDoubleNode extends PythonUnaryBuiltinNode {
2420
+
2421
+ @ Specialization
2422
+ double asDouble (PComplex d ) {
2423
+ return d .getReal ();
2424
+ }
2425
+
2426
+ @ Specialization (guards = {"!isPComplex(obj)" , "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)" })
2427
+ public Object asDouble (VirtualFrame frame , Object obj ,
2428
+ @ Cached PyObjectGetAttr getAttr ,
2429
+ @ Cached CallNode callNode ,
2430
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
2431
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
2432
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
2433
+ try {
2434
+ return callNode .execute (getAttr .execute (frame , obj , "real" ));
2435
+ } catch (PException e ) {
2436
+ transformExceptionToNativeNode .execute (e );
2437
+ return -1.0 ;
2438
+ }
2439
+ }
2440
+
2441
+ @ Specialization (guards = {"!isPComplex(obj)" , "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)" })
2442
+ public Object asDoubleFloat (VirtualFrame frame , Object obj ,
2443
+ @ Cached PyObjectGetAttr getAttr ,
2444
+ @ Cached CallNode callNode ,
2445
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
2446
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
2447
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
2448
+ try {
2449
+ return callNode .execute (getAttr .execute (frame , obj , __FLOAT__ ));
2450
+ } catch (PException e ) {
2451
+ transformExceptionToNativeNode .execute (e );
2452
+ return -1.0 ;
2453
+ }
2454
+ }
2455
+
2456
+ protected boolean isComplexSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
2457
+ return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PComplex );
2458
+ }
2459
+ }
2460
+
2461
+ @ Builtin (name = "PyComplex_ImagAsDouble" , minNumOfPositionalArgs = 1 )
2462
+ @ GenerateNodeFactory
2463
+ abstract static class PyComplexImagAsDoubleNode extends PythonUnaryBuiltinNode {
2464
+
2465
+ @ Specialization
2466
+ double asDouble (PComplex d ) {
2467
+ return d .getImag ();
2468
+ }
2469
+
2470
+ @ Specialization (guards = {"!isPComplex(obj)" , "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)" })
2471
+ public Object asDouble (VirtualFrame frame , Object obj ,
2472
+ @ Cached PyObjectGetAttr getAttr ,
2473
+ @ Cached CallNode callNode ,
2474
+ @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
2475
+ @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
2476
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
2477
+ try {
2478
+ return callNode .execute (getAttr .execute (frame , obj , "imag" ));
2479
+ } catch (PException e ) {
2480
+ transformExceptionToNativeNode .execute (e );
2481
+ return -1 ;
2482
+ }
2483
+ }
2484
+
2485
+ @ SuppressWarnings ("unused" )
2486
+ @ Specialization (guards = {"!isPComplex(obj)" , "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)" })
2487
+ public Object asDouble (VirtualFrame frame , Object obj ,
2488
+ @ Cached GetClassNode getClassNode ,
2489
+ @ Cached IsSubtypeNode isSubtypeNode ) {
2490
+ return 0.0 ;
2491
+ }
2492
+
2493
+ protected boolean isComplexSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
2494
+ return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PComplex );
2495
+ }
2496
+ }
2497
+
2498
+ @ Builtin (name = "PyComplex_FromDoubles" , minNumOfPositionalArgs = 1 )
2499
+ @ GenerateNodeFactory
2500
+ abstract static class PyComplexFromDoublesNode extends PythonBinaryBuiltinNode {
2501
+
2502
+ @ Specialization
2503
+ public PComplex asDouble (double r , double i ) {
2504
+ return factory ().createComplex (r , i );
2505
+ }
2506
+ }
2507
+
2389
2508
/**
2390
2509
* This is used in the ExternalFunctionNode below, so all arguments passed from Python code into
2391
2510
* a C function are automatically unwrapped if they are wrapped. This function is also called
0 commit comments