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 #include "interfaceType.h"
17
18 #include <binder/variable.h>
19 #include <typescript/types/typeParameter.h>
20 #include <typescript/checker.h>
21
22 #include <algorithm>
23 #include <utility>
24
25 namespace panda::es2panda::checker {
26
ToString(std::stringstream & ss) const27 void InterfaceType::ToString(std::stringstream &ss) const
28 {
29 ss << name_;
30
31 if (!typeParamTypes_.empty()) {
32 ss << "<";
33
34 for (auto it = typeParamTypes_.begin(); it != typeParamTypes_.end(); it++) {
35 (*it)->ToString(ss);
36
37 if (std::next(it) != typeParamTypes_.end()) {
38 ss << ", ";
39 }
40 }
41
42 ss << ">";
43 }
44 }
45
Identical(TypeRelation * relation,Type * other)46 void InterfaceType::Identical(TypeRelation *relation, Type *other)
47 {
48 if (!other->IsObjectType() || !other->AsObjectType()->IsInterfaceType()) {
49 return;
50 }
51
52 InterfaceType *otherInterface = other->AsObjectType()->AsInterfaceType();
53
54 const ArenaVector<binder::LocalVariable *> &targetProperties = Properties();
55 const ArenaVector<binder::LocalVariable *> &sourceProperties = otherInterface->Properties();
56
57 if (targetProperties.size() != sourceProperties.size()) {
58 relation->Result(false);
59 return;
60 }
61
62 for (auto *targetProp : targetProperties) {
63 bool foundProp =
64 std::any_of(sourceProperties.begin(), sourceProperties.end(),
65 [targetProp, relation](binder::LocalVariable *sourceProp) {
66 if (targetProp->Name() == sourceProp->Name()) {
67 Type *targetType = relation->GetChecker()->GetTypeOfVariable(targetProp);
68 Type *sourceType = relation->GetChecker()->GetTypeOfVariable(sourceProp);
69 return relation->IsIdenticalTo(targetType, sourceType);
70 }
71
72 return false;
73 });
74 if (!foundProp) {
75 relation->Result(false);
76 return;
77 }
78 }
79
80 const ArenaVector<Signature *> &targetCallSignatures = CallSignatures();
81 const ArenaVector<Signature *> &sourceCallSignatures = otherInterface->CallSignatures();
82
83 if (targetCallSignatures.size() != sourceCallSignatures.size()) {
84 relation->Result(false);
85 return;
86 }
87
88 if (!EachSignatureRelatedToSomeSignature(relation, targetCallSignatures, sourceCallSignatures) ||
89 !EachSignatureRelatedToSomeSignature(relation, sourceCallSignatures, targetCallSignatures)) {
90 return;
91 }
92
93 const ArenaVector<Signature *> &targetConstructSignatures = ConstructSignatures();
94 const ArenaVector<Signature *> &sourceConstructSignatures = otherInterface->ConstructSignatures();
95
96 if (targetConstructSignatures.size() != sourceConstructSignatures.size()) {
97 relation->Result(false);
98 return;
99 }
100
101 if (!EachSignatureRelatedToSomeSignature(relation, targetConstructSignatures, sourceConstructSignatures) ||
102 !EachSignatureRelatedToSomeSignature(relation, sourceConstructSignatures, targetConstructSignatures)) {
103 return;
104 }
105
106 IndexInfo *targetNumberInfo = NumberIndexInfo();
107 IndexInfo *sourceNumberInfo = otherInterface->NumberIndexInfo();
108
109 if ((targetNumberInfo && !sourceNumberInfo) || (!targetNumberInfo && sourceNumberInfo)) {
110 relation->Result(false);
111 return;
112 }
113
114 relation->IsIdenticalTo(targetNumberInfo, sourceNumberInfo);
115
116 if (relation->IsTrue()) {
117 IndexInfo *targetStringInfo = StringIndexInfo();
118 IndexInfo *sourceStringInfo = otherInterface->StringIndexInfo();
119
120 if ((targetStringInfo && !sourceStringInfo) || (!targetStringInfo && sourceStringInfo)) {
121 relation->Result(false);
122 return;
123 }
124
125 relation->IsIdenticalTo(targetStringInfo, sourceStringInfo);
126 }
127 }
128
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)129 Type *InterfaceType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes)
130 {
131 ObjectDescriptor *copiedDesc = allocator->New<ObjectDescriptor>(allocator);
132
133 desc_->Copy(allocator, copiedDesc, relation, globalTypes);
134
135 Type *newInterfaceType = allocator->New<InterfaceType>(allocator, name_, copiedDesc);
136
137 for (auto *it : bases_) {
138 newInterfaceType->AsObjectType()->AsInterfaceType()->AddBase(
139 it->Instantiate(allocator, relation, globalTypes)->AsObjectType());
140 }
141
142 return newInterfaceType;
143 }
144
CollectSignatures(ArenaVector<Signature * > * collectedSignatures,bool collectCallSignatures) const145 void InterfaceType::CollectSignatures(ArenaVector<Signature *> *collectedSignatures, bool collectCallSignatures) const
146 {
147 if (collectCallSignatures) {
148 for (auto *it : desc_->callSignatures) {
149 collectedSignatures->push_back(it);
150 }
151 } else {
152 for (auto *it : desc_->constructSignatures) {
153 collectedSignatures->push_back(it);
154 }
155 }
156
157 for (auto *it : bases_) {
158 it->AsInterfaceType()->CollectSignatures(collectedSignatures, collectCallSignatures);
159 }
160 }
161
CollectProperties(ArenaVector<binder::LocalVariable * > * collectedPropeties) const162 void InterfaceType::CollectProperties(ArenaVector<binder::LocalVariable *> *collectedPropeties) const
163 {
164 for (auto *currentProp : desc_->properties) {
165 bool propAlreadyCollected = false;
166 for (auto *collectedProp : *collectedPropeties) {
167 if (currentProp->Name() == collectedProp->Name()) {
168 propAlreadyCollected = true;
169 break;
170 }
171 }
172
173 if (propAlreadyCollected) {
174 continue;
175 }
176
177 collectedPropeties->push_back(currentProp);
178 }
179
180 for (auto *it : bases_) {
181 it->AsInterfaceType()->CollectProperties(collectedPropeties);
182 }
183 }
184
FindIndexInfo(bool findNumberInfo) const185 const IndexInfo *InterfaceType::FindIndexInfo(bool findNumberInfo) const
186 {
187 const IndexInfo *foundInfo = nullptr;
188
189 if (findNumberInfo && desc_->numberIndexInfo) {
190 foundInfo = desc_->numberIndexInfo;
191 } else if (!findNumberInfo && desc_->stringIndexInfo) {
192 foundInfo = desc_->stringIndexInfo;
193 }
194
195 for (auto it = bases_.begin(); it != bases_.end() && !foundInfo; it++) {
196 foundInfo = (*it)->AsInterfaceType()->FindIndexInfo(findNumberInfo);
197 }
198
199 return foundInfo;
200 }
201
FindIndexInfo(bool findNumberInfo)202 IndexInfo *InterfaceType::FindIndexInfo(bool findNumberInfo)
203 {
204 IndexInfo *foundInfo = nullptr;
205
206 if (findNumberInfo && desc_->numberIndexInfo) {
207 foundInfo = desc_->numberIndexInfo;
208 } else if (!findNumberInfo && desc_->stringIndexInfo) {
209 foundInfo = desc_->stringIndexInfo;
210 }
211
212 for (auto it = bases_.begin(); it != bases_.end() && !foundInfo; it++) {
213 foundInfo = (*it)->AsInterfaceType()->FindIndexInfo(findNumberInfo);
214 }
215
216 return foundInfo;
217 }
218
GetTypeFacts() const219 TypeFacts InterfaceType::GetTypeFacts() const
220 {
221 if (desc_->properties.empty() && desc_->callSignatures.empty() && desc_->constructSignatures.empty() &&
222 !desc_->stringIndexInfo && !desc_->numberIndexInfo) {
223 if (bases_.empty()) {
224 return TypeFacts::EMPTY_OBJECT_FACTS;
225 }
226
227 bool isEmpty = true;
228 for (auto it = bases_.begin(); isEmpty && it != bases_.end(); it++) {
229 if (!(*it)->Properties().empty() || !(*it)->CallSignatures().empty() ||
230 !(*it)->ConstructSignatures().empty() || (*it)->StringIndexInfo() || (*it)->NumberIndexInfo()) {
231 isEmpty = false;
232 }
233 }
234
235 if (isEmpty) {
236 return TypeFacts::EMPTY_OBJECT_FACTS;
237 }
238 }
239
240 return TypeFacts::OBJECT_FACTS;
241 }
242
243 } // namespace panda::es2panda::checker
244