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