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