This patch started by adding comprehensive unit tests for enum_flags. For the testing part, it adds: - tests of normal expected uses of the API. - checks that _invalid_ uses of the API would fail to compile. I.e., it validates that enum_flags really is a strong type, and that incorrect mixing of enum types would be caught at compile time. It pulls that off making use of SFINEA and C++11's decltype/constexpr. This revealed many holes in the enum_flags API. For example, the f1 assignment below currently incorrectly fails to compile: enum_flags<flags> f1 = FLAG1; enum_flags<flags> f2 = FLAG2 | f1; The unit tests also revealed that this useful use case doesn't work: enum flag { FLAG1 = 1, FLAG2 = 2 }; enum_flags<flag> src = FLAG1; enum_flags<flag> f1 = condition ? src : FLAG2; It fails to compile because enum_flags<flag> and flag are convertible to each other. Turns out that making enum_flags be implicitly convertible to the backing raw enum type was not a good idea. If we make it convertible to the underlying type instead, we fix that ternary operator use case, and, we find cases throughout the codebase that should be using the enum_flags but were using the raw backing enum instead. So it's a good change overall. Also, several operators were missing. These holes and more are plugged by this patch, by reworking how the enum_flags operators are implemented, and making use of C++11's feature of being able to delete methods/functions. There are cases in gdb/compile/ where we need to call a function in a C plugin API that expects the raw enum. To address cases like that, this adds a "raw()" method to enum_flags. This way we can keep using the safer enum_flags to construct the value, and then be explicit when we need to get at the raw enum. This makes most of the enum_flags operators constexpr. Beyond enabling more compiler optimizations and enabling the new unit tests, this has other advantages, like making it possible to use operator| with enum_flags values in switch cases, where only compile-time constants are allowed: enum_flags<flags> f = FLAG1 | FLAG2; switch (f) { case FLAG1 | FLAG2: break; } Currently that fails to compile. It also switches to a different mechanism of enabling the global operators. The current mechanism isn't namespace friendly, the new one is. It also switches to C++11-style SFINAE -- instead of wrapping the return type in a SFINAE-friently structure, we use an unnamed template parameter. I.e., this: template <typename enum_type, typename = is_enum_flags_enum_type_t<enum_type>> enum_type operator& (enum_type e1, enum_type e2) instead of: template <typename enum_type> typename enum_flags_type<enum_type>::type operator& (enum_type e1, enum_type e2) Note that the static_assert inside operator~() was converted to a couple overloads (signed vs unsigned), because static_assert is too late for SFINAE-based tests, which is important for the CHECK_VALID unit tests. Tested with gcc {4.8, 7.1, 9.3} and clang {5.0.2, 10.0.0}. gdb/ChangeLog: * Makefile.in (SELFTESTS_SRCS): Add unittests/enum-flags-selftests.c. * btrace.c (ftrace_update_caller, ftrace_fixup_calle): Use btrace_function_flags instead of enum btrace_function_flag. * compile/compile-c-types.c (convert_qualified): Use enum_flags::raw. * compile/compile-cplus-symbols.c (convert_one_symbol) (convert_symbol_bmsym): * compile/compile-cplus-types.c (compile_cplus_convert_method) (compile_cplus_convert_struct_or_union_methods) (compile_cplus_instance::convert_qualified_base): * go-exp.y (parse_string_or_char): Add cast to int. * unittests/enum-flags-selftests.c: New file. * record-btrace.c (btrace_thread_flag_to_str): Change parameter's type to btrace_thread_flags from btrace_thread_flag. (record_btrace_cancel_resume, record_btrace_step_thread): Change local's type to btrace_thread_flags from btrace_thread_flag. Add cast in DEBUG call. gdbsupport/ChangeLog: * enum-flags.h: Include "traits.h". (DEF_ENUM_FLAGS_TYPE): Declare a function instead of defining a structure. (enum_underlying_type): Update comment. (namespace enum_flags_detail): New. Move struct zero_type here. (EnumIsUnsigned, EnumIsSigned): New. (class enum_flags): Make most methods constexpr. (operator&=, operator|=, operator^=): Take an enum_flags instead of an enum_type. Make rvalue ref versions deleted. (operator enum_type()): Delete. (operator&, operator|, operator^, operator~): Delete, moved out of class. (raw()): New method. (is_enum_flags_enum_type_t): Declare. (ENUM_FLAGS_GEN_BINOP, ENUM_FLAGS_GEN_COMPOUND_ASSIGN) (ENUM_FLAGS_GEN_COMP): New. Use them to reimplement global operators. (operator~): Now constexpr and reimplemented. (operator<<, operator>>): New deleted functions. * valid-expr.h (CHECK_VALID_EXPR_5, CHECK_VALID_EXPR_6): New.
402 lines
12 KiB
C
402 lines
12 KiB
C
/* Convert types from GDB to GCC
|
||
|
||
Copyright (C) 2014-2020 Free Software Foundation, Inc.
|
||
|
||
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/>. */
|
||
|
||
|
||
#include "defs.h"
|
||
#include "gdbtypes.h"
|
||
#include "compile-internal.h"
|
||
#include "compile-c.h"
|
||
#include "objfiles.h"
|
||
|
||
/* Convert a pointer type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_pointer (compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type target = context->convert_type (TYPE_TARGET_TYPE (type));
|
||
|
||
return context->plugin ().build_pointer_type (target);
|
||
}
|
||
|
||
/* Convert an array type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_array (compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type element_type;
|
||
struct type *range = type->index_type ();
|
||
|
||
element_type = context->convert_type (TYPE_TARGET_TYPE (type));
|
||
|
||
if (range->bounds ()->low.kind () != PROP_CONST)
|
||
return context->plugin ().error (_("array type with non-constant"
|
||
" lower bound is not supported"));
|
||
if (range->bounds ()->low.const_val () != 0)
|
||
return context->plugin ().error (_("cannot convert array type with "
|
||
"non-zero lower bound to C"));
|
||
|
||
if (range->bounds ()->high.kind () == PROP_LOCEXPR
|
||
|| range->bounds ()->high.kind () == PROP_LOCLIST)
|
||
{
|
||
gcc_type result;
|
||
|
||
if (type->is_vector ())
|
||
return context->plugin ().error (_("variably-sized vector type"
|
||
" is not supported"));
|
||
|
||
std::string upper_bound
|
||
= c_get_range_decl_name (&range->bounds ()->high);
|
||
result = context->plugin ().build_vla_array_type (element_type,
|
||
upper_bound.c_str ());
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
LONGEST low_bound, high_bound, count;
|
||
|
||
if (get_array_bounds (type, &low_bound, &high_bound) == 0)
|
||
count = -1;
|
||
else
|
||
{
|
||
gdb_assert (low_bound == 0); /* Ensured above. */
|
||
count = high_bound + 1;
|
||
}
|
||
|
||
if (type->is_vector ())
|
||
return context->plugin ().build_vector_type (element_type, count);
|
||
return context->plugin ().build_array_type (element_type, count);
|
||
}
|
||
}
|
||
|
||
/* Convert a struct or union type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_struct_or_union (compile_c_instance *context, struct type *type)
|
||
{
|
||
int i;
|
||
gcc_type result;
|
||
|
||
/* First we create the resulting type and enter it into our hash
|
||
table. This lets recursive types work. */
|
||
if (type->code () == TYPE_CODE_STRUCT)
|
||
result = context->plugin ().build_record_type ();
|
||
else
|
||
{
|
||
gdb_assert (type->code () == TYPE_CODE_UNION);
|
||
result = context->plugin ().build_union_type ();
|
||
}
|
||
context->insert_type (type, result);
|
||
|
||
for (i = 0; i < type->num_fields (); ++i)
|
||
{
|
||
gcc_type field_type;
|
||
unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
|
||
|
||
field_type = context->convert_type (type->field (i).type ());
|
||
if (bitsize == 0)
|
||
bitsize = 8 * TYPE_LENGTH (type->field (i).type ());
|
||
context->plugin ().build_add_field (result,
|
||
TYPE_FIELD_NAME (type, i),
|
||
field_type,
|
||
bitsize,
|
||
TYPE_FIELD_BITPOS (type, i));
|
||
}
|
||
|
||
context->plugin ().finish_record_or_union (result, TYPE_LENGTH (type));
|
||
return result;
|
||
}
|
||
|
||
/* Convert an enum type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_enum (compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type int_type, result;
|
||
int i;
|
||
|
||
int_type = context->plugin ().int_type_v0 (type->is_unsigned (),
|
||
TYPE_LENGTH (type));
|
||
|
||
result = context->plugin ().build_enum_type (int_type);
|
||
for (i = 0; i < type->num_fields (); ++i)
|
||
{
|
||
context->plugin ().build_add_enum_constant
|
||
(result, TYPE_FIELD_NAME (type, i), TYPE_FIELD_ENUMVAL (type, i));
|
||
}
|
||
|
||
context->plugin ().finish_enum_type (result);
|
||
|
||
return result;
|
||
}
|
||
|
||
/* Convert a function type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_func (compile_c_instance *context, struct type *type)
|
||
{
|
||
int i;
|
||
gcc_type result, return_type;
|
||
struct gcc_type_array array;
|
||
int is_varargs = type->has_varargs () || !type->is_prototyped ();
|
||
|
||
struct type *target_type = TYPE_TARGET_TYPE (type);
|
||
|
||
/* Functions with no debug info have no return type. Ideally we'd
|
||
want to fallback to the type of the cast just before the
|
||
function, like GDB's built-in expression parser, but we don't
|
||
have access to that type here. For now, fallback to int, like
|
||
GDB's parser used to do. */
|
||
if (target_type == NULL)
|
||
{
|
||
if (TYPE_OBJFILE_OWNED (type))
|
||
target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
|
||
else
|
||
target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
|
||
warning (_("function has unknown return type; assuming int"));
|
||
}
|
||
|
||
/* This approach means we can't make self-referential function
|
||
types. Those are impossible in C, though. */
|
||
return_type = context->convert_type (target_type);
|
||
|
||
array.n_elements = type->num_fields ();
|
||
array.elements = XNEWVEC (gcc_type, type->num_fields ());
|
||
for (i = 0; i < type->num_fields (); ++i)
|
||
array.elements[i] = context->convert_type (type->field (i).type ());
|
||
|
||
result = context->plugin ().build_function_type (return_type,
|
||
&array, is_varargs);
|
||
xfree (array.elements);
|
||
|
||
return result;
|
||
}
|
||
|
||
/* Convert an integer type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_int (compile_c_instance *context, struct type *type)
|
||
{
|
||
if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
|
||
{
|
||
if (type->has_no_signedness ())
|
||
{
|
||
gdb_assert (TYPE_LENGTH (type) == 1);
|
||
return context->plugin ().char_type ();
|
||
}
|
||
return context->plugin ().int_type (type->is_unsigned (),
|
||
TYPE_LENGTH (type),
|
||
type->name ());
|
||
}
|
||
else
|
||
return context->plugin ().int_type_v0 (type->is_unsigned (),
|
||
TYPE_LENGTH (type));
|
||
}
|
||
|
||
/* Convert a floating-point type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_float (compile_c_instance *context, struct type *type)
|
||
{
|
||
if (context->plugin ().version () >= GCC_C_FE_VERSION_1)
|
||
return context->plugin ().float_type (TYPE_LENGTH (type),
|
||
type->name ());
|
||
else
|
||
return context->plugin ().float_type_v0 (TYPE_LENGTH (type));
|
||
}
|
||
|
||
/* Convert the 'void' type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_void (compile_c_instance *context, struct type *type)
|
||
{
|
||
return context->plugin ().void_type ();
|
||
}
|
||
|
||
/* Convert a boolean type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_bool (compile_c_instance *context, struct type *type)
|
||
{
|
||
return context->plugin ().bool_type ();
|
||
}
|
||
|
||
/* Convert a qualified type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_qualified (compile_c_instance *context, struct type *type)
|
||
{
|
||
struct type *unqual = make_unqualified_type (type);
|
||
gcc_type unqual_converted;
|
||
gcc_qualifiers_flags quals = 0;
|
||
|
||
unqual_converted = context->convert_type (unqual);
|
||
|
||
if (TYPE_CONST (type))
|
||
quals |= GCC_QUALIFIER_CONST;
|
||
if (TYPE_VOLATILE (type))
|
||
quals |= GCC_QUALIFIER_VOLATILE;
|
||
if (TYPE_RESTRICT (type))
|
||
quals |= GCC_QUALIFIER_RESTRICT;
|
||
|
||
return context->plugin ().build_qualified_type (unqual_converted,
|
||
quals.raw ());
|
||
}
|
||
|
||
/* Convert a complex type to its gcc representation. */
|
||
|
||
static gcc_type
|
||
convert_complex (compile_c_instance *context, struct type *type)
|
||
{
|
||
gcc_type base = context->convert_type (TYPE_TARGET_TYPE (type));
|
||
|
||
return context->plugin ().build_complex_type (base);
|
||
}
|
||
|
||
/* A helper function which knows how to convert most types from their
|
||
gdb representation to the corresponding gcc form. This examines
|
||
the TYPE and dispatches to the appropriate conversion function. It
|
||
returns the gcc type. */
|
||
|
||
static gcc_type
|
||
convert_type_basic (compile_c_instance *context, struct type *type)
|
||
{
|
||
/* If we are converting a qualified type, first convert the
|
||
unqualified type and then apply the qualifiers. */
|
||
if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
|
||
| TYPE_INSTANCE_FLAG_VOLATILE
|
||
| TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
|
||
return convert_qualified (context, type);
|
||
|
||
switch (type->code ())
|
||
{
|
||
case TYPE_CODE_PTR:
|
||
return convert_pointer (context, type);
|
||
|
||
case TYPE_CODE_ARRAY:
|
||
return convert_array (context, type);
|
||
|
||
case TYPE_CODE_STRUCT:
|
||
case TYPE_CODE_UNION:
|
||
return convert_struct_or_union (context, type);
|
||
|
||
case TYPE_CODE_ENUM:
|
||
return convert_enum (context, type);
|
||
|
||
case TYPE_CODE_FUNC:
|
||
return convert_func (context, type);
|
||
|
||
case TYPE_CODE_INT:
|
||
return convert_int (context, type);
|
||
|
||
case TYPE_CODE_FLT:
|
||
return convert_float (context, type);
|
||
|
||
case TYPE_CODE_VOID:
|
||
return convert_void (context, type);
|
||
|
||
case TYPE_CODE_BOOL:
|
||
return convert_bool (context, type);
|
||
|
||
case TYPE_CODE_COMPLEX:
|
||
return convert_complex (context, type);
|
||
|
||
case TYPE_CODE_ERROR:
|
||
{
|
||
/* Ideally, if we get here due to a cast expression, we'd use
|
||
the cast-to type as the variable's type, like GDB's
|
||
built-in parser does. For now, assume "int" like GDB's
|
||
built-in parser used to do, but at least warn. */
|
||
struct type *fallback;
|
||
if (TYPE_OBJFILE_OWNED (type))
|
||
fallback = objfile_type (TYPE_OWNER (type).objfile)->builtin_int;
|
||
else
|
||
fallback = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int;
|
||
warning (_("variable has unknown type; assuming int"));
|
||
return convert_int (context, fallback);
|
||
}
|
||
}
|
||
|
||
return context->plugin ().error (_("cannot convert gdb type to gcc type"));
|
||
}
|
||
|
||
/* Default compile flags for C. */
|
||
|
||
const char *compile_c_instance::m_default_cflags = "-std=gnu11"
|
||
/* Otherwise the .o file may need
|
||
"_Unwind_Resume" and
|
||
"__gcc_personality_v0". */
|
||
" -fno-exceptions"
|
||
" -Wno-implicit-function-declaration";
|
||
|
||
/* See compile-c.h. */
|
||
|
||
gcc_type
|
||
compile_c_instance::convert_type (struct type *type)
|
||
{
|
||
/* We don't ever have to deal with typedefs in this code, because
|
||
those are only needed as symbols by the C compiler. */
|
||
type = check_typedef (type);
|
||
|
||
gcc_type result;
|
||
if (get_cached_type (type, &result))
|
||
return result;
|
||
|
||
result = convert_type_basic (this, type);
|
||
insert_type (type, result);
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
/* C plug-in wrapper. */
|
||
|
||
#define FORWARD(OP,...) m_context->c_ops->OP(m_context, ##__VA_ARGS__)
|
||
#define GCC_METHOD0(R, N) \
|
||
R gcc_c_plugin::N () const \
|
||
{ return FORWARD (N); }
|
||
#define GCC_METHOD1(R, N, A) \
|
||
R gcc_c_plugin::N (A a) const \
|
||
{ return FORWARD (N, a); }
|
||
#define GCC_METHOD2(R, N, A, B) \
|
||
R gcc_c_plugin::N (A a, B b) const \
|
||
{ return FORWARD (N, a, b); }
|
||
#define GCC_METHOD3(R, N, A, B, C) \
|
||
R gcc_c_plugin::N (A a, B b, C c) const \
|
||
{ return FORWARD (N, a, b, c); }
|
||
#define GCC_METHOD4(R, N, A, B, C, D) \
|
||
R gcc_c_plugin::N (A a, B b, C c, D d) const \
|
||
{ return FORWARD (N, a, b, c, d); }
|
||
#define GCC_METHOD5(R, N, A, B, C, D, E) \
|
||
R gcc_c_plugin::N (A a, B b, C c, D d, E e) const \
|
||
{ return FORWARD (N, a, b, c, d, e); }
|
||
#define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
|
||
R gcc_c_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
|
||
{ return FORWARD (N, a, b, c, d, e, f, g); }
|
||
|
||
#include "gcc-c-fe.def"
|
||
|
||
#undef GCC_METHOD0
|
||
#undef GCC_METHOD1
|
||
#undef GCC_METHOD2
|
||
#undef GCC_METHOD3
|
||
#undef GCC_METHOD4
|
||
#undef GCC_METHOD5
|
||
#undef GCC_METHOD7
|
||
#undef FORWARD
|