|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.objects.itertools; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.StopIteration; |
| 44 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError; |
| 45 | +import static com.oracle.graal.python.nodes.ErrorMessages.IS_NOT_A; |
| 46 | +import static com.oracle.graal.python.nodes.ErrorMessages.STATE_ARGUMENT_D_MUST_BE_A_S; |
| 47 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__; |
| 48 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__; |
| 49 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__REDUCE__; |
| 50 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETSTATE__; |
| 51 | + |
| 52 | +import com.oracle.graal.python.builtins.Builtin; |
| 53 | +import java.util.List; |
| 54 | + |
| 55 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 56 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 57 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 58 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions; |
| 59 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 60 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToArrayNode; |
| 61 | +import com.oracle.graal.python.builtins.objects.list.PList; |
| 62 | +import com.oracle.graal.python.builtins.objects.object.PythonObject; |
| 63 | +import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
| 64 | +import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode; |
| 65 | +import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.LenNode; |
| 66 | +import com.oracle.graal.python.lib.PyNumberAsSizeNode; |
| 67 | +import com.oracle.graal.python.lib.PyObjectGetIter; |
| 68 | +import com.oracle.graal.python.lib.PyObjectLookupAttr; |
| 69 | +import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode; |
| 70 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 71 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 72 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 73 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 74 | +import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile; |
| 75 | +import com.oracle.graal.python.runtime.exception.PException; |
| 76 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 77 | +import com.oracle.truffle.api.dsl.Cached; |
| 78 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 79 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 80 | +import com.oracle.truffle.api.dsl.Specialization; |
| 81 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 82 | +import com.oracle.truffle.api.profiles.BranchProfile; |
| 83 | +import java.util.ArrayList; |
| 84 | +import java.util.Arrays; |
| 85 | + |
| 86 | +@CoreFunctions(extendClasses = {PythonBuiltinClassType.PCycle}) |
| 87 | +public final class CycleBuiltins extends PythonBuiltins { |
| 88 | + |
| 89 | + @Override |
| 90 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 91 | + return CycleBuiltinsFactory.getFactories(); |
| 92 | + } |
| 93 | + |
| 94 | + @Builtin(name = __ITER__, minNumOfPositionalArgs = 1) |
| 95 | + @GenerateNodeFactory |
| 96 | + public abstract static class IterNode extends PythonUnaryBuiltinNode { |
| 97 | + @Specialization |
| 98 | + static Object iter(PCycle self) { |
| 99 | + return self; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Builtin(name = __NEXT__, minNumOfPositionalArgs = 1) |
| 104 | + @GenerateNodeFactory |
| 105 | + public abstract static class NextNode extends PythonUnaryBuiltinNode { |
| 106 | + @Specialization |
| 107 | + Object next(VirtualFrame frame, PCycle self, |
| 108 | + @Cached BuiltinFunctions.NextNode nextNode, |
| 109 | + @Cached IsBuiltinClassProfile isStopIterationProfile, |
| 110 | + @Cached BranchProfile iterableProfile, |
| 111 | + @Cached BranchProfile firstPassProfile, |
| 112 | + @Cached BranchProfile savedProfile) { |
| 113 | + if (self.getIterable() != null) { |
| 114 | + iterableProfile.enter(); |
| 115 | + try { |
| 116 | + Object item = nextNode.execute(frame, self.getIterable(), PNone.NO_VALUE); |
| 117 | + if (!self.isFirstpass()) { |
| 118 | + firstPassProfile.enter(); |
| 119 | + add(self.getSaved(), item); |
| 120 | + } |
| 121 | + return item; |
| 122 | + } catch (PException e) { |
| 123 | + e.expectStopIteration(isStopIterationProfile); |
| 124 | + self.setIterable(null); |
| 125 | + } |
| 126 | + } |
| 127 | + if (isEmpty(self.getSaved())) { |
| 128 | + savedProfile.enter(); |
| 129 | + throw raise(StopIteration); |
| 130 | + } |
| 131 | + Object item = get(self.getSaved(), self.getIndex()); |
| 132 | + self.setIndex(self.getIndex() + 1); |
| 133 | + if (self.getIndex() >= size(self.getSaved())) { |
| 134 | + self.setIndex(0); |
| 135 | + } |
| 136 | + return item; |
| 137 | + } |
| 138 | + |
| 139 | + @TruffleBoundary |
| 140 | + private boolean isEmpty(List<Object> l) { |
| 141 | + return l.isEmpty(); |
| 142 | + } |
| 143 | + |
| 144 | + @TruffleBoundary |
| 145 | + private Object add(List<Object> l, Object item) { |
| 146 | + return l.add(item); |
| 147 | + } |
| 148 | + |
| 149 | + @TruffleBoundary |
| 150 | + private Object get(List<Object> l, int idx) { |
| 151 | + return l.get(idx); |
| 152 | + } |
| 153 | + |
| 154 | + @TruffleBoundary |
| 155 | + private int size(List<Object> l) { |
| 156 | + return l.size(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Builtin(name = __REDUCE__, minNumOfPositionalArgs = 1) |
| 161 | + @GenerateNodeFactory |
| 162 | + public abstract static class ReduceNode extends PythonUnaryBuiltinNode { |
| 163 | + @Specialization(guards = "hasIterable(self)") |
| 164 | + Object reduce(PCycle self, |
| 165 | + @Cached GetClassNode getClass) { |
| 166 | + Object type = getClass.execute(self); |
| 167 | + PTuple iterableTuple = factory().createTuple(new Object[]{self.getIterable()}); |
| 168 | + PTuple tuple = factory().createTuple(new Object[]{getSavedList(self), self.isFirstpass()}); |
| 169 | + return factory().createTuple(new Object[]{type, iterableTuple, tuple}); |
| 170 | + } |
| 171 | + |
| 172 | + @Specialization(guards = "!hasIterable(self)") |
| 173 | + Object reduceNoIterable(VirtualFrame frame, PCycle self, |
| 174 | + @Cached GetClassNode getClass, |
| 175 | + @Cached PyObjectLookupAttr lookupAttrNode, |
| 176 | + @Cached CallUnaryMethodNode callNode, |
| 177 | + @Cached PyObjectGetIter getIterNode, |
| 178 | + @Cached BranchProfile indexProfile) { |
| 179 | + Object type = getClass.execute(self); |
| 180 | + PList savedList = getSavedList(self); |
| 181 | + Object it = getIterNode.execute(frame, savedList); |
| 182 | + if (self.getIndex() > 0) { |
| 183 | + indexProfile.enter(); |
| 184 | + Object setStateCallable = lookupAttrNode.execute(frame, it, __SETSTATE__); |
| 185 | + callNode.executeObject(frame, setStateCallable, self.getIndex()); |
| 186 | + } |
| 187 | + PTuple iteratorTuple = factory().createTuple(new Object[]{it}); |
| 188 | + PTuple tuple = factory().createTuple(new Object[]{savedList, true}); |
| 189 | + return factory().createTuple(new Object[]{type, iteratorTuple, tuple}); |
| 190 | + } |
| 191 | + |
| 192 | + PList getSavedList(PCycle self) { |
| 193 | + return factory().createList(toArray(self.getSaved())); |
| 194 | + } |
| 195 | + |
| 196 | + @TruffleBoundary |
| 197 | + private static Object[] toArray(List<Object> l) { |
| 198 | + return l.toArray(new Object[l.size()]); |
| 199 | + } |
| 200 | + |
| 201 | + protected boolean hasIterable(PCycle self) { |
| 202 | + return self.getIterable() != null; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Builtin(name = __SETSTATE__, minNumOfPositionalArgs = 2) |
| 207 | + @GenerateNodeFactory |
| 208 | + public abstract static class SetStateNode extends PythonBinaryBuiltinNode { |
| 209 | + abstract Object execute(VirtualFrame frame, PythonObject self, Object state); |
| 210 | + |
| 211 | + @Specialization |
| 212 | + Object setState(VirtualFrame frame, PCycle self, Object state, |
| 213 | + @Cached LenNode lenNode, |
| 214 | + @Cached GetItemNode getItemNode, |
| 215 | + @Cached IsBuiltinClassProfile isTypeErrorProfile, |
| 216 | + @Cached ToArrayNode toArrayNode, |
| 217 | + @Cached PyNumberAsSizeNode asSizeNode, |
| 218 | + @Cached BranchProfile isNotTupleProfile) { |
| 219 | + if (!((state instanceof PTuple) && ((int) lenNode.execute(frame, state) == 2))) { |
| 220 | + isNotTupleProfile.enter(); |
| 221 | + throw raise(TypeError, IS_NOT_A, "state", "2-tuple"); |
| 222 | + } |
| 223 | + Object obj = getItemNode.execute(frame, state, 0); |
| 224 | + if (!(obj instanceof PList)) { |
| 225 | + throw raise(TypeError, STATE_ARGUMENT_D_MUST_BE_A_S, 1, "Plist"); |
| 226 | + } |
| 227 | + PList saved = (PList) obj; |
| 228 | + |
| 229 | + boolean firstPass; |
| 230 | + try { |
| 231 | + firstPass = asSizeNode.executeLossy(frame, getItemNode.execute(frame, state, 1)) != 0; |
| 232 | + } catch (PException e) { |
| 233 | + e.expectTypeError(isTypeErrorProfile); |
| 234 | + throw raise(TypeError, STATE_ARGUMENT_D_MUST_BE_A_S, 2, "int"); |
| 235 | + } |
| 236 | + |
| 237 | + Object[] savedArray = toArrayNode.execute(saved.getSequenceStorage()); |
| 238 | + self.setSaved(toList(savedArray)); |
| 239 | + self.setFirstpass(firstPass); |
| 240 | + self.setIndex(0); |
| 241 | + return PNone.NONE; |
| 242 | + } |
| 243 | + |
| 244 | + @TruffleBoundary |
| 245 | + private static ArrayList<Object> toList(Object[] savedArray) { |
| 246 | + return new ArrayList<>(Arrays.asList(savedArray)); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | +} |
0 commit comments