/* * This file is part of the DXX-Rebirth project . * It is copyright by its individual contributors, as recorded in the * project's Git history. See COPYING.txt at the top level for license * terms and a link to the Git history. */ #pragma once #include #include #include #include #include #include #include #include "fwd-valptridx.h" #include "pack.h" #include "compiler-poison.h" #ifdef DXX_CONSTANT_TRUE #ifdef DXX_HAVE_ATTRIBUTE_WARNING /* This causes many warnings because some conversions are not checked for * safety. Eliminating the warnings by changing the call sites to check first * would be a useful improvement. */ //#define DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT __attribute__((__warning__("call not eliminated"))) #endif #endif #define DXX_VALPTRIDX_CHECK(SUCCESS_CONDITION,ERROR,...) \ static_cast( \ static_cast(SUCCESS_CONDITION) || (ERROR::report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VA(__VA_ARGS__)), 0) \ ) \ #ifndef DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT #define DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT #endif template class valptridx::array_base_count_type { protected: /* Use the smallest size that will store all legal values. Currently, * no user has a maximum size that needs more than uint16_t, so reject * attempts to use that size. This protects against silently getting * the wrong result if a future change raises the maximum size above * uint16_t. */ using count_type = typename std::conditional<(array_size <= UINT8_MAX), uint8_t, typename std::conditional<(array_size <= UINT16_MAX), uint16_t, void>::type>::type; union { count_type count{}; /* * Use DXX_VALPTRIDX_FOR_EACH_PPI_TYPE to generate empty union * members based on basic_{i,v}val_member_factory * specializations. */ #define DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES(MANAGED_TYPE, DERIVED_TYPE_PREFIX, CONTEXT, PISUFFIX, IVPREFIX, MCPREFIX) \ valptridx::f ## IVPREFIX ## MCPREFIX ## PISUFFIX \ IVPREFIX ## MCPREFIX ## PISUFFIX DXX_VALPTRIDX_FOR_EACH_PPI_TYPE(DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES, managed_type,,); #undef DXX_VALPTRIDX_DEFINE_MEMBER_FACTORIES }; #if __GNUC__ >= 13 constexpr array_base_count_type() = default; // requires fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98423 (>=gcc-13) #else constexpr array_base_count_type() : count{} { } #endif public: count_type get_count() const { return count; } void set_count(const count_type c) { count = c; } }; template < typename INTEGRAL_TYPE, std::size_t array_size_value, valptridx_detail::untyped_utilities::report_error_style report_const_error_value, valptridx_detail::untyped_utilities::report_error_style report_mutable_error_value > constexpr std::integral_constant valptridx_detail::specialized_type_parameters::array_size; class valptridx_detail::untyped_utilities::report_error_undefined { public: dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(...) { } }; class valptridx_detail::untyped_utilities::report_error_trap_terse { public: /* Accept and discard any arguments, to encourage the compiler to discard * as dead any values that exist only as arguments to `report()`. */ [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(...) { __builtin_trap(); } }; class valptridx_detail::untyped_utilities::index_mismatch_trap_verbose { public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array, const unsigned long supplied_index, const void *const expected_pointer, const void *const actual_pointer) { /* Load each of the arguments into storage before executing the trap, * so that inspection of those locations in the core dump can readily * retrieve these values, even if the values would otherwise be unused * and optimized out. */ __asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array), "rm" (supplied_index), "rm" (expected_pointer), "rm" (actual_pointer)); __builtin_trap(); } }; class valptridx_detail::untyped_utilities::index_range_trap_verbose { public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array, const unsigned long supplied_index) { __asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array), "rm" (supplied_index)); __builtin_trap(); } }; class valptridx_detail::untyped_utilities::null_pointer_trap_verbose { public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS) { __asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_N_VARS); __builtin_trap(); } [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const void *const array) { __asm__ __volatile__("" :: DXX_VALPTRIDX_REPORT_STANDARD_ASM_LOAD_COMMA_R_VARS "rm" (array)); __builtin_trap(); } }; template class valptridx

::index_mismatch_exception : public std::logic_error { using std::logic_error::logic_error; public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *, index_type, const_pointer_type, const_pointer_type); }; template class valptridx

::index_range_exception : public std::out_of_range { using std::out_of_range::out_of_range; public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *, long); }; template class valptridx

