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 "charType.h"
17
18 #include "checker/ets/conversion.h"
19 #include "checker/ets/narrowingWideningConverter.h"
20
21 namespace ark::es2panda::checker {
Identical(TypeRelation * relation,Type * other)22 void CharType::Identical(TypeRelation *relation, Type *other)
23 {
24 if (other->IsCharType()) {
25 relation->Result(true);
26 }
27 }
28
AssignmentTarget(TypeRelation * relation,Type * source)29 void CharType::AssignmentTarget(TypeRelation *relation, [[maybe_unused]] Type *source)
30 {
31 if (relation->ApplyUnboxing()) {
32 relation->GetChecker()->AsETSChecker()->MaybeAddUnboxingFlagInRelation(relation, source, this);
33 }
34 NarrowingWideningConverter(relation->GetChecker()->AsETSChecker(), relation, this, source);
35 }
36
AssignmentSource(TypeRelation * relation,Type * target)37 bool CharType::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
38 {
39 if (relation->InAssignmentContext()) {
40 if (target->IsETSStringType()) {
41 conversion::Boxing(relation, this);
42 relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::CONVERT_TO_STRING);
43 return relation->Result(true);
44 }
45 relation->GetChecker()->AsETSChecker()->CheckUnboxedTypeWidenable(relation, target, this);
46 if (!relation->IsTrue()) {
47 return false;
48 }
49 }
50
51 if (relation->ApplyBoxing() && target->IsETSObjectType()) {
52 relation->GetChecker()->AsETSChecker()->CheckBoxedSourceTypeAssignable(relation, this, target);
53 }
54
55 return relation->IsTrue();
56 }
57
Cast(TypeRelation * const relation,Type * const target)58 void CharType::Cast(TypeRelation *const relation, Type *const target)
59 {
60 if (target->HasTypeFlag(TypeFlag::CHAR)) {
61 conversion::Identity(relation, this, target);
62 return;
63 }
64
65 if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT)) {
66 conversion::NarrowingPrimitive(relation, this, target);
67 return;
68 }
69
70 if (target->HasTypeFlag(TypeFlag::INT | TypeFlag::LONG | TypeFlag::FLOAT | TypeFlag::DOUBLE)) {
71 conversion::WideningPrimitive(relation, this, target);
72 return;
73 }
74
75 if (target->IsETSStringType()) {
76 conversion::String(relation, this);
77 return;
78 }
79
80 if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) {
81 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_CHAR)) {
82 conversion::Boxing(relation, this);
83 return;
84 }
85
86 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) {
87 auto unboxedTarget = relation->GetChecker()->AsETSChecker()->MaybeUnboxInRelation(target);
88 if (unboxedTarget == nullptr) {
89 conversion::Forbidden(relation);
90 return;
91 }
92 Cast(relation, unboxedTarget);
93 if (relation->IsTrue()) {
94 conversion::Boxing(relation, unboxedTarget);
95 return;
96 }
97 conversion::Forbidden(relation);
98 return;
99 }
100
101 conversion::BoxingWideningReference(relation, this, target->AsETSObjectType());
102 return;
103 }
104
105 conversion::Forbidden(relation);
106 }
107
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)108 Type *CharType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
109 [[maybe_unused]] GlobalTypesHolder *globalTypes)
110 {
111 return this;
112 }
113 } // namespace ark::es2panda::checker
114