2929
3030import com .oracle .graal .python .PythonLanguage ;
3131import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
32- import com .oracle .graal .python .builtins .objects .cext .CExtNodes .PCallCapiFunction ;
3332import com .oracle .graal .python .builtins .objects .cext .PythonNativeWrapperLibrary ;
3433import com .oracle .graal .python .builtins .objects .function .PArguments .ThreadState ;
3534import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
3635import com .oracle .graal .python .builtins .objects .str .StringNodes .StringMaterializeNode ;
36+ import com .oracle .graal .python .builtins .objects .str .StringNodesFactory .StringMaterializeNodeGen ;
3737import com .oracle .graal .python .nodes .ErrorMessages ;
3838import com .oracle .graal .python .nodes .PRaiseNode ;
3939import com .oracle .graal .python .nodes .attributes .LookupAttributeInMRONode ;
4040import com .oracle .graal .python .nodes .attributes .LookupInheritedAttributeNode ;
4141import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
4242import com .oracle .graal .python .nodes .util .CannotCastException ;
43+ import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
4344import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
4445import com .oracle .graal .python .runtime .sequence .PSequence ;
4546import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
4647import com .oracle .truffle .api .CompilerDirectives ;
4748import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49+ import com .oracle .truffle .api .dsl .Bind ;
4850import com .oracle .truffle .api .dsl .Cached ;
4951import com .oracle .truffle .api .dsl .Cached .Exclusive ;
5052import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -69,19 +71,7 @@ public PString(Object clazz, Shape instanceShape, CharSequence value) {
6971 }
7072
7173 public String getValue () {
72- return PString .getValue (value );
73- }
74-
75- public static String getValue (CharSequence charSequence ) {
76- if (charSequence instanceof LazyString ) {
77- LazyString s = (LazyString ) charSequence ;
78- return s .materialize ();
79- } else if (charSequence instanceof NativeCharSequence ) {
80- NativeCharSequence s = (NativeCharSequence ) charSequence ;
81- return s .materialize ();
82- } else {
83- return (String ) charSequence ;
84- }
74+ return StringMaterializeNodeGen .getUncached ().execute (this );
8575 }
8676
8777 public CharSequence getCharSequence () {
@@ -127,7 +117,7 @@ static int string(PString self, @SuppressWarnings("unused") ThreadState state,
127117 @ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
128118 @ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
129119 @ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
130- return (( String ) self .value ).length ();
120+ return CompilerDirectives . castExact ( self .value , String . class ).length ();
131121 }
132122
133123 @ Specialization (guards = {
@@ -138,7 +128,7 @@ static int lazyString(PString self, @SuppressWarnings("unused") ThreadState stat
138128 @ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
139129 @ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
140130 @ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
141- return (( LazyString ) self .value ).length ();
131+ return CompilerDirectives . castExact ( self .value , LazyString . class ).length ();
142132 }
143133
144134 @ Specialization (guards = {
@@ -149,21 +139,21 @@ static int nativeString(PString self, @SuppressWarnings("unused") ThreadState st
149139 @ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
150140 @ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
151141 @ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
152- return (( NativeCharSequence ) self .value ).length ();
142+ return CompilerDirectives . castExact ( self .value , NativeCharSequence . class ). getMaterialized ( ).length ();
153143 }
154144
155145 @ Specialization (guards = {
156146 "isNativeString(self.getCharSequence())" , "!isMaterialized(self.getCharSequence())" ,
157147 "isBuiltin(self, profile) || hasBuiltinLen(self, lookupSelf, lookupString)"
158148 }, replaces = "nativeString" , limit = "1" )
159- static int nativeStringMat (PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
149+ static int nativeStringMat (@ SuppressWarnings ("unused" ) PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
150+ @ Bind ("getNativeCharSequence(self)" ) NativeCharSequence nativeCharSequence ,
160151 @ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
161152 @ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
162153 @ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ,
163- @ Cached PCallCapiFunction callCapi ) {
164- NativeCharSequence ncs = (NativeCharSequence ) self .value ;
165- ncs .materialize (callCapi );
166- return ncs .length ();
154+ @ CachedLibrary ("nativeCharSequence" ) InteropLibrary lib ,
155+ @ Cached CastToJavaIntExactNode castToJavaIntNode ) {
156+ return nativeCharSequence .length (lib , castToJavaIntNode );
167157 }
168158
169159 @ Specialization (replaces = {"string" , "lazyString" , "nativeString" , "nativeStringMat" })
@@ -178,6 +168,10 @@ static int subclassedString(PString self, ThreadState state,
178168 // call the generic implementation in the superclass
179169 return self .lengthWithState (state , plib , methodLib , gotState , hasLen , ltZero , raiseNode , lib );
180170 }
171+
172+ static NativeCharSequence getNativeCharSequence (PString self ) {
173+ return (NativeCharSequence ) self .value ;
174+ }
181175 }
182176
183177 @ ExportMessage
0 commit comments