1 /** 2 * Copyright (c) 2021 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_TYPESCRIPT_TYPES_INTERFACE_TYPE_H 17 #define ES2PANDA_COMPILER_TYPESCRIPT_TYPES_INTERFACE_TYPE_H 18 19 #include "objectType.h" 20 21 namespace panda::es2panda::checker { 22 23 class InterfaceType : public ObjectType { 24 public: InterfaceType(ArenaAllocator * allocator,util::StringView name,ObjectDescriptor * desc)25 InterfaceType(ArenaAllocator *allocator, util::StringView name, ObjectDescriptor *desc) 26 : ObjectType(ObjectType::ObjectTypeKind::INTERFACE, desc), 27 name_(name), 28 bases_(allocator->Adapter()), 29 allocator_(allocator) 30 { 31 } 32 AddBase(ObjectType * base)33 void AddBase(ObjectType *base) 34 { 35 bases_.push_back(base); 36 } 37 Bases()38 ArenaVector<ObjectType *> &Bases() 39 { 40 return bases_; 41 } 42 Name()43 const util::StringView &Name() const 44 { 45 return name_; 46 } 47 SetMergedTypeParams(std::pair<std::vector<binder::Variable * >,size_t> && mergedTypeParams)48 void SetMergedTypeParams(std::pair<std::vector<binder::Variable *>, size_t> &&mergedTypeParams) 49 { 50 mergedTypeParams_ = std::move(mergedTypeParams); 51 } 52 GetMergedTypeParams()53 const std::pair<std::vector<binder::Variable *>, size_t> &GetMergedTypeParams() const 54 { 55 return mergedTypeParams_; 56 } 57 SetTypeParamTypes(std::vector<Type * > && typeParamTypes)58 void SetTypeParamTypes(std::vector<Type *> &&typeParamTypes) 59 { 60 typeParamTypes_ = std::move(typeParamTypes); 61 } 62 GetTypeParamTypes()63 const std::vector<Type *> &GetTypeParamTypes() const 64 { 65 return typeParamTypes_; 66 } 67 GetProperty(const util::StringView & name,bool searchInBase)68 binder::LocalVariable *GetProperty(const util::StringView &name, [[maybe_unused]] bool searchInBase) const override 69 { 70 binder::LocalVariable *resultProp = ObjectType::GetProperty(name, false); 71 72 if (resultProp) { 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) { 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<binder::LocalVariable *> Properties() override 125 { 126 ArenaVector<binder::LocalVariable *> properties(allocator_->Adapter()); 127 CollectProperties(&properties); 128 return properties; 129 } 130 131 void ToString(std::stringstream &ss) const override; 132 TypeFacts GetTypeFacts() const override; 133 void Identical(TypeRelation *relation, Type *other) override; 134 Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; 135 136 void CollectSignatures(ArenaVector<Signature *> *collectedSignatures, bool collectCallSignatures) const; 137 void CollectProperties(ArenaVector<binder::LocalVariable *> *collectedPropeties) const; 138 const IndexInfo *FindIndexInfo(bool findNumberInfo) const; 139 IndexInfo *FindIndexInfo(bool findNumberInfo); 140 141 private: 142 util::StringView name_; 143 ArenaVector<ObjectType *> bases_; 144 ArenaAllocator *allocator_; 145 std::pair<std::vector<binder::Variable *>, size_t> mergedTypeParams_ {}; 146 std::vector<Type *> typeParamTypes_ {}; 147 148 bool IsPropertiesIdentical(TypeRelation *relation, InterfaceType *other); 149 bool IsIndexInfoIdentical(TypeRelation *relation, InterfaceType *other); 150 }; 151 152 } // namespace panda::es2panda::checker 153 154 #endif /* TYPESCRIPT_TYPES_INTERFACE_TYPE_H */ 155