gdb/gdbtypes.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Support routines for manipulating internal types for GDB.

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

  3.    Contributed by Cygnus Support, using pieces from other GDB modules.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "bfd.h"
  17. #include "symtab.h"
  18. #include "symfile.h"
  19. #include "objfiles.h"
  20. #include "gdbtypes.h"
  21. #include "expression.h"
  22. #include "language.h"
  23. #include "target.h"
  24. #include "value.h"
  25. #include "demangle.h"
  26. #include "complaints.h"
  27. #include "gdbcmd.h"
  28. #include "cp-abi.h"
  29. #include "hashtab.h"
  30. #include "cp-support.h"
  31. #include "bcache.h"
  32. #include "dwarf2loc.h"
  33. #include "gdbcore.h"

  34. /* Initialize BADNESS constants.  */

  35. const struct rank LENGTH_MISMATCH_BADNESS = {100,0};

  36. const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
  37. const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};

  38. const struct rank EXACT_MATCH_BADNESS = {0,0};

  39. const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
  40. const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
  41. const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
  42. const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
  43. const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
  44. const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
  45. const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
  46. const struct rank BOOL_CONVERSION_BADNESS = {3,0};
  47. const struct rank BASE_CONVERSION_BADNESS = {2,0};
  48. const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
  49. const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
  50. const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
  51. const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};

  52. /* Floatformat pairs.  */
  53. const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
  54.   &floatformat_ieee_half_big,
  55.   &floatformat_ieee_half_little
  56. };
  57. const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
  58.   &floatformat_ieee_single_big,
  59.   &floatformat_ieee_single_little
  60. };
  61. const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
  62.   &floatformat_ieee_double_big,
  63.   &floatformat_ieee_double_little
  64. };
  65. const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
  66.   &floatformat_ieee_double_big,
  67.   &floatformat_ieee_double_littlebyte_bigword
  68. };
  69. const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
  70.   &floatformat_i387_ext,
  71.   &floatformat_i387_ext
  72. };
  73. const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
  74.   &floatformat_m68881_ext,
  75.   &floatformat_m68881_ext
  76. };
  77. const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
  78.   &floatformat_arm_ext_big,
  79.   &floatformat_arm_ext_littlebyte_bigword
  80. };
  81. const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
  82.   &floatformat_ia64_spill_big,
  83.   &floatformat_ia64_spill_little
  84. };
  85. const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
  86.   &floatformat_ia64_quad_big,
  87.   &floatformat_ia64_quad_little
  88. };
  89. const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
  90.   &floatformat_vax_f,
  91.   &floatformat_vax_f
  92. };
  93. const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
  94.   &floatformat_vax_d,
  95.   &floatformat_vax_d
  96. };
  97. const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
  98.   &floatformat_ibm_long_double_big,
  99.   &floatformat_ibm_long_double_little
  100. };

  101. /* Should opaque types be resolved?  */

  102. static int opaque_type_resolution = 1;

  103. /* A flag to enable printing of debugging information of C++
  104.    overloading.  */

  105. unsigned int overload_debug = 0;

  106. /* A flag to enable strict type checking.  */

  107. static int strict_type_checking = 1;

  108. /* A function to show whether opaque types are resolved.  */

  109. static void
  110. show_opaque_type_resolution (struct ui_file *file, int from_tty,
  111.                              struct cmd_list_element *c,
  112.                              const char *value)
  113. {
  114.   fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
  115.                             "(if set before loading symbols) is %s.\n"),
  116.                     value);
  117. }

  118. /* A function to show whether C++ overload debugging is enabled.  */

  119. static void
  120. show_overload_debug (struct ui_file *file, int from_tty,
  121.                      struct cmd_list_element *c, const char *value)
  122. {
  123.   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
  124.                     value);
  125. }

  126. /* A function to show the status of strict type checking.  */

  127. static void
  128. show_strict_type_checking (struct ui_file *file, int from_tty,
  129.                            struct cmd_list_element *c, const char *value)
  130. {
  131.   fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
  132. }


  133. /* Allocate a new OBJFILE-associated type structure and fill it
  134.    with some defaults.  Space for the type structure is allocated
  135.    on the objfile's objfile_obstack.  */

  136. struct type *
  137. alloc_type (struct objfile *objfile)
  138. {
  139.   struct type *type;

  140.   gdb_assert (objfile != NULL);

  141.   /* Alloc the structure and start off with all fields zeroed.  */
  142.   type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
  143.   TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  144.                                           struct main_type);
  145.   OBJSTAT (objfile, n_types++);

  146.   TYPE_OBJFILE_OWNED (type) = 1;
  147.   TYPE_OWNER (type).objfile = objfile;

  148.   /* Initialize the fields that might not be zero.  */

  149.   TYPE_CODE (type) = TYPE_CODE_UNDEF;
  150.   TYPE_VPTR_FIELDNO (type) = -1;
  151.   TYPE_CHAIN (type) = type;        /* Chain back to itself.  */

  152.   return type;
  153. }

  154. /* Allocate a new GDBARCH-associated type structure and fill it
  155.    with some defaults.  Space for the type structure is allocated
  156.    on the heap.  */

  157. struct type *
  158. alloc_type_arch (struct gdbarch *gdbarch)
  159. {
  160.   struct type *type;

  161.   gdb_assert (gdbarch != NULL);

  162.   /* Alloc the structure and start off with all fields zeroed.  */

  163.   type = XCNEW (struct type);
  164.   TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);

  165.   TYPE_OBJFILE_OWNED (type) = 0;
  166.   TYPE_OWNER (type).gdbarch = gdbarch;

  167.   /* Initialize the fields that might not be zero.  */

  168.   TYPE_CODE (type) = TYPE_CODE_UNDEF;
  169.   TYPE_VPTR_FIELDNO (type) = -1;
  170.   TYPE_CHAIN (type) = type;        /* Chain back to itself.  */

  171.   return type;
  172. }

  173. /* If TYPE is objfile-associated, allocate a new type structure
  174.    associated with the same objfile.  If TYPE is gdbarch-associated,
  175.    allocate a new type structure associated with the same gdbarch.  */

  176. struct type *
  177. alloc_type_copy (const struct type *type)
  178. {
  179.   if (TYPE_OBJFILE_OWNED (type))
  180.     return alloc_type (TYPE_OWNER (type).objfile);
  181.   else
  182.     return alloc_type_arch (TYPE_OWNER (type).gdbarch);
  183. }

  184. /* If TYPE is gdbarch-associated, return that architecture.
  185.    If TYPE is objfile-associated, return that objfile's architecture.  */

  186. struct gdbarch *
  187. get_type_arch (const struct type *type)
  188. {
  189.   if (TYPE_OBJFILE_OWNED (type))
  190.     return get_objfile_arch (TYPE_OWNER (type).objfile);
  191.   else
  192.     return TYPE_OWNER (type).gdbarch;
  193. }

  194. /* See gdbtypes.h.  */

  195. struct type *
  196. get_target_type (struct type *type)
  197. {
  198.   if (type != NULL)
  199.     {
  200.       type = TYPE_TARGET_TYPE (type);
  201.       if (type != NULL)
  202.         type = check_typedef (type);
  203.     }

  204.   return type;
  205. }

  206. /* Alloc a new type instance structure, fill it with some defaults,
  207.    and point it at OLDTYPE.  Allocate the new type instance from the
  208.    same place as OLDTYPE.  */

  209. static struct type *
  210. alloc_type_instance (struct type *oldtype)
  211. {
  212.   struct type *type;

  213.   /* Allocate the structure.  */

  214.   if (! TYPE_OBJFILE_OWNED (oldtype))
  215.     type = XCNEW (struct type);
  216.   else
  217.     type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
  218.                            struct type);

  219.   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);

  220.   TYPE_CHAIN (type) = type;        /* Chain back to itself for now.  */

  221.   return type;
  222. }

  223. /* Clear all remnants of the previous type at TYPE, in preparation for
  224.    replacing it with something else.  Preserve owner information.  */

  225. static void
  226. smash_type (struct type *type)
  227. {
  228.   int objfile_owned = TYPE_OBJFILE_OWNED (type);
  229.   union type_owner owner = TYPE_OWNER (type);

  230.   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));

  231.   /* Restore owner information.  */
  232.   TYPE_OBJFILE_OWNED (type) = objfile_owned;
  233.   TYPE_OWNER (type) = owner;

  234.   /* For now, delete the rings.  */
  235.   TYPE_CHAIN (type) = type;

  236.   /* For now, leave the pointer/reference types alone.  */
  237. }

  238. /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
  239.    to a pointer to memory where the pointer type should be stored.
  240.    If *TYPEPTR is zero, update it to point to the pointer type we return.
  241.    We allocate new memory if needed.  */

  242. struct type *
  243. make_pointer_type (struct type *type, struct type **typeptr)
  244. {
  245.   struct type *ntype;        /* New type */
  246.   struct type *chain;

  247.   ntype = TYPE_POINTER_TYPE (type);

  248.   if (ntype)
  249.     {
  250.       if (typeptr == 0)
  251.         return ntype;                /* Don't care about alloc,
  252.                                    and have new type.  */
  253.       else if (*typeptr == 0)
  254.         {
  255.           *typeptr = ntype;        /* Tracking alloc, and have new type.  */
  256.           return ntype;
  257.         }
  258.     }

  259.   if (typeptr == 0 || *typeptr == 0)        /* We'll need to allocate one.  */
  260.     {
  261.       ntype = alloc_type_copy (type);
  262.       if (typeptr)
  263.         *typeptr = ntype;
  264.     }
  265.   else                        /* We have storage, but need to reset it.  */
  266.     {
  267.       ntype = *typeptr;
  268.       chain = TYPE_CHAIN (ntype);
  269.       smash_type (ntype);
  270.       TYPE_CHAIN (ntype) = chain;
  271.     }

  272.   TYPE_TARGET_TYPE (ntype) = type;
  273.   TYPE_POINTER_TYPE (type) = ntype;

  274.   /* FIXME!  Assumes the machine has only one representation for pointers!  */

  275.   TYPE_LENGTH (ntype)
  276.     = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
  277.   TYPE_CODE (ntype) = TYPE_CODE_PTR;

  278.   /* Mark pointers as unsigned.  The target converts between pointers
  279.      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
  280.      gdbarch_address_to_pointer.  */
  281.   TYPE_UNSIGNED (ntype) = 1;

  282.   /* Update the length of all the other variants of this type.  */
  283.   chain = TYPE_CHAIN (ntype);
  284.   while (chain != ntype)
  285.     {
  286.       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
  287.       chain = TYPE_CHAIN (chain);
  288.     }

  289.   return ntype;
  290. }

  291. /* Given a type TYPE, return a type of pointers to that type.
  292.    May need to construct such a type if this is the first use.  */

  293. struct type *
  294. lookup_pointer_type (struct type *type)
  295. {
  296.   return make_pointer_type (type, (struct type **) 0);
  297. }

  298. /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero,
  299.    points to a pointer to memory where the reference type should be
  300.    stored.  If *TYPEPTR is zero, update it to point to the reference
  301.    type we return.  We allocate new memory if needed.  */

  302. struct type *
  303. make_reference_type (struct type *type, struct type **typeptr)
  304. {
  305.   struct type *ntype;        /* New type */
  306.   struct type *chain;

  307.   ntype = TYPE_REFERENCE_TYPE (type);

  308.   if (ntype)
  309.     {
  310.       if (typeptr == 0)
  311.         return ntype;                /* Don't care about alloc,
  312.                                    and have new type.  */
  313.       else if (*typeptr == 0)
  314.         {
  315.           *typeptr = ntype;        /* Tracking alloc, and have new type.  */
  316.           return ntype;
  317.         }
  318.     }

  319.   if (typeptr == 0 || *typeptr == 0)        /* We'll need to allocate one.  */
  320.     {
  321.       ntype = alloc_type_copy (type);
  322.       if (typeptr)
  323.         *typeptr = ntype;
  324.     }
  325.   else                        /* We have storage, but need to reset it.  */
  326.     {
  327.       ntype = *typeptr;
  328.       chain = TYPE_CHAIN (ntype);
  329.       smash_type (ntype);
  330.       TYPE_CHAIN (ntype) = chain;
  331.     }

  332.   TYPE_TARGET_TYPE (ntype) = type;
  333.   TYPE_REFERENCE_TYPE (type) = ntype;

  334.   /* FIXME!  Assume the machine has only one representation for
  335.      references, and that it matches the (only) representation for
  336.      pointers!  */

  337.   TYPE_LENGTH (ntype) =
  338.     gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
  339.   TYPE_CODE (ntype) = TYPE_CODE_REF;

  340.   if (!TYPE_REFERENCE_TYPE (type))        /* Remember it, if don't have one.  */
  341.     TYPE_REFERENCE_TYPE (type) = ntype;

  342.   /* Update the length of all the other variants of this type.  */
  343.   chain = TYPE_CHAIN (ntype);
  344.   while (chain != ntype)
  345.     {
  346.       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
  347.       chain = TYPE_CHAIN (chain);
  348.     }

  349.   return ntype;
  350. }

  351. /* Same as above, but caller doesn't care about memory allocation
  352.    details.  */

  353. struct type *
  354. lookup_reference_type (struct type *type)
  355. {
  356.   return make_reference_type (type, (struct type **) 0);
  357. }

  358. /* Lookup a function type that returns type TYPE.  TYPEPTR, if
  359.    nonzero, points to a pointer to memory where the function type
  360.    should be stored.  If *TYPEPTR is zero, update it to point to the
  361.    function type we return.  We allocate new memory if needed.  */

  362. struct type *
  363. make_function_type (struct type *type, struct type **typeptr)
  364. {
  365.   struct type *ntype;        /* New type */

  366.   if (typeptr == 0 || *typeptr == 0)        /* We'll need to allocate one.  */
  367.     {
  368.       ntype = alloc_type_copy (type);
  369.       if (typeptr)
  370.         *typeptr = ntype;
  371.     }
  372.   else                        /* We have storage, but need to reset it.  */
  373.     {
  374.       ntype = *typeptr;
  375.       smash_type (ntype);
  376.     }

  377.   TYPE_TARGET_TYPE (ntype) = type;

  378.   TYPE_LENGTH (ntype) = 1;
  379.   TYPE_CODE (ntype) = TYPE_CODE_FUNC;

  380.   INIT_FUNC_SPECIFIC (ntype);

  381.   return ntype;
  382. }

  383. /* Given a type TYPE, return a type of functions that return that type.
  384.    May need to construct such a type if this is the first use.  */

  385. struct type *
  386. lookup_function_type (struct type *type)
  387. {
  388.   return make_function_type (type, (struct type **) 0);
  389. }

  390. /* Given a type TYPE and argument types, return the appropriate
  391.    function type.  If the final type in PARAM_TYPES is NULL, make a
  392.    varargs function.  */

  393. struct type *
  394. lookup_function_type_with_arguments (struct type *type,
  395.                                      int nparams,
  396.                                      struct type **param_types)
  397. {
  398.   struct type *fn = make_function_type (type, (struct type **) 0);
  399.   int i;

  400.   if (nparams > 0)
  401.     {
  402.       if (param_types[nparams - 1] == NULL)
  403.         {
  404.           --nparams;
  405.           TYPE_VARARGS (fn) = 1;
  406.         }
  407.       else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
  408.                == TYPE_CODE_VOID)
  409.         {
  410.           --nparams;
  411.           /* Caller should have ensured this.  */
  412.           gdb_assert (nparams == 0);
  413.           TYPE_PROTOTYPED (fn) = 1;
  414.         }
  415.     }

  416.   TYPE_NFIELDS (fn) = nparams;
  417.   TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
  418.   for (i = 0; i < nparams; ++i)
  419.     TYPE_FIELD_TYPE (fn, i) = param_types[i];

  420.   return fn;
  421. }

  422. /* Identify address space identifier by name --
  423.    return the integer flag defined in gdbtypes.h.  */

  424. int
  425. address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
  426. {
  427.   int type_flags;

  428.   /* Check for known address space delimiters.  */
  429.   if (!strcmp (space_identifier, "code"))
  430.     return TYPE_INSTANCE_FLAG_CODE_SPACE;
  431.   else if (!strcmp (space_identifier, "data"))
  432.     return TYPE_INSTANCE_FLAG_DATA_SPACE;
  433.   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
  434.            && gdbarch_address_class_name_to_type_flags (gdbarch,
  435.                                                         space_identifier,
  436.                                                         &type_flags))
  437.     return type_flags;
  438.   else
  439.     error (_("Unknown address space specifier: \"%s\""), space_identifier);
  440. }

  441. /* Identify address space identifier by integer flag as defined in
  442.    gdbtypes.h -- return the string version of the adress space name.  */

  443. const char *
  444. address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
  445. {
  446.   if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
  447.     return "code";
  448.   else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
  449.     return "data";
  450.   else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
  451.            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
  452.     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
  453.   else
  454.     return NULL;
  455. }

  456. /* Create a new type with instance flags NEW_FLAGS, based on TYPE.

  457.    If STORAGE is non-NULL, create the new type instance there.
  458.    STORAGE must be in the same obstack as TYPE.  */

  459. static struct type *
  460. make_qualified_type (struct type *type, int new_flags,
  461.                      struct type *storage)
  462. {
  463.   struct type *ntype;

  464.   ntype = type;
  465.   do
  466.     {
  467.       if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
  468.         return ntype;
  469.       ntype = TYPE_CHAIN (ntype);
  470.     }
  471.   while (ntype != type);

  472.   /* Create a new type instance.  */
  473.   if (storage == NULL)
  474.     ntype = alloc_type_instance (type);
  475.   else
  476.     {
  477.       /* If STORAGE was provided, it had better be in the same objfile
  478.          as TYPE.  Otherwise, we can't link it into TYPE's cv chain:
  479.          if one objfile is freed and the other kept, we'd have
  480.          dangling pointers.  */
  481.       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));

  482.       ntype = storage;
  483.       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
  484.       TYPE_CHAIN (ntype) = ntype;
  485.     }

  486.   /* Pointers or references to the original type are not relevant to
  487.      the new type.  */
  488.   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
  489.   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;

  490.   /* Chain the new qualified type to the old type.  */
  491.   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
  492.   TYPE_CHAIN (type) = ntype;

  493.   /* Now set the instance flags and return the new type.  */
  494.   TYPE_INSTANCE_FLAGS (ntype) = new_flags;

  495.   /* Set length of new type to that of the original type.  */
  496.   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);

  497.   return ntype;
  498. }

  499. /* Make an address-space-delimited variant of a type -- a type that
  500.    is identical to the one supplied except that it has an address
  501.    space attribute attached to it (such as "code" or "data").

  502.    The space attributes "code" and "data" are for Harvard
  503.    architectures.  The address space attributes are for architectures
  504.    which have alternately sized pointers or pointers with alternate
  505.    representations.  */

  506. struct type *
  507. make_type_with_address_space (struct type *type, int space_flag)
  508. {
  509.   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
  510.                     & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
  511.                         | TYPE_INSTANCE_FLAG_DATA_SPACE
  512.                         | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
  513.                    | space_flag);

  514.   return make_qualified_type (type, new_flags, NULL);
  515. }

  516. /* Make a "c-v" variant of a type -- a type that is identical to the
  517.    one supplied except that it may have const or volatile attributes
  518.    CNST is a flag for setting the const attribute
  519.    VOLTL is a flag for setting the volatile attribute
  520.    TYPE is the base type whose variant we are creating.

  521.    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
  522.    storage to hold the new qualified type; *TYPEPTR and TYPE must be
  523.    in the same objfile.  Otherwise, allocate fresh memory for the new
  524.    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
  525.    new type we construct.  */

  526. struct type *
  527. make_cv_type (int cnst, int voltl,
  528.               struct type *type,
  529.               struct type **typeptr)
  530. {
  531.   struct type *ntype;        /* New type */

  532.   int new_flags = (TYPE_INSTANCE_FLAGS (type)
  533.                    & ~(TYPE_INSTANCE_FLAG_CONST
  534.                        | TYPE_INSTANCE_FLAG_VOLATILE));

  535.   if (cnst)
  536.     new_flags |= TYPE_INSTANCE_FLAG_CONST;

  537.   if (voltl)
  538.     new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;

  539.   if (typeptr && *typeptr != NULL)
  540.     {
  541.       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
  542.          a C-V variant chain that threads across objfiles: if one
  543.          objfile gets freed, then the other has a broken C-V chain.

  544.          This code used to try to copy over the main type from TYPE to
  545.          *TYPEPTR if they were in different objfiles, but that's
  546.          wrong, too: TYPE may have a field list or member function
  547.          lists, which refer to types of their own, etc. etc.  The
  548.          whole shebang would need to be copied over recursively; you
  549.          can't have inter-objfile pointers.  The only thing to do is
  550.          to leave stub types as stub types, and look them up afresh by
  551.          name each time you encounter them.  */
  552.       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
  553.     }

  554.   ntype = make_qualified_type (type, new_flags,
  555.                                typeptr ? *typeptr : NULL);

  556.   if (typeptr != NULL)
  557.     *typeptr = ntype;

  558.   return ntype;
  559. }

  560. /* Make a 'restrict'-qualified version of TYPE.  */

  561. struct type *
  562. make_restrict_type (struct type *type)
  563. {
  564.   return make_qualified_type (type,
  565.                               (TYPE_INSTANCE_FLAGS (type)
  566.                                | TYPE_INSTANCE_FLAG_RESTRICT),
  567.                               NULL);
  568. }

  569. /* Make a type without const, volatile, or restrict.  */

  570. struct type *
  571. make_unqualified_type (struct type *type)
  572. {
  573.   return make_qualified_type (type,
  574.                               (TYPE_INSTANCE_FLAGS (type)
  575.                                & ~(TYPE_INSTANCE_FLAG_CONST
  576.                                    | TYPE_INSTANCE_FLAG_VOLATILE
  577.                                    | TYPE_INSTANCE_FLAG_RESTRICT)),
  578.                               NULL);
  579. }

  580. /* Replace the contents of ntype with the type *type.  This changes the
  581.    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
  582.    the changes are propogated to all types in the TYPE_CHAIN.

  583.    In order to build recursive types, it's inevitable that we'll need
  584.    to update types in place --- but this sort of indiscriminate
  585.    smashing is ugly, and needs to be replaced with something more
  586.    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
  587.    clear if more steps are needed.  */

  588. void
  589. replace_type (struct type *ntype, struct type *type)
  590. {
  591.   struct type *chain;

  592.   /* These two types had better be in the same objfile.  Otherwise,
  593.      the assignment of one type's main type structure to the other
  594.      will produce a type with references to objects (names; field
  595.      lists; etc.) allocated on an objfile other than its own.  */
  596.   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));

  597.   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);

  598.   /* The type length is not a part of the main type.  Update it for
  599.      each type on the variant chain.  */
  600.   chain = ntype;
  601.   do
  602.     {
  603.       /* Assert that this element of the chain has no address-class bits
  604.          set in its flags.  Such type variants might have type lengths
  605.          which are supposed to be different from the non-address-class
  606.          variants.  This assertion shouldn't ever be triggered because
  607.          symbol readers which do construct address-class variants don't
  608.          call replace_type().  */
  609.       gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);

  610.       TYPE_LENGTH (chain) = TYPE_LENGTH (type);
  611.       chain = TYPE_CHAIN (chain);
  612.     }
  613.   while (ntype != chain);

  614.   /* Assert that the two types have equivalent instance qualifiers.
  615.      This should be true for at least all of our debug readers.  */
  616.   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
  617. }

  618. /* Implement direct support for MEMBER_TYPE in GNU C++.
  619.    May need to construct such a type if this is the first use.
  620.    The TYPE is the type of the member.  The DOMAIN is the type
  621.    of the aggregate that the member belongs to.  */

  622. struct type *
  623. lookup_memberptr_type (struct type *type, struct type *domain)
  624. {
  625.   struct type *mtype;

  626.   mtype = alloc_type_copy (type);
  627.   smash_to_memberptr_type (mtype, domain, type);
  628.   return mtype;
  629. }

  630. /* Return a pointer-to-method type, for a method of type TO_TYPE.  */

  631. struct type *
  632. lookup_methodptr_type (struct type *to_type)
  633. {
  634.   struct type *mtype;

  635.   mtype = alloc_type_copy (to_type);
  636.   smash_to_methodptr_type (mtype, to_type);
  637.   return mtype;
  638. }

  639. /* Allocate a stub method whose return type is TYPE.  This apparently
  640.    happens for speed of symbol reading, since parsing out the
  641.    arguments to the method is cpu-intensive, the way we are doing it.
  642.    So, we will fill in arguments later.  This always returns a fresh
  643.    type.  */

  644. struct type *
  645. allocate_stub_method (struct type *type)
  646. {
  647.   struct type *mtype;

  648.   mtype = alloc_type_copy (type);
  649.   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
  650.   TYPE_LENGTH (mtype) = 1;
  651.   TYPE_STUB (mtype) = 1;
  652.   TYPE_TARGET_TYPE (mtype) = type;
  653.   /*  _DOMAIN_TYPE (mtype) = unknown yet */
  654.   return mtype;
  655. }

  656. /* Create a range type with a dynamic range from LOW_BOUND to
  657.    HIGH_BOUND, inclusive.  See create_range_type for further details. */

  658. struct type *
  659. create_range_type (struct type *result_type, struct type *index_type,
  660.                    const struct dynamic_prop *low_bound,
  661.                    const struct dynamic_prop *high_bound)
  662. {
  663.   if (result_type == NULL)
  664.     result_type = alloc_type_copy (index_type);
  665.   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
  666.   TYPE_TARGET_TYPE (result_type) = index_type;
  667.   if (TYPE_STUB (index_type))
  668.     TYPE_TARGET_STUB (result_type) = 1;
  669.   else
  670.     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));

  671.   TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
  672.     TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
  673.   TYPE_RANGE_DATA (result_type)->low = *low_bound;
  674.   TYPE_RANGE_DATA (result_type)->high = *high_bound;

  675.   if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
  676.     TYPE_UNSIGNED (result_type) = 1;

  677.   /* Ada allows the declaration of range types whose upper bound is
  678.      less than the lower bound, so checking the lower bound is not
  679.      enough.  Make sure we do not mark a range type whose upper bound
  680.      is negative as unsigned.  */
  681.   if (high_bound->kind == PROP_CONST && high_bound->data.const_val < 0)
  682.     TYPE_UNSIGNED (result_type) = 0;

  683.   return result_type;
  684. }

  685. /* Create a range type using either a blank type supplied in
  686.    RESULT_TYPE, or creating a new type, inheriting the objfile from
  687.    INDEX_TYPE.

  688.    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
  689.    to HIGH_BOUND, inclusive.

  690.    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  691.    sure it is TYPE_CODE_UNDEF before we bash it into a range type?  */

  692. struct type *
  693. create_static_range_type (struct type *result_type, struct type *index_type,
  694.                           LONGEST low_bound, LONGEST high_bound)
  695. {
  696.   struct dynamic_prop low, high;

  697.   low.kind = PROP_CONST;
  698.   low.data.const_val = low_bound;

  699.   high.kind = PROP_CONST;
  700.   high.data.const_val = high_bound;

  701.   result_type = create_range_type (result_type, index_type, &low, &high);

  702.   return result_type;
  703. }

  704. /* Predicate tests whether BOUNDS are static.  Returns 1 if all bounds values
  705.    are static, otherwise returns 0.  */

  706. static int
  707. has_static_range (const struct range_bounds *bounds)
  708. {
  709.   return (bounds->low.kind == PROP_CONST
  710.           && bounds->high.kind == PROP_CONST);
  711. }


  712. /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
  713.    TYPE.  Return 1 if type is a range type, 0 if it is discrete (and
  714.    bounds will fit in LONGEST), or -1 otherwise.  */

  715. int
  716. get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
  717. {
  718.   CHECK_TYPEDEF (type);
  719.   switch (TYPE_CODE (type))
  720.     {
  721.     case TYPE_CODE_RANGE:
  722.       *lowp = TYPE_LOW_BOUND (type);
  723.       *highp = TYPE_HIGH_BOUND (type);
  724.       return 1;
  725.     case TYPE_CODE_ENUM:
  726.       if (TYPE_NFIELDS (type) > 0)
  727.         {
  728.           /* The enums may not be sorted by value, so search all
  729.              entries.  */
  730.           int i;

  731.           *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
  732.           for (i = 0; i < TYPE_NFIELDS (type); i++)
  733.             {
  734.               if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
  735.                 *lowp = TYPE_FIELD_ENUMVAL (type, i);
  736.               if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
  737.                 *highp = TYPE_FIELD_ENUMVAL (type, i);
  738.             }

  739.           /* Set unsigned indicator if warranted.  */
  740.           if (*lowp >= 0)
  741.             {
  742.               TYPE_UNSIGNED (type) = 1;
  743.             }
  744.         }
  745.       else
  746.         {
  747.           *lowp = 0;
  748.           *highp = -1;
  749.         }
  750.       return 0;
  751.     case TYPE_CODE_BOOL:
  752.       *lowp = 0;
  753.       *highp = 1;
  754.       return 0;
  755.     case TYPE_CODE_INT:
  756.       if (TYPE_LENGTH (type) > sizeof (LONGEST))        /* Too big */
  757.         return -1;
  758.       if (!TYPE_UNSIGNED (type))
  759.         {
  760.           *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
  761.           *highp = -*lowp - 1;
  762.           return 0;
  763.         }
  764.       /* ... fall through for unsigned ints ...  */
  765.     case TYPE_CODE_CHAR:
  766.       *lowp = 0;
  767.       /* This round-about calculation is to avoid shifting by
  768.          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
  769.          if TYPE_LENGTH (type) == sizeof (LONGEST).  */
  770.       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
  771.       *highp = (*highp - 1) | *highp;
  772.       return 0;
  773.     default:
  774.       return -1;
  775.     }
  776. }

  777. /* Assuming TYPE is a simple, non-empty array type, compute its upper
  778.    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
  779.    Save the high bound into HIGH_BOUND if not NULL.

  780.    Return 1 if the operation was successful.  Return zero otherwise,
  781.    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.

  782.    We now simply use get_discrete_bounds call to get the values
  783.    of the low and high bounds.
  784.    get_discrete_bounds can return three values:
  785.    1, meaning that index is a range,
  786.    0, meaning that index is a discrete type,
  787.    or -1 for failure.  */

  788. int
  789. get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
  790. {
  791.   struct type *index = TYPE_INDEX_TYPE (type);
  792.   LONGEST low = 0;
  793.   LONGEST high = 0;
  794.   int res;

  795.   if (index == NULL)
  796.     return 0;

  797.   res = get_discrete_bounds (index, &low, &high);
  798.   if (res == -1)
  799.     return 0;

  800.   /* Check if the array bounds are undefined.  */
  801.   if (res == 1
  802.       && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
  803.           || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
  804.     return 0;

  805.   if (low_bound)
  806.     *low_bound = low;

  807.   if (high_bound)
  808.     *high_bound = high;

  809.   return 1;
  810. }

  811. /* Create an array type using either a blank type supplied in
  812.    RESULT_TYPE, or creating a new type, inheriting the objfile from
  813.    RANGE_TYPE.

  814.    Elements will be of type ELEMENT_TYPE, the indices will be of type
  815.    RANGE_TYPE.

  816.    If BIT_STRIDE is not zero, build a packed array type whose element
  817.    size is BIT_STRIDE.  Otherwise, ignore this parameter.

  818.    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  819.    sure it is TYPE_CODE_UNDEF before we bash it into an array
  820.    type?  */

  821. struct type *
  822. create_array_type_with_stride (struct type *result_type,
  823.                                struct type *element_type,
  824.                                struct type *range_type,
  825.                                unsigned int bit_stride)
  826. {
  827.   if (result_type == NULL)
  828.     result_type = alloc_type_copy (range_type);

  829.   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
  830.   TYPE_TARGET_TYPE (result_type) = element_type;
  831.   if (has_static_range (TYPE_RANGE_DATA (range_type)))
  832.     {
  833.       LONGEST low_bound, high_bound;

  834.       if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
  835.         low_bound = high_bound = 0;
  836.       CHECK_TYPEDEF (element_type);
  837.       /* Be careful when setting the array length.  Ada arrays can be
  838.          empty arrays with the high_bound being smaller than the low_bound.
  839.          In such cases, the array length should be zero.  */
  840.       if (high_bound < low_bound)
  841.         TYPE_LENGTH (result_type) = 0;
  842.       else if (bit_stride > 0)
  843.         TYPE_LENGTH (result_type) =
  844.           (bit_stride * (high_bound - low_bound + 1) + 7) / 8;
  845.       else
  846.         TYPE_LENGTH (result_type) =
  847.           TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
  848.     }
  849.   else
  850.     {
  851.       /* This type is dynamic and its length needs to be computed
  852.          on demand.  In the meantime, avoid leaving the TYPE_LENGTH
  853.          undefined by setting it to zero.  Although we are not expected
  854.          to trust TYPE_LENGTH in this case, setting the size to zero
  855.          allows us to avoid allocating objects of random sizes in case
  856.          we accidently do.  */
  857.       TYPE_LENGTH (result_type) = 0;
  858.     }

  859.   TYPE_NFIELDS (result_type) = 1;
  860.   TYPE_FIELDS (result_type) =
  861.     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
  862.   TYPE_INDEX_TYPE (result_type) = range_type;
  863.   TYPE_VPTR_FIELDNO (result_type) = -1;
  864.   if (bit_stride > 0)
  865.     TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;

  866.   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
  867.   if (TYPE_LENGTH (result_type) == 0)
  868.     TYPE_TARGET_STUB (result_type) = 1;

  869.   return result_type;
  870. }

  871. /* Same as create_array_type_with_stride but with no bit_stride
  872.    (BIT_STRIDE = 0), thus building an unpacked array.  */

  873. struct type *
  874. create_array_type (struct type *result_type,
  875.                    struct type *element_type,
  876.                    struct type *range_type)
  877. {
  878.   return create_array_type_with_stride (result_type, element_type,
  879.                                         range_type, 0);
  880. }

  881. struct type *
  882. lookup_array_range_type (struct type *element_type,
  883.                          LONGEST low_bound, LONGEST high_bound)
  884. {
  885.   struct gdbarch *gdbarch = get_type_arch (element_type);
  886.   struct type *index_type = builtin_type (gdbarch)->builtin_int;
  887.   struct type *range_type
  888.     = create_static_range_type (NULL, index_type, low_bound, high_bound);

  889.   return create_array_type (NULL, element_type, range_type);
  890. }

  891. /* Create a string type using either a blank type supplied in
  892.    RESULT_TYPE, or creating a new type.  String types are similar
  893.    enough to array of char types that we can use create_array_type to
  894.    build the basic type and then bash it into a string type.

  895.    For fixed length strings, the range type contains 0 as the lower
  896.    bound and the length of the string minus one as the upper bound.

  897.    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  898.    sure it is TYPE_CODE_UNDEF before we bash it into a string
  899.    type?  */

  900. struct type *
  901. create_string_type (struct type *result_type,
  902.                     struct type *string_char_type,
  903.                     struct type *range_type)
  904. {
  905.   result_type = create_array_type (result_type,
  906.                                    string_char_type,
  907.                                    range_type);
  908.   TYPE_CODE (result_type) = TYPE_CODE_STRING;
  909.   return result_type;
  910. }

  911. struct type *
  912. lookup_string_range_type (struct type *string_char_type,
  913.                           LONGEST low_bound, LONGEST high_bound)
  914. {
  915.   struct type *result_type;

  916.   result_type = lookup_array_range_type (string_char_type,
  917.                                          low_bound, high_bound);
  918.   TYPE_CODE (result_type) = TYPE_CODE_STRING;
  919.   return result_type;
  920. }

  921. struct type *
  922. create_set_type (struct type *result_type, struct type *domain_type)
  923. {
  924.   if (result_type == NULL)
  925.     result_type = alloc_type_copy (domain_type);

  926.   TYPE_CODE (result_type) = TYPE_CODE_SET;
  927.   TYPE_NFIELDS (result_type) = 1;
  928.   TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));

  929.   if (!TYPE_STUB (domain_type))
  930.     {
  931.       LONGEST low_bound, high_bound, bit_length;

  932.       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
  933.         low_bound = high_bound = 0;
  934.       bit_length = high_bound - low_bound + 1;
  935.       TYPE_LENGTH (result_type)
  936.         = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
  937.       if (low_bound >= 0)
  938.         TYPE_UNSIGNED (result_type) = 1;
  939.     }
  940.   TYPE_FIELD_TYPE (result_type, 0) = domain_type;

  941.   return result_type;
  942. }

  943. /* Convert ARRAY_TYPE to a vector type.  This may modify ARRAY_TYPE
  944.    and any array types nested inside it.  */

  945. void
  946. make_vector_type (struct type *array_type)
  947. {
  948.   struct type *inner_array, *elt_type;
  949.   int flags;

  950.   /* Find the innermost array type, in case the array is
  951.      multi-dimensional.  */
  952.   inner_array = array_type;
  953.   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
  954.     inner_array = TYPE_TARGET_TYPE (inner_array);

  955.   elt_type = TYPE_TARGET_TYPE (inner_array);
  956.   if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
  957.     {
  958.       flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
  959.       elt_type = make_qualified_type (elt_type, flags, NULL);
  960.       TYPE_TARGET_TYPE (inner_array) = elt_type;
  961.     }

  962.   TYPE_VECTOR (array_type) = 1;
  963. }

  964. struct type *
  965. init_vector_type (struct type *elt_type, int n)
  966. {
  967.   struct type *array_type;

  968.   array_type = lookup_array_range_type (elt_type, 0, n - 1);
  969.   make_vector_type (array_type);
  970.   return array_type;
  971. }

  972. /* Smash TYPE to be a type of pointers to members of DOMAIN with type
  973.    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
  974.    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
  975.    TYPE doesn't include the offset (that's the value of the MEMBER
  976.    itself), but does include the structure type into which it points
  977.    (for some reason).

  978.    When "smashing" the type, we preserve the objfile that the old type
  979.    pointed to, since we aren't changing where the type is actually
  980.    allocated.  */

  981. void
  982. smash_to_memberptr_type (struct type *type, struct type *domain,
  983.                          struct type *to_type)
  984. {
  985.   smash_type (type);
  986.   TYPE_TARGET_TYPE (type) = to_type;
  987.   TYPE_DOMAIN_TYPE (type) = domain;
  988.   /* Assume that a data member pointer is the same size as a normal
  989.      pointer.  */
  990.   TYPE_LENGTH (type)
  991.     = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
  992.   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
  993. }

  994. /* Smash TYPE to be a type of pointer to methods type TO_TYPE.

  995.    When "smashing" the type, we preserve the objfile that the old type
  996.    pointed to, since we aren't changing where the type is actually
  997.    allocated.  */

  998. void
  999. smash_to_methodptr_type (struct type *type, struct type *to_type)
  1000. {
  1001.   smash_type (type);
  1002.   TYPE_TARGET_TYPE (type) = to_type;
  1003.   TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
  1004.   TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
  1005.   TYPE_CODE (type) = TYPE_CODE_METHODPTR;
  1006. }

  1007. /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
  1008.    METHOD just means `function that gets an extra "this" argument'.

  1009.    When "smashing" the type, we preserve the objfile that the old type
  1010.    pointed to, since we aren't changing where the type is actually
  1011.    allocated.  */

  1012. void
  1013. smash_to_method_type (struct type *type, struct type *domain,
  1014.                       struct type *to_type, struct field *args,
  1015.                       int nargs, int varargs)
  1016. {
  1017.   smash_type (type);
  1018.   TYPE_TARGET_TYPE (type) = to_type;
  1019.   TYPE_DOMAIN_TYPE (type) = domain;
  1020.   TYPE_FIELDS (type) = args;
  1021.   TYPE_NFIELDS (type) = nargs;
  1022.   if (varargs)
  1023.     TYPE_VARARGS (type) = 1;
  1024.   TYPE_LENGTH (type) = 1;        /* In practice, this is never needed.  */
  1025.   TYPE_CODE (type) = TYPE_CODE_METHOD;
  1026. }

  1027. /* Return a typename for a struct/union/enum type without "struct ",
  1028.    "union ", or "enum ".  If the type has a NULL name, return NULL.  */

  1029. const char *
  1030. type_name_no_tag (const struct type *type)
  1031. {
  1032.   if (TYPE_TAG_NAME (type) != NULL)
  1033.     return TYPE_TAG_NAME (type);

  1034.   /* Is there code which expects this to return the name if there is
  1035.      no tag name?  My guess is that this is mainly used for C++ in
  1036.      cases where the two will always be the same.  */
  1037.   return TYPE_NAME (type);
  1038. }

  1039. /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
  1040.    Since GCC PR debug/47510 DWARF provides associated information to detect the
  1041.    anonymous class linkage name from its typedef.

  1042.    Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
  1043.    apply it itself.  */

  1044. const char *
  1045. type_name_no_tag_or_error (struct type *type)
  1046. {
  1047.   struct type *saved_type = type;
  1048.   const char *name;
  1049.   struct objfile *objfile;

  1050.   CHECK_TYPEDEF (type);

  1051.   name = type_name_no_tag (type);
  1052.   if (name != NULL)
  1053.     return name;

  1054.   name = type_name_no_tag (saved_type);
  1055.   objfile = TYPE_OBJFILE (saved_type);
  1056.   error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
  1057.          name ? name : "<anonymous>",
  1058.          objfile ? objfile_name (objfile) : "<arch>");
  1059. }

  1060. /* Lookup a typedef or primitive type named NAME, visible in lexical
  1061.    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
  1062.    suitably defined.  */

  1063. struct type *
  1064. lookup_typename (const struct language_defn *language,
  1065.                  struct gdbarch *gdbarch, const char *name,
  1066.                  const struct block *block, int noerr)
  1067. {
  1068.   struct symbol *sym;
  1069.   struct type *type;

  1070.   sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
  1071.                                    language->la_language, NULL);
  1072.   if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1073.     return SYMBOL_TYPE (sym);

  1074.   if (noerr)
  1075.     return NULL;
  1076.   error (_("No type named %s."), name);
  1077. }

  1078. struct type *
  1079. lookup_unsigned_typename (const struct language_defn *language,
  1080.                           struct gdbarch *gdbarch, const char *name)
  1081. {
  1082.   char *uns = alloca (strlen (name) + 10);

  1083.   strcpy (uns, "unsigned ");
  1084.   strcpy (uns + 9, name);
  1085.   return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
  1086. }

  1087. struct type *
  1088. lookup_signed_typename (const struct language_defn *language,
  1089.                         struct gdbarch *gdbarch, const char *name)
  1090. {
  1091.   struct type *t;
  1092.   char *uns = alloca (strlen (name) + 8);

  1093.   strcpy (uns, "signed ");
  1094.   strcpy (uns + 7, name);
  1095.   t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
  1096.   /* If we don't find "signed FOO" just try again with plain "FOO".  */
  1097.   if (t != NULL)
  1098.     return t;
  1099.   return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
  1100. }

  1101. /* Lookup a structure type named "struct NAME",
  1102.    visible in lexical block BLOCK.  */

  1103. struct type *
  1104. lookup_struct (const char *name, const struct block *block)
  1105. {
  1106.   struct symbol *sym;

  1107.   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);

  1108.   if (sym == NULL)
  1109.     {
  1110.       error (_("No struct type named %s."), name);
  1111.     }
  1112.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  1113.     {
  1114.       error (_("This context has class, union or enum %s, not a struct."),
  1115.              name);
  1116.     }
  1117.   return (SYMBOL_TYPE (sym));
  1118. }

  1119. /* Lookup a union type named "union NAME",
  1120.    visible in lexical block BLOCK.  */

  1121. struct type *
  1122. lookup_union (const char *name, const struct block *block)
  1123. {
  1124.   struct symbol *sym;
  1125.   struct type *t;

  1126.   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);

  1127.   if (sym == NULL)
  1128.     error (_("No union type named %s."), name);

  1129.   t = SYMBOL_TYPE (sym);

  1130.   if (TYPE_CODE (t) == TYPE_CODE_UNION)
  1131.     return t;

  1132.   /* If we get here, it's not a union.  */
  1133.   error (_("This context has class, struct or enum %s, not a union."),
  1134.          name);
  1135. }

  1136. /* Lookup an enum type named "enum NAME",
  1137.    visible in lexical block BLOCK.  */

  1138. struct type *
  1139. lookup_enum (const char *name, const struct block *block)
  1140. {
  1141.   struct symbol *sym;

  1142.   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
  1143.   if (sym == NULL)
  1144.     {
  1145.       error (_("No enum type named %s."), name);
  1146.     }
  1147.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
  1148.     {
  1149.       error (_("This context has class, struct or union %s, not an enum."),
  1150.              name);
  1151.     }
  1152.   return (SYMBOL_TYPE (sym));
  1153. }

  1154. /* Lookup a template type named "template NAME<TYPE>",
  1155.    visible in lexical block BLOCK.  */

  1156. struct type *
  1157. lookup_template_type (char *name, struct type *type,
  1158.                       const struct block *block)
  1159. {
  1160.   struct symbol *sym;
  1161.   char *nam = (char *)
  1162.     alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);

  1163.   strcpy (nam, name);
  1164.   strcat (nam, "<");
  1165.   strcat (nam, TYPE_NAME (type));
  1166.   strcat (nam, " >");        /* FIXME, extra space still introduced in gcc?  */

  1167.   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);

  1168.   if (sym == NULL)
  1169.     {
  1170.       error (_("No template type named %s."), name);
  1171.     }
  1172.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  1173.     {
  1174.       error (_("This context has class, union or enum %s, not a struct."),
  1175.              name);
  1176.     }
  1177.   return (SYMBOL_TYPE (sym));
  1178. }

  1179. /* Given a type TYPE, lookup the type of the component of type named
  1180.    NAME.

  1181.    TYPE can be either a struct or union, or a pointer or reference to
  1182.    a struct or union.  If it is a pointer or reference, its target
  1183.    type is automatically used.  Thus '.' and '->' are interchangable,
  1184.    as specified for the definitions of the expression element types
  1185.    STRUCTOP_STRUCT and STRUCTOP_PTR.

  1186.    If NOERR is nonzero, return zero if NAME is not suitably defined.
  1187.    If NAME is the name of a baseclass type, return that type.  */

  1188. struct type *
  1189. lookup_struct_elt_type (struct type *type, const char *name, int noerr)
  1190. {
  1191.   int i;
  1192.   char *typename;

  1193.   for (;;)
  1194.     {
  1195.       CHECK_TYPEDEF (type);
  1196.       if (TYPE_CODE (type) != TYPE_CODE_PTR
  1197.           && TYPE_CODE (type) != TYPE_CODE_REF)
  1198.         break;
  1199.       type = TYPE_TARGET_TYPE (type);
  1200.     }

  1201.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  1202.       && TYPE_CODE (type) != TYPE_CODE_UNION)
  1203.     {
  1204.       typename = type_to_string (type);
  1205.       make_cleanup (xfree, typename);
  1206.       error (_("Type %s is not a structure or union type."), typename);
  1207.     }

  1208. #if 0
  1209.   /* FIXME: This change put in by Michael seems incorrect for the case
  1210.      where the structure tag name is the same as the member name.
  1211.      I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
  1212.      foo; } bell;" Disabled by fnf.  */
  1213.   {
  1214.     char *typename;

  1215.     typename = type_name_no_tag (type);
  1216.     if (typename != NULL && strcmp (typename, name) == 0)
  1217.       return type;
  1218.   }
  1219. #endif

  1220.   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1221.     {
  1222.       const char *t_field_name = TYPE_FIELD_NAME (type, i);

  1223.       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1224.         {
  1225.           return TYPE_FIELD_TYPE (type, i);
  1226.         }
  1227.      else if (!t_field_name || *t_field_name == '\0')
  1228.         {
  1229.           struct type *subtype
  1230.             = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);

  1231.           if (subtype != NULL)
  1232.             return subtype;
  1233.         }
  1234.     }

  1235.   /* OK, it's not in this class.  Recursively check the baseclasses.  */
  1236.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1237.     {
  1238.       struct type *t;

  1239.       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
  1240.       if (t != NULL)
  1241.         {
  1242.           return t;
  1243.         }
  1244.     }

  1245.   if (noerr)
  1246.     {
  1247.       return NULL;
  1248.     }

  1249.   typename = type_to_string (type);
  1250.   make_cleanup (xfree, typename);
  1251.   error (_("Type %s has no component named %s."), typename, name);
  1252. }

  1253. /* Store in *MAX the largest number representable by unsigned integer type
  1254.    TYPE.  */

  1255. void
  1256. get_unsigned_type_max (struct type *type, ULONGEST *max)
  1257. {
  1258.   unsigned int n;

  1259.   CHECK_TYPEDEF (type);
  1260.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
  1261.   gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));

  1262.   /* Written this way to avoid overflow.  */
  1263.   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  1264.   *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
  1265. }

  1266. /* Store in *MIN, *MAX the smallest and largest numbers representable by
  1267.    signed integer type TYPE.  */

  1268. void
  1269. get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
  1270. {
  1271.   unsigned int n;

  1272.   CHECK_TYPEDEF (type);
  1273.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
  1274.   gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));

  1275.   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  1276.   *min = -((ULONGEST) 1 << (n - 1));
  1277.   *max = ((ULONGEST) 1 << (n - 1)) - 1;
  1278. }

  1279. /* Lookup the vptr basetype/fieldno values for TYPE.
  1280.    If found store vptr_basetype in *BASETYPEP if non-NULL, and return
  1281.    vptr_fieldno.  Also, if found and basetype is from the same objfile,
  1282.    cache the results.
  1283.    If not found, return -1 and ignore BASETYPEP.
  1284.    Callers should be aware that in some cases (for example,
  1285.    the type or one of its baseclasses is a stub type and we are
  1286.    debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
  1287.    this function will not be able to find the
  1288.    virtual function table pointer, and vptr_fieldno will remain -1 and
  1289.    vptr_basetype will remain NULL or incomplete.  */

  1290. int
  1291. get_vptr_fieldno (struct type *type, struct type **basetypep)
  1292. {
  1293.   CHECK_TYPEDEF (type);

  1294.   if (TYPE_VPTR_FIELDNO (type) < 0)
  1295.     {
  1296.       int i;

  1297.       /* We must start at zero in case the first (and only) baseclass
  1298.          is virtual (and hence we cannot share the table pointer).  */
  1299.       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
  1300.         {
  1301.           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  1302.           int fieldno;
  1303.           struct type *basetype;

  1304.           fieldno = get_vptr_fieldno (baseclass, &basetype);
  1305.           if (fieldno >= 0)
  1306.             {
  1307.               /* If the type comes from a different objfile we can't cache
  1308.                  it, it may have a different lifetime.  PR 2384 */
  1309.               if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
  1310.                 {
  1311.                   TYPE_VPTR_FIELDNO (type) = fieldno;
  1312.                   TYPE_VPTR_BASETYPE (type) = basetype;
  1313.                 }
  1314.               if (basetypep)
  1315.                 *basetypep = basetype;
  1316.               return fieldno;
  1317.             }
  1318.         }

  1319.       /* Not found.  */
  1320.       return -1;
  1321.     }
  1322.   else
  1323.     {
  1324.       if (basetypep)
  1325.         *basetypep = TYPE_VPTR_BASETYPE (type);
  1326.       return TYPE_VPTR_FIELDNO (type);
  1327.     }
  1328. }

  1329. static void
  1330. stub_noname_complaint (void)
  1331. {
  1332.   complaint (&symfile_complaints, _("stub type has NULL name"));
  1333. }

  1334. /* Worker for is_dynamic_type.  */

  1335. static int
  1336. is_dynamic_type_internal (struct type *type, int top_level)
  1337. {
  1338.   type = check_typedef (type);

  1339.   /* We only want to recognize references at the outermost level.  */
  1340.   if (top_level && TYPE_CODE (type) == TYPE_CODE_REF)
  1341.     type = check_typedef (TYPE_TARGET_TYPE (type));

  1342.   /* Types that have a dynamic TYPE_DATA_LOCATION are considered
  1343.      dynamic, even if the type itself is statically defined.
  1344.      From a user's point of view, this may appear counter-intuitive;
  1345.      but it makes sense in this context, because the point is to determine
  1346.      whether any part of the type needs to be resolved before it can
  1347.      be exploited.  */
  1348.   if (TYPE_DATA_LOCATION (type) != NULL
  1349.       && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
  1350.           || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
  1351.     return 1;

  1352.   switch (TYPE_CODE (type))
  1353.     {
  1354.     case TYPE_CODE_RANGE:
  1355.       return !has_static_range (TYPE_RANGE_DATA (type));

  1356.     case TYPE_CODE_ARRAY:
  1357.       {
  1358.         gdb_assert (TYPE_NFIELDS (type) == 1);

  1359.         /* The array is dynamic if either the bounds are dynamic,
  1360.            or the elements it contains have a dynamic contents.  */
  1361.         if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
  1362.           return 1;
  1363.         return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
  1364.       }

  1365.     case TYPE_CODE_STRUCT:
  1366.     case TYPE_CODE_UNION:
  1367.       {
  1368.         int i;

  1369.         for (i = 0; i < TYPE_NFIELDS (type); ++i)
  1370.           if (!field_is_static (&TYPE_FIELD (type, i))
  1371.               && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
  1372.             return 1;
  1373.       }
  1374.       break;
  1375.     }

  1376.   return 0;
  1377. }

  1378. /* See gdbtypes.h.  */

  1379. int
  1380. is_dynamic_type (struct type *type)
  1381. {
  1382.   return is_dynamic_type_internal (type, 1);
  1383. }

  1384. static struct type *resolve_dynamic_type_internal (struct type *type,
  1385.                                                    CORE_ADDR addr,
  1386.                                                    int top_level);

  1387. /* Given a dynamic range type (dyn_range_type) and address,
  1388.    return a static version of that type.  */

  1389. static struct type *
  1390. resolve_dynamic_range (struct type *dyn_range_type, CORE_ADDR addr)
  1391. {
  1392.   CORE_ADDR value;
  1393.   struct type *static_range_type;
  1394.   const struct dynamic_prop *prop;
  1395.   const struct dwarf2_locexpr_baton *baton;
  1396.   struct dynamic_prop low_bound, high_bound;

  1397.   gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);

  1398.   prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
  1399.   if (dwarf2_evaluate_property (prop, addr, &value))
  1400.     {
  1401.       low_bound.kind = PROP_CONST;
  1402.       low_bound.data.const_val = value;
  1403.     }
  1404.   else
  1405.     {
  1406.       low_bound.kind = PROP_UNDEFINED;
  1407.       low_bound.data.const_val = 0;
  1408.     }

  1409.   prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
  1410.   if (dwarf2_evaluate_property (prop, addr, &value))
  1411.     {
  1412.       high_bound.kind = PROP_CONST;
  1413.       high_bound.data.const_val = value;

  1414.       if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count)
  1415.         high_bound.data.const_val
  1416.           = low_bound.data.const_val + high_bound.data.const_val - 1;
  1417.     }
  1418.   else
  1419.     {
  1420.       high_bound.kind = PROP_UNDEFINED;
  1421.       high_bound.data.const_val = 0;
  1422.     }

  1423.   static_range_type = create_range_type (copy_type (dyn_range_type),
  1424.                                          TYPE_TARGET_TYPE (dyn_range_type),
  1425.                                          &low_bound, &high_bound);
  1426.   TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1;
  1427.   return static_range_type;
  1428. }

  1429. /* Resolves dynamic bound values of an array type TYPE to static ones.
  1430.    ADDRESS might be needed to resolve the subrange bounds, it is the location
  1431.    of the associated array.  */

  1432. static struct type *
  1433. resolve_dynamic_array (struct type *type, CORE_ADDR addr)
  1434. {
  1435.   CORE_ADDR value;
  1436.   struct type *elt_type;
  1437.   struct type *range_type;
  1438.   struct type *ary_dim;

  1439.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);

  1440.   elt_type = type;
  1441.   range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
  1442.   range_type = resolve_dynamic_range (range_type, addr);

  1443.   ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));

  1444.   if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
  1445.     elt_type = resolve_dynamic_array (TYPE_TARGET_TYPE (type), addr);
  1446.   else
  1447.     elt_type = TYPE_TARGET_TYPE (type);

  1448.   return create_array_type (copy_type (type),
  1449.                             elt_type,
  1450.                             range_type);
  1451. }

  1452. /* Resolve dynamic bounds of members of the union TYPE to static
  1453.    bounds.  */

  1454. static struct type *
  1455. resolve_dynamic_union (struct type *type, CORE_ADDR addr)
  1456. {
  1457.   struct type *resolved_type;
  1458.   int i;
  1459.   unsigned int max_len = 0;

  1460.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);

  1461.   resolved_type = copy_type (type);
  1462.   TYPE_FIELDS (resolved_type)
  1463.     = TYPE_ALLOC (resolved_type,
  1464.                   TYPE_NFIELDS (resolved_type) * sizeof (struct field));
  1465.   memcpy (TYPE_FIELDS (resolved_type),
  1466.           TYPE_FIELDS (type),
  1467.           TYPE_NFIELDS (resolved_type) * sizeof (struct field));
  1468.   for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
  1469.     {
  1470.       struct type *t;

  1471.       if (field_is_static (&TYPE_FIELD (type, i)))
  1472.         continue;

  1473.       t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
  1474.                                          addr, 0);
  1475.       TYPE_FIELD_TYPE (resolved_type, i) = t;
  1476.       if (TYPE_LENGTH (t) > max_len)
  1477.         max_len = TYPE_LENGTH (t);
  1478.     }

  1479.   TYPE_LENGTH (resolved_type) = max_len;
  1480.   return resolved_type;
  1481. }

  1482. /* Resolve dynamic bounds of members of the struct TYPE to static
  1483.    bounds.  */

  1484. static struct type *
  1485. resolve_dynamic_struct (struct type *type, CORE_ADDR addr)
  1486. {
  1487.   struct type *resolved_type;
  1488.   int i;
  1489.   unsigned resolved_type_bit_length = 0;

  1490.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
  1491.   gdb_assert (TYPE_NFIELDS (type) > 0);

  1492.   resolved_type = copy_type (type);
  1493.   TYPE_FIELDS (resolved_type)
  1494.     = TYPE_ALLOC (resolved_type,
  1495.                   TYPE_NFIELDS (resolved_type) * sizeof (struct field));
  1496.   memcpy (TYPE_FIELDS (resolved_type),
  1497.           TYPE_FIELDS (type),
  1498.           TYPE_NFIELDS (resolved_type) * sizeof (struct field));
  1499.   for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
  1500.     {
  1501.       unsigned new_bit_length;

  1502.       if (field_is_static (&TYPE_FIELD (type, i)))
  1503.         continue;

  1504.       TYPE_FIELD_TYPE (resolved_type, i)
  1505.         = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
  1506.                                          addr, 0);

  1507.       /* As we know this field is not a static field, the field's
  1508.          field_loc_kind should be FIELD_LOC_KIND_BITPOS.  Verify
  1509.          this is the case, but only trigger a simple error rather
  1510.          than an internal error if that fails.  While failing
  1511.          that verification indicates a bug in our code, the error
  1512.          is not severe enough to suggest to the user he stops
  1513.          his debugging session because of it.  */
  1514.       if (TYPE_FIELD_LOC_KIND (resolved_type, i) != FIELD_LOC_KIND_BITPOS)
  1515.         error (_("Cannot determine struct field location"
  1516.                  " (invalid location kind)"));
  1517.       new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
  1518.       if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
  1519.         new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
  1520.       else
  1521.         new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
  1522.                            * TARGET_CHAR_BIT);

  1523.       /* Normally, we would use the position and size of the last field
  1524.          to determine the size of the enclosing structure.  But GCC seems
  1525.          to be encoding the position of some fields incorrectly when
  1526.          the struct contains a dynamic field that is not placed last.
  1527.          So we compute the struct size based on the field that has
  1528.          the highest position + size - probably the best we can do.  */
  1529.       if (new_bit_length > resolved_type_bit_length)
  1530.         resolved_type_bit_length = new_bit_length;
  1531.     }

  1532.   TYPE_LENGTH (resolved_type)
  1533.     = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;

  1534.   return resolved_type;
  1535. }

  1536. /* Worker for resolved_dynamic_type.  */

  1537. static struct type *
  1538. resolve_dynamic_type_internal (struct type *type, CORE_ADDR addr,
  1539.                                int top_level)
  1540. {
  1541.   struct type *real_type = check_typedef (type);
  1542.   struct type *resolved_type = type;
  1543.   const struct dynamic_prop *prop;
  1544.   CORE_ADDR value;

  1545.   if (!is_dynamic_type_internal (real_type, top_level))
  1546.     return type;

  1547.   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  1548.     {
  1549.       resolved_type = copy_type (type);
  1550.       TYPE_TARGET_TYPE (resolved_type)
  1551.         = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr,
  1552.                                          top_level);
  1553.     }
  1554.   else
  1555.     {
  1556.       /* Before trying to resolve TYPE, make sure it is not a stub.  */
  1557.       type = real_type;

  1558.       switch (TYPE_CODE (type))
  1559.         {
  1560.         case TYPE_CODE_REF:
  1561.           {
  1562.             CORE_ADDR target_addr = read_memory_typed_address (addr, type);

  1563.             resolved_type = copy_type (type);
  1564.             TYPE_TARGET_TYPE (resolved_type)
  1565.               = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
  1566.                                                target_addr, top_level);
  1567.             break;
  1568.           }

  1569.         case TYPE_CODE_ARRAY:
  1570.           resolved_type = resolve_dynamic_array (type, addr);
  1571.           break;

  1572.         case TYPE_CODE_RANGE:
  1573.           resolved_type = resolve_dynamic_range (type, addr);
  1574.           break;

  1575.         case TYPE_CODE_UNION:
  1576.           resolved_type = resolve_dynamic_union (type, addr);
  1577.           break;

  1578.         case TYPE_CODE_STRUCT:
  1579.           resolved_type = resolve_dynamic_struct (type, addr);
  1580.           break;
  1581.         }
  1582.     }

  1583.   /* Resolve data_location attribute.  */
  1584.   prop = TYPE_DATA_LOCATION (resolved_type);
  1585.   if (dwarf2_evaluate_property (prop, addr, &value))
  1586.     {
  1587.       TYPE_DATA_LOCATION_ADDR (resolved_type) = value;
  1588.       TYPE_DATA_LOCATION_KIND (resolved_type) = PROP_CONST;
  1589.     }
  1590.   else
  1591.     TYPE_DATA_LOCATION (resolved_type) = NULL;

  1592.   return resolved_type;
  1593. }

  1594. /* See gdbtypes.h  */

  1595. struct type *
  1596. resolve_dynamic_type (struct type *type, CORE_ADDR addr)
  1597. {
  1598.   return resolve_dynamic_type_internal (type, addr, 1);
  1599. }

  1600. /* Find the real type of TYPE.  This function returns the real type,
  1601.    after removing all layers of typedefs, and completing opaque or stub
  1602.    types.  Completion changes the TYPE argument, but stripping of
  1603.    typedefs does not.

  1604.    Instance flags (e.g. const/volatile) are preserved as typedefs are
  1605.    stripped.  If necessary a new qualified form of the underlying type
  1606.    is created.

  1607.    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
  1608.    not been computed and we're either in the middle of reading symbols, or
  1609.    there was no name for the typedef in the debug info.

  1610.    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
  1611.    QUITs in the symbol reading code can also throw.
  1612.    Thus this function can throw an exception.

  1613.    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
  1614.    the target type.

  1615.    If this is a stubbed struct (i.e. declared as struct foo *), see if
  1616.    we can find a full definition in some other file.  If so, copy this
  1617.    definition, so we can use it in future.  There used to be a comment
  1618.    (but not any code) that if we don't find a full definition, we'd
  1619.    set a flag so we don't spend time in the future checking the same
  1620.    type.  That would be a mistake, though--we might load in more
  1621.    symbols which contain a full definition for the type.  */

  1622. struct type *
  1623. check_typedef (struct type *type)
  1624. {
  1625.   struct type *orig_type = type;
  1626.   /* While we're removing typedefs, we don't want to lose qualifiers.
  1627.      E.g., const/volatile.  */
  1628.   int instance_flags = TYPE_INSTANCE_FLAGS (type);

  1629.   gdb_assert (type);

  1630.   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  1631.     {
  1632.       if (!TYPE_TARGET_TYPE (type))
  1633.         {
  1634.           const char *name;
  1635.           struct symbol *sym;

  1636.           /* It is dangerous to call lookup_symbol if we are currently
  1637.              reading a symtab.  Infinite recursion is one danger.  */
  1638.           if (currently_reading_symtab)
  1639.             return make_qualified_type (type, instance_flags, NULL);

  1640.           name = type_name_no_tag (type);
  1641.           /* FIXME: shouldn't we separately check the TYPE_NAME and
  1642.              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
  1643.              VAR_DOMAIN as appropriate?  (this code was written before
  1644.              TYPE_NAME and TYPE_TAG_NAME were separate).  */
  1645.           if (name == NULL)
  1646.             {
  1647.               stub_noname_complaint ();
  1648.               return make_qualified_type (type, instance_flags, NULL);
  1649.             }
  1650.           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
  1651.           if (sym)
  1652.             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
  1653.           else                                        /* TYPE_CODE_UNDEF */
  1654.             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
  1655.         }
  1656.       type = TYPE_TARGET_TYPE (type);

  1657.       /* Preserve the instance flags as we traverse down the typedef chain.

  1658.          Handling address spaces/classes is nasty, what do we do if there's a
  1659.          conflict?
  1660.          E.g., what if an outer typedef marks the type as class_1 and an inner
  1661.          typedef marks the type as class_2?
  1662.          This is the wrong place to do such error checking.  We leave it to
  1663.          the code that created the typedef in the first place to flag the
  1664.          error.  We just pick the outer address space (akin to letting the
  1665.          outer cast in a chain of casting win), instead of assuming
  1666.          "it can't happen".  */
  1667.       {
  1668.         const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
  1669.                                 | TYPE_INSTANCE_FLAG_DATA_SPACE);
  1670.         const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
  1671.         int new_instance_flags = TYPE_INSTANCE_FLAGS (type);

  1672.         /* Treat code vs data spaces and address classes separately.  */
  1673.         if ((instance_flags & ALL_SPACES) != 0)
  1674.           new_instance_flags &= ~ALL_SPACES;
  1675.         if ((instance_flags & ALL_CLASSES) != 0)
  1676.           new_instance_flags &= ~ALL_CLASSES;

  1677.         instance_flags |= new_instance_flags;
  1678.       }
  1679.     }

  1680.   /* If this is a struct/class/union with no fields, then check
  1681.      whether a full definition exists somewhere else.  This is for
  1682.      systems where a type definition with no fields is issued for such
  1683.      types, instead of identifying them as stub types in the first
  1684.      place.  */

  1685.   if (TYPE_IS_OPAQUE (type)
  1686.       && opaque_type_resolution
  1687.       && !currently_reading_symtab)
  1688.     {
  1689.       const char *name = type_name_no_tag (type);
  1690.       struct type *newtype;

  1691.       if (name == NULL)
  1692.         {
  1693.           stub_noname_complaint ();
  1694.           return make_qualified_type (type, instance_flags, NULL);
  1695.         }
  1696.       newtype = lookup_transparent_type (name);

  1697.       if (newtype)
  1698.         {
  1699.           /* If the resolved type and the stub are in the same
  1700.              objfile, then replace the stub type with the real deal.
  1701.              But if they're in separate objfiles, leave the stub
  1702.              alone; we'll just look up the transparent type every time
  1703.              we call check_typedef.  We can't create pointers between
  1704.              types allocated to different objfiles, since they may
  1705.              have different lifetimes.  Trying to copy NEWTYPE over to
  1706.              TYPE's objfile is pointless, too, since you'll have to
  1707.              move over any other types NEWTYPE refers to, which could
  1708.              be an unbounded amount of stuff.  */
  1709.           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
  1710.             type = make_qualified_type (newtype,
  1711.                                         TYPE_INSTANCE_FLAGS (type),
  1712.                                         type);
  1713.           else
  1714.             type = newtype;
  1715.         }
  1716.     }
  1717.   /* Otherwise, rely on the stub flag being set for opaque/stubbed
  1718.      types.  */
  1719.   else if (TYPE_STUB (type) && !currently_reading_symtab)
  1720.     {
  1721.       const char *name = type_name_no_tag (type);
  1722.       /* FIXME: shouldn't we separately check the TYPE_NAME and the
  1723.          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
  1724.          as appropriate?  (this code was written before TYPE_NAME and
  1725.          TYPE_TAG_NAME were separate).  */
  1726.       struct symbol *sym;

  1727.       if (name == NULL)
  1728.         {
  1729.           stub_noname_complaint ();
  1730.           return make_qualified_type (type, instance_flags, NULL);
  1731.         }
  1732.       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
  1733.       if (sym)
  1734.         {
  1735.           /* Same as above for opaque types, we can replace the stub
  1736.              with the complete type only if they are in the same
  1737.              objfile.  */
  1738.           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
  1739.             type = make_qualified_type (SYMBOL_TYPE (sym),
  1740.                                         TYPE_INSTANCE_FLAGS (type),
  1741.                                         type);
  1742.           else
  1743.             type = SYMBOL_TYPE (sym);
  1744.         }
  1745.     }

  1746.   if (TYPE_TARGET_STUB (type))
  1747.     {
  1748.       struct type *range_type;
  1749.       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));

  1750.       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
  1751.         {
  1752.           /* Nothing we can do.  */
  1753.         }
  1754.       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
  1755.         {
  1756.           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
  1757.           TYPE_TARGET_STUB (type) = 0;
  1758.         }
  1759.     }

  1760.   type = make_qualified_type (type, instance_flags, NULL);

  1761.   /* Cache TYPE_LENGTH for future use.  */
  1762.   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);

  1763.   return type;
  1764. }

  1765. /* Parse a type expression in the string [P..P+LENGTH).  If an error
  1766.    occurs, silently return a void type.  */

  1767. static struct type *
  1768. safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
  1769. {
  1770.   struct ui_file *saved_gdb_stderr;
  1771.   struct type *type = NULL; /* Initialize to keep gcc happy.  */
  1772.   volatile struct gdb_exception except;

  1773.   /* Suppress error messages.  */
  1774.   saved_gdb_stderr = gdb_stderr;
  1775.   gdb_stderr = ui_file_new ();

  1776.   /* Call parse_and_eval_type() without fear of longjmp()s.  */
  1777.   TRY_CATCH (except, RETURN_MASK_ERROR)
  1778.     {
  1779.       type = parse_and_eval_type (p, length);
  1780.     }

  1781.   if (except.reason < 0)
  1782.     type = builtin_type (gdbarch)->builtin_void;

  1783.   /* Stop suppressing error messages.  */
  1784.   ui_file_delete (gdb_stderr);
  1785.   gdb_stderr = saved_gdb_stderr;

  1786.   return type;
  1787. }

  1788. /* Ugly hack to convert method stubs into method types.

  1789.    He ain't kiddin'.  This demangles the name of the method into a
  1790.    string including argument types, parses out each argument type,
  1791.    generates a string casting a zero to that type, evaluates the
  1792.    string, and stuffs the resulting type into an argtype vector!!!
  1793.    Then it knows the type of the whole function (including argument
  1794.    types for overloading), which info used to be in the stab's but was
  1795.    removed to hack back the space required for them.  */

  1796. static void
  1797. check_stub_method (struct type *type, int method_id, int signature_id)
  1798. {
  1799.   struct gdbarch *gdbarch = get_type_arch (type);
  1800.   struct fn_field *f;
  1801.   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
  1802.   char *demangled_name = gdb_demangle (mangled_name,
  1803.                                        DMGL_PARAMS | DMGL_ANSI);
  1804.   char *argtypetext, *p;
  1805.   int depth = 0, argcount = 1;
  1806.   struct field *argtypes;
  1807.   struct type *mtype;

  1808.   /* Make sure we got back a function string that we can use.  */
  1809.   if (demangled_name)
  1810.     p = strchr (demangled_name, '(');
  1811.   else
  1812.     p = NULL;

  1813.   if (demangled_name == NULL || p == NULL)
  1814.     error (_("Internal: Cannot demangle mangled name `%s'."),
  1815.            mangled_name);

  1816.   /* Now, read in the parameters that define this type.  */
  1817.   p += 1;
  1818.   argtypetext = p;
  1819.   while (*p)
  1820.     {
  1821.       if (*p == '(' || *p == '<')
  1822.         {
  1823.           depth += 1;
  1824.         }
  1825.       else if (*p == ')' || *p == '>')
  1826.         {
  1827.           depth -= 1;
  1828.         }
  1829.       else if (*p == ',' && depth == 0)
  1830.         {
  1831.           argcount += 1;
  1832.         }

  1833.       p += 1;
  1834.     }

  1835.   /* If we read one argument and it was ``void'', don't count it.  */
  1836.   if (strncmp (argtypetext, "(void)", 6) == 0)
  1837.     argcount -= 1;

  1838.   /* We need one extra slot, for the THIS pointer.  */

  1839.   argtypes = (struct field *)
  1840.     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
  1841.   p = argtypetext;

  1842.   /* Add THIS pointer for non-static methods.  */
  1843.   f = TYPE_FN_FIELDLIST1 (type, method_id);
  1844.   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
  1845.     argcount = 0;
  1846.   else
  1847.     {
  1848.       argtypes[0].type = lookup_pointer_type (type);
  1849.       argcount = 1;
  1850.     }

  1851.   if (*p != ')')                /* () means no args, skip while.  */
  1852.     {
  1853.       depth = 0;
  1854.       while (*p)
  1855.         {
  1856.           if (depth <= 0 && (*p == ',' || *p == ')'))
  1857.             {
  1858.               /* Avoid parsing of ellipsis, they will be handled below.
  1859.                  Also avoid ``void'' as above.  */
  1860.               if (strncmp (argtypetext, "...", p - argtypetext) != 0
  1861.                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
  1862.                 {
  1863.                   argtypes[argcount].type =
  1864.                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
  1865.                   argcount += 1;
  1866.                 }
  1867.               argtypetext = p + 1;
  1868.             }

  1869.           if (*p == '(' || *p == '<')
  1870.             {
  1871.               depth += 1;
  1872.             }
  1873.           else if (*p == ')' || *p == '>')
  1874.             {
  1875.               depth -= 1;
  1876.             }

  1877.           p += 1;
  1878.         }
  1879.     }

  1880.   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;

  1881.   /* Now update the old "stub" type into a real type.  */
  1882.   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
  1883.   TYPE_DOMAIN_TYPE (mtype) = type;
  1884.   TYPE_FIELDS (mtype) = argtypes;
  1885.   TYPE_NFIELDS (mtype) = argcount;
  1886.   TYPE_STUB (mtype) = 0;
  1887.   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
  1888.   if (p[-2] == '.')
  1889.     TYPE_VARARGS (mtype) = 1;

  1890.   xfree (demangled_name);
  1891. }

  1892. /* This is the external interface to check_stub_method, above.  This
  1893.    function unstubs all of the signatures for TYPE's METHOD_ID method
  1894.    name.  After calling this function TYPE_FN_FIELD_STUB will be
  1895.    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
  1896.    correct.

  1897.    This function unfortunately can not die until stabs do.  */

  1898. void
  1899. check_stub_method_group (struct type *type, int method_id)
  1900. {
  1901.   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
  1902.   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
  1903.   int j, found_stub = 0;

  1904.   for (j = 0; j < len; j++)
  1905.     if (TYPE_FN_FIELD_STUB (f, j))
  1906.       {
  1907.         found_stub = 1;
  1908.         check_stub_method (type, method_id, j);
  1909.       }

  1910.   /* GNU v3 methods with incorrect names were corrected when we read
  1911.      in type information, because it was cheaper to do it then.  The
  1912.      only GNU v2 methods with incorrect method names are operators and
  1913.      destructors; destructors were also corrected when we read in type
  1914.      information.

  1915.      Therefore the only thing we need to handle here are v2 operator
  1916.      names.  */
  1917.   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
  1918.     {
  1919.       int ret;
  1920.       char dem_opname[256];

  1921.       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
  1922.                                                            method_id),
  1923.                                    dem_opname, DMGL_ANSI);
  1924.       if (!ret)
  1925.         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
  1926.                                                              method_id),
  1927.                                      dem_opname, 0);
  1928.       if (ret)
  1929.         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
  1930.     }
  1931. }

  1932. /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
  1933. const struct cplus_struct_type cplus_struct_default = { };

  1934. void
  1935. allocate_cplus_struct_type (struct type *type)
  1936. {
  1937.   if (HAVE_CPLUS_STRUCT (type))
  1938.     /* Structure was already allocated.  Nothing more to do.  */
  1939.     return;

  1940.   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
  1941.   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
  1942.     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
  1943.   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
  1944. }

  1945. const struct gnat_aux_type gnat_aux_default =
  1946.   { NULL };

  1947. /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
  1948.    and allocate the associated gnat-specific data.  The gnat-specific
  1949.    data is also initialized to gnat_aux_default.  */

  1950. void
  1951. allocate_gnat_aux_type (struct type *type)
  1952. {
  1953.   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
  1954.   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
  1955.     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
  1956.   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
  1957. }

  1958. /* Helper function to initialize the standard scalar types.

  1959.    If NAME is non-NULL, then it is used to initialize the type name.
  1960.    Note that NAME is not copied; it is required to have a lifetime at
  1961.    least as long as OBJFILE.  */

  1962. struct type *
  1963. init_type (enum type_code code, int length, int flags,
  1964.            const char *name, struct objfile *objfile)
  1965. {
  1966.   struct type *type;

  1967.   type = alloc_type (objfile);
  1968.   TYPE_CODE (type) = code;
  1969.   TYPE_LENGTH (type) = length;

  1970.   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
  1971.   if (flags & TYPE_FLAG_UNSIGNED)
  1972.     TYPE_UNSIGNED (type) = 1;
  1973.   if (flags & TYPE_FLAG_NOSIGN)
  1974.     TYPE_NOSIGN (type) = 1;
  1975.   if (flags & TYPE_FLAG_STUB)
  1976.     TYPE_STUB (type) = 1;
  1977.   if (flags & TYPE_FLAG_TARGET_STUB)
  1978.     TYPE_TARGET_STUB (type) = 1;
  1979.   if (flags & TYPE_FLAG_STATIC)
  1980.     TYPE_STATIC (type) = 1;
  1981.   if (flags & TYPE_FLAG_PROTOTYPED)
  1982.     TYPE_PROTOTYPED (type) = 1;
  1983.   if (flags & TYPE_FLAG_INCOMPLETE)
  1984.     TYPE_INCOMPLETE (type) = 1;
  1985.   if (flags & TYPE_FLAG_VARARGS)
  1986.     TYPE_VARARGS (type) = 1;
  1987.   if (flags & TYPE_FLAG_VECTOR)
  1988.     TYPE_VECTOR (type) = 1;
  1989.   if (flags & TYPE_FLAG_STUB_SUPPORTED)
  1990.     TYPE_STUB_SUPPORTED (type) = 1;
  1991.   if (flags & TYPE_FLAG_FIXED_INSTANCE)
  1992.     TYPE_FIXED_INSTANCE (type) = 1;
  1993.   if (flags & TYPE_FLAG_GNU_IFUNC)
  1994.     TYPE_GNU_IFUNC (type) = 1;

  1995.   TYPE_NAME (type) = name;

  1996.   /* C++ fancies.  */

  1997.   if (name && strcmp (name, "char") == 0)
  1998.     TYPE_NOSIGN (type) = 1;

  1999.   switch (code)
  2000.     {
  2001.       case TYPE_CODE_STRUCT:
  2002.       case TYPE_CODE_UNION:
  2003.       case TYPE_CODE_NAMESPACE:
  2004.         INIT_CPLUS_SPECIFIC (type);
  2005.         break;
  2006.       case TYPE_CODE_FLT:
  2007.         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
  2008.         break;
  2009.       case TYPE_CODE_FUNC:
  2010.         INIT_FUNC_SPECIFIC (type);
  2011.         break;
  2012.     }
  2013.   return type;
  2014. }

  2015. /* Queries on types.  */

  2016. int
  2017. can_dereference (struct type *t)
  2018. {
  2019.   /* FIXME: Should we return true for references as well as
  2020.      pointers?  */
  2021.   CHECK_TYPEDEF (t);
  2022.   return
  2023.     (t != NULL
  2024.      && TYPE_CODE (t) == TYPE_CODE_PTR
  2025.      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
  2026. }

  2027. int
  2028. is_integral_type (struct type *t)
  2029. {
  2030.   CHECK_TYPEDEF (t);
  2031.   return
  2032.     ((t != NULL)
  2033.      && ((TYPE_CODE (t) == TYPE_CODE_INT)
  2034.          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
  2035.          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
  2036.          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
  2037.          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
  2038.          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
  2039. }

  2040. /* Return true if TYPE is scalar.  */

  2041. static int
  2042. is_scalar_type (struct type *type)
  2043. {
  2044.   CHECK_TYPEDEF (type);

  2045.   switch (TYPE_CODE (type))
  2046.     {
  2047.     case TYPE_CODE_ARRAY:
  2048.     case TYPE_CODE_STRUCT:
  2049.     case TYPE_CODE_UNION:
  2050.     case TYPE_CODE_SET:
  2051.     case TYPE_CODE_STRING:
  2052.       return 0;
  2053.     default:
  2054.       return 1;
  2055.     }
  2056. }

  2057. /* Return true if T is scalar, or a composite type which in practice has
  2058.    the memory layout of a scalar typeE.g., an array or struct with only
  2059.    one scalar element inside it, or a union with only scalar elements.  */

  2060. int
  2061. is_scalar_type_recursive (struct type *t)
  2062. {
  2063.   CHECK_TYPEDEF (t);

  2064.   if (is_scalar_type (t))
  2065.     return 1;
  2066.   /* Are we dealing with an array or string of known dimensions?  */
  2067.   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
  2068.             || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
  2069.            && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
  2070.     {
  2071.       LONGEST low_bound, high_bound;
  2072.       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));

  2073.       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);

  2074.       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
  2075.     }
  2076.   /* Are we dealing with a struct with one element?  */
  2077.   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
  2078.     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
  2079.   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
  2080.     {
  2081.       int i, n = TYPE_NFIELDS (t);

  2082.       /* If all elements of the union are scalar, then the union is scalar.  */
  2083.       for (i = 0; i < n; i++)
  2084.         if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
  2085.           return 0;

  2086.       return 1;
  2087.     }

  2088.   return 0;
  2089. }

  2090. /* Return true is T is a class or a union.  False otherwise.  */

  2091. int
  2092. class_or_union_p (const struct type *t)
  2093. {
  2094.   return (TYPE_CODE (t) == TYPE_CODE_STRUCT
  2095.           || TYPE_CODE (t) == TYPE_CODE_UNION);
  2096. }

  2097. /* A helper function which returns true if types A and B represent the
  2098.    "same" class type.  This is true if the types have the same main
  2099.    type, or the same name.  */

  2100. int
  2101. class_types_same_p (const struct type *a, const struct type *b)
  2102. {
  2103.   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
  2104.           || (TYPE_NAME (a) && TYPE_NAME (b)
  2105.               && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
  2106. }

  2107. /* If BASE is an ancestor of DCLASS return the distance between them.
  2108.    otherwise return -1;
  2109.    eg:

  2110.    class A {};
  2111.    class B: public A {};
  2112.    class C: public B {};
  2113.    class D: C {};

  2114.    distance_to_ancestor (A, A, 0) = 0
  2115.    distance_to_ancestor (A, B, 0) = 1
  2116.    distance_to_ancestor (A, C, 0) = 2
  2117.    distance_to_ancestor (A, D, 0) = 3

  2118.    If PUBLIC is 1 then only public ancestors are considered,
  2119.    and the function returns the distance only if BASE is a public ancestor
  2120.    of DCLASS.
  2121.    Eg:

  2122.    distance_to_ancestor (A, D, 1) = -1.  */

  2123. static int
  2124. distance_to_ancestor (struct type *base, struct type *dclass, int public)
  2125. {
  2126.   int i;
  2127.   int d;

  2128.   CHECK_TYPEDEF (base);
  2129.   CHECK_TYPEDEF (dclass);

  2130.   if (class_types_same_p (base, dclass))
  2131.     return 0;

  2132.   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
  2133.     {
  2134.       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
  2135.         continue;

  2136.       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
  2137.       if (d >= 0)
  2138.         return 1 + d;
  2139.     }

  2140.   return -1;
  2141. }

  2142. /* Check whether BASE is an ancestor or base class or DCLASS
  2143.    Return 1 if so, and 0 if not.
  2144.    Note: If BASE and DCLASS are of the same type, this function
  2145.    will return 1. So for some class A, is_ancestor (A, A) will
  2146.    return 1.  */

  2147. int
  2148. is_ancestor (struct type *base, struct type *dclass)
  2149. {
  2150.   return distance_to_ancestor (base, dclass, 0) >= 0;
  2151. }

  2152. /* Like is_ancestor, but only returns true when BASE is a public
  2153.    ancestor of DCLASS.  */

  2154. int
  2155. is_public_ancestor (struct type *base, struct type *dclass)
  2156. {
  2157.   return distance_to_ancestor (base, dclass, 1) >= 0;
  2158. }

  2159. /* A helper function for is_unique_ancestor.  */

  2160. static int
  2161. is_unique_ancestor_worker (struct type *base, struct type *dclass,
  2162.                            int *offset,
  2163.                            const gdb_byte *valaddr, int embedded_offset,
  2164.                            CORE_ADDR address, struct value *val)
  2165. {
  2166.   int i, count = 0;

  2167.   CHECK_TYPEDEF (base);
  2168.   CHECK_TYPEDEF (dclass);

  2169.   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
  2170.     {
  2171.       struct type *iter;
  2172.       int this_offset;

  2173.       iter = check_typedef (TYPE_BASECLASS (dclass, i));

  2174.       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
  2175.                                       address, val);

  2176.       if (class_types_same_p (base, iter))
  2177.         {
  2178.           /* If this is the first subclass, set *OFFSET and set count
  2179.              to 1.  Otherwise, if this is at the same offset as
  2180.              previous instances, do nothing.  Otherwise, increment
  2181.              count.  */
  2182.           if (*offset == -1)
  2183.             {
  2184.               *offset = this_offset;
  2185.               count = 1;
  2186.             }
  2187.           else if (this_offset == *offset)
  2188.             {
  2189.               /* Nothing.  */
  2190.             }
  2191.           else
  2192.             ++count;
  2193.         }
  2194.       else
  2195.         count += is_unique_ancestor_worker (base, iter, offset,
  2196.                                             valaddr,
  2197.                                             embedded_offset + this_offset,
  2198.                                             address, val);
  2199.     }

  2200.   return count;
  2201. }

  2202. /* Like is_ancestor, but only returns true if BASE is a unique base
  2203.    class of the type of VAL.  */

  2204. int
  2205. is_unique_ancestor (struct type *base, struct value *val)
  2206. {
  2207.   int offset = -1;

  2208.   return is_unique_ancestor_worker (base, value_type (val), &offset,
  2209.                                     value_contents_for_printing (val),
  2210.                                     value_embedded_offset (val),
  2211.                                     value_address (val), val) == 1;
  2212. }


  2213. /* Overload resolution.  */

  2214. /* Return the sum of the rank of A with the rank of B.  */

  2215. struct rank
  2216. sum_ranks (struct rank a, struct rank b)
  2217. {
  2218.   struct rank c;
  2219.   c.rank = a.rank + b.rank;
  2220.   c.subrank = a.subrank + b.subrank;
  2221.   return c;
  2222. }

  2223. /* Compare rank A and B and return:
  2224.    0 if a = b
  2225.    1 if a is better than b
  2226.   -1 if b is better than a.  */

  2227. int
  2228. compare_ranks (struct rank a, struct rank b)
  2229. {
  2230.   if (a.rank == b.rank)
  2231.     {
  2232.       if (a.subrank == b.subrank)
  2233.         return 0;
  2234.       if (a.subrank < b.subrank)
  2235.         return 1;
  2236.       if (a.subrank > b.subrank)
  2237.         return -1;
  2238.     }

  2239.   if (a.rank < b.rank)
  2240.     return 1;

  2241.   /* a.rank > b.rank */
  2242.   return -1;
  2243. }

  2244. /* Functions for overload resolution begin here.  */

  2245. /* Compare two badness vectors A and B and return the result.
  2246.    0 => A and B are identical
  2247.    1 => A and B are incomparable
  2248.    2 => A is better than B
  2249.    3 => A is worse than B  */

  2250. int
  2251. compare_badness (struct badness_vector *a, struct badness_vector *b)
  2252. {
  2253.   int i;
  2254.   int tmp;
  2255.   short found_pos = 0;                /* any positives in c? */
  2256.   short found_neg = 0;                /* any negatives in c? */

  2257.   /* differing lengths => incomparable */
  2258.   if (a->length != b->length)
  2259.     return 1;

  2260.   /* Subtract b from a */
  2261.   for (i = 0; i < a->length; i++)
  2262.     {
  2263.       tmp = compare_ranks (b->rank[i], a->rank[i]);
  2264.       if (tmp > 0)
  2265.         found_pos = 1;
  2266.       else if (tmp < 0)
  2267.         found_neg = 1;
  2268.     }

  2269.   if (found_pos)
  2270.     {
  2271.       if (found_neg)
  2272.         return 1;                /* incomparable */
  2273.       else
  2274.         return 3;                /* A > B */
  2275.     }
  2276.   else
  2277.     /* no positives */
  2278.     {
  2279.       if (found_neg)
  2280.         return 2;                /* A < B */
  2281.       else
  2282.         return 0;                /* A == B */
  2283.     }
  2284. }

  2285. /* Rank a function by comparing its parameter types (PARMS, length
  2286.    NPARMS), to the types of an argument list (ARGS, length NARGS).
  2287.    Return a pointer to a badness vector.  This has NARGS + 1
  2288.    entries.  */

  2289. struct badness_vector *
  2290. rank_function (struct type **parms, int nparms,
  2291.                struct value **args, int nargs)
  2292. {
  2293.   int i;
  2294.   struct badness_vector *bv;
  2295.   int min_len = nparms < nargs ? nparms : nargs;

  2296.   bv = xmalloc (sizeof (struct badness_vector));
  2297.   bv->length = nargs + 1;        /* add 1 for the length-match rank.  */
  2298.   bv->rank = XNEWVEC (struct rank, nargs + 1);

  2299.   /* First compare the lengths of the supplied lists.
  2300.      If there is a mismatch, set it to a high value.  */

  2301.   /* pai/1997-06-03 FIXME: when we have debug info about default
  2302.      arguments and ellipsis parameter lists, we should consider those
  2303.      and rank the length-match more finely.  */

  2304.   LENGTH_MATCH (bv) = (nargs != nparms)
  2305.                       ? LENGTH_MISMATCH_BADNESS
  2306.                       : EXACT_MATCH_BADNESS;

  2307.   /* Now rank all the parameters of the candidate function.  */
  2308.   for (i = 1; i <= min_len; i++)
  2309.     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
  2310.                                  args[i - 1]);

  2311.   /* If more arguments than parameters, add dummy entries.  */
  2312.   for (i = min_len + 1; i <= nargs; i++)
  2313.     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;

  2314.   return bv;
  2315. }

  2316. /* Compare the names of two integer types, assuming that any sign
  2317.    qualifiers have been checked already.  We do it this way because
  2318.    there may be an "int" in the name of one of the types.  */

  2319. static int
  2320. integer_types_same_name_p (const char *first, const char *second)
  2321. {
  2322.   int first_p, second_p;

  2323.   /* If both are shorts, return 1; if neither is a short, keep
  2324.      checking.  */
  2325.   first_p = (strstr (first, "short") != NULL);
  2326.   second_p = (strstr (second, "short") != NULL);
  2327.   if (first_p && second_p)
  2328.     return 1;
  2329.   if (first_p || second_p)
  2330.     return 0;

  2331.   /* Likewise for long.  */
  2332.   first_p = (strstr (first, "long") != NULL);
  2333.   second_p = (strstr (second, "long") != NULL);
  2334.   if (first_p && second_p)
  2335.     return 1;
  2336.   if (first_p || second_p)
  2337.     return 0;

  2338.   /* Likewise for char.  */
  2339.   first_p = (strstr (first, "char") != NULL);
  2340.   second_p = (strstr (second, "char") != NULL);
  2341.   if (first_p && second_p)
  2342.     return 1;
  2343.   if (first_p || second_p)
  2344.     return 0;

  2345.   /* They must both be ints.  */
  2346.   return 1;
  2347. }

  2348. /* Compares type A to type B returns 1 if the represent the same type
  2349.    0 otherwise.  */

  2350. int
  2351. types_equal (struct type *a, struct type *b)
  2352. {
  2353.   /* Identical type pointers.  */
  2354.   /* However, this still doesn't catch all cases of same type for b
  2355.      and a.  The reason is that builtin types are different from
  2356.      the same ones constructed from the object.  */
  2357.   if (a == b)
  2358.     return 1;

  2359.   /* Resolve typedefs */
  2360.   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
  2361.     a = check_typedef (a);
  2362.   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
  2363.     b = check_typedef (b);

  2364.   /* If after resolving typedefs a and b are not of the same type
  2365.      code then they are not equal.  */
  2366.   if (TYPE_CODE (a) != TYPE_CODE (b))
  2367.     return 0;

  2368.   /* If a and b are both pointers types or both reference types then
  2369.      they are equal of the same type iff the objects they refer to are
  2370.      of the same type.  */
  2371.   if (TYPE_CODE (a) == TYPE_CODE_PTR
  2372.       || TYPE_CODE (a) == TYPE_CODE_REF)
  2373.     return types_equal (TYPE_TARGET_TYPE (a),
  2374.                         TYPE_TARGET_TYPE (b));

  2375.   /* Well, damnit, if the names are exactly the same, I'll say they
  2376.      are exactly the same.  This happens when we generate method
  2377.      stubs.  The types won't point to the same address, but they
  2378.      really are the same.  */

  2379.   if (TYPE_NAME (a) && TYPE_NAME (b)
  2380.       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
  2381.     return 1;

  2382.   /* Check if identical after resolving typedefs.  */
  2383.   if (a == b)
  2384.     return 1;

  2385.   /* Two function types are equal if their argument and return types
  2386.      are equal.  */
  2387.   if (TYPE_CODE (a) == TYPE_CODE_FUNC)
  2388.     {
  2389.       int i;

  2390.       if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
  2391.         return 0;

  2392.       if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
  2393.         return 0;

  2394.       for (i = 0; i < TYPE_NFIELDS (a); ++i)
  2395.         if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
  2396.           return 0;

  2397.       return 1;
  2398.     }

  2399.   return 0;
  2400. }

  2401. /* Deep comparison of types.  */

  2402. /* An entry in the type-equality bcache.  */

  2403. typedef struct type_equality_entry
  2404. {
  2405.   struct type *type1, *type2;
  2406. } type_equality_entry_d;

  2407. DEF_VEC_O (type_equality_entry_d);

  2408. /* A helper function to compare two strings.  Returns 1 if they are
  2409.    the same, 0 otherwise.  Handles NULLs properly.  */

  2410. static int
  2411. compare_maybe_null_strings (const char *s, const char *t)
  2412. {
  2413.   if (s == NULL && t != NULL)
  2414.     return 0;
  2415.   else if (s != NULL && t == NULL)
  2416.     return 0;
  2417.   else if (s == NULL && t== NULL)
  2418.     return 1;
  2419.   return strcmp (s, t) == 0;
  2420. }

  2421. /* A helper function for check_types_worklist that checks two types for
  2422.    "deep" equality.  Returns non-zero if the types are considered the
  2423.    same, zero otherwise.  */

  2424. static int
  2425. check_types_equal (struct type *type1, struct type *type2,
  2426.                    VEC (type_equality_entry_d) **worklist)
  2427. {
  2428.   CHECK_TYPEDEF (type1);
  2429.   CHECK_TYPEDEF (type2);

  2430.   if (type1 == type2)
  2431.     return 1;

  2432.   if (TYPE_CODE (type1) != TYPE_CODE (type2)
  2433.       || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
  2434.       || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
  2435.       || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
  2436.       || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
  2437.       || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
  2438.       || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
  2439.       || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
  2440.       || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
  2441.     return 0;

  2442.   if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
  2443.                                    TYPE_TAG_NAME (type2)))
  2444.     return 0;
  2445.   if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
  2446.     return 0;

  2447.   if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
  2448.     {
  2449.       if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
  2450.                   sizeof (*TYPE_RANGE_DATA (type1))) != 0)
  2451.         return 0;
  2452.     }
  2453.   else
  2454.     {
  2455.       int i;

  2456.       for (i = 0; i < TYPE_NFIELDS (type1); ++i)
  2457.         {
  2458.           const struct field *field1 = &TYPE_FIELD (type1, i);
  2459.           const struct field *field2 = &TYPE_FIELD (type2, i);
  2460.           struct type_equality_entry entry;

  2461.           if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
  2462.               || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
  2463.               || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
  2464.             return 0;
  2465.           if (!compare_maybe_null_strings (FIELD_NAME (*field1),
  2466.                                            FIELD_NAME (*field2)))
  2467.             return 0;
  2468.           switch (FIELD_LOC_KIND (*field1))
  2469.             {
  2470.             case FIELD_LOC_KIND_BITPOS:
  2471.               if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
  2472.                 return 0;
  2473.               break;
  2474.             case FIELD_LOC_KIND_ENUMVAL:
  2475.               if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
  2476.                 return 0;
  2477.               break;
  2478.             case FIELD_LOC_KIND_PHYSADDR:
  2479.               if (FIELD_STATIC_PHYSADDR (*field1)
  2480.                   != FIELD_STATIC_PHYSADDR (*field2))
  2481.                 return 0;
  2482.               break;
  2483.             case FIELD_LOC_KIND_PHYSNAME:
  2484.               if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
  2485.                                                FIELD_STATIC_PHYSNAME (*field2)))
  2486.                 return 0;
  2487.               break;
  2488.             case FIELD_LOC_KIND_DWARF_BLOCK:
  2489.               {
  2490.                 struct dwarf2_locexpr_baton *block1, *block2;

  2491.                 block1 = FIELD_DWARF_BLOCK (*field1);
  2492.                 block2 = FIELD_DWARF_BLOCK (*field2);
  2493.                 if (block1->per_cu != block2->per_cu
  2494.                     || block1->size != block2->size
  2495.                     || memcmp (block1->data, block2->data, block1->size) != 0)
  2496.                   return 0;
  2497.               }
  2498.               break;
  2499.             default:
  2500.               internal_error (__FILE__, __LINE__, _("Unsupported field kind "
  2501.                                                     "%d by check_types_equal"),
  2502.                               FIELD_LOC_KIND (*field1));
  2503.             }

  2504.           entry.type1 = FIELD_TYPE (*field1);
  2505.           entry.type2 = FIELD_TYPE (*field2);
  2506.           VEC_safe_push (type_equality_entry_d, *worklist, &entry);
  2507.         }
  2508.     }

  2509.   if (TYPE_TARGET_TYPE (type1) != NULL)
  2510.     {
  2511.       struct type_equality_entry entry;

  2512.       if (TYPE_TARGET_TYPE (type2) == NULL)
  2513.         return 0;

  2514.       entry.type1 = TYPE_TARGET_TYPE (type1);
  2515.       entry.type2 = TYPE_TARGET_TYPE (type2);
  2516.       VEC_safe_push (type_equality_entry_d, *worklist, &entry);
  2517.     }
  2518.   else if (TYPE_TARGET_TYPE (type2) != NULL)
  2519.     return 0;

  2520.   return 1;
  2521. }

  2522. /* Check types on a worklist for equality.  Returns zero if any pair
  2523.    is not equal, non-zero if they are all considered equal.  */

  2524. static int
  2525. check_types_worklist (VEC (type_equality_entry_d) **worklist,
  2526.                       struct bcache *cache)
  2527. {
  2528.   while (!VEC_empty (type_equality_entry_d, *worklist))
  2529.     {
  2530.       struct type_equality_entry entry;
  2531.       int added;

  2532.       entry = *VEC_last (type_equality_entry_d, *worklist);
  2533.       VEC_pop (type_equality_entry_d, *worklist);

  2534.       /* If the type pair has already been visited, we know it is
  2535.          ok.  */
  2536.       bcache_full (&entry, sizeof (entry), cache, &added);
  2537.       if (!added)
  2538.         continue;

  2539.       if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
  2540.         return 0;
  2541.     }

  2542.   return 1;
  2543. }

  2544. /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
  2545.    "deep comparison".  Otherwise return zero.  */

  2546. int
  2547. types_deeply_equal (struct type *type1, struct type *type2)
  2548. {
  2549.   volatile struct gdb_exception except;
  2550.   int result = 0;
  2551.   struct bcache *cache;
  2552.   VEC (type_equality_entry_d) *worklist = NULL;
  2553.   struct type_equality_entry entry;

  2554.   gdb_assert (type1 != NULL && type2 != NULL);

  2555.   /* Early exit for the simple case.  */
  2556.   if (type1 == type2)
  2557.     return 1;

  2558.   cache = bcache_xmalloc (NULL, NULL);

  2559.   entry.type1 = type1;
  2560.   entry.type2 = type2;
  2561.   VEC_safe_push (type_equality_entry_d, worklist, &entry);

  2562.   TRY_CATCH (except, RETURN_MASK_ALL)
  2563.     {
  2564.       result = check_types_worklist (&worklist, cache);
  2565.     }
  2566.   /* check_types_worklist calls several nested helper functions,
  2567.      some of which can raise a GDB Exception, so we just check
  2568.      and rethrow here.  If there is a GDB exception, a comparison
  2569.      is not capable (or trusted), so exit.  */
  2570.   bcache_xfree (cache);
  2571.   VEC_free (type_equality_entry_d, worklist);
  2572.   /* Rethrow if there was a problem.  */
  2573.   if (except.reason < 0)
  2574.     throw_exception (except);

  2575.   return result;
  2576. }

  2577. /* Compare one type (PARM) for compatibility with another (ARG).
  2578. * PARM is intended to be the parameter type of a function; and
  2579. * ARG is the supplied argument's type.  This function tests if
  2580. * the latter can be converted to the former.
  2581. * VALUE is the argument's value or NULL if none (or called recursively)
  2582. *
  2583. * Return 0 if they are identical types;
  2584. * Otherwise, return an integer which corresponds to how compatible
  2585. * PARM is to ARG.  The higher the return value, the worse the match.
  2586. * Generally the "bad" conversions are all uniformly assigned a 100.  */

  2587. struct rank
  2588. rank_one_type (struct type *parm, struct type *arg, struct value *value)
  2589. {
  2590.   struct rank rank = {0,0};

  2591.   if (types_equal (parm, arg))
  2592.     return EXACT_MATCH_BADNESS;

  2593.   /* Resolve typedefs */
  2594.   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
  2595.     parm = check_typedef (parm);
  2596.   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
  2597.     arg = check_typedef (arg);

  2598.   /* See through references, since we can almost make non-references
  2599.      references.  */
  2600.   if (TYPE_CODE (arg) == TYPE_CODE_REF)
  2601.     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
  2602.                        REFERENCE_CONVERSION_BADNESS));
  2603.   if (TYPE_CODE (parm) == TYPE_CODE_REF)
  2604.     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
  2605.                        REFERENCE_CONVERSION_BADNESS));
  2606.   if (overload_debug)
  2607.   /* Debugging only.  */
  2608.     fprintf_filtered (gdb_stderr,
  2609.                       "------ Arg is %s [%d], parm is %s [%d]\n",
  2610.                       TYPE_NAME (arg), TYPE_CODE (arg),
  2611.                       TYPE_NAME (parm), TYPE_CODE (parm));

  2612.   /* x -> y means arg of type x being supplied for parameter of type y.  */

  2613.   switch (TYPE_CODE (parm))
  2614.     {
  2615.     case TYPE_CODE_PTR:
  2616.       switch (TYPE_CODE (arg))
  2617.         {
  2618.         case TYPE_CODE_PTR:

  2619.           /* Allowed pointer conversions are:
  2620.              (a) pointer to void-pointer conversion.  */
  2621.           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
  2622.             return VOID_PTR_CONVERSION_BADNESS;

  2623.           /* (b) pointer to ancestor-pointer conversion.  */
  2624.           rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
  2625.                                                TYPE_TARGET_TYPE (arg),
  2626.                                                0);
  2627.           if (rank.subrank >= 0)
  2628.             return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);

  2629.           return INCOMPATIBLE_TYPE_BADNESS;
  2630.         case TYPE_CODE_ARRAY:
  2631.           if (types_equal (TYPE_TARGET_TYPE (parm),
  2632.                            TYPE_TARGET_TYPE (arg)))
  2633.             return EXACT_MATCH_BADNESS;
  2634.           return INCOMPATIBLE_TYPE_BADNESS;
  2635.         case TYPE_CODE_FUNC:
  2636.           return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
  2637.         case TYPE_CODE_INT:
  2638.           if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
  2639.             {
  2640.               if (value_as_long (value) == 0)
  2641.                 {
  2642.                   /* Null pointer conversion: allow it to be cast to a pointer.
  2643.                      [4.10.1 of C++ standard draft n3290]  */
  2644.                   return NULL_POINTER_CONVERSION_BADNESS;
  2645.                 }
  2646.               else
  2647.                 {
  2648.                   /* If type checking is disabled, allow the conversion.  */
  2649.                   if (!strict_type_checking)
  2650.                     return NS_INTEGER_POINTER_CONVERSION_BADNESS;
  2651.                 }
  2652.             }
  2653.           /* fall through  */
  2654.         case TYPE_CODE_ENUM:
  2655.         case TYPE_CODE_FLAGS:
  2656.         case TYPE_CODE_CHAR:
  2657.         case TYPE_CODE_RANGE:
  2658.         case TYPE_CODE_BOOL:
  2659.         default:
  2660.           return INCOMPATIBLE_TYPE_BADNESS;
  2661.         }
  2662.     case TYPE_CODE_ARRAY:
  2663.       switch (TYPE_CODE (arg))
  2664.         {
  2665.         case TYPE_CODE_PTR:
  2666.         case TYPE_CODE_ARRAY:
  2667.           return rank_one_type (TYPE_TARGET_TYPE (parm),
  2668.                                 TYPE_TARGET_TYPE (arg), NULL);
  2669.         default:
  2670.           return INCOMPATIBLE_TYPE_BADNESS;
  2671.         }
  2672.     case TYPE_CODE_FUNC:
  2673.       switch (TYPE_CODE (arg))
  2674.         {
  2675.         case TYPE_CODE_PTR:        /* funcptr -> func */
  2676.           return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
  2677.         default:
  2678.           return INCOMPATIBLE_TYPE_BADNESS;
  2679.         }
  2680.     case TYPE_CODE_INT:
  2681.       switch (TYPE_CODE (arg))
  2682.         {
  2683.         case TYPE_CODE_INT:
  2684.           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
  2685.             {
  2686.               /* Deal with signed, unsigned, and plain chars and
  2687.                  signed and unsigned ints.  */
  2688.               if (TYPE_NOSIGN (parm))
  2689.                 {
  2690.                   /* This case only for character types.  */
  2691.                   if (TYPE_NOSIGN (arg))
  2692.                     return EXACT_MATCH_BADNESS;        /* plain char -> plain char */
  2693.                   else                /* signed/unsigned char -> plain char */
  2694.                     return INTEGER_CONVERSION_BADNESS;
  2695.                 }
  2696.               else if (TYPE_UNSIGNED (parm))
  2697.                 {
  2698.                   if (TYPE_UNSIGNED (arg))
  2699.                     {
  2700.                       /* unsigned int -> unsigned int, or
  2701.                          unsigned long -> unsigned long */
  2702.                       if (integer_types_same_name_p (TYPE_NAME (parm),
  2703.                                                      TYPE_NAME (arg)))
  2704.                         return EXACT_MATCH_BADNESS;
  2705.                       else if (integer_types_same_name_p (TYPE_NAME (arg),
  2706.                                                           "int")
  2707.                                && integer_types_same_name_p (TYPE_NAME (parm),
  2708.                                                              "long"))
  2709.                         /* unsigned int -> unsigned long */
  2710.                         return INTEGER_PROMOTION_BADNESS;
  2711.                       else
  2712.                         /* unsigned long -> unsigned int */
  2713.                         return INTEGER_CONVERSION_BADNESS;
  2714.                     }
  2715.                   else
  2716.                     {
  2717.                       if (integer_types_same_name_p (TYPE_NAME (arg),
  2718.                                                      "long")
  2719.                           && integer_types_same_name_p (TYPE_NAME (parm),
  2720.                                                         "int"))
  2721.                         /* signed long -> unsigned int */
  2722.                         return INTEGER_CONVERSION_BADNESS;
  2723.                       else
  2724.                         /* signed int/long -> unsigned int/long */
  2725.                         return INTEGER_CONVERSION_BADNESS;
  2726.                     }
  2727.                 }
  2728.               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
  2729.                 {
  2730.                   if (integer_types_same_name_p (TYPE_NAME (parm),
  2731.                                                  TYPE_NAME (arg)))
  2732.                     return EXACT_MATCH_BADNESS;
  2733.                   else if (integer_types_same_name_p (TYPE_NAME (arg),
  2734.                                                       "int")
  2735.                            && integer_types_same_name_p (TYPE_NAME (parm),
  2736.                                                          "long"))
  2737.                     return INTEGER_PROMOTION_BADNESS;
  2738.                   else
  2739.                     return INTEGER_CONVERSION_BADNESS;
  2740.                 }
  2741.               else
  2742.                 return INTEGER_CONVERSION_BADNESS;
  2743.             }
  2744.           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  2745.             return INTEGER_PROMOTION_BADNESS;
  2746.           else
  2747.             return INTEGER_CONVERSION_BADNESS;
  2748.         case TYPE_CODE_ENUM:
  2749.         case TYPE_CODE_FLAGS:
  2750.         case TYPE_CODE_CHAR:
  2751.         case TYPE_CODE_RANGE:
  2752.         case TYPE_CODE_BOOL:
  2753.           if (TYPE_DECLARED_CLASS (arg))
  2754.             return INCOMPATIBLE_TYPE_BADNESS;
  2755.           return INTEGER_PROMOTION_BADNESS;
  2756.         case TYPE_CODE_FLT:
  2757.           return INT_FLOAT_CONVERSION_BADNESS;
  2758.         case TYPE_CODE_PTR:
  2759.           return NS_POINTER_CONVERSION_BADNESS;
  2760.         default:
  2761.           return INCOMPATIBLE_TYPE_BADNESS;
  2762.         }
  2763.       break;
  2764.     case TYPE_CODE_ENUM:
  2765.       switch (TYPE_CODE (arg))
  2766.         {
  2767.         case TYPE_CODE_INT:
  2768.         case TYPE_CODE_CHAR:
  2769.         case TYPE_CODE_RANGE:
  2770.         case TYPE_CODE_BOOL:
  2771.         case TYPE_CODE_ENUM:
  2772.           if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg))
  2773.             return INCOMPATIBLE_TYPE_BADNESS;
  2774.           return INTEGER_CONVERSION_BADNESS;
  2775.         case TYPE_CODE_FLT:
  2776.           return INT_FLOAT_CONVERSION_BADNESS;
  2777.         default:
  2778.           return INCOMPATIBLE_TYPE_BADNESS;
  2779.         }
  2780.       break;
  2781.     case TYPE_CODE_CHAR:
  2782.       switch (TYPE_CODE (arg))
  2783.         {
  2784.         case TYPE_CODE_RANGE:
  2785.         case TYPE_CODE_BOOL:
  2786.         case TYPE_CODE_ENUM:
  2787.           if (TYPE_DECLARED_CLASS (arg))
  2788.             return INCOMPATIBLE_TYPE_BADNESS;
  2789.           return INTEGER_CONVERSION_BADNESS;
  2790.         case TYPE_CODE_FLT:
  2791.           return INT_FLOAT_CONVERSION_BADNESS;
  2792.         case TYPE_CODE_INT:
  2793.           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
  2794.             return INTEGER_CONVERSION_BADNESS;
  2795.           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  2796.             return INTEGER_PROMOTION_BADNESS;
  2797.           /* >>> !! else fall through !! <<< */
  2798.         case TYPE_CODE_CHAR:
  2799.           /* Deal with signed, unsigned, and plain chars for C++ and
  2800.              with int cases falling through from previous case.  */
  2801.           if (TYPE_NOSIGN (parm))
  2802.             {
  2803.               if (TYPE_NOSIGN (arg))
  2804.                 return EXACT_MATCH_BADNESS;
  2805.               else
  2806.                 return INTEGER_CONVERSION_BADNESS;
  2807.             }
  2808.           else if (TYPE_UNSIGNED (parm))
  2809.             {
  2810.               if (TYPE_UNSIGNED (arg))
  2811.                 return EXACT_MATCH_BADNESS;
  2812.               else
  2813.                 return INTEGER_PROMOTION_BADNESS;
  2814.             }
  2815.           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
  2816.             return EXACT_MATCH_BADNESS;
  2817.           else
  2818.             return INTEGER_CONVERSION_BADNESS;
  2819.         default:
  2820.           return INCOMPATIBLE_TYPE_BADNESS;
  2821.         }
  2822.       break;
  2823.     case TYPE_CODE_RANGE:
  2824.       switch (TYPE_CODE (arg))
  2825.         {
  2826.         case TYPE_CODE_INT:
  2827.         case TYPE_CODE_CHAR:
  2828.         case TYPE_CODE_RANGE:
  2829.         case TYPE_CODE_BOOL:
  2830.         case TYPE_CODE_ENUM:
  2831.           return INTEGER_CONVERSION_BADNESS;
  2832.         case TYPE_CODE_FLT:
  2833.           return INT_FLOAT_CONVERSION_BADNESS;
  2834.         default:
  2835.           return INCOMPATIBLE_TYPE_BADNESS;
  2836.         }
  2837.       break;
  2838.     case TYPE_CODE_BOOL:
  2839.       switch (TYPE_CODE (arg))
  2840.         {
  2841.           /* n3290 draft, section 4.12.1 (conv.bool):

  2842.              "A prvalue of arithmetic, unscoped enumeration, pointer, or
  2843.              pointer to member type can be converted to a prvalue of type
  2844.              boolA zero value, null pointer value, or null member pointer
  2845.              value is converted to false; any other value is converted to
  2846.              trueA prvalue of type std::nullptr_t can be converted to a
  2847.              prvalue of type bool; the resulting value is false."  */
  2848.         case TYPE_CODE_INT:
  2849.         case TYPE_CODE_CHAR:
  2850.         case TYPE_CODE_ENUM:
  2851.         case TYPE_CODE_FLT:
  2852.         case TYPE_CODE_MEMBERPTR:
  2853.         case TYPE_CODE_PTR:
  2854.           return BOOL_CONVERSION_BADNESS;
  2855.         case TYPE_CODE_RANGE:
  2856.           return INCOMPATIBLE_TYPE_BADNESS;
  2857.         case TYPE_CODE_BOOL:
  2858.           return EXACT_MATCH_BADNESS;
  2859.         default:
  2860.           return INCOMPATIBLE_TYPE_BADNESS;
  2861.         }
  2862.       break;
  2863.     case TYPE_CODE_FLT:
  2864.       switch (TYPE_CODE (arg))
  2865.         {
  2866.         case TYPE_CODE_FLT:
  2867.           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  2868.             return FLOAT_PROMOTION_BADNESS;
  2869.           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
  2870.             return EXACT_MATCH_BADNESS;
  2871.           else
  2872.             return FLOAT_CONVERSION_BADNESS;
  2873.         case TYPE_CODE_INT:
  2874.         case TYPE_CODE_BOOL:
  2875.         case TYPE_CODE_ENUM:
  2876.         case TYPE_CODE_RANGE:
  2877.         case TYPE_CODE_CHAR:
  2878.           return INT_FLOAT_CONVERSION_BADNESS;
  2879.         default:
  2880.           return INCOMPATIBLE_TYPE_BADNESS;
  2881.         }
  2882.       break;
  2883.     case TYPE_CODE_COMPLEX:
  2884.       switch (TYPE_CODE (arg))
  2885.         {                /* Strictly not needed for C++, but...  */
  2886.         case TYPE_CODE_FLT:
  2887.           return FLOAT_PROMOTION_BADNESS;
  2888.         case TYPE_CODE_COMPLEX:
  2889.           return EXACT_MATCH_BADNESS;
  2890.         default:
  2891.           return INCOMPATIBLE_TYPE_BADNESS;
  2892.         }
  2893.       break;
  2894.     case TYPE_CODE_STRUCT:
  2895.       switch (TYPE_CODE (arg))
  2896.         {
  2897.         case TYPE_CODE_STRUCT:
  2898.           /* Check for derivation */
  2899.           rank.subrank = distance_to_ancestor (parm, arg, 0);
  2900.           if (rank.subrank >= 0)
  2901.             return sum_ranks (BASE_CONVERSION_BADNESS, rank);
  2902.           /* else fall through */
  2903.         default:
  2904.           return INCOMPATIBLE_TYPE_BADNESS;
  2905.         }
  2906.       break;
  2907.     case TYPE_CODE_UNION:
  2908.       switch (TYPE_CODE (arg))
  2909.         {
  2910.         case TYPE_CODE_UNION:
  2911.         default:
  2912.           return INCOMPATIBLE_TYPE_BADNESS;
  2913.         }
  2914.       break;
  2915.     case TYPE_CODE_MEMBERPTR:
  2916.       switch (TYPE_CODE (arg))
  2917.         {
  2918.         default:
  2919.           return INCOMPATIBLE_TYPE_BADNESS;
  2920.         }
  2921.       break;
  2922.     case TYPE_CODE_METHOD:
  2923.       switch (TYPE_CODE (arg))
  2924.         {

  2925.         default:
  2926.           return INCOMPATIBLE_TYPE_BADNESS;
  2927.         }
  2928.       break;
  2929.     case TYPE_CODE_REF:
  2930.       switch (TYPE_CODE (arg))
  2931.         {

  2932.         default:
  2933.           return INCOMPATIBLE_TYPE_BADNESS;
  2934.         }

  2935.       break;
  2936.     case TYPE_CODE_SET:
  2937.       switch (TYPE_CODE (arg))
  2938.         {
  2939.           /* Not in C++ */
  2940.         case TYPE_CODE_SET:
  2941.           return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
  2942.                                 TYPE_FIELD_TYPE (arg, 0), NULL);
  2943.         default:
  2944.           return INCOMPATIBLE_TYPE_BADNESS;
  2945.         }
  2946.       break;
  2947.     case TYPE_CODE_VOID:
  2948.     default:
  2949.       return INCOMPATIBLE_TYPE_BADNESS;
  2950.     }                                /* switch (TYPE_CODE (arg)) */
  2951. }

  2952. /* End of functions for overload resolution.  */

  2953. /* Routines to pretty-print types.  */

  2954. static void
  2955. print_bit_vector (B_TYPE *bits, int nbits)
  2956. {
  2957.   int bitno;

  2958.   for (bitno = 0; bitno < nbits; bitno++)
  2959.     {
  2960.       if ((bitno % 8) == 0)
  2961.         {
  2962.           puts_filtered (" ");
  2963.         }
  2964.       if (B_TST (bits, bitno))
  2965.         printf_filtered (("1"));
  2966.       else
  2967.         printf_filtered (("0"));
  2968.     }
  2969. }

  2970. /* Note the first arg should be the "this" pointer, we may not want to
  2971.    include it since we may get into a infinitely recursive
  2972.    situation.  */

  2973. static void
  2974. print_args (struct field *args, int nargs, int spaces)
  2975. {
  2976.   if (args != NULL)
  2977.     {
  2978.       int i;

  2979.       for (i = 0; i < nargs; i++)
  2980.         {
  2981.           printfi_filtered (spaces, "[%d] name '%s'\n", i,
  2982.                             args[i].name != NULL ? args[i].name : "<NULL>");
  2983.           recursive_dump_type (args[i].type, spaces + 2);
  2984.         }
  2985.     }
  2986. }

  2987. int
  2988. field_is_static (struct field *f)
  2989. {
  2990.   /* "static" fields are the fields whose location is not relative
  2991.      to the address of the enclosing struct.  It would be nice to
  2992.      have a dedicated flag that would be set for static fields when
  2993.      the type is being created.  But in practice, checking the field
  2994.      loc_kind should give us an accurate answer.  */
  2995.   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
  2996.           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
  2997. }

  2998. static void
  2999. dump_fn_fieldlists (struct type *type, int spaces)
  3000. {
  3001.   int method_idx;
  3002.   int overload_idx;
  3003.   struct fn_field *f;

  3004.   printfi_filtered (spaces, "fn_fieldlists ");
  3005.   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
  3006.   printf_filtered ("\n");
  3007.   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
  3008.     {
  3009.       f = TYPE_FN_FIELDLIST1 (type, method_idx);
  3010.       printfi_filtered (spaces + 2, "[%d] name '%s' (",
  3011.                         method_idx,
  3012.                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
  3013.       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
  3014.                               gdb_stdout);
  3015.       printf_filtered (_(") length %d\n"),
  3016.                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
  3017.       for (overload_idx = 0;
  3018.            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
  3019.            overload_idx++)
  3020.         {
  3021.           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
  3022.                             overload_idx,
  3023.                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
  3024.           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
  3025.                                   gdb_stdout);
  3026.           printf_filtered (")\n");
  3027.           printfi_filtered (spaces + 8, "type ");
  3028.           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
  3029.                                   gdb_stdout);
  3030.           printf_filtered ("\n");

  3031.           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
  3032.                                spaces + 8 + 2);

  3033.           printfi_filtered (spaces + 8, "args ");
  3034.           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
  3035.                                   gdb_stdout);
  3036.           printf_filtered ("\n");
  3037.           print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
  3038.                       TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)),
  3039.                       spaces + 8 + 2);
  3040.           printfi_filtered (spaces + 8, "fcontext ");
  3041.           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
  3042.                                   gdb_stdout);
  3043.           printf_filtered ("\n");

  3044.           printfi_filtered (spaces + 8, "is_const %d\n",
  3045.                             TYPE_FN_FIELD_CONST (f, overload_idx));
  3046.           printfi_filtered (spaces + 8, "is_volatile %d\n",
  3047.                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
  3048.           printfi_filtered (spaces + 8, "is_private %d\n",
  3049.                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
  3050.           printfi_filtered (spaces + 8, "is_protected %d\n",
  3051.                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
  3052.           printfi_filtered (spaces + 8, "is_stub %d\n",
  3053.                             TYPE_FN_FIELD_STUB (f, overload_idx));
  3054.           printfi_filtered (spaces + 8, "voffset %u\n",
  3055.                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
  3056.         }
  3057.     }
  3058. }

  3059. static void
  3060. print_cplus_stuff (struct type *type, int spaces)
  3061. {
  3062.   printfi_filtered (spaces, "n_baseclasses %d\n",
  3063.                     TYPE_N_BASECLASSES (type));
  3064.   printfi_filtered (spaces, "nfn_fields %d\n",
  3065.                     TYPE_NFN_FIELDS (type));
  3066.   if (TYPE_N_BASECLASSES (type) > 0)
  3067.     {
  3068.       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
  3069.                         TYPE_N_BASECLASSES (type));
  3070.       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
  3071.                               gdb_stdout);
  3072.       printf_filtered (")");

  3073.       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
  3074.                         TYPE_N_BASECLASSES (type));
  3075.       puts_filtered ("\n");
  3076.     }
  3077.   if (TYPE_NFIELDS (type) > 0)
  3078.     {
  3079.       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
  3080.         {
  3081.           printfi_filtered (spaces,
  3082.                             "private_field_bits (%d bits at *",
  3083.                             TYPE_NFIELDS (type));
  3084.           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
  3085.                                   gdb_stdout);
  3086.           printf_filtered (")");
  3087.           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
  3088.                             TYPE_NFIELDS (type));
  3089.           puts_filtered ("\n");
  3090.         }
  3091.       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
  3092.         {
  3093.           printfi_filtered (spaces,
  3094.                             "protected_field_bits (%d bits at *",
  3095.                             TYPE_NFIELDS (type));
  3096.           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
  3097.                                   gdb_stdout);
  3098.           printf_filtered (")");
  3099.           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
  3100.                             TYPE_NFIELDS (type));
  3101.           puts_filtered ("\n");
  3102.         }
  3103.     }
  3104.   if (TYPE_NFN_FIELDS (type) > 0)
  3105.     {
  3106.       dump_fn_fieldlists (type, spaces);
  3107.     }
  3108. }

  3109. /* Print the contents of the TYPE's type_specific union, assuming that
  3110.    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */

  3111. static void
  3112. print_gnat_stuff (struct type *type, int spaces)
  3113. {
  3114.   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);

  3115.   recursive_dump_type (descriptive_type, spaces + 2);
  3116. }

  3117. static struct obstack dont_print_type_obstack;

  3118. void
  3119. recursive_dump_type (struct type *type, int spaces)
  3120. {
  3121.   int idx;

  3122.   if (spaces == 0)
  3123.     obstack_begin (&dont_print_type_obstack, 0);

  3124.   if (TYPE_NFIELDS (type) > 0
  3125.       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
  3126.     {
  3127.       struct type **first_dont_print
  3128.         = (struct type **) obstack_base (&dont_print_type_obstack);

  3129.       int i = (struct type **)
  3130.         obstack_next_free (&dont_print_type_obstack) - first_dont_print;

  3131.       while (--i >= 0)
  3132.         {
  3133.           if (type == first_dont_print[i])
  3134.             {
  3135.               printfi_filtered (spaces, "type node ");
  3136.               gdb_print_host_address (type, gdb_stdout);
  3137.               printf_filtered (_(" <same as already seen type>\n"));
  3138.               return;
  3139.             }
  3140.         }

  3141.       obstack_ptr_grow (&dont_print_type_obstack, type);
  3142.     }

  3143.   printfi_filtered (spaces, "type node ");
  3144.   gdb_print_host_address (type, gdb_stdout);
  3145.   printf_filtered ("\n");
  3146.   printfi_filtered (spaces, "name '%s' (",
  3147.                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
  3148.   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
  3149.   printf_filtered (")\n");
  3150.   printfi_filtered (spaces, "tagname '%s' (",
  3151.                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
  3152.   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
  3153.   printf_filtered (")\n");
  3154.   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
  3155.   switch (TYPE_CODE (type))
  3156.     {
  3157.     case TYPE_CODE_UNDEF:
  3158.       printf_filtered ("(TYPE_CODE_UNDEF)");
  3159.       break;
  3160.     case TYPE_CODE_PTR:
  3161.       printf_filtered ("(TYPE_CODE_PTR)");
  3162.       break;
  3163.     case TYPE_CODE_ARRAY:
  3164.       printf_filtered ("(TYPE_CODE_ARRAY)");
  3165.       break;
  3166.     case TYPE_CODE_STRUCT:
  3167.       printf_filtered ("(TYPE_CODE_STRUCT)");
  3168.       break;
  3169.     case TYPE_CODE_UNION:
  3170.       printf_filtered ("(TYPE_CODE_UNION)");
  3171.       break;
  3172.     case TYPE_CODE_ENUM:
  3173.       printf_filtered ("(TYPE_CODE_ENUM)");
  3174.       break;
  3175.     case TYPE_CODE_FLAGS:
  3176.       printf_filtered ("(TYPE_CODE_FLAGS)");
  3177.       break;
  3178.     case TYPE_CODE_FUNC:
  3179.       printf_filtered ("(TYPE_CODE_FUNC)");
  3180.       break;
  3181.     case TYPE_CODE_INT:
  3182.       printf_filtered ("(TYPE_CODE_INT)");
  3183.       break;
  3184.     case TYPE_CODE_FLT:
  3185.       printf_filtered ("(TYPE_CODE_FLT)");
  3186.       break;
  3187.     case TYPE_CODE_VOID:
  3188.       printf_filtered ("(TYPE_CODE_VOID)");
  3189.       break;
  3190.     case TYPE_CODE_SET:
  3191.       printf_filtered ("(TYPE_CODE_SET)");
  3192.       break;
  3193.     case TYPE_CODE_RANGE:
  3194.       printf_filtered ("(TYPE_CODE_RANGE)");
  3195.       break;
  3196.     case TYPE_CODE_STRING:
  3197.       printf_filtered ("(TYPE_CODE_STRING)");
  3198.       break;
  3199.     case TYPE_CODE_ERROR:
  3200.       printf_filtered ("(TYPE_CODE_ERROR)");
  3201.       break;
  3202.     case TYPE_CODE_MEMBERPTR:
  3203.       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
  3204.       break;
  3205.     case TYPE_CODE_METHODPTR:
  3206.       printf_filtered ("(TYPE_CODE_METHODPTR)");
  3207.       break;
  3208.     case TYPE_CODE_METHOD:
  3209.       printf_filtered ("(TYPE_CODE_METHOD)");
  3210.       break;
  3211.     case TYPE_CODE_REF:
  3212.       printf_filtered ("(TYPE_CODE_REF)");
  3213.       break;
  3214.     case TYPE_CODE_CHAR:
  3215.       printf_filtered ("(TYPE_CODE_CHAR)");
  3216.       break;
  3217.     case TYPE_CODE_BOOL:
  3218.       printf_filtered ("(TYPE_CODE_BOOL)");
  3219.       break;
  3220.     case TYPE_CODE_COMPLEX:
  3221.       printf_filtered ("(TYPE_CODE_COMPLEX)");
  3222.       break;
  3223.     case TYPE_CODE_TYPEDEF:
  3224.       printf_filtered ("(TYPE_CODE_TYPEDEF)");
  3225.       break;
  3226.     case TYPE_CODE_NAMESPACE:
  3227.       printf_filtered ("(TYPE_CODE_NAMESPACE)");
  3228.       break;
  3229.     default:
  3230.       printf_filtered ("(UNKNOWN TYPE CODE)");
  3231.       break;
  3232.     }
  3233.   puts_filtered ("\n");
  3234.   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
  3235.   if (TYPE_OBJFILE_OWNED (type))
  3236.     {
  3237.       printfi_filtered (spaces, "objfile ");
  3238.       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
  3239.     }
  3240.   else
  3241.     {
  3242.       printfi_filtered (spaces, "gdbarch ");
  3243.       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
  3244.     }
  3245.   printf_filtered ("\n");
  3246.   printfi_filtered (spaces, "target_type ");
  3247.   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
  3248.   printf_filtered ("\n");
  3249.   if (TYPE_TARGET_TYPE (type) != NULL)
  3250.     {
  3251.       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
  3252.     }
  3253.   printfi_filtered (spaces, "pointer_type ");
  3254.   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
  3255.   printf_filtered ("\n");
  3256.   printfi_filtered (spaces, "reference_type ");
  3257.   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
  3258.   printf_filtered ("\n");
  3259.   printfi_filtered (spaces, "type_chain ");
  3260.   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
  3261.   printf_filtered ("\n");
  3262.   printfi_filtered (spaces, "instance_flags 0x%x",
  3263.                     TYPE_INSTANCE_FLAGS (type));
  3264.   if (TYPE_CONST (type))
  3265.     {
  3266.       puts_filtered (" TYPE_FLAG_CONST");
  3267.     }
  3268.   if (TYPE_VOLATILE (type))
  3269.     {
  3270.       puts_filtered (" TYPE_FLAG_VOLATILE");
  3271.     }
  3272.   if (TYPE_CODE_SPACE (type))
  3273.     {
  3274.       puts_filtered (" TYPE_FLAG_CODE_SPACE");
  3275.     }
  3276.   if (TYPE_DATA_SPACE (type))
  3277.     {
  3278.       puts_filtered (" TYPE_FLAG_DATA_SPACE");
  3279.     }
  3280.   if (TYPE_ADDRESS_CLASS_1 (type))
  3281.     {
  3282.       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
  3283.     }
  3284.   if (TYPE_ADDRESS_CLASS_2 (type))
  3285.     {
  3286.       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
  3287.     }
  3288.   if (TYPE_RESTRICT (type))
  3289.     {
  3290.       puts_filtered (" TYPE_FLAG_RESTRICT");
  3291.     }
  3292.   puts_filtered ("\n");

  3293.   printfi_filtered (spaces, "flags");
  3294.   if (TYPE_UNSIGNED (type))
  3295.     {
  3296.       puts_filtered (" TYPE_FLAG_UNSIGNED");
  3297.     }
  3298.   if (TYPE_NOSIGN (type))
  3299.     {
  3300.       puts_filtered (" TYPE_FLAG_NOSIGN");
  3301.     }
  3302.   if (TYPE_STUB (type))
  3303.     {
  3304.       puts_filtered (" TYPE_FLAG_STUB");
  3305.     }
  3306.   if (TYPE_TARGET_STUB (type))
  3307.     {
  3308.       puts_filtered (" TYPE_FLAG_TARGET_STUB");
  3309.     }
  3310.   if (TYPE_STATIC (type))
  3311.     {
  3312.       puts_filtered (" TYPE_FLAG_STATIC");
  3313.     }
  3314.   if (TYPE_PROTOTYPED (type))
  3315.     {
  3316.       puts_filtered (" TYPE_FLAG_PROTOTYPED");
  3317.     }
  3318.   if (TYPE_INCOMPLETE (type))
  3319.     {
  3320.       puts_filtered (" TYPE_FLAG_INCOMPLETE");
  3321.     }
  3322.   if (TYPE_VARARGS (type))
  3323.     {
  3324.       puts_filtered (" TYPE_FLAG_VARARGS");
  3325.     }
  3326.   /* This is used for things like AltiVec registers on ppc.  Gcc emits
  3327.      an attribute for the array type, which tells whether or not we
  3328.      have a vector, instead of a regular array.  */
  3329.   if (TYPE_VECTOR (type))
  3330.     {
  3331.       puts_filtered (" TYPE_FLAG_VECTOR");
  3332.     }
  3333.   if (TYPE_FIXED_INSTANCE (type))
  3334.     {
  3335.       puts_filtered (" TYPE_FIXED_INSTANCE");
  3336.     }
  3337.   if (TYPE_STUB_SUPPORTED (type))
  3338.     {
  3339.       puts_filtered (" TYPE_STUB_SUPPORTED");
  3340.     }
  3341.   if (TYPE_NOTTEXT (type))
  3342.     {
  3343.       puts_filtered (" TYPE_NOTTEXT");
  3344.     }
  3345.   puts_filtered ("\n");
  3346.   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
  3347.   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
  3348.   puts_filtered ("\n");
  3349.   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
  3350.     {
  3351.       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
  3352.         printfi_filtered (spaces + 2,
  3353.                           "[%d] enumval %s type ",
  3354.                           idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
  3355.       else
  3356.         printfi_filtered (spaces + 2,
  3357.                           "[%d] bitpos %d bitsize %d type ",
  3358.                           idx, TYPE_FIELD_BITPOS (type, idx),
  3359.                           TYPE_FIELD_BITSIZE (type, idx));
  3360.       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
  3361.       printf_filtered (" name '%s' (",
  3362.                        TYPE_FIELD_NAME (type, idx) != NULL
  3363.                        ? TYPE_FIELD_NAME (type, idx)
  3364.                        : "<NULL>");
  3365.       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
  3366.       printf_filtered (")\n");
  3367.       if (TYPE_FIELD_TYPE (type, idx) != NULL)
  3368.         {
  3369.           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
  3370.         }
  3371.     }
  3372.   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
  3373.     {
  3374.       printfi_filtered (spaces, "low %s%s  high %s%s\n",
  3375.                         plongest (TYPE_LOW_BOUND (type)),
  3376.                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
  3377.                         plongest (TYPE_HIGH_BOUND (type)),
  3378.                         TYPE_HIGH_BOUND_UNDEFINED (type)
  3379.                         ? " (undefined)" : "");
  3380.     }
  3381.   printfi_filtered (spaces, "vptr_basetype ");
  3382.   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
  3383.   puts_filtered ("\n");
  3384.   if (TYPE_VPTR_BASETYPE (type) != NULL)
  3385.     {
  3386.       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
  3387.     }
  3388.   printfi_filtered (spaces, "vptr_fieldno %d\n",
  3389.                     TYPE_VPTR_FIELDNO (type));

  3390.   switch (TYPE_SPECIFIC_FIELD (type))
  3391.     {
  3392.       case TYPE_SPECIFIC_CPLUS_STUFF:
  3393.         printfi_filtered (spaces, "cplus_stuff ");
  3394.         gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
  3395.                                 gdb_stdout);
  3396.         puts_filtered ("\n");
  3397.         print_cplus_stuff (type, spaces);
  3398.         break;

  3399.       case TYPE_SPECIFIC_GNAT_STUFF:
  3400.         printfi_filtered (spaces, "gnat_stuff ");
  3401.         gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
  3402.         puts_filtered ("\n");
  3403.         print_gnat_stuff (type, spaces);
  3404.         break;

  3405.       case TYPE_SPECIFIC_FLOATFORMAT:
  3406.         printfi_filtered (spaces, "floatformat ");
  3407.         if (TYPE_FLOATFORMAT (type) == NULL)
  3408.           puts_filtered ("(null)");
  3409.         else
  3410.           {
  3411.             puts_filtered ("{ ");
  3412.             if (TYPE_FLOATFORMAT (type)[0] == NULL
  3413.                 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
  3414.               puts_filtered ("(null)");
  3415.             else
  3416.               puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);

  3417.             puts_filtered (", ");
  3418.             if (TYPE_FLOATFORMAT (type)[1] == NULL
  3419.                 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
  3420.               puts_filtered ("(null)");
  3421.             else
  3422.               puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);

  3423.             puts_filtered (" }");
  3424.           }
  3425.         puts_filtered ("\n");
  3426.         break;

  3427.       case TYPE_SPECIFIC_FUNC:
  3428.         printfi_filtered (spaces, "calling_convention %d\n",
  3429.                           TYPE_CALLING_CONVENTION (type));
  3430.         /* tail_call_list is not printed.  */
  3431.         break;
  3432.     }

  3433.   if (spaces == 0)
  3434.     obstack_free (&dont_print_type_obstack, NULL);
  3435. }

  3436. /* Trivial helpers for the libiberty hash table, for mapping one
  3437.    type to another.  */

  3438. struct type_pair
  3439. {
  3440.   struct type *old, *new;
  3441. };

  3442. static hashval_t
  3443. type_pair_hash (const void *item)
  3444. {
  3445.   const struct type_pair *pair = item;

  3446.   return htab_hash_pointer (pair->old);
  3447. }

  3448. static int
  3449. type_pair_eq (const void *item_lhs, const void *item_rhs)
  3450. {
  3451.   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;

  3452.   return lhs->old == rhs->old;
  3453. }

  3454. /* Allocate the hash table used by copy_type_recursive to walk
  3455.    types without duplicates.  We use OBJFILE's obstack, because
  3456.    OBJFILE is about to be deleted.  */

  3457. htab_t
  3458. create_copied_types_hash (struct objfile *objfile)
  3459. {
  3460.   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
  3461.                                NULL, &objfile->objfile_obstack,
  3462.                                hashtab_obstack_allocate,
  3463.                                dummy_obstack_deallocate);
  3464. }

  3465. /* Recursively copy (deep copy) TYPE, if it is associated with
  3466.    OBJFILE.  Return a new type allocated using malloc, a saved type if
  3467.    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
  3468.    not associated with OBJFILE.  */

  3469. struct type *
  3470. copy_type_recursive (struct objfile *objfile,
  3471.                      struct type *type,
  3472.                      htab_t copied_types)
  3473. {
  3474.   struct type_pair *stored, pair;
  3475.   void **slot;
  3476.   struct type *new_type;

  3477.   if (! TYPE_OBJFILE_OWNED (type))
  3478.     return type;

  3479.   /* This type shouldn't be pointing to any types in other objfiles;
  3480.      if it did, the type might disappear unexpectedly.  */
  3481.   gdb_assert (TYPE_OBJFILE (type) == objfile);

  3482.   pair.old = type;
  3483.   slot = htab_find_slot (copied_types, &pair, INSERT);
  3484.   if (*slot != NULL)
  3485.     return ((struct type_pair *) *slot)->new;

  3486.   new_type = alloc_type_arch (get_type_arch (type));

  3487.   /* We must add the new type to the hash table immediately, in case
  3488.      we encounter this type again during a recursive call below.  */
  3489.   stored
  3490.     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
  3491.   stored->old = type;
  3492.   stored->new = new_type;
  3493.   *slot = stored;

  3494.   /* Copy the common fields of types.  For the main type, we simply
  3495.      copy the entire thing and then update specific fields as needed.  */
  3496.   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
  3497.   TYPE_OBJFILE_OWNED (new_type) = 0;
  3498.   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);

  3499.   if (TYPE_NAME (type))
  3500.     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
  3501.   if (TYPE_TAG_NAME (type))
  3502.     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));

  3503.   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
  3504.   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);

  3505.   /* Copy the fields.  */
  3506.   if (TYPE_NFIELDS (type))
  3507.     {
  3508.       int i, nfields;

  3509.       nfields = TYPE_NFIELDS (type);
  3510.       TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
  3511.       for (i = 0; i < nfields; i++)
  3512.         {
  3513.           TYPE_FIELD_ARTIFICIAL (new_type, i) =
  3514.             TYPE_FIELD_ARTIFICIAL (type, i);
  3515.           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
  3516.           if (TYPE_FIELD_TYPE (type, i))
  3517.             TYPE_FIELD_TYPE (new_type, i)
  3518.               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
  3519.                                      copied_types);
  3520.           if (TYPE_FIELD_NAME (type, i))
  3521.             TYPE_FIELD_NAME (new_type, i) =
  3522.               xstrdup (TYPE_FIELD_NAME (type, i));
  3523.           switch (TYPE_FIELD_LOC_KIND (type, i))
  3524.             {
  3525.             case FIELD_LOC_KIND_BITPOS:
  3526.               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
  3527.                                 TYPE_FIELD_BITPOS (type, i));
  3528.               break;
  3529.             case FIELD_LOC_KIND_ENUMVAL:
  3530.               SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
  3531.                                  TYPE_FIELD_ENUMVAL (type, i));
  3532.               break;
  3533.             case FIELD_LOC_KIND_PHYSADDR:
  3534.               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
  3535.                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
  3536.               break;
  3537.             case FIELD_LOC_KIND_PHYSNAME:
  3538.               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
  3539.                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
  3540.                                                                        i)));
  3541.               break;
  3542.             default:
  3543.               internal_error (__FILE__, __LINE__,
  3544.                               _("Unexpected type field location kind: %d"),
  3545.                               TYPE_FIELD_LOC_KIND (type, i));
  3546.             }
  3547.         }
  3548.     }

  3549.   /* For range types, copy the bounds information.  */
  3550.   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
  3551.     {
  3552.       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
  3553.       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
  3554.     }

  3555.   /* Copy the data location information.  */
  3556.   if (TYPE_DATA_LOCATION (type) != NULL)
  3557.     {
  3558.       TYPE_DATA_LOCATION (new_type)
  3559.         = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
  3560.       memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
  3561.               sizeof (struct dynamic_prop));
  3562.     }

  3563.   /* Copy pointers to other types.  */
  3564.   if (TYPE_TARGET_TYPE (type))
  3565.     TYPE_TARGET_TYPE (new_type) =
  3566.       copy_type_recursive (objfile,
  3567.                            TYPE_TARGET_TYPE (type),
  3568.                            copied_types);
  3569.   if (TYPE_VPTR_BASETYPE (type))
  3570.     TYPE_VPTR_BASETYPE (new_type) =
  3571.       copy_type_recursive (objfile,
  3572.                            TYPE_VPTR_BASETYPE (type),
  3573.                            copied_types);
  3574.   /* Maybe copy the type_specific bits.

  3575.      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
  3576.      base classes and methods.  There's no fundamental reason why we
  3577.      can't, but at the moment it is not needed.  */

  3578.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  3579.     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
  3580.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  3581.            || TYPE_CODE (type) == TYPE_CODE_UNION
  3582.            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
  3583.     INIT_CPLUS_SPECIFIC (new_type);

  3584.   return new_type;
  3585. }

  3586. /* Make a copy of the given TYPE, except that the pointer & reference
  3587.    types are not preserved.

  3588.    This function assumes that the given type has an associated objfile.
  3589.    This objfile is used to allocate the new type.  */

  3590. struct type *
  3591. copy_type (const struct type *type)
  3592. {
  3593.   struct type *new_type;

  3594.   gdb_assert (TYPE_OBJFILE_OWNED (type));

  3595.   new_type = alloc_type_copy (type);
  3596.   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
  3597.   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
  3598.   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
  3599.           sizeof (struct main_type));
  3600.   if (TYPE_DATA_LOCATION (type) != NULL)
  3601.     {
  3602.       TYPE_DATA_LOCATION (new_type)
  3603.         = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
  3604.       memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
  3605.               sizeof (struct dynamic_prop));
  3606.     }

  3607.   return new_type;
  3608. }

  3609. /* Helper functions to initialize architecture-specific types.  */

  3610. /* Allocate a type structure associated with GDBARCH and set its
  3611.    CODE, LENGTH, and NAME fields.  */

  3612. struct type *
  3613. arch_type (struct gdbarch *gdbarch,
  3614.            enum type_code code, int length, char *name)
  3615. {
  3616.   struct type *type;

  3617.   type = alloc_type_arch (gdbarch);
  3618.   TYPE_CODE (type) = code;
  3619.   TYPE_LENGTH (type) = length;

  3620.   if (name)
  3621.     TYPE_NAME (type) = xstrdup (name);

  3622.   return type;
  3623. }

  3624. /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
  3625.    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
  3626.    the type's TYPE_UNSIGNED flagNAME is the type name.  */

  3627. struct type *
  3628. arch_integer_type (struct gdbarch *gdbarch,
  3629.                    int bit, int unsigned_p, char *name)
  3630. {
  3631.   struct type *t;

  3632.   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
  3633.   if (unsigned_p)
  3634.     TYPE_UNSIGNED (t) = 1;
  3635.   if (name && strcmp (name, "char") == 0)
  3636.     TYPE_NOSIGN (t) = 1;

  3637.   return t;
  3638. }

  3639. /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
  3640.    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
  3641.    the type's TYPE_UNSIGNED flagNAME is the type name.  */

  3642. struct type *
  3643. arch_character_type (struct gdbarch *gdbarch,
  3644.                      int bit, int unsigned_p, char *name)
  3645. {
  3646.   struct type *t;

  3647.   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
  3648.   if (unsigned_p)
  3649.     TYPE_UNSIGNED (t) = 1;

  3650.   return t;
  3651. }

  3652. /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
  3653.    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
  3654.    the type's TYPE_UNSIGNED flagNAME is the type name.  */

  3655. struct type *
  3656. arch_boolean_type (struct gdbarch *gdbarch,
  3657.                    int bit, int unsigned_p, char *name)
  3658. {
  3659.   struct type *t;

  3660.   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
  3661.   if (unsigned_p)
  3662.     TYPE_UNSIGNED (t) = 1;

  3663.   return t;
  3664. }

  3665. /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
  3666.    BIT is the type size in bits; if BIT equals -1, the size is
  3667.    determined by the floatformat.  NAME is the type name.  Set the
  3668.    TYPE_FLOATFORMAT from FLOATFORMATS.  */

  3669. struct type *
  3670. arch_float_type (struct gdbarch *gdbarch,
  3671.                  int bit, char *name, const struct floatformat **floatformats)
  3672. {
  3673.   struct type *t;

  3674.   if (bit == -1)
  3675.     {
  3676.       gdb_assert (floatformats != NULL);
  3677.       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
  3678.       bit = floatformats[0]->totalsize;
  3679.     }
  3680.   gdb_assert (bit >= 0);

  3681.   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
  3682.   TYPE_FLOATFORMAT (t) = floatformats;
  3683.   return t;
  3684. }

  3685. /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
  3686.    NAME is the type name.  TARGET_TYPE is the component float type.  */

  3687. struct type *
  3688. arch_complex_type (struct gdbarch *gdbarch,
  3689.                    char *name, struct type *target_type)
  3690. {
  3691.   struct type *t;

  3692.   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
  3693.                  2 * TYPE_LENGTH (target_type), name);
  3694.   TYPE_TARGET_TYPE (t) = target_type;
  3695.   return t;
  3696. }

  3697. /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
  3698.    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */

  3699. struct type *
  3700. arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
  3701. {
  3702.   int nfields = length * TARGET_CHAR_BIT;
  3703.   struct type *type;

  3704.   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
  3705.   TYPE_UNSIGNED (type) = 1;
  3706.   TYPE_NFIELDS (type) = nfields;
  3707.   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));

  3708.   return type;
  3709. }

  3710. /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
  3711.    position BITPOS is called NAME.  */

  3712. void
  3713. append_flags_type_flag (struct type *type, int bitpos, char *name)
  3714. {
  3715.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
  3716.   gdb_assert (bitpos < TYPE_NFIELDS (type));
  3717.   gdb_assert (bitpos >= 0);

  3718.   if (name)
  3719.     {
  3720.       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
  3721.       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
  3722.     }
  3723.   else
  3724.     {
  3725.       /* Don't show this field to the user.  */
  3726.       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
  3727.     }
  3728. }

  3729. /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
  3730.    specified by CODE) associated with GDBARCH.  NAME is the type name.  */

  3731. struct type *
  3732. arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
  3733. {
  3734.   struct type *t;

  3735.   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
  3736.   t = arch_type (gdbarch, code, 0, NULL);
  3737.   TYPE_TAG_NAME (t) = name;
  3738.   INIT_CPLUS_SPECIFIC (t);
  3739.   return t;
  3740. }

  3741. /* Add new field with name NAME and type FIELD to composite type T.
  3742.    Do not set the field's position or adjust the type's length;
  3743.    the caller should do so.  Return the new field.  */

  3744. struct field *
  3745. append_composite_type_field_raw (struct type *t, char *name,
  3746.                                  struct type *field)
  3747. {
  3748.   struct field *f;

  3749.   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
  3750.   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
  3751.                               sizeof (struct field) * TYPE_NFIELDS (t));
  3752.   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
  3753.   memset (f, 0, sizeof f[0]);
  3754.   FIELD_TYPE (f[0]) = field;
  3755.   FIELD_NAME (f[0]) = name;
  3756.   return f;
  3757. }

  3758. /* Add new field with name NAME and type FIELD to composite type T.
  3759.    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */

  3760. void
  3761. append_composite_type_field_aligned (struct type *t, char *name,
  3762.                                      struct type *field, int alignment)
  3763. {
  3764.   struct field *f = append_composite_type_field_raw (t, name, field);

  3765.   if (TYPE_CODE (t) == TYPE_CODE_UNION)
  3766.     {
  3767.       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
  3768.         TYPE_LENGTH (t) = TYPE_LENGTH (field);
  3769.     }
  3770.   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
  3771.     {
  3772.       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
  3773.       if (TYPE_NFIELDS (t) > 1)
  3774.         {
  3775.           SET_FIELD_BITPOS (f[0],
  3776.                             (FIELD_BITPOS (f[-1])
  3777.                              + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
  3778.                                 * TARGET_CHAR_BIT)));

  3779.           if (alignment)
  3780.             {
  3781.               int left;

  3782.               alignment *= TARGET_CHAR_BIT;
  3783.               left = FIELD_BITPOS (f[0]) % alignment;

  3784.               if (left)
  3785.                 {
  3786.                   SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
  3787.                   TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
  3788.                 }
  3789.             }
  3790.         }
  3791.     }
  3792. }

  3793. /* Add new field with name NAME and type FIELD to composite type T.  */

  3794. void
  3795. append_composite_type_field (struct type *t, char *name,
  3796.                              struct type *field)
  3797. {
  3798.   append_composite_type_field_aligned (t, name, field, 0);
  3799. }

  3800. static struct gdbarch_data *gdbtypes_data;

  3801. const struct builtin_type *
  3802. builtin_type (struct gdbarch *gdbarch)
  3803. {
  3804.   return gdbarch_data (gdbarch, gdbtypes_data);
  3805. }

  3806. static void *
  3807. gdbtypes_post_init (struct gdbarch *gdbarch)
  3808. {
  3809.   struct builtin_type *builtin_type
  3810.     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);

  3811.   /* Basic types.  */
  3812.   builtin_type->builtin_void
  3813.     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
  3814.   builtin_type->builtin_char
  3815.     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  3816.                          !gdbarch_char_signed (gdbarch), "char");
  3817.   builtin_type->builtin_signed_char
  3818.     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  3819.                          0, "signed char");
  3820.   builtin_type->builtin_unsigned_char
  3821.     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  3822.                          1, "unsigned char");
  3823.   builtin_type->builtin_short
  3824.     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
  3825.                          0, "short");
  3826.   builtin_type->builtin_unsigned_short
  3827.     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
  3828.                          1, "unsigned short");
  3829.   builtin_type->builtin_int
  3830.     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  3831.                          0, "int");
  3832.   builtin_type->builtin_unsigned_int
  3833.     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  3834.                          1, "unsigned int");
  3835.   builtin_type->builtin_long
  3836.     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  3837.                          0, "long");
  3838.   builtin_type->builtin_unsigned_long
  3839.     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  3840.                          1, "unsigned long");
  3841.   builtin_type->builtin_long_long
  3842.     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
  3843.                          0, "long long");
  3844.   builtin_type->builtin_unsigned_long_long
  3845.     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
  3846.                          1, "unsigned long long");
  3847.   builtin_type->builtin_float
  3848.     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  3849.                        "float", gdbarch_float_format (gdbarch));
  3850.   builtin_type->builtin_double
  3851.     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  3852.                        "double", gdbarch_double_format (gdbarch));
  3853.   builtin_type->builtin_long_double
  3854.     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  3855.                        "long double", gdbarch_long_double_format (gdbarch));
  3856.   builtin_type->builtin_complex
  3857.     = arch_complex_type (gdbarch, "complex",
  3858.                          builtin_type->builtin_float);
  3859.   builtin_type->builtin_double_complex
  3860.     = arch_complex_type (gdbarch, "double complex",
  3861.                          builtin_type->builtin_double);
  3862.   builtin_type->builtin_string
  3863.     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
  3864.   builtin_type->builtin_bool
  3865.     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");

  3866.   /* The following three are about decimal floating point types, which
  3867.      are 32-bits, 64-bits and 128-bits respectively.  */
  3868.   builtin_type->builtin_decfloat
  3869.     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
  3870.   builtin_type->builtin_decdouble
  3871.     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
  3872.   builtin_type->builtin_declong
  3873.     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");

  3874.   /* "True" character types.  */
  3875.   builtin_type->builtin_true_char
  3876.     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
  3877.   builtin_type->builtin_true_unsigned_char
  3878.     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");

  3879.   /* Fixed-size integer types.  */
  3880.   builtin_type->builtin_int0
  3881.     = arch_integer_type (gdbarch, 0, 0, "int0_t");
  3882.   builtin_type->builtin_int8
  3883.     = arch_integer_type (gdbarch, 8, 0, "int8_t");
  3884.   builtin_type->builtin_uint8
  3885.     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
  3886.   builtin_type->builtin_int16
  3887.     = arch_integer_type (gdbarch, 16, 0, "int16_t");
  3888.   builtin_type->builtin_uint16
  3889.     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
  3890.   builtin_type->builtin_int32
  3891.     = arch_integer_type (gdbarch, 32, 0, "int32_t");
  3892.   builtin_type->builtin_uint32
  3893.     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
  3894.   builtin_type->builtin_int64
  3895.     = arch_integer_type (gdbarch, 64, 0, "int64_t");
  3896.   builtin_type->builtin_uint64
  3897.     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
  3898.   builtin_type->builtin_int128
  3899.     = arch_integer_type (gdbarch, 128, 0, "int128_t");
  3900.   builtin_type->builtin_uint128
  3901.     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
  3902.   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
  3903.     TYPE_INSTANCE_FLAG_NOTTEXT;
  3904.   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
  3905.     TYPE_INSTANCE_FLAG_NOTTEXT;

  3906.   /* Wide character types.  */
  3907.   builtin_type->builtin_char16
  3908.     = arch_integer_type (gdbarch, 16, 0, "char16_t");
  3909.   builtin_type->builtin_char32
  3910.     = arch_integer_type (gdbarch, 32, 0, "char32_t");


  3911.   /* Default data/code pointer types.  */
  3912.   builtin_type->builtin_data_ptr
  3913.     = lookup_pointer_type (builtin_type->builtin_void);
  3914.   builtin_type->builtin_func_ptr
  3915.     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
  3916.   builtin_type->builtin_func_func
  3917.     = lookup_function_type (builtin_type->builtin_func_ptr);

  3918.   /* This type represents a GDB internal function.  */
  3919.   builtin_type->internal_fn
  3920.     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
  3921.                  "<internal function>");

  3922.   /* This type represents an xmethod.  */
  3923.   builtin_type->xmethod
  3924.     = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");

  3925.   return builtin_type;
  3926. }

  3927. /* This set of objfile-based types is intended to be used by symbol
  3928.    readers as basic types.  */

  3929. static const struct objfile_data *objfile_type_data;

  3930. const struct objfile_type *
  3931. objfile_type (struct objfile *objfile)
  3932. {
  3933.   struct gdbarch *gdbarch;
  3934.   struct objfile_type *objfile_type
  3935.     = objfile_data (objfile, objfile_type_data);

  3936.   if (objfile_type)
  3937.     return objfile_type;

  3938.   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
  3939.                                  1, struct objfile_type);

  3940.   /* Use the objfile architecture to determine basic type properties.  */
  3941.   gdbarch = get_objfile_arch (objfile);

  3942.   /* Basic types.  */
  3943.   objfile_type->builtin_void
  3944.     = init_type (TYPE_CODE_VOID, 1,
  3945.                  0,
  3946.                  "void", objfile);

  3947.   objfile_type->builtin_char
  3948.     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
  3949.                  (TYPE_FLAG_NOSIGN
  3950.                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
  3951.                  "char", objfile);
  3952.   objfile_type->builtin_signed_char
  3953.     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
  3954.                  0,
  3955.                  "signed char", objfile);
  3956.   objfile_type->builtin_unsigned_char
  3957.     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
  3958.                  TYPE_FLAG_UNSIGNED,
  3959.                  "unsigned char", objfile);
  3960.   objfile_type->builtin_short
  3961.     = init_type (TYPE_CODE_INT,
  3962.                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
  3963.                  0, "short", objfile);
  3964.   objfile_type->builtin_unsigned_short
  3965.     = init_type (TYPE_CODE_INT,
  3966.                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
  3967.                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
  3968.   objfile_type->builtin_int
  3969.     = init_type (TYPE_CODE_INT,
  3970.                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
  3971.                  0, "int", objfile);
  3972.   objfile_type->builtin_unsigned_int
  3973.     = init_type (TYPE_CODE_INT,
  3974.                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
  3975.                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
  3976.   objfile_type->builtin_long
  3977.     = init_type (TYPE_CODE_INT,
  3978.                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
  3979.                  0, "long", objfile);
  3980.   objfile_type->builtin_unsigned_long
  3981.     = init_type (TYPE_CODE_INT,
  3982.                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
  3983.                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
  3984.   objfile_type->builtin_long_long
  3985.     = init_type (TYPE_CODE_INT,
  3986.                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
  3987.                  0, "long long", objfile);
  3988.   objfile_type->builtin_unsigned_long_long
  3989.     = init_type (TYPE_CODE_INT,
  3990.                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
  3991.                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);

  3992.   objfile_type->builtin_float
  3993.     = init_type (TYPE_CODE_FLT,
  3994.                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
  3995.                  0, "float", objfile);
  3996.   TYPE_FLOATFORMAT (objfile_type->builtin_float)
  3997.     = gdbarch_float_format (gdbarch);
  3998.   objfile_type->builtin_double
  3999.     = init_type (TYPE_CODE_FLT,
  4000.                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
  4001.                  0, "double", objfile);
  4002.   TYPE_FLOATFORMAT (objfile_type->builtin_double)
  4003.     = gdbarch_double_format (gdbarch);
  4004.   objfile_type->builtin_long_double
  4005.     = init_type (TYPE_CODE_FLT,
  4006.                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
  4007.                  0, "long double", objfile);
  4008.   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
  4009.     = gdbarch_long_double_format (gdbarch);

  4010.   /* This type represents a type that was unrecognized in symbol read-in.  */
  4011.   objfile_type->builtin_error
  4012.     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);

  4013.   /* The following set of types is used for symbols with no
  4014.      debug information.  */
  4015.   objfile_type->nodebug_text_symbol
  4016.     = init_type (TYPE_CODE_FUNC, 1, 0,
  4017.                  "<text variable, no debug info>", objfile);
  4018.   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
  4019.     = objfile_type->builtin_int;
  4020.   objfile_type->nodebug_text_gnu_ifunc_symbol
  4021.     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
  4022.                  "<text gnu-indirect-function variable, no debug info>",
  4023.                  objfile);
  4024.   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
  4025.     = objfile_type->nodebug_text_symbol;
  4026.   objfile_type->nodebug_got_plt_symbol
  4027.     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
  4028.                  "<text from jump slot in .got.plt, no debug info>",
  4029.                  objfile);
  4030.   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
  4031.     = objfile_type->nodebug_text_symbol;
  4032.   objfile_type->nodebug_data_symbol
  4033.     = init_type (TYPE_CODE_INT,
  4034.                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
  4035.                  "<data variable, no debug info>", objfile);
  4036.   objfile_type->nodebug_unknown_symbol
  4037.     = init_type (TYPE_CODE_INT, 1, 0,
  4038.                  "<variable (not text or data), no debug info>", objfile);
  4039.   objfile_type->nodebug_tls_symbol
  4040.     = init_type (TYPE_CODE_INT,
  4041.                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
  4042.                  "<thread local variable, no debug info>", objfile);

  4043.   /* NOTE: on some targets, addresses and pointers are not necessarily
  4044.      the same.

  4045.      The upshot is:
  4046.      - gdb's `struct type' always describes the target's
  4047.        representation.
  4048.      - gdb's `struct value' objects should always hold values in
  4049.        target form.
  4050.      - gdb's CORE_ADDR values are addresses in the unified virtual
  4051.        address space that the assembler and linker work with.  Thus,
  4052.        since target_read_memory takes a CORE_ADDR as an argument, it
  4053.        can access any memory on the target, even if the processor has
  4054.        separate code and data address spaces.

  4055.      In this context, objfile_type->builtin_core_addr is a bit odd:
  4056.      it's a target type for a value the target will never see.  It's
  4057.      only used to hold the values of (typeless) linker symbols, which
  4058.      are indeed in the unified virtual address space.  */

  4059.   objfile_type->builtin_core_addr
  4060.     = init_type (TYPE_CODE_INT,
  4061.                  gdbarch_addr_bit (gdbarch) / 8,
  4062.                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);

  4063.   set_objfile_data (objfile, objfile_type_data, objfile_type);
  4064.   return objfile_type;
  4065. }

  4066. extern initialize_file_ftype _initialize_gdbtypes;

  4067. void
  4068. _initialize_gdbtypes (void)
  4069. {
  4070.   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
  4071.   objfile_type_data = register_objfile_data ();

  4072.   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
  4073.                              _("Set debugging of C++ overloading."),
  4074.                              _("Show debugging of C++ overloading."),
  4075.                              _("When enabled, ranking of the "
  4076.                                "functions is displayed."),
  4077.                              NULL,
  4078.                              show_overload_debug,
  4079.                              &setdebuglist, &showdebuglist);

  4080.   /* Add user knob for controlling resolution of opaque types.  */
  4081.   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
  4082.                            &opaque_type_resolution,
  4083.                            _("Set resolution of opaque struct/class/union"
  4084.                              " types (if set before loading symbols)."),
  4085.                            _("Show resolution of opaque struct/class/union"
  4086.                              " types (if set before loading symbols)."),
  4087.                            NULL, NULL,
  4088.                            show_opaque_type_resolution,
  4089.                            &setlist, &showlist);

  4090.   /* Add an option to permit non-strict type checking.  */
  4091.   add_setshow_boolean_cmd ("type", class_support,
  4092.                            &strict_type_checking,
  4093.                            _("Set strict type checking."),
  4094.                            _("Show strict type checking."),
  4095.                            NULL, NULL,
  4096.                            show_strict_type_checking,
  4097.                            &setchecklist, &showchecklist);
  4098. }