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