From a5284549da1ccb8b27380c4a6012b248d7ba011f Mon Sep 17 00:00:00 2001 From: Thierry Treyer Date: Thu, 31 Aug 2023 08:25:53 -0700 Subject: [PATCH] Implement Container V2 for std::shared_ptr --- test/integration/std_smart_ptr.toml | 57 +++++++++++++++++++++++++--- types/shrd_ptr_type.toml | 59 +++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/test/integration/std_smart_ptr.toml b/test/integration/std_smart_ptr.toml index c0a47e7..8fc66bf 100644 --- a/test/integration/std_smart_ptr.toml +++ b/test/integration/std_smart_ptr.toml @@ -158,7 +158,6 @@ definitions = ''' ] ''' [cases.shared_ptr_uint64_empty] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr&"] setup = "return {nullptr};" expect_json = ''' @@ -172,8 +171,17 @@ definitions = ''' } ] ''' + expect_json_v2 = ''' + [ + { + "staticSize": 16, + "exclusiveSize": 16, + "length": 0, + "capacity": 1 + } + ] + ''' [cases.shared_ptr_uint64_present] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr&"] setup = "return std::make_shared(64);" expect_json = ''' @@ -187,8 +195,20 @@ definitions = ''' } ] ''' + expect_json_v2 = ''' + [ + { + "staticSize": 16, + "exclusiveSize": 16, + "length": 1, + "capacity": 1, + "members": [ + { "staticSize": 8, "exclusiveSize": 8 } + ] + } + ] + ''' [cases.shared_ptr_vector_empty] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr>&"] setup = "return {nullptr};" expect_json = ''' @@ -202,8 +222,17 @@ definitions = ''' } ] ''' + expect_json_v2 = ''' + [ + { + "staticSize": 16, + "exclusiveSize": 16, + "length": 0, + "capacity": 1 + } + ] + ''' [cases.shared_ptr_vector_present] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr>&"] setup = "return std::make_shared>(std::initializer_list({1,2,3,4,5}));" expect_json = ''' @@ -223,8 +252,25 @@ definitions = ''' } ] ''' + expect_json_v2 = ''' + [ + { + "staticSize": 16, + "exclusiveSize": 16, + "length": 1, + "capacity": 1, + "members": [ + { + "staticSize": 24, + "exclusiveSize": 24, + "length": 5, + "capacity": 5 + } + ] + } + ] + ''' [cases.shared_ptr_void_empty] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr&"] setup = "return {nullptr};" expect_json = ''' @@ -237,7 +283,6 @@ definitions = ''' ] ''' [cases.shared_ptr_void_present] - oil_skip = "std::shared_ptr is not implemented for treebuilder v2" # https://github.com/facebookexperimental/object-introspection/issues/300 param_types = ["std::shared_ptr&"] setup = "return {std::shared_ptr(new int)};" expect_json = ''' diff --git a/types/shrd_ptr_type.toml b/types/shrd_ptr_type.toml index fff8f54..2f142cb 100644 --- a/types/shrd_ptr_type.toml +++ b/types/shrd_ptr_type.toml @@ -65,3 +65,62 @@ struct TypeHandler> { } }; """ + +traversal_func = """ +auto tail = returnArg.write((uintptr_t)container.get()); + +if constexpr (std::is_void::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(*container, ret); + }); +} +""" + +[[codegen.processor]] +type = "types::st::VarInt" +func = """ +el.pointer = std::get(d.val).value; +""" + +[[codegen.processor]] +type = """ +types::st::Sum, + typename TypeHandler::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(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 +if constexpr (!std::is_void::value) { + if (sum.index == 1) { + static constexpr auto element = make_field("ptr_val"); + ins.emplace(element); + } +} +"""