Skip to content

Commit 5e796bf

Browse files
committed
[GR-26456] Make state-less POL messages final
PullRequest: graalpython/1323
2 parents 30d68f9 + b221f25 commit 5e796bf

15 files changed

+210
-225
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3434
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
3535
import com.oracle.graal.python.nodes.BuiltinNames;
36+
import com.oracle.graal.python.nodes.ErrorMessages;
37+
import com.oracle.graal.python.nodes.PRaiseNode;
3638
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
3739
import com.oracle.graal.python.runtime.PythonContext;
3840
import com.oracle.truffle.api.CompilerAsserts;
3941
import com.oracle.truffle.api.CompilerDirectives;
4042
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4143
import com.oracle.truffle.api.dsl.Cached;
44+
import com.oracle.truffle.api.dsl.Cached.Shared;
4245
import com.oracle.truffle.api.dsl.CachedContext;
4346
import com.oracle.truffle.api.dsl.Fallback;
4447
import com.oracle.truffle.api.dsl.Specialization;
@@ -414,116 +417,137 @@ public boolean hasMembers() {
414417

415418
@ExportMessage
416419
public Object getMembers(boolean includeInternal,
417-
@CachedLibrary(limit = "1") InteropLibrary lib,
420+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
418421
@CachedContext(PythonLanguage.class) PythonContext context) throws UnsupportedMessageException {
419422
return lib.getMembers(context.getCore().lookupType(this), includeInternal);
420423
}
421424

422425
@ExportMessage
423426
public boolean isMemberReadable(String key,
424-
@CachedLibrary(limit = "1") InteropLibrary lib,
427+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
425428
@CachedContext(PythonLanguage.class) PythonContext context) {
426429
return lib.isMemberReadable(context.getCore().lookupType(this), key);
427430
}
428431

429432
@ExportMessage
430433
public Object readMember(String key,
431-
@CachedLibrary(limit = "1") InteropLibrary lib,
434+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
432435
@CachedContext(PythonLanguage.class) PythonContext context) throws UnsupportedMessageException, UnknownIdentifierException {
433436
return lib.readMember(context.getCore().lookupType(this), key);
434437
}
435438

436439
@ExportMessage
437440
public boolean isMemberModifiable(String key,
438-
@CachedLibrary(limit = "1") InteropLibrary lib,
441+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
439442
@CachedContext(PythonLanguage.class) PythonContext context) {
440443
return lib.isMemberModifiable(context.getCore().lookupType(this), key);
441444
}
442445

443446
@ExportMessage
444447
public boolean isMemberInsertable(String key,
445-
@CachedLibrary(limit = "1") InteropLibrary lib,
448+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
446449
@CachedContext(PythonLanguage.class) PythonContext context) {
447450
return lib.isMemberInsertable(context.getCore().lookupType(this), key);
448451
}
449452

450453
@ExportMessage
451454
public void writeMember(String key, Object value,
452-
@CachedLibrary(limit = "1") InteropLibrary lib,
455+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
453456
@CachedContext(PythonLanguage.class) PythonContext context) throws UnsupportedMessageException, UnknownIdentifierException, UnsupportedTypeException {
454457
lib.writeMember(context.getCore().lookupType(this), key, value);
455458
}
456459

457460
@ExportMessage
458461
public boolean isMemberRemovable(String key,
459-
@CachedLibrary(limit = "1") InteropLibrary lib,
462+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
460463
@CachedContext(PythonLanguage.class) PythonContext context) {
461464
return lib.isMemberRemovable(context.getCore().lookupType(this), key);
462465
}
463466

464467
@ExportMessage
465468
public void removeMember(String key,
466-
@CachedLibrary(limit = "1") InteropLibrary lib,
469+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
467470
@CachedContext(PythonLanguage.class) PythonContext context) throws UnsupportedMessageException, UnknownIdentifierException {
468471
lib.removeMember(context.getCore().lookupType(this), key);
469472
}
470473

471474
@ExportMessage
472475
public boolean isMemberInvocable(String key,
473-
@CachedLibrary(limit = "1") InteropLibrary lib,
476+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
474477
@CachedContext(PythonLanguage.class) PythonContext context) {
475478
return lib.isMemberInvocable(context.getCore().lookupType(this), key);
476479
}
477480

478481
@ExportMessage
479482
public Object invokeMember(String key, Object[] arguments,
480-
@CachedLibrary(limit = "1") InteropLibrary lib,
483+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
481484
@CachedContext(PythonLanguage.class) PythonContext context) throws UnsupportedMessageException, ArityException, UnknownIdentifierException, UnsupportedTypeException {
482485
return lib.invokeMember(context.getCore().lookupType(this), key, arguments);
483486
}
484487

485488
@ExportMessage
486489
public boolean isMemberInternal(String key,
487-
@CachedLibrary(limit = "1") InteropLibrary lib,
490+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
488491
@CachedContext(PythonLanguage.class) PythonContext context) {
489492
return lib.isMemberInternal(context.getCore().lookupType(this), key);
490493
}
491494

492495
@ExportMessage
493496
public boolean hasMemberReadSideEffects(String key,
494-
@CachedLibrary(limit = "1") InteropLibrary lib,
497+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
495498
@CachedContext(PythonLanguage.class) PythonContext context) {
496499
return lib.hasMemberReadSideEffects(context.getCore().lookupType(this), key);
497500
}
498501

499502
@ExportMessage
500503
public boolean hasMemberWriteSideEffects(String key,
501-
@CachedLibrary(limit = "1") InteropLibrary lib,
504+
@Shared("interop") @CachedLibrary(limit = "1") InteropLibrary lib,
502505
@CachedContext(PythonLanguage.class) PythonContext context) {
503506
return lib.hasMemberWriteSideEffects(context.getCore().lookupType(this), key);
504507
}
505508

506509
@ExportMessage
507510
static boolean isSequenceType(PythonBuiltinClassType type,
508511
@CachedContext(PythonLanguage.class) PythonContext context,
509-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
512+
@Shared("pol") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
510513
return lib.isSequenceType(context.getCore().lookupType(type));
511514
}
512515

513516
@ExportMessage
514517
static boolean isMappingType(PythonBuiltinClassType type,
515518
@CachedContext(PythonLanguage.class) PythonContext context,
516-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
519+
@Shared("pol") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
517520
return lib.isMappingType(context.getCore().lookupType(type));
518521
}
519522

520523
@ExportMessage
521524
static long hashWithState(PythonBuiltinClassType type, ThreadState state,
522525
@CachedContext(PythonLanguage.class) PythonContext context,
523-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
526+
@Shared("pol") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
524527
return lib.hashWithState(context.getCore().lookupType(type), state);
525528
}
526529

530+
@ExportMessage
531+
@SuppressWarnings("unused")
532+
static double asJavaDoubleWithState(PythonBuiltinClassType type, ThreadState state,
533+
@Shared("raise") @Cached PRaiseNode raiseNode) {
534+
throw raiseNode.raise(TypeError, ErrorMessages.MUST_BE_REAL_NUMBER, type);
535+
}
536+
537+
@ExportMessage
538+
@SuppressWarnings("unused")
539+
static Object asPIntWithState(PythonBuiltinClassType type, ThreadState state,
540+
@Shared("raise") @Cached PRaiseNode raiseNode) {
541+
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER, type);
542+
}
543+
544+
@ExportMessage
545+
@SuppressWarnings("unused")
546+
static long asJavaLongWithState(PythonBuiltinClassType type, ThreadState state,
547+
@Shared("raise") @Cached PRaiseNode raiseNode) {
548+
throw raiseNode.raise(TypeError, ErrorMessages.MUST_BE_NUMERIC, type);
549+
}
550+
527551
@ExportMessage
528552
static Object getLazyPythonClass(@SuppressWarnings("unused") PythonBuiltinClassType type) {
529553
return PythonClass;
@@ -610,13 +634,8 @@ public static boolean isExceptionType(PythonBuiltinClassType type) {
610634
* {@link com.oracle.graal.python.builtins.objects.type.TypeBuiltins.ReprNode
611635
* TypeBuiltins.ReprNode}
612636
*/
613-
@ExportMessage
614-
String asPString() {
615-
return getQualifiedName();
616-
}
617-
618637
@ExportMessage
619638
String asPStringWithState(@SuppressWarnings("unused") ThreadState state) {
620-
return asPString();
639+
return getQualifiedName();
621640
}
622641
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/PFloat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.oracle.graal.python.PythonLanguage;
2929
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3030
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapperLibrary;
31+
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
3132
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
3233
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3334
import com.oracle.truffle.api.CompilerAsserts;
@@ -188,7 +189,7 @@ public boolean canBeJavaDouble() {
188189
}
189190

190191
@ExportMessage
191-
public double asJavaDouble() {
192+
public double asJavaDoubleWithState(@SuppressWarnings("unused") ThreadState threadState) {
192193
return value;
193194
}
194195
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,10 @@ public boolean canBeJavaDouble() {
245245
return true;
246246
}
247247

248-
@ExportMessage
249-
public double asJavaDouble(
250-
@CachedLibrary("this") PythonObjectLibrary lib,
251-
@Shared("castToDouble") @Cached CastToJavaDoubleNode castToDouble) {
252-
return castToDouble.execute(lib.asIndex(this));
253-
}
254-
255248
@ExportMessage
256249
public double asJavaDoubleWithState(ThreadState threadState,
257250
@CachedLibrary("this") PythonObjectLibrary lib,
258-
@Shared("castToDouble") @Cached CastToJavaDoubleNode castToDouble) {
251+
@Cached CastToJavaDoubleNode castToDouble) {
259252
return castToDouble.execute(lib.asIndexWithState(this, threadState));
260253
}
261254

@@ -265,15 +258,9 @@ public boolean canBeJavaLong() {
265258
return true;
266259
}
267260

268-
@ExportMessage
269-
public long asJavaLong(
270-
@Cached @Shared("asJavaLong") CastToJavaLongLossyNode castToLong) {
271-
return castToLong.execute(this);
272-
}
273-
274261
@ExportMessage
275262
public long asJavaLongWithState(@SuppressWarnings("unused") ThreadState threadState,
276-
@Cached @Shared("asJavaLong") CastToJavaLongLossyNode castToLong) {
263+
@Cached CastToJavaLongLossyNode castToLong) {
277264
return castToLong.execute(this);
278265
}
279266

@@ -283,11 +270,6 @@ public boolean canBePInt() {
283270
return true;
284271
}
285272

286-
@ExportMessage
287-
public PInt asPInt() {
288-
return this;
289-
}
290-
291273
@ExportMessage
292274
public PInt asPIntWithState(@SuppressWarnings("unused") ThreadState state) {
293275
return this;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/AbstractMethodBuiltins.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SELF__;
3434
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
3535
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
36+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
3637

3738
import java.util.List;
3839

@@ -137,6 +138,20 @@ boolean eq(@SuppressWarnings("unused") Object self, @SuppressWarnings("unused")
137138
}
138139
}
139140

141+
@Builtin(name = __HASH__, minNumOfPositionalArgs = 2)
142+
@GenerateNodeFactory
143+
abstract static class HashNode extends PythonUnaryBuiltinNode {
144+
@Specialization
145+
static long hash(PMethod self) {
146+
return self.hash();
147+
}
148+
149+
@Specialization
150+
static long hash(PBuiltinMethod self) {
151+
return self.hash();
152+
}
153+
}
154+
140155
@Builtin(name = __MODULE__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
141156
@GenerateNodeFactory
142157
abstract static class GetModuleNode extends PythonBinaryBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/PBuiltinMethod.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.oracle.truffle.api.CompilerAsserts;
3434
import com.oracle.truffle.api.library.ExportLibrary;
3535
import com.oracle.truffle.api.library.ExportMessage;
36+
import com.oracle.truffle.api.library.ExportMessage.Ignore;
3637
import com.oracle.truffle.api.object.Shape;
3738

3839
// Corresponds to PyCFunction, but that name is just confusing
@@ -85,8 +86,13 @@ boolean isHashable() {
8586
return true;
8687
}
8788

89+
@Ignore
90+
public long hash() {
91+
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
92+
}
93+
8894
@ExportMessage
8995
protected long hashWithState(@SuppressWarnings("unused") ThreadState state) {
90-
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
96+
return hash();
9197
}
9298
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/PMethod.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.truffle.api.library.CachedLibrary;
3636
import com.oracle.truffle.api.library.ExportLibrary;
3737
import com.oracle.truffle.api.library.ExportMessage;
38+
import com.oracle.truffle.api.library.ExportMessage.Ignore;
3839
import com.oracle.truffle.api.object.Shape;
3940
import com.oracle.truffle.api.source.SourceSection;
4041

@@ -99,8 +100,13 @@ boolean isHashable() {
99100
return true;
100101
}
101102

103+
@Ignore
104+
public long hash() {
105+
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
106+
}
107+
102108
@ExportMessage
103109
protected long hashWithState(@SuppressWarnings("unused") ThreadState state) {
104-
return PythonAbstractObject.systemHashCode(this.getSelf()) ^ PythonAbstractObject.systemHashCode(this.getFunction());
110+
return hash();
105111
}
106112
}

0 commit comments

Comments
 (0)