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