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_COMPILER_CHECKER_TYPES_ETS_FUNCTION_TYPE_H 17 #define ES2PANDA_COMPILER_CHECKER_TYPES_ETS_FUNCTION_TYPE_H 18 19 #include "checker/types/type.h" 20 #include "checker/types/signature.h" 21 #include "ir/base/scriptFunction.h" 22 23 namespace ark::es2panda::checker { 24 25 class ETSFunctionType : public Type { 26 public: 27 // create an "arrow" type 28 explicit ETSFunctionType(ETSChecker *checker, Signature *signature); 29 30 // create a "method" or "function" ETSFunctionType 31 explicit ETSFunctionType(ETSChecker *checker, util::StringView name, ArenaVector<Signature *> &&signatures); 32 CallSignatures()33 [[nodiscard]] ArenaVector<Signature *> &CallSignatures() 34 { 35 ES2PANDA_ASSERT(!IsETSArrowType()); 36 return callSignatures_; 37 } 38 CallSignatures()39 [[nodiscard]] const ArenaVector<Signature *> &CallSignatures() const 40 { 41 ES2PANDA_ASSERT(!IsETSArrowType()); 42 return callSignatures_; 43 } 44 45 // #22952: used temporarily as arrow and method types share the ETSFunctionType representation CallSignaturesOfMethodOrArrow()46 [[nodiscard]] ArenaVector<Signature *> &CallSignaturesOfMethodOrArrow() 47 { 48 return callSignatures_; 49 } 50 Name()51 [[nodiscard]] util::StringView Name() const 52 { 53 ES2PANDA_ASSERT(!IsETSArrowType()); 54 return name_; 55 } 56 57 void AddCallSignature(Signature *signature); 58 59 template <class UnaryPredicate> FindSpecificSignature(UnaryPredicate predicate)60 Signature *FindSpecificSignature(UnaryPredicate predicate) const noexcept 61 { 62 ES2PANDA_ASSERT(!IsETSArrowType()); 63 auto const it = std::find_if(callSignatures_.cbegin(), callSignatures_.cend(), predicate); 64 return it != callSignatures_.cend() ? *it : nullptr; 65 } 66 FindSignature(const ir::ScriptFunction * func)67 [[nodiscard]] Signature *FindSignature(const ir::ScriptFunction *func) const noexcept 68 { 69 return FindSpecificSignature([func](auto const *const sig) -> bool { return sig->Function() == func; }); 70 } 71 FindGetter()72 [[nodiscard]] Signature *FindGetter() const noexcept 73 { 74 return FindSpecificSignature([](auto const *const sig) -> bool { return sig->Function()->IsGetter(); }); 75 } 76 FindSetter()77 [[nodiscard]] Signature *FindSetter() const noexcept 78 { 79 return FindSpecificSignature([](auto const *const sig) -> bool { return sig->Function()->IsSetter(); }); 80 } 81 GetExtensionAccessorSigs()82 [[nodiscard]] ArenaVector<Signature *> &GetExtensionAccessorSigs() 83 { 84 ES2PANDA_ASSERT(!IsETSArrowType()); 85 return extensionAccessorSigs_; 86 } 87 GetExtensionFunctionSigs()88 [[nodiscard]] ArenaVector<Signature *> &GetExtensionFunctionSigs() 89 { 90 ES2PANDA_ASSERT(!IsETSArrowType()); 91 return extensionFunctionSigs_; 92 } 93 FirstAbstractSignature()94 [[nodiscard]] Signature *FirstAbstractSignature() const noexcept 95 { 96 return FindSpecificSignature( 97 [](auto const *const sig) -> bool { return sig->HasSignatureFlag(SignatureFlags::ABSTRACT); }); 98 } 99 ArrowSignature()100 Signature *ArrowSignature() const 101 { 102 ES2PANDA_ASSERT(IsETSArrowType()); 103 ES2PANDA_ASSERT(callSignatures_.size() == 1); 104 auto sig = callSignatures_[0]; 105 ES2PANDA_ASSERT(!sig->HasFunction()); 106 return sig; 107 } 108 109 ETSFunctionType *MethodToArrow(ETSChecker *checker); 110 ETSObjectType *ArrowToFunctionalInterface(ETSChecker *checker); 111 ETSObjectType *ArrowToFunctionalInterfaceDesiredArity(ETSChecker *checker, size_t arity); IsExtensionFunctionType()112 [[nodiscard]] bool IsExtensionFunctionType() const 113 { 114 return !extensionFunctionSigs_.empty() || !extensionAccessorSigs_.empty(); 115 } 116 IsExtensionAccessorType()117 [[nodiscard]] bool IsExtensionAccessorType() const 118 { 119 return !extensionAccessorSigs_.empty(); 120 } 121 122 void ToAssemblerType(std::stringstream &ss) const override; 123 void ToDebugInfoType(std::stringstream &ss) const override; 124 125 void ToString(std::stringstream &ss, bool precise) const override; 126 void Identical(TypeRelation *relation, Type *other) override; 127 void AssignmentTarget(TypeRelation *relation, Type *source) override; 128 bool AssignmentSource(TypeRelation *relation, Type *target) override; 129 void IsSupertypeOf(TypeRelation *relation, Type *source) override; 130 void IsSubtypeOf(TypeRelation *relation, Type *target) override; 131 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 132 ETSFunctionType *Substitute(TypeRelation *relation, const Substitution *substitution) override; 133 void CheckVarianceRecursively(TypeRelation *relation, VarianceFlag varianceFlag) override; 134 void Cast(TypeRelation *relation, Type *target) override; 135 void CastTarget(TypeRelation *relation, Type *source) override; 136 ResolveConditionExpr()137 std::tuple<bool, bool> ResolveConditionExpr() const override 138 { 139 return {false, false}; 140 } 141 SetHelperSignature(Signature * signature)142 void SetHelperSignature(Signature *signature) noexcept 143 { 144 helperSignature_ = signature; 145 } 146 GetHelperSignature()147 const Signature *GetHelperSignature() const 148 { 149 return helperSignature_; 150 } 151 GetHelperSignature()152 Signature *GetHelperSignature() 153 { 154 return helperSignature_; 155 } 156 HasHelperSignature()157 bool HasHelperSignature() 158 { 159 return helperSignature_ != nullptr; 160 } 161 162 private: 163 ArenaVector<Signature *> callSignatures_; 164 ArenaVector<Signature *> extensionFunctionSigs_; 165 ArenaVector<Signature *> extensionAccessorSigs_; 166 util::StringView const name_; 167 util::StringView const assemblerName_; 168 ETSFunctionType *invokeToArrowSignature_ {}; 169 ETSObjectType *arrowToFuncInterface_ {}; 170 Signature *helperSignature_ {}; 171 }; 172 } // namespace ark::es2panda::checker 173 174 #endif /* TYPESCRIPT_TYPES_FUNCTION_TYPE_H */ 175