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_TS_INTERFACE_TYPE_H 17 #define ES2PANDA_COMPILER_CHECKER_TYPES_TS_INTERFACE_TYPE_H 18 19 #include "objectType.h" 20 21 namespace ark::es2panda::checker { 22 class InterfaceType : public ObjectType { 23 public: InterfaceType(ArenaAllocator * allocator,util::StringView name,ObjectDescriptor * desc)24 InterfaceType(ArenaAllocator *allocator, util::StringView name, ObjectDescriptor *desc) 25 : ObjectType(ObjectType::ObjectTypeKind::INTERFACE, desc), 26 name_(name), 27 bases_(allocator->Adapter()), 28 allocator_(allocator) 29 { 30 } 31 AddBase(ObjectType * base)32 void AddBase(ObjectType *base) 33 { 34 bases_.push_back(base); 35 } 36 Bases()37 ArenaVector<ObjectType *> &Bases() 38 { 39 return bases_; 40 } 41 Name()42 const util::StringView &Name() const 43 { 44 return name_; 45 } 46 SetMergedTypeParams(std::pair<std::vector<varbinder::Variable * >,size_t> && mergedTypeParams)47 void SetMergedTypeParams(std::pair<std::vector<varbinder::Variable *>, size_t> &&mergedTypeParams) 48 { 49 mergedTypeParams_ = std::move(mergedTypeParams); 50 } 51 GetMergedTypeParams()52 const std::pair<std::vector<varbinder::Variable *>, size_t> &GetMergedTypeParams() const 53 { 54 return mergedTypeParams_; 55 } 56 SetTypeParamTypes(std::vector<Type * > && typeParamTypes)57 void SetTypeParamTypes(std::vector<Type *> &&typeParamTypes) 58 { 59 typeParamTypes_ = std::move(typeParamTypes); 60 } 61 GetTypeParamTypes()62 const std::vector<Type *> &GetTypeParamTypes() const 63 { 64 return typeParamTypes_; 65 } 66 GetProperty(const util::StringView & name,bool searchInBase)67 varbinder::LocalVariable *GetProperty(const util::StringView &name, 68 [[maybe_unused]] bool searchInBase) const override 69 { 70 varbinder::LocalVariable *resultProp = ObjectType::GetProperty(name, false); 71 72 if (resultProp != nullptr) { 73 return resultProp; 74 } 75 76 if (!searchInBase) { 77 return nullptr; 78 } 79 80 for (auto *base : bases_) { 81 resultProp = base->GetProperty(name, true); 82 if (resultProp != nullptr) { 83 return resultProp; 84 } 85 } 86 87 return nullptr; 88 } 89 CallSignatures()90 ArenaVector<Signature *> CallSignatures() override 91 { 92 ArenaVector<Signature *> signatures(allocator_->Adapter()); 93 CollectSignatures(&signatures, true); 94 return signatures; 95 } 96 ConstructSignatures()97 ArenaVector<Signature *> ConstructSignatures() override 98 { 99 ArenaVector<Signature *> signatures(allocator_->Adapter()); 100 CollectSignatures(&signatures, false); 101 return signatures; 102 } 103 StringIndexInfo()104 const IndexInfo *StringIndexInfo() const override 105 { 106 return FindIndexInfo(false); 107 } 108 NumberIndexInfo()109 const IndexInfo *NumberIndexInfo() const override 110 { 111 return FindIndexInfo(true); 112 } 113 StringIndexInfo()114 IndexInfo *StringIndexInfo() override 115 { 116 return FindIndexInfo(false); 117 } 118 NumberIndexInfo()119 IndexInfo *NumberIndexInfo() override 120 { 121 return FindIndexInfo(true); 122 } 123 Properties()124 ArenaVector<varbinder::LocalVariable *> Properties() override 125 { 126 ArenaVector<varbinder::LocalVariable *> properties(allocator_->Adapter()); 127 CollectProperties(&properties); 128 return properties; 129 } 130 131 void ToString(std::stringstream &ss, [[maybe_unused]] bool precise) const override; 132 TypeFacts GetTypeFacts() const override; 133 void CheckStringInfo(TypeRelation *relation, InterfaceType *otherInterface); 134 bool CheckVarType(TypeRelation *relation, const ArenaVector<varbinder::LocalVariable *> &targetProperties, 135 const ArenaVector<varbinder::LocalVariable *> &sourceProperties); 136 void Identical(TypeRelation *relation, Type *other) override; 137 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 138 139 void CollectSignatures(ArenaVector<Signature *> *collectedSignatures, bool collectCallSignatures) const; 140 void CollectProperties(ArenaVector<varbinder::LocalVariable *> *collectedProperties) const; 141 const IndexInfo *FindIndexInfo(bool findNumberInfo) const; 142 IndexInfo *FindIndexInfo(bool findNumberInfo); 143 144 private: 145 util::StringView name_; 146 ArenaVector<ObjectType *> bases_; 147 ArenaAllocator *allocator_; 148 std::pair<std::vector<varbinder::Variable *>, size_t> mergedTypeParams_ {}; 149 std::vector<Type *> typeParamTypes_ {}; 150 }; 151 } // namespace ark::es2panda::checker 152 153 #endif /* TYPESCRIPT_TYPES_INTERFACE_TYPE_H */ 154