1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_COMPILER_BASE_ITERATORS_H 17 #define ES2PANDA_COMPILER_BASE_ITERATORS_H 18 19 #include <ir/irnode.h> 20 21 namespace panda::es2panda::ir { 22 class AstNode; 23 } // namespace panda::es2panda::ir 24 25 namespace panda::es2panda::compiler { 26 27 class PandaGen; 28 29 enum class IteratorType { SYNC, ASYNC }; 30 31 class Iterator { 32 public: 33 Iterator(PandaGen *pg, const ir::AstNode *node, IteratorType type); 34 DEFAULT_COPY_SEMANTIC(Iterator); 35 DEFAULT_MOVE_SEMANTIC(Iterator); 36 ~Iterator() = default; 37 Type()38 IteratorType Type() const 39 { 40 return type_; 41 } 42 Method()43 VReg Method() const 44 { 45 return method_; 46 } 47 NextResult()48 VReg NextResult() const 49 { 50 return nextResult_; 51 } 52 Node()53 const ir::AstNode *Node() const 54 { 55 return node_; 56 } 57 58 void GetMethod(util::StringView name) const; 59 void CallMethod() const; 60 void CallMethodWithValue() const; 61 62 void Next() const; 63 void Complete() const; 64 void Value() const; 65 void Close(bool abruptCompletion) const; 66 67 protected: 68 PandaGen *pg_; 69 const ir::AstNode *node_; 70 VReg closed_; 71 // These 3 regs must be allocated continuously 72 VReg method_; 73 VReg iterator_; 74 VReg nextResult_; 75 IteratorType type_; 76 }; 77 78 class DestructuringIterator : public Iterator { 79 public: 80 explicit DestructuringIterator(PandaGen *pg, const ir::AstNode *node); 81 82 DEFAULT_COPY_SEMANTIC(DestructuringIterator); 83 DEFAULT_MOVE_SEMANTIC(DestructuringIterator); 84 ~DestructuringIterator() = default; 85 Done()86 VReg Done() const 87 { 88 return done_; 89 } 90 Result()91 VReg Result() const 92 { 93 return result_; 94 } 95 96 void Step(Label *doneTarget = nullptr) const; 97 98 virtual void OnIterDone([[maybe_unused]] Label *doneTarget) const; 99 100 friend class DestructuringRestIterator; 101 102 protected: 103 VReg done_; 104 VReg result_; 105 }; 106 107 class DestructuringRestIterator : public DestructuringIterator { 108 public: DestructuringRestIterator(const DestructuringIterator & iterator)109 explicit DestructuringRestIterator(const DestructuringIterator &iterator) : DestructuringIterator(iterator) {} 110 111 DEFAULT_COPY_SEMANTIC(DestructuringRestIterator); 112 DEFAULT_MOVE_SEMANTIC(DestructuringRestIterator); 113 ~DestructuringRestIterator() = default; 114 115 void OnIterDone([[maybe_unused]] Label *doneTarget) const override; 116 }; 117 118 } // namespace panda::es2panda::compiler 119 120 #endif 121