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