1 /** 2 * Copyright (c) 2022-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 #ifndef PANDA_RUNTIME_TYPED_VALUE_H 17 #define PANDA_RUNTIME_TYPED_VALUE_H 18 19 #include <cstdint> 20 #include <string> 21 #include <variant> 22 23 #include "libpandabase/macros.h" 24 #include "libpandafile/file_items.h" 25 #include "runtime/include/coretypes/tagged_value.h" 26 27 namespace ark { 28 class ObjectHeader; 29 30 class TypedValue { 31 using Value = std::variant<std::monostate, std::monostate, bool, int8_t, uint8_t, int16_t, uint16_t, int32_t, 32 uint32_t, float, double, int64_t, uint64_t, coretypes::TaggedValue, ObjectHeader *>; 33 34 public: 35 ~TypedValue() = default; 36 Invalid()37 static TypedValue Invalid() 38 { 39 return TypedValue {Value(std::in_place_index<0>)}; 40 } 41 Void()42 static TypedValue Void() 43 { 44 return TypedValue {Value(std::in_place_index<1>)}; 45 } 46 U1(bool value)47 static TypedValue U1(bool value) 48 { 49 return TypedValue {value}; 50 } 51 I8(int8_t value)52 static TypedValue I8(int8_t value) 53 { 54 return TypedValue {value}; 55 } 56 U8(uint8_t value)57 static TypedValue U8(uint8_t value) 58 { 59 return TypedValue {value}; 60 } 61 I16(int16_t value)62 static TypedValue I16(int16_t value) 63 { 64 return TypedValue {value}; 65 } 66 U16(uint16_t value)67 static TypedValue U16(uint16_t value) 68 { 69 return TypedValue {value}; 70 } 71 I32(int32_t value)72 static TypedValue I32(int32_t value) 73 { 74 return TypedValue {value}; 75 } 76 U32(uint32_t value)77 static TypedValue U32(uint32_t value) 78 { 79 return TypedValue {value}; 80 } 81 F32(float value)82 static TypedValue F32(float value) 83 { 84 return TypedValue {value}; 85 } 86 F64(double value)87 static TypedValue F64(double value) 88 { 89 return TypedValue {value}; 90 } 91 I64(int64_t value)92 static TypedValue I64(int64_t value) 93 { 94 return TypedValue {value}; 95 } 96 U64(uint64_t value)97 static TypedValue U64(uint64_t value) 98 { 99 return TypedValue {value}; 100 } 101 Reference(ObjectHeader * value)102 static TypedValue Reference(ObjectHeader *value) 103 { 104 return TypedValue {value}; 105 } 106 Tagged(coretypes::TaggedValue value)107 static TypedValue Tagged(coretypes::TaggedValue value) 108 { 109 return TypedValue {value}; 110 } 111 112 DEFAULT_COPY_SEMANTIC(TypedValue); 113 DEFAULT_MOVE_SEMANTIC(TypedValue); 114 GetType()115 panda_file::Type::TypeId GetType() const 116 { 117 return static_cast<panda_file::Type::TypeId>(value_.index()); 118 } 119 IsInvalid()120 bool IsInvalid() const 121 { 122 return GetType() == panda_file::Type::TypeId::INVALID; 123 } 124 IsVoid()125 bool IsVoid() const 126 { 127 return GetType() == panda_file::Type::TypeId::VOID; 128 } 129 IsU1()130 bool IsU1() const 131 { 132 return GetType() == panda_file::Type::TypeId::U1; 133 } 134 IsI8()135 bool IsI8() const 136 { 137 return GetType() == panda_file::Type::TypeId::I8; 138 } 139 IsU8()140 bool IsU8() const 141 { 142 return GetType() == panda_file::Type::TypeId::U8; 143 } 144 IsI16()145 bool IsI16() const 146 { 147 return GetType() == panda_file::Type::TypeId::I16; 148 } 149 IsU16()150 bool IsU16() const 151 { 152 return GetType() == panda_file::Type::TypeId::U16; 153 } 154 IsI32()155 bool IsI32() const 156 { 157 return GetType() == panda_file::Type::TypeId::I32; 158 } 159 IsU32()160 bool IsU32() const 161 { 162 return GetType() == panda_file::Type::TypeId::U32; 163 } 164 IsF32()165 bool IsF32() const 166 { 167 return GetType() == panda_file::Type::TypeId::F32; 168 } 169 IsF64()170 bool IsF64() const 171 { 172 return GetType() == panda_file::Type::TypeId::F64; 173 } 174 IsI64()175 bool IsI64() const 176 { 177 return GetType() == panda_file::Type::TypeId::I64; 178 } 179 IsU64()180 bool IsU64() const 181 { 182 return GetType() == panda_file::Type::TypeId::U64; 183 } 184 IsReference()185 bool IsReference() const 186 { 187 return GetType() == panda_file::Type::TypeId::REFERENCE; 188 } 189 IsTagged()190 bool IsTagged() const 191 { 192 return GetType() == panda_file::Type::TypeId::TAGGED; 193 } 194 GetAsU1()195 bool GetAsU1() const 196 { 197 ASSERT(IsU1()); 198 return std::get<bool>(value_); 199 } 200 GetAsI8()201 int8_t GetAsI8() const 202 { 203 ASSERT(IsI8()); 204 return std::get<int8_t>(value_); 205 } 206 GetAsU8()207 uint8_t GetAsU8() const 208 { 209 ASSERT(IsU8()); 210 return std::get<uint8_t>(value_); 211 } 212 GetAsI16()213 int16_t GetAsI16() const 214 { 215 ASSERT(IsI16()); 216 return std::get<int16_t>(value_); 217 } 218 GetAsU16()219 uint16_t GetAsU16() const 220 { 221 ASSERT(IsU16()); 222 return std::get<uint16_t>(value_); 223 } 224 GetAsI32()225 int32_t GetAsI32() const 226 { 227 ASSERT(IsI32()); 228 return std::get<int32_t>(value_); 229 } 230 GetAsU32()231 uint32_t GetAsU32() const 232 { 233 ASSERT(IsU32()); 234 return std::get<uint32_t>(value_); 235 } 236 GetAsF32()237 float GetAsF32() const 238 { 239 ASSERT(IsF32()); 240 return std::get<float>(value_); 241 } 242 GetAsF64()243 double GetAsF64() const 244 { 245 ASSERT(IsF64()); 246 return std::get<double>(value_); 247 } 248 GetAsI64()249 int64_t GetAsI64() const 250 { 251 ASSERT(IsI64()); 252 return std::get<int64_t>(value_); 253 } 254 GetAsU64()255 uint64_t GetAsU64() const 256 { 257 ASSERT(IsU64()); 258 return std::get<uint64_t>(value_); 259 } 260 GetAsReference()261 ObjectHeader *GetAsReference() const 262 { 263 ASSERT(IsReference()); 264 return std::get<ObjectHeader *>(value_); 265 } 266 GetAsTagged()267 coretypes::TaggedValue GetAsTagged() const 268 { 269 ASSERT(IsTagged()); 270 return std::get<coretypes::TaggedValue>(value_); 271 } 272 273 private: TypedValue(Value value)274 explicit TypedValue(Value value) : value_(value) {} 275 276 Value value_; 277 }; 278 } // namespace ark 279 280 #endif // PANDA_RUNTIME_TYPED_VALUE_H 281