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 "etsEnumType.h"
17
18 #include "checker/ets/conversion.h"
19 #include "checker/types/ets/etsUnionType.h"
20
21 namespace ark::es2panda::checker {
22
AssignmentSource(TypeRelation * relation,Type * target)23 bool ETSStringEnumType::AssignmentSource(TypeRelation *relation, Type *target)
24 {
25 bool result = false;
26 if (target->IsETSObjectType() && target->AsETSObjectType()->IsGlobalETSObjectType()) {
27 result = true;
28 } else if (target->IsETSStringType()) {
29 result = true;
30 if (relation->GetNode() != nullptr) {
31 relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF);
32 }
33 } else if (target->IsETSUnionType()) {
34 auto &unionConstituentTypes = target->AsETSUnionType()->ConstituentTypes();
35 for (auto *constituentType : unionConstituentTypes) {
36 if (relation->IsIdenticalTo(this, constituentType)) {
37 result = true;
38 break;
39 }
40 }
41 }
42 relation->Result(result);
43 return relation->IsTrue();
44 }
45
AssignmentTarget(TypeRelation * relation,Type * source)46 void ETSStringEnumType::AssignmentTarget(TypeRelation *relation, Type *source)
47 {
48 relation->IsIdenticalTo(this, source) ? relation->Result(true) : relation->Result(false);
49 }
50
Cast(TypeRelation * const relation,Type * const target)51 void ETSStringEnumType::Cast(TypeRelation *const relation, Type *const target)
52 {
53 if (relation->IsIdenticalTo(this, target)) {
54 relation->Result(true);
55 return;
56 }
57 if (target->IsETSStringType()) {
58 relation->Result(true);
59 return;
60 }
61 conversion::Forbidden(relation);
62 }
63
CastTarget(TypeRelation * relation,Type * source)64 void ETSStringEnumType::CastTarget(TypeRelation *relation, Type *source)
65 {
66 if (source->IsETSStringType()) {
67 relation->Result(true);
68 return;
69 }
70 conversion::Forbidden(relation);
71 }
72
AssignmentSource(TypeRelation * relation,Type * target)73 bool ETSIntEnumType::AssignmentSource(TypeRelation *relation, Type *target)
74 {
75 bool result = false;
76 if (target->IsETSObjectType()) {
77 if (target->AsETSObjectType()->IsGlobalETSObjectType() ||
78 target->AsETSObjectType()->Name() == compiler::Signatures::NUMERIC) {
79 result = true;
80 } else if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_NUMERIC)) {
81 result = true;
82 relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF);
83 }
84 } else if (target->HasTypeFlag(TypeFlag::ETS_NUMERIC)) {
85 result = true;
86 relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF);
87 } else if (target->IsETSUnionType()) {
88 auto &unionConstituentTypes = target->AsETSUnionType()->ConstituentTypes();
89 for (auto *constituentType : unionConstituentTypes) {
90 if (relation->IsIdenticalTo(this, constituentType)) {
91 result = true;
92 break;
93 }
94 }
95 }
96 relation->Result(result);
97 return relation->IsTrue();
98 }
99
AssignmentTarget(TypeRelation * relation,Type * source)100 void ETSIntEnumType::AssignmentTarget(TypeRelation *relation, Type *source)
101 {
102 relation->IsIdenticalTo(this, source) ? relation->Result(true) : relation->Result(false);
103 }
104
Cast(TypeRelation * const relation,Type * const target)105 void ETSIntEnumType::Cast(TypeRelation *const relation, Type *const target)
106 {
107 if (relation->IsIdenticalTo(this, target)) {
108 relation->Result(true);
109 return;
110 }
111 if (target->HasTypeFlag(TypeFlag::ETS_NUMERIC) ||
112 (target->IsETSObjectType() && target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_NUMERIC))) {
113 relation->Result(true);
114 return;
115 }
116 conversion::Forbidden(relation);
117 }
118
CastTarget(TypeRelation * relation,Type * source)119 void ETSIntEnumType::CastTarget(TypeRelation *relation, Type *source)
120 {
121 if (source->IsIntType()) {
122 relation->Result(true);
123 return;
124 }
125 if (source->IsETSObjectType() && source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_NUMERIC)) {
126 relation->Result(true);
127 return;
128 }
129 conversion::Forbidden(relation);
130 }
131
132 } // namespace ark::es2panda::checker