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