/** * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PANDA_RUNTIME_VALUE_H_ #define PANDA_RUNTIME_VALUE_H_ #include #include #include #include #include "libpandafile/file_items.h" #include "runtime/bridge/bridge.h" #include "runtime/include/coretypes/tagged_value.h" namespace ark { class ObjectHeader; class Value { public: explicit Value() : value_(0) {} DEFAULT_COPY_SEMANTIC(Value); DEFAULT_MOVE_SEMANTIC(Value); ~Value() = default; template explicit Value(T value) { // Disable checks due to clang-tidy bug https://bugs.llvm.org/show_bug.cgi?id=32203 // NOLINTNEXTLINE(readability-braces-around-statements, hicpp-braces-around-statements, bugprone-branch-clone) if constexpr (std::is_integral_v) { value_ = static_cast(value); // NOLINTNEXTLINE(readability-braces-around-statements, readability-misleading-indentation) } else if constexpr (std::is_same_v) { value_ = bit_cast(value); // NOLINTNEXTLINE(readability-braces-around-statements, readability-misleading-indentation) } else if constexpr (std::is_same_v) { value_ = bit_cast(value); } else if constexpr (std::is_pointer_v && std::is_base_of_v>) { value_ = static_cast(value); } else { // NOLINTNEXTLINE(readability-misleading-indentation) value_ = value; } } template T GetAs() const { static_assert(std::is_integral_v, "T must be integral type"); ASSERT(IsPrimitive()); return static_cast(std::get<0>(value_)); } int64_t GetAsLong() const; bool IsReference() const { return std::holds_alternative(value_); } bool IsPrimitive() const { return std::holds_alternative(value_); } ObjectHeader **GetGCRoot() { ASSERT(IsReference()); return &std::get(value_); } template static ALWAYS_INLINE Value FromVReg(VRegisterRef vreg) { if (vreg.HasObject()) { return Value(vreg.GetReference()); } return Value(vreg.GetValue()); } private: std::variant value_; }; } // namespace ark #endif // PANDA_RUNTIME_VALUE_H_