xref: /dragonfly/contrib/gdb-7/gdb/gdbtypes.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Support routines for manipulating internal types for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1992-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert #include "bfd.h"
255796c8dcSSimon Schubert #include "symtab.h"
265796c8dcSSimon Schubert #include "symfile.h"
275796c8dcSSimon Schubert #include "objfiles.h"
285796c8dcSSimon Schubert #include "gdbtypes.h"
295796c8dcSSimon Schubert #include "expression.h"
305796c8dcSSimon Schubert #include "language.h"
315796c8dcSSimon Schubert #include "target.h"
325796c8dcSSimon Schubert #include "value.h"
335796c8dcSSimon Schubert #include "demangle.h"
345796c8dcSSimon Schubert #include "complaints.h"
355796c8dcSSimon Schubert #include "gdbcmd.h"
365796c8dcSSimon Schubert #include "cp-abi.h"
375796c8dcSSimon Schubert #include "gdb_assert.h"
385796c8dcSSimon Schubert #include "hashtab.h"
39*ef5ccd6cSJohn Marino #include "exceptions.h"
405796c8dcSSimon Schubert 
41c50c785cSJohn Marino /* Initialize BADNESS constants.  */
42c50c785cSJohn Marino 
43c50c785cSJohn Marino const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
44c50c785cSJohn Marino 
45c50c785cSJohn Marino const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
46c50c785cSJohn Marino const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
47c50c785cSJohn Marino 
48c50c785cSJohn Marino const struct rank EXACT_MATCH_BADNESS = {0,0};
49c50c785cSJohn Marino 
50c50c785cSJohn Marino const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
51c50c785cSJohn Marino const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
52c50c785cSJohn Marino const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
53c50c785cSJohn Marino const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
54c50c785cSJohn Marino const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
55c50c785cSJohn Marino const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
56c50c785cSJohn Marino const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
57*ef5ccd6cSJohn Marino const struct rank BOOL_CONVERSION_BADNESS = {3,0};
58c50c785cSJohn Marino const struct rank BASE_CONVERSION_BADNESS = {2,0};
59c50c785cSJohn Marino const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
60a45ae5f8SJohn Marino const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
61c50c785cSJohn Marino const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
62*ef5ccd6cSJohn Marino const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
63c50c785cSJohn Marino 
645796c8dcSSimon Schubert /* Floatformat pairs.  */
65cf7f2e2dSJohn Marino const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
66cf7f2e2dSJohn Marino   &floatformat_ieee_half_big,
67cf7f2e2dSJohn Marino   &floatformat_ieee_half_little
68cf7f2e2dSJohn Marino };
695796c8dcSSimon Schubert const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
705796c8dcSSimon Schubert   &floatformat_ieee_single_big,
715796c8dcSSimon Schubert   &floatformat_ieee_single_little
725796c8dcSSimon Schubert };
735796c8dcSSimon Schubert const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
745796c8dcSSimon Schubert   &floatformat_ieee_double_big,
755796c8dcSSimon Schubert   &floatformat_ieee_double_little
765796c8dcSSimon Schubert };
775796c8dcSSimon Schubert const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
785796c8dcSSimon Schubert   &floatformat_ieee_double_big,
795796c8dcSSimon Schubert   &floatformat_ieee_double_littlebyte_bigword
805796c8dcSSimon Schubert };
815796c8dcSSimon Schubert const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
825796c8dcSSimon Schubert   &floatformat_i387_ext,
835796c8dcSSimon Schubert   &floatformat_i387_ext
845796c8dcSSimon Schubert };
855796c8dcSSimon Schubert const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
865796c8dcSSimon Schubert   &floatformat_m68881_ext,
875796c8dcSSimon Schubert   &floatformat_m68881_ext
885796c8dcSSimon Schubert };
895796c8dcSSimon Schubert const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
905796c8dcSSimon Schubert   &floatformat_arm_ext_big,
915796c8dcSSimon Schubert   &floatformat_arm_ext_littlebyte_bigword
925796c8dcSSimon Schubert };
935796c8dcSSimon Schubert const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
945796c8dcSSimon Schubert   &floatformat_ia64_spill_big,
955796c8dcSSimon Schubert   &floatformat_ia64_spill_little
965796c8dcSSimon Schubert };
975796c8dcSSimon Schubert const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
985796c8dcSSimon Schubert   &floatformat_ia64_quad_big,
995796c8dcSSimon Schubert   &floatformat_ia64_quad_little
1005796c8dcSSimon Schubert };
1015796c8dcSSimon Schubert const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
1025796c8dcSSimon Schubert   &floatformat_vax_f,
1035796c8dcSSimon Schubert   &floatformat_vax_f
1045796c8dcSSimon Schubert };
1055796c8dcSSimon Schubert const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
1065796c8dcSSimon Schubert   &floatformat_vax_d,
1075796c8dcSSimon Schubert   &floatformat_vax_d
1085796c8dcSSimon Schubert };
1095796c8dcSSimon Schubert const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
1105796c8dcSSimon Schubert   &floatformat_ibm_long_double,
1115796c8dcSSimon Schubert   &floatformat_ibm_long_double
1125796c8dcSSimon Schubert };
1135796c8dcSSimon Schubert 
114*ef5ccd6cSJohn Marino /* Should opaque types be resolved?  */
1155796c8dcSSimon Schubert 
116*ef5ccd6cSJohn Marino static int opaque_type_resolution = 1;
117*ef5ccd6cSJohn Marino 
118*ef5ccd6cSJohn Marino /* A flag to enable printing of debugging information of C++
119*ef5ccd6cSJohn Marino    overloading.  */
120*ef5ccd6cSJohn Marino 
121*ef5ccd6cSJohn Marino unsigned int overload_debug = 0;
122*ef5ccd6cSJohn Marino 
123*ef5ccd6cSJohn Marino /* A flag to enable strict type checking.  */
124*ef5ccd6cSJohn Marino 
125*ef5ccd6cSJohn Marino static int strict_type_checking = 1;
126*ef5ccd6cSJohn Marino 
127*ef5ccd6cSJohn Marino /* A function to show whether opaque types are resolved.  */
128*ef5ccd6cSJohn Marino 
1295796c8dcSSimon Schubert static void
show_opaque_type_resolution(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1305796c8dcSSimon Schubert show_opaque_type_resolution (struct ui_file *file, int from_tty,
1315796c8dcSSimon Schubert 			     struct cmd_list_element *c,
1325796c8dcSSimon Schubert 			     const char *value)
1335796c8dcSSimon Schubert {
134c50c785cSJohn Marino   fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
135c50c785cSJohn Marino 			    "(if set before loading symbols) is %s.\n"),
1365796c8dcSSimon Schubert 		    value);
1375796c8dcSSimon Schubert }
1385796c8dcSSimon Schubert 
139*ef5ccd6cSJohn Marino /* A function to show whether C++ overload debugging is enabled.  */
140*ef5ccd6cSJohn Marino 
1415796c8dcSSimon Schubert static void
show_overload_debug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1425796c8dcSSimon Schubert show_overload_debug (struct ui_file *file, int from_tty,
1435796c8dcSSimon Schubert 		     struct cmd_list_element *c, const char *value)
1445796c8dcSSimon Schubert {
1455796c8dcSSimon Schubert   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
1465796c8dcSSimon Schubert 		    value);
1475796c8dcSSimon Schubert }
1485796c8dcSSimon Schubert 
149*ef5ccd6cSJohn Marino /* A function to show the status of strict type checking.  */
150*ef5ccd6cSJohn Marino 
151*ef5ccd6cSJohn Marino static void
show_strict_type_checking(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)152*ef5ccd6cSJohn Marino show_strict_type_checking (struct ui_file *file, int from_tty,
153*ef5ccd6cSJohn Marino 			   struct cmd_list_element *c, const char *value)
1545796c8dcSSimon Schubert {
155*ef5ccd6cSJohn Marino   fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
156*ef5ccd6cSJohn Marino }
1575796c8dcSSimon Schubert 
158*ef5ccd6cSJohn Marino 
1595796c8dcSSimon Schubert /* Allocate a new OBJFILE-associated type structure and fill it
1605796c8dcSSimon Schubert    with some defaults.  Space for the type structure is allocated
1615796c8dcSSimon Schubert    on the objfile's objfile_obstack.  */
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert struct type *
alloc_type(struct objfile * objfile)1645796c8dcSSimon Schubert alloc_type (struct objfile *objfile)
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert   struct type *type;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   gdb_assert (objfile != NULL);
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert   /* Alloc the structure and start off with all fields zeroed.  */
1715796c8dcSSimon Schubert   type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
1725796c8dcSSimon Schubert   TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1735796c8dcSSimon Schubert 					  struct main_type);
1745796c8dcSSimon Schubert   OBJSTAT (objfile, n_types++);
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert   TYPE_OBJFILE_OWNED (type) = 1;
1775796c8dcSSimon Schubert   TYPE_OWNER (type).objfile = objfile;
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert   /* Initialize the fields that might not be zero.  */
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_UNDEF;
1825796c8dcSSimon Schubert   TYPE_VPTR_FIELDNO (type) = -1;
1835796c8dcSSimon Schubert   TYPE_CHAIN (type) = type;	/* Chain back to itself.  */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert   return type;
1865796c8dcSSimon Schubert }
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert /* Allocate a new GDBARCH-associated type structure and fill it
1895796c8dcSSimon Schubert    with some defaults.  Space for the type structure is allocated
1905796c8dcSSimon Schubert    on the heap.  */
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert struct type *
alloc_type_arch(struct gdbarch * gdbarch)1935796c8dcSSimon Schubert alloc_type_arch (struct gdbarch *gdbarch)
1945796c8dcSSimon Schubert {
1955796c8dcSSimon Schubert   struct type *type;
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert   gdb_assert (gdbarch != NULL);
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert   /* Alloc the structure and start off with all fields zeroed.  */
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert   type = XZALLOC (struct type);
2025796c8dcSSimon Schubert   TYPE_MAIN_TYPE (type) = XZALLOC (struct main_type);
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert   TYPE_OBJFILE_OWNED (type) = 0;
2055796c8dcSSimon Schubert   TYPE_OWNER (type).gdbarch = gdbarch;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* Initialize the fields that might not be zero.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_UNDEF;
2105796c8dcSSimon Schubert   TYPE_VPTR_FIELDNO (type) = -1;
2115796c8dcSSimon Schubert   TYPE_CHAIN (type) = type;	/* Chain back to itself.  */
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert   return type;
2145796c8dcSSimon Schubert }
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert /* If TYPE is objfile-associated, allocate a new type structure
2175796c8dcSSimon Schubert    associated with the same objfile.  If TYPE is gdbarch-associated,
2185796c8dcSSimon Schubert    allocate a new type structure associated with the same gdbarch.  */
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert struct type *
alloc_type_copy(const struct type * type)2215796c8dcSSimon Schubert alloc_type_copy (const struct type *type)
2225796c8dcSSimon Schubert {
2235796c8dcSSimon Schubert   if (TYPE_OBJFILE_OWNED (type))
2245796c8dcSSimon Schubert     return alloc_type (TYPE_OWNER (type).objfile);
2255796c8dcSSimon Schubert   else
2265796c8dcSSimon Schubert     return alloc_type_arch (TYPE_OWNER (type).gdbarch);
2275796c8dcSSimon Schubert }
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert /* If TYPE is gdbarch-associated, return that architecture.
2305796c8dcSSimon Schubert    If TYPE is objfile-associated, return that objfile's architecture.  */
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert struct gdbarch *
get_type_arch(const struct type * type)2335796c8dcSSimon Schubert get_type_arch (const struct type *type)
2345796c8dcSSimon Schubert {
2355796c8dcSSimon Schubert   if (TYPE_OBJFILE_OWNED (type))
2365796c8dcSSimon Schubert     return get_objfile_arch (TYPE_OWNER (type).objfile);
2375796c8dcSSimon Schubert   else
2385796c8dcSSimon Schubert     return TYPE_OWNER (type).gdbarch;
2395796c8dcSSimon Schubert }
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert /* Alloc a new type instance structure, fill it with some defaults,
2425796c8dcSSimon Schubert    and point it at OLDTYPE.  Allocate the new type instance from the
2435796c8dcSSimon Schubert    same place as OLDTYPE.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert static struct type *
alloc_type_instance(struct type * oldtype)2465796c8dcSSimon Schubert alloc_type_instance (struct type *oldtype)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert   struct type *type;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert   /* Allocate the structure.  */
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   if (! TYPE_OBJFILE_OWNED (oldtype))
2535796c8dcSSimon Schubert     type = XZALLOC (struct type);
2545796c8dcSSimon Schubert   else
2555796c8dcSSimon Schubert     type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
2565796c8dcSSimon Schubert 			   struct type);
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert   TYPE_CHAIN (type) = type;	/* Chain back to itself for now.  */
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert   return type;
2635796c8dcSSimon Schubert }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert /* Clear all remnants of the previous type at TYPE, in preparation for
2665796c8dcSSimon Schubert    replacing it with something else.  Preserve owner information.  */
267*ef5ccd6cSJohn Marino 
2685796c8dcSSimon Schubert static void
smash_type(struct type * type)2695796c8dcSSimon Schubert smash_type (struct type *type)
2705796c8dcSSimon Schubert {
2715796c8dcSSimon Schubert   int objfile_owned = TYPE_OBJFILE_OWNED (type);
2725796c8dcSSimon Schubert   union type_owner owner = TYPE_OWNER (type);
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert   /* Restore owner information.  */
2775796c8dcSSimon Schubert   TYPE_OBJFILE_OWNED (type) = objfile_owned;
2785796c8dcSSimon Schubert   TYPE_OWNER (type) = owner;
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert   /* For now, delete the rings.  */
2815796c8dcSSimon Schubert   TYPE_CHAIN (type) = type;
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert   /* For now, leave the pointer/reference types alone.  */
2845796c8dcSSimon Schubert }
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
2875796c8dcSSimon Schubert    to a pointer to memory where the pointer type should be stored.
2885796c8dcSSimon Schubert    If *TYPEPTR is zero, update it to point to the pointer type we return.
2895796c8dcSSimon Schubert    We allocate new memory if needed.  */
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert struct type *
make_pointer_type(struct type * type,struct type ** typeptr)2925796c8dcSSimon Schubert make_pointer_type (struct type *type, struct type **typeptr)
2935796c8dcSSimon Schubert {
2945796c8dcSSimon Schubert   struct type *ntype;	/* New type */
2955796c8dcSSimon Schubert   struct type *chain;
2965796c8dcSSimon Schubert 
2975796c8dcSSimon Schubert   ntype = TYPE_POINTER_TYPE (type);
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert   if (ntype)
3005796c8dcSSimon Schubert     {
3015796c8dcSSimon Schubert       if (typeptr == 0)
3025796c8dcSSimon Schubert 	return ntype;		/* Don't care about alloc,
3035796c8dcSSimon Schubert 				   and have new type.  */
3045796c8dcSSimon Schubert       else if (*typeptr == 0)
3055796c8dcSSimon Schubert 	{
3065796c8dcSSimon Schubert 	  *typeptr = ntype;	/* Tracking alloc, and have new type.  */
3075796c8dcSSimon Schubert 	  return ntype;
3085796c8dcSSimon Schubert 	}
3095796c8dcSSimon Schubert     }
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert   if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
3125796c8dcSSimon Schubert     {
3135796c8dcSSimon Schubert       ntype = alloc_type_copy (type);
3145796c8dcSSimon Schubert       if (typeptr)
3155796c8dcSSimon Schubert 	*typeptr = ntype;
3165796c8dcSSimon Schubert     }
3175796c8dcSSimon Schubert   else			/* We have storage, but need to reset it.  */
3185796c8dcSSimon Schubert     {
3195796c8dcSSimon Schubert       ntype = *typeptr;
3205796c8dcSSimon Schubert       chain = TYPE_CHAIN (ntype);
3215796c8dcSSimon Schubert       smash_type (ntype);
3225796c8dcSSimon Schubert       TYPE_CHAIN (ntype) = chain;
3235796c8dcSSimon Schubert     }
3245796c8dcSSimon Schubert 
3255796c8dcSSimon Schubert   TYPE_TARGET_TYPE (ntype) = type;
3265796c8dcSSimon Schubert   TYPE_POINTER_TYPE (type) = ntype;
3275796c8dcSSimon Schubert 
328*ef5ccd6cSJohn Marino   /* FIXME!  Assumes the machine has only one representation for pointers!  */
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   TYPE_LENGTH (ntype)
3315796c8dcSSimon Schubert     = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
3325796c8dcSSimon Schubert   TYPE_CODE (ntype) = TYPE_CODE_PTR;
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert   /* Mark pointers as unsigned.  The target converts between pointers
3355796c8dcSSimon Schubert      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
3365796c8dcSSimon Schubert      gdbarch_address_to_pointer.  */
3375796c8dcSSimon Schubert   TYPE_UNSIGNED (ntype) = 1;
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert   /* Update the length of all the other variants of this type.  */
3405796c8dcSSimon Schubert   chain = TYPE_CHAIN (ntype);
3415796c8dcSSimon Schubert   while (chain != ntype)
3425796c8dcSSimon Schubert     {
3435796c8dcSSimon Schubert       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
3445796c8dcSSimon Schubert       chain = TYPE_CHAIN (chain);
3455796c8dcSSimon Schubert     }
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert   return ntype;
3485796c8dcSSimon Schubert }
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert /* Given a type TYPE, return a type of pointers to that type.
3515796c8dcSSimon Schubert    May need to construct such a type if this is the first use.  */
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert struct type *
lookup_pointer_type(struct type * type)3545796c8dcSSimon Schubert lookup_pointer_type (struct type *type)
3555796c8dcSSimon Schubert {
3565796c8dcSSimon Schubert   return make_pointer_type (type, (struct type **) 0);
3575796c8dcSSimon Schubert }
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero,
3605796c8dcSSimon Schubert    points to a pointer to memory where the reference type should be
3615796c8dcSSimon Schubert    stored.  If *TYPEPTR is zero, update it to point to the reference
3625796c8dcSSimon Schubert    type we return.  We allocate new memory if needed.  */
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert struct type *
make_reference_type(struct type * type,struct type ** typeptr)3655796c8dcSSimon Schubert make_reference_type (struct type *type, struct type **typeptr)
3665796c8dcSSimon Schubert {
3675796c8dcSSimon Schubert   struct type *ntype;	/* New type */
3685796c8dcSSimon Schubert   struct type *chain;
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   ntype = TYPE_REFERENCE_TYPE (type);
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert   if (ntype)
3735796c8dcSSimon Schubert     {
3745796c8dcSSimon Schubert       if (typeptr == 0)
3755796c8dcSSimon Schubert 	return ntype;		/* Don't care about alloc,
3765796c8dcSSimon Schubert 				   and have new type.  */
3775796c8dcSSimon Schubert       else if (*typeptr == 0)
3785796c8dcSSimon Schubert 	{
3795796c8dcSSimon Schubert 	  *typeptr = ntype;	/* Tracking alloc, and have new type.  */
3805796c8dcSSimon Schubert 	  return ntype;
3815796c8dcSSimon Schubert 	}
3825796c8dcSSimon Schubert     }
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert   if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
3855796c8dcSSimon Schubert     {
3865796c8dcSSimon Schubert       ntype = alloc_type_copy (type);
3875796c8dcSSimon Schubert       if (typeptr)
3885796c8dcSSimon Schubert 	*typeptr = ntype;
3895796c8dcSSimon Schubert     }
3905796c8dcSSimon Schubert   else			/* We have storage, but need to reset it.  */
3915796c8dcSSimon Schubert     {
3925796c8dcSSimon Schubert       ntype = *typeptr;
3935796c8dcSSimon Schubert       chain = TYPE_CHAIN (ntype);
3945796c8dcSSimon Schubert       smash_type (ntype);
3955796c8dcSSimon Schubert       TYPE_CHAIN (ntype) = chain;
3965796c8dcSSimon Schubert     }
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert   TYPE_TARGET_TYPE (ntype) = type;
3995796c8dcSSimon Schubert   TYPE_REFERENCE_TYPE (type) = ntype;
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert   /* FIXME!  Assume the machine has only one representation for
4025796c8dcSSimon Schubert      references, and that it matches the (only) representation for
4035796c8dcSSimon Schubert      pointers!  */
4045796c8dcSSimon Schubert 
4055796c8dcSSimon Schubert   TYPE_LENGTH (ntype) =
4065796c8dcSSimon Schubert     gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
4075796c8dcSSimon Schubert   TYPE_CODE (ntype) = TYPE_CODE_REF;
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert   if (!TYPE_REFERENCE_TYPE (type))	/* Remember it, if don't have one.  */
4105796c8dcSSimon Schubert     TYPE_REFERENCE_TYPE (type) = ntype;
4115796c8dcSSimon Schubert 
4125796c8dcSSimon Schubert   /* Update the length of all the other variants of this type.  */
4135796c8dcSSimon Schubert   chain = TYPE_CHAIN (ntype);
4145796c8dcSSimon Schubert   while (chain != ntype)
4155796c8dcSSimon Schubert     {
4165796c8dcSSimon Schubert       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
4175796c8dcSSimon Schubert       chain = TYPE_CHAIN (chain);
4185796c8dcSSimon Schubert     }
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   return ntype;
4215796c8dcSSimon Schubert }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert /* Same as above, but caller doesn't care about memory allocation
4245796c8dcSSimon Schubert    details.  */
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert struct type *
lookup_reference_type(struct type * type)4275796c8dcSSimon Schubert lookup_reference_type (struct type *type)
4285796c8dcSSimon Schubert {
4295796c8dcSSimon Schubert   return make_reference_type (type, (struct type **) 0);
4305796c8dcSSimon Schubert }
4315796c8dcSSimon Schubert 
4325796c8dcSSimon Schubert /* Lookup a function type that returns type TYPE.  TYPEPTR, if
4335796c8dcSSimon Schubert    nonzero, points to a pointer to memory where the function type
4345796c8dcSSimon Schubert    should be stored.  If *TYPEPTR is zero, update it to point to the
4355796c8dcSSimon Schubert    function type we return.  We allocate new memory if needed.  */
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert struct type *
make_function_type(struct type * type,struct type ** typeptr)4385796c8dcSSimon Schubert make_function_type (struct type *type, struct type **typeptr)
4395796c8dcSSimon Schubert {
4405796c8dcSSimon Schubert   struct type *ntype;	/* New type */
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert   if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
4435796c8dcSSimon Schubert     {
4445796c8dcSSimon Schubert       ntype = alloc_type_copy (type);
4455796c8dcSSimon Schubert       if (typeptr)
4465796c8dcSSimon Schubert 	*typeptr = ntype;
4475796c8dcSSimon Schubert     }
4485796c8dcSSimon Schubert   else			/* We have storage, but need to reset it.  */
4495796c8dcSSimon Schubert     {
4505796c8dcSSimon Schubert       ntype = *typeptr;
4515796c8dcSSimon Schubert       smash_type (ntype);
4525796c8dcSSimon Schubert     }
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert   TYPE_TARGET_TYPE (ntype) = type;
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   TYPE_LENGTH (ntype) = 1;
4575796c8dcSSimon Schubert   TYPE_CODE (ntype) = TYPE_CODE_FUNC;
4585796c8dcSSimon Schubert 
459a45ae5f8SJohn Marino   INIT_FUNC_SPECIFIC (ntype);
460a45ae5f8SJohn Marino 
4615796c8dcSSimon Schubert   return ntype;
4625796c8dcSSimon Schubert }
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert /* Given a type TYPE, return a type of functions that return that type.
4655796c8dcSSimon Schubert    May need to construct such a type if this is the first use.  */
4665796c8dcSSimon Schubert 
4675796c8dcSSimon Schubert struct type *
lookup_function_type(struct type * type)4685796c8dcSSimon Schubert lookup_function_type (struct type *type)
4695796c8dcSSimon Schubert {
4705796c8dcSSimon Schubert   return make_function_type (type, (struct type **) 0);
4715796c8dcSSimon Schubert }
4725796c8dcSSimon Schubert 
473*ef5ccd6cSJohn Marino /* Given a type TYPE and argument types, return the appropriate
474*ef5ccd6cSJohn Marino    function type.  If the final type in PARAM_TYPES is NULL, make a
475*ef5ccd6cSJohn Marino    varargs function.  */
476*ef5ccd6cSJohn Marino 
477*ef5ccd6cSJohn Marino struct type *
lookup_function_type_with_arguments(struct type * type,int nparams,struct type ** param_types)478*ef5ccd6cSJohn Marino lookup_function_type_with_arguments (struct type *type,
479*ef5ccd6cSJohn Marino 				     int nparams,
480*ef5ccd6cSJohn Marino 				     struct type **param_types)
481*ef5ccd6cSJohn Marino {
482*ef5ccd6cSJohn Marino   struct type *fn = make_function_type (type, (struct type **) 0);
483*ef5ccd6cSJohn Marino   int i;
484*ef5ccd6cSJohn Marino 
485*ef5ccd6cSJohn Marino   if (nparams > 0)
486*ef5ccd6cSJohn Marino     {
487*ef5ccd6cSJohn Marino       if (param_types[nparams - 1] == NULL)
488*ef5ccd6cSJohn Marino 	{
489*ef5ccd6cSJohn Marino 	  --nparams;
490*ef5ccd6cSJohn Marino 	  TYPE_VARARGS (fn) = 1;
491*ef5ccd6cSJohn Marino 	}
492*ef5ccd6cSJohn Marino       else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
493*ef5ccd6cSJohn Marino 	       == TYPE_CODE_VOID)
494*ef5ccd6cSJohn Marino 	{
495*ef5ccd6cSJohn Marino 	  --nparams;
496*ef5ccd6cSJohn Marino 	  /* Caller should have ensured this.  */
497*ef5ccd6cSJohn Marino 	  gdb_assert (nparams == 0);
498*ef5ccd6cSJohn Marino 	  TYPE_PROTOTYPED (fn) = 1;
499*ef5ccd6cSJohn Marino 	}
500*ef5ccd6cSJohn Marino     }
501*ef5ccd6cSJohn Marino 
502*ef5ccd6cSJohn Marino   TYPE_NFIELDS (fn) = nparams;
503*ef5ccd6cSJohn Marino   TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
504*ef5ccd6cSJohn Marino   for (i = 0; i < nparams; ++i)
505*ef5ccd6cSJohn Marino     TYPE_FIELD_TYPE (fn, i) = param_types[i];
506*ef5ccd6cSJohn Marino 
507*ef5ccd6cSJohn Marino   return fn;
508*ef5ccd6cSJohn Marino }
509*ef5ccd6cSJohn Marino 
5105796c8dcSSimon Schubert /* Identify address space identifier by name --
5115796c8dcSSimon Schubert    return the integer flag defined in gdbtypes.h.  */
512*ef5ccd6cSJohn Marino 
513*ef5ccd6cSJohn Marino int
address_space_name_to_int(struct gdbarch * gdbarch,char * space_identifier)5145796c8dcSSimon Schubert address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
5155796c8dcSSimon Schubert {
5165796c8dcSSimon Schubert   int type_flags;
517cf7f2e2dSJohn Marino 
5185796c8dcSSimon Schubert   /* Check for known address space delimiters.  */
5195796c8dcSSimon Schubert   if (!strcmp (space_identifier, "code"))
5205796c8dcSSimon Schubert     return TYPE_INSTANCE_FLAG_CODE_SPACE;
5215796c8dcSSimon Schubert   else if (!strcmp (space_identifier, "data"))
5225796c8dcSSimon Schubert     return TYPE_INSTANCE_FLAG_DATA_SPACE;
5235796c8dcSSimon Schubert   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
5245796c8dcSSimon Schubert            && gdbarch_address_class_name_to_type_flags (gdbarch,
5255796c8dcSSimon Schubert 							space_identifier,
5265796c8dcSSimon Schubert 							&type_flags))
5275796c8dcSSimon Schubert     return type_flags;
5285796c8dcSSimon Schubert   else
5295796c8dcSSimon Schubert     error (_("Unknown address space specifier: \"%s\""), space_identifier);
5305796c8dcSSimon Schubert }
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert /* Identify address space identifier by integer flag as defined in
5335796c8dcSSimon Schubert    gdbtypes.h -- return the string version of the adress space name.  */
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert const char *
address_space_int_to_name(struct gdbarch * gdbarch,int space_flag)5365796c8dcSSimon Schubert address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
5375796c8dcSSimon Schubert {
5385796c8dcSSimon Schubert   if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
5395796c8dcSSimon Schubert     return "code";
5405796c8dcSSimon Schubert   else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
5415796c8dcSSimon Schubert     return "data";
5425796c8dcSSimon Schubert   else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5435796c8dcSSimon Schubert            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
5445796c8dcSSimon Schubert     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
5455796c8dcSSimon Schubert   else
5465796c8dcSSimon Schubert     return NULL;
5475796c8dcSSimon Schubert }
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert    If STORAGE is non-NULL, create the new type instance there.
5525796c8dcSSimon Schubert    STORAGE must be in the same obstack as TYPE.  */
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert static struct type *
make_qualified_type(struct type * type,int new_flags,struct type * storage)5555796c8dcSSimon Schubert make_qualified_type (struct type *type, int new_flags,
5565796c8dcSSimon Schubert 		     struct type *storage)
5575796c8dcSSimon Schubert {
5585796c8dcSSimon Schubert   struct type *ntype;
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   ntype = type;
5615796c8dcSSimon Schubert   do
5625796c8dcSSimon Schubert     {
5635796c8dcSSimon Schubert       if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
5645796c8dcSSimon Schubert 	return ntype;
5655796c8dcSSimon Schubert       ntype = TYPE_CHAIN (ntype);
5665796c8dcSSimon Schubert     }
5675796c8dcSSimon Schubert   while (ntype != type);
5685796c8dcSSimon Schubert 
5695796c8dcSSimon Schubert   /* Create a new type instance.  */
5705796c8dcSSimon Schubert   if (storage == NULL)
5715796c8dcSSimon Schubert     ntype = alloc_type_instance (type);
5725796c8dcSSimon Schubert   else
5735796c8dcSSimon Schubert     {
5745796c8dcSSimon Schubert       /* If STORAGE was provided, it had better be in the same objfile
5755796c8dcSSimon Schubert 	 as TYPE.  Otherwise, we can't link it into TYPE's cv chain:
5765796c8dcSSimon Schubert 	 if one objfile is freed and the other kept, we'd have
5775796c8dcSSimon Schubert 	 dangling pointers.  */
5785796c8dcSSimon Schubert       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert       ntype = storage;
5815796c8dcSSimon Schubert       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
5825796c8dcSSimon Schubert       TYPE_CHAIN (ntype) = ntype;
5835796c8dcSSimon Schubert     }
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert   /* Pointers or references to the original type are not relevant to
5865796c8dcSSimon Schubert      the new type.  */
5875796c8dcSSimon Schubert   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
5885796c8dcSSimon Schubert   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   /* Chain the new qualified type to the old type.  */
5915796c8dcSSimon Schubert   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
5925796c8dcSSimon Schubert   TYPE_CHAIN (type) = ntype;
5935796c8dcSSimon Schubert 
5945796c8dcSSimon Schubert   /* Now set the instance flags and return the new type.  */
5955796c8dcSSimon Schubert   TYPE_INSTANCE_FLAGS (ntype) = new_flags;
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert   /* Set length of new type to that of the original type.  */
5985796c8dcSSimon Schubert   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert   return ntype;
6015796c8dcSSimon Schubert }
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert /* Make an address-space-delimited variant of a type -- a type that
6045796c8dcSSimon Schubert    is identical to the one supplied except that it has an address
6055796c8dcSSimon Schubert    space attribute attached to it (such as "code" or "data").
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert    The space attributes "code" and "data" are for Harvard
6085796c8dcSSimon Schubert    architectures.  The address space attributes are for architectures
6095796c8dcSSimon Schubert    which have alternately sized pointers or pointers with alternate
6105796c8dcSSimon Schubert    representations.  */
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert struct type *
make_type_with_address_space(struct type * type,int space_flag)6135796c8dcSSimon Schubert make_type_with_address_space (struct type *type, int space_flag)
6145796c8dcSSimon Schubert {
6155796c8dcSSimon Schubert   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
6165796c8dcSSimon Schubert 		    & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
6175796c8dcSSimon Schubert 			| TYPE_INSTANCE_FLAG_DATA_SPACE
6185796c8dcSSimon Schubert 		        | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
6195796c8dcSSimon Schubert 		   | space_flag);
6205796c8dcSSimon Schubert 
6215796c8dcSSimon Schubert   return make_qualified_type (type, new_flags, NULL);
6225796c8dcSSimon Schubert }
6235796c8dcSSimon Schubert 
6245796c8dcSSimon Schubert /* Make a "c-v" variant of a type -- a type that is identical to the
6255796c8dcSSimon Schubert    one supplied except that it may have const or volatile attributes
6265796c8dcSSimon Schubert    CNST is a flag for setting the const attribute
6275796c8dcSSimon Schubert    VOLTL is a flag for setting the volatile attribute
6285796c8dcSSimon Schubert    TYPE is the base type whose variant we are creating.
6295796c8dcSSimon Schubert 
6305796c8dcSSimon Schubert    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
6315796c8dcSSimon Schubert    storage to hold the new qualified type; *TYPEPTR and TYPE must be
6325796c8dcSSimon Schubert    in the same objfile.  Otherwise, allocate fresh memory for the new
6335796c8dcSSimon Schubert    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
6345796c8dcSSimon Schubert    new type we construct.  */
635*ef5ccd6cSJohn Marino 
6365796c8dcSSimon Schubert struct type *
make_cv_type(int cnst,int voltl,struct type * type,struct type ** typeptr)6375796c8dcSSimon Schubert make_cv_type (int cnst, int voltl,
6385796c8dcSSimon Schubert 	      struct type *type,
6395796c8dcSSimon Schubert 	      struct type **typeptr)
6405796c8dcSSimon Schubert {
6415796c8dcSSimon Schubert   struct type *ntype;	/* New type */
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert   int new_flags = (TYPE_INSTANCE_FLAGS (type)
644cf7f2e2dSJohn Marino 		   & ~(TYPE_INSTANCE_FLAG_CONST
645cf7f2e2dSJohn Marino 		       | TYPE_INSTANCE_FLAG_VOLATILE));
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert   if (cnst)
6485796c8dcSSimon Schubert     new_flags |= TYPE_INSTANCE_FLAG_CONST;
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert   if (voltl)
6515796c8dcSSimon Schubert     new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert   if (typeptr && *typeptr != NULL)
6545796c8dcSSimon Schubert     {
6555796c8dcSSimon Schubert       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
6565796c8dcSSimon Schubert 	 a C-V variant chain that threads across objfiles: if one
6575796c8dcSSimon Schubert 	 objfile gets freed, then the other has a broken C-V chain.
6585796c8dcSSimon Schubert 
6595796c8dcSSimon Schubert 	 This code used to try to copy over the main type from TYPE to
6605796c8dcSSimon Schubert 	 *TYPEPTR if they were in different objfiles, but that's
6615796c8dcSSimon Schubert 	 wrong, too: TYPE may have a field list or member function
6625796c8dcSSimon Schubert 	 lists, which refer to types of their own, etc. etc.  The
6635796c8dcSSimon Schubert 	 whole shebang would need to be copied over recursively; you
6645796c8dcSSimon Schubert 	 can't have inter-objfile pointers.  The only thing to do is
6655796c8dcSSimon Schubert 	 to leave stub types as stub types, and look them up afresh by
6665796c8dcSSimon Schubert 	 name each time you encounter them.  */
6675796c8dcSSimon Schubert       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
6685796c8dcSSimon Schubert     }
6695796c8dcSSimon Schubert 
6705796c8dcSSimon Schubert   ntype = make_qualified_type (type, new_flags,
6715796c8dcSSimon Schubert 			       typeptr ? *typeptr : NULL);
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert   if (typeptr != NULL)
6745796c8dcSSimon Schubert     *typeptr = ntype;
6755796c8dcSSimon Schubert 
6765796c8dcSSimon Schubert   return ntype;
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert 
679*ef5ccd6cSJohn Marino /* Make a 'restrict'-qualified version of TYPE.  */
680*ef5ccd6cSJohn Marino 
681*ef5ccd6cSJohn Marino struct type *
make_restrict_type(struct type * type)682*ef5ccd6cSJohn Marino make_restrict_type (struct type *type)
683*ef5ccd6cSJohn Marino {
684*ef5ccd6cSJohn Marino   return make_qualified_type (type,
685*ef5ccd6cSJohn Marino 			      (TYPE_INSTANCE_FLAGS (type)
686*ef5ccd6cSJohn Marino 			       | TYPE_INSTANCE_FLAG_RESTRICT),
687*ef5ccd6cSJohn Marino 			      NULL);
688*ef5ccd6cSJohn Marino }
689*ef5ccd6cSJohn Marino 
6905796c8dcSSimon Schubert /* Replace the contents of ntype with the type *type.  This changes the
6915796c8dcSSimon Schubert    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
6925796c8dcSSimon Schubert    the changes are propogated to all types in the TYPE_CHAIN.
6935796c8dcSSimon Schubert 
6945796c8dcSSimon Schubert    In order to build recursive types, it's inevitable that we'll need
6955796c8dcSSimon Schubert    to update types in place --- but this sort of indiscriminate
6965796c8dcSSimon Schubert    smashing is ugly, and needs to be replaced with something more
6975796c8dcSSimon Schubert    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
6985796c8dcSSimon Schubert    clear if more steps are needed.  */
699*ef5ccd6cSJohn Marino 
7005796c8dcSSimon Schubert void
replace_type(struct type * ntype,struct type * type)7015796c8dcSSimon Schubert replace_type (struct type *ntype, struct type *type)
7025796c8dcSSimon Schubert {
7035796c8dcSSimon Schubert   struct type *chain;
7045796c8dcSSimon Schubert 
7055796c8dcSSimon Schubert   /* These two types had better be in the same objfile.  Otherwise,
7065796c8dcSSimon Schubert      the assignment of one type's main type structure to the other
7075796c8dcSSimon Schubert      will produce a type with references to objects (names; field
7085796c8dcSSimon Schubert      lists; etc.) allocated on an objfile other than its own.  */
7095796c8dcSSimon Schubert   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert   /* The type length is not a part of the main type.  Update it for
7145796c8dcSSimon Schubert      each type on the variant chain.  */
7155796c8dcSSimon Schubert   chain = ntype;
7165796c8dcSSimon Schubert   do
7175796c8dcSSimon Schubert     {
7185796c8dcSSimon Schubert       /* Assert that this element of the chain has no address-class bits
7195796c8dcSSimon Schubert 	 set in its flags.  Such type variants might have type lengths
7205796c8dcSSimon Schubert 	 which are supposed to be different from the non-address-class
7215796c8dcSSimon Schubert 	 variants.  This assertion shouldn't ever be triggered because
7225796c8dcSSimon Schubert 	 symbol readers which do construct address-class variants don't
7235796c8dcSSimon Schubert 	 call replace_type().  */
7245796c8dcSSimon Schubert       gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
7255796c8dcSSimon Schubert 
7265796c8dcSSimon Schubert       TYPE_LENGTH (chain) = TYPE_LENGTH (type);
7275796c8dcSSimon Schubert       chain = TYPE_CHAIN (chain);
7285796c8dcSSimon Schubert     }
7295796c8dcSSimon Schubert   while (ntype != chain);
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert   /* Assert that the two types have equivalent instance qualifiers.
7325796c8dcSSimon Schubert      This should be true for at least all of our debug readers.  */
7335796c8dcSSimon Schubert   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
7345796c8dcSSimon Schubert }
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert /* Implement direct support for MEMBER_TYPE in GNU C++.
7375796c8dcSSimon Schubert    May need to construct such a type if this is the first use.
7385796c8dcSSimon Schubert    The TYPE is the type of the member.  The DOMAIN is the type
7395796c8dcSSimon Schubert    of the aggregate that the member belongs to.  */
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert struct type *
lookup_memberptr_type(struct type * type,struct type * domain)7425796c8dcSSimon Schubert lookup_memberptr_type (struct type *type, struct type *domain)
7435796c8dcSSimon Schubert {
7445796c8dcSSimon Schubert   struct type *mtype;
7455796c8dcSSimon Schubert 
7465796c8dcSSimon Schubert   mtype = alloc_type_copy (type);
7475796c8dcSSimon Schubert   smash_to_memberptr_type (mtype, domain, type);
7485796c8dcSSimon Schubert   return mtype;
7495796c8dcSSimon Schubert }
7505796c8dcSSimon Schubert 
7515796c8dcSSimon Schubert /* Return a pointer-to-method type, for a method of type TO_TYPE.  */
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert struct type *
lookup_methodptr_type(struct type * to_type)7545796c8dcSSimon Schubert lookup_methodptr_type (struct type *to_type)
7555796c8dcSSimon Schubert {
7565796c8dcSSimon Schubert   struct type *mtype;
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert   mtype = alloc_type_copy (to_type);
759cf7f2e2dSJohn Marino   smash_to_methodptr_type (mtype, to_type);
7605796c8dcSSimon Schubert   return mtype;
7615796c8dcSSimon Schubert }
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert /* Allocate a stub method whose return type is TYPE.  This apparently
7645796c8dcSSimon Schubert    happens for speed of symbol reading, since parsing out the
7655796c8dcSSimon Schubert    arguments to the method is cpu-intensive, the way we are doing it.
7665796c8dcSSimon Schubert    So, we will fill in arguments later.  This always returns a fresh
7675796c8dcSSimon Schubert    type.  */
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert struct type *
allocate_stub_method(struct type * type)7705796c8dcSSimon Schubert allocate_stub_method (struct type *type)
7715796c8dcSSimon Schubert {
7725796c8dcSSimon Schubert   struct type *mtype;
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert   mtype = alloc_type_copy (type);
7755796c8dcSSimon Schubert   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
7765796c8dcSSimon Schubert   TYPE_LENGTH (mtype) = 1;
7775796c8dcSSimon Schubert   TYPE_STUB (mtype) = 1;
7785796c8dcSSimon Schubert   TYPE_TARGET_TYPE (mtype) = type;
7795796c8dcSSimon Schubert   /*  _DOMAIN_TYPE (mtype) = unknown yet */
7805796c8dcSSimon Schubert   return mtype;
7815796c8dcSSimon Schubert }
7825796c8dcSSimon Schubert 
7835796c8dcSSimon Schubert /* Create a range type using either a blank type supplied in
7845796c8dcSSimon Schubert    RESULT_TYPE, or creating a new type, inheriting the objfile from
7855796c8dcSSimon Schubert    INDEX_TYPE.
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
7885796c8dcSSimon Schubert    to HIGH_BOUND, inclusive.
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
7915796c8dcSSimon Schubert    sure it is TYPE_CODE_UNDEF before we bash it into a range type?  */
7925796c8dcSSimon Schubert 
7935796c8dcSSimon Schubert struct type *
create_range_type(struct type * result_type,struct type * index_type,LONGEST low_bound,LONGEST high_bound)7945796c8dcSSimon Schubert create_range_type (struct type *result_type, struct type *index_type,
795cf7f2e2dSJohn Marino 		   LONGEST low_bound, LONGEST high_bound)
7965796c8dcSSimon Schubert {
7975796c8dcSSimon Schubert   if (result_type == NULL)
7985796c8dcSSimon Schubert     result_type = alloc_type_copy (index_type);
7995796c8dcSSimon Schubert   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
8005796c8dcSSimon Schubert   TYPE_TARGET_TYPE (result_type) = index_type;
8015796c8dcSSimon Schubert   if (TYPE_STUB (index_type))
8025796c8dcSSimon Schubert     TYPE_TARGET_STUB (result_type) = 1;
8035796c8dcSSimon Schubert   else
8045796c8dcSSimon Schubert     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
805cf7f2e2dSJohn Marino   TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
806cf7f2e2dSJohn Marino     TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
8075796c8dcSSimon Schubert   TYPE_LOW_BOUND (result_type) = low_bound;
8085796c8dcSSimon Schubert   TYPE_HIGH_BOUND (result_type) = high_bound;
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert   if (low_bound >= 0)
8115796c8dcSSimon Schubert     TYPE_UNSIGNED (result_type) = 1;
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert   return result_type;
8145796c8dcSSimon Schubert }
8155796c8dcSSimon Schubert 
8165796c8dcSSimon Schubert /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
8175796c8dcSSimon Schubert    TYPE.  Return 1 if type is a range type, 0 if it is discrete (and
8185796c8dcSSimon Schubert    bounds will fit in LONGEST), or -1 otherwise.  */
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert int
get_discrete_bounds(struct type * type,LONGEST * lowp,LONGEST * highp)8215796c8dcSSimon Schubert get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
8225796c8dcSSimon Schubert {
8235796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
8245796c8dcSSimon Schubert   switch (TYPE_CODE (type))
8255796c8dcSSimon Schubert     {
8265796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
8275796c8dcSSimon Schubert       *lowp = TYPE_LOW_BOUND (type);
8285796c8dcSSimon Schubert       *highp = TYPE_HIGH_BOUND (type);
8295796c8dcSSimon Schubert       return 1;
8305796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
8315796c8dcSSimon Schubert       if (TYPE_NFIELDS (type) > 0)
8325796c8dcSSimon Schubert 	{
8335796c8dcSSimon Schubert 	  /* The enums may not be sorted by value, so search all
834c50c785cSJohn Marino 	     entries.  */
8355796c8dcSSimon Schubert 	  int i;
8365796c8dcSSimon Schubert 
837*ef5ccd6cSJohn Marino 	  *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
8385796c8dcSSimon Schubert 	  for (i = 0; i < TYPE_NFIELDS (type); i++)
8395796c8dcSSimon Schubert 	    {
840*ef5ccd6cSJohn Marino 	      if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
841*ef5ccd6cSJohn Marino 		*lowp = TYPE_FIELD_ENUMVAL (type, i);
842*ef5ccd6cSJohn Marino 	      if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
843*ef5ccd6cSJohn Marino 		*highp = TYPE_FIELD_ENUMVAL (type, i);
8445796c8dcSSimon Schubert 	    }
8455796c8dcSSimon Schubert 
8465796c8dcSSimon Schubert 	  /* Set unsigned indicator if warranted.  */
8475796c8dcSSimon Schubert 	  if (*lowp >= 0)
8485796c8dcSSimon Schubert 	    {
8495796c8dcSSimon Schubert 	      TYPE_UNSIGNED (type) = 1;
8505796c8dcSSimon Schubert 	    }
8515796c8dcSSimon Schubert 	}
8525796c8dcSSimon Schubert       else
8535796c8dcSSimon Schubert 	{
8545796c8dcSSimon Schubert 	  *lowp = 0;
8555796c8dcSSimon Schubert 	  *highp = -1;
8565796c8dcSSimon Schubert 	}
8575796c8dcSSimon Schubert       return 0;
8585796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
8595796c8dcSSimon Schubert       *lowp = 0;
8605796c8dcSSimon Schubert       *highp = 1;
8615796c8dcSSimon Schubert       return 0;
8625796c8dcSSimon Schubert     case TYPE_CODE_INT:
8635796c8dcSSimon Schubert       if (TYPE_LENGTH (type) > sizeof (LONGEST))	/* Too big */
8645796c8dcSSimon Schubert 	return -1;
8655796c8dcSSimon Schubert       if (!TYPE_UNSIGNED (type))
8665796c8dcSSimon Schubert 	{
8675796c8dcSSimon Schubert 	  *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
8685796c8dcSSimon Schubert 	  *highp = -*lowp - 1;
8695796c8dcSSimon Schubert 	  return 0;
8705796c8dcSSimon Schubert 	}
8715796c8dcSSimon Schubert       /* ... fall through for unsigned ints ...  */
8725796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
8735796c8dcSSimon Schubert       *lowp = 0;
8745796c8dcSSimon Schubert       /* This round-about calculation is to avoid shifting by
8755796c8dcSSimon Schubert          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
8765796c8dcSSimon Schubert          if TYPE_LENGTH (type) == sizeof (LONGEST).  */
8775796c8dcSSimon Schubert       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
8785796c8dcSSimon Schubert       *highp = (*highp - 1) | *highp;
8795796c8dcSSimon Schubert       return 0;
8805796c8dcSSimon Schubert     default:
8815796c8dcSSimon Schubert       return -1;
8825796c8dcSSimon Schubert     }
8835796c8dcSSimon Schubert }
8845796c8dcSSimon Schubert 
885c50c785cSJohn Marino /* Assuming TYPE is a simple, non-empty array type, compute its upper
886c50c785cSJohn Marino    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
887c50c785cSJohn Marino    Save the high bound into HIGH_BOUND if not NULL.
888c50c785cSJohn Marino 
889c50c785cSJohn Marino    Return 1 if the operation was successful.  Return zero otherwise,
890c50c785cSJohn Marino    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
891c50c785cSJohn Marino 
892c50c785cSJohn Marino    We now simply use get_discrete_bounds call to get the values
893c50c785cSJohn Marino    of the low and high bounds.
894c50c785cSJohn Marino    get_discrete_bounds can return three values:
895c50c785cSJohn Marino    1, meaning that index is a range,
896c50c785cSJohn Marino    0, meaning that index is a discrete type,
897c50c785cSJohn Marino    or -1 for failure.  */
898c50c785cSJohn Marino 
899c50c785cSJohn Marino int
get_array_bounds(struct type * type,LONGEST * low_bound,LONGEST * high_bound)900c50c785cSJohn Marino get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
901c50c785cSJohn Marino {
902c50c785cSJohn Marino   struct type *index = TYPE_INDEX_TYPE (type);
903c50c785cSJohn Marino   LONGEST low = 0;
904c50c785cSJohn Marino   LONGEST high = 0;
905c50c785cSJohn Marino   int res;
906c50c785cSJohn Marino 
907c50c785cSJohn Marino   if (index == NULL)
908c50c785cSJohn Marino     return 0;
909c50c785cSJohn Marino 
910c50c785cSJohn Marino   res = get_discrete_bounds (index, &low, &high);
911c50c785cSJohn Marino   if (res == -1)
912c50c785cSJohn Marino     return 0;
913c50c785cSJohn Marino 
914c50c785cSJohn Marino   /* Check if the array bounds are undefined.  */
915c50c785cSJohn Marino   if (res == 1
916c50c785cSJohn Marino       && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
917c50c785cSJohn Marino 	  || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
918c50c785cSJohn Marino     return 0;
919c50c785cSJohn Marino 
920c50c785cSJohn Marino   if (low_bound)
921c50c785cSJohn Marino     *low_bound = low;
922c50c785cSJohn Marino 
923c50c785cSJohn Marino   if (high_bound)
924c50c785cSJohn Marino     *high_bound = high;
925c50c785cSJohn Marino 
926c50c785cSJohn Marino   return 1;
927c50c785cSJohn Marino }
928c50c785cSJohn Marino 
9295796c8dcSSimon Schubert /* Create an array type using either a blank type supplied in
9305796c8dcSSimon Schubert    RESULT_TYPE, or creating a new type, inheriting the objfile from
9315796c8dcSSimon Schubert    RANGE_TYPE.
9325796c8dcSSimon Schubert 
9335796c8dcSSimon Schubert    Elements will be of type ELEMENT_TYPE, the indices will be of type
9345796c8dcSSimon Schubert    RANGE_TYPE.
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
9375796c8dcSSimon Schubert    sure it is TYPE_CODE_UNDEF before we bash it into an array
9385796c8dcSSimon Schubert    type?  */
9395796c8dcSSimon Schubert 
9405796c8dcSSimon Schubert struct type *
create_array_type(struct type * result_type,struct type * element_type,struct type * range_type)9415796c8dcSSimon Schubert create_array_type (struct type *result_type,
9425796c8dcSSimon Schubert 		   struct type *element_type,
9435796c8dcSSimon Schubert 		   struct type *range_type)
9445796c8dcSSimon Schubert {
9455796c8dcSSimon Schubert   LONGEST low_bound, high_bound;
9465796c8dcSSimon Schubert 
9475796c8dcSSimon Schubert   if (result_type == NULL)
9485796c8dcSSimon Schubert     result_type = alloc_type_copy (range_type);
9495796c8dcSSimon Schubert 
9505796c8dcSSimon Schubert   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
9515796c8dcSSimon Schubert   TYPE_TARGET_TYPE (result_type) = element_type;
9525796c8dcSSimon Schubert   if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
9535796c8dcSSimon Schubert     low_bound = high_bound = 0;
9545796c8dcSSimon Schubert   CHECK_TYPEDEF (element_type);
9555796c8dcSSimon Schubert   /* Be careful when setting the array length.  Ada arrays can be
9565796c8dcSSimon Schubert      empty arrays with the high_bound being smaller than the low_bound.
9575796c8dcSSimon Schubert      In such cases, the array length should be zero.  */
9585796c8dcSSimon Schubert   if (high_bound < low_bound)
9595796c8dcSSimon Schubert     TYPE_LENGTH (result_type) = 0;
9605796c8dcSSimon Schubert   else
9615796c8dcSSimon Schubert     TYPE_LENGTH (result_type) =
9625796c8dcSSimon Schubert       TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
9635796c8dcSSimon Schubert   TYPE_NFIELDS (result_type) = 1;
9645796c8dcSSimon Schubert   TYPE_FIELDS (result_type) =
9655796c8dcSSimon Schubert     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
9665796c8dcSSimon Schubert   TYPE_INDEX_TYPE (result_type) = range_type;
9675796c8dcSSimon Schubert   TYPE_VPTR_FIELDNO (result_type) = -1;
9685796c8dcSSimon Schubert 
969c50c785cSJohn Marino   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
9705796c8dcSSimon Schubert   if (TYPE_LENGTH (result_type) == 0)
9715796c8dcSSimon Schubert     TYPE_TARGET_STUB (result_type) = 1;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert   return result_type;
9745796c8dcSSimon Schubert }
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert struct type *
lookup_array_range_type(struct type * element_type,LONGEST low_bound,LONGEST high_bound)9775796c8dcSSimon Schubert lookup_array_range_type (struct type *element_type,
978*ef5ccd6cSJohn Marino 			 LONGEST low_bound, LONGEST high_bound)
9795796c8dcSSimon Schubert {
9805796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (element_type);
9815796c8dcSSimon Schubert   struct type *index_type = builtin_type (gdbarch)->builtin_int;
9825796c8dcSSimon Schubert   struct type *range_type
9835796c8dcSSimon Schubert     = create_range_type (NULL, index_type, low_bound, high_bound);
984cf7f2e2dSJohn Marino 
9855796c8dcSSimon Schubert   return create_array_type (NULL, element_type, range_type);
9865796c8dcSSimon Schubert }
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert /* Create a string type using either a blank type supplied in
9895796c8dcSSimon Schubert    RESULT_TYPE, or creating a new type.  String types are similar
9905796c8dcSSimon Schubert    enough to array of char types that we can use create_array_type to
9915796c8dcSSimon Schubert    build the basic type and then bash it into a string type.
9925796c8dcSSimon Schubert 
9935796c8dcSSimon Schubert    For fixed length strings, the range type contains 0 as the lower
9945796c8dcSSimon Schubert    bound and the length of the string minus one as the upper bound.
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
9975796c8dcSSimon Schubert    sure it is TYPE_CODE_UNDEF before we bash it into a string
9985796c8dcSSimon Schubert    type?  */
9995796c8dcSSimon Schubert 
10005796c8dcSSimon Schubert struct type *
create_string_type(struct type * result_type,struct type * string_char_type,struct type * range_type)10015796c8dcSSimon Schubert create_string_type (struct type *result_type,
10025796c8dcSSimon Schubert 		    struct type *string_char_type,
10035796c8dcSSimon Schubert 		    struct type *range_type)
10045796c8dcSSimon Schubert {
10055796c8dcSSimon Schubert   result_type = create_array_type (result_type,
10065796c8dcSSimon Schubert 				   string_char_type,
10075796c8dcSSimon Schubert 				   range_type);
10085796c8dcSSimon Schubert   TYPE_CODE (result_type) = TYPE_CODE_STRING;
10095796c8dcSSimon Schubert   return result_type;
10105796c8dcSSimon Schubert }
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert struct type *
lookup_string_range_type(struct type * string_char_type,LONGEST low_bound,LONGEST high_bound)10135796c8dcSSimon Schubert lookup_string_range_type (struct type *string_char_type,
1014*ef5ccd6cSJohn Marino 			  LONGEST low_bound, LONGEST high_bound)
10155796c8dcSSimon Schubert {
10165796c8dcSSimon Schubert   struct type *result_type;
1017cf7f2e2dSJohn Marino 
10185796c8dcSSimon Schubert   result_type = lookup_array_range_type (string_char_type,
10195796c8dcSSimon Schubert 					 low_bound, high_bound);
10205796c8dcSSimon Schubert   TYPE_CODE (result_type) = TYPE_CODE_STRING;
10215796c8dcSSimon Schubert   return result_type;
10225796c8dcSSimon Schubert }
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert struct type *
create_set_type(struct type * result_type,struct type * domain_type)10255796c8dcSSimon Schubert create_set_type (struct type *result_type, struct type *domain_type)
10265796c8dcSSimon Schubert {
10275796c8dcSSimon Schubert   if (result_type == NULL)
10285796c8dcSSimon Schubert     result_type = alloc_type_copy (domain_type);
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert   TYPE_CODE (result_type) = TYPE_CODE_SET;
10315796c8dcSSimon Schubert   TYPE_NFIELDS (result_type) = 1;
10325796c8dcSSimon Schubert   TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert   if (!TYPE_STUB (domain_type))
10355796c8dcSSimon Schubert     {
10365796c8dcSSimon Schubert       LONGEST low_bound, high_bound, bit_length;
1037cf7f2e2dSJohn Marino 
10385796c8dcSSimon Schubert       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
10395796c8dcSSimon Schubert 	low_bound = high_bound = 0;
10405796c8dcSSimon Schubert       bit_length = high_bound - low_bound + 1;
10415796c8dcSSimon Schubert       TYPE_LENGTH (result_type)
10425796c8dcSSimon Schubert 	= (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
10435796c8dcSSimon Schubert       if (low_bound >= 0)
10445796c8dcSSimon Schubert 	TYPE_UNSIGNED (result_type) = 1;
10455796c8dcSSimon Schubert     }
10465796c8dcSSimon Schubert   TYPE_FIELD_TYPE (result_type, 0) = domain_type;
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert   return result_type;
10495796c8dcSSimon Schubert }
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert /* Convert ARRAY_TYPE to a vector type.  This may modify ARRAY_TYPE
10525796c8dcSSimon Schubert    and any array types nested inside it.  */
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert void
make_vector_type(struct type * array_type)10555796c8dcSSimon Schubert make_vector_type (struct type *array_type)
10565796c8dcSSimon Schubert {
10575796c8dcSSimon Schubert   struct type *inner_array, *elt_type;
10585796c8dcSSimon Schubert   int flags;
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert   /* Find the innermost array type, in case the array is
10615796c8dcSSimon Schubert      multi-dimensional.  */
10625796c8dcSSimon Schubert   inner_array = array_type;
10635796c8dcSSimon Schubert   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10645796c8dcSSimon Schubert     inner_array = TYPE_TARGET_TYPE (inner_array);
10655796c8dcSSimon Schubert 
10665796c8dcSSimon Schubert   elt_type = TYPE_TARGET_TYPE (inner_array);
10675796c8dcSSimon Schubert   if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
10685796c8dcSSimon Schubert     {
1069c50c785cSJohn Marino       flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
10705796c8dcSSimon Schubert       elt_type = make_qualified_type (elt_type, flags, NULL);
10715796c8dcSSimon Schubert       TYPE_TARGET_TYPE (inner_array) = elt_type;
10725796c8dcSSimon Schubert     }
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert   TYPE_VECTOR (array_type) = 1;
10755796c8dcSSimon Schubert }
10765796c8dcSSimon Schubert 
10775796c8dcSSimon Schubert struct type *
init_vector_type(struct type * elt_type,int n)10785796c8dcSSimon Schubert init_vector_type (struct type *elt_type, int n)
10795796c8dcSSimon Schubert {
10805796c8dcSSimon Schubert   struct type *array_type;
1081cf7f2e2dSJohn Marino 
10825796c8dcSSimon Schubert   array_type = lookup_array_range_type (elt_type, 0, n - 1);
10835796c8dcSSimon Schubert   make_vector_type (array_type);
10845796c8dcSSimon Schubert   return array_type;
10855796c8dcSSimon Schubert }
10865796c8dcSSimon Schubert 
10875796c8dcSSimon Schubert /* Smash TYPE to be a type of pointers to members of DOMAIN with type
10885796c8dcSSimon Schubert    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
10895796c8dcSSimon Schubert    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
10905796c8dcSSimon Schubert    TYPE doesn't include the offset (that's the value of the MEMBER
10915796c8dcSSimon Schubert    itself), but does include the structure type into which it points
10925796c8dcSSimon Schubert    (for some reason).
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert    When "smashing" the type, we preserve the objfile that the old type
10955796c8dcSSimon Schubert    pointed to, since we aren't changing where the type is actually
10965796c8dcSSimon Schubert    allocated.  */
10975796c8dcSSimon Schubert 
10985796c8dcSSimon Schubert void
smash_to_memberptr_type(struct type * type,struct type * domain,struct type * to_type)10995796c8dcSSimon Schubert smash_to_memberptr_type (struct type *type, struct type *domain,
11005796c8dcSSimon Schubert 			 struct type *to_type)
11015796c8dcSSimon Schubert {
11025796c8dcSSimon Schubert   smash_type (type);
11035796c8dcSSimon Schubert   TYPE_TARGET_TYPE (type) = to_type;
11045796c8dcSSimon Schubert   TYPE_DOMAIN_TYPE (type) = domain;
11055796c8dcSSimon Schubert   /* Assume that a data member pointer is the same size as a normal
11065796c8dcSSimon Schubert      pointer.  */
11075796c8dcSSimon Schubert   TYPE_LENGTH (type)
11085796c8dcSSimon Schubert     = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
11095796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
11105796c8dcSSimon Schubert }
11115796c8dcSSimon Schubert 
1112cf7f2e2dSJohn Marino /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1113cf7f2e2dSJohn Marino 
1114cf7f2e2dSJohn Marino    When "smashing" the type, we preserve the objfile that the old type
1115cf7f2e2dSJohn Marino    pointed to, since we aren't changing where the type is actually
1116cf7f2e2dSJohn Marino    allocated.  */
1117cf7f2e2dSJohn Marino 
1118cf7f2e2dSJohn Marino void
smash_to_methodptr_type(struct type * type,struct type * to_type)1119cf7f2e2dSJohn Marino smash_to_methodptr_type (struct type *type, struct type *to_type)
1120cf7f2e2dSJohn Marino {
1121cf7f2e2dSJohn Marino   smash_type (type);
1122cf7f2e2dSJohn Marino   TYPE_TARGET_TYPE (type) = to_type;
1123cf7f2e2dSJohn Marino   TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1124cf7f2e2dSJohn Marino   TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1125cf7f2e2dSJohn Marino   TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1126cf7f2e2dSJohn Marino }
1127cf7f2e2dSJohn Marino 
11285796c8dcSSimon Schubert /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
11295796c8dcSSimon Schubert    METHOD just means `function that gets an extra "this" argument'.
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert    When "smashing" the type, we preserve the objfile that the old type
11325796c8dcSSimon Schubert    pointed to, since we aren't changing where the type is actually
11335796c8dcSSimon Schubert    allocated.  */
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert void
smash_to_method_type(struct type * type,struct type * domain,struct type * to_type,struct field * args,int nargs,int varargs)11365796c8dcSSimon Schubert smash_to_method_type (struct type *type, struct type *domain,
11375796c8dcSSimon Schubert 		      struct type *to_type, struct field *args,
11385796c8dcSSimon Schubert 		      int nargs, int varargs)
11395796c8dcSSimon Schubert {
11405796c8dcSSimon Schubert   smash_type (type);
11415796c8dcSSimon Schubert   TYPE_TARGET_TYPE (type) = to_type;
11425796c8dcSSimon Schubert   TYPE_DOMAIN_TYPE (type) = domain;
11435796c8dcSSimon Schubert   TYPE_FIELDS (type) = args;
11445796c8dcSSimon Schubert   TYPE_NFIELDS (type) = nargs;
11455796c8dcSSimon Schubert   if (varargs)
11465796c8dcSSimon Schubert     TYPE_VARARGS (type) = 1;
11475796c8dcSSimon Schubert   TYPE_LENGTH (type) = 1;	/* In practice, this is never needed.  */
11485796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_METHOD;
11495796c8dcSSimon Schubert }
11505796c8dcSSimon Schubert 
11515796c8dcSSimon Schubert /* Return a typename for a struct/union/enum type without "struct ",
11525796c8dcSSimon Schubert    "union ", or "enum ".  If the type has a NULL name, return NULL.  */
11535796c8dcSSimon Schubert 
1154*ef5ccd6cSJohn Marino const char *
type_name_no_tag(const struct type * type)11555796c8dcSSimon Schubert type_name_no_tag (const struct type *type)
11565796c8dcSSimon Schubert {
11575796c8dcSSimon Schubert   if (TYPE_TAG_NAME (type) != NULL)
11585796c8dcSSimon Schubert     return TYPE_TAG_NAME (type);
11595796c8dcSSimon Schubert 
11605796c8dcSSimon Schubert   /* Is there code which expects this to return the name if there is
11615796c8dcSSimon Schubert      no tag name?  My guess is that this is mainly used for C++ in
11625796c8dcSSimon Schubert      cases where the two will always be the same.  */
11635796c8dcSSimon Schubert   return TYPE_NAME (type);
11645796c8dcSSimon Schubert }
11655796c8dcSSimon Schubert 
1166a45ae5f8SJohn Marino /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1167a45ae5f8SJohn Marino    Since GCC PR debug/47510 DWARF provides associated information to detect the
1168a45ae5f8SJohn Marino    anonymous class linkage name from its typedef.
1169a45ae5f8SJohn Marino 
1170a45ae5f8SJohn Marino    Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1171a45ae5f8SJohn Marino    apply it itself.  */
1172a45ae5f8SJohn Marino 
1173a45ae5f8SJohn Marino const char *
type_name_no_tag_or_error(struct type * type)1174a45ae5f8SJohn Marino type_name_no_tag_or_error (struct type *type)
1175a45ae5f8SJohn Marino {
1176a45ae5f8SJohn Marino   struct type *saved_type = type;
1177a45ae5f8SJohn Marino   const char *name;
1178a45ae5f8SJohn Marino   struct objfile *objfile;
1179a45ae5f8SJohn Marino 
1180a45ae5f8SJohn Marino   CHECK_TYPEDEF (type);
1181a45ae5f8SJohn Marino 
1182a45ae5f8SJohn Marino   name = type_name_no_tag (type);
1183a45ae5f8SJohn Marino   if (name != NULL)
1184a45ae5f8SJohn Marino     return name;
1185a45ae5f8SJohn Marino 
1186a45ae5f8SJohn Marino   name = type_name_no_tag (saved_type);
1187a45ae5f8SJohn Marino   objfile = TYPE_OBJFILE (saved_type);
1188a45ae5f8SJohn Marino   error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1189a45ae5f8SJohn Marino 	 name ? name : "<anonymous>", objfile ? objfile->name : "<arch>");
1190a45ae5f8SJohn Marino }
1191a45ae5f8SJohn Marino 
11925796c8dcSSimon Schubert /* Lookup a typedef or primitive type named NAME, visible in lexical
11935796c8dcSSimon Schubert    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
11945796c8dcSSimon Schubert    suitably defined.  */
11955796c8dcSSimon Schubert 
11965796c8dcSSimon Schubert struct type *
lookup_typename(const struct language_defn * language,struct gdbarch * gdbarch,const char * name,const struct block * block,int noerr)11975796c8dcSSimon Schubert lookup_typename (const struct language_defn *language,
1198a45ae5f8SJohn Marino 		 struct gdbarch *gdbarch, const char *name,
1199c50c785cSJohn Marino 		 const struct block *block, int noerr)
12005796c8dcSSimon Schubert {
12015796c8dcSSimon Schubert   struct symbol *sym;
1202*ef5ccd6cSJohn Marino   struct type *type;
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert   sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
1205*ef5ccd6cSJohn Marino   if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1206*ef5ccd6cSJohn Marino     return SYMBOL_TYPE (sym);
1207*ef5ccd6cSJohn Marino 
1208*ef5ccd6cSJohn Marino   type = language_lookup_primitive_type_by_name (language, gdbarch, name);
1209*ef5ccd6cSJohn Marino   if (type)
1210*ef5ccd6cSJohn Marino     return type;
1211*ef5ccd6cSJohn Marino 
1212*ef5ccd6cSJohn Marino   if (noerr)
12135796c8dcSSimon Schubert     return NULL;
12145796c8dcSSimon Schubert   error (_("No type named %s."), name);
12155796c8dcSSimon Schubert }
12165796c8dcSSimon Schubert 
12175796c8dcSSimon Schubert struct type *
lookup_unsigned_typename(const struct language_defn * language,struct gdbarch * gdbarch,const char * name)12185796c8dcSSimon Schubert lookup_unsigned_typename (const struct language_defn *language,
1219*ef5ccd6cSJohn Marino 			  struct gdbarch *gdbarch, const char *name)
12205796c8dcSSimon Schubert {
12215796c8dcSSimon Schubert   char *uns = alloca (strlen (name) + 10);
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert   strcpy (uns, "unsigned ");
12245796c8dcSSimon Schubert   strcpy (uns + 9, name);
12255796c8dcSSimon Schubert   return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
12265796c8dcSSimon Schubert }
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert struct type *
lookup_signed_typename(const struct language_defn * language,struct gdbarch * gdbarch,const char * name)12295796c8dcSSimon Schubert lookup_signed_typename (const struct language_defn *language,
1230*ef5ccd6cSJohn Marino 			struct gdbarch *gdbarch, const char *name)
12315796c8dcSSimon Schubert {
12325796c8dcSSimon Schubert   struct type *t;
12335796c8dcSSimon Schubert   char *uns = alloca (strlen (name) + 8);
12345796c8dcSSimon Schubert 
12355796c8dcSSimon Schubert   strcpy (uns, "signed ");
12365796c8dcSSimon Schubert   strcpy (uns + 7, name);
12375796c8dcSSimon Schubert   t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
12385796c8dcSSimon Schubert   /* If we don't find "signed FOO" just try again with plain "FOO".  */
12395796c8dcSSimon Schubert   if (t != NULL)
12405796c8dcSSimon Schubert     return t;
12415796c8dcSSimon Schubert   return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert /* Lookup a structure type named "struct NAME",
12455796c8dcSSimon Schubert    visible in lexical block BLOCK.  */
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert struct type *
lookup_struct(const char * name,const struct block * block)1248*ef5ccd6cSJohn Marino lookup_struct (const char *name, const struct block *block)
12495796c8dcSSimon Schubert {
12505796c8dcSSimon Schubert   struct symbol *sym;
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
12535796c8dcSSimon Schubert 
12545796c8dcSSimon Schubert   if (sym == NULL)
12555796c8dcSSimon Schubert     {
12565796c8dcSSimon Schubert       error (_("No struct type named %s."), name);
12575796c8dcSSimon Schubert     }
12585796c8dcSSimon Schubert   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
12595796c8dcSSimon Schubert     {
12605796c8dcSSimon Schubert       error (_("This context has class, union or enum %s, not a struct."),
12615796c8dcSSimon Schubert 	     name);
12625796c8dcSSimon Schubert     }
12635796c8dcSSimon Schubert   return (SYMBOL_TYPE (sym));
12645796c8dcSSimon Schubert }
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert /* Lookup a union type named "union NAME",
12675796c8dcSSimon Schubert    visible in lexical block BLOCK.  */
12685796c8dcSSimon Schubert 
12695796c8dcSSimon Schubert struct type *
lookup_union(const char * name,const struct block * block)1270*ef5ccd6cSJohn Marino lookup_union (const char *name, const struct block *block)
12715796c8dcSSimon Schubert {
12725796c8dcSSimon Schubert   struct symbol *sym;
12735796c8dcSSimon Schubert   struct type *t;
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
12765796c8dcSSimon Schubert 
12775796c8dcSSimon Schubert   if (sym == NULL)
12785796c8dcSSimon Schubert     error (_("No union type named %s."), name);
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   t = SYMBOL_TYPE (sym);
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert   if (TYPE_CODE (t) == TYPE_CODE_UNION)
12835796c8dcSSimon Schubert     return t;
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert   /* If we get here, it's not a union.  */
12865796c8dcSSimon Schubert   error (_("This context has class, struct or enum %s, not a union."),
12875796c8dcSSimon Schubert 	 name);
12885796c8dcSSimon Schubert }
12895796c8dcSSimon Schubert 
12905796c8dcSSimon Schubert /* Lookup an enum type named "enum NAME",
12915796c8dcSSimon Schubert    visible in lexical block BLOCK.  */
12925796c8dcSSimon Schubert 
12935796c8dcSSimon Schubert struct type *
lookup_enum(const char * name,const struct block * block)1294*ef5ccd6cSJohn Marino lookup_enum (const char *name, const struct block *block)
12955796c8dcSSimon Schubert {
12965796c8dcSSimon Schubert   struct symbol *sym;
12975796c8dcSSimon Schubert 
12985796c8dcSSimon Schubert   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
12995796c8dcSSimon Schubert   if (sym == NULL)
13005796c8dcSSimon Schubert     {
13015796c8dcSSimon Schubert       error (_("No enum type named %s."), name);
13025796c8dcSSimon Schubert     }
13035796c8dcSSimon Schubert   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
13045796c8dcSSimon Schubert     {
13055796c8dcSSimon Schubert       error (_("This context has class, struct or union %s, not an enum."),
13065796c8dcSSimon Schubert 	     name);
13075796c8dcSSimon Schubert     }
13085796c8dcSSimon Schubert   return (SYMBOL_TYPE (sym));
13095796c8dcSSimon Schubert }
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert /* Lookup a template type named "template NAME<TYPE>",
13125796c8dcSSimon Schubert    visible in lexical block BLOCK.  */
13135796c8dcSSimon Schubert 
13145796c8dcSSimon Schubert struct type *
lookup_template_type(char * name,struct type * type,const struct block * block)13155796c8dcSSimon Schubert lookup_template_type (char *name, struct type *type,
1316*ef5ccd6cSJohn Marino 		      const struct block *block)
13175796c8dcSSimon Schubert {
13185796c8dcSSimon Schubert   struct symbol *sym;
13195796c8dcSSimon Schubert   char *nam = (char *)
13205796c8dcSSimon Schubert     alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1321cf7f2e2dSJohn Marino 
13225796c8dcSSimon Schubert   strcpy (nam, name);
13235796c8dcSSimon Schubert   strcat (nam, "<");
13245796c8dcSSimon Schubert   strcat (nam, TYPE_NAME (type));
13255796c8dcSSimon Schubert   strcat (nam, " >");	/* FIXME, extra space still introduced in gcc?  */
13265796c8dcSSimon Schubert 
13275796c8dcSSimon Schubert   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   if (sym == NULL)
13305796c8dcSSimon Schubert     {
13315796c8dcSSimon Schubert       error (_("No template type named %s."), name);
13325796c8dcSSimon Schubert     }
13335796c8dcSSimon Schubert   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
13345796c8dcSSimon Schubert     {
13355796c8dcSSimon Schubert       error (_("This context has class, union or enum %s, not a struct."),
13365796c8dcSSimon Schubert 	     name);
13375796c8dcSSimon Schubert     }
13385796c8dcSSimon Schubert   return (SYMBOL_TYPE (sym));
13395796c8dcSSimon Schubert }
13405796c8dcSSimon Schubert 
13415796c8dcSSimon Schubert /* Given a type TYPE, lookup the type of the component of type named
13425796c8dcSSimon Schubert    NAME.
13435796c8dcSSimon Schubert 
13445796c8dcSSimon Schubert    TYPE can be either a struct or union, or a pointer or reference to
13455796c8dcSSimon Schubert    a struct or union.  If it is a pointer or reference, its target
13465796c8dcSSimon Schubert    type is automatically used.  Thus '.' and '->' are interchangable,
13475796c8dcSSimon Schubert    as specified for the definitions of the expression element types
13485796c8dcSSimon Schubert    STRUCTOP_STRUCT and STRUCTOP_PTR.
13495796c8dcSSimon Schubert 
13505796c8dcSSimon Schubert    If NOERR is nonzero, return zero if NAME is not suitably defined.
13515796c8dcSSimon Schubert    If NAME is the name of a baseclass type, return that type.  */
13525796c8dcSSimon Schubert 
13535796c8dcSSimon Schubert struct type *
lookup_struct_elt_type(struct type * type,char * name,int noerr)13545796c8dcSSimon Schubert lookup_struct_elt_type (struct type *type, char *name, int noerr)
13555796c8dcSSimon Schubert {
13565796c8dcSSimon Schubert   int i;
1357c50c785cSJohn Marino   char *typename;
13585796c8dcSSimon Schubert 
13595796c8dcSSimon Schubert   for (;;)
13605796c8dcSSimon Schubert     {
13615796c8dcSSimon Schubert       CHECK_TYPEDEF (type);
13625796c8dcSSimon Schubert       if (TYPE_CODE (type) != TYPE_CODE_PTR
13635796c8dcSSimon Schubert 	  && TYPE_CODE (type) != TYPE_CODE_REF)
13645796c8dcSSimon Schubert 	break;
13655796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
13665796c8dcSSimon Schubert     }
13675796c8dcSSimon Schubert 
13685796c8dcSSimon Schubert   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
13695796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_UNION)
13705796c8dcSSimon Schubert     {
1371c50c785cSJohn Marino       typename = type_to_string (type);
1372c50c785cSJohn Marino       make_cleanup (xfree, typename);
1373c50c785cSJohn Marino       error (_("Type %s is not a structure or union type."), typename);
13745796c8dcSSimon Schubert     }
13755796c8dcSSimon Schubert 
13765796c8dcSSimon Schubert #if 0
13775796c8dcSSimon Schubert   /* FIXME: This change put in by Michael seems incorrect for the case
13785796c8dcSSimon Schubert      where the structure tag name is the same as the member name.
1379c50c785cSJohn Marino      I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
13805796c8dcSSimon Schubert      foo; } bell;" Disabled by fnf.  */
13815796c8dcSSimon Schubert   {
13825796c8dcSSimon Schubert     char *typename;
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert     typename = type_name_no_tag (type);
13855796c8dcSSimon Schubert     if (typename != NULL && strcmp (typename, name) == 0)
13865796c8dcSSimon Schubert       return type;
13875796c8dcSSimon Schubert   }
13885796c8dcSSimon Schubert #endif
13895796c8dcSSimon Schubert 
13905796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
13915796c8dcSSimon Schubert     {
1392*ef5ccd6cSJohn Marino       const char *t_field_name = TYPE_FIELD_NAME (type, i);
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
13955796c8dcSSimon Schubert 	{
13965796c8dcSSimon Schubert 	  return TYPE_FIELD_TYPE (type, i);
13975796c8dcSSimon Schubert 	}
1398cf7f2e2dSJohn Marino      else if (!t_field_name || *t_field_name == '\0')
1399cf7f2e2dSJohn Marino 	{
1400cf7f2e2dSJohn Marino 	  struct type *subtype
1401cf7f2e2dSJohn Marino 	    = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1402cf7f2e2dSJohn Marino 
1403cf7f2e2dSJohn Marino 	  if (subtype != NULL)
1404cf7f2e2dSJohn Marino 	    return subtype;
1405cf7f2e2dSJohn Marino 	}
14065796c8dcSSimon Schubert     }
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert   /* OK, it's not in this class.  Recursively check the baseclasses.  */
14095796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
14105796c8dcSSimon Schubert     {
14115796c8dcSSimon Schubert       struct type *t;
14125796c8dcSSimon Schubert 
14135796c8dcSSimon Schubert       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
14145796c8dcSSimon Schubert       if (t != NULL)
14155796c8dcSSimon Schubert 	{
14165796c8dcSSimon Schubert 	  return t;
14175796c8dcSSimon Schubert 	}
14185796c8dcSSimon Schubert     }
14195796c8dcSSimon Schubert 
14205796c8dcSSimon Schubert   if (noerr)
14215796c8dcSSimon Schubert     {
14225796c8dcSSimon Schubert       return NULL;
14235796c8dcSSimon Schubert     }
14245796c8dcSSimon Schubert 
1425c50c785cSJohn Marino   typename = type_to_string (type);
1426c50c785cSJohn Marino   make_cleanup (xfree, typename);
1427c50c785cSJohn Marino   error (_("Type %s has no component named %s."), typename, name);
14285796c8dcSSimon Schubert }
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert /* Lookup the vptr basetype/fieldno values for TYPE.
14315796c8dcSSimon Schubert    If found store vptr_basetype in *BASETYPEP if non-NULL, and return
14325796c8dcSSimon Schubert    vptr_fieldno.  Also, if found and basetype is from the same objfile,
14335796c8dcSSimon Schubert    cache the results.
14345796c8dcSSimon Schubert    If not found, return -1 and ignore BASETYPEP.
14355796c8dcSSimon Schubert    Callers should be aware that in some cases (for example,
14365796c8dcSSimon Schubert    the type or one of its baseclasses is a stub type and we are
1437cf7f2e2dSJohn Marino    debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1438cf7f2e2dSJohn Marino    this function will not be able to find the
14395796c8dcSSimon Schubert    virtual function table pointer, and vptr_fieldno will remain -1 and
14405796c8dcSSimon Schubert    vptr_basetype will remain NULL or incomplete.  */
14415796c8dcSSimon Schubert 
14425796c8dcSSimon Schubert int
get_vptr_fieldno(struct type * type,struct type ** basetypep)14435796c8dcSSimon Schubert get_vptr_fieldno (struct type *type, struct type **basetypep)
14445796c8dcSSimon Schubert {
14455796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
14465796c8dcSSimon Schubert 
14475796c8dcSSimon Schubert   if (TYPE_VPTR_FIELDNO (type) < 0)
14485796c8dcSSimon Schubert     {
14495796c8dcSSimon Schubert       int i;
14505796c8dcSSimon Schubert 
14515796c8dcSSimon Schubert       /* We must start at zero in case the first (and only) baseclass
14525796c8dcSSimon Schubert          is virtual (and hence we cannot share the table pointer).  */
14535796c8dcSSimon Schubert       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
14545796c8dcSSimon Schubert 	{
14555796c8dcSSimon Schubert 	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
14565796c8dcSSimon Schubert 	  int fieldno;
14575796c8dcSSimon Schubert 	  struct type *basetype;
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert 	  fieldno = get_vptr_fieldno (baseclass, &basetype);
14605796c8dcSSimon Schubert 	  if (fieldno >= 0)
14615796c8dcSSimon Schubert 	    {
14625796c8dcSSimon Schubert 	      /* If the type comes from a different objfile we can't cache
14635796c8dcSSimon Schubert 		 it, it may have a different lifetime.  PR 2384 */
14645796c8dcSSimon Schubert 	      if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
14655796c8dcSSimon Schubert 		{
14665796c8dcSSimon Schubert 		  TYPE_VPTR_FIELDNO (type) = fieldno;
14675796c8dcSSimon Schubert 		  TYPE_VPTR_BASETYPE (type) = basetype;
14685796c8dcSSimon Schubert 		}
14695796c8dcSSimon Schubert 	      if (basetypep)
14705796c8dcSSimon Schubert 		*basetypep = basetype;
14715796c8dcSSimon Schubert 	      return fieldno;
14725796c8dcSSimon Schubert 	    }
14735796c8dcSSimon Schubert 	}
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert       /* Not found.  */
14765796c8dcSSimon Schubert       return -1;
14775796c8dcSSimon Schubert     }
14785796c8dcSSimon Schubert   else
14795796c8dcSSimon Schubert     {
14805796c8dcSSimon Schubert       if (basetypep)
14815796c8dcSSimon Schubert 	*basetypep = TYPE_VPTR_BASETYPE (type);
14825796c8dcSSimon Schubert       return TYPE_VPTR_FIELDNO (type);
14835796c8dcSSimon Schubert     }
14845796c8dcSSimon Schubert }
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert static void
stub_noname_complaint(void)14875796c8dcSSimon Schubert stub_noname_complaint (void)
14885796c8dcSSimon Schubert {
14895796c8dcSSimon Schubert   complaint (&symfile_complaints, _("stub type has NULL name"));
14905796c8dcSSimon Schubert }
14915796c8dcSSimon Schubert 
1492c50c785cSJohn Marino /* Find the real type of TYPE.  This function returns the real type,
1493c50c785cSJohn Marino    after removing all layers of typedefs, and completing opaque or stub
1494c50c785cSJohn Marino    types.  Completion changes the TYPE argument, but stripping of
1495c50c785cSJohn Marino    typedefs does not.
1496c50c785cSJohn Marino 
1497c50c785cSJohn Marino    Instance flags (e.g. const/volatile) are preserved as typedefs are
1498c50c785cSJohn Marino    stripped.  If necessary a new qualified form of the underlying type
1499c50c785cSJohn Marino    is created.
1500c50c785cSJohn Marino 
1501c50c785cSJohn Marino    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1502c50c785cSJohn Marino    not been computed and we're either in the middle of reading symbols, or
1503c50c785cSJohn Marino    there was no name for the typedef in the debug info.
1504c50c785cSJohn Marino 
1505a45ae5f8SJohn Marino    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1506a45ae5f8SJohn Marino    QUITs in the symbol reading code can also throw.
1507a45ae5f8SJohn Marino    Thus this function can throw an exception.
1508a45ae5f8SJohn Marino 
1509c50c785cSJohn Marino    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1510c50c785cSJohn Marino    the target type.
15115796c8dcSSimon Schubert 
15125796c8dcSSimon Schubert    If this is a stubbed struct (i.e. declared as struct foo *), see if
15135796c8dcSSimon Schubert    we can find a full definition in some other file.  If so, copy this
15145796c8dcSSimon Schubert    definition, so we can use it in future.  There used to be a comment
15155796c8dcSSimon Schubert    (but not any code) that if we don't find a full definition, we'd
15165796c8dcSSimon Schubert    set a flag so we don't spend time in the future checking the same
15175796c8dcSSimon Schubert    type.  That would be a mistake, though--we might load in more
1518c50c785cSJohn Marino    symbols which contain a full definition for the type.  */
15195796c8dcSSimon Schubert 
15205796c8dcSSimon Schubert struct type *
check_typedef(struct type * type)15215796c8dcSSimon Schubert check_typedef (struct type *type)
15225796c8dcSSimon Schubert {
15235796c8dcSSimon Schubert   struct type *orig_type = type;
1524c50c785cSJohn Marino   /* While we're removing typedefs, we don't want to lose qualifiers.
1525c50c785cSJohn Marino      E.g., const/volatile.  */
1526c50c785cSJohn Marino   int instance_flags = TYPE_INSTANCE_FLAGS (type);
15275796c8dcSSimon Schubert 
15285796c8dcSSimon Schubert   gdb_assert (type);
15295796c8dcSSimon Schubert 
15305796c8dcSSimon Schubert   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
15315796c8dcSSimon Schubert     {
15325796c8dcSSimon Schubert       if (!TYPE_TARGET_TYPE (type))
15335796c8dcSSimon Schubert 	{
1534*ef5ccd6cSJohn Marino 	  const char *name;
15355796c8dcSSimon Schubert 	  struct symbol *sym;
15365796c8dcSSimon Schubert 
15375796c8dcSSimon Schubert 	  /* It is dangerous to call lookup_symbol if we are currently
15385796c8dcSSimon Schubert 	     reading a symtab.  Infinite recursion is one danger.  */
15395796c8dcSSimon Schubert 	  if (currently_reading_symtab)
1540c50c785cSJohn Marino 	    return make_qualified_type (type, instance_flags, NULL);
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert 	  name = type_name_no_tag (type);
15435796c8dcSSimon Schubert 	  /* FIXME: shouldn't we separately check the TYPE_NAME and
15445796c8dcSSimon Schubert 	     the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
15455796c8dcSSimon Schubert 	     VAR_DOMAIN as appropriate?  (this code was written before
15465796c8dcSSimon Schubert 	     TYPE_NAME and TYPE_TAG_NAME were separate).  */
15475796c8dcSSimon Schubert 	  if (name == NULL)
15485796c8dcSSimon Schubert 	    {
15495796c8dcSSimon Schubert 	      stub_noname_complaint ();
1550c50c785cSJohn Marino 	      return make_qualified_type (type, instance_flags, NULL);
15515796c8dcSSimon Schubert 	    }
15525796c8dcSSimon Schubert 	  sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
15535796c8dcSSimon Schubert 	  if (sym)
15545796c8dcSSimon Schubert 	    TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
15555796c8dcSSimon Schubert 	  else					/* TYPE_CODE_UNDEF */
15565796c8dcSSimon Schubert 	    TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
15575796c8dcSSimon Schubert 	}
15585796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
15595796c8dcSSimon Schubert 
1560c50c785cSJohn Marino       /* Preserve the instance flags as we traverse down the typedef chain.
1561c50c785cSJohn Marino 
1562c50c785cSJohn Marino 	 Handling address spaces/classes is nasty, what do we do if there's a
1563c50c785cSJohn Marino 	 conflict?
1564c50c785cSJohn Marino 	 E.g., what if an outer typedef marks the type as class_1 and an inner
1565c50c785cSJohn Marino 	 typedef marks the type as class_2?
1566c50c785cSJohn Marino 	 This is the wrong place to do such error checking.  We leave it to
1567c50c785cSJohn Marino 	 the code that created the typedef in the first place to flag the
1568c50c785cSJohn Marino 	 error.  We just pick the outer address space (akin to letting the
1569c50c785cSJohn Marino 	 outer cast in a chain of casting win), instead of assuming
1570c50c785cSJohn Marino 	 "it can't happen".  */
1571c50c785cSJohn Marino       {
1572c50c785cSJohn Marino 	const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
1573c50c785cSJohn Marino 				| TYPE_INSTANCE_FLAG_DATA_SPACE);
1574c50c785cSJohn Marino 	const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
1575c50c785cSJohn Marino 	int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
1576c50c785cSJohn Marino 
1577c50c785cSJohn Marino 	/* Treat code vs data spaces and address classes separately.  */
1578c50c785cSJohn Marino 	if ((instance_flags & ALL_SPACES) != 0)
1579c50c785cSJohn Marino 	  new_instance_flags &= ~ALL_SPACES;
1580c50c785cSJohn Marino 	if ((instance_flags & ALL_CLASSES) != 0)
1581c50c785cSJohn Marino 	  new_instance_flags &= ~ALL_CLASSES;
1582c50c785cSJohn Marino 
1583c50c785cSJohn Marino 	instance_flags |= new_instance_flags;
1584c50c785cSJohn Marino       }
1585c50c785cSJohn Marino     }
15865796c8dcSSimon Schubert 
15875796c8dcSSimon Schubert   /* If this is a struct/class/union with no fields, then check
15885796c8dcSSimon Schubert      whether a full definition exists somewhere else.  This is for
15895796c8dcSSimon Schubert      systems where a type definition with no fields is issued for such
15905796c8dcSSimon Schubert      types, instead of identifying them as stub types in the first
15915796c8dcSSimon Schubert      place.  */
15925796c8dcSSimon Schubert 
15935796c8dcSSimon Schubert   if (TYPE_IS_OPAQUE (type)
15945796c8dcSSimon Schubert       && opaque_type_resolution
15955796c8dcSSimon Schubert       && !currently_reading_symtab)
15965796c8dcSSimon Schubert     {
1597*ef5ccd6cSJohn Marino       const char *name = type_name_no_tag (type);
15985796c8dcSSimon Schubert       struct type *newtype;
1599cf7f2e2dSJohn Marino 
16005796c8dcSSimon Schubert       if (name == NULL)
16015796c8dcSSimon Schubert 	{
16025796c8dcSSimon Schubert 	  stub_noname_complaint ();
1603c50c785cSJohn Marino 	  return make_qualified_type (type, instance_flags, NULL);
16045796c8dcSSimon Schubert 	}
16055796c8dcSSimon Schubert       newtype = lookup_transparent_type (name);
16065796c8dcSSimon Schubert 
16075796c8dcSSimon Schubert       if (newtype)
16085796c8dcSSimon Schubert 	{
16095796c8dcSSimon Schubert 	  /* If the resolved type and the stub are in the same
16105796c8dcSSimon Schubert 	     objfile, then replace the stub type with the real deal.
16115796c8dcSSimon Schubert 	     But if they're in separate objfiles, leave the stub
16125796c8dcSSimon Schubert 	     alone; we'll just look up the transparent type every time
16135796c8dcSSimon Schubert 	     we call check_typedef.  We can't create pointers between
16145796c8dcSSimon Schubert 	     types allocated to different objfiles, since they may
16155796c8dcSSimon Schubert 	     have different lifetimes.  Trying to copy NEWTYPE over to
16165796c8dcSSimon Schubert 	     TYPE's objfile is pointless, too, since you'll have to
16175796c8dcSSimon Schubert 	     move over any other types NEWTYPE refers to, which could
16185796c8dcSSimon Schubert 	     be an unbounded amount of stuff.  */
16195796c8dcSSimon Schubert 	  if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1620c50c785cSJohn Marino 	    type = make_qualified_type (newtype,
1621c50c785cSJohn Marino 					TYPE_INSTANCE_FLAGS (type),
1622c50c785cSJohn Marino 					type);
16235796c8dcSSimon Schubert 	  else
16245796c8dcSSimon Schubert 	    type = newtype;
16255796c8dcSSimon Schubert 	}
16265796c8dcSSimon Schubert     }
16275796c8dcSSimon Schubert   /* Otherwise, rely on the stub flag being set for opaque/stubbed
16285796c8dcSSimon Schubert      types.  */
16295796c8dcSSimon Schubert   else if (TYPE_STUB (type) && !currently_reading_symtab)
16305796c8dcSSimon Schubert     {
1631*ef5ccd6cSJohn Marino       const char *name = type_name_no_tag (type);
16325796c8dcSSimon Schubert       /* FIXME: shouldn't we separately check the TYPE_NAME and the
16335796c8dcSSimon Schubert          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
16345796c8dcSSimon Schubert          as appropriate?  (this code was written before TYPE_NAME and
16355796c8dcSSimon Schubert          TYPE_TAG_NAME were separate).  */
16365796c8dcSSimon Schubert       struct symbol *sym;
1637cf7f2e2dSJohn Marino 
16385796c8dcSSimon Schubert       if (name == NULL)
16395796c8dcSSimon Schubert 	{
16405796c8dcSSimon Schubert 	  stub_noname_complaint ();
1641c50c785cSJohn Marino 	  return make_qualified_type (type, instance_flags, NULL);
16425796c8dcSSimon Schubert 	}
16435796c8dcSSimon Schubert       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
16445796c8dcSSimon Schubert       if (sym)
16455796c8dcSSimon Schubert         {
16465796c8dcSSimon Schubert           /* Same as above for opaque types, we can replace the stub
1647c50c785cSJohn Marino              with the complete type only if they are in the same
16485796c8dcSSimon Schubert              objfile.  */
16495796c8dcSSimon Schubert 	  if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1650c50c785cSJohn Marino             type = make_qualified_type (SYMBOL_TYPE (sym),
1651c50c785cSJohn Marino 					TYPE_INSTANCE_FLAGS (type),
1652c50c785cSJohn Marino 					type);
16535796c8dcSSimon Schubert 	  else
16545796c8dcSSimon Schubert 	    type = SYMBOL_TYPE (sym);
16555796c8dcSSimon Schubert         }
16565796c8dcSSimon Schubert     }
16575796c8dcSSimon Schubert 
16585796c8dcSSimon Schubert   if (TYPE_TARGET_STUB (type))
16595796c8dcSSimon Schubert     {
16605796c8dcSSimon Schubert       struct type *range_type;
16615796c8dcSSimon Schubert       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
16625796c8dcSSimon Schubert 
16635796c8dcSSimon Schubert       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
16645796c8dcSSimon Schubert 	{
1665c50c785cSJohn Marino 	  /* Nothing we can do.  */
16665796c8dcSSimon Schubert 	}
16675796c8dcSSimon Schubert       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
16685796c8dcSSimon Schubert 	       && TYPE_NFIELDS (type) == 1
16695796c8dcSSimon Schubert 	       && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
16705796c8dcSSimon Schubert 		   == TYPE_CODE_RANGE))
16715796c8dcSSimon Schubert 	{
16725796c8dcSSimon Schubert 	  /* Now recompute the length of the array type, based on its
16735796c8dcSSimon Schubert 	     number of elements and the target type's length.
16745796c8dcSSimon Schubert 	     Watch out for Ada null Ada arrays where the high bound
16755796c8dcSSimon Schubert 	     is smaller than the low bound.  */
1676cf7f2e2dSJohn Marino 	  const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
1677cf7f2e2dSJohn Marino 	  const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
1678cf7f2e2dSJohn Marino 	  ULONGEST len;
16795796c8dcSSimon Schubert 
16805796c8dcSSimon Schubert 	  if (high_bound < low_bound)
1681cf7f2e2dSJohn Marino 	    len = 0;
16825796c8dcSSimon Schubert 	  else
1683cf7f2e2dSJohn Marino 	    {
1684cf7f2e2dSJohn Marino 	      /* For now, we conservatively take the array length to be 0
1685cf7f2e2dSJohn Marino 		 if its length exceeds UINT_MAX.  The code below assumes
1686cf7f2e2dSJohn Marino 		 that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
1687cf7f2e2dSJohn Marino 		 which is technically not guaranteed by C, but is usually true
1688cf7f2e2dSJohn Marino 		 (because it would be true if x were unsigned with its
1689cf7f2e2dSJohn Marino 		 high-order bit on).  It uses the fact that
1690cf7f2e2dSJohn Marino 		 high_bound-low_bound is always representable in
1691cf7f2e2dSJohn Marino 		 ULONGEST and that if high_bound-low_bound+1 overflows,
1692cf7f2e2dSJohn Marino 		 it overflows to 0.  We must change these tests if we
1693cf7f2e2dSJohn Marino 		 decide to increase the representation of TYPE_LENGTH
1694cf7f2e2dSJohn Marino 		 from unsigned int to ULONGEST.  */
1695cf7f2e2dSJohn Marino 	      ULONGEST ulow = low_bound, uhigh = high_bound;
1696cf7f2e2dSJohn Marino 	      ULONGEST tlen = TYPE_LENGTH (target_type);
16975796c8dcSSimon Schubert 
1698cf7f2e2dSJohn Marino 	      len = tlen * (uhigh - ulow + 1);
1699cf7f2e2dSJohn Marino 	      if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh
1700cf7f2e2dSJohn Marino 		  || len > UINT_MAX)
1701cf7f2e2dSJohn Marino 		len = 0;
1702cf7f2e2dSJohn Marino 	    }
1703cf7f2e2dSJohn Marino 	  TYPE_LENGTH (type) = len;
17045796c8dcSSimon Schubert 	  TYPE_TARGET_STUB (type) = 0;
17055796c8dcSSimon Schubert 	}
17065796c8dcSSimon Schubert       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
17075796c8dcSSimon Schubert 	{
17085796c8dcSSimon Schubert 	  TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
17095796c8dcSSimon Schubert 	  TYPE_TARGET_STUB (type) = 0;
17105796c8dcSSimon Schubert 	}
17115796c8dcSSimon Schubert     }
1712c50c785cSJohn Marino 
1713c50c785cSJohn Marino   type = make_qualified_type (type, instance_flags, NULL);
1714c50c785cSJohn Marino 
17155796c8dcSSimon Schubert   /* Cache TYPE_LENGTH for future use.  */
17165796c8dcSSimon Schubert   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1717c50c785cSJohn Marino 
17185796c8dcSSimon Schubert   return type;
17195796c8dcSSimon Schubert }
17205796c8dcSSimon Schubert 
17215796c8dcSSimon Schubert /* Parse a type expression in the string [P..P+LENGTH).  If an error
17225796c8dcSSimon Schubert    occurs, silently return a void type.  */
17235796c8dcSSimon Schubert 
17245796c8dcSSimon Schubert static struct type *
safe_parse_type(struct gdbarch * gdbarch,char * p,int length)17255796c8dcSSimon Schubert safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
17265796c8dcSSimon Schubert {
17275796c8dcSSimon Schubert   struct ui_file *saved_gdb_stderr;
1728*ef5ccd6cSJohn Marino   struct type *type = NULL; /* Initialize to keep gcc happy.  */
1729*ef5ccd6cSJohn Marino   volatile struct gdb_exception except;
17305796c8dcSSimon Schubert 
17315796c8dcSSimon Schubert   /* Suppress error messages.  */
17325796c8dcSSimon Schubert   saved_gdb_stderr = gdb_stderr;
17335796c8dcSSimon Schubert   gdb_stderr = ui_file_new ();
17345796c8dcSSimon Schubert 
17355796c8dcSSimon Schubert   /* Call parse_and_eval_type() without fear of longjmp()s.  */
1736*ef5ccd6cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
1737*ef5ccd6cSJohn Marino     {
1738*ef5ccd6cSJohn Marino       type = parse_and_eval_type (p, length);
1739*ef5ccd6cSJohn Marino     }
1740*ef5ccd6cSJohn Marino 
1741*ef5ccd6cSJohn Marino   if (except.reason < 0)
17425796c8dcSSimon Schubert     type = builtin_type (gdbarch)->builtin_void;
17435796c8dcSSimon Schubert 
17445796c8dcSSimon Schubert   /* Stop suppressing error messages.  */
17455796c8dcSSimon Schubert   ui_file_delete (gdb_stderr);
17465796c8dcSSimon Schubert   gdb_stderr = saved_gdb_stderr;
17475796c8dcSSimon Schubert 
17485796c8dcSSimon Schubert   return type;
17495796c8dcSSimon Schubert }
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert /* Ugly hack to convert method stubs into method types.
17525796c8dcSSimon Schubert 
17535796c8dcSSimon Schubert    He ain't kiddin'.  This demangles the name of the method into a
17545796c8dcSSimon Schubert    string including argument types, parses out each argument type,
17555796c8dcSSimon Schubert    generates a string casting a zero to that type, evaluates the
17565796c8dcSSimon Schubert    string, and stuffs the resulting type into an argtype vector!!!
17575796c8dcSSimon Schubert    Then it knows the type of the whole function (including argument
17585796c8dcSSimon Schubert    types for overloading), which info used to be in the stab's but was
17595796c8dcSSimon Schubert    removed to hack back the space required for them.  */
17605796c8dcSSimon Schubert 
17615796c8dcSSimon Schubert static void
check_stub_method(struct type * type,int method_id,int signature_id)17625796c8dcSSimon Schubert check_stub_method (struct type *type, int method_id, int signature_id)
17635796c8dcSSimon Schubert {
17645796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
17655796c8dcSSimon Schubert   struct fn_field *f;
17665796c8dcSSimon Schubert   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
17675796c8dcSSimon Schubert   char *demangled_name = cplus_demangle (mangled_name,
17685796c8dcSSimon Schubert 					 DMGL_PARAMS | DMGL_ANSI);
17695796c8dcSSimon Schubert   char *argtypetext, *p;
17705796c8dcSSimon Schubert   int depth = 0, argcount = 1;
17715796c8dcSSimon Schubert   struct field *argtypes;
17725796c8dcSSimon Schubert   struct type *mtype;
17735796c8dcSSimon Schubert 
17745796c8dcSSimon Schubert   /* Make sure we got back a function string that we can use.  */
17755796c8dcSSimon Schubert   if (demangled_name)
17765796c8dcSSimon Schubert     p = strchr (demangled_name, '(');
17775796c8dcSSimon Schubert   else
17785796c8dcSSimon Schubert     p = NULL;
17795796c8dcSSimon Schubert 
17805796c8dcSSimon Schubert   if (demangled_name == NULL || p == NULL)
17815796c8dcSSimon Schubert     error (_("Internal: Cannot demangle mangled name `%s'."),
17825796c8dcSSimon Schubert 	   mangled_name);
17835796c8dcSSimon Schubert 
17845796c8dcSSimon Schubert   /* Now, read in the parameters that define this type.  */
17855796c8dcSSimon Schubert   p += 1;
17865796c8dcSSimon Schubert   argtypetext = p;
17875796c8dcSSimon Schubert   while (*p)
17885796c8dcSSimon Schubert     {
17895796c8dcSSimon Schubert       if (*p == '(' || *p == '<')
17905796c8dcSSimon Schubert 	{
17915796c8dcSSimon Schubert 	  depth += 1;
17925796c8dcSSimon Schubert 	}
17935796c8dcSSimon Schubert       else if (*p == ')' || *p == '>')
17945796c8dcSSimon Schubert 	{
17955796c8dcSSimon Schubert 	  depth -= 1;
17965796c8dcSSimon Schubert 	}
17975796c8dcSSimon Schubert       else if (*p == ',' && depth == 0)
17985796c8dcSSimon Schubert 	{
17995796c8dcSSimon Schubert 	  argcount += 1;
18005796c8dcSSimon Schubert 	}
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert       p += 1;
18035796c8dcSSimon Schubert     }
18045796c8dcSSimon Schubert 
18055796c8dcSSimon Schubert   /* If we read one argument and it was ``void'', don't count it.  */
18065796c8dcSSimon Schubert   if (strncmp (argtypetext, "(void)", 6) == 0)
18075796c8dcSSimon Schubert     argcount -= 1;
18085796c8dcSSimon Schubert 
18095796c8dcSSimon Schubert   /* We need one extra slot, for the THIS pointer.  */
18105796c8dcSSimon Schubert 
18115796c8dcSSimon Schubert   argtypes = (struct field *)
18125796c8dcSSimon Schubert     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
18135796c8dcSSimon Schubert   p = argtypetext;
18145796c8dcSSimon Schubert 
18155796c8dcSSimon Schubert   /* Add THIS pointer for non-static methods.  */
18165796c8dcSSimon Schubert   f = TYPE_FN_FIELDLIST1 (type, method_id);
18175796c8dcSSimon Schubert   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
18185796c8dcSSimon Schubert     argcount = 0;
18195796c8dcSSimon Schubert   else
18205796c8dcSSimon Schubert     {
18215796c8dcSSimon Schubert       argtypes[0].type = lookup_pointer_type (type);
18225796c8dcSSimon Schubert       argcount = 1;
18235796c8dcSSimon Schubert     }
18245796c8dcSSimon Schubert 
1825c50c785cSJohn Marino   if (*p != ')')		/* () means no args, skip while.  */
18265796c8dcSSimon Schubert     {
18275796c8dcSSimon Schubert       depth = 0;
18285796c8dcSSimon Schubert       while (*p)
18295796c8dcSSimon Schubert 	{
18305796c8dcSSimon Schubert 	  if (depth <= 0 && (*p == ',' || *p == ')'))
18315796c8dcSSimon Schubert 	    {
18325796c8dcSSimon Schubert 	      /* Avoid parsing of ellipsis, they will be handled below.
18335796c8dcSSimon Schubert 	         Also avoid ``void'' as above.  */
18345796c8dcSSimon Schubert 	      if (strncmp (argtypetext, "...", p - argtypetext) != 0
18355796c8dcSSimon Schubert 		  && strncmp (argtypetext, "void", p - argtypetext) != 0)
18365796c8dcSSimon Schubert 		{
18375796c8dcSSimon Schubert 		  argtypes[argcount].type =
18385796c8dcSSimon Schubert 		    safe_parse_type (gdbarch, argtypetext, p - argtypetext);
18395796c8dcSSimon Schubert 		  argcount += 1;
18405796c8dcSSimon Schubert 		}
18415796c8dcSSimon Schubert 	      argtypetext = p + 1;
18425796c8dcSSimon Schubert 	    }
18435796c8dcSSimon Schubert 
18445796c8dcSSimon Schubert 	  if (*p == '(' || *p == '<')
18455796c8dcSSimon Schubert 	    {
18465796c8dcSSimon Schubert 	      depth += 1;
18475796c8dcSSimon Schubert 	    }
18485796c8dcSSimon Schubert 	  else if (*p == ')' || *p == '>')
18495796c8dcSSimon Schubert 	    {
18505796c8dcSSimon Schubert 	      depth -= 1;
18515796c8dcSSimon Schubert 	    }
18525796c8dcSSimon Schubert 
18535796c8dcSSimon Schubert 	  p += 1;
18545796c8dcSSimon Schubert 	}
18555796c8dcSSimon Schubert     }
18565796c8dcSSimon Schubert 
18575796c8dcSSimon Schubert   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert   /* Now update the old "stub" type into a real type.  */
18605796c8dcSSimon Schubert   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
18615796c8dcSSimon Schubert   TYPE_DOMAIN_TYPE (mtype) = type;
18625796c8dcSSimon Schubert   TYPE_FIELDS (mtype) = argtypes;
18635796c8dcSSimon Schubert   TYPE_NFIELDS (mtype) = argcount;
18645796c8dcSSimon Schubert   TYPE_STUB (mtype) = 0;
18655796c8dcSSimon Schubert   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
18665796c8dcSSimon Schubert   if (p[-2] == '.')
18675796c8dcSSimon Schubert     TYPE_VARARGS (mtype) = 1;
18685796c8dcSSimon Schubert 
18695796c8dcSSimon Schubert   xfree (demangled_name);
18705796c8dcSSimon Schubert }
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert /* This is the external interface to check_stub_method, above.  This
18735796c8dcSSimon Schubert    function unstubs all of the signatures for TYPE's METHOD_ID method
18745796c8dcSSimon Schubert    name.  After calling this function TYPE_FN_FIELD_STUB will be
18755796c8dcSSimon Schubert    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
18765796c8dcSSimon Schubert    correct.
18775796c8dcSSimon Schubert 
18785796c8dcSSimon Schubert    This function unfortunately can not die until stabs do.  */
18795796c8dcSSimon Schubert 
18805796c8dcSSimon Schubert void
check_stub_method_group(struct type * type,int method_id)18815796c8dcSSimon Schubert check_stub_method_group (struct type *type, int method_id)
18825796c8dcSSimon Schubert {
18835796c8dcSSimon Schubert   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
18845796c8dcSSimon Schubert   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
18855796c8dcSSimon Schubert   int j, found_stub = 0;
18865796c8dcSSimon Schubert 
18875796c8dcSSimon Schubert   for (j = 0; j < len; j++)
18885796c8dcSSimon Schubert     if (TYPE_FN_FIELD_STUB (f, j))
18895796c8dcSSimon Schubert       {
18905796c8dcSSimon Schubert 	found_stub = 1;
18915796c8dcSSimon Schubert 	check_stub_method (type, method_id, j);
18925796c8dcSSimon Schubert       }
18935796c8dcSSimon Schubert 
18945796c8dcSSimon Schubert   /* GNU v3 methods with incorrect names were corrected when we read
18955796c8dcSSimon Schubert      in type information, because it was cheaper to do it then.  The
18965796c8dcSSimon Schubert      only GNU v2 methods with incorrect method names are operators and
18975796c8dcSSimon Schubert      destructors; destructors were also corrected when we read in type
18985796c8dcSSimon Schubert      information.
18995796c8dcSSimon Schubert 
19005796c8dcSSimon Schubert      Therefore the only thing we need to handle here are v2 operator
19015796c8dcSSimon Schubert      names.  */
19025796c8dcSSimon Schubert   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
19035796c8dcSSimon Schubert     {
19045796c8dcSSimon Schubert       int ret;
19055796c8dcSSimon Schubert       char dem_opname[256];
19065796c8dcSSimon Schubert 
19075796c8dcSSimon Schubert       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
19085796c8dcSSimon Schubert 							   method_id),
19095796c8dcSSimon Schubert 				   dem_opname, DMGL_ANSI);
19105796c8dcSSimon Schubert       if (!ret)
19115796c8dcSSimon Schubert 	ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
19125796c8dcSSimon Schubert 							     method_id),
19135796c8dcSSimon Schubert 				     dem_opname, 0);
19145796c8dcSSimon Schubert       if (ret)
19155796c8dcSSimon Schubert 	TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
19165796c8dcSSimon Schubert     }
19175796c8dcSSimon Schubert }
19185796c8dcSSimon Schubert 
1919cf7f2e2dSJohn Marino /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
1920cf7f2e2dSJohn Marino const struct cplus_struct_type cplus_struct_default = { };
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert void
allocate_cplus_struct_type(struct type * type)19235796c8dcSSimon Schubert allocate_cplus_struct_type (struct type *type)
19245796c8dcSSimon Schubert {
1925cf7f2e2dSJohn Marino   if (HAVE_CPLUS_STRUCT (type))
1926cf7f2e2dSJohn Marino     /* Structure was already allocated.  Nothing more to do.  */
1927cf7f2e2dSJohn Marino     return;
1928cf7f2e2dSJohn Marino 
1929cf7f2e2dSJohn Marino   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
1930cf7f2e2dSJohn Marino   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
19315796c8dcSSimon Schubert     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1932cf7f2e2dSJohn Marino   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
19335796c8dcSSimon Schubert }
1934cf7f2e2dSJohn Marino 
1935cf7f2e2dSJohn Marino const struct gnat_aux_type gnat_aux_default =
1936cf7f2e2dSJohn Marino   { NULL };
1937cf7f2e2dSJohn Marino 
1938cf7f2e2dSJohn Marino /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
1939cf7f2e2dSJohn Marino    and allocate the associated gnat-specific data.  The gnat-specific
1940cf7f2e2dSJohn Marino    data is also initialized to gnat_aux_default.  */
1941*ef5ccd6cSJohn Marino 
1942cf7f2e2dSJohn Marino void
allocate_gnat_aux_type(struct type * type)1943cf7f2e2dSJohn Marino allocate_gnat_aux_type (struct type *type)
1944cf7f2e2dSJohn Marino {
1945cf7f2e2dSJohn Marino   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
1946cf7f2e2dSJohn Marino   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
1947cf7f2e2dSJohn Marino     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
1948cf7f2e2dSJohn Marino   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
19495796c8dcSSimon Schubert }
19505796c8dcSSimon Schubert 
19515796c8dcSSimon Schubert /* Helper function to initialize the standard scalar types.
19525796c8dcSSimon Schubert 
1953*ef5ccd6cSJohn Marino    If NAME is non-NULL, then it is used to initialize the type name.
1954*ef5ccd6cSJohn Marino    Note that NAME is not copied; it is required to have a lifetime at
1955*ef5ccd6cSJohn Marino    least as long as OBJFILE.  */
19565796c8dcSSimon Schubert 
19575796c8dcSSimon Schubert struct type *
init_type(enum type_code code,int length,int flags,const char * name,struct objfile * objfile)19585796c8dcSSimon Schubert init_type (enum type_code code, int length, int flags,
1959*ef5ccd6cSJohn Marino 	   const char *name, struct objfile *objfile)
19605796c8dcSSimon Schubert {
19615796c8dcSSimon Schubert   struct type *type;
19625796c8dcSSimon Schubert 
19635796c8dcSSimon Schubert   type = alloc_type (objfile);
19645796c8dcSSimon Schubert   TYPE_CODE (type) = code;
19655796c8dcSSimon Schubert   TYPE_LENGTH (type) = length;
19665796c8dcSSimon Schubert 
19675796c8dcSSimon Schubert   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
19685796c8dcSSimon Schubert   if (flags & TYPE_FLAG_UNSIGNED)
19695796c8dcSSimon Schubert     TYPE_UNSIGNED (type) = 1;
19705796c8dcSSimon Schubert   if (flags & TYPE_FLAG_NOSIGN)
19715796c8dcSSimon Schubert     TYPE_NOSIGN (type) = 1;
19725796c8dcSSimon Schubert   if (flags & TYPE_FLAG_STUB)
19735796c8dcSSimon Schubert     TYPE_STUB (type) = 1;
19745796c8dcSSimon Schubert   if (flags & TYPE_FLAG_TARGET_STUB)
19755796c8dcSSimon Schubert     TYPE_TARGET_STUB (type) = 1;
19765796c8dcSSimon Schubert   if (flags & TYPE_FLAG_STATIC)
19775796c8dcSSimon Schubert     TYPE_STATIC (type) = 1;
19785796c8dcSSimon Schubert   if (flags & TYPE_FLAG_PROTOTYPED)
19795796c8dcSSimon Schubert     TYPE_PROTOTYPED (type) = 1;
19805796c8dcSSimon Schubert   if (flags & TYPE_FLAG_INCOMPLETE)
19815796c8dcSSimon Schubert     TYPE_INCOMPLETE (type) = 1;
19825796c8dcSSimon Schubert   if (flags & TYPE_FLAG_VARARGS)
19835796c8dcSSimon Schubert     TYPE_VARARGS (type) = 1;
19845796c8dcSSimon Schubert   if (flags & TYPE_FLAG_VECTOR)
19855796c8dcSSimon Schubert     TYPE_VECTOR (type) = 1;
19865796c8dcSSimon Schubert   if (flags & TYPE_FLAG_STUB_SUPPORTED)
19875796c8dcSSimon Schubert     TYPE_STUB_SUPPORTED (type) = 1;
19885796c8dcSSimon Schubert   if (flags & TYPE_FLAG_FIXED_INSTANCE)
19895796c8dcSSimon Schubert     TYPE_FIXED_INSTANCE (type) = 1;
1990c50c785cSJohn Marino   if (flags & TYPE_FLAG_GNU_IFUNC)
1991c50c785cSJohn Marino     TYPE_GNU_IFUNC (type) = 1;
19925796c8dcSSimon Schubert 
1993*ef5ccd6cSJohn Marino   TYPE_NAME (type) = name;
19945796c8dcSSimon Schubert 
19955796c8dcSSimon Schubert   /* C++ fancies.  */
19965796c8dcSSimon Schubert 
19975796c8dcSSimon Schubert   if (name && strcmp (name, "char") == 0)
19985796c8dcSSimon Schubert     TYPE_NOSIGN (type) = 1;
19995796c8dcSSimon Schubert 
2000cf7f2e2dSJohn Marino   switch (code)
20015796c8dcSSimon Schubert     {
2002cf7f2e2dSJohn Marino       case TYPE_CODE_STRUCT:
2003cf7f2e2dSJohn Marino       case TYPE_CODE_UNION:
2004cf7f2e2dSJohn Marino       case TYPE_CODE_NAMESPACE:
20055796c8dcSSimon Schubert         INIT_CPLUS_SPECIFIC (type);
2006cf7f2e2dSJohn Marino         break;
2007cf7f2e2dSJohn Marino       case TYPE_CODE_FLT:
2008cf7f2e2dSJohn Marino         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2009cf7f2e2dSJohn Marino         break;
2010cf7f2e2dSJohn Marino       case TYPE_CODE_FUNC:
2011a45ae5f8SJohn Marino 	INIT_FUNC_SPECIFIC (type);
2012cf7f2e2dSJohn Marino         break;
20135796c8dcSSimon Schubert     }
20145796c8dcSSimon Schubert   return type;
20155796c8dcSSimon Schubert }
2016*ef5ccd6cSJohn Marino 
2017*ef5ccd6cSJohn Marino /* Queries on types.  */
20185796c8dcSSimon Schubert 
20195796c8dcSSimon Schubert int
can_dereference(struct type * t)20205796c8dcSSimon Schubert can_dereference (struct type *t)
20215796c8dcSSimon Schubert {
20225796c8dcSSimon Schubert   /* FIXME: Should we return true for references as well as
20235796c8dcSSimon Schubert      pointers?  */
20245796c8dcSSimon Schubert   CHECK_TYPEDEF (t);
20255796c8dcSSimon Schubert   return
20265796c8dcSSimon Schubert     (t != NULL
20275796c8dcSSimon Schubert      && TYPE_CODE (t) == TYPE_CODE_PTR
20285796c8dcSSimon Schubert      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
20295796c8dcSSimon Schubert }
20305796c8dcSSimon Schubert 
20315796c8dcSSimon Schubert int
is_integral_type(struct type * t)20325796c8dcSSimon Schubert is_integral_type (struct type *t)
20335796c8dcSSimon Schubert {
20345796c8dcSSimon Schubert   CHECK_TYPEDEF (t);
20355796c8dcSSimon Schubert   return
20365796c8dcSSimon Schubert     ((t != NULL)
20375796c8dcSSimon Schubert      && ((TYPE_CODE (t) == TYPE_CODE_INT)
20385796c8dcSSimon Schubert 	 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
20395796c8dcSSimon Schubert 	 || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
20405796c8dcSSimon Schubert 	 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
20415796c8dcSSimon Schubert 	 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
20425796c8dcSSimon Schubert 	 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
20435796c8dcSSimon Schubert }
20445796c8dcSSimon Schubert 
2045c50c785cSJohn Marino /* Return true if TYPE is scalar.  */
2046c50c785cSJohn Marino 
2047c50c785cSJohn Marino static int
is_scalar_type(struct type * type)2048c50c785cSJohn Marino is_scalar_type (struct type *type)
2049c50c785cSJohn Marino {
2050c50c785cSJohn Marino   CHECK_TYPEDEF (type);
2051c50c785cSJohn Marino 
2052c50c785cSJohn Marino   switch (TYPE_CODE (type))
2053c50c785cSJohn Marino     {
2054c50c785cSJohn Marino     case TYPE_CODE_ARRAY:
2055c50c785cSJohn Marino     case TYPE_CODE_STRUCT:
2056c50c785cSJohn Marino     case TYPE_CODE_UNION:
2057c50c785cSJohn Marino     case TYPE_CODE_SET:
2058c50c785cSJohn Marino     case TYPE_CODE_STRING:
2059c50c785cSJohn Marino       return 0;
2060c50c785cSJohn Marino     default:
2061c50c785cSJohn Marino       return 1;
2062c50c785cSJohn Marino     }
2063c50c785cSJohn Marino }
2064c50c785cSJohn Marino 
2065c50c785cSJohn Marino /* Return true if T is scalar, or a composite type which in practice has
2066c50c785cSJohn Marino    the memory layout of a scalar type.  E.g., an array or struct with only
2067c50c785cSJohn Marino    one scalar element inside it, or a union with only scalar elements.  */
2068c50c785cSJohn Marino 
2069c50c785cSJohn Marino int
is_scalar_type_recursive(struct type * t)2070c50c785cSJohn Marino is_scalar_type_recursive (struct type *t)
2071c50c785cSJohn Marino {
2072c50c785cSJohn Marino   CHECK_TYPEDEF (t);
2073c50c785cSJohn Marino 
2074c50c785cSJohn Marino   if (is_scalar_type (t))
2075c50c785cSJohn Marino     return 1;
2076c50c785cSJohn Marino   /* Are we dealing with an array or string of known dimensions?  */
2077c50c785cSJohn Marino   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2078c50c785cSJohn Marino 	    || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2079c50c785cSJohn Marino 	   && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2080c50c785cSJohn Marino     {
2081c50c785cSJohn Marino       LONGEST low_bound, high_bound;
2082c50c785cSJohn Marino       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2083c50c785cSJohn Marino 
2084c50c785cSJohn Marino       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2085c50c785cSJohn Marino 
2086c50c785cSJohn Marino       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2087c50c785cSJohn Marino     }
2088c50c785cSJohn Marino   /* Are we dealing with a struct with one element?  */
2089c50c785cSJohn Marino   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2090c50c785cSJohn Marino     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2091c50c785cSJohn Marino   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2092c50c785cSJohn Marino     {
2093c50c785cSJohn Marino       int i, n = TYPE_NFIELDS (t);
2094c50c785cSJohn Marino 
2095c50c785cSJohn Marino       /* If all elements of the union are scalar, then the union is scalar.  */
2096c50c785cSJohn Marino       for (i = 0; i < n; i++)
2097c50c785cSJohn Marino 	if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2098c50c785cSJohn Marino 	  return 0;
2099c50c785cSJohn Marino 
2100c50c785cSJohn Marino       return 1;
2101c50c785cSJohn Marino     }
2102c50c785cSJohn Marino 
2103c50c785cSJohn Marino   return 0;
2104c50c785cSJohn Marino }
2105c50c785cSJohn Marino 
2106cf7f2e2dSJohn Marino /* A helper function which returns true if types A and B represent the
2107cf7f2e2dSJohn Marino    "same" class type.  This is true if the types have the same main
2108cf7f2e2dSJohn Marino    type, or the same name.  */
2109cf7f2e2dSJohn Marino 
2110cf7f2e2dSJohn Marino int
class_types_same_p(const struct type * a,const struct type * b)2111cf7f2e2dSJohn Marino class_types_same_p (const struct type *a, const struct type *b)
2112cf7f2e2dSJohn Marino {
2113cf7f2e2dSJohn Marino   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2114cf7f2e2dSJohn Marino 	  || (TYPE_NAME (a) && TYPE_NAME (b)
2115cf7f2e2dSJohn Marino 	      && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2116cf7f2e2dSJohn Marino }
2117cf7f2e2dSJohn Marino 
2118c50c785cSJohn Marino /* If BASE is an ancestor of DCLASS return the distance between them.
2119c50c785cSJohn Marino    otherwise return -1;
2120c50c785cSJohn Marino    eg:
21215796c8dcSSimon Schubert 
2122c50c785cSJohn Marino    class A {};
2123c50c785cSJohn Marino    class B: public A {};
2124c50c785cSJohn Marino    class C: public B {};
2125c50c785cSJohn Marino    class D: C {};
2126c50c785cSJohn Marino 
2127c50c785cSJohn Marino    distance_to_ancestor (A, A, 0) = 0
2128c50c785cSJohn Marino    distance_to_ancestor (A, B, 0) = 1
2129c50c785cSJohn Marino    distance_to_ancestor (A, C, 0) = 2
2130c50c785cSJohn Marino    distance_to_ancestor (A, D, 0) = 3
2131c50c785cSJohn Marino 
2132c50c785cSJohn Marino    If PUBLIC is 1 then only public ancestors are considered,
2133c50c785cSJohn Marino    and the function returns the distance only if BASE is a public ancestor
2134c50c785cSJohn Marino    of DCLASS.
2135c50c785cSJohn Marino    Eg:
2136c50c785cSJohn Marino 
2137c50c785cSJohn Marino    distance_to_ancestor (A, D, 1) = -1.  */
2138c50c785cSJohn Marino 
2139c50c785cSJohn Marino static int
distance_to_ancestor(struct type * base,struct type * dclass,int public)2140c50c785cSJohn Marino distance_to_ancestor (struct type *base, struct type *dclass, int public)
21415796c8dcSSimon Schubert {
21425796c8dcSSimon Schubert   int i;
2143c50c785cSJohn Marino   int d;
21445796c8dcSSimon Schubert 
21455796c8dcSSimon Schubert   CHECK_TYPEDEF (base);
21465796c8dcSSimon Schubert   CHECK_TYPEDEF (dclass);
21475796c8dcSSimon Schubert 
2148cf7f2e2dSJohn Marino   if (class_types_same_p (base, dclass))
2149c50c785cSJohn Marino     return 0;
21505796c8dcSSimon Schubert 
21515796c8dcSSimon Schubert   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2152cf7f2e2dSJohn Marino     {
2153c50c785cSJohn Marino       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2154c50c785cSJohn Marino 	continue;
2155c50c785cSJohn Marino 
2156c50c785cSJohn Marino       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2157c50c785cSJohn Marino       if (d >= 0)
2158c50c785cSJohn Marino 	return 1 + d;
2159cf7f2e2dSJohn Marino     }
21605796c8dcSSimon Schubert 
2161c50c785cSJohn Marino   return -1;
2162c50c785cSJohn Marino }
2163c50c785cSJohn Marino 
2164c50c785cSJohn Marino /* Check whether BASE is an ancestor or base class or DCLASS
2165c50c785cSJohn Marino    Return 1 if so, and 0 if not.
2166c50c785cSJohn Marino    Note: If BASE and DCLASS are of the same type, this function
2167c50c785cSJohn Marino    will return 1. So for some class A, is_ancestor (A, A) will
2168c50c785cSJohn Marino    return 1.  */
2169c50c785cSJohn Marino 
2170c50c785cSJohn Marino int
is_ancestor(struct type * base,struct type * dclass)2171c50c785cSJohn Marino is_ancestor (struct type *base, struct type *dclass)
2172c50c785cSJohn Marino {
2173c50c785cSJohn Marino   return distance_to_ancestor (base, dclass, 0) >= 0;
21745796c8dcSSimon Schubert }
2175cf7f2e2dSJohn Marino 
2176cf7f2e2dSJohn Marino /* Like is_ancestor, but only returns true when BASE is a public
2177cf7f2e2dSJohn Marino    ancestor of DCLASS.  */
2178cf7f2e2dSJohn Marino 
2179cf7f2e2dSJohn Marino int
is_public_ancestor(struct type * base,struct type * dclass)2180cf7f2e2dSJohn Marino is_public_ancestor (struct type *base, struct type *dclass)
2181cf7f2e2dSJohn Marino {
2182c50c785cSJohn Marino   return distance_to_ancestor (base, dclass, 1) >= 0;
2183cf7f2e2dSJohn Marino }
2184cf7f2e2dSJohn Marino 
2185cf7f2e2dSJohn Marino /* A helper function for is_unique_ancestor.  */
2186cf7f2e2dSJohn Marino 
2187cf7f2e2dSJohn Marino static int
is_unique_ancestor_worker(struct type * base,struct type * dclass,int * offset,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct value * val)2188cf7f2e2dSJohn Marino is_unique_ancestor_worker (struct type *base, struct type *dclass,
2189cf7f2e2dSJohn Marino 			   int *offset,
2190c50c785cSJohn Marino 			   const gdb_byte *valaddr, int embedded_offset,
2191c50c785cSJohn Marino 			   CORE_ADDR address, struct value *val)
2192cf7f2e2dSJohn Marino {
2193cf7f2e2dSJohn Marino   int i, count = 0;
2194cf7f2e2dSJohn Marino 
2195cf7f2e2dSJohn Marino   CHECK_TYPEDEF (base);
2196cf7f2e2dSJohn Marino   CHECK_TYPEDEF (dclass);
2197cf7f2e2dSJohn Marino 
2198cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2199cf7f2e2dSJohn Marino     {
2200c50c785cSJohn Marino       struct type *iter;
2201c50c785cSJohn Marino       int this_offset;
2202cf7f2e2dSJohn Marino 
2203c50c785cSJohn Marino       iter = check_typedef (TYPE_BASECLASS (dclass, i));
2204c50c785cSJohn Marino 
2205c50c785cSJohn Marino       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2206c50c785cSJohn Marino 				      address, val);
2207cf7f2e2dSJohn Marino 
2208cf7f2e2dSJohn Marino       if (class_types_same_p (base, iter))
2209cf7f2e2dSJohn Marino 	{
2210cf7f2e2dSJohn Marino 	  /* If this is the first subclass, set *OFFSET and set count
2211cf7f2e2dSJohn Marino 	     to 1.  Otherwise, if this is at the same offset as
2212cf7f2e2dSJohn Marino 	     previous instances, do nothing.  Otherwise, increment
2213cf7f2e2dSJohn Marino 	     count.  */
2214cf7f2e2dSJohn Marino 	  if (*offset == -1)
2215cf7f2e2dSJohn Marino 	    {
2216cf7f2e2dSJohn Marino 	      *offset = this_offset;
2217cf7f2e2dSJohn Marino 	      count = 1;
2218cf7f2e2dSJohn Marino 	    }
2219cf7f2e2dSJohn Marino 	  else if (this_offset == *offset)
2220cf7f2e2dSJohn Marino 	    {
2221cf7f2e2dSJohn Marino 	      /* Nothing.  */
2222cf7f2e2dSJohn Marino 	    }
2223cf7f2e2dSJohn Marino 	  else
2224cf7f2e2dSJohn Marino 	    ++count;
2225cf7f2e2dSJohn Marino 	}
2226cf7f2e2dSJohn Marino       else
2227cf7f2e2dSJohn Marino 	count += is_unique_ancestor_worker (base, iter, offset,
2228c50c785cSJohn Marino 					    valaddr,
2229c50c785cSJohn Marino 					    embedded_offset + this_offset,
2230c50c785cSJohn Marino 					    address, val);
2231cf7f2e2dSJohn Marino     }
2232cf7f2e2dSJohn Marino 
2233cf7f2e2dSJohn Marino   return count;
2234cf7f2e2dSJohn Marino }
2235cf7f2e2dSJohn Marino 
2236cf7f2e2dSJohn Marino /* Like is_ancestor, but only returns true if BASE is a unique base
2237cf7f2e2dSJohn Marino    class of the type of VAL.  */
2238cf7f2e2dSJohn Marino 
2239cf7f2e2dSJohn Marino int
is_unique_ancestor(struct type * base,struct value * val)2240cf7f2e2dSJohn Marino is_unique_ancestor (struct type *base, struct value *val)
2241cf7f2e2dSJohn Marino {
2242cf7f2e2dSJohn Marino   int offset = -1;
2243cf7f2e2dSJohn Marino 
2244cf7f2e2dSJohn Marino   return is_unique_ancestor_worker (base, value_type (val), &offset,
2245c50c785cSJohn Marino 				    value_contents_for_printing (val),
2246c50c785cSJohn Marino 				    value_embedded_offset (val),
2247c50c785cSJohn Marino 				    value_address (val), val) == 1;
2248cf7f2e2dSJohn Marino }
2249cf7f2e2dSJohn Marino 
22505796c8dcSSimon Schubert 
2251*ef5ccd6cSJohn Marino /* Overload resolution.  */
22525796c8dcSSimon Schubert 
2253c50c785cSJohn Marino /* Return the sum of the rank of A with the rank of B.  */
22545796c8dcSSimon Schubert 
2255c50c785cSJohn Marino struct rank
sum_ranks(struct rank a,struct rank b)2256c50c785cSJohn Marino sum_ranks (struct rank a, struct rank b)
2257c50c785cSJohn Marino {
2258c50c785cSJohn Marino   struct rank c;
2259c50c785cSJohn Marino   c.rank = a.rank + b.rank;
2260c50c785cSJohn Marino   c.subrank = a.subrank + b.subrank;
2261c50c785cSJohn Marino   return c;
2262c50c785cSJohn Marino }
2263c50c785cSJohn Marino 
2264c50c785cSJohn Marino /* Compare rank A and B and return:
2265c50c785cSJohn Marino    0 if a = b
2266c50c785cSJohn Marino    1 if a is better than b
2267c50c785cSJohn Marino   -1 if b is better than a.  */
2268c50c785cSJohn Marino 
2269c50c785cSJohn Marino int
compare_ranks(struct rank a,struct rank b)2270c50c785cSJohn Marino compare_ranks (struct rank a, struct rank b)
2271c50c785cSJohn Marino {
2272c50c785cSJohn Marino   if (a.rank == b.rank)
2273c50c785cSJohn Marino     {
2274c50c785cSJohn Marino       if (a.subrank == b.subrank)
2275c50c785cSJohn Marino 	return 0;
2276c50c785cSJohn Marino       if (a.subrank < b.subrank)
2277c50c785cSJohn Marino 	return 1;
2278c50c785cSJohn Marino       if (a.subrank > b.subrank)
2279c50c785cSJohn Marino 	return -1;
2280c50c785cSJohn Marino     }
2281c50c785cSJohn Marino 
2282c50c785cSJohn Marino   if (a.rank < b.rank)
2283c50c785cSJohn Marino     return 1;
2284c50c785cSJohn Marino 
2285c50c785cSJohn Marino   /* a.rank > b.rank */
2286c50c785cSJohn Marino   return -1;
2287c50c785cSJohn Marino }
2288c50c785cSJohn Marino 
2289c50c785cSJohn Marino /* Functions for overload resolution begin here.  */
22905796c8dcSSimon Schubert 
22915796c8dcSSimon Schubert /* Compare two badness vectors A and B and return the result.
22925796c8dcSSimon Schubert    0 => A and B are identical
22935796c8dcSSimon Schubert    1 => A and B are incomparable
22945796c8dcSSimon Schubert    2 => A is better than B
22955796c8dcSSimon Schubert    3 => A is worse than B  */
22965796c8dcSSimon Schubert 
22975796c8dcSSimon Schubert int
compare_badness(struct badness_vector * a,struct badness_vector * b)22985796c8dcSSimon Schubert compare_badness (struct badness_vector *a, struct badness_vector *b)
22995796c8dcSSimon Schubert {
23005796c8dcSSimon Schubert   int i;
23015796c8dcSSimon Schubert   int tmp;
23025796c8dcSSimon Schubert   short found_pos = 0;		/* any positives in c? */
23035796c8dcSSimon Schubert   short found_neg = 0;		/* any negatives in c? */
23045796c8dcSSimon Schubert 
23055796c8dcSSimon Schubert   /* differing lengths => incomparable */
23065796c8dcSSimon Schubert   if (a->length != b->length)
23075796c8dcSSimon Schubert     return 1;
23085796c8dcSSimon Schubert 
23095796c8dcSSimon Schubert   /* Subtract b from a */
23105796c8dcSSimon Schubert   for (i = 0; i < a->length; i++)
23115796c8dcSSimon Schubert     {
2312c50c785cSJohn Marino       tmp = compare_ranks (b->rank[i], a->rank[i]);
23135796c8dcSSimon Schubert       if (tmp > 0)
23145796c8dcSSimon Schubert 	found_pos = 1;
23155796c8dcSSimon Schubert       else if (tmp < 0)
23165796c8dcSSimon Schubert 	found_neg = 1;
23175796c8dcSSimon Schubert     }
23185796c8dcSSimon Schubert 
23195796c8dcSSimon Schubert   if (found_pos)
23205796c8dcSSimon Schubert     {
23215796c8dcSSimon Schubert       if (found_neg)
23225796c8dcSSimon Schubert 	return 1;		/* incomparable */
23235796c8dcSSimon Schubert       else
23245796c8dcSSimon Schubert 	return 3;		/* A > B */
23255796c8dcSSimon Schubert     }
23265796c8dcSSimon Schubert   else
23275796c8dcSSimon Schubert     /* no positives */
23285796c8dcSSimon Schubert     {
23295796c8dcSSimon Schubert       if (found_neg)
23305796c8dcSSimon Schubert 	return 2;		/* A < B */
23315796c8dcSSimon Schubert       else
23325796c8dcSSimon Schubert 	return 0;		/* A == B */
23335796c8dcSSimon Schubert     }
23345796c8dcSSimon Schubert }
23355796c8dcSSimon Schubert 
23365796c8dcSSimon Schubert /* Rank a function by comparing its parameter types (PARMS, length
23375796c8dcSSimon Schubert    NPARMS), to the types of an argument list (ARGS, length NARGS).
23385796c8dcSSimon Schubert    Return a pointer to a badness vector.  This has NARGS + 1
23395796c8dcSSimon Schubert    entries.  */
23405796c8dcSSimon Schubert 
23415796c8dcSSimon Schubert struct badness_vector *
rank_function(struct type ** parms,int nparms,struct value ** args,int nargs)23425796c8dcSSimon Schubert rank_function (struct type **parms, int nparms,
2343a45ae5f8SJohn Marino 	       struct value **args, int nargs)
23445796c8dcSSimon Schubert {
23455796c8dcSSimon Schubert   int i;
23465796c8dcSSimon Schubert   struct badness_vector *bv;
23475796c8dcSSimon Schubert   int min_len = nparms < nargs ? nparms : nargs;
23485796c8dcSSimon Schubert 
23495796c8dcSSimon Schubert   bv = xmalloc (sizeof (struct badness_vector));
2350c50c785cSJohn Marino   bv->length = nargs + 1;	/* add 1 for the length-match rank.  */
23515796c8dcSSimon Schubert   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
23525796c8dcSSimon Schubert 
23535796c8dcSSimon Schubert   /* First compare the lengths of the supplied lists.
23545796c8dcSSimon Schubert      If there is a mismatch, set it to a high value.  */
23555796c8dcSSimon Schubert 
23565796c8dcSSimon Schubert   /* pai/1997-06-03 FIXME: when we have debug info about default
23575796c8dcSSimon Schubert      arguments and ellipsis parameter lists, we should consider those
23585796c8dcSSimon Schubert      and rank the length-match more finely.  */
23595796c8dcSSimon Schubert 
2360c50c785cSJohn Marino   LENGTH_MATCH (bv) = (nargs != nparms)
2361c50c785cSJohn Marino 		      ? LENGTH_MISMATCH_BADNESS
2362c50c785cSJohn Marino 		      : EXACT_MATCH_BADNESS;
23635796c8dcSSimon Schubert 
2364c50c785cSJohn Marino   /* Now rank all the parameters of the candidate function.  */
23655796c8dcSSimon Schubert   for (i = 1; i <= min_len; i++)
2366a45ae5f8SJohn Marino     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2367a45ae5f8SJohn Marino 				 args[i - 1]);
23685796c8dcSSimon Schubert 
2369c50c785cSJohn Marino   /* If more arguments than parameters, add dummy entries.  */
23705796c8dcSSimon Schubert   for (i = min_len + 1; i <= nargs; i++)
23715796c8dcSSimon Schubert     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
23725796c8dcSSimon Schubert 
23735796c8dcSSimon Schubert   return bv;
23745796c8dcSSimon Schubert }
23755796c8dcSSimon Schubert 
23765796c8dcSSimon Schubert /* Compare the names of two integer types, assuming that any sign
23775796c8dcSSimon Schubert    qualifiers have been checked already.  We do it this way because
23785796c8dcSSimon Schubert    there may be an "int" in the name of one of the types.  */
23795796c8dcSSimon Schubert 
23805796c8dcSSimon Schubert static int
integer_types_same_name_p(const char * first,const char * second)23815796c8dcSSimon Schubert integer_types_same_name_p (const char *first, const char *second)
23825796c8dcSSimon Schubert {
23835796c8dcSSimon Schubert   int first_p, second_p;
23845796c8dcSSimon Schubert 
23855796c8dcSSimon Schubert   /* If both are shorts, return 1; if neither is a short, keep
23865796c8dcSSimon Schubert      checking.  */
23875796c8dcSSimon Schubert   first_p = (strstr (first, "short") != NULL);
23885796c8dcSSimon Schubert   second_p = (strstr (second, "short") != NULL);
23895796c8dcSSimon Schubert   if (first_p && second_p)
23905796c8dcSSimon Schubert     return 1;
23915796c8dcSSimon Schubert   if (first_p || second_p)
23925796c8dcSSimon Schubert     return 0;
23935796c8dcSSimon Schubert 
23945796c8dcSSimon Schubert   /* Likewise for long.  */
23955796c8dcSSimon Schubert   first_p = (strstr (first, "long") != NULL);
23965796c8dcSSimon Schubert   second_p = (strstr (second, "long") != NULL);
23975796c8dcSSimon Schubert   if (first_p && second_p)
23985796c8dcSSimon Schubert     return 1;
23995796c8dcSSimon Schubert   if (first_p || second_p)
24005796c8dcSSimon Schubert     return 0;
24015796c8dcSSimon Schubert 
24025796c8dcSSimon Schubert   /* Likewise for char.  */
24035796c8dcSSimon Schubert   first_p = (strstr (first, "char") != NULL);
24045796c8dcSSimon Schubert   second_p = (strstr (second, "char") != NULL);
24055796c8dcSSimon Schubert   if (first_p && second_p)
24065796c8dcSSimon Schubert     return 1;
24075796c8dcSSimon Schubert   if (first_p || second_p)
24085796c8dcSSimon Schubert     return 0;
24095796c8dcSSimon Schubert 
24105796c8dcSSimon Schubert   /* They must both be ints.  */
24115796c8dcSSimon Schubert   return 1;
24125796c8dcSSimon Schubert }
24135796c8dcSSimon Schubert 
2414c50c785cSJohn Marino /* Compares type A to type B returns 1 if the represent the same type
2415c50c785cSJohn Marino    0 otherwise.  */
2416c50c785cSJohn Marino 
2417*ef5ccd6cSJohn Marino int
types_equal(struct type * a,struct type * b)2418c50c785cSJohn Marino types_equal (struct type *a, struct type *b)
2419c50c785cSJohn Marino {
2420c50c785cSJohn Marino   /* Identical type pointers.  */
2421c50c785cSJohn Marino   /* However, this still doesn't catch all cases of same type for b
2422c50c785cSJohn Marino      and a.  The reason is that builtin types are different from
2423c50c785cSJohn Marino      the same ones constructed from the object.  */
2424c50c785cSJohn Marino   if (a == b)
2425c50c785cSJohn Marino     return 1;
2426c50c785cSJohn Marino 
2427c50c785cSJohn Marino   /* Resolve typedefs */
2428c50c785cSJohn Marino   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2429c50c785cSJohn Marino     a = check_typedef (a);
2430c50c785cSJohn Marino   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2431c50c785cSJohn Marino     b = check_typedef (b);
2432c50c785cSJohn Marino 
2433c50c785cSJohn Marino   /* If after resolving typedefs a and b are not of the same type
2434c50c785cSJohn Marino      code then they are not equal.  */
2435c50c785cSJohn Marino   if (TYPE_CODE (a) != TYPE_CODE (b))
2436c50c785cSJohn Marino     return 0;
2437c50c785cSJohn Marino 
2438c50c785cSJohn Marino   /* If a and b are both pointers types or both reference types then
2439c50c785cSJohn Marino      they are equal of the same type iff the objects they refer to are
2440c50c785cSJohn Marino      of the same type.  */
2441c50c785cSJohn Marino   if (TYPE_CODE (a) == TYPE_CODE_PTR
2442c50c785cSJohn Marino       || TYPE_CODE (a) == TYPE_CODE_REF)
2443c50c785cSJohn Marino     return types_equal (TYPE_TARGET_TYPE (a),
2444c50c785cSJohn Marino                         TYPE_TARGET_TYPE (b));
2445c50c785cSJohn Marino 
2446c50c785cSJohn Marino   /* Well, damnit, if the names are exactly the same, I'll say they
2447c50c785cSJohn Marino      are exactly the same.  This happens when we generate method
2448c50c785cSJohn Marino      stubs.  The types won't point to the same address, but they
2449c50c785cSJohn Marino      really are the same.  */
2450c50c785cSJohn Marino 
2451c50c785cSJohn Marino   if (TYPE_NAME (a) && TYPE_NAME (b)
2452c50c785cSJohn Marino       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2453c50c785cSJohn Marino     return 1;
2454c50c785cSJohn Marino 
2455c50c785cSJohn Marino   /* Check if identical after resolving typedefs.  */
2456c50c785cSJohn Marino   if (a == b)
2457c50c785cSJohn Marino     return 1;
2458c50c785cSJohn Marino 
2459c50c785cSJohn Marino   return 0;
2460c50c785cSJohn Marino }
2461c50c785cSJohn Marino 
24625796c8dcSSimon Schubert /* Compare one type (PARM) for compatibility with another (ARG).
24635796c8dcSSimon Schubert  * PARM is intended to be the parameter type of a function; and
24645796c8dcSSimon Schubert  * ARG is the supplied argument's type.  This function tests if
24655796c8dcSSimon Schubert  * the latter can be converted to the former.
2466a45ae5f8SJohn Marino  * VALUE is the argument's value or NULL if none (or called recursively)
24675796c8dcSSimon Schubert  *
24685796c8dcSSimon Schubert  * Return 0 if they are identical types;
24695796c8dcSSimon Schubert  * Otherwise, return an integer which corresponds to how compatible
24705796c8dcSSimon Schubert  * PARM is to ARG.  The higher the return value, the worse the match.
24715796c8dcSSimon Schubert  * Generally the "bad" conversions are all uniformly assigned a 100.  */
24725796c8dcSSimon Schubert 
2473c50c785cSJohn Marino struct rank
rank_one_type(struct type * parm,struct type * arg,struct value * value)2474a45ae5f8SJohn Marino rank_one_type (struct type *parm, struct type *arg, struct value *value)
24755796c8dcSSimon Schubert {
2476c50c785cSJohn Marino   struct rank rank = {0,0};
2477c50c785cSJohn Marino 
2478c50c785cSJohn Marino   if (types_equal (parm, arg))
2479c50c785cSJohn Marino     return EXACT_MATCH_BADNESS;
24805796c8dcSSimon Schubert 
24815796c8dcSSimon Schubert   /* Resolve typedefs */
24825796c8dcSSimon Schubert   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
24835796c8dcSSimon Schubert     parm = check_typedef (parm);
24845796c8dcSSimon Schubert   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
24855796c8dcSSimon Schubert     arg = check_typedef (arg);
24865796c8dcSSimon Schubert 
24875796c8dcSSimon Schubert   /* See through references, since we can almost make non-references
24885796c8dcSSimon Schubert      references.  */
24895796c8dcSSimon Schubert   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2490a45ae5f8SJohn Marino     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
2491c50c785cSJohn Marino                        REFERENCE_CONVERSION_BADNESS));
24925796c8dcSSimon Schubert   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2493a45ae5f8SJohn Marino     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
2494c50c785cSJohn Marino                        REFERENCE_CONVERSION_BADNESS));
24955796c8dcSSimon Schubert   if (overload_debug)
24965796c8dcSSimon Schubert   /* Debugging only.  */
24975796c8dcSSimon Schubert     fprintf_filtered (gdb_stderr,
24985796c8dcSSimon Schubert 		      "------ Arg is %s [%d], parm is %s [%d]\n",
24995796c8dcSSimon Schubert 		      TYPE_NAME (arg), TYPE_CODE (arg),
25005796c8dcSSimon Schubert 		      TYPE_NAME (parm), TYPE_CODE (parm));
25015796c8dcSSimon Schubert 
2502c50c785cSJohn Marino   /* x -> y means arg of type x being supplied for parameter of type y.  */
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert   switch (TYPE_CODE (parm))
25055796c8dcSSimon Schubert     {
25065796c8dcSSimon Schubert     case TYPE_CODE_PTR:
25075796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
25085796c8dcSSimon Schubert 	{
25095796c8dcSSimon Schubert 	case TYPE_CODE_PTR:
2510c50c785cSJohn Marino 
2511c50c785cSJohn Marino 	  /* Allowed pointer conversions are:
2512c50c785cSJohn Marino 	     (a) pointer to void-pointer conversion.  */
2513c50c785cSJohn Marino 	  if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
25145796c8dcSSimon Schubert 	    return VOID_PTR_CONVERSION_BADNESS;
2515c50c785cSJohn Marino 
2516c50c785cSJohn Marino 	  /* (b) pointer to ancestor-pointer conversion.  */
2517c50c785cSJohn Marino 	  rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
2518c50c785cSJohn Marino 	                                       TYPE_TARGET_TYPE (arg),
2519c50c785cSJohn Marino 	                                       0);
2520c50c785cSJohn Marino 	  if (rank.subrank >= 0)
2521c50c785cSJohn Marino 	    return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
2522c50c785cSJohn Marino 
2523c50c785cSJohn Marino 	  return INCOMPATIBLE_TYPE_BADNESS;
25245796c8dcSSimon Schubert 	case TYPE_CODE_ARRAY:
2525c50c785cSJohn Marino 	  if (types_equal (TYPE_TARGET_TYPE (parm),
2526c50c785cSJohn Marino 	                   TYPE_TARGET_TYPE (arg)))
2527c50c785cSJohn Marino 	    return EXACT_MATCH_BADNESS;
2528c50c785cSJohn Marino 	  return INCOMPATIBLE_TYPE_BADNESS;
25295796c8dcSSimon Schubert 	case TYPE_CODE_FUNC:
2530a45ae5f8SJohn Marino 	  return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
25315796c8dcSSimon Schubert 	case TYPE_CODE_INT:
2532*ef5ccd6cSJohn Marino 	  if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
2533*ef5ccd6cSJohn Marino 	    {
2534*ef5ccd6cSJohn Marino 	      if (value_as_long (value) == 0)
2535a45ae5f8SJohn Marino 		{
2536a45ae5f8SJohn Marino 		  /* Null pointer conversion: allow it to be cast to a pointer.
2537a45ae5f8SJohn Marino 		     [4.10.1 of C++ standard draft n3290]  */
2538a45ae5f8SJohn Marino 		  return NULL_POINTER_CONVERSION_BADNESS;
2539a45ae5f8SJohn Marino 		}
2540*ef5ccd6cSJohn Marino 	      else
2541*ef5ccd6cSJohn Marino 		{
2542*ef5ccd6cSJohn Marino 		  /* If type checking is disabled, allow the conversion.  */
2543*ef5ccd6cSJohn Marino 		  if (!strict_type_checking)
2544*ef5ccd6cSJohn Marino 		    return NS_INTEGER_POINTER_CONVERSION_BADNESS;
2545*ef5ccd6cSJohn Marino 		}
2546*ef5ccd6cSJohn Marino 	    }
2547a45ae5f8SJohn Marino 	  /* fall through  */
25485796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
25495796c8dcSSimon Schubert 	case TYPE_CODE_FLAGS:
25505796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
25515796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
25525796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
25535796c8dcSSimon Schubert 	default:
25545796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
25555796c8dcSSimon Schubert 	}
25565796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
25575796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
25585796c8dcSSimon Schubert 	{
25595796c8dcSSimon Schubert 	case TYPE_CODE_PTR:
25605796c8dcSSimon Schubert 	case TYPE_CODE_ARRAY:
25615796c8dcSSimon Schubert 	  return rank_one_type (TYPE_TARGET_TYPE (parm),
2562a45ae5f8SJohn Marino 				TYPE_TARGET_TYPE (arg), NULL);
25635796c8dcSSimon Schubert 	default:
25645796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
25655796c8dcSSimon Schubert 	}
25665796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
25675796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
25685796c8dcSSimon Schubert 	{
25695796c8dcSSimon Schubert 	case TYPE_CODE_PTR:	/* funcptr -> func */
2570a45ae5f8SJohn Marino 	  return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
25715796c8dcSSimon Schubert 	default:
25725796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
25735796c8dcSSimon Schubert 	}
25745796c8dcSSimon Schubert     case TYPE_CODE_INT:
25755796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
25765796c8dcSSimon Schubert 	{
25775796c8dcSSimon Schubert 	case TYPE_CODE_INT:
25785796c8dcSSimon Schubert 	  if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
25795796c8dcSSimon Schubert 	    {
25805796c8dcSSimon Schubert 	      /* Deal with signed, unsigned, and plain chars and
25815796c8dcSSimon Schubert 	         signed and unsigned ints.  */
25825796c8dcSSimon Schubert 	      if (TYPE_NOSIGN (parm))
25835796c8dcSSimon Schubert 		{
2584c50c785cSJohn Marino 		  /* This case only for character types.  */
25855796c8dcSSimon Schubert 		  if (TYPE_NOSIGN (arg))
2586c50c785cSJohn Marino 		    return EXACT_MATCH_BADNESS;	/* plain char -> plain char */
25875796c8dcSSimon Schubert 		  else		/* signed/unsigned char -> plain char */
25885796c8dcSSimon Schubert 		    return INTEGER_CONVERSION_BADNESS;
25895796c8dcSSimon Schubert 		}
25905796c8dcSSimon Schubert 	      else if (TYPE_UNSIGNED (parm))
25915796c8dcSSimon Schubert 		{
25925796c8dcSSimon Schubert 		  if (TYPE_UNSIGNED (arg))
25935796c8dcSSimon Schubert 		    {
25945796c8dcSSimon Schubert 		      /* unsigned int -> unsigned int, or
25955796c8dcSSimon Schubert 			 unsigned long -> unsigned long */
25965796c8dcSSimon Schubert 		      if (integer_types_same_name_p (TYPE_NAME (parm),
25975796c8dcSSimon Schubert 						     TYPE_NAME (arg)))
2598c50c785cSJohn Marino 			return EXACT_MATCH_BADNESS;
25995796c8dcSSimon Schubert 		      else if (integer_types_same_name_p (TYPE_NAME (arg),
26005796c8dcSSimon Schubert 							  "int")
26015796c8dcSSimon Schubert 			       && integer_types_same_name_p (TYPE_NAME (parm),
26025796c8dcSSimon Schubert 							     "long"))
2603c50c785cSJohn Marino 			/* unsigned int -> unsigned long */
2604c50c785cSJohn Marino 			return INTEGER_PROMOTION_BADNESS;
26055796c8dcSSimon Schubert 		      else
2606c50c785cSJohn Marino 			/* unsigned long -> unsigned int */
2607c50c785cSJohn Marino 			return INTEGER_CONVERSION_BADNESS;
26085796c8dcSSimon Schubert 		    }
26095796c8dcSSimon Schubert 		  else
26105796c8dcSSimon Schubert 		    {
26115796c8dcSSimon Schubert 		      if (integer_types_same_name_p (TYPE_NAME (arg),
26125796c8dcSSimon Schubert 						     "long")
26135796c8dcSSimon Schubert 			  && integer_types_same_name_p (TYPE_NAME (parm),
26145796c8dcSSimon Schubert 							"int"))
2615c50c785cSJohn Marino 			/* signed long -> unsigned int */
2616c50c785cSJohn Marino 			return INTEGER_CONVERSION_BADNESS;
26175796c8dcSSimon Schubert 		      else
2618c50c785cSJohn Marino 			/* signed int/long -> unsigned int/long */
2619c50c785cSJohn Marino 			return INTEGER_CONVERSION_BADNESS;
26205796c8dcSSimon Schubert 		    }
26215796c8dcSSimon Schubert 		}
26225796c8dcSSimon Schubert 	      else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
26235796c8dcSSimon Schubert 		{
26245796c8dcSSimon Schubert 		  if (integer_types_same_name_p (TYPE_NAME (parm),
26255796c8dcSSimon Schubert 						 TYPE_NAME (arg)))
2626c50c785cSJohn Marino 		    return EXACT_MATCH_BADNESS;
26275796c8dcSSimon Schubert 		  else if (integer_types_same_name_p (TYPE_NAME (arg),
26285796c8dcSSimon Schubert 						      "int")
26295796c8dcSSimon Schubert 			   && integer_types_same_name_p (TYPE_NAME (parm),
26305796c8dcSSimon Schubert 							 "long"))
26315796c8dcSSimon Schubert 		    return INTEGER_PROMOTION_BADNESS;
26325796c8dcSSimon Schubert 		  else
26335796c8dcSSimon Schubert 		    return INTEGER_CONVERSION_BADNESS;
26345796c8dcSSimon Schubert 		}
26355796c8dcSSimon Schubert 	      else
26365796c8dcSSimon Schubert 		return INTEGER_CONVERSION_BADNESS;
26375796c8dcSSimon Schubert 	    }
26385796c8dcSSimon Schubert 	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
26395796c8dcSSimon Schubert 	    return INTEGER_PROMOTION_BADNESS;
26405796c8dcSSimon Schubert 	  else
26415796c8dcSSimon Schubert 	    return INTEGER_CONVERSION_BADNESS;
26425796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
26435796c8dcSSimon Schubert 	case TYPE_CODE_FLAGS:
26445796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
26455796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
26465796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
26475796c8dcSSimon Schubert 	  return INTEGER_PROMOTION_BADNESS;
26485796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
26495796c8dcSSimon Schubert 	  return INT_FLOAT_CONVERSION_BADNESS;
26505796c8dcSSimon Schubert 	case TYPE_CODE_PTR:
26515796c8dcSSimon Schubert 	  return NS_POINTER_CONVERSION_BADNESS;
26525796c8dcSSimon Schubert 	default:
26535796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
26545796c8dcSSimon Schubert 	}
26555796c8dcSSimon Schubert       break;
26565796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
26575796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
26585796c8dcSSimon Schubert 	{
26595796c8dcSSimon Schubert 	case TYPE_CODE_INT:
26605796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
26615796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
26625796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
26635796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
26645796c8dcSSimon Schubert 	  return INTEGER_CONVERSION_BADNESS;
26655796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
26665796c8dcSSimon Schubert 	  return INT_FLOAT_CONVERSION_BADNESS;
26675796c8dcSSimon Schubert 	default:
26685796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
26695796c8dcSSimon Schubert 	}
26705796c8dcSSimon Schubert       break;
26715796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
26725796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
26735796c8dcSSimon Schubert 	{
26745796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
26755796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
26765796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
26775796c8dcSSimon Schubert 	  return INTEGER_CONVERSION_BADNESS;
26785796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
26795796c8dcSSimon Schubert 	  return INT_FLOAT_CONVERSION_BADNESS;
26805796c8dcSSimon Schubert 	case TYPE_CODE_INT:
26815796c8dcSSimon Schubert 	  if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
26825796c8dcSSimon Schubert 	    return INTEGER_CONVERSION_BADNESS;
26835796c8dcSSimon Schubert 	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
26845796c8dcSSimon Schubert 	    return INTEGER_PROMOTION_BADNESS;
26855796c8dcSSimon Schubert 	  /* >>> !! else fall through !! <<< */
26865796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
26875796c8dcSSimon Schubert 	  /* Deal with signed, unsigned, and plain chars for C++ and
26885796c8dcSSimon Schubert 	     with int cases falling through from previous case.  */
26895796c8dcSSimon Schubert 	  if (TYPE_NOSIGN (parm))
26905796c8dcSSimon Schubert 	    {
26915796c8dcSSimon Schubert 	      if (TYPE_NOSIGN (arg))
2692c50c785cSJohn Marino 		return EXACT_MATCH_BADNESS;
26935796c8dcSSimon Schubert 	      else
26945796c8dcSSimon Schubert 		return INTEGER_CONVERSION_BADNESS;
26955796c8dcSSimon Schubert 	    }
26965796c8dcSSimon Schubert 	  else if (TYPE_UNSIGNED (parm))
26975796c8dcSSimon Schubert 	    {
26985796c8dcSSimon Schubert 	      if (TYPE_UNSIGNED (arg))
2699c50c785cSJohn Marino 		return EXACT_MATCH_BADNESS;
27005796c8dcSSimon Schubert 	      else
27015796c8dcSSimon Schubert 		return INTEGER_PROMOTION_BADNESS;
27025796c8dcSSimon Schubert 	    }
27035796c8dcSSimon Schubert 	  else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2704c50c785cSJohn Marino 	    return EXACT_MATCH_BADNESS;
27055796c8dcSSimon Schubert 	  else
27065796c8dcSSimon Schubert 	    return INTEGER_CONVERSION_BADNESS;
27075796c8dcSSimon Schubert 	default:
27085796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27095796c8dcSSimon Schubert 	}
27105796c8dcSSimon Schubert       break;
27115796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
27125796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27135796c8dcSSimon Schubert 	{
27145796c8dcSSimon Schubert 	case TYPE_CODE_INT:
27155796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
27165796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
27175796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
27185796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
27195796c8dcSSimon Schubert 	  return INTEGER_CONVERSION_BADNESS;
27205796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
27215796c8dcSSimon Schubert 	  return INT_FLOAT_CONVERSION_BADNESS;
27225796c8dcSSimon Schubert 	default:
27235796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27245796c8dcSSimon Schubert 	}
27255796c8dcSSimon Schubert       break;
27265796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
27275796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27285796c8dcSSimon Schubert 	{
2729*ef5ccd6cSJohn Marino 	  /* n3290 draft, section 4.12.1 (conv.bool):
2730*ef5ccd6cSJohn Marino 
2731*ef5ccd6cSJohn Marino 	     "A prvalue of arithmetic, unscoped enumeration, pointer, or
2732*ef5ccd6cSJohn Marino 	     pointer to member type can be converted to a prvalue of type
2733*ef5ccd6cSJohn Marino 	     bool.  A zero value, null pointer value, or null member pointer
2734*ef5ccd6cSJohn Marino 	     value is converted to false; any other value is converted to
2735*ef5ccd6cSJohn Marino 	     true.  A prvalue of type std::nullptr_t can be converted to a
2736*ef5ccd6cSJohn Marino 	     prvalue of type bool; the resulting value is false."  */
27375796c8dcSSimon Schubert 	case TYPE_CODE_INT:
27385796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
27395796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
27405796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
2741*ef5ccd6cSJohn Marino 	case TYPE_CODE_MEMBERPTR:
27425796c8dcSSimon Schubert 	case TYPE_CODE_PTR:
2743*ef5ccd6cSJohn Marino 	  return BOOL_CONVERSION_BADNESS;
2744*ef5ccd6cSJohn Marino 	case TYPE_CODE_RANGE:
2745*ef5ccd6cSJohn Marino 	  return INCOMPATIBLE_TYPE_BADNESS;
27465796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
2747c50c785cSJohn Marino 	  return EXACT_MATCH_BADNESS;
27485796c8dcSSimon Schubert 	default:
27495796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27505796c8dcSSimon Schubert 	}
27515796c8dcSSimon Schubert       break;
27525796c8dcSSimon Schubert     case TYPE_CODE_FLT:
27535796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27545796c8dcSSimon Schubert 	{
27555796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
27565796c8dcSSimon Schubert 	  if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
27575796c8dcSSimon Schubert 	    return FLOAT_PROMOTION_BADNESS;
27585796c8dcSSimon Schubert 	  else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2759c50c785cSJohn Marino 	    return EXACT_MATCH_BADNESS;
27605796c8dcSSimon Schubert 	  else
27615796c8dcSSimon Schubert 	    return FLOAT_CONVERSION_BADNESS;
27625796c8dcSSimon Schubert 	case TYPE_CODE_INT:
27635796c8dcSSimon Schubert 	case TYPE_CODE_BOOL:
27645796c8dcSSimon Schubert 	case TYPE_CODE_ENUM:
27655796c8dcSSimon Schubert 	case TYPE_CODE_RANGE:
27665796c8dcSSimon Schubert 	case TYPE_CODE_CHAR:
27675796c8dcSSimon Schubert 	  return INT_FLOAT_CONVERSION_BADNESS;
27685796c8dcSSimon Schubert 	default:
27695796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27705796c8dcSSimon Schubert 	}
27715796c8dcSSimon Schubert       break;
27725796c8dcSSimon Schubert     case TYPE_CODE_COMPLEX:
27735796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27745796c8dcSSimon Schubert 	{		/* Strictly not needed for C++, but...  */
27755796c8dcSSimon Schubert 	case TYPE_CODE_FLT:
27765796c8dcSSimon Schubert 	  return FLOAT_PROMOTION_BADNESS;
27775796c8dcSSimon Schubert 	case TYPE_CODE_COMPLEX:
2778c50c785cSJohn Marino 	  return EXACT_MATCH_BADNESS;
27795796c8dcSSimon Schubert 	default:
27805796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27815796c8dcSSimon Schubert 	}
27825796c8dcSSimon Schubert       break;
27835796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
2784c50c785cSJohn Marino       /* currently same as TYPE_CODE_CLASS.  */
27855796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27865796c8dcSSimon Schubert 	{
27875796c8dcSSimon Schubert 	case TYPE_CODE_STRUCT:
27885796c8dcSSimon Schubert 	  /* Check for derivation */
2789c50c785cSJohn Marino 	  rank.subrank = distance_to_ancestor (parm, arg, 0);
2790c50c785cSJohn Marino 	  if (rank.subrank >= 0)
2791c50c785cSJohn Marino 	    return sum_ranks (BASE_CONVERSION_BADNESS, rank);
27925796c8dcSSimon Schubert 	  /* else fall through */
27935796c8dcSSimon Schubert 	default:
27945796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
27955796c8dcSSimon Schubert 	}
27965796c8dcSSimon Schubert       break;
27975796c8dcSSimon Schubert     case TYPE_CODE_UNION:
27985796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
27995796c8dcSSimon Schubert 	{
28005796c8dcSSimon Schubert 	case TYPE_CODE_UNION:
28015796c8dcSSimon Schubert 	default:
28025796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
28035796c8dcSSimon Schubert 	}
28045796c8dcSSimon Schubert       break;
28055796c8dcSSimon Schubert     case TYPE_CODE_MEMBERPTR:
28065796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
28075796c8dcSSimon Schubert 	{
28085796c8dcSSimon Schubert 	default:
28095796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
28105796c8dcSSimon Schubert 	}
28115796c8dcSSimon Schubert       break;
28125796c8dcSSimon Schubert     case TYPE_CODE_METHOD:
28135796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
28145796c8dcSSimon Schubert 	{
28155796c8dcSSimon Schubert 
28165796c8dcSSimon Schubert 	default:
28175796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
28185796c8dcSSimon Schubert 	}
28195796c8dcSSimon Schubert       break;
28205796c8dcSSimon Schubert     case TYPE_CODE_REF:
28215796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
28225796c8dcSSimon Schubert 	{
28235796c8dcSSimon Schubert 
28245796c8dcSSimon Schubert 	default:
28255796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
28265796c8dcSSimon Schubert 	}
28275796c8dcSSimon Schubert 
28285796c8dcSSimon Schubert       break;
28295796c8dcSSimon Schubert     case TYPE_CODE_SET:
28305796c8dcSSimon Schubert       switch (TYPE_CODE (arg))
28315796c8dcSSimon Schubert 	{
28325796c8dcSSimon Schubert 	  /* Not in C++ */
28335796c8dcSSimon Schubert 	case TYPE_CODE_SET:
28345796c8dcSSimon Schubert 	  return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
2835a45ae5f8SJohn Marino 				TYPE_FIELD_TYPE (arg, 0), NULL);
28365796c8dcSSimon Schubert 	default:
28375796c8dcSSimon Schubert 	  return INCOMPATIBLE_TYPE_BADNESS;
28385796c8dcSSimon Schubert 	}
28395796c8dcSSimon Schubert       break;
28405796c8dcSSimon Schubert     case TYPE_CODE_VOID:
28415796c8dcSSimon Schubert     default:
28425796c8dcSSimon Schubert       return INCOMPATIBLE_TYPE_BADNESS;
28435796c8dcSSimon Schubert     }				/* switch (TYPE_CODE (arg)) */
28445796c8dcSSimon Schubert }
28455796c8dcSSimon Schubert 
2846c50c785cSJohn Marino /* End of functions for overload resolution.  */
2847*ef5ccd6cSJohn Marino 
2848*ef5ccd6cSJohn Marino /* Routines to pretty-print types.  */
28495796c8dcSSimon Schubert 
28505796c8dcSSimon Schubert static void
print_bit_vector(B_TYPE * bits,int nbits)28515796c8dcSSimon Schubert print_bit_vector (B_TYPE *bits, int nbits)
28525796c8dcSSimon Schubert {
28535796c8dcSSimon Schubert   int bitno;
28545796c8dcSSimon Schubert 
28555796c8dcSSimon Schubert   for (bitno = 0; bitno < nbits; bitno++)
28565796c8dcSSimon Schubert     {
28575796c8dcSSimon Schubert       if ((bitno % 8) == 0)
28585796c8dcSSimon Schubert 	{
28595796c8dcSSimon Schubert 	  puts_filtered (" ");
28605796c8dcSSimon Schubert 	}
28615796c8dcSSimon Schubert       if (B_TST (bits, bitno))
28625796c8dcSSimon Schubert 	printf_filtered (("1"));
28635796c8dcSSimon Schubert       else
28645796c8dcSSimon Schubert 	printf_filtered (("0"));
28655796c8dcSSimon Schubert     }
28665796c8dcSSimon Schubert }
28675796c8dcSSimon Schubert 
28685796c8dcSSimon Schubert /* Note the first arg should be the "this" pointer, we may not want to
28695796c8dcSSimon Schubert    include it since we may get into a infinitely recursive
28705796c8dcSSimon Schubert    situation.  */
28715796c8dcSSimon Schubert 
28725796c8dcSSimon Schubert static void
print_arg_types(struct field * args,int nargs,int spaces)28735796c8dcSSimon Schubert print_arg_types (struct field *args, int nargs, int spaces)
28745796c8dcSSimon Schubert {
28755796c8dcSSimon Schubert   if (args != NULL)
28765796c8dcSSimon Schubert     {
28775796c8dcSSimon Schubert       int i;
28785796c8dcSSimon Schubert 
28795796c8dcSSimon Schubert       for (i = 0; i < nargs; i++)
28805796c8dcSSimon Schubert 	recursive_dump_type (args[i].type, spaces + 2);
28815796c8dcSSimon Schubert     }
28825796c8dcSSimon Schubert }
28835796c8dcSSimon Schubert 
28845796c8dcSSimon Schubert int
field_is_static(struct field * f)28855796c8dcSSimon Schubert field_is_static (struct field *f)
28865796c8dcSSimon Schubert {
28875796c8dcSSimon Schubert   /* "static" fields are the fields whose location is not relative
28885796c8dcSSimon Schubert      to the address of the enclosing struct.  It would be nice to
28895796c8dcSSimon Schubert      have a dedicated flag that would be set for static fields when
28905796c8dcSSimon Schubert      the type is being created.  But in practice, checking the field
2891cf7f2e2dSJohn Marino      loc_kind should give us an accurate answer.  */
28925796c8dcSSimon Schubert   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
28935796c8dcSSimon Schubert 	  || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
28945796c8dcSSimon Schubert }
28955796c8dcSSimon Schubert 
28965796c8dcSSimon Schubert static void
dump_fn_fieldlists(struct type * type,int spaces)28975796c8dcSSimon Schubert dump_fn_fieldlists (struct type *type, int spaces)
28985796c8dcSSimon Schubert {
28995796c8dcSSimon Schubert   int method_idx;
29005796c8dcSSimon Schubert   int overload_idx;
29015796c8dcSSimon Schubert   struct fn_field *f;
29025796c8dcSSimon Schubert 
29035796c8dcSSimon Schubert   printfi_filtered (spaces, "fn_fieldlists ");
29045796c8dcSSimon Schubert   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
29055796c8dcSSimon Schubert   printf_filtered ("\n");
29065796c8dcSSimon Schubert   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
29075796c8dcSSimon Schubert     {
29085796c8dcSSimon Schubert       f = TYPE_FN_FIELDLIST1 (type, method_idx);
29095796c8dcSSimon Schubert       printfi_filtered (spaces + 2, "[%d] name '%s' (",
29105796c8dcSSimon Schubert 			method_idx,
29115796c8dcSSimon Schubert 			TYPE_FN_FIELDLIST_NAME (type, method_idx));
29125796c8dcSSimon Schubert       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
29135796c8dcSSimon Schubert 			      gdb_stdout);
29145796c8dcSSimon Schubert       printf_filtered (_(") length %d\n"),
29155796c8dcSSimon Schubert 		       TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
29165796c8dcSSimon Schubert       for (overload_idx = 0;
29175796c8dcSSimon Schubert 	   overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
29185796c8dcSSimon Schubert 	   overload_idx++)
29195796c8dcSSimon Schubert 	{
29205796c8dcSSimon Schubert 	  printfi_filtered (spaces + 4, "[%d] physname '%s' (",
29215796c8dcSSimon Schubert 			    overload_idx,
29225796c8dcSSimon Schubert 			    TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
29235796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
29245796c8dcSSimon Schubert 				  gdb_stdout);
29255796c8dcSSimon Schubert 	  printf_filtered (")\n");
29265796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "type ");
29275796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
29285796c8dcSSimon Schubert 				  gdb_stdout);
29295796c8dcSSimon Schubert 	  printf_filtered ("\n");
29305796c8dcSSimon Schubert 
29315796c8dcSSimon Schubert 	  recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
29325796c8dcSSimon Schubert 			       spaces + 8 + 2);
29335796c8dcSSimon Schubert 
29345796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "args ");
29355796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
29365796c8dcSSimon Schubert 				  gdb_stdout);
29375796c8dcSSimon Schubert 	  printf_filtered ("\n");
29385796c8dcSSimon Schubert 
29395796c8dcSSimon Schubert 	  print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
29405796c8dcSSimon Schubert 			   TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f,
29415796c8dcSSimon Schubert 							     overload_idx)),
29425796c8dcSSimon Schubert 			   spaces);
29435796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "fcontext ");
29445796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
29455796c8dcSSimon Schubert 				  gdb_stdout);
29465796c8dcSSimon Schubert 	  printf_filtered ("\n");
29475796c8dcSSimon Schubert 
29485796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "is_const %d\n",
29495796c8dcSSimon Schubert 			    TYPE_FN_FIELD_CONST (f, overload_idx));
29505796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "is_volatile %d\n",
29515796c8dcSSimon Schubert 			    TYPE_FN_FIELD_VOLATILE (f, overload_idx));
29525796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "is_private %d\n",
29535796c8dcSSimon Schubert 			    TYPE_FN_FIELD_PRIVATE (f, overload_idx));
29545796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "is_protected %d\n",
29555796c8dcSSimon Schubert 			    TYPE_FN_FIELD_PROTECTED (f, overload_idx));
29565796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "is_stub %d\n",
29575796c8dcSSimon Schubert 			    TYPE_FN_FIELD_STUB (f, overload_idx));
29585796c8dcSSimon Schubert 	  printfi_filtered (spaces + 8, "voffset %u\n",
29595796c8dcSSimon Schubert 			    TYPE_FN_FIELD_VOFFSET (f, overload_idx));
29605796c8dcSSimon Schubert 	}
29615796c8dcSSimon Schubert     }
29625796c8dcSSimon Schubert }
29635796c8dcSSimon Schubert 
29645796c8dcSSimon Schubert static void
print_cplus_stuff(struct type * type,int spaces)29655796c8dcSSimon Schubert print_cplus_stuff (struct type *type, int spaces)
29665796c8dcSSimon Schubert {
29675796c8dcSSimon Schubert   printfi_filtered (spaces, "n_baseclasses %d\n",
29685796c8dcSSimon Schubert 		    TYPE_N_BASECLASSES (type));
29695796c8dcSSimon Schubert   printfi_filtered (spaces, "nfn_fields %d\n",
29705796c8dcSSimon Schubert 		    TYPE_NFN_FIELDS (type));
29715796c8dcSSimon Schubert   if (TYPE_N_BASECLASSES (type) > 0)
29725796c8dcSSimon Schubert     {
29735796c8dcSSimon Schubert       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
29745796c8dcSSimon Schubert 			TYPE_N_BASECLASSES (type));
29755796c8dcSSimon Schubert       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
29765796c8dcSSimon Schubert 			      gdb_stdout);
29775796c8dcSSimon Schubert       printf_filtered (")");
29785796c8dcSSimon Schubert 
29795796c8dcSSimon Schubert       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
29805796c8dcSSimon Schubert 			TYPE_N_BASECLASSES (type));
29815796c8dcSSimon Schubert       puts_filtered ("\n");
29825796c8dcSSimon Schubert     }
29835796c8dcSSimon Schubert   if (TYPE_NFIELDS (type) > 0)
29845796c8dcSSimon Schubert     {
29855796c8dcSSimon Schubert       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
29865796c8dcSSimon Schubert 	{
29875796c8dcSSimon Schubert 	  printfi_filtered (spaces,
29885796c8dcSSimon Schubert 			    "private_field_bits (%d bits at *",
29895796c8dcSSimon Schubert 			    TYPE_NFIELDS (type));
29905796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
29915796c8dcSSimon Schubert 				  gdb_stdout);
29925796c8dcSSimon Schubert 	  printf_filtered (")");
29935796c8dcSSimon Schubert 	  print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
29945796c8dcSSimon Schubert 			    TYPE_NFIELDS (type));
29955796c8dcSSimon Schubert 	  puts_filtered ("\n");
29965796c8dcSSimon Schubert 	}
29975796c8dcSSimon Schubert       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
29985796c8dcSSimon Schubert 	{
29995796c8dcSSimon Schubert 	  printfi_filtered (spaces,
30005796c8dcSSimon Schubert 			    "protected_field_bits (%d bits at *",
30015796c8dcSSimon Schubert 			    TYPE_NFIELDS (type));
30025796c8dcSSimon Schubert 	  gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
30035796c8dcSSimon Schubert 				  gdb_stdout);
30045796c8dcSSimon Schubert 	  printf_filtered (")");
30055796c8dcSSimon Schubert 	  print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
30065796c8dcSSimon Schubert 			    TYPE_NFIELDS (type));
30075796c8dcSSimon Schubert 	  puts_filtered ("\n");
30085796c8dcSSimon Schubert 	}
30095796c8dcSSimon Schubert     }
30105796c8dcSSimon Schubert   if (TYPE_NFN_FIELDS (type) > 0)
30115796c8dcSSimon Schubert     {
30125796c8dcSSimon Schubert       dump_fn_fieldlists (type, spaces);
30135796c8dcSSimon Schubert     }
30145796c8dcSSimon Schubert }
30155796c8dcSSimon Schubert 
3016cf7f2e2dSJohn Marino /* Print the contents of the TYPE's type_specific union, assuming that
3017cf7f2e2dSJohn Marino    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */
3018cf7f2e2dSJohn Marino 
3019cf7f2e2dSJohn Marino static void
print_gnat_stuff(struct type * type,int spaces)3020cf7f2e2dSJohn Marino print_gnat_stuff (struct type *type, int spaces)
3021cf7f2e2dSJohn Marino {
3022cf7f2e2dSJohn Marino   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3023cf7f2e2dSJohn Marino 
3024cf7f2e2dSJohn Marino   recursive_dump_type (descriptive_type, spaces + 2);
3025cf7f2e2dSJohn Marino }
3026cf7f2e2dSJohn Marino 
30275796c8dcSSimon Schubert static struct obstack dont_print_type_obstack;
30285796c8dcSSimon Schubert 
30295796c8dcSSimon Schubert void
recursive_dump_type(struct type * type,int spaces)30305796c8dcSSimon Schubert recursive_dump_type (struct type *type, int spaces)
30315796c8dcSSimon Schubert {
30325796c8dcSSimon Schubert   int idx;
30335796c8dcSSimon Schubert 
30345796c8dcSSimon Schubert   if (spaces == 0)
30355796c8dcSSimon Schubert     obstack_begin (&dont_print_type_obstack, 0);
30365796c8dcSSimon Schubert 
30375796c8dcSSimon Schubert   if (TYPE_NFIELDS (type) > 0
3038cf7f2e2dSJohn Marino       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
30395796c8dcSSimon Schubert     {
30405796c8dcSSimon Schubert       struct type **first_dont_print
30415796c8dcSSimon Schubert 	= (struct type **) obstack_base (&dont_print_type_obstack);
30425796c8dcSSimon Schubert 
30435796c8dcSSimon Schubert       int i = (struct type **)
30445796c8dcSSimon Schubert 	obstack_next_free (&dont_print_type_obstack) - first_dont_print;
30455796c8dcSSimon Schubert 
30465796c8dcSSimon Schubert       while (--i >= 0)
30475796c8dcSSimon Schubert 	{
30485796c8dcSSimon Schubert 	  if (type == first_dont_print[i])
30495796c8dcSSimon Schubert 	    {
30505796c8dcSSimon Schubert 	      printfi_filtered (spaces, "type node ");
30515796c8dcSSimon Schubert 	      gdb_print_host_address (type, gdb_stdout);
30525796c8dcSSimon Schubert 	      printf_filtered (_(" <same as already seen type>\n"));
30535796c8dcSSimon Schubert 	      return;
30545796c8dcSSimon Schubert 	    }
30555796c8dcSSimon Schubert 	}
30565796c8dcSSimon Schubert 
30575796c8dcSSimon Schubert       obstack_ptr_grow (&dont_print_type_obstack, type);
30585796c8dcSSimon Schubert     }
30595796c8dcSSimon Schubert 
30605796c8dcSSimon Schubert   printfi_filtered (spaces, "type node ");
30615796c8dcSSimon Schubert   gdb_print_host_address (type, gdb_stdout);
30625796c8dcSSimon Schubert   printf_filtered ("\n");
30635796c8dcSSimon Schubert   printfi_filtered (spaces, "name '%s' (",
30645796c8dcSSimon Schubert 		    TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
30655796c8dcSSimon Schubert   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
30665796c8dcSSimon Schubert   printf_filtered (")\n");
30675796c8dcSSimon Schubert   printfi_filtered (spaces, "tagname '%s' (",
30685796c8dcSSimon Schubert 		    TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
30695796c8dcSSimon Schubert   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
30705796c8dcSSimon Schubert   printf_filtered (")\n");
30715796c8dcSSimon Schubert   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
30725796c8dcSSimon Schubert   switch (TYPE_CODE (type))
30735796c8dcSSimon Schubert     {
30745796c8dcSSimon Schubert     case TYPE_CODE_UNDEF:
30755796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_UNDEF)");
30765796c8dcSSimon Schubert       break;
30775796c8dcSSimon Schubert     case TYPE_CODE_PTR:
30785796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_PTR)");
30795796c8dcSSimon Schubert       break;
30805796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
30815796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_ARRAY)");
30825796c8dcSSimon Schubert       break;
30835796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
30845796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_STRUCT)");
30855796c8dcSSimon Schubert       break;
30865796c8dcSSimon Schubert     case TYPE_CODE_UNION:
30875796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_UNION)");
30885796c8dcSSimon Schubert       break;
30895796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
30905796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_ENUM)");
30915796c8dcSSimon Schubert       break;
30925796c8dcSSimon Schubert     case TYPE_CODE_FLAGS:
30935796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_FLAGS)");
30945796c8dcSSimon Schubert       break;
30955796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
30965796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_FUNC)");
30975796c8dcSSimon Schubert       break;
30985796c8dcSSimon Schubert     case TYPE_CODE_INT:
30995796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_INT)");
31005796c8dcSSimon Schubert       break;
31015796c8dcSSimon Schubert     case TYPE_CODE_FLT:
31025796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_FLT)");
31035796c8dcSSimon Schubert       break;
31045796c8dcSSimon Schubert     case TYPE_CODE_VOID:
31055796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_VOID)");
31065796c8dcSSimon Schubert       break;
31075796c8dcSSimon Schubert     case TYPE_CODE_SET:
31085796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_SET)");
31095796c8dcSSimon Schubert       break;
31105796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
31115796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_RANGE)");
31125796c8dcSSimon Schubert       break;
31135796c8dcSSimon Schubert     case TYPE_CODE_STRING:
31145796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_STRING)");
31155796c8dcSSimon Schubert       break;
31165796c8dcSSimon Schubert     case TYPE_CODE_ERROR:
31175796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_ERROR)");
31185796c8dcSSimon Schubert       break;
31195796c8dcSSimon Schubert     case TYPE_CODE_MEMBERPTR:
31205796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
31215796c8dcSSimon Schubert       break;
31225796c8dcSSimon Schubert     case TYPE_CODE_METHODPTR:
31235796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_METHODPTR)");
31245796c8dcSSimon Schubert       break;
31255796c8dcSSimon Schubert     case TYPE_CODE_METHOD:
31265796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_METHOD)");
31275796c8dcSSimon Schubert       break;
31285796c8dcSSimon Schubert     case TYPE_CODE_REF:
31295796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_REF)");
31305796c8dcSSimon Schubert       break;
31315796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
31325796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_CHAR)");
31335796c8dcSSimon Schubert       break;
31345796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
31355796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_BOOL)");
31365796c8dcSSimon Schubert       break;
31375796c8dcSSimon Schubert     case TYPE_CODE_COMPLEX:
31385796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_COMPLEX)");
31395796c8dcSSimon Schubert       break;
31405796c8dcSSimon Schubert     case TYPE_CODE_TYPEDEF:
31415796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_TYPEDEF)");
31425796c8dcSSimon Schubert       break;
31435796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
31445796c8dcSSimon Schubert       printf_filtered ("(TYPE_CODE_NAMESPACE)");
31455796c8dcSSimon Schubert       break;
31465796c8dcSSimon Schubert     default:
31475796c8dcSSimon Schubert       printf_filtered ("(UNKNOWN TYPE CODE)");
31485796c8dcSSimon Schubert       break;
31495796c8dcSSimon Schubert     }
31505796c8dcSSimon Schubert   puts_filtered ("\n");
31515796c8dcSSimon Schubert   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
31525796c8dcSSimon Schubert   if (TYPE_OBJFILE_OWNED (type))
31535796c8dcSSimon Schubert     {
31545796c8dcSSimon Schubert       printfi_filtered (spaces, "objfile ");
31555796c8dcSSimon Schubert       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
31565796c8dcSSimon Schubert     }
31575796c8dcSSimon Schubert   else
31585796c8dcSSimon Schubert     {
31595796c8dcSSimon Schubert       printfi_filtered (spaces, "gdbarch ");
31605796c8dcSSimon Schubert       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
31615796c8dcSSimon Schubert     }
31625796c8dcSSimon Schubert   printf_filtered ("\n");
31635796c8dcSSimon Schubert   printfi_filtered (spaces, "target_type ");
31645796c8dcSSimon Schubert   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
31655796c8dcSSimon Schubert   printf_filtered ("\n");
31665796c8dcSSimon Schubert   if (TYPE_TARGET_TYPE (type) != NULL)
31675796c8dcSSimon Schubert     {
31685796c8dcSSimon Schubert       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
31695796c8dcSSimon Schubert     }
31705796c8dcSSimon Schubert   printfi_filtered (spaces, "pointer_type ");
31715796c8dcSSimon Schubert   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
31725796c8dcSSimon Schubert   printf_filtered ("\n");
31735796c8dcSSimon Schubert   printfi_filtered (spaces, "reference_type ");
31745796c8dcSSimon Schubert   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
31755796c8dcSSimon Schubert   printf_filtered ("\n");
31765796c8dcSSimon Schubert   printfi_filtered (spaces, "type_chain ");
31775796c8dcSSimon Schubert   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
31785796c8dcSSimon Schubert   printf_filtered ("\n");
31795796c8dcSSimon Schubert   printfi_filtered (spaces, "instance_flags 0x%x",
31805796c8dcSSimon Schubert 		    TYPE_INSTANCE_FLAGS (type));
31815796c8dcSSimon Schubert   if (TYPE_CONST (type))
31825796c8dcSSimon Schubert     {
31835796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_CONST");
31845796c8dcSSimon Schubert     }
31855796c8dcSSimon Schubert   if (TYPE_VOLATILE (type))
31865796c8dcSSimon Schubert     {
31875796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_VOLATILE");
31885796c8dcSSimon Schubert     }
31895796c8dcSSimon Schubert   if (TYPE_CODE_SPACE (type))
31905796c8dcSSimon Schubert     {
31915796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_CODE_SPACE");
31925796c8dcSSimon Schubert     }
31935796c8dcSSimon Schubert   if (TYPE_DATA_SPACE (type))
31945796c8dcSSimon Schubert     {
31955796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_DATA_SPACE");
31965796c8dcSSimon Schubert     }
31975796c8dcSSimon Schubert   if (TYPE_ADDRESS_CLASS_1 (type))
31985796c8dcSSimon Schubert     {
31995796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
32005796c8dcSSimon Schubert     }
32015796c8dcSSimon Schubert   if (TYPE_ADDRESS_CLASS_2 (type))
32025796c8dcSSimon Schubert     {
32035796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
32045796c8dcSSimon Schubert     }
3205*ef5ccd6cSJohn Marino   if (TYPE_RESTRICT (type))
3206*ef5ccd6cSJohn Marino     {
3207*ef5ccd6cSJohn Marino       puts_filtered (" TYPE_FLAG_RESTRICT");
3208*ef5ccd6cSJohn Marino     }
32095796c8dcSSimon Schubert   puts_filtered ("\n");
32105796c8dcSSimon Schubert 
32115796c8dcSSimon Schubert   printfi_filtered (spaces, "flags");
32125796c8dcSSimon Schubert   if (TYPE_UNSIGNED (type))
32135796c8dcSSimon Schubert     {
32145796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_UNSIGNED");
32155796c8dcSSimon Schubert     }
32165796c8dcSSimon Schubert   if (TYPE_NOSIGN (type))
32175796c8dcSSimon Schubert     {
32185796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_NOSIGN");
32195796c8dcSSimon Schubert     }
32205796c8dcSSimon Schubert   if (TYPE_STUB (type))
32215796c8dcSSimon Schubert     {
32225796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_STUB");
32235796c8dcSSimon Schubert     }
32245796c8dcSSimon Schubert   if (TYPE_TARGET_STUB (type))
32255796c8dcSSimon Schubert     {
32265796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_TARGET_STUB");
32275796c8dcSSimon Schubert     }
32285796c8dcSSimon Schubert   if (TYPE_STATIC (type))
32295796c8dcSSimon Schubert     {
32305796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_STATIC");
32315796c8dcSSimon Schubert     }
32325796c8dcSSimon Schubert   if (TYPE_PROTOTYPED (type))
32335796c8dcSSimon Schubert     {
32345796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_PROTOTYPED");
32355796c8dcSSimon Schubert     }
32365796c8dcSSimon Schubert   if (TYPE_INCOMPLETE (type))
32375796c8dcSSimon Schubert     {
32385796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_INCOMPLETE");
32395796c8dcSSimon Schubert     }
32405796c8dcSSimon Schubert   if (TYPE_VARARGS (type))
32415796c8dcSSimon Schubert     {
32425796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_VARARGS");
32435796c8dcSSimon Schubert     }
32445796c8dcSSimon Schubert   /* This is used for things like AltiVec registers on ppc.  Gcc emits
32455796c8dcSSimon Schubert      an attribute for the array type, which tells whether or not we
32465796c8dcSSimon Schubert      have a vector, instead of a regular array.  */
32475796c8dcSSimon Schubert   if (TYPE_VECTOR (type))
32485796c8dcSSimon Schubert     {
32495796c8dcSSimon Schubert       puts_filtered (" TYPE_FLAG_VECTOR");
32505796c8dcSSimon Schubert     }
32515796c8dcSSimon Schubert   if (TYPE_FIXED_INSTANCE (type))
32525796c8dcSSimon Schubert     {
32535796c8dcSSimon Schubert       puts_filtered (" TYPE_FIXED_INSTANCE");
32545796c8dcSSimon Schubert     }
32555796c8dcSSimon Schubert   if (TYPE_STUB_SUPPORTED (type))
32565796c8dcSSimon Schubert     {
32575796c8dcSSimon Schubert       puts_filtered (" TYPE_STUB_SUPPORTED");
32585796c8dcSSimon Schubert     }
32595796c8dcSSimon Schubert   if (TYPE_NOTTEXT (type))
32605796c8dcSSimon Schubert     {
32615796c8dcSSimon Schubert       puts_filtered (" TYPE_NOTTEXT");
32625796c8dcSSimon Schubert     }
32635796c8dcSSimon Schubert   puts_filtered ("\n");
32645796c8dcSSimon Schubert   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
32655796c8dcSSimon Schubert   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
32665796c8dcSSimon Schubert   puts_filtered ("\n");
32675796c8dcSSimon Schubert   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
32685796c8dcSSimon Schubert     {
3269*ef5ccd6cSJohn Marino       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3270*ef5ccd6cSJohn Marino 	printfi_filtered (spaces + 2,
3271*ef5ccd6cSJohn Marino 			  "[%d] enumval %s type ",
3272*ef5ccd6cSJohn Marino 			  idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3273*ef5ccd6cSJohn Marino       else
32745796c8dcSSimon Schubert 	printfi_filtered (spaces + 2,
32755796c8dcSSimon Schubert 			  "[%d] bitpos %d bitsize %d type ",
32765796c8dcSSimon Schubert 			  idx, TYPE_FIELD_BITPOS (type, idx),
32775796c8dcSSimon Schubert 			  TYPE_FIELD_BITSIZE (type, idx));
32785796c8dcSSimon Schubert       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
32795796c8dcSSimon Schubert       printf_filtered (" name '%s' (",
32805796c8dcSSimon Schubert 		       TYPE_FIELD_NAME (type, idx) != NULL
32815796c8dcSSimon Schubert 		       ? TYPE_FIELD_NAME (type, idx)
32825796c8dcSSimon Schubert 		       : "<NULL>");
32835796c8dcSSimon Schubert       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
32845796c8dcSSimon Schubert       printf_filtered (")\n");
32855796c8dcSSimon Schubert       if (TYPE_FIELD_TYPE (type, idx) != NULL)
32865796c8dcSSimon Schubert 	{
32875796c8dcSSimon Schubert 	  recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
32885796c8dcSSimon Schubert 	}
32895796c8dcSSimon Schubert     }
3290cf7f2e2dSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3291cf7f2e2dSJohn Marino     {
3292cf7f2e2dSJohn Marino       printfi_filtered (spaces, "low %s%s  high %s%s\n",
3293cf7f2e2dSJohn Marino 			plongest (TYPE_LOW_BOUND (type)),
3294cf7f2e2dSJohn Marino 			TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3295cf7f2e2dSJohn Marino 			plongest (TYPE_HIGH_BOUND (type)),
3296c50c785cSJohn Marino 			TYPE_HIGH_BOUND_UNDEFINED (type)
3297c50c785cSJohn Marino 			? " (undefined)" : "");
3298cf7f2e2dSJohn Marino     }
32995796c8dcSSimon Schubert   printfi_filtered (spaces, "vptr_basetype ");
33005796c8dcSSimon Schubert   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
33015796c8dcSSimon Schubert   puts_filtered ("\n");
33025796c8dcSSimon Schubert   if (TYPE_VPTR_BASETYPE (type) != NULL)
33035796c8dcSSimon Schubert     {
33045796c8dcSSimon Schubert       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
33055796c8dcSSimon Schubert     }
33065796c8dcSSimon Schubert   printfi_filtered (spaces, "vptr_fieldno %d\n",
33075796c8dcSSimon Schubert 		    TYPE_VPTR_FIELDNO (type));
3308cf7f2e2dSJohn Marino 
3309cf7f2e2dSJohn Marino   switch (TYPE_SPECIFIC_FIELD (type))
33105796c8dcSSimon Schubert     {
3311cf7f2e2dSJohn Marino       case TYPE_SPECIFIC_CPLUS_STUFF:
33125796c8dcSSimon Schubert 	printfi_filtered (spaces, "cplus_stuff ");
33135796c8dcSSimon Schubert 	gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
33145796c8dcSSimon Schubert 				gdb_stdout);
33155796c8dcSSimon Schubert 	puts_filtered ("\n");
33165796c8dcSSimon Schubert 	print_cplus_stuff (type, spaces);
33175796c8dcSSimon Schubert 	break;
33185796c8dcSSimon Schubert 
3319cf7f2e2dSJohn Marino       case TYPE_SPECIFIC_GNAT_STUFF:
3320cf7f2e2dSJohn Marino 	printfi_filtered (spaces, "gnat_stuff ");
3321cf7f2e2dSJohn Marino 	gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3322cf7f2e2dSJohn Marino 	puts_filtered ("\n");
3323cf7f2e2dSJohn Marino 	print_gnat_stuff (type, spaces);
3324cf7f2e2dSJohn Marino 	break;
3325cf7f2e2dSJohn Marino 
3326cf7f2e2dSJohn Marino       case TYPE_SPECIFIC_FLOATFORMAT:
33275796c8dcSSimon Schubert 	printfi_filtered (spaces, "floatformat ");
33285796c8dcSSimon Schubert 	if (TYPE_FLOATFORMAT (type) == NULL)
33295796c8dcSSimon Schubert 	  puts_filtered ("(null)");
33305796c8dcSSimon Schubert 	else
33315796c8dcSSimon Schubert 	  {
33325796c8dcSSimon Schubert 	    puts_filtered ("{ ");
33335796c8dcSSimon Schubert 	    if (TYPE_FLOATFORMAT (type)[0] == NULL
33345796c8dcSSimon Schubert 		|| TYPE_FLOATFORMAT (type)[0]->name == NULL)
33355796c8dcSSimon Schubert 	      puts_filtered ("(null)");
33365796c8dcSSimon Schubert 	    else
33375796c8dcSSimon Schubert 	      puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
33385796c8dcSSimon Schubert 
33395796c8dcSSimon Schubert 	    puts_filtered (", ");
33405796c8dcSSimon Schubert 	    if (TYPE_FLOATFORMAT (type)[1] == NULL
33415796c8dcSSimon Schubert 		|| TYPE_FLOATFORMAT (type)[1]->name == NULL)
33425796c8dcSSimon Schubert 	      puts_filtered ("(null)");
33435796c8dcSSimon Schubert 	    else
33445796c8dcSSimon Schubert 	      puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
33455796c8dcSSimon Schubert 
33465796c8dcSSimon Schubert 	    puts_filtered (" }");
33475796c8dcSSimon Schubert 	  }
33485796c8dcSSimon Schubert 	puts_filtered ("\n");
33495796c8dcSSimon Schubert 	break;
33505796c8dcSSimon Schubert 
3351a45ae5f8SJohn Marino       case TYPE_SPECIFIC_FUNC:
3352cf7f2e2dSJohn Marino 	printfi_filtered (spaces, "calling_convention %d\n",
3353cf7f2e2dSJohn Marino                           TYPE_CALLING_CONVENTION (type));
3354a45ae5f8SJohn Marino 	/* tail_call_list is not printed.  */
33555796c8dcSSimon Schubert 	break;
33565796c8dcSSimon Schubert     }
3357cf7f2e2dSJohn Marino 
33585796c8dcSSimon Schubert   if (spaces == 0)
33595796c8dcSSimon Schubert     obstack_free (&dont_print_type_obstack, NULL);
33605796c8dcSSimon Schubert }
3361*ef5ccd6cSJohn Marino 
33625796c8dcSSimon Schubert /* Trivial helpers for the libiberty hash table, for mapping one
33635796c8dcSSimon Schubert    type to another.  */
33645796c8dcSSimon Schubert 
33655796c8dcSSimon Schubert struct type_pair
33665796c8dcSSimon Schubert {
33675796c8dcSSimon Schubert   struct type *old, *new;
33685796c8dcSSimon Schubert };
33695796c8dcSSimon Schubert 
33705796c8dcSSimon Schubert static hashval_t
type_pair_hash(const void * item)33715796c8dcSSimon Schubert type_pair_hash (const void *item)
33725796c8dcSSimon Schubert {
33735796c8dcSSimon Schubert   const struct type_pair *pair = item;
3374cf7f2e2dSJohn Marino 
33755796c8dcSSimon Schubert   return htab_hash_pointer (pair->old);
33765796c8dcSSimon Schubert }
33775796c8dcSSimon Schubert 
33785796c8dcSSimon Schubert static int
type_pair_eq(const void * item_lhs,const void * item_rhs)33795796c8dcSSimon Schubert type_pair_eq (const void *item_lhs, const void *item_rhs)
33805796c8dcSSimon Schubert {
33815796c8dcSSimon Schubert   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3382cf7f2e2dSJohn Marino 
33835796c8dcSSimon Schubert   return lhs->old == rhs->old;
33845796c8dcSSimon Schubert }
33855796c8dcSSimon Schubert 
33865796c8dcSSimon Schubert /* Allocate the hash table used by copy_type_recursive to walk
33875796c8dcSSimon Schubert    types without duplicates.  We use OBJFILE's obstack, because
33885796c8dcSSimon Schubert    OBJFILE is about to be deleted.  */
33895796c8dcSSimon Schubert 
33905796c8dcSSimon Schubert htab_t
create_copied_types_hash(struct objfile * objfile)33915796c8dcSSimon Schubert create_copied_types_hash (struct objfile *objfile)
33925796c8dcSSimon Schubert {
33935796c8dcSSimon Schubert   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
33945796c8dcSSimon Schubert 			       NULL, &objfile->objfile_obstack,
33955796c8dcSSimon Schubert 			       hashtab_obstack_allocate,
33965796c8dcSSimon Schubert 			       dummy_obstack_deallocate);
33975796c8dcSSimon Schubert }
33985796c8dcSSimon Schubert 
33995796c8dcSSimon Schubert /* Recursively copy (deep copy) TYPE, if it is associated with
34005796c8dcSSimon Schubert    OBJFILE.  Return a new type allocated using malloc, a saved type if
34015796c8dcSSimon Schubert    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
34025796c8dcSSimon Schubert    not associated with OBJFILE.  */
34035796c8dcSSimon Schubert 
34045796c8dcSSimon Schubert struct type *
copy_type_recursive(struct objfile * objfile,struct type * type,htab_t copied_types)34055796c8dcSSimon Schubert copy_type_recursive (struct objfile *objfile,
34065796c8dcSSimon Schubert 		     struct type *type,
34075796c8dcSSimon Schubert 		     htab_t copied_types)
34085796c8dcSSimon Schubert {
34095796c8dcSSimon Schubert   struct type_pair *stored, pair;
34105796c8dcSSimon Schubert   void **slot;
34115796c8dcSSimon Schubert   struct type *new_type;
34125796c8dcSSimon Schubert 
34135796c8dcSSimon Schubert   if (! TYPE_OBJFILE_OWNED (type))
34145796c8dcSSimon Schubert     return type;
34155796c8dcSSimon Schubert 
34165796c8dcSSimon Schubert   /* This type shouldn't be pointing to any types in other objfiles;
34175796c8dcSSimon Schubert      if it did, the type might disappear unexpectedly.  */
34185796c8dcSSimon Schubert   gdb_assert (TYPE_OBJFILE (type) == objfile);
34195796c8dcSSimon Schubert 
34205796c8dcSSimon Schubert   pair.old = type;
34215796c8dcSSimon Schubert   slot = htab_find_slot (copied_types, &pair, INSERT);
34225796c8dcSSimon Schubert   if (*slot != NULL)
34235796c8dcSSimon Schubert     return ((struct type_pair *) *slot)->new;
34245796c8dcSSimon Schubert 
34255796c8dcSSimon Schubert   new_type = alloc_type_arch (get_type_arch (type));
34265796c8dcSSimon Schubert 
34275796c8dcSSimon Schubert   /* We must add the new type to the hash table immediately, in case
34285796c8dcSSimon Schubert      we encounter this type again during a recursive call below.  */
3429c50c785cSJohn Marino   stored
3430c50c785cSJohn Marino     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
34315796c8dcSSimon Schubert   stored->old = type;
34325796c8dcSSimon Schubert   stored->new = new_type;
34335796c8dcSSimon Schubert   *slot = stored;
34345796c8dcSSimon Schubert 
34355796c8dcSSimon Schubert   /* Copy the common fields of types.  For the main type, we simply
34365796c8dcSSimon Schubert      copy the entire thing and then update specific fields as needed.  */
34375796c8dcSSimon Schubert   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
34385796c8dcSSimon Schubert   TYPE_OBJFILE_OWNED (new_type) = 0;
34395796c8dcSSimon Schubert   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
34405796c8dcSSimon Schubert 
34415796c8dcSSimon Schubert   if (TYPE_NAME (type))
34425796c8dcSSimon Schubert     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
34435796c8dcSSimon Schubert   if (TYPE_TAG_NAME (type))
34445796c8dcSSimon Schubert     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
34455796c8dcSSimon Schubert 
34465796c8dcSSimon Schubert   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
34475796c8dcSSimon Schubert   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
34485796c8dcSSimon Schubert 
34495796c8dcSSimon Schubert   /* Copy the fields.  */
34505796c8dcSSimon Schubert   if (TYPE_NFIELDS (type))
34515796c8dcSSimon Schubert     {
34525796c8dcSSimon Schubert       int i, nfields;
34535796c8dcSSimon Schubert 
34545796c8dcSSimon Schubert       nfields = TYPE_NFIELDS (type);
34555796c8dcSSimon Schubert       TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field);
34565796c8dcSSimon Schubert       for (i = 0; i < nfields; i++)
34575796c8dcSSimon Schubert 	{
34585796c8dcSSimon Schubert 	  TYPE_FIELD_ARTIFICIAL (new_type, i) =
34595796c8dcSSimon Schubert 	    TYPE_FIELD_ARTIFICIAL (type, i);
34605796c8dcSSimon Schubert 	  TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
34615796c8dcSSimon Schubert 	  if (TYPE_FIELD_TYPE (type, i))
34625796c8dcSSimon Schubert 	    TYPE_FIELD_TYPE (new_type, i)
34635796c8dcSSimon Schubert 	      = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
34645796c8dcSSimon Schubert 				     copied_types);
34655796c8dcSSimon Schubert 	  if (TYPE_FIELD_NAME (type, i))
34665796c8dcSSimon Schubert 	    TYPE_FIELD_NAME (new_type, i) =
34675796c8dcSSimon Schubert 	      xstrdup (TYPE_FIELD_NAME (type, i));
34685796c8dcSSimon Schubert 	  switch (TYPE_FIELD_LOC_KIND (type, i))
34695796c8dcSSimon Schubert 	    {
34705796c8dcSSimon Schubert 	    case FIELD_LOC_KIND_BITPOS:
34715796c8dcSSimon Schubert 	      SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
34725796c8dcSSimon Schubert 				TYPE_FIELD_BITPOS (type, i));
34735796c8dcSSimon Schubert 	      break;
3474*ef5ccd6cSJohn Marino 	    case FIELD_LOC_KIND_ENUMVAL:
3475*ef5ccd6cSJohn Marino 	      SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
3476*ef5ccd6cSJohn Marino 				 TYPE_FIELD_ENUMVAL (type, i));
3477*ef5ccd6cSJohn Marino 	      break;
34785796c8dcSSimon Schubert 	    case FIELD_LOC_KIND_PHYSADDR:
34795796c8dcSSimon Schubert 	      SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
34805796c8dcSSimon Schubert 				  TYPE_FIELD_STATIC_PHYSADDR (type, i));
34815796c8dcSSimon Schubert 	      break;
34825796c8dcSSimon Schubert 	    case FIELD_LOC_KIND_PHYSNAME:
34835796c8dcSSimon Schubert 	      SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
34845796c8dcSSimon Schubert 				  xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
34855796c8dcSSimon Schubert 								       i)));
34865796c8dcSSimon Schubert 	      break;
34875796c8dcSSimon Schubert 	    default:
34885796c8dcSSimon Schubert 	      internal_error (__FILE__, __LINE__,
34895796c8dcSSimon Schubert 			      _("Unexpected type field location kind: %d"),
34905796c8dcSSimon Schubert 			      TYPE_FIELD_LOC_KIND (type, i));
34915796c8dcSSimon Schubert 	    }
34925796c8dcSSimon Schubert 	}
34935796c8dcSSimon Schubert     }
34945796c8dcSSimon Schubert 
3495cf7f2e2dSJohn Marino   /* For range types, copy the bounds information.  */
3496cf7f2e2dSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3497cf7f2e2dSJohn Marino     {
3498cf7f2e2dSJohn Marino       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
3499cf7f2e2dSJohn Marino       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
3500cf7f2e2dSJohn Marino     }
3501cf7f2e2dSJohn Marino 
35025796c8dcSSimon Schubert   /* Copy pointers to other types.  */
35035796c8dcSSimon Schubert   if (TYPE_TARGET_TYPE (type))
35045796c8dcSSimon Schubert     TYPE_TARGET_TYPE (new_type) =
35055796c8dcSSimon Schubert       copy_type_recursive (objfile,
35065796c8dcSSimon Schubert 			   TYPE_TARGET_TYPE (type),
35075796c8dcSSimon Schubert 			   copied_types);
35085796c8dcSSimon Schubert   if (TYPE_VPTR_BASETYPE (type))
35095796c8dcSSimon Schubert     TYPE_VPTR_BASETYPE (new_type) =
35105796c8dcSSimon Schubert       copy_type_recursive (objfile,
35115796c8dcSSimon Schubert 			   TYPE_VPTR_BASETYPE (type),
35125796c8dcSSimon Schubert 			   copied_types);
35135796c8dcSSimon Schubert   /* Maybe copy the type_specific bits.
35145796c8dcSSimon Schubert 
35155796c8dcSSimon Schubert      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
35165796c8dcSSimon Schubert      base classes and methods.  There's no fundamental reason why we
35175796c8dcSSimon Schubert      can't, but at the moment it is not needed.  */
35185796c8dcSSimon Schubert 
35195796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FLT)
35205796c8dcSSimon Schubert     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
35215796c8dcSSimon Schubert   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
35225796c8dcSSimon Schubert 	   || TYPE_CODE (type) == TYPE_CODE_UNION
35235796c8dcSSimon Schubert 	   || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
35245796c8dcSSimon Schubert     INIT_CPLUS_SPECIFIC (new_type);
35255796c8dcSSimon Schubert 
35265796c8dcSSimon Schubert   return new_type;
35275796c8dcSSimon Schubert }
35285796c8dcSSimon Schubert 
35295796c8dcSSimon Schubert /* Make a copy of the given TYPE, except that the pointer & reference
35305796c8dcSSimon Schubert    types are not preserved.
35315796c8dcSSimon Schubert 
35325796c8dcSSimon Schubert    This function assumes that the given type has an associated objfile.
35335796c8dcSSimon Schubert    This objfile is used to allocate the new type.  */
35345796c8dcSSimon Schubert 
35355796c8dcSSimon Schubert struct type *
copy_type(const struct type * type)35365796c8dcSSimon Schubert copy_type (const struct type *type)
35375796c8dcSSimon Schubert {
35385796c8dcSSimon Schubert   struct type *new_type;
35395796c8dcSSimon Schubert 
35405796c8dcSSimon Schubert   gdb_assert (TYPE_OBJFILE_OWNED (type));
35415796c8dcSSimon Schubert 
35425796c8dcSSimon Schubert   new_type = alloc_type_copy (type);
35435796c8dcSSimon Schubert   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
35445796c8dcSSimon Schubert   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
35455796c8dcSSimon Schubert   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
35465796c8dcSSimon Schubert 	  sizeof (struct main_type));
35475796c8dcSSimon Schubert 
35485796c8dcSSimon Schubert   return new_type;
35495796c8dcSSimon Schubert }
3550*ef5ccd6cSJohn Marino 
35515796c8dcSSimon Schubert /* Helper functions to initialize architecture-specific types.  */
35525796c8dcSSimon Schubert 
35535796c8dcSSimon Schubert /* Allocate a type structure associated with GDBARCH and set its
35545796c8dcSSimon Schubert    CODE, LENGTH, and NAME fields.  */
3555*ef5ccd6cSJohn Marino 
35565796c8dcSSimon Schubert struct type *
arch_type(struct gdbarch * gdbarch,enum type_code code,int length,char * name)35575796c8dcSSimon Schubert arch_type (struct gdbarch *gdbarch,
35585796c8dcSSimon Schubert 	   enum type_code code, int length, char *name)
35595796c8dcSSimon Schubert {
35605796c8dcSSimon Schubert   struct type *type;
35615796c8dcSSimon Schubert 
35625796c8dcSSimon Schubert   type = alloc_type_arch (gdbarch);
35635796c8dcSSimon Schubert   TYPE_CODE (type) = code;
35645796c8dcSSimon Schubert   TYPE_LENGTH (type) = length;
35655796c8dcSSimon Schubert 
35665796c8dcSSimon Schubert   if (name)
35675796c8dcSSimon Schubert     TYPE_NAME (type) = xstrdup (name);
35685796c8dcSSimon Schubert 
35695796c8dcSSimon Schubert   return type;
35705796c8dcSSimon Schubert }
35715796c8dcSSimon Schubert 
35725796c8dcSSimon Schubert /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
35735796c8dcSSimon Schubert    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
35745796c8dcSSimon Schubert    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3575*ef5ccd6cSJohn Marino 
35765796c8dcSSimon Schubert struct type *
arch_integer_type(struct gdbarch * gdbarch,int bit,int unsigned_p,char * name)35775796c8dcSSimon Schubert arch_integer_type (struct gdbarch *gdbarch,
35785796c8dcSSimon Schubert 		   int bit, int unsigned_p, char *name)
35795796c8dcSSimon Schubert {
35805796c8dcSSimon Schubert   struct type *t;
35815796c8dcSSimon Schubert 
35825796c8dcSSimon Schubert   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
35835796c8dcSSimon Schubert   if (unsigned_p)
35845796c8dcSSimon Schubert     TYPE_UNSIGNED (t) = 1;
35855796c8dcSSimon Schubert   if (name && strcmp (name, "char") == 0)
35865796c8dcSSimon Schubert     TYPE_NOSIGN (t) = 1;
35875796c8dcSSimon Schubert 
35885796c8dcSSimon Schubert   return t;
35895796c8dcSSimon Schubert }
35905796c8dcSSimon Schubert 
35915796c8dcSSimon Schubert /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
35925796c8dcSSimon Schubert    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
35935796c8dcSSimon Schubert    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3594*ef5ccd6cSJohn Marino 
35955796c8dcSSimon Schubert struct type *
arch_character_type(struct gdbarch * gdbarch,int bit,int unsigned_p,char * name)35965796c8dcSSimon Schubert arch_character_type (struct gdbarch *gdbarch,
35975796c8dcSSimon Schubert 		     int bit, int unsigned_p, char *name)
35985796c8dcSSimon Schubert {
35995796c8dcSSimon Schubert   struct type *t;
36005796c8dcSSimon Schubert 
36015796c8dcSSimon Schubert   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
36025796c8dcSSimon Schubert   if (unsigned_p)
36035796c8dcSSimon Schubert     TYPE_UNSIGNED (t) = 1;
36045796c8dcSSimon Schubert 
36055796c8dcSSimon Schubert   return t;
36065796c8dcSSimon Schubert }
36075796c8dcSSimon Schubert 
36085796c8dcSSimon Schubert /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
36095796c8dcSSimon Schubert    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
36105796c8dcSSimon Schubert    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3611*ef5ccd6cSJohn Marino 
36125796c8dcSSimon Schubert struct type *
arch_boolean_type(struct gdbarch * gdbarch,int bit,int unsigned_p,char * name)36135796c8dcSSimon Schubert arch_boolean_type (struct gdbarch *gdbarch,
36145796c8dcSSimon Schubert 		   int bit, int unsigned_p, char *name)
36155796c8dcSSimon Schubert {
36165796c8dcSSimon Schubert   struct type *t;
36175796c8dcSSimon Schubert 
36185796c8dcSSimon Schubert   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
36195796c8dcSSimon Schubert   if (unsigned_p)
36205796c8dcSSimon Schubert     TYPE_UNSIGNED (t) = 1;
36215796c8dcSSimon Schubert 
36225796c8dcSSimon Schubert   return t;
36235796c8dcSSimon Schubert }
36245796c8dcSSimon Schubert 
36255796c8dcSSimon Schubert /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
36265796c8dcSSimon Schubert    BIT is the type size in bits; if BIT equals -1, the size is
36275796c8dcSSimon Schubert    determined by the floatformat.  NAME is the type name.  Set the
36285796c8dcSSimon Schubert    TYPE_FLOATFORMAT from FLOATFORMATS.  */
3629*ef5ccd6cSJohn Marino 
36305796c8dcSSimon Schubert struct type *
arch_float_type(struct gdbarch * gdbarch,int bit,char * name,const struct floatformat ** floatformats)36315796c8dcSSimon Schubert arch_float_type (struct gdbarch *gdbarch,
36325796c8dcSSimon Schubert 		 int bit, char *name, const struct floatformat **floatformats)
36335796c8dcSSimon Schubert {
36345796c8dcSSimon Schubert   struct type *t;
36355796c8dcSSimon Schubert 
36365796c8dcSSimon Schubert   if (bit == -1)
36375796c8dcSSimon Schubert     {
36385796c8dcSSimon Schubert       gdb_assert (floatformats != NULL);
36395796c8dcSSimon Schubert       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
36405796c8dcSSimon Schubert       bit = floatformats[0]->totalsize;
36415796c8dcSSimon Schubert     }
36425796c8dcSSimon Schubert   gdb_assert (bit >= 0);
36435796c8dcSSimon Schubert 
36445796c8dcSSimon Schubert   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
36455796c8dcSSimon Schubert   TYPE_FLOATFORMAT (t) = floatformats;
36465796c8dcSSimon Schubert   return t;
36475796c8dcSSimon Schubert }
36485796c8dcSSimon Schubert 
36495796c8dcSSimon Schubert /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
36505796c8dcSSimon Schubert    NAME is the type name.  TARGET_TYPE is the component float type.  */
3651*ef5ccd6cSJohn Marino 
36525796c8dcSSimon Schubert struct type *
arch_complex_type(struct gdbarch * gdbarch,char * name,struct type * target_type)36535796c8dcSSimon Schubert arch_complex_type (struct gdbarch *gdbarch,
36545796c8dcSSimon Schubert 		   char *name, struct type *target_type)
36555796c8dcSSimon Schubert {
36565796c8dcSSimon Schubert   struct type *t;
3657cf7f2e2dSJohn Marino 
36585796c8dcSSimon Schubert   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
36595796c8dcSSimon Schubert 		 2 * TYPE_LENGTH (target_type), name);
36605796c8dcSSimon Schubert   TYPE_TARGET_TYPE (t) = target_type;
36615796c8dcSSimon Schubert   return t;
36625796c8dcSSimon Schubert }
36635796c8dcSSimon Schubert 
36645796c8dcSSimon Schubert /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
3665cf7f2e2dSJohn Marino    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */
3666*ef5ccd6cSJohn Marino 
36675796c8dcSSimon Schubert struct type *
arch_flags_type(struct gdbarch * gdbarch,char * name,int length)36685796c8dcSSimon Schubert arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
36695796c8dcSSimon Schubert {
36705796c8dcSSimon Schubert   int nfields = length * TARGET_CHAR_BIT;
36715796c8dcSSimon Schubert   struct type *type;
36725796c8dcSSimon Schubert 
36735796c8dcSSimon Schubert   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
36745796c8dcSSimon Schubert   TYPE_UNSIGNED (type) = 1;
36755796c8dcSSimon Schubert   TYPE_NFIELDS (type) = nfields;
36765796c8dcSSimon Schubert   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
36775796c8dcSSimon Schubert 
36785796c8dcSSimon Schubert   return type;
36795796c8dcSSimon Schubert }
36805796c8dcSSimon Schubert 
36815796c8dcSSimon Schubert /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
36825796c8dcSSimon Schubert    position BITPOS is called NAME.  */
3683*ef5ccd6cSJohn Marino 
36845796c8dcSSimon Schubert void
append_flags_type_flag(struct type * type,int bitpos,char * name)36855796c8dcSSimon Schubert append_flags_type_flag (struct type *type, int bitpos, char *name)
36865796c8dcSSimon Schubert {
36875796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
36885796c8dcSSimon Schubert   gdb_assert (bitpos < TYPE_NFIELDS (type));
36895796c8dcSSimon Schubert   gdb_assert (bitpos >= 0);
36905796c8dcSSimon Schubert 
36915796c8dcSSimon Schubert   if (name)
36925796c8dcSSimon Schubert     {
36935796c8dcSSimon Schubert       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
3694*ef5ccd6cSJohn Marino       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
36955796c8dcSSimon Schubert     }
36965796c8dcSSimon Schubert   else
36975796c8dcSSimon Schubert     {
36985796c8dcSSimon Schubert       /* Don't show this field to the user.  */
3699*ef5ccd6cSJohn Marino       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
37005796c8dcSSimon Schubert     }
37015796c8dcSSimon Schubert }
37025796c8dcSSimon Schubert 
37035796c8dcSSimon Schubert /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
37045796c8dcSSimon Schubert    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
3705*ef5ccd6cSJohn Marino 
37065796c8dcSSimon Schubert struct type *
arch_composite_type(struct gdbarch * gdbarch,char * name,enum type_code code)37075796c8dcSSimon Schubert arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
37085796c8dcSSimon Schubert {
37095796c8dcSSimon Schubert   struct type *t;
3710cf7f2e2dSJohn Marino 
37115796c8dcSSimon Schubert   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
37125796c8dcSSimon Schubert   t = arch_type (gdbarch, code, 0, NULL);
37135796c8dcSSimon Schubert   TYPE_TAG_NAME (t) = name;
37145796c8dcSSimon Schubert   INIT_CPLUS_SPECIFIC (t);
37155796c8dcSSimon Schubert   return t;
37165796c8dcSSimon Schubert }
37175796c8dcSSimon Schubert 
37185796c8dcSSimon Schubert /* Add new field with name NAME and type FIELD to composite type T.
3719cf7f2e2dSJohn Marino    Do not set the field's position or adjust the type's length;
3720cf7f2e2dSJohn Marino    the caller should do so.  Return the new field.  */
3721*ef5ccd6cSJohn Marino 
3722cf7f2e2dSJohn Marino struct field *
append_composite_type_field_raw(struct type * t,char * name,struct type * field)3723cf7f2e2dSJohn Marino append_composite_type_field_raw (struct type *t, char *name,
3724cf7f2e2dSJohn Marino 				 struct type *field)
37255796c8dcSSimon Schubert {
37265796c8dcSSimon Schubert   struct field *f;
3727cf7f2e2dSJohn Marino 
37285796c8dcSSimon Schubert   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
37295796c8dcSSimon Schubert   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
37305796c8dcSSimon Schubert 			      sizeof (struct field) * TYPE_NFIELDS (t));
37315796c8dcSSimon Schubert   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
37325796c8dcSSimon Schubert   memset (f, 0, sizeof f[0]);
37335796c8dcSSimon Schubert   FIELD_TYPE (f[0]) = field;
37345796c8dcSSimon Schubert   FIELD_NAME (f[0]) = name;
3735cf7f2e2dSJohn Marino   return f;
3736cf7f2e2dSJohn Marino }
3737cf7f2e2dSJohn Marino 
3738cf7f2e2dSJohn Marino /* Add new field with name NAME and type FIELD to composite type T.
3739cf7f2e2dSJohn Marino    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
3740*ef5ccd6cSJohn Marino 
3741cf7f2e2dSJohn Marino void
append_composite_type_field_aligned(struct type * t,char * name,struct type * field,int alignment)3742cf7f2e2dSJohn Marino append_composite_type_field_aligned (struct type *t, char *name,
3743cf7f2e2dSJohn Marino 				     struct type *field, int alignment)
3744cf7f2e2dSJohn Marino {
3745cf7f2e2dSJohn Marino   struct field *f = append_composite_type_field_raw (t, name, field);
3746cf7f2e2dSJohn Marino 
37475796c8dcSSimon Schubert   if (TYPE_CODE (t) == TYPE_CODE_UNION)
37485796c8dcSSimon Schubert     {
37495796c8dcSSimon Schubert       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
37505796c8dcSSimon Schubert 	TYPE_LENGTH (t) = TYPE_LENGTH (field);
37515796c8dcSSimon Schubert     }
37525796c8dcSSimon Schubert   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
37535796c8dcSSimon Schubert     {
37545796c8dcSSimon Schubert       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
37555796c8dcSSimon Schubert       if (TYPE_NFIELDS (t) > 1)
37565796c8dcSSimon Schubert 	{
3757*ef5ccd6cSJohn Marino 	  SET_FIELD_BITPOS (f[0],
3758*ef5ccd6cSJohn Marino 			    (FIELD_BITPOS (f[-1])
37595796c8dcSSimon Schubert 			     + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
3760*ef5ccd6cSJohn Marino 				* TARGET_CHAR_BIT)));
37615796c8dcSSimon Schubert 
37625796c8dcSSimon Schubert 	  if (alignment)
37635796c8dcSSimon Schubert 	    {
3764a45ae5f8SJohn Marino 	      int left;
3765a45ae5f8SJohn Marino 
3766a45ae5f8SJohn Marino 	      alignment *= TARGET_CHAR_BIT;
3767a45ae5f8SJohn Marino 	      left = FIELD_BITPOS (f[0]) % alignment;
3768cf7f2e2dSJohn Marino 
37695796c8dcSSimon Schubert 	      if (left)
37705796c8dcSSimon Schubert 		{
3771*ef5ccd6cSJohn Marino 		  SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
3772a45ae5f8SJohn Marino 		  TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
37735796c8dcSSimon Schubert 		}
37745796c8dcSSimon Schubert 	    }
37755796c8dcSSimon Schubert 	}
37765796c8dcSSimon Schubert     }
37775796c8dcSSimon Schubert }
37785796c8dcSSimon Schubert 
37795796c8dcSSimon Schubert /* Add new field with name NAME and type FIELD to composite type T.  */
3780*ef5ccd6cSJohn Marino 
37815796c8dcSSimon Schubert void
append_composite_type_field(struct type * t,char * name,struct type * field)37825796c8dcSSimon Schubert append_composite_type_field (struct type *t, char *name,
37835796c8dcSSimon Schubert 			     struct type *field)
37845796c8dcSSimon Schubert {
37855796c8dcSSimon Schubert   append_composite_type_field_aligned (t, name, field, 0);
37865796c8dcSSimon Schubert }
37875796c8dcSSimon Schubert 
37885796c8dcSSimon Schubert static struct gdbarch_data *gdbtypes_data;
37895796c8dcSSimon Schubert 
37905796c8dcSSimon Schubert const struct builtin_type *
builtin_type(struct gdbarch * gdbarch)37915796c8dcSSimon Schubert builtin_type (struct gdbarch *gdbarch)
37925796c8dcSSimon Schubert {
37935796c8dcSSimon Schubert   return gdbarch_data (gdbarch, gdbtypes_data);
37945796c8dcSSimon Schubert }
37955796c8dcSSimon Schubert 
37965796c8dcSSimon Schubert static void *
gdbtypes_post_init(struct gdbarch * gdbarch)37975796c8dcSSimon Schubert gdbtypes_post_init (struct gdbarch *gdbarch)
37985796c8dcSSimon Schubert {
37995796c8dcSSimon Schubert   struct builtin_type *builtin_type
38005796c8dcSSimon Schubert     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
38015796c8dcSSimon Schubert 
38025796c8dcSSimon Schubert   /* Basic types.  */
38035796c8dcSSimon Schubert   builtin_type->builtin_void
38045796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
38055796c8dcSSimon Schubert   builtin_type->builtin_char
38065796c8dcSSimon Schubert     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
38075796c8dcSSimon Schubert 			 !gdbarch_char_signed (gdbarch), "char");
38085796c8dcSSimon Schubert   builtin_type->builtin_signed_char
38095796c8dcSSimon Schubert     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
38105796c8dcSSimon Schubert 			 0, "signed char");
38115796c8dcSSimon Schubert   builtin_type->builtin_unsigned_char
38125796c8dcSSimon Schubert     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
38135796c8dcSSimon Schubert 			 1, "unsigned char");
38145796c8dcSSimon Schubert   builtin_type->builtin_short
38155796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
38165796c8dcSSimon Schubert 			 0, "short");
38175796c8dcSSimon Schubert   builtin_type->builtin_unsigned_short
38185796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
38195796c8dcSSimon Schubert 			 1, "unsigned short");
38205796c8dcSSimon Schubert   builtin_type->builtin_int
38215796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
38225796c8dcSSimon Schubert 			 0, "int");
38235796c8dcSSimon Schubert   builtin_type->builtin_unsigned_int
38245796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
38255796c8dcSSimon Schubert 			 1, "unsigned int");
38265796c8dcSSimon Schubert   builtin_type->builtin_long
38275796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
38285796c8dcSSimon Schubert 			 0, "long");
38295796c8dcSSimon Schubert   builtin_type->builtin_unsigned_long
38305796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
38315796c8dcSSimon Schubert 			 1, "unsigned long");
38325796c8dcSSimon Schubert   builtin_type->builtin_long_long
38335796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
38345796c8dcSSimon Schubert 			 0, "long long");
38355796c8dcSSimon Schubert   builtin_type->builtin_unsigned_long_long
38365796c8dcSSimon Schubert     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
38375796c8dcSSimon Schubert 			 1, "unsigned long long");
38385796c8dcSSimon Schubert   builtin_type->builtin_float
38395796c8dcSSimon Schubert     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
38405796c8dcSSimon Schubert 		       "float", gdbarch_float_format (gdbarch));
38415796c8dcSSimon Schubert   builtin_type->builtin_double
38425796c8dcSSimon Schubert     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
38435796c8dcSSimon Schubert 		       "double", gdbarch_double_format (gdbarch));
38445796c8dcSSimon Schubert   builtin_type->builtin_long_double
38455796c8dcSSimon Schubert     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
38465796c8dcSSimon Schubert 		       "long double", gdbarch_long_double_format (gdbarch));
38475796c8dcSSimon Schubert   builtin_type->builtin_complex
38485796c8dcSSimon Schubert     = arch_complex_type (gdbarch, "complex",
38495796c8dcSSimon Schubert 			 builtin_type->builtin_float);
38505796c8dcSSimon Schubert   builtin_type->builtin_double_complex
38515796c8dcSSimon Schubert     = arch_complex_type (gdbarch, "double complex",
38525796c8dcSSimon Schubert 			 builtin_type->builtin_double);
38535796c8dcSSimon Schubert   builtin_type->builtin_string
38545796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
38555796c8dcSSimon Schubert   builtin_type->builtin_bool
38565796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
38575796c8dcSSimon Schubert 
38585796c8dcSSimon Schubert   /* The following three are about decimal floating point types, which
38595796c8dcSSimon Schubert      are 32-bits, 64-bits and 128-bits respectively.  */
38605796c8dcSSimon Schubert   builtin_type->builtin_decfloat
38615796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
38625796c8dcSSimon Schubert   builtin_type->builtin_decdouble
38635796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
38645796c8dcSSimon Schubert   builtin_type->builtin_declong
38655796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
38665796c8dcSSimon Schubert 
38675796c8dcSSimon Schubert   /* "True" character types.  */
38685796c8dcSSimon Schubert   builtin_type->builtin_true_char
38695796c8dcSSimon Schubert     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
38705796c8dcSSimon Schubert   builtin_type->builtin_true_unsigned_char
38715796c8dcSSimon Schubert     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
38725796c8dcSSimon Schubert 
38735796c8dcSSimon Schubert   /* Fixed-size integer types.  */
38745796c8dcSSimon Schubert   builtin_type->builtin_int0
38755796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 0, 0, "int0_t");
38765796c8dcSSimon Schubert   builtin_type->builtin_int8
38775796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 8, 0, "int8_t");
38785796c8dcSSimon Schubert   builtin_type->builtin_uint8
38795796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
38805796c8dcSSimon Schubert   builtin_type->builtin_int16
38815796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 16, 0, "int16_t");
38825796c8dcSSimon Schubert   builtin_type->builtin_uint16
38835796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
38845796c8dcSSimon Schubert   builtin_type->builtin_int32
38855796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 32, 0, "int32_t");
38865796c8dcSSimon Schubert   builtin_type->builtin_uint32
38875796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
38885796c8dcSSimon Schubert   builtin_type->builtin_int64
38895796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 64, 0, "int64_t");
38905796c8dcSSimon Schubert   builtin_type->builtin_uint64
38915796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
38925796c8dcSSimon Schubert   builtin_type->builtin_int128
38935796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 128, 0, "int128_t");
38945796c8dcSSimon Schubert   builtin_type->builtin_uint128
38955796c8dcSSimon Schubert     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
3896c50c785cSJohn Marino   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
3897c50c785cSJohn Marino     TYPE_INSTANCE_FLAG_NOTTEXT;
3898c50c785cSJohn Marino   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
3899c50c785cSJohn Marino     TYPE_INSTANCE_FLAG_NOTTEXT;
39005796c8dcSSimon Schubert 
3901cf7f2e2dSJohn Marino   /* Wide character types.  */
3902cf7f2e2dSJohn Marino   builtin_type->builtin_char16
3903cf7f2e2dSJohn Marino     = arch_integer_type (gdbarch, 16, 0, "char16_t");
3904cf7f2e2dSJohn Marino   builtin_type->builtin_char32
3905cf7f2e2dSJohn Marino     = arch_integer_type (gdbarch, 32, 0, "char32_t");
3906cf7f2e2dSJohn Marino 
3907cf7f2e2dSJohn Marino 
39085796c8dcSSimon Schubert   /* Default data/code pointer types.  */
39095796c8dcSSimon Schubert   builtin_type->builtin_data_ptr
39105796c8dcSSimon Schubert     = lookup_pointer_type (builtin_type->builtin_void);
39115796c8dcSSimon Schubert   builtin_type->builtin_func_ptr
39125796c8dcSSimon Schubert     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
3913c50c785cSJohn Marino   builtin_type->builtin_func_func
3914c50c785cSJohn Marino     = lookup_function_type (builtin_type->builtin_func_ptr);
39155796c8dcSSimon Schubert 
39165796c8dcSSimon Schubert   /* This type represents a GDB internal function.  */
39175796c8dcSSimon Schubert   builtin_type->internal_fn
39185796c8dcSSimon Schubert     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
39195796c8dcSSimon Schubert 		 "<internal function>");
39205796c8dcSSimon Schubert 
39215796c8dcSSimon Schubert   return builtin_type;
39225796c8dcSSimon Schubert }
39235796c8dcSSimon Schubert 
39245796c8dcSSimon Schubert /* This set of objfile-based types is intended to be used by symbol
39255796c8dcSSimon Schubert    readers as basic types.  */
39265796c8dcSSimon Schubert 
39275796c8dcSSimon Schubert static const struct objfile_data *objfile_type_data;
39285796c8dcSSimon Schubert 
39295796c8dcSSimon Schubert const struct objfile_type *
objfile_type(struct objfile * objfile)39305796c8dcSSimon Schubert objfile_type (struct objfile *objfile)
39315796c8dcSSimon Schubert {
39325796c8dcSSimon Schubert   struct gdbarch *gdbarch;
39335796c8dcSSimon Schubert   struct objfile_type *objfile_type
39345796c8dcSSimon Schubert     = objfile_data (objfile, objfile_type_data);
39355796c8dcSSimon Schubert 
39365796c8dcSSimon Schubert   if (objfile_type)
39375796c8dcSSimon Schubert     return objfile_type;
39385796c8dcSSimon Schubert 
39395796c8dcSSimon Schubert   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
39405796c8dcSSimon Schubert 				 1, struct objfile_type);
39415796c8dcSSimon Schubert 
39425796c8dcSSimon Schubert   /* Use the objfile architecture to determine basic type properties.  */
39435796c8dcSSimon Schubert   gdbarch = get_objfile_arch (objfile);
39445796c8dcSSimon Schubert 
39455796c8dcSSimon Schubert   /* Basic types.  */
39465796c8dcSSimon Schubert   objfile_type->builtin_void
39475796c8dcSSimon Schubert     = init_type (TYPE_CODE_VOID, 1,
39485796c8dcSSimon Schubert 		 0,
39495796c8dcSSimon Schubert 		 "void", objfile);
39505796c8dcSSimon Schubert 
39515796c8dcSSimon Schubert   objfile_type->builtin_char
39525796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
39535796c8dcSSimon Schubert 		 (TYPE_FLAG_NOSIGN
39545796c8dcSSimon Schubert 		  | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
39555796c8dcSSimon Schubert 		 "char", objfile);
39565796c8dcSSimon Schubert   objfile_type->builtin_signed_char
39575796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
39585796c8dcSSimon Schubert 		 0,
39595796c8dcSSimon Schubert 		 "signed char", objfile);
39605796c8dcSSimon Schubert   objfile_type->builtin_unsigned_char
39615796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
39625796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED,
39635796c8dcSSimon Schubert 		 "unsigned char", objfile);
39645796c8dcSSimon Schubert   objfile_type->builtin_short
39655796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39665796c8dcSSimon Schubert 		 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
39675796c8dcSSimon Schubert 		 0, "short", objfile);
39685796c8dcSSimon Schubert   objfile_type->builtin_unsigned_short
39695796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39705796c8dcSSimon Schubert 		 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
39715796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
39725796c8dcSSimon Schubert   objfile_type->builtin_int
39735796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39745796c8dcSSimon Schubert 		 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
39755796c8dcSSimon Schubert 		 0, "int", objfile);
39765796c8dcSSimon Schubert   objfile_type->builtin_unsigned_int
39775796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39785796c8dcSSimon Schubert 		 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
39795796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
39805796c8dcSSimon Schubert   objfile_type->builtin_long
39815796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39825796c8dcSSimon Schubert 		 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
39835796c8dcSSimon Schubert 		 0, "long", objfile);
39845796c8dcSSimon Schubert   objfile_type->builtin_unsigned_long
39855796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39865796c8dcSSimon Schubert 		 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
39875796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
39885796c8dcSSimon Schubert   objfile_type->builtin_long_long
39895796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39905796c8dcSSimon Schubert 		 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
39915796c8dcSSimon Schubert 		 0, "long long", objfile);
39925796c8dcSSimon Schubert   objfile_type->builtin_unsigned_long_long
39935796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
39945796c8dcSSimon Schubert 		 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
39955796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
39965796c8dcSSimon Schubert 
39975796c8dcSSimon Schubert   objfile_type->builtin_float
39985796c8dcSSimon Schubert     = init_type (TYPE_CODE_FLT,
39995796c8dcSSimon Schubert 		 gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
40005796c8dcSSimon Schubert 		 0, "float", objfile);
40015796c8dcSSimon Schubert   TYPE_FLOATFORMAT (objfile_type->builtin_float)
40025796c8dcSSimon Schubert     = gdbarch_float_format (gdbarch);
40035796c8dcSSimon Schubert   objfile_type->builtin_double
40045796c8dcSSimon Schubert     = init_type (TYPE_CODE_FLT,
40055796c8dcSSimon Schubert 		 gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
40065796c8dcSSimon Schubert 		 0, "double", objfile);
40075796c8dcSSimon Schubert   TYPE_FLOATFORMAT (objfile_type->builtin_double)
40085796c8dcSSimon Schubert     = gdbarch_double_format (gdbarch);
40095796c8dcSSimon Schubert   objfile_type->builtin_long_double
40105796c8dcSSimon Schubert     = init_type (TYPE_CODE_FLT,
40115796c8dcSSimon Schubert 		 gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
40125796c8dcSSimon Schubert 		 0, "long double", objfile);
40135796c8dcSSimon Schubert   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
40145796c8dcSSimon Schubert     = gdbarch_long_double_format (gdbarch);
40155796c8dcSSimon Schubert 
40165796c8dcSSimon Schubert   /* This type represents a type that was unrecognized in symbol read-in.  */
40175796c8dcSSimon Schubert   objfile_type->builtin_error
40185796c8dcSSimon Schubert     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
40195796c8dcSSimon Schubert 
40205796c8dcSSimon Schubert   /* The following set of types is used for symbols with no
40215796c8dcSSimon Schubert      debug information.  */
40225796c8dcSSimon Schubert   objfile_type->nodebug_text_symbol
40235796c8dcSSimon Schubert     = init_type (TYPE_CODE_FUNC, 1, 0,
40245796c8dcSSimon Schubert 		 "<text variable, no debug info>", objfile);
40255796c8dcSSimon Schubert   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
40265796c8dcSSimon Schubert     = objfile_type->builtin_int;
4027c50c785cSJohn Marino   objfile_type->nodebug_text_gnu_ifunc_symbol
4028c50c785cSJohn Marino     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4029c50c785cSJohn Marino 		 "<text gnu-indirect-function variable, no debug info>",
4030c50c785cSJohn Marino 		 objfile);
4031c50c785cSJohn Marino   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4032c50c785cSJohn Marino     = objfile_type->nodebug_text_symbol;
4033c50c785cSJohn Marino   objfile_type->nodebug_got_plt_symbol
4034c50c785cSJohn Marino     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4035c50c785cSJohn Marino 		 "<text from jump slot in .got.plt, no debug info>",
4036c50c785cSJohn Marino 		 objfile);
4037c50c785cSJohn Marino   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4038c50c785cSJohn Marino     = objfile_type->nodebug_text_symbol;
40395796c8dcSSimon Schubert   objfile_type->nodebug_data_symbol
40405796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
40415796c8dcSSimon Schubert 		 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
40425796c8dcSSimon Schubert 		 "<data variable, no debug info>", objfile);
40435796c8dcSSimon Schubert   objfile_type->nodebug_unknown_symbol
40445796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT, 1, 0,
40455796c8dcSSimon Schubert 		 "<variable (not text or data), no debug info>", objfile);
40465796c8dcSSimon Schubert   objfile_type->nodebug_tls_symbol
40475796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
40485796c8dcSSimon Schubert 		 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
40495796c8dcSSimon Schubert 		 "<thread local variable, no debug info>", objfile);
40505796c8dcSSimon Schubert 
40515796c8dcSSimon Schubert   /* NOTE: on some targets, addresses and pointers are not necessarily
40525796c8dcSSimon Schubert      the same --- for example, on the D10V, pointers are 16 bits long,
40535796c8dcSSimon Schubert      but addresses are 32 bits long.  See doc/gdbint.texinfo,
40545796c8dcSSimon Schubert      ``Pointers Are Not Always Addresses''.
40555796c8dcSSimon Schubert 
40565796c8dcSSimon Schubert      The upshot is:
40575796c8dcSSimon Schubert      - gdb's `struct type' always describes the target's
40585796c8dcSSimon Schubert        representation.
40595796c8dcSSimon Schubert      - gdb's `struct value' objects should always hold values in
40605796c8dcSSimon Schubert        target form.
40615796c8dcSSimon Schubert      - gdb's CORE_ADDR values are addresses in the unified virtual
40625796c8dcSSimon Schubert        address space that the assembler and linker work with.  Thus,
40635796c8dcSSimon Schubert        since target_read_memory takes a CORE_ADDR as an argument, it
40645796c8dcSSimon Schubert        can access any memory on the target, even if the processor has
40655796c8dcSSimon Schubert        separate code and data address spaces.
40665796c8dcSSimon Schubert 
40675796c8dcSSimon Schubert      So, for example:
40685796c8dcSSimon Schubert      - If v is a value holding a D10V code pointer, its contents are
40695796c8dcSSimon Schubert        in target form: a big-endian address left-shifted two bits.
40705796c8dcSSimon Schubert      - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
40715796c8dcSSimon Schubert        sizeof (void *) == 2 on the target.
40725796c8dcSSimon Schubert 
40735796c8dcSSimon Schubert      In this context, objfile_type->builtin_core_addr is a bit odd:
40745796c8dcSSimon Schubert      it's a target type for a value the target will never see.  It's
40755796c8dcSSimon Schubert      only used to hold the values of (typeless) linker symbols, which
40765796c8dcSSimon Schubert      are indeed in the unified virtual address space.  */
40775796c8dcSSimon Schubert 
40785796c8dcSSimon Schubert   objfile_type->builtin_core_addr
40795796c8dcSSimon Schubert     = init_type (TYPE_CODE_INT,
40805796c8dcSSimon Schubert 		 gdbarch_addr_bit (gdbarch) / 8,
40815796c8dcSSimon Schubert 		 TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
40825796c8dcSSimon Schubert 
40835796c8dcSSimon Schubert   set_objfile_data (objfile, objfile_type_data, objfile_type);
40845796c8dcSSimon Schubert   return objfile_type;
40855796c8dcSSimon Schubert }
40865796c8dcSSimon Schubert 
4087*ef5ccd6cSJohn Marino extern initialize_file_ftype _initialize_gdbtypes;
40885796c8dcSSimon Schubert 
40895796c8dcSSimon Schubert void
_initialize_gdbtypes(void)40905796c8dcSSimon Schubert _initialize_gdbtypes (void)
40915796c8dcSSimon Schubert {
40925796c8dcSSimon Schubert   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
40935796c8dcSSimon Schubert   objfile_type_data = register_objfile_data ();
40945796c8dcSSimon Schubert 
4095*ef5ccd6cSJohn Marino   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4096c50c785cSJohn Marino 			     _("Set debugging of C++ overloading."),
4097c50c785cSJohn Marino 			     _("Show debugging of C++ overloading."),
4098c50c785cSJohn Marino 			     _("When enabled, ranking of the "
4099c50c785cSJohn Marino 			       "functions is displayed."),
41005796c8dcSSimon Schubert 			     NULL,
41015796c8dcSSimon Schubert 			     show_overload_debug,
41025796c8dcSSimon Schubert 			     &setdebuglist, &showdebuglist);
41035796c8dcSSimon Schubert 
41045796c8dcSSimon Schubert   /* Add user knob for controlling resolution of opaque types.  */
41055796c8dcSSimon Schubert   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
4106c50c785cSJohn Marino 			   &opaque_type_resolution,
4107c50c785cSJohn Marino 			   _("Set resolution of opaque struct/class/union"
4108c50c785cSJohn Marino 			     " types (if set before loading symbols)."),
4109c50c785cSJohn Marino 			   _("Show resolution of opaque struct/class/union"
4110c50c785cSJohn Marino 			     " types (if set before loading symbols)."),
4111c50c785cSJohn Marino 			   NULL, NULL,
41125796c8dcSSimon Schubert 			   show_opaque_type_resolution,
41135796c8dcSSimon Schubert 			   &setlist, &showlist);
4114*ef5ccd6cSJohn Marino 
4115*ef5ccd6cSJohn Marino   /* Add an option to permit non-strict type checking.  */
4116*ef5ccd6cSJohn Marino   add_setshow_boolean_cmd ("type", class_support,
4117*ef5ccd6cSJohn Marino 			   &strict_type_checking,
4118*ef5ccd6cSJohn Marino 			   _("Set strict type checking."),
4119*ef5ccd6cSJohn Marino 			   _("Show strict type checking."),
4120*ef5ccd6cSJohn Marino 			   NULL, NULL,
4121*ef5ccd6cSJohn Marino 			   show_strict_type_checking,
4122*ef5ccd6cSJohn Marino 			   &setchecklist, &showchecklist);
41235796c8dcSSimon Schubert }
4124