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