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_NARROWING_CONVERTER_H 17 #define ES2PANDA_COMPILER_CHECKER_ETS_NARROWING_CONVERTER_H 18 19 #include "checker/ets/typeConverter.h" 20 #include "checker/ETSchecker.h" 21 #include "util/helpers.h" 22 23 namespace panda::es2panda::checker { 24 class NarrowingConverter : public TypeConverter { 25 public: NarrowingConverter(ETSChecker * checker,TypeRelation * relation,Type * target,Type * source)26 explicit NarrowingConverter(ETSChecker *checker, TypeRelation *relation, Type *target, Type *source) 27 : TypeConverter(checker, relation, target, source) 28 { 29 if (!relation->ApplyNarrowing()) { 30 return; 31 } 32 33 ASSERT(relation->GetNode()); 34 35 switch (ETSChecker::ETSChecker::ETSType(target)) { 36 case TypeFlag::BYTE: { 37 ApplyNarrowing<ByteType>(TypeFlag::NARROWABLE_TO_BYTE); 38 break; 39 } 40 case TypeFlag::CHAR: { 41 ApplyNarrowing<CharType>(TypeFlag::NARROWABLE_TO_CHAR); 42 break; 43 } 44 case TypeFlag::SHORT: { 45 ApplyNarrowing<ShortType>(TypeFlag::NARROWABLE_TO_SHORT); 46 break; 47 } 48 case TypeFlag::INT: { 49 ApplyNarrowing<IntType>(TypeFlag::NARROWABLE_TO_INT); 50 break; 51 } 52 case TypeFlag::LONG: { 53 ApplyNarrowing<LongType>(TypeFlag::NARROWABLE_TO_LONG); 54 break; 55 } 56 case TypeFlag::FLOAT: { 57 ApplyNarrowing<FloatType>(TypeFlag::NARROWABLE_TO_FLOAT); 58 break; 59 } 60 61 default: { 62 break; 63 } 64 } 65 } 66 67 private: 68 template <typename TargetType> ApplyNarrowing(TypeFlag flag)69 void ApplyNarrowing(TypeFlag flag) 70 { 71 if (!Source()->HasTypeFlag(flag)) { 72 return; 73 } 74 75 switch (ETSChecker::ETSChecker::ETSType(Source())) { 76 case TypeFlag::CHAR: { 77 ApplyNarrowing<TargetType, CharType>(); 78 break; 79 } 80 case TypeFlag::SHORT: { 81 ApplyNarrowing<TargetType, ShortType>(); 82 break; 83 } 84 case TypeFlag::INT: { 85 ApplyNarrowing<TargetType, IntType>(); 86 break; 87 } 88 case TypeFlag::LONG: { 89 ApplyNarrowing<TargetType, LongType>(); 90 break; 91 } 92 case TypeFlag::FLOAT: { 93 ApplyNarrowing<TargetType, FloatType>(); 94 break; 95 } 96 case TypeFlag::DOUBLE: { 97 ApplyNarrowing<TargetType, DoubleType>(); 98 break; 99 } 100 default: { 101 break; 102 } 103 } 104 } 105 106 template <typename TargetType, typename SourceType> ApplyNarrowing()107 void ApplyNarrowing() 108 { 109 using SType = typename SourceType::UType; 110 using TType = typename TargetType::UType; 111 112 if (Source()->HasTypeFlag(TypeFlag::CONSTANT)) { 113 SType value = reinterpret_cast<SourceType *>(Source())->GetValue(); 114 if (Relation()->InCastingContext() || util::Helpers::IsTargetFitInSourceRange<TType, SType>(value)) { 115 Relation()->GetNode()->SetTsType(Checker()->Allocator()->New<TargetType>(static_cast<TType>(value))); 116 Relation()->Result(true); 117 return; 118 } 119 120 Relation()->Result(RelationResult::ERROR); 121 return; 122 } 123 124 Relation()->Result(true); 125 } 126 }; 127 } // namespace panda::es2panda::checker 128 129 #endif 130