1 /*
2  * Copyright (c) 2021 - 2023 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_FUNCTION_FUNCTION_BUILDER_H
17 #define ES2PANDA_COMPILER_FUNCTION_FUNCTION_BUILDER_H
18 
19 #include "macros.h"
20 #include "ir/irnode.h"
21 
22 namespace ark::es2panda::ir {
23 class ScriptFunction;
24 }  // namespace ark::es2panda::ir
25 
26 namespace ark::es2panda::compiler {
27 class PandaGen;
28 class CatchTable;
29 enum class IteratorType;
30 
31 enum class ResumeMode {
32     RETURN,
33     THROW,
34     NEXT,
35 };
36 
37 class FunctionBuilder {
38 public:
39     enum class BuilderType {
40         NORMAL,
41         GENERATOR,
42         ASYNC,
43         ASYNC_GENERATOR,
44     };
45 
46     explicit FunctionBuilder(PandaGen *pg, CatchTable *catchTable);
47     virtual ~FunctionBuilder() = default;
48     NO_COPY_SEMANTIC(FunctionBuilder);
49     NO_MOVE_SEMANTIC(FunctionBuilder);
50 
Prepare([[maybe_unused]] const ir::ScriptFunction *node) const51     virtual void Prepare([[maybe_unused]] const ir::ScriptFunction *node) const {};
CleanUp([[maybe_unused]] const ir::ScriptFunction *node) const52     virtual void CleanUp([[maybe_unused]] const ir::ScriptFunction *node) const {};
53 
54     virtual void DirectReturn(const ir::AstNode *node) const;
55     virtual void ImplicitReturn(const ir::AstNode *node) const;
56 
57     virtual void Await(const ir::AstNode *node);
58     virtual void YieldStar(const ir::AstNode *node);
59 
Yield([[maybe_unused]] const ir::AstNode *node)60     virtual void Yield([[maybe_unused]] const ir::AstNode *node)
61     {
62         UNREACHABLE();
63     };
64 
65 protected:
BuilderKind() const66     virtual BuilderType BuilderKind() const
67     {
68         return BuilderType::NORMAL;
69     }
70 
71     virtual IteratorType GeneratorKind() const;
72 
73     void SuspendResumeExecution(const ir::AstNode *node, VReg completionType, VReg completionValue) const;
74     void AsyncYield(const ir::AstNode *node, VReg completionType, VReg completionValue) const;
75 
76     VReg FunctionReg(const ir::ScriptFunction *node) const;
77     void HandleCompletion(const ir::AstNode *node, VReg completionType, VReg completionValue);
78 
79     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
80     PandaGen *pg_;
81     CatchTable *catchTable_;
82     VReg funcObj_ {};
83     bool handleReturn_ {};
84     // NOLINTEND(misc-non-private-member-variables-in-classes)
85 
86 private:
87     void ResumeGenerator(const ir::AstNode *node, VReg completionType, VReg completionValue) const;
88 };
89 }  // namespace ark::es2panda::compiler
90 
91 #endif
92