* gdbarch.sh (bfd_vma): Remove.
[external/binutils.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
4    2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6    Contributed by Cygnus Support, using pieces from other GDB modules.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor,
23    Boston, MA 02110-1301, USA.  */
24
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include "bfd.h"
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbtypes.h"
32 #include "expression.h"
33 #include "language.h"
34 #include "target.h"
35 #include "value.h"
36 #include "demangle.h"
37 #include "complaints.h"
38 #include "gdbcmd.h"
39 #include "wrapper.h"
40 #include "cp-abi.h"
41 #include "gdb_assert.h"
42 #include "hashtab.h"
43
44 /* These variables point to the objects
45    representing the predefined C data types.  */
46
47 struct type *builtin_type_void;
48 struct type *builtin_type_char;
49 struct type *builtin_type_true_char;
50 struct type *builtin_type_short;
51 struct type *builtin_type_int;
52 struct type *builtin_type_long;
53 struct type *builtin_type_long_long;
54 struct type *builtin_type_signed_char;
55 struct type *builtin_type_unsigned_char;
56 struct type *builtin_type_unsigned_short;
57 struct type *builtin_type_unsigned_int;
58 struct type *builtin_type_unsigned_long;
59 struct type *builtin_type_unsigned_long_long;
60 struct type *builtin_type_float;
61 struct type *builtin_type_double;
62 struct type *builtin_type_long_double;
63 struct type *builtin_type_complex;
64 struct type *builtin_type_double_complex;
65 struct type *builtin_type_string;
66 struct type *builtin_type_int0;
67 struct type *builtin_type_int8;
68 struct type *builtin_type_uint8;
69 struct type *builtin_type_int16;
70 struct type *builtin_type_uint16;
71 struct type *builtin_type_int32;
72 struct type *builtin_type_uint32;
73 struct type *builtin_type_int64;
74 struct type *builtin_type_uint64;
75 struct type *builtin_type_int128;
76 struct type *builtin_type_uint128;
77 struct type *builtin_type_bool;
78
79 /* Floatformat pairs.  */
80 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
81   &floatformat_ieee_single_big,
82   &floatformat_ieee_single_little
83 };
84 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
85   &floatformat_ieee_double_big,
86   &floatformat_ieee_double_little
87 };
88 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
89   &floatformat_ieee_double_big,
90   &floatformat_ieee_double_littlebyte_bigword
91 };
92 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
93   &floatformat_i387_ext,
94   &floatformat_i387_ext
95 };
96 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
97   &floatformat_m68881_ext,
98   &floatformat_m68881_ext
99 };
100 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
101   &floatformat_arm_ext_big,
102   &floatformat_arm_ext_littlebyte_bigword
103 };
104 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
105   &floatformat_ia64_spill_big,
106   &floatformat_ia64_spill_little
107 };
108 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
109   &floatformat_ia64_quad_big,
110   &floatformat_ia64_quad_little
111 };
112 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
113   &floatformat_vax_f,
114   &floatformat_vax_f
115 };
116 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
117   &floatformat_vax_d,
118   &floatformat_vax_d
119 };
120
121 struct type *builtin_type_ieee_single;
122 struct type *builtin_type_ieee_double;
123 struct type *builtin_type_i387_ext;
124 struct type *builtin_type_m68881_ext;
125 struct type *builtin_type_arm_ext;
126 struct type *builtin_type_ia64_spill;
127 struct type *builtin_type_ia64_quad;
128
129 struct type *builtin_type_void_data_ptr;
130 struct type *builtin_type_void_func_ptr;
131 struct type *builtin_type_CORE_ADDR;
132
133 int opaque_type_resolution = 1;
134 static void
135 show_opaque_type_resolution (struct ui_file *file, int from_tty,
136                              struct cmd_list_element *c, const char *value)
137 {
138   fprintf_filtered (file, _("\
139 Resolution of opaque struct/class/union types (if set before loading symbols) is %s.\n"),
140                     value);
141 }
142
143 int overload_debug = 0;
144 static void
145 show_overload_debug (struct ui_file *file, int from_tty,
146                      struct cmd_list_element *c, const char *value)
147 {
148   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), value);
149 }
150
151 struct extra
152   {
153     char str[128];
154     int len;
155   };                            /* maximum extension is 128! FIXME */
156
157 static void print_bit_vector (B_TYPE *, int);
158 static void print_arg_types (struct field *, int, int);
159 static void dump_fn_fieldlists (struct type *, int);
160 static void print_cplus_stuff (struct type *, int);
161 static void virtual_base_list_aux (struct type *dclass);
162
163
164 /* Alloc a new type structure and fill it with some defaults.  If
165    OBJFILE is non-NULL, then allocate the space for the type structure
166    in that objfile's objfile_obstack.  Otherwise allocate the new type structure
167    by xmalloc () (for permanent types).  */
168
169 struct type *
170 alloc_type (struct objfile *objfile)
171 {
172   struct type *type;
173
174   /* Alloc the structure and start off with all fields zeroed. */
175
176   if (objfile == NULL)
177     {
178       type = xmalloc (sizeof (struct type));
179       memset (type, 0, sizeof (struct type));
180       TYPE_MAIN_TYPE (type) = xmalloc (sizeof (struct main_type));
181     }
182   else
183     {
184       type = obstack_alloc (&objfile->objfile_obstack,
185                             sizeof (struct type));
186       memset (type, 0, sizeof (struct type));
187       TYPE_MAIN_TYPE (type) = obstack_alloc (&objfile->objfile_obstack,
188                                              sizeof (struct main_type));
189       OBJSTAT (objfile, n_types++);
190     }
191   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
192
193   /* Initialize the fields that might not be zero. */
194
195   TYPE_CODE (type) = TYPE_CODE_UNDEF;
196   TYPE_OBJFILE (type) = objfile;
197   TYPE_VPTR_FIELDNO (type) = -1;
198   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
199
200   return (type);
201 }
202
203 /* Alloc a new type instance structure, fill it with some defaults,
204    and point it at OLDTYPE.  Allocate the new type instance from the
205    same place as OLDTYPE.  */
206
207 static struct type *
208 alloc_type_instance (struct type *oldtype)
209 {
210   struct type *type;
211
212   /* Allocate the structure.  */
213
214   if (TYPE_OBJFILE (oldtype) == NULL)
215     {
216       type = xmalloc (sizeof (struct type));
217       memset (type, 0, sizeof (struct type));
218     }
219   else
220     {
221       type = obstack_alloc (&TYPE_OBJFILE (oldtype)->objfile_obstack,
222                             sizeof (struct type));
223       memset (type, 0, sizeof (struct type));
224     }
225   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
226
227   TYPE_CHAIN (type) = type;     /* Chain back to itself for now.  */
228
229   return (type);
230 }
231
232 /* Clear all remnants of the previous type at TYPE, in preparation for
233    replacing it with something else.  */
234 static void
235 smash_type (struct type *type)
236 {
237   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
238
239   /* For now, delete the rings.  */
240   TYPE_CHAIN (type) = type;
241
242   /* For now, leave the pointer/reference types alone.  */
243 }
244
245 /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
246    to a pointer to memory where the pointer type should be stored.
247    If *TYPEPTR is zero, update it to point to the pointer type we return.
248    We allocate new memory if needed.  */
249
250 struct type *
251 make_pointer_type (struct type *type, struct type **typeptr)
252 {
253   struct type *ntype;   /* New type */
254   struct objfile *objfile;
255   struct type *chain;
256
257   ntype = TYPE_POINTER_TYPE (type);
258
259   if (ntype)
260     {
261       if (typeptr == 0)
262         return ntype;           /* Don't care about alloc, and have new type.  */
263       else if (*typeptr == 0)
264         {
265           *typeptr = ntype;     /* Tracking alloc, and we have new type.  */
266           return ntype;
267         }
268     }
269
270   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
271     {
272       ntype = alloc_type (TYPE_OBJFILE (type));
273       if (typeptr)
274         *typeptr = ntype;
275     }
276   else
277     /* We have storage, but need to reset it.  */
278     {
279       ntype = *typeptr;
280       objfile = TYPE_OBJFILE (ntype);
281       chain = TYPE_CHAIN (ntype);
282       smash_type (ntype);
283       TYPE_CHAIN (ntype) = chain;
284       TYPE_OBJFILE (ntype) = objfile;
285     }
286
287   TYPE_TARGET_TYPE (ntype) = type;
288   TYPE_POINTER_TYPE (type) = ntype;
289
290   /* FIXME!  Assume the machine has only one representation for pointers!  */
291
292   TYPE_LENGTH (ntype) = gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT;
293   TYPE_CODE (ntype) = TYPE_CODE_PTR;
294
295   /* Mark pointers as unsigned.  The target converts between pointers
296      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
297      gdbarch_address_to_pointer. */
298   TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
299
300   if (!TYPE_POINTER_TYPE (type))        /* Remember it, if don't have one.  */
301     TYPE_POINTER_TYPE (type) = ntype;
302
303   /* Update the length of all the other variants of this type.  */
304   chain = TYPE_CHAIN (ntype);
305   while (chain != ntype)
306     {
307       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
308       chain = TYPE_CHAIN (chain);
309     }
310
311   return ntype;
312 }
313
314 /* Given a type TYPE, return a type of pointers to that type.
315    May need to construct such a type if this is the first use.  */
316
317 struct type *
318 lookup_pointer_type (struct type *type)
319 {
320   return make_pointer_type (type, (struct type **) 0);
321 }
322
323 /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero, points
324    to a pointer to memory where the reference type should be stored.
325    If *TYPEPTR is zero, update it to point to the reference type we return.
326    We allocate new memory if needed.  */
327
328 struct type *
329 make_reference_type (struct type *type, struct type **typeptr)
330 {
331   struct type *ntype;   /* New type */
332   struct objfile *objfile;
333   struct type *chain;
334
335   ntype = TYPE_REFERENCE_TYPE (type);
336
337   if (ntype)
338     {
339       if (typeptr == 0)
340         return ntype;           /* Don't care about alloc, and have new type.  */
341       else if (*typeptr == 0)
342         {
343           *typeptr = ntype;     /* Tracking alloc, and we have new type.  */
344           return ntype;
345         }
346     }
347
348   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
349     {
350       ntype = alloc_type (TYPE_OBJFILE (type));
351       if (typeptr)
352         *typeptr = ntype;
353     }
354   else
355     /* We have storage, but need to reset it.  */
356     {
357       ntype = *typeptr;
358       objfile = TYPE_OBJFILE (ntype);
359       chain = TYPE_CHAIN (ntype);
360       smash_type (ntype);
361       TYPE_CHAIN (ntype) = chain;
362       TYPE_OBJFILE (ntype) = objfile;
363     }
364
365   TYPE_TARGET_TYPE (ntype) = type;
366   TYPE_REFERENCE_TYPE (type) = ntype;
367
368   /* FIXME!  Assume the machine has only one representation for references,
369      and that it matches the (only) representation for pointers!  */
370
371   TYPE_LENGTH (ntype) = gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT;
372   TYPE_CODE (ntype) = TYPE_CODE_REF;
373
374   if (!TYPE_REFERENCE_TYPE (type))      /* Remember it, if don't have one.  */
375     TYPE_REFERENCE_TYPE (type) = ntype;
376
377   /* Update the length of all the other variants of this type.  */
378   chain = TYPE_CHAIN (ntype);
379   while (chain != ntype)
380     {
381       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
382       chain = TYPE_CHAIN (chain);
383     }
384
385   return ntype;
386 }
387
388 /* Same as above, but caller doesn't care about memory allocation details.  */
389
390 struct type *
391 lookup_reference_type (struct type *type)
392 {
393   return make_reference_type (type, (struct type **) 0);
394 }
395
396 /* Lookup a function type that returns type TYPE.  TYPEPTR, if nonzero, points
397    to a pointer to memory where the function type should be stored.
398    If *TYPEPTR is zero, update it to point to the function type we return.
399    We allocate new memory if needed.  */
400
401 struct type *
402 make_function_type (struct type *type, struct type **typeptr)
403 {
404   struct type *ntype;   /* New type */
405   struct objfile *objfile;
406
407   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
408     {
409       ntype = alloc_type (TYPE_OBJFILE (type));
410       if (typeptr)
411         *typeptr = ntype;
412     }
413   else
414     /* We have storage, but need to reset it.  */
415     {
416       ntype = *typeptr;
417       objfile = TYPE_OBJFILE (ntype);
418       smash_type (ntype);
419       TYPE_OBJFILE (ntype) = objfile;
420     }
421
422   TYPE_TARGET_TYPE (ntype) = type;
423
424   TYPE_LENGTH (ntype) = 1;
425   TYPE_CODE (ntype) = TYPE_CODE_FUNC;
426
427   return ntype;
428 }
429
430
431 /* Given a type TYPE, return a type of functions that return that type.
432    May need to construct such a type if this is the first use.  */
433
434 struct type *
435 lookup_function_type (struct type *type)
436 {
437   return make_function_type (type, (struct type **) 0);
438 }
439
440 /* Identify address space identifier by name --
441    return the integer flag defined in gdbtypes.h.  */
442 extern int
443 address_space_name_to_int (char *space_identifier)
444 {
445   struct gdbarch *gdbarch = current_gdbarch;
446   int type_flags;
447   /* Check for known address space delimiters. */
448   if (!strcmp (space_identifier, "code"))
449     return TYPE_FLAG_CODE_SPACE;
450   else if (!strcmp (space_identifier, "data"))
451     return TYPE_FLAG_DATA_SPACE;
452   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
453            && gdbarch_address_class_name_to_type_flags (gdbarch,
454                                                         space_identifier,
455                                                         &type_flags))
456     return type_flags;
457   else
458     error (_("Unknown address space specifier: \"%s\""), space_identifier);
459 }
460
461 /* Identify address space identifier by integer flag as defined in 
462    gdbtypes.h -- return the string version of the adress space name. */
463
464 const char *
465 address_space_int_to_name (int space_flag)
466 {
467   struct gdbarch *gdbarch = current_gdbarch;
468   if (space_flag & TYPE_FLAG_CODE_SPACE)
469     return "code";
470   else if (space_flag & TYPE_FLAG_DATA_SPACE)
471     return "data";
472   else if ((space_flag & TYPE_FLAG_ADDRESS_CLASS_ALL)
473            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
474     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
475   else
476     return NULL;
477 }
478
479 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
480
481    If STORAGE is non-NULL, create the new type instance there.
482    STORAGE must be in the same obstack as TYPE.  */
483
484 static struct type *
485 make_qualified_type (struct type *type, int new_flags,
486                      struct type *storage)
487 {
488   struct type *ntype;
489
490   ntype = type;
491   do {
492     if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
493       return ntype;
494     ntype = TYPE_CHAIN (ntype);
495   } while (ntype != type);
496
497   /* Create a new type instance.  */
498   if (storage == NULL)
499     ntype = alloc_type_instance (type);
500   else
501     {
502       /* If STORAGE was provided, it had better be in the same objfile as
503          TYPE.  Otherwise, we can't link it into TYPE's cv chain: if one
504          objfile is freed and the other kept, we'd have dangling
505          pointers.  */
506       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
507
508       ntype = storage;
509       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
510       TYPE_CHAIN (ntype) = ntype;
511     }
512
513   /* Pointers or references to the original type are not relevant to
514      the new type.  */
515   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
516   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
517
518   /* Chain the new qualified type to the old type.  */
519   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
520   TYPE_CHAIN (type) = ntype;
521
522   /* Now set the instance flags and return the new type.  */
523   TYPE_INSTANCE_FLAGS (ntype) = new_flags;
524
525   /* Set length of new type to that of the original type.  */
526   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
527
528   return ntype;
529 }
530
531 /* Make an address-space-delimited variant of a type -- a type that
532    is identical to the one supplied except that it has an address
533    space attribute attached to it (such as "code" or "data").
534
535    The space attributes "code" and "data" are for Harvard architectures.
536    The address space attributes are for architectures which have
537    alternately sized pointers or pointers with alternate representations.  */
538
539 struct type *
540 make_type_with_address_space (struct type *type, int space_flag)
541 {
542   struct type *ntype;
543   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
544                     & ~(TYPE_FLAG_CODE_SPACE | TYPE_FLAG_DATA_SPACE
545                         | TYPE_FLAG_ADDRESS_CLASS_ALL))
546                    | space_flag);
547
548   return make_qualified_type (type, new_flags, NULL);
549 }
550
551 /* Make a "c-v" variant of a type -- a type that is identical to the
552    one supplied except that it may have const or volatile attributes
553    CNST is a flag for setting the const attribute
554    VOLTL is a flag for setting the volatile attribute
555    TYPE is the base type whose variant we are creating.
556
557    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
558    storage to hold the new qualified type; *TYPEPTR and TYPE must be
559    in the same objfile.  Otherwise, allocate fresh memory for the new
560    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
561    new type we construct.  */
562 struct type *
563 make_cv_type (int cnst, int voltl, struct type *type, struct type **typeptr)
564 {
565   struct type *ntype;   /* New type */
566   struct type *tmp_type = type; /* tmp type */
567   struct objfile *objfile;
568
569   int new_flags = (TYPE_INSTANCE_FLAGS (type)
570                    & ~(TYPE_FLAG_CONST | TYPE_FLAG_VOLATILE));
571
572   if (cnst)
573     new_flags |= TYPE_FLAG_CONST;
574
575   if (voltl)
576     new_flags |= TYPE_FLAG_VOLATILE;
577
578   if (typeptr && *typeptr != NULL)
579     {
580       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
581          a C-V variant chain that threads across objfiles: if one
582          objfile gets freed, then the other has a broken C-V chain.
583
584          This code used to try to copy over the main type from TYPE to
585          *TYPEPTR if they were in different objfiles, but that's
586          wrong, too: TYPE may have a field list or member function
587          lists, which refer to types of their own, etc. etc.  The
588          whole shebang would need to be copied over recursively; you
589          can't have inter-objfile pointers.  The only thing to do is
590          to leave stub types as stub types, and look them up afresh by
591          name each time you encounter them.  */
592       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
593     }
594   
595   ntype = make_qualified_type (type, new_flags, typeptr ? *typeptr : NULL);
596
597   if (typeptr != NULL)
598     *typeptr = ntype;
599
600   return ntype;
601 }
602
603 /* Replace the contents of ntype with the type *type.  This changes the
604    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
605    the changes are propogated to all types in the TYPE_CHAIN.
606
607    In order to build recursive types, it's inevitable that we'll need
608    to update types in place --- but this sort of indiscriminate
609    smashing is ugly, and needs to be replaced with something more
610    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
611    clear if more steps are needed.  */
612 void
613 replace_type (struct type *ntype, struct type *type)
614 {
615   struct type *chain;
616
617   /* These two types had better be in the same objfile.  Otherwise,
618      the assignment of one type's main type structure to the other
619      will produce a type with references to objects (names; field
620      lists; etc.) allocated on an objfile other than its own.  */
621   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
622
623   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
624
625   /* The type length is not a part of the main type.  Update it for each
626      type on the variant chain.  */
627   chain = ntype;
628   do {
629     /* Assert that this element of the chain has no address-class bits
630        set in its flags.  Such type variants might have type lengths
631        which are supposed to be different from the non-address-class
632        variants.  This assertion shouldn't ever be triggered because
633        symbol readers which do construct address-class variants don't
634        call replace_type().  */
635     gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
636
637     TYPE_LENGTH (chain) = TYPE_LENGTH (type);
638     chain = TYPE_CHAIN (chain);
639   } while (ntype != chain);
640
641   /* Assert that the two types have equivalent instance qualifiers.
642      This should be true for at least all of our debug readers.  */
643   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
644 }
645
646 /* Implement direct support for MEMBER_TYPE in GNU C++.
647    May need to construct such a type if this is the first use.
648    The TYPE is the type of the member.  The DOMAIN is the type
649    of the aggregate that the member belongs to.  */
650
651 struct type *
652 lookup_memberptr_type (struct type *type, struct type *domain)
653 {
654   struct type *mtype;
655
656   mtype = alloc_type (TYPE_OBJFILE (type));
657   smash_to_memberptr_type (mtype, domain, type);
658   return (mtype);
659 }
660
661 /* Return a pointer-to-method type, for a method of type TO_TYPE.  */
662
663 struct type *
664 lookup_methodptr_type (struct type *to_type)
665 {
666   struct type *mtype;
667
668   mtype = alloc_type (TYPE_OBJFILE (to_type));
669   TYPE_TARGET_TYPE (mtype) = to_type;
670   TYPE_DOMAIN_TYPE (mtype) = TYPE_DOMAIN_TYPE (to_type);
671   TYPE_LENGTH (mtype) = cplus_method_ptr_size ();
672   TYPE_CODE (mtype) = TYPE_CODE_METHODPTR;
673   return mtype;
674 }
675
676 /* Allocate a stub method whose return type is TYPE.  
677    This apparently happens for speed of symbol reading, since parsing
678    out the arguments to the method is cpu-intensive, the way we are doing
679    it.  So, we will fill in arguments later.
680    This always returns a fresh type.   */
681
682 struct type *
683 allocate_stub_method (struct type *type)
684 {
685   struct type *mtype;
686
687   mtype = init_type (TYPE_CODE_METHOD, 1, TYPE_FLAG_STUB, NULL,
688                      TYPE_OBJFILE (type));
689   TYPE_TARGET_TYPE (mtype) = type;
690   /*  _DOMAIN_TYPE (mtype) = unknown yet */
691   return (mtype);
692 }
693
694 /* Create a range type using either a blank type supplied in RESULT_TYPE,
695    or creating a new type, inheriting the objfile from INDEX_TYPE.
696
697    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND to
698    HIGH_BOUND, inclusive.
699
700    FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
701    sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
702
703 struct type *
704 create_range_type (struct type *result_type, struct type *index_type,
705                    int low_bound, int high_bound)
706 {
707   if (result_type == NULL)
708     {
709       result_type = alloc_type (TYPE_OBJFILE (index_type));
710     }
711   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
712   TYPE_TARGET_TYPE (result_type) = index_type;
713   if (TYPE_STUB (index_type))
714     TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
715   else
716     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
717   TYPE_NFIELDS (result_type) = 2;
718   TYPE_FIELDS (result_type) = (struct field *)
719     TYPE_ALLOC (result_type, 2 * sizeof (struct field));
720   memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
721   TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
722   TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
723   TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int;  /* FIXME */
724   TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int;  /* FIXME */
725
726   if (low_bound >= 0)
727     TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
728
729   return (result_type);
730 }
731
732 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type TYPE.
733    Return 1 if type is a range type, 0 if it is discrete (and bounds
734    will fit in LONGEST), or -1 otherwise. */
735
736 int
737 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
738 {
739   CHECK_TYPEDEF (type);
740   switch (TYPE_CODE (type))
741     {
742     case TYPE_CODE_RANGE:
743       *lowp = TYPE_LOW_BOUND (type);
744       *highp = TYPE_HIGH_BOUND (type);
745       return 1;
746     case TYPE_CODE_ENUM:
747       if (TYPE_NFIELDS (type) > 0)
748         {
749           /* The enums may not be sorted by value, so search all
750              entries */
751           int i;
752
753           *lowp = *highp = TYPE_FIELD_BITPOS (type, 0);
754           for (i = 0; i < TYPE_NFIELDS (type); i++)
755             {
756               if (TYPE_FIELD_BITPOS (type, i) < *lowp)
757                 *lowp = TYPE_FIELD_BITPOS (type, i);
758               if (TYPE_FIELD_BITPOS (type, i) > *highp)
759                 *highp = TYPE_FIELD_BITPOS (type, i);
760             }
761
762           /* Set unsigned indicator if warranted. */
763           if (*lowp >= 0)
764             {
765               TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
766             }
767         }
768       else
769         {
770           *lowp = 0;
771           *highp = -1;
772         }
773       return 0;
774     case TYPE_CODE_BOOL:
775       *lowp = 0;
776       *highp = 1;
777       return 0;
778     case TYPE_CODE_INT:
779       if (TYPE_LENGTH (type) > sizeof (LONGEST))        /* Too big */
780         return -1;
781       if (!TYPE_UNSIGNED (type))
782         {
783           *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
784           *highp = -*lowp - 1;
785           return 0;
786         }
787       /* ... fall through for unsigned ints ... */
788     case TYPE_CODE_CHAR:
789       *lowp = 0;
790       /* This round-about calculation is to avoid shifting by
791          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
792          if TYPE_LENGTH (type) == sizeof (LONGEST). */
793       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
794       *highp = (*highp - 1) | *highp;
795       return 0;
796     default:
797       return -1;
798     }
799 }
800
801 /* Create an array type using either a blank type supplied in RESULT_TYPE,
802    or creating a new type, inheriting the objfile from RANGE_TYPE.
803
804    Elements will be of type ELEMENT_TYPE, the indices will be of type
805    RANGE_TYPE.
806
807    FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
808    sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
809
810 struct type *
811 create_array_type (struct type *result_type, struct type *element_type,
812                    struct type *range_type)
813 {
814   LONGEST low_bound, high_bound;
815
816   if (result_type == NULL)
817     {
818       result_type = alloc_type (TYPE_OBJFILE (range_type));
819     }
820   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
821   TYPE_TARGET_TYPE (result_type) = element_type;
822   if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
823     low_bound = high_bound = 0;
824   CHECK_TYPEDEF (element_type);
825   TYPE_LENGTH (result_type) =
826     TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
827   TYPE_NFIELDS (result_type) = 1;
828   TYPE_FIELDS (result_type) =
829     (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
830   memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
831   TYPE_FIELD_TYPE (result_type, 0) = range_type;
832   TYPE_VPTR_FIELDNO (result_type) = -1;
833
834   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */
835   if (TYPE_LENGTH (result_type) == 0)
836     TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
837
838   return (result_type);
839 }
840
841 /* Create a string type using either a blank type supplied in RESULT_TYPE,
842    or creating a new type.  String types are similar enough to array of
843    char types that we can use create_array_type to build the basic type
844    and then bash it into a string type.
845
846    For fixed length strings, the range type contains 0 as the lower
847    bound and the length of the string minus one as the upper bound.
848
849    FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
850    sure it is TYPE_CODE_UNDEF before we bash it into a string type? */
851
852 struct type *
853 create_string_type (struct type *result_type, struct type *range_type)
854 {
855   struct type *string_char_type;
856       
857   string_char_type = language_string_char_type (current_language,
858                                                 current_gdbarch);
859   result_type = create_array_type (result_type,
860                                    string_char_type,
861                                    range_type);
862   TYPE_CODE (result_type) = TYPE_CODE_STRING;
863   return (result_type);
864 }
865
866 struct type *
867 create_set_type (struct type *result_type, struct type *domain_type)
868 {
869   LONGEST low_bound, high_bound, bit_length;
870   if (result_type == NULL)
871     {
872       result_type = alloc_type (TYPE_OBJFILE (domain_type));
873     }
874   TYPE_CODE (result_type) = TYPE_CODE_SET;
875   TYPE_NFIELDS (result_type) = 1;
876   TYPE_FIELDS (result_type) = (struct field *)
877     TYPE_ALLOC (result_type, 1 * sizeof (struct field));
878   memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
879
880   if (!TYPE_STUB (domain_type))
881     {
882       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
883         low_bound = high_bound = 0;
884       bit_length = high_bound - low_bound + 1;
885       TYPE_LENGTH (result_type)
886         = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
887     }
888   TYPE_FIELD_TYPE (result_type, 0) = domain_type;
889
890   if (low_bound >= 0)
891     TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
892
893   return (result_type);
894 }
895
896 void
897 append_flags_type_flag (struct type *type, int bitpos, char *name)
898 {
899   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
900   gdb_assert (bitpos < TYPE_NFIELDS (type));
901   gdb_assert (bitpos >= 0);
902
903   if (name)
904     {
905       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
906       TYPE_FIELD_BITPOS (type, bitpos) = bitpos;
907     }
908   else
909     {
910       /* Don't show this field to the user.  */
911       TYPE_FIELD_BITPOS (type, bitpos) = -1;
912     }
913 }
914
915 struct type *
916 init_flags_type (char *name, int length)
917 {
918   int nfields = length * TARGET_CHAR_BIT;
919   struct type *type;
920
921   type = init_type (TYPE_CODE_FLAGS, length, TYPE_FLAG_UNSIGNED, name, NULL);
922   TYPE_NFIELDS (type) = nfields;
923   TYPE_FIELDS (type) = TYPE_ALLOC (type, nfields * sizeof (struct field));
924   memset (TYPE_FIELDS (type), 0, nfields * sizeof (struct field));
925
926   return type;
927 }
928
929 struct type *
930 init_vector_type (struct type *elt_type, int n)
931 {
932   struct type *array_type;
933  
934   array_type = create_array_type (0, elt_type,
935                                   create_range_type (0, builtin_type_int,
936                                                      0, n-1));
937   TYPE_FLAGS (array_type) |= TYPE_FLAG_VECTOR;
938   return array_type;
939 }
940
941 /* Smash TYPE to be a type of pointers to members of DOMAIN with type
942    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
943    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
944    TYPE doesn't include the offset (that's the value of the MEMBER
945    itself), but does include the structure type into which it points
946    (for some reason).
947
948    When "smashing" the type, we preserve the objfile that the
949    old type pointed to, since we aren't changing where the type is actually
950    allocated.  */
951
952 void
953 smash_to_memberptr_type (struct type *type, struct type *domain,
954                          struct type *to_type)
955 {
956   struct objfile *objfile;
957
958   objfile = TYPE_OBJFILE (type);
959
960   smash_type (type);
961   TYPE_OBJFILE (type) = objfile;
962   TYPE_TARGET_TYPE (type) = to_type;
963   TYPE_DOMAIN_TYPE (type) = domain;
964   /* Assume that a data member pointer is the same size as a normal
965      pointer.  */
966   TYPE_LENGTH (type) = gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT;
967   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
968 }
969
970 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
971    METHOD just means `function that gets an extra "this" argument'.
972
973    When "smashing" the type, we preserve the objfile that the
974    old type pointed to, since we aren't changing where the type is actually
975    allocated.  */
976
977 void
978 smash_to_method_type (struct type *type, struct type *domain,
979                       struct type *to_type, struct field *args,
980                       int nargs, int varargs)
981 {
982   struct objfile *objfile;
983
984   objfile = TYPE_OBJFILE (type);
985
986   smash_type (type);
987   TYPE_OBJFILE (type) = objfile;
988   TYPE_TARGET_TYPE (type) = to_type;
989   TYPE_DOMAIN_TYPE (type) = domain;
990   TYPE_FIELDS (type) = args;
991   TYPE_NFIELDS (type) = nargs;
992   if (varargs)
993     TYPE_FLAGS (type) |= TYPE_FLAG_VARARGS;
994   TYPE_LENGTH (type) = 1;       /* In practice, this is never needed.  */
995   TYPE_CODE (type) = TYPE_CODE_METHOD;
996 }
997
998 /* Return a typename for a struct/union/enum type without "struct ",
999    "union ", or "enum ".  If the type has a NULL name, return NULL.  */
1000
1001 char *
1002 type_name_no_tag (const struct type *type)
1003 {
1004   if (TYPE_TAG_NAME (type) != NULL)
1005     return TYPE_TAG_NAME (type);
1006
1007   /* Is there code which expects this to return the name if there is no
1008      tag name?  My guess is that this is mainly used for C++ in cases where
1009      the two will always be the same.  */
1010   return TYPE_NAME (type);
1011 }
1012
1013 /* Lookup a typedef or primitive type named NAME,
1014    visible in lexical block BLOCK.
1015    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
1016
1017 struct type *
1018 lookup_typename (char *name, struct block *block, int noerr)
1019 {
1020   struct symbol *sym;
1021   struct type *tmp;
1022
1023   sym = lookup_symbol (name, block, VAR_DOMAIN, 0, (struct symtab **) NULL);
1024   if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1025     {
1026       tmp = language_lookup_primitive_type_by_name (current_language,
1027                                                     current_gdbarch,
1028                                                     name);
1029       if (tmp)
1030         {
1031           return (tmp);
1032         }
1033       else if (!tmp && noerr)
1034         {
1035           return (NULL);
1036         }
1037       else
1038         {
1039           error (_("No type named %s."), name);
1040         }
1041     }
1042   return (SYMBOL_TYPE (sym));
1043 }
1044
1045 struct type *
1046 lookup_unsigned_typename (char *name)
1047 {
1048   char *uns = alloca (strlen (name) + 10);
1049
1050   strcpy (uns, "unsigned ");
1051   strcpy (uns + 9, name);
1052   return (lookup_typename (uns, (struct block *) NULL, 0));
1053 }
1054
1055 struct type *
1056 lookup_signed_typename (char *name)
1057 {
1058   struct type *t;
1059   char *uns = alloca (strlen (name) + 8);
1060
1061   strcpy (uns, "signed ");
1062   strcpy (uns + 7, name);
1063   t = lookup_typename (uns, (struct block *) NULL, 1);
1064   /* If we don't find "signed FOO" just try again with plain "FOO". */
1065   if (t != NULL)
1066     return t;
1067   return lookup_typename (name, (struct block *) NULL, 0);
1068 }
1069
1070 /* Lookup a structure type named "struct NAME",
1071    visible in lexical block BLOCK.  */
1072
1073 struct type *
1074 lookup_struct (char *name, struct block *block)
1075 {
1076   struct symbol *sym;
1077
1078   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1079                        (struct symtab **) NULL);
1080
1081   if (sym == NULL)
1082     {
1083       error (_("No struct type named %s."), name);
1084     }
1085   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1086     {
1087       error (_("This context has class, union or enum %s, not a struct."), name);
1088     }
1089   return (SYMBOL_TYPE (sym));
1090 }
1091
1092 /* Lookup a union type named "union NAME",
1093    visible in lexical block BLOCK.  */
1094
1095 struct type *
1096 lookup_union (char *name, struct block *block)
1097 {
1098   struct symbol *sym;
1099   struct type *t;
1100
1101   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1102                        (struct symtab **) NULL);
1103
1104   if (sym == NULL)
1105     error (_("No union type named %s."), name);
1106
1107   t = SYMBOL_TYPE (sym);
1108
1109   if (TYPE_CODE (t) == TYPE_CODE_UNION)
1110     return (t);
1111
1112   /* C++ unions may come out with TYPE_CODE_CLASS, but we look at
1113    * a further "declared_type" field to discover it is really a union.
1114    */
1115   if (HAVE_CPLUS_STRUCT (t))
1116     if (TYPE_DECLARED_TYPE (t) == DECLARED_TYPE_UNION)
1117       return (t);
1118
1119   /* If we get here, it's not a union */
1120   error (_("This context has class, struct or enum %s, not a union."), name);
1121 }
1122
1123
1124 /* Lookup an enum type named "enum NAME",
1125    visible in lexical block BLOCK.  */
1126
1127 struct type *
1128 lookup_enum (char *name, struct block *block)
1129 {
1130   struct symbol *sym;
1131
1132   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1133                        (struct symtab **) NULL);
1134   if (sym == NULL)
1135     {
1136       error (_("No enum type named %s."), name);
1137     }
1138   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1139     {
1140       error (_("This context has class, struct or union %s, not an enum."), name);
1141     }
1142   return (SYMBOL_TYPE (sym));
1143 }
1144
1145 /* Lookup a template type named "template NAME<TYPE>",
1146    visible in lexical block BLOCK.  */
1147
1148 struct type *
1149 lookup_template_type (char *name, struct type *type, struct block *block)
1150 {
1151   struct symbol *sym;
1152   char *nam = (char *) alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1153   strcpy (nam, name);
1154   strcat (nam, "<");
1155   strcat (nam, TYPE_NAME (type));
1156   strcat (nam, " >");           /* FIXME, extra space still introduced in gcc? */
1157
1158   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0, (struct symtab **) NULL);
1159
1160   if (sym == NULL)
1161     {
1162       error (_("No template type named %s."), name);
1163     }
1164   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1165     {
1166       error (_("This context has class, union or enum %s, not a struct."), name);
1167     }
1168   return (SYMBOL_TYPE (sym));
1169 }
1170
1171 /* Given a type TYPE, lookup the type of the component of type named NAME.  
1172
1173    TYPE can be either a struct or union, or a pointer or reference to a struct or
1174    union.  If it is a pointer or reference, its target type is automatically used.
1175    Thus '.' and '->' are interchangable, as specified for the definitions of the
1176    expression element types STRUCTOP_STRUCT and STRUCTOP_PTR.
1177
1178    If NOERR is nonzero, return zero if NAME is not suitably defined.
1179    If NAME is the name of a baseclass type, return that type.  */
1180
1181 struct type *
1182 lookup_struct_elt_type (struct type *type, char *name, int noerr)
1183 {
1184   int i;
1185
1186   for (;;)
1187     {
1188       CHECK_TYPEDEF (type);
1189       if (TYPE_CODE (type) != TYPE_CODE_PTR
1190           && TYPE_CODE (type) != TYPE_CODE_REF)
1191         break;
1192       type = TYPE_TARGET_TYPE (type);
1193     }
1194
1195   if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
1196       TYPE_CODE (type) != TYPE_CODE_UNION)
1197     {
1198       target_terminal_ours ();
1199       gdb_flush (gdb_stdout);
1200       fprintf_unfiltered (gdb_stderr, "Type ");
1201       type_print (type, "", gdb_stderr, -1);
1202       error (_(" is not a structure or union type."));
1203     }
1204
1205 #if 0
1206   /* FIXME:  This change put in by Michael seems incorrect for the case where
1207      the structure tag name is the same as the member name.  I.E. when doing
1208      "ptype bell->bar" for "struct foo { int bar; int foo; } bell;"
1209      Disabled by fnf. */
1210   {
1211     char *typename;
1212
1213     typename = type_name_no_tag (type);
1214     if (typename != NULL && strcmp (typename, name) == 0)
1215       return type;
1216   }
1217 #endif
1218
1219   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1220     {
1221       char *t_field_name = TYPE_FIELD_NAME (type, i);
1222
1223       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1224         {
1225           return TYPE_FIELD_TYPE (type, i);
1226         }
1227     }
1228
1229   /* OK, it's not in this class.  Recursively check the baseclasses.  */
1230   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1231     {
1232       struct type *t;
1233
1234       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
1235       if (t != NULL)
1236         {
1237           return t;
1238         }
1239     }
1240
1241   if (noerr)
1242     {
1243       return NULL;
1244     }
1245
1246   target_terminal_ours ();
1247   gdb_flush (gdb_stdout);
1248   fprintf_unfiltered (gdb_stderr, "Type ");
1249   type_print (type, "", gdb_stderr, -1);
1250   fprintf_unfiltered (gdb_stderr, " has no component named ");
1251   fputs_filtered (name, gdb_stderr);
1252   error (("."));
1253   return (struct type *) -1;    /* For lint */
1254 }
1255
1256 /* If possible, make the vptr_fieldno and vptr_basetype fields of TYPE
1257    valid.  Callers should be aware that in some cases (for example,
1258    the type or one of its baseclasses is a stub type and we are
1259    debugging a .o file), this function will not be able to find the virtual
1260    function table pointer, and vptr_fieldno will remain -1 and vptr_basetype
1261    will remain NULL.  */
1262
1263 void
1264 fill_in_vptr_fieldno (struct type *type)
1265 {
1266   CHECK_TYPEDEF (type);
1267
1268   if (TYPE_VPTR_FIELDNO (type) < 0)
1269     {
1270       int i;
1271
1272       /* We must start at zero in case the first (and only) baseclass is
1273          virtual (and hence we cannot share the table pointer).  */
1274       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1275         {
1276           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1277           fill_in_vptr_fieldno (baseclass);
1278           if (TYPE_VPTR_FIELDNO (baseclass) >= 0)
1279             {
1280               TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (baseclass);
1281               TYPE_VPTR_BASETYPE (type) = TYPE_VPTR_BASETYPE (baseclass);
1282               break;
1283             }
1284         }
1285     }
1286 }
1287
1288 /* Find the method and field indices for the destructor in class type T.
1289    Return 1 if the destructor was found, otherwise, return 0.  */
1290
1291 int
1292 get_destructor_fn_field (struct type *t, int *method_indexp, int *field_indexp)
1293 {
1294   int i;
1295
1296   for (i = 0; i < TYPE_NFN_FIELDS (t); i++)
1297     {
1298       int j;
1299       struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1300
1301       for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (t, i); j++)
1302         {
1303           if (is_destructor_name (TYPE_FN_FIELD_PHYSNAME (f, j)) != 0)
1304             {
1305               *method_indexp = i;
1306               *field_indexp = j;
1307               return 1;
1308             }
1309         }
1310     }
1311   return 0;
1312 }
1313
1314 static void
1315 stub_noname_complaint (void)
1316 {
1317   complaint (&symfile_complaints, _("stub type has NULL name"));
1318 }
1319
1320 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
1321
1322    If this is a stubbed struct (i.e. declared as struct foo *), see if
1323    we can find a full definition in some other file. If so, copy this
1324    definition, so we can use it in future.  There used to be a comment (but
1325    not any code) that if we don't find a full definition, we'd set a flag
1326    so we don't spend time in the future checking the same type.  That would
1327    be a mistake, though--we might load in more symbols which contain a
1328    full definition for the type.
1329
1330    This used to be coded as a macro, but I don't think it is called 
1331    often enough to merit such treatment.  */
1332
1333 /* Find the real type of TYPE.  This function returns the real type, after
1334    removing all layers of typedefs and completing opaque or stub types.
1335    Completion changes the TYPE argument, but stripping of typedefs does
1336    not.  */
1337
1338 struct type *
1339 check_typedef (struct type *type)
1340 {
1341   struct type *orig_type = type;
1342   int is_const, is_volatile;
1343
1344   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1345     {
1346       if (!TYPE_TARGET_TYPE (type))
1347         {
1348           char *name;
1349           struct symbol *sym;
1350
1351           /* It is dangerous to call lookup_symbol if we are currently
1352              reading a symtab.  Infinite recursion is one danger. */
1353           if (currently_reading_symtab)
1354             return type;
1355
1356           name = type_name_no_tag (type);
1357           /* FIXME: shouldn't we separately check the TYPE_NAME and the
1358              TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1359              as appropriate?  (this code was written before TYPE_NAME and
1360              TYPE_TAG_NAME were separate).  */
1361           if (name == NULL)
1362             {
1363               stub_noname_complaint ();
1364               return type;
1365             }
1366           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0,
1367                                (struct symtab **) NULL);
1368           if (sym)
1369             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1370           else
1371             TYPE_TARGET_TYPE (type) = alloc_type (NULL);        /* TYPE_CODE_UNDEF */
1372         }
1373       type = TYPE_TARGET_TYPE (type);
1374     }
1375
1376   is_const = TYPE_CONST (type);
1377   is_volatile = TYPE_VOLATILE (type);
1378
1379   /* If this is a struct/class/union with no fields, then check whether a
1380      full definition exists somewhere else.  This is for systems where a
1381      type definition with no fields is issued for such types, instead of
1382      identifying them as stub types in the first place */
1383
1384   if (TYPE_IS_OPAQUE (type) && opaque_type_resolution && !currently_reading_symtab)
1385     {
1386       char *name = type_name_no_tag (type);
1387       struct type *newtype;
1388       if (name == NULL)
1389         {
1390           stub_noname_complaint ();
1391           return type;
1392         }
1393       newtype = lookup_transparent_type (name);
1394
1395       if (newtype)
1396         {
1397           /* If the resolved type and the stub are in the same objfile,
1398              then replace the stub type with the real deal.  But if
1399              they're in separate objfiles, leave the stub alone; we'll
1400              just look up the transparent type every time we call
1401              check_typedef.  We can't create pointers between types
1402              allocated to different objfiles, since they may have
1403              different lifetimes.  Trying to copy NEWTYPE over to TYPE's
1404              objfile is pointless, too, since you'll have to move over any
1405              other types NEWTYPE refers to, which could be an unbounded
1406              amount of stuff.  */
1407           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1408             make_cv_type (is_const, is_volatile, newtype, &type);
1409           else
1410             type = newtype;
1411         }
1412     }
1413   /* Otherwise, rely on the stub flag being set for opaque/stubbed types */
1414   else if (TYPE_STUB (type) && !currently_reading_symtab)
1415     {
1416       char *name = type_name_no_tag (type);
1417       /* FIXME: shouldn't we separately check the TYPE_NAME and the
1418          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1419          as appropriate?  (this code was written before TYPE_NAME and
1420          TYPE_TAG_NAME were separate).  */
1421       struct symbol *sym;
1422       if (name == NULL)
1423         {
1424           stub_noname_complaint ();
1425           return type;
1426         }
1427       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0, (struct symtab **) NULL);
1428       if (sym)
1429         {
1430           /* Same as above for opaque types, we can replace the stub
1431              with the complete type only if they are int the same
1432              objfile.  */
1433           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1434             make_cv_type (is_const, is_volatile, SYMBOL_TYPE (sym), &type);
1435           else
1436             type = SYMBOL_TYPE (sym);
1437         }
1438     }
1439
1440   if (TYPE_TARGET_STUB (type))
1441     {
1442       struct type *range_type;
1443       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1444
1445       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1446         {
1447         }
1448       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1449                && TYPE_NFIELDS (type) == 1
1450                && (TYPE_CODE (range_type = TYPE_FIELD_TYPE (type, 0))
1451                    == TYPE_CODE_RANGE))
1452         {
1453           /* Now recompute the length of the array type, based on its
1454              number of elements and the target type's length.  */
1455           TYPE_LENGTH (type) =
1456             ((TYPE_FIELD_BITPOS (range_type, 1)
1457               - TYPE_FIELD_BITPOS (range_type, 0)
1458               + 1)
1459              * TYPE_LENGTH (target_type));
1460           TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1461         }
1462       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1463         {
1464           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1465           TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1466         }
1467     }
1468   /* Cache TYPE_LENGTH for future use. */
1469   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1470   return type;
1471 }
1472
1473 /* Parse a type expression in the string [P..P+LENGTH).  If an error occurs,
1474    silently return builtin_type_void. */
1475
1476 static struct type *
1477 safe_parse_type (char *p, int length)
1478 {
1479   struct ui_file *saved_gdb_stderr;
1480   struct type *type;
1481
1482   /* Suppress error messages. */
1483   saved_gdb_stderr = gdb_stderr;
1484   gdb_stderr = ui_file_new ();
1485
1486   /* Call parse_and_eval_type() without fear of longjmp()s. */
1487   if (!gdb_parse_and_eval_type (p, length, &type))
1488     type = builtin_type_void;
1489
1490   /* Stop suppressing error messages. */
1491   ui_file_delete (gdb_stderr);
1492   gdb_stderr = saved_gdb_stderr;
1493
1494   return type;
1495 }
1496
1497 /* Ugly hack to convert method stubs into method types.
1498
1499    He ain't kiddin'.  This demangles the name of the method into a string
1500    including argument types, parses out each argument type, generates
1501    a string casting a zero to that type, evaluates the string, and stuffs
1502    the resulting type into an argtype vector!!!  Then it knows the type
1503    of the whole function (including argument types for overloading),
1504    which info used to be in the stab's but was removed to hack back
1505    the space required for them.  */
1506
1507 static void
1508 check_stub_method (struct type *type, int method_id, int signature_id)
1509 {
1510   struct fn_field *f;
1511   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1512   char *demangled_name = cplus_demangle (mangled_name,
1513                                          DMGL_PARAMS | DMGL_ANSI);
1514   char *argtypetext, *p;
1515   int depth = 0, argcount = 1;
1516   struct field *argtypes;
1517   struct type *mtype;
1518
1519   /* Make sure we got back a function string that we can use.  */
1520   if (demangled_name)
1521     p = strchr (demangled_name, '(');
1522   else
1523     p = NULL;
1524
1525   if (demangled_name == NULL || p == NULL)
1526     error (_("Internal: Cannot demangle mangled name `%s'."), mangled_name);
1527
1528   /* Now, read in the parameters that define this type.  */
1529   p += 1;
1530   argtypetext = p;
1531   while (*p)
1532     {
1533       if (*p == '(' || *p == '<')
1534         {
1535           depth += 1;
1536         }
1537       else if (*p == ')' || *p == '>')
1538         {
1539           depth -= 1;
1540         }
1541       else if (*p == ',' && depth == 0)
1542         {
1543           argcount += 1;
1544         }
1545
1546       p += 1;
1547     }
1548
1549   /* If we read one argument and it was ``void'', don't count it.  */
1550   if (strncmp (argtypetext, "(void)", 6) == 0)
1551     argcount -= 1;
1552
1553   /* We need one extra slot, for the THIS pointer.  */
1554
1555   argtypes = (struct field *)
1556     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
1557   p = argtypetext;
1558
1559   /* Add THIS pointer for non-static methods.  */
1560   f = TYPE_FN_FIELDLIST1 (type, method_id);
1561   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1562     argcount = 0;
1563   else
1564     {
1565       argtypes[0].type = lookup_pointer_type (type);
1566       argcount = 1;
1567     }
1568
1569   if (*p != ')')                /* () means no args, skip while */
1570     {
1571       depth = 0;
1572       while (*p)
1573         {
1574           if (depth <= 0 && (*p == ',' || *p == ')'))
1575             {
1576               /* Avoid parsing of ellipsis, they will be handled below.
1577                  Also avoid ``void'' as above.  */
1578               if (strncmp (argtypetext, "...", p - argtypetext) != 0
1579                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
1580                 {
1581                   argtypes[argcount].type =
1582                     safe_parse_type (argtypetext, p - argtypetext);
1583                   argcount += 1;
1584                 }
1585               argtypetext = p + 1;
1586             }
1587
1588           if (*p == '(' || *p == '<')
1589             {
1590               depth += 1;
1591             }
1592           else if (*p == ')' || *p == '>')
1593             {
1594               depth -= 1;
1595             }
1596
1597           p += 1;
1598         }
1599     }
1600
1601   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1602
1603   /* Now update the old "stub" type into a real type.  */
1604   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1605   TYPE_DOMAIN_TYPE (mtype) = type;
1606   TYPE_FIELDS (mtype) = argtypes;
1607   TYPE_NFIELDS (mtype) = argcount;
1608   TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
1609   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1610   if (p[-2] == '.')
1611     TYPE_FLAGS (mtype) |= TYPE_FLAG_VARARGS;
1612
1613   xfree (demangled_name);
1614 }
1615
1616 /* This is the external interface to check_stub_method, above.  This function
1617    unstubs all of the signatures for TYPE's METHOD_ID method name.  After
1618    calling this function TYPE_FN_FIELD_STUB will be cleared for each signature
1619    and TYPE_FN_FIELDLIST_NAME will be correct.
1620
1621    This function unfortunately can not die until stabs do.  */
1622
1623 void
1624 check_stub_method_group (struct type *type, int method_id)
1625 {
1626   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1627   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
1628   int j, found_stub = 0;
1629
1630   for (j = 0; j < len; j++)
1631     if (TYPE_FN_FIELD_STUB (f, j))
1632       {
1633         found_stub = 1;
1634         check_stub_method (type, method_id, j);
1635       }
1636
1637   /* GNU v3 methods with incorrect names were corrected when we read in
1638      type information, because it was cheaper to do it then.  The only GNU v2
1639      methods with incorrect method names are operators and destructors;
1640      destructors were also corrected when we read in type information.
1641
1642      Therefore the only thing we need to handle here are v2 operator
1643      names.  */
1644   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1645     {
1646       int ret;
1647       char dem_opname[256];
1648
1649       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, method_id),
1650                                    dem_opname, DMGL_ANSI);
1651       if (!ret)
1652         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, method_id),
1653                                      dem_opname, 0);
1654       if (ret)
1655         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
1656     }
1657 }
1658
1659 const struct cplus_struct_type cplus_struct_default;
1660
1661 void
1662 allocate_cplus_struct_type (struct type *type)
1663 {
1664   if (!HAVE_CPLUS_STRUCT (type))
1665     {
1666       TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1667         TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1668       *(TYPE_CPLUS_SPECIFIC (type)) = cplus_struct_default;
1669     }
1670 }
1671
1672 /* Helper function to initialize the standard scalar types.
1673
1674    If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
1675    of the string pointed to by name in the objfile_obstack for that objfile,
1676    and initialize the type name to that copy.  There are places (mipsread.c
1677    in particular, where init_type is called with a NULL value for NAME). */
1678
1679 struct type *
1680 init_type (enum type_code code, int length, int flags, char *name,
1681            struct objfile *objfile)
1682 {
1683   struct type *type;
1684
1685   type = alloc_type (objfile);
1686   TYPE_CODE (type) = code;
1687   TYPE_LENGTH (type) = length;
1688   TYPE_FLAGS (type) |= flags;
1689   if ((name != NULL) && (objfile != NULL))
1690     {
1691       TYPE_NAME (type) =
1692         obsavestring (name, strlen (name), &objfile->objfile_obstack);
1693     }
1694   else
1695     {
1696       TYPE_NAME (type) = name;
1697     }
1698
1699   /* C++ fancies.  */
1700
1701   if (name && strcmp (name, "char") == 0)
1702     TYPE_FLAGS (type) |= TYPE_FLAG_NOSIGN;
1703
1704   if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
1705       || code == TYPE_CODE_NAMESPACE)
1706     {
1707       INIT_CPLUS_SPECIFIC (type);
1708     }
1709   return (type);
1710 }
1711
1712 /* Helper function.  Create an empty composite type.  */
1713
1714 struct type *
1715 init_composite_type (char *name, enum type_code code)
1716 {
1717   struct type *t;
1718   gdb_assert (code == TYPE_CODE_STRUCT
1719               || code == TYPE_CODE_UNION);
1720   t = init_type (code, 0, 0, NULL, NULL);
1721   TYPE_TAG_NAME (t) = name;
1722   return t;
1723 }
1724
1725 /* Helper function.  Append a field to a composite type.  */
1726
1727 void
1728 append_composite_type_field (struct type *t, char *name, struct type *field)
1729 {
1730   struct field *f;
1731   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
1732   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
1733                               sizeof (struct field) * TYPE_NFIELDS (t));
1734   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
1735   memset (f, 0, sizeof f[0]);
1736   FIELD_TYPE (f[0]) = field;
1737   FIELD_NAME (f[0]) = name;
1738   if (TYPE_CODE (t) == TYPE_CODE_UNION)
1739     {
1740       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
1741         TYPE_LENGTH (t) = TYPE_LENGTH (field);
1742     }
1743   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
1744     {
1745       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
1746       if (TYPE_NFIELDS (t) > 1)
1747         {
1748           FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1])
1749                                  + TYPE_LENGTH (field) * TARGET_CHAR_BIT);
1750         }
1751     }
1752 }
1753
1754 /* Look up a fundamental type for the specified objfile.
1755    May need to construct such a type if this is the first use.
1756
1757    Some object file formats (ELF, COFF, etc) do not define fundamental
1758    types such as "int" or "double".  Others (stabs for example), do
1759    define fundamental types.
1760
1761    For the formats which don't provide fundamental types, gdb can create
1762    such types, using defaults reasonable for the current language and
1763    the current target machine.
1764
1765    NOTE:  This routine is obsolescent.  Each debugging format reader
1766    should manage it's own fundamental types, either creating them from
1767    suitable defaults or reading them from the debugging information,
1768    whichever is appropriate.  The DWARF reader has already been
1769    fixed to do this.  Once the other readers are fixed, this routine
1770    will go away.  Also note that fundamental types should be managed
1771    on a compilation unit basis in a multi-language environment, not
1772    on a linkage unit basis as is done here. */
1773
1774
1775 struct type *
1776 lookup_fundamental_type (struct objfile *objfile, int typeid)
1777 {
1778   struct type **typep;
1779   int nbytes;
1780
1781   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
1782     {
1783       error (_("internal error - invalid fundamental type id %d"), typeid);
1784     }
1785
1786   /* If this is the first time we need a fundamental type for this objfile
1787      then we need to initialize the vector of type pointers. */
1788
1789   if (objfile->fundamental_types == NULL)
1790     {
1791       nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
1792       objfile->fundamental_types = (struct type **)
1793         obstack_alloc (&objfile->objfile_obstack, nbytes);
1794       memset ((char *) objfile->fundamental_types, 0, nbytes);
1795       OBJSTAT (objfile, n_types += FT_NUM_MEMBERS);
1796     }
1797
1798   /* Look for this particular type in the fundamental type vector.  If one is
1799      not found, create and install one appropriate for the current language. */
1800
1801   typep = objfile->fundamental_types + typeid;
1802   if (*typep == NULL)
1803     {
1804       *typep = create_fundamental_type (objfile, typeid);
1805     }
1806
1807   return (*typep);
1808 }
1809
1810 int
1811 can_dereference (struct type *t)
1812 {
1813   /* FIXME: Should we return true for references as well as pointers?  */
1814   CHECK_TYPEDEF (t);
1815   return
1816     (t != NULL
1817      && TYPE_CODE (t) == TYPE_CODE_PTR
1818      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1819 }
1820
1821 int
1822 is_integral_type (struct type *t)
1823 {
1824   CHECK_TYPEDEF (t);
1825   return
1826     ((t != NULL)
1827      && ((TYPE_CODE (t) == TYPE_CODE_INT)
1828          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
1829          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
1830          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
1831          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
1832          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
1833 }
1834
1835 /* Check whether BASE is an ancestor or base class or DCLASS 
1836    Return 1 if so, and 0 if not.
1837    Note: callers may want to check for identity of the types before
1838    calling this function -- identical types are considered to satisfy
1839    the ancestor relationship even if they're identical */
1840
1841 int
1842 is_ancestor (struct type *base, struct type *dclass)
1843 {
1844   int i;
1845
1846   CHECK_TYPEDEF (base);
1847   CHECK_TYPEDEF (dclass);
1848
1849   if (base == dclass)
1850     return 1;
1851   if (TYPE_NAME (base) && TYPE_NAME (dclass) &&
1852       !strcmp (TYPE_NAME (base), TYPE_NAME (dclass)))
1853     return 1;
1854
1855   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1856     if (is_ancestor (base, TYPE_BASECLASS (dclass, i)))
1857       return 1;
1858
1859   return 0;
1860 }
1861
1862
1863
1864 /* See whether DCLASS has a virtual table.  This routine is aimed at
1865    the HP/Taligent ANSI C++ runtime model, and may not work with other
1866    runtime models.  Return 1 => Yes, 0 => No.  */
1867
1868 int
1869 has_vtable (struct type *dclass)
1870 {
1871   /* In the HP ANSI C++ runtime model, a class has a vtable only if it
1872      has virtual functions or virtual bases.  */
1873
1874   int i;
1875
1876   if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1877     return 0;
1878
1879   /* First check for the presence of virtual bases */
1880   if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1881     for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1882       if (B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i))
1883         return 1;
1884
1885   /* Next check for virtual functions */
1886   if (TYPE_FN_FIELDLISTS (dclass))
1887     for (i = 0; i < TYPE_NFN_FIELDS (dclass); i++)
1888       if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, i), 0))
1889         return 1;
1890
1891   /* Recurse on non-virtual bases to see if any of them needs a vtable */
1892   if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1893     for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1894       if ((!B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i)) &&
1895           (has_vtable (TYPE_FIELD_TYPE (dclass, i))))
1896         return 1;
1897
1898   /* Well, maybe we don't need a virtual table */
1899   return 0;
1900 }
1901
1902 /* Return a pointer to the "primary base class" of DCLASS.
1903
1904    A NULL return indicates that DCLASS has no primary base, or that it
1905    couldn't be found (insufficient information).
1906
1907    This routine is aimed at the HP/Taligent ANSI C++ runtime model,
1908    and may not work with other runtime models.  */
1909
1910 struct type *
1911 primary_base_class (struct type *dclass)
1912 {
1913   /* In HP ANSI C++'s runtime model, a "primary base class" of a class
1914      is the first directly inherited, non-virtual base class that
1915      requires a virtual table */
1916
1917   int i;
1918
1919   if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1920     return NULL;
1921
1922   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1923     if (!TYPE_FIELD_VIRTUAL (dclass, i) &&
1924         has_vtable (TYPE_FIELD_TYPE (dclass, i)))
1925       return TYPE_FIELD_TYPE (dclass, i);
1926
1927   return NULL;
1928 }
1929
1930 /* Global manipulated by virtual_base_list[_aux]() */
1931
1932 static struct vbase *current_vbase_list = NULL;
1933
1934 /* Return a pointer to a null-terminated list of struct vbase
1935    items. The vbasetype pointer of each item in the list points to the
1936    type information for a virtual base of the argument DCLASS.
1937
1938    Helper function for virtual_base_list(). 
1939    Note: the list goes backward, right-to-left. virtual_base_list()
1940    copies the items out in reverse order.  */
1941
1942 static void
1943 virtual_base_list_aux (struct type *dclass)
1944 {
1945   struct vbase *tmp_vbase;
1946   int i;
1947
1948   if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1949     return;
1950
1951   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1952     {
1953       /* Recurse on this ancestor, first */
1954       virtual_base_list_aux (TYPE_FIELD_TYPE (dclass, i));
1955
1956       /* If this current base is itself virtual, add it to the list */
1957       if (BASETYPE_VIA_VIRTUAL (dclass, i))
1958         {
1959           struct type *basetype = TYPE_FIELD_TYPE (dclass, i);
1960
1961           /* Check if base already recorded */
1962           tmp_vbase = current_vbase_list;
1963           while (tmp_vbase)
1964             {
1965               if (tmp_vbase->vbasetype == basetype)
1966                 break;          /* found it */
1967               tmp_vbase = tmp_vbase->next;
1968             }
1969
1970           if (!tmp_vbase)       /* normal exit from loop */
1971             {
1972               /* Allocate new item for this virtual base */
1973               tmp_vbase = (struct vbase *) xmalloc (sizeof (struct vbase));
1974
1975               /* Stick it on at the end of the list */
1976               tmp_vbase->vbasetype = basetype;
1977               tmp_vbase->next = current_vbase_list;
1978               current_vbase_list = tmp_vbase;
1979             }
1980         }                       /* if virtual */
1981     }                           /* for loop over bases */
1982 }
1983
1984
1985 /* Compute the list of virtual bases in the right order.  Virtual
1986    bases are laid out in the object's memory area in order of their
1987    occurrence in a depth-first, left-to-right search through the
1988    ancestors.
1989
1990    Argument DCLASS is the type whose virtual bases are required.
1991    Return value is the address of a null-terminated array of pointers
1992    to struct type items.
1993
1994    This routine is aimed at the HP/Taligent ANSI C++ runtime model,
1995    and may not work with other runtime models.
1996
1997    This routine merely hands off the argument to virtual_base_list_aux()
1998    and then copies the result into an array to save space.  */
1999
2000 struct type **
2001 virtual_base_list (struct type *dclass)
2002 {
2003   struct vbase *tmp_vbase;
2004   struct vbase *tmp_vbase_2;
2005   int i;
2006   int count;
2007   struct type **vbase_array;
2008
2009   current_vbase_list = NULL;
2010   virtual_base_list_aux (dclass);
2011
2012   for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
2013     /* no body */ ;
2014
2015   count = i;
2016
2017   vbase_array = (struct type **) xmalloc ((count + 1) * sizeof (struct type *));
2018
2019   for (i = count - 1, tmp_vbase = current_vbase_list; i >= 0; i--, tmp_vbase = tmp_vbase->next)
2020     vbase_array[i] = tmp_vbase->vbasetype;
2021
2022   /* Get rid of constructed chain */
2023   tmp_vbase_2 = tmp_vbase = current_vbase_list;
2024   while (tmp_vbase)
2025     {
2026       tmp_vbase = tmp_vbase->next;
2027       xfree (tmp_vbase_2);
2028       tmp_vbase_2 = tmp_vbase;
2029     }
2030
2031   vbase_array[count] = NULL;
2032   return vbase_array;
2033 }
2034
2035 /* Return the length of the virtual base list of the type DCLASS.  */
2036
2037 int
2038 virtual_base_list_length (struct type *dclass)
2039 {
2040   int i;
2041   struct vbase *tmp_vbase;
2042
2043   current_vbase_list = NULL;
2044   virtual_base_list_aux (dclass);
2045
2046   for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
2047     /* no body */ ;
2048   return i;
2049 }
2050
2051 /* Return the number of elements of the virtual base list of the type
2052    DCLASS, ignoring those appearing in the primary base (and its
2053    primary base, recursively).  */
2054
2055 int
2056 virtual_base_list_length_skip_primaries (struct type *dclass)
2057 {
2058   int i;
2059   struct vbase *tmp_vbase;
2060   struct type *primary;
2061
2062   primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
2063
2064   if (!primary)
2065     return virtual_base_list_length (dclass);
2066
2067   current_vbase_list = NULL;
2068   virtual_base_list_aux (dclass);
2069
2070   for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; tmp_vbase = tmp_vbase->next)
2071     {
2072       if (virtual_base_index (tmp_vbase->vbasetype, primary) >= 0)
2073         continue;
2074       i++;
2075     }
2076   return i;
2077 }
2078
2079
2080 /* Return the index (position) of type BASE, which is a virtual base
2081    class of DCLASS, in the latter's virtual base list.  A return of -1
2082    indicates "not found" or a problem.  */
2083
2084 int
2085 virtual_base_index (struct type *base, struct type *dclass)
2086 {
2087   struct type *vbase;
2088   int i;
2089
2090   if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
2091       (TYPE_CODE (base) != TYPE_CODE_CLASS))
2092     return -1;
2093
2094   i = 0;
2095   vbase = virtual_base_list (dclass)[0];
2096   while (vbase)
2097     {
2098       if (vbase == base)
2099         break;
2100       vbase = virtual_base_list (dclass)[++i];
2101     }
2102
2103   return vbase ? i : -1;
2104 }
2105
2106
2107
2108 /* Return the index (position) of type BASE, which is a virtual base
2109    class of DCLASS, in the latter's virtual base list. Skip over all
2110    bases that may appear in the virtual base list of the primary base
2111    class of DCLASS (recursively).  A return of -1 indicates "not
2112    found" or a problem.  */
2113
2114 int
2115 virtual_base_index_skip_primaries (struct type *base, struct type *dclass)
2116 {
2117   struct type *vbase;
2118   int i, j;
2119   struct type *primary;
2120
2121   if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
2122       (TYPE_CODE (base) != TYPE_CODE_CLASS))
2123     return -1;
2124
2125   primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
2126
2127   j = -1;
2128   i = 0;
2129   vbase = virtual_base_list (dclass)[0];
2130   while (vbase)
2131     {
2132       if (!primary || (virtual_base_index_skip_primaries (vbase, primary) < 0))
2133         j++;
2134       if (vbase == base)
2135         break;
2136       vbase = virtual_base_list (dclass)[++i];
2137     }
2138
2139   return vbase ? j : -1;
2140 }
2141
2142 /* Return position of a derived class DCLASS in the list of
2143  * primary bases starting with the remotest ancestor.
2144  * Position returned is 0-based. */
2145
2146 int
2147 class_index_in_primary_list (struct type *dclass)
2148 {
2149   struct type *pbc;             /* primary base class */
2150
2151   /* Simply recurse on primary base */
2152   pbc = TYPE_PRIMARY_BASE (dclass);
2153   if (pbc)
2154     return 1 + class_index_in_primary_list (pbc);
2155   else
2156     return 0;
2157 }
2158
2159 /* Return a count of the number of virtual functions a type has.
2160  * This includes all the virtual functions it inherits from its
2161  * base classes too.
2162  */
2163
2164 /* pai: FIXME This doesn't do the right thing: count redefined virtual
2165  * functions only once (latest redefinition)
2166  */
2167
2168 int
2169 count_virtual_fns (struct type *dclass)
2170 {
2171   int fn, oi;                   /* function and overloaded instance indices */
2172   int vfuncs;                   /* count to return */
2173
2174   /* recurse on bases that can share virtual table */
2175   struct type *pbc = primary_base_class (dclass);
2176   if (pbc)
2177     vfuncs = count_virtual_fns (pbc);
2178   else
2179     vfuncs = 0;
2180
2181   for (fn = 0; fn < TYPE_NFN_FIELDS (dclass); fn++)
2182     for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (dclass, fn); oi++)
2183       if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, fn), oi))
2184         vfuncs++;
2185
2186   return vfuncs;
2187 }
2188 \f
2189
2190
2191 /* Functions for overload resolution begin here */
2192
2193 /* Compare two badness vectors A and B and return the result.
2194  * 0 => A and B are identical
2195  * 1 => A and B are incomparable
2196  * 2 => A is better than B
2197  * 3 => A is worse than B */
2198
2199 int
2200 compare_badness (struct badness_vector *a, struct badness_vector *b)
2201 {
2202   int i;
2203   int tmp;
2204   short found_pos = 0;          /* any positives in c? */
2205   short found_neg = 0;          /* any negatives in c? */
2206
2207   /* differing lengths => incomparable */
2208   if (a->length != b->length)
2209     return 1;
2210
2211   /* Subtract b from a */
2212   for (i = 0; i < a->length; i++)
2213     {
2214       tmp = a->rank[i] - b->rank[i];
2215       if (tmp > 0)
2216         found_pos = 1;
2217       else if (tmp < 0)
2218         found_neg = 1;
2219     }
2220
2221   if (found_pos)
2222     {
2223       if (found_neg)
2224         return 1;               /* incomparable */
2225       else
2226         return 3;               /* A > B */
2227     }
2228   else
2229     /* no positives */
2230     {
2231       if (found_neg)
2232         return 2;               /* A < B */
2233       else
2234         return 0;               /* A == B */
2235     }
2236 }
2237
2238 /* Rank a function by comparing its parameter types (PARMS, length NPARMS),
2239  * to the types of an argument list (ARGS, length NARGS).
2240  * Return a pointer to a badness vector. This has NARGS + 1 entries. */
2241
2242 struct badness_vector *
2243 rank_function (struct type **parms, int nparms, struct type **args, int nargs)
2244 {
2245   int i;
2246   struct badness_vector *bv;
2247   int min_len = nparms < nargs ? nparms : nargs;
2248
2249   bv = xmalloc (sizeof (struct badness_vector));
2250   bv->length = nargs + 1;       /* add 1 for the length-match rank */
2251   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2252
2253   /* First compare the lengths of the supplied lists.
2254    * If there is a mismatch, set it to a high value. */
2255
2256   /* pai/1997-06-03 FIXME: when we have debug info about default
2257    * arguments and ellipsis parameter lists, we should consider those
2258    * and rank the length-match more finely. */
2259
2260   LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
2261
2262   /* Now rank all the parameters of the candidate function */
2263   for (i = 1; i <= min_len; i++)
2264     bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
2265
2266   /* If more arguments than parameters, add dummy entries */
2267   for (i = min_len + 1; i <= nargs; i++)
2268     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2269
2270   return bv;
2271 }
2272
2273 /* Compare the names of two integer types, assuming that any sign
2274    qualifiers have been checked already.  We do it this way because
2275    there may be an "int" in the name of one of the types.  */
2276
2277 static int
2278 integer_types_same_name_p (const char *first, const char *second)
2279 {
2280   int first_p, second_p;
2281
2282   /* If both are shorts, return 1; if neither is a short, keep checking.  */
2283   first_p = (strstr (first, "short") != NULL);
2284   second_p = (strstr (second, "short") != NULL);
2285   if (first_p && second_p)
2286     return 1;
2287   if (first_p || second_p)
2288     return 0;
2289
2290   /* Likewise for long.  */
2291   first_p = (strstr (first, "long") != NULL);
2292   second_p = (strstr (second, "long") != NULL);
2293   if (first_p && second_p)
2294     return 1;
2295   if (first_p || second_p)
2296     return 0;
2297
2298   /* Likewise for char.  */
2299   first_p = (strstr (first, "char") != NULL);
2300   second_p = (strstr (second, "char") != NULL);
2301   if (first_p && second_p)
2302     return 1;
2303   if (first_p || second_p)
2304     return 0;
2305
2306   /* They must both be ints.  */
2307   return 1;
2308 }
2309
2310 /* Compare one type (PARM) for compatibility with another (ARG).
2311  * PARM is intended to be the parameter type of a function; and
2312  * ARG is the supplied argument's type.  This function tests if
2313  * the latter can be converted to the former.
2314  *
2315  * Return 0 if they are identical types;
2316  * Otherwise, return an integer which corresponds to how compatible
2317  * PARM is to ARG. The higher the return value, the worse the match.
2318  * Generally the "bad" conversions are all uniformly assigned a 100 */
2319
2320 int
2321 rank_one_type (struct type *parm, struct type *arg)
2322 {
2323   /* Identical type pointers */
2324   /* However, this still doesn't catch all cases of same type for arg
2325    * and param. The reason is that builtin types are different from
2326    * the same ones constructed from the object. */
2327   if (parm == arg)
2328     return 0;
2329
2330   /* Resolve typedefs */
2331   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2332     parm = check_typedef (parm);
2333   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2334     arg = check_typedef (arg);
2335
2336   /*
2337      Well, damnit, if the names are exactly the same,
2338      i'll say they are exactly the same. This happens when we generate
2339      method stubs. The types won't point to the same address, but they
2340      really are the same.
2341   */
2342
2343   if (TYPE_NAME (parm) && TYPE_NAME (arg) &&
2344       !strcmp (TYPE_NAME (parm), TYPE_NAME (arg)))
2345       return 0;
2346
2347   /* Check if identical after resolving typedefs */
2348   if (parm == arg)
2349     return 0;
2350
2351   /* See through references, since we can almost make non-references
2352      references. */
2353   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2354     return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
2355             + REFERENCE_CONVERSION_BADNESS);
2356   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2357     return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
2358             + REFERENCE_CONVERSION_BADNESS);
2359   if (overload_debug)
2360   /* Debugging only. */
2361     fprintf_filtered (gdb_stderr,"------ Arg is %s [%d], parm is %s [%d]\n",
2362         TYPE_NAME (arg), TYPE_CODE (arg), TYPE_NAME (parm), TYPE_CODE (parm));
2363
2364   /* x -> y means arg of type x being supplied for parameter of type y */
2365
2366   switch (TYPE_CODE (parm))
2367     {
2368     case TYPE_CODE_PTR:
2369       switch (TYPE_CODE (arg))
2370         {
2371         case TYPE_CODE_PTR:
2372           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2373             return VOID_PTR_CONVERSION_BADNESS;
2374           else
2375             return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2376         case TYPE_CODE_ARRAY:
2377           return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2378         case TYPE_CODE_FUNC:
2379           return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2380         case TYPE_CODE_INT:
2381         case TYPE_CODE_ENUM:
2382         case TYPE_CODE_FLAGS:
2383         case TYPE_CODE_CHAR:
2384         case TYPE_CODE_RANGE:
2385         case TYPE_CODE_BOOL:
2386           return POINTER_CONVERSION_BADNESS;
2387         default:
2388           return INCOMPATIBLE_TYPE_BADNESS;
2389         }
2390     case TYPE_CODE_ARRAY:
2391       switch (TYPE_CODE (arg))
2392         {
2393         case TYPE_CODE_PTR:
2394         case TYPE_CODE_ARRAY:
2395           return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2396         default:
2397           return INCOMPATIBLE_TYPE_BADNESS;
2398         }
2399     case TYPE_CODE_FUNC:
2400       switch (TYPE_CODE (arg))
2401         {
2402         case TYPE_CODE_PTR:     /* funcptr -> func */
2403           return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2404         default:
2405           return INCOMPATIBLE_TYPE_BADNESS;
2406         }
2407     case TYPE_CODE_INT:
2408       switch (TYPE_CODE (arg))
2409         {
2410         case TYPE_CODE_INT:
2411           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2412             {
2413               /* Deal with signed, unsigned, and plain chars and
2414                  signed and unsigned ints */
2415               if (TYPE_NOSIGN (parm))
2416                 {
2417                   /* This case only for character types */
2418                   if (TYPE_NOSIGN (arg))        /* plain char -> plain char */
2419                     return 0;
2420                   else
2421                     return INTEGER_CONVERSION_BADNESS;  /* signed/unsigned char -> plain char */
2422                 }
2423               else if (TYPE_UNSIGNED (parm))
2424                 {
2425                   if (TYPE_UNSIGNED (arg))
2426                     {
2427                       /* unsigned int -> unsigned int, or unsigned long -> unsigned long */
2428                       if (integer_types_same_name_p (TYPE_NAME (parm), TYPE_NAME (arg)))
2429                         return 0;
2430                       else if (integer_types_same_name_p (TYPE_NAME (arg), "int")
2431                                && integer_types_same_name_p (TYPE_NAME (parm), "long"))
2432                         return INTEGER_PROMOTION_BADNESS;       /* unsigned int -> unsigned long */
2433                       else
2434                         return INTEGER_CONVERSION_BADNESS;      /* unsigned long -> unsigned int */
2435                     }
2436                   else
2437                     {
2438                       if (integer_types_same_name_p (TYPE_NAME (arg), "long")
2439                           && integer_types_same_name_p (TYPE_NAME (parm), "int"))
2440                         return INTEGER_CONVERSION_BADNESS;      /* signed long -> unsigned int */
2441                       else
2442                         return INTEGER_CONVERSION_BADNESS;      /* signed int/long -> unsigned int/long */
2443                     }
2444                 }
2445               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2446                 {
2447                   if (integer_types_same_name_p (TYPE_NAME (parm), TYPE_NAME (arg)))
2448                     return 0;
2449                   else if (integer_types_same_name_p (TYPE_NAME (arg), "int")
2450                            && integer_types_same_name_p (TYPE_NAME (parm), "long"))
2451                     return INTEGER_PROMOTION_BADNESS;
2452                   else
2453                     return INTEGER_CONVERSION_BADNESS;
2454                 }
2455               else
2456                 return INTEGER_CONVERSION_BADNESS;
2457             }
2458           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2459             return INTEGER_PROMOTION_BADNESS;
2460           else
2461             return INTEGER_CONVERSION_BADNESS;
2462         case TYPE_CODE_ENUM:
2463         case TYPE_CODE_FLAGS:
2464         case TYPE_CODE_CHAR:
2465         case TYPE_CODE_RANGE:
2466         case TYPE_CODE_BOOL:
2467           return INTEGER_PROMOTION_BADNESS;
2468         case TYPE_CODE_FLT:
2469           return INT_FLOAT_CONVERSION_BADNESS;
2470         case TYPE_CODE_PTR:
2471           return NS_POINTER_CONVERSION_BADNESS;
2472         default:
2473           return INCOMPATIBLE_TYPE_BADNESS;
2474         }
2475       break;
2476     case TYPE_CODE_ENUM:
2477       switch (TYPE_CODE (arg))
2478         {
2479         case TYPE_CODE_INT:
2480         case TYPE_CODE_CHAR:
2481         case TYPE_CODE_RANGE:
2482         case TYPE_CODE_BOOL:
2483         case TYPE_CODE_ENUM:
2484           return INTEGER_CONVERSION_BADNESS;
2485         case TYPE_CODE_FLT:
2486           return INT_FLOAT_CONVERSION_BADNESS;
2487         default:
2488           return INCOMPATIBLE_TYPE_BADNESS;
2489         }
2490       break;
2491     case TYPE_CODE_CHAR:
2492       switch (TYPE_CODE (arg))
2493         {
2494         case TYPE_CODE_RANGE:
2495         case TYPE_CODE_BOOL:
2496         case TYPE_CODE_ENUM:
2497           return INTEGER_CONVERSION_BADNESS;
2498         case TYPE_CODE_FLT:
2499           return INT_FLOAT_CONVERSION_BADNESS;
2500         case TYPE_CODE_INT:
2501           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2502             return INTEGER_CONVERSION_BADNESS;
2503           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2504             return INTEGER_PROMOTION_BADNESS;
2505           /* >>> !! else fall through !! <<< */
2506         case TYPE_CODE_CHAR:
2507           /* Deal with signed, unsigned, and plain chars for C++
2508              and with int cases falling through from previous case */
2509           if (TYPE_NOSIGN (parm))
2510             {
2511               if (TYPE_NOSIGN (arg))
2512                 return 0;
2513               else
2514                 return INTEGER_CONVERSION_BADNESS;
2515             }
2516           else if (TYPE_UNSIGNED (parm))
2517             {
2518               if (TYPE_UNSIGNED (arg))
2519                 return 0;
2520               else
2521                 return INTEGER_PROMOTION_BADNESS;
2522             }
2523           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2524             return 0;
2525           else
2526             return INTEGER_CONVERSION_BADNESS;
2527         default:
2528           return INCOMPATIBLE_TYPE_BADNESS;
2529         }
2530       break;
2531     case TYPE_CODE_RANGE:
2532       switch (TYPE_CODE (arg))
2533         {
2534         case TYPE_CODE_INT:
2535         case TYPE_CODE_CHAR:
2536         case TYPE_CODE_RANGE:
2537         case TYPE_CODE_BOOL:
2538         case TYPE_CODE_ENUM:
2539           return INTEGER_CONVERSION_BADNESS;
2540         case TYPE_CODE_FLT:
2541           return INT_FLOAT_CONVERSION_BADNESS;
2542         default:
2543           return INCOMPATIBLE_TYPE_BADNESS;
2544         }
2545       break;
2546     case TYPE_CODE_BOOL:
2547       switch (TYPE_CODE (arg))
2548         {
2549         case TYPE_CODE_INT:
2550         case TYPE_CODE_CHAR:
2551         case TYPE_CODE_RANGE:
2552         case TYPE_CODE_ENUM:
2553         case TYPE_CODE_FLT:
2554         case TYPE_CODE_PTR:
2555           return BOOLEAN_CONVERSION_BADNESS;
2556         case TYPE_CODE_BOOL:
2557           return 0;
2558         default:
2559           return INCOMPATIBLE_TYPE_BADNESS;
2560         }
2561       break;
2562     case TYPE_CODE_FLT:
2563       switch (TYPE_CODE (arg))
2564         {
2565         case TYPE_CODE_FLT:
2566           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2567             return FLOAT_PROMOTION_BADNESS;
2568           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2569             return 0;
2570           else
2571             return FLOAT_CONVERSION_BADNESS;
2572         case TYPE_CODE_INT:
2573         case TYPE_CODE_BOOL:
2574         case TYPE_CODE_ENUM:
2575         case TYPE_CODE_RANGE:
2576         case TYPE_CODE_CHAR:
2577           return INT_FLOAT_CONVERSION_BADNESS;
2578         default:
2579           return INCOMPATIBLE_TYPE_BADNESS;
2580         }
2581       break;
2582     case TYPE_CODE_COMPLEX:
2583       switch (TYPE_CODE (arg))
2584         {                       /* Strictly not needed for C++, but... */
2585         case TYPE_CODE_FLT:
2586           return FLOAT_PROMOTION_BADNESS;
2587         case TYPE_CODE_COMPLEX:
2588           return 0;
2589         default:
2590           return INCOMPATIBLE_TYPE_BADNESS;
2591         }
2592       break;
2593     case TYPE_CODE_STRUCT:
2594       /* currently same as TYPE_CODE_CLASS */
2595       switch (TYPE_CODE (arg))
2596         {
2597         case TYPE_CODE_STRUCT:
2598           /* Check for derivation */
2599           if (is_ancestor (parm, arg))
2600             return BASE_CONVERSION_BADNESS;
2601           /* else fall through */
2602         default:
2603           return INCOMPATIBLE_TYPE_BADNESS;
2604         }
2605       break;
2606     case TYPE_CODE_UNION:
2607       switch (TYPE_CODE (arg))
2608         {
2609         case TYPE_CODE_UNION:
2610         default:
2611           return INCOMPATIBLE_TYPE_BADNESS;
2612         }
2613       break;
2614     case TYPE_CODE_MEMBERPTR:
2615       switch (TYPE_CODE (arg))
2616         {
2617         default:
2618           return INCOMPATIBLE_TYPE_BADNESS;
2619         }
2620       break;
2621     case TYPE_CODE_METHOD:
2622       switch (TYPE_CODE (arg))
2623         {
2624
2625         default:
2626           return INCOMPATIBLE_TYPE_BADNESS;
2627         }
2628       break;
2629     case TYPE_CODE_REF:
2630       switch (TYPE_CODE (arg))
2631         {
2632
2633         default:
2634           return INCOMPATIBLE_TYPE_BADNESS;
2635         }
2636
2637       break;
2638     case TYPE_CODE_SET:
2639       switch (TYPE_CODE (arg))
2640         {
2641           /* Not in C++ */
2642         case TYPE_CODE_SET:
2643           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), TYPE_FIELD_TYPE (arg, 0));
2644         default:
2645           return INCOMPATIBLE_TYPE_BADNESS;
2646         }
2647       break;
2648     case TYPE_CODE_VOID:
2649     default:
2650       return INCOMPATIBLE_TYPE_BADNESS;
2651     }                           /* switch (TYPE_CODE (arg)) */
2652 }
2653
2654
2655 /* End of functions for overload resolution */
2656
2657 static void
2658 print_bit_vector (B_TYPE *bits, int nbits)
2659 {
2660   int bitno;
2661
2662   for (bitno = 0; bitno < nbits; bitno++)
2663     {
2664       if ((bitno % 8) == 0)
2665         {
2666           puts_filtered (" ");
2667         }
2668       if (B_TST (bits, bitno))
2669         printf_filtered (("1"));
2670       else
2671         printf_filtered (("0"));
2672     }
2673 }
2674
2675 /* Note the first arg should be the "this" pointer, we may not want to
2676    include it since we may get into a infinitely recursive situation.  */
2677
2678 static void
2679 print_arg_types (struct field *args, int nargs, int spaces)
2680 {
2681   if (args != NULL)
2682     {
2683       int i;
2684
2685       for (i = 0; i < nargs; i++)
2686         recursive_dump_type (args[i].type, spaces + 2);
2687     }
2688 }
2689
2690 static void
2691 dump_fn_fieldlists (struct type *type, int spaces)
2692 {
2693   int method_idx;
2694   int overload_idx;
2695   struct fn_field *f;
2696
2697   printfi_filtered (spaces, "fn_fieldlists ");
2698   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
2699   printf_filtered ("\n");
2700   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2701     {
2702       f = TYPE_FN_FIELDLIST1 (type, method_idx);
2703       printfi_filtered (spaces + 2, "[%d] name '%s' (",
2704                         method_idx,
2705                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
2706       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2707                               gdb_stdout);
2708       printf_filtered (_(") length %d\n"),
2709                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2710       for (overload_idx = 0;
2711            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2712            overload_idx++)
2713         {
2714           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2715                             overload_idx,
2716                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
2717           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2718                                   gdb_stdout);
2719           printf_filtered (")\n");
2720           printfi_filtered (spaces + 8, "type ");
2721           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), gdb_stdout);
2722           printf_filtered ("\n");
2723
2724           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2725                                spaces + 8 + 2);
2726
2727           printfi_filtered (spaces + 8, "args ");
2728           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), gdb_stdout);
2729           printf_filtered ("\n");
2730
2731           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
2732                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)),
2733                            spaces);
2734           printfi_filtered (spaces + 8, "fcontext ");
2735           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2736                                   gdb_stdout);
2737           printf_filtered ("\n");
2738
2739           printfi_filtered (spaces + 8, "is_const %d\n",
2740                             TYPE_FN_FIELD_CONST (f, overload_idx));
2741           printfi_filtered (spaces + 8, "is_volatile %d\n",
2742                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2743           printfi_filtered (spaces + 8, "is_private %d\n",
2744                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2745           printfi_filtered (spaces + 8, "is_protected %d\n",
2746                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2747           printfi_filtered (spaces + 8, "is_stub %d\n",
2748                             TYPE_FN_FIELD_STUB (f, overload_idx));
2749           printfi_filtered (spaces + 8, "voffset %u\n",
2750                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2751         }
2752     }
2753 }
2754
2755 static void
2756 print_cplus_stuff (struct type *type, int spaces)
2757 {
2758   printfi_filtered (spaces, "n_baseclasses %d\n",
2759                     TYPE_N_BASECLASSES (type));
2760   printfi_filtered (spaces, "nfn_fields %d\n",
2761                     TYPE_NFN_FIELDS (type));
2762   printfi_filtered (spaces, "nfn_fields_total %d\n",
2763                     TYPE_NFN_FIELDS_TOTAL (type));
2764   if (TYPE_N_BASECLASSES (type) > 0)
2765     {
2766       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2767                         TYPE_N_BASECLASSES (type));
2768       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), gdb_stdout);
2769       printf_filtered (")");
2770
2771       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2772                         TYPE_N_BASECLASSES (type));
2773       puts_filtered ("\n");
2774     }
2775   if (TYPE_NFIELDS (type) > 0)
2776     {
2777       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2778         {
2779           printfi_filtered (spaces, "private_field_bits (%d bits at *",
2780                             TYPE_NFIELDS (type));
2781           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), gdb_stdout);
2782           printf_filtered (")");
2783           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2784                             TYPE_NFIELDS (type));
2785           puts_filtered ("\n");
2786         }
2787       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2788         {
2789           printfi_filtered (spaces, "protected_field_bits (%d bits at *",
2790                             TYPE_NFIELDS (type));
2791           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), gdb_stdout);
2792           printf_filtered (")");
2793           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2794                             TYPE_NFIELDS (type));
2795           puts_filtered ("\n");
2796         }
2797     }
2798   if (TYPE_NFN_FIELDS (type) > 0)
2799     {
2800       dump_fn_fieldlists (type, spaces);
2801     }
2802 }
2803
2804 static void
2805 print_bound_type (int bt)
2806 {
2807   switch (bt)
2808     {
2809     case BOUND_CANNOT_BE_DETERMINED:
2810       printf_filtered ("(BOUND_CANNOT_BE_DETERMINED)");
2811       break;
2812     case BOUND_BY_REF_ON_STACK:
2813       printf_filtered ("(BOUND_BY_REF_ON_STACK)");
2814       break;
2815     case BOUND_BY_VALUE_ON_STACK:
2816       printf_filtered ("(BOUND_BY_VALUE_ON_STACK)");
2817       break;
2818     case BOUND_BY_REF_IN_REG:
2819       printf_filtered ("(BOUND_BY_REF_IN_REG)");
2820       break;
2821     case BOUND_BY_VALUE_IN_REG:
2822       printf_filtered ("(BOUND_BY_VALUE_IN_REG)");
2823       break;
2824     case BOUND_SIMPLE:
2825       printf_filtered ("(BOUND_SIMPLE)");
2826       break;
2827     default:
2828       printf_filtered (_("(unknown bound type)"));
2829       break;
2830     }
2831 }
2832
2833 static struct obstack dont_print_type_obstack;
2834
2835 void
2836 recursive_dump_type (struct type *type, int spaces)
2837 {
2838   int idx;
2839
2840   if (spaces == 0)
2841     obstack_begin (&dont_print_type_obstack, 0);
2842
2843   if (TYPE_NFIELDS (type) > 0
2844       || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
2845     {
2846       struct type **first_dont_print
2847       = (struct type **) obstack_base (&dont_print_type_obstack);
2848
2849       int i = (struct type **) obstack_next_free (&dont_print_type_obstack)
2850       - first_dont_print;
2851
2852       while (--i >= 0)
2853         {
2854           if (type == first_dont_print[i])
2855             {
2856               printfi_filtered (spaces, "type node ");
2857               gdb_print_host_address (type, gdb_stdout);
2858               printf_filtered (_(" <same as already seen type>\n"));
2859               return;
2860             }
2861         }
2862
2863       obstack_ptr_grow (&dont_print_type_obstack, type);
2864     }
2865
2866   printfi_filtered (spaces, "type node ");
2867   gdb_print_host_address (type, gdb_stdout);
2868   printf_filtered ("\n");
2869   printfi_filtered (spaces, "name '%s' (",
2870                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
2871   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
2872   printf_filtered (")\n");
2873   printfi_filtered (spaces, "tagname '%s' (",
2874                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
2875   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2876   printf_filtered (")\n");
2877   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2878   switch (TYPE_CODE (type))
2879     {
2880     case TYPE_CODE_UNDEF:
2881       printf_filtered ("(TYPE_CODE_UNDEF)");
2882       break;
2883     case TYPE_CODE_PTR:
2884       printf_filtered ("(TYPE_CODE_PTR)");
2885       break;
2886     case TYPE_CODE_ARRAY:
2887       printf_filtered ("(TYPE_CODE_ARRAY)");
2888       break;
2889     case TYPE_CODE_STRUCT:
2890       printf_filtered ("(TYPE_CODE_STRUCT)");
2891       break;
2892     case TYPE_CODE_UNION:
2893       printf_filtered ("(TYPE_CODE_UNION)");
2894       break;
2895     case TYPE_CODE_ENUM:
2896       printf_filtered ("(TYPE_CODE_ENUM)");
2897       break;
2898     case TYPE_CODE_FLAGS:
2899       printf_filtered ("(TYPE_CODE_FLAGS)");
2900       break;
2901     case TYPE_CODE_FUNC:
2902       printf_filtered ("(TYPE_CODE_FUNC)");
2903       break;
2904     case TYPE_CODE_INT:
2905       printf_filtered ("(TYPE_CODE_INT)");
2906       break;
2907     case TYPE_CODE_FLT:
2908       printf_filtered ("(TYPE_CODE_FLT)");
2909       break;
2910     case TYPE_CODE_VOID:
2911       printf_filtered ("(TYPE_CODE_VOID)");
2912       break;
2913     case TYPE_CODE_SET:
2914       printf_filtered ("(TYPE_CODE_SET)");
2915       break;
2916     case TYPE_CODE_RANGE:
2917       printf_filtered ("(TYPE_CODE_RANGE)");
2918       break;
2919     case TYPE_CODE_STRING:
2920       printf_filtered ("(TYPE_CODE_STRING)");
2921       break;
2922     case TYPE_CODE_BITSTRING:
2923       printf_filtered ("(TYPE_CODE_BITSTRING)");
2924       break;
2925     case TYPE_CODE_ERROR:
2926       printf_filtered ("(TYPE_CODE_ERROR)");
2927       break;
2928     case TYPE_CODE_MEMBERPTR:
2929       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
2930       break;
2931     case TYPE_CODE_METHODPTR:
2932       printf_filtered ("(TYPE_CODE_METHODPTR)");
2933       break;
2934     case TYPE_CODE_METHOD:
2935       printf_filtered ("(TYPE_CODE_METHOD)");
2936       break;
2937     case TYPE_CODE_REF:
2938       printf_filtered ("(TYPE_CODE_REF)");
2939       break;
2940     case TYPE_CODE_CHAR:
2941       printf_filtered ("(TYPE_CODE_CHAR)");
2942       break;
2943     case TYPE_CODE_BOOL:
2944       printf_filtered ("(TYPE_CODE_BOOL)");
2945       break;
2946     case TYPE_CODE_COMPLEX:
2947       printf_filtered ("(TYPE_CODE_COMPLEX)");
2948       break;
2949     case TYPE_CODE_TYPEDEF:
2950       printf_filtered ("(TYPE_CODE_TYPEDEF)");
2951       break;
2952     case TYPE_CODE_TEMPLATE:
2953       printf_filtered ("(TYPE_CODE_TEMPLATE)");
2954       break;
2955     case TYPE_CODE_TEMPLATE_ARG:
2956       printf_filtered ("(TYPE_CODE_TEMPLATE_ARG)");
2957       break;
2958     case TYPE_CODE_NAMESPACE:
2959       printf_filtered ("(TYPE_CODE_NAMESPACE)");
2960       break;
2961     default:
2962       printf_filtered ("(UNKNOWN TYPE CODE)");
2963       break;
2964     }
2965   puts_filtered ("\n");
2966   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
2967   printfi_filtered (spaces, "upper_bound_type 0x%x ",
2968                     TYPE_ARRAY_UPPER_BOUND_TYPE (type));
2969   print_bound_type (TYPE_ARRAY_UPPER_BOUND_TYPE (type));
2970   puts_filtered ("\n");
2971   printfi_filtered (spaces, "lower_bound_type 0x%x ",
2972                     TYPE_ARRAY_LOWER_BOUND_TYPE (type));
2973   print_bound_type (TYPE_ARRAY_LOWER_BOUND_TYPE (type));
2974   puts_filtered ("\n");
2975   printfi_filtered (spaces, "objfile ");
2976   gdb_print_host_address (TYPE_OBJFILE (type), gdb_stdout);
2977   printf_filtered ("\n");
2978   printfi_filtered (spaces, "target_type ");
2979   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
2980   printf_filtered ("\n");
2981   if (TYPE_TARGET_TYPE (type) != NULL)
2982     {
2983       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
2984     }
2985   printfi_filtered (spaces, "pointer_type ");
2986   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
2987   printf_filtered ("\n");
2988   printfi_filtered (spaces, "reference_type ");
2989   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
2990   printf_filtered ("\n");
2991   printfi_filtered (spaces, "type_chain ");
2992   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
2993   printf_filtered ("\n");
2994   printfi_filtered (spaces, "instance_flags 0x%x", TYPE_INSTANCE_FLAGS (type));
2995   if (TYPE_CONST (type))
2996     {
2997       puts_filtered (" TYPE_FLAG_CONST");
2998     }
2999   if (TYPE_VOLATILE (type))
3000     {
3001       puts_filtered (" TYPE_FLAG_VOLATILE");
3002     }
3003   if (TYPE_CODE_SPACE (type))
3004     {
3005       puts_filtered (" TYPE_FLAG_CODE_SPACE");
3006     }
3007   if (TYPE_DATA_SPACE (type))
3008     {
3009       puts_filtered (" TYPE_FLAG_DATA_SPACE");
3010     }
3011   if (TYPE_ADDRESS_CLASS_1 (type))
3012     {
3013       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3014     }
3015   if (TYPE_ADDRESS_CLASS_2 (type))
3016     {
3017       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3018     }
3019   puts_filtered ("\n");
3020   printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
3021   if (TYPE_UNSIGNED (type))
3022     {
3023       puts_filtered (" TYPE_FLAG_UNSIGNED");
3024     }
3025   if (TYPE_NOSIGN (type))
3026     {
3027       puts_filtered (" TYPE_FLAG_NOSIGN");
3028     }
3029   if (TYPE_STUB (type))
3030     {
3031       puts_filtered (" TYPE_FLAG_STUB");
3032     }
3033   if (TYPE_TARGET_STUB (type))
3034     {
3035       puts_filtered (" TYPE_FLAG_TARGET_STUB");
3036     }
3037   if (TYPE_STATIC (type))
3038     {
3039       puts_filtered (" TYPE_FLAG_STATIC");
3040     }
3041   if (TYPE_PROTOTYPED (type))
3042     {
3043       puts_filtered (" TYPE_FLAG_PROTOTYPED");
3044     }
3045   if (TYPE_INCOMPLETE (type))
3046     {
3047       puts_filtered (" TYPE_FLAG_INCOMPLETE");
3048     }
3049   if (TYPE_VARARGS (type))
3050     {
3051       puts_filtered (" TYPE_FLAG_VARARGS");
3052     }
3053   /* This is used for things like AltiVec registers on ppc.  Gcc emits
3054      an attribute for the array type, which tells whether or not we
3055      have a vector, instead of a regular array.  */
3056   if (TYPE_VECTOR (type))
3057     {
3058       puts_filtered (" TYPE_FLAG_VECTOR");
3059     }
3060   puts_filtered ("\n");
3061   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3062   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3063   puts_filtered ("\n");
3064   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3065     {
3066       printfi_filtered (spaces + 2,
3067                         "[%d] bitpos %d bitsize %d type ",
3068                         idx, TYPE_FIELD_BITPOS (type, idx),
3069                         TYPE_FIELD_BITSIZE (type, idx));
3070       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3071       printf_filtered (" name '%s' (",
3072                        TYPE_FIELD_NAME (type, idx) != NULL
3073                        ? TYPE_FIELD_NAME (type, idx)
3074                        : "<NULL>");
3075       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3076       printf_filtered (")\n");
3077       if (TYPE_FIELD_TYPE (type, idx) != NULL)
3078         {
3079           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3080         }
3081     }
3082   printfi_filtered (spaces, "vptr_basetype ");
3083   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3084   puts_filtered ("\n");
3085   if (TYPE_VPTR_BASETYPE (type) != NULL)
3086     {
3087       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3088     }
3089   printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
3090   switch (TYPE_CODE (type))
3091     {
3092     case TYPE_CODE_STRUCT:
3093       printfi_filtered (spaces, "cplus_stuff ");
3094       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
3095       puts_filtered ("\n");
3096       print_cplus_stuff (type, spaces);
3097       break;
3098
3099     case TYPE_CODE_FLT:
3100       printfi_filtered (spaces, "floatformat ");
3101       if (TYPE_FLOATFORMAT (type) == NULL)
3102         puts_filtered ("(null)");
3103       else
3104         {
3105           puts_filtered ("{ ");
3106           if (TYPE_FLOATFORMAT (type)[0] == NULL
3107               || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3108             puts_filtered ("(null)");
3109           else
3110             puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3111
3112           puts_filtered (", ");
3113           if (TYPE_FLOATFORMAT (type)[1] == NULL
3114               || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3115             puts_filtered ("(null)");
3116           else
3117             puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3118
3119           puts_filtered (" }");
3120         }
3121       puts_filtered ("\n");
3122       break;
3123
3124     default:
3125       /* We have to pick one of the union types to be able print and test
3126          the value.  Pick cplus_struct_type, even though we know it isn't
3127          any particular one. */
3128       printfi_filtered (spaces, "type_specific ");
3129       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
3130       if (TYPE_CPLUS_SPECIFIC (type) != NULL)
3131         {
3132           printf_filtered (_(" (unknown data form)"));
3133         }
3134       printf_filtered ("\n");
3135       break;
3136
3137     }
3138   if (spaces == 0)
3139     obstack_free (&dont_print_type_obstack, NULL);
3140 }
3141
3142 /* Trivial helpers for the libiberty hash table, for mapping one
3143    type to another.  */
3144
3145 struct type_pair
3146 {
3147   struct type *old, *new;
3148 };
3149
3150 static hashval_t
3151 type_pair_hash (const void *item)
3152 {
3153   const struct type_pair *pair = item;
3154   return htab_hash_pointer (pair->old);
3155 }
3156
3157 static int
3158 type_pair_eq (const void *item_lhs, const void *item_rhs)
3159 {
3160   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3161   return lhs->old == rhs->old;
3162 }
3163
3164 /* Allocate the hash table used by copy_type_recursive to walk
3165    types without duplicates.  We use OBJFILE's obstack, because
3166    OBJFILE is about to be deleted.  */
3167
3168 htab_t
3169 create_copied_types_hash (struct objfile *objfile)
3170 {
3171   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3172                                NULL, &objfile->objfile_obstack,
3173                                hashtab_obstack_allocate,
3174                                dummy_obstack_deallocate);
3175 }
3176
3177 /* Recursively copy (deep copy) TYPE, if it is associated with OBJFILE.
3178    Return a new type allocated using malloc, a saved type if we have already
3179    visited TYPE (using COPIED_TYPES), or TYPE if it is not associated with
3180    OBJFILE.  */
3181
3182 struct type *
3183 copy_type_recursive (struct objfile *objfile, struct type *type,
3184                      htab_t copied_types)
3185 {
3186   struct type_pair *stored, pair;
3187   void **slot;
3188   struct type *new_type;
3189
3190   if (TYPE_OBJFILE (type) == NULL)
3191     return type;
3192
3193   /* This type shouldn't be pointing to any types in other objfiles; if
3194      it did, the type might disappear unexpectedly.  */
3195   gdb_assert (TYPE_OBJFILE (type) == objfile);
3196
3197   pair.old = type;
3198   slot = htab_find_slot (copied_types, &pair, INSERT);
3199   if (*slot != NULL)
3200     return ((struct type_pair *) *slot)->new;
3201
3202   new_type = alloc_type (NULL);
3203
3204   /* We must add the new type to the hash table immediately, in case
3205      we encounter this type again during a recursive call below.  */
3206   stored = xmalloc (sizeof (struct type_pair));
3207   stored->old = type;
3208   stored->new = new_type;
3209   *slot = stored;
3210
3211   /* Copy the common fields of types.  */
3212   TYPE_CODE (new_type) = TYPE_CODE (type);
3213   TYPE_ARRAY_UPPER_BOUND_TYPE (new_type) = TYPE_ARRAY_UPPER_BOUND_TYPE (type);
3214   TYPE_ARRAY_LOWER_BOUND_TYPE (new_type) = TYPE_ARRAY_LOWER_BOUND_TYPE (type);
3215   if (TYPE_NAME (type))
3216     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3217   if (TYPE_TAG_NAME (type))
3218     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
3219   TYPE_FLAGS (new_type) = TYPE_FLAGS (type);
3220   TYPE_VPTR_FIELDNO (new_type) = TYPE_VPTR_FIELDNO (type);
3221
3222   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3223   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3224
3225   /* Copy the fields.  */
3226   TYPE_NFIELDS (new_type) = TYPE_NFIELDS (type);
3227   if (TYPE_NFIELDS (type))
3228     {
3229       int i, nfields;
3230
3231       nfields = TYPE_NFIELDS (type);
3232       TYPE_FIELDS (new_type) = xmalloc (sizeof (struct field) * nfields);
3233       for (i = 0; i < nfields; i++)
3234         {
3235           TYPE_FIELD_ARTIFICIAL (new_type, i) = TYPE_FIELD_ARTIFICIAL (type, i);
3236           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3237           if (TYPE_FIELD_TYPE (type, i))
3238             TYPE_FIELD_TYPE (new_type, i)
3239               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3240                                      copied_types);
3241           if (TYPE_FIELD_NAME (type, i))
3242             TYPE_FIELD_NAME (new_type, i) = xstrdup (TYPE_FIELD_NAME (type, i));
3243           if (TYPE_FIELD_STATIC_HAS_ADDR (type, i))
3244             SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3245                                 TYPE_FIELD_STATIC_PHYSADDR (type, i));
3246           else if (TYPE_FIELD_STATIC (type, i))
3247             SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3248                                 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type, i)));
3249           else
3250             {
3251               TYPE_FIELD_BITPOS (new_type, i) = TYPE_FIELD_BITPOS (type, i);
3252               TYPE_FIELD_STATIC_KIND (new_type, i) = 0;
3253             }
3254         }
3255     }
3256
3257   /* Copy pointers to other types.  */
3258   if (TYPE_TARGET_TYPE (type))
3259     TYPE_TARGET_TYPE (new_type) = copy_type_recursive (objfile,
3260                                                        TYPE_TARGET_TYPE (type),
3261                                                        copied_types);
3262   if (TYPE_VPTR_BASETYPE (type))
3263     TYPE_VPTR_BASETYPE (new_type) = copy_type_recursive (objfile,
3264                                                          TYPE_VPTR_BASETYPE (type),
3265                                                          copied_types);
3266   /* Maybe copy the type_specific bits.
3267
3268      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3269      base classes and methods.  There's no fundamental reason why we
3270      can't, but at the moment it is not needed.  */
3271
3272   if (TYPE_CODE (type) == TYPE_CODE_FLT)
3273     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
3274   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3275            || TYPE_CODE (type) == TYPE_CODE_UNION
3276            || TYPE_CODE (type) == TYPE_CODE_TEMPLATE
3277            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3278     INIT_CPLUS_SPECIFIC (new_type);
3279
3280   return new_type;
3281 }
3282
3283 static struct type *
3284 build_flt (int bit, char *name, const struct floatformat **floatformats)
3285 {
3286   struct type *t;
3287
3288   if (bit == -1)
3289     {
3290       gdb_assert (floatformats != NULL);
3291       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3292       bit = floatformats[0]->totalsize;
3293     }
3294   gdb_assert (bit >= 0);
3295
3296   t = init_type (TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, 0, name, NULL);
3297   TYPE_FLOATFORMAT (t) = floatformats;
3298   return t;
3299 }
3300
3301 static void
3302 build_gdbtypes (void)
3303 {
3304   builtin_type_void =
3305     init_type (TYPE_CODE_VOID, 1,
3306                0,
3307                "void", (struct objfile *) NULL);
3308   builtin_type_char =
3309     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3310                (TYPE_FLAG_NOSIGN
3311                 | (gdbarch_char_signed (current_gdbarch) ?
3312                    0 : TYPE_FLAG_UNSIGNED)),
3313                "char", (struct objfile *) NULL);
3314   builtin_type_true_char =
3315     init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3316                0,
3317                "true character", (struct objfile *) NULL);
3318   builtin_type_signed_char =
3319     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3320                0,
3321                "signed char", (struct objfile *) NULL);
3322   builtin_type_unsigned_char =
3323     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3324                TYPE_FLAG_UNSIGNED,
3325                "unsigned char", (struct objfile *) NULL);
3326   builtin_type_short =
3327     init_type (TYPE_CODE_INT,
3328                gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3329                0, "short", (struct objfile *) NULL);
3330   builtin_type_unsigned_short =
3331     init_type (TYPE_CODE_INT,
3332                gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3333                TYPE_FLAG_UNSIGNED, "unsigned short", (struct objfile *) NULL);
3334   builtin_type_int =
3335     init_type (TYPE_CODE_INT,
3336                gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3337                0, "int", (struct objfile *) NULL);
3338   builtin_type_unsigned_int =
3339     init_type (TYPE_CODE_INT,
3340                gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3341                TYPE_FLAG_UNSIGNED, "unsigned int", (struct objfile *) NULL);
3342   builtin_type_long =
3343     init_type (TYPE_CODE_INT,
3344                gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3345                0, "long", (struct objfile *) NULL);
3346   builtin_type_unsigned_long =
3347     init_type (TYPE_CODE_INT,
3348                gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3349                TYPE_FLAG_UNSIGNED, "unsigned long", (struct objfile *) NULL);
3350   builtin_type_long_long =
3351     init_type (TYPE_CODE_INT,
3352                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3353                0, "long long", (struct objfile *) NULL);
3354   builtin_type_unsigned_long_long =
3355     init_type (TYPE_CODE_INT,
3356                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3357                TYPE_FLAG_UNSIGNED, 
3358                "unsigned long long", (struct objfile *) NULL);
3359
3360   builtin_type_float
3361     = build_flt (gdbarch_float_bit (current_gdbarch), "float",
3362                                     gdbarch_float_format (current_gdbarch));
3363   builtin_type_double
3364     = build_flt (gdbarch_double_bit (current_gdbarch), "double",
3365                                      gdbarch_double_format (current_gdbarch));
3366   builtin_type_long_double
3367     = build_flt (gdbarch_long_double_bit (current_gdbarch), "long double",
3368                                           gdbarch_long_double_format
3369                                             (current_gdbarch));
3370
3371   builtin_type_complex =
3372     init_type (TYPE_CODE_COMPLEX,
3373                2 * gdbarch_float_bit (current_gdbarch) / TARGET_CHAR_BIT,
3374                0,
3375                "complex", (struct objfile *) NULL);
3376   TYPE_TARGET_TYPE (builtin_type_complex) = builtin_type_float;
3377   builtin_type_double_complex =
3378     init_type (TYPE_CODE_COMPLEX,
3379                2 * gdbarch_double_bit (current_gdbarch) / TARGET_CHAR_BIT,
3380                0,
3381                "double complex", (struct objfile *) NULL);
3382   TYPE_TARGET_TYPE (builtin_type_double_complex) = builtin_type_double;
3383   builtin_type_string =
3384     init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3385                0,
3386                "string", (struct objfile *) NULL);
3387   builtin_type_bool =
3388     init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3389                0,
3390                "bool", (struct objfile *) NULL);
3391
3392   /* Add user knob for controlling resolution of opaque types */
3393   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3394                            &opaque_type_resolution, _("\
3395 Set resolution of opaque struct/class/union types (if set before loading symbols)."), _("\
3396 Show resolution of opaque struct/class/union types (if set before loading symbols)."), NULL,
3397                            NULL,
3398                            show_opaque_type_resolution,
3399                            &setlist, &showlist);
3400   opaque_type_resolution = 1;
3401
3402   /* Pointer/Address types. */
3403
3404   /* NOTE: on some targets, addresses and pointers are not necessarily
3405      the same --- for example, on the D10V, pointers are 16 bits long,
3406      but addresses are 32 bits long.  See doc/gdbint.texinfo,
3407      ``Pointers Are Not Always Addresses''.
3408
3409      The upshot is:
3410      - gdb's `struct type' always describes the target's
3411        representation.
3412      - gdb's `struct value' objects should always hold values in
3413        target form.
3414      - gdb's CORE_ADDR values are addresses in the unified virtual
3415        address space that the assembler and linker work with.  Thus,
3416        since target_read_memory takes a CORE_ADDR as an argument, it
3417        can access any memory on the target, even if the processor has
3418        separate code and data address spaces.
3419
3420      So, for example:
3421      - If v is a value holding a D10V code pointer, its contents are
3422        in target form: a big-endian address left-shifted two bits.
3423      - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3424        sizeof (void *) == 2 on the target.
3425
3426      In this context, builtin_type_CORE_ADDR is a bit odd: it's a
3427      target type for a value the target will never see.  It's only
3428      used to hold the values of (typeless) linker symbols, which are
3429      indeed in the unified virtual address space.  */
3430   builtin_type_void_data_ptr = make_pointer_type (builtin_type_void, NULL);
3431   builtin_type_void_func_ptr
3432     = lookup_pointer_type (lookup_function_type (builtin_type_void));
3433   builtin_type_CORE_ADDR =
3434     init_type (TYPE_CODE_INT, gdbarch_addr_bit (current_gdbarch) / 8,
3435                TYPE_FLAG_UNSIGNED,
3436                "__CORE_ADDR", (struct objfile *) NULL);
3437 }
3438
3439 static struct gdbarch_data *gdbtypes_data;
3440
3441 const struct builtin_type *
3442 builtin_type (struct gdbarch *gdbarch)
3443 {
3444   return gdbarch_data (gdbarch, gdbtypes_data);
3445 }
3446
3447
3448 static struct type *
3449 build_complex (int bit, char *name, struct type *target_type)
3450 {
3451   struct type *t;
3452   if (bit <= 0 || target_type == builtin_type_error)
3453     {
3454       gdb_assert (builtin_type_error != NULL);
3455       return builtin_type_error;
3456     }
3457   t = init_type (TYPE_CODE_COMPLEX, 2 * bit / TARGET_CHAR_BIT,
3458                  0, name, (struct objfile *) NULL);
3459   TYPE_TARGET_TYPE (t) = target_type;
3460   return t;
3461 }
3462
3463 static void *
3464 gdbtypes_post_init (struct gdbarch *gdbarch)
3465 {
3466   struct builtin_type *builtin_type
3467     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
3468
3469   builtin_type->builtin_void =
3470     init_type (TYPE_CODE_VOID, 1,
3471                0,
3472                "void", (struct objfile *) NULL);
3473   builtin_type->builtin_char =
3474     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3475                (TYPE_FLAG_NOSIGN
3476                 | (gdbarch_char_signed (current_gdbarch) ? 
3477                    0 : TYPE_FLAG_UNSIGNED)),
3478                "char", (struct objfile *) NULL);
3479   builtin_type->builtin_true_char =
3480     init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3481                0,
3482                "true character", (struct objfile *) NULL);
3483   builtin_type->builtin_signed_char =
3484     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3485                0,
3486                "signed char", (struct objfile *) NULL);
3487   builtin_type->builtin_unsigned_char =
3488     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3489                TYPE_FLAG_UNSIGNED,
3490                "unsigned char", (struct objfile *) NULL);
3491   builtin_type->builtin_short =
3492     init_type
3493         (TYPE_CODE_INT, gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3494          0, "short", (struct objfile *) NULL);
3495   builtin_type->builtin_unsigned_short =
3496     init_type
3497         (TYPE_CODE_INT, gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3498          TYPE_FLAG_UNSIGNED, "unsigned short", (struct objfile *) NULL);
3499   builtin_type->builtin_int =
3500     init_type
3501         (TYPE_CODE_INT, gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3502          0, "int", (struct objfile *) NULL);
3503   builtin_type->builtin_unsigned_int =
3504     init_type
3505         (TYPE_CODE_INT, gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3506          TYPE_FLAG_UNSIGNED, "unsigned int", (struct objfile *) NULL);
3507   builtin_type->builtin_long =
3508     init_type
3509         (TYPE_CODE_INT, gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3510          0, "long", (struct objfile *) NULL);
3511   builtin_type->builtin_unsigned_long =
3512     init_type
3513         (TYPE_CODE_INT, gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3514          TYPE_FLAG_UNSIGNED, "unsigned long", (struct objfile *) NULL);
3515   builtin_type->builtin_long_long =
3516     init_type (TYPE_CODE_INT,
3517                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3518                0, "long long", (struct objfile *) NULL);
3519   builtin_type->builtin_unsigned_long_long =
3520     init_type (TYPE_CODE_INT,
3521                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3522                TYPE_FLAG_UNSIGNED, "unsigned long long", 
3523                (struct objfile *) NULL);
3524   builtin_type->builtin_float
3525     = build_flt (gdbarch_float_bit (gdbarch), "float",
3526                  gdbarch_float_format (gdbarch));
3527   builtin_type->builtin_double
3528     = build_flt (gdbarch_double_bit (gdbarch), "double",
3529                  gdbarch_double_format (gdbarch));
3530   builtin_type->builtin_long_double
3531     = build_flt (gdbarch_long_double_bit (gdbarch), "long double",
3532                  gdbarch_long_double_format (gdbarch));
3533   builtin_type->builtin_complex
3534     = build_complex (gdbarch_float_bit (gdbarch), "complex",
3535                      builtin_type->builtin_float);
3536   builtin_type->builtin_double_complex
3537     = build_complex (gdbarch_double_bit (gdbarch), "double complex",
3538                      builtin_type->builtin_double);
3539   builtin_type->builtin_string =
3540     init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3541                0,
3542                "string", (struct objfile *) NULL);
3543   builtin_type->builtin_bool =
3544     init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3545                0,
3546                "bool", (struct objfile *) NULL);
3547
3548   /* Pointer/Address types. */
3549
3550   /* NOTE: on some targets, addresses and pointers are not necessarily
3551      the same --- for example, on the D10V, pointers are 16 bits long,
3552      but addresses are 32 bits long.  See doc/gdbint.texinfo,
3553      ``Pointers Are Not Always Addresses''.
3554
3555      The upshot is:
3556      - gdb's `struct type' always describes the target's
3557        representation.
3558      - gdb's `struct value' objects should always hold values in
3559        target form.
3560      - gdb's CORE_ADDR values are addresses in the unified virtual
3561        address space that the assembler and linker work with.  Thus,
3562        since target_read_memory takes a CORE_ADDR as an argument, it
3563        can access any memory on the target, even if the processor has
3564        separate code and data address spaces.
3565
3566      So, for example:
3567      - If v is a value holding a D10V code pointer, its contents are
3568        in target form: a big-endian address left-shifted two bits.
3569      - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3570        sizeof (void *) == 2 on the target.
3571
3572      In this context, builtin_type->CORE_ADDR is a bit odd: it's a
3573      target type for a value the target will never see.  It's only
3574      used to hold the values of (typeless) linker symbols, which are
3575      indeed in the unified virtual address space.  */
3576   builtin_type->builtin_data_ptr
3577     = make_pointer_type (builtin_type->builtin_void, NULL);
3578   builtin_type->builtin_func_ptr
3579     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
3580   builtin_type->builtin_core_addr =
3581     init_type (TYPE_CODE_INT, gdbarch_addr_bit (current_gdbarch) / 8,
3582                TYPE_FLAG_UNSIGNED,
3583                "__CORE_ADDR", (struct objfile *) NULL);
3584
3585   return builtin_type;
3586 }
3587
3588 extern void _initialize_gdbtypes (void);
3589 void
3590 _initialize_gdbtypes (void)
3591 {
3592   struct cmd_list_element *c;
3593
3594   /* FIXME: Why don't the following types need to be arch-swapped?
3595      See the comment at the top of the calls to
3596      DEPRECATED_REGISTER_GDBARCH_SWAP below.  */
3597   builtin_type_int0 =
3598     init_type (TYPE_CODE_INT, 0 / 8,
3599                0,
3600                "int0_t", (struct objfile *) NULL);
3601   builtin_type_int8 =
3602     init_type (TYPE_CODE_INT, 8 / 8,
3603                0,
3604                "int8_t", (struct objfile *) NULL);
3605   builtin_type_uint8 =
3606     init_type (TYPE_CODE_INT, 8 / 8,
3607                TYPE_FLAG_UNSIGNED,
3608                "uint8_t", (struct objfile *) NULL);
3609   builtin_type_int16 =
3610     init_type (TYPE_CODE_INT, 16 / 8,
3611                0,
3612                "int16_t", (struct objfile *) NULL);
3613   builtin_type_uint16 =
3614     init_type (TYPE_CODE_INT, 16 / 8,
3615                TYPE_FLAG_UNSIGNED,
3616                "uint16_t", (struct objfile *) NULL);
3617   builtin_type_int32 =
3618     init_type (TYPE_CODE_INT, 32 / 8,
3619                0,
3620                "int32_t", (struct objfile *) NULL);
3621   builtin_type_uint32 =
3622     init_type (TYPE_CODE_INT, 32 / 8,
3623                TYPE_FLAG_UNSIGNED,
3624                "uint32_t", (struct objfile *) NULL);
3625   builtin_type_int64 =
3626     init_type (TYPE_CODE_INT, 64 / 8,
3627                0,
3628                "int64_t", (struct objfile *) NULL);
3629   builtin_type_uint64 =
3630     init_type (TYPE_CODE_INT, 64 / 8,
3631                TYPE_FLAG_UNSIGNED,
3632                "uint64_t", (struct objfile *) NULL);
3633   builtin_type_int128 =
3634     init_type (TYPE_CODE_INT, 128 / 8,
3635                0,
3636                "int128_t", (struct objfile *) NULL);
3637   builtin_type_uint128 =
3638     init_type (TYPE_CODE_INT, 128 / 8,
3639                TYPE_FLAG_UNSIGNED,
3640                "uint128_t", (struct objfile *) NULL);
3641
3642   build_gdbtypes ();
3643
3644   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
3645
3646   /* FIXME - For the moment, handle types by swapping them in and out.
3647      Should be using the per-architecture data-pointer and a large
3648      struct. 
3649
3650      Note that any type T that we might create a 'T *' type for must
3651      be arch-swapped: we cache a type's 'T *' type in the pointer_type
3652      field, so if we change architectures but don't swap T, then
3653      lookup_pointer_type will start handing out pointer types made for
3654      a different architecture.  */
3655   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void);
3656   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_char);
3657   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_short);
3658   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int);
3659   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long);
3660   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long_long);
3661   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_signed_char);
3662   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_char);
3663   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_short);
3664   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_int);
3665   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_long);
3666   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_long_long);
3667   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_float);
3668   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_double);
3669   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long_double);
3670   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_complex);
3671   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_double_complex);
3672   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_string);
3673   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void_data_ptr);
3674   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void_func_ptr);
3675   DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_CORE_ADDR);
3676   deprecated_register_gdbarch_swap (NULL, 0, build_gdbtypes);
3677
3678   /* Note: These types do not need to be swapped - they are target
3679      neutral.  FIXME: Are you sure?  See the comment above the calls
3680      to DEPRECATED_REGISTER_GDBARCH_SWAP above.  */
3681   builtin_type_ieee_single
3682     = build_flt (-1, "builtin_type_ieee_single", floatformats_ieee_single);
3683   builtin_type_ieee_double
3684     = build_flt (-1, "builtin_type_ieee_double", floatformats_ieee_double);
3685   builtin_type_i387_ext
3686     = build_flt (-1, "builtin_type_i387_ext", floatformats_i387_ext);
3687   builtin_type_m68881_ext
3688     = build_flt (-1, "builtin_type_m68881_ext", floatformats_m68881_ext);
3689   builtin_type_arm_ext
3690     = build_flt (-1, "builtin_type_arm_ext", floatformats_arm_ext);
3691   builtin_type_ia64_spill
3692     = build_flt (-1, "builtin_type_ia64_spill", floatformats_ia64_spill);
3693   builtin_type_ia64_quad
3694     = build_flt (-1, "builtin_type_ia64_quad", floatformats_ia64_quad);
3695
3696   add_setshow_zinteger_cmd ("overload", no_class, &overload_debug, _("\
3697 Set debugging of C++ overloading."), _("\
3698 Show debugging of C++ overloading."), _("\
3699 When enabled, ranking of the functions is displayed."),
3700                             NULL,
3701                             show_overload_debug,
3702                             &setdebuglist, &showdebuglist);
3703 }