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