1 /* 2 * Copyright (c) 2021-2025 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_IR_ETS_FUNCTION_TYPE_H 17 #define ES2PANDA_IR_ETS_FUNCTION_TYPE_H 18 19 #include "ir/typeNode.h" 20 #include "ir/base/scriptFunctionSignature.h" 21 22 namespace ark::es2panda::checker { 23 class ETSAnalyzer; 24 } // namespace ark::es2panda::checker 25 26 namespace ark::es2panda::ir { 27 class TSTypeParameterDeclaration; 28 29 class ETSFunctionType : public TypeNode { 30 public: ETSFunctionType(FunctionSignature && signature,ir::ScriptFunctionFlags const funcFlags,ArenaAllocator * const allocator)31 explicit ETSFunctionType(FunctionSignature &&signature, ir::ScriptFunctionFlags const funcFlags, 32 ArenaAllocator *const allocator) noexcept 33 : TypeNode(AstNodeType::ETS_FUNCTION_TYPE, allocator), signature_(std::move(signature)), funcFlags_(funcFlags) 34 { 35 } 36 37 ETSFunctionType() = delete; 38 ~ETSFunctionType() override = default; 39 NO_COPY_SEMANTIC(ETSFunctionType); 40 NO_MOVE_SEMANTIC(ETSFunctionType); 41 IsScopeBearer()42 [[nodiscard]] bool IsScopeBearer() const noexcept override 43 { 44 return true; 45 } 46 Scope()47 [[nodiscard]] varbinder::Scope *Scope() const noexcept override 48 { 49 return scope_; 50 } 51 SetScope(varbinder::Scope * scope)52 void SetScope(varbinder::Scope *scope) 53 { 54 scope_ = scope; 55 } 56 ClearScope()57 void ClearScope() noexcept override 58 { 59 scope_ = nullptr; 60 } 61 TypeParams()62 const TSTypeParameterDeclaration *TypeParams() const 63 { 64 return signature_.TypeParams(); 65 } 66 TypeParams()67 TSTypeParameterDeclaration *TypeParams() 68 { 69 return signature_.TypeParams(); 70 } 71 Params()72 const ArenaVector<ir::Expression *> &Params() const 73 { 74 return signature_.Params(); 75 } 76 ReturnType()77 const TypeNode *ReturnType() const 78 { 79 return signature_.ReturnType(); 80 } 81 ReturnType()82 TypeNode *ReturnType() 83 { 84 return signature_.ReturnType(); 85 } 86 FunctionalInterface()87 ir::TSInterfaceDeclaration *FunctionalInterface() 88 { 89 return functionalInterface_; 90 } 91 FunctionalInterface()92 const ir::TSInterfaceDeclaration *FunctionalInterface() const 93 { 94 return functionalInterface_; 95 } 96 SetFunctionalInterface(ir::TSInterfaceDeclaration * functionalInterface)97 void SetFunctionalInterface(ir::TSInterfaceDeclaration *functionalInterface) 98 { 99 functionalInterface_ = functionalInterface; 100 } 101 Flags()102 ir::ScriptFunctionFlags Flags() 103 { 104 return funcFlags_; 105 } 106 IsThrowing()107 bool IsThrowing() const 108 { 109 return (funcFlags_ & ir::ScriptFunctionFlags::THROWS) != 0; 110 } 111 IsRethrowing()112 bool IsRethrowing() const 113 { 114 return (funcFlags_ & ir::ScriptFunctionFlags::RETHROWS) != 0; 115 } 116 IsExtensionFunction()117 bool IsExtensionFunction() const 118 { 119 return signature_.HasReceiver(); 120 } 121 122 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 123 void Iterate(const NodeTraverser &cb) const override; 124 void Dump(ir::AstDumper *dumper) const override; 125 void Dump(ir::SrcDumper *dumper) const override; 126 void Compile(compiler::PandaGen *pg) const override; 127 void Compile(compiler::ETSGen *etsg) const override; 128 checker::Type *Check(checker::TSChecker *checker) override; 129 checker::Type *GetType([[maybe_unused]] checker::TSChecker *checker) override; 130 checker::VerifiedType Check(checker::ETSChecker *checker) override; 131 checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; 132 Accept(ASTVisitorT * v)133 void Accept(ASTVisitorT *v) override 134 { 135 v->Accept(this); 136 } 137 138 [[nodiscard]] ETSFunctionType *Clone(ArenaAllocator *allocator, AstNode *parent) override; 139 140 private: 141 varbinder::Scope *scope_ {}; 142 FunctionSignature signature_; 143 ir::TSInterfaceDeclaration *functionalInterface_ {}; 144 ir::ScriptFunctionFlags funcFlags_; 145 }; 146 } // namespace ark::es2panda::ir 147 148 #endif 149