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 "etsBooleanType.h"
17
18 #include "checker/ETSchecker.h"
19 #include "checker/ets/conversion.h"
20
21 namespace ark::es2panda::checker {
Identical(TypeRelation * relation,Type * other)22 void ETSBooleanType::Identical(TypeRelation *relation, Type *other)
23 {
24 if (other->IsETSBooleanType()) {
25 relation->Result(true);
26 }
27 }
28
AssignmentTarget(TypeRelation * relation,Type * source)29 void ETSBooleanType::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source)
30 {
31 if (relation->ApplyUnboxing() && !relation->IsTrue()) {
32 relation->GetChecker()->AsETSChecker()->MaybeAddUnboxingFlagInRelation(relation, source, this);
33 }
34 }
35
AssignmentSource(TypeRelation * relation,Type * target)36 bool ETSBooleanType::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target)
37 {
38 if (relation->ApplyBoxing() && target->IsETSObjectType()) {
39 relation->GetChecker()->AsETSChecker()->CheckBoxedSourceTypeAssignable(relation, this, target);
40 }
41
42 return relation->IsTrue();
43 }
44
Cast(TypeRelation * const relation,Type * const target)45 void ETSBooleanType::Cast(TypeRelation *const relation, Type *const target)
46 {
47 if (target->HasTypeFlag(TypeFlag::ETS_BOOLEAN)) {
48 conversion::Identity(relation, this, target);
49 return;
50 }
51
52 if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) {
53 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_BOOLEAN)) {
54 conversion::Boxing(relation, this);
55 return;
56 }
57
58 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) {
59 conversion::Forbidden(relation);
60 return;
61 }
62
63 conversion::BoxingWideningReference(relation, this, target->AsETSObjectType());
64 return;
65 }
66
67 conversion::Forbidden(relation);
68 }
69
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)70 Type *ETSBooleanType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
71 [[maybe_unused]] GlobalTypesHolder *globalTypes)
72 {
73 return this;
74 }
75 } // namespace ark::es2panda::checker
76