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 "arrayType.h"
17
18 #include <typescript/types/objectType.h>
19
20 namespace panda::es2panda::checker {
21
ToString(std::stringstream & ss) const22 void ArrayType::ToString(std::stringstream &ss) const
23 {
24 bool elemIsUnion = (element_->TypeFlags() == TypeFlag::UNION);
25 if (elemIsUnion) {
26 ss << "(";
27 }
28 ElementType()->ToString(ss);
29 if (elemIsUnion) {
30 ss << ")";
31 }
32 ss << "[]";
33 }
34
Identical(TypeRelation * relation,Type * other)35 void ArrayType::Identical(TypeRelation *relation, Type *other)
36 {
37 if (other->IsArrayType()) {
38 relation->IsIdenticalTo(element_, other->AsArrayType()->ElementType());
39 }
40 }
41
AssignmentTarget(TypeRelation * relation,Type * source)42 void ArrayType::AssignmentTarget(TypeRelation *relation, Type *source)
43 {
44 if (source->IsArrayType()) {
45 relation->IsAssignableTo(source->AsArrayType()->ElementType(), element_);
46 } else if (source->IsObjectType() && source->AsObjectType()->IsTupleType()) {
47 ObjectType *sourceObj = source->AsObjectType();
48 for (auto *it : sourceObj->Properties()) {
49 if (!relation->IsAssignableTo(it->TsType(), element_)) {
50 return;
51 }
52 }
53 relation->Result(true);
54 }
55 }
56
GetTypeFacts() const57 TypeFacts ArrayType::GetTypeFacts() const
58 {
59 return TypeFacts::OBJECT_FACTS;
60 }
61
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)62 Type *ArrayType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes)
63 {
64 return allocator->New<ArrayType>(element_->Instantiate(allocator, relation, globalTypes));
65 }
66
67 } // namespace panda::es2panda::checker
68