GDB (API)
/home/stan/gdb/src/gdb/gdbtypes.c
Go to the documentation of this file.
00001 /* Support routines for manipulating internal types for GDB.
00002 
00003    Copyright (C) 1992-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Cygnus Support, using pieces from other GDB modules.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "gdb_string.h"
00024 #include "bfd.h"
00025 #include "symtab.h"
00026 #include "symfile.h"
00027 #include "objfiles.h"
00028 #include "gdbtypes.h"
00029 #include "expression.h"
00030 #include "language.h"
00031 #include "target.h"
00032 #include "value.h"
00033 #include "demangle.h"
00034 #include "complaints.h"
00035 #include "gdbcmd.h"
00036 #include "cp-abi.h"
00037 #include "gdb_assert.h"
00038 #include "hashtab.h"
00039 #include "exceptions.h"
00040 #include "cp-support.h"
00041 
00042 /* Initialize BADNESS constants.  */
00043 
00044 const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
00045 
00046 const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
00047 const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
00048 
00049 const struct rank EXACT_MATCH_BADNESS = {0,0};
00050 
00051 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
00052 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
00053 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
00054 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
00055 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
00056 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
00057 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
00058 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
00059 const struct rank BASE_CONVERSION_BADNESS = {2,0};
00060 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
00061 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
00062 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
00063 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
00064 
00065 /* Floatformat pairs.  */
00066 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
00067   &floatformat_ieee_half_big,
00068   &floatformat_ieee_half_little
00069 };
00070 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
00071   &floatformat_ieee_single_big,
00072   &floatformat_ieee_single_little
00073 };
00074 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
00075   &floatformat_ieee_double_big,
00076   &floatformat_ieee_double_little
00077 };
00078 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
00079   &floatformat_ieee_double_big,
00080   &floatformat_ieee_double_littlebyte_bigword
00081 };
00082 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
00083   &floatformat_i387_ext,
00084   &floatformat_i387_ext
00085 };
00086 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
00087   &floatformat_m68881_ext,
00088   &floatformat_m68881_ext
00089 };
00090 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
00091   &floatformat_arm_ext_big,
00092   &floatformat_arm_ext_littlebyte_bigword
00093 };
00094 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
00095   &floatformat_ia64_spill_big,
00096   &floatformat_ia64_spill_little
00097 };
00098 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
00099   &floatformat_ia64_quad_big,
00100   &floatformat_ia64_quad_little
00101 };
00102 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
00103   &floatformat_vax_f,
00104   &floatformat_vax_f
00105 };
00106 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
00107   &floatformat_vax_d,
00108   &floatformat_vax_d
00109 };
00110 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
00111   &floatformat_ibm_long_double_big,
00112   &floatformat_ibm_long_double_little
00113 };
00114 
00115 /* Should opaque types be resolved?  */
00116 
00117 static int opaque_type_resolution = 1;
00118 
00119 /* A flag to enable printing of debugging information of C++
00120    overloading.  */
00121 
00122 unsigned int overload_debug = 0;
00123 
00124 /* A flag to enable strict type checking.  */
00125 
00126 static int strict_type_checking = 1;
00127 
00128 /* A function to show whether opaque types are resolved.  */
00129 
00130 static void
00131 show_opaque_type_resolution (struct ui_file *file, int from_tty,
00132                              struct cmd_list_element *c, 
00133                              const char *value)
00134 {
00135   fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
00136                             "(if set before loading symbols) is %s.\n"),
00137                     value);
00138 }
00139 
00140 /* A function to show whether C++ overload debugging is enabled.  */
00141 
00142 static void
00143 show_overload_debug (struct ui_file *file, int from_tty,
00144                      struct cmd_list_element *c, const char *value)
00145 {
00146   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), 
00147                     value);
00148 }
00149 
00150 /* A function to show the status of strict type checking.  */
00151 
00152 static void
00153 show_strict_type_checking (struct ui_file *file, int from_tty,
00154                            struct cmd_list_element *c, const char *value)
00155 {
00156   fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
00157 }
00158 
00159 
00160 /* Allocate a new OBJFILE-associated type structure and fill it
00161    with some defaults.  Space for the type structure is allocated
00162    on the objfile's objfile_obstack.  */
00163 
00164 struct type *
00165 alloc_type (struct objfile *objfile)
00166 {
00167   struct type *type;
00168 
00169   gdb_assert (objfile != NULL);
00170 
00171   /* Alloc the structure and start off with all fields zeroed.  */
00172   type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
00173   TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
00174                                           struct main_type);
00175   OBJSTAT (objfile, n_types++);
00176 
00177   TYPE_OBJFILE_OWNED (type) = 1;
00178   TYPE_OWNER (type).objfile = objfile;
00179 
00180   /* Initialize the fields that might not be zero.  */
00181 
00182   TYPE_CODE (type) = TYPE_CODE_UNDEF;
00183   TYPE_VPTR_FIELDNO (type) = -1;
00184   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
00185 
00186   return type;
00187 }
00188 
00189 /* Allocate a new GDBARCH-associated type structure and fill it
00190    with some defaults.  Space for the type structure is allocated
00191    on the heap.  */
00192 
00193 struct type *
00194 alloc_type_arch (struct gdbarch *gdbarch)
00195 {
00196   struct type *type;
00197 
00198   gdb_assert (gdbarch != NULL);
00199 
00200   /* Alloc the structure and start off with all fields zeroed.  */
00201 
00202   type = XZALLOC (struct type);
00203   TYPE_MAIN_TYPE (type) = XZALLOC (struct main_type);
00204 
00205   TYPE_OBJFILE_OWNED (type) = 0;
00206   TYPE_OWNER (type).gdbarch = gdbarch;
00207 
00208   /* Initialize the fields that might not be zero.  */
00209 
00210   TYPE_CODE (type) = TYPE_CODE_UNDEF;
00211   TYPE_VPTR_FIELDNO (type) = -1;
00212   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
00213 
00214   return type;
00215 }
00216 
00217 /* If TYPE is objfile-associated, allocate a new type structure
00218    associated with the same objfile.  If TYPE is gdbarch-associated,
00219    allocate a new type structure associated with the same gdbarch.  */
00220 
00221 struct type *
00222 alloc_type_copy (const struct type *type)
00223 {
00224   if (TYPE_OBJFILE_OWNED (type))
00225     return alloc_type (TYPE_OWNER (type).objfile);
00226   else
00227     return alloc_type_arch (TYPE_OWNER (type).gdbarch);
00228 }
00229 
00230 /* If TYPE is gdbarch-associated, return that architecture.
00231    If TYPE is objfile-associated, return that objfile's architecture.  */
00232 
00233 struct gdbarch *
00234 get_type_arch (const struct type *type)
00235 {
00236   if (TYPE_OBJFILE_OWNED (type))
00237     return get_objfile_arch (TYPE_OWNER (type).objfile);
00238   else
00239     return TYPE_OWNER (type).gdbarch;
00240 }
00241 
00242 /* Alloc a new type instance structure, fill it with some defaults,
00243    and point it at OLDTYPE.  Allocate the new type instance from the
00244    same place as OLDTYPE.  */
00245 
00246 static struct type *
00247 alloc_type_instance (struct type *oldtype)
00248 {
00249   struct type *type;
00250 
00251   /* Allocate the structure.  */
00252 
00253   if (! TYPE_OBJFILE_OWNED (oldtype))
00254     type = XZALLOC (struct type);
00255   else
00256     type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
00257                            struct type);
00258 
00259   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
00260 
00261   TYPE_CHAIN (type) = type;     /* Chain back to itself for now.  */
00262 
00263   return type;
00264 }
00265 
00266 /* Clear all remnants of the previous type at TYPE, in preparation for
00267    replacing it with something else.  Preserve owner information.  */
00268 
00269 static void
00270 smash_type (struct type *type)
00271 {
00272   int objfile_owned = TYPE_OBJFILE_OWNED (type);
00273   union type_owner owner = TYPE_OWNER (type);
00274 
00275   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
00276 
00277   /* Restore owner information.  */
00278   TYPE_OBJFILE_OWNED (type) = objfile_owned;
00279   TYPE_OWNER (type) = owner;
00280 
00281   /* For now, delete the rings.  */
00282   TYPE_CHAIN (type) = type;
00283 
00284   /* For now, leave the pointer/reference types alone.  */
00285 }
00286 
00287 /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
00288    to a pointer to memory where the pointer type should be stored.
00289    If *TYPEPTR is zero, update it to point to the pointer type we return.
00290    We allocate new memory if needed.  */
00291 
00292 struct type *
00293 make_pointer_type (struct type *type, struct type **typeptr)
00294 {
00295   struct type *ntype;   /* New type */
00296   struct type *chain;
00297 
00298   ntype = TYPE_POINTER_TYPE (type);
00299 
00300   if (ntype)
00301     {
00302       if (typeptr == 0)
00303         return ntype;           /* Don't care about alloc, 
00304                                    and have new type.  */
00305       else if (*typeptr == 0)
00306         {
00307           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
00308           return ntype;
00309         }
00310     }
00311 
00312   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
00313     {
00314       ntype = alloc_type_copy (type);
00315       if (typeptr)
00316         *typeptr = ntype;
00317     }
00318   else                  /* We have storage, but need to reset it.  */
00319     {
00320       ntype = *typeptr;
00321       chain = TYPE_CHAIN (ntype);
00322       smash_type (ntype);
00323       TYPE_CHAIN (ntype) = chain;
00324     }
00325 
00326   TYPE_TARGET_TYPE (ntype) = type;
00327   TYPE_POINTER_TYPE (type) = ntype;
00328 
00329   /* FIXME!  Assumes the machine has only one representation for pointers!  */
00330 
00331   TYPE_LENGTH (ntype)
00332     = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
00333   TYPE_CODE (ntype) = TYPE_CODE_PTR;
00334 
00335   /* Mark pointers as unsigned.  The target converts between pointers
00336      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
00337      gdbarch_address_to_pointer.  */
00338   TYPE_UNSIGNED (ntype) = 1;
00339 
00340   /* Update the length of all the other variants of this type.  */
00341   chain = TYPE_CHAIN (ntype);
00342   while (chain != ntype)
00343     {
00344       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
00345       chain = TYPE_CHAIN (chain);
00346     }
00347 
00348   return ntype;
00349 }
00350 
00351 /* Given a type TYPE, return a type of pointers to that type.
00352    May need to construct such a type if this is the first use.  */
00353 
00354 struct type *
00355 lookup_pointer_type (struct type *type)
00356 {
00357   return make_pointer_type (type, (struct type **) 0);
00358 }
00359 
00360 /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero,
00361    points to a pointer to memory where the reference type should be
00362    stored.  If *TYPEPTR is zero, update it to point to the reference
00363    type we return.  We allocate new memory if needed.  */
00364 
00365 struct type *
00366 make_reference_type (struct type *type, struct type **typeptr)
00367 {
00368   struct type *ntype;   /* New type */
00369   struct type *chain;
00370 
00371   ntype = TYPE_REFERENCE_TYPE (type);
00372 
00373   if (ntype)
00374     {
00375       if (typeptr == 0)
00376         return ntype;           /* Don't care about alloc, 
00377                                    and have new type.  */
00378       else if (*typeptr == 0)
00379         {
00380           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
00381           return ntype;
00382         }
00383     }
00384 
00385   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
00386     {
00387       ntype = alloc_type_copy (type);
00388       if (typeptr)
00389         *typeptr = ntype;
00390     }
00391   else                  /* We have storage, but need to reset it.  */
00392     {
00393       ntype = *typeptr;
00394       chain = TYPE_CHAIN (ntype);
00395       smash_type (ntype);
00396       TYPE_CHAIN (ntype) = chain;
00397     }
00398 
00399   TYPE_TARGET_TYPE (ntype) = type;
00400   TYPE_REFERENCE_TYPE (type) = ntype;
00401 
00402   /* FIXME!  Assume the machine has only one representation for
00403      references, and that it matches the (only) representation for
00404      pointers!  */
00405 
00406   TYPE_LENGTH (ntype) =
00407     gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
00408   TYPE_CODE (ntype) = TYPE_CODE_REF;
00409 
00410   if (!TYPE_REFERENCE_TYPE (type))      /* Remember it, if don't have one.  */
00411     TYPE_REFERENCE_TYPE (type) = ntype;
00412 
00413   /* Update the length of all the other variants of this type.  */
00414   chain = TYPE_CHAIN (ntype);
00415   while (chain != ntype)
00416     {
00417       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
00418       chain = TYPE_CHAIN (chain);
00419     }
00420 
00421   return ntype;
00422 }
00423 
00424 /* Same as above, but caller doesn't care about memory allocation
00425    details.  */
00426 
00427 struct type *
00428 lookup_reference_type (struct type *type)
00429 {
00430   return make_reference_type (type, (struct type **) 0);
00431 }
00432 
00433 /* Lookup a function type that returns type TYPE.  TYPEPTR, if
00434    nonzero, points to a pointer to memory where the function type
00435    should be stored.  If *TYPEPTR is zero, update it to point to the
00436    function type we return.  We allocate new memory if needed.  */
00437 
00438 struct type *
00439 make_function_type (struct type *type, struct type **typeptr)
00440 {
00441   struct type *ntype;   /* New type */
00442 
00443   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
00444     {
00445       ntype = alloc_type_copy (type);
00446       if (typeptr)
00447         *typeptr = ntype;
00448     }
00449   else                  /* We have storage, but need to reset it.  */
00450     {
00451       ntype = *typeptr;
00452       smash_type (ntype);
00453     }
00454 
00455   TYPE_TARGET_TYPE (ntype) = type;
00456 
00457   TYPE_LENGTH (ntype) = 1;
00458   TYPE_CODE (ntype) = TYPE_CODE_FUNC;
00459 
00460   INIT_FUNC_SPECIFIC (ntype);
00461 
00462   return ntype;
00463 }
00464 
00465 /* Given a type TYPE, return a type of functions that return that type.
00466    May need to construct such a type if this is the first use.  */
00467 
00468 struct type *
00469 lookup_function_type (struct type *type)
00470 {
00471   return make_function_type (type, (struct type **) 0);
00472 }
00473 
00474 /* Given a type TYPE and argument types, return the appropriate
00475    function type.  If the final type in PARAM_TYPES is NULL, make a
00476    varargs function.  */
00477 
00478 struct type *
00479 lookup_function_type_with_arguments (struct type *type,
00480                                      int nparams,
00481                                      struct type **param_types)
00482 {
00483   struct type *fn = make_function_type (type, (struct type **) 0);
00484   int i;
00485 
00486   if (nparams > 0)
00487     {
00488       if (param_types[nparams - 1] == NULL)
00489         {
00490           --nparams;
00491           TYPE_VARARGS (fn) = 1;
00492         }
00493       else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
00494                == TYPE_CODE_VOID)
00495         {
00496           --nparams;
00497           /* Caller should have ensured this.  */
00498           gdb_assert (nparams == 0);
00499           TYPE_PROTOTYPED (fn) = 1;
00500         }
00501     }
00502 
00503   TYPE_NFIELDS (fn) = nparams;
00504   TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
00505   for (i = 0; i < nparams; ++i)
00506     TYPE_FIELD_TYPE (fn, i) = param_types[i];
00507 
00508   return fn;
00509 }
00510 
00511 /* Identify address space identifier by name --
00512    return the integer flag defined in gdbtypes.h.  */
00513 
00514 int
00515 address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
00516 {
00517   int type_flags;
00518 
00519   /* Check for known address space delimiters.  */
00520   if (!strcmp (space_identifier, "code"))
00521     return TYPE_INSTANCE_FLAG_CODE_SPACE;
00522   else if (!strcmp (space_identifier, "data"))
00523     return TYPE_INSTANCE_FLAG_DATA_SPACE;
00524   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
00525            && gdbarch_address_class_name_to_type_flags (gdbarch,
00526                                                         space_identifier,
00527                                                         &type_flags))
00528     return type_flags;
00529   else
00530     error (_("Unknown address space specifier: \"%s\""), space_identifier);
00531 }
00532 
00533 /* Identify address space identifier by integer flag as defined in 
00534    gdbtypes.h -- return the string version of the adress space name.  */
00535 
00536 const char *
00537 address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
00538 {
00539   if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
00540     return "code";
00541   else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
00542     return "data";
00543   else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
00544            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
00545     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
00546   else
00547     return NULL;
00548 }
00549 
00550 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
00551 
00552    If STORAGE is non-NULL, create the new type instance there.
00553    STORAGE must be in the same obstack as TYPE.  */
00554 
00555 static struct type *
00556 make_qualified_type (struct type *type, int new_flags,
00557                      struct type *storage)
00558 {
00559   struct type *ntype;
00560 
00561   ntype = type;
00562   do
00563     {
00564       if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
00565         return ntype;
00566       ntype = TYPE_CHAIN (ntype);
00567     }
00568   while (ntype != type);
00569 
00570   /* Create a new type instance.  */
00571   if (storage == NULL)
00572     ntype = alloc_type_instance (type);
00573   else
00574     {
00575       /* If STORAGE was provided, it had better be in the same objfile
00576          as TYPE.  Otherwise, we can't link it into TYPE's cv chain:
00577          if one objfile is freed and the other kept, we'd have
00578          dangling pointers.  */
00579       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
00580 
00581       ntype = storage;
00582       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
00583       TYPE_CHAIN (ntype) = ntype;
00584     }
00585 
00586   /* Pointers or references to the original type are not relevant to
00587      the new type.  */
00588   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
00589   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
00590 
00591   /* Chain the new qualified type to the old type.  */
00592   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
00593   TYPE_CHAIN (type) = ntype;
00594 
00595   /* Now set the instance flags and return the new type.  */
00596   TYPE_INSTANCE_FLAGS (ntype) = new_flags;
00597 
00598   /* Set length of new type to that of the original type.  */
00599   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
00600 
00601   return ntype;
00602 }
00603 
00604 /* Make an address-space-delimited variant of a type -- a type that
00605    is identical to the one supplied except that it has an address
00606    space attribute attached to it (such as "code" or "data").
00607 
00608    The space attributes "code" and "data" are for Harvard
00609    architectures.  The address space attributes are for architectures
00610    which have alternately sized pointers or pointers with alternate
00611    representations.  */
00612 
00613 struct type *
00614 make_type_with_address_space (struct type *type, int space_flag)
00615 {
00616   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
00617                     & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
00618                         | TYPE_INSTANCE_FLAG_DATA_SPACE
00619                         | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
00620                    | space_flag);
00621 
00622   return make_qualified_type (type, new_flags, NULL);
00623 }
00624 
00625 /* Make a "c-v" variant of a type -- a type that is identical to the
00626    one supplied except that it may have const or volatile attributes
00627    CNST is a flag for setting the const attribute
00628    VOLTL is a flag for setting the volatile attribute
00629    TYPE is the base type whose variant we are creating.
00630 
00631    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
00632    storage to hold the new qualified type; *TYPEPTR and TYPE must be
00633    in the same objfile.  Otherwise, allocate fresh memory for the new
00634    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
00635    new type we construct.  */
00636 
00637 struct type *
00638 make_cv_type (int cnst, int voltl, 
00639               struct type *type, 
00640               struct type **typeptr)
00641 {
00642   struct type *ntype;   /* New type */
00643 
00644   int new_flags = (TYPE_INSTANCE_FLAGS (type)
00645                    & ~(TYPE_INSTANCE_FLAG_CONST 
00646                        | TYPE_INSTANCE_FLAG_VOLATILE));
00647 
00648   if (cnst)
00649     new_flags |= TYPE_INSTANCE_FLAG_CONST;
00650 
00651   if (voltl)
00652     new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
00653 
00654   if (typeptr && *typeptr != NULL)
00655     {
00656       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
00657          a C-V variant chain that threads across objfiles: if one
00658          objfile gets freed, then the other has a broken C-V chain.
00659 
00660          This code used to try to copy over the main type from TYPE to
00661          *TYPEPTR if they were in different objfiles, but that's
00662          wrong, too: TYPE may have a field list or member function
00663          lists, which refer to types of their own, etc. etc.  The
00664          whole shebang would need to be copied over recursively; you
00665          can't have inter-objfile pointers.  The only thing to do is
00666          to leave stub types as stub types, and look them up afresh by
00667          name each time you encounter them.  */
00668       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
00669     }
00670   
00671   ntype = make_qualified_type (type, new_flags, 
00672                                typeptr ? *typeptr : NULL);
00673 
00674   if (typeptr != NULL)
00675     *typeptr = ntype;
00676 
00677   return ntype;
00678 }
00679 
00680 /* Make a 'restrict'-qualified version of TYPE.  */
00681 
00682 struct type *
00683 make_restrict_type (struct type *type)
00684 {
00685   return make_qualified_type (type,
00686                               (TYPE_INSTANCE_FLAGS (type)
00687                                | TYPE_INSTANCE_FLAG_RESTRICT),
00688                               NULL);
00689 }
00690 
00691 /* Replace the contents of ntype with the type *type.  This changes the
00692    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
00693    the changes are propogated to all types in the TYPE_CHAIN.
00694 
00695    In order to build recursive types, it's inevitable that we'll need
00696    to update types in place --- but this sort of indiscriminate
00697    smashing is ugly, and needs to be replaced with something more
00698    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
00699    clear if more steps are needed.  */
00700 
00701 void
00702 replace_type (struct type *ntype, struct type *type)
00703 {
00704   struct type *chain;
00705 
00706   /* These two types had better be in the same objfile.  Otherwise,
00707      the assignment of one type's main type structure to the other
00708      will produce a type with references to objects (names; field
00709      lists; etc.) allocated on an objfile other than its own.  */
00710   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
00711 
00712   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
00713 
00714   /* The type length is not a part of the main type.  Update it for
00715      each type on the variant chain.  */
00716   chain = ntype;
00717   do
00718     {
00719       /* Assert that this element of the chain has no address-class bits
00720          set in its flags.  Such type variants might have type lengths
00721          which are supposed to be different from the non-address-class
00722          variants.  This assertion shouldn't ever be triggered because
00723          symbol readers which do construct address-class variants don't
00724          call replace_type().  */
00725       gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
00726 
00727       TYPE_LENGTH (chain) = TYPE_LENGTH (type);
00728       chain = TYPE_CHAIN (chain);
00729     }
00730   while (ntype != chain);
00731 
00732   /* Assert that the two types have equivalent instance qualifiers.
00733      This should be true for at least all of our debug readers.  */
00734   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
00735 }
00736 
00737 /* Implement direct support for MEMBER_TYPE in GNU C++.
00738    May need to construct such a type if this is the first use.
00739    The TYPE is the type of the member.  The DOMAIN is the type
00740    of the aggregate that the member belongs to.  */
00741 
00742 struct type *
00743 lookup_memberptr_type (struct type *type, struct type *domain)
00744 {
00745   struct type *mtype;
00746 
00747   mtype = alloc_type_copy (type);
00748   smash_to_memberptr_type (mtype, domain, type);
00749   return mtype;
00750 }
00751 
00752 /* Return a pointer-to-method type, for a method of type TO_TYPE.  */
00753 
00754 struct type *
00755 lookup_methodptr_type (struct type *to_type)
00756 {
00757   struct type *mtype;
00758 
00759   mtype = alloc_type_copy (to_type);
00760   smash_to_methodptr_type (mtype, to_type);
00761   return mtype;
00762 }
00763 
00764 /* Allocate a stub method whose return type is TYPE.  This apparently
00765    happens for speed of symbol reading, since parsing out the
00766    arguments to the method is cpu-intensive, the way we are doing it.
00767    So, we will fill in arguments later.  This always returns a fresh
00768    type.  */
00769 
00770 struct type *
00771 allocate_stub_method (struct type *type)
00772 {
00773   struct type *mtype;
00774 
00775   mtype = alloc_type_copy (type);
00776   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
00777   TYPE_LENGTH (mtype) = 1;
00778   TYPE_STUB (mtype) = 1;
00779   TYPE_TARGET_TYPE (mtype) = type;
00780   /*  _DOMAIN_TYPE (mtype) = unknown yet */
00781   return mtype;
00782 }
00783 
00784 /* Create a range type using either a blank type supplied in
00785    RESULT_TYPE, or creating a new type, inheriting the objfile from
00786    INDEX_TYPE.
00787 
00788    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
00789    to HIGH_BOUND, inclusive.
00790 
00791    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
00792    sure it is TYPE_CODE_UNDEF before we bash it into a range type?  */
00793 
00794 struct type *
00795 create_range_type (struct type *result_type, struct type *index_type,
00796                    LONGEST low_bound, LONGEST high_bound)
00797 {
00798   if (result_type == NULL)
00799     result_type = alloc_type_copy (index_type);
00800   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
00801   TYPE_TARGET_TYPE (result_type) = index_type;
00802   if (TYPE_STUB (index_type))
00803     TYPE_TARGET_STUB (result_type) = 1;
00804   else
00805     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
00806   TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
00807     TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
00808   TYPE_LOW_BOUND (result_type) = low_bound;
00809   TYPE_HIGH_BOUND (result_type) = high_bound;
00810 
00811   if (low_bound >= 0)
00812     TYPE_UNSIGNED (result_type) = 1;
00813 
00814   return result_type;
00815 }
00816 
00817 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
00818    TYPE.  Return 1 if type is a range type, 0 if it is discrete (and
00819    bounds will fit in LONGEST), or -1 otherwise.  */
00820 
00821 int
00822 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
00823 {
00824   CHECK_TYPEDEF (type);
00825   switch (TYPE_CODE (type))
00826     {
00827     case TYPE_CODE_RANGE:
00828       *lowp = TYPE_LOW_BOUND (type);
00829       *highp = TYPE_HIGH_BOUND (type);
00830       return 1;
00831     case TYPE_CODE_ENUM:
00832       if (TYPE_NFIELDS (type) > 0)
00833         {
00834           /* The enums may not be sorted by value, so search all
00835              entries.  */
00836           int i;
00837 
00838           *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
00839           for (i = 0; i < TYPE_NFIELDS (type); i++)
00840             {
00841               if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
00842                 *lowp = TYPE_FIELD_ENUMVAL (type, i);
00843               if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
00844                 *highp = TYPE_FIELD_ENUMVAL (type, i);
00845             }
00846 
00847           /* Set unsigned indicator if warranted.  */
00848           if (*lowp >= 0)
00849             {
00850               TYPE_UNSIGNED (type) = 1;
00851             }
00852         }
00853       else
00854         {
00855           *lowp = 0;
00856           *highp = -1;
00857         }
00858       return 0;
00859     case TYPE_CODE_BOOL:
00860       *lowp = 0;
00861       *highp = 1;
00862       return 0;
00863     case TYPE_CODE_INT:
00864       if (TYPE_LENGTH (type) > sizeof (LONGEST))        /* Too big */
00865         return -1;
00866       if (!TYPE_UNSIGNED (type))
00867         {
00868           *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
00869           *highp = -*lowp - 1;
00870           return 0;
00871         }
00872       /* ... fall through for unsigned ints ...  */
00873     case TYPE_CODE_CHAR:
00874       *lowp = 0;
00875       /* This round-about calculation is to avoid shifting by
00876          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
00877          if TYPE_LENGTH (type) == sizeof (LONGEST).  */
00878       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
00879       *highp = (*highp - 1) | *highp;
00880       return 0;
00881     default:
00882       return -1;
00883     }
00884 }
00885 
00886 /* Assuming TYPE is a simple, non-empty array type, compute its upper
00887    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
00888    Save the high bound into HIGH_BOUND if not NULL.
00889 
00890    Return 1 if the operation was successful.  Return zero otherwise,
00891    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
00892 
00893    We now simply use get_discrete_bounds call to get the values
00894    of the low and high bounds.
00895    get_discrete_bounds can return three values:
00896    1, meaning that index is a range,
00897    0, meaning that index is a discrete type,
00898    or -1 for failure.  */
00899 
00900 int
00901 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
00902 {
00903   struct type *index = TYPE_INDEX_TYPE (type);
00904   LONGEST low = 0;
00905   LONGEST high = 0;
00906   int res;
00907 
00908   if (index == NULL)
00909     return 0;
00910 
00911   res = get_discrete_bounds (index, &low, &high);
00912   if (res == -1)
00913     return 0;
00914 
00915   /* Check if the array bounds are undefined.  */
00916   if (res == 1
00917       && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
00918           || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
00919     return 0;
00920 
00921   if (low_bound)
00922     *low_bound = low;
00923 
00924   if (high_bound)
00925     *high_bound = high;
00926 
00927   return 1;
00928 }
00929 
00930 /* Create an array type using either a blank type supplied in
00931    RESULT_TYPE, or creating a new type, inheriting the objfile from
00932    RANGE_TYPE.
00933 
00934    Elements will be of type ELEMENT_TYPE, the indices will be of type
00935    RANGE_TYPE.
00936 
00937    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
00938    sure it is TYPE_CODE_UNDEF before we bash it into an array
00939    type?  */
00940 
00941 struct type *
00942 create_array_type (struct type *result_type, 
00943                    struct type *element_type,
00944                    struct type *range_type)
00945 {
00946   LONGEST low_bound, high_bound;
00947 
00948   if (result_type == NULL)
00949     result_type = alloc_type_copy (range_type);
00950 
00951   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
00952   TYPE_TARGET_TYPE (result_type) = element_type;
00953   if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
00954     low_bound = high_bound = 0;
00955   CHECK_TYPEDEF (element_type);
00956   /* Be careful when setting the array length.  Ada arrays can be
00957      empty arrays with the high_bound being smaller than the low_bound.
00958      In such cases, the array length should be zero.  */
00959   if (high_bound < low_bound)
00960     TYPE_LENGTH (result_type) = 0;
00961   else
00962     TYPE_LENGTH (result_type) =
00963       TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
00964   TYPE_NFIELDS (result_type) = 1;
00965   TYPE_FIELDS (result_type) =
00966     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
00967   TYPE_INDEX_TYPE (result_type) = range_type;
00968   TYPE_VPTR_FIELDNO (result_type) = -1;
00969 
00970   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
00971   if (TYPE_LENGTH (result_type) == 0)
00972     TYPE_TARGET_STUB (result_type) = 1;
00973 
00974   return result_type;
00975 }
00976 
00977 struct type *
00978 lookup_array_range_type (struct type *element_type,
00979                          LONGEST low_bound, LONGEST high_bound)
00980 {
00981   struct gdbarch *gdbarch = get_type_arch (element_type);
00982   struct type *index_type = builtin_type (gdbarch)->builtin_int;
00983   struct type *range_type
00984     = create_range_type (NULL, index_type, low_bound, high_bound);
00985 
00986   return create_array_type (NULL, element_type, range_type);
00987 }
00988 
00989 /* Create a string type using either a blank type supplied in
00990    RESULT_TYPE, or creating a new type.  String types are similar
00991    enough to array of char types that we can use create_array_type to
00992    build the basic type and then bash it into a string type.
00993 
00994    For fixed length strings, the range type contains 0 as the lower
00995    bound and the length of the string minus one as the upper bound.
00996 
00997    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
00998    sure it is TYPE_CODE_UNDEF before we bash it into a string
00999    type?  */
01000 
01001 struct type *
01002 create_string_type (struct type *result_type,
01003                     struct type *string_char_type,
01004                     struct type *range_type)
01005 {
01006   result_type = create_array_type (result_type,
01007                                    string_char_type,
01008                                    range_type);
01009   TYPE_CODE (result_type) = TYPE_CODE_STRING;
01010   return result_type;
01011 }
01012 
01013 struct type *
01014 lookup_string_range_type (struct type *string_char_type,
01015                           LONGEST low_bound, LONGEST high_bound)
01016 {
01017   struct type *result_type;
01018 
01019   result_type = lookup_array_range_type (string_char_type,
01020                                          low_bound, high_bound);
01021   TYPE_CODE (result_type) = TYPE_CODE_STRING;
01022   return result_type;
01023 }
01024 
01025 struct type *
01026 create_set_type (struct type *result_type, struct type *domain_type)
01027 {
01028   if (result_type == NULL)
01029     result_type = alloc_type_copy (domain_type);
01030 
01031   TYPE_CODE (result_type) = TYPE_CODE_SET;
01032   TYPE_NFIELDS (result_type) = 1;
01033   TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
01034 
01035   if (!TYPE_STUB (domain_type))
01036     {
01037       LONGEST low_bound, high_bound, bit_length;
01038 
01039       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
01040         low_bound = high_bound = 0;
01041       bit_length = high_bound - low_bound + 1;
01042       TYPE_LENGTH (result_type)
01043         = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
01044       if (low_bound >= 0)
01045         TYPE_UNSIGNED (result_type) = 1;
01046     }
01047   TYPE_FIELD_TYPE (result_type, 0) = domain_type;
01048 
01049   return result_type;
01050 }
01051 
01052 /* Convert ARRAY_TYPE to a vector type.  This may modify ARRAY_TYPE
01053    and any array types nested inside it.  */
01054 
01055 void
01056 make_vector_type (struct type *array_type)
01057 {
01058   struct type *inner_array, *elt_type;
01059   int flags;
01060 
01061   /* Find the innermost array type, in case the array is
01062      multi-dimensional.  */
01063   inner_array = array_type;
01064   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
01065     inner_array = TYPE_TARGET_TYPE (inner_array);
01066 
01067   elt_type = TYPE_TARGET_TYPE (inner_array);
01068   if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
01069     {
01070       flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
01071       elt_type = make_qualified_type (elt_type, flags, NULL);
01072       TYPE_TARGET_TYPE (inner_array) = elt_type;
01073     }
01074 
01075   TYPE_VECTOR (array_type) = 1;
01076 }
01077 
01078 struct type *
01079 init_vector_type (struct type *elt_type, int n)
01080 {
01081   struct type *array_type;
01082 
01083   array_type = lookup_array_range_type (elt_type, 0, n - 1);
01084   make_vector_type (array_type);
01085   return array_type;
01086 }
01087 
01088 /* Smash TYPE to be a type of pointers to members of DOMAIN with type
01089    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
01090    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
01091    TYPE doesn't include the offset (that's the value of the MEMBER
01092    itself), but does include the structure type into which it points
01093    (for some reason).
01094 
01095    When "smashing" the type, we preserve the objfile that the old type
01096    pointed to, since we aren't changing where the type is actually
01097    allocated.  */
01098 
01099 void
01100 smash_to_memberptr_type (struct type *type, struct type *domain,
01101                          struct type *to_type)
01102 {
01103   smash_type (type);
01104   TYPE_TARGET_TYPE (type) = to_type;
01105   TYPE_DOMAIN_TYPE (type) = domain;
01106   /* Assume that a data member pointer is the same size as a normal
01107      pointer.  */
01108   TYPE_LENGTH (type)
01109     = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
01110   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
01111 }
01112 
01113 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
01114 
01115    When "smashing" the type, we preserve the objfile that the old type
01116    pointed to, since we aren't changing where the type is actually
01117    allocated.  */
01118 
01119 void
01120 smash_to_methodptr_type (struct type *type, struct type *to_type)
01121 {
01122   smash_type (type);
01123   TYPE_TARGET_TYPE (type) = to_type;
01124   TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
01125   TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
01126   TYPE_CODE (type) = TYPE_CODE_METHODPTR;
01127 }
01128 
01129 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
01130    METHOD just means `function that gets an extra "this" argument'.
01131 
01132    When "smashing" the type, we preserve the objfile that the old type
01133    pointed to, since we aren't changing where the type is actually
01134    allocated.  */
01135 
01136 void
01137 smash_to_method_type (struct type *type, struct type *domain,
01138                       struct type *to_type, struct field *args,
01139                       int nargs, int varargs)
01140 {
01141   smash_type (type);
01142   TYPE_TARGET_TYPE (type) = to_type;
01143   TYPE_DOMAIN_TYPE (type) = domain;
01144   TYPE_FIELDS (type) = args;
01145   TYPE_NFIELDS (type) = nargs;
01146   if (varargs)
01147     TYPE_VARARGS (type) = 1;
01148   TYPE_LENGTH (type) = 1;       /* In practice, this is never needed.  */
01149   TYPE_CODE (type) = TYPE_CODE_METHOD;
01150 }
01151 
01152 /* Return a typename for a struct/union/enum type without "struct ",
01153    "union ", or "enum ".  If the type has a NULL name, return NULL.  */
01154 
01155 const char *
01156 type_name_no_tag (const struct type *type)
01157 {
01158   if (TYPE_TAG_NAME (type) != NULL)
01159     return TYPE_TAG_NAME (type);
01160 
01161   /* Is there code which expects this to return the name if there is
01162      no tag name?  My guess is that this is mainly used for C++ in
01163      cases where the two will always be the same.  */
01164   return TYPE_NAME (type);
01165 }
01166 
01167 /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
01168    Since GCC PR debug/47510 DWARF provides associated information to detect the
01169    anonymous class linkage name from its typedef.
01170 
01171    Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
01172    apply it itself.  */
01173 
01174 const char *
01175 type_name_no_tag_or_error (struct type *type)
01176 {
01177   struct type *saved_type = type;
01178   const char *name;
01179   struct objfile *objfile;
01180 
01181   CHECK_TYPEDEF (type);
01182 
01183   name = type_name_no_tag (type);
01184   if (name != NULL)
01185     return name;
01186 
01187   name = type_name_no_tag (saved_type);
01188   objfile = TYPE_OBJFILE (saved_type);
01189   error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
01190          name ? name : "<anonymous>",
01191          objfile ? objfile_name (objfile) : "<arch>");
01192 }
01193 
01194 /* Lookup a typedef or primitive type named NAME, visible in lexical
01195    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
01196    suitably defined.  */
01197 
01198 struct type *
01199 lookup_typename (const struct language_defn *language,
01200                  struct gdbarch *gdbarch, const char *name,
01201                  const struct block *block, int noerr)
01202 {
01203   struct symbol *sym;
01204   struct type *type;
01205 
01206   sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
01207   if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
01208     return SYMBOL_TYPE (sym);
01209 
01210   type = language_lookup_primitive_type_by_name (language, gdbarch, name);
01211   if (type)
01212     return type;
01213 
01214   if (noerr)
01215     return NULL;
01216   error (_("No type named %s."), name);
01217 }
01218 
01219 struct type *
01220 lookup_unsigned_typename (const struct language_defn *language,
01221                           struct gdbarch *gdbarch, const char *name)
01222 {
01223   char *uns = alloca (strlen (name) + 10);
01224 
01225   strcpy (uns, "unsigned ");
01226   strcpy (uns + 9, name);
01227   return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
01228 }
01229 
01230 struct type *
01231 lookup_signed_typename (const struct language_defn *language,
01232                         struct gdbarch *gdbarch, const char *name)
01233 {
01234   struct type *t;
01235   char *uns = alloca (strlen (name) + 8);
01236 
01237   strcpy (uns, "signed ");
01238   strcpy (uns + 7, name);
01239   t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
01240   /* If we don't find "signed FOO" just try again with plain "FOO".  */
01241   if (t != NULL)
01242     return t;
01243   return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
01244 }
01245 
01246 /* Lookup a structure type named "struct NAME",
01247    visible in lexical block BLOCK.  */
01248 
01249 struct type *
01250 lookup_struct (const char *name, const struct block *block)
01251 {
01252   struct symbol *sym;
01253 
01254   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
01255 
01256   if (sym == NULL)
01257     {
01258       error (_("No struct type named %s."), name);
01259     }
01260   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
01261     {
01262       error (_("This context has class, union or enum %s, not a struct."),
01263              name);
01264     }
01265   return (SYMBOL_TYPE (sym));
01266 }
01267 
01268 /* Lookup a union type named "union NAME",
01269    visible in lexical block BLOCK.  */
01270 
01271 struct type *
01272 lookup_union (const char *name, const struct block *block)
01273 {
01274   struct symbol *sym;
01275   struct type *t;
01276 
01277   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
01278 
01279   if (sym == NULL)
01280     error (_("No union type named %s."), name);
01281 
01282   t = SYMBOL_TYPE (sym);
01283 
01284   if (TYPE_CODE (t) == TYPE_CODE_UNION)
01285     return t;
01286 
01287   /* If we get here, it's not a union.  */
01288   error (_("This context has class, struct or enum %s, not a union."), 
01289          name);
01290 }
01291 
01292 /* Lookup an enum type named "enum NAME",
01293    visible in lexical block BLOCK.  */
01294 
01295 struct type *
01296 lookup_enum (const char *name, const struct block *block)
01297 {
01298   struct symbol *sym;
01299 
01300   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
01301   if (sym == NULL)
01302     {
01303       error (_("No enum type named %s."), name);
01304     }
01305   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
01306     {
01307       error (_("This context has class, struct or union %s, not an enum."), 
01308              name);
01309     }
01310   return (SYMBOL_TYPE (sym));
01311 }
01312 
01313 /* Lookup a template type named "template NAME<TYPE>",
01314    visible in lexical block BLOCK.  */
01315 
01316 struct type *
01317 lookup_template_type (char *name, struct type *type, 
01318                       const struct block *block)
01319 {
01320   struct symbol *sym;
01321   char *nam = (char *) 
01322     alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
01323 
01324   strcpy (nam, name);
01325   strcat (nam, "<");
01326   strcat (nam, TYPE_NAME (type));
01327   strcat (nam, " >");   /* FIXME, extra space still introduced in gcc?  */
01328 
01329   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
01330 
01331   if (sym == NULL)
01332     {
01333       error (_("No template type named %s."), name);
01334     }
01335   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
01336     {
01337       error (_("This context has class, union or enum %s, not a struct."),
01338              name);
01339     }
01340   return (SYMBOL_TYPE (sym));
01341 }
01342 
01343 /* Given a type TYPE, lookup the type of the component of type named
01344    NAME.
01345 
01346    TYPE can be either a struct or union, or a pointer or reference to
01347    a struct or union.  If it is a pointer or reference, its target
01348    type is automatically used.  Thus '.' and '->' are interchangable,
01349    as specified for the definitions of the expression element types
01350    STRUCTOP_STRUCT and STRUCTOP_PTR.
01351 
01352    If NOERR is nonzero, return zero if NAME is not suitably defined.
01353    If NAME is the name of a baseclass type, return that type.  */
01354 
01355 struct type *
01356 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
01357 {
01358   int i;
01359   char *typename;
01360 
01361   for (;;)
01362     {
01363       CHECK_TYPEDEF (type);
01364       if (TYPE_CODE (type) != TYPE_CODE_PTR
01365           && TYPE_CODE (type) != TYPE_CODE_REF)
01366         break;
01367       type = TYPE_TARGET_TYPE (type);
01368     }
01369 
01370   if (TYPE_CODE (type) != TYPE_CODE_STRUCT 
01371       && TYPE_CODE (type) != TYPE_CODE_UNION)
01372     {
01373       typename = type_to_string (type);
01374       make_cleanup (xfree, typename);
01375       error (_("Type %s is not a structure or union type."), typename);
01376     }
01377 
01378 #if 0
01379   /* FIXME: This change put in by Michael seems incorrect for the case
01380      where the structure tag name is the same as the member name.
01381      I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
01382      foo; } bell;" Disabled by fnf.  */
01383   {
01384     char *typename;
01385 
01386     typename = type_name_no_tag (type);
01387     if (typename != NULL && strcmp (typename, name) == 0)
01388       return type;
01389   }
01390 #endif
01391 
01392   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
01393     {
01394       const char *t_field_name = TYPE_FIELD_NAME (type, i);
01395 
01396       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
01397         {
01398           return TYPE_FIELD_TYPE (type, i);
01399         }
01400      else if (!t_field_name || *t_field_name == '\0')
01401         {
01402           struct type *subtype 
01403             = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
01404 
01405           if (subtype != NULL)
01406             return subtype;
01407         }
01408     }
01409 
01410   /* OK, it's not in this class.  Recursively check the baseclasses.  */
01411   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
01412     {
01413       struct type *t;
01414 
01415       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
01416       if (t != NULL)
01417         {
01418           return t;
01419         }
01420     }
01421 
01422   if (noerr)
01423     {
01424       return NULL;
01425     }
01426 
01427   typename = type_to_string (type);
01428   make_cleanup (xfree, typename);
01429   error (_("Type %s has no component named %s."), typename, name);
01430 }
01431 
01432 /* Lookup the vptr basetype/fieldno values for TYPE.
01433    If found store vptr_basetype in *BASETYPEP if non-NULL, and return
01434    vptr_fieldno.  Also, if found and basetype is from the same objfile,
01435    cache the results.
01436    If not found, return -1 and ignore BASETYPEP.
01437    Callers should be aware that in some cases (for example,
01438    the type or one of its baseclasses is a stub type and we are
01439    debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
01440    this function will not be able to find the
01441    virtual function table pointer, and vptr_fieldno will remain -1 and
01442    vptr_basetype will remain NULL or incomplete.  */
01443 
01444 int
01445 get_vptr_fieldno (struct type *type, struct type **basetypep)
01446 {
01447   CHECK_TYPEDEF (type);
01448 
01449   if (TYPE_VPTR_FIELDNO (type) < 0)
01450     {
01451       int i;
01452 
01453       /* We must start at zero in case the first (and only) baseclass
01454          is virtual (and hence we cannot share the table pointer).  */
01455       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
01456         {
01457           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
01458           int fieldno;
01459           struct type *basetype;
01460 
01461           fieldno = get_vptr_fieldno (baseclass, &basetype);
01462           if (fieldno >= 0)
01463             {
01464               /* If the type comes from a different objfile we can't cache
01465                  it, it may have a different lifetime.  PR 2384 */
01466               if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
01467                 {
01468                   TYPE_VPTR_FIELDNO (type) = fieldno;
01469                   TYPE_VPTR_BASETYPE (type) = basetype;
01470                 }
01471               if (basetypep)
01472                 *basetypep = basetype;
01473               return fieldno;
01474             }
01475         }
01476 
01477       /* Not found.  */
01478       return -1;
01479     }
01480   else
01481     {
01482       if (basetypep)
01483         *basetypep = TYPE_VPTR_BASETYPE (type);
01484       return TYPE_VPTR_FIELDNO (type);
01485     }
01486 }
01487 
01488 static void
01489 stub_noname_complaint (void)
01490 {
01491   complaint (&symfile_complaints, _("stub type has NULL name"));
01492 }
01493 
01494 /* Find the real type of TYPE.  This function returns the real type,
01495    after removing all layers of typedefs, and completing opaque or stub
01496    types.  Completion changes the TYPE argument, but stripping of
01497    typedefs does not.
01498 
01499    Instance flags (e.g. const/volatile) are preserved as typedefs are
01500    stripped.  If necessary a new qualified form of the underlying type
01501    is created.
01502 
01503    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
01504    not been computed and we're either in the middle of reading symbols, or
01505    there was no name for the typedef in the debug info.
01506 
01507    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
01508    QUITs in the symbol reading code can also throw.
01509    Thus this function can throw an exception.
01510 
01511    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
01512    the target type.
01513 
01514    If this is a stubbed struct (i.e. declared as struct foo *), see if
01515    we can find a full definition in some other file.  If so, copy this
01516    definition, so we can use it in future.  There used to be a comment
01517    (but not any code) that if we don't find a full definition, we'd
01518    set a flag so we don't spend time in the future checking the same
01519    type.  That would be a mistake, though--we might load in more
01520    symbols which contain a full definition for the type.  */
01521 
01522 struct type *
01523 check_typedef (struct type *type)
01524 {
01525   struct type *orig_type = type;
01526   /* While we're removing typedefs, we don't want to lose qualifiers.
01527      E.g., const/volatile.  */
01528   int instance_flags = TYPE_INSTANCE_FLAGS (type);
01529 
01530   gdb_assert (type);
01531 
01532   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
01533     {
01534       if (!TYPE_TARGET_TYPE (type))
01535         {
01536           const char *name;
01537           struct symbol *sym;
01538 
01539           /* It is dangerous to call lookup_symbol if we are currently
01540              reading a symtab.  Infinite recursion is one danger.  */
01541           if (currently_reading_symtab)
01542             return make_qualified_type (type, instance_flags, NULL);
01543 
01544           name = type_name_no_tag (type);
01545           /* FIXME: shouldn't we separately check the TYPE_NAME and
01546              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
01547              VAR_DOMAIN as appropriate?  (this code was written before
01548              TYPE_NAME and TYPE_TAG_NAME were separate).  */
01549           if (name == NULL)
01550             {
01551               stub_noname_complaint ();
01552               return make_qualified_type (type, instance_flags, NULL);
01553             }
01554           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
01555           if (sym)
01556             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
01557           else                                  /* TYPE_CODE_UNDEF */
01558             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
01559         }
01560       type = TYPE_TARGET_TYPE (type);
01561 
01562       /* Preserve the instance flags as we traverse down the typedef chain.
01563 
01564          Handling address spaces/classes is nasty, what do we do if there's a
01565          conflict?
01566          E.g., what if an outer typedef marks the type as class_1 and an inner
01567          typedef marks the type as class_2?
01568          This is the wrong place to do such error checking.  We leave it to
01569          the code that created the typedef in the first place to flag the
01570          error.  We just pick the outer address space (akin to letting the
01571          outer cast in a chain of casting win), instead of assuming
01572          "it can't happen".  */
01573       {
01574         const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
01575                                 | TYPE_INSTANCE_FLAG_DATA_SPACE);
01576         const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
01577         int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
01578 
01579         /* Treat code vs data spaces and address classes separately.  */
01580         if ((instance_flags & ALL_SPACES) != 0)
01581           new_instance_flags &= ~ALL_SPACES;
01582         if ((instance_flags & ALL_CLASSES) != 0)
01583           new_instance_flags &= ~ALL_CLASSES;
01584 
01585         instance_flags |= new_instance_flags;
01586       }
01587     }
01588 
01589   /* If this is a struct/class/union with no fields, then check
01590      whether a full definition exists somewhere else.  This is for
01591      systems where a type definition with no fields is issued for such
01592      types, instead of identifying them as stub types in the first
01593      place.  */
01594 
01595   if (TYPE_IS_OPAQUE (type) 
01596       && opaque_type_resolution 
01597       && !currently_reading_symtab)
01598     {
01599       const char *name = type_name_no_tag (type);
01600       struct type *newtype;
01601 
01602       if (name == NULL)
01603         {
01604           stub_noname_complaint ();
01605           return make_qualified_type (type, instance_flags, NULL);
01606         }
01607       newtype = lookup_transparent_type (name);
01608 
01609       if (newtype)
01610         {
01611           /* If the resolved type and the stub are in the same
01612              objfile, then replace the stub type with the real deal.
01613              But if they're in separate objfiles, leave the stub
01614              alone; we'll just look up the transparent type every time
01615              we call check_typedef.  We can't create pointers between
01616              types allocated to different objfiles, since they may
01617              have different lifetimes.  Trying to copy NEWTYPE over to
01618              TYPE's objfile is pointless, too, since you'll have to
01619              move over any other types NEWTYPE refers to, which could
01620              be an unbounded amount of stuff.  */
01621           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
01622             type = make_qualified_type (newtype,
01623                                         TYPE_INSTANCE_FLAGS (type),
01624                                         type);
01625           else
01626             type = newtype;
01627         }
01628     }
01629   /* Otherwise, rely on the stub flag being set for opaque/stubbed
01630      types.  */
01631   else if (TYPE_STUB (type) && !currently_reading_symtab)
01632     {
01633       const char *name = type_name_no_tag (type);
01634       /* FIXME: shouldn't we separately check the TYPE_NAME and the
01635          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
01636          as appropriate?  (this code was written before TYPE_NAME and
01637          TYPE_TAG_NAME were separate).  */
01638       struct symbol *sym;
01639 
01640       if (name == NULL)
01641         {
01642           stub_noname_complaint ();
01643           return make_qualified_type (type, instance_flags, NULL);
01644         }
01645       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
01646       if (sym)
01647         {
01648           /* Same as above for opaque types, we can replace the stub
01649              with the complete type only if they are in the same
01650              objfile.  */
01651           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
01652             type = make_qualified_type (SYMBOL_TYPE (sym),
01653                                         TYPE_INSTANCE_FLAGS (type),
01654                                         type);
01655           else
01656             type = SYMBOL_TYPE (sym);
01657         }
01658     }
01659 
01660   if (TYPE_TARGET_STUB (type))
01661     {
01662       struct type *range_type;
01663       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
01664 
01665       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
01666         {
01667           /* Nothing we can do.  */
01668         }
01669       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
01670                && TYPE_NFIELDS (type) == 1
01671                && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
01672                    == TYPE_CODE_RANGE))
01673         {
01674           /* Now recompute the length of the array type, based on its
01675              number of elements and the target type's length.
01676              Watch out for Ada null Ada arrays where the high bound
01677              is smaller than the low bound.  */
01678           const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
01679           const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
01680           ULONGEST len;
01681 
01682           if (high_bound < low_bound)
01683             len = 0;
01684           else
01685             {
01686               /* For now, we conservatively take the array length to be 0
01687                  if its length exceeds UINT_MAX.  The code below assumes
01688                  that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
01689                  which is technically not guaranteed by C, but is usually true
01690                  (because it would be true if x were unsigned with its
01691                  high-order bit on).  It uses the fact that
01692                  high_bound-low_bound is always representable in
01693                  ULONGEST and that if high_bound-low_bound+1 overflows,
01694                  it overflows to 0.  We must change these tests if we 
01695                  decide to increase the representation of TYPE_LENGTH
01696                  from unsigned int to ULONGEST.  */
01697               ULONGEST ulow = low_bound, uhigh = high_bound;
01698               ULONGEST tlen = TYPE_LENGTH (target_type);
01699 
01700               len = tlen * (uhigh - ulow + 1);
01701               if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh 
01702                   || len > UINT_MAX)
01703                 len = 0;
01704             }
01705           TYPE_LENGTH (type) = len;
01706           TYPE_TARGET_STUB (type) = 0;
01707         }
01708       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
01709         {
01710           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
01711           TYPE_TARGET_STUB (type) = 0;
01712         }
01713     }
01714 
01715   type = make_qualified_type (type, instance_flags, NULL);
01716 
01717   /* Cache TYPE_LENGTH for future use.  */
01718   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
01719 
01720   return type;
01721 }
01722 
01723 /* Parse a type expression in the string [P..P+LENGTH).  If an error
01724    occurs, silently return a void type.  */
01725 
01726 static struct type *
01727 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
01728 {
01729   struct ui_file *saved_gdb_stderr;
01730   struct type *type = NULL; /* Initialize to keep gcc happy.  */
01731   volatile struct gdb_exception except;
01732 
01733   /* Suppress error messages.  */
01734   saved_gdb_stderr = gdb_stderr;
01735   gdb_stderr = ui_file_new ();
01736 
01737   /* Call parse_and_eval_type() without fear of longjmp()s.  */
01738   TRY_CATCH (except, RETURN_MASK_ERROR)
01739     {
01740       type = parse_and_eval_type (p, length);
01741     }
01742 
01743   if (except.reason < 0)
01744     type = builtin_type (gdbarch)->builtin_void;
01745 
01746   /* Stop suppressing error messages.  */
01747   ui_file_delete (gdb_stderr);
01748   gdb_stderr = saved_gdb_stderr;
01749 
01750   return type;
01751 }
01752 
01753 /* Ugly hack to convert method stubs into method types.
01754 
01755    He ain't kiddin'.  This demangles the name of the method into a
01756    string including argument types, parses out each argument type,
01757    generates a string casting a zero to that type, evaluates the
01758    string, and stuffs the resulting type into an argtype vector!!!
01759    Then it knows the type of the whole function (including argument
01760    types for overloading), which info used to be in the stab's but was
01761    removed to hack back the space required for them.  */
01762 
01763 static void
01764 check_stub_method (struct type *type, int method_id, int signature_id)
01765 {
01766   struct gdbarch *gdbarch = get_type_arch (type);
01767   struct fn_field *f;
01768   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
01769   char *demangled_name = gdb_demangle (mangled_name,
01770                                        DMGL_PARAMS | DMGL_ANSI);
01771   char *argtypetext, *p;
01772   int depth = 0, argcount = 1;
01773   struct field *argtypes;
01774   struct type *mtype;
01775 
01776   /* Make sure we got back a function string that we can use.  */
01777   if (demangled_name)
01778     p = strchr (demangled_name, '(');
01779   else
01780     p = NULL;
01781 
01782   if (demangled_name == NULL || p == NULL)
01783     error (_("Internal: Cannot demangle mangled name `%s'."), 
01784            mangled_name);
01785 
01786   /* Now, read in the parameters that define this type.  */
01787   p += 1;
01788   argtypetext = p;
01789   while (*p)
01790     {
01791       if (*p == '(' || *p == '<')
01792         {
01793           depth += 1;
01794         }
01795       else if (*p == ')' || *p == '>')
01796         {
01797           depth -= 1;
01798         }
01799       else if (*p == ',' && depth == 0)
01800         {
01801           argcount += 1;
01802         }
01803 
01804       p += 1;
01805     }
01806 
01807   /* If we read one argument and it was ``void'', don't count it.  */
01808   if (strncmp (argtypetext, "(void)", 6) == 0)
01809     argcount -= 1;
01810 
01811   /* We need one extra slot, for the THIS pointer.  */
01812 
01813   argtypes = (struct field *)
01814     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
01815   p = argtypetext;
01816 
01817   /* Add THIS pointer for non-static methods.  */
01818   f = TYPE_FN_FIELDLIST1 (type, method_id);
01819   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
01820     argcount = 0;
01821   else
01822     {
01823       argtypes[0].type = lookup_pointer_type (type);
01824       argcount = 1;
01825     }
01826 
01827   if (*p != ')')                /* () means no args, skip while.  */
01828     {
01829       depth = 0;
01830       while (*p)
01831         {
01832           if (depth <= 0 && (*p == ',' || *p == ')'))
01833             {
01834               /* Avoid parsing of ellipsis, they will be handled below.
01835                  Also avoid ``void'' as above.  */
01836               if (strncmp (argtypetext, "...", p - argtypetext) != 0
01837                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
01838                 {
01839                   argtypes[argcount].type =
01840                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
01841                   argcount += 1;
01842                 }
01843               argtypetext = p + 1;
01844             }
01845 
01846           if (*p == '(' || *p == '<')
01847             {
01848               depth += 1;
01849             }
01850           else if (*p == ')' || *p == '>')
01851             {
01852               depth -= 1;
01853             }
01854 
01855           p += 1;
01856         }
01857     }
01858 
01859   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
01860 
01861   /* Now update the old "stub" type into a real type.  */
01862   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
01863   TYPE_DOMAIN_TYPE (mtype) = type;
01864   TYPE_FIELDS (mtype) = argtypes;
01865   TYPE_NFIELDS (mtype) = argcount;
01866   TYPE_STUB (mtype) = 0;
01867   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
01868   if (p[-2] == '.')
01869     TYPE_VARARGS (mtype) = 1;
01870 
01871   xfree (demangled_name);
01872 }
01873 
01874 /* This is the external interface to check_stub_method, above.  This
01875    function unstubs all of the signatures for TYPE's METHOD_ID method
01876    name.  After calling this function TYPE_FN_FIELD_STUB will be
01877    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
01878    correct.
01879 
01880    This function unfortunately can not die until stabs do.  */
01881 
01882 void
01883 check_stub_method_group (struct type *type, int method_id)
01884 {
01885   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
01886   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
01887   int j, found_stub = 0;
01888 
01889   for (j = 0; j < len; j++)
01890     if (TYPE_FN_FIELD_STUB (f, j))
01891       {
01892         found_stub = 1;
01893         check_stub_method (type, method_id, j);
01894       }
01895 
01896   /* GNU v3 methods with incorrect names were corrected when we read
01897      in type information, because it was cheaper to do it then.  The
01898      only GNU v2 methods with incorrect method names are operators and
01899      destructors; destructors were also corrected when we read in type
01900      information.
01901 
01902      Therefore the only thing we need to handle here are v2 operator
01903      names.  */
01904   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
01905     {
01906       int ret;
01907       char dem_opname[256];
01908 
01909       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
01910                                                            method_id),
01911                                    dem_opname, DMGL_ANSI);
01912       if (!ret)
01913         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
01914                                                              method_id),
01915                                      dem_opname, 0);
01916       if (ret)
01917         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
01918     }
01919 }
01920 
01921 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
01922 const struct cplus_struct_type cplus_struct_default = { };
01923 
01924 void
01925 allocate_cplus_struct_type (struct type *type)
01926 {
01927   if (HAVE_CPLUS_STRUCT (type))
01928     /* Structure was already allocated.  Nothing more to do.  */
01929     return;
01930 
01931   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
01932   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
01933     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
01934   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
01935 }
01936 
01937 const struct gnat_aux_type gnat_aux_default =
01938   { NULL };
01939 
01940 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
01941    and allocate the associated gnat-specific data.  The gnat-specific
01942    data is also initialized to gnat_aux_default.  */
01943 
01944 void
01945 allocate_gnat_aux_type (struct type *type)
01946 {
01947   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
01948   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
01949     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
01950   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
01951 }
01952 
01953 /* Helper function to initialize the standard scalar types.
01954 
01955    If NAME is non-NULL, then it is used to initialize the type name.
01956    Note that NAME is not copied; it is required to have a lifetime at
01957    least as long as OBJFILE.  */
01958 
01959 struct type *
01960 init_type (enum type_code code, int length, int flags,
01961            const char *name, struct objfile *objfile)
01962 {
01963   struct type *type;
01964 
01965   type = alloc_type (objfile);
01966   TYPE_CODE (type) = code;
01967   TYPE_LENGTH (type) = length;
01968 
01969   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
01970   if (flags & TYPE_FLAG_UNSIGNED)
01971     TYPE_UNSIGNED (type) = 1;
01972   if (flags & TYPE_FLAG_NOSIGN)
01973     TYPE_NOSIGN (type) = 1;
01974   if (flags & TYPE_FLAG_STUB)
01975     TYPE_STUB (type) = 1;
01976   if (flags & TYPE_FLAG_TARGET_STUB)
01977     TYPE_TARGET_STUB (type) = 1;
01978   if (flags & TYPE_FLAG_STATIC)
01979     TYPE_STATIC (type) = 1;
01980   if (flags & TYPE_FLAG_PROTOTYPED)
01981     TYPE_PROTOTYPED (type) = 1;
01982   if (flags & TYPE_FLAG_INCOMPLETE)
01983     TYPE_INCOMPLETE (type) = 1;
01984   if (flags & TYPE_FLAG_VARARGS)
01985     TYPE_VARARGS (type) = 1;
01986   if (flags & TYPE_FLAG_VECTOR)
01987     TYPE_VECTOR (type) = 1;
01988   if (flags & TYPE_FLAG_STUB_SUPPORTED)
01989     TYPE_STUB_SUPPORTED (type) = 1;
01990   if (flags & TYPE_FLAG_FIXED_INSTANCE)
01991     TYPE_FIXED_INSTANCE (type) = 1;
01992   if (flags & TYPE_FLAG_GNU_IFUNC)
01993     TYPE_GNU_IFUNC (type) = 1;
01994 
01995   TYPE_NAME (type) = name;
01996 
01997   /* C++ fancies.  */
01998 
01999   if (name && strcmp (name, "char") == 0)
02000     TYPE_NOSIGN (type) = 1;
02001 
02002   switch (code)
02003     {
02004       case TYPE_CODE_STRUCT:
02005       case TYPE_CODE_UNION:
02006       case TYPE_CODE_NAMESPACE:
02007         INIT_CPLUS_SPECIFIC (type);
02008         break;
02009       case TYPE_CODE_FLT:
02010         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
02011         break;
02012       case TYPE_CODE_FUNC:
02013         INIT_FUNC_SPECIFIC (type);
02014         break;
02015     }
02016   return type;
02017 }
02018 
02019 /* Queries on types.  */
02020 
02021 int
02022 can_dereference (struct type *t)
02023 {
02024   /* FIXME: Should we return true for references as well as
02025      pointers?  */
02026   CHECK_TYPEDEF (t);
02027   return
02028     (t != NULL
02029      && TYPE_CODE (t) == TYPE_CODE_PTR
02030      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
02031 }
02032 
02033 int
02034 is_integral_type (struct type *t)
02035 {
02036   CHECK_TYPEDEF (t);
02037   return
02038     ((t != NULL)
02039      && ((TYPE_CODE (t) == TYPE_CODE_INT)
02040          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
02041          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
02042          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
02043          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
02044          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
02045 }
02046 
02047 /* Return true if TYPE is scalar.  */
02048 
02049 static int
02050 is_scalar_type (struct type *type)
02051 {
02052   CHECK_TYPEDEF (type);
02053 
02054   switch (TYPE_CODE (type))
02055     {
02056     case TYPE_CODE_ARRAY:
02057     case TYPE_CODE_STRUCT:
02058     case TYPE_CODE_UNION:
02059     case TYPE_CODE_SET:
02060     case TYPE_CODE_STRING:
02061       return 0;
02062     default:
02063       return 1;
02064     }
02065 }
02066 
02067 /* Return true if T is scalar, or a composite type which in practice has
02068    the memory layout of a scalar type.  E.g., an array or struct with only
02069    one scalar element inside it, or a union with only scalar elements.  */
02070 
02071 int
02072 is_scalar_type_recursive (struct type *t)
02073 {
02074   CHECK_TYPEDEF (t);
02075 
02076   if (is_scalar_type (t))
02077     return 1;
02078   /* Are we dealing with an array or string of known dimensions?  */
02079   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
02080             || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
02081            && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
02082     {
02083       LONGEST low_bound, high_bound;
02084       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
02085 
02086       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
02087 
02088       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
02089     }
02090   /* Are we dealing with a struct with one element?  */
02091   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
02092     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
02093   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
02094     {
02095       int i, n = TYPE_NFIELDS (t);
02096 
02097       /* If all elements of the union are scalar, then the union is scalar.  */
02098       for (i = 0; i < n; i++)
02099         if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
02100           return 0;
02101 
02102       return 1;
02103     }
02104 
02105   return 0;
02106 }
02107 
02108 /* A helper function which returns true if types A and B represent the
02109    "same" class type.  This is true if the types have the same main
02110    type, or the same name.  */
02111 
02112 int
02113 class_types_same_p (const struct type *a, const struct type *b)
02114 {
02115   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
02116           || (TYPE_NAME (a) && TYPE_NAME (b)
02117               && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
02118 }
02119 
02120 /* If BASE is an ancestor of DCLASS return the distance between them.
02121    otherwise return -1;
02122    eg:
02123 
02124    class A {};
02125    class B: public A {};
02126    class C: public B {};
02127    class D: C {};
02128 
02129    distance_to_ancestor (A, A, 0) = 0
02130    distance_to_ancestor (A, B, 0) = 1
02131    distance_to_ancestor (A, C, 0) = 2
02132    distance_to_ancestor (A, D, 0) = 3
02133 
02134    If PUBLIC is 1 then only public ancestors are considered,
02135    and the function returns the distance only if BASE is a public ancestor
02136    of DCLASS.
02137    Eg:
02138 
02139    distance_to_ancestor (A, D, 1) = -1.  */
02140 
02141 static int
02142 distance_to_ancestor (struct type *base, struct type *dclass, int public)
02143 {
02144   int i;
02145   int d;
02146 
02147   CHECK_TYPEDEF (base);
02148   CHECK_TYPEDEF (dclass);
02149 
02150   if (class_types_same_p (base, dclass))
02151     return 0;
02152 
02153   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
02154     {
02155       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
02156         continue;
02157 
02158       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
02159       if (d >= 0)
02160         return 1 + d;
02161     }
02162 
02163   return -1;
02164 }
02165 
02166 /* Check whether BASE is an ancestor or base class or DCLASS
02167    Return 1 if so, and 0 if not.
02168    Note: If BASE and DCLASS are of the same type, this function
02169    will return 1. So for some class A, is_ancestor (A, A) will
02170    return 1.  */
02171 
02172 int
02173 is_ancestor (struct type *base, struct type *dclass)
02174 {
02175   return distance_to_ancestor (base, dclass, 0) >= 0;
02176 }
02177 
02178 /* Like is_ancestor, but only returns true when BASE is a public
02179    ancestor of DCLASS.  */
02180 
02181 int
02182 is_public_ancestor (struct type *base, struct type *dclass)
02183 {
02184   return distance_to_ancestor (base, dclass, 1) >= 0;
02185 }
02186 
02187 /* A helper function for is_unique_ancestor.  */
02188 
02189 static int
02190 is_unique_ancestor_worker (struct type *base, struct type *dclass,
02191                            int *offset,
02192                            const gdb_byte *valaddr, int embedded_offset,
02193                            CORE_ADDR address, struct value *val)
02194 {
02195   int i, count = 0;
02196 
02197   CHECK_TYPEDEF (base);
02198   CHECK_TYPEDEF (dclass);
02199 
02200   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
02201     {
02202       struct type *iter;
02203       int this_offset;
02204 
02205       iter = check_typedef (TYPE_BASECLASS (dclass, i));
02206 
02207       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
02208                                       address, val);
02209 
02210       if (class_types_same_p (base, iter))
02211         {
02212           /* If this is the first subclass, set *OFFSET and set count
02213              to 1.  Otherwise, if this is at the same offset as
02214              previous instances, do nothing.  Otherwise, increment
02215              count.  */
02216           if (*offset == -1)
02217             {
02218               *offset = this_offset;
02219               count = 1;
02220             }
02221           else if (this_offset == *offset)
02222             {
02223               /* Nothing.  */
02224             }
02225           else
02226             ++count;
02227         }
02228       else
02229         count += is_unique_ancestor_worker (base, iter, offset,
02230                                             valaddr,
02231                                             embedded_offset + this_offset,
02232                                             address, val);
02233     }
02234 
02235   return count;
02236 }
02237 
02238 /* Like is_ancestor, but only returns true if BASE is a unique base
02239    class of the type of VAL.  */
02240 
02241 int
02242 is_unique_ancestor (struct type *base, struct value *val)
02243 {
02244   int offset = -1;
02245 
02246   return is_unique_ancestor_worker (base, value_type (val), &offset,
02247                                     value_contents_for_printing (val),
02248                                     value_embedded_offset (val),
02249                                     value_address (val), val) == 1;
02250 }
02251 
02252 
02253 /* Overload resolution.  */
02254 
02255 /* Return the sum of the rank of A with the rank of B.  */
02256 
02257 struct rank
02258 sum_ranks (struct rank a, struct rank b)
02259 {
02260   struct rank c;
02261   c.rank = a.rank + b.rank;
02262   c.subrank = a.subrank + b.subrank;
02263   return c;
02264 }
02265 
02266 /* Compare rank A and B and return:
02267    0 if a = b
02268    1 if a is better than b
02269   -1 if b is better than a.  */
02270 
02271 int
02272 compare_ranks (struct rank a, struct rank b)
02273 {
02274   if (a.rank == b.rank)
02275     {
02276       if (a.subrank == b.subrank)
02277         return 0;
02278       if (a.subrank < b.subrank)
02279         return 1;
02280       if (a.subrank > b.subrank)
02281         return -1;
02282     }
02283 
02284   if (a.rank < b.rank)
02285     return 1;
02286 
02287   /* a.rank > b.rank */
02288   return -1;
02289 }
02290 
02291 /* Functions for overload resolution begin here.  */
02292 
02293 /* Compare two badness vectors A and B and return the result.
02294    0 => A and B are identical
02295    1 => A and B are incomparable
02296    2 => A is better than B
02297    3 => A is worse than B  */
02298 
02299 int
02300 compare_badness (struct badness_vector *a, struct badness_vector *b)
02301 {
02302   int i;
02303   int tmp;
02304   short found_pos = 0;          /* any positives in c? */
02305   short found_neg = 0;          /* any negatives in c? */
02306 
02307   /* differing lengths => incomparable */
02308   if (a->length != b->length)
02309     return 1;
02310 
02311   /* Subtract b from a */
02312   for (i = 0; i < a->length; i++)
02313     {
02314       tmp = compare_ranks (b->rank[i], a->rank[i]);
02315       if (tmp > 0)
02316         found_pos = 1;
02317       else if (tmp < 0)
02318         found_neg = 1;
02319     }
02320 
02321   if (found_pos)
02322     {
02323       if (found_neg)
02324         return 1;               /* incomparable */
02325       else
02326         return 3;               /* A > B */
02327     }
02328   else
02329     /* no positives */
02330     {
02331       if (found_neg)
02332         return 2;               /* A < B */
02333       else
02334         return 0;               /* A == B */
02335     }
02336 }
02337 
02338 /* Rank a function by comparing its parameter types (PARMS, length
02339    NPARMS), to the types of an argument list (ARGS, length NARGS).
02340    Return a pointer to a badness vector.  This has NARGS + 1
02341    entries.  */
02342 
02343 struct badness_vector *
02344 rank_function (struct type **parms, int nparms, 
02345                struct value **args, int nargs)
02346 {
02347   int i;
02348   struct badness_vector *bv;
02349   int min_len = nparms < nargs ? nparms : nargs;
02350 
02351   bv = xmalloc (sizeof (struct badness_vector));
02352   bv->length = nargs + 1;       /* add 1 for the length-match rank.  */
02353   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
02354 
02355   /* First compare the lengths of the supplied lists.
02356      If there is a mismatch, set it to a high value.  */
02357 
02358   /* pai/1997-06-03 FIXME: when we have debug info about default
02359      arguments and ellipsis parameter lists, we should consider those
02360      and rank the length-match more finely.  */
02361 
02362   LENGTH_MATCH (bv) = (nargs != nparms)
02363                       ? LENGTH_MISMATCH_BADNESS
02364                       : EXACT_MATCH_BADNESS;
02365 
02366   /* Now rank all the parameters of the candidate function.  */
02367   for (i = 1; i <= min_len; i++)
02368     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
02369                                  args[i - 1]);
02370 
02371   /* If more arguments than parameters, add dummy entries.  */
02372   for (i = min_len + 1; i <= nargs; i++)
02373     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
02374 
02375   return bv;
02376 }
02377 
02378 /* Compare the names of two integer types, assuming that any sign
02379    qualifiers have been checked already.  We do it this way because
02380    there may be an "int" in the name of one of the types.  */
02381 
02382 static int
02383 integer_types_same_name_p (const char *first, const char *second)
02384 {
02385   int first_p, second_p;
02386 
02387   /* If both are shorts, return 1; if neither is a short, keep
02388      checking.  */
02389   first_p = (strstr (first, "short") != NULL);
02390   second_p = (strstr (second, "short") != NULL);
02391   if (first_p && second_p)
02392     return 1;
02393   if (first_p || second_p)
02394     return 0;
02395 
02396   /* Likewise for long.  */
02397   first_p = (strstr (first, "long") != NULL);
02398   second_p = (strstr (second, "long") != NULL);
02399   if (first_p && second_p)
02400     return 1;
02401   if (first_p || second_p)
02402     return 0;
02403 
02404   /* Likewise for char.  */
02405   first_p = (strstr (first, "char") != NULL);
02406   second_p = (strstr (second, "char") != NULL);
02407   if (first_p && second_p)
02408     return 1;
02409   if (first_p || second_p)
02410     return 0;
02411 
02412   /* They must both be ints.  */
02413   return 1;
02414 }
02415 
02416 /* Compares type A to type B returns 1 if the represent the same type
02417    0 otherwise.  */
02418 
02419 int
02420 types_equal (struct type *a, struct type *b)
02421 {
02422   /* Identical type pointers.  */
02423   /* However, this still doesn't catch all cases of same type for b
02424      and a.  The reason is that builtin types are different from
02425      the same ones constructed from the object.  */
02426   if (a == b)
02427     return 1;
02428 
02429   /* Resolve typedefs */
02430   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
02431     a = check_typedef (a);
02432   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
02433     b = check_typedef (b);
02434 
02435   /* If after resolving typedefs a and b are not of the same type
02436      code then they are not equal.  */
02437   if (TYPE_CODE (a) != TYPE_CODE (b))
02438     return 0;
02439 
02440   /* If a and b are both pointers types or both reference types then
02441      they are equal of the same type iff the objects they refer to are
02442      of the same type.  */
02443   if (TYPE_CODE (a) == TYPE_CODE_PTR
02444       || TYPE_CODE (a) == TYPE_CODE_REF)
02445     return types_equal (TYPE_TARGET_TYPE (a),
02446                         TYPE_TARGET_TYPE (b));
02447 
02448   /* Well, damnit, if the names are exactly the same, I'll say they
02449      are exactly the same.  This happens when we generate method
02450      stubs.  The types won't point to the same address, but they
02451      really are the same.  */
02452 
02453   if (TYPE_NAME (a) && TYPE_NAME (b)
02454       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
02455     return 1;
02456 
02457   /* Check if identical after resolving typedefs.  */
02458   if (a == b)
02459     return 1;
02460 
02461   /* Two function types are equal if their argument and return types
02462      are equal.  */
02463   if (TYPE_CODE (a) == TYPE_CODE_FUNC)
02464     {
02465       int i;
02466 
02467       if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
02468         return 0;
02469       
02470       if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
02471         return 0;
02472 
02473       for (i = 0; i < TYPE_NFIELDS (a); ++i)
02474         if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
02475           return 0;
02476 
02477       return 1;
02478     }
02479 
02480   return 0;
02481 }
02482 
02483 /* Compare one type (PARM) for compatibility with another (ARG).
02484  * PARM is intended to be the parameter type of a function; and
02485  * ARG is the supplied argument's type.  This function tests if
02486  * the latter can be converted to the former.
02487  * VALUE is the argument's value or NULL if none (or called recursively)
02488  *
02489  * Return 0 if they are identical types;
02490  * Otherwise, return an integer which corresponds to how compatible
02491  * PARM is to ARG.  The higher the return value, the worse the match.
02492  * Generally the "bad" conversions are all uniformly assigned a 100.  */
02493 
02494 struct rank
02495 rank_one_type (struct type *parm, struct type *arg, struct value *value)
02496 {
02497   struct rank rank = {0,0};
02498 
02499   if (types_equal (parm, arg))
02500     return EXACT_MATCH_BADNESS;
02501 
02502   /* Resolve typedefs */
02503   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
02504     parm = check_typedef (parm);
02505   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
02506     arg = check_typedef (arg);
02507 
02508   /* See through references, since we can almost make non-references
02509      references.  */
02510   if (TYPE_CODE (arg) == TYPE_CODE_REF)
02511     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
02512                        REFERENCE_CONVERSION_BADNESS));
02513   if (TYPE_CODE (parm) == TYPE_CODE_REF)
02514     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
02515                        REFERENCE_CONVERSION_BADNESS));
02516   if (overload_debug)
02517   /* Debugging only.  */
02518     fprintf_filtered (gdb_stderr, 
02519                       "------ Arg is %s [%d], parm is %s [%d]\n",
02520                       TYPE_NAME (arg), TYPE_CODE (arg), 
02521                       TYPE_NAME (parm), TYPE_CODE (parm));
02522 
02523   /* x -> y means arg of type x being supplied for parameter of type y.  */
02524 
02525   switch (TYPE_CODE (parm))
02526     {
02527     case TYPE_CODE_PTR:
02528       switch (TYPE_CODE (arg))
02529         {
02530         case TYPE_CODE_PTR:
02531 
02532           /* Allowed pointer conversions are:
02533              (a) pointer to void-pointer conversion.  */
02534           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
02535             return VOID_PTR_CONVERSION_BADNESS;
02536 
02537           /* (b) pointer to ancestor-pointer conversion.  */
02538           rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
02539                                                TYPE_TARGET_TYPE (arg),
02540                                                0);
02541           if (rank.subrank >= 0)
02542             return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
02543 
02544           return INCOMPATIBLE_TYPE_BADNESS;
02545         case TYPE_CODE_ARRAY:
02546           if (types_equal (TYPE_TARGET_TYPE (parm),
02547                            TYPE_TARGET_TYPE (arg)))
02548             return EXACT_MATCH_BADNESS;
02549           return INCOMPATIBLE_TYPE_BADNESS;
02550         case TYPE_CODE_FUNC:
02551           return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
02552         case TYPE_CODE_INT:
02553           if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
02554             {
02555               if (value_as_long (value) == 0)
02556                 {
02557                   /* Null pointer conversion: allow it to be cast to a pointer.
02558                      [4.10.1 of C++ standard draft n3290]  */
02559                   return NULL_POINTER_CONVERSION_BADNESS;
02560                 }
02561               else
02562                 {
02563                   /* If type checking is disabled, allow the conversion.  */
02564                   if (!strict_type_checking)
02565                     return NS_INTEGER_POINTER_CONVERSION_BADNESS;
02566                 }
02567             }
02568           /* fall through  */
02569         case TYPE_CODE_ENUM:
02570         case TYPE_CODE_FLAGS:
02571         case TYPE_CODE_CHAR:
02572         case TYPE_CODE_RANGE:
02573         case TYPE_CODE_BOOL:
02574         default:
02575           return INCOMPATIBLE_TYPE_BADNESS;
02576         }
02577     case TYPE_CODE_ARRAY:
02578       switch (TYPE_CODE (arg))
02579         {
02580         case TYPE_CODE_PTR:
02581         case TYPE_CODE_ARRAY:
02582           return rank_one_type (TYPE_TARGET_TYPE (parm), 
02583                                 TYPE_TARGET_TYPE (arg), NULL);
02584         default:
02585           return INCOMPATIBLE_TYPE_BADNESS;
02586         }
02587     case TYPE_CODE_FUNC:
02588       switch (TYPE_CODE (arg))
02589         {
02590         case TYPE_CODE_PTR:     /* funcptr -> func */
02591           return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
02592         default:
02593           return INCOMPATIBLE_TYPE_BADNESS;
02594         }
02595     case TYPE_CODE_INT:
02596       switch (TYPE_CODE (arg))
02597         {
02598         case TYPE_CODE_INT:
02599           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
02600             {
02601               /* Deal with signed, unsigned, and plain chars and
02602                  signed and unsigned ints.  */
02603               if (TYPE_NOSIGN (parm))
02604                 {
02605                   /* This case only for character types.  */
02606                   if (TYPE_NOSIGN (arg))
02607                     return EXACT_MATCH_BADNESS; /* plain char -> plain char */
02608                   else          /* signed/unsigned char -> plain char */
02609                     return INTEGER_CONVERSION_BADNESS;
02610                 }
02611               else if (TYPE_UNSIGNED (parm))
02612                 {
02613                   if (TYPE_UNSIGNED (arg))
02614                     {
02615                       /* unsigned int -> unsigned int, or 
02616                          unsigned long -> unsigned long */
02617                       if (integer_types_same_name_p (TYPE_NAME (parm), 
02618                                                      TYPE_NAME (arg)))
02619                         return EXACT_MATCH_BADNESS;
02620                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
02621                                                           "int")
02622                                && integer_types_same_name_p (TYPE_NAME (parm),
02623                                                              "long"))
02624                         /* unsigned int -> unsigned long */
02625                         return INTEGER_PROMOTION_BADNESS;
02626                       else
02627                         /* unsigned long -> unsigned int */
02628                         return INTEGER_CONVERSION_BADNESS;
02629                     }
02630                   else
02631                     {
02632                       if (integer_types_same_name_p (TYPE_NAME (arg), 
02633                                                      "long")
02634                           && integer_types_same_name_p (TYPE_NAME (parm), 
02635                                                         "int"))
02636                         /* signed long -> unsigned int */
02637                         return INTEGER_CONVERSION_BADNESS;
02638                       else
02639                         /* signed int/long -> unsigned int/long */
02640                         return INTEGER_CONVERSION_BADNESS;
02641                     }
02642                 }
02643               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
02644                 {
02645                   if (integer_types_same_name_p (TYPE_NAME (parm), 
02646                                                  TYPE_NAME (arg)))
02647                     return EXACT_MATCH_BADNESS;
02648                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
02649                                                       "int")
02650                            && integer_types_same_name_p (TYPE_NAME (parm), 
02651                                                          "long"))
02652                     return INTEGER_PROMOTION_BADNESS;
02653                   else
02654                     return INTEGER_CONVERSION_BADNESS;
02655                 }
02656               else
02657                 return INTEGER_CONVERSION_BADNESS;
02658             }
02659           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
02660             return INTEGER_PROMOTION_BADNESS;
02661           else
02662             return INTEGER_CONVERSION_BADNESS;
02663         case TYPE_CODE_ENUM:
02664         case TYPE_CODE_FLAGS:
02665         case TYPE_CODE_CHAR:
02666         case TYPE_CODE_RANGE:
02667         case TYPE_CODE_BOOL:
02668           return INTEGER_PROMOTION_BADNESS;
02669         case TYPE_CODE_FLT:
02670           return INT_FLOAT_CONVERSION_BADNESS;
02671         case TYPE_CODE_PTR:
02672           return NS_POINTER_CONVERSION_BADNESS;
02673         default:
02674           return INCOMPATIBLE_TYPE_BADNESS;
02675         }
02676       break;
02677     case TYPE_CODE_ENUM:
02678       switch (TYPE_CODE (arg))
02679         {
02680         case TYPE_CODE_INT:
02681         case TYPE_CODE_CHAR:
02682         case TYPE_CODE_RANGE:
02683         case TYPE_CODE_BOOL:
02684         case TYPE_CODE_ENUM:
02685           return INTEGER_CONVERSION_BADNESS;
02686         case TYPE_CODE_FLT:
02687           return INT_FLOAT_CONVERSION_BADNESS;
02688         default:
02689           return INCOMPATIBLE_TYPE_BADNESS;
02690         }
02691       break;
02692     case TYPE_CODE_CHAR:
02693       switch (TYPE_CODE (arg))
02694         {
02695         case TYPE_CODE_RANGE:
02696         case TYPE_CODE_BOOL:
02697         case TYPE_CODE_ENUM:
02698           return INTEGER_CONVERSION_BADNESS;
02699         case TYPE_CODE_FLT:
02700           return INT_FLOAT_CONVERSION_BADNESS;
02701         case TYPE_CODE_INT:
02702           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
02703             return INTEGER_CONVERSION_BADNESS;
02704           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
02705             return INTEGER_PROMOTION_BADNESS;
02706           /* >>> !! else fall through !! <<< */
02707         case TYPE_CODE_CHAR:
02708           /* Deal with signed, unsigned, and plain chars for C++ and
02709              with int cases falling through from previous case.  */
02710           if (TYPE_NOSIGN (parm))
02711             {
02712               if (TYPE_NOSIGN (arg))
02713                 return EXACT_MATCH_BADNESS;
02714               else
02715                 return INTEGER_CONVERSION_BADNESS;
02716             }
02717           else if (TYPE_UNSIGNED (parm))
02718             {
02719               if (TYPE_UNSIGNED (arg))
02720                 return EXACT_MATCH_BADNESS;
02721               else
02722                 return INTEGER_PROMOTION_BADNESS;
02723             }
02724           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
02725             return EXACT_MATCH_BADNESS;
02726           else
02727             return INTEGER_CONVERSION_BADNESS;
02728         default:
02729           return INCOMPATIBLE_TYPE_BADNESS;
02730         }
02731       break;
02732     case TYPE_CODE_RANGE:
02733       switch (TYPE_CODE (arg))
02734         {
02735         case TYPE_CODE_INT:
02736         case TYPE_CODE_CHAR:
02737         case TYPE_CODE_RANGE:
02738         case TYPE_CODE_BOOL:
02739         case TYPE_CODE_ENUM:
02740           return INTEGER_CONVERSION_BADNESS;
02741         case TYPE_CODE_FLT:
02742           return INT_FLOAT_CONVERSION_BADNESS;
02743         default:
02744           return INCOMPATIBLE_TYPE_BADNESS;
02745         }
02746       break;
02747     case TYPE_CODE_BOOL:
02748       switch (TYPE_CODE (arg))
02749         {
02750           /* n3290 draft, section 4.12.1 (conv.bool):
02751 
02752              "A prvalue of arithmetic, unscoped enumeration, pointer, or
02753              pointer to member type can be converted to a prvalue of type
02754              bool.  A zero value, null pointer value, or null member pointer
02755              value is converted to false; any other value is converted to
02756              true.  A prvalue of type std::nullptr_t can be converted to a
02757              prvalue of type bool; the resulting value is false."  */
02758         case TYPE_CODE_INT:
02759         case TYPE_CODE_CHAR:
02760         case TYPE_CODE_ENUM:
02761         case TYPE_CODE_FLT:
02762         case TYPE_CODE_MEMBERPTR:
02763         case TYPE_CODE_PTR:
02764           return BOOL_CONVERSION_BADNESS;
02765         case TYPE_CODE_RANGE:
02766           return INCOMPATIBLE_TYPE_BADNESS;
02767         case TYPE_CODE_BOOL:
02768           return EXACT_MATCH_BADNESS;
02769         default:
02770           return INCOMPATIBLE_TYPE_BADNESS;
02771         }
02772       break;
02773     case TYPE_CODE_FLT:
02774       switch (TYPE_CODE (arg))
02775         {
02776         case TYPE_CODE_FLT:
02777           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
02778             return FLOAT_PROMOTION_BADNESS;
02779           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
02780             return EXACT_MATCH_BADNESS;
02781           else
02782             return FLOAT_CONVERSION_BADNESS;
02783         case TYPE_CODE_INT:
02784         case TYPE_CODE_BOOL:
02785         case TYPE_CODE_ENUM:
02786         case TYPE_CODE_RANGE:
02787         case TYPE_CODE_CHAR:
02788           return INT_FLOAT_CONVERSION_BADNESS;
02789         default:
02790           return INCOMPATIBLE_TYPE_BADNESS;
02791         }
02792       break;
02793     case TYPE_CODE_COMPLEX:
02794       switch (TYPE_CODE (arg))
02795         {               /* Strictly not needed for C++, but...  */
02796         case TYPE_CODE_FLT:
02797           return FLOAT_PROMOTION_BADNESS;
02798         case TYPE_CODE_COMPLEX:
02799           return EXACT_MATCH_BADNESS;
02800         default:
02801           return INCOMPATIBLE_TYPE_BADNESS;
02802         }
02803       break;
02804     case TYPE_CODE_STRUCT:
02805       /* currently same as TYPE_CODE_CLASS.  */
02806       switch (TYPE_CODE (arg))
02807         {
02808         case TYPE_CODE_STRUCT:
02809           /* Check for derivation */
02810           rank.subrank = distance_to_ancestor (parm, arg, 0);
02811           if (rank.subrank >= 0)
02812             return sum_ranks (BASE_CONVERSION_BADNESS, rank);
02813           /* else fall through */
02814         default:
02815           return INCOMPATIBLE_TYPE_BADNESS;
02816         }
02817       break;
02818     case TYPE_CODE_UNION:
02819       switch (TYPE_CODE (arg))
02820         {
02821         case TYPE_CODE_UNION:
02822         default:
02823           return INCOMPATIBLE_TYPE_BADNESS;
02824         }
02825       break;
02826     case TYPE_CODE_MEMBERPTR:
02827       switch (TYPE_CODE (arg))
02828         {
02829         default:
02830           return INCOMPATIBLE_TYPE_BADNESS;
02831         }
02832       break;
02833     case TYPE_CODE_METHOD:
02834       switch (TYPE_CODE (arg))
02835         {
02836 
02837         default:
02838           return INCOMPATIBLE_TYPE_BADNESS;
02839         }
02840       break;
02841     case TYPE_CODE_REF:
02842       switch (TYPE_CODE (arg))
02843         {
02844 
02845         default:
02846           return INCOMPATIBLE_TYPE_BADNESS;
02847         }
02848 
02849       break;
02850     case TYPE_CODE_SET:
02851       switch (TYPE_CODE (arg))
02852         {
02853           /* Not in C++ */
02854         case TYPE_CODE_SET:
02855           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
02856                                 TYPE_FIELD_TYPE (arg, 0), NULL);
02857         default:
02858           return INCOMPATIBLE_TYPE_BADNESS;
02859         }
02860       break;
02861     case TYPE_CODE_VOID:
02862     default:
02863       return INCOMPATIBLE_TYPE_BADNESS;
02864     }                           /* switch (TYPE_CODE (arg)) */
02865 }
02866 
02867 /* End of functions for overload resolution.  */
02868 
02869 /* Routines to pretty-print types.  */
02870 
02871 static void
02872 print_bit_vector (B_TYPE *bits, int nbits)
02873 {
02874   int bitno;
02875 
02876   for (bitno = 0; bitno < nbits; bitno++)
02877     {
02878       if ((bitno % 8) == 0)
02879         {
02880           puts_filtered (" ");
02881         }
02882       if (B_TST (bits, bitno))
02883         printf_filtered (("1"));
02884       else
02885         printf_filtered (("0"));
02886     }
02887 }
02888 
02889 /* Note the first arg should be the "this" pointer, we may not want to
02890    include it since we may get into a infinitely recursive
02891    situation.  */
02892 
02893 static void
02894 print_arg_types (struct field *args, int nargs, int spaces)
02895 {
02896   if (args != NULL)
02897     {
02898       int i;
02899 
02900       for (i = 0; i < nargs; i++)
02901         recursive_dump_type (args[i].type, spaces + 2);
02902     }
02903 }
02904 
02905 int
02906 field_is_static (struct field *f)
02907 {
02908   /* "static" fields are the fields whose location is not relative
02909      to the address of the enclosing struct.  It would be nice to
02910      have a dedicated flag that would be set for static fields when
02911      the type is being created.  But in practice, checking the field
02912      loc_kind should give us an accurate answer.  */
02913   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
02914           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
02915 }
02916 
02917 static void
02918 dump_fn_fieldlists (struct type *type, int spaces)
02919 {
02920   int method_idx;
02921   int overload_idx;
02922   struct fn_field *f;
02923 
02924   printfi_filtered (spaces, "fn_fieldlists ");
02925   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
02926   printf_filtered ("\n");
02927   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
02928     {
02929       f = TYPE_FN_FIELDLIST1 (type, method_idx);
02930       printfi_filtered (spaces + 2, "[%d] name '%s' (",
02931                         method_idx,
02932                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
02933       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
02934                               gdb_stdout);
02935       printf_filtered (_(") length %d\n"),
02936                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
02937       for (overload_idx = 0;
02938            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
02939            overload_idx++)
02940         {
02941           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
02942                             overload_idx,
02943                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
02944           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
02945                                   gdb_stdout);
02946           printf_filtered (")\n");
02947           printfi_filtered (spaces + 8, "type ");
02948           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
02949                                   gdb_stdout);
02950           printf_filtered ("\n");
02951 
02952           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
02953                                spaces + 8 + 2);
02954 
02955           printfi_filtered (spaces + 8, "args ");
02956           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
02957                                   gdb_stdout);
02958           printf_filtered ("\n");
02959 
02960           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
02961                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, 
02962                                                              overload_idx)),
02963                            spaces);
02964           printfi_filtered (spaces + 8, "fcontext ");
02965           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
02966                                   gdb_stdout);
02967           printf_filtered ("\n");
02968 
02969           printfi_filtered (spaces + 8, "is_const %d\n",
02970                             TYPE_FN_FIELD_CONST (f, overload_idx));
02971           printfi_filtered (spaces + 8, "is_volatile %d\n",
02972                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
02973           printfi_filtered (spaces + 8, "is_private %d\n",
02974                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
02975           printfi_filtered (spaces + 8, "is_protected %d\n",
02976                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
02977           printfi_filtered (spaces + 8, "is_stub %d\n",
02978                             TYPE_FN_FIELD_STUB (f, overload_idx));
02979           printfi_filtered (spaces + 8, "voffset %u\n",
02980                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
02981         }
02982     }
02983 }
02984 
02985 static void
02986 print_cplus_stuff (struct type *type, int spaces)
02987 {
02988   printfi_filtered (spaces, "n_baseclasses %d\n",
02989                     TYPE_N_BASECLASSES (type));
02990   printfi_filtered (spaces, "nfn_fields %d\n",
02991                     TYPE_NFN_FIELDS (type));
02992   if (TYPE_N_BASECLASSES (type) > 0)
02993     {
02994       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
02995                         TYPE_N_BASECLASSES (type));
02996       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
02997                               gdb_stdout);
02998       printf_filtered (")");
02999 
03000       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
03001                         TYPE_N_BASECLASSES (type));
03002       puts_filtered ("\n");
03003     }
03004   if (TYPE_NFIELDS (type) > 0)
03005     {
03006       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
03007         {
03008           printfi_filtered (spaces, 
03009                             "private_field_bits (%d bits at *",
03010                             TYPE_NFIELDS (type));
03011           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
03012                                   gdb_stdout);
03013           printf_filtered (")");
03014           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
03015                             TYPE_NFIELDS (type));
03016           puts_filtered ("\n");
03017         }
03018       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
03019         {
03020           printfi_filtered (spaces, 
03021                             "protected_field_bits (%d bits at *",
03022                             TYPE_NFIELDS (type));
03023           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
03024                                   gdb_stdout);
03025           printf_filtered (")");
03026           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
03027                             TYPE_NFIELDS (type));
03028           puts_filtered ("\n");
03029         }
03030     }
03031   if (TYPE_NFN_FIELDS (type) > 0)
03032     {
03033       dump_fn_fieldlists (type, spaces);
03034     }
03035 }
03036 
03037 /* Print the contents of the TYPE's type_specific union, assuming that
03038    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */
03039 
03040 static void
03041 print_gnat_stuff (struct type *type, int spaces)
03042 {
03043   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
03044 
03045   recursive_dump_type (descriptive_type, spaces + 2);
03046 }
03047 
03048 static struct obstack dont_print_type_obstack;
03049 
03050 void
03051 recursive_dump_type (struct type *type, int spaces)
03052 {
03053   int idx;
03054 
03055   if (spaces == 0)
03056     obstack_begin (&dont_print_type_obstack, 0);
03057 
03058   if (TYPE_NFIELDS (type) > 0
03059       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
03060     {
03061       struct type **first_dont_print
03062         = (struct type **) obstack_base (&dont_print_type_obstack);
03063 
03064       int i = (struct type **) 
03065         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
03066 
03067       while (--i >= 0)
03068         {
03069           if (type == first_dont_print[i])
03070             {
03071               printfi_filtered (spaces, "type node ");
03072               gdb_print_host_address (type, gdb_stdout);
03073               printf_filtered (_(" <same as already seen type>\n"));
03074               return;
03075             }
03076         }
03077 
03078       obstack_ptr_grow (&dont_print_type_obstack, type);
03079     }
03080 
03081   printfi_filtered (spaces, "type node ");
03082   gdb_print_host_address (type, gdb_stdout);
03083   printf_filtered ("\n");
03084   printfi_filtered (spaces, "name '%s' (",
03085                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
03086   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
03087   printf_filtered (")\n");
03088   printfi_filtered (spaces, "tagname '%s' (",
03089                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
03090   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
03091   printf_filtered (")\n");
03092   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
03093   switch (TYPE_CODE (type))
03094     {
03095     case TYPE_CODE_UNDEF:
03096       printf_filtered ("(TYPE_CODE_UNDEF)");
03097       break;
03098     case TYPE_CODE_PTR:
03099       printf_filtered ("(TYPE_CODE_PTR)");
03100       break;
03101     case TYPE_CODE_ARRAY:
03102       printf_filtered ("(TYPE_CODE_ARRAY)");
03103       break;
03104     case TYPE_CODE_STRUCT:
03105       printf_filtered ("(TYPE_CODE_STRUCT)");
03106       break;
03107     case TYPE_CODE_UNION:
03108       printf_filtered ("(TYPE_CODE_UNION)");
03109       break;
03110     case TYPE_CODE_ENUM:
03111       printf_filtered ("(TYPE_CODE_ENUM)");
03112       break;
03113     case TYPE_CODE_FLAGS:
03114       printf_filtered ("(TYPE_CODE_FLAGS)");
03115       break;
03116     case TYPE_CODE_FUNC:
03117       printf_filtered ("(TYPE_CODE_FUNC)");
03118       break;
03119     case TYPE_CODE_INT:
03120       printf_filtered ("(TYPE_CODE_INT)");
03121       break;
03122     case TYPE_CODE_FLT:
03123       printf_filtered ("(TYPE_CODE_FLT)");
03124       break;
03125     case TYPE_CODE_VOID:
03126       printf_filtered ("(TYPE_CODE_VOID)");
03127       break;
03128     case TYPE_CODE_SET:
03129       printf_filtered ("(TYPE_CODE_SET)");
03130       break;
03131     case TYPE_CODE_RANGE:
03132       printf_filtered ("(TYPE_CODE_RANGE)");
03133       break;
03134     case TYPE_CODE_STRING:
03135       printf_filtered ("(TYPE_CODE_STRING)");
03136       break;
03137     case TYPE_CODE_ERROR:
03138       printf_filtered ("(TYPE_CODE_ERROR)");
03139       break;
03140     case TYPE_CODE_MEMBERPTR:
03141       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
03142       break;
03143     case TYPE_CODE_METHODPTR:
03144       printf_filtered ("(TYPE_CODE_METHODPTR)");
03145       break;
03146     case TYPE_CODE_METHOD:
03147       printf_filtered ("(TYPE_CODE_METHOD)");
03148       break;
03149     case TYPE_CODE_REF:
03150       printf_filtered ("(TYPE_CODE_REF)");
03151       break;
03152     case TYPE_CODE_CHAR:
03153       printf_filtered ("(TYPE_CODE_CHAR)");
03154       break;
03155     case TYPE_CODE_BOOL:
03156       printf_filtered ("(TYPE_CODE_BOOL)");
03157       break;
03158     case TYPE_CODE_COMPLEX:
03159       printf_filtered ("(TYPE_CODE_COMPLEX)");
03160       break;
03161     case TYPE_CODE_TYPEDEF:
03162       printf_filtered ("(TYPE_CODE_TYPEDEF)");
03163       break;
03164     case TYPE_CODE_NAMESPACE:
03165       printf_filtered ("(TYPE_CODE_NAMESPACE)");
03166       break;
03167     default:
03168       printf_filtered ("(UNKNOWN TYPE CODE)");
03169       break;
03170     }
03171   puts_filtered ("\n");
03172   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
03173   if (TYPE_OBJFILE_OWNED (type))
03174     {
03175       printfi_filtered (spaces, "objfile ");
03176       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
03177     }
03178   else
03179     {
03180       printfi_filtered (spaces, "gdbarch ");
03181       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
03182     }
03183   printf_filtered ("\n");
03184   printfi_filtered (spaces, "target_type ");
03185   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
03186   printf_filtered ("\n");
03187   if (TYPE_TARGET_TYPE (type) != NULL)
03188     {
03189       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
03190     }
03191   printfi_filtered (spaces, "pointer_type ");
03192   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
03193   printf_filtered ("\n");
03194   printfi_filtered (spaces, "reference_type ");
03195   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
03196   printf_filtered ("\n");
03197   printfi_filtered (spaces, "type_chain ");
03198   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
03199   printf_filtered ("\n");
03200   printfi_filtered (spaces, "instance_flags 0x%x", 
03201                     TYPE_INSTANCE_FLAGS (type));
03202   if (TYPE_CONST (type))
03203     {
03204       puts_filtered (" TYPE_FLAG_CONST");
03205     }
03206   if (TYPE_VOLATILE (type))
03207     {
03208       puts_filtered (" TYPE_FLAG_VOLATILE");
03209     }
03210   if (TYPE_CODE_SPACE (type))
03211     {
03212       puts_filtered (" TYPE_FLAG_CODE_SPACE");
03213     }
03214   if (TYPE_DATA_SPACE (type))
03215     {
03216       puts_filtered (" TYPE_FLAG_DATA_SPACE");
03217     }
03218   if (TYPE_ADDRESS_CLASS_1 (type))
03219     {
03220       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
03221     }
03222   if (TYPE_ADDRESS_CLASS_2 (type))
03223     {
03224       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
03225     }
03226   if (TYPE_RESTRICT (type))
03227     {
03228       puts_filtered (" TYPE_FLAG_RESTRICT");
03229     }
03230   puts_filtered ("\n");
03231 
03232   printfi_filtered (spaces, "flags");
03233   if (TYPE_UNSIGNED (type))
03234     {
03235       puts_filtered (" TYPE_FLAG_UNSIGNED");
03236     }
03237   if (TYPE_NOSIGN (type))
03238     {
03239       puts_filtered (" TYPE_FLAG_NOSIGN");
03240     }
03241   if (TYPE_STUB (type))
03242     {
03243       puts_filtered (" TYPE_FLAG_STUB");
03244     }
03245   if (TYPE_TARGET_STUB (type))
03246     {
03247       puts_filtered (" TYPE_FLAG_TARGET_STUB");
03248     }
03249   if (TYPE_STATIC (type))
03250     {
03251       puts_filtered (" TYPE_FLAG_STATIC");
03252     }
03253   if (TYPE_PROTOTYPED (type))
03254     {
03255       puts_filtered (" TYPE_FLAG_PROTOTYPED");
03256     }
03257   if (TYPE_INCOMPLETE (type))
03258     {
03259       puts_filtered (" TYPE_FLAG_INCOMPLETE");
03260     }
03261   if (TYPE_VARARGS (type))
03262     {
03263       puts_filtered (" TYPE_FLAG_VARARGS");
03264     }
03265   /* This is used for things like AltiVec registers on ppc.  Gcc emits
03266      an attribute for the array type, which tells whether or not we
03267      have a vector, instead of a regular array.  */
03268   if (TYPE_VECTOR (type))
03269     {
03270       puts_filtered (" TYPE_FLAG_VECTOR");
03271     }
03272   if (TYPE_FIXED_INSTANCE (type))
03273     {
03274       puts_filtered (" TYPE_FIXED_INSTANCE");
03275     }
03276   if (TYPE_STUB_SUPPORTED (type))
03277     {
03278       puts_filtered (" TYPE_STUB_SUPPORTED");
03279     }
03280   if (TYPE_NOTTEXT (type))
03281     {
03282       puts_filtered (" TYPE_NOTTEXT");
03283     }
03284   puts_filtered ("\n");
03285   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
03286   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
03287   puts_filtered ("\n");
03288   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
03289     {
03290       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
03291         printfi_filtered (spaces + 2,
03292                           "[%d] enumval %s type ",
03293                           idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
03294       else
03295         printfi_filtered (spaces + 2,
03296                           "[%d] bitpos %d bitsize %d type ",
03297                           idx, TYPE_FIELD_BITPOS (type, idx),
03298                           TYPE_FIELD_BITSIZE (type, idx));
03299       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
03300       printf_filtered (" name '%s' (",
03301                        TYPE_FIELD_NAME (type, idx) != NULL
03302                        ? TYPE_FIELD_NAME (type, idx)
03303                        : "<NULL>");
03304       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
03305       printf_filtered (")\n");
03306       if (TYPE_FIELD_TYPE (type, idx) != NULL)
03307         {
03308           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
03309         }
03310     }
03311   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
03312     {
03313       printfi_filtered (spaces, "low %s%s  high %s%s\n",
03314                         plongest (TYPE_LOW_BOUND (type)), 
03315                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
03316                         plongest (TYPE_HIGH_BOUND (type)),
03317                         TYPE_HIGH_BOUND_UNDEFINED (type) 
03318                         ? " (undefined)" : "");
03319     }
03320   printfi_filtered (spaces, "vptr_basetype ");
03321   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
03322   puts_filtered ("\n");
03323   if (TYPE_VPTR_BASETYPE (type) != NULL)
03324     {
03325       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
03326     }
03327   printfi_filtered (spaces, "vptr_fieldno %d\n", 
03328                     TYPE_VPTR_FIELDNO (type));
03329 
03330   switch (TYPE_SPECIFIC_FIELD (type))
03331     {
03332       case TYPE_SPECIFIC_CPLUS_STUFF:
03333         printfi_filtered (spaces, "cplus_stuff ");
03334         gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
03335                                 gdb_stdout);
03336         puts_filtered ("\n");
03337         print_cplus_stuff (type, spaces);
03338         break;
03339 
03340       case TYPE_SPECIFIC_GNAT_STUFF:
03341         printfi_filtered (spaces, "gnat_stuff ");
03342         gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
03343         puts_filtered ("\n");
03344         print_gnat_stuff (type, spaces);
03345         break;
03346 
03347       case TYPE_SPECIFIC_FLOATFORMAT:
03348         printfi_filtered (spaces, "floatformat ");
03349         if (TYPE_FLOATFORMAT (type) == NULL)
03350           puts_filtered ("(null)");
03351         else
03352           {
03353             puts_filtered ("{ ");
03354             if (TYPE_FLOATFORMAT (type)[0] == NULL
03355                 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
03356               puts_filtered ("(null)");
03357             else
03358               puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
03359 
03360             puts_filtered (", ");
03361             if (TYPE_FLOATFORMAT (type)[1] == NULL
03362                 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
03363               puts_filtered ("(null)");
03364             else
03365               puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
03366 
03367             puts_filtered (" }");
03368           }
03369         puts_filtered ("\n");
03370         break;
03371 
03372       case TYPE_SPECIFIC_FUNC:
03373         printfi_filtered (spaces, "calling_convention %d\n",
03374                           TYPE_CALLING_CONVENTION (type));
03375         /* tail_call_list is not printed.  */
03376         break;
03377     }
03378 
03379   if (spaces == 0)
03380     obstack_free (&dont_print_type_obstack, NULL);
03381 }
03382 
03383 /* Trivial helpers for the libiberty hash table, for mapping one
03384    type to another.  */
03385 
03386 struct type_pair
03387 {
03388   struct type *old, *new;
03389 };
03390 
03391 static hashval_t
03392 type_pair_hash (const void *item)
03393 {
03394   const struct type_pair *pair = item;
03395 
03396   return htab_hash_pointer (pair->old);
03397 }
03398 
03399 static int
03400 type_pair_eq (const void *item_lhs, const void *item_rhs)
03401 {
03402   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
03403 
03404   return lhs->old == rhs->old;
03405 }
03406 
03407 /* Allocate the hash table used by copy_type_recursive to walk
03408    types without duplicates.  We use OBJFILE's obstack, because
03409    OBJFILE is about to be deleted.  */
03410 
03411 htab_t
03412 create_copied_types_hash (struct objfile *objfile)
03413 {
03414   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
03415                                NULL, &objfile->objfile_obstack,
03416                                hashtab_obstack_allocate,
03417                                dummy_obstack_deallocate);
03418 }
03419 
03420 /* Recursively copy (deep copy) TYPE, if it is associated with
03421    OBJFILE.  Return a new type allocated using malloc, a saved type if
03422    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
03423    not associated with OBJFILE.  */
03424 
03425 struct type *
03426 copy_type_recursive (struct objfile *objfile, 
03427                      struct type *type,
03428                      htab_t copied_types)
03429 {
03430   struct type_pair *stored, pair;
03431   void **slot;
03432   struct type *new_type;
03433 
03434   if (! TYPE_OBJFILE_OWNED (type))
03435     return type;
03436 
03437   /* This type shouldn't be pointing to any types in other objfiles;
03438      if it did, the type might disappear unexpectedly.  */
03439   gdb_assert (TYPE_OBJFILE (type) == objfile);
03440 
03441   pair.old = type;
03442   slot = htab_find_slot (copied_types, &pair, INSERT);
03443   if (*slot != NULL)
03444     return ((struct type_pair *) *slot)->new;
03445 
03446   new_type = alloc_type_arch (get_type_arch (type));
03447 
03448   /* We must add the new type to the hash table immediately, in case
03449      we encounter this type again during a recursive call below.  */
03450   stored
03451     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
03452   stored->old = type;
03453   stored->new = new_type;
03454   *slot = stored;
03455 
03456   /* Copy the common fields of types.  For the main type, we simply
03457      copy the entire thing and then update specific fields as needed.  */
03458   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
03459   TYPE_OBJFILE_OWNED (new_type) = 0;
03460   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
03461 
03462   if (TYPE_NAME (type))
03463     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
03464   if (TYPE_TAG_NAME (type))
03465     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
03466 
03467   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
03468   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
03469 
03470   /* Copy the fields.  */
03471   if (TYPE_NFIELDS (type))
03472     {
03473       int i, nfields;
03474 
03475       nfields = TYPE_NFIELDS (type);
03476       TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field);
03477       for (i = 0; i < nfields; i++)
03478         {
03479           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
03480             TYPE_FIELD_ARTIFICIAL (type, i);
03481           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
03482           if (TYPE_FIELD_TYPE (type, i))
03483             TYPE_FIELD_TYPE (new_type, i)
03484               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
03485                                      copied_types);
03486           if (TYPE_FIELD_NAME (type, i))
03487             TYPE_FIELD_NAME (new_type, i) = 
03488               xstrdup (TYPE_FIELD_NAME (type, i));
03489           switch (TYPE_FIELD_LOC_KIND (type, i))
03490             {
03491             case FIELD_LOC_KIND_BITPOS:
03492               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
03493                                 TYPE_FIELD_BITPOS (type, i));
03494               break;
03495             case FIELD_LOC_KIND_ENUMVAL:
03496               SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
03497                                  TYPE_FIELD_ENUMVAL (type, i));
03498               break;
03499             case FIELD_LOC_KIND_PHYSADDR:
03500               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
03501                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
03502               break;
03503             case FIELD_LOC_KIND_PHYSNAME:
03504               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
03505                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
03506                                                                        i)));
03507               break;
03508             default:
03509               internal_error (__FILE__, __LINE__,
03510                               _("Unexpected type field location kind: %d"),
03511                               TYPE_FIELD_LOC_KIND (type, i));
03512             }
03513         }
03514     }
03515 
03516   /* For range types, copy the bounds information.  */
03517   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
03518     {
03519       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
03520       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
03521     }
03522 
03523   /* Copy pointers to other types.  */
03524   if (TYPE_TARGET_TYPE (type))
03525     TYPE_TARGET_TYPE (new_type) = 
03526       copy_type_recursive (objfile, 
03527                            TYPE_TARGET_TYPE (type),
03528                            copied_types);
03529   if (TYPE_VPTR_BASETYPE (type))
03530     TYPE_VPTR_BASETYPE (new_type) = 
03531       copy_type_recursive (objfile,
03532                            TYPE_VPTR_BASETYPE (type),
03533                            copied_types);
03534   /* Maybe copy the type_specific bits.
03535 
03536      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
03537      base classes and methods.  There's no fundamental reason why we
03538      can't, but at the moment it is not needed.  */
03539 
03540   if (TYPE_CODE (type) == TYPE_CODE_FLT)
03541     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
03542   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
03543            || TYPE_CODE (type) == TYPE_CODE_UNION
03544            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
03545     INIT_CPLUS_SPECIFIC (new_type);
03546 
03547   return new_type;
03548 }
03549 
03550 /* Make a copy of the given TYPE, except that the pointer & reference
03551    types are not preserved.
03552    
03553    This function assumes that the given type has an associated objfile.
03554    This objfile is used to allocate the new type.  */
03555 
03556 struct type *
03557 copy_type (const struct type *type)
03558 {
03559   struct type *new_type;
03560 
03561   gdb_assert (TYPE_OBJFILE_OWNED (type));
03562 
03563   new_type = alloc_type_copy (type);
03564   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
03565   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
03566   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
03567           sizeof (struct main_type));
03568 
03569   return new_type;
03570 }
03571 
03572 /* Helper functions to initialize architecture-specific types.  */
03573 
03574 /* Allocate a type structure associated with GDBARCH and set its
03575    CODE, LENGTH, and NAME fields.  */
03576 
03577 struct type *
03578 arch_type (struct gdbarch *gdbarch,
03579            enum type_code code, int length, char *name)
03580 {
03581   struct type *type;
03582 
03583   type = alloc_type_arch (gdbarch);
03584   TYPE_CODE (type) = code;
03585   TYPE_LENGTH (type) = length;
03586 
03587   if (name)
03588     TYPE_NAME (type) = xstrdup (name);
03589 
03590   return type;
03591 }
03592 
03593 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
03594    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
03595    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
03596 
03597 struct type *
03598 arch_integer_type (struct gdbarch *gdbarch,
03599                    int bit, int unsigned_p, char *name)
03600 {
03601   struct type *t;
03602 
03603   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
03604   if (unsigned_p)
03605     TYPE_UNSIGNED (t) = 1;
03606   if (name && strcmp (name, "char") == 0)
03607     TYPE_NOSIGN (t) = 1;
03608 
03609   return t;
03610 }
03611 
03612 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
03613    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
03614    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
03615 
03616 struct type *
03617 arch_character_type (struct gdbarch *gdbarch,
03618                      int bit, int unsigned_p, char *name)
03619 {
03620   struct type *t;
03621 
03622   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
03623   if (unsigned_p)
03624     TYPE_UNSIGNED (t) = 1;
03625 
03626   return t;
03627 }
03628 
03629 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
03630    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
03631    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
03632 
03633 struct type *
03634 arch_boolean_type (struct gdbarch *gdbarch,
03635                    int bit, int unsigned_p, char *name)
03636 {
03637   struct type *t;
03638 
03639   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
03640   if (unsigned_p)
03641     TYPE_UNSIGNED (t) = 1;
03642 
03643   return t;
03644 }
03645 
03646 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
03647    BIT is the type size in bits; if BIT equals -1, the size is
03648    determined by the floatformat.  NAME is the type name.  Set the
03649    TYPE_FLOATFORMAT from FLOATFORMATS.  */
03650 
03651 struct type *
03652 arch_float_type (struct gdbarch *gdbarch,
03653                  int bit, char *name, const struct floatformat **floatformats)
03654 {
03655   struct type *t;
03656 
03657   if (bit == -1)
03658     {
03659       gdb_assert (floatformats != NULL);
03660       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
03661       bit = floatformats[0]->totalsize;
03662     }
03663   gdb_assert (bit >= 0);
03664 
03665   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
03666   TYPE_FLOATFORMAT (t) = floatformats;
03667   return t;
03668 }
03669 
03670 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
03671    NAME is the type name.  TARGET_TYPE is the component float type.  */
03672 
03673 struct type *
03674 arch_complex_type (struct gdbarch *gdbarch,
03675                    char *name, struct type *target_type)
03676 {
03677   struct type *t;
03678 
03679   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
03680                  2 * TYPE_LENGTH (target_type), name);
03681   TYPE_TARGET_TYPE (t) = target_type;
03682   return t;
03683 }
03684 
03685 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
03686    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */
03687 
03688 struct type *
03689 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
03690 {
03691   int nfields = length * TARGET_CHAR_BIT;
03692   struct type *type;
03693 
03694   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
03695   TYPE_UNSIGNED (type) = 1;
03696   TYPE_NFIELDS (type) = nfields;
03697   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
03698 
03699   return type;
03700 }
03701 
03702 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
03703    position BITPOS is called NAME.  */
03704 
03705 void
03706 append_flags_type_flag (struct type *type, int bitpos, char *name)
03707 {
03708   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
03709   gdb_assert (bitpos < TYPE_NFIELDS (type));
03710   gdb_assert (bitpos >= 0);
03711 
03712   if (name)
03713     {
03714       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
03715       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
03716     }
03717   else
03718     {
03719       /* Don't show this field to the user.  */
03720       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
03721     }
03722 }
03723 
03724 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
03725    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
03726 
03727 struct type *
03728 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
03729 {
03730   struct type *t;
03731 
03732   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
03733   t = arch_type (gdbarch, code, 0, NULL);
03734   TYPE_TAG_NAME (t) = name;
03735   INIT_CPLUS_SPECIFIC (t);
03736   return t;
03737 }
03738 
03739 /* Add new field with name NAME and type FIELD to composite type T.
03740    Do not set the field's position or adjust the type's length;
03741    the caller should do so.  Return the new field.  */
03742 
03743 struct field *
03744 append_composite_type_field_raw (struct type *t, char *name,
03745                                  struct type *field)
03746 {
03747   struct field *f;
03748 
03749   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
03750   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
03751                               sizeof (struct field) * TYPE_NFIELDS (t));
03752   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
03753   memset (f, 0, sizeof f[0]);
03754   FIELD_TYPE (f[0]) = field;
03755   FIELD_NAME (f[0]) = name;
03756   return f;
03757 }
03758 
03759 /* Add new field with name NAME and type FIELD to composite type T.
03760    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
03761 
03762 void
03763 append_composite_type_field_aligned (struct type *t, char *name,
03764                                      struct type *field, int alignment)
03765 {
03766   struct field *f = append_composite_type_field_raw (t, name, field);
03767 
03768   if (TYPE_CODE (t) == TYPE_CODE_UNION)
03769     {
03770       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
03771         TYPE_LENGTH (t) = TYPE_LENGTH (field);
03772     }
03773   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
03774     {
03775       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
03776       if (TYPE_NFIELDS (t) > 1)
03777         {
03778           SET_FIELD_BITPOS (f[0],
03779                             (FIELD_BITPOS (f[-1])
03780                              + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
03781                                 * TARGET_CHAR_BIT)));
03782 
03783           if (alignment)
03784             {
03785               int left;
03786 
03787               alignment *= TARGET_CHAR_BIT;
03788               left = FIELD_BITPOS (f[0]) % alignment;
03789 
03790               if (left)
03791                 {
03792                   SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
03793                   TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
03794                 }
03795             }
03796         }
03797     }
03798 }
03799 
03800 /* Add new field with name NAME and type FIELD to composite type T.  */
03801 
03802 void
03803 append_composite_type_field (struct type *t, char *name,
03804                              struct type *field)
03805 {
03806   append_composite_type_field_aligned (t, name, field, 0);
03807 }
03808 
03809 static struct gdbarch_data *gdbtypes_data;
03810 
03811 const struct builtin_type *
03812 builtin_type (struct gdbarch *gdbarch)
03813 {
03814   return gdbarch_data (gdbarch, gdbtypes_data);
03815 }
03816 
03817 static void *
03818 gdbtypes_post_init (struct gdbarch *gdbarch)
03819 {
03820   struct builtin_type *builtin_type
03821     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
03822 
03823   /* Basic types.  */
03824   builtin_type->builtin_void
03825     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
03826   builtin_type->builtin_char
03827     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
03828                          !gdbarch_char_signed (gdbarch), "char");
03829   builtin_type->builtin_signed_char
03830     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
03831                          0, "signed char");
03832   builtin_type->builtin_unsigned_char
03833     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
03834                          1, "unsigned char");
03835   builtin_type->builtin_short
03836     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
03837                          0, "short");
03838   builtin_type->builtin_unsigned_short
03839     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
03840                          1, "unsigned short");
03841   builtin_type->builtin_int
03842     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
03843                          0, "int");
03844   builtin_type->builtin_unsigned_int
03845     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
03846                          1, "unsigned int");
03847   builtin_type->builtin_long
03848     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
03849                          0, "long");
03850   builtin_type->builtin_unsigned_long
03851     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
03852                          1, "unsigned long");
03853   builtin_type->builtin_long_long
03854     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
03855                          0, "long long");
03856   builtin_type->builtin_unsigned_long_long
03857     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
03858                          1, "unsigned long long");
03859   builtin_type->builtin_float
03860     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
03861                        "float", gdbarch_float_format (gdbarch));
03862   builtin_type->builtin_double
03863     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
03864                        "double", gdbarch_double_format (gdbarch));
03865   builtin_type->builtin_long_double
03866     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
03867                        "long double", gdbarch_long_double_format (gdbarch));
03868   builtin_type->builtin_complex
03869     = arch_complex_type (gdbarch, "complex",
03870                          builtin_type->builtin_float);
03871   builtin_type->builtin_double_complex
03872     = arch_complex_type (gdbarch, "double complex",
03873                          builtin_type->builtin_double);
03874   builtin_type->builtin_string
03875     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
03876   builtin_type->builtin_bool
03877     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
03878 
03879   /* The following three are about decimal floating point types, which
03880      are 32-bits, 64-bits and 128-bits respectively.  */
03881   builtin_type->builtin_decfloat
03882     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
03883   builtin_type->builtin_decdouble
03884     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
03885   builtin_type->builtin_declong
03886     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
03887 
03888   /* "True" character types.  */
03889   builtin_type->builtin_true_char
03890     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
03891   builtin_type->builtin_true_unsigned_char
03892     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
03893 
03894   /* Fixed-size integer types.  */
03895   builtin_type->builtin_int0
03896     = arch_integer_type (gdbarch, 0, 0, "int0_t");
03897   builtin_type->builtin_int8
03898     = arch_integer_type (gdbarch, 8, 0, "int8_t");
03899   builtin_type->builtin_uint8
03900     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
03901   builtin_type->builtin_int16
03902     = arch_integer_type (gdbarch, 16, 0, "int16_t");
03903   builtin_type->builtin_uint16
03904     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
03905   builtin_type->builtin_int32
03906     = arch_integer_type (gdbarch, 32, 0, "int32_t");
03907   builtin_type->builtin_uint32
03908     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
03909   builtin_type->builtin_int64
03910     = arch_integer_type (gdbarch, 64, 0, "int64_t");
03911   builtin_type->builtin_uint64
03912     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
03913   builtin_type->builtin_int128
03914     = arch_integer_type (gdbarch, 128, 0, "int128_t");
03915   builtin_type->builtin_uint128
03916     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
03917   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
03918     TYPE_INSTANCE_FLAG_NOTTEXT;
03919   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
03920     TYPE_INSTANCE_FLAG_NOTTEXT;
03921 
03922   /* Wide character types.  */
03923   builtin_type->builtin_char16
03924     = arch_integer_type (gdbarch, 16, 0, "char16_t");
03925   builtin_type->builtin_char32
03926     = arch_integer_type (gdbarch, 32, 0, "char32_t");
03927         
03928 
03929   /* Default data/code pointer types.  */
03930   builtin_type->builtin_data_ptr
03931     = lookup_pointer_type (builtin_type->builtin_void);
03932   builtin_type->builtin_func_ptr
03933     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
03934   builtin_type->builtin_func_func
03935     = lookup_function_type (builtin_type->builtin_func_ptr);
03936 
03937   /* This type represents a GDB internal function.  */
03938   builtin_type->internal_fn
03939     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
03940                  "<internal function>");
03941 
03942   return builtin_type;
03943 }
03944 
03945 /* This set of objfile-based types is intended to be used by symbol
03946    readers as basic types.  */
03947 
03948 static const struct objfile_data *objfile_type_data;
03949 
03950 const struct objfile_type *
03951 objfile_type (struct objfile *objfile)
03952 {
03953   struct gdbarch *gdbarch;
03954   struct objfile_type *objfile_type
03955     = objfile_data (objfile, objfile_type_data);
03956 
03957   if (objfile_type)
03958     return objfile_type;
03959 
03960   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
03961                                  1, struct objfile_type);
03962 
03963   /* Use the objfile architecture to determine basic type properties.  */
03964   gdbarch = get_objfile_arch (objfile);
03965 
03966   /* Basic types.  */
03967   objfile_type->builtin_void
03968     = init_type (TYPE_CODE_VOID, 1,
03969                  0,
03970                  "void", objfile);
03971 
03972   objfile_type->builtin_char
03973     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
03974                  (TYPE_FLAG_NOSIGN
03975                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
03976                  "char", objfile);
03977   objfile_type->builtin_signed_char
03978     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
03979                  0,
03980                  "signed char", objfile);
03981   objfile_type->builtin_unsigned_char
03982     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
03983                  TYPE_FLAG_UNSIGNED,
03984                  "unsigned char", objfile);
03985   objfile_type->builtin_short
03986     = init_type (TYPE_CODE_INT,
03987                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
03988                  0, "short", objfile);
03989   objfile_type->builtin_unsigned_short
03990     = init_type (TYPE_CODE_INT,
03991                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
03992                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
03993   objfile_type->builtin_int
03994     = init_type (TYPE_CODE_INT,
03995                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
03996                  0, "int", objfile);
03997   objfile_type->builtin_unsigned_int
03998     = init_type (TYPE_CODE_INT,
03999                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
04000                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
04001   objfile_type->builtin_long
04002     = init_type (TYPE_CODE_INT,
04003                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
04004                  0, "long", objfile);
04005   objfile_type->builtin_unsigned_long
04006     = init_type (TYPE_CODE_INT,
04007                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
04008                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
04009   objfile_type->builtin_long_long
04010     = init_type (TYPE_CODE_INT,
04011                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
04012                  0, "long long", objfile);
04013   objfile_type->builtin_unsigned_long_long
04014     = init_type (TYPE_CODE_INT,
04015                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
04016                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
04017 
04018   objfile_type->builtin_float
04019     = init_type (TYPE_CODE_FLT,
04020                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
04021                  0, "float", objfile);
04022   TYPE_FLOATFORMAT (objfile_type->builtin_float)
04023     = gdbarch_float_format (gdbarch);
04024   objfile_type->builtin_double
04025     = init_type (TYPE_CODE_FLT,
04026                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
04027                  0, "double", objfile);
04028   TYPE_FLOATFORMAT (objfile_type->builtin_double)
04029     = gdbarch_double_format (gdbarch);
04030   objfile_type->builtin_long_double
04031     = init_type (TYPE_CODE_FLT,
04032                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
04033                  0, "long double", objfile);
04034   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
04035     = gdbarch_long_double_format (gdbarch);
04036 
04037   /* This type represents a type that was unrecognized in symbol read-in.  */
04038   objfile_type->builtin_error
04039     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
04040 
04041   /* The following set of types is used for symbols with no
04042      debug information.  */
04043   objfile_type->nodebug_text_symbol
04044     = init_type (TYPE_CODE_FUNC, 1, 0,
04045                  "<text variable, no debug info>", objfile);
04046   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
04047     = objfile_type->builtin_int;
04048   objfile_type->nodebug_text_gnu_ifunc_symbol
04049     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
04050                  "<text gnu-indirect-function variable, no debug info>",
04051                  objfile);
04052   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
04053     = objfile_type->nodebug_text_symbol;
04054   objfile_type->nodebug_got_plt_symbol
04055     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
04056                  "<text from jump slot in .got.plt, no debug info>",
04057                  objfile);
04058   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
04059     = objfile_type->nodebug_text_symbol;
04060   objfile_type->nodebug_data_symbol
04061     = init_type (TYPE_CODE_INT,
04062                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
04063                  "<data variable, no debug info>", objfile);
04064   objfile_type->nodebug_unknown_symbol
04065     = init_type (TYPE_CODE_INT, 1, 0,
04066                  "<variable (not text or data), no debug info>", objfile);
04067   objfile_type->nodebug_tls_symbol
04068     = init_type (TYPE_CODE_INT,
04069                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
04070                  "<thread local variable, no debug info>", objfile);
04071 
04072   /* NOTE: on some targets, addresses and pointers are not necessarily
04073      the same.
04074 
04075      The upshot is:
04076      - gdb's `struct type' always describes the target's
04077        representation.
04078      - gdb's `struct value' objects should always hold values in
04079        target form.
04080      - gdb's CORE_ADDR values are addresses in the unified virtual
04081        address space that the assembler and linker work with.  Thus,
04082        since target_read_memory takes a CORE_ADDR as an argument, it
04083        can access any memory on the target, even if the processor has
04084        separate code and data address spaces.
04085 
04086      In this context, objfile_type->builtin_core_addr is a bit odd:
04087      it's a target type for a value the target will never see.  It's
04088      only used to hold the values of (typeless) linker symbols, which
04089      are indeed in the unified virtual address space.  */
04090 
04091   objfile_type->builtin_core_addr
04092     = init_type (TYPE_CODE_INT,
04093                  gdbarch_addr_bit (gdbarch) / 8,
04094                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
04095 
04096   set_objfile_data (objfile, objfile_type_data, objfile_type);
04097   return objfile_type;
04098 }
04099 
04100 extern initialize_file_ftype _initialize_gdbtypes;
04101 
04102 void
04103 _initialize_gdbtypes (void)
04104 {
04105   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
04106   objfile_type_data = register_objfile_data ();
04107 
04108   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
04109                              _("Set debugging of C++ overloading."),
04110                              _("Show debugging of C++ overloading."),
04111                              _("When enabled, ranking of the "
04112                                "functions is displayed."),
04113                              NULL,
04114                              show_overload_debug,
04115                              &setdebuglist, &showdebuglist);
04116 
04117   /* Add user knob for controlling resolution of opaque types.  */
04118   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
04119                            &opaque_type_resolution,
04120                            _("Set resolution of opaque struct/class/union"
04121                              " types (if set before loading symbols)."),
04122                            _("Show resolution of opaque struct/class/union"
04123                              " types (if set before loading symbols)."),
04124                            NULL, NULL,
04125                            show_opaque_type_resolution,
04126                            &setlist, &showlist);
04127 
04128   /* Add an option to permit non-strict type checking.  */
04129   add_setshow_boolean_cmd ("type", class_support,
04130                            &strict_type_checking,
04131                            _("Set strict type checking."),
04132                            _("Show strict type checking."),
04133                            NULL, NULL,
04134                            show_strict_type_checking,
04135                            &setchecklist, &showchecklist);
04136 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines