|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules;
|
42 | 42 |
|
43 |
| -import java.util.ArrayList; |
44 | 43 | import java.util.List;
|
45 | 44 |
|
| 45 | +import com.oracle.graal.python.builtins.Builtin; |
46 | 46 | import com.oracle.graal.python.builtins.CoreFunctions;
|
| 47 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
47 | 48 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
| 49 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 50 | +import com.oracle.graal.python.builtins.objects.deque.DequeIterBuiltins.DequeIterNextNode; |
| 51 | +import com.oracle.graal.python.builtins.objects.deque.PDeque; |
| 52 | +import com.oracle.graal.python.builtins.objects.deque.PDequeIter; |
| 53 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 54 | +import com.oracle.graal.python.lib.PyNumberIndexNode; |
| 55 | +import com.oracle.graal.python.nodes.BuiltinNames; |
| 56 | +import com.oracle.graal.python.nodes.ErrorMessages; |
48 | 57 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
| 58 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode; |
| 59 | +import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode; |
| 60 | +import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode; |
| 61 | +import com.oracle.truffle.api.CompilerDirectives; |
| 62 | +import com.oracle.truffle.api.dsl.Cached; |
| 63 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 64 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
49 | 65 | import com.oracle.truffle.api.dsl.NodeFactory;
|
| 66 | +import com.oracle.truffle.api.dsl.Specialization; |
| 67 | +import com.oracle.truffle.api.frame.VirtualFrame; |
50 | 68 |
|
51 | 69 | @CoreFunctions(defineModule = "_collections")
|
52 | 70 | public class CollectionsModuleBuiltins extends PythonBuiltins {
|
53 | 71 | @Override
|
54 | 72 | protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
|
55 |
| - return new ArrayList<>(); |
| 73 | + return CollectionsModuleBuiltinsFactory.getFactories(); |
| 74 | + } |
| 75 | + |
| 76 | + // _collections.deque |
| 77 | + @Builtin(name = BuiltinNames.DEQUE, minNumOfPositionalArgs = 1, constructsClass = PythonBuiltinClassType.PDeque, takesVarArgs = true, takesVarKeywordArgs = true) |
| 78 | + @GenerateNodeFactory |
| 79 | + abstract static class DequeNode extends PythonVarargsBuiltinNode { |
| 80 | + |
| 81 | + @Override |
| 82 | + public Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported { |
| 83 | + if (arguments.length >= 1) { |
| 84 | + return doGeneric(arguments[0], null, null); |
| 85 | + } |
| 86 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 87 | + throw new VarargsBuiltinDirectInvocationNotSupported(); |
| 88 | + } |
| 89 | + |
| 90 | + @Specialization |
| 91 | + @SuppressWarnings("unused") |
| 92 | + PDeque doGeneric(Object cls, Object[] args, PKeyword[] kwargs) { |
| 93 | + return factory().createDeque(cls); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // _collections._deque_iterator |
| 98 | + @Builtin(name = BuiltinNames.DEQUE_ITER, constructsClass = PythonBuiltinClassType.PDequeIter, // |
| 99 | + minNumOfPositionalArgs = 2, parameterNames = {"$self", "iterable", "index"}) |
| 100 | + @GenerateNodeFactory |
| 101 | + abstract static class DequeIterNode extends PythonTernaryBuiltinNode { |
| 102 | + |
| 103 | + @Specialization(guards = "isNoValue(index)") |
| 104 | + PDequeIter doDeque(@SuppressWarnings("unused") Object cls, PDeque deque, @SuppressWarnings("unused") PNone index) { |
| 105 | + return factory().createDequeIter(deque); |
| 106 | + } |
| 107 | + |
| 108 | + @Specialization |
| 109 | + PDequeIter doDequeInt(@SuppressWarnings("unused") Object cls, PDeque deque, int index, |
| 110 | + @Shared("getNextNode") @Cached DequeIterNextNode getNextNode) { |
| 111 | + PDequeIter dequeIter = factory().createDequeIter(deque); |
| 112 | + for (int i = 0; i < index; i++) { |
| 113 | + getNextNode.execute(dequeIter); |
| 114 | + } |
| 115 | + return dequeIter; |
| 116 | + } |
| 117 | + |
| 118 | + @Specialization(replaces = {"doDeque", "doDequeInt"}) |
| 119 | + PDequeIter doGeneric(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object deque, Object indexObj, |
| 120 | + @Cached PyNumberIndexNode toIndexNode, |
| 121 | + @Cached CastToJavaIntExactNode castToJavaIntExactNode, |
| 122 | + @Shared("getNextNode") @Cached DequeIterNextNode getNextNode) { |
| 123 | + if (deque instanceof PDeque) { |
| 124 | + if (indexObj != PNone.NO_VALUE) { |
| 125 | + int index = castToJavaIntExactNode.execute(toIndexNode.execute(frame, indexObj)); |
| 126 | + return doDequeInt(cls, (PDeque) deque, index, getNextNode); |
| 127 | + } |
| 128 | + return doDeque(cls, (PDeque) deque, PNone.NO_VALUE); |
| 129 | + } |
| 130 | + throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.EXPECTED_OBJ_TYPE_S_GOT_P, BuiltinNames.DEQUE, deque); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // _collections._deque_reverse_iterator |
| 135 | + @Builtin(name = BuiltinNames.DEQUE_REV_ITER, constructsClass = PythonBuiltinClassType.PDequeRevIter, // |
| 136 | + minNumOfPositionalArgs = 2, parameterNames = {"$self", "iterable", "index"}) |
| 137 | + @GenerateNodeFactory |
| 138 | + abstract static class DequeRevIterNode extends PythonTernaryBuiltinNode { |
| 139 | + |
| 140 | + @Specialization(guards = "isNoValue(index)") |
| 141 | + PDequeIter doDeque(@SuppressWarnings("unused") Object cls, PDeque deque, @SuppressWarnings("unused") PNone index) { |
| 142 | + return factory().createDequeRevIter(deque); |
| 143 | + } |
| 144 | + |
| 145 | + @Specialization |
| 146 | + PDequeIter doDequeInt(@SuppressWarnings("unused") Object cls, PDeque deque, int index, |
| 147 | + @Shared("getNextNode") @Cached DequeIterNextNode getNextNode) { |
| 148 | + PDequeIter dequeIter = factory().createDequeRevIter(deque); |
| 149 | + for (int i = 0; i < index; i++) { |
| 150 | + getNextNode.execute(dequeIter); |
| 151 | + } |
| 152 | + return dequeIter; |
| 153 | + } |
| 154 | + |
| 155 | + @Specialization(replaces = {"doDeque", "doDequeInt"}) |
| 156 | + PDequeIter doGeneric(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object deque, Object indexObj, |
| 157 | + @Cached PyNumberIndexNode toIndexNode, |
| 158 | + @Cached CastToJavaIntExactNode castToJavaIntExactNode, |
| 159 | + @Shared("getNextNode") @Cached DequeIterNextNode getNextNode) { |
| 160 | + if (deque instanceof PDeque) { |
| 161 | + if (indexObj != PNone.NO_VALUE) { |
| 162 | + int index = castToJavaIntExactNode.execute(toIndexNode.execute(frame, indexObj)); |
| 163 | + return doDequeInt(cls, (PDeque) deque, index, getNextNode); |
| 164 | + } |
| 165 | + return doDeque(cls, (PDeque) deque, PNone.NO_VALUE); |
| 166 | + } |
| 167 | + throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.EXPECTED_OBJ_TYPE_S_GOT_P, BuiltinNames.DEQUE, deque); |
| 168 | + } |
56 | 169 | }
|
57 | 170 | }
|
0 commit comments