|
26 | 26 |
|
27 | 27 | package com.oracle.graal.python.builtins.objects.foreign; |
28 | 28 |
|
29 | | -import static com.oracle.graal.python.nodes.SpecialAttributeNames.__CLASS__; |
30 | 29 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__; |
31 | 30 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__BOOL__; |
32 | 31 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__; |
33 | 32 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELATTR__; |
34 | | -import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELETE__; |
35 | 33 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELITEM__; |
36 | 34 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__; |
37 | 35 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOORDIV__; |
38 | | -import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__; |
39 | | -import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__; |
40 | 36 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__; |
41 | | -import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__; |
42 | 37 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__; |
43 | 38 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__; |
44 | 39 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__; |
|
54 | 49 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__RTRUEDIV__; |
55 | 50 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__; |
56 | 51 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__; |
57 | | -import static com.oracle.graal.python.nodes.SpecialMethodNames.__SET__; |
58 | 52 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUB__; |
59 | 53 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__TRUEDIV__; |
60 | 54 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError; |
|
69 | 63 | import com.oracle.graal.python.builtins.objects.PNone; |
70 | 64 | import com.oracle.graal.python.builtins.objects.PNotImplemented; |
71 | 65 | import com.oracle.graal.python.builtins.objects.function.PKeyword; |
72 | | -import com.oracle.graal.python.builtins.objects.function.PythonCallable; |
73 | 66 | import com.oracle.graal.python.builtins.objects.list.PList; |
74 | | -import com.oracle.graal.python.builtins.objects.type.PythonClass; |
75 | 67 | import com.oracle.graal.python.nodes.PGuards; |
76 | | -import com.oracle.graal.python.nodes.SpecialMethodNames; |
77 | | -import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode; |
78 | | -import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode; |
79 | | -import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode; |
80 | 68 | import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
81 | 69 | import com.oracle.graal.python.nodes.expression.BinaryArithmetic; |
82 | 70 | import com.oracle.graal.python.nodes.expression.BinaryComparisonNode; |
|
87 | 75 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
88 | 76 | import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode; |
89 | 77 | import com.oracle.graal.python.nodes.interop.PTypeToForeignNode; |
90 | | -import com.oracle.graal.python.nodes.object.GetClassNode; |
91 | 78 | import com.oracle.graal.python.runtime.exception.PythonErrorType; |
92 | 79 | import com.oracle.truffle.api.CompilerDirectives; |
93 | 80 | import com.oracle.truffle.api.dsl.Cached; |
|
104 | 91 | import com.oracle.truffle.api.interop.UnsupportedMessageException; |
105 | 92 | import com.oracle.truffle.api.interop.UnsupportedTypeException; |
106 | 93 | import com.oracle.truffle.api.nodes.Node; |
107 | | -import com.oracle.truffle.api.profiles.BranchProfile; |
108 | | -import com.oracle.truffle.api.profiles.ConditionProfile; |
109 | 94 | import com.oracle.truffle.api.profiles.ValueProfile; |
110 | 95 |
|
111 | 96 | @CoreFunctions(extendClasses = TruffleObject.class) |
@@ -802,153 +787,6 @@ protected Object doGeneric(Object callee, @SuppressWarnings("unused") Object arg |
802 | 787 | } |
803 | 788 | } |
804 | 789 |
|
805 | | - @Builtin(name = __GETATTRIBUTE__, fixedNumOfArguments = 2) |
806 | | - @GenerateNodeFactory |
807 | | - public abstract static class GetattributeNode extends PythonBinaryBuiltinNode { |
808 | | - private final BranchProfile hasDescProfile = BranchProfile.create(); |
809 | | - private final BranchProfile isDescProfile = BranchProfile.create(); |
810 | | - private final BranchProfile hasValueProfile = BranchProfile.create(); |
811 | | - private final BranchProfile errorProfile = BranchProfile.create(); |
812 | | - private final ConditionProfile typeIsObjectProfile = ConditionProfile.createBinaryProfile(); |
813 | | - |
814 | | - @Child private LookupInheritedAttributeNode lookup = LookupInheritedAttributeNode.create(); |
815 | | - private final ValueProfile typeProfile = ValueProfile.createIdentityProfile(); |
816 | | - @Child private GetClassNode getObjectClassNode; |
817 | | - @Child private GetClassNode getDataClassNode; |
818 | | - @Child private LookupAttributeInMRONode lookupGetNode; |
819 | | - @Child private LookupAttributeInMRONode lookupSetNode; |
820 | | - @Child private LookupAttributeInMRONode lookupDeleteNode; |
821 | | - @Child private CallTernaryMethodNode dispatchGet; |
822 | | - @Child private Node attrReadNode; |
823 | | - @Child private LookupAndCallBinaryNode getattrNode; |
824 | | - |
825 | | - @Specialization |
826 | | - protected Object doIt(TruffleObject object, Object key) { |
827 | | - Object descr = lookup.execute(object, key); |
828 | | - PythonClass dataDescClass = null; |
829 | | - if (descr != PNone.NO_VALUE) { |
830 | | - hasDescProfile.enter(); |
831 | | - dataDescClass = getDataClass(descr); |
832 | | - Object delete = PNone.NO_VALUE; |
833 | | - Object set = lookupSet(dataDescClass); |
834 | | - if (set == PNone.NO_VALUE) { |
835 | | - delete = lookupDelete(dataDescClass); |
836 | | - } |
837 | | - if (set != PNone.NO_VALUE || delete != PNone.NO_VALUE) { |
838 | | - isDescProfile.enter(); |
839 | | - Object get = lookupGet(dataDescClass); |
840 | | - if (get instanceof PythonCallable) { |
841 | | - // Only override if __get__ is defined, too, for compatibility with CPython. |
842 | | - return dispatch(object, descr, get); |
843 | | - } |
844 | | - } |
845 | | - } |
846 | | - Object value = readAttribute(object, key); |
847 | | - if (value != PNone.NO_VALUE) { |
848 | | - hasValueProfile.enter(); |
849 | | - return value; |
850 | | - } |
851 | | - if (descr != PNone.NO_VALUE) { |
852 | | - hasDescProfile.enter(); |
853 | | - Object get = lookupGet(dataDescClass); |
854 | | - if (get == PNone.NO_VALUE) { |
855 | | - return descr; |
856 | | - } else if (get instanceof PythonCallable) { |
857 | | - return dispatch(object, descr, get); |
858 | | - } |
859 | | - } |
860 | | - errorProfile.enter(); |
861 | | - return fallbackGetattr(object, key); |
862 | | - } |
863 | | - |
864 | | - private Object fallbackGetattr(Object object, Object key) { |
865 | | - if (getattrNode == null) { |
866 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
867 | | - getattrNode = insert(LookupAndCallBinaryNode.create(SpecialMethodNames.__GETATTR__)); |
868 | | - } |
869 | | - return getattrNode.executeObject(object, key); |
870 | | - } |
871 | | - |
872 | | - private Object readAttribute(TruffleObject object, Object key) { |
873 | | - if (attrReadNode == null) { |
874 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
875 | | - attrReadNode = insert(Message.READ.createNode()); |
876 | | - } |
877 | | - try { |
878 | | - return ForeignAccess.sendRead(attrReadNode, object, key); |
879 | | - } catch (UnknownIdentifierException | UnsupportedMessageException e) { |
880 | | - return PNone.NO_VALUE; |
881 | | - } |
882 | | - } |
883 | | - |
884 | | - private Object dispatch(Object object, Object descr, Object get) { |
885 | | - if (dispatchGet == null) { |
886 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
887 | | - dispatchGet = insert(CallTernaryMethodNode.create()); |
888 | | - } |
889 | | - PythonClass type = getObjectClass(object); |
890 | | - return dispatchGet.execute(get, descr, typeIsObjectProfile.profile(type == object) ? PNone.NONE : object, type); |
891 | | - } |
892 | | - |
893 | | - private Object lookupGet(PythonClass dataDescClass) { |
894 | | - if (lookupGetNode == null) { |
895 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
896 | | - lookupGetNode = insert(LookupAttributeInMRONode.create()); |
897 | | - } |
898 | | - return lookupGetNode.execute(dataDescClass, __GET__); |
899 | | - } |
900 | | - |
901 | | - private Object lookupDelete(PythonClass dataDescClass) { |
902 | | - if (lookupDeleteNode == null) { |
903 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
904 | | - lookupDeleteNode = insert(LookupAttributeInMRONode.create()); |
905 | | - } |
906 | | - return lookupDeleteNode.execute(dataDescClass, __DELETE__); |
907 | | - } |
908 | | - |
909 | | - private Object lookupSet(PythonClass dataDescClass) { |
910 | | - if (lookupSetNode == null) { |
911 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
912 | | - lookupSetNode = insert(LookupAttributeInMRONode.create()); |
913 | | - } |
914 | | - return lookupSetNode.execute(dataDescClass, __SET__); |
915 | | - } |
916 | | - |
917 | | - private PythonClass getObjectClass(Object object) { |
918 | | - if (getObjectClassNode == null) { |
919 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
920 | | - getObjectClassNode = insert(GetClassNode.create()); |
921 | | - } |
922 | | - return typeProfile.profile(getObjectClassNode.execute(object)); |
923 | | - } |
924 | | - |
925 | | - private PythonClass getDataClass(Object descr) { |
926 | | - if (getDataClassNode == null) { |
927 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
928 | | - getDataClassNode = insert(GetClassNode.create()); |
929 | | - } |
930 | | - return getDataClassNode.execute(descr); |
931 | | - } |
932 | | - } |
933 | | - |
934 | | - @Builtin(name = __GETATTR__, fixedNumOfArguments = 3) |
935 | | - @GenerateNodeFactory |
936 | | - public abstract static class GetattrNode extends PythonBinaryBuiltinNode { |
937 | | - @Specialization(guards = {"isForeignObject(object)"}) |
938 | | - protected Object doIt(TruffleObject object, Object key, |
939 | | - @Cached("createReadNode()") Node readNode) { |
940 | | - try { |
941 | | - return ForeignAccess.sendRead(readNode, object, key); |
942 | | - } catch (UnknownIdentifierException | UnsupportedMessageException e) { |
943 | | - throw raise(PythonErrorType.AttributeError, "foreign object %s has no attribute %s", object, key); |
944 | | - } |
945 | | - } |
946 | | - |
947 | | - protected Node createReadNode() { |
948 | | - return Message.READ.createNode(); |
949 | | - } |
950 | | - } |
951 | | - |
952 | 790 | @Builtin(name = __GETITEM__, fixedNumOfArguments = 2) |
953 | 791 | @GenerateNodeFactory |
954 | 792 | abstract static class GetitemNode extends PythonBinaryBuiltinNode { |
@@ -1030,15 +868,4 @@ protected Object doIt(TruffleObject object, |
1030 | 868 | } |
1031 | 869 | } |
1032 | 870 | } |
1033 | | - |
1034 | | - @Builtin(name = __CLASS__, fixedNumOfArguments = 1, isGetter = true) |
1035 | | - @GenerateNodeFactory |
1036 | | - public abstract static class ClassNode extends PythonUnaryBuiltinNode { |
1037 | | - @Specialization(guards = {"isForeignObject(object)"}) |
1038 | | - protected Object doIt(TruffleObject object, |
1039 | | - @Cached("create()") GetClassNode getClassNode) { |
1040 | | - return getClassNode.execute(object); |
1041 | | - } |
1042 | | - } |
1043 | | - |
1044 | 871 | } |
0 commit comments