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