1 /** 2 * Copyright (c) 2021-2022 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 ASSEMBLER_ASSEMBLY_LITERALS_H 17 #define ASSEMBLER_ASSEMBLY_LITERALS_H 18 19 #include <string> 20 #include <vector> 21 22 #include "libpandafile/literal_data_accessor-inl.h" 23 24 namespace panda::pandasm { 25 26 struct LiteralArray { 27 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) 28 struct Literal { 29 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) 30 panda_file::LiteralTag tag_; 31 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) 32 std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::string> value_; 33 IsBoolValueLiteralArray::Literal34 bool IsBoolValue() const 35 { 36 switch (tag_) { 37 case panda_file::LiteralTag::ARRAY_U1: 38 case panda_file::LiteralTag::BOOL: 39 return true; 40 default: 41 return false; 42 } 43 } 44 IsByteValueLiteralArray::Literal45 bool IsByteValue() const 46 { 47 switch (tag_) { 48 case panda_file::LiteralTag::ARRAY_U8: 49 case panda_file::LiteralTag::ARRAY_I8: 50 case panda_file::LiteralTag::TAGVALUE: 51 case panda_file::LiteralTag::ACCESSOR: 52 case panda_file::LiteralTag::NULLVALUE: 53 return true; 54 default: 55 return false; 56 } 57 } 58 IsShortValueLiteralArray::Literal59 bool IsShortValue() const 60 { 61 switch (tag_) { 62 case panda_file::LiteralTag::ARRAY_U16: 63 case panda_file::LiteralTag::ARRAY_I16: 64 return true; 65 default: 66 return false; 67 } 68 } 69 IsIntegerValueLiteralArray::Literal70 bool IsIntegerValue() const 71 { 72 switch (tag_) { 73 case panda_file::LiteralTag::ARRAY_U32: 74 case panda_file::LiteralTag::ARRAY_I32: 75 case panda_file::LiteralTag::INTEGER: 76 return true; 77 default: 78 return false; 79 } 80 } 81 IsLongValueLiteralArray::Literal82 bool IsLongValue() const 83 { 84 switch (tag_) { 85 case panda_file::LiteralTag::ARRAY_U64: 86 case panda_file::LiteralTag::ARRAY_I64: 87 return true; 88 default: 89 return false; 90 } 91 } 92 IsFloatValueLiteralArray::Literal93 bool IsFloatValue() const 94 { 95 switch (tag_) { 96 case panda_file::LiteralTag::ARRAY_F32: 97 case panda_file::LiteralTag::FLOAT: 98 return true; 99 default: 100 return false; 101 } 102 } 103 IsDoubleValueLiteralArray::Literal104 bool IsDoubleValue() const 105 { 106 switch (tag_) { 107 case panda_file::LiteralTag::ARRAY_F64: 108 case panda_file::LiteralTag::DOUBLE: 109 return true; 110 default: 111 return false; 112 } 113 } 114 IsStringValueLiteralArray::Literal115 bool IsStringValue() const 116 { 117 switch (tag_) { 118 case panda_file::LiteralTag::ARRAY_STRING: 119 case panda_file::LiteralTag::STRING: 120 case panda_file::LiteralTag::METHOD: 121 case panda_file::LiteralTag::GENERATORMETHOD: 122 case panda_file::LiteralTag::ASYNCGENERATORMETHOD: 123 return true; 124 default: 125 return false; 126 } 127 } 128 }; 129 130 std::vector<panda::pandasm::LiteralArray::Literal> 131 literals_; // NOLINT(misc-non-private-member-variables-in-classes) 132 LiteralArrayLiteralArray133 explicit LiteralArray(std::vector<panda::pandasm::LiteralArray::Literal> literals) : literals_(std::move(literals)) 134 { 135 } 136 explicit LiteralArray() = default; 137 GetArrayTagFromComponentTypeLiteralArray138 static constexpr panda_file::LiteralTag GetArrayTagFromComponentType(panda_file::Type::TypeId type) 139 { 140 switch (type) { 141 case panda_file::Type::TypeId::U1: 142 return panda_file::LiteralTag::ARRAY_U1; 143 case panda_file::Type::TypeId::U8: 144 return panda_file::LiteralTag::ARRAY_U8; 145 case panda_file::Type::TypeId::I8: 146 return panda_file::LiteralTag::ARRAY_I8; 147 case panda_file::Type::TypeId::U16: 148 return panda_file::LiteralTag::ARRAY_U16; 149 case panda_file::Type::TypeId::I16: 150 return panda_file::LiteralTag::ARRAY_I16; 151 case panda_file::Type::TypeId::U32: 152 return panda_file::LiteralTag::ARRAY_U32; 153 case panda_file::Type::TypeId::I32: 154 return panda_file::LiteralTag::ARRAY_I32; 155 case panda_file::Type::TypeId::U64: 156 return panda_file::LiteralTag::ARRAY_U64; 157 case panda_file::Type::TypeId::I64: 158 return panda_file::LiteralTag::ARRAY_I64; 159 case panda_file::Type::TypeId::F32: 160 return panda_file::LiteralTag::ARRAY_F32; 161 case panda_file::Type::TypeId::F64: 162 return panda_file::LiteralTag::ARRAY_F64; 163 default: 164 UNREACHABLE(); 165 } 166 } 167 }; 168 169 } // namespace panda::pandasm 170 171 #endif // ASSEMBLER_ASSEMBLY_LITERALS_H 172