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 ;
@@ -2542,34 +2545,26 @@ static String doNativeVoidPtr(PythonNativeVoidPtr self,
2542
2545
abstract static class ReprNode extends StrNode {
2543
2546
}
2544
2547
2545
- @ Builtin (name = __FORMAT__ , minNumOfPositionalArgs = 2 )
2548
+ @ Builtin (name = __FORMAT__ , minNumOfPositionalArgs = 2 , parameterNames = {"$self" , "format_spec" })
2549
+ @ ArgumentClinic (name = "format_spec" , conversion = ClinicConversion .String )
2546
2550
@ GenerateNodeFactory
2547
2551
abstract static class FormatNode extends FormatNodeBase {
2548
2552
@ Child private BuiltinConstructors .FloatNode floatNode ;
2549
2553
2550
- // We cannot use PythonArithmeticTypes, because for empty format string we need to call the
2551
- // boolean's __str__ and not int's __str__
2552
- @ Specialization
2553
- Object formatB (VirtualFrame frame , boolean self , Object formatStringObj ,
2554
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2555
- String formatString = castFormatString (formatStringObj , castToStringNode );
2556
- if (formatString .isEmpty ()) {
2557
- return ensureStrCallNode ().executeObject (frame , self );
2558
- }
2559
- return doFormatInt (self ? 1 : 0 , formatString );
2554
+ @ Override
2555
+ protected ArgumentClinicProvider getArgumentClinic () {
2556
+ return FormatNodeClinicProviderGen .INSTANCE ;
2560
2557
}
2561
2558
2562
- @ Specialization
2563
- Object formatI (VirtualFrame frame , int self , Object formatStringObj ,
2564
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2565
- String formatString = castFormatString (formatStringObj , castToStringNode );
2566
- if (formatString .isEmpty ()) {
2567
- return ensureStrCallNode ().executeObject (frame , self );
2568
- }
2569
- return doFormatInt (self , formatString );
2559
+ // We cannot use PythonArithmeticTypes, because for empty format string we need to call the
2560
+ // boolean's __str__ and not int's __str__ (that specialization is inherited)
2561
+ @ Specialization (guards = "!formatString.isEmpty()" )
2562
+ Object formatB (boolean self , String formatString ) {
2563
+ return formatI (self ? 1 : 0 , formatString );
2570
2564
}
2571
2565
2572
- private String doFormatInt (int self , String formatString ) {
2566
+ @ Specialization (guards = "!formatString.isEmpty()" )
2567
+ Object formatI (int self , String formatString ) {
2573
2568
PythonCore core = getCore ();
2574
2569
Spec spec = getSpec (formatString , core );
2575
2570
if (isDoubleSpec (spec )) {
@@ -2579,23 +2574,19 @@ private String doFormatInt(int self, String formatString) {
2579
2574
return formatInt (self , core , spec );
2580
2575
}
2581
2576
2582
- @ Specialization
2583
- Object formatL (VirtualFrame frame , long self , Object formatString ,
2584
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2585
- return formatPI (frame , factory ().createInt (self ), formatString , castToStringNode );
2577
+ @ Specialization (guards = "!formatString.isEmpty()" )
2578
+ Object formatL (VirtualFrame frame , long self , String formatString ) {
2579
+ return formatPI (frame , factory ().createInt (self ), formatString );
2586
2580
}
2587
2581
2588
- @ Specialization
2589
- Object formatPI (VirtualFrame frame , PInt self , Object formatStringObj ,
2590
- @ Shared ("cast" ) @ Cached CastToJavaStringNode castToStringNode ) {
2591
- String formatString = castFormatString (formatStringObj , castToStringNode );
2592
- if (formatString .isEmpty ()) {
2593
- return ensureStrCallNode ().executeObject (frame , self );
2594
- }
2582
+ @ Specialization (guards = "!formatString.isEmpty()" )
2583
+ Object formatPI (VirtualFrame frame , PInt self , String formatString ) {
2595
2584
PythonCore core = getCore ();
2596
2585
Spec spec = getSpec (formatString , core );
2597
2586
if (isDoubleSpec (spec )) {
2598
- return formatDouble (core , spec , asDouble (frame , self ));
2587
+ // lazy init of floatNode serves as branch profile
2588
+ double doubleVal = asDouble (frame , self );
2589
+ return formatDouble (core , spec , doubleVal );
2599
2590
}
2600
2591
validateIntegerSpec (core , spec );
2601
2592
return formatPInt (self , core , spec );
0 commit comments