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