/** * Copyright (c) 2021-2022 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" namespace panda { class ObjectHeader; class Value { public: explicit Value() : value_(DecodedTaggedValue(0, 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 { // NOLINTNEXTLINE(readability-misleading-indentation) value_ = value; } } Value(int64_t value, int64_t tag) : value_(DecodedTaggedValue(value, tag)) {} 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; DecodedTaggedValue GetDecodedTaggedValue() const { return IsDecodedTaggedValue() ? (std::get<2>(value_)) : DecodedTaggedValue(0, 0); } bool IsReference() const { return std::holds_alternative(value_); } bool IsPrimitive() const { return std::holds_alternative(value_); } bool IsDecodedTaggedValue() const { return std::holds_alternative(value_); } ObjectHeader **GetGCRoot() { ASSERT(IsReference()); return &std::get(value_); } private: std::variant value_; }; } // namespace panda #endif // PANDA_RUNTIME_VALUE_H_