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 22 namespace panda::es2panda::ir { 23 class ScriptFunction; 24 } // namespace panda::es2panda::ir 25 26 namespace panda::es2panda::compiler { 27 28 class PandaGen; 29 class CatchTable; 30 enum class IteratorType; 31 32 enum class ResumeMode { 33 RETURN, 34 THROW, 35 NEXT, 36 }; 37 38 class FunctionBuilder { 39 public: 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 Prepare(const ir::ScriptFunction * node)52 virtual void Prepare([[maybe_unused]] const ir::ScriptFunction *node) {}; CleanUp(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 Yield(const ir::AstNode * node)62 virtual void Yield([[maybe_unused]] const ir::AstNode *node) 63 { 64 UNREACHABLE(); 65 }; 66 67 protected: BuilderKind()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 86 private: 87 void resumeGenerator(const ir::AstNode *node, VReg completionType, VReg completionValue) const; 88 }; 89 90 } // namespace panda::es2panda::compiler 91 92 #endif 93