1 /*
2  * Copyright (c) 2021 - 2023 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 #ifndef ES2PANDA_COMPILER_CHECKER_ETS_BOXING_CONVERTER_H
17 #define ES2PANDA_COMPILER_CHECKER_ETS_BOXING_CONVERTER_H
18 
19 #include "checker/ets/typeConverter.h"
20 #include "checker/types/ets/etsObjectType.h"
21 
22 namespace ark::es2panda::checker {
23 class BoxingConverter : public TypeConverter {
24 public:
BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source)25     BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source)
26         : TypeConverter(checker, relation, nullptr, source)
27     {
28         if (!source->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) {
29             Relation()->Result(false);
30             return;
31         }
32 
33         SetResult(ETSTypeFromSource(checker, source));
34         Relation()->Result(true);
35     }
36 
BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source, Type *target)37     BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source, Type *target)
38         : TypeConverter(checker, relation, target, source)
39     {
40         if (!target->IsETSObjectType() || relation->IsTrue()) {
41             return;
42         }
43 
44         if (!source->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) {
45             Relation()->Result(false);
46             return;
47         }
48 
49         SetResult(ETSTypeFromSource(checker, source));
50 
51         Relation()->Result(relation->IsAssignableTo(Result(), target));
52     }
53 
54     static checker::ETSObjectType *ETSTypeFromSource(ETSChecker const *checker, Type const *source);
55 };
56 }  // namespace ark::es2panda::checker
57 
58 #endif
59