1 /*
2 * Copyright (c) 2021 - 2025 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 "etsStringType.h"
17
18 #include "checker/ETSchecker.h"
19 #include "varbinder/ETSBinder.h"
20
21 namespace ark::es2panda::checker {
AreStringTypesAssignable(Type * source,Type * target)22 bool AreStringTypesAssignable(Type *source, Type *target)
23 {
24 if (target->IsConstantType()) {
25 return source->IsConstantType() &&
26 source->AsETSStringType()->GetValue() == target->AsETSStringType()->GetValue();
27 }
28 return true;
29 }
30
Identical(TypeRelation * relation,Type * other)31 void ETSStringType::Identical(TypeRelation *relation, Type *other)
32 {
33 if (!other->IsETSStringType() || (IsConstantType() != other->IsConstantType())) {
34 return;
35 }
36 if (!IsConstantType() || (IsConstantType() && value_ == other->AsETSStringType()->value_)) {
37 relation->Result(true);
38 }
39 }
40
AssignmentSource(TypeRelation * relation,Type * target)41 bool ETSStringType::AssignmentSource(TypeRelation *relation, Type *target)
42 {
43 relation->Result(target->IsETSStringType() && AreStringTypesAssignable(this, target));
44 return relation->IsTrue();
45 }
46
AssignmentTarget(TypeRelation * relation,Type * source)47 void ETSStringType::AssignmentTarget(TypeRelation *relation, Type *source)
48 {
49 relation->Result(source->IsETSStringType() && AreStringTypesAssignable(source, this));
50 }
51
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)52 Type *ETSStringType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
53 [[maybe_unused]] GlobalTypesHolder *globalTypes)
54 {
55 return this;
56 }
57
IsSupertypeOf(TypeRelation * relation,Type * source)58 void ETSStringType::IsSupertypeOf(TypeRelation *relation, Type *source)
59 {
60 if (IsConstantType()) {
61 Identical(relation, source);
62 return;
63 }
64 auto *const checker = relation->GetChecker()->AsETSChecker();
65 relation->IsSupertypeOf(checker->GlobalBuiltinETSStringType(), source);
66 }
67
IsSubtypeOf(TypeRelation * relation,Type * source)68 void ETSStringType::IsSubtypeOf(TypeRelation *relation, Type *source)
69 {
70 if (source->IsConstantType()) {
71 Identical(relation, source);
72 return;
73 }
74 auto *const checker = relation->GetChecker()->AsETSChecker();
75 relation->IsSupertypeOf(source, checker->GlobalBuiltinETSStringType());
76 }
77
78 } // namespace ark::es2panda::checker
79