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