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