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 "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 auto node = relation->GetNode();
44 if ((relation->InAssignmentContext() || relation->ApplyStringToChar()) && IsConvertibleTo(target)) {
45 node->AddBoxingUnboxingFlags(ir::BoxingUnboxingFlags::UNBOX_TO_CHAR);
46 if (target->IsETSObjectType()) {
47 node->AddBoxingUnboxingFlags(ir::BoxingUnboxingFlags::BOX_TO_CHAR);
48 }
49 relation->Result(true);
50 } else {
51 relation->Result(target->IsETSStringType() && AreStringTypesAssignable(this, target));
52 }
53
54 return relation->IsTrue();
55 }
56
AssignmentTarget(TypeRelation * relation,Type * source)57 void ETSStringType::AssignmentTarget([[maybe_unused]] TypeRelation *relation, Type *source)
58 {
59 relation->Result(source->IsETSStringType() && AreStringTypesAssignable(source, this));
60 }
61
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)62 Type *ETSStringType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
63 [[maybe_unused]] GlobalTypesHolder *globalTypes)
64 {
65 return this;
66 }
67
IsSupertypeOf(TypeRelation * relation,Type * source)68 void ETSStringType::IsSupertypeOf(TypeRelation *relation, Type *source)
69 {
70 if (IsConstantType()) {
71 Identical(relation, source);
72 return;
73 }
74 auto *const checker = relation->GetChecker()->AsETSChecker();
75 relation->IsSupertypeOf(checker->GlobalBuiltinETSStringType(), source);
76 }
77
IsSubtypeOf(TypeRelation * relation,Type * source)78 void ETSStringType::IsSubtypeOf(TypeRelation *relation, Type *source)
79 {
80 if (source->IsConstantType()) {
81 Identical(relation, source);
82 return;
83 }
84 auto *const checker = relation->GetChecker()->AsETSChecker();
85 relation->IsSupertypeOf(source, checker->GlobalBuiltinETSStringType());
86 }
87
IsConvertibleTo(Type const * to) const88 bool ETSStringType::IsConvertibleTo(Type const *to) const
89 {
90 const bool targetIsChar =
91 to->IsCharType() ||
92 // ETSObjectType is used in arrow function expression call.
93 (to->IsETSObjectType() && to->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_CHAR));
94
95 return targetIsChar && IsConstantType() && value_.IsConvertibleToChar();
96 }
97
98 } // namespace ark::es2panda::checker
99