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