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