object-introspection/types/shrd_ptr_type.toml
Jake Hillion d71307cb43 oil: change std::stack reference to a std::function (#345)
Summary:

Previously on large types OIL would have problems with corrupting the `std::stack<exporter::inst::Inst>` that is passed to the processors. This change hides the implementation of the stack from the processors by wrapping the call to emplace in a `std::function` written by the non-generated code, which solves the test case I've seen for this crashing. It also allows us to easily change the stack implementation in future - I plan to change it to a `std::stack<T, std::vector<T>>` in a follow up.

Reviewed By: tyroguru

Differential Revision: D49273116
2023-09-14 16:57:45 +01:00

127 lines
3.3 KiB
TOML

[info]
type_name = "std::shared_ptr"
ctype = "SHRD_PTR_TYPE"
header = "memory"
# Old:
typeName = "std::shared_ptr"
ns = ["namespace std"]
numTemplateParams = 1
replaceTemplateParamIndex = []
[codegen]
decl = """
template<typename T>
void getSizeType(const %1%<T> &s_ptr, size_t& returnArg);
"""
func = """
template<typename T>
void getSizeType(const %1%<T> &s_ptr, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<T>));
if constexpr (!std::is_void<T>::value) {
SAVE_DATA((uintptr_t)(s_ptr.get()));
if (s_ptr && pointers.add((uintptr_t)(s_ptr.get()))) {
SAVE_DATA(1);
getSizeType(*(s_ptr.get()), returnArg);
} else {
SAVE_DATA(0);
}
}
}
"""
handler = """
template <typename DB, typename T0>
struct TypeHandler<DB, %1%<T0>> {
using type = typename std::conditional<
std::is_void<T0>::value,
types::st::Unit<DB>,
types::st::Pair<DB,
types::st::VarInt<DB>,
types::st::Sum<DB,
types::st::Unit<DB>,
typename TypeHandler<DB, T0>::type
>>>::type;
static types::st::Unit<DB> getSizeType(
const %1%<T0>& container,
typename TypeHandler<DB, %1%<T0>>::type returnArg) {
if constexpr (!std::is_void<T0>::value) {
auto r0 = returnArg.write((uintptr_t)(container.get()));
if (container && pointers.add((uintptr_t)(container.get()))) {
return r0.template delegate<1>([&container](auto ret) {
return OIInternal::getSizeType<DB>(*(container.get()), ret);
});
} else {
return r0.template delegate<0>(std::identity());
}
} else {
return returnArg;
}
}
};
"""
traversal_func = """
auto tail = returnArg.write((uintptr_t)container.get());
if constexpr (std::is_void<T0>::value) {
return tail.template delegate<0>(std::identity());
} else {
bool do_visit = container && pointers.add((uintptr_t)container.get());
if (!do_visit)
return tail.template delegate<0>(std::identity());
return tail.template delegate<1>([&container](auto ret) {
return OIInternal::getSizeType<DB>(*container, ret);
});
}
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
el.pointer = std::get<ParsedData::VarInt>(d.val).value;
"""
[[codegen.processor]]
type = """
types::st::Sum<DB,
types::st::Unit<DB>,
typename TypeHandler<DB, T0>::type>
"""
func = """
#ifdef __GLIBCXX__
// TODO: Accurately report the control block overhead
// accounting for both in-place and non-in-place
// _Sp_counted_base (_Atomic_word, _Atomic_word)
// _Sp_counted_ptr : _Sp_counted_base (_Ptr)
// _Sp_counted_ptr_inplace : _Sp_counted_base (_Tp)
// _Sp_counted_deleter : _Sp_counted_base (_Ptr)
// _Sp_counted_array : _Sp_counted_base (_Ptr)
#elif _LIBCPP_VERSION
static_assert(false && "libc++ is currently not supported");
#else
static_assert(false && "No known element_size for sets. See types/set_type.toml");
#endif
auto sum = std::get<ParsedData::Sum>(d.val);
el.container_stats.emplace(result::Element::ContainerStats {
.capacity = 1,
.length = sum.index, // 0 for empty containers/void, 1 otherwise
});
// Must be in a `if constexpr` or the compiler will complain about make_field<DB, void>
if constexpr (!std::is_void<T0>::value) {
if (sum.index == 1) {
static constexpr auto element = make_field<DB, T0>("ptr_val");
stack_ins(element);
}
}
"""