• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "intType.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 IntType::Identical(TypeRelation *relation, Type *other)
23 {
24     if (other->IsIntType()) {
25         relation->Result(true);
26     }
27 }
28 
AssignmentTarget(TypeRelation * relation,Type * source)29 void IntType::AssignmentTarget(TypeRelation *relation, [[maybe_unused]] Type *source)
30 {
31     if (relation->ApplyUnboxing() && !relation->IsTrue()) {
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 IntType::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 IntType::Cast(TypeRelation *const relation, Type *const target)
54 {
55     if (target->HasTypeFlag(TypeFlag::INT)) {
56         conversion::Identity(relation, this, target);
57         return;
58     }
59 
60     if (target->IsETSEnumType()) {
61         relation->Result(true);
62         return;
63     }
64 
65     if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR)) {
66         conversion::NarrowingPrimitive(relation, this, target);
67         return;
68     }
69 
70     if (target->HasTypeFlag(TypeFlag::LONG | TypeFlag::FLOAT | TypeFlag::DOUBLE)) {
71         conversion::WideningPrimitive(relation, this, target);
72         return;
73     }
74 
75     if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) {
76         if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_INT)) {
77             conversion::Boxing(relation, this);
78             return;
79         }
80 
81         if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) {
82             auto unboxedTarget = relation->GetChecker()->AsETSChecker()->MaybeUnboxInRelation(target);
83             if (unboxedTarget == nullptr) {
84                 conversion::Forbidden(relation);
85                 return;
86             }
87             Cast(relation, unboxedTarget);
88             if (relation->IsTrue()) {
89                 conversion::Boxing(relation, unboxedTarget);
90                 return;
91             }
92             conversion::Forbidden(relation);
93             return;
94         }
95 
96         conversion::BoxingWideningReference(relation, this, target->AsETSObjectType());
97         return;
98     }
99 
100     conversion::Forbidden(relation);
101 }
102 
Instantiate(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)103 Type *IntType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation,
104                            [[maybe_unused]] GlobalTypesHolder *globalTypes)
105 {
106     return this;
107 }
108 }  // namespace ark::es2panda::checker
109