• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
83             if (resultProp) {
84                 return resultProp;
85             }
86         }
87 
88         return nullptr;
89     }
90 
CallSignatures()91     ArenaVector<Signature *> CallSignatures() override
92     {
93         ArenaVector<Signature *> signatures(allocator_->Adapter());
94         CollectSignatures(&signatures, true);
95         return signatures;
96     }
97 
ConstructSignatures()98     ArenaVector<Signature *> ConstructSignatures() override
99     {
100         ArenaVector<Signature *> signatures(allocator_->Adapter());
101         CollectSignatures(&signatures, false);
102         return signatures;
103     }
104 
StringIndexInfo()105     const IndexInfo *StringIndexInfo() const override
106     {
107         return FindIndexInfo(false);
108     }
109 
NumberIndexInfo()110     const IndexInfo *NumberIndexInfo() const override
111     {
112         return FindIndexInfo(true);
113     }
114 
StringIndexInfo()115     IndexInfo *StringIndexInfo() override
116     {
117         return FindIndexInfo(false);
118     }
119 
NumberIndexInfo()120     IndexInfo *NumberIndexInfo() override
121     {
122         return FindIndexInfo(true);
123     }
124 
Properties()125     ArenaVector<binder::LocalVariable *> Properties() override
126     {
127         ArenaVector<binder::LocalVariable *> properties(allocator_->Adapter());
128         CollectProperties(&properties);
129         return properties;
130     }
131 
132     void ToString(std::stringstream &ss) const override;
133     TypeFacts GetTypeFacts() const override;
134     void Identical(TypeRelation *relation, Type *other) override;
135     Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override;
136 
137     void CollectSignatures(ArenaVector<Signature *> *collectedSignatures, bool collectCallSignatures) const;
138     void CollectProperties(ArenaVector<binder::LocalVariable *> *collectedPropeties) const;
139     const IndexInfo *FindIndexInfo(bool findNumberInfo) const;
140     IndexInfo *FindIndexInfo(bool findNumberInfo);
141 
142 private:
143     util::StringView name_;
144     ArenaVector<ObjectType *> bases_;
145     ArenaAllocator *allocator_;
146     std::pair<std::vector<binder::Variable *>, size_t> mergedTypeParams_ {};
147     std::vector<Type *> typeParamTypes_ {};
148 };
149 
150 }  // namespace panda::es2panda::checker
151 
152 #endif /* TYPESCRIPT_TYPES_INTERFACE_TYPE_H */
153