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