Revert the entire VLA series.
[platform/upstream/binutils.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3    Copyright (C) 1992-2014 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Support, using pieces from other GDB modules.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include <string.h>
24 #include "bfd.h"
25 #include "symtab.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "language.h"
31 #include "target.h"
32 #include "value.h"
33 #include "demangle.h"
34 #include "complaints.h"
35 #include "gdbcmd.h"
36 #include "cp-abi.h"
37 #include "gdb_assert.h"
38 #include "hashtab.h"
39 #include "exceptions.h"
40 #include "cp-support.h"
41 #include "bcache.h"
42 #include "dwarf2loc.h"
43
44 /* Initialize BADNESS constants.  */
45
46 const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
47
48 const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
49 const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
50
51 const struct rank EXACT_MATCH_BADNESS = {0,0};
52
53 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
54 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
55 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
56 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
57 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
58 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
59 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
60 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
61 const struct rank BASE_CONVERSION_BADNESS = {2,0};
62 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
63 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
64 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
65 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
66
67 /* Floatformat pairs.  */
68 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
69   &floatformat_ieee_half_big,
70   &floatformat_ieee_half_little
71 };
72 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
73   &floatformat_ieee_single_big,
74   &floatformat_ieee_single_little
75 };
76 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
77   &floatformat_ieee_double_big,
78   &floatformat_ieee_double_little
79 };
80 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
81   &floatformat_ieee_double_big,
82   &floatformat_ieee_double_littlebyte_bigword
83 };
84 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
85   &floatformat_i387_ext,
86   &floatformat_i387_ext
87 };
88 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
89   &floatformat_m68881_ext,
90   &floatformat_m68881_ext
91 };
92 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
93   &floatformat_arm_ext_big,
94   &floatformat_arm_ext_littlebyte_bigword
95 };
96 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
97   &floatformat_ia64_spill_big,
98   &floatformat_ia64_spill_little
99 };
100 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
101   &floatformat_ia64_quad_big,
102   &floatformat_ia64_quad_little
103 };
104 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
105   &floatformat_vax_f,
106   &floatformat_vax_f
107 };
108 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
109   &floatformat_vax_d,
110   &floatformat_vax_d
111 };
112 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
113   &floatformat_ibm_long_double_big,
114   &floatformat_ibm_long_double_little
115 };
116
117 /* Should opaque types be resolved?  */
118
119 static int opaque_type_resolution = 1;
120
121 /* A flag to enable printing of debugging information of C++
122    overloading.  */
123
124 unsigned int overload_debug = 0;
125
126 /* A flag to enable strict type checking.  */
127
128 static int strict_type_checking = 1;
129
130 /* A function to show whether opaque types are resolved.  */
131
132 static void
133 show_opaque_type_resolution (struct ui_file *file, int from_tty,
134                              struct cmd_list_element *c, 
135                              const char *value)
136 {
137   fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
138                             "(if set before loading symbols) is %s.\n"),
139                     value);
140 }
141
142 /* A function to show whether C++ overload debugging is enabled.  */
143
144 static void
145 show_overload_debug (struct ui_file *file, int from_tty,
146                      struct cmd_list_element *c, const char *value)
147 {
148   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), 
149                     value);
150 }
151
152 /* A function to show the status of strict type checking.  */
153
154 static void
155 show_strict_type_checking (struct ui_file *file, int from_tty,
156                            struct cmd_list_element *c, const char *value)
157 {
158   fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
159 }
160
161 \f
162 /* Allocate a new OBJFILE-associated type structure and fill it
163    with some defaults.  Space for the type structure is allocated
164    on the objfile's objfile_obstack.  */
165
166 struct type *
167 alloc_type (struct objfile *objfile)
168 {
169   struct type *type;
170
171   gdb_assert (objfile != NULL);
172
173   /* Alloc the structure and start off with all fields zeroed.  */
174   type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
175   TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
176                                           struct main_type);
177   OBJSTAT (objfile, n_types++);
178
179   TYPE_OBJFILE_OWNED (type) = 1;
180   TYPE_OWNER (type).objfile = objfile;
181
182   /* Initialize the fields that might not be zero.  */
183
184   TYPE_CODE (type) = TYPE_CODE_UNDEF;
185   TYPE_VPTR_FIELDNO (type) = -1;
186   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
187
188   return type;
189 }
190
191 /* Allocate a new GDBARCH-associated type structure and fill it
192    with some defaults.  Space for the type structure is allocated
193    on the heap.  */
194
195 struct type *
196 alloc_type_arch (struct gdbarch *gdbarch)
197 {
198   struct type *type;
199
200   gdb_assert (gdbarch != NULL);
201
202   /* Alloc the structure and start off with all fields zeroed.  */
203
204   type = XCNEW (struct type);
205   TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
206
207   TYPE_OBJFILE_OWNED (type) = 0;
208   TYPE_OWNER (type).gdbarch = gdbarch;
209
210   /* Initialize the fields that might not be zero.  */
211
212   TYPE_CODE (type) = TYPE_CODE_UNDEF;
213   TYPE_VPTR_FIELDNO (type) = -1;
214   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
215
216   return type;
217 }
218
219 /* If TYPE is objfile-associated, allocate a new type structure
220    associated with the same objfile.  If TYPE is gdbarch-associated,
221    allocate a new type structure associated with the same gdbarch.  */
222
223 struct type *
224 alloc_type_copy (const struct type *type)
225 {
226   if (TYPE_OBJFILE_OWNED (type))
227     return alloc_type (TYPE_OWNER (type).objfile);
228   else
229     return alloc_type_arch (TYPE_OWNER (type).gdbarch);
230 }
231
232 /* If TYPE is gdbarch-associated, return that architecture.
233    If TYPE is objfile-associated, return that objfile's architecture.  */
234
235 struct gdbarch *
236 get_type_arch (const struct type *type)
237 {
238   if (TYPE_OBJFILE_OWNED (type))
239     return get_objfile_arch (TYPE_OWNER (type).objfile);
240   else
241     return TYPE_OWNER (type).gdbarch;
242 }
243
244 /* See gdbtypes.h.  */
245
246 struct type *
247 get_target_type (struct type *type)
248 {
249   if (type != NULL)
250     {
251       type = TYPE_TARGET_TYPE (type);
252       if (type != NULL)
253         type = check_typedef (type);
254     }
255
256   return type;
257 }
258
259 /* Alloc a new type instance structure, fill it with some defaults,
260    and point it at OLDTYPE.  Allocate the new type instance from the
261    same place as OLDTYPE.  */
262
263 static struct type *
264 alloc_type_instance (struct type *oldtype)
265 {
266   struct type *type;
267
268   /* Allocate the structure.  */
269
270   if (! TYPE_OBJFILE_OWNED (oldtype))
271     type = XCNEW (struct type);
272   else
273     type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
274                            struct type);
275
276   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
277
278   TYPE_CHAIN (type) = type;     /* Chain back to itself for now.  */
279
280   return type;
281 }
282
283 /* Clear all remnants of the previous type at TYPE, in preparation for
284    replacing it with something else.  Preserve owner information.  */
285
286 static void
287 smash_type (struct type *type)
288 {
289   int objfile_owned = TYPE_OBJFILE_OWNED (type);
290   union type_owner owner = TYPE_OWNER (type);
291
292   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
293
294   /* Restore owner information.  */
295   TYPE_OBJFILE_OWNED (type) = objfile_owned;
296   TYPE_OWNER (type) = owner;
297
298   /* For now, delete the rings.  */
299   TYPE_CHAIN (type) = type;
300
301   /* For now, leave the pointer/reference types alone.  */
302 }
303
304 /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
305    to a pointer to memory where the pointer type should be stored.
306    If *TYPEPTR is zero, update it to point to the pointer type we return.
307    We allocate new memory if needed.  */
308
309 struct type *
310 make_pointer_type (struct type *type, struct type **typeptr)
311 {
312   struct type *ntype;   /* New type */
313   struct type *chain;
314
315   ntype = TYPE_POINTER_TYPE (type);
316
317   if (ntype)
318     {
319       if (typeptr == 0)
320         return ntype;           /* Don't care about alloc, 
321                                    and have new type.  */
322       else if (*typeptr == 0)
323         {
324           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
325           return ntype;
326         }
327     }
328
329   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
330     {
331       ntype = alloc_type_copy (type);
332       if (typeptr)
333         *typeptr = ntype;
334     }
335   else                  /* We have storage, but need to reset it.  */
336     {
337       ntype = *typeptr;
338       chain = TYPE_CHAIN (ntype);
339       smash_type (ntype);
340       TYPE_CHAIN (ntype) = chain;
341     }
342
343   TYPE_TARGET_TYPE (ntype) = type;
344   TYPE_POINTER_TYPE (type) = ntype;
345
346   /* FIXME!  Assumes the machine has only one representation for pointers!  */
347
348   TYPE_LENGTH (ntype)
349     = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
350   TYPE_CODE (ntype) = TYPE_CODE_PTR;
351
352   /* Mark pointers as unsigned.  The target converts between pointers
353      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
354      gdbarch_address_to_pointer.  */
355   TYPE_UNSIGNED (ntype) = 1;
356
357   /* Update the length of all the other variants of this type.  */
358   chain = TYPE_CHAIN (ntype);
359   while (chain != ntype)
360     {
361       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
362       chain = TYPE_CHAIN (chain);
363     }
364
365   return ntype;
366 }
367
368 /* Given a type TYPE, return a type of pointers to that type.
369    May need to construct such a type if this is the first use.  */
370
371 struct type *
372 lookup_pointer_type (struct type *type)
373 {
374   return make_pointer_type (type, (struct type **) 0);
375 }
376
377 /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero,
378    points to a pointer to memory where the reference type should be
379    stored.  If *TYPEPTR is zero, update it to point to the reference
380    type we return.  We allocate new memory if needed.  */
381
382 struct type *
383 make_reference_type (struct type *type, struct type **typeptr)
384 {
385   struct type *ntype;   /* New type */
386   struct type *chain;
387
388   ntype = TYPE_REFERENCE_TYPE (type);
389
390   if (ntype)
391     {
392       if (typeptr == 0)
393         return ntype;           /* Don't care about alloc, 
394                                    and have new type.  */
395       else if (*typeptr == 0)
396         {
397           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
398           return ntype;
399         }
400     }
401
402   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
403     {
404       ntype = alloc_type_copy (type);
405       if (typeptr)
406         *typeptr = ntype;
407     }
408   else                  /* We have storage, but need to reset it.  */
409     {
410       ntype = *typeptr;
411       chain = TYPE_CHAIN (ntype);
412       smash_type (ntype);
413       TYPE_CHAIN (ntype) = chain;
414     }
415
416   TYPE_TARGET_TYPE (ntype) = type;
417   TYPE_REFERENCE_TYPE (type) = ntype;
418
419   /* FIXME!  Assume the machine has only one representation for
420      references, and that it matches the (only) representation for
421      pointers!  */
422
423   TYPE_LENGTH (ntype) =
424     gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
425   TYPE_CODE (ntype) = TYPE_CODE_REF;
426
427   if (!TYPE_REFERENCE_TYPE (type))      /* Remember it, if don't have one.  */
428     TYPE_REFERENCE_TYPE (type) = ntype;
429
430   /* Update the length of all the other variants of this type.  */
431   chain = TYPE_CHAIN (ntype);
432   while (chain != ntype)
433     {
434       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
435       chain = TYPE_CHAIN (chain);
436     }
437
438   return ntype;
439 }
440
441 /* Same as above, but caller doesn't care about memory allocation
442    details.  */
443
444 struct type *
445 lookup_reference_type (struct type *type)
446 {
447   return make_reference_type (type, (struct type **) 0);
448 }
449
450 /* Lookup a function type that returns type TYPE.  TYPEPTR, if
451    nonzero, points to a pointer to memory where the function type
452    should be stored.  If *TYPEPTR is zero, update it to point to the
453    function type we return.  We allocate new memory if needed.  */
454
455 struct type *
456 make_function_type (struct type *type, struct type **typeptr)
457 {
458   struct type *ntype;   /* New type */
459
460   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
461     {
462       ntype = alloc_type_copy (type);
463       if (typeptr)
464         *typeptr = ntype;
465     }
466   else                  /* We have storage, but need to reset it.  */
467     {
468       ntype = *typeptr;
469       smash_type (ntype);
470     }
471
472   TYPE_TARGET_TYPE (ntype) = type;
473
474   TYPE_LENGTH (ntype) = 1;
475   TYPE_CODE (ntype) = TYPE_CODE_FUNC;
476
477   INIT_FUNC_SPECIFIC (ntype);
478
479   return ntype;
480 }
481
482 /* Given a type TYPE, return a type of functions that return that type.
483    May need to construct such a type if this is the first use.  */
484
485 struct type *
486 lookup_function_type (struct type *type)
487 {
488   return make_function_type (type, (struct type **) 0);
489 }
490
491 /* Given a type TYPE and argument types, return the appropriate
492    function type.  If the final type in PARAM_TYPES is NULL, make a
493    varargs function.  */
494
495 struct type *
496 lookup_function_type_with_arguments (struct type *type,
497                                      int nparams,
498                                      struct type **param_types)
499 {
500   struct type *fn = make_function_type (type, (struct type **) 0);
501   int i;
502
503   if (nparams > 0)
504     {
505       if (param_types[nparams - 1] == NULL)
506         {
507           --nparams;
508           TYPE_VARARGS (fn) = 1;
509         }
510       else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
511                == TYPE_CODE_VOID)
512         {
513           --nparams;
514           /* Caller should have ensured this.  */
515           gdb_assert (nparams == 0);
516           TYPE_PROTOTYPED (fn) = 1;
517         }
518     }
519
520   TYPE_NFIELDS (fn) = nparams;
521   TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
522   for (i = 0; i < nparams; ++i)
523     TYPE_FIELD_TYPE (fn, i) = param_types[i];
524
525   return fn;
526 }
527
528 /* Identify address space identifier by name --
529    return the integer flag defined in gdbtypes.h.  */
530
531 int
532 address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
533 {
534   int type_flags;
535
536   /* Check for known address space delimiters.  */
537   if (!strcmp (space_identifier, "code"))
538     return TYPE_INSTANCE_FLAG_CODE_SPACE;
539   else if (!strcmp (space_identifier, "data"))
540     return TYPE_INSTANCE_FLAG_DATA_SPACE;
541   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
542            && gdbarch_address_class_name_to_type_flags (gdbarch,
543                                                         space_identifier,
544                                                         &type_flags))
545     return type_flags;
546   else
547     error (_("Unknown address space specifier: \"%s\""), space_identifier);
548 }
549
550 /* Identify address space identifier by integer flag as defined in 
551    gdbtypes.h -- return the string version of the adress space name.  */
552
553 const char *
554 address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
555 {
556   if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
557     return "code";
558   else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
559     return "data";
560   else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
561            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
562     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
563   else
564     return NULL;
565 }
566
567 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
568
569    If STORAGE is non-NULL, create the new type instance there.
570    STORAGE must be in the same obstack as TYPE.  */
571
572 static struct type *
573 make_qualified_type (struct type *type, int new_flags,
574                      struct type *storage)
575 {
576   struct type *ntype;
577
578   ntype = type;
579   do
580     {
581       if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
582         return ntype;
583       ntype = TYPE_CHAIN (ntype);
584     }
585   while (ntype != type);
586
587   /* Create a new type instance.  */
588   if (storage == NULL)
589     ntype = alloc_type_instance (type);
590   else
591     {
592       /* If STORAGE was provided, it had better be in the same objfile
593          as TYPE.  Otherwise, we can't link it into TYPE's cv chain:
594          if one objfile is freed and the other kept, we'd have
595          dangling pointers.  */
596       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
597
598       ntype = storage;
599       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
600       TYPE_CHAIN (ntype) = ntype;
601     }
602
603   /* Pointers or references to the original type are not relevant to
604      the new type.  */
605   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
606   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
607
608   /* Chain the new qualified type to the old type.  */
609   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
610   TYPE_CHAIN (type) = ntype;
611
612   /* Now set the instance flags and return the new type.  */
613   TYPE_INSTANCE_FLAGS (ntype) = new_flags;
614
615   /* Set length of new type to that of the original type.  */
616   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
617
618   return ntype;
619 }
620
621 /* Make an address-space-delimited variant of a type -- a type that
622    is identical to the one supplied except that it has an address
623    space attribute attached to it (such as "code" or "data").
624
625    The space attributes "code" and "data" are for Harvard
626    architectures.  The address space attributes are for architectures
627    which have alternately sized pointers or pointers with alternate
628    representations.  */
629
630 struct type *
631 make_type_with_address_space (struct type *type, int space_flag)
632 {
633   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
634                     & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
635                         | TYPE_INSTANCE_FLAG_DATA_SPACE
636                         | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
637                    | space_flag);
638
639   return make_qualified_type (type, new_flags, NULL);
640 }
641
642 /* Make a "c-v" variant of a type -- a type that is identical to the
643    one supplied except that it may have const or volatile attributes
644    CNST is a flag for setting the const attribute
645    VOLTL is a flag for setting the volatile attribute
646    TYPE is the base type whose variant we are creating.
647
648    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
649    storage to hold the new qualified type; *TYPEPTR and TYPE must be
650    in the same objfile.  Otherwise, allocate fresh memory for the new
651    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
652    new type we construct.  */
653
654 struct type *
655 make_cv_type (int cnst, int voltl, 
656               struct type *type, 
657               struct type **typeptr)
658 {
659   struct type *ntype;   /* New type */
660
661   int new_flags = (TYPE_INSTANCE_FLAGS (type)
662                    & ~(TYPE_INSTANCE_FLAG_CONST 
663                        | TYPE_INSTANCE_FLAG_VOLATILE));
664
665   if (cnst)
666     new_flags |= TYPE_INSTANCE_FLAG_CONST;
667
668   if (voltl)
669     new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
670
671   if (typeptr && *typeptr != NULL)
672     {
673       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
674          a C-V variant chain that threads across objfiles: if one
675          objfile gets freed, then the other has a broken C-V chain.
676
677          This code used to try to copy over the main type from TYPE to
678          *TYPEPTR if they were in different objfiles, but that's
679          wrong, too: TYPE may have a field list or member function
680          lists, which refer to types of their own, etc. etc.  The
681          whole shebang would need to be copied over recursively; you
682          can't have inter-objfile pointers.  The only thing to do is
683          to leave stub types as stub types, and look them up afresh by
684          name each time you encounter them.  */
685       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
686     }
687   
688   ntype = make_qualified_type (type, new_flags, 
689                                typeptr ? *typeptr : NULL);
690
691   if (typeptr != NULL)
692     *typeptr = ntype;
693
694   return ntype;
695 }
696
697 /* Make a 'restrict'-qualified version of TYPE.  */
698
699 struct type *
700 make_restrict_type (struct type *type)
701 {
702   return make_qualified_type (type,
703                               (TYPE_INSTANCE_FLAGS (type)
704                                | TYPE_INSTANCE_FLAG_RESTRICT),
705                               NULL);
706 }
707
708 /* Replace the contents of ntype with the type *type.  This changes the
709    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
710    the changes are propogated to all types in the TYPE_CHAIN.
711
712    In order to build recursive types, it's inevitable that we'll need
713    to update types in place --- but this sort of indiscriminate
714    smashing is ugly, and needs to be replaced with something more
715    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
716    clear if more steps are needed.  */
717
718 void
719 replace_type (struct type *ntype, struct type *type)
720 {
721   struct type *chain;
722
723   /* These two types had better be in the same objfile.  Otherwise,
724      the assignment of one type's main type structure to the other
725      will produce a type with references to objects (names; field
726      lists; etc.) allocated on an objfile other than its own.  */
727   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
728
729   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
730
731   /* The type length is not a part of the main type.  Update it for
732      each type on the variant chain.  */
733   chain = ntype;
734   do
735     {
736       /* Assert that this element of the chain has no address-class bits
737          set in its flags.  Such type variants might have type lengths
738          which are supposed to be different from the non-address-class
739          variants.  This assertion shouldn't ever be triggered because
740          symbol readers which do construct address-class variants don't
741          call replace_type().  */
742       gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
743
744       TYPE_LENGTH (chain) = TYPE_LENGTH (type);
745       chain = TYPE_CHAIN (chain);
746     }
747   while (ntype != chain);
748
749   /* Assert that the two types have equivalent instance qualifiers.
750      This should be true for at least all of our debug readers.  */
751   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
752 }
753
754 /* Implement direct support for MEMBER_TYPE in GNU C++.
755    May need to construct such a type if this is the first use.
756    The TYPE is the type of the member.  The DOMAIN is the type
757    of the aggregate that the member belongs to.  */
758
759 struct type *
760 lookup_memberptr_type (struct type *type, struct type *domain)
761 {
762   struct type *mtype;
763
764   mtype = alloc_type_copy (type);
765   smash_to_memberptr_type (mtype, domain, type);
766   return mtype;
767 }
768
769 /* Return a pointer-to-method type, for a method of type TO_TYPE.  */
770
771 struct type *
772 lookup_methodptr_type (struct type *to_type)
773 {
774   struct type *mtype;
775
776   mtype = alloc_type_copy (to_type);
777   smash_to_methodptr_type (mtype, to_type);
778   return mtype;
779 }
780
781 /* Allocate a stub method whose return type is TYPE.  This apparently
782    happens for speed of symbol reading, since parsing out the
783    arguments to the method is cpu-intensive, the way we are doing it.
784    So, we will fill in arguments later.  This always returns a fresh
785    type.  */
786
787 struct type *
788 allocate_stub_method (struct type *type)
789 {
790   struct type *mtype;
791
792   mtype = alloc_type_copy (type);
793   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
794   TYPE_LENGTH (mtype) = 1;
795   TYPE_STUB (mtype) = 1;
796   TYPE_TARGET_TYPE (mtype) = type;
797   /*  _DOMAIN_TYPE (mtype) = unknown yet */
798   return mtype;
799 }
800
801 /* Create a range type with a dynamic range from LOW_BOUND to
802    HIGH_BOUND, inclusive.  See create_range_type for further details. */
803
804 struct type *
805 create_range_type (struct type *result_type, struct type *index_type,
806                    const struct dynamic_prop *low_bound,
807                    const struct dynamic_prop *high_bound)
808 {
809   if (result_type == NULL)
810     result_type = alloc_type_copy (index_type);
811   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
812   TYPE_TARGET_TYPE (result_type) = index_type;
813   if (TYPE_STUB (index_type))
814     TYPE_TARGET_STUB (result_type) = 1;
815   else
816     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
817
818   TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
819     TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
820   TYPE_RANGE_DATA (result_type)->low = *low_bound;
821   TYPE_RANGE_DATA (result_type)->high = *high_bound;
822
823   if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
824     TYPE_UNSIGNED (result_type) = 1;
825
826   return result_type;
827 }
828
829 /* Create a range type using either a blank type supplied in
830    RESULT_TYPE, or creating a new type, inheriting the objfile from
831    INDEX_TYPE.
832
833    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
834    to HIGH_BOUND, inclusive.
835
836    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
837    sure it is TYPE_CODE_UNDEF before we bash it into a range type?  */
838
839 struct type *
840 create_static_range_type (struct type *result_type, struct type *index_type,
841                           LONGEST low_bound, LONGEST high_bound)
842 {
843   struct dynamic_prop low, high;
844
845   low.kind = PROP_CONST;
846   low.data.const_val = low_bound;
847
848   high.kind = PROP_CONST;
849   high.data.const_val = high_bound;
850
851   result_type = create_range_type (result_type, index_type, &low, &high);
852
853   return result_type;
854 }
855
856 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
857    TYPE.  Return 1 if type is a range type, 0 if it is discrete (and
858    bounds will fit in LONGEST), or -1 otherwise.  */
859
860 int
861 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
862 {
863   CHECK_TYPEDEF (type);
864   switch (TYPE_CODE (type))
865     {
866     case TYPE_CODE_RANGE:
867       *lowp = TYPE_LOW_BOUND (type);
868       *highp = TYPE_HIGH_BOUND (type);
869       return 1;
870     case TYPE_CODE_ENUM:
871       if (TYPE_NFIELDS (type) > 0)
872         {
873           /* The enums may not be sorted by value, so search all
874              entries.  */
875           int i;
876
877           *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
878           for (i = 0; i < TYPE_NFIELDS (type); i++)
879             {
880               if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
881                 *lowp = TYPE_FIELD_ENUMVAL (type, i);
882               if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
883                 *highp = TYPE_FIELD_ENUMVAL (type, i);
884             }
885
886           /* Set unsigned indicator if warranted.  */
887           if (*lowp >= 0)
888             {
889               TYPE_UNSIGNED (type) = 1;
890             }
891         }
892       else
893         {
894           *lowp = 0;
895           *highp = -1;
896         }
897       return 0;
898     case TYPE_CODE_BOOL:
899       *lowp = 0;
900       *highp = 1;
901       return 0;
902     case TYPE_CODE_INT:
903       if (TYPE_LENGTH (type) > sizeof (LONGEST))        /* Too big */
904         return -1;
905       if (!TYPE_UNSIGNED (type))
906         {
907           *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
908           *highp = -*lowp - 1;
909           return 0;
910         }
911       /* ... fall through for unsigned ints ...  */
912     case TYPE_CODE_CHAR:
913       *lowp = 0;
914       /* This round-about calculation is to avoid shifting by
915          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
916          if TYPE_LENGTH (type) == sizeof (LONGEST).  */
917       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
918       *highp = (*highp - 1) | *highp;
919       return 0;
920     default:
921       return -1;
922     }
923 }
924
925 /* Assuming TYPE is a simple, non-empty array type, compute its upper
926    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
927    Save the high bound into HIGH_BOUND if not NULL.
928
929    Return 1 if the operation was successful.  Return zero otherwise,
930    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
931
932    We now simply use get_discrete_bounds call to get the values
933    of the low and high bounds.
934    get_discrete_bounds can return three values:
935    1, meaning that index is a range,
936    0, meaning that index is a discrete type,
937    or -1 for failure.  */
938
939 int
940 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
941 {
942   struct type *index = TYPE_INDEX_TYPE (type);
943   LONGEST low = 0;
944   LONGEST high = 0;
945   int res;
946
947   if (index == NULL)
948     return 0;
949
950   res = get_discrete_bounds (index, &low, &high);
951   if (res == -1)
952     return 0;
953
954   /* Check if the array bounds are undefined.  */
955   if (res == 1
956       && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
957           || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
958     return 0;
959
960   if (low_bound)
961     *low_bound = low;
962
963   if (high_bound)
964     *high_bound = high;
965
966   return 1;
967 }
968
969 /* Create an array type using either a blank type supplied in
970    RESULT_TYPE, or creating a new type, inheriting the objfile from
971    RANGE_TYPE.
972
973    Elements will be of type ELEMENT_TYPE, the indices will be of type
974    RANGE_TYPE.
975
976    If BIT_STRIDE is not zero, build a packed array type whose element
977    size is BIT_STRIDE.  Otherwise, ignore this parameter.
978
979    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
980    sure it is TYPE_CODE_UNDEF before we bash it into an array
981    type?  */
982
983 struct type *
984 create_array_type_with_stride (struct type *result_type,
985                                struct type *element_type,
986                                struct type *range_type,
987                                unsigned int bit_stride)
988 {
989   LONGEST low_bound, high_bound;
990
991   if (result_type == NULL)
992     result_type = alloc_type_copy (range_type);
993
994   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
995   TYPE_TARGET_TYPE (result_type) = element_type;
996   if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
997     low_bound = high_bound = 0;
998   CHECK_TYPEDEF (element_type);
999   /* Be careful when setting the array length.  Ada arrays can be
1000      empty arrays with the high_bound being smaller than the low_bound.
1001      In such cases, the array length should be zero.  */
1002   if (high_bound < low_bound)
1003     TYPE_LENGTH (result_type) = 0;
1004   else if (bit_stride > 0)
1005     TYPE_LENGTH (result_type) =
1006       (bit_stride * (high_bound - low_bound + 1) + 7) / 8;
1007   else
1008     TYPE_LENGTH (result_type) =
1009       TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
1010   TYPE_NFIELDS (result_type) = 1;
1011   TYPE_FIELDS (result_type) =
1012     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
1013   TYPE_INDEX_TYPE (result_type) = range_type;
1014   TYPE_VPTR_FIELDNO (result_type) = -1;
1015   if (bit_stride > 0)
1016     TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
1017
1018   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
1019   if (TYPE_LENGTH (result_type) == 0)
1020     TYPE_TARGET_STUB (result_type) = 1;
1021
1022   return result_type;
1023 }
1024
1025 /* Same as create_array_type_with_stride but with no bit_stride
1026    (BIT_STRIDE = 0), thus building an unpacked array.  */
1027
1028 struct type *
1029 create_array_type (struct type *result_type,
1030                    struct type *element_type,
1031                    struct type *range_type)
1032 {
1033   return create_array_type_with_stride (result_type, element_type,
1034                                         range_type, 0);
1035 }
1036
1037 struct type *
1038 lookup_array_range_type (struct type *element_type,
1039                          LONGEST low_bound, LONGEST high_bound)
1040 {
1041   struct gdbarch *gdbarch = get_type_arch (element_type);
1042   struct type *index_type = builtin_type (gdbarch)->builtin_int;
1043   struct type *range_type
1044     = create_static_range_type (NULL, index_type, low_bound, high_bound);
1045
1046   return create_array_type (NULL, element_type, range_type);
1047 }
1048
1049 /* Create a string type using either a blank type supplied in
1050    RESULT_TYPE, or creating a new type.  String types are similar
1051    enough to array of char types that we can use create_array_type to
1052    build the basic type and then bash it into a string type.
1053
1054    For fixed length strings, the range type contains 0 as the lower
1055    bound and the length of the string minus one as the upper bound.
1056
1057    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1058    sure it is TYPE_CODE_UNDEF before we bash it into a string
1059    type?  */
1060
1061 struct type *
1062 create_string_type (struct type *result_type,
1063                     struct type *string_char_type,
1064                     struct type *range_type)
1065 {
1066   result_type = create_array_type (result_type,
1067                                    string_char_type,
1068                                    range_type);
1069   TYPE_CODE (result_type) = TYPE_CODE_STRING;
1070   return result_type;
1071 }
1072
1073 struct type *
1074 lookup_string_range_type (struct type *string_char_type,
1075                           LONGEST low_bound, LONGEST high_bound)
1076 {
1077   struct type *result_type;
1078
1079   result_type = lookup_array_range_type (string_char_type,
1080                                          low_bound, high_bound);
1081   TYPE_CODE (result_type) = TYPE_CODE_STRING;
1082   return result_type;
1083 }
1084
1085 struct type *
1086 create_set_type (struct type *result_type, struct type *domain_type)
1087 {
1088   if (result_type == NULL)
1089     result_type = alloc_type_copy (domain_type);
1090
1091   TYPE_CODE (result_type) = TYPE_CODE_SET;
1092   TYPE_NFIELDS (result_type) = 1;
1093   TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
1094
1095   if (!TYPE_STUB (domain_type))
1096     {
1097       LONGEST low_bound, high_bound, bit_length;
1098
1099       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
1100         low_bound = high_bound = 0;
1101       bit_length = high_bound - low_bound + 1;
1102       TYPE_LENGTH (result_type)
1103         = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1104       if (low_bound >= 0)
1105         TYPE_UNSIGNED (result_type) = 1;
1106     }
1107   TYPE_FIELD_TYPE (result_type, 0) = domain_type;
1108
1109   return result_type;
1110 }
1111
1112 /* Convert ARRAY_TYPE to a vector type.  This may modify ARRAY_TYPE
1113    and any array types nested inside it.  */
1114
1115 void
1116 make_vector_type (struct type *array_type)
1117 {
1118   struct type *inner_array, *elt_type;
1119   int flags;
1120
1121   /* Find the innermost array type, in case the array is
1122      multi-dimensional.  */
1123   inner_array = array_type;
1124   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
1125     inner_array = TYPE_TARGET_TYPE (inner_array);
1126
1127   elt_type = TYPE_TARGET_TYPE (inner_array);
1128   if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
1129     {
1130       flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
1131       elt_type = make_qualified_type (elt_type, flags, NULL);
1132       TYPE_TARGET_TYPE (inner_array) = elt_type;
1133     }
1134
1135   TYPE_VECTOR (array_type) = 1;
1136 }
1137
1138 struct type *
1139 init_vector_type (struct type *elt_type, int n)
1140 {
1141   struct type *array_type;
1142
1143   array_type = lookup_array_range_type (elt_type, 0, n - 1);
1144   make_vector_type (array_type);
1145   return array_type;
1146 }
1147
1148 /* Smash TYPE to be a type of pointers to members of DOMAIN with type
1149    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
1150    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
1151    TYPE doesn't include the offset (that's the value of the MEMBER
1152    itself), but does include the structure type into which it points
1153    (for some reason).
1154
1155    When "smashing" the type, we preserve the objfile that the old type
1156    pointed to, since we aren't changing where the type is actually
1157    allocated.  */
1158
1159 void
1160 smash_to_memberptr_type (struct type *type, struct type *domain,
1161                          struct type *to_type)
1162 {
1163   smash_type (type);
1164   TYPE_TARGET_TYPE (type) = to_type;
1165   TYPE_DOMAIN_TYPE (type) = domain;
1166   /* Assume that a data member pointer is the same size as a normal
1167      pointer.  */
1168   TYPE_LENGTH (type)
1169     = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
1170   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
1171 }
1172
1173 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1174
1175    When "smashing" the type, we preserve the objfile that the old type
1176    pointed to, since we aren't changing where the type is actually
1177    allocated.  */
1178
1179 void
1180 smash_to_methodptr_type (struct type *type, struct type *to_type)
1181 {
1182   smash_type (type);
1183   TYPE_TARGET_TYPE (type) = to_type;
1184   TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1185   TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1186   TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1187 }
1188
1189 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
1190    METHOD just means `function that gets an extra "this" argument'.
1191
1192    When "smashing" the type, we preserve the objfile that the old type
1193    pointed to, since we aren't changing where the type is actually
1194    allocated.  */
1195
1196 void
1197 smash_to_method_type (struct type *type, struct type *domain,
1198                       struct type *to_type, struct field *args,
1199                       int nargs, int varargs)
1200 {
1201   smash_type (type);
1202   TYPE_TARGET_TYPE (type) = to_type;
1203   TYPE_DOMAIN_TYPE (type) = domain;
1204   TYPE_FIELDS (type) = args;
1205   TYPE_NFIELDS (type) = nargs;
1206   if (varargs)
1207     TYPE_VARARGS (type) = 1;
1208   TYPE_LENGTH (type) = 1;       /* In practice, this is never needed.  */
1209   TYPE_CODE (type) = TYPE_CODE_METHOD;
1210 }
1211
1212 /* Return a typename for a struct/union/enum type without "struct ",
1213    "union ", or "enum ".  If the type has a NULL name, return NULL.  */
1214
1215 const char *
1216 type_name_no_tag (const struct type *type)
1217 {
1218   if (TYPE_TAG_NAME (type) != NULL)
1219     return TYPE_TAG_NAME (type);
1220
1221   /* Is there code which expects this to return the name if there is
1222      no tag name?  My guess is that this is mainly used for C++ in
1223      cases where the two will always be the same.  */
1224   return TYPE_NAME (type);
1225 }
1226
1227 /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1228    Since GCC PR debug/47510 DWARF provides associated information to detect the
1229    anonymous class linkage name from its typedef.
1230
1231    Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1232    apply it itself.  */
1233
1234 const char *
1235 type_name_no_tag_or_error (struct type *type)
1236 {
1237   struct type *saved_type = type;
1238   const char *name;
1239   struct objfile *objfile;
1240
1241   CHECK_TYPEDEF (type);
1242
1243   name = type_name_no_tag (type);
1244   if (name != NULL)
1245     return name;
1246
1247   name = type_name_no_tag (saved_type);
1248   objfile = TYPE_OBJFILE (saved_type);
1249   error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1250          name ? name : "<anonymous>",
1251          objfile ? objfile_name (objfile) : "<arch>");
1252 }
1253
1254 /* Lookup a typedef or primitive type named NAME, visible in lexical
1255    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
1256    suitably defined.  */
1257
1258 struct type *
1259 lookup_typename (const struct language_defn *language,
1260                  struct gdbarch *gdbarch, const char *name,
1261                  const struct block *block, int noerr)
1262 {
1263   struct symbol *sym;
1264   struct type *type;
1265
1266   sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
1267   if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1268     return SYMBOL_TYPE (sym);
1269
1270   type = language_lookup_primitive_type_by_name (language, gdbarch, name);
1271   if (type)
1272     return type;
1273
1274   if (noerr)
1275     return NULL;
1276   error (_("No type named %s."), name);
1277 }
1278
1279 struct type *
1280 lookup_unsigned_typename (const struct language_defn *language,
1281                           struct gdbarch *gdbarch, const char *name)
1282 {
1283   char *uns = alloca (strlen (name) + 10);
1284
1285   strcpy (uns, "unsigned ");
1286   strcpy (uns + 9, name);
1287   return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
1288 }
1289
1290 struct type *
1291 lookup_signed_typename (const struct language_defn *language,
1292                         struct gdbarch *gdbarch, const char *name)
1293 {
1294   struct type *t;
1295   char *uns = alloca (strlen (name) + 8);
1296
1297   strcpy (uns, "signed ");
1298   strcpy (uns + 7, name);
1299   t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
1300   /* If we don't find "signed FOO" just try again with plain "FOO".  */
1301   if (t != NULL)
1302     return t;
1303   return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
1304 }
1305
1306 /* Lookup a structure type named "struct NAME",
1307    visible in lexical block BLOCK.  */
1308
1309 struct type *
1310 lookup_struct (const char *name, const struct block *block)
1311 {
1312   struct symbol *sym;
1313
1314   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1315
1316   if (sym == NULL)
1317     {
1318       error (_("No struct type named %s."), name);
1319     }
1320   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1321     {
1322       error (_("This context has class, union or enum %s, not a struct."),
1323              name);
1324     }
1325   return (SYMBOL_TYPE (sym));
1326 }
1327
1328 /* Lookup a union type named "union NAME",
1329    visible in lexical block BLOCK.  */
1330
1331 struct type *
1332 lookup_union (const char *name, const struct block *block)
1333 {
1334   struct symbol *sym;
1335   struct type *t;
1336
1337   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1338
1339   if (sym == NULL)
1340     error (_("No union type named %s."), name);
1341
1342   t = SYMBOL_TYPE (sym);
1343
1344   if (TYPE_CODE (t) == TYPE_CODE_UNION)
1345     return t;
1346
1347   /* If we get here, it's not a union.  */
1348   error (_("This context has class, struct or enum %s, not a union."), 
1349          name);
1350 }
1351
1352 /* Lookup an enum type named "enum NAME",
1353    visible in lexical block BLOCK.  */
1354
1355 struct type *
1356 lookup_enum (const char *name, const struct block *block)
1357 {
1358   struct symbol *sym;
1359
1360   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1361   if (sym == NULL)
1362     {
1363       error (_("No enum type named %s."), name);
1364     }
1365   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1366     {
1367       error (_("This context has class, struct or union %s, not an enum."), 
1368              name);
1369     }
1370   return (SYMBOL_TYPE (sym));
1371 }
1372
1373 /* Lookup a template type named "template NAME<TYPE>",
1374    visible in lexical block BLOCK.  */
1375
1376 struct type *
1377 lookup_template_type (char *name, struct type *type, 
1378                       const struct block *block)
1379 {
1380   struct symbol *sym;
1381   char *nam = (char *) 
1382     alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1383
1384   strcpy (nam, name);
1385   strcat (nam, "<");
1386   strcat (nam, TYPE_NAME (type));
1387   strcat (nam, " >");   /* FIXME, extra space still introduced in gcc?  */
1388
1389   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
1390
1391   if (sym == NULL)
1392     {
1393       error (_("No template type named %s."), name);
1394     }
1395   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1396     {
1397       error (_("This context has class, union or enum %s, not a struct."),
1398              name);
1399     }
1400   return (SYMBOL_TYPE (sym));
1401 }
1402
1403 /* Given a type TYPE, lookup the type of the component of type named
1404    NAME.
1405
1406    TYPE can be either a struct or union, or a pointer or reference to
1407    a struct or union.  If it is a pointer or reference, its target
1408    type is automatically used.  Thus '.' and '->' are interchangable,
1409    as specified for the definitions of the expression element types
1410    STRUCTOP_STRUCT and STRUCTOP_PTR.
1411
1412    If NOERR is nonzero, return zero if NAME is not suitably defined.
1413    If NAME is the name of a baseclass type, return that type.  */
1414
1415 struct type *
1416 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1417 {
1418   int i;
1419   char *typename;
1420
1421   for (;;)
1422     {
1423       CHECK_TYPEDEF (type);
1424       if (TYPE_CODE (type) != TYPE_CODE_PTR
1425           && TYPE_CODE (type) != TYPE_CODE_REF)
1426         break;
1427       type = TYPE_TARGET_TYPE (type);
1428     }
1429
1430   if (TYPE_CODE (type) != TYPE_CODE_STRUCT 
1431       && TYPE_CODE (type) != TYPE_CODE_UNION)
1432     {
1433       typename = type_to_string (type);
1434       make_cleanup (xfree, typename);
1435       error (_("Type %s is not a structure or union type."), typename);
1436     }
1437
1438 #if 0
1439   /* FIXME: This change put in by Michael seems incorrect for the case
1440      where the structure tag name is the same as the member name.
1441      I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
1442      foo; } bell;" Disabled by fnf.  */
1443   {
1444     char *typename;
1445
1446     typename = type_name_no_tag (type);
1447     if (typename != NULL && strcmp (typename, name) == 0)
1448       return type;
1449   }
1450 #endif
1451
1452   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1453     {
1454       const char *t_field_name = TYPE_FIELD_NAME (type, i);
1455
1456       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1457         {
1458           return TYPE_FIELD_TYPE (type, i);
1459         }
1460      else if (!t_field_name || *t_field_name == '\0')
1461         {
1462           struct type *subtype 
1463             = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1464
1465           if (subtype != NULL)
1466             return subtype;
1467         }
1468     }
1469
1470   /* OK, it's not in this class.  Recursively check the baseclasses.  */
1471   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1472     {
1473       struct type *t;
1474
1475       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
1476       if (t != NULL)
1477         {
1478           return t;
1479         }
1480     }
1481
1482   if (noerr)
1483     {
1484       return NULL;
1485     }
1486
1487   typename = type_to_string (type);
1488   make_cleanup (xfree, typename);
1489   error (_("Type %s has no component named %s."), typename, name);
1490 }
1491
1492 /* Store in *MAX the largest number representable by unsigned integer type
1493    TYPE.  */
1494
1495 void
1496 get_unsigned_type_max (struct type *type, ULONGEST *max)
1497 {
1498   unsigned int n;
1499
1500   CHECK_TYPEDEF (type);
1501   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
1502   gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
1503
1504   /* Written this way to avoid overflow.  */
1505   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1506   *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1507 }
1508
1509 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1510    signed integer type TYPE.  */
1511
1512 void
1513 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1514 {
1515   unsigned int n;
1516
1517   CHECK_TYPEDEF (type);
1518   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
1519   gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
1520
1521   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1522   *min = -((ULONGEST) 1 << (n - 1));
1523   *max = ((ULONGEST) 1 << (n - 1)) - 1;
1524 }
1525
1526 /* Lookup the vptr basetype/fieldno values for TYPE.
1527    If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1528    vptr_fieldno.  Also, if found and basetype is from the same objfile,
1529    cache the results.
1530    If not found, return -1 and ignore BASETYPEP.
1531    Callers should be aware that in some cases (for example,
1532    the type or one of its baseclasses is a stub type and we are
1533    debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1534    this function will not be able to find the
1535    virtual function table pointer, and vptr_fieldno will remain -1 and
1536    vptr_basetype will remain NULL or incomplete.  */
1537
1538 int
1539 get_vptr_fieldno (struct type *type, struct type **basetypep)
1540 {
1541   CHECK_TYPEDEF (type);
1542
1543   if (TYPE_VPTR_FIELDNO (type) < 0)
1544     {
1545       int i;
1546
1547       /* We must start at zero in case the first (and only) baseclass
1548          is virtual (and hence we cannot share the table pointer).  */
1549       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1550         {
1551           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1552           int fieldno;
1553           struct type *basetype;
1554
1555           fieldno = get_vptr_fieldno (baseclass, &basetype);
1556           if (fieldno >= 0)
1557             {
1558               /* If the type comes from a different objfile we can't cache
1559                  it, it may have a different lifetime.  PR 2384 */
1560               if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
1561                 {
1562                   TYPE_VPTR_FIELDNO (type) = fieldno;
1563                   TYPE_VPTR_BASETYPE (type) = basetype;
1564                 }
1565               if (basetypep)
1566                 *basetypep = basetype;
1567               return fieldno;
1568             }
1569         }
1570
1571       /* Not found.  */
1572       return -1;
1573     }
1574   else
1575     {
1576       if (basetypep)
1577         *basetypep = TYPE_VPTR_BASETYPE (type);
1578       return TYPE_VPTR_FIELDNO (type);
1579     }
1580 }
1581
1582 static void
1583 stub_noname_complaint (void)
1584 {
1585   complaint (&symfile_complaints, _("stub type has NULL name"));
1586 }
1587
1588 /* Find the real type of TYPE.  This function returns the real type,
1589    after removing all layers of typedefs, and completing opaque or stub
1590    types.  Completion changes the TYPE argument, but stripping of
1591    typedefs does not.
1592
1593    Instance flags (e.g. const/volatile) are preserved as typedefs are
1594    stripped.  If necessary a new qualified form of the underlying type
1595    is created.
1596
1597    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1598    not been computed and we're either in the middle of reading symbols, or
1599    there was no name for the typedef in the debug info.
1600
1601    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1602    QUITs in the symbol reading code can also throw.
1603    Thus this function can throw an exception.
1604
1605    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1606    the target type.
1607
1608    If this is a stubbed struct (i.e. declared as struct foo *), see if
1609    we can find a full definition in some other file.  If so, copy this
1610    definition, so we can use it in future.  There used to be a comment
1611    (but not any code) that if we don't find a full definition, we'd
1612    set a flag so we don't spend time in the future checking the same
1613    type.  That would be a mistake, though--we might load in more
1614    symbols which contain a full definition for the type.  */
1615
1616 struct type *
1617 check_typedef (struct type *type)
1618 {
1619   struct type *orig_type = type;
1620   /* While we're removing typedefs, we don't want to lose qualifiers.
1621      E.g., const/volatile.  */
1622   int instance_flags = TYPE_INSTANCE_FLAGS (type);
1623
1624   gdb_assert (type);
1625
1626   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1627     {
1628       if (!TYPE_TARGET_TYPE (type))
1629         {
1630           const char *name;
1631           struct symbol *sym;
1632
1633           /* It is dangerous to call lookup_symbol if we are currently
1634              reading a symtab.  Infinite recursion is one danger.  */
1635           if (currently_reading_symtab)
1636             return make_qualified_type (type, instance_flags, NULL);
1637
1638           name = type_name_no_tag (type);
1639           /* FIXME: shouldn't we separately check the TYPE_NAME and
1640              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1641              VAR_DOMAIN as appropriate?  (this code was written before
1642              TYPE_NAME and TYPE_TAG_NAME were separate).  */
1643           if (name == NULL)
1644             {
1645               stub_noname_complaint ();
1646               return make_qualified_type (type, instance_flags, NULL);
1647             }
1648           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1649           if (sym)
1650             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1651           else                                  /* TYPE_CODE_UNDEF */
1652             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
1653         }
1654       type = TYPE_TARGET_TYPE (type);
1655
1656       /* Preserve the instance flags as we traverse down the typedef chain.
1657
1658          Handling address spaces/classes is nasty, what do we do if there's a
1659          conflict?
1660          E.g., what if an outer typedef marks the type as class_1 and an inner
1661          typedef marks the type as class_2?
1662          This is the wrong place to do such error checking.  We leave it to
1663          the code that created the typedef in the first place to flag the
1664          error.  We just pick the outer address space (akin to letting the
1665          outer cast in a chain of casting win), instead of assuming
1666          "it can't happen".  */
1667       {
1668         const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
1669                                 | TYPE_INSTANCE_FLAG_DATA_SPACE);
1670         const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
1671         int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
1672
1673         /* Treat code vs data spaces and address classes separately.  */
1674         if ((instance_flags & ALL_SPACES) != 0)
1675           new_instance_flags &= ~ALL_SPACES;
1676         if ((instance_flags & ALL_CLASSES) != 0)
1677           new_instance_flags &= ~ALL_CLASSES;
1678
1679         instance_flags |= new_instance_flags;
1680       }
1681     }
1682
1683   /* If this is a struct/class/union with no fields, then check
1684      whether a full definition exists somewhere else.  This is for
1685      systems where a type definition with no fields is issued for such
1686      types, instead of identifying them as stub types in the first
1687      place.  */
1688
1689   if (TYPE_IS_OPAQUE (type) 
1690       && opaque_type_resolution 
1691       && !currently_reading_symtab)
1692     {
1693       const char *name = type_name_no_tag (type);
1694       struct type *newtype;
1695
1696       if (name == NULL)
1697         {
1698           stub_noname_complaint ();
1699           return make_qualified_type (type, instance_flags, NULL);
1700         }
1701       newtype = lookup_transparent_type (name);
1702
1703       if (newtype)
1704         {
1705           /* If the resolved type and the stub are in the same
1706              objfile, then replace the stub type with the real deal.
1707              But if they're in separate objfiles, leave the stub
1708              alone; we'll just look up the transparent type every time
1709              we call check_typedef.  We can't create pointers between
1710              types allocated to different objfiles, since they may
1711              have different lifetimes.  Trying to copy NEWTYPE over to
1712              TYPE's objfile is pointless, too, since you'll have to
1713              move over any other types NEWTYPE refers to, which could
1714              be an unbounded amount of stuff.  */
1715           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1716             type = make_qualified_type (newtype,
1717                                         TYPE_INSTANCE_FLAGS (type),
1718                                         type);
1719           else
1720             type = newtype;
1721         }
1722     }
1723   /* Otherwise, rely on the stub flag being set for opaque/stubbed
1724      types.  */
1725   else if (TYPE_STUB (type) && !currently_reading_symtab)
1726     {
1727       const char *name = type_name_no_tag (type);
1728       /* FIXME: shouldn't we separately check the TYPE_NAME and the
1729          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1730          as appropriate?  (this code was written before TYPE_NAME and
1731          TYPE_TAG_NAME were separate).  */
1732       struct symbol *sym;
1733
1734       if (name == NULL)
1735         {
1736           stub_noname_complaint ();
1737           return make_qualified_type (type, instance_flags, NULL);
1738         }
1739       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1740       if (sym)
1741         {
1742           /* Same as above for opaque types, we can replace the stub
1743              with the complete type only if they are in the same
1744              objfile.  */
1745           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1746             type = make_qualified_type (SYMBOL_TYPE (sym),
1747                                         TYPE_INSTANCE_FLAGS (type),
1748                                         type);
1749           else
1750             type = SYMBOL_TYPE (sym);
1751         }
1752     }
1753
1754   if (TYPE_TARGET_STUB (type))
1755     {
1756       struct type *range_type;
1757       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1758
1759       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1760         {
1761           /* Nothing we can do.  */
1762         }
1763       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1764                && TYPE_NFIELDS (type) == 1
1765                && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
1766                    == TYPE_CODE_RANGE))
1767         {
1768           /* Now recompute the length of the array type, based on its
1769              number of elements and the target type's length.
1770              Watch out for Ada null Ada arrays where the high bound
1771              is smaller than the low bound.  */
1772           const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
1773           const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
1774           ULONGEST len;
1775
1776           if (high_bound < low_bound)
1777             len = 0;
1778           else
1779             {
1780               /* For now, we conservatively take the array length to be 0
1781                  if its length exceeds UINT_MAX.  The code below assumes
1782                  that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
1783                  which is technically not guaranteed by C, but is usually true
1784                  (because it would be true if x were unsigned with its
1785                  high-order bit on).  It uses the fact that
1786                  high_bound-low_bound is always representable in
1787                  ULONGEST and that if high_bound-low_bound+1 overflows,
1788                  it overflows to 0.  We must change these tests if we 
1789                  decide to increase the representation of TYPE_LENGTH
1790                  from unsigned int to ULONGEST.  */
1791               ULONGEST ulow = low_bound, uhigh = high_bound;
1792               ULONGEST tlen = TYPE_LENGTH (target_type);
1793
1794               len = tlen * (uhigh - ulow + 1);
1795               if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh 
1796                   || len > UINT_MAX)
1797                 len = 0;
1798             }
1799           TYPE_LENGTH (type) = len;
1800           TYPE_TARGET_STUB (type) = 0;
1801         }
1802       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1803         {
1804           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1805           TYPE_TARGET_STUB (type) = 0;
1806         }
1807     }
1808
1809   type = make_qualified_type (type, instance_flags, NULL);
1810
1811   /* Cache TYPE_LENGTH for future use.  */
1812   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1813
1814   return type;
1815 }
1816
1817 /* Parse a type expression in the string [P..P+LENGTH).  If an error
1818    occurs, silently return a void type.  */
1819
1820 static struct type *
1821 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
1822 {
1823   struct ui_file *saved_gdb_stderr;
1824   struct type *type = NULL; /* Initialize to keep gcc happy.  */
1825   volatile struct gdb_exception except;
1826
1827   /* Suppress error messages.  */
1828   saved_gdb_stderr = gdb_stderr;
1829   gdb_stderr = ui_file_new ();
1830
1831   /* Call parse_and_eval_type() without fear of longjmp()s.  */
1832   TRY_CATCH (except, RETURN_MASK_ERROR)
1833     {
1834       type = parse_and_eval_type (p, length);
1835     }
1836
1837   if (except.reason < 0)
1838     type = builtin_type (gdbarch)->builtin_void;
1839
1840   /* Stop suppressing error messages.  */
1841   ui_file_delete (gdb_stderr);
1842   gdb_stderr = saved_gdb_stderr;
1843
1844   return type;
1845 }
1846
1847 /* Ugly hack to convert method stubs into method types.
1848
1849    He ain't kiddin'.  This demangles the name of the method into a
1850    string including argument types, parses out each argument type,
1851    generates a string casting a zero to that type, evaluates the
1852    string, and stuffs the resulting type into an argtype vector!!!
1853    Then it knows the type of the whole function (including argument
1854    types for overloading), which info used to be in the stab's but was
1855    removed to hack back the space required for them.  */
1856
1857 static void
1858 check_stub_method (struct type *type, int method_id, int signature_id)
1859 {
1860   struct gdbarch *gdbarch = get_type_arch (type);
1861   struct fn_field *f;
1862   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1863   char *demangled_name = gdb_demangle (mangled_name,
1864                                        DMGL_PARAMS | DMGL_ANSI);
1865   char *argtypetext, *p;
1866   int depth = 0, argcount = 1;
1867   struct field *argtypes;
1868   struct type *mtype;
1869
1870   /* Make sure we got back a function string that we can use.  */
1871   if (demangled_name)
1872     p = strchr (demangled_name, '(');
1873   else
1874     p = NULL;
1875
1876   if (demangled_name == NULL || p == NULL)
1877     error (_("Internal: Cannot demangle mangled name `%s'."), 
1878            mangled_name);
1879
1880   /* Now, read in the parameters that define this type.  */
1881   p += 1;
1882   argtypetext = p;
1883   while (*p)
1884     {
1885       if (*p == '(' || *p == '<')
1886         {
1887           depth += 1;
1888         }
1889       else if (*p == ')' || *p == '>')
1890         {
1891           depth -= 1;
1892         }
1893       else if (*p == ',' && depth == 0)
1894         {
1895           argcount += 1;
1896         }
1897
1898       p += 1;
1899     }
1900
1901   /* If we read one argument and it was ``void'', don't count it.  */
1902   if (strncmp (argtypetext, "(void)", 6) == 0)
1903     argcount -= 1;
1904
1905   /* We need one extra slot, for the THIS pointer.  */
1906
1907   argtypes = (struct field *)
1908     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
1909   p = argtypetext;
1910
1911   /* Add THIS pointer for non-static methods.  */
1912   f = TYPE_FN_FIELDLIST1 (type, method_id);
1913   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1914     argcount = 0;
1915   else
1916     {
1917       argtypes[0].type = lookup_pointer_type (type);
1918       argcount = 1;
1919     }
1920
1921   if (*p != ')')                /* () means no args, skip while.  */
1922     {
1923       depth = 0;
1924       while (*p)
1925         {
1926           if (depth <= 0 && (*p == ',' || *p == ')'))
1927             {
1928               /* Avoid parsing of ellipsis, they will be handled below.
1929                  Also avoid ``void'' as above.  */
1930               if (strncmp (argtypetext, "...", p - argtypetext) != 0
1931                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
1932                 {
1933                   argtypes[argcount].type =
1934                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
1935                   argcount += 1;
1936                 }
1937               argtypetext = p + 1;
1938             }
1939
1940           if (*p == '(' || *p == '<')
1941             {
1942               depth += 1;
1943             }
1944           else if (*p == ')' || *p == '>')
1945             {
1946               depth -= 1;
1947             }
1948
1949           p += 1;
1950         }
1951     }
1952
1953   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1954
1955   /* Now update the old "stub" type into a real type.  */
1956   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1957   TYPE_DOMAIN_TYPE (mtype) = type;
1958   TYPE_FIELDS (mtype) = argtypes;
1959   TYPE_NFIELDS (mtype) = argcount;
1960   TYPE_STUB (mtype) = 0;
1961   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1962   if (p[-2] == '.')
1963     TYPE_VARARGS (mtype) = 1;
1964
1965   xfree (demangled_name);
1966 }
1967
1968 /* This is the external interface to check_stub_method, above.  This
1969    function unstubs all of the signatures for TYPE's METHOD_ID method
1970    name.  After calling this function TYPE_FN_FIELD_STUB will be
1971    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
1972    correct.
1973
1974    This function unfortunately can not die until stabs do.  */
1975
1976 void
1977 check_stub_method_group (struct type *type, int method_id)
1978 {
1979   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1980   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
1981   int j, found_stub = 0;
1982
1983   for (j = 0; j < len; j++)
1984     if (TYPE_FN_FIELD_STUB (f, j))
1985       {
1986         found_stub = 1;
1987         check_stub_method (type, method_id, j);
1988       }
1989
1990   /* GNU v3 methods with incorrect names were corrected when we read
1991      in type information, because it was cheaper to do it then.  The
1992      only GNU v2 methods with incorrect method names are operators and
1993      destructors; destructors were also corrected when we read in type
1994      information.
1995
1996      Therefore the only thing we need to handle here are v2 operator
1997      names.  */
1998   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1999     {
2000       int ret;
2001       char dem_opname[256];
2002
2003       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2004                                                            method_id),
2005                                    dem_opname, DMGL_ANSI);
2006       if (!ret)
2007         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2008                                                              method_id),
2009                                      dem_opname, 0);
2010       if (ret)
2011         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
2012     }
2013 }
2014
2015 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
2016 const struct cplus_struct_type cplus_struct_default = { };
2017
2018 void
2019 allocate_cplus_struct_type (struct type *type)
2020 {
2021   if (HAVE_CPLUS_STRUCT (type))
2022     /* Structure was already allocated.  Nothing more to do.  */
2023     return;
2024
2025   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
2026   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2027     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
2028   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
2029 }
2030
2031 const struct gnat_aux_type gnat_aux_default =
2032   { NULL };
2033
2034 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
2035    and allocate the associated gnat-specific data.  The gnat-specific
2036    data is also initialized to gnat_aux_default.  */
2037
2038 void
2039 allocate_gnat_aux_type (struct type *type)
2040 {
2041   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
2042   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
2043     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
2044   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
2045 }
2046
2047 /* Helper function to initialize the standard scalar types.
2048
2049    If NAME is non-NULL, then it is used to initialize the type name.
2050    Note that NAME is not copied; it is required to have a lifetime at
2051    least as long as OBJFILE.  */
2052
2053 struct type *
2054 init_type (enum type_code code, int length, int flags,
2055            const char *name, struct objfile *objfile)
2056 {
2057   struct type *type;
2058
2059   type = alloc_type (objfile);
2060   TYPE_CODE (type) = code;
2061   TYPE_LENGTH (type) = length;
2062
2063   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
2064   if (flags & TYPE_FLAG_UNSIGNED)
2065     TYPE_UNSIGNED (type) = 1;
2066   if (flags & TYPE_FLAG_NOSIGN)
2067     TYPE_NOSIGN (type) = 1;
2068   if (flags & TYPE_FLAG_STUB)
2069     TYPE_STUB (type) = 1;
2070   if (flags & TYPE_FLAG_TARGET_STUB)
2071     TYPE_TARGET_STUB (type) = 1;
2072   if (flags & TYPE_FLAG_STATIC)
2073     TYPE_STATIC (type) = 1;
2074   if (flags & TYPE_FLAG_PROTOTYPED)
2075     TYPE_PROTOTYPED (type) = 1;
2076   if (flags & TYPE_FLAG_INCOMPLETE)
2077     TYPE_INCOMPLETE (type) = 1;
2078   if (flags & TYPE_FLAG_VARARGS)
2079     TYPE_VARARGS (type) = 1;
2080   if (flags & TYPE_FLAG_VECTOR)
2081     TYPE_VECTOR (type) = 1;
2082   if (flags & TYPE_FLAG_STUB_SUPPORTED)
2083     TYPE_STUB_SUPPORTED (type) = 1;
2084   if (flags & TYPE_FLAG_FIXED_INSTANCE)
2085     TYPE_FIXED_INSTANCE (type) = 1;
2086   if (flags & TYPE_FLAG_GNU_IFUNC)
2087     TYPE_GNU_IFUNC (type) = 1;
2088
2089   TYPE_NAME (type) = name;
2090
2091   /* C++ fancies.  */
2092
2093   if (name && strcmp (name, "char") == 0)
2094     TYPE_NOSIGN (type) = 1;
2095
2096   switch (code)
2097     {
2098       case TYPE_CODE_STRUCT:
2099       case TYPE_CODE_UNION:
2100       case TYPE_CODE_NAMESPACE:
2101         INIT_CPLUS_SPECIFIC (type);
2102         break;
2103       case TYPE_CODE_FLT:
2104         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2105         break;
2106       case TYPE_CODE_FUNC:
2107         INIT_FUNC_SPECIFIC (type);
2108         break;
2109     }
2110   return type;
2111 }
2112 \f
2113 /* Queries on types.  */
2114
2115 int
2116 can_dereference (struct type *t)
2117 {
2118   /* FIXME: Should we return true for references as well as
2119      pointers?  */
2120   CHECK_TYPEDEF (t);
2121   return
2122     (t != NULL
2123      && TYPE_CODE (t) == TYPE_CODE_PTR
2124      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2125 }
2126
2127 int
2128 is_integral_type (struct type *t)
2129 {
2130   CHECK_TYPEDEF (t);
2131   return
2132     ((t != NULL)
2133      && ((TYPE_CODE (t) == TYPE_CODE_INT)
2134          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
2135          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
2136          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2137          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2138          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
2139 }
2140
2141 /* Return true if TYPE is scalar.  */
2142
2143 static int
2144 is_scalar_type (struct type *type)
2145 {
2146   CHECK_TYPEDEF (type);
2147
2148   switch (TYPE_CODE (type))
2149     {
2150     case TYPE_CODE_ARRAY:
2151     case TYPE_CODE_STRUCT:
2152     case TYPE_CODE_UNION:
2153     case TYPE_CODE_SET:
2154     case TYPE_CODE_STRING:
2155       return 0;
2156     default:
2157       return 1;
2158     }
2159 }
2160
2161 /* Return true if T is scalar, or a composite type which in practice has
2162    the memory layout of a scalar type.  E.g., an array or struct with only
2163    one scalar element inside it, or a union with only scalar elements.  */
2164
2165 int
2166 is_scalar_type_recursive (struct type *t)
2167 {
2168   CHECK_TYPEDEF (t);
2169
2170   if (is_scalar_type (t))
2171     return 1;
2172   /* Are we dealing with an array or string of known dimensions?  */
2173   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2174             || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2175            && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2176     {
2177       LONGEST low_bound, high_bound;
2178       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2179
2180       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2181
2182       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2183     }
2184   /* Are we dealing with a struct with one element?  */
2185   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2186     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2187   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2188     {
2189       int i, n = TYPE_NFIELDS (t);
2190
2191       /* If all elements of the union are scalar, then the union is scalar.  */
2192       for (i = 0; i < n; i++)
2193         if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2194           return 0;
2195
2196       return 1;
2197     }
2198
2199   return 0;
2200 }
2201
2202 /* A helper function which returns true if types A and B represent the
2203    "same" class type.  This is true if the types have the same main
2204    type, or the same name.  */
2205
2206 int
2207 class_types_same_p (const struct type *a, const struct type *b)
2208 {
2209   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2210           || (TYPE_NAME (a) && TYPE_NAME (b)
2211               && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2212 }
2213
2214 /* If BASE is an ancestor of DCLASS return the distance between them.
2215    otherwise return -1;
2216    eg:
2217
2218    class A {};
2219    class B: public A {};
2220    class C: public B {};
2221    class D: C {};
2222
2223    distance_to_ancestor (A, A, 0) = 0
2224    distance_to_ancestor (A, B, 0) = 1
2225    distance_to_ancestor (A, C, 0) = 2
2226    distance_to_ancestor (A, D, 0) = 3
2227
2228    If PUBLIC is 1 then only public ancestors are considered,
2229    and the function returns the distance only if BASE is a public ancestor
2230    of DCLASS.
2231    Eg:
2232
2233    distance_to_ancestor (A, D, 1) = -1.  */
2234
2235 static int
2236 distance_to_ancestor (struct type *base, struct type *dclass, int public)
2237 {
2238   int i;
2239   int d;
2240
2241   CHECK_TYPEDEF (base);
2242   CHECK_TYPEDEF (dclass);
2243
2244   if (class_types_same_p (base, dclass))
2245     return 0;
2246
2247   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2248     {
2249       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2250         continue;
2251
2252       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2253       if (d >= 0)
2254         return 1 + d;
2255     }
2256
2257   return -1;
2258 }
2259
2260 /* Check whether BASE is an ancestor or base class or DCLASS
2261    Return 1 if so, and 0 if not.
2262    Note: If BASE and DCLASS are of the same type, this function
2263    will return 1. So for some class A, is_ancestor (A, A) will
2264    return 1.  */
2265
2266 int
2267 is_ancestor (struct type *base, struct type *dclass)
2268 {
2269   return distance_to_ancestor (base, dclass, 0) >= 0;
2270 }
2271
2272 /* Like is_ancestor, but only returns true when BASE is a public
2273    ancestor of DCLASS.  */
2274
2275 int
2276 is_public_ancestor (struct type *base, struct type *dclass)
2277 {
2278   return distance_to_ancestor (base, dclass, 1) >= 0;
2279 }
2280
2281 /* A helper function for is_unique_ancestor.  */
2282
2283 static int
2284 is_unique_ancestor_worker (struct type *base, struct type *dclass,
2285                            int *offset,
2286                            const gdb_byte *valaddr, int embedded_offset,
2287                            CORE_ADDR address, struct value *val)
2288 {
2289   int i, count = 0;
2290
2291   CHECK_TYPEDEF (base);
2292   CHECK_TYPEDEF (dclass);
2293
2294   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2295     {
2296       struct type *iter;
2297       int this_offset;
2298
2299       iter = check_typedef (TYPE_BASECLASS (dclass, i));
2300
2301       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2302                                       address, val);
2303
2304       if (class_types_same_p (base, iter))
2305         {
2306           /* If this is the first subclass, set *OFFSET and set count
2307              to 1.  Otherwise, if this is at the same offset as
2308              previous instances, do nothing.  Otherwise, increment
2309              count.  */
2310           if (*offset == -1)
2311             {
2312               *offset = this_offset;
2313               count = 1;
2314             }
2315           else if (this_offset == *offset)
2316             {
2317               /* Nothing.  */
2318             }
2319           else
2320             ++count;
2321         }
2322       else
2323         count += is_unique_ancestor_worker (base, iter, offset,
2324                                             valaddr,
2325                                             embedded_offset + this_offset,
2326                                             address, val);
2327     }
2328
2329   return count;
2330 }
2331
2332 /* Like is_ancestor, but only returns true if BASE is a unique base
2333    class of the type of VAL.  */
2334
2335 int
2336 is_unique_ancestor (struct type *base, struct value *val)
2337 {
2338   int offset = -1;
2339
2340   return is_unique_ancestor_worker (base, value_type (val), &offset,
2341                                     value_contents_for_printing (val),
2342                                     value_embedded_offset (val),
2343                                     value_address (val), val) == 1;
2344 }
2345
2346 \f
2347 /* Overload resolution.  */
2348
2349 /* Return the sum of the rank of A with the rank of B.  */
2350
2351 struct rank
2352 sum_ranks (struct rank a, struct rank b)
2353 {
2354   struct rank c;
2355   c.rank = a.rank + b.rank;
2356   c.subrank = a.subrank + b.subrank;
2357   return c;
2358 }
2359
2360 /* Compare rank A and B and return:
2361    0 if a = b
2362    1 if a is better than b
2363   -1 if b is better than a.  */
2364
2365 int
2366 compare_ranks (struct rank a, struct rank b)
2367 {
2368   if (a.rank == b.rank)
2369     {
2370       if (a.subrank == b.subrank)
2371         return 0;
2372       if (a.subrank < b.subrank)
2373         return 1;
2374       if (a.subrank > b.subrank)
2375         return -1;
2376     }
2377
2378   if (a.rank < b.rank)
2379     return 1;
2380
2381   /* a.rank > b.rank */
2382   return -1;
2383 }
2384
2385 /* Functions for overload resolution begin here.  */
2386
2387 /* Compare two badness vectors A and B and return the result.
2388    0 => A and B are identical
2389    1 => A and B are incomparable
2390    2 => A is better than B
2391    3 => A is worse than B  */
2392
2393 int
2394 compare_badness (struct badness_vector *a, struct badness_vector *b)
2395 {
2396   int i;
2397   int tmp;
2398   short found_pos = 0;          /* any positives in c? */
2399   short found_neg = 0;          /* any negatives in c? */
2400
2401   /* differing lengths => incomparable */
2402   if (a->length != b->length)
2403     return 1;
2404
2405   /* Subtract b from a */
2406   for (i = 0; i < a->length; i++)
2407     {
2408       tmp = compare_ranks (b->rank[i], a->rank[i]);
2409       if (tmp > 0)
2410         found_pos = 1;
2411       else if (tmp < 0)
2412         found_neg = 1;
2413     }
2414
2415   if (found_pos)
2416     {
2417       if (found_neg)
2418         return 1;               /* incomparable */
2419       else
2420         return 3;               /* A > B */
2421     }
2422   else
2423     /* no positives */
2424     {
2425       if (found_neg)
2426         return 2;               /* A < B */
2427       else
2428         return 0;               /* A == B */
2429     }
2430 }
2431
2432 /* Rank a function by comparing its parameter types (PARMS, length
2433    NPARMS), to the types of an argument list (ARGS, length NARGS).
2434    Return a pointer to a badness vector.  This has NARGS + 1
2435    entries.  */
2436
2437 struct badness_vector *
2438 rank_function (struct type **parms, int nparms, 
2439                struct value **args, int nargs)
2440 {
2441   int i;
2442   struct badness_vector *bv;
2443   int min_len = nparms < nargs ? nparms : nargs;
2444
2445   bv = xmalloc (sizeof (struct badness_vector));
2446   bv->length = nargs + 1;       /* add 1 for the length-match rank.  */
2447   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2448
2449   /* First compare the lengths of the supplied lists.
2450      If there is a mismatch, set it to a high value.  */
2451
2452   /* pai/1997-06-03 FIXME: when we have debug info about default
2453      arguments and ellipsis parameter lists, we should consider those
2454      and rank the length-match more finely.  */
2455
2456   LENGTH_MATCH (bv) = (nargs != nparms)
2457                       ? LENGTH_MISMATCH_BADNESS
2458                       : EXACT_MATCH_BADNESS;
2459
2460   /* Now rank all the parameters of the candidate function.  */
2461   for (i = 1; i <= min_len; i++)
2462     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2463                                  args[i - 1]);
2464
2465   /* If more arguments than parameters, add dummy entries.  */
2466   for (i = min_len + 1; i <= nargs; i++)
2467     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2468
2469   return bv;
2470 }
2471
2472 /* Compare the names of two integer types, assuming that any sign
2473    qualifiers have been checked already.  We do it this way because
2474    there may be an "int" in the name of one of the types.  */
2475
2476 static int
2477 integer_types_same_name_p (const char *first, const char *second)
2478 {
2479   int first_p, second_p;
2480
2481   /* If both are shorts, return 1; if neither is a short, keep
2482      checking.  */
2483   first_p = (strstr (first, "short") != NULL);
2484   second_p = (strstr (second, "short") != NULL);
2485   if (first_p && second_p)
2486     return 1;
2487   if (first_p || second_p)
2488     return 0;
2489
2490   /* Likewise for long.  */
2491   first_p = (strstr (first, "long") != NULL);
2492   second_p = (strstr (second, "long") != NULL);
2493   if (first_p && second_p)
2494     return 1;
2495   if (first_p || second_p)
2496     return 0;
2497
2498   /* Likewise for char.  */
2499   first_p = (strstr (first, "char") != NULL);
2500   second_p = (strstr (second, "char") != NULL);
2501   if (first_p && second_p)
2502     return 1;
2503   if (first_p || second_p)
2504     return 0;
2505
2506   /* They must both be ints.  */
2507   return 1;
2508 }
2509
2510 /* Compares type A to type B returns 1 if the represent the same type
2511    0 otherwise.  */
2512
2513 int
2514 types_equal (struct type *a, struct type *b)
2515 {
2516   /* Identical type pointers.  */
2517   /* However, this still doesn't catch all cases of same type for b
2518      and a.  The reason is that builtin types are different from
2519      the same ones constructed from the object.  */
2520   if (a == b)
2521     return 1;
2522
2523   /* Resolve typedefs */
2524   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2525     a = check_typedef (a);
2526   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2527     b = check_typedef (b);
2528
2529   /* If after resolving typedefs a and b are not of the same type
2530      code then they are not equal.  */
2531   if (TYPE_CODE (a) != TYPE_CODE (b))
2532     return 0;
2533
2534   /* If a and b are both pointers types or both reference types then
2535      they are equal of the same type iff the objects they refer to are
2536      of the same type.  */
2537   if (TYPE_CODE (a) == TYPE_CODE_PTR
2538       || TYPE_CODE (a) == TYPE_CODE_REF)
2539     return types_equal (TYPE_TARGET_TYPE (a),
2540                         TYPE_TARGET_TYPE (b));
2541
2542   /* Well, damnit, if the names are exactly the same, I'll say they
2543      are exactly the same.  This happens when we generate method
2544      stubs.  The types won't point to the same address, but they
2545      really are the same.  */
2546
2547   if (TYPE_NAME (a) && TYPE_NAME (b)
2548       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2549     return 1;
2550
2551   /* Check if identical after resolving typedefs.  */
2552   if (a == b)
2553     return 1;
2554
2555   /* Two function types are equal if their argument and return types
2556      are equal.  */
2557   if (TYPE_CODE (a) == TYPE_CODE_FUNC)
2558     {
2559       int i;
2560
2561       if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
2562         return 0;
2563       
2564       if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
2565         return 0;
2566
2567       for (i = 0; i < TYPE_NFIELDS (a); ++i)
2568         if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
2569           return 0;
2570
2571       return 1;
2572     }
2573
2574   return 0;
2575 }
2576 \f
2577 /* Deep comparison of types.  */
2578
2579 /* An entry in the type-equality bcache.  */
2580
2581 typedef struct type_equality_entry
2582 {
2583   struct type *type1, *type2;
2584 } type_equality_entry_d;
2585
2586 DEF_VEC_O (type_equality_entry_d);
2587
2588 /* A helper function to compare two strings.  Returns 1 if they are
2589    the same, 0 otherwise.  Handles NULLs properly.  */
2590
2591 static int
2592 compare_maybe_null_strings (const char *s, const char *t)
2593 {
2594   if (s == NULL && t != NULL)
2595     return 0;
2596   else if (s != NULL && t == NULL)
2597     return 0;
2598   else if (s == NULL && t== NULL)
2599     return 1;
2600   return strcmp (s, t) == 0;
2601 }
2602
2603 /* A helper function for check_types_worklist that checks two types for
2604    "deep" equality.  Returns non-zero if the types are considered the
2605    same, zero otherwise.  */
2606
2607 static int
2608 check_types_equal (struct type *type1, struct type *type2,
2609                    VEC (type_equality_entry_d) **worklist)
2610 {
2611   CHECK_TYPEDEF (type1);
2612   CHECK_TYPEDEF (type2);
2613
2614   if (type1 == type2)
2615     return 1;
2616
2617   if (TYPE_CODE (type1) != TYPE_CODE (type2)
2618       || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
2619       || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
2620       || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
2621       || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
2622       || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
2623       || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
2624       || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
2625       || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
2626     return 0;
2627
2628   if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
2629                                    TYPE_TAG_NAME (type2)))
2630     return 0;
2631   if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
2632     return 0;
2633
2634   if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
2635     {
2636       if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
2637                   sizeof (*TYPE_RANGE_DATA (type1))) != 0)
2638         return 0;
2639     }
2640   else
2641     {
2642       int i;
2643
2644       for (i = 0; i < TYPE_NFIELDS (type1); ++i)
2645         {
2646           const struct field *field1 = &TYPE_FIELD (type1, i);
2647           const struct field *field2 = &TYPE_FIELD (type2, i);
2648           struct type_equality_entry entry;
2649
2650           if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
2651               || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
2652               || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
2653             return 0;
2654           if (!compare_maybe_null_strings (FIELD_NAME (*field1),
2655                                            FIELD_NAME (*field2)))
2656             return 0;
2657           switch (FIELD_LOC_KIND (*field1))
2658             {
2659             case FIELD_LOC_KIND_BITPOS:
2660               if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
2661                 return 0;
2662               break;
2663             case FIELD_LOC_KIND_ENUMVAL:
2664               if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
2665                 return 0;
2666               break;
2667             case FIELD_LOC_KIND_PHYSADDR:
2668               if (FIELD_STATIC_PHYSADDR (*field1)
2669                   != FIELD_STATIC_PHYSADDR (*field2))
2670                 return 0;
2671               break;
2672             case FIELD_LOC_KIND_PHYSNAME:
2673               if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
2674                                                FIELD_STATIC_PHYSNAME (*field2)))
2675                 return 0;
2676               break;
2677             case FIELD_LOC_KIND_DWARF_BLOCK:
2678               {
2679                 struct dwarf2_locexpr_baton *block1, *block2;
2680
2681                 block1 = FIELD_DWARF_BLOCK (*field1);
2682                 block2 = FIELD_DWARF_BLOCK (*field2);
2683                 if (block1->per_cu != block2->per_cu
2684                     || block1->size != block2->size
2685                     || memcmp (block1->data, block2->data, block1->size) != 0)
2686                   return 0;
2687               }
2688               break;
2689             default:
2690               internal_error (__FILE__, __LINE__, _("Unsupported field kind "
2691                                                     "%d by check_types_equal"),
2692                               FIELD_LOC_KIND (*field1));
2693             }
2694
2695           entry.type1 = FIELD_TYPE (*field1);
2696           entry.type2 = FIELD_TYPE (*field2);
2697           VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2698         }
2699     }
2700
2701   if (TYPE_TARGET_TYPE (type1) != NULL)
2702     {
2703       struct type_equality_entry entry;
2704
2705       if (TYPE_TARGET_TYPE (type2) == NULL)
2706         return 0;
2707
2708       entry.type1 = TYPE_TARGET_TYPE (type1);
2709       entry.type2 = TYPE_TARGET_TYPE (type2);
2710       VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2711     }
2712   else if (TYPE_TARGET_TYPE (type2) != NULL)
2713     return 0;
2714
2715   return 1;
2716 }
2717
2718 /* Check types on a worklist for equality.  Returns zero if any pair
2719    is not equal, non-zero if they are all considered equal.  */
2720
2721 static int
2722 check_types_worklist (VEC (type_equality_entry_d) **worklist,
2723                       struct bcache *cache)
2724 {
2725   while (!VEC_empty (type_equality_entry_d, *worklist))
2726     {
2727       struct type_equality_entry entry;
2728       int added;
2729
2730       entry = *VEC_last (type_equality_entry_d, *worklist);
2731       VEC_pop (type_equality_entry_d, *worklist);
2732
2733       /* If the type pair has already been visited, we know it is
2734          ok.  */
2735       bcache_full (&entry, sizeof (entry), cache, &added);
2736       if (!added)
2737         continue;
2738
2739       if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
2740         return 0;
2741     }
2742
2743   return 1;
2744 }
2745
2746 /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
2747    "deep comparison".  Otherwise return zero.  */
2748
2749 int
2750 types_deeply_equal (struct type *type1, struct type *type2)
2751 {
2752   volatile struct gdb_exception except;
2753   int result = 0;
2754   struct bcache *cache;
2755   VEC (type_equality_entry_d) *worklist = NULL;
2756   struct type_equality_entry entry;
2757
2758   gdb_assert (type1 != NULL && type2 != NULL);
2759
2760   /* Early exit for the simple case.  */
2761   if (type1 == type2)
2762     return 1;
2763
2764   cache = bcache_xmalloc (NULL, NULL);
2765
2766   entry.type1 = type1;
2767   entry.type2 = type2;
2768   VEC_safe_push (type_equality_entry_d, worklist, &entry);
2769
2770   TRY_CATCH (except, RETURN_MASK_ALL)
2771     {
2772       result = check_types_worklist (&worklist, cache);
2773     }
2774   /* check_types_worklist calls several nested helper functions,
2775      some of which can raise a GDB Exception, so we just check
2776      and rethrow here.  If there is a GDB exception, a comparison
2777      is not capable (or trusted), so exit.  */
2778   bcache_xfree (cache);
2779   VEC_free (type_equality_entry_d, worklist);
2780   /* Rethrow if there was a problem.  */
2781   if (except.reason < 0)
2782     throw_exception (except);
2783
2784   return result;
2785 }
2786 \f
2787 /* Compare one type (PARM) for compatibility with another (ARG).
2788  * PARM is intended to be the parameter type of a function; and
2789  * ARG is the supplied argument's type.  This function tests if
2790  * the latter can be converted to the former.
2791  * VALUE is the argument's value or NULL if none (or called recursively)
2792  *
2793  * Return 0 if they are identical types;
2794  * Otherwise, return an integer which corresponds to how compatible
2795  * PARM is to ARG.  The higher the return value, the worse the match.
2796  * Generally the "bad" conversions are all uniformly assigned a 100.  */
2797
2798 struct rank
2799 rank_one_type (struct type *parm, struct type *arg, struct value *value)
2800 {
2801   struct rank rank = {0,0};
2802
2803   if (types_equal (parm, arg))
2804     return EXACT_MATCH_BADNESS;
2805
2806   /* Resolve typedefs */
2807   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2808     parm = check_typedef (parm);
2809   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2810     arg = check_typedef (arg);
2811
2812   /* See through references, since we can almost make non-references
2813      references.  */
2814   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2815     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
2816                        REFERENCE_CONVERSION_BADNESS));
2817   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2818     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
2819                        REFERENCE_CONVERSION_BADNESS));
2820   if (overload_debug)
2821   /* Debugging only.  */
2822     fprintf_filtered (gdb_stderr, 
2823                       "------ Arg is %s [%d], parm is %s [%d]\n",
2824                       TYPE_NAME (arg), TYPE_CODE (arg), 
2825                       TYPE_NAME (parm), TYPE_CODE (parm));
2826
2827   /* x -> y means arg of type x being supplied for parameter of type y.  */
2828
2829   switch (TYPE_CODE (parm))
2830     {
2831     case TYPE_CODE_PTR:
2832       switch (TYPE_CODE (arg))
2833         {
2834         case TYPE_CODE_PTR:
2835
2836           /* Allowed pointer conversions are:
2837              (a) pointer to void-pointer conversion.  */
2838           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2839             return VOID_PTR_CONVERSION_BADNESS;
2840
2841           /* (b) pointer to ancestor-pointer conversion.  */
2842           rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
2843                                                TYPE_TARGET_TYPE (arg),
2844                                                0);
2845           if (rank.subrank >= 0)
2846             return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
2847
2848           return INCOMPATIBLE_TYPE_BADNESS;
2849         case TYPE_CODE_ARRAY:
2850           if (types_equal (TYPE_TARGET_TYPE (parm),
2851                            TYPE_TARGET_TYPE (arg)))
2852             return EXACT_MATCH_BADNESS;
2853           return INCOMPATIBLE_TYPE_BADNESS;
2854         case TYPE_CODE_FUNC:
2855           return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
2856         case TYPE_CODE_INT:
2857           if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
2858             {
2859               if (value_as_long (value) == 0)
2860                 {
2861                   /* Null pointer conversion: allow it to be cast to a pointer.
2862                      [4.10.1 of C++ standard draft n3290]  */
2863                   return NULL_POINTER_CONVERSION_BADNESS;
2864                 }
2865               else
2866                 {
2867                   /* If type checking is disabled, allow the conversion.  */
2868                   if (!strict_type_checking)
2869                     return NS_INTEGER_POINTER_CONVERSION_BADNESS;
2870                 }
2871             }
2872           /* fall through  */
2873         case TYPE_CODE_ENUM:
2874         case TYPE_CODE_FLAGS:
2875         case TYPE_CODE_CHAR:
2876         case TYPE_CODE_RANGE:
2877         case TYPE_CODE_BOOL:
2878         default:
2879           return INCOMPATIBLE_TYPE_BADNESS;
2880         }
2881     case TYPE_CODE_ARRAY:
2882       switch (TYPE_CODE (arg))
2883         {
2884         case TYPE_CODE_PTR:
2885         case TYPE_CODE_ARRAY:
2886           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2887                                 TYPE_TARGET_TYPE (arg), NULL);
2888         default:
2889           return INCOMPATIBLE_TYPE_BADNESS;
2890         }
2891     case TYPE_CODE_FUNC:
2892       switch (TYPE_CODE (arg))
2893         {
2894         case TYPE_CODE_PTR:     /* funcptr -> func */
2895           return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
2896         default:
2897           return INCOMPATIBLE_TYPE_BADNESS;
2898         }
2899     case TYPE_CODE_INT:
2900       switch (TYPE_CODE (arg))
2901         {
2902         case TYPE_CODE_INT:
2903           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2904             {
2905               /* Deal with signed, unsigned, and plain chars and
2906                  signed and unsigned ints.  */
2907               if (TYPE_NOSIGN (parm))
2908                 {
2909                   /* This case only for character types.  */
2910                   if (TYPE_NOSIGN (arg))
2911                     return EXACT_MATCH_BADNESS; /* plain char -> plain char */
2912                   else          /* signed/unsigned char -> plain char */
2913                     return INTEGER_CONVERSION_BADNESS;
2914                 }
2915               else if (TYPE_UNSIGNED (parm))
2916                 {
2917                   if (TYPE_UNSIGNED (arg))
2918                     {
2919                       /* unsigned int -> unsigned int, or 
2920                          unsigned long -> unsigned long */
2921                       if (integer_types_same_name_p (TYPE_NAME (parm), 
2922                                                      TYPE_NAME (arg)))
2923                         return EXACT_MATCH_BADNESS;
2924                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
2925                                                           "int")
2926                                && integer_types_same_name_p (TYPE_NAME (parm),
2927                                                              "long"))
2928                         /* unsigned int -> unsigned long */
2929                         return INTEGER_PROMOTION_BADNESS;
2930                       else
2931                         /* unsigned long -> unsigned int */
2932                         return INTEGER_CONVERSION_BADNESS;
2933                     }
2934                   else
2935                     {
2936                       if (integer_types_same_name_p (TYPE_NAME (arg), 
2937                                                      "long")
2938                           && integer_types_same_name_p (TYPE_NAME (parm), 
2939                                                         "int"))
2940                         /* signed long -> unsigned int */
2941                         return INTEGER_CONVERSION_BADNESS;
2942                       else
2943                         /* signed int/long -> unsigned int/long */
2944                         return INTEGER_CONVERSION_BADNESS;
2945                     }
2946                 }
2947               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2948                 {
2949                   if (integer_types_same_name_p (TYPE_NAME (parm), 
2950                                                  TYPE_NAME (arg)))
2951                     return EXACT_MATCH_BADNESS;
2952                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
2953                                                       "int")
2954                            && integer_types_same_name_p (TYPE_NAME (parm), 
2955                                                          "long"))
2956                     return INTEGER_PROMOTION_BADNESS;
2957                   else
2958                     return INTEGER_CONVERSION_BADNESS;
2959                 }
2960               else
2961                 return INTEGER_CONVERSION_BADNESS;
2962             }
2963           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2964             return INTEGER_PROMOTION_BADNESS;
2965           else
2966             return INTEGER_CONVERSION_BADNESS;
2967         case TYPE_CODE_ENUM:
2968         case TYPE_CODE_FLAGS:
2969         case TYPE_CODE_CHAR:
2970         case TYPE_CODE_RANGE:
2971         case TYPE_CODE_BOOL:
2972           return INTEGER_PROMOTION_BADNESS;
2973         case TYPE_CODE_FLT:
2974           return INT_FLOAT_CONVERSION_BADNESS;
2975         case TYPE_CODE_PTR:
2976           return NS_POINTER_CONVERSION_BADNESS;
2977         default:
2978           return INCOMPATIBLE_TYPE_BADNESS;
2979         }
2980       break;
2981     case TYPE_CODE_ENUM:
2982       switch (TYPE_CODE (arg))
2983         {
2984         case TYPE_CODE_INT:
2985         case TYPE_CODE_CHAR:
2986         case TYPE_CODE_RANGE:
2987         case TYPE_CODE_BOOL:
2988         case TYPE_CODE_ENUM:
2989           return INTEGER_CONVERSION_BADNESS;
2990         case TYPE_CODE_FLT:
2991           return INT_FLOAT_CONVERSION_BADNESS;
2992         default:
2993           return INCOMPATIBLE_TYPE_BADNESS;
2994         }
2995       break;
2996     case TYPE_CODE_CHAR:
2997       switch (TYPE_CODE (arg))
2998         {
2999         case TYPE_CODE_RANGE:
3000         case TYPE_CODE_BOOL:
3001         case TYPE_CODE_ENUM:
3002           return INTEGER_CONVERSION_BADNESS;
3003         case TYPE_CODE_FLT:
3004           return INT_FLOAT_CONVERSION_BADNESS;
3005         case TYPE_CODE_INT:
3006           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
3007             return INTEGER_CONVERSION_BADNESS;
3008           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3009             return INTEGER_PROMOTION_BADNESS;
3010           /* >>> !! else fall through !! <<< */
3011         case TYPE_CODE_CHAR:
3012           /* Deal with signed, unsigned, and plain chars for C++ and
3013              with int cases falling through from previous case.  */
3014           if (TYPE_NOSIGN (parm))
3015             {
3016               if (TYPE_NOSIGN (arg))
3017                 return EXACT_MATCH_BADNESS;
3018               else
3019                 return INTEGER_CONVERSION_BADNESS;
3020             }
3021           else if (TYPE_UNSIGNED (parm))
3022             {
3023               if (TYPE_UNSIGNED (arg))
3024                 return EXACT_MATCH_BADNESS;
3025               else
3026                 return INTEGER_PROMOTION_BADNESS;
3027             }
3028           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3029             return EXACT_MATCH_BADNESS;
3030           else
3031             return INTEGER_CONVERSION_BADNESS;
3032         default:
3033           return INCOMPATIBLE_TYPE_BADNESS;
3034         }
3035       break;
3036     case TYPE_CODE_RANGE:
3037       switch (TYPE_CODE (arg))
3038         {
3039         case TYPE_CODE_INT:
3040         case TYPE_CODE_CHAR:
3041         case TYPE_CODE_RANGE:
3042         case TYPE_CODE_BOOL:
3043         case TYPE_CODE_ENUM:
3044           return INTEGER_CONVERSION_BADNESS;
3045         case TYPE_CODE_FLT:
3046           return INT_FLOAT_CONVERSION_BADNESS;
3047         default:
3048           return INCOMPATIBLE_TYPE_BADNESS;
3049         }
3050       break;
3051     case TYPE_CODE_BOOL:
3052       switch (TYPE_CODE (arg))
3053         {
3054           /* n3290 draft, section 4.12.1 (conv.bool):
3055
3056              "A prvalue of arithmetic, unscoped enumeration, pointer, or
3057              pointer to member type can be converted to a prvalue of type
3058              bool.  A zero value, null pointer value, or null member pointer
3059              value is converted to false; any other value is converted to
3060              true.  A prvalue of type std::nullptr_t can be converted to a
3061              prvalue of type bool; the resulting value is false."  */
3062         case TYPE_CODE_INT:
3063         case TYPE_CODE_CHAR:
3064         case TYPE_CODE_ENUM:
3065         case TYPE_CODE_FLT:
3066         case TYPE_CODE_MEMBERPTR:
3067         case TYPE_CODE_PTR:
3068           return BOOL_CONVERSION_BADNESS;
3069         case TYPE_CODE_RANGE:
3070           return INCOMPATIBLE_TYPE_BADNESS;
3071         case TYPE_CODE_BOOL:
3072           return EXACT_MATCH_BADNESS;
3073         default:
3074           return INCOMPATIBLE_TYPE_BADNESS;
3075         }
3076       break;
3077     case TYPE_CODE_FLT:
3078       switch (TYPE_CODE (arg))
3079         {
3080         case TYPE_CODE_FLT:
3081           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3082             return FLOAT_PROMOTION_BADNESS;
3083           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3084             return EXACT_MATCH_BADNESS;
3085           else
3086             return FLOAT_CONVERSION_BADNESS;
3087         case TYPE_CODE_INT:
3088         case TYPE_CODE_BOOL:
3089         case TYPE_CODE_ENUM:
3090         case TYPE_CODE_RANGE:
3091         case TYPE_CODE_CHAR:
3092           return INT_FLOAT_CONVERSION_BADNESS;
3093         default:
3094           return INCOMPATIBLE_TYPE_BADNESS;
3095         }
3096       break;
3097     case TYPE_CODE_COMPLEX:
3098       switch (TYPE_CODE (arg))
3099         {               /* Strictly not needed for C++, but...  */
3100         case TYPE_CODE_FLT:
3101           return FLOAT_PROMOTION_BADNESS;
3102         case TYPE_CODE_COMPLEX:
3103           return EXACT_MATCH_BADNESS;
3104         default:
3105           return INCOMPATIBLE_TYPE_BADNESS;
3106         }
3107       break;
3108     case TYPE_CODE_STRUCT:
3109       /* currently same as TYPE_CODE_CLASS.  */
3110       switch (TYPE_CODE (arg))
3111         {
3112         case TYPE_CODE_STRUCT:
3113           /* Check for derivation */
3114           rank.subrank = distance_to_ancestor (parm, arg, 0);
3115           if (rank.subrank >= 0)
3116             return sum_ranks (BASE_CONVERSION_BADNESS, rank);
3117           /* else fall through */
3118         default:
3119           return INCOMPATIBLE_TYPE_BADNESS;
3120         }
3121       break;
3122     case TYPE_CODE_UNION:
3123       switch (TYPE_CODE (arg))
3124         {
3125         case TYPE_CODE_UNION:
3126         default:
3127           return INCOMPATIBLE_TYPE_BADNESS;
3128         }
3129       break;
3130     case TYPE_CODE_MEMBERPTR:
3131       switch (TYPE_CODE (arg))
3132         {
3133         default:
3134           return INCOMPATIBLE_TYPE_BADNESS;
3135         }
3136       break;
3137     case TYPE_CODE_METHOD:
3138       switch (TYPE_CODE (arg))
3139         {
3140
3141         default:
3142           return INCOMPATIBLE_TYPE_BADNESS;
3143         }
3144       break;
3145     case TYPE_CODE_REF:
3146       switch (TYPE_CODE (arg))
3147         {
3148
3149         default:
3150           return INCOMPATIBLE_TYPE_BADNESS;
3151         }
3152
3153       break;
3154     case TYPE_CODE_SET:
3155       switch (TYPE_CODE (arg))
3156         {
3157           /* Not in C++ */
3158         case TYPE_CODE_SET:
3159           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
3160                                 TYPE_FIELD_TYPE (arg, 0), NULL);
3161         default:
3162           return INCOMPATIBLE_TYPE_BADNESS;
3163         }
3164       break;
3165     case TYPE_CODE_VOID:
3166     default:
3167       return INCOMPATIBLE_TYPE_BADNESS;
3168     }                           /* switch (TYPE_CODE (arg)) */
3169 }
3170
3171 /* End of functions for overload resolution.  */
3172 \f
3173 /* Routines to pretty-print types.  */
3174
3175 static void
3176 print_bit_vector (B_TYPE *bits, int nbits)
3177 {
3178   int bitno;
3179
3180   for (bitno = 0; bitno < nbits; bitno++)
3181     {
3182       if ((bitno % 8) == 0)
3183         {
3184           puts_filtered (" ");
3185         }
3186       if (B_TST (bits, bitno))
3187         printf_filtered (("1"));
3188       else
3189         printf_filtered (("0"));
3190     }
3191 }
3192
3193 /* Note the first arg should be the "this" pointer, we may not want to
3194    include it since we may get into a infinitely recursive
3195    situation.  */
3196
3197 static void
3198 print_arg_types (struct field *args, int nargs, int spaces)
3199 {
3200   if (args != NULL)
3201     {
3202       int i;
3203
3204       for (i = 0; i < nargs; i++)
3205         recursive_dump_type (args[i].type, spaces + 2);
3206     }
3207 }
3208
3209 int
3210 field_is_static (struct field *f)
3211 {
3212   /* "static" fields are the fields whose location is not relative
3213      to the address of the enclosing struct.  It would be nice to
3214      have a dedicated flag that would be set for static fields when
3215      the type is being created.  But in practice, checking the field
3216      loc_kind should give us an accurate answer.  */
3217   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3218           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3219 }
3220
3221 static void
3222 dump_fn_fieldlists (struct type *type, int spaces)
3223 {
3224   int method_idx;
3225   int overload_idx;
3226   struct fn_field *f;
3227
3228   printfi_filtered (spaces, "fn_fieldlists ");
3229   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
3230   printf_filtered ("\n");
3231   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3232     {
3233       f = TYPE_FN_FIELDLIST1 (type, method_idx);
3234       printfi_filtered (spaces + 2, "[%d] name '%s' (",
3235                         method_idx,
3236                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
3237       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3238                               gdb_stdout);
3239       printf_filtered (_(") length %d\n"),
3240                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3241       for (overload_idx = 0;
3242            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3243            overload_idx++)
3244         {
3245           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3246                             overload_idx,
3247                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
3248           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3249                                   gdb_stdout);
3250           printf_filtered (")\n");
3251           printfi_filtered (spaces + 8, "type ");
3252           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
3253                                   gdb_stdout);
3254           printf_filtered ("\n");
3255
3256           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3257                                spaces + 8 + 2);
3258
3259           printfi_filtered (spaces + 8, "args ");
3260           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
3261                                   gdb_stdout);
3262           printf_filtered ("\n");
3263
3264           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
3265                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, 
3266                                                              overload_idx)),
3267                            spaces);
3268           printfi_filtered (spaces + 8, "fcontext ");
3269           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3270                                   gdb_stdout);
3271           printf_filtered ("\n");
3272
3273           printfi_filtered (spaces + 8, "is_const %d\n",
3274                             TYPE_FN_FIELD_CONST (f, overload_idx));
3275           printfi_filtered (spaces + 8, "is_volatile %d\n",
3276                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3277           printfi_filtered (spaces + 8, "is_private %d\n",
3278                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3279           printfi_filtered (spaces + 8, "is_protected %d\n",
3280                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3281           printfi_filtered (spaces + 8, "is_stub %d\n",
3282                             TYPE_FN_FIELD_STUB (f, overload_idx));
3283           printfi_filtered (spaces + 8, "voffset %u\n",
3284                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3285         }
3286     }
3287 }
3288
3289 static void
3290 print_cplus_stuff (struct type *type, int spaces)
3291 {
3292   printfi_filtered (spaces, "n_baseclasses %d\n",
3293                     TYPE_N_BASECLASSES (type));
3294   printfi_filtered (spaces, "nfn_fields %d\n",
3295                     TYPE_NFN_FIELDS (type));
3296   if (TYPE_N_BASECLASSES (type) > 0)
3297     {
3298       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3299                         TYPE_N_BASECLASSES (type));
3300       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
3301                               gdb_stdout);
3302       printf_filtered (")");
3303
3304       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3305                         TYPE_N_BASECLASSES (type));
3306       puts_filtered ("\n");
3307     }
3308   if (TYPE_NFIELDS (type) > 0)
3309     {
3310       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3311         {
3312           printfi_filtered (spaces, 
3313                             "private_field_bits (%d bits at *",
3314                             TYPE_NFIELDS (type));
3315           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
3316                                   gdb_stdout);
3317           printf_filtered (")");
3318           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3319                             TYPE_NFIELDS (type));
3320           puts_filtered ("\n");
3321         }
3322       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3323         {
3324           printfi_filtered (spaces, 
3325                             "protected_field_bits (%d bits at *",
3326                             TYPE_NFIELDS (type));
3327           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
3328                                   gdb_stdout);
3329           printf_filtered (")");
3330           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3331                             TYPE_NFIELDS (type));
3332           puts_filtered ("\n");
3333         }
3334     }
3335   if (TYPE_NFN_FIELDS (type) > 0)
3336     {
3337       dump_fn_fieldlists (type, spaces);
3338     }
3339 }
3340
3341 /* Print the contents of the TYPE's type_specific union, assuming that
3342    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */
3343
3344 static void
3345 print_gnat_stuff (struct type *type, int spaces)
3346 {
3347   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3348
3349   recursive_dump_type (descriptive_type, spaces + 2);
3350 }
3351
3352 static struct obstack dont_print_type_obstack;
3353
3354 void
3355 recursive_dump_type (struct type *type, int spaces)
3356 {
3357   int idx;
3358
3359   if (spaces == 0)
3360     obstack_begin (&dont_print_type_obstack, 0);
3361
3362   if (TYPE_NFIELDS (type) > 0
3363       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
3364     {
3365       struct type **first_dont_print
3366         = (struct type **) obstack_base (&dont_print_type_obstack);
3367
3368       int i = (struct type **) 
3369         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
3370
3371       while (--i >= 0)
3372         {
3373           if (type == first_dont_print[i])
3374             {
3375               printfi_filtered (spaces, "type node ");
3376               gdb_print_host_address (type, gdb_stdout);
3377               printf_filtered (_(" <same as already seen type>\n"));
3378               return;
3379             }
3380         }
3381
3382       obstack_ptr_grow (&dont_print_type_obstack, type);
3383     }
3384
3385   printfi_filtered (spaces, "type node ");
3386   gdb_print_host_address (type, gdb_stdout);
3387   printf_filtered ("\n");
3388   printfi_filtered (spaces, "name '%s' (",
3389                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
3390   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
3391   printf_filtered (")\n");
3392   printfi_filtered (spaces, "tagname '%s' (",
3393                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
3394   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
3395   printf_filtered (")\n");
3396   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
3397   switch (TYPE_CODE (type))
3398     {
3399     case TYPE_CODE_UNDEF:
3400       printf_filtered ("(TYPE_CODE_UNDEF)");
3401       break;
3402     case TYPE_CODE_PTR:
3403       printf_filtered ("(TYPE_CODE_PTR)");
3404       break;
3405     case TYPE_CODE_ARRAY:
3406       printf_filtered ("(TYPE_CODE_ARRAY)");
3407       break;
3408     case TYPE_CODE_STRUCT:
3409       printf_filtered ("(TYPE_CODE_STRUCT)");
3410       break;
3411     case TYPE_CODE_UNION:
3412       printf_filtered ("(TYPE_CODE_UNION)");
3413       break;
3414     case TYPE_CODE_ENUM:
3415       printf_filtered ("(TYPE_CODE_ENUM)");
3416       break;
3417     case TYPE_CODE_FLAGS:
3418       printf_filtered ("(TYPE_CODE_FLAGS)");
3419       break;
3420     case TYPE_CODE_FUNC:
3421       printf_filtered ("(TYPE_CODE_FUNC)");
3422       break;
3423     case TYPE_CODE_INT:
3424       printf_filtered ("(TYPE_CODE_INT)");
3425       break;
3426     case TYPE_CODE_FLT:
3427       printf_filtered ("(TYPE_CODE_FLT)");
3428       break;
3429     case TYPE_CODE_VOID:
3430       printf_filtered ("(TYPE_CODE_VOID)");
3431       break;
3432     case TYPE_CODE_SET:
3433       printf_filtered ("(TYPE_CODE_SET)");
3434       break;
3435     case TYPE_CODE_RANGE:
3436       printf_filtered ("(TYPE_CODE_RANGE)");
3437       break;
3438     case TYPE_CODE_STRING:
3439       printf_filtered ("(TYPE_CODE_STRING)");
3440       break;
3441     case TYPE_CODE_ERROR:
3442       printf_filtered ("(TYPE_CODE_ERROR)");
3443       break;
3444     case TYPE_CODE_MEMBERPTR:
3445       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
3446       break;
3447     case TYPE_CODE_METHODPTR:
3448       printf_filtered ("(TYPE_CODE_METHODPTR)");
3449       break;
3450     case TYPE_CODE_METHOD:
3451       printf_filtered ("(TYPE_CODE_METHOD)");
3452       break;
3453     case TYPE_CODE_REF:
3454       printf_filtered ("(TYPE_CODE_REF)");
3455       break;
3456     case TYPE_CODE_CHAR:
3457       printf_filtered ("(TYPE_CODE_CHAR)");
3458       break;
3459     case TYPE_CODE_BOOL:
3460       printf_filtered ("(TYPE_CODE_BOOL)");
3461       break;
3462     case TYPE_CODE_COMPLEX:
3463       printf_filtered ("(TYPE_CODE_COMPLEX)");
3464       break;
3465     case TYPE_CODE_TYPEDEF:
3466       printf_filtered ("(TYPE_CODE_TYPEDEF)");
3467       break;
3468     case TYPE_CODE_NAMESPACE:
3469       printf_filtered ("(TYPE_CODE_NAMESPACE)");
3470       break;
3471     default:
3472       printf_filtered ("(UNKNOWN TYPE CODE)");
3473       break;
3474     }
3475   puts_filtered ("\n");
3476   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
3477   if (TYPE_OBJFILE_OWNED (type))
3478     {
3479       printfi_filtered (spaces, "objfile ");
3480       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
3481     }
3482   else
3483     {
3484       printfi_filtered (spaces, "gdbarch ");
3485       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
3486     }
3487   printf_filtered ("\n");
3488   printfi_filtered (spaces, "target_type ");
3489   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
3490   printf_filtered ("\n");
3491   if (TYPE_TARGET_TYPE (type) != NULL)
3492     {
3493       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3494     }
3495   printfi_filtered (spaces, "pointer_type ");
3496   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
3497   printf_filtered ("\n");
3498   printfi_filtered (spaces, "reference_type ");
3499   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
3500   printf_filtered ("\n");
3501   printfi_filtered (spaces, "type_chain ");
3502   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3503   printf_filtered ("\n");
3504   printfi_filtered (spaces, "instance_flags 0x%x", 
3505                     TYPE_INSTANCE_FLAGS (type));
3506   if (TYPE_CONST (type))
3507     {
3508       puts_filtered (" TYPE_FLAG_CONST");
3509     }
3510   if (TYPE_VOLATILE (type))
3511     {
3512       puts_filtered (" TYPE_FLAG_VOLATILE");
3513     }
3514   if (TYPE_CODE_SPACE (type))
3515     {
3516       puts_filtered (" TYPE_FLAG_CODE_SPACE");
3517     }
3518   if (TYPE_DATA_SPACE (type))
3519     {
3520       puts_filtered (" TYPE_FLAG_DATA_SPACE");
3521     }
3522   if (TYPE_ADDRESS_CLASS_1 (type))
3523     {
3524       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3525     }
3526   if (TYPE_ADDRESS_CLASS_2 (type))
3527     {
3528       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3529     }
3530   if (TYPE_RESTRICT (type))
3531     {
3532       puts_filtered (" TYPE_FLAG_RESTRICT");
3533     }
3534   puts_filtered ("\n");
3535
3536   printfi_filtered (spaces, "flags");
3537   if (TYPE_UNSIGNED (type))
3538     {
3539       puts_filtered (" TYPE_FLAG_UNSIGNED");
3540     }
3541   if (TYPE_NOSIGN (type))
3542     {
3543       puts_filtered (" TYPE_FLAG_NOSIGN");
3544     }
3545   if (TYPE_STUB (type))
3546     {
3547       puts_filtered (" TYPE_FLAG_STUB");
3548     }
3549   if (TYPE_TARGET_STUB (type))
3550     {
3551       puts_filtered (" TYPE_FLAG_TARGET_STUB");
3552     }
3553   if (TYPE_STATIC (type))
3554     {
3555       puts_filtered (" TYPE_FLAG_STATIC");
3556     }
3557   if (TYPE_PROTOTYPED (type))
3558     {
3559       puts_filtered (" TYPE_FLAG_PROTOTYPED");
3560     }
3561   if (TYPE_INCOMPLETE (type))
3562     {
3563       puts_filtered (" TYPE_FLAG_INCOMPLETE");
3564     }
3565   if (TYPE_VARARGS (type))
3566     {
3567       puts_filtered (" TYPE_FLAG_VARARGS");
3568     }
3569   /* This is used for things like AltiVec registers on ppc.  Gcc emits
3570      an attribute for the array type, which tells whether or not we
3571      have a vector, instead of a regular array.  */
3572   if (TYPE_VECTOR (type))
3573     {
3574       puts_filtered (" TYPE_FLAG_VECTOR");
3575     }
3576   if (TYPE_FIXED_INSTANCE (type))
3577     {
3578       puts_filtered (" TYPE_FIXED_INSTANCE");
3579     }
3580   if (TYPE_STUB_SUPPORTED (type))
3581     {
3582       puts_filtered (" TYPE_STUB_SUPPORTED");
3583     }
3584   if (TYPE_NOTTEXT (type))
3585     {
3586       puts_filtered (" TYPE_NOTTEXT");
3587     }
3588   puts_filtered ("\n");
3589   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3590   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3591   puts_filtered ("\n");
3592   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3593     {
3594       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3595         printfi_filtered (spaces + 2,
3596                           "[%d] enumval %s type ",
3597                           idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3598       else
3599         printfi_filtered (spaces + 2,
3600                           "[%d] bitpos %d bitsize %d type ",
3601                           idx, TYPE_FIELD_BITPOS (type, idx),
3602                           TYPE_FIELD_BITSIZE (type, idx));
3603       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3604       printf_filtered (" name '%s' (",
3605                        TYPE_FIELD_NAME (type, idx) != NULL
3606                        ? TYPE_FIELD_NAME (type, idx)
3607                        : "<NULL>");
3608       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3609       printf_filtered (")\n");
3610       if (TYPE_FIELD_TYPE (type, idx) != NULL)
3611         {
3612           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3613         }
3614     }
3615   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3616     {
3617       printfi_filtered (spaces, "low %s%s  high %s%s\n",
3618                         plongest (TYPE_LOW_BOUND (type)), 
3619                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3620                         plongest (TYPE_HIGH_BOUND (type)),
3621                         TYPE_HIGH_BOUND_UNDEFINED (type) 
3622                         ? " (undefined)" : "");
3623     }
3624   printfi_filtered (spaces, "vptr_basetype ");
3625   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3626   puts_filtered ("\n");
3627   if (TYPE_VPTR_BASETYPE (type) != NULL)
3628     {
3629       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3630     }
3631   printfi_filtered (spaces, "vptr_fieldno %d\n", 
3632                     TYPE_VPTR_FIELDNO (type));
3633
3634   switch (TYPE_SPECIFIC_FIELD (type))
3635     {
3636       case TYPE_SPECIFIC_CPLUS_STUFF:
3637         printfi_filtered (spaces, "cplus_stuff ");
3638         gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
3639                                 gdb_stdout);
3640         puts_filtered ("\n");
3641         print_cplus_stuff (type, spaces);
3642         break;
3643
3644       case TYPE_SPECIFIC_GNAT_STUFF:
3645         printfi_filtered (spaces, "gnat_stuff ");
3646         gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3647         puts_filtered ("\n");
3648         print_gnat_stuff (type, spaces);
3649         break;
3650
3651       case TYPE_SPECIFIC_FLOATFORMAT:
3652         printfi_filtered (spaces, "floatformat ");
3653         if (TYPE_FLOATFORMAT (type) == NULL)
3654           puts_filtered ("(null)");
3655         else
3656           {
3657             puts_filtered ("{ ");
3658             if (TYPE_FLOATFORMAT (type)[0] == NULL
3659                 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3660               puts_filtered ("(null)");
3661             else
3662               puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3663
3664             puts_filtered (", ");
3665             if (TYPE_FLOATFORMAT (type)[1] == NULL
3666                 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3667               puts_filtered ("(null)");
3668             else
3669               puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3670
3671             puts_filtered (" }");
3672           }
3673         puts_filtered ("\n");
3674         break;
3675
3676       case TYPE_SPECIFIC_FUNC:
3677         printfi_filtered (spaces, "calling_convention %d\n",
3678                           TYPE_CALLING_CONVENTION (type));
3679         /* tail_call_list is not printed.  */
3680         break;
3681     }
3682
3683   if (spaces == 0)
3684     obstack_free (&dont_print_type_obstack, NULL);
3685 }
3686 \f
3687 /* Trivial helpers for the libiberty hash table, for mapping one
3688    type to another.  */
3689
3690 struct type_pair
3691 {
3692   struct type *old, *new;
3693 };
3694
3695 static hashval_t
3696 type_pair_hash (const void *item)
3697 {
3698   const struct type_pair *pair = item;
3699
3700   return htab_hash_pointer (pair->old);
3701 }
3702
3703 static int
3704 type_pair_eq (const void *item_lhs, const void *item_rhs)
3705 {
3706   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3707
3708   return lhs->old == rhs->old;
3709 }
3710
3711 /* Allocate the hash table used by copy_type_recursive to walk
3712    types without duplicates.  We use OBJFILE's obstack, because
3713    OBJFILE is about to be deleted.  */
3714
3715 htab_t
3716 create_copied_types_hash (struct objfile *objfile)
3717 {
3718   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3719                                NULL, &objfile->objfile_obstack,
3720                                hashtab_obstack_allocate,
3721                                dummy_obstack_deallocate);
3722 }
3723
3724 /* Recursively copy (deep copy) TYPE, if it is associated with
3725    OBJFILE.  Return a new type allocated using malloc, a saved type if
3726    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
3727    not associated with OBJFILE.  */
3728
3729 struct type *
3730 copy_type_recursive (struct objfile *objfile, 
3731                      struct type *type,
3732                      htab_t copied_types)
3733 {
3734   struct type_pair *stored, pair;
3735   void **slot;
3736   struct type *new_type;
3737
3738   if (! TYPE_OBJFILE_OWNED (type))
3739     return type;
3740
3741   /* This type shouldn't be pointing to any types in other objfiles;
3742      if it did, the type might disappear unexpectedly.  */
3743   gdb_assert (TYPE_OBJFILE (type) == objfile);
3744
3745   pair.old = type;
3746   slot = htab_find_slot (copied_types, &pair, INSERT);
3747   if (*slot != NULL)
3748     return ((struct type_pair *) *slot)->new;
3749
3750   new_type = alloc_type_arch (get_type_arch (type));
3751
3752   /* We must add the new type to the hash table immediately, in case
3753      we encounter this type again during a recursive call below.  */
3754   stored
3755     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
3756   stored->old = type;
3757   stored->new = new_type;
3758   *slot = stored;
3759
3760   /* Copy the common fields of types.  For the main type, we simply
3761      copy the entire thing and then update specific fields as needed.  */
3762   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
3763   TYPE_OBJFILE_OWNED (new_type) = 0;
3764   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
3765
3766   if (TYPE_NAME (type))
3767     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3768   if (TYPE_TAG_NAME (type))
3769     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
3770
3771   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3772   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3773
3774   /* Copy the fields.  */
3775   if (TYPE_NFIELDS (type))
3776     {
3777       int i, nfields;
3778
3779       nfields = TYPE_NFIELDS (type);
3780       TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
3781       for (i = 0; i < nfields; i++)
3782         {
3783           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
3784             TYPE_FIELD_ARTIFICIAL (type, i);
3785           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3786           if (TYPE_FIELD_TYPE (type, i))
3787             TYPE_FIELD_TYPE (new_type, i)
3788               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3789                                      copied_types);
3790           if (TYPE_FIELD_NAME (type, i))
3791             TYPE_FIELD_NAME (new_type, i) = 
3792               xstrdup (TYPE_FIELD_NAME (type, i));
3793           switch (TYPE_FIELD_LOC_KIND (type, i))
3794             {
3795             case FIELD_LOC_KIND_BITPOS:
3796               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
3797                                 TYPE_FIELD_BITPOS (type, i));
3798               break;
3799             case FIELD_LOC_KIND_ENUMVAL:
3800               SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
3801                                  TYPE_FIELD_ENUMVAL (type, i));
3802               break;
3803             case FIELD_LOC_KIND_PHYSADDR:
3804               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3805                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
3806               break;
3807             case FIELD_LOC_KIND_PHYSNAME:
3808               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3809                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
3810                                                                        i)));
3811               break;
3812             default:
3813               internal_error (__FILE__, __LINE__,
3814                               _("Unexpected type field location kind: %d"),
3815                               TYPE_FIELD_LOC_KIND (type, i));
3816             }
3817         }
3818     }
3819
3820   /* For range types, copy the bounds information.  */
3821   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3822     {
3823       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
3824       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
3825     }
3826
3827   /* Copy pointers to other types.  */
3828   if (TYPE_TARGET_TYPE (type))
3829     TYPE_TARGET_TYPE (new_type) = 
3830       copy_type_recursive (objfile, 
3831                            TYPE_TARGET_TYPE (type),
3832                            copied_types);
3833   if (TYPE_VPTR_BASETYPE (type))
3834     TYPE_VPTR_BASETYPE (new_type) = 
3835       copy_type_recursive (objfile,
3836                            TYPE_VPTR_BASETYPE (type),
3837                            copied_types);
3838   /* Maybe copy the type_specific bits.
3839
3840      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3841      base classes and methods.  There's no fundamental reason why we
3842      can't, but at the moment it is not needed.  */
3843
3844   if (TYPE_CODE (type) == TYPE_CODE_FLT)
3845     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
3846   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3847            || TYPE_CODE (type) == TYPE_CODE_UNION
3848            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3849     INIT_CPLUS_SPECIFIC (new_type);
3850
3851   return new_type;
3852 }
3853
3854 /* Make a copy of the given TYPE, except that the pointer & reference
3855    types are not preserved.
3856    
3857    This function assumes that the given type has an associated objfile.
3858    This objfile is used to allocate the new type.  */
3859
3860 struct type *
3861 copy_type (const struct type *type)
3862 {
3863   struct type *new_type;
3864
3865   gdb_assert (TYPE_OBJFILE_OWNED (type));
3866
3867   new_type = alloc_type_copy (type);
3868   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3869   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3870   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
3871           sizeof (struct main_type));
3872
3873   return new_type;
3874 }
3875 \f
3876 /* Helper functions to initialize architecture-specific types.  */
3877
3878 /* Allocate a type structure associated with GDBARCH and set its
3879    CODE, LENGTH, and NAME fields.  */
3880
3881 struct type *
3882 arch_type (struct gdbarch *gdbarch,
3883            enum type_code code, int length, char *name)
3884 {
3885   struct type *type;
3886
3887   type = alloc_type_arch (gdbarch);
3888   TYPE_CODE (type) = code;
3889   TYPE_LENGTH (type) = length;
3890
3891   if (name)
3892     TYPE_NAME (type) = xstrdup (name);
3893
3894   return type;
3895 }
3896
3897 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
3898    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3899    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3900
3901 struct type *
3902 arch_integer_type (struct gdbarch *gdbarch,
3903                    int bit, int unsigned_p, char *name)
3904 {
3905   struct type *t;
3906
3907   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
3908   if (unsigned_p)
3909     TYPE_UNSIGNED (t) = 1;
3910   if (name && strcmp (name, "char") == 0)
3911     TYPE_NOSIGN (t) = 1;
3912
3913   return t;
3914 }
3915
3916 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
3917    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3918    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3919
3920 struct type *
3921 arch_character_type (struct gdbarch *gdbarch,
3922                      int bit, int unsigned_p, char *name)
3923 {
3924   struct type *t;
3925
3926   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
3927   if (unsigned_p)
3928     TYPE_UNSIGNED (t) = 1;
3929
3930   return t;
3931 }
3932
3933 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
3934    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
3935    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
3936
3937 struct type *
3938 arch_boolean_type (struct gdbarch *gdbarch,
3939                    int bit, int unsigned_p, char *name)
3940 {
3941   struct type *t;
3942
3943   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
3944   if (unsigned_p)
3945     TYPE_UNSIGNED (t) = 1;
3946
3947   return t;
3948 }
3949
3950 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
3951    BIT is the type size in bits; if BIT equals -1, the size is
3952    determined by the floatformat.  NAME is the type name.  Set the
3953    TYPE_FLOATFORMAT from FLOATFORMATS.  */
3954
3955 struct type *
3956 arch_float_type (struct gdbarch *gdbarch,
3957                  int bit, char *name, const struct floatformat **floatformats)
3958 {
3959   struct type *t;
3960
3961   if (bit == -1)
3962     {
3963       gdb_assert (floatformats != NULL);
3964       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3965       bit = floatformats[0]->totalsize;
3966     }
3967   gdb_assert (bit >= 0);
3968
3969   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
3970   TYPE_FLOATFORMAT (t) = floatformats;
3971   return t;
3972 }
3973
3974 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
3975    NAME is the type name.  TARGET_TYPE is the component float type.  */
3976
3977 struct type *
3978 arch_complex_type (struct gdbarch *gdbarch,
3979                    char *name, struct type *target_type)
3980 {
3981   struct type *t;
3982
3983   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
3984                  2 * TYPE_LENGTH (target_type), name);
3985   TYPE_TARGET_TYPE (t) = target_type;
3986   return t;
3987 }
3988
3989 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
3990    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */
3991
3992 struct type *
3993 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
3994 {
3995   int nfields = length * TARGET_CHAR_BIT;
3996   struct type *type;
3997
3998   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
3999   TYPE_UNSIGNED (type) = 1;
4000   TYPE_NFIELDS (type) = nfields;
4001   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
4002
4003   return type;
4004 }
4005
4006 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
4007    position BITPOS is called NAME.  */
4008
4009 void
4010 append_flags_type_flag (struct type *type, int bitpos, char *name)
4011 {
4012   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
4013   gdb_assert (bitpos < TYPE_NFIELDS (type));
4014   gdb_assert (bitpos >= 0);
4015
4016   if (name)
4017     {
4018       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
4019       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
4020     }
4021   else
4022     {
4023       /* Don't show this field to the user.  */
4024       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
4025     }
4026 }
4027
4028 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
4029    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
4030
4031 struct type *
4032 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
4033 {
4034   struct type *t;
4035
4036   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
4037   t = arch_type (gdbarch, code, 0, NULL);
4038   TYPE_TAG_NAME (t) = name;
4039   INIT_CPLUS_SPECIFIC (t);
4040   return t;
4041 }
4042
4043 /* Add new field with name NAME and type FIELD to composite type T.
4044    Do not set the field's position or adjust the type's length;
4045    the caller should do so.  Return the new field.  */
4046
4047 struct field *
4048 append_composite_type_field_raw (struct type *t, char *name,
4049                                  struct type *field)
4050 {
4051   struct field *f;
4052
4053   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
4054   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
4055                               sizeof (struct field) * TYPE_NFIELDS (t));
4056   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
4057   memset (f, 0, sizeof f[0]);
4058   FIELD_TYPE (f[0]) = field;
4059   FIELD_NAME (f[0]) = name;
4060   return f;
4061 }
4062
4063 /* Add new field with name NAME and type FIELD to composite type T.
4064    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
4065
4066 void
4067 append_composite_type_field_aligned (struct type *t, char *name,
4068                                      struct type *field, int alignment)
4069 {
4070   struct field *f = append_composite_type_field_raw (t, name, field);
4071
4072   if (TYPE_CODE (t) == TYPE_CODE_UNION)
4073     {
4074       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
4075         TYPE_LENGTH (t) = TYPE_LENGTH (field);
4076     }
4077   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4078     {
4079       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4080       if (TYPE_NFIELDS (t) > 1)
4081         {
4082           SET_FIELD_BITPOS (f[0],
4083                             (FIELD_BITPOS (f[-1])
4084                              + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4085                                 * TARGET_CHAR_BIT)));
4086
4087           if (alignment)
4088             {
4089               int left;
4090
4091               alignment *= TARGET_CHAR_BIT;
4092               left = FIELD_BITPOS (f[0]) % alignment;
4093
4094               if (left)
4095                 {
4096                   SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
4097                   TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
4098                 }
4099             }
4100         }
4101     }
4102 }
4103
4104 /* Add new field with name NAME and type FIELD to composite type T.  */
4105
4106 void
4107 append_composite_type_field (struct type *t, char *name,
4108                              struct type *field)
4109 {
4110   append_composite_type_field_aligned (t, name, field, 0);
4111 }
4112
4113 static struct gdbarch_data *gdbtypes_data;
4114
4115 const struct builtin_type *
4116 builtin_type (struct gdbarch *gdbarch)
4117 {
4118   return gdbarch_data (gdbarch, gdbtypes_data);
4119 }
4120
4121 static void *
4122 gdbtypes_post_init (struct gdbarch *gdbarch)
4123 {
4124   struct builtin_type *builtin_type
4125     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4126
4127   /* Basic types.  */
4128   builtin_type->builtin_void
4129     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4130   builtin_type->builtin_char
4131     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4132                          !gdbarch_char_signed (gdbarch), "char");
4133   builtin_type->builtin_signed_char
4134     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4135                          0, "signed char");
4136   builtin_type->builtin_unsigned_char
4137     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4138                          1, "unsigned char");
4139   builtin_type->builtin_short
4140     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4141                          0, "short");
4142   builtin_type->builtin_unsigned_short
4143     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4144                          1, "unsigned short");
4145   builtin_type->builtin_int
4146     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4147                          0, "int");
4148   builtin_type->builtin_unsigned_int
4149     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4150                          1, "unsigned int");
4151   builtin_type->builtin_long
4152     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4153                          0, "long");
4154   builtin_type->builtin_unsigned_long
4155     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4156                          1, "unsigned long");
4157   builtin_type->builtin_long_long
4158     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4159                          0, "long long");
4160   builtin_type->builtin_unsigned_long_long
4161     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4162                          1, "unsigned long long");
4163   builtin_type->builtin_float
4164     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
4165                        "float", gdbarch_float_format (gdbarch));
4166   builtin_type->builtin_double
4167     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
4168                        "double", gdbarch_double_format (gdbarch));
4169   builtin_type->builtin_long_double
4170     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
4171                        "long double", gdbarch_long_double_format (gdbarch));
4172   builtin_type->builtin_complex
4173     = arch_complex_type (gdbarch, "complex",
4174                          builtin_type->builtin_float);
4175   builtin_type->builtin_double_complex
4176     = arch_complex_type (gdbarch, "double complex",
4177                          builtin_type->builtin_double);
4178   builtin_type->builtin_string
4179     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4180   builtin_type->builtin_bool
4181     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
4182
4183   /* The following three are about decimal floating point types, which
4184      are 32-bits, 64-bits and 128-bits respectively.  */
4185   builtin_type->builtin_decfloat
4186     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
4187   builtin_type->builtin_decdouble
4188     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
4189   builtin_type->builtin_declong
4190     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
4191
4192   /* "True" character types.  */
4193   builtin_type->builtin_true_char
4194     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4195   builtin_type->builtin_true_unsigned_char
4196     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
4197
4198   /* Fixed-size integer types.  */
4199   builtin_type->builtin_int0
4200     = arch_integer_type (gdbarch, 0, 0, "int0_t");
4201   builtin_type->builtin_int8
4202     = arch_integer_type (gdbarch, 8, 0, "int8_t");
4203   builtin_type->builtin_uint8
4204     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4205   builtin_type->builtin_int16
4206     = arch_integer_type (gdbarch, 16, 0, "int16_t");
4207   builtin_type->builtin_uint16
4208     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4209   builtin_type->builtin_int32
4210     = arch_integer_type (gdbarch, 32, 0, "int32_t");
4211   builtin_type->builtin_uint32
4212     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4213   builtin_type->builtin_int64
4214     = arch_integer_type (gdbarch, 64, 0, "int64_t");
4215   builtin_type->builtin_uint64
4216     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4217   builtin_type->builtin_int128
4218     = arch_integer_type (gdbarch, 128, 0, "int128_t");
4219   builtin_type->builtin_uint128
4220     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
4221   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4222     TYPE_INSTANCE_FLAG_NOTTEXT;
4223   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4224     TYPE_INSTANCE_FLAG_NOTTEXT;
4225
4226   /* Wide character types.  */
4227   builtin_type->builtin_char16
4228     = arch_integer_type (gdbarch, 16, 0, "char16_t");
4229   builtin_type->builtin_char32
4230     = arch_integer_type (gdbarch, 32, 0, "char32_t");
4231         
4232
4233   /* Default data/code pointer types.  */
4234   builtin_type->builtin_data_ptr
4235     = lookup_pointer_type (builtin_type->builtin_void);
4236   builtin_type->builtin_func_ptr
4237     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
4238   builtin_type->builtin_func_func
4239     = lookup_function_type (builtin_type->builtin_func_ptr);
4240
4241   /* This type represents a GDB internal function.  */
4242   builtin_type->internal_fn
4243     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4244                  "<internal function>");
4245
4246   return builtin_type;
4247 }
4248
4249 /* This set of objfile-based types is intended to be used by symbol
4250    readers as basic types.  */
4251
4252 static const struct objfile_data *objfile_type_data;
4253
4254 const struct objfile_type *
4255 objfile_type (struct objfile *objfile)
4256 {
4257   struct gdbarch *gdbarch;
4258   struct objfile_type *objfile_type
4259     = objfile_data (objfile, objfile_type_data);
4260
4261   if (objfile_type)
4262     return objfile_type;
4263
4264   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4265                                  1, struct objfile_type);
4266
4267   /* Use the objfile architecture to determine basic type properties.  */
4268   gdbarch = get_objfile_arch (objfile);
4269
4270   /* Basic types.  */
4271   objfile_type->builtin_void
4272     = init_type (TYPE_CODE_VOID, 1,
4273                  0,
4274                  "void", objfile);
4275
4276   objfile_type->builtin_char
4277     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4278                  (TYPE_FLAG_NOSIGN
4279                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4280                  "char", objfile);
4281   objfile_type->builtin_signed_char
4282     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4283                  0,
4284                  "signed char", objfile);
4285   objfile_type->builtin_unsigned_char
4286     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4287                  TYPE_FLAG_UNSIGNED,
4288                  "unsigned char", objfile);
4289   objfile_type->builtin_short
4290     = init_type (TYPE_CODE_INT,
4291                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4292                  0, "short", objfile);
4293   objfile_type->builtin_unsigned_short
4294     = init_type (TYPE_CODE_INT,
4295                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4296                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4297   objfile_type->builtin_int
4298     = init_type (TYPE_CODE_INT,
4299                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4300                  0, "int", objfile);
4301   objfile_type->builtin_unsigned_int
4302     = init_type (TYPE_CODE_INT,
4303                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4304                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4305   objfile_type->builtin_long
4306     = init_type (TYPE_CODE_INT,
4307                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4308                  0, "long", objfile);
4309   objfile_type->builtin_unsigned_long
4310     = init_type (TYPE_CODE_INT,
4311                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4312                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4313   objfile_type->builtin_long_long
4314     = init_type (TYPE_CODE_INT,
4315                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4316                  0, "long long", objfile);
4317   objfile_type->builtin_unsigned_long_long
4318     = init_type (TYPE_CODE_INT,
4319                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4320                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4321
4322   objfile_type->builtin_float
4323     = init_type (TYPE_CODE_FLT,
4324                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4325                  0, "float", objfile);
4326   TYPE_FLOATFORMAT (objfile_type->builtin_float)
4327     = gdbarch_float_format (gdbarch);
4328   objfile_type->builtin_double
4329     = init_type (TYPE_CODE_FLT,
4330                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4331                  0, "double", objfile);
4332   TYPE_FLOATFORMAT (objfile_type->builtin_double)
4333     = gdbarch_double_format (gdbarch);
4334   objfile_type->builtin_long_double
4335     = init_type (TYPE_CODE_FLT,
4336                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
4337                  0, "long double", objfile);
4338   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
4339     = gdbarch_long_double_format (gdbarch);
4340
4341   /* This type represents a type that was unrecognized in symbol read-in.  */
4342   objfile_type->builtin_error
4343     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
4344
4345   /* The following set of types is used for symbols with no
4346      debug information.  */
4347   objfile_type->nodebug_text_symbol
4348     = init_type (TYPE_CODE_FUNC, 1, 0,
4349                  "<text variable, no debug info>", objfile);
4350   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
4351     = objfile_type->builtin_int;
4352   objfile_type->nodebug_text_gnu_ifunc_symbol
4353     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4354                  "<text gnu-indirect-function variable, no debug info>",
4355                  objfile);
4356   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4357     = objfile_type->nodebug_text_symbol;
4358   objfile_type->nodebug_got_plt_symbol
4359     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4360                  "<text from jump slot in .got.plt, no debug info>",
4361                  objfile);
4362   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4363     = objfile_type->nodebug_text_symbol;
4364   objfile_type->nodebug_data_symbol
4365     = init_type (TYPE_CODE_INT,
4366                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4367                  "<data variable, no debug info>", objfile);
4368   objfile_type->nodebug_unknown_symbol
4369     = init_type (TYPE_CODE_INT, 1, 0,
4370                  "<variable (not text or data), no debug info>", objfile);
4371   objfile_type->nodebug_tls_symbol
4372     = init_type (TYPE_CODE_INT,
4373                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4374                  "<thread local variable, no debug info>", objfile);
4375
4376   /* NOTE: on some targets, addresses and pointers are not necessarily
4377      the same.
4378
4379      The upshot is:
4380      - gdb's `struct type' always describes the target's
4381        representation.
4382      - gdb's `struct value' objects should always hold values in
4383        target form.
4384      - gdb's CORE_ADDR values are addresses in the unified virtual
4385        address space that the assembler and linker work with.  Thus,
4386        since target_read_memory takes a CORE_ADDR as an argument, it
4387        can access any memory on the target, even if the processor has
4388        separate code and data address spaces.
4389
4390      In this context, objfile_type->builtin_core_addr is a bit odd:
4391      it's a target type for a value the target will never see.  It's
4392      only used to hold the values of (typeless) linker symbols, which
4393      are indeed in the unified virtual address space.  */
4394
4395   objfile_type->builtin_core_addr
4396     = init_type (TYPE_CODE_INT,
4397                  gdbarch_addr_bit (gdbarch) / 8,
4398                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
4399
4400   set_objfile_data (objfile, objfile_type_data, objfile_type);
4401   return objfile_type;
4402 }
4403
4404 extern initialize_file_ftype _initialize_gdbtypes;
4405
4406 void
4407 _initialize_gdbtypes (void)
4408 {
4409   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
4410   objfile_type_data = register_objfile_data ();
4411
4412   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4413                              _("Set debugging of C++ overloading."),
4414                              _("Show debugging of C++ overloading."),
4415                              _("When enabled, ranking of the "
4416                                "functions is displayed."),
4417                              NULL,
4418                              show_overload_debug,
4419                              &setdebuglist, &showdebuglist);
4420
4421   /* Add user knob for controlling resolution of opaque types.  */
4422   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
4423                            &opaque_type_resolution,
4424                            _("Set resolution of opaque struct/class/union"
4425                              " types (if set before loading symbols)."),
4426                            _("Show resolution of opaque struct/class/union"
4427                              " types (if set before loading symbols)."),
4428                            NULL, NULL,
4429                            show_opaque_type_resolution,
4430                            &setlist, &showlist);
4431
4432   /* Add an option to permit non-strict type checking.  */
4433   add_setshow_boolean_cmd ("type", class_support,
4434                            &strict_type_checking,
4435                            _("Set strict type checking."),
4436                            _("Show strict type checking."),
4437                            NULL, NULL,
4438                            show_strict_type_checking,
4439                            &setchecklist, &showchecklist);
4440 }