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 21namespace panda::es2panda::ir { 22class AstNode; 23} // namespace panda::es2panda::ir 24 25namespace panda::es2panda::compiler { 26 27class PandaGen; 28 29enum class IteratorType { SYNC, ASYNC }; 30 31class Iterator { 32public: 33 Iterator(PandaGen *pg, const ir::AstNode *node, IteratorType type); 34 DEFAULT_COPY_SEMANTIC(Iterator); 35 DEFAULT_MOVE_SEMANTIC(Iterator); 36 ~Iterator() = default; 37 38 IteratorType Type() const 39 { 40 return type_; 41 } 42 43 VReg Method() const 44 { 45 return method_; 46 } 47 48 VReg NextResult() const 49 { 50 return nextResult_; 51 } 52 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 67protected: 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 78class DestructuringIterator : public Iterator { 79public: 80 explicit DestructuringIterator(PandaGen *pg, const ir::AstNode *node); 81 82 DEFAULT_COPY_SEMANTIC(DestructuringIterator); 83 DEFAULT_MOVE_SEMANTIC(DestructuringIterator); 84 ~DestructuringIterator() = default; 85 86 VReg Done() const 87 { 88 return done_; 89 } 90 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 102protected: 103 VReg done_; 104 VReg result_; 105 106private: 107 void JumpIfDone(Label *noClose) const; 108}; 109 110class DestructuringRestIterator : public DestructuringIterator { 111public: 112 explicit DestructuringRestIterator(const DestructuringIterator &iterator) : DestructuringIterator(iterator) {} 113 114 DEFAULT_COPY_SEMANTIC(DestructuringRestIterator); 115 DEFAULT_MOVE_SEMANTIC(DestructuringRestIterator); 116 ~DestructuringRestIterator() = default; 117 118 void OnIterDone([[maybe_unused]] Label *doneTarget) const override; 119}; 120 121} // namespace panda::es2panda::compiler 122 123#endif 124