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_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 explicit ETSFunctionType(ETSChecker *checker, util::StringView name, ArenaVector<Signature *> &&signatures); 28 ETSFunctionType(util::StringView name,Signature * signature,ArenaAllocator * allocator)29 explicit ETSFunctionType(util::StringView name, Signature *signature, ArenaAllocator *allocator) 30 : Type(TypeFlag::FUNCTION), callSignatures_(allocator->Adapter()), name_(name), funcInterface_(nullptr) 31 { 32 callSignatures_.push_back(signature); 33 } ETSFunctionType(util::StringView name,ArenaAllocator * allocator)34 explicit ETSFunctionType(util::StringView name, ArenaAllocator *allocator) 35 : Type(TypeFlag::FUNCTION), callSignatures_(allocator->Adapter()), name_(name), funcInterface_(nullptr) 36 { 37 } 38 CallSignatures()39 ArenaVector<Signature *> &CallSignatures() 40 { 41 return callSignatures_; 42 } 43 CallSignatures()44 const ArenaVector<Signature *> &CallSignatures() const 45 { 46 return callSignatures_; 47 } 48 Name()49 util::StringView Name() const 50 { 51 return name_; 52 } 53 FunctionalInterface()54 Type *FunctionalInterface() const 55 { 56 return funcInterface_; 57 } 58 AddCallSignature(Signature * signature)59 void AddCallSignature(Signature *signature) 60 { 61 if (signature->Function()->IsGetter()) { 62 AddTypeFlag(TypeFlag::GETTER); 63 } else if (signature->Function()->IsSetter()) { 64 AddTypeFlag(TypeFlag::SETTER); 65 } 66 callSignatures_.push_back(signature); 67 } 68 SetReferencedSignature(Signature * refSignature)69 void SetReferencedSignature(Signature *refSignature) 70 { 71 refSignature_ = refSignature; 72 } 73 GetReferencedSignature()74 Signature *GetReferencedSignature() const 75 { 76 return refSignature_; 77 } 78 FindSignature(const ir::ScriptFunction * func)79 Signature *FindSignature(const ir::ScriptFunction *func) const 80 { 81 for (auto *it : callSignatures_) { 82 if (it->Function() == func) { 83 return it; 84 } 85 } 86 87 return nullptr; 88 } 89 FindGetter()90 Signature *FindGetter() const 91 { 92 for (auto *sig : callSignatures_) { 93 if (sig->Function()->IsGetter()) { 94 return sig; 95 } 96 } 97 return nullptr; 98 } 99 FindSetter()100 Signature *FindSetter() const 101 { 102 for (auto *sig : callSignatures_) { 103 if (sig->Function()->IsSetter()) { 104 return sig; 105 } 106 } 107 return nullptr; 108 } 109 ToAssemblerType(std::stringstream & ss)110 void ToAssemblerType([[maybe_unused]] std::stringstream &ss) const override 111 { 112 funcInterface_->ToAssemblerType(ss); 113 } 114 ToDebugInfoType(std::stringstream & ss)115 void ToDebugInfoType([[maybe_unused]] std::stringstream &ss) const override 116 { 117 UNREACHABLE(); 118 } 119 120 Signature *FirstAbstractSignature(); 121 void ToString(std::stringstream &ss, bool precise) const override; 122 void Identical(TypeRelation *relation, Type *other) override; 123 void AssignmentTarget(TypeRelation *relation, Type *source) override; 124 bool AssignmentSource(TypeRelation *relation, Type *target) override; 125 void IsSupertypeOf(TypeRelation *relation, Type *source) override; 126 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 127 ETSFunctionType *Substitute(TypeRelation *relation, const Substitution *substitution) override; 128 void Cast(TypeRelation *relation, Type *target) override; 129 checker::RelationResult CastFunctionParams(TypeRelation *relation, Signature *targetInvokeSig); 130 ETSFunctionType *BoxPrimitives(ETSChecker *checker); 131 void IsSubtypeOf(TypeRelation *relation, Type *target) override; 132 133 private: 134 ArenaVector<Signature *> callSignatures_; 135 util::StringView name_; 136 Signature *refSignature_ {}; 137 Type *const funcInterface_; 138 }; 139 } // namespace ark::es2panda::checker 140 141 #endif /* TYPESCRIPT_TYPES_FUNCTION_TYPE_H */ 142