binutils-gdb/gdbsupport/traits.h

110 lines
3.0 KiB
C
Raw Normal View History

/* Copyright (C) 2017-2020 Free Software Foundation, Inc.
Make sect_offset and cu_offset strong typedefs instead of structs A while ago, back when GDB was a C program, the sect_offset and cu_offset types were made structs in order to prevent incorrect mixing of those offsets. Now that we require C++11, we can make them integers again, while keeping the safety, by exploiting "enum class". We can add a bit more safety, even, by defining operators that the types _should_ support, helping making the suspicious uses stand out more. Getting at the underlying type is done with the new to_underlying function added by the previous patch, which also helps better spot where do we need to step out of the safety net. Mostly, that's around parsing the DWARF, and when we print the offset for complaint/debug purposes. But there are other occasional uses. Since we have to define the sect_offset/cu_offset types in a header anyway, I went ahead and generalized/library-fied the idea of "offset" types, making it trivial to add more such types if we find a use. See common/offset-type.h and the DEFINE_OFFSET_TYPE macro. I needed a couple generaly-useful preprocessor bits (e.g., yet another CONCAT implementation), so I started a new common/preprocessor.h file. I included units tests covering the "offset" types API. These are mostly compile-time tests, using SFINAE to check that expressions that shouldn't compile (e.g., comparing unrelated offset types) really are invalid and would fail to compile. This same idea appeared in my pending enum-flags revamp from a few months ago (though this version is a bit further modernized compared to what I had posted), and I plan on reusing the "check valid expression" bits added here in that series, so I went ahead and defined the CHECK_VALID_EXPR macro in its own header -- common/valid-expr.h. I think that's nicer regardless. I was borderline between calling the new types "offset" types, or "index" types, BTW. I stuck with "offset" simply because that's what we're already calling them, mostly. gdb/ChangeLog: 2017-04-04 Pedro Alves <palves@redhat.com> * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/offset-type-selftests.c. (SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o. * common/offset-type.h: New file. * common/preprocessor.h: New file. * common/traits.h: New file. * common/valid-expr.h: New file. * dwarf2expr.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2expr.h: Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2loc.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2read.c: Adjust to use sect_offset and cu_offset strong typedefs throughout. * gdbtypes.h: Include "common/offset-type.h". (cu_offset): Now an offset type (strong typedef) instead of a struct. (sect_offset): Likewise. (union call_site_parameter_u): Rename "param_offset" field to "param_cu_off". * unittests/offset-type-selftests.c: New file.
2017-04-04 21:03:26 +02:00
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef COMMON_TRAITS_H
#define COMMON_TRAITS_H
More gdb::optional features Currently we can't use gdb::optional<T> as function return type, because gdb::optional's copy ctor is deleted. For example, with: gdb::optional<int> function () { gdb::optional<int> opt; .... return opt; we get: src/gdb/foo.c: In function ‘gdb::optional<int> foo()’: src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’ return opt; ^ In file included from src/gdb/foo.c:68:0: src/gdb/common/gdb_optional.h:53:3: note: declared here optional (const optional &other) = delete; ^ I started by fixing that, and then ran into another missing feature, also fixed by this patch. The next feature I'm missing most from gdb::optional<T> compared to std::optional<T> is construction/move/assignment from a T, instead of having to default construct an gdb::optional and then use optional::emplace(....). For example: gdb::optional<std::string> function () { gdb::optional<std::string> opt; std::string str; ... opt.emplace (std::move (str)); return opt; vs gdb::optional<std::string> function () { std::string str; ... return str; The copy/move ctor/assign methods weren't initialy implemented because std::optional supports construction from a type U if U is convertible to T too, and has rules to decide whether the ctors are explicit/implicit based on that, and rules for whether the ctor should be trivial or not, etc., which leads to a much more complicated implementation. If we stick to supporting copy/move construction/assignment of/to an optional<T> from exactly only optional<T> and T, then all that conversion-related complication disappears, and we still gain convenience in most use cases. The patch also makes emplace return a reference to the constructor object, per C++17 std::optional, and adds a reset method, againt because std::optional has one and it's trivial to support it. These two changes are a requirement of the gdb::optional unit testing patch that will follow. gdb/ChangeLog: 2017-04-18 Pedro Alves <palves@redhat.com> * common/gdb_optional.h: Include common/traits.h. (in_place_t): New type. (in_place): New constexpr variable. (optional::optional): Remove member initialization of m_instantiated. (optional::optional(in_place_t...)): New constructor. (optional::~optional): Use reset. (optional::optional(const optional&)): New. (optional::optional(const optional&&)): New. (optional::optional(T &)): New. (optional::optional(T &&)): New. (operator::operator=(const optional &)): New. (operator::operator=(optional &&)): New. (operator::operator= (const T &)) (operator::operator= (T &&)) (operator::emplace (Args &&... args)): Return a T&. Use reset. (operator::reset): New. (operator::m_instantiated):: Add in-class initializer. * common/traits.h: Include <type_traits>. (struct And): New types.
2017-04-18 22:39:24 +02:00
#include <type_traits>
/* GCC does not understand __has_feature. */
#if !defined(__has_feature)
# define __has_feature(x) 0
#endif
/* HAVE_IS_TRIVIALLY_COPYABLE is defined as 1 iff
std::is_trivially_copyable is available. GCC only implemented it
in GCC 5. */
#if (__has_feature(is_trivially_copyable) \
|| (defined __GNUC__ && __GNUC__ >= 5))
# define HAVE_IS_TRIVIALLY_COPYABLE 1
#endif
Introduce obstack_new, poison other "typed" obstack functions Since we use obstacks with objects that are not default constructible, we sometimes need to manually call the constructor by hand using placement new: foo *f = obstack_alloc (obstack, sizeof (foo)); f = new (f) foo; It's possible to use allocate_on_obstack instead, but there are types that we sometimes want to allocate on an obstack, and sometimes on the regular heap. This patch introduces a utility to make this pattern simpler if allocate_on_obstack is not an option: foo *f = obstack_new<foo> (obstack); Right now there's only one usage (in tdesc_data_init). To help catch places where we would forget to call new when allocating such an object on an obstack, this patch also poisons some other methods of allocating an instance of a type on an obstack: - OBSTACK_ZALLOC/OBSTACK_CALLOC - XOBNEW/XOBNEW - GDBARCH_OBSTACK_ZALLOC/GDBARCH_OBSTACK_CALLOC Unfortunately, there's no way to catch wrong usages of obstack_alloc. By pulling on that string though, it tripped on allocating struct template_symbol using OBSTACK_ZALLOC. The criterion currently used to know whether it's safe to "malloc" an instance of a struct is whether it is a POD. Because it inherits from struct symbol, template_symbol is not a POD. This criterion is a bit too strict however, it should still safe to allocate memory for a template_symbol and memset it to 0. We didn't use is_trivially_constructible as the criterion in the first place only because it is not available in gcc < 5. So here I considered two alternatives: 1. Relax that criterion to use std::is_trivially_constructible and add a bit more glue code to make it work with gcc < 5 2. Continue pulling on the string and change how the symbol structures are allocated and initialized I managed to do both, but I decided to go with #1 to keep this patch simpler and more focused. When building with a compiler that does not have is_trivially_constructible, the check will just not be enforced. gdb/ChangeLog: * common/traits.h (HAVE_IS_TRIVIALLY_COPYABLE): Define if compiler supports std::is_trivially_constructible. * common/poison.h: Include obstack.h. (IsMallocable): Define to is_trivially_constructible if the compiler supports it, define to true_type otherwise. (xobnew): New. (XOBNEW): Redefine. (xobnewvec): New. (XOBNEWVEC): Redefine. * gdb_obstack.h (obstack_zalloc): New. (OBSTACK_ZALLOC): Redefine. (obstack_calloc): New. (OBSTACK_CALLOC): Redefine. (obstack_new): New. * gdbarch.sh: Include gdb_obstack in gdbarch.h. (gdbarch_obstack): New declaration in gdbarch.h, definition in gdbarch.c. (GDBARCH_OBSTACK_CALLOC, GDBARCH_OBSTACK_ZALLOC): Use obstack_calloc/obstack_zalloc. (gdbarch_obstack_zalloc): Remove. * target-descriptions.c (tdesc_data_init): Use obstack_new.
2018-05-21 03:06:03 +02:00
/* HAVE_IS_TRIVIALLY_CONSTRUCTIBLE is defined as 1 iff
std::is_trivially_constructible is available. GCC only implemented it
in GCC 5. */
#if (__has_feature(is_trivially_constructible) \
|| (defined __GNUC__ && __GNUC__ >= 5))
# define HAVE_IS_TRIVIALLY_CONSTRUCTIBLE 1
Introduce obstack_new, poison other "typed" obstack functions Since we use obstacks with objects that are not default constructible, we sometimes need to manually call the constructor by hand using placement new: foo *f = obstack_alloc (obstack, sizeof (foo)); f = new (f) foo; It's possible to use allocate_on_obstack instead, but there are types that we sometimes want to allocate on an obstack, and sometimes on the regular heap. This patch introduces a utility to make this pattern simpler if allocate_on_obstack is not an option: foo *f = obstack_new<foo> (obstack); Right now there's only one usage (in tdesc_data_init). To help catch places where we would forget to call new when allocating such an object on an obstack, this patch also poisons some other methods of allocating an instance of a type on an obstack: - OBSTACK_ZALLOC/OBSTACK_CALLOC - XOBNEW/XOBNEW - GDBARCH_OBSTACK_ZALLOC/GDBARCH_OBSTACK_CALLOC Unfortunately, there's no way to catch wrong usages of obstack_alloc. By pulling on that string though, it tripped on allocating struct template_symbol using OBSTACK_ZALLOC. The criterion currently used to know whether it's safe to "malloc" an instance of a struct is whether it is a POD. Because it inherits from struct symbol, template_symbol is not a POD. This criterion is a bit too strict however, it should still safe to allocate memory for a template_symbol and memset it to 0. We didn't use is_trivially_constructible as the criterion in the first place only because it is not available in gcc < 5. So here I considered two alternatives: 1. Relax that criterion to use std::is_trivially_constructible and add a bit more glue code to make it work with gcc < 5 2. Continue pulling on the string and change how the symbol structures are allocated and initialized I managed to do both, but I decided to go with #1 to keep this patch simpler and more focused. When building with a compiler that does not have is_trivially_constructible, the check will just not be enforced. gdb/ChangeLog: * common/traits.h (HAVE_IS_TRIVIALLY_COPYABLE): Define if compiler supports std::is_trivially_constructible. * common/poison.h: Include obstack.h. (IsMallocable): Define to is_trivially_constructible if the compiler supports it, define to true_type otherwise. (xobnew): New. (XOBNEW): Redefine. (xobnewvec): New. (XOBNEWVEC): Redefine. * gdb_obstack.h (obstack_zalloc): New. (OBSTACK_ZALLOC): Redefine. (obstack_calloc): New. (OBSTACK_CALLOC): Redefine. (obstack_new): New. * gdbarch.sh: Include gdb_obstack in gdbarch.h. (gdbarch_obstack): New declaration in gdbarch.h, definition in gdbarch.c. (GDBARCH_OBSTACK_CALLOC, GDBARCH_OBSTACK_ZALLOC): Use obstack_calloc/obstack_zalloc. (gdbarch_obstack_zalloc): Remove. * target-descriptions.c (tdesc_data_init): Use obstack_new.
2018-05-21 03:06:03 +02:00
#endif
Make sect_offset and cu_offset strong typedefs instead of structs A while ago, back when GDB was a C program, the sect_offset and cu_offset types were made structs in order to prevent incorrect mixing of those offsets. Now that we require C++11, we can make them integers again, while keeping the safety, by exploiting "enum class". We can add a bit more safety, even, by defining operators that the types _should_ support, helping making the suspicious uses stand out more. Getting at the underlying type is done with the new to_underlying function added by the previous patch, which also helps better spot where do we need to step out of the safety net. Mostly, that's around parsing the DWARF, and when we print the offset for complaint/debug purposes. But there are other occasional uses. Since we have to define the sect_offset/cu_offset types in a header anyway, I went ahead and generalized/library-fied the idea of "offset" types, making it trivial to add more such types if we find a use. See common/offset-type.h and the DEFINE_OFFSET_TYPE macro. I needed a couple generaly-useful preprocessor bits (e.g., yet another CONCAT implementation), so I started a new common/preprocessor.h file. I included units tests covering the "offset" types API. These are mostly compile-time tests, using SFINAE to check that expressions that shouldn't compile (e.g., comparing unrelated offset types) really are invalid and would fail to compile. This same idea appeared in my pending enum-flags revamp from a few months ago (though this version is a bit further modernized compared to what I had posted), and I plan on reusing the "check valid expression" bits added here in that series, so I went ahead and defined the CHECK_VALID_EXPR macro in its own header -- common/valid-expr.h. I think that's nicer regardless. I was borderline between calling the new types "offset" types, or "index" types, BTW. I stuck with "offset" simply because that's what we're already calling them, mostly. gdb/ChangeLog: 2017-04-04 Pedro Alves <palves@redhat.com> * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/offset-type-selftests.c. (SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o. * common/offset-type.h: New file. * common/preprocessor.h: New file. * common/traits.h: New file. * common/valid-expr.h: New file. * dwarf2expr.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2expr.h: Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2loc.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2read.c: Adjust to use sect_offset and cu_offset strong typedefs throughout. * gdbtypes.h: Include "common/offset-type.h". (cu_offset): Now an offset type (strong typedef) instead of a struct. (sect_offset): Likewise. (union call_site_parameter_u): Rename "param_offset" field to "param_cu_off". * unittests/offset-type-selftests.c: New file.
2017-04-04 21:03:26 +02:00
namespace gdb {
/* Pre C++14-safe (CWG 1558) version of C++17's std::void_t. See
<http://en.cppreference.com/w/cpp/types/void_t>. */
template<typename... Ts>
struct make_void { typedef void type; };
template<typename... Ts>
using void_t = typename make_void<Ts...>::type;
More gdb::optional features Currently we can't use gdb::optional<T> as function return type, because gdb::optional's copy ctor is deleted. For example, with: gdb::optional<int> function () { gdb::optional<int> opt; .... return opt; we get: src/gdb/foo.c: In function ‘gdb::optional<int> foo()’: src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’ return opt; ^ In file included from src/gdb/foo.c:68:0: src/gdb/common/gdb_optional.h:53:3: note: declared here optional (const optional &other) = delete; ^ I started by fixing that, and then ran into another missing feature, also fixed by this patch. The next feature I'm missing most from gdb::optional<T> compared to std::optional<T> is construction/move/assignment from a T, instead of having to default construct an gdb::optional and then use optional::emplace(....). For example: gdb::optional<std::string> function () { gdb::optional<std::string> opt; std::string str; ... opt.emplace (std::move (str)); return opt; vs gdb::optional<std::string> function () { std::string str; ... return str; The copy/move ctor/assign methods weren't initialy implemented because std::optional supports construction from a type U if U is convertible to T too, and has rules to decide whether the ctors are explicit/implicit based on that, and rules for whether the ctor should be trivial or not, etc., which leads to a much more complicated implementation. If we stick to supporting copy/move construction/assignment of/to an optional<T> from exactly only optional<T> and T, then all that conversion-related complication disappears, and we still gain convenience in most use cases. The patch also makes emplace return a reference to the constructor object, per C++17 std::optional, and adds a reset method, againt because std::optional has one and it's trivial to support it. These two changes are a requirement of the gdb::optional unit testing patch that will follow. gdb/ChangeLog: 2017-04-18 Pedro Alves <palves@redhat.com> * common/gdb_optional.h: Include common/traits.h. (in_place_t): New type. (in_place): New constexpr variable. (optional::optional): Remove member initialization of m_instantiated. (optional::optional(in_place_t...)): New constructor. (optional::~optional): Use reset. (optional::optional(const optional&)): New. (optional::optional(const optional&&)): New. (optional::optional(T &)): New. (optional::optional(T &&)): New. (operator::operator=(const optional &)): New. (operator::operator=(optional &&)): New. (operator::operator= (const T &)) (operator::operator= (T &&)) (operator::emplace (Args &&... args)): Return a T&. Use reset. (operator::reset): New. (operator::m_instantiated):: Add in-class initializer. * common/traits.h: Include <type_traits>. (struct And): New types.
2017-04-18 22:39:24 +02:00
/* A few trait helpers, mainly stolen from libstdc++. Uppercase
because "and/or", etc. are reserved keywords. */
template<typename Predicate>
struct Not : public std::integral_constant<bool, !Predicate::value>
{};
template<typename...>
struct Or;
template<>
struct Or<> : public std::false_type
{};
template<typename B1>
struct Or<B1> : public B1
{};
template<typename B1, typename B2>
struct Or<B1, B2>
: public std::conditional<B1::value, B1, B2>::type
{};
template<typename B1,typename B2,typename B3, typename... Bn>
struct Or<B1, B2, B3, Bn...>
: public std::conditional<B1::value, B1, Or<B2, B3, Bn...>>::type
{};
More gdb::optional features Currently we can't use gdb::optional<T> as function return type, because gdb::optional's copy ctor is deleted. For example, with: gdb::optional<int> function () { gdb::optional<int> opt; .... return opt; we get: src/gdb/foo.c: In function ‘gdb::optional<int> foo()’: src/gdb/foo.c:75:10: error: use of deleted function ‘gdb::optional<T>::optional(const gdb::optional<T>&) [with T = int]’ return opt; ^ In file included from src/gdb/foo.c:68:0: src/gdb/common/gdb_optional.h:53:3: note: declared here optional (const optional &other) = delete; ^ I started by fixing that, and then ran into another missing feature, also fixed by this patch. The next feature I'm missing most from gdb::optional<T> compared to std::optional<T> is construction/move/assignment from a T, instead of having to default construct an gdb::optional and then use optional::emplace(....). For example: gdb::optional<std::string> function () { gdb::optional<std::string> opt; std::string str; ... opt.emplace (std::move (str)); return opt; vs gdb::optional<std::string> function () { std::string str; ... return str; The copy/move ctor/assign methods weren't initialy implemented because std::optional supports construction from a type U if U is convertible to T too, and has rules to decide whether the ctors are explicit/implicit based on that, and rules for whether the ctor should be trivial or not, etc., which leads to a much more complicated implementation. If we stick to supporting copy/move construction/assignment of/to an optional<T> from exactly only optional<T> and T, then all that conversion-related complication disappears, and we still gain convenience in most use cases. The patch also makes emplace return a reference to the constructor object, per C++17 std::optional, and adds a reset method, againt because std::optional has one and it's trivial to support it. These two changes are a requirement of the gdb::optional unit testing patch that will follow. gdb/ChangeLog: 2017-04-18 Pedro Alves <palves@redhat.com> * common/gdb_optional.h: Include common/traits.h. (in_place_t): New type. (in_place): New constexpr variable. (optional::optional): Remove member initialization of m_instantiated. (optional::optional(in_place_t...)): New constructor. (optional::~optional): Use reset. (optional::optional(const optional&)): New. (optional::optional(const optional&&)): New. (optional::optional(T &)): New. (optional::optional(T &&)): New. (operator::operator=(const optional &)): New. (operator::operator=(optional &&)): New. (operator::operator= (const T &)) (operator::operator= (T &&)) (operator::emplace (Args &&... args)): Return a T&. Use reset. (operator::reset): New. (operator::m_instantiated):: Add in-class initializer. * common/traits.h: Include <type_traits>. (struct And): New types.
2017-04-18 22:39:24 +02:00
template<typename...>
struct And;
template<>
struct And<> : public std::true_type
{};
template<typename B1>
struct And<B1> : public B1
{};
template<typename B1, typename B2>
struct And<B1, B2>
: public std::conditional<B1::value, B2, B1>::type
{};
template<typename B1, typename B2, typename B3, typename... Bn>
struct And<B1, B2, B3, Bn...>
: public std::conditional<B1::value, And<B2, B3, Bn...>, B1>::type
{};
/* Concepts-light-like helper to make SFINAE logic easier to read. */
template<typename Condition>
using Requires = typename std::enable_if<Condition::value, void>::type;
Make sect_offset and cu_offset strong typedefs instead of structs A while ago, back when GDB was a C program, the sect_offset and cu_offset types were made structs in order to prevent incorrect mixing of those offsets. Now that we require C++11, we can make them integers again, while keeping the safety, by exploiting "enum class". We can add a bit more safety, even, by defining operators that the types _should_ support, helping making the suspicious uses stand out more. Getting at the underlying type is done with the new to_underlying function added by the previous patch, which also helps better spot where do we need to step out of the safety net. Mostly, that's around parsing the DWARF, and when we print the offset for complaint/debug purposes. But there are other occasional uses. Since we have to define the sect_offset/cu_offset types in a header anyway, I went ahead and generalized/library-fied the idea of "offset" types, making it trivial to add more such types if we find a use. See common/offset-type.h and the DEFINE_OFFSET_TYPE macro. I needed a couple generaly-useful preprocessor bits (e.g., yet another CONCAT implementation), so I started a new common/preprocessor.h file. I included units tests covering the "offset" types API. These are mostly compile-time tests, using SFINAE to check that expressions that shouldn't compile (e.g., comparing unrelated offset types) really are invalid and would fail to compile. This same idea appeared in my pending enum-flags revamp from a few months ago (though this version is a bit further modernized compared to what I had posted), and I plan on reusing the "check valid expression" bits added here in that series, so I went ahead and defined the CHECK_VALID_EXPR macro in its own header -- common/valid-expr.h. I think that's nicer regardless. I was borderline between calling the new types "offset" types, or "index" types, BTW. I stuck with "offset" simply because that's what we're already calling them, mostly. gdb/ChangeLog: 2017-04-04 Pedro Alves <palves@redhat.com> * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add unittests/offset-type-selftests.c. (SUBDIR_UNITTESTS_OBS): Add offset-type-selftests.o. * common/offset-type.h: New file. * common/preprocessor.h: New file. * common/traits.h: New file. * common/valid-expr.h: New file. * dwarf2expr.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2expr.h: Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2loc.c: Include "common/underlying.h". Adjust to use sect_offset and cu_offset strong typedefs throughout. * dwarf2read.c: Adjust to use sect_offset and cu_offset strong typedefs throughout. * gdbtypes.h: Include "common/offset-type.h". (cu_offset): Now an offset type (strong typedef) instead of a struct. (sect_offset): Likewise. (union call_site_parameter_u): Rename "param_offset" field to "param_cu_off". * unittests/offset-type-selftests.c: New file.
2017-04-04 21:03:26 +02:00
}
#endif /* COMMON_TRAITS_H */