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