2007-08-10 Michael Snyder <msnyder@access-company.com>
[external/binutils.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
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 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
2116 /* Return the index (position) of type BASE, which is a virtual base
2117    class of DCLASS, in the latter's virtual base list.  A return of -1
2118    indicates "not found" or a problem.  */
2119
2120 int
2121 virtual_base_index (struct type *base, struct type *dclass)
2122 {
2123   struct type *vbase;
2124   int i;
2125
2126   if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) 
2127       || (TYPE_CODE (base) != TYPE_CODE_CLASS))
2128     return -1;
2129
2130   i = 0;
2131   vbase = virtual_base_list (dclass)[0];
2132   while (vbase)
2133     {
2134       if (vbase == base)
2135         break;
2136       vbase = virtual_base_list (dclass)[++i];
2137     }
2138
2139   return vbase ? i : -1;
2140 }
2141
2142
2143
2144 /* Return the index (position) of type BASE, which is a virtual base
2145    class of DCLASS, in the latter's virtual base list.  Skip over all
2146    bases that may appear in the virtual base list of the primary base
2147    class of DCLASS (recursively).  A return of -1 indicates "not
2148    found" or a problem.  */
2149
2150 int
2151 virtual_base_index_skip_primaries (struct type *base, 
2152                                    struct type *dclass)
2153 {
2154   struct type *vbase;
2155   int i, j;
2156   struct type *primary;
2157
2158   if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) 
2159       || (TYPE_CODE (base) != TYPE_CODE_CLASS))
2160     return -1;
2161
2162   primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
2163
2164   j = -1;
2165   i = 0;
2166   vbase = virtual_base_list (dclass)[0];
2167   while (vbase)
2168     {
2169       if (!primary 
2170           || (virtual_base_index_skip_primaries (vbase, primary) < 0))
2171         j++;
2172       if (vbase == base)
2173         break;
2174       vbase = virtual_base_list (dclass)[++i];
2175     }
2176
2177   return vbase ? j : -1;
2178 }
2179
2180 /* Return position of a derived class DCLASS in the list of primary
2181    bases starting with the remotest ancestor.  Position returned is
2182    0-based.  */
2183
2184 int
2185 class_index_in_primary_list (struct type *dclass)
2186 {
2187   struct type *pbc;             /* primary base class */
2188
2189   /* Simply recurse on primary base */
2190   pbc = TYPE_PRIMARY_BASE (dclass);
2191   if (pbc)
2192     return 1 + class_index_in_primary_list (pbc);
2193   else
2194     return 0;
2195 }
2196
2197 /* Return a count of the number of virtual functions a type has.  This
2198    includes all the virtual functions it inherits from its base
2199    classes too.  */
2200
2201 /* pai: FIXME This doesn't do the right thing: count redefined virtual
2202    functions only once (latest redefinition).  */
2203
2204 int
2205 count_virtual_fns (struct type *dclass)
2206 {
2207   int fn, oi;                   /* function and overloaded instance indices */
2208   int vfuncs;                   /* count to return */
2209
2210   /* recurse on bases that can share virtual table */
2211   struct type *pbc = primary_base_class (dclass);
2212   if (pbc)
2213     vfuncs = count_virtual_fns (pbc);
2214   else
2215     vfuncs = 0;
2216
2217   for (fn = 0; fn < TYPE_NFN_FIELDS (dclass); fn++)
2218     for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (dclass, fn); oi++)
2219       if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, fn), oi))
2220         vfuncs++;
2221
2222   return vfuncs;
2223 }
2224 \f
2225
2226
2227 /* Functions for overload resolution begin here */
2228
2229 /* Compare two badness vectors A and B and return the result.
2230    0 => A and B are identical
2231    1 => A and B are incomparable
2232    2 => A is better than B
2233    3 => A is worse than B  */
2234
2235 int
2236 compare_badness (struct badness_vector *a, struct badness_vector *b)
2237 {
2238   int i;
2239   int tmp;
2240   short found_pos = 0;          /* any positives in c? */
2241   short found_neg = 0;          /* any negatives in c? */
2242
2243   /* differing lengths => incomparable */
2244   if (a->length != b->length)
2245     return 1;
2246
2247   /* Subtract b from a */
2248   for (i = 0; i < a->length; i++)
2249     {
2250       tmp = a->rank[i] - b->rank[i];
2251       if (tmp > 0)
2252         found_pos = 1;
2253       else if (tmp < 0)
2254         found_neg = 1;
2255     }
2256
2257   if (found_pos)
2258     {
2259       if (found_neg)
2260         return 1;               /* incomparable */
2261       else
2262         return 3;               /* A > B */
2263     }
2264   else
2265     /* no positives */
2266     {
2267       if (found_neg)
2268         return 2;               /* A < B */
2269       else
2270         return 0;               /* A == B */
2271     }
2272 }
2273
2274 /* Rank a function by comparing its parameter types (PARMS, length
2275    NPARMS), to the types of an argument list (ARGS, length NARGS).
2276    Return a pointer to a badness vector.  This has NARGS + 1
2277    entries.  */
2278
2279 struct badness_vector *
2280 rank_function (struct type **parms, int nparms, 
2281                struct type **args, int nargs)
2282 {
2283   int i;
2284   struct badness_vector *bv;
2285   int min_len = nparms < nargs ? nparms : nargs;
2286
2287   bv = xmalloc (sizeof (struct badness_vector));
2288   bv->length = nargs + 1;       /* add 1 for the length-match rank */
2289   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2290
2291   /* First compare the lengths of the supplied lists.
2292      If there is a mismatch, set it to a high value.  */
2293
2294   /* pai/1997-06-03 FIXME: when we have debug info about default
2295      arguments and ellipsis parameter lists, we should consider those
2296      and rank the length-match more finely.  */
2297
2298   LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
2299
2300   /* Now rank all the parameters of the candidate function */
2301   for (i = 1; i <= min_len; i++)
2302     bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
2303
2304   /* If more arguments than parameters, add dummy entries */
2305   for (i = min_len + 1; i <= nargs; i++)
2306     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2307
2308   return bv;
2309 }
2310
2311 /* Compare the names of two integer types, assuming that any sign
2312    qualifiers have been checked already.  We do it this way because
2313    there may be an "int" in the name of one of the types.  */
2314
2315 static int
2316 integer_types_same_name_p (const char *first, const char *second)
2317 {
2318   int first_p, second_p;
2319
2320   /* If both are shorts, return 1; if neither is a short, keep
2321      checking.  */
2322   first_p = (strstr (first, "short") != NULL);
2323   second_p = (strstr (second, "short") != NULL);
2324   if (first_p && second_p)
2325     return 1;
2326   if (first_p || second_p)
2327     return 0;
2328
2329   /* Likewise for long.  */
2330   first_p = (strstr (first, "long") != NULL);
2331   second_p = (strstr (second, "long") != NULL);
2332   if (first_p && second_p)
2333     return 1;
2334   if (first_p || second_p)
2335     return 0;
2336
2337   /* Likewise for char.  */
2338   first_p = (strstr (first, "char") != NULL);
2339   second_p = (strstr (second, "char") != NULL);
2340   if (first_p && second_p)
2341     return 1;
2342   if (first_p || second_p)
2343     return 0;
2344
2345   /* They must both be ints.  */
2346   return 1;
2347 }
2348
2349 /* Compare one type (PARM) for compatibility with another (ARG).
2350  * PARM is intended to be the parameter type of a function; and
2351  * ARG is the supplied argument's type.  This function tests if
2352  * the latter can be converted to the former.
2353  *
2354  * Return 0 if they are identical types;
2355  * Otherwise, return an integer which corresponds to how compatible
2356  * PARM is to ARG.  The higher the return value, the worse the match.
2357  * Generally the "bad" conversions are all uniformly assigned a 100.  */
2358
2359 int
2360 rank_one_type (struct type *parm, struct type *arg)
2361 {
2362   /* Identical type pointers.  */
2363   /* However, this still doesn't catch all cases of same type for arg
2364      and param.  The reason is that builtin types are different from
2365      the same ones constructed from the object.  */
2366   if (parm == arg)
2367     return 0;
2368
2369   /* Resolve typedefs */
2370   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2371     parm = check_typedef (parm);
2372   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2373     arg = check_typedef (arg);
2374
2375   /*
2376      Well, damnit, if the names are exactly the same, I'll say they
2377      are exactly the same.  This happens when we generate method
2378      stubs.  The types won't point to the same address, but they
2379      really are the same.
2380   */
2381
2382   if (TYPE_NAME (parm) && TYPE_NAME (arg) 
2383       && !strcmp (TYPE_NAME (parm), TYPE_NAME (arg)))
2384     return 0;
2385
2386   /* Check if identical after resolving typedefs.  */
2387   if (parm == arg)
2388     return 0;
2389
2390   /* See through references, since we can almost make non-references
2391      references.  */
2392   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2393     return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
2394             + REFERENCE_CONVERSION_BADNESS);
2395   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2396     return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
2397             + REFERENCE_CONVERSION_BADNESS);
2398   if (overload_debug)
2399   /* Debugging only.  */
2400     fprintf_filtered (gdb_stderr, 
2401                       "------ Arg is %s [%d], parm is %s [%d]\n",
2402                       TYPE_NAME (arg), TYPE_CODE (arg), 
2403                       TYPE_NAME (parm), TYPE_CODE (parm));
2404
2405   /* x -> y means arg of type x being supplied for parameter of type y */
2406
2407   switch (TYPE_CODE (parm))
2408     {
2409     case TYPE_CODE_PTR:
2410       switch (TYPE_CODE (arg))
2411         {
2412         case TYPE_CODE_PTR:
2413           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2414             return VOID_PTR_CONVERSION_BADNESS;
2415           else
2416             return rank_one_type (TYPE_TARGET_TYPE (parm), 
2417                                   TYPE_TARGET_TYPE (arg));
2418         case TYPE_CODE_ARRAY:
2419           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2420                                 TYPE_TARGET_TYPE (arg));
2421         case TYPE_CODE_FUNC:
2422           return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2423         case TYPE_CODE_INT:
2424         case TYPE_CODE_ENUM:
2425         case TYPE_CODE_FLAGS:
2426         case TYPE_CODE_CHAR:
2427         case TYPE_CODE_RANGE:
2428         case TYPE_CODE_BOOL:
2429           return POINTER_CONVERSION_BADNESS;
2430         default:
2431           return INCOMPATIBLE_TYPE_BADNESS;
2432         }
2433     case TYPE_CODE_ARRAY:
2434       switch (TYPE_CODE (arg))
2435         {
2436         case TYPE_CODE_PTR:
2437         case TYPE_CODE_ARRAY:
2438           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2439                                 TYPE_TARGET_TYPE (arg));
2440         default:
2441           return INCOMPATIBLE_TYPE_BADNESS;
2442         }
2443     case TYPE_CODE_FUNC:
2444       switch (TYPE_CODE (arg))
2445         {
2446         case TYPE_CODE_PTR:     /* funcptr -> func */
2447           return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2448         default:
2449           return INCOMPATIBLE_TYPE_BADNESS;
2450         }
2451     case TYPE_CODE_INT:
2452       switch (TYPE_CODE (arg))
2453         {
2454         case TYPE_CODE_INT:
2455           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2456             {
2457               /* Deal with signed, unsigned, and plain chars and
2458                  signed and unsigned ints.  */
2459               if (TYPE_NOSIGN (parm))
2460                 {
2461                   /* This case only for character types */
2462                   if (TYPE_NOSIGN (arg))
2463                     return 0;   /* plain char -> plain char */
2464                   else          /* signed/unsigned char -> plain char */
2465                     return INTEGER_CONVERSION_BADNESS;
2466                 }
2467               else if (TYPE_UNSIGNED (parm))
2468                 {
2469                   if (TYPE_UNSIGNED (arg))
2470                     {
2471                       /* unsigned int -> unsigned int, or 
2472                          unsigned long -> unsigned long */
2473                       if (integer_types_same_name_p (TYPE_NAME (parm), 
2474                                                      TYPE_NAME (arg)))
2475                         return 0;
2476                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
2477                                                           "int")
2478                                && integer_types_same_name_p (TYPE_NAME (parm),
2479                                                              "long"))
2480                         return INTEGER_PROMOTION_BADNESS;       /* unsigned int -> unsigned long */
2481                       else
2482                         return INTEGER_CONVERSION_BADNESS;      /* unsigned long -> unsigned int */
2483                     }
2484                   else
2485                     {
2486                       if (integer_types_same_name_p (TYPE_NAME (arg), 
2487                                                      "long")
2488                           && integer_types_same_name_p (TYPE_NAME (parm), 
2489                                                         "int"))
2490                         return INTEGER_CONVERSION_BADNESS;      /* signed long -> unsigned int */
2491                       else
2492                         return INTEGER_CONVERSION_BADNESS;      /* signed int/long -> unsigned int/long */
2493                     }
2494                 }
2495               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2496                 {
2497                   if (integer_types_same_name_p (TYPE_NAME (parm), 
2498                                                  TYPE_NAME (arg)))
2499                     return 0;
2500                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
2501                                                       "int")
2502                            && integer_types_same_name_p (TYPE_NAME (parm), 
2503                                                          "long"))
2504                     return INTEGER_PROMOTION_BADNESS;
2505                   else
2506                     return INTEGER_CONVERSION_BADNESS;
2507                 }
2508               else
2509                 return INTEGER_CONVERSION_BADNESS;
2510             }
2511           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2512             return INTEGER_PROMOTION_BADNESS;
2513           else
2514             return INTEGER_CONVERSION_BADNESS;
2515         case TYPE_CODE_ENUM:
2516         case TYPE_CODE_FLAGS:
2517         case TYPE_CODE_CHAR:
2518         case TYPE_CODE_RANGE:
2519         case TYPE_CODE_BOOL:
2520           return INTEGER_PROMOTION_BADNESS;
2521         case TYPE_CODE_FLT:
2522           return INT_FLOAT_CONVERSION_BADNESS;
2523         case TYPE_CODE_PTR:
2524           return NS_POINTER_CONVERSION_BADNESS;
2525         default:
2526           return INCOMPATIBLE_TYPE_BADNESS;
2527         }
2528       break;
2529     case TYPE_CODE_ENUM:
2530       switch (TYPE_CODE (arg))
2531         {
2532         case TYPE_CODE_INT:
2533         case TYPE_CODE_CHAR:
2534         case TYPE_CODE_RANGE:
2535         case TYPE_CODE_BOOL:
2536         case TYPE_CODE_ENUM:
2537           return INTEGER_CONVERSION_BADNESS;
2538         case TYPE_CODE_FLT:
2539           return INT_FLOAT_CONVERSION_BADNESS;
2540         default:
2541           return INCOMPATIBLE_TYPE_BADNESS;
2542         }
2543       break;
2544     case TYPE_CODE_CHAR:
2545       switch (TYPE_CODE (arg))
2546         {
2547         case TYPE_CODE_RANGE:
2548         case TYPE_CODE_BOOL:
2549         case TYPE_CODE_ENUM:
2550           return INTEGER_CONVERSION_BADNESS;
2551         case TYPE_CODE_FLT:
2552           return INT_FLOAT_CONVERSION_BADNESS;
2553         case TYPE_CODE_INT:
2554           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2555             return INTEGER_CONVERSION_BADNESS;
2556           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2557             return INTEGER_PROMOTION_BADNESS;
2558           /* >>> !! else fall through !! <<< */
2559         case TYPE_CODE_CHAR:
2560           /* Deal with signed, unsigned, and plain chars for C++ and
2561              with int cases falling through from previous case.  */
2562           if (TYPE_NOSIGN (parm))
2563             {
2564               if (TYPE_NOSIGN (arg))
2565                 return 0;
2566               else
2567                 return INTEGER_CONVERSION_BADNESS;
2568             }
2569           else if (TYPE_UNSIGNED (parm))
2570             {
2571               if (TYPE_UNSIGNED (arg))
2572                 return 0;
2573               else
2574                 return INTEGER_PROMOTION_BADNESS;
2575             }
2576           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2577             return 0;
2578           else
2579             return INTEGER_CONVERSION_BADNESS;
2580         default:
2581           return INCOMPATIBLE_TYPE_BADNESS;
2582         }
2583       break;
2584     case TYPE_CODE_RANGE:
2585       switch (TYPE_CODE (arg))
2586         {
2587         case TYPE_CODE_INT:
2588         case TYPE_CODE_CHAR:
2589         case TYPE_CODE_RANGE:
2590         case TYPE_CODE_BOOL:
2591         case TYPE_CODE_ENUM:
2592           return INTEGER_CONVERSION_BADNESS;
2593         case TYPE_CODE_FLT:
2594           return INT_FLOAT_CONVERSION_BADNESS;
2595         default:
2596           return INCOMPATIBLE_TYPE_BADNESS;
2597         }
2598       break;
2599     case TYPE_CODE_BOOL:
2600       switch (TYPE_CODE (arg))
2601         {
2602         case TYPE_CODE_INT:
2603         case TYPE_CODE_CHAR:
2604         case TYPE_CODE_RANGE:
2605         case TYPE_CODE_ENUM:
2606         case TYPE_CODE_FLT:
2607         case TYPE_CODE_PTR:
2608           return BOOLEAN_CONVERSION_BADNESS;
2609         case TYPE_CODE_BOOL:
2610           return 0;
2611         default:
2612           return INCOMPATIBLE_TYPE_BADNESS;
2613         }
2614       break;
2615     case TYPE_CODE_FLT:
2616       switch (TYPE_CODE (arg))
2617         {
2618         case TYPE_CODE_FLT:
2619           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2620             return FLOAT_PROMOTION_BADNESS;
2621           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2622             return 0;
2623           else
2624             return FLOAT_CONVERSION_BADNESS;
2625         case TYPE_CODE_INT:
2626         case TYPE_CODE_BOOL:
2627         case TYPE_CODE_ENUM:
2628         case TYPE_CODE_RANGE:
2629         case TYPE_CODE_CHAR:
2630           return INT_FLOAT_CONVERSION_BADNESS;
2631         default:
2632           return INCOMPATIBLE_TYPE_BADNESS;
2633         }
2634       break;
2635     case TYPE_CODE_COMPLEX:
2636       switch (TYPE_CODE (arg))
2637         {               /* Strictly not needed for C++, but...  */
2638         case TYPE_CODE_FLT:
2639           return FLOAT_PROMOTION_BADNESS;
2640         case TYPE_CODE_COMPLEX:
2641           return 0;
2642         default:
2643           return INCOMPATIBLE_TYPE_BADNESS;
2644         }
2645       break;
2646     case TYPE_CODE_STRUCT:
2647       /* currently same as TYPE_CODE_CLASS */
2648       switch (TYPE_CODE (arg))
2649         {
2650         case TYPE_CODE_STRUCT:
2651           /* Check for derivation */
2652           if (is_ancestor (parm, arg))
2653             return BASE_CONVERSION_BADNESS;
2654           /* else fall through */
2655         default:
2656           return INCOMPATIBLE_TYPE_BADNESS;
2657         }
2658       break;
2659     case TYPE_CODE_UNION:
2660       switch (TYPE_CODE (arg))
2661         {
2662         case TYPE_CODE_UNION:
2663         default:
2664           return INCOMPATIBLE_TYPE_BADNESS;
2665         }
2666       break;
2667     case TYPE_CODE_MEMBERPTR:
2668       switch (TYPE_CODE (arg))
2669         {
2670         default:
2671           return INCOMPATIBLE_TYPE_BADNESS;
2672         }
2673       break;
2674     case TYPE_CODE_METHOD:
2675       switch (TYPE_CODE (arg))
2676         {
2677
2678         default:
2679           return INCOMPATIBLE_TYPE_BADNESS;
2680         }
2681       break;
2682     case TYPE_CODE_REF:
2683       switch (TYPE_CODE (arg))
2684         {
2685
2686         default:
2687           return INCOMPATIBLE_TYPE_BADNESS;
2688         }
2689
2690       break;
2691     case TYPE_CODE_SET:
2692       switch (TYPE_CODE (arg))
2693         {
2694           /* Not in C++ */
2695         case TYPE_CODE_SET:
2696           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
2697                                 TYPE_FIELD_TYPE (arg, 0));
2698         default:
2699           return INCOMPATIBLE_TYPE_BADNESS;
2700         }
2701       break;
2702     case TYPE_CODE_VOID:
2703     default:
2704       return INCOMPATIBLE_TYPE_BADNESS;
2705     }                           /* switch (TYPE_CODE (arg)) */
2706 }
2707
2708
2709 /* End of functions for overload resolution */
2710
2711 static void
2712 print_bit_vector (B_TYPE *bits, int nbits)
2713 {
2714   int bitno;
2715
2716   for (bitno = 0; bitno < nbits; bitno++)
2717     {
2718       if ((bitno % 8) == 0)
2719         {
2720           puts_filtered (" ");
2721         }
2722       if (B_TST (bits, bitno))
2723         printf_filtered (("1"));
2724       else
2725         printf_filtered (("0"));
2726     }
2727 }
2728
2729 /* Note the first arg should be the "this" pointer, we may not want to
2730    include it since we may get into a infinitely recursive
2731    situation.  */
2732
2733 static void
2734 print_arg_types (struct field *args, int nargs, int spaces)
2735 {
2736   if (args != NULL)
2737     {
2738       int i;
2739
2740       for (i = 0; i < nargs; i++)
2741         recursive_dump_type (args[i].type, spaces + 2);
2742     }
2743 }
2744
2745 static void
2746 dump_fn_fieldlists (struct type *type, int spaces)
2747 {
2748   int method_idx;
2749   int overload_idx;
2750   struct fn_field *f;
2751
2752   printfi_filtered (spaces, "fn_fieldlists ");
2753   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
2754   printf_filtered ("\n");
2755   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2756     {
2757       f = TYPE_FN_FIELDLIST1 (type, method_idx);
2758       printfi_filtered (spaces + 2, "[%d] name '%s' (",
2759                         method_idx,
2760                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
2761       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2762                               gdb_stdout);
2763       printf_filtered (_(") length %d\n"),
2764                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2765       for (overload_idx = 0;
2766            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2767            overload_idx++)
2768         {
2769           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2770                             overload_idx,
2771                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
2772           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2773                                   gdb_stdout);
2774           printf_filtered (")\n");
2775           printfi_filtered (spaces + 8, "type ");
2776           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
2777                                   gdb_stdout);
2778           printf_filtered ("\n");
2779
2780           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2781                                spaces + 8 + 2);
2782
2783           printfi_filtered (spaces + 8, "args ");
2784           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
2785                                   gdb_stdout);
2786           printf_filtered ("\n");
2787
2788           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
2789                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, 
2790                                                              overload_idx)),
2791                            spaces);
2792           printfi_filtered (spaces + 8, "fcontext ");
2793           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2794                                   gdb_stdout);
2795           printf_filtered ("\n");
2796
2797           printfi_filtered (spaces + 8, "is_const %d\n",
2798                             TYPE_FN_FIELD_CONST (f, overload_idx));
2799           printfi_filtered (spaces + 8, "is_volatile %d\n",
2800                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2801           printfi_filtered (spaces + 8, "is_private %d\n",
2802                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2803           printfi_filtered (spaces + 8, "is_protected %d\n",
2804                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2805           printfi_filtered (spaces + 8, "is_stub %d\n",
2806                             TYPE_FN_FIELD_STUB (f, overload_idx));
2807           printfi_filtered (spaces + 8, "voffset %u\n",
2808                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2809         }
2810     }
2811 }
2812
2813 static void
2814 print_cplus_stuff (struct type *type, int spaces)
2815 {
2816   printfi_filtered (spaces, "n_baseclasses %d\n",
2817                     TYPE_N_BASECLASSES (type));
2818   printfi_filtered (spaces, "nfn_fields %d\n",
2819                     TYPE_NFN_FIELDS (type));
2820   printfi_filtered (spaces, "nfn_fields_total %d\n",
2821                     TYPE_NFN_FIELDS_TOTAL (type));
2822   if (TYPE_N_BASECLASSES (type) > 0)
2823     {
2824       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2825                         TYPE_N_BASECLASSES (type));
2826       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
2827                               gdb_stdout);
2828       printf_filtered (")");
2829
2830       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2831                         TYPE_N_BASECLASSES (type));
2832       puts_filtered ("\n");
2833     }
2834   if (TYPE_NFIELDS (type) > 0)
2835     {
2836       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2837         {
2838           printfi_filtered (spaces, 
2839                             "private_field_bits (%d bits at *",
2840                             TYPE_NFIELDS (type));
2841           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
2842                                   gdb_stdout);
2843           printf_filtered (")");
2844           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2845                             TYPE_NFIELDS (type));
2846           puts_filtered ("\n");
2847         }
2848       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2849         {
2850           printfi_filtered (spaces, 
2851                             "protected_field_bits (%d bits at *",
2852                             TYPE_NFIELDS (type));
2853           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
2854                                   gdb_stdout);
2855           printf_filtered (")");
2856           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2857                             TYPE_NFIELDS (type));
2858           puts_filtered ("\n");
2859         }
2860     }
2861   if (TYPE_NFN_FIELDS (type) > 0)
2862     {
2863       dump_fn_fieldlists (type, spaces);
2864     }
2865 }
2866
2867 static void
2868 print_bound_type (int bt)
2869 {
2870   switch (bt)
2871     {
2872     case BOUND_CANNOT_BE_DETERMINED:
2873       printf_filtered ("(BOUND_CANNOT_BE_DETERMINED)");
2874       break;
2875     case BOUND_BY_REF_ON_STACK:
2876       printf_filtered ("(BOUND_BY_REF_ON_STACK)");
2877       break;
2878     case BOUND_BY_VALUE_ON_STACK:
2879       printf_filtered ("(BOUND_BY_VALUE_ON_STACK)");
2880       break;
2881     case BOUND_BY_REF_IN_REG:
2882       printf_filtered ("(BOUND_BY_REF_IN_REG)");
2883       break;
2884     case BOUND_BY_VALUE_IN_REG:
2885       printf_filtered ("(BOUND_BY_VALUE_IN_REG)");
2886       break;
2887     case BOUND_SIMPLE:
2888       printf_filtered ("(BOUND_SIMPLE)");
2889       break;
2890     default:
2891       printf_filtered (_("(unknown bound type)"));
2892       break;
2893     }
2894 }
2895
2896 static struct obstack dont_print_type_obstack;
2897
2898 void
2899 recursive_dump_type (struct type *type, int spaces)
2900 {
2901   int idx;
2902
2903   if (spaces == 0)
2904     obstack_begin (&dont_print_type_obstack, 0);
2905
2906   if (TYPE_NFIELDS (type) > 0
2907       || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
2908     {
2909       struct type **first_dont_print
2910         = (struct type **) obstack_base (&dont_print_type_obstack);
2911
2912       int i = (struct type **) 
2913         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
2914
2915       while (--i >= 0)
2916         {
2917           if (type == first_dont_print[i])
2918             {
2919               printfi_filtered (spaces, "type node ");
2920               gdb_print_host_address (type, gdb_stdout);
2921               printf_filtered (_(" <same as already seen type>\n"));
2922               return;
2923             }
2924         }
2925
2926       obstack_ptr_grow (&dont_print_type_obstack, type);
2927     }
2928
2929   printfi_filtered (spaces, "type node ");
2930   gdb_print_host_address (type, gdb_stdout);
2931   printf_filtered ("\n");
2932   printfi_filtered (spaces, "name '%s' (",
2933                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
2934   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
2935   printf_filtered (")\n");
2936   printfi_filtered (spaces, "tagname '%s' (",
2937                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
2938   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2939   printf_filtered (")\n");
2940   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2941   switch (TYPE_CODE (type))
2942     {
2943     case TYPE_CODE_UNDEF:
2944       printf_filtered ("(TYPE_CODE_UNDEF)");
2945       break;
2946     case TYPE_CODE_PTR:
2947       printf_filtered ("(TYPE_CODE_PTR)");
2948       break;
2949     case TYPE_CODE_ARRAY:
2950       printf_filtered ("(TYPE_CODE_ARRAY)");
2951       break;
2952     case TYPE_CODE_STRUCT:
2953       printf_filtered ("(TYPE_CODE_STRUCT)");
2954       break;
2955     case TYPE_CODE_UNION:
2956       printf_filtered ("(TYPE_CODE_UNION)");
2957       break;
2958     case TYPE_CODE_ENUM:
2959       printf_filtered ("(TYPE_CODE_ENUM)");
2960       break;
2961     case TYPE_CODE_FLAGS:
2962       printf_filtered ("(TYPE_CODE_FLAGS)");
2963       break;
2964     case TYPE_CODE_FUNC:
2965       printf_filtered ("(TYPE_CODE_FUNC)");
2966       break;
2967     case TYPE_CODE_INT:
2968       printf_filtered ("(TYPE_CODE_INT)");
2969       break;
2970     case TYPE_CODE_FLT:
2971       printf_filtered ("(TYPE_CODE_FLT)");
2972       break;
2973     case TYPE_CODE_VOID:
2974       printf_filtered ("(TYPE_CODE_VOID)");
2975       break;
2976     case TYPE_CODE_SET:
2977       printf_filtered ("(TYPE_CODE_SET)");
2978       break;
2979     case TYPE_CODE_RANGE:
2980       printf_filtered ("(TYPE_CODE_RANGE)");
2981       break;
2982     case TYPE_CODE_STRING:
2983       printf_filtered ("(TYPE_CODE_STRING)");
2984       break;
2985     case TYPE_CODE_BITSTRING:
2986       printf_filtered ("(TYPE_CODE_BITSTRING)");
2987       break;
2988     case TYPE_CODE_ERROR:
2989       printf_filtered ("(TYPE_CODE_ERROR)");
2990       break;
2991     case TYPE_CODE_MEMBERPTR:
2992       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
2993       break;
2994     case TYPE_CODE_METHODPTR:
2995       printf_filtered ("(TYPE_CODE_METHODPTR)");
2996       break;
2997     case TYPE_CODE_METHOD:
2998       printf_filtered ("(TYPE_CODE_METHOD)");
2999       break;
3000     case TYPE_CODE_REF:
3001       printf_filtered ("(TYPE_CODE_REF)");
3002       break;
3003     case TYPE_CODE_CHAR:
3004       printf_filtered ("(TYPE_CODE_CHAR)");
3005       break;
3006     case TYPE_CODE_BOOL:
3007       printf_filtered ("(TYPE_CODE_BOOL)");
3008       break;
3009     case TYPE_CODE_COMPLEX:
3010       printf_filtered ("(TYPE_CODE_COMPLEX)");
3011       break;
3012     case TYPE_CODE_TYPEDEF:
3013       printf_filtered ("(TYPE_CODE_TYPEDEF)");
3014       break;
3015     case TYPE_CODE_TEMPLATE:
3016       printf_filtered ("(TYPE_CODE_TEMPLATE)");
3017       break;
3018     case TYPE_CODE_TEMPLATE_ARG:
3019       printf_filtered ("(TYPE_CODE_TEMPLATE_ARG)");
3020       break;
3021     case TYPE_CODE_NAMESPACE:
3022       printf_filtered ("(TYPE_CODE_NAMESPACE)");
3023       break;
3024     default:
3025       printf_filtered ("(UNKNOWN TYPE CODE)");
3026       break;
3027     }
3028   puts_filtered ("\n");
3029   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
3030   printfi_filtered (spaces, "upper_bound_type 0x%x ",
3031                     TYPE_ARRAY_UPPER_BOUND_TYPE (type));
3032   print_bound_type (TYPE_ARRAY_UPPER_BOUND_TYPE (type));
3033   puts_filtered ("\n");
3034   printfi_filtered (spaces, "lower_bound_type 0x%x ",
3035                     TYPE_ARRAY_LOWER_BOUND_TYPE (type));
3036   print_bound_type (TYPE_ARRAY_LOWER_BOUND_TYPE (type));
3037   puts_filtered ("\n");
3038   printfi_filtered (spaces, "objfile ");
3039   gdb_print_host_address (TYPE_OBJFILE (type), gdb_stdout);
3040   printf_filtered ("\n");
3041   printfi_filtered (spaces, "target_type ");
3042   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
3043   printf_filtered ("\n");
3044   if (TYPE_TARGET_TYPE (type) != NULL)
3045     {
3046       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3047     }
3048   printfi_filtered (spaces, "pointer_type ");
3049   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
3050   printf_filtered ("\n");
3051   printfi_filtered (spaces, "reference_type ");
3052   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
3053   printf_filtered ("\n");
3054   printfi_filtered (spaces, "type_chain ");
3055   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3056   printf_filtered ("\n");
3057   printfi_filtered (spaces, "instance_flags 0x%x", 
3058                     TYPE_INSTANCE_FLAGS (type));
3059   if (TYPE_CONST (type))
3060     {
3061       puts_filtered (" TYPE_FLAG_CONST");
3062     }
3063   if (TYPE_VOLATILE (type))
3064     {
3065       puts_filtered (" TYPE_FLAG_VOLATILE");
3066     }
3067   if (TYPE_CODE_SPACE (type))
3068     {
3069       puts_filtered (" TYPE_FLAG_CODE_SPACE");
3070     }
3071   if (TYPE_DATA_SPACE (type))
3072     {
3073       puts_filtered (" TYPE_FLAG_DATA_SPACE");
3074     }
3075   if (TYPE_ADDRESS_CLASS_1 (type))
3076     {
3077       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3078     }
3079   if (TYPE_ADDRESS_CLASS_2 (type))
3080     {
3081       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3082     }
3083   puts_filtered ("\n");
3084   printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
3085   if (TYPE_UNSIGNED (type))
3086     {
3087       puts_filtered (" TYPE_FLAG_UNSIGNED");
3088     }
3089   if (TYPE_NOSIGN (type))
3090     {
3091       puts_filtered (" TYPE_FLAG_NOSIGN");
3092     }
3093   if (TYPE_STUB (type))
3094     {
3095       puts_filtered (" TYPE_FLAG_STUB");
3096     }
3097   if (TYPE_TARGET_STUB (type))
3098     {
3099       puts_filtered (" TYPE_FLAG_TARGET_STUB");
3100     }
3101   if (TYPE_STATIC (type))
3102     {
3103       puts_filtered (" TYPE_FLAG_STATIC");
3104     }
3105   if (TYPE_PROTOTYPED (type))
3106     {
3107       puts_filtered (" TYPE_FLAG_PROTOTYPED");
3108     }
3109   if (TYPE_INCOMPLETE (type))
3110     {
3111       puts_filtered (" TYPE_FLAG_INCOMPLETE");
3112     }
3113   if (TYPE_VARARGS (type))
3114     {
3115       puts_filtered (" TYPE_FLAG_VARARGS");
3116     }
3117   /* This is used for things like AltiVec registers on ppc.  Gcc emits
3118      an attribute for the array type, which tells whether or not we
3119      have a vector, instead of a regular array.  */
3120   if (TYPE_VECTOR (type))
3121     {
3122       puts_filtered (" TYPE_FLAG_VECTOR");
3123     }
3124   puts_filtered ("\n");
3125   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3126   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3127   puts_filtered ("\n");
3128   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3129     {
3130       printfi_filtered (spaces + 2,
3131                         "[%d] bitpos %d bitsize %d type ",
3132                         idx, TYPE_FIELD_BITPOS (type, idx),
3133                         TYPE_FIELD_BITSIZE (type, idx));
3134       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3135       printf_filtered (" name '%s' (",
3136                        TYPE_FIELD_NAME (type, idx) != NULL
3137                        ? TYPE_FIELD_NAME (type, idx)
3138                        : "<NULL>");
3139       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3140       printf_filtered (")\n");
3141       if (TYPE_FIELD_TYPE (type, idx) != NULL)
3142         {
3143           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3144         }
3145     }
3146   printfi_filtered (spaces, "vptr_basetype ");
3147   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3148   puts_filtered ("\n");
3149   if (TYPE_VPTR_BASETYPE (type) != NULL)
3150     {
3151       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3152     }
3153   printfi_filtered (spaces, "vptr_fieldno %d\n", 
3154                     TYPE_VPTR_FIELDNO (type));
3155   switch (TYPE_CODE (type))
3156     {
3157     case TYPE_CODE_STRUCT:
3158       printfi_filtered (spaces, "cplus_stuff ");
3159       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
3160                               gdb_stdout);
3161       puts_filtered ("\n");
3162       print_cplus_stuff (type, spaces);
3163       break;
3164
3165     case TYPE_CODE_FLT:
3166       printfi_filtered (spaces, "floatformat ");
3167       if (TYPE_FLOATFORMAT (type) == NULL)
3168         puts_filtered ("(null)");
3169       else
3170         {
3171           puts_filtered ("{ ");
3172           if (TYPE_FLOATFORMAT (type)[0] == NULL
3173               || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3174             puts_filtered ("(null)");
3175           else
3176             puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3177
3178           puts_filtered (", ");
3179           if (TYPE_FLOATFORMAT (type)[1] == NULL
3180               || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3181             puts_filtered ("(null)");
3182           else
3183             puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3184
3185           puts_filtered (" }");
3186         }
3187       puts_filtered ("\n");
3188       break;
3189
3190     default:
3191       /* We have to pick one of the union types to be able print and
3192          test the value.  Pick cplus_struct_type, even though we know
3193          it isn't any particular one.  */
3194       printfi_filtered (spaces, "type_specific ");
3195       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
3196       if (TYPE_CPLUS_SPECIFIC (type) != NULL)
3197         {
3198           printf_filtered (_(" (unknown data form)"));
3199         }
3200       printf_filtered ("\n");
3201       break;
3202
3203     }
3204   if (spaces == 0)
3205     obstack_free (&dont_print_type_obstack, NULL);
3206 }
3207
3208 /* Trivial helpers for the libiberty hash table, for mapping one
3209    type to another.  */
3210
3211 struct type_pair
3212 {
3213   struct type *old, *new;
3214 };
3215
3216 static hashval_t
3217 type_pair_hash (const void *item)
3218 {
3219   const struct type_pair *pair = item;
3220   return htab_hash_pointer (pair->old);
3221 }
3222
3223 static int
3224 type_pair_eq (const void *item_lhs, const void *item_rhs)
3225 {
3226   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3227   return lhs->old == rhs->old;
3228 }
3229
3230 /* Allocate the hash table used by copy_type_recursive to walk
3231    types without duplicates.  We use OBJFILE's obstack, because
3232    OBJFILE is about to be deleted.  */
3233
3234 htab_t
3235 create_copied_types_hash (struct objfile *objfile)
3236 {
3237   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3238                                NULL, &objfile->objfile_obstack,
3239                                hashtab_obstack_allocate,
3240                                dummy_obstack_deallocate);
3241 }
3242
3243 /* Recursively copy (deep copy) TYPE, if it is associated with
3244    OBJFILE.  Return a new type allocated using malloc, a saved type if
3245    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
3246    not associated with OBJFILE.  */
3247
3248 struct type *
3249 copy_type_recursive (struct objfile *objfile, 
3250                      struct type *type,
3251                      htab_t copied_types)
3252 {
3253   struct type_pair *stored, pair;
3254   void **slot;
3255   struct type *new_type;
3256
3257   if (TYPE_OBJFILE (type) == NULL)
3258     return type;
3259
3260   /* This type shouldn't be pointing to any types in other objfiles;
3261      if it did, the type might disappear unexpectedly.  */
3262   gdb_assert (TYPE_OBJFILE (type) == objfile);
3263
3264   pair.old = type;
3265   slot = htab_find_slot (copied_types, &pair, INSERT);
3266   if (*slot != NULL)
3267     return ((struct type_pair *) *slot)->new;
3268
3269   new_type = alloc_type (NULL);
3270
3271   /* We must add the new type to the hash table immediately, in case
3272      we encounter this type again during a recursive call below.  */
3273   stored = xmalloc (sizeof (struct type_pair));
3274   stored->old = type;
3275   stored->new = new_type;
3276   *slot = stored;
3277
3278   /* Copy the common fields of types.  */
3279   TYPE_CODE (new_type) = TYPE_CODE (type);
3280   TYPE_ARRAY_UPPER_BOUND_TYPE (new_type) = 
3281     TYPE_ARRAY_UPPER_BOUND_TYPE (type);
3282   TYPE_ARRAY_LOWER_BOUND_TYPE (new_type) = 
3283     TYPE_ARRAY_LOWER_BOUND_TYPE (type);
3284   if (TYPE_NAME (type))
3285     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3286   if (TYPE_TAG_NAME (type))
3287     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
3288   TYPE_FLAGS (new_type) = TYPE_FLAGS (type);
3289   TYPE_VPTR_FIELDNO (new_type) = TYPE_VPTR_FIELDNO (type);
3290
3291   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3292   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3293
3294   /* Copy the fields.  */
3295   TYPE_NFIELDS (new_type) = TYPE_NFIELDS (type);
3296   if (TYPE_NFIELDS (type))
3297     {
3298       int i, nfields;
3299
3300       nfields = TYPE_NFIELDS (type);
3301       TYPE_FIELDS (new_type) = xmalloc (sizeof (struct field) * nfields);
3302       for (i = 0; i < nfields; i++)
3303         {
3304           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
3305             TYPE_FIELD_ARTIFICIAL (type, i);
3306           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3307           if (TYPE_FIELD_TYPE (type, i))
3308             TYPE_FIELD_TYPE (new_type, i)
3309               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3310                                      copied_types);
3311           if (TYPE_FIELD_NAME (type, i))
3312             TYPE_FIELD_NAME (new_type, i) = 
3313               xstrdup (TYPE_FIELD_NAME (type, i));
3314           if (TYPE_FIELD_STATIC_HAS_ADDR (type, i))
3315             SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3316                                 TYPE_FIELD_STATIC_PHYSADDR (type, i));
3317           else if (TYPE_FIELD_STATIC (type, i))
3318             SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3319                                 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type, 
3320                                                                      i)));
3321           else
3322             {
3323               TYPE_FIELD_BITPOS (new_type, i) = 
3324                 TYPE_FIELD_BITPOS (type, i);
3325               TYPE_FIELD_STATIC_KIND (new_type, i) = 0;
3326             }
3327         }
3328     }
3329
3330   /* Copy pointers to other types.  */
3331   if (TYPE_TARGET_TYPE (type))
3332     TYPE_TARGET_TYPE (new_type) = 
3333       copy_type_recursive (objfile, 
3334                            TYPE_TARGET_TYPE (type),
3335                            copied_types);
3336   if (TYPE_VPTR_BASETYPE (type))
3337     TYPE_VPTR_BASETYPE (new_type) = 
3338       copy_type_recursive (objfile,
3339                            TYPE_VPTR_BASETYPE (type),
3340                            copied_types);
3341   /* Maybe copy the type_specific bits.
3342
3343      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3344      base classes and methods.  There's no fundamental reason why we
3345      can't, but at the moment it is not needed.  */
3346
3347   if (TYPE_CODE (type) == TYPE_CODE_FLT)
3348     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
3349   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3350            || TYPE_CODE (type) == TYPE_CODE_UNION
3351            || TYPE_CODE (type) == TYPE_CODE_TEMPLATE
3352            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3353     INIT_CPLUS_SPECIFIC (new_type);
3354
3355   return new_type;
3356 }
3357
3358 static struct type *
3359 build_flt (int bit, char *name, const struct floatformat **floatformats)
3360 {
3361   struct type *t;
3362
3363   if (bit == -1)
3364     {
3365       gdb_assert (floatformats != NULL);
3366       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3367       bit = floatformats[0]->totalsize;
3368     }
3369   gdb_assert (bit >= 0);
3370
3371   t = init_type (TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, 0, name, NULL);
3372   TYPE_FLOATFORMAT (t) = floatformats;
3373   return t;
3374 }
3375
3376 static struct gdbarch_data *gdbtypes_data;
3377
3378 const struct builtin_type *
3379 builtin_type (struct gdbarch *gdbarch)
3380 {
3381   return gdbarch_data (gdbarch, gdbtypes_data);
3382 }
3383
3384
3385 static struct type *
3386 build_complex (int bit, char *name, struct type *target_type)
3387 {
3388   struct type *t;
3389   if (bit <= 0 || target_type == builtin_type_error)
3390     {
3391       gdb_assert (builtin_type_error != NULL);
3392       return builtin_type_error;
3393     }
3394   t = init_type (TYPE_CODE_COMPLEX, 2 * bit / TARGET_CHAR_BIT,
3395                  0, name, (struct objfile *) NULL);
3396   TYPE_TARGET_TYPE (t) = target_type;
3397   return t;
3398 }
3399
3400 static void *
3401 gdbtypes_post_init (struct gdbarch *gdbarch)
3402 {
3403   struct builtin_type *builtin_type
3404     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
3405
3406   builtin_type->builtin_void =
3407     init_type (TYPE_CODE_VOID, 1,
3408                0,
3409                "void", (struct objfile *) NULL);
3410   builtin_type->builtin_char =
3411     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3412                (TYPE_FLAG_NOSIGN
3413                 | (gdbarch_char_signed (current_gdbarch) ? 
3414                    0 : TYPE_FLAG_UNSIGNED)),
3415                "char", (struct objfile *) NULL);
3416   builtin_type->builtin_true_char =
3417     init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3418                0,
3419                "true character", (struct objfile *) NULL);
3420   builtin_type->builtin_signed_char =
3421     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3422                0,
3423                "signed char", (struct objfile *) NULL);
3424   builtin_type->builtin_unsigned_char =
3425     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3426                TYPE_FLAG_UNSIGNED,
3427                "unsigned char", (struct objfile *) NULL);
3428   builtin_type->builtin_short =
3429     init_type (TYPE_CODE_INT, 
3430                gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3431                0, "short", (struct objfile *) NULL);
3432   builtin_type->builtin_unsigned_short =
3433     init_type (TYPE_CODE_INT, 
3434                gdbarch_short_bit (current_gdbarch) / TARGET_CHAR_BIT,
3435                TYPE_FLAG_UNSIGNED, "unsigned short", 
3436                (struct objfile *) NULL);
3437   builtin_type->builtin_int =
3438     init_type (TYPE_CODE_INT, 
3439                gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3440                0, "int", (struct objfile *) NULL);
3441   builtin_type->builtin_unsigned_int =
3442     init_type (TYPE_CODE_INT, 
3443                gdbarch_int_bit (current_gdbarch) / TARGET_CHAR_BIT,
3444                TYPE_FLAG_UNSIGNED, "unsigned int", 
3445                (struct objfile *) NULL);
3446   builtin_type->builtin_long =
3447     init_type (TYPE_CODE_INT, 
3448                gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3449                0, "long", (struct objfile *) NULL);
3450   builtin_type->builtin_unsigned_long =
3451     init_type (TYPE_CODE_INT, 
3452                gdbarch_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3453                TYPE_FLAG_UNSIGNED, "unsigned long", 
3454                (struct objfile *) NULL);
3455   builtin_type->builtin_long_long =
3456     init_type (TYPE_CODE_INT,
3457                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3458                0, "long long", (struct objfile *) NULL);
3459   builtin_type->builtin_unsigned_long_long =
3460     init_type (TYPE_CODE_INT,
3461                gdbarch_long_long_bit (current_gdbarch) / TARGET_CHAR_BIT,
3462                TYPE_FLAG_UNSIGNED, "unsigned long long", 
3463                (struct objfile *) NULL);
3464   builtin_type->builtin_float
3465     = build_flt (gdbarch_float_bit (gdbarch), "float",
3466                  gdbarch_float_format (gdbarch));
3467   builtin_type->builtin_double
3468     = build_flt (gdbarch_double_bit (gdbarch), "double",
3469                  gdbarch_double_format (gdbarch));
3470   builtin_type->builtin_long_double
3471     = build_flt (gdbarch_long_double_bit (gdbarch), "long double",
3472                  gdbarch_long_double_format (gdbarch));
3473   builtin_type->builtin_complex
3474     = build_complex (gdbarch_float_bit (gdbarch), "complex",
3475                      builtin_type->builtin_float);
3476   builtin_type->builtin_double_complex
3477     = build_complex (gdbarch_double_bit (gdbarch), "double complex",
3478                      builtin_type->builtin_double);
3479   builtin_type->builtin_string =
3480     init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3481                0,
3482                "string", (struct objfile *) NULL);
3483   builtin_type->builtin_bool =
3484     init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3485                0,
3486                "bool", (struct objfile *) NULL);
3487
3488   /* Pointer/Address types.  */
3489
3490   /* NOTE: on some targets, addresses and pointers are not necessarily
3491      the same --- for example, on the D10V, pointers are 16 bits long,
3492      but addresses are 32 bits long.  See doc/gdbint.texinfo,
3493      ``Pointers Are Not Always Addresses''.
3494
3495      The upshot is:
3496      - gdb's `struct type' always describes the target's
3497        representation.
3498      - gdb's `struct value' objects should always hold values in
3499        target form.
3500      - gdb's CORE_ADDR values are addresses in the unified virtual
3501        address space that the assembler and linker work with.  Thus,
3502        since target_read_memory takes a CORE_ADDR as an argument, it
3503        can access any memory on the target, even if the processor has
3504        separate code and data address spaces.
3505
3506      So, for example:
3507      - If v is a value holding a D10V code pointer, its contents are
3508        in target form: a big-endian address left-shifted two bits.
3509      - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3510        sizeof (void *) == 2 on the target.
3511
3512      In this context, builtin_type->CORE_ADDR is a bit odd: it's a
3513      target type for a value the target will never see.  It's only
3514      used to hold the values of (typeless) linker symbols, which are
3515      indeed in the unified virtual address space.  */
3516
3517   builtin_type->builtin_data_ptr =
3518     make_pointer_type (builtin_type->builtin_void, NULL);
3519   builtin_type->builtin_func_ptr =
3520     lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
3521   builtin_type->builtin_core_addr =
3522     init_type (TYPE_CODE_INT, 
3523                gdbarch_addr_bit (current_gdbarch) / 8,
3524                TYPE_FLAG_UNSIGNED,
3525                "__CORE_ADDR", (struct objfile *) NULL);
3526
3527
3528   /* The following set of types is used for symbols with no
3529      debug information.  */
3530   builtin_type->nodebug_text_symbol =
3531     init_type (TYPE_CODE_FUNC, 1, 0, 
3532                "<text variable, no debug info>", NULL);
3533   TYPE_TARGET_TYPE (builtin_type->nodebug_text_symbol) =
3534     builtin_type->builtin_int;
3535   builtin_type->nodebug_data_symbol =
3536     init_type (TYPE_CODE_INT, 
3537                gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3538                "<data variable, no debug info>", NULL);
3539   builtin_type->nodebug_unknown_symbol =
3540     init_type (TYPE_CODE_INT, 1, 0,
3541                "<variable (not text or data), no debug info>", NULL);
3542   builtin_type->nodebug_tls_symbol =
3543     init_type (TYPE_CODE_INT, 
3544                gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3545                "<thread local variable, no debug info>", NULL);
3546
3547   return builtin_type;
3548 }
3549
3550 extern void _initialize_gdbtypes (void);
3551 void
3552 _initialize_gdbtypes (void)
3553 {
3554   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
3555
3556   /* FIXME: The following types are architecture-neutral.  However,
3557      they contain pointer_type and reference_type fields potentially
3558      caching pointer or reference types that *are* architecture
3559      dependent.  */
3560
3561   builtin_type_int0 =
3562     init_type (TYPE_CODE_INT, 0 / 8,
3563                0,
3564                "int0_t", (struct objfile *) NULL);
3565   builtin_type_int8 =
3566     init_type (TYPE_CODE_INT, 8 / 8,
3567                0,
3568                "int8_t", (struct objfile *) NULL);
3569   builtin_type_uint8 =
3570     init_type (TYPE_CODE_INT, 8 / 8,
3571                TYPE_FLAG_UNSIGNED,
3572                "uint8_t", (struct objfile *) NULL);
3573   builtin_type_int16 =
3574     init_type (TYPE_CODE_INT, 16 / 8,
3575                0,
3576                "int16_t", (struct objfile *) NULL);
3577   builtin_type_uint16 =
3578     init_type (TYPE_CODE_INT, 16 / 8,
3579                TYPE_FLAG_UNSIGNED,
3580                "uint16_t", (struct objfile *) NULL);
3581   builtin_type_int32 =
3582     init_type (TYPE_CODE_INT, 32 / 8,
3583                0,
3584                "int32_t", (struct objfile *) NULL);
3585   builtin_type_uint32 =
3586     init_type (TYPE_CODE_INT, 32 / 8,
3587                TYPE_FLAG_UNSIGNED,
3588                "uint32_t", (struct objfile *) NULL);
3589   builtin_type_int64 =
3590     init_type (TYPE_CODE_INT, 64 / 8,
3591                0,
3592                "int64_t", (struct objfile *) NULL);
3593   builtin_type_uint64 =
3594     init_type (TYPE_CODE_INT, 64 / 8,
3595                TYPE_FLAG_UNSIGNED,
3596                "uint64_t", (struct objfile *) NULL);
3597   builtin_type_int128 =
3598     init_type (TYPE_CODE_INT, 128 / 8,
3599                0,
3600                "int128_t", (struct objfile *) NULL);
3601   builtin_type_uint128 =
3602     init_type (TYPE_CODE_INT, 128 / 8,
3603                TYPE_FLAG_UNSIGNED,
3604                "uint128_t", (struct objfile *) NULL);
3605
3606   builtin_type_ieee_single =
3607     build_flt (-1, "builtin_type_ieee_single", floatformats_ieee_single);
3608   builtin_type_ieee_double =
3609     build_flt (-1, "builtin_type_ieee_double", floatformats_ieee_double);
3610   builtin_type_i387_ext =
3611     build_flt (-1, "builtin_type_i387_ext", floatformats_i387_ext);
3612   builtin_type_m68881_ext =
3613     build_flt (-1, "builtin_type_m68881_ext", floatformats_m68881_ext);
3614   builtin_type_arm_ext =
3615     build_flt (-1, "builtin_type_arm_ext", floatformats_arm_ext);
3616   builtin_type_ia64_spill =
3617     build_flt (-1, "builtin_type_ia64_spill", floatformats_ia64_spill);
3618   builtin_type_ia64_quad =
3619     build_flt (-1, "builtin_type_ia64_quad", floatformats_ia64_quad);
3620
3621   add_setshow_zinteger_cmd ("overload", no_class, &overload_debug, _("\
3622 Set debugging of C++ overloading."), _("\
3623 Show debugging of C++ overloading."), _("\
3624 When enabled, ranking of the functions is displayed."),
3625                             NULL,
3626                             show_overload_debug,
3627                             &setdebuglist, &showdebuglist);
3628
3629   /* Add user knob for controlling resolution of opaque types.  */
3630   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3631                            &opaque_type_resolution, _("\
3632 Set resolution of opaque struct/class/union types (if set before loading symbols)."), _("\
3633 Show resolution of opaque struct/class/union types (if set before loading symbols)."), NULL,
3634                            NULL,
3635                            show_opaque_type_resolution,
3636                            &setlist, &showlist);
3637 }