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