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