OSDN Git Service

daily update
[pf3gnuchains/pf3gnuchains4x.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 struct type *
1353 check_typedef (struct type *type)
1354 {
1355   struct type *orig_type = type;
1356   int is_const, is_volatile;
1357
1358   gdb_assert (type);
1359
1360   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1361     {
1362       if (!TYPE_TARGET_TYPE (type))
1363         {
1364           char *name;
1365           struct symbol *sym;
1366
1367           /* It is dangerous to call lookup_symbol if we are currently
1368              reading a symtab.  Infinite recursion is one danger.  */
1369           if (currently_reading_symtab)
1370             return type;
1371
1372           name = type_name_no_tag (type);
1373           /* FIXME: shouldn't we separately check the TYPE_NAME and
1374              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1375              VAR_DOMAIN as appropriate?  (this code was written before
1376              TYPE_NAME and TYPE_TAG_NAME were separate).  */
1377           if (name == NULL)
1378             {
1379               stub_noname_complaint ();
1380               return type;
1381             }
1382           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1383           if (sym)
1384             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1385           else                                  /* TYPE_CODE_UNDEF */
1386             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
1387         }
1388       type = TYPE_TARGET_TYPE (type);
1389     }
1390
1391   is_const = TYPE_CONST (type);
1392   is_volatile = TYPE_VOLATILE (type);
1393
1394   /* If this is a struct/class/union with no fields, then check
1395      whether a full definition exists somewhere else.  This is for
1396      systems where a type definition with no fields is issued for such
1397      types, instead of identifying them as stub types in the first
1398      place.  */
1399
1400   if (TYPE_IS_OPAQUE (type) 
1401       && opaque_type_resolution 
1402       && !currently_reading_symtab)
1403     {
1404       char *name = type_name_no_tag (type);
1405       struct type *newtype;
1406       if (name == NULL)
1407         {
1408           stub_noname_complaint ();
1409           return type;
1410         }
1411       newtype = lookup_transparent_type (name);
1412
1413       if (newtype)
1414         {
1415           /* If the resolved type and the stub are in the same
1416              objfile, then replace the stub type with the real deal.
1417              But if they're in separate objfiles, leave the stub
1418              alone; we'll just look up the transparent type every time
1419              we call check_typedef.  We can't create pointers between
1420              types allocated to different objfiles, since they may
1421              have different lifetimes.  Trying to copy NEWTYPE over to
1422              TYPE's objfile is pointless, too, since you'll have to
1423              move over any other types NEWTYPE refers to, which could
1424              be an unbounded amount of stuff.  */
1425           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1426             make_cv_type (is_const, is_volatile, newtype, &type);
1427           else
1428             type = newtype;
1429         }
1430     }
1431   /* Otherwise, rely on the stub flag being set for opaque/stubbed
1432      types.  */
1433   else if (TYPE_STUB (type) && !currently_reading_symtab)
1434     {
1435       char *name = type_name_no_tag (type);
1436       /* FIXME: shouldn't we separately check the TYPE_NAME and the
1437          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1438          as appropriate?  (this code was written before TYPE_NAME and
1439          TYPE_TAG_NAME were separate).  */
1440       struct symbol *sym;
1441       if (name == NULL)
1442         {
1443           stub_noname_complaint ();
1444           return type;
1445         }
1446       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1447       if (sym)
1448         {
1449           /* Same as above for opaque types, we can replace the stub
1450              with the complete type only if they are int the same
1451              objfile.  */
1452           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1453             make_cv_type (is_const, is_volatile, 
1454                           SYMBOL_TYPE (sym), &type);
1455           else
1456             type = SYMBOL_TYPE (sym);
1457         }
1458     }
1459
1460   if (TYPE_TARGET_STUB (type))
1461     {
1462       struct type *range_type;
1463       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1464
1465       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1466         {
1467           /* Empty.  */
1468         }
1469       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1470                && TYPE_NFIELDS (type) == 1
1471                && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
1472                    == TYPE_CODE_RANGE))
1473         {
1474           /* Now recompute the length of the array type, based on its
1475              number of elements and the target type's length.
1476              Watch out for Ada null Ada arrays where the high bound
1477              is smaller than the low bound. */
1478           const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
1479           const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
1480           ULONGEST len;
1481
1482           if (high_bound < low_bound)
1483             len = 0;
1484           else {
1485             /* For now, we conservatively take the array length to be 0
1486                if its length exceeds UINT_MAX.  The code below assumes
1487                that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
1488                which is technically not guaranteed by C, but is usually true
1489                (because it would be true if x were unsigned with its
1490                high-order bit on). It uses the fact that
1491                high_bound-low_bound is always representable in
1492                ULONGEST and that if high_bound-low_bound+1 overflows,
1493                it overflows to 0.  We must change these tests if we 
1494                decide to increase the representation of TYPE_LENGTH
1495                from unsigned int to ULONGEST. */
1496             ULONGEST ulow = low_bound, uhigh = high_bound;
1497             ULONGEST tlen = TYPE_LENGTH (target_type);
1498
1499             len = tlen * (uhigh - ulow + 1);
1500             if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh 
1501                 || len > UINT_MAX)
1502               len = 0;
1503           }
1504           TYPE_LENGTH (type) = len;
1505           TYPE_TARGET_STUB (type) = 0;
1506         }
1507       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1508         {
1509           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1510           TYPE_TARGET_STUB (type) = 0;
1511         }
1512     }
1513   /* Cache TYPE_LENGTH for future use.  */
1514   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1515   return type;
1516 }
1517
1518 /* Parse a type expression in the string [P..P+LENGTH).  If an error
1519    occurs, silently return a void type.  */
1520
1521 static struct type *
1522 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
1523 {
1524   struct ui_file *saved_gdb_stderr;
1525   struct type *type;
1526
1527   /* Suppress error messages.  */
1528   saved_gdb_stderr = gdb_stderr;
1529   gdb_stderr = ui_file_new ();
1530
1531   /* Call parse_and_eval_type() without fear of longjmp()s.  */
1532   if (!gdb_parse_and_eval_type (p, length, &type))
1533     type = builtin_type (gdbarch)->builtin_void;
1534
1535   /* Stop suppressing error messages.  */
1536   ui_file_delete (gdb_stderr);
1537   gdb_stderr = saved_gdb_stderr;
1538
1539   return type;
1540 }
1541
1542 /* Ugly hack to convert method stubs into method types.
1543
1544    He ain't kiddin'.  This demangles the name of the method into a
1545    string including argument types, parses out each argument type,
1546    generates a string casting a zero to that type, evaluates the
1547    string, and stuffs the resulting type into an argtype vector!!!
1548    Then it knows the type of the whole function (including argument
1549    types for overloading), which info used to be in the stab's but was
1550    removed to hack back the space required for them.  */
1551
1552 static void
1553 check_stub_method (struct type *type, int method_id, int signature_id)
1554 {
1555   struct gdbarch *gdbarch = get_type_arch (type);
1556   struct fn_field *f;
1557   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1558   char *demangled_name = cplus_demangle (mangled_name,
1559                                          DMGL_PARAMS | DMGL_ANSI);
1560   char *argtypetext, *p;
1561   int depth = 0, argcount = 1;
1562   struct field *argtypes;
1563   struct type *mtype;
1564
1565   /* Make sure we got back a function string that we can use.  */
1566   if (demangled_name)
1567     p = strchr (demangled_name, '(');
1568   else
1569     p = NULL;
1570
1571   if (demangled_name == NULL || p == NULL)
1572     error (_("Internal: Cannot demangle mangled name `%s'."), 
1573            mangled_name);
1574
1575   /* Now, read in the parameters that define this type.  */
1576   p += 1;
1577   argtypetext = p;
1578   while (*p)
1579     {
1580       if (*p == '(' || *p == '<')
1581         {
1582           depth += 1;
1583         }
1584       else if (*p == ')' || *p == '>')
1585         {
1586           depth -= 1;
1587         }
1588       else if (*p == ',' && depth == 0)
1589         {
1590           argcount += 1;
1591         }
1592
1593       p += 1;
1594     }
1595
1596   /* If we read one argument and it was ``void'', don't count it.  */
1597   if (strncmp (argtypetext, "(void)", 6) == 0)
1598     argcount -= 1;
1599
1600   /* We need one extra slot, for the THIS pointer.  */
1601
1602   argtypes = (struct field *)
1603     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
1604   p = argtypetext;
1605
1606   /* Add THIS pointer for non-static methods.  */
1607   f = TYPE_FN_FIELDLIST1 (type, method_id);
1608   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1609     argcount = 0;
1610   else
1611     {
1612       argtypes[0].type = lookup_pointer_type (type);
1613       argcount = 1;
1614     }
1615
1616   if (*p != ')')                /* () means no args, skip while */
1617     {
1618       depth = 0;
1619       while (*p)
1620         {
1621           if (depth <= 0 && (*p == ',' || *p == ')'))
1622             {
1623               /* Avoid parsing of ellipsis, they will be handled below.
1624                  Also avoid ``void'' as above.  */
1625               if (strncmp (argtypetext, "...", p - argtypetext) != 0
1626                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
1627                 {
1628                   argtypes[argcount].type =
1629                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
1630                   argcount += 1;
1631                 }
1632               argtypetext = p + 1;
1633             }
1634
1635           if (*p == '(' || *p == '<')
1636             {
1637               depth += 1;
1638             }
1639           else if (*p == ')' || *p == '>')
1640             {
1641               depth -= 1;
1642             }
1643
1644           p += 1;
1645         }
1646     }
1647
1648   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1649
1650   /* Now update the old "stub" type into a real type.  */
1651   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1652   TYPE_DOMAIN_TYPE (mtype) = type;
1653   TYPE_FIELDS (mtype) = argtypes;
1654   TYPE_NFIELDS (mtype) = argcount;
1655   TYPE_STUB (mtype) = 0;
1656   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1657   if (p[-2] == '.')
1658     TYPE_VARARGS (mtype) = 1;
1659
1660   xfree (demangled_name);
1661 }
1662
1663 /* This is the external interface to check_stub_method, above.  This
1664    function unstubs all of the signatures for TYPE's METHOD_ID method
1665    name.  After calling this function TYPE_FN_FIELD_STUB will be
1666    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
1667    correct.
1668
1669    This function unfortunately can not die until stabs do.  */
1670
1671 void
1672 check_stub_method_group (struct type *type, int method_id)
1673 {
1674   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1675   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
1676   int j, found_stub = 0;
1677
1678   for (j = 0; j < len; j++)
1679     if (TYPE_FN_FIELD_STUB (f, j))
1680       {
1681         found_stub = 1;
1682         check_stub_method (type, method_id, j);
1683       }
1684
1685   /* GNU v3 methods with incorrect names were corrected when we read
1686      in type information, because it was cheaper to do it then.  The
1687      only GNU v2 methods with incorrect method names are operators and
1688      destructors; destructors were also corrected when we read in type
1689      information.
1690
1691      Therefore the only thing we need to handle here are v2 operator
1692      names.  */
1693   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1694     {
1695       int ret;
1696       char dem_opname[256];
1697
1698       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
1699                                                            method_id),
1700                                    dem_opname, DMGL_ANSI);
1701       if (!ret)
1702         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
1703                                                              method_id),
1704                                      dem_opname, 0);
1705       if (ret)
1706         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
1707     }
1708 }
1709
1710 const struct cplus_struct_type cplus_struct_default;
1711
1712 void
1713 allocate_cplus_struct_type (struct type *type)
1714 {
1715   if (!HAVE_CPLUS_STRUCT (type))
1716     {
1717       TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1718         TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1719       *(TYPE_CPLUS_SPECIFIC (type)) = cplus_struct_default;
1720     }
1721 }
1722
1723 /* Helper function to initialize the standard scalar types.
1724
1725    If NAME is non-NULL, then we make a copy of the string pointed
1726    to by name in the objfile_obstack for that objfile, and initialize
1727    the type name to that copy.  There are places (mipsread.c in particular),
1728    where init_type is called with a NULL value for NAME).  */
1729
1730 struct type *
1731 init_type (enum type_code code, int length, int flags,
1732            char *name, struct objfile *objfile)
1733 {
1734   struct type *type;
1735
1736   type = alloc_type (objfile);
1737   TYPE_CODE (type) = code;
1738   TYPE_LENGTH (type) = length;
1739
1740   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
1741   if (flags & TYPE_FLAG_UNSIGNED)
1742     TYPE_UNSIGNED (type) = 1;
1743   if (flags & TYPE_FLAG_NOSIGN)
1744     TYPE_NOSIGN (type) = 1;
1745   if (flags & TYPE_FLAG_STUB)
1746     TYPE_STUB (type) = 1;
1747   if (flags & TYPE_FLAG_TARGET_STUB)
1748     TYPE_TARGET_STUB (type) = 1;
1749   if (flags & TYPE_FLAG_STATIC)
1750     TYPE_STATIC (type) = 1;
1751   if (flags & TYPE_FLAG_PROTOTYPED)
1752     TYPE_PROTOTYPED (type) = 1;
1753   if (flags & TYPE_FLAG_INCOMPLETE)
1754     TYPE_INCOMPLETE (type) = 1;
1755   if (flags & TYPE_FLAG_VARARGS)
1756     TYPE_VARARGS (type) = 1;
1757   if (flags & TYPE_FLAG_VECTOR)
1758     TYPE_VECTOR (type) = 1;
1759   if (flags & TYPE_FLAG_STUB_SUPPORTED)
1760     TYPE_STUB_SUPPORTED (type) = 1;
1761   if (flags & TYPE_FLAG_NOTTEXT)
1762     TYPE_NOTTEXT (type) = 1;
1763   if (flags & TYPE_FLAG_FIXED_INSTANCE)
1764     TYPE_FIXED_INSTANCE (type) = 1;
1765
1766   if (name)
1767     TYPE_NAME (type) = obsavestring (name, strlen (name),
1768                                      &objfile->objfile_obstack);
1769
1770   /* C++ fancies.  */
1771
1772   if (name && strcmp (name, "char") == 0)
1773     TYPE_NOSIGN (type) = 1;
1774
1775   if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
1776       || code == TYPE_CODE_NAMESPACE)
1777     {
1778       INIT_CPLUS_SPECIFIC (type);
1779     }
1780   return type;
1781 }
1782
1783 int
1784 can_dereference (struct type *t)
1785 {
1786   /* FIXME: Should we return true for references as well as
1787      pointers?  */
1788   CHECK_TYPEDEF (t);
1789   return
1790     (t != NULL
1791      && TYPE_CODE (t) == TYPE_CODE_PTR
1792      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1793 }
1794
1795 int
1796 is_integral_type (struct type *t)
1797 {
1798   CHECK_TYPEDEF (t);
1799   return
1800     ((t != NULL)
1801      && ((TYPE_CODE (t) == TYPE_CODE_INT)
1802          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
1803          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
1804          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
1805          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
1806          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
1807 }
1808
1809 /* Check whether BASE is an ancestor or base class or DCLASS 
1810    Return 1 if so, and 0 if not.
1811    Note: callers may want to check for identity of the types before
1812    calling this function -- identical types are considered to satisfy
1813    the ancestor relationship even if they're identical.  */
1814
1815 int
1816 is_ancestor (struct type *base, struct type *dclass)
1817 {
1818   int i;
1819
1820   CHECK_TYPEDEF (base);
1821   CHECK_TYPEDEF (dclass);
1822
1823   if (base == dclass)
1824     return 1;
1825   if (TYPE_NAME (base) && TYPE_NAME (dclass) 
1826       && !strcmp (TYPE_NAME (base), TYPE_NAME (dclass)))
1827     return 1;
1828
1829   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1830     if (is_ancestor (base, TYPE_BASECLASS (dclass, i)))
1831       return 1;
1832
1833   return 0;
1834 }
1835 \f
1836
1837
1838 /* Functions for overload resolution begin here */
1839
1840 /* Compare two badness vectors A and B and return the result.
1841    0 => A and B are identical
1842    1 => A and B are incomparable
1843    2 => A is better than B
1844    3 => A is worse than B  */
1845
1846 int
1847 compare_badness (struct badness_vector *a, struct badness_vector *b)
1848 {
1849   int i;
1850   int tmp;
1851   short found_pos = 0;          /* any positives in c? */
1852   short found_neg = 0;          /* any negatives in c? */
1853
1854   /* differing lengths => incomparable */
1855   if (a->length != b->length)
1856     return 1;
1857
1858   /* Subtract b from a */
1859   for (i = 0; i < a->length; i++)
1860     {
1861       tmp = a->rank[i] - b->rank[i];
1862       if (tmp > 0)
1863         found_pos = 1;
1864       else if (tmp < 0)
1865         found_neg = 1;
1866     }
1867
1868   if (found_pos)
1869     {
1870       if (found_neg)
1871         return 1;               /* incomparable */
1872       else
1873         return 3;               /* A > B */
1874     }
1875   else
1876     /* no positives */
1877     {
1878       if (found_neg)
1879         return 2;               /* A < B */
1880       else
1881         return 0;               /* A == B */
1882     }
1883 }
1884
1885 /* Rank a function by comparing its parameter types (PARMS, length
1886    NPARMS), to the types of an argument list (ARGS, length NARGS).
1887    Return a pointer to a badness vector.  This has NARGS + 1
1888    entries.  */
1889
1890 struct badness_vector *
1891 rank_function (struct type **parms, int nparms, 
1892                struct type **args, int nargs)
1893 {
1894   int i;
1895   struct badness_vector *bv;
1896   int min_len = nparms < nargs ? nparms : nargs;
1897
1898   bv = xmalloc (sizeof (struct badness_vector));
1899   bv->length = nargs + 1;       /* add 1 for the length-match rank */
1900   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
1901
1902   /* First compare the lengths of the supplied lists.
1903      If there is a mismatch, set it to a high value.  */
1904
1905   /* pai/1997-06-03 FIXME: when we have debug info about default
1906      arguments and ellipsis parameter lists, we should consider those
1907      and rank the length-match more finely.  */
1908
1909   LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
1910
1911   /* Now rank all the parameters of the candidate function */
1912   for (i = 1; i <= min_len; i++)
1913     bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
1914
1915   /* If more arguments than parameters, add dummy entries */
1916   for (i = min_len + 1; i <= nargs; i++)
1917     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
1918
1919   return bv;
1920 }
1921
1922 /* Compare the names of two integer types, assuming that any sign
1923    qualifiers have been checked already.  We do it this way because
1924    there may be an "int" in the name of one of the types.  */
1925
1926 static int
1927 integer_types_same_name_p (const char *first, const char *second)
1928 {
1929   int first_p, second_p;
1930
1931   /* If both are shorts, return 1; if neither is a short, keep
1932      checking.  */
1933   first_p = (strstr (first, "short") != NULL);
1934   second_p = (strstr (second, "short") != NULL);
1935   if (first_p && second_p)
1936     return 1;
1937   if (first_p || second_p)
1938     return 0;
1939
1940   /* Likewise for long.  */
1941   first_p = (strstr (first, "long") != NULL);
1942   second_p = (strstr (second, "long") != NULL);
1943   if (first_p && second_p)
1944     return 1;
1945   if (first_p || second_p)
1946     return 0;
1947
1948   /* Likewise for char.  */
1949   first_p = (strstr (first, "char") != NULL);
1950   second_p = (strstr (second, "char") != NULL);
1951   if (first_p && second_p)
1952     return 1;
1953   if (first_p || second_p)
1954     return 0;
1955
1956   /* They must both be ints.  */
1957   return 1;
1958 }
1959
1960 /* Compare one type (PARM) for compatibility with another (ARG).
1961  * PARM is intended to be the parameter type of a function; and
1962  * ARG is the supplied argument's type.  This function tests if
1963  * the latter can be converted to the former.
1964  *
1965  * Return 0 if they are identical types;
1966  * Otherwise, return an integer which corresponds to how compatible
1967  * PARM is to ARG.  The higher the return value, the worse the match.
1968  * Generally the "bad" conversions are all uniformly assigned a 100.  */
1969
1970 int
1971 rank_one_type (struct type *parm, struct type *arg)
1972 {
1973   /* Identical type pointers.  */
1974   /* However, this still doesn't catch all cases of same type for arg
1975      and param.  The reason is that builtin types are different from
1976      the same ones constructed from the object.  */
1977   if (parm == arg)
1978     return 0;
1979
1980   /* Resolve typedefs */
1981   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
1982     parm = check_typedef (parm);
1983   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
1984     arg = check_typedef (arg);
1985
1986   /*
1987      Well, damnit, if the names are exactly the same, I'll say they
1988      are exactly the same.  This happens when we generate method
1989      stubs.  The types won't point to the same address, but they
1990      really are the same.
1991   */
1992
1993   if (TYPE_NAME (parm) && TYPE_NAME (arg) 
1994       && !strcmp (TYPE_NAME (parm), TYPE_NAME (arg)))
1995     return 0;
1996
1997   /* Check if identical after resolving typedefs.  */
1998   if (parm == arg)
1999     return 0;
2000
2001   /* See through references, since we can almost make non-references
2002      references.  */
2003   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2004     return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
2005             + REFERENCE_CONVERSION_BADNESS);
2006   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2007     return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
2008             + REFERENCE_CONVERSION_BADNESS);
2009   if (overload_debug)
2010   /* Debugging only.  */
2011     fprintf_filtered (gdb_stderr, 
2012                       "------ Arg is %s [%d], parm is %s [%d]\n",
2013                       TYPE_NAME (arg), TYPE_CODE (arg), 
2014                       TYPE_NAME (parm), TYPE_CODE (parm));
2015
2016   /* x -> y means arg of type x being supplied for parameter of type y */
2017
2018   switch (TYPE_CODE (parm))
2019     {
2020     case TYPE_CODE_PTR:
2021       switch (TYPE_CODE (arg))
2022         {
2023         case TYPE_CODE_PTR:
2024           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID
2025               && TYPE_CODE (TYPE_TARGET_TYPE (arg)) != TYPE_CODE_VOID)
2026             return VOID_PTR_CONVERSION_BADNESS;
2027           else
2028             return rank_one_type (TYPE_TARGET_TYPE (parm), 
2029                                   TYPE_TARGET_TYPE (arg));
2030         case TYPE_CODE_ARRAY:
2031           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2032                                 TYPE_TARGET_TYPE (arg));
2033         case TYPE_CODE_FUNC:
2034           return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2035         case TYPE_CODE_INT:
2036         case TYPE_CODE_ENUM:
2037         case TYPE_CODE_FLAGS:
2038         case TYPE_CODE_CHAR:
2039         case TYPE_CODE_RANGE:
2040         case TYPE_CODE_BOOL:
2041           return POINTER_CONVERSION_BADNESS;
2042         default:
2043           return INCOMPATIBLE_TYPE_BADNESS;
2044         }
2045     case TYPE_CODE_ARRAY:
2046       switch (TYPE_CODE (arg))
2047         {
2048         case TYPE_CODE_PTR:
2049         case TYPE_CODE_ARRAY:
2050           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2051                                 TYPE_TARGET_TYPE (arg));
2052         default:
2053           return INCOMPATIBLE_TYPE_BADNESS;
2054         }
2055     case TYPE_CODE_FUNC:
2056       switch (TYPE_CODE (arg))
2057         {
2058         case TYPE_CODE_PTR:     /* funcptr -> func */
2059           return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2060         default:
2061           return INCOMPATIBLE_TYPE_BADNESS;
2062         }
2063     case TYPE_CODE_INT:
2064       switch (TYPE_CODE (arg))
2065         {
2066         case TYPE_CODE_INT:
2067           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2068             {
2069               /* Deal with signed, unsigned, and plain chars and
2070                  signed and unsigned ints.  */
2071               if (TYPE_NOSIGN (parm))
2072                 {
2073                   /* This case only for character types */
2074                   if (TYPE_NOSIGN (arg))
2075                     return 0;   /* plain char -> plain char */
2076                   else          /* signed/unsigned char -> plain char */
2077                     return INTEGER_CONVERSION_BADNESS;
2078                 }
2079               else if (TYPE_UNSIGNED (parm))
2080                 {
2081                   if (TYPE_UNSIGNED (arg))
2082                     {
2083                       /* unsigned int -> unsigned int, or 
2084                          unsigned long -> unsigned long */
2085                       if (integer_types_same_name_p (TYPE_NAME (parm), 
2086                                                      TYPE_NAME (arg)))
2087                         return 0;
2088                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
2089                                                           "int")
2090                                && integer_types_same_name_p (TYPE_NAME (parm),
2091                                                              "long"))
2092                         return INTEGER_PROMOTION_BADNESS;       /* unsigned int -> unsigned long */
2093                       else
2094                         return INTEGER_CONVERSION_BADNESS;      /* unsigned long -> unsigned int */
2095                     }
2096                   else
2097                     {
2098                       if (integer_types_same_name_p (TYPE_NAME (arg), 
2099                                                      "long")
2100                           && integer_types_same_name_p (TYPE_NAME (parm), 
2101                                                         "int"))
2102                         return INTEGER_CONVERSION_BADNESS;      /* signed long -> unsigned int */
2103                       else
2104                         return INTEGER_CONVERSION_BADNESS;      /* signed int/long -> unsigned int/long */
2105                     }
2106                 }
2107               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2108                 {
2109                   if (integer_types_same_name_p (TYPE_NAME (parm), 
2110                                                  TYPE_NAME (arg)))
2111                     return 0;
2112                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
2113                                                       "int")
2114                            && integer_types_same_name_p (TYPE_NAME (parm), 
2115                                                          "long"))
2116                     return INTEGER_PROMOTION_BADNESS;
2117                   else
2118                     return INTEGER_CONVERSION_BADNESS;
2119                 }
2120               else
2121                 return INTEGER_CONVERSION_BADNESS;
2122             }
2123           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2124             return INTEGER_PROMOTION_BADNESS;
2125           else
2126             return INTEGER_CONVERSION_BADNESS;
2127         case TYPE_CODE_ENUM:
2128         case TYPE_CODE_FLAGS:
2129         case TYPE_CODE_CHAR:
2130         case TYPE_CODE_RANGE:
2131         case TYPE_CODE_BOOL:
2132           return INTEGER_PROMOTION_BADNESS;
2133         case TYPE_CODE_FLT:
2134           return INT_FLOAT_CONVERSION_BADNESS;
2135         case TYPE_CODE_PTR:
2136           return NS_POINTER_CONVERSION_BADNESS;
2137         default:
2138           return INCOMPATIBLE_TYPE_BADNESS;
2139         }
2140       break;
2141     case TYPE_CODE_ENUM:
2142       switch (TYPE_CODE (arg))
2143         {
2144         case TYPE_CODE_INT:
2145         case TYPE_CODE_CHAR:
2146         case TYPE_CODE_RANGE:
2147         case TYPE_CODE_BOOL:
2148         case TYPE_CODE_ENUM:
2149           return INTEGER_CONVERSION_BADNESS;
2150         case TYPE_CODE_FLT:
2151           return INT_FLOAT_CONVERSION_BADNESS;
2152         default:
2153           return INCOMPATIBLE_TYPE_BADNESS;
2154         }
2155       break;
2156     case TYPE_CODE_CHAR:
2157       switch (TYPE_CODE (arg))
2158         {
2159         case TYPE_CODE_RANGE:
2160         case TYPE_CODE_BOOL:
2161         case TYPE_CODE_ENUM:
2162           return INTEGER_CONVERSION_BADNESS;
2163         case TYPE_CODE_FLT:
2164           return INT_FLOAT_CONVERSION_BADNESS;
2165         case TYPE_CODE_INT:
2166           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2167             return INTEGER_CONVERSION_BADNESS;
2168           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2169             return INTEGER_PROMOTION_BADNESS;
2170           /* >>> !! else fall through !! <<< */
2171         case TYPE_CODE_CHAR:
2172           /* Deal with signed, unsigned, and plain chars for C++ and
2173              with int cases falling through from previous case.  */
2174           if (TYPE_NOSIGN (parm))
2175             {
2176               if (TYPE_NOSIGN (arg))
2177                 return 0;
2178               else
2179                 return INTEGER_CONVERSION_BADNESS;
2180             }
2181           else if (TYPE_UNSIGNED (parm))
2182             {
2183               if (TYPE_UNSIGNED (arg))
2184                 return 0;
2185               else
2186                 return INTEGER_PROMOTION_BADNESS;
2187             }
2188           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2189             return 0;
2190           else
2191             return INTEGER_CONVERSION_BADNESS;
2192         default:
2193           return INCOMPATIBLE_TYPE_BADNESS;
2194         }
2195       break;
2196     case TYPE_CODE_RANGE:
2197       switch (TYPE_CODE (arg))
2198         {
2199         case TYPE_CODE_INT:
2200         case TYPE_CODE_CHAR:
2201         case TYPE_CODE_RANGE:
2202         case TYPE_CODE_BOOL:
2203         case TYPE_CODE_ENUM:
2204           return INTEGER_CONVERSION_BADNESS;
2205         case TYPE_CODE_FLT:
2206           return INT_FLOAT_CONVERSION_BADNESS;
2207         default:
2208           return INCOMPATIBLE_TYPE_BADNESS;
2209         }
2210       break;
2211     case TYPE_CODE_BOOL:
2212       switch (TYPE_CODE (arg))
2213         {
2214         case TYPE_CODE_INT:
2215         case TYPE_CODE_CHAR:
2216         case TYPE_CODE_RANGE:
2217         case TYPE_CODE_ENUM:
2218         case TYPE_CODE_FLT:
2219         case TYPE_CODE_PTR:
2220           return BOOLEAN_CONVERSION_BADNESS;
2221         case TYPE_CODE_BOOL:
2222           return 0;
2223         default:
2224           return INCOMPATIBLE_TYPE_BADNESS;
2225         }
2226       break;
2227     case TYPE_CODE_FLT:
2228       switch (TYPE_CODE (arg))
2229         {
2230         case TYPE_CODE_FLT:
2231           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2232             return FLOAT_PROMOTION_BADNESS;
2233           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2234             return 0;
2235           else
2236             return FLOAT_CONVERSION_BADNESS;
2237         case TYPE_CODE_INT:
2238         case TYPE_CODE_BOOL:
2239         case TYPE_CODE_ENUM:
2240         case TYPE_CODE_RANGE:
2241         case TYPE_CODE_CHAR:
2242           return INT_FLOAT_CONVERSION_BADNESS;
2243         default:
2244           return INCOMPATIBLE_TYPE_BADNESS;
2245         }
2246       break;
2247     case TYPE_CODE_COMPLEX:
2248       switch (TYPE_CODE (arg))
2249         {               /* Strictly not needed for C++, but...  */
2250         case TYPE_CODE_FLT:
2251           return FLOAT_PROMOTION_BADNESS;
2252         case TYPE_CODE_COMPLEX:
2253           return 0;
2254         default:
2255           return INCOMPATIBLE_TYPE_BADNESS;
2256         }
2257       break;
2258     case TYPE_CODE_STRUCT:
2259       /* currently same as TYPE_CODE_CLASS */
2260       switch (TYPE_CODE (arg))
2261         {
2262         case TYPE_CODE_STRUCT:
2263           /* Check for derivation */
2264           if (is_ancestor (parm, arg))
2265             return BASE_CONVERSION_BADNESS;
2266           /* else fall through */
2267         default:
2268           return INCOMPATIBLE_TYPE_BADNESS;
2269         }
2270       break;
2271     case TYPE_CODE_UNION:
2272       switch (TYPE_CODE (arg))
2273         {
2274         case TYPE_CODE_UNION:
2275         default:
2276           return INCOMPATIBLE_TYPE_BADNESS;
2277         }
2278       break;
2279     case TYPE_CODE_MEMBERPTR:
2280       switch (TYPE_CODE (arg))
2281         {
2282         default:
2283           return INCOMPATIBLE_TYPE_BADNESS;
2284         }
2285       break;
2286     case TYPE_CODE_METHOD:
2287       switch (TYPE_CODE (arg))
2288         {
2289
2290         default:
2291           return INCOMPATIBLE_TYPE_BADNESS;
2292         }
2293       break;
2294     case TYPE_CODE_REF:
2295       switch (TYPE_CODE (arg))
2296         {
2297
2298         default:
2299           return INCOMPATIBLE_TYPE_BADNESS;
2300         }
2301
2302       break;
2303     case TYPE_CODE_SET:
2304       switch (TYPE_CODE (arg))
2305         {
2306           /* Not in C++ */
2307         case TYPE_CODE_SET:
2308           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
2309                                 TYPE_FIELD_TYPE (arg, 0));
2310         default:
2311           return INCOMPATIBLE_TYPE_BADNESS;
2312         }
2313       break;
2314     case TYPE_CODE_VOID:
2315     default:
2316       return INCOMPATIBLE_TYPE_BADNESS;
2317     }                           /* switch (TYPE_CODE (arg)) */
2318 }
2319
2320
2321 /* End of functions for overload resolution */
2322
2323 static void
2324 print_bit_vector (B_TYPE *bits, int nbits)
2325 {
2326   int bitno;
2327
2328   for (bitno = 0; bitno < nbits; bitno++)
2329     {
2330       if ((bitno % 8) == 0)
2331         {
2332           puts_filtered (" ");
2333         }
2334       if (B_TST (bits, bitno))
2335         printf_filtered (("1"));
2336       else
2337         printf_filtered (("0"));
2338     }
2339 }
2340
2341 /* Note the first arg should be the "this" pointer, we may not want to
2342    include it since we may get into a infinitely recursive
2343    situation.  */
2344
2345 static void
2346 print_arg_types (struct field *args, int nargs, int spaces)
2347 {
2348   if (args != NULL)
2349     {
2350       int i;
2351
2352       for (i = 0; i < nargs; i++)
2353         recursive_dump_type (args[i].type, spaces + 2);
2354     }
2355 }
2356
2357 int
2358 field_is_static (struct field *f)
2359 {
2360   /* "static" fields are the fields whose location is not relative
2361      to the address of the enclosing struct.  It would be nice to
2362      have a dedicated flag that would be set for static fields when
2363      the type is being created.  But in practice, checking the field
2364      loc_kind should give us an accurate answer (at least as long as
2365      we assume that DWARF block locations are not going to be used
2366      for static fields).  FIXME?  */
2367   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
2368           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
2369 }
2370
2371 static void
2372 dump_fn_fieldlists (struct type *type, int spaces)
2373 {
2374   int method_idx;
2375   int overload_idx;
2376   struct fn_field *f;
2377
2378   printfi_filtered (spaces, "fn_fieldlists ");
2379   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
2380   printf_filtered ("\n");
2381   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2382     {
2383       f = TYPE_FN_FIELDLIST1 (type, method_idx);
2384       printfi_filtered (spaces + 2, "[%d] name '%s' (",
2385                         method_idx,
2386                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
2387       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2388                               gdb_stdout);
2389       printf_filtered (_(") length %d\n"),
2390                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2391       for (overload_idx = 0;
2392            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2393            overload_idx++)
2394         {
2395           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2396                             overload_idx,
2397                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
2398           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2399                                   gdb_stdout);
2400           printf_filtered (")\n");
2401           printfi_filtered (spaces + 8, "type ");
2402           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
2403                                   gdb_stdout);
2404           printf_filtered ("\n");
2405
2406           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2407                                spaces + 8 + 2);
2408
2409           printfi_filtered (spaces + 8, "args ");
2410           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
2411                                   gdb_stdout);
2412           printf_filtered ("\n");
2413
2414           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
2415                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, 
2416                                                              overload_idx)),
2417                            spaces);
2418           printfi_filtered (spaces + 8, "fcontext ");
2419           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2420                                   gdb_stdout);
2421           printf_filtered ("\n");
2422
2423           printfi_filtered (spaces + 8, "is_const %d\n",
2424                             TYPE_FN_FIELD_CONST (f, overload_idx));
2425           printfi_filtered (spaces + 8, "is_volatile %d\n",
2426                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2427           printfi_filtered (spaces + 8, "is_private %d\n",
2428                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2429           printfi_filtered (spaces + 8, "is_protected %d\n",
2430                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2431           printfi_filtered (spaces + 8, "is_stub %d\n",
2432                             TYPE_FN_FIELD_STUB (f, overload_idx));
2433           printfi_filtered (spaces + 8, "voffset %u\n",
2434                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2435         }
2436     }
2437 }
2438
2439 static void
2440 print_cplus_stuff (struct type *type, int spaces)
2441 {
2442   printfi_filtered (spaces, "n_baseclasses %d\n",
2443                     TYPE_N_BASECLASSES (type));
2444   printfi_filtered (spaces, "nfn_fields %d\n",
2445                     TYPE_NFN_FIELDS (type));
2446   printfi_filtered (spaces, "nfn_fields_total %d\n",
2447                     TYPE_NFN_FIELDS_TOTAL (type));
2448   if (TYPE_N_BASECLASSES (type) > 0)
2449     {
2450       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2451                         TYPE_N_BASECLASSES (type));
2452       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
2453                               gdb_stdout);
2454       printf_filtered (")");
2455
2456       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2457                         TYPE_N_BASECLASSES (type));
2458       puts_filtered ("\n");
2459     }
2460   if (TYPE_NFIELDS (type) > 0)
2461     {
2462       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2463         {
2464           printfi_filtered (spaces, 
2465                             "private_field_bits (%d bits at *",
2466                             TYPE_NFIELDS (type));
2467           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
2468                                   gdb_stdout);
2469           printf_filtered (")");
2470           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2471                             TYPE_NFIELDS (type));
2472           puts_filtered ("\n");
2473         }
2474       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2475         {
2476           printfi_filtered (spaces, 
2477                             "protected_field_bits (%d bits at *",
2478                             TYPE_NFIELDS (type));
2479           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
2480                                   gdb_stdout);
2481           printf_filtered (")");
2482           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2483                             TYPE_NFIELDS (type));
2484           puts_filtered ("\n");
2485         }
2486     }
2487   if (TYPE_NFN_FIELDS (type) > 0)
2488     {
2489       dump_fn_fieldlists (type, spaces);
2490     }
2491 }
2492
2493 static struct obstack dont_print_type_obstack;
2494
2495 void
2496 recursive_dump_type (struct type *type, int spaces)
2497 {
2498   int idx;
2499
2500   if (spaces == 0)
2501     obstack_begin (&dont_print_type_obstack, 0);
2502
2503   if (TYPE_NFIELDS (type) > 0
2504       || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
2505     {
2506       struct type **first_dont_print
2507         = (struct type **) obstack_base (&dont_print_type_obstack);
2508
2509       int i = (struct type **) 
2510         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
2511
2512       while (--i >= 0)
2513         {
2514           if (type == first_dont_print[i])
2515             {
2516               printfi_filtered (spaces, "type node ");
2517               gdb_print_host_address (type, gdb_stdout);
2518               printf_filtered (_(" <same as already seen type>\n"));
2519               return;
2520             }
2521         }
2522
2523       obstack_ptr_grow (&dont_print_type_obstack, type);
2524     }
2525
2526   printfi_filtered (spaces, "type node ");
2527   gdb_print_host_address (type, gdb_stdout);
2528   printf_filtered ("\n");
2529   printfi_filtered (spaces, "name '%s' (",
2530                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
2531   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
2532   printf_filtered (")\n");
2533   printfi_filtered (spaces, "tagname '%s' (",
2534                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
2535   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2536   printf_filtered (")\n");
2537   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2538   switch (TYPE_CODE (type))
2539     {
2540     case TYPE_CODE_UNDEF:
2541       printf_filtered ("(TYPE_CODE_UNDEF)");
2542       break;
2543     case TYPE_CODE_PTR:
2544       printf_filtered ("(TYPE_CODE_PTR)");
2545       break;
2546     case TYPE_CODE_ARRAY:
2547       printf_filtered ("(TYPE_CODE_ARRAY)");
2548       break;
2549     case TYPE_CODE_STRUCT:
2550       printf_filtered ("(TYPE_CODE_STRUCT)");
2551       break;
2552     case TYPE_CODE_UNION:
2553       printf_filtered ("(TYPE_CODE_UNION)");
2554       break;
2555     case TYPE_CODE_ENUM:
2556       printf_filtered ("(TYPE_CODE_ENUM)");
2557       break;
2558     case TYPE_CODE_FLAGS:
2559       printf_filtered ("(TYPE_CODE_FLAGS)");
2560       break;
2561     case TYPE_CODE_FUNC:
2562       printf_filtered ("(TYPE_CODE_FUNC)");
2563       break;
2564     case TYPE_CODE_INT:
2565       printf_filtered ("(TYPE_CODE_INT)");
2566       break;
2567     case TYPE_CODE_FLT:
2568       printf_filtered ("(TYPE_CODE_FLT)");
2569       break;
2570     case TYPE_CODE_VOID:
2571       printf_filtered ("(TYPE_CODE_VOID)");
2572       break;
2573     case TYPE_CODE_SET:
2574       printf_filtered ("(TYPE_CODE_SET)");
2575       break;
2576     case TYPE_CODE_RANGE:
2577       printf_filtered ("(TYPE_CODE_RANGE)");
2578       break;
2579     case TYPE_CODE_STRING:
2580       printf_filtered ("(TYPE_CODE_STRING)");
2581       break;
2582     case TYPE_CODE_BITSTRING:
2583       printf_filtered ("(TYPE_CODE_BITSTRING)");
2584       break;
2585     case TYPE_CODE_ERROR:
2586       printf_filtered ("(TYPE_CODE_ERROR)");
2587       break;
2588     case TYPE_CODE_MEMBERPTR:
2589       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
2590       break;
2591     case TYPE_CODE_METHODPTR:
2592       printf_filtered ("(TYPE_CODE_METHODPTR)");
2593       break;
2594     case TYPE_CODE_METHOD:
2595       printf_filtered ("(TYPE_CODE_METHOD)");
2596       break;
2597     case TYPE_CODE_REF:
2598       printf_filtered ("(TYPE_CODE_REF)");
2599       break;
2600     case TYPE_CODE_CHAR:
2601       printf_filtered ("(TYPE_CODE_CHAR)");
2602       break;
2603     case TYPE_CODE_BOOL:
2604       printf_filtered ("(TYPE_CODE_BOOL)");
2605       break;
2606     case TYPE_CODE_COMPLEX:
2607       printf_filtered ("(TYPE_CODE_COMPLEX)");
2608       break;
2609     case TYPE_CODE_TYPEDEF:
2610       printf_filtered ("(TYPE_CODE_TYPEDEF)");
2611       break;
2612     case TYPE_CODE_TEMPLATE:
2613       printf_filtered ("(TYPE_CODE_TEMPLATE)");
2614       break;
2615     case TYPE_CODE_TEMPLATE_ARG:
2616       printf_filtered ("(TYPE_CODE_TEMPLATE_ARG)");
2617       break;
2618     case TYPE_CODE_NAMESPACE:
2619       printf_filtered ("(TYPE_CODE_NAMESPACE)");
2620       break;
2621     default:
2622       printf_filtered ("(UNKNOWN TYPE CODE)");
2623       break;
2624     }
2625   puts_filtered ("\n");
2626   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
2627   if (TYPE_OBJFILE_OWNED (type))
2628     {
2629       printfi_filtered (spaces, "objfile ");
2630       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
2631     }
2632   else
2633     {
2634       printfi_filtered (spaces, "gdbarch ");
2635       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
2636     }
2637   printf_filtered ("\n");
2638   printfi_filtered (spaces, "target_type ");
2639   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
2640   printf_filtered ("\n");
2641   if (TYPE_TARGET_TYPE (type) != NULL)
2642     {
2643       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
2644     }
2645   printfi_filtered (spaces, "pointer_type ");
2646   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
2647   printf_filtered ("\n");
2648   printfi_filtered (spaces, "reference_type ");
2649   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
2650   printf_filtered ("\n");
2651   printfi_filtered (spaces, "type_chain ");
2652   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
2653   printf_filtered ("\n");
2654   printfi_filtered (spaces, "instance_flags 0x%x", 
2655                     TYPE_INSTANCE_FLAGS (type));
2656   if (TYPE_CONST (type))
2657     {
2658       puts_filtered (" TYPE_FLAG_CONST");
2659     }
2660   if (TYPE_VOLATILE (type))
2661     {
2662       puts_filtered (" TYPE_FLAG_VOLATILE");
2663     }
2664   if (TYPE_CODE_SPACE (type))
2665     {
2666       puts_filtered (" TYPE_FLAG_CODE_SPACE");
2667     }
2668   if (TYPE_DATA_SPACE (type))
2669     {
2670       puts_filtered (" TYPE_FLAG_DATA_SPACE");
2671     }
2672   if (TYPE_ADDRESS_CLASS_1 (type))
2673     {
2674       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
2675     }
2676   if (TYPE_ADDRESS_CLASS_2 (type))
2677     {
2678       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
2679     }
2680   puts_filtered ("\n");
2681
2682   printfi_filtered (spaces, "flags");
2683   if (TYPE_UNSIGNED (type))
2684     {
2685       puts_filtered (" TYPE_FLAG_UNSIGNED");
2686     }
2687   if (TYPE_NOSIGN (type))
2688     {
2689       puts_filtered (" TYPE_FLAG_NOSIGN");
2690     }
2691   if (TYPE_STUB (type))
2692     {
2693       puts_filtered (" TYPE_FLAG_STUB");
2694     }
2695   if (TYPE_TARGET_STUB (type))
2696     {
2697       puts_filtered (" TYPE_FLAG_TARGET_STUB");
2698     }
2699   if (TYPE_STATIC (type))
2700     {
2701       puts_filtered (" TYPE_FLAG_STATIC");
2702     }
2703   if (TYPE_PROTOTYPED (type))
2704     {
2705       puts_filtered (" TYPE_FLAG_PROTOTYPED");
2706     }
2707   if (TYPE_INCOMPLETE (type))
2708     {
2709       puts_filtered (" TYPE_FLAG_INCOMPLETE");
2710     }
2711   if (TYPE_VARARGS (type))
2712     {
2713       puts_filtered (" TYPE_FLAG_VARARGS");
2714     }
2715   /* This is used for things like AltiVec registers on ppc.  Gcc emits
2716      an attribute for the array type, which tells whether or not we
2717      have a vector, instead of a regular array.  */
2718   if (TYPE_VECTOR (type))
2719     {
2720       puts_filtered (" TYPE_FLAG_VECTOR");
2721     }
2722   if (TYPE_FIXED_INSTANCE (type))
2723     {
2724       puts_filtered (" TYPE_FIXED_INSTANCE");
2725     }
2726   if (TYPE_STUB_SUPPORTED (type))
2727     {
2728       puts_filtered (" TYPE_STUB_SUPPORTED");
2729     }
2730   if (TYPE_NOTTEXT (type))
2731     {
2732       puts_filtered (" TYPE_NOTTEXT");
2733     }
2734   puts_filtered ("\n");
2735   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
2736   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
2737   puts_filtered ("\n");
2738   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
2739     {
2740       printfi_filtered (spaces + 2,
2741                         "[%d] bitpos %d bitsize %d type ",
2742                         idx, TYPE_FIELD_BITPOS (type, idx),
2743                         TYPE_FIELD_BITSIZE (type, idx));
2744       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
2745       printf_filtered (" name '%s' (",
2746                        TYPE_FIELD_NAME (type, idx) != NULL
2747                        ? TYPE_FIELD_NAME (type, idx)
2748                        : "<NULL>");
2749       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
2750       printf_filtered (")\n");
2751       if (TYPE_FIELD_TYPE (type, idx) != NULL)
2752         {
2753           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
2754         }
2755     }
2756   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
2757     {
2758       printfi_filtered (spaces, "low %s%s  high %s%s\n",
2759                         plongest (TYPE_LOW_BOUND (type)), 
2760                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
2761                         plongest (TYPE_HIGH_BOUND (type)),
2762                         TYPE_HIGH_BOUND_UNDEFINED (type) ? " (undefined)" : "");
2763     }
2764   printfi_filtered (spaces, "vptr_basetype ");
2765   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
2766   puts_filtered ("\n");
2767   if (TYPE_VPTR_BASETYPE (type) != NULL)
2768     {
2769       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
2770     }
2771   printfi_filtered (spaces, "vptr_fieldno %d\n", 
2772                     TYPE_VPTR_FIELDNO (type));
2773   switch (TYPE_CODE (type))
2774     {
2775     case TYPE_CODE_STRUCT:
2776       printfi_filtered (spaces, "cplus_stuff ");
2777       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
2778                               gdb_stdout);
2779       puts_filtered ("\n");
2780       print_cplus_stuff (type, spaces);
2781       break;
2782
2783     case TYPE_CODE_FLT:
2784       printfi_filtered (spaces, "floatformat ");
2785       if (TYPE_FLOATFORMAT (type) == NULL)
2786         puts_filtered ("(null)");
2787       else
2788         {
2789           puts_filtered ("{ ");
2790           if (TYPE_FLOATFORMAT (type)[0] == NULL
2791               || TYPE_FLOATFORMAT (type)[0]->name == NULL)
2792             puts_filtered ("(null)");
2793           else
2794             puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
2795
2796           puts_filtered (", ");
2797           if (TYPE_FLOATFORMAT (type)[1] == NULL
2798               || TYPE_FLOATFORMAT (type)[1]->name == NULL)
2799             puts_filtered ("(null)");
2800           else
2801             puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
2802
2803           puts_filtered (" }");
2804         }
2805       puts_filtered ("\n");
2806       break;
2807
2808     default:
2809       /* We have to pick one of the union types to be able print and
2810          test the value.  Pick cplus_struct_type, even though we know
2811          it isn't any particular one.  */
2812       printfi_filtered (spaces, "type_specific ");
2813       gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
2814       if (TYPE_CPLUS_SPECIFIC (type) != NULL)
2815         {
2816           printf_filtered (_(" (unknown data form)"));
2817         }
2818       printf_filtered ("\n");
2819       break;
2820
2821     }
2822   if (spaces == 0)
2823     obstack_free (&dont_print_type_obstack, NULL);
2824 }
2825
2826 /* Trivial helpers for the libiberty hash table, for mapping one
2827    type to another.  */
2828
2829 struct type_pair
2830 {
2831   struct type *old, *new;
2832 };
2833
2834 static hashval_t
2835 type_pair_hash (const void *item)
2836 {
2837   const struct type_pair *pair = item;
2838   return htab_hash_pointer (pair->old);
2839 }
2840
2841 static int
2842 type_pair_eq (const void *item_lhs, const void *item_rhs)
2843 {
2844   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
2845   return lhs->old == rhs->old;
2846 }
2847
2848 /* Allocate the hash table used by copy_type_recursive to walk
2849    types without duplicates.  We use OBJFILE's obstack, because
2850    OBJFILE is about to be deleted.  */
2851
2852 htab_t
2853 create_copied_types_hash (struct objfile *objfile)
2854 {
2855   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
2856                                NULL, &objfile->objfile_obstack,
2857                                hashtab_obstack_allocate,
2858                                dummy_obstack_deallocate);
2859 }
2860
2861 /* Recursively copy (deep copy) TYPE, if it is associated with
2862    OBJFILE.  Return a new type allocated using malloc, a saved type if
2863    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
2864    not associated with OBJFILE.  */
2865
2866 struct type *
2867 copy_type_recursive (struct objfile *objfile, 
2868                      struct type *type,
2869                      htab_t copied_types)
2870 {
2871   struct type_pair *stored, pair;
2872   void **slot;
2873   struct type *new_type;
2874
2875   if (! TYPE_OBJFILE_OWNED (type))
2876     return type;
2877
2878   /* This type shouldn't be pointing to any types in other objfiles;
2879      if it did, the type might disappear unexpectedly.  */
2880   gdb_assert (TYPE_OBJFILE (type) == objfile);
2881
2882   pair.old = type;
2883   slot = htab_find_slot (copied_types, &pair, INSERT);
2884   if (*slot != NULL)
2885     return ((struct type_pair *) *slot)->new;
2886
2887   new_type = alloc_type_arch (get_type_arch (type));
2888
2889   /* We must add the new type to the hash table immediately, in case
2890      we encounter this type again during a recursive call below.  */
2891   stored = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
2892   stored->old = type;
2893   stored->new = new_type;
2894   *slot = stored;
2895
2896   /* Copy the common fields of types.  For the main type, we simply
2897      copy the entire thing and then update specific fields as needed.  */
2898   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
2899   TYPE_OBJFILE_OWNED (new_type) = 0;
2900   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
2901
2902   if (TYPE_NAME (type))
2903     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
2904   if (TYPE_TAG_NAME (type))
2905     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
2906
2907   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
2908   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
2909
2910   /* Copy the fields.  */
2911   if (TYPE_NFIELDS (type))
2912     {
2913       int i, nfields;
2914
2915       nfields = TYPE_NFIELDS (type);
2916       TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field);
2917       for (i = 0; i < nfields; i++)
2918         {
2919           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
2920             TYPE_FIELD_ARTIFICIAL (type, i);
2921           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
2922           if (TYPE_FIELD_TYPE (type, i))
2923             TYPE_FIELD_TYPE (new_type, i)
2924               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
2925                                      copied_types);
2926           if (TYPE_FIELD_NAME (type, i))
2927             TYPE_FIELD_NAME (new_type, i) = 
2928               xstrdup (TYPE_FIELD_NAME (type, i));
2929           switch (TYPE_FIELD_LOC_KIND (type, i))
2930             {
2931             case FIELD_LOC_KIND_BITPOS:
2932               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
2933                                 TYPE_FIELD_BITPOS (type, i));
2934               break;
2935             case FIELD_LOC_KIND_PHYSADDR:
2936               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
2937                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
2938               break;
2939             case FIELD_LOC_KIND_PHYSNAME:
2940               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
2941                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
2942                                                                        i)));
2943               break;
2944             default:
2945               internal_error (__FILE__, __LINE__,
2946                               _("Unexpected type field location kind: %d"),
2947                               TYPE_FIELD_LOC_KIND (type, i));
2948             }
2949         }
2950     }
2951
2952   /* For range types, copy the bounds information. */
2953   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
2954     {
2955       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
2956       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
2957     }
2958
2959   /* Copy pointers to other types.  */
2960   if (TYPE_TARGET_TYPE (type))
2961     TYPE_TARGET_TYPE (new_type) = 
2962       copy_type_recursive (objfile, 
2963                            TYPE_TARGET_TYPE (type),
2964                            copied_types);
2965   if (TYPE_VPTR_BASETYPE (type))
2966     TYPE_VPTR_BASETYPE (new_type) = 
2967       copy_type_recursive (objfile,
2968                            TYPE_VPTR_BASETYPE (type),
2969                            copied_types);
2970   /* Maybe copy the type_specific bits.
2971
2972      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
2973      base classes and methods.  There's no fundamental reason why we
2974      can't, but at the moment it is not needed.  */
2975
2976   if (TYPE_CODE (type) == TYPE_CODE_FLT)
2977     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
2978   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2979            || TYPE_CODE (type) == TYPE_CODE_UNION
2980            || TYPE_CODE (type) == TYPE_CODE_TEMPLATE
2981            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
2982     INIT_CPLUS_SPECIFIC (new_type);
2983
2984   return new_type;
2985 }
2986
2987 /* Make a copy of the given TYPE, except that the pointer & reference
2988    types are not preserved.
2989    
2990    This function assumes that the given type has an associated objfile.
2991    This objfile is used to allocate the new type.  */
2992
2993 struct type *
2994 copy_type (const struct type *type)
2995 {
2996   struct type *new_type;
2997
2998   gdb_assert (TYPE_OBJFILE_OWNED (type));
2999
3000   new_type = alloc_type_copy (type);
3001   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3002   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3003   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
3004           sizeof (struct main_type));
3005
3006   return new_type;
3007 }
3008
3009
3010 /* Helper functions to initialize architecture-specific types.  */
3011
3012 /* Allocate a type structure associated with GDBARCH and set its
3013    CODE, LENGTH, and NAME fields.  */
3014 struct type *
3015 arch_type (struct gdbarch *gdbarch,
3016            enum type_code code, int length, char *name)
3017 {
3018   struct type *type;
3019
3020   type = alloc_type_arch (gdbarch);
3021   TYPE_CODE (type) = code;
3022   TYPE_LENGTH (type) = length;
3023
3024   if (name)
3025     TYPE_NAME (type) = xstrdup (name);
3026
3027   return type;
3028 }
3029
3030 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
3031    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3032    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3033 struct type *
3034 arch_integer_type (struct gdbarch *gdbarch,
3035                    int bit, int unsigned_p, char *name)
3036 {
3037   struct type *t;
3038
3039   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
3040   if (unsigned_p)
3041     TYPE_UNSIGNED (t) = 1;
3042   if (name && strcmp (name, "char") == 0)
3043     TYPE_NOSIGN (t) = 1;
3044
3045   return t;
3046 }
3047
3048 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
3049    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3050    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3051 struct type *
3052 arch_character_type (struct gdbarch *gdbarch,
3053                      int bit, int unsigned_p, char *name)
3054 {
3055   struct type *t;
3056
3057   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
3058   if (unsigned_p)
3059     TYPE_UNSIGNED (t) = 1;
3060
3061   return t;
3062 }
3063
3064 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
3065    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3066    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3067 struct type *
3068 arch_boolean_type (struct gdbarch *gdbarch,
3069                    int bit, int unsigned_p, char *name)
3070 {
3071   struct type *t;
3072
3073   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
3074   if (unsigned_p)
3075     TYPE_UNSIGNED (t) = 1;
3076
3077   return t;
3078 }
3079
3080 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
3081    BIT is the type size in bits; if BIT equals -1, the size is
3082    determined by the floatformat.  NAME is the type name.  Set the
3083    TYPE_FLOATFORMAT from FLOATFORMATS.  */
3084 struct type *
3085 arch_float_type (struct gdbarch *gdbarch,
3086                  int bit, char *name, const struct floatformat **floatformats)
3087 {
3088   struct type *t;
3089
3090   if (bit == -1)
3091     {
3092       gdb_assert (floatformats != NULL);
3093       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3094       bit = floatformats[0]->totalsize;
3095     }
3096   gdb_assert (bit >= 0);
3097
3098   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
3099   TYPE_FLOATFORMAT (t) = floatformats;
3100   return t;
3101 }
3102
3103 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
3104    NAME is the type name.  TARGET_TYPE is the component float type.  */
3105 struct type *
3106 arch_complex_type (struct gdbarch *gdbarch,
3107                    char *name, struct type *target_type)
3108 {
3109   struct type *t;
3110   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
3111                  2 * TYPE_LENGTH (target_type), name);
3112   TYPE_TARGET_TYPE (t) = target_type;
3113   return t;
3114 }
3115
3116 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
3117    NAME is the type name.  LENGTH is the number of flag bits.  */
3118 struct type *
3119 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
3120 {
3121   int nfields = length * TARGET_CHAR_BIT;
3122   struct type *type;
3123
3124   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
3125   TYPE_UNSIGNED (type) = 1;
3126   TYPE_NFIELDS (type) = nfields;
3127   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
3128
3129   return type;
3130 }
3131
3132 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
3133    position BITPOS is called NAME.  */
3134 void
3135 append_flags_type_flag (struct type *type, int bitpos, char *name)
3136 {
3137   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
3138   gdb_assert (bitpos < TYPE_NFIELDS (type));
3139   gdb_assert (bitpos >= 0);
3140
3141   if (name)
3142     {
3143       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
3144       TYPE_FIELD_BITPOS (type, bitpos) = bitpos;
3145     }
3146   else
3147     {
3148       /* Don't show this field to the user.  */
3149       TYPE_FIELD_BITPOS (type, bitpos) = -1;
3150     }
3151 }
3152
3153 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
3154    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
3155 struct type *
3156 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
3157 {
3158   struct type *t;
3159   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
3160   t = arch_type (gdbarch, code, 0, NULL);
3161   TYPE_TAG_NAME (t) = name;
3162   INIT_CPLUS_SPECIFIC (t);
3163   return t;
3164 }
3165
3166 /* Add new field with name NAME and type FIELD to composite type T.
3167    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
3168 void
3169 append_composite_type_field_aligned (struct type *t, char *name,
3170                                      struct type *field, int alignment)
3171 {
3172   struct field *f;
3173   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
3174   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
3175                               sizeof (struct field) * TYPE_NFIELDS (t));
3176   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
3177   memset (f, 0, sizeof f[0]);
3178   FIELD_TYPE (f[0]) = field;
3179   FIELD_NAME (f[0]) = name;
3180   if (TYPE_CODE (t) == TYPE_CODE_UNION)
3181     {
3182       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
3183         TYPE_LENGTH (t) = TYPE_LENGTH (field);
3184     }
3185   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
3186     {
3187       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
3188       if (TYPE_NFIELDS (t) > 1)
3189         {
3190           FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1])
3191                                  + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
3192                                     * TARGET_CHAR_BIT));
3193
3194           if (alignment)
3195             {
3196               int left = FIELD_BITPOS (f[0]) % (alignment * TARGET_CHAR_BIT);
3197               if (left)
3198                 {
3199                   FIELD_BITPOS (f[0]) += left;
3200                   TYPE_LENGTH (t) += left / TARGET_CHAR_BIT;
3201                 }
3202             }
3203         }
3204     }
3205 }
3206
3207 /* Add new field with name NAME and type FIELD to composite type T.  */
3208 void
3209 append_composite_type_field (struct type *t, char *name,
3210                              struct type *field)
3211 {
3212   append_composite_type_field_aligned (t, name, field, 0);
3213 }
3214
3215
3216 static struct gdbarch_data *gdbtypes_data;
3217
3218 const struct builtin_type *
3219 builtin_type (struct gdbarch *gdbarch)
3220 {
3221   return gdbarch_data (gdbarch, gdbtypes_data);
3222 }
3223
3224 static void *
3225 gdbtypes_post_init (struct gdbarch *gdbarch)
3226 {
3227   struct builtin_type *builtin_type
3228     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
3229
3230   /* Basic types.  */
3231   builtin_type->builtin_void
3232     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
3233   builtin_type->builtin_char
3234     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3235                          !gdbarch_char_signed (gdbarch), "char");
3236   builtin_type->builtin_signed_char
3237     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3238                          0, "signed char");
3239   builtin_type->builtin_unsigned_char
3240     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3241                          1, "unsigned char");
3242   builtin_type->builtin_short
3243     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
3244                          0, "short");
3245   builtin_type->builtin_unsigned_short
3246     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
3247                          1, "unsigned short");
3248   builtin_type->builtin_int
3249     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
3250                          0, "int");
3251   builtin_type->builtin_unsigned_int
3252     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
3253                          1, "unsigned int");
3254   builtin_type->builtin_long
3255     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
3256                          0, "long");
3257   builtin_type->builtin_unsigned_long
3258     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
3259                          1, "unsigned long");
3260   builtin_type->builtin_long_long
3261     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
3262                          0, "long long");
3263   builtin_type->builtin_unsigned_long_long
3264     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
3265                          1, "unsigned long long");
3266   builtin_type->builtin_float
3267     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
3268                        "float", gdbarch_float_format (gdbarch));
3269   builtin_type->builtin_double
3270     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
3271                        "double", gdbarch_double_format (gdbarch));
3272   builtin_type->builtin_long_double
3273     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
3274                        "long double", gdbarch_long_double_format (gdbarch));
3275   builtin_type->builtin_complex
3276     = arch_complex_type (gdbarch, "complex",
3277                          builtin_type->builtin_float);
3278   builtin_type->builtin_double_complex
3279     = arch_complex_type (gdbarch, "double complex",
3280                          builtin_type->builtin_double);
3281   builtin_type->builtin_string
3282     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
3283   builtin_type->builtin_bool
3284     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
3285
3286   /* The following three are about decimal floating point types, which
3287      are 32-bits, 64-bits and 128-bits respectively.  */
3288   builtin_type->builtin_decfloat
3289     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
3290   builtin_type->builtin_decdouble
3291     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
3292   builtin_type->builtin_declong
3293     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
3294
3295   /* "True" character types.  */
3296   builtin_type->builtin_true_char
3297     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
3298   builtin_type->builtin_true_unsigned_char
3299     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
3300
3301   /* Fixed-size integer types.  */
3302   builtin_type->builtin_int0
3303     = arch_integer_type (gdbarch, 0, 0, "int0_t");
3304   builtin_type->builtin_int8
3305     = arch_integer_type (gdbarch, 8, 0, "int8_t");
3306   builtin_type->builtin_uint8
3307     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
3308   builtin_type->builtin_int16
3309     = arch_integer_type (gdbarch, 16, 0, "int16_t");
3310   builtin_type->builtin_uint16
3311     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
3312   builtin_type->builtin_int32
3313     = arch_integer_type (gdbarch, 32, 0, "int32_t");
3314   builtin_type->builtin_uint32
3315     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
3316   builtin_type->builtin_int64
3317     = arch_integer_type (gdbarch, 64, 0, "int64_t");
3318   builtin_type->builtin_uint64
3319     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
3320   builtin_type->builtin_int128
3321     = arch_integer_type (gdbarch, 128, 0, "int128_t");
3322   builtin_type->builtin_uint128
3323     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
3324   TYPE_NOTTEXT (builtin_type->builtin_int8) = 1;
3325   TYPE_NOTTEXT (builtin_type->builtin_uint8) = 1;
3326
3327   /* Default data/code pointer types.  */
3328   builtin_type->builtin_data_ptr
3329     = lookup_pointer_type (builtin_type->builtin_void);
3330   builtin_type->builtin_func_ptr
3331     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
3332
3333   /* This type represents a GDB internal function.  */
3334   builtin_type->internal_fn
3335     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
3336                  "<internal function>");
3337
3338   return builtin_type;
3339 }
3340
3341
3342 /* This set of objfile-based types is intended to be used by symbol
3343    readers as basic types.  */
3344
3345 static const struct objfile_data *objfile_type_data;
3346
3347 const struct objfile_type *
3348 objfile_type (struct objfile *objfile)
3349 {
3350   struct gdbarch *gdbarch;
3351   struct objfile_type *objfile_type
3352     = objfile_data (objfile, objfile_type_data);
3353
3354   if (objfile_type)
3355     return objfile_type;
3356
3357   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
3358                                  1, struct objfile_type);
3359
3360   /* Use the objfile architecture to determine basic type properties.  */
3361   gdbarch = get_objfile_arch (objfile);
3362
3363   /* Basic types.  */
3364   objfile_type->builtin_void
3365     = init_type (TYPE_CODE_VOID, 1,
3366                  0,
3367                  "void", objfile);
3368
3369   objfile_type->builtin_char
3370     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3371                  (TYPE_FLAG_NOSIGN
3372                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
3373                  "char", objfile);
3374   objfile_type->builtin_signed_char
3375     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3376                  0,
3377                  "signed char", objfile);
3378   objfile_type->builtin_unsigned_char
3379     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3380                  TYPE_FLAG_UNSIGNED,
3381                  "unsigned char", objfile);
3382   objfile_type->builtin_short
3383     = init_type (TYPE_CODE_INT,
3384                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
3385                  0, "short", objfile);
3386   objfile_type->builtin_unsigned_short
3387     = init_type (TYPE_CODE_INT,
3388                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
3389                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
3390   objfile_type->builtin_int
3391     = init_type (TYPE_CODE_INT,
3392                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
3393                  0, "int", objfile);
3394   objfile_type->builtin_unsigned_int
3395     = init_type (TYPE_CODE_INT,
3396                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
3397                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
3398   objfile_type->builtin_long
3399     = init_type (TYPE_CODE_INT,
3400                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
3401                  0, "long", objfile);
3402   objfile_type->builtin_unsigned_long
3403     = init_type (TYPE_CODE_INT,
3404                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
3405                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
3406   objfile_type->builtin_long_long
3407     = init_type (TYPE_CODE_INT,
3408                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
3409                  0, "long long", objfile);
3410   objfile_type->builtin_unsigned_long_long
3411     = init_type (TYPE_CODE_INT,
3412                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
3413                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
3414
3415   objfile_type->builtin_float
3416     = init_type (TYPE_CODE_FLT,
3417                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
3418                  0, "float", objfile);
3419   TYPE_FLOATFORMAT (objfile_type->builtin_float)
3420     = gdbarch_float_format (gdbarch);
3421   objfile_type->builtin_double
3422     = init_type (TYPE_CODE_FLT,
3423                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
3424                  0, "double", objfile);
3425   TYPE_FLOATFORMAT (objfile_type->builtin_double)
3426     = gdbarch_double_format (gdbarch);
3427   objfile_type->builtin_long_double
3428     = init_type (TYPE_CODE_FLT,
3429                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
3430                  0, "long double", objfile);
3431   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
3432     = gdbarch_long_double_format (gdbarch);
3433
3434   /* This type represents a type that was unrecognized in symbol read-in.  */
3435   objfile_type->builtin_error
3436     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
3437
3438   /* The following set of types is used for symbols with no
3439      debug information.  */
3440   objfile_type->nodebug_text_symbol
3441     = init_type (TYPE_CODE_FUNC, 1, 0,
3442                  "<text variable, no debug info>", objfile);
3443   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
3444     = objfile_type->builtin_int;
3445   objfile_type->nodebug_data_symbol
3446     = init_type (TYPE_CODE_INT,
3447                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3448                  "<data variable, no debug info>", objfile);
3449   objfile_type->nodebug_unknown_symbol
3450     = init_type (TYPE_CODE_INT, 1, 0,
3451                  "<variable (not text or data), no debug info>", objfile);
3452   objfile_type->nodebug_tls_symbol
3453     = init_type (TYPE_CODE_INT,
3454                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3455                  "<thread local variable, no debug info>", objfile);
3456
3457   /* NOTE: on some targets, addresses and pointers are not necessarily
3458      the same --- for example, on the D10V, pointers are 16 bits long,
3459      but addresses are 32 bits long.  See doc/gdbint.texinfo,
3460      ``Pointers Are Not Always Addresses''.
3461
3462      The upshot is:
3463      - gdb's `struct type' always describes the target's
3464        representation.
3465      - gdb's `struct value' objects should always hold values in
3466        target form.
3467      - gdb's CORE_ADDR values are addresses in the unified virtual
3468        address space that the assembler and linker work with.  Thus,
3469        since target_read_memory takes a CORE_ADDR as an argument, it
3470        can access any memory on the target, even if the processor has
3471        separate code and data address spaces.
3472
3473      So, for example:
3474      - If v is a value holding a D10V code pointer, its contents are
3475        in target form: a big-endian address left-shifted two bits.
3476      - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3477        sizeof (void *) == 2 on the target.
3478
3479      In this context, objfile_type->builtin_core_addr is a bit odd:
3480      it's a target type for a value the target will never see.  It's
3481      only used to hold the values of (typeless) linker symbols, which
3482      are indeed in the unified virtual address space.  */
3483
3484   objfile_type->builtin_core_addr
3485     = init_type (TYPE_CODE_INT,
3486                  gdbarch_addr_bit (gdbarch) / 8,
3487                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
3488
3489   set_objfile_data (objfile, objfile_type_data, objfile_type);
3490   return objfile_type;
3491 }
3492
3493
3494 extern void _initialize_gdbtypes (void);
3495 void
3496 _initialize_gdbtypes (void)
3497 {
3498   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
3499   objfile_type_data = register_objfile_data ();
3500
3501   add_setshow_zinteger_cmd ("overload", no_class, &overload_debug, _("\
3502 Set debugging of C++ overloading."), _("\
3503 Show debugging of C++ overloading."), _("\
3504 When enabled, ranking of the functions is displayed."),
3505                             NULL,
3506                             show_overload_debug,
3507                             &setdebuglist, &showdebuglist);
3508
3509   /* Add user knob for controlling resolution of opaque types.  */
3510   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3511                            &opaque_type_resolution, _("\
3512 Set resolution of opaque struct/class/union types (if set before loading symbols)."), _("\
3513 Show resolution of opaque struct/class/union types (if set before loading symbols)."), NULL,
3514                            NULL,
3515                            show_opaque_type_resolution,
3516                            &setlist, &showlist);
3517 }