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