|
1 | 1 | /* |
2 | | - * Copyright (c) 2017, 2025, Oracle and/or its affiliates. |
| 2 | + * Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
3 | 3 | * Copyright (c) 2014, Regents of the University of California |
4 | 4 | * |
5 | 5 | * All rights reserved. |
|
55 | 55 | import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject; |
56 | 56 | import com.oracle.graal.python.builtins.objects.function.PKeyword; |
57 | 57 | import com.oracle.graal.python.builtins.objects.module.PythonModule; |
58 | | -import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode; |
59 | 58 | import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
60 | 59 | import com.oracle.graal.python.builtins.objects.type.TpSlots; |
61 | | -import com.oracle.graal.python.builtins.objects.type.TypeNodes; |
62 | 60 | import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun.HashBuiltinNode; |
63 | 61 | import com.oracle.graal.python.builtins.objects.type.slots.TpSlotRichCompare.RichCmpBuiltinNode; |
64 | 62 | import com.oracle.graal.python.lib.PyObjectGetAttr; |
|
76 | 74 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
77 | 75 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
78 | 76 | import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode; |
79 | | -import com.oracle.graal.python.nodes.object.GetClassNode; |
80 | 77 | import com.oracle.graal.python.nodes.util.CannotCastException; |
81 | 78 | import com.oracle.graal.python.nodes.util.CastToTruffleStringNode; |
82 | 79 | import com.oracle.graal.python.runtime.ExecutionContext.BoundaryCallContext; |
83 | 80 | import com.oracle.graal.python.runtime.IndirectCallData.BoundaryCallData; |
84 | 81 | import com.oracle.graal.python.runtime.PythonContext; |
85 | 82 | import com.oracle.graal.python.runtime.object.PFactory; |
86 | 83 | import com.oracle.graal.python.util.PythonUtils; |
87 | | -import com.oracle.truffle.api.CompilerDirectives; |
88 | 84 | import com.oracle.truffle.api.dsl.Bind; |
89 | 85 | import com.oracle.truffle.api.dsl.Cached; |
90 | 86 | import com.oracle.truffle.api.dsl.Cached.Shared; |
@@ -285,105 +281,44 @@ static Object getDoc(PBuiltinMethod self, |
285 | 281 | } |
286 | 282 | } |
287 | 283 |
|
| 284 | + static TruffleString getFunctionAttr(VirtualFrame frame, Object self, Node inliningTarget, CastToTruffleStringNode toStringNode, PyObjectGetAttr getAttr, PRaiseNode raiseNode, |
| 285 | + TruffleString attrName) { |
| 286 | + Object function; |
| 287 | + if (self instanceof PMethod method) { |
| 288 | + function = method.getFunction(); |
| 289 | + } else { |
| 290 | + function = ((PBuiltinMethod) self).getFunction(); |
| 291 | + } |
| 292 | + try { |
| 293 | + return toStringNode.execute(inliningTarget, getAttr.execute(frame, inliningTarget, function, attrName)); |
| 294 | + } catch (CannotCastException cce) { |
| 295 | + throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A_UNICODE_OBJECT, attrName); |
| 296 | + } |
| 297 | + } |
| 298 | + |
288 | 299 | @Builtin(name = J___NAME__, minNumOfPositionalArgs = 1, isGetter = true) |
289 | 300 | @GenerateNodeFactory |
290 | 301 | public abstract static class NameNode extends PythonUnaryBuiltinNode { |
291 | 302 | @Specialization |
292 | | - static Object getName(VirtualFrame frame, PBuiltinMethod method, |
293 | | - @Bind Node inliningTarget, |
294 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
295 | | - @Shared("getAttr") @Cached PyObjectGetAttr getAttr) { |
296 | | - try { |
297 | | - return toStringNode.execute(inliningTarget, getAttr.execute(frame, inliningTarget, method.getFunction(), T___NAME__)); |
298 | | - } catch (CannotCastException cce) { |
299 | | - throw CompilerDirectives.shouldNotReachHere(); |
300 | | - } |
301 | | - } |
302 | | - |
303 | | - @Specialization |
304 | | - static Object getName(VirtualFrame frame, PMethod method, |
| 303 | + static TruffleString getName(VirtualFrame frame, Object self, |
305 | 304 | @Bind Node inliningTarget, |
306 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
307 | | - @Shared("getAttr") @Cached PyObjectGetAttr getAttr) { |
308 | | - try { |
309 | | - return toStringNode.execute(inliningTarget, getAttr.execute(frame, inliningTarget, method.getFunction(), T___NAME__)); |
310 | | - } catch (CannotCastException cce) { |
311 | | - throw CompilerDirectives.shouldNotReachHere(); |
312 | | - } |
| 305 | + @Cached CastToTruffleStringNode toStringNode, |
| 306 | + @Cached PyObjectGetAttr getAttr, |
| 307 | + @Cached PRaiseNode raiseNode) { |
| 308 | + return getFunctionAttr(frame, self, inliningTarget, toStringNode, getAttr, raiseNode, T___NAME__); |
313 | 309 | } |
314 | 310 | } |
315 | 311 |
|
316 | 312 | @Builtin(name = J___QUALNAME__, minNumOfPositionalArgs = 1, isGetter = true) |
317 | 313 | @GenerateNodeFactory |
318 | 314 | public abstract static class QualNameNode extends PythonUnaryBuiltinNode { |
319 | | - |
320 | | - protected static boolean isSelfModuleOrNull(PMethod method) { |
321 | | - return method.getSelf() == PNone.NO_VALUE || PGuards.isPythonModule(method.getSelf()); |
322 | | - } |
323 | | - |
324 | | - protected static boolean isSelfModuleOrNull(PBuiltinMethod method) { |
325 | | - return method.getSelf() == PNone.NO_VALUE || PGuards.isPythonModule(method.getSelf()); |
326 | | - } |
327 | | - |
328 | | - @Specialization(guards = "isSelfModuleOrNull(method)") |
329 | | - static TruffleString doSelfIsModule(VirtualFrame frame, PMethod method, |
330 | | - @Bind Node inliningTarget, |
331 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
332 | | - @Shared("lookupName") @Cached PyObjectLookupAttr lookupName) { |
333 | | - return getName(frame, inliningTarget, method.getFunction(), toStringNode, lookupName); |
334 | | - } |
335 | | - |
336 | | - @Specialization(guards = "isSelfModuleOrNull(method)") |
337 | | - static TruffleString doSelfIsModule(VirtualFrame frame, PBuiltinMethod method, |
338 | | - @Bind Node inliningTarget, |
339 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
340 | | - @Shared("lookupName") @Cached PyObjectLookupAttr lookupName) { |
341 | | - return getName(frame, inliningTarget, method.getFunction(), toStringNode, lookupName); |
342 | | - } |
343 | | - |
344 | | - @Specialization(guards = "!isSelfModuleOrNull(method)") |
345 | | - static TruffleString doSelfIsObject(VirtualFrame frame, PMethod method, |
346 | | - @Bind Node inliningTarget, |
347 | | - @Shared @Cached GetClassNode getClassNode, |
348 | | - @Shared @Cached TypeNodes.IsTypeNode isTypeNode, |
349 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
350 | | - @Shared("getQualname") @Cached PyObjectGetAttr getQualname, |
351 | | - @Shared("lookupName") @Cached PyObjectLookupAttr lookupName, |
352 | | - @Shared("formatter") @Cached SimpleTruffleStringFormatNode simpleTruffleStringFormatNode, |
353 | | - @Shared @Cached PRaiseNode raiseNode) { |
354 | | - return getQualName(frame, inliningTarget, method.getSelf(), method.getFunction(), getClassNode, isTypeNode, toStringNode, getQualname, lookupName, simpleTruffleStringFormatNode, |
355 | | - raiseNode); |
356 | | - } |
357 | | - |
358 | | - @Specialization(guards = "!isSelfModuleOrNull(method)") |
359 | | - static TruffleString doSelfIsObject(VirtualFrame frame, PBuiltinMethod method, |
| 315 | + @Specialization |
| 316 | + static TruffleString getQualname(VirtualFrame frame, Object self, |
360 | 317 | @Bind Node inliningTarget, |
361 | | - @Shared @Cached GetClassNode getClassNode, |
362 | | - @Shared @Cached TypeNodes.IsTypeNode isTypeNode, |
363 | | - @Shared("toStringNode") @Cached CastToTruffleStringNode toStringNode, |
364 | | - @Shared("getQualname") @Cached PyObjectGetAttr getQualname, |
365 | | - @Shared("lookupName") @Cached PyObjectLookupAttr lookupName, |
366 | | - @Shared("formatter") @Cached SimpleTruffleStringFormatNode simpleTruffleStringFormatNode, |
367 | | - @Shared @Cached PRaiseNode raiseNode) { |
368 | | - return getQualName(frame, inliningTarget, method.getSelf(), method.getFunction(), getClassNode, isTypeNode, toStringNode, getQualname, lookupName, simpleTruffleStringFormatNode, |
369 | | - raiseNode); |
370 | | - } |
371 | | - |
372 | | - private static TruffleString getQualName(VirtualFrame frame, Node inliningTarget, Object self, Object func, GetClassNode getClassNode, TypeNodes.IsTypeNode isTypeNode, |
373 | | - CastToTruffleStringNode toStringNode, PyObjectGetAttr getQualname, PyObjectLookupAttr lookupName, SimpleTruffleStringFormatNode simpleTruffleStringFormatNode, |
374 | | - PRaiseNode raiseNode) { |
375 | | - Object type = isTypeNode.execute(inliningTarget, self) ? self : getClassNode.execute(inliningTarget, self); |
376 | | - |
377 | | - try { |
378 | | - TruffleString typeQualName = toStringNode.execute(inliningTarget, getQualname.execute(frame, inliningTarget, type, T___QUALNAME__)); |
379 | | - return simpleTruffleStringFormatNode.format("%s.%s", typeQualName, getName(frame, inliningTarget, func, toStringNode, lookupName)); |
380 | | - } catch (CannotCastException cce) { |
381 | | - throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A_UNICODE_OBJECT, T___QUALNAME__); |
382 | | - } |
383 | | - } |
384 | | - |
385 | | - private static TruffleString getName(VirtualFrame frame, Node inliningTarget, Object func, CastToTruffleStringNode toStringNode, PyObjectLookupAttr lookupName) { |
386 | | - return toStringNode.execute(inliningTarget, lookupName.execute(frame, inliningTarget, func, T___NAME__)); |
| 318 | + @Cached CastToTruffleStringNode toStringNode, |
| 319 | + @Cached PyObjectGetAttr getQualname, |
| 320 | + @Cached PRaiseNode raiseNode) { |
| 321 | + return getFunctionAttr(frame, self, inliningTarget, toStringNode, getQualname, raiseNode, T___QUALNAME__); |
387 | 322 | } |
388 | 323 | } |
389 | 324 |
|
|
0 commit comments