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