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