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