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