::null_pointer_exception : public std::logic_error { using std::logic_error::logic_error; public: [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_N_DEFN_VARS); [[noreturn]] dxx_compiler_attribute_cold DXX_VALPTRIDX_WARN_CALL_NOT_OPTIMIZED_OUT static void report(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const array_managed_type *); }; template template void valptridx::check_index_match(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type r, index_type i, const array_managed_type &a dxx_compiler_attribute_unused) { const auto pi = &a[i]; DXX_VALPTRIDX_CHECK(pi == &r, handle_index_mismatch, &a, i, pi, &r); } template template class Compare> typename valptridx::index_type valptridx::check_index_range(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const index_type i, const array_managed_type *const a) { std::size_t si; if constexpr (std::is_enum::value) si = static_cast(i); else si = i; check_index_range_size(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS si, a); return i; } template template class Compare> typename valptridx::index_type valptridx::check_index_range_size(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const std::size_t s, const array_managed_type *const a) { DXX_VALPTRIDX_CHECK(Compare()(s, array_size), handle_index_range_error, a, s); /* This cast will truncate the value to fit in index_type, which is * normally smaller than std::size_t. However, truncation is legal * here, because (1) DXX_VALPTRIDX_CHECK would trap on an index that * was outside the array size and (2) index_type can represent any * valid size in the array. Thus, if DXX_VALPTRIDX_CHECK did not * trap[1], the truncation cannot change the value of the index. * * [1] If valptridx was built without index validation, then no trap * would be issued even for invalid data. Validation-disabled builds * are permitted to exhibit undefined behavior in cases where the * validation-enabled build would have trapped. */ return static_cast(s); } template template void valptridx::check_null_pointer_conversion(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type p) { DXX_VALPTRIDX_CHECK(p, handle_null_pointer); } template template void valptridx::check_null_pointer(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_pointer_type p, const array_managed_type &a dxx_compiler_attribute_unused) { DXX_VALPTRIDX_CHECK(p, handle_null_pointer, &a); } template template void valptridx::check_implicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const managed_type &r, const array_managed_type &a) { check_explicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, static_cast(&r) - static_cast(&a.front()), a); } template template void valptridx::check_explicit_index_range_ref(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_DEFN_VARS const_reference_type &r, std::size_t i, const array_managed_type &a) { const auto ii = check_index_range_size(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS i, &a); check_index_match(DXX_VALPTRIDX_REPORT_STANDARD_LEADER_COMMA_R_PASS_VARS r, ii, a); } template class valptridx::partial_policy::require_valid { public: static constexpr bool allow_nullptr{false}; [[nodiscard]] static constexpr std::false_type check_allowed_invalid_index(index_type) { return {}; } template [[nodiscard]] static constexpr std::optional check_nothrow_index(input_index_type entry_index) #if !defined(__clang_major__) || (__clang_major__ >= 16) /* ::type' (aka 'dcx::wallnum_t') and 'std::numeric_limits::type' (aka 'unsigned short')) ``` * A `requires` expression should be short-circuited, so since * `std::unsigned_integral` is not satisfied for * `input_index_type` of `dcx::wallnum_t`, the second expression * should never have been examined. * * gcc (all supported versions) and clang-16 get this right. When * clang-15 is no longer supported, the preprocessor guard can be * removed. */ requires( std::same_as || /* Require that the input_index_type can represent all values * that the underlying `index_type` can represent. */ (std::unsigned_integral && std::numeric_limits::max() >= std::numeric_limits::max()) ) #endif { return std::less()(static_cast(entry_index), array_size) /* This static_cast may truncate the type to a smaller size * (such as `uint32_t` -> `uint16_t`), but will not change the * value, since `entry_index` is less than array_size, and * array_size must be representable in `index_type`. */ ? std::optional(static_cast(entry_index)) : std::nullopt; } }; template class valptridx::partial_policy::allow_invalid { public: static constexpr bool allow_nullptr{true}; [[nodiscard]] static constexpr auto check_allowed_invalid_index(const index_type i) { return i == index_type{std::numeric_limits::max()}; } template [[nodiscard]] static constexpr std::optional check_nothrow_index(const input_index_type entry_index) { return check_allowed_invalid_index(entry_index) ? std::optional(static_cast(entry_index)) : require_valid::check_nothrow_index(entry_index); } }; template template