Skip to content

Commit 663eade

Browse files
committed
move meta messages to appropriate subclasses
1 parent bb32648 commit 663eade

File tree

5 files changed

+160
-66
lines changed

5 files changed

+160
-66
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
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.classes.IsSubtypeNode;
37+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
3638
import com.oracle.graal.python.runtime.PythonContext;
3739
import com.oracle.truffle.api.CompilerAsserts;
3840
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
41+
import com.oracle.truffle.api.dsl.Cached;
3942
import com.oracle.truffle.api.dsl.CachedContext;
4043
import com.oracle.truffle.api.interop.ArityException;
4144
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -492,4 +495,26 @@ static int equalsInternal(PythonBuiltinClassType self, Object other, @SuppressWa
492495
return 0;
493496
}
494497
}
498+
499+
@ExportMessage
500+
static boolean isMetaObject(@SuppressWarnings("unused") PythonBuiltinClassType self) {
501+
return true;
502+
}
503+
504+
@ExportMessage
505+
static boolean isMetaInstance(PythonBuiltinClassType self, Object instance,
506+
@Cached GetLazyClassNode getClass,
507+
@Cached IsSubtypeNode isSubtype) {
508+
return isSubtype.execute(getClass.execute(instance), self);
509+
}
510+
511+
@ExportMessage
512+
static String getMetaSimpleName(PythonBuiltinClassType self) {
513+
return self.getName();
514+
}
515+
516+
@ExportMessage
517+
static String getMetaQualifiedName(PythonBuiltinClassType self) {
518+
return self.getQualifiedName();
519+
}
495520
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
package com.oracle.graal.python.builtins.objects;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
44-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
45-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__QUALNAME__;
4644
import static com.oracle.graal.python.nodes.SpecialMethodNames.__BOOL__;
4745
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
4846
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELETE__;
@@ -582,71 +580,10 @@ public LazyPythonClass getLazyPythonClass() {
582580

583581
@ExportMessage
584582
public boolean isInstantiable(
585-
@Shared("isTypeThis") @Cached TypeNodes.IsTypeNode isTypeNode) {
583+
@Cached TypeNodes.IsTypeNode isTypeNode) {
586584
return isTypeNode.execute(this);
587585
}
588586

589-
@ExportMessage
590-
public boolean isMetaObject(
591-
@Shared("isTypeThis") @Cached TypeNodes.IsTypeNode isTypeNode) {
592-
return isTypeNode.execute(this);
593-
}
594-
595-
@ExportMessage
596-
public boolean isMetaInstance(Object instance,
597-
@Shared("isTypeThis") @Cached TypeNodes.IsTypeNode isTypeNode,
598-
@Exclusive @Cached GetLazyClassNode getClass,
599-
@Exclusive @Cached IsSubtypeNode isSubtype) throws UnsupportedMessageException {
600-
if (!isTypeNode.execute(this)) {
601-
throw UnsupportedMessageException.create();
602-
}
603-
if (this instanceof LazyPythonClass) {
604-
return isSubtype.execute(getClass.execute(instance), (LazyPythonClass) this);
605-
} else {
606-
// TODO: (tfel) support subtype checking for native classes
607-
throw UnsupportedMessageException.create();
608-
}
609-
}
610-
611-
@ExportMessage
612-
public String getMetaSimpleName(
613-
@CachedLibrary("this") InteropLibrary lib,
614-
@Shared("nameCast") @Cached CastToJavaStringNode castStr,
615-
@Shared("isTypeThis") @Cached TypeNodes.IsTypeNode isTypeNode) throws UnsupportedMessageException {
616-
if (!isTypeNode.execute(this)) {
617-
throw UnsupportedMessageException.create();
618-
}
619-
try {
620-
return castStr.execute(lib.readMember(this, __NAME__));
621-
} catch (UnsupportedMessageException | UnknownIdentifierException e) {
622-
return fallbackName();
623-
}
624-
}
625-
626-
@ExportMessage
627-
public String getMetaQualifiedName(
628-
@CachedLibrary("this") InteropLibrary lib,
629-
@Shared("nameCast") @Cached CastToJavaStringNode castStr,
630-
@Shared("isTypeThis") @Cached TypeNodes.IsTypeNode isTypeNode) throws UnsupportedMessageException {
631-
if (!isTypeNode.execute(this)) {
632-
throw UnsupportedMessageException.create();
633-
}
634-
try {
635-
return castStr.execute(lib.readMember(this, __QUALNAME__));
636-
} catch (UnsupportedMessageException | UnknownIdentifierException e) {
637-
return fallbackName();
638-
}
639-
}
640-
641-
private String fallbackName() {
642-
CompilerDirectives.transferToInterpreter();
643-
if (this instanceof LazyPythonClass) {
644-
return ((LazyPythonClass) this).getName();
645-
} else {
646-
return "unknown-class";
647-
}
648-
}
649-
650587
@ExportMessage
651588
public Object instantiate(Object[] arguments,
652589
@Exclusive @Cached PExecuteNode executeNode) throws UnsupportedMessageException {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonAbstractNativeObject.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.objects.PNone;
5151
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
5252
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.AsPythonObjectNode;
53+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.GetTypeMemberNode;
5354
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ImportCAPISymbolNode;
5455
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
5556
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToJavaNode;
@@ -58,6 +59,7 @@
5859
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
5960
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6061
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
62+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6163
import com.oracle.graal.python.nodes.PRaiseNode;
6264
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
6365
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
@@ -66,6 +68,7 @@
6668
import com.oracle.truffle.api.Assumption;
6769
import com.oracle.truffle.api.CompilerAsserts;
6870
import com.oracle.truffle.api.CompilerDirectives;
71+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6972
import com.oracle.truffle.api.dsl.Cached;
7073
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7174
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -88,7 +91,7 @@
8891

8992
@ExportLibrary(PythonObjectLibrary.class)
9093
@ExportLibrary(ReferenceLibrary.class)
91-
@ExportLibrary(PythonObjectLibrary.class)
94+
@ExportLibrary(InteropLibrary.class)
9295
public final class PythonAbstractNativeObject extends PythonAbstractObject implements PythonNativeObject, PythonNativeClass {
9396

9497
public final TruffleObject object;
@@ -273,4 +276,48 @@ static boolean doOther(PythonAbstractNativeObject receiver, Object other) {
273276
return false;
274277
}
275278
}
279+
280+
@ExportMessage
281+
boolean isMetaObject(
282+
@Shared("isType") @Cached TypeNodes.IsTypeNode isType) {
283+
return isType.execute(this);
284+
}
285+
286+
@ExportMessage
287+
boolean isMetaInstance(Object instance,
288+
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
289+
@Cached GetLazyClassNode getClass,
290+
@Cached IsSubtypeNode isSubtype) throws UnsupportedMessageException {
291+
if (!isType.execute(this)) {
292+
throw UnsupportedMessageException.create();
293+
}
294+
return isSubtype.execute(getClass.execute(instance), this);
295+
}
296+
297+
@ExportMessage
298+
String getMetaSimpleName(
299+
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
300+
@Shared("getTypeMember") @Cached GetTypeMemberNode getTpNameNode) throws UnsupportedMessageException {
301+
return getSimpleName(getMetaQualifiedName(isType, getTpNameNode));
302+
}
303+
304+
@TruffleBoundary
305+
private static String getSimpleName(String fqname) {
306+
int firstDot = fqname.indexOf('.');
307+
if (firstDot != -1) {
308+
return fqname.substring(firstDot + 1);
309+
}
310+
return fqname;
311+
}
312+
313+
@ExportMessage
314+
String getMetaQualifiedName(
315+
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
316+
@Shared("getTypeMember") @Cached GetTypeMemberNode getTpNameNode) throws UnsupportedMessageException {
317+
if (!isType.execute(this)) {
318+
throw UnsupportedMessageException.create();
319+
}
320+
// 'tp_name' contains the fully-qualified name, i.e., 'module.A.B...'
321+
return (String) getTpNameNode.execute(this, NativeMemberNames.TP_NAME);
322+
}
276323
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonBuiltinClass.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929

3030
import com.oracle.graal.python.PythonLanguage;
3131
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
32+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
33+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
3234
import com.oracle.truffle.api.CompilerAsserts;
35+
import com.oracle.truffle.api.dsl.Cached;
36+
import com.oracle.truffle.api.interop.InteropLibrary;
37+
import com.oracle.truffle.api.library.ExportLibrary;
38+
import com.oracle.truffle.api.library.ExportMessage;
3339
import com.oracle.truffle.api.object.HiddenKey;
3440

3541
/**
3642
* A Python built-in class that is immutable.
3743
*/
44+
@ExportLibrary(InteropLibrary.class)
3845
public final class PythonBuiltinClass extends PythonManagedClass {
3946
private final PythonBuiltinClassType type;
4047

@@ -63,4 +70,27 @@ public void setAttributeUnsafe(Object name, Object value) {
6370
public PythonBuiltinClassType getType() {
6471
return type;
6572
}
73+
74+
@ExportMessage
75+
@SuppressWarnings("static-method")
76+
boolean isMetaObject() {
77+
return true;
78+
}
79+
80+
@ExportMessage
81+
boolean isMetaInstance(Object instance,
82+
@Cached GetLazyClassNode getClass,
83+
@Cached IsSubtypeNode isSubtype) {
84+
return isSubtype.execute(getClass.execute(instance), this);
85+
}
86+
87+
@ExportMessage
88+
String getMetaSimpleName() {
89+
return type.getName();
90+
}
91+
92+
@ExportMessage
93+
String getMetaQualifiedName() {
94+
return type.getQualifiedName();
95+
}
6696
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonClass.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,13 +25,68 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.type;
2727

28+
import com.oracle.graal.python.nodes.SpecialAttributeNames;
29+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
30+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
31+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
32+
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
33+
import com.oracle.truffle.api.dsl.Cached;
34+
import com.oracle.truffle.api.dsl.Cached.Exclusive;
35+
import com.oracle.truffle.api.dsl.Cached.Shared;
36+
import com.oracle.truffle.api.interop.InteropLibrary;
37+
import com.oracle.truffle.api.library.ExportLibrary;
38+
import com.oracle.truffle.api.library.ExportMessage;
39+
2840
/**
2941
* Mutable class.
3042
*/
43+
@ExportLibrary(InteropLibrary.class)
3144
public final class PythonClass extends PythonManagedClass {
3245

3346
public PythonClass(LazyPythonClass typeClass, String name, PythonAbstractClass[] baseClasses) {
3447
super(typeClass, name, baseClasses);
3548
}
3649

50+
@ExportMessage
51+
@SuppressWarnings("static-method")
52+
boolean isMetaObject() {
53+
return true;
54+
}
55+
56+
@ExportMessage
57+
boolean isMetaInstance(Object instance,
58+
@Cached GetLazyClassNode getClass,
59+
@Cached IsSubtypeNode isSubtype) {
60+
return isSubtype.execute(getClass.execute(instance), this);
61+
}
62+
63+
@ExportMessage
64+
String getMetaSimpleName(
65+
@Exclusive @Cached ReadAttributeFromDynamicObjectNode getName,
66+
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
67+
// n.b.: we're reading directly from the storage here, because this
68+
// method must not have side-effects, so even if there's a __dict__, we
69+
// cannot call its __getitem__
70+
String result = castStr.execute(getName.execute(getStorage(), SpecialAttributeNames.__NAME__));
71+
if (result == null) {
72+
return "unnamed-class";
73+
} else {
74+
return result;
75+
}
76+
}
77+
78+
@ExportMessage
79+
String getMetaQualifiedName(
80+
@Exclusive @Cached ReadAttributeFromDynamicObjectNode getName,
81+
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
82+
// n.b.: we're reading directly from the storage here, because this
83+
// method must not have side-effects, so even if there's a __dict__, we
84+
// cannot call its __getitem__
85+
String result = castStr.execute(getName.execute(getStorage(), SpecialAttributeNames.__QUALNAME__));
86+
if (result == null) {
87+
return "unnamed-class";
88+
} else {
89+
return result;
90+
}
91+
}
3792
}

0 commit comments

Comments
 (0)