gdb/compile/compile-c-types.c - gdb

Data types defined

Functions defined

Source code

  1. /* Convert types from GDB to GCC

  2.    Copyright (C) 2014-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  14. #include "defs.h"
  15. #include "gdbtypes.h"
  16. #include "compile-internal.h"
  17. #include "gdb_assert.h"

  18. /* An object that maps a gdb type to a gcc type.  */

  19. struct type_map_instance
  20. {
  21.   /* The gdb type.  */

  22.   struct type *type;

  23.   /* The corresponding gcc type handle.  */

  24.   gcc_type gcc_type;
  25. };

  26. /* Hash a type_map_instance.  */

  27. static hashval_t
  28. hash_type_map_instance (const void *p)
  29. {
  30.   const struct type_map_instance *inst = p;

  31.   return htab_hash_pointer (inst->type);
  32. }

  33. /* Check two type_map_instance objects for equality.  */

  34. static int
  35. eq_type_map_instance (const void *a, const void *b)
  36. {
  37.   const struct type_map_instance *insta = a;
  38.   const struct type_map_instance *instb = b;

  39.   return insta->type == instb->type;
  40. }



  41. /* Insert an entry into the type map associated with CONTEXT that maps
  42.    from the gdb type TYPE to the gcc type GCC_TYPE.  It is ok for a
  43.    given type to be inserted more than once, provided that the exact
  44.    same association is made each time.  This simplifies how type
  45.    caching works elsewhere in this file -- see how struct type caching
  46.    is handled.  */

  47. static void
  48. insert_type (struct compile_c_instance *context, struct type *type,
  49.              gcc_type gcc_type)
  50. {
  51.   struct type_map_instance inst, *add;
  52.   void **slot;

  53.   inst.type = type;
  54.   inst.gcc_type = gcc_type;
  55.   slot = htab_find_slot (context->type_map, &inst, INSERT);

  56.   add = *slot;
  57.   /* The type might have already been inserted in order to handle
  58.      recursive types.  */
  59.   gdb_assert (add == NULL || add->gcc_type == gcc_type);

  60.   if (add == NULL)
  61.     {
  62.       add = XNEW (struct type_map_instance);
  63.       *add = inst;
  64.       *slot = add;
  65.     }
  66. }

  67. /* Convert a pointer type to its gcc representation.  */

  68. static gcc_type
  69. convert_pointer (struct compile_c_instance *context, struct type *type)
  70. {
  71.   gcc_type target = convert_type (context, TYPE_TARGET_TYPE (type));

  72.   return C_CTX (context)->c_ops->build_pointer_type (C_CTX (context),
  73.                                                      target);
  74. }

  75. /* Convert an array type to its gcc representation.  */

  76. static gcc_type
  77. convert_array (struct compile_c_instance *context, struct type *type)
  78. {
  79.   gcc_type element_type;
  80.   struct type *range = TYPE_INDEX_TYPE (type);

  81.   element_type = convert_type (context, TYPE_TARGET_TYPE (type));

  82.   if (TYPE_LOW_BOUND_KIND (range) != PROP_CONST)
  83.     return C_CTX (context)->c_ops->error (C_CTX (context),
  84.                                           _("array type with non-constant"
  85.                                             " lower bound is not supported"));
  86.   if (TYPE_LOW_BOUND (range) != 0)
  87.     return C_CTX (context)->c_ops->error (C_CTX (context),
  88.                                           _("cannot convert array type with "
  89.                                             "non-zero lower bound to C"));

  90.   if (TYPE_HIGH_BOUND_KIND (range) == PROP_LOCEXPR
  91.       || TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST)
  92.     {
  93.       gcc_type result;
  94.       char *upper_bound;

  95.       if (TYPE_VECTOR (type))
  96.         return C_CTX (context)->c_ops->error (C_CTX (context),
  97.                                               _("variably-sized vector type"
  98.                                                 " is not supported"));

  99.       upper_bound = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high);
  100.       result = C_CTX (context)->c_ops->build_vla_array_type (C_CTX (context),
  101.                                                              element_type,
  102.                                                              upper_bound);
  103.       xfree (upper_bound);
  104.       return result;
  105.     }
  106.   else
  107.     {
  108.       LONGEST low_bound, high_bound, count;

  109.       if (get_array_bounds (type, &low_bound, &high_bound) == 0)
  110.         count = -1;
  111.       else
  112.         {
  113.           gdb_assert (low_bound == 0); /* Ensured above.  */
  114.           count = high_bound + 1;
  115.         }

  116.       if (TYPE_VECTOR (type))
  117.         return C_CTX (context)->c_ops->build_vector_type (C_CTX (context),
  118.                                                           element_type,
  119.                                                           count);
  120.       return C_CTX (context)->c_ops->build_array_type (C_CTX (context),
  121.                                                        element_type, count);
  122.     }
  123. }

  124. /* Convert a struct or union type to its gcc representation.  */

  125. static gcc_type
  126. convert_struct_or_union (struct compile_c_instance *context, struct type *type)
  127. {
  128.   int i;
  129.   gcc_type result;

  130.   /* First we create the resulting type and enter it into our hash
  131.      table.  This lets recursive types work.  */
  132.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  133.     result = C_CTX (context)->c_ops->build_record_type (C_CTX (context));
  134.   else
  135.     {
  136.       gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
  137.       result = C_CTX (context)->c_ops->build_union_type (C_CTX (context));
  138.     }
  139.   insert_type (context, type, result);

  140.   for (i = 0; i < TYPE_NFIELDS (type); ++i)
  141.     {
  142.       gcc_type field_type;
  143.       unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);

  144.       field_type = convert_type (context, TYPE_FIELD_TYPE (type, i));
  145.       if (bitsize == 0)
  146.         bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
  147.       C_CTX (context)->c_ops->build_add_field (C_CTX (context), result,
  148.                                                TYPE_FIELD_NAME (type, i),
  149.                                                field_type,
  150.                                                bitsize,
  151.                                                TYPE_FIELD_BITPOS (type, i));
  152.     }

  153.   C_CTX (context)->c_ops->finish_record_or_union (C_CTX (context), result,
  154.                                                   TYPE_LENGTH (type));
  155.   return result;
  156. }

  157. /* Convert an enum type to its gcc representation.  */

  158. static gcc_type
  159. convert_enum (struct compile_c_instance *context, struct type *type)
  160. {
  161.   gcc_type int_type, result;
  162.   int i;
  163.   struct gcc_c_context *ctx = C_CTX (context);

  164.   int_type = ctx->c_ops->int_type (ctx,
  165.                                    TYPE_UNSIGNED (type),
  166.                                    TYPE_LENGTH (type));

  167.   result = ctx->c_ops->build_enum_type (ctx, int_type);
  168.   for (i = 0; i < TYPE_NFIELDS (type); ++i)
  169.     {
  170.       ctx->c_ops->build_add_enum_constant (ctx,
  171.                                            result,
  172.                                            TYPE_FIELD_NAME (type, i),
  173.                                            TYPE_FIELD_ENUMVAL (type, i));
  174.     }

  175.   ctx->c_ops->finish_enum_type (ctx, result);

  176.   return result;
  177. }

  178. /* Convert a function type to its gcc representation.  */

  179. static gcc_type
  180. convert_func (struct compile_c_instance *context, struct type *type)
  181. {
  182.   int i;
  183.   gcc_type result, return_type;
  184.   struct gcc_type_array array;
  185.   int is_varargs = TYPE_VARARGS (type) || !TYPE_PROTOTYPED (type);

  186.   /* This approach means we can't make self-referential function
  187.      types.  Those are impossible in C, though.  */
  188.   return_type = convert_type (context, TYPE_TARGET_TYPE (type));

  189.   array.n_elements = TYPE_NFIELDS (type);
  190.   array.elements = XNEWVEC (gcc_type, TYPE_NFIELDS (type));
  191.   for (i = 0; i < TYPE_NFIELDS (type); ++i)
  192.     array.elements[i] = convert_type (context, TYPE_FIELD_TYPE (type, i));

  193.   result = C_CTX (context)->c_ops->build_function_type (C_CTX (context),
  194.                                                         return_type,
  195.                                                         &array, is_varargs);
  196.   xfree (array.elements);

  197.   return result;
  198. }

  199. /* Convert an integer type to its gcc representation.  */

  200. static gcc_type
  201. convert_int (struct compile_c_instance *context, struct type *type)
  202. {
  203.   return C_CTX (context)->c_ops->int_type (C_CTX (context),
  204.                                            TYPE_UNSIGNED (type),
  205.                                            TYPE_LENGTH (type));
  206. }

  207. /* Convert a floating-point type to its gcc representation.  */

  208. static gcc_type
  209. convert_float (struct compile_c_instance *context, struct type *type)
  210. {
  211.   return C_CTX (context)->c_ops->float_type (C_CTX (context),
  212.                                              TYPE_LENGTH (type));
  213. }

  214. /* Convert the 'void' type to its gcc representation.  */

  215. static gcc_type
  216. convert_void (struct compile_c_instance *context, struct type *type)
  217. {
  218.   return C_CTX (context)->c_ops->void_type (C_CTX (context));
  219. }

  220. /* Convert a boolean type to its gcc representation.  */

  221. static gcc_type
  222. convert_bool (struct compile_c_instance *context, struct type *type)
  223. {
  224.   return C_CTX (context)->c_ops->bool_type (C_CTX (context));
  225. }

  226. /* Convert a qualified type to its gcc representation.  */

  227. static gcc_type
  228. convert_qualified (struct compile_c_instance *context, struct type *type)
  229. {
  230.   struct type *unqual = make_unqualified_type (type);
  231.   gcc_type unqual_converted;
  232.   int quals = 0;

  233.   unqual_converted = convert_type (context, unqual);

  234.   if (TYPE_CONST (type))
  235.     quals |= GCC_QUALIFIER_CONST;
  236.   if (TYPE_VOLATILE (type))
  237.     quals |= GCC_QUALIFIER_VOLATILE;
  238.   if (TYPE_RESTRICT (type))
  239.     quals |= GCC_QUALIFIER_RESTRICT;

  240.   return C_CTX (context)->c_ops->build_qualified_type (C_CTX (context),
  241.                                                        unqual_converted,
  242.                                                        quals);
  243. }

  244. /* Convert a complex type to its gcc representation.  */

  245. static gcc_type
  246. convert_complex (struct compile_c_instance *context, struct type *type)
  247. {
  248.   gcc_type base = convert_type (context, TYPE_TARGET_TYPE (type));

  249.   return C_CTX (context)->c_ops->build_complex_type (C_CTX (context), base);
  250. }

  251. /* A helper function which knows how to convert most types from their
  252.    gdb representation to the corresponding gcc form.  This examines
  253.    the TYPE and dispatches to the appropriate conversion function.  It
  254.    returns the gcc type.  */

  255. static gcc_type
  256. convert_type_basic (struct compile_c_instance *context, struct type *type)
  257. {
  258.   /* If we are converting a qualified type, first convert the
  259.      unqualified type and then apply the qualifiers.  */
  260.   if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
  261.                                      | TYPE_INSTANCE_FLAG_VOLATILE
  262.                                      | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
  263.     return convert_qualified (context, type);

  264.   switch (TYPE_CODE (type))
  265.     {
  266.     case TYPE_CODE_PTR:
  267.       return convert_pointer (context, type);

  268.     case TYPE_CODE_ARRAY:
  269.       return convert_array (context, type);

  270.     case TYPE_CODE_STRUCT:
  271.     case TYPE_CODE_UNION:
  272.       return convert_struct_or_union (context, type);

  273.     case TYPE_CODE_ENUM:
  274.       return convert_enum (context, type);

  275.     case TYPE_CODE_FUNC:
  276.       return convert_func (context, type);

  277.     case TYPE_CODE_INT:
  278.       return convert_int (context, type);

  279.     case TYPE_CODE_FLT:
  280.       return convert_float (context, type);

  281.     case TYPE_CODE_VOID:
  282.       return convert_void (context, type);

  283.     case TYPE_CODE_BOOL:
  284.       return convert_bool (context, type);

  285.     case TYPE_CODE_COMPLEX:
  286.       return convert_complex (context, type);
  287.     }

  288.   return C_CTX (context)->c_ops->error (C_CTX (context),
  289.                                         _("cannot convert gdb type "
  290.                                           "to gcc type"));
  291. }

  292. /* See compile-internal.h.  */

  293. gcc_type
  294. convert_type (struct compile_c_instance *context, struct type *type)
  295. {
  296.   struct type_map_instance inst, *found;
  297.   gcc_type result;

  298.   /* We don't ever have to deal with typedefs in this code, because
  299.      those are only needed as symbols by the C compiler.  */
  300.   CHECK_TYPEDEF (type);

  301.   inst.type = type;
  302.   found = htab_find (context->type_map, &inst);
  303.   if (found != NULL)
  304.     return found->gcc_type;

  305.   result = convert_type_basic (context, type);
  306.   insert_type (context, type, result);
  307.   return result;
  308. }



  309. /* Delete the compiler instance C.  */

  310. static void
  311. delete_instance (struct compile_instance *c)
  312. {
  313.   struct compile_c_instance *context = (struct compile_c_instance *) c;

  314.   context->base.fe->ops->destroy (context->base.fe);
  315.   htab_delete (context->type_map);
  316.   if (context->symbol_err_map != NULL)
  317.     htab_delete (context->symbol_err_map);
  318.   xfree (context);
  319. }

  320. /* See compile-internal.h.  */

  321. struct compile_instance *
  322. new_compile_instance (struct gcc_c_context *fe)
  323. {
  324.   struct compile_c_instance *result = XCNEW (struct compile_c_instance);

  325.   result->base.fe = &fe->base;
  326.   result->base.destroy = delete_instance;
  327.   result->base.gcc_target_options = ("-std=gnu11"
  328.                                      /* Otherwise the .o file may need
  329.                                         "_Unwind_Resume" and
  330.                                         "__gcc_personality_v0".  */
  331.                                      " -fno-exceptions");

  332.   result->type_map = htab_create_alloc (10, hash_type_map_instance,
  333.                                         eq_type_map_instance,
  334.                                         xfree, xcalloc, xfree);

  335.   fe->c_ops->set_callbacks (fe, gcc_convert_symbol,
  336.                             gcc_symbol_address, result);

  337.   return &result->base;
  338. }