51
51
import java .util .List ;
52
52
53
53
import com .oracle .graal .python .PythonLanguage ;
54
+ import com .oracle .graal .python .annotations .ArgumentClinic ;
55
+ import com .oracle .graal .python .annotations .ArgumentClinic .ClinicConversion ;
54
56
import com .oracle .graal .python .builtins .Builtin ;
55
57
import com .oracle .graal .python .builtins .CoreFunctions ;
56
58
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
70
72
import com .oracle .graal .python .builtins .objects .cext .PythonNativeVoidPtr ;
71
73
import com .oracle .graal .python .builtins .objects .common .FormatNodeBase ;
72
74
import com .oracle .graal .python .builtins .objects .function .PArguments ;
75
+ import com .oracle .graal .python .builtins .objects .ints .IntBuiltinsClinicProviders .FormatNodeClinicProviderGen ;
73
76
import com .oracle .graal .python .builtins .objects .list .PList ;
74
77
import com .oracle .graal .python .builtins .objects .memoryview .PMemoryView ;
75
78
import com .oracle .graal .python .builtins .objects .object .PythonObject ;
88
91
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
89
92
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
90
93
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
94
+ import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
91
95
import com .oracle .graal .python .nodes .object .GetClassNode ;
92
96
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
93
- import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
94
97
import com .oracle .graal .python .runtime .PythonContext ;
95
98
import com .oracle .graal .python .runtime .PythonCore ;
96
99
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
@@ -2510,34 +2513,26 @@ static String doNativeVoidPtr(PythonNativeVoidPtr self,
2510
2513
abstract static class ReprNode extends StrNode {
2511
2514
}
2512
2515
2513
- @ Builtin (name = __FORMAT__ , minNumOfPositionalArgs = 2 )
2516
+ @ Builtin (name = __FORMAT__ , minNumOfPositionalArgs = 2 , parameterNames = {"$self" , "format_spec" })
2517
+ @ ArgumentClinic (name = "format_spec" , conversion = ClinicConversion .String )
2514
2518
@ GenerateNodeFactory
2515
2519
abstract static class FormatNode extends FormatNodeBase {
2516
2520
@ Child private BuiltinConstructors .FloatNode floatNode ;
2517
2521
2518
- // We cannot use PythonArithmeticTypes, because for empty format string we need to call the
2519
- // boolean's __str__ and not int's __str__
2520
- @ Specialization
2521
- Object formatB (VirtualFrame frame , boolean self , Object formatStringObj ,
2522
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2523
- String formatString = castFormatString (formatStringObj , castToStringNode );
2524
- if (formatString .isEmpty ()) {
2525
- return ensureStrCallNode ().executeObject (frame , self );
2526
- }
2527
- return doFormatInt (self ? 1 : 0 , formatString );
2522
+ @ Override
2523
+ protected ArgumentClinicProvider getArgumentClinic () {
2524
+ return FormatNodeClinicProviderGen .INSTANCE ;
2528
2525
}
2529
2526
2530
- @ Specialization
2531
- Object formatI (VirtualFrame frame , int self , Object formatStringObj ,
2532
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2533
- String formatString = castFormatString (formatStringObj , castToStringNode );
2534
- if (formatString .isEmpty ()) {
2535
- return ensureStrCallNode ().executeObject (frame , self );
2536
- }
2537
- return doFormatInt (self , formatString );
2527
+ // We cannot use PythonArithmeticTypes, because for empty format string we need to call the
2528
+ // boolean's __str__ and not int's __str__ (that specialization is inherited)
2529
+ @ Specialization (guards = "!formatString.isEmpty()" )
2530
+ Object formatB (boolean self , String formatString ) {
2531
+ return formatI (self ? 1 : 0 , formatString );
2538
2532
}
2539
2533
2540
- private String doFormatInt (int self , String formatString ) {
2534
+ @ Specialization (guards = "!formatString.isEmpty()" )
2535
+ Object formatI (int self , String formatString ) {
2541
2536
PythonCore core = getCore ();
2542
2537
Spec spec = getSpec (formatString , core );
2543
2538
if (isDoubleSpec (spec )) {
@@ -2547,23 +2542,19 @@ private String doFormatInt(int self, String formatString) {
2547
2542
return formatInt (self , core , spec );
2548
2543
}
2549
2544
2550
- @ Specialization
2551
- Object formatL (VirtualFrame frame , long self , Object formatString ,
2552
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2553
- return formatPI (frame , factory ().createInt (self ), formatString , castToStringNode );
2545
+ @ Specialization (guards = "!formatString.isEmpty()" )
2546
+ Object formatL (VirtualFrame frame , long self , String formatString ) {
2547
+ return formatPI (frame , factory ().createInt (self ), formatString );
2554
2548
}
2555
2549
2556
- @ Specialization
2557
- Object formatPI (VirtualFrame frame , PInt self , Object formatStringObj ,
2558
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2559
- String formatString = castFormatString (formatStringObj , castToStringNode );
2560
- if (formatString .isEmpty ()) {
2561
- return ensureStrCallNode ().executeObject (frame , self );
2562
- }
2550
+ @ Specialization (guards = "!formatString.isEmpty()" )
2551
+ Object formatPI (VirtualFrame frame , PInt self , String formatString ) {
2563
2552
PythonCore core = getCore ();
2564
2553
Spec spec = getSpec (formatString , core );
2565
2554
if (isDoubleSpec (spec )) {
2566
- return formatDouble (core , spec , asDouble (frame , self ));
2555
+ // lazy init of floatNode serves as branch profile
2556
+ double doubleVal = asDouble (frame , self );
2557
+ return formatDouble (core , spec , doubleVal );
2567
2558
}
2568
2559
validateIntegerSpec (core , spec );
2569
2560
return formatPInt (self , core , spec );
0 commit comments