1 /*
2  * Copyright (c) 2021 - 2024 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 ark::es2panda::ir {
22 class AstNode;
23 }  // namespace ark::es2panda::ir
24 
25 namespace ark::es2panda::compiler {
26 class PandaGen;
27 
28 enum class IteratorType { SYNC, ASYNC };
29 
30 class Iterator {
31 public:
32     Iterator(PandaGen *pg, const ir::AstNode *node, IteratorType type);
33     DEFAULT_COPY_SEMANTIC(Iterator);
34     DEFAULT_MOVE_SEMANTIC(Iterator);
35     ~Iterator() = default;
36 
Type() const37     IteratorType Type() const
38     {
39         return type_;
40     }
41 
Method() const42     VReg Method() const
43     {
44         return method_;
45     }
46 
NextResult() const47     VReg NextResult() const
48     {
49         return nextResult_;
50     }
51 
Node() const52     const ir::AstNode *Node() const
53     {
54         return node_;
55     }
56 
57     void GetMethod(util::StringView name) const;
58     void CallMethod() const;
59     void CallMethodWithValue() const;
60 
61     void Next() const;
62     void Complete() const;
63     void Value() const;
64     void CloseInnerResultNormal(bool abruptCompletion, Label *returnExits) const;
65     void Close(bool abruptCompletion) const;
66 
67 protected:
68     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
69     PandaGen *pg_;
70     const ir::AstNode *node_;
71     VReg method_;
72     VReg iterator_;
73     VReg nextResult_;
74     IteratorType type_;
75     // NOLINTEND(misc-non-private-member-variables-in-classes)
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() const86     VReg Done() const
87     {
88         return done_;
89     }
90 
Result() const91     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     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
104     VReg done_;
105     VReg result_;
106     // NOLINTEND(misc-non-private-member-variables-in-classes)
107 };
108 
109 class DestructuringRestIterator : public DestructuringIterator {
110 public:
DestructuringRestIterator(const DestructuringIterator &iterator)111     explicit DestructuringRestIterator(const DestructuringIterator &iterator) : DestructuringIterator(iterator) {}
112 
113     DEFAULT_COPY_SEMANTIC(DestructuringRestIterator);
114     DEFAULT_MOVE_SEMANTIC(DestructuringRestIterator);
115     ~DestructuringRestIterator() = default;
116 
117     void OnIterDone([[maybe_unused]] Label *doneTarget) const override;
118 };
119 }  // namespace ark::es2panda::compiler
120 
121 #endif
122