chromium/third_party/dawn/src/tint/utils/reflection/reflection.h

// Copyright 2022 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef SRC_TINT_UTILS_REFLECTION_REFLECTION_H_
#define SRC_TINT_UTILS_REFLECTION_REFLECTION_H_

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

#include "src/tint/utils/macros/foreach.h"
#include "src/tint/utils/memory/aligned_storage.h"
#include "src/tint/utils/result/result.h"

/// Forward declarations
namespace tint {
class CastableBase;
}

namespace tint::reflection::detail {

/// Helper for detecting whether the type T contains a nested Reflection class.
template <typename T, typename ENABLE = void>
struct HasReflection : std::false_type {};

/// Specialization for types that have a nested Reflection class.
HasReflection<T, std::void_t<typename T::Reflection>>;

/// Helper for inferring the base class size of T.
template <typename T, typename ENABLE = void>
struct BaseClassSize {};

/// Specialization for types that contain a 'Base' type alias.
BaseClassSize<T, std::void_t<typename T::Base>>;

/// ReflectedFieldInfo describes a single reflected field. Used by CheckAllFieldsReflected()
struct ReflectedFieldInfo {};

/// @returns Success if the sequential fields of @p fields have the expected offsets, and aligned
/// sum match the class size @p class_size.
::tint::Result<SuccessType> CheckAllFieldsReflected(VectorRef<ReflectedFieldInfo> fields,
                                                    std::string_view class_name,
                                                    size_t class_size,
                                                    size_t class_align,
                                                    bool class_is_castable,
                                                    std::string_view reflect_file,
                                                    uint32_t reflect_line);

/// @returns Success if the TINT_REFLECT() reflected fields of @tparam CLASS match the declaration
/// order, do not have any gaps, and fully account for the entire size of the class.
template <typename CLASS>
::tint::Result<SuccessType> CheckAllFieldsReflected() {}

}  // namespace tint::reflection::detail

namespace tint {

/// Is true if the class T has reflected its fields with TINT_REFLECT()
HasReflection;

/// Calls @p callback with each field of @p object
/// @param object the object
/// @param callback a function that is called for each field of @p object.
/// @tparam CB a function with one of the signatures:
///         `void(auto& FIELD)`
///         `void(auto& FIELD, std::string_view NAME)`
template <typename OBJECT, typename CB>
void ForeachField(OBJECT& object, CB&& callback) {}

/// Calls @p callback with each field of @p object
/// @param object the object
/// @param callback a function that is called for each field of @p object.
/// @tparam CB a function with one of the signatures:
///         `void(const auto& FIELD)`
///         `void(const auto& FIELD, std::string_view NAME)`
template <typename OBJECT, typename CB>
void ForeachField(const OBJECT& object, CB&& callback) {}

/// Macro used by TINT_FOREACH() in TINT_REFLECT() to generate the T::Reflection::Fields tuple.
#define TINT_REFLECT_FIELD_TYPE(FIELD)

/// Macro used by TINT_FOREACH() in TINT_REFLECT() to call the callback function with each field in
/// the variadic.
#define TINT_REFLECT_CALLBACK_FIELD(FIELD)

// TINT_REFLECT(CLASS, ...) reflects each of the fields arguments of CLASS so that the types can be
// used with tint::ForeachField().
#define TINT_REFLECT(CLASS, ...)

/// TINT_ASSERT_ALL_FIELDS_REFLECTED(...) performs a compile-time assertion that all the fields of
/// CLASS have been reflected with TINT_REFLECT().
/// @note The order in which the fields are passed to TINT_REFLECT must match the declaration order
/// in the class.
#define TINT_ASSERT_ALL_FIELDS_REFLECTED(CLASS)

/// A template that can be specialized to reflect the valid range of an enum
/// Use TINT_REFLECT_ENUM_RANGE to specialize this class
template <typename T>
struct EnumRange;

/// Declares a specialization of EnumRange for the enum ENUM with the lowest enum value MIN and
/// largest enum value MAX. Must only be used in the `tint` namespace.
#define TINT_REFLECT_ENUM_RANGE(ENUM, MIN, MAX)

}  // namespace tint

#endif  // SRC_TINT_UTILS_REFLECTION_REFLECTION_H_