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 "floatType.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 FloatType::Identical(TypeRelation *relation, Type *other)
23 {
24 if (other->IsFloatType()) {
25 relation->Result(true);
26 }
27 }
28
AssignmentTarget(TypeRelation * relation,Type * source)29 void FloatType::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 FloatType::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
46 if (relation->ApplyBoxing() && target->IsETSObjectType()) {
47 relation->GetChecker()->AsETSChecker()->CheckBoxedSourceTypeAssignable(relation, this, target);
48 }
49
50 return relation->IsTrue();
51 }
52
Cast(TypeRelation * const relation,Type * const target)53 void FloatType::Cast(TypeRelation *const relation, Type *const target)
54 {
55 if (target->HasTypeFlag(TypeFlag::FLOAT)) {
56 conversion::Identity(relation, this, target);
57 return;
58 }
59
60 if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR | TypeFlag::INT | TypeFlag::LONG)) {
61 conversion::NarrowingPrimitive(relation, this, target);
62 return;
63 }
64
65 if (target->HasTypeFlag(TypeFlag::DOUBLE)) {
66 conversion::WideningPrimitive(relation, this, target);
67 return;
68 }
69
70 if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) {
71 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_FLOAT)) {
72 conversion::Boxing(relation, this);
73 return;
74 }
75
76 if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) {
77 auto unboxedTarget = relation->GetChecker()->AsETSChecker()->MaybeUnboxInRelation(target);
78 if (unboxedTarget == nullptr) {
79 conversion::Forbidden(relation);
80 return;
81 }
82 Cast(relation, unboxedTarget);
83 if (relation->IsTrue()) {
84 conversion::Boxing(relation, unboxedTarget);
85 return;
86 }
87 conversion::Forbidden(relation);
88 return;
89 }
90
91 conversion::BoxingWideningReference(relation, this, target->AsETSObjectType());
92 return;
93 }
94
95 conversion::Forbidden(relation);
96 }
97
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)98 Type *FloatType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
99 [[maybe_unused]] GlobalTypesHolder *globalTypes)
100 {
101 return this;
102 }
103 } // namespace ark::es2panda::checker
104