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