Imported Upstream version 7.9
[platform/upstream/gdb.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       return !has_static_range (TYPE_RANGE_DATA (type));
1654
1655     case TYPE_CODE_ARRAY:
1656       {
1657         gdb_assert (TYPE_NFIELDS (type) == 1);
1658
1659         /* The array is dynamic if either the bounds are dynamic,
1660            or the elements it contains have a dynamic contents.  */
1661         if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
1662           return 1;
1663         return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
1664       }
1665
1666     case TYPE_CODE_STRUCT:
1667     case TYPE_CODE_UNION:
1668       {
1669         int i;
1670
1671         for (i = 0; i < TYPE_NFIELDS (type); ++i)
1672           if (!field_is_static (&TYPE_FIELD (type, i))
1673               && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
1674             return 1;
1675       }
1676       break;
1677     }
1678
1679   return 0;
1680 }
1681
1682 /* See gdbtypes.h.  */
1683
1684 int
1685 is_dynamic_type (struct type *type)
1686 {
1687   return is_dynamic_type_internal (type, 1);
1688 }
1689
1690 static struct type *resolve_dynamic_type_internal (struct type *type,
1691                                                    CORE_ADDR addr,
1692                                                    int top_level);
1693
1694 /* Given a dynamic range type (dyn_range_type) and address,
1695    return a static version of that type.  */
1696
1697 static struct type *
1698 resolve_dynamic_range (struct type *dyn_range_type, CORE_ADDR addr)
1699 {
1700   CORE_ADDR value;
1701   struct type *static_range_type;
1702   const struct dynamic_prop *prop;
1703   const struct dwarf2_locexpr_baton *baton;
1704   struct dynamic_prop low_bound, high_bound;
1705
1706   gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);
1707
1708   prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
1709   if (dwarf2_evaluate_property (prop, addr, &value))
1710     {
1711       low_bound.kind = PROP_CONST;
1712       low_bound.data.const_val = value;
1713     }
1714   else
1715     {
1716       low_bound.kind = PROP_UNDEFINED;
1717       low_bound.data.const_val = 0;
1718     }
1719
1720   prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
1721   if (dwarf2_evaluate_property (prop, addr, &value))
1722     {
1723       high_bound.kind = PROP_CONST;
1724       high_bound.data.const_val = value;
1725
1726       if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count)
1727         high_bound.data.const_val
1728           = low_bound.data.const_val + high_bound.data.const_val - 1;
1729     }
1730   else
1731     {
1732       high_bound.kind = PROP_UNDEFINED;
1733       high_bound.data.const_val = 0;
1734     }
1735
1736   static_range_type = create_range_type (copy_type (dyn_range_type),
1737                                          TYPE_TARGET_TYPE (dyn_range_type),
1738                                          &low_bound, &high_bound);
1739   TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1;
1740   return static_range_type;
1741 }
1742
1743 /* Resolves dynamic bound values of an array type TYPE to static ones.
1744    ADDRESS might be needed to resolve the subrange bounds, it is the location
1745    of the associated array.  */
1746
1747 static struct type *
1748 resolve_dynamic_array (struct type *type, CORE_ADDR addr)
1749 {
1750   CORE_ADDR value;
1751   struct type *elt_type;
1752   struct type *range_type;
1753   struct type *ary_dim;
1754
1755   gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
1756
1757   elt_type = type;
1758   range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
1759   range_type = resolve_dynamic_range (range_type, addr);
1760
1761   ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));
1762
1763   if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
1764     elt_type = resolve_dynamic_array (TYPE_TARGET_TYPE (type), addr);
1765   else
1766     elt_type = TYPE_TARGET_TYPE (type);
1767
1768   return create_array_type (copy_type (type),
1769                             elt_type,
1770                             range_type);
1771 }
1772
1773 /* Resolve dynamic bounds of members of the union TYPE to static
1774    bounds.  */
1775
1776 static struct type *
1777 resolve_dynamic_union (struct type *type, CORE_ADDR addr)
1778 {
1779   struct type *resolved_type;
1780   int i;
1781   unsigned int max_len = 0;
1782
1783   gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
1784
1785   resolved_type = copy_type (type);
1786   TYPE_FIELDS (resolved_type)
1787     = TYPE_ALLOC (resolved_type,
1788                   TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1789   memcpy (TYPE_FIELDS (resolved_type),
1790           TYPE_FIELDS (type),
1791           TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1792   for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
1793     {
1794       struct type *t;
1795
1796       if (field_is_static (&TYPE_FIELD (type, i)))
1797         continue;
1798
1799       t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
1800                                          addr, 0);
1801       TYPE_FIELD_TYPE (resolved_type, i) = t;
1802       if (TYPE_LENGTH (t) > max_len)
1803         max_len = TYPE_LENGTH (t);
1804     }
1805
1806   TYPE_LENGTH (resolved_type) = max_len;
1807   return resolved_type;
1808 }
1809
1810 /* Resolve dynamic bounds of members of the struct TYPE to static
1811    bounds.  */
1812
1813 static struct type *
1814 resolve_dynamic_struct (struct type *type, CORE_ADDR addr)
1815 {
1816   struct type *resolved_type;
1817   int i;
1818   unsigned resolved_type_bit_length = 0;
1819
1820   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
1821   gdb_assert (TYPE_NFIELDS (type) > 0);
1822
1823   resolved_type = copy_type (type);
1824   TYPE_FIELDS (resolved_type)
1825     = TYPE_ALLOC (resolved_type,
1826                   TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1827   memcpy (TYPE_FIELDS (resolved_type),
1828           TYPE_FIELDS (type),
1829           TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1830   for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
1831     {
1832       unsigned new_bit_length;
1833
1834       if (field_is_static (&TYPE_FIELD (type, i)))
1835         continue;
1836
1837       TYPE_FIELD_TYPE (resolved_type, i)
1838         = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
1839                                          addr, 0);
1840
1841       /* As we know this field is not a static field, the field's
1842          field_loc_kind should be FIELD_LOC_KIND_BITPOS.  Verify
1843          this is the case, but only trigger a simple error rather
1844          than an internal error if that fails.  While failing
1845          that verification indicates a bug in our code, the error
1846          is not severe enough to suggest to the user he stops
1847          his debugging session because of it.  */
1848       if (TYPE_FIELD_LOC_KIND (resolved_type, i) != FIELD_LOC_KIND_BITPOS)
1849         error (_("Cannot determine struct field location"
1850                  " (invalid location kind)"));
1851       new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
1852       if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
1853         new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
1854       else
1855         new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
1856                            * TARGET_CHAR_BIT);
1857
1858       /* Normally, we would use the position and size of the last field
1859          to determine the size of the enclosing structure.  But GCC seems
1860          to be encoding the position of some fields incorrectly when
1861          the struct contains a dynamic field that is not placed last.
1862          So we compute the struct size based on the field that has
1863          the highest position + size - probably the best we can do.  */
1864       if (new_bit_length > resolved_type_bit_length)
1865         resolved_type_bit_length = new_bit_length;
1866     }
1867
1868   TYPE_LENGTH (resolved_type)
1869     = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1870
1871   return resolved_type;
1872 }
1873
1874 /* Worker for resolved_dynamic_type.  */
1875
1876 static struct type *
1877 resolve_dynamic_type_internal (struct type *type, CORE_ADDR addr,
1878                                int top_level)
1879 {
1880   struct type *real_type = check_typedef (type);
1881   struct type *resolved_type = type;
1882   const struct dynamic_prop *prop;
1883   CORE_ADDR value;
1884
1885   if (!is_dynamic_type_internal (real_type, top_level))
1886     return type;
1887
1888   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1889     {
1890       resolved_type = copy_type (type);
1891       TYPE_TARGET_TYPE (resolved_type)
1892         = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr,
1893                                          top_level);
1894     }
1895   else 
1896     {
1897       /* Before trying to resolve TYPE, make sure it is not a stub.  */
1898       type = real_type;
1899
1900       switch (TYPE_CODE (type))
1901         {
1902         case TYPE_CODE_REF:
1903           {
1904             CORE_ADDR target_addr = read_memory_typed_address (addr, type);
1905
1906             resolved_type = copy_type (type);
1907             TYPE_TARGET_TYPE (resolved_type)
1908               = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
1909                                                target_addr, top_level);
1910             break;
1911           }
1912
1913         case TYPE_CODE_ARRAY:
1914           resolved_type = resolve_dynamic_array (type, addr);
1915           break;
1916
1917         case TYPE_CODE_RANGE:
1918           resolved_type = resolve_dynamic_range (type, addr);
1919           break;
1920
1921         case TYPE_CODE_UNION:
1922           resolved_type = resolve_dynamic_union (type, addr);
1923           break;
1924
1925         case TYPE_CODE_STRUCT:
1926           resolved_type = resolve_dynamic_struct (type, addr);
1927           break;
1928         }
1929     }
1930
1931   /* Resolve data_location attribute.  */
1932   prop = TYPE_DATA_LOCATION (resolved_type);
1933   if (dwarf2_evaluate_property (prop, addr, &value))
1934     {
1935       TYPE_DATA_LOCATION_ADDR (resolved_type) = value;
1936       TYPE_DATA_LOCATION_KIND (resolved_type) = PROP_CONST;
1937     }
1938   else
1939     TYPE_DATA_LOCATION (resolved_type) = NULL;
1940
1941   return resolved_type;
1942 }
1943
1944 /* See gdbtypes.h  */
1945
1946 struct type *
1947 resolve_dynamic_type (struct type *type, CORE_ADDR addr)
1948 {
1949   return resolve_dynamic_type_internal (type, addr, 1);
1950 }
1951
1952 /* Find the real type of TYPE.  This function returns the real type,
1953    after removing all layers of typedefs, and completing opaque or stub
1954    types.  Completion changes the TYPE argument, but stripping of
1955    typedefs does not.
1956
1957    Instance flags (e.g. const/volatile) are preserved as typedefs are
1958    stripped.  If necessary a new qualified form of the underlying type
1959    is created.
1960
1961    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1962    not been computed and we're either in the middle of reading symbols, or
1963    there was no name for the typedef in the debug info.
1964
1965    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1966    QUITs in the symbol reading code can also throw.
1967    Thus this function can throw an exception.
1968
1969    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1970    the target type.
1971
1972    If this is a stubbed struct (i.e. declared as struct foo *), see if
1973    we can find a full definition in some other file.  If so, copy this
1974    definition, so we can use it in future.  There used to be a comment
1975    (but not any code) that if we don't find a full definition, we'd
1976    set a flag so we don't spend time in the future checking the same
1977    type.  That would be a mistake, though--we might load in more
1978    symbols which contain a full definition for the type.  */
1979
1980 struct type *
1981 check_typedef (struct type *type)
1982 {
1983   struct type *orig_type = type;
1984   /* While we're removing typedefs, we don't want to lose qualifiers.
1985      E.g., const/volatile.  */
1986   int instance_flags = TYPE_INSTANCE_FLAGS (type);
1987
1988   gdb_assert (type);
1989
1990   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1991     {
1992       if (!TYPE_TARGET_TYPE (type))
1993         {
1994           const char *name;
1995           struct symbol *sym;
1996
1997           /* It is dangerous to call lookup_symbol if we are currently
1998              reading a symtab.  Infinite recursion is one danger.  */
1999           if (currently_reading_symtab)
2000             return make_qualified_type (type, instance_flags, NULL);
2001
2002           name = type_name_no_tag (type);
2003           /* FIXME: shouldn't we separately check the TYPE_NAME and
2004              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
2005              VAR_DOMAIN as appropriate?  (this code was written before
2006              TYPE_NAME and TYPE_TAG_NAME were separate).  */
2007           if (name == NULL)
2008             {
2009               stub_noname_complaint ();
2010               return make_qualified_type (type, instance_flags, NULL);
2011             }
2012           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
2013           if (sym)
2014             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
2015           else                                  /* TYPE_CODE_UNDEF */
2016             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
2017         }
2018       type = TYPE_TARGET_TYPE (type);
2019
2020       /* Preserve the instance flags as we traverse down the typedef chain.
2021
2022          Handling address spaces/classes is nasty, what do we do if there's a
2023          conflict?
2024          E.g., what if an outer typedef marks the type as class_1 and an inner
2025          typedef marks the type as class_2?
2026          This is the wrong place to do such error checking.  We leave it to
2027          the code that created the typedef in the first place to flag the
2028          error.  We just pick the outer address space (akin to letting the
2029          outer cast in a chain of casting win), instead of assuming
2030          "it can't happen".  */
2031       {
2032         const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
2033                                 | TYPE_INSTANCE_FLAG_DATA_SPACE);
2034         const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
2035         int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
2036
2037         /* Treat code vs data spaces and address classes separately.  */
2038         if ((instance_flags & ALL_SPACES) != 0)
2039           new_instance_flags &= ~ALL_SPACES;
2040         if ((instance_flags & ALL_CLASSES) != 0)
2041           new_instance_flags &= ~ALL_CLASSES;
2042
2043         instance_flags |= new_instance_flags;
2044       }
2045     }
2046
2047   /* If this is a struct/class/union with no fields, then check
2048      whether a full definition exists somewhere else.  This is for
2049      systems where a type definition with no fields is issued for such
2050      types, instead of identifying them as stub types in the first
2051      place.  */
2052
2053   if (TYPE_IS_OPAQUE (type) 
2054       && opaque_type_resolution 
2055       && !currently_reading_symtab)
2056     {
2057       const char *name = type_name_no_tag (type);
2058       struct type *newtype;
2059
2060       if (name == NULL)
2061         {
2062           stub_noname_complaint ();
2063           return make_qualified_type (type, instance_flags, NULL);
2064         }
2065       newtype = lookup_transparent_type (name);
2066
2067       if (newtype)
2068         {
2069           /* If the resolved type and the stub are in the same
2070              objfile, then replace the stub type with the real deal.
2071              But if they're in separate objfiles, leave the stub
2072              alone; we'll just look up the transparent type every time
2073              we call check_typedef.  We can't create pointers between
2074              types allocated to different objfiles, since they may
2075              have different lifetimes.  Trying to copy NEWTYPE over to
2076              TYPE's objfile is pointless, too, since you'll have to
2077              move over any other types NEWTYPE refers to, which could
2078              be an unbounded amount of stuff.  */
2079           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
2080             type = make_qualified_type (newtype,
2081                                         TYPE_INSTANCE_FLAGS (type),
2082                                         type);
2083           else
2084             type = newtype;
2085         }
2086     }
2087   /* Otherwise, rely on the stub flag being set for opaque/stubbed
2088      types.  */
2089   else if (TYPE_STUB (type) && !currently_reading_symtab)
2090     {
2091       const char *name = type_name_no_tag (type);
2092       /* FIXME: shouldn't we separately check the TYPE_NAME and the
2093          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
2094          as appropriate?  (this code was written before TYPE_NAME and
2095          TYPE_TAG_NAME were separate).  */
2096       struct symbol *sym;
2097
2098       if (name == NULL)
2099         {
2100           stub_noname_complaint ();
2101           return make_qualified_type (type, instance_flags, NULL);
2102         }
2103       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
2104       if (sym)
2105         {
2106           /* Same as above for opaque types, we can replace the stub
2107              with the complete type only if they are in the same
2108              objfile.  */
2109           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
2110             type = make_qualified_type (SYMBOL_TYPE (sym),
2111                                         TYPE_INSTANCE_FLAGS (type),
2112                                         type);
2113           else
2114             type = SYMBOL_TYPE (sym);
2115         }
2116     }
2117
2118   if (TYPE_TARGET_STUB (type))
2119     {
2120       struct type *range_type;
2121       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
2122
2123       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
2124         {
2125           /* Nothing we can do.  */
2126         }
2127       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
2128         {
2129           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
2130           TYPE_TARGET_STUB (type) = 0;
2131         }
2132     }
2133
2134   type = make_qualified_type (type, instance_flags, NULL);
2135
2136   /* Cache TYPE_LENGTH for future use.  */
2137   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
2138
2139   return type;
2140 }
2141
2142 /* Parse a type expression in the string [P..P+LENGTH).  If an error
2143    occurs, silently return a void type.  */
2144
2145 static struct type *
2146 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
2147 {
2148   struct ui_file *saved_gdb_stderr;
2149   struct type *type = NULL; /* Initialize to keep gcc happy.  */
2150   volatile struct gdb_exception except;
2151
2152   /* Suppress error messages.  */
2153   saved_gdb_stderr = gdb_stderr;
2154   gdb_stderr = ui_file_new ();
2155
2156   /* Call parse_and_eval_type() without fear of longjmp()s.  */
2157   TRY_CATCH (except, RETURN_MASK_ERROR)
2158     {
2159       type = parse_and_eval_type (p, length);
2160     }
2161
2162   if (except.reason < 0)
2163     type = builtin_type (gdbarch)->builtin_void;
2164
2165   /* Stop suppressing error messages.  */
2166   ui_file_delete (gdb_stderr);
2167   gdb_stderr = saved_gdb_stderr;
2168
2169   return type;
2170 }
2171
2172 /* Ugly hack to convert method stubs into method types.
2173
2174    He ain't kiddin'.  This demangles the name of the method into a
2175    string including argument types, parses out each argument type,
2176    generates a string casting a zero to that type, evaluates the
2177    string, and stuffs the resulting type into an argtype vector!!!
2178    Then it knows the type of the whole function (including argument
2179    types for overloading), which info used to be in the stab's but was
2180    removed to hack back the space required for them.  */
2181
2182 static void
2183 check_stub_method (struct type *type, int method_id, int signature_id)
2184 {
2185   struct gdbarch *gdbarch = get_type_arch (type);
2186   struct fn_field *f;
2187   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
2188   char *demangled_name = gdb_demangle (mangled_name,
2189                                        DMGL_PARAMS | DMGL_ANSI);
2190   char *argtypetext, *p;
2191   int depth = 0, argcount = 1;
2192   struct field *argtypes;
2193   struct type *mtype;
2194
2195   /* Make sure we got back a function string that we can use.  */
2196   if (demangled_name)
2197     p = strchr (demangled_name, '(');
2198   else
2199     p = NULL;
2200
2201   if (demangled_name == NULL || p == NULL)
2202     error (_("Internal: Cannot demangle mangled name `%s'."), 
2203            mangled_name);
2204
2205   /* Now, read in the parameters that define this type.  */
2206   p += 1;
2207   argtypetext = p;
2208   while (*p)
2209     {
2210       if (*p == '(' || *p == '<')
2211         {
2212           depth += 1;
2213         }
2214       else if (*p == ')' || *p == '>')
2215         {
2216           depth -= 1;
2217         }
2218       else if (*p == ',' && depth == 0)
2219         {
2220           argcount += 1;
2221         }
2222
2223       p += 1;
2224     }
2225
2226   /* If we read one argument and it was ``void'', don't count it.  */
2227   if (strncmp (argtypetext, "(void)", 6) == 0)
2228     argcount -= 1;
2229
2230   /* We need one extra slot, for the THIS pointer.  */
2231
2232   argtypes = (struct field *)
2233     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
2234   p = argtypetext;
2235
2236   /* Add THIS pointer for non-static methods.  */
2237   f = TYPE_FN_FIELDLIST1 (type, method_id);
2238   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
2239     argcount = 0;
2240   else
2241     {
2242       argtypes[0].type = lookup_pointer_type (type);
2243       argcount = 1;
2244     }
2245
2246   if (*p != ')')                /* () means no args, skip while.  */
2247     {
2248       depth = 0;
2249       while (*p)
2250         {
2251           if (depth <= 0 && (*p == ',' || *p == ')'))
2252             {
2253               /* Avoid parsing of ellipsis, they will be handled below.
2254                  Also avoid ``void'' as above.  */
2255               if (strncmp (argtypetext, "...", p - argtypetext) != 0
2256                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
2257                 {
2258                   argtypes[argcount].type =
2259                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
2260                   argcount += 1;
2261                 }
2262               argtypetext = p + 1;
2263             }
2264
2265           if (*p == '(' || *p == '<')
2266             {
2267               depth += 1;
2268             }
2269           else if (*p == ')' || *p == '>')
2270             {
2271               depth -= 1;
2272             }
2273
2274           p += 1;
2275         }
2276     }
2277
2278   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
2279
2280   /* Now update the old "stub" type into a real type.  */
2281   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
2282   TYPE_DOMAIN_TYPE (mtype) = type;
2283   TYPE_FIELDS (mtype) = argtypes;
2284   TYPE_NFIELDS (mtype) = argcount;
2285   TYPE_STUB (mtype) = 0;
2286   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
2287   if (p[-2] == '.')
2288     TYPE_VARARGS (mtype) = 1;
2289
2290   xfree (demangled_name);
2291 }
2292
2293 /* This is the external interface to check_stub_method, above.  This
2294    function unstubs all of the signatures for TYPE's METHOD_ID method
2295    name.  After calling this function TYPE_FN_FIELD_STUB will be
2296    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
2297    correct.
2298
2299    This function unfortunately can not die until stabs do.  */
2300
2301 void
2302 check_stub_method_group (struct type *type, int method_id)
2303 {
2304   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
2305   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
2306   int j, found_stub = 0;
2307
2308   for (j = 0; j < len; j++)
2309     if (TYPE_FN_FIELD_STUB (f, j))
2310       {
2311         found_stub = 1;
2312         check_stub_method (type, method_id, j);
2313       }
2314
2315   /* GNU v3 methods with incorrect names were corrected when we read
2316      in type information, because it was cheaper to do it then.  The
2317      only GNU v2 methods with incorrect method names are operators and
2318      destructors; destructors were also corrected when we read in type
2319      information.
2320
2321      Therefore the only thing we need to handle here are v2 operator
2322      names.  */
2323   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
2324     {
2325       int ret;
2326       char dem_opname[256];
2327
2328       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2329                                                            method_id),
2330                                    dem_opname, DMGL_ANSI);
2331       if (!ret)
2332         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2333                                                              method_id),
2334                                      dem_opname, 0);
2335       if (ret)
2336         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
2337     }
2338 }
2339
2340 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
2341 const struct cplus_struct_type cplus_struct_default = { };
2342
2343 void
2344 allocate_cplus_struct_type (struct type *type)
2345 {
2346   if (HAVE_CPLUS_STRUCT (type))
2347     /* Structure was already allocated.  Nothing more to do.  */
2348     return;
2349
2350   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
2351   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2352     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
2353   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
2354 }
2355
2356 const struct gnat_aux_type gnat_aux_default =
2357   { NULL };
2358
2359 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
2360    and allocate the associated gnat-specific data.  The gnat-specific
2361    data is also initialized to gnat_aux_default.  */
2362
2363 void
2364 allocate_gnat_aux_type (struct type *type)
2365 {
2366   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
2367   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
2368     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
2369   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
2370 }
2371
2372 /* Helper function to initialize the standard scalar types.
2373
2374    If NAME is non-NULL, then it is used to initialize the type name.
2375    Note that NAME is not copied; it is required to have a lifetime at
2376    least as long as OBJFILE.  */
2377
2378 struct type *
2379 init_type (enum type_code code, int length, int flags,
2380            const char *name, struct objfile *objfile)
2381 {
2382   struct type *type;
2383
2384   type = alloc_type (objfile);
2385   TYPE_CODE (type) = code;
2386   TYPE_LENGTH (type) = length;
2387
2388   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
2389   if (flags & TYPE_FLAG_UNSIGNED)
2390     TYPE_UNSIGNED (type) = 1;
2391   if (flags & TYPE_FLAG_NOSIGN)
2392     TYPE_NOSIGN (type) = 1;
2393   if (flags & TYPE_FLAG_STUB)
2394     TYPE_STUB (type) = 1;
2395   if (flags & TYPE_FLAG_TARGET_STUB)
2396     TYPE_TARGET_STUB (type) = 1;
2397   if (flags & TYPE_FLAG_STATIC)
2398     TYPE_STATIC (type) = 1;
2399   if (flags & TYPE_FLAG_PROTOTYPED)
2400     TYPE_PROTOTYPED (type) = 1;
2401   if (flags & TYPE_FLAG_INCOMPLETE)
2402     TYPE_INCOMPLETE (type) = 1;
2403   if (flags & TYPE_FLAG_VARARGS)
2404     TYPE_VARARGS (type) = 1;
2405   if (flags & TYPE_FLAG_VECTOR)
2406     TYPE_VECTOR (type) = 1;
2407   if (flags & TYPE_FLAG_STUB_SUPPORTED)
2408     TYPE_STUB_SUPPORTED (type) = 1;
2409   if (flags & TYPE_FLAG_FIXED_INSTANCE)
2410     TYPE_FIXED_INSTANCE (type) = 1;
2411   if (flags & TYPE_FLAG_GNU_IFUNC)
2412     TYPE_GNU_IFUNC (type) = 1;
2413
2414   TYPE_NAME (type) = name;
2415
2416   /* C++ fancies.  */
2417
2418   if (name && strcmp (name, "char") == 0)
2419     TYPE_NOSIGN (type) = 1;
2420
2421   switch (code)
2422     {
2423       case TYPE_CODE_STRUCT:
2424       case TYPE_CODE_UNION:
2425       case TYPE_CODE_NAMESPACE:
2426         INIT_CPLUS_SPECIFIC (type);
2427         break;
2428       case TYPE_CODE_FLT:
2429         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2430         break;
2431       case TYPE_CODE_FUNC:
2432         INIT_FUNC_SPECIFIC (type);
2433         break;
2434     }
2435   return type;
2436 }
2437 \f
2438 /* Queries on types.  */
2439
2440 int
2441 can_dereference (struct type *t)
2442 {
2443   /* FIXME: Should we return true for references as well as
2444      pointers?  */
2445   CHECK_TYPEDEF (t);
2446   return
2447     (t != NULL
2448      && TYPE_CODE (t) == TYPE_CODE_PTR
2449      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2450 }
2451
2452 int
2453 is_integral_type (struct type *t)
2454 {
2455   CHECK_TYPEDEF (t);
2456   return
2457     ((t != NULL)
2458      && ((TYPE_CODE (t) == TYPE_CODE_INT)
2459          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
2460          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
2461          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2462          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2463          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
2464 }
2465
2466 /* Return true if TYPE is scalar.  */
2467
2468 static int
2469 is_scalar_type (struct type *type)
2470 {
2471   CHECK_TYPEDEF (type);
2472
2473   switch (TYPE_CODE (type))
2474     {
2475     case TYPE_CODE_ARRAY:
2476     case TYPE_CODE_STRUCT:
2477     case TYPE_CODE_UNION:
2478     case TYPE_CODE_SET:
2479     case TYPE_CODE_STRING:
2480       return 0;
2481     default:
2482       return 1;
2483     }
2484 }
2485
2486 /* Return true if T is scalar, or a composite type which in practice has
2487    the memory layout of a scalar type.  E.g., an array or struct with only
2488    one scalar element inside it, or a union with only scalar elements.  */
2489
2490 int
2491 is_scalar_type_recursive (struct type *t)
2492 {
2493   CHECK_TYPEDEF (t);
2494
2495   if (is_scalar_type (t))
2496     return 1;
2497   /* Are we dealing with an array or string of known dimensions?  */
2498   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2499             || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2500            && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2501     {
2502       LONGEST low_bound, high_bound;
2503       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2504
2505       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2506
2507       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2508     }
2509   /* Are we dealing with a struct with one element?  */
2510   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2511     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2512   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2513     {
2514       int i, n = TYPE_NFIELDS (t);
2515
2516       /* If all elements of the union are scalar, then the union is scalar.  */
2517       for (i = 0; i < n; i++)
2518         if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2519           return 0;
2520
2521       return 1;
2522     }
2523
2524   return 0;
2525 }
2526
2527 /* Return true is T is a class or a union.  False otherwise.  */
2528
2529 int
2530 class_or_union_p (const struct type *t)
2531 {
2532   return (TYPE_CODE (t) == TYPE_CODE_STRUCT
2533           || TYPE_CODE (t) == TYPE_CODE_UNION);
2534 }
2535
2536 /* A helper function which returns true if types A and B represent the
2537    "same" class type.  This is true if the types have the same main
2538    type, or the same name.  */
2539
2540 int
2541 class_types_same_p (const struct type *a, const struct type *b)
2542 {
2543   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2544           || (TYPE_NAME (a) && TYPE_NAME (b)
2545               && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2546 }
2547
2548 /* If BASE is an ancestor of DCLASS return the distance between them.
2549    otherwise return -1;
2550    eg:
2551
2552    class A {};
2553    class B: public A {};
2554    class C: public B {};
2555    class D: C {};
2556
2557    distance_to_ancestor (A, A, 0) = 0
2558    distance_to_ancestor (A, B, 0) = 1
2559    distance_to_ancestor (A, C, 0) = 2
2560    distance_to_ancestor (A, D, 0) = 3
2561
2562    If PUBLIC is 1 then only public ancestors are considered,
2563    and the function returns the distance only if BASE is a public ancestor
2564    of DCLASS.
2565    Eg:
2566
2567    distance_to_ancestor (A, D, 1) = -1.  */
2568
2569 static int
2570 distance_to_ancestor (struct type *base, struct type *dclass, int public)
2571 {
2572   int i;
2573   int d;
2574
2575   CHECK_TYPEDEF (base);
2576   CHECK_TYPEDEF (dclass);
2577
2578   if (class_types_same_p (base, dclass))
2579     return 0;
2580
2581   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2582     {
2583       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2584         continue;
2585
2586       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2587       if (d >= 0)
2588         return 1 + d;
2589     }
2590
2591   return -1;
2592 }
2593
2594 /* Check whether BASE is an ancestor or base class or DCLASS
2595    Return 1 if so, and 0 if not.
2596    Note: If BASE and DCLASS are of the same type, this function
2597    will return 1. So for some class A, is_ancestor (A, A) will
2598    return 1.  */
2599
2600 int
2601 is_ancestor (struct type *base, struct type *dclass)
2602 {
2603   return distance_to_ancestor (base, dclass, 0) >= 0;
2604 }
2605
2606 /* Like is_ancestor, but only returns true when BASE is a public
2607    ancestor of DCLASS.  */
2608
2609 int
2610 is_public_ancestor (struct type *base, struct type *dclass)
2611 {
2612   return distance_to_ancestor (base, dclass, 1) >= 0;
2613 }
2614
2615 /* A helper function for is_unique_ancestor.  */
2616
2617 static int
2618 is_unique_ancestor_worker (struct type *base, struct type *dclass,
2619                            int *offset,
2620                            const gdb_byte *valaddr, int embedded_offset,
2621                            CORE_ADDR address, struct value *val)
2622 {
2623   int i, count = 0;
2624
2625   CHECK_TYPEDEF (base);
2626   CHECK_TYPEDEF (dclass);
2627
2628   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2629     {
2630       struct type *iter;
2631       int this_offset;
2632
2633       iter = check_typedef (TYPE_BASECLASS (dclass, i));
2634
2635       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2636                                       address, val);
2637
2638       if (class_types_same_p (base, iter))
2639         {
2640           /* If this is the first subclass, set *OFFSET and set count
2641              to 1.  Otherwise, if this is at the same offset as
2642              previous instances, do nothing.  Otherwise, increment
2643              count.  */
2644           if (*offset == -1)
2645             {
2646               *offset = this_offset;
2647               count = 1;
2648             }
2649           else if (this_offset == *offset)
2650             {
2651               /* Nothing.  */
2652             }
2653           else
2654             ++count;
2655         }
2656       else
2657         count += is_unique_ancestor_worker (base, iter, offset,
2658                                             valaddr,
2659                                             embedded_offset + this_offset,
2660                                             address, val);
2661     }
2662
2663   return count;
2664 }
2665
2666 /* Like is_ancestor, but only returns true if BASE is a unique base
2667    class of the type of VAL.  */
2668
2669 int
2670 is_unique_ancestor (struct type *base, struct value *val)
2671 {
2672   int offset = -1;
2673
2674   return is_unique_ancestor_worker (base, value_type (val), &offset,
2675                                     value_contents_for_printing (val),
2676                                     value_embedded_offset (val),
2677                                     value_address (val), val) == 1;
2678 }
2679
2680 \f
2681 /* Overload resolution.  */
2682
2683 /* Return the sum of the rank of A with the rank of B.  */
2684
2685 struct rank
2686 sum_ranks (struct rank a, struct rank b)
2687 {
2688   struct rank c;
2689   c.rank = a.rank + b.rank;
2690   c.subrank = a.subrank + b.subrank;
2691   return c;
2692 }
2693
2694 /* Compare rank A and B and return:
2695    0 if a = b
2696    1 if a is better than b
2697   -1 if b is better than a.  */
2698
2699 int
2700 compare_ranks (struct rank a, struct rank b)
2701 {
2702   if (a.rank == b.rank)
2703     {
2704       if (a.subrank == b.subrank)
2705         return 0;
2706       if (a.subrank < b.subrank)
2707         return 1;
2708       if (a.subrank > b.subrank)
2709         return -1;
2710     }
2711
2712   if (a.rank < b.rank)
2713     return 1;
2714
2715   /* a.rank > b.rank */
2716   return -1;
2717 }
2718
2719 /* Functions for overload resolution begin here.  */
2720
2721 /* Compare two badness vectors A and B and return the result.
2722    0 => A and B are identical
2723    1 => A and B are incomparable
2724    2 => A is better than B
2725    3 => A is worse than B  */
2726
2727 int
2728 compare_badness (struct badness_vector *a, struct badness_vector *b)
2729 {
2730   int i;
2731   int tmp;
2732   short found_pos = 0;          /* any positives in c? */
2733   short found_neg = 0;          /* any negatives in c? */
2734
2735   /* differing lengths => incomparable */
2736   if (a->length != b->length)
2737     return 1;
2738
2739   /* Subtract b from a */
2740   for (i = 0; i < a->length; i++)
2741     {
2742       tmp = compare_ranks (b->rank[i], a->rank[i]);
2743       if (tmp > 0)
2744         found_pos = 1;
2745       else if (tmp < 0)
2746         found_neg = 1;
2747     }
2748
2749   if (found_pos)
2750     {
2751       if (found_neg)
2752         return 1;               /* incomparable */
2753       else
2754         return 3;               /* A > B */
2755     }
2756   else
2757     /* no positives */
2758     {
2759       if (found_neg)
2760         return 2;               /* A < B */
2761       else
2762         return 0;               /* A == B */
2763     }
2764 }
2765
2766 /* Rank a function by comparing its parameter types (PARMS, length
2767    NPARMS), to the types of an argument list (ARGS, length NARGS).
2768    Return a pointer to a badness vector.  This has NARGS + 1
2769    entries.  */
2770
2771 struct badness_vector *
2772 rank_function (struct type **parms, int nparms, 
2773                struct value **args, int nargs)
2774 {
2775   int i;
2776   struct badness_vector *bv;
2777   int min_len = nparms < nargs ? nparms : nargs;
2778
2779   bv = xmalloc (sizeof (struct badness_vector));
2780   bv->length = nargs + 1;       /* add 1 for the length-match rank.  */
2781   bv->rank = XNEWVEC (struct rank, nargs + 1);
2782
2783   /* First compare the lengths of the supplied lists.
2784      If there is a mismatch, set it to a high value.  */
2785
2786   /* pai/1997-06-03 FIXME: when we have debug info about default
2787      arguments and ellipsis parameter lists, we should consider those
2788      and rank the length-match more finely.  */
2789
2790   LENGTH_MATCH (bv) = (nargs != nparms)
2791                       ? LENGTH_MISMATCH_BADNESS
2792                       : EXACT_MATCH_BADNESS;
2793
2794   /* Now rank all the parameters of the candidate function.  */
2795   for (i = 1; i <= min_len; i++)
2796     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2797                                  args[i - 1]);
2798
2799   /* If more arguments than parameters, add dummy entries.  */
2800   for (i = min_len + 1; i <= nargs; i++)
2801     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2802
2803   return bv;
2804 }
2805
2806 /* Compare the names of two integer types, assuming that any sign
2807    qualifiers have been checked already.  We do it this way because
2808    there may be an "int" in the name of one of the types.  */
2809
2810 static int
2811 integer_types_same_name_p (const char *first, const char *second)
2812 {
2813   int first_p, second_p;
2814
2815   /* If both are shorts, return 1; if neither is a short, keep
2816      checking.  */
2817   first_p = (strstr (first, "short") != NULL);
2818   second_p = (strstr (second, "short") != NULL);
2819   if (first_p && second_p)
2820     return 1;
2821   if (first_p || second_p)
2822     return 0;
2823
2824   /* Likewise for long.  */
2825   first_p = (strstr (first, "long") != NULL);
2826   second_p = (strstr (second, "long") != NULL);
2827   if (first_p && second_p)
2828     return 1;
2829   if (first_p || second_p)
2830     return 0;
2831
2832   /* Likewise for char.  */
2833   first_p = (strstr (first, "char") != NULL);
2834   second_p = (strstr (second, "char") != NULL);
2835   if (first_p && second_p)
2836     return 1;
2837   if (first_p || second_p)
2838     return 0;
2839
2840   /* They must both be ints.  */
2841   return 1;
2842 }
2843
2844 /* Compares type A to type B returns 1 if the represent the same type
2845    0 otherwise.  */
2846
2847 int
2848 types_equal (struct type *a, struct type *b)
2849 {
2850   /* Identical type pointers.  */
2851   /* However, this still doesn't catch all cases of same type for b
2852      and a.  The reason is that builtin types are different from
2853      the same ones constructed from the object.  */
2854   if (a == b)
2855     return 1;
2856
2857   /* Resolve typedefs */
2858   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2859     a = check_typedef (a);
2860   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2861     b = check_typedef (b);
2862
2863   /* If after resolving typedefs a and b are not of the same type
2864      code then they are not equal.  */
2865   if (TYPE_CODE (a) != TYPE_CODE (b))
2866     return 0;
2867
2868   /* If a and b are both pointers types or both reference types then
2869      they are equal of the same type iff the objects they refer to are
2870      of the same type.  */
2871   if (TYPE_CODE (a) == TYPE_CODE_PTR
2872       || TYPE_CODE (a) == TYPE_CODE_REF)
2873     return types_equal (TYPE_TARGET_TYPE (a),
2874                         TYPE_TARGET_TYPE (b));
2875
2876   /* Well, damnit, if the names are exactly the same, I'll say they
2877      are exactly the same.  This happens when we generate method
2878      stubs.  The types won't point to the same address, but they
2879      really are the same.  */
2880
2881   if (TYPE_NAME (a) && TYPE_NAME (b)
2882       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2883     return 1;
2884
2885   /* Check if identical after resolving typedefs.  */
2886   if (a == b)
2887     return 1;
2888
2889   /* Two function types are equal if their argument and return types
2890      are equal.  */
2891   if (TYPE_CODE (a) == TYPE_CODE_FUNC)
2892     {
2893       int i;
2894
2895       if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
2896         return 0;
2897       
2898       if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
2899         return 0;
2900
2901       for (i = 0; i < TYPE_NFIELDS (a); ++i)
2902         if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
2903           return 0;
2904
2905       return 1;
2906     }
2907
2908   return 0;
2909 }
2910 \f
2911 /* Deep comparison of types.  */
2912
2913 /* An entry in the type-equality bcache.  */
2914
2915 typedef struct type_equality_entry
2916 {
2917   struct type *type1, *type2;
2918 } type_equality_entry_d;
2919
2920 DEF_VEC_O (type_equality_entry_d);
2921
2922 /* A helper function to compare two strings.  Returns 1 if they are
2923    the same, 0 otherwise.  Handles NULLs properly.  */
2924
2925 static int
2926 compare_maybe_null_strings (const char *s, const char *t)
2927 {
2928   if (s == NULL && t != NULL)
2929     return 0;
2930   else if (s != NULL && t == NULL)
2931     return 0;
2932   else if (s == NULL && t== NULL)
2933     return 1;
2934   return strcmp (s, t) == 0;
2935 }
2936
2937 /* A helper function for check_types_worklist that checks two types for
2938    "deep" equality.  Returns non-zero if the types are considered the
2939    same, zero otherwise.  */
2940
2941 static int
2942 check_types_equal (struct type *type1, struct type *type2,
2943                    VEC (type_equality_entry_d) **worklist)
2944 {
2945   CHECK_TYPEDEF (type1);
2946   CHECK_TYPEDEF (type2);
2947
2948   if (type1 == type2)
2949     return 1;
2950
2951   if (TYPE_CODE (type1) != TYPE_CODE (type2)
2952       || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
2953       || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
2954       || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
2955       || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
2956       || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
2957       || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
2958       || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
2959       || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
2960     return 0;
2961
2962   if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
2963                                    TYPE_TAG_NAME (type2)))
2964     return 0;
2965   if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
2966     return 0;
2967
2968   if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
2969     {
2970       if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
2971                   sizeof (*TYPE_RANGE_DATA (type1))) != 0)
2972         return 0;
2973     }
2974   else
2975     {
2976       int i;
2977
2978       for (i = 0; i < TYPE_NFIELDS (type1); ++i)
2979         {
2980           const struct field *field1 = &TYPE_FIELD (type1, i);
2981           const struct field *field2 = &TYPE_FIELD (type2, i);
2982           struct type_equality_entry entry;
2983
2984           if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
2985               || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
2986               || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
2987             return 0;
2988           if (!compare_maybe_null_strings (FIELD_NAME (*field1),
2989                                            FIELD_NAME (*field2)))
2990             return 0;
2991           switch (FIELD_LOC_KIND (*field1))
2992             {
2993             case FIELD_LOC_KIND_BITPOS:
2994               if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
2995                 return 0;
2996               break;
2997             case FIELD_LOC_KIND_ENUMVAL:
2998               if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
2999                 return 0;
3000               break;
3001             case FIELD_LOC_KIND_PHYSADDR:
3002               if (FIELD_STATIC_PHYSADDR (*field1)
3003                   != FIELD_STATIC_PHYSADDR (*field2))
3004                 return 0;
3005               break;
3006             case FIELD_LOC_KIND_PHYSNAME:
3007               if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
3008                                                FIELD_STATIC_PHYSNAME (*field2)))
3009                 return 0;
3010               break;
3011             case FIELD_LOC_KIND_DWARF_BLOCK:
3012               {
3013                 struct dwarf2_locexpr_baton *block1, *block2;
3014
3015                 block1 = FIELD_DWARF_BLOCK (*field1);
3016                 block2 = FIELD_DWARF_BLOCK (*field2);
3017                 if (block1->per_cu != block2->per_cu
3018                     || block1->size != block2->size
3019                     || memcmp (block1->data, block2->data, block1->size) != 0)
3020                   return 0;
3021               }
3022               break;
3023             default:
3024               internal_error (__FILE__, __LINE__, _("Unsupported field kind "
3025                                                     "%d by check_types_equal"),
3026                               FIELD_LOC_KIND (*field1));
3027             }
3028
3029           entry.type1 = FIELD_TYPE (*field1);
3030           entry.type2 = FIELD_TYPE (*field2);
3031           VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3032         }
3033     }
3034
3035   if (TYPE_TARGET_TYPE (type1) != NULL)
3036     {
3037       struct type_equality_entry entry;
3038
3039       if (TYPE_TARGET_TYPE (type2) == NULL)
3040         return 0;
3041
3042       entry.type1 = TYPE_TARGET_TYPE (type1);
3043       entry.type2 = TYPE_TARGET_TYPE (type2);
3044       VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3045     }
3046   else if (TYPE_TARGET_TYPE (type2) != NULL)
3047     return 0;
3048
3049   return 1;
3050 }
3051
3052 /* Check types on a worklist for equality.  Returns zero if any pair
3053    is not equal, non-zero if they are all considered equal.  */
3054
3055 static int
3056 check_types_worklist (VEC (type_equality_entry_d) **worklist,
3057                       struct bcache *cache)
3058 {
3059   while (!VEC_empty (type_equality_entry_d, *worklist))
3060     {
3061       struct type_equality_entry entry;
3062       int added;
3063
3064       entry = *VEC_last (type_equality_entry_d, *worklist);
3065       VEC_pop (type_equality_entry_d, *worklist);
3066
3067       /* If the type pair has already been visited, we know it is
3068          ok.  */
3069       bcache_full (&entry, sizeof (entry), cache, &added);
3070       if (!added)
3071         continue;
3072
3073       if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
3074         return 0;
3075     }
3076
3077   return 1;
3078 }
3079
3080 /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
3081    "deep comparison".  Otherwise return zero.  */
3082
3083 int
3084 types_deeply_equal (struct type *type1, struct type *type2)
3085 {
3086   volatile struct gdb_exception except;
3087   int result = 0;
3088   struct bcache *cache;
3089   VEC (type_equality_entry_d) *worklist = NULL;
3090   struct type_equality_entry entry;
3091
3092   gdb_assert (type1 != NULL && type2 != NULL);
3093
3094   /* Early exit for the simple case.  */
3095   if (type1 == type2)
3096     return 1;
3097
3098   cache = bcache_xmalloc (NULL, NULL);
3099
3100   entry.type1 = type1;
3101   entry.type2 = type2;
3102   VEC_safe_push (type_equality_entry_d, worklist, &entry);
3103
3104   TRY_CATCH (except, RETURN_MASK_ALL)
3105     {
3106       result = check_types_worklist (&worklist, cache);
3107     }
3108   /* check_types_worklist calls several nested helper functions,
3109      some of which can raise a GDB Exception, so we just check
3110      and rethrow here.  If there is a GDB exception, a comparison
3111      is not capable (or trusted), so exit.  */
3112   bcache_xfree (cache);
3113   VEC_free (type_equality_entry_d, worklist);
3114   /* Rethrow if there was a problem.  */
3115   if (except.reason < 0)
3116     throw_exception (except);
3117
3118   return result;
3119 }
3120 \f
3121 /* Compare one type (PARM) for compatibility with another (ARG).
3122  * PARM is intended to be the parameter type of a function; and
3123  * ARG is the supplied argument's type.  This function tests if
3124  * the latter can be converted to the former.
3125  * VALUE is the argument's value or NULL if none (or called recursively)
3126  *
3127  * Return 0 if they are identical types;
3128  * Otherwise, return an integer which corresponds to how compatible
3129  * PARM is to ARG.  The higher the return value, the worse the match.
3130  * Generally the "bad" conversions are all uniformly assigned a 100.  */
3131
3132 struct rank
3133 rank_one_type (struct type *parm, struct type *arg, struct value *value)
3134 {
3135   struct rank rank = {0,0};
3136
3137   if (types_equal (parm, arg))
3138     return EXACT_MATCH_BADNESS;
3139
3140   /* Resolve typedefs */
3141   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
3142     parm = check_typedef (parm);
3143   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
3144     arg = check_typedef (arg);
3145
3146   /* See through references, since we can almost make non-references
3147      references.  */
3148   if (TYPE_CODE (arg) == TYPE_CODE_REF)
3149     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
3150                        REFERENCE_CONVERSION_BADNESS));
3151   if (TYPE_CODE (parm) == TYPE_CODE_REF)
3152     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
3153                        REFERENCE_CONVERSION_BADNESS));
3154   if (overload_debug)
3155   /* Debugging only.  */
3156     fprintf_filtered (gdb_stderr, 
3157                       "------ Arg is %s [%d], parm is %s [%d]\n",
3158                       TYPE_NAME (arg), TYPE_CODE (arg), 
3159                       TYPE_NAME (parm), TYPE_CODE (parm));
3160
3161   /* x -> y means arg of type x being supplied for parameter of type y.  */
3162
3163   switch (TYPE_CODE (parm))
3164     {
3165     case TYPE_CODE_PTR:
3166       switch (TYPE_CODE (arg))
3167         {
3168         case TYPE_CODE_PTR:
3169
3170           /* Allowed pointer conversions are:
3171              (a) pointer to void-pointer conversion.  */
3172           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
3173             return VOID_PTR_CONVERSION_BADNESS;
3174
3175           /* (b) pointer to ancestor-pointer conversion.  */
3176           rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
3177                                                TYPE_TARGET_TYPE (arg),
3178                                                0);
3179           if (rank.subrank >= 0)
3180             return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
3181
3182           return INCOMPATIBLE_TYPE_BADNESS;
3183         case TYPE_CODE_ARRAY:
3184           if (types_equal (TYPE_TARGET_TYPE (parm),
3185                            TYPE_TARGET_TYPE (arg)))
3186             return EXACT_MATCH_BADNESS;
3187           return INCOMPATIBLE_TYPE_BADNESS;
3188         case TYPE_CODE_FUNC:
3189           return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
3190         case TYPE_CODE_INT:
3191           if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
3192             {
3193               if (value_as_long (value) == 0)
3194                 {
3195                   /* Null pointer conversion: allow it to be cast to a pointer.
3196                      [4.10.1 of C++ standard draft n3290]  */
3197                   return NULL_POINTER_CONVERSION_BADNESS;
3198                 }
3199               else
3200                 {
3201                   /* If type checking is disabled, allow the conversion.  */
3202                   if (!strict_type_checking)
3203                     return NS_INTEGER_POINTER_CONVERSION_BADNESS;
3204                 }
3205             }
3206           /* fall through  */
3207         case TYPE_CODE_ENUM:
3208         case TYPE_CODE_FLAGS:
3209         case TYPE_CODE_CHAR:
3210         case TYPE_CODE_RANGE:
3211         case TYPE_CODE_BOOL:
3212         default:
3213           return INCOMPATIBLE_TYPE_BADNESS;
3214         }
3215     case TYPE_CODE_ARRAY:
3216       switch (TYPE_CODE (arg))
3217         {
3218         case TYPE_CODE_PTR:
3219         case TYPE_CODE_ARRAY:
3220           return rank_one_type (TYPE_TARGET_TYPE (parm), 
3221                                 TYPE_TARGET_TYPE (arg), NULL);
3222         default:
3223           return INCOMPATIBLE_TYPE_BADNESS;
3224         }
3225     case TYPE_CODE_FUNC:
3226       switch (TYPE_CODE (arg))
3227         {
3228         case TYPE_CODE_PTR:     /* funcptr -> func */
3229           return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
3230         default:
3231           return INCOMPATIBLE_TYPE_BADNESS;
3232         }
3233     case TYPE_CODE_INT:
3234       switch (TYPE_CODE (arg))
3235         {
3236         case TYPE_CODE_INT:
3237           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3238             {
3239               /* Deal with signed, unsigned, and plain chars and
3240                  signed and unsigned ints.  */
3241               if (TYPE_NOSIGN (parm))
3242                 {
3243                   /* This case only for character types.  */
3244                   if (TYPE_NOSIGN (arg))
3245                     return EXACT_MATCH_BADNESS; /* plain char -> plain char */
3246                   else          /* signed/unsigned char -> plain char */
3247                     return INTEGER_CONVERSION_BADNESS;
3248                 }
3249               else if (TYPE_UNSIGNED (parm))
3250                 {
3251                   if (TYPE_UNSIGNED (arg))
3252                     {
3253                       /* unsigned int -> unsigned int, or 
3254                          unsigned long -> unsigned long */
3255                       if (integer_types_same_name_p (TYPE_NAME (parm), 
3256                                                      TYPE_NAME (arg)))
3257                         return EXACT_MATCH_BADNESS;
3258                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
3259                                                           "int")
3260                                && integer_types_same_name_p (TYPE_NAME (parm),
3261                                                              "long"))
3262                         /* unsigned int -> unsigned long */
3263                         return INTEGER_PROMOTION_BADNESS;
3264                       else
3265                         /* unsigned long -> unsigned int */
3266                         return INTEGER_CONVERSION_BADNESS;
3267                     }
3268                   else
3269                     {
3270                       if (integer_types_same_name_p (TYPE_NAME (arg), 
3271                                                      "long")
3272                           && integer_types_same_name_p (TYPE_NAME (parm), 
3273                                                         "int"))
3274                         /* signed long -> unsigned int */
3275                         return INTEGER_CONVERSION_BADNESS;
3276                       else
3277                         /* signed int/long -> unsigned int/long */
3278                         return INTEGER_CONVERSION_BADNESS;
3279                     }
3280                 }
3281               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3282                 {
3283                   if (integer_types_same_name_p (TYPE_NAME (parm), 
3284                                                  TYPE_NAME (arg)))
3285                     return EXACT_MATCH_BADNESS;
3286                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
3287                                                       "int")
3288                            && integer_types_same_name_p (TYPE_NAME (parm), 
3289                                                          "long"))
3290                     return INTEGER_PROMOTION_BADNESS;
3291                   else
3292                     return INTEGER_CONVERSION_BADNESS;
3293                 }
3294               else
3295                 return INTEGER_CONVERSION_BADNESS;
3296             }
3297           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3298             return INTEGER_PROMOTION_BADNESS;
3299           else
3300             return INTEGER_CONVERSION_BADNESS;
3301         case TYPE_CODE_ENUM:
3302         case TYPE_CODE_FLAGS:
3303         case TYPE_CODE_CHAR:
3304         case TYPE_CODE_RANGE:
3305         case TYPE_CODE_BOOL:
3306           if (TYPE_DECLARED_CLASS (arg))
3307             return INCOMPATIBLE_TYPE_BADNESS;
3308           return INTEGER_PROMOTION_BADNESS;
3309         case TYPE_CODE_FLT:
3310           return INT_FLOAT_CONVERSION_BADNESS;
3311         case TYPE_CODE_PTR:
3312           return NS_POINTER_CONVERSION_BADNESS;
3313         default:
3314           return INCOMPATIBLE_TYPE_BADNESS;
3315         }
3316       break;
3317     case TYPE_CODE_ENUM:
3318       switch (TYPE_CODE (arg))
3319         {
3320         case TYPE_CODE_INT:
3321         case TYPE_CODE_CHAR:
3322         case TYPE_CODE_RANGE:
3323         case TYPE_CODE_BOOL:
3324         case TYPE_CODE_ENUM:
3325           if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg))
3326             return INCOMPATIBLE_TYPE_BADNESS;
3327           return INTEGER_CONVERSION_BADNESS;
3328         case TYPE_CODE_FLT:
3329           return INT_FLOAT_CONVERSION_BADNESS;
3330         default:
3331           return INCOMPATIBLE_TYPE_BADNESS;
3332         }
3333       break;
3334     case TYPE_CODE_CHAR:
3335       switch (TYPE_CODE (arg))
3336         {
3337         case TYPE_CODE_RANGE:
3338         case TYPE_CODE_BOOL:
3339         case TYPE_CODE_ENUM:
3340           if (TYPE_DECLARED_CLASS (arg))
3341             return INCOMPATIBLE_TYPE_BADNESS;
3342           return INTEGER_CONVERSION_BADNESS;
3343         case TYPE_CODE_FLT:
3344           return INT_FLOAT_CONVERSION_BADNESS;
3345         case TYPE_CODE_INT:
3346           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
3347             return INTEGER_CONVERSION_BADNESS;
3348           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3349             return INTEGER_PROMOTION_BADNESS;
3350           /* >>> !! else fall through !! <<< */
3351         case TYPE_CODE_CHAR:
3352           /* Deal with signed, unsigned, and plain chars for C++ and
3353              with int cases falling through from previous case.  */
3354           if (TYPE_NOSIGN (parm))
3355             {
3356               if (TYPE_NOSIGN (arg))
3357                 return EXACT_MATCH_BADNESS;
3358               else
3359                 return INTEGER_CONVERSION_BADNESS;
3360             }
3361           else if (TYPE_UNSIGNED (parm))
3362             {
3363               if (TYPE_UNSIGNED (arg))
3364                 return EXACT_MATCH_BADNESS;
3365               else
3366                 return INTEGER_PROMOTION_BADNESS;
3367             }
3368           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3369             return EXACT_MATCH_BADNESS;
3370           else
3371             return INTEGER_CONVERSION_BADNESS;
3372         default:
3373           return INCOMPATIBLE_TYPE_BADNESS;
3374         }
3375       break;
3376     case TYPE_CODE_RANGE:
3377       switch (TYPE_CODE (arg))
3378         {
3379         case TYPE_CODE_INT:
3380         case TYPE_CODE_CHAR:
3381         case TYPE_CODE_RANGE:
3382         case TYPE_CODE_BOOL:
3383         case TYPE_CODE_ENUM:
3384           return INTEGER_CONVERSION_BADNESS;
3385         case TYPE_CODE_FLT:
3386           return INT_FLOAT_CONVERSION_BADNESS;
3387         default:
3388           return INCOMPATIBLE_TYPE_BADNESS;
3389         }
3390       break;
3391     case TYPE_CODE_BOOL:
3392       switch (TYPE_CODE (arg))
3393         {
3394           /* n3290 draft, section 4.12.1 (conv.bool):
3395
3396              "A prvalue of arithmetic, unscoped enumeration, pointer, or
3397              pointer to member type can be converted to a prvalue of type
3398              bool.  A zero value, null pointer value, or null member pointer
3399              value is converted to false; any other value is converted to
3400              true.  A prvalue of type std::nullptr_t can be converted to a
3401              prvalue of type bool; the resulting value is false."  */
3402         case TYPE_CODE_INT:
3403         case TYPE_CODE_CHAR:
3404         case TYPE_CODE_ENUM:
3405         case TYPE_CODE_FLT:
3406         case TYPE_CODE_MEMBERPTR:
3407         case TYPE_CODE_PTR:
3408           return BOOL_CONVERSION_BADNESS;
3409         case TYPE_CODE_RANGE:
3410           return INCOMPATIBLE_TYPE_BADNESS;
3411         case TYPE_CODE_BOOL:
3412           return EXACT_MATCH_BADNESS;
3413         default:
3414           return INCOMPATIBLE_TYPE_BADNESS;
3415         }
3416       break;
3417     case TYPE_CODE_FLT:
3418       switch (TYPE_CODE (arg))
3419         {
3420         case TYPE_CODE_FLT:
3421           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3422             return FLOAT_PROMOTION_BADNESS;
3423           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3424             return EXACT_MATCH_BADNESS;
3425           else
3426             return FLOAT_CONVERSION_BADNESS;
3427         case TYPE_CODE_INT:
3428         case TYPE_CODE_BOOL:
3429         case TYPE_CODE_ENUM:
3430         case TYPE_CODE_RANGE:
3431         case TYPE_CODE_CHAR:
3432           return INT_FLOAT_CONVERSION_BADNESS;
3433         default:
3434           return INCOMPATIBLE_TYPE_BADNESS;
3435         }
3436       break;
3437     case TYPE_CODE_COMPLEX:
3438       switch (TYPE_CODE (arg))
3439         {               /* Strictly not needed for C++, but...  */
3440         case TYPE_CODE_FLT:
3441           return FLOAT_PROMOTION_BADNESS;
3442         case TYPE_CODE_COMPLEX:
3443           return EXACT_MATCH_BADNESS;
3444         default:
3445           return INCOMPATIBLE_TYPE_BADNESS;
3446         }
3447       break;
3448     case TYPE_CODE_STRUCT:
3449       switch (TYPE_CODE (arg))
3450         {
3451         case TYPE_CODE_STRUCT:
3452           /* Check for derivation */
3453           rank.subrank = distance_to_ancestor (parm, arg, 0);
3454           if (rank.subrank >= 0)
3455             return sum_ranks (BASE_CONVERSION_BADNESS, rank);
3456           /* else fall through */
3457         default:
3458           return INCOMPATIBLE_TYPE_BADNESS;
3459         }
3460       break;
3461     case TYPE_CODE_UNION:
3462       switch (TYPE_CODE (arg))
3463         {
3464         case TYPE_CODE_UNION:
3465         default:
3466           return INCOMPATIBLE_TYPE_BADNESS;
3467         }
3468       break;
3469     case TYPE_CODE_MEMBERPTR:
3470       switch (TYPE_CODE (arg))
3471         {
3472         default:
3473           return INCOMPATIBLE_TYPE_BADNESS;
3474         }
3475       break;
3476     case TYPE_CODE_METHOD:
3477       switch (TYPE_CODE (arg))
3478         {
3479
3480         default:
3481           return INCOMPATIBLE_TYPE_BADNESS;
3482         }
3483       break;
3484     case TYPE_CODE_REF:
3485       switch (TYPE_CODE (arg))
3486         {
3487
3488         default:
3489           return INCOMPATIBLE_TYPE_BADNESS;
3490         }
3491
3492       break;
3493     case TYPE_CODE_SET:
3494       switch (TYPE_CODE (arg))
3495         {
3496           /* Not in C++ */
3497         case TYPE_CODE_SET:
3498           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
3499                                 TYPE_FIELD_TYPE (arg, 0), NULL);
3500         default:
3501           return INCOMPATIBLE_TYPE_BADNESS;
3502         }
3503       break;
3504     case TYPE_CODE_VOID:
3505     default:
3506       return INCOMPATIBLE_TYPE_BADNESS;
3507     }                           /* switch (TYPE_CODE (arg)) */
3508 }
3509
3510 /* End of functions for overload resolution.  */
3511 \f
3512 /* Routines to pretty-print types.  */
3513
3514 static void
3515 print_bit_vector (B_TYPE *bits, int nbits)
3516 {
3517   int bitno;
3518
3519   for (bitno = 0; bitno < nbits; bitno++)
3520     {
3521       if ((bitno % 8) == 0)
3522         {
3523           puts_filtered (" ");
3524         }
3525       if (B_TST (bits, bitno))
3526         printf_filtered (("1"));
3527       else
3528         printf_filtered (("0"));
3529     }
3530 }
3531
3532 /* Note the first arg should be the "this" pointer, we may not want to
3533    include it since we may get into a infinitely recursive
3534    situation.  */
3535
3536 static void
3537 print_args (struct field *args, int nargs, int spaces)
3538 {
3539   if (args != NULL)
3540     {
3541       int i;
3542
3543       for (i = 0; i < nargs; i++)
3544         {
3545           printfi_filtered (spaces, "[%d] name '%s'\n", i,
3546                             args[i].name != NULL ? args[i].name : "<NULL>");
3547           recursive_dump_type (args[i].type, spaces + 2);
3548         }
3549     }
3550 }
3551
3552 int
3553 field_is_static (struct field *f)
3554 {
3555   /* "static" fields are the fields whose location is not relative
3556      to the address of the enclosing struct.  It would be nice to
3557      have a dedicated flag that would be set for static fields when
3558      the type is being created.  But in practice, checking the field
3559      loc_kind should give us an accurate answer.  */
3560   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3561           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3562 }
3563
3564 static void
3565 dump_fn_fieldlists (struct type *type, int spaces)
3566 {
3567   int method_idx;
3568   int overload_idx;
3569   struct fn_field *f;
3570
3571   printfi_filtered (spaces, "fn_fieldlists ");
3572   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
3573   printf_filtered ("\n");
3574   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3575     {
3576       f = TYPE_FN_FIELDLIST1 (type, method_idx);
3577       printfi_filtered (spaces + 2, "[%d] name '%s' (",
3578                         method_idx,
3579                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
3580       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3581                               gdb_stdout);
3582       printf_filtered (_(") length %d\n"),
3583                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3584       for (overload_idx = 0;
3585            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3586            overload_idx++)
3587         {
3588           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3589                             overload_idx,
3590                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
3591           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3592                                   gdb_stdout);
3593           printf_filtered (")\n");
3594           printfi_filtered (spaces + 8, "type ");
3595           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
3596                                   gdb_stdout);
3597           printf_filtered ("\n");
3598
3599           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3600                                spaces + 8 + 2);
3601
3602           printfi_filtered (spaces + 8, "args ");
3603           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
3604                                   gdb_stdout);
3605           printf_filtered ("\n");
3606           print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
3607                       TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)),
3608                       spaces + 8 + 2);
3609           printfi_filtered (spaces + 8, "fcontext ");
3610           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3611                                   gdb_stdout);
3612           printf_filtered ("\n");
3613
3614           printfi_filtered (spaces + 8, "is_const %d\n",
3615                             TYPE_FN_FIELD_CONST (f, overload_idx));
3616           printfi_filtered (spaces + 8, "is_volatile %d\n",
3617                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3618           printfi_filtered (spaces + 8, "is_private %d\n",
3619                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3620           printfi_filtered (spaces + 8, "is_protected %d\n",
3621                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3622           printfi_filtered (spaces + 8, "is_stub %d\n",
3623                             TYPE_FN_FIELD_STUB (f, overload_idx));
3624           printfi_filtered (spaces + 8, "voffset %u\n",
3625                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3626         }
3627     }
3628 }
3629
3630 static void
3631 print_cplus_stuff (struct type *type, int spaces)
3632 {
3633   printfi_filtered (spaces, "n_baseclasses %d\n",
3634                     TYPE_N_BASECLASSES (type));
3635   printfi_filtered (spaces, "nfn_fields %d\n",
3636                     TYPE_NFN_FIELDS (type));
3637   if (TYPE_N_BASECLASSES (type) > 0)
3638     {
3639       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3640                         TYPE_N_BASECLASSES (type));
3641       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
3642                               gdb_stdout);
3643       printf_filtered (")");
3644
3645       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3646                         TYPE_N_BASECLASSES (type));
3647       puts_filtered ("\n");
3648     }
3649   if (TYPE_NFIELDS (type) > 0)
3650     {
3651       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3652         {
3653           printfi_filtered (spaces, 
3654                             "private_field_bits (%d bits at *",
3655                             TYPE_NFIELDS (type));
3656           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
3657                                   gdb_stdout);
3658           printf_filtered (")");
3659           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3660                             TYPE_NFIELDS (type));
3661           puts_filtered ("\n");
3662         }
3663       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3664         {
3665           printfi_filtered (spaces, 
3666                             "protected_field_bits (%d bits at *",
3667                             TYPE_NFIELDS (type));
3668           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
3669                                   gdb_stdout);
3670           printf_filtered (")");
3671           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3672                             TYPE_NFIELDS (type));
3673           puts_filtered ("\n");
3674         }
3675     }
3676   if (TYPE_NFN_FIELDS (type) > 0)
3677     {
3678       dump_fn_fieldlists (type, spaces);
3679     }
3680 }
3681
3682 /* Print the contents of the TYPE's type_specific union, assuming that
3683    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */
3684
3685 static void
3686 print_gnat_stuff (struct type *type, int spaces)
3687 {
3688   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3689
3690   recursive_dump_type (descriptive_type, spaces + 2);
3691 }
3692
3693 static struct obstack dont_print_type_obstack;
3694
3695 void
3696 recursive_dump_type (struct type *type, int spaces)
3697 {
3698   int idx;
3699
3700   if (spaces == 0)
3701     obstack_begin (&dont_print_type_obstack, 0);
3702
3703   if (TYPE_NFIELDS (type) > 0
3704       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
3705     {
3706       struct type **first_dont_print
3707         = (struct type **) obstack_base (&dont_print_type_obstack);
3708
3709       int i = (struct type **) 
3710         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
3711
3712       while (--i >= 0)
3713         {
3714           if (type == first_dont_print[i])
3715             {
3716               printfi_filtered (spaces, "type node ");
3717               gdb_print_host_address (type, gdb_stdout);
3718               printf_filtered (_(" <same as already seen type>\n"));
3719               return;
3720             }
3721         }
3722
3723       obstack_ptr_grow (&dont_print_type_obstack, type);
3724     }
3725
3726   printfi_filtered (spaces, "type node ");
3727   gdb_print_host_address (type, gdb_stdout);
3728   printf_filtered ("\n");
3729   printfi_filtered (spaces, "name '%s' (",
3730                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
3731   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
3732   printf_filtered (")\n");
3733   printfi_filtered (spaces, "tagname '%s' (",
3734                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
3735   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
3736   printf_filtered (")\n");
3737   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
3738   switch (TYPE_CODE (type))
3739     {
3740     case TYPE_CODE_UNDEF:
3741       printf_filtered ("(TYPE_CODE_UNDEF)");
3742       break;
3743     case TYPE_CODE_PTR:
3744       printf_filtered ("(TYPE_CODE_PTR)");
3745       break;
3746     case TYPE_CODE_ARRAY:
3747       printf_filtered ("(TYPE_CODE_ARRAY)");
3748       break;
3749     case TYPE_CODE_STRUCT:
3750       printf_filtered ("(TYPE_CODE_STRUCT)");
3751       break;
3752     case TYPE_CODE_UNION:
3753       printf_filtered ("(TYPE_CODE_UNION)");
3754       break;
3755     case TYPE_CODE_ENUM:
3756       printf_filtered ("(TYPE_CODE_ENUM)");
3757       break;
3758     case TYPE_CODE_FLAGS:
3759       printf_filtered ("(TYPE_CODE_FLAGS)");
3760       break;
3761     case TYPE_CODE_FUNC:
3762       printf_filtered ("(TYPE_CODE_FUNC)");
3763       break;
3764     case TYPE_CODE_INT:
3765       printf_filtered ("(TYPE_CODE_INT)");
3766       break;
3767     case TYPE_CODE_FLT:
3768       printf_filtered ("(TYPE_CODE_FLT)");
3769       break;
3770     case TYPE_CODE_VOID:
3771       printf_filtered ("(TYPE_CODE_VOID)");
3772       break;
3773     case TYPE_CODE_SET:
3774       printf_filtered ("(TYPE_CODE_SET)");
3775       break;
3776     case TYPE_CODE_RANGE:
3777       printf_filtered ("(TYPE_CODE_RANGE)");
3778       break;
3779     case TYPE_CODE_STRING:
3780       printf_filtered ("(TYPE_CODE_STRING)");
3781       break;
3782     case TYPE_CODE_ERROR:
3783       printf_filtered ("(TYPE_CODE_ERROR)");
3784       break;
3785     case TYPE_CODE_MEMBERPTR:
3786       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
3787       break;
3788     case TYPE_CODE_METHODPTR:
3789       printf_filtered ("(TYPE_CODE_METHODPTR)");
3790       break;
3791     case TYPE_CODE_METHOD:
3792       printf_filtered ("(TYPE_CODE_METHOD)");
3793       break;
3794     case TYPE_CODE_REF:
3795       printf_filtered ("(TYPE_CODE_REF)");
3796       break;
3797     case TYPE_CODE_CHAR:
3798       printf_filtered ("(TYPE_CODE_CHAR)");
3799       break;
3800     case TYPE_CODE_BOOL:
3801       printf_filtered ("(TYPE_CODE_BOOL)");
3802       break;
3803     case TYPE_CODE_COMPLEX:
3804       printf_filtered ("(TYPE_CODE_COMPLEX)");
3805       break;
3806     case TYPE_CODE_TYPEDEF:
3807       printf_filtered ("(TYPE_CODE_TYPEDEF)");
3808       break;
3809     case TYPE_CODE_NAMESPACE:
3810       printf_filtered ("(TYPE_CODE_NAMESPACE)");
3811       break;
3812     default:
3813       printf_filtered ("(UNKNOWN TYPE CODE)");
3814       break;
3815     }
3816   puts_filtered ("\n");
3817   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
3818   if (TYPE_OBJFILE_OWNED (type))
3819     {
3820       printfi_filtered (spaces, "objfile ");
3821       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
3822     }
3823   else
3824     {
3825       printfi_filtered (spaces, "gdbarch ");
3826       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
3827     }
3828   printf_filtered ("\n");
3829   printfi_filtered (spaces, "target_type ");
3830   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
3831   printf_filtered ("\n");
3832   if (TYPE_TARGET_TYPE (type) != NULL)
3833     {
3834       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3835     }
3836   printfi_filtered (spaces, "pointer_type ");
3837   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
3838   printf_filtered ("\n");
3839   printfi_filtered (spaces, "reference_type ");
3840   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
3841   printf_filtered ("\n");
3842   printfi_filtered (spaces, "type_chain ");
3843   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3844   printf_filtered ("\n");
3845   printfi_filtered (spaces, "instance_flags 0x%x", 
3846                     TYPE_INSTANCE_FLAGS (type));
3847   if (TYPE_CONST (type))
3848     {
3849       puts_filtered (" TYPE_FLAG_CONST");
3850     }
3851   if (TYPE_VOLATILE (type))
3852     {
3853       puts_filtered (" TYPE_FLAG_VOLATILE");
3854     }
3855   if (TYPE_CODE_SPACE (type))
3856     {
3857       puts_filtered (" TYPE_FLAG_CODE_SPACE");
3858     }
3859   if (TYPE_DATA_SPACE (type))
3860     {
3861       puts_filtered (" TYPE_FLAG_DATA_SPACE");
3862     }
3863   if (TYPE_ADDRESS_CLASS_1 (type))
3864     {
3865       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3866     }
3867   if (TYPE_ADDRESS_CLASS_2 (type))
3868     {
3869       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3870     }
3871   if (TYPE_RESTRICT (type))
3872     {
3873       puts_filtered (" TYPE_FLAG_RESTRICT");
3874     }
3875   puts_filtered ("\n");
3876
3877   printfi_filtered (spaces, "flags");
3878   if (TYPE_UNSIGNED (type))
3879     {
3880       puts_filtered (" TYPE_FLAG_UNSIGNED");
3881     }
3882   if (TYPE_NOSIGN (type))
3883     {
3884       puts_filtered (" TYPE_FLAG_NOSIGN");
3885     }
3886   if (TYPE_STUB (type))
3887     {
3888       puts_filtered (" TYPE_FLAG_STUB");
3889     }
3890   if (TYPE_TARGET_STUB (type))
3891     {
3892       puts_filtered (" TYPE_FLAG_TARGET_STUB");
3893     }
3894   if (TYPE_STATIC (type))
3895     {
3896       puts_filtered (" TYPE_FLAG_STATIC");
3897     }
3898   if (TYPE_PROTOTYPED (type))
3899     {
3900       puts_filtered (" TYPE_FLAG_PROTOTYPED");
3901     }
3902   if (TYPE_INCOMPLETE (type))
3903     {
3904       puts_filtered (" TYPE_FLAG_INCOMPLETE");
3905     }
3906   if (TYPE_VARARGS (type))
3907     {
3908       puts_filtered (" TYPE_FLAG_VARARGS");
3909     }
3910   /* This is used for things like AltiVec registers on ppc.  Gcc emits
3911      an attribute for the array type, which tells whether or not we
3912      have a vector, instead of a regular array.  */
3913   if (TYPE_VECTOR (type))
3914     {
3915       puts_filtered (" TYPE_FLAG_VECTOR");
3916     }
3917   if (TYPE_FIXED_INSTANCE (type))
3918     {
3919       puts_filtered (" TYPE_FIXED_INSTANCE");
3920     }
3921   if (TYPE_STUB_SUPPORTED (type))
3922     {
3923       puts_filtered (" TYPE_STUB_SUPPORTED");
3924     }
3925   if (TYPE_NOTTEXT (type))
3926     {
3927       puts_filtered (" TYPE_NOTTEXT");
3928     }
3929   puts_filtered ("\n");
3930   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3931   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3932   puts_filtered ("\n");
3933   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3934     {
3935       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3936         printfi_filtered (spaces + 2,
3937                           "[%d] enumval %s type ",
3938                           idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3939       else
3940         printfi_filtered (spaces + 2,
3941                           "[%d] bitpos %d bitsize %d type ",
3942                           idx, TYPE_FIELD_BITPOS (type, idx),
3943                           TYPE_FIELD_BITSIZE (type, idx));
3944       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3945       printf_filtered (" name '%s' (",
3946                        TYPE_FIELD_NAME (type, idx) != NULL
3947                        ? TYPE_FIELD_NAME (type, idx)
3948                        : "<NULL>");
3949       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3950       printf_filtered (")\n");
3951       if (TYPE_FIELD_TYPE (type, idx) != NULL)
3952         {
3953           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3954         }
3955     }
3956   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3957     {
3958       printfi_filtered (spaces, "low %s%s  high %s%s\n",
3959                         plongest (TYPE_LOW_BOUND (type)), 
3960                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3961                         plongest (TYPE_HIGH_BOUND (type)),
3962                         TYPE_HIGH_BOUND_UNDEFINED (type) 
3963                         ? " (undefined)" : "");
3964     }
3965   printfi_filtered (spaces, "vptr_basetype ");
3966   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3967   puts_filtered ("\n");
3968   if (TYPE_VPTR_BASETYPE (type) != NULL)
3969     {
3970       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3971     }
3972   printfi_filtered (spaces, "vptr_fieldno %d\n", 
3973                     TYPE_VPTR_FIELDNO (type));
3974
3975   switch (TYPE_SPECIFIC_FIELD (type))
3976     {
3977       case TYPE_SPECIFIC_CPLUS_STUFF:
3978         printfi_filtered (spaces, "cplus_stuff ");
3979         gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
3980                                 gdb_stdout);
3981         puts_filtered ("\n");
3982         print_cplus_stuff (type, spaces);
3983         break;
3984
3985       case TYPE_SPECIFIC_GNAT_STUFF:
3986         printfi_filtered (spaces, "gnat_stuff ");
3987         gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3988         puts_filtered ("\n");
3989         print_gnat_stuff (type, spaces);
3990         break;
3991
3992       case TYPE_SPECIFIC_FLOATFORMAT:
3993         printfi_filtered (spaces, "floatformat ");
3994         if (TYPE_FLOATFORMAT (type) == NULL)
3995           puts_filtered ("(null)");
3996         else
3997           {
3998             puts_filtered ("{ ");
3999             if (TYPE_FLOATFORMAT (type)[0] == NULL
4000                 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
4001               puts_filtered ("(null)");
4002             else
4003               puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
4004
4005             puts_filtered (", ");
4006             if (TYPE_FLOATFORMAT (type)[1] == NULL
4007                 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
4008               puts_filtered ("(null)");
4009             else
4010               puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
4011
4012             puts_filtered (" }");
4013           }
4014         puts_filtered ("\n");
4015         break;
4016
4017       case TYPE_SPECIFIC_FUNC:
4018         printfi_filtered (spaces, "calling_convention %d\n",
4019                           TYPE_CALLING_CONVENTION (type));
4020         /* tail_call_list is not printed.  */
4021         break;
4022     }
4023
4024   if (spaces == 0)
4025     obstack_free (&dont_print_type_obstack, NULL);
4026 }
4027 \f
4028 /* Trivial helpers for the libiberty hash table, for mapping one
4029    type to another.  */
4030
4031 struct type_pair
4032 {
4033   struct type *old, *new;
4034 };
4035
4036 static hashval_t
4037 type_pair_hash (const void *item)
4038 {
4039   const struct type_pair *pair = item;
4040
4041   return htab_hash_pointer (pair->old);
4042 }
4043
4044 static int
4045 type_pair_eq (const void *item_lhs, const void *item_rhs)
4046 {
4047   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
4048
4049   return lhs->old == rhs->old;
4050 }
4051
4052 /* Allocate the hash table used by copy_type_recursive to walk
4053    types without duplicates.  We use OBJFILE's obstack, because
4054    OBJFILE is about to be deleted.  */
4055
4056 htab_t
4057 create_copied_types_hash (struct objfile *objfile)
4058 {
4059   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
4060                                NULL, &objfile->objfile_obstack,
4061                                hashtab_obstack_allocate,
4062                                dummy_obstack_deallocate);
4063 }
4064
4065 /* Recursively copy (deep copy) TYPE, if it is associated with
4066    OBJFILE.  Return a new type allocated using malloc, a saved type if
4067    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
4068    not associated with OBJFILE.  */
4069
4070 struct type *
4071 copy_type_recursive (struct objfile *objfile, 
4072                      struct type *type,
4073                      htab_t copied_types)
4074 {
4075   struct type_pair *stored, pair;
4076   void **slot;
4077   struct type *new_type;
4078
4079   if (! TYPE_OBJFILE_OWNED (type))
4080     return type;
4081
4082   /* This type shouldn't be pointing to any types in other objfiles;
4083      if it did, the type might disappear unexpectedly.  */
4084   gdb_assert (TYPE_OBJFILE (type) == objfile);
4085
4086   pair.old = type;
4087   slot = htab_find_slot (copied_types, &pair, INSERT);
4088   if (*slot != NULL)
4089     return ((struct type_pair *) *slot)->new;
4090
4091   new_type = alloc_type_arch (get_type_arch (type));
4092
4093   /* We must add the new type to the hash table immediately, in case
4094      we encounter this type again during a recursive call below.  */
4095   stored
4096     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
4097   stored->old = type;
4098   stored->new = new_type;
4099   *slot = stored;
4100
4101   /* Copy the common fields of types.  For the main type, we simply
4102      copy the entire thing and then update specific fields as needed.  */
4103   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
4104   TYPE_OBJFILE_OWNED (new_type) = 0;
4105   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
4106
4107   if (TYPE_NAME (type))
4108     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
4109   if (TYPE_TAG_NAME (type))
4110     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
4111
4112   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4113   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4114
4115   /* Copy the fields.  */
4116   if (TYPE_NFIELDS (type))
4117     {
4118       int i, nfields;
4119
4120       nfields = TYPE_NFIELDS (type);
4121       TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
4122       for (i = 0; i < nfields; i++)
4123         {
4124           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
4125             TYPE_FIELD_ARTIFICIAL (type, i);
4126           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
4127           if (TYPE_FIELD_TYPE (type, i))
4128             TYPE_FIELD_TYPE (new_type, i)
4129               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
4130                                      copied_types);
4131           if (TYPE_FIELD_NAME (type, i))
4132             TYPE_FIELD_NAME (new_type, i) = 
4133               xstrdup (TYPE_FIELD_NAME (type, i));
4134           switch (TYPE_FIELD_LOC_KIND (type, i))
4135             {
4136             case FIELD_LOC_KIND_BITPOS:
4137               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
4138                                 TYPE_FIELD_BITPOS (type, i));
4139               break;
4140             case FIELD_LOC_KIND_ENUMVAL:
4141               SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
4142                                  TYPE_FIELD_ENUMVAL (type, i));
4143               break;
4144             case FIELD_LOC_KIND_PHYSADDR:
4145               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
4146                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
4147               break;
4148             case FIELD_LOC_KIND_PHYSNAME:
4149               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
4150                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
4151                                                                        i)));
4152               break;
4153             default:
4154               internal_error (__FILE__, __LINE__,
4155                               _("Unexpected type field location kind: %d"),
4156                               TYPE_FIELD_LOC_KIND (type, i));
4157             }
4158         }
4159     }
4160
4161   /* For range types, copy the bounds information.  */
4162   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
4163     {
4164       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
4165       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
4166     }
4167
4168   /* Copy the data location information.  */
4169   if (TYPE_DATA_LOCATION (type) != NULL)
4170     {
4171       TYPE_DATA_LOCATION (new_type)
4172         = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
4173       memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
4174               sizeof (struct dynamic_prop));
4175     }
4176
4177   /* Copy pointers to other types.  */
4178   if (TYPE_TARGET_TYPE (type))
4179     TYPE_TARGET_TYPE (new_type) = 
4180       copy_type_recursive (objfile, 
4181                            TYPE_TARGET_TYPE (type),
4182                            copied_types);
4183   if (TYPE_VPTR_BASETYPE (type))
4184     TYPE_VPTR_BASETYPE (new_type) = 
4185       copy_type_recursive (objfile,
4186                            TYPE_VPTR_BASETYPE (type),
4187                            copied_types);
4188   /* Maybe copy the type_specific bits.
4189
4190      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
4191      base classes and methods.  There's no fundamental reason why we
4192      can't, but at the moment it is not needed.  */
4193
4194   if (TYPE_CODE (type) == TYPE_CODE_FLT)
4195     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
4196   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4197            || TYPE_CODE (type) == TYPE_CODE_UNION
4198            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
4199     INIT_CPLUS_SPECIFIC (new_type);
4200
4201   return new_type;
4202 }
4203
4204 /* Make a copy of the given TYPE, except that the pointer & reference
4205    types are not preserved.
4206    
4207    This function assumes that the given type has an associated objfile.
4208    This objfile is used to allocate the new type.  */
4209
4210 struct type *
4211 copy_type (const struct type *type)
4212 {
4213   struct type *new_type;
4214
4215   gdb_assert (TYPE_OBJFILE_OWNED (type));
4216
4217   new_type = alloc_type_copy (type);
4218   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4219   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4220   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
4221           sizeof (struct main_type));
4222   if (TYPE_DATA_LOCATION (type) != NULL)
4223     {
4224       TYPE_DATA_LOCATION (new_type)
4225         = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
4226       memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
4227               sizeof (struct dynamic_prop));
4228     }
4229
4230   return new_type;
4231 }
4232 \f
4233 /* Helper functions to initialize architecture-specific types.  */
4234
4235 /* Allocate a type structure associated with GDBARCH and set its
4236    CODE, LENGTH, and NAME fields.  */
4237
4238 struct type *
4239 arch_type (struct gdbarch *gdbarch,
4240            enum type_code code, int length, char *name)
4241 {
4242   struct type *type;
4243
4244   type = alloc_type_arch (gdbarch);
4245   TYPE_CODE (type) = code;
4246   TYPE_LENGTH (type) = length;
4247
4248   if (name)
4249     TYPE_NAME (type) = xstrdup (name);
4250
4251   return type;
4252 }
4253
4254 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
4255    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4256    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4257
4258 struct type *
4259 arch_integer_type (struct gdbarch *gdbarch,
4260                    int bit, int unsigned_p, char *name)
4261 {
4262   struct type *t;
4263
4264   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
4265   if (unsigned_p)
4266     TYPE_UNSIGNED (t) = 1;
4267   if (name && strcmp (name, "char") == 0)
4268     TYPE_NOSIGN (t) = 1;
4269
4270   return t;
4271 }
4272
4273 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
4274    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4275    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4276
4277 struct type *
4278 arch_character_type (struct gdbarch *gdbarch,
4279                      int bit, int unsigned_p, char *name)
4280 {
4281   struct type *t;
4282
4283   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
4284   if (unsigned_p)
4285     TYPE_UNSIGNED (t) = 1;
4286
4287   return t;
4288 }
4289
4290 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
4291    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4292    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4293
4294 struct type *
4295 arch_boolean_type (struct gdbarch *gdbarch,
4296                    int bit, int unsigned_p, char *name)
4297 {
4298   struct type *t;
4299
4300   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
4301   if (unsigned_p)
4302     TYPE_UNSIGNED (t) = 1;
4303
4304   return t;
4305 }
4306
4307 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
4308    BIT is the type size in bits; if BIT equals -1, the size is
4309    determined by the floatformat.  NAME is the type name.  Set the
4310    TYPE_FLOATFORMAT from FLOATFORMATS.  */
4311
4312 struct type *
4313 arch_float_type (struct gdbarch *gdbarch,
4314                  int bit, char *name, const struct floatformat **floatformats)
4315 {
4316   struct type *t;
4317
4318   if (bit == -1)
4319     {
4320       gdb_assert (floatformats != NULL);
4321       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
4322       bit = floatformats[0]->totalsize;
4323     }
4324   gdb_assert (bit >= 0);
4325
4326   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
4327   TYPE_FLOATFORMAT (t) = floatformats;
4328   return t;
4329 }
4330
4331 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
4332    NAME is the type name.  TARGET_TYPE is the component float type.  */
4333
4334 struct type *
4335 arch_complex_type (struct gdbarch *gdbarch,
4336                    char *name, struct type *target_type)
4337 {
4338   struct type *t;
4339
4340   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
4341                  2 * TYPE_LENGTH (target_type), name);
4342   TYPE_TARGET_TYPE (t) = target_type;
4343   return t;
4344 }
4345
4346 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
4347    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */
4348
4349 struct type *
4350 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
4351 {
4352   int nfields = length * TARGET_CHAR_BIT;
4353   struct type *type;
4354
4355   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
4356   TYPE_UNSIGNED (type) = 1;
4357   TYPE_NFIELDS (type) = nfields;
4358   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
4359
4360   return type;
4361 }
4362
4363 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
4364    position BITPOS is called NAME.  */
4365
4366 void
4367 append_flags_type_flag (struct type *type, int bitpos, char *name)
4368 {
4369   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
4370   gdb_assert (bitpos < TYPE_NFIELDS (type));
4371   gdb_assert (bitpos >= 0);
4372
4373   if (name)
4374     {
4375       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
4376       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
4377     }
4378   else
4379     {
4380       /* Don't show this field to the user.  */
4381       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
4382     }
4383 }
4384
4385 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
4386    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
4387
4388 struct type *
4389 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
4390 {
4391   struct type *t;
4392
4393   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
4394   t = arch_type (gdbarch, code, 0, NULL);
4395   TYPE_TAG_NAME (t) = name;
4396   INIT_CPLUS_SPECIFIC (t);
4397   return t;
4398 }
4399
4400 /* Add new field with name NAME and type FIELD to composite type T.
4401    Do not set the field's position or adjust the type's length;
4402    the caller should do so.  Return the new field.  */
4403
4404 struct field *
4405 append_composite_type_field_raw (struct type *t, char *name,
4406                                  struct type *field)
4407 {
4408   struct field *f;
4409
4410   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
4411   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
4412                               sizeof (struct field) * TYPE_NFIELDS (t));
4413   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
4414   memset (f, 0, sizeof f[0]);
4415   FIELD_TYPE (f[0]) = field;
4416   FIELD_NAME (f[0]) = name;
4417   return f;
4418 }
4419
4420 /* Add new field with name NAME and type FIELD to composite type T.
4421    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
4422
4423 void
4424 append_composite_type_field_aligned (struct type *t, char *name,
4425                                      struct type *field, int alignment)
4426 {
4427   struct field *f = append_composite_type_field_raw (t, name, field);
4428
4429   if (TYPE_CODE (t) == TYPE_CODE_UNION)
4430     {
4431       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
4432         TYPE_LENGTH (t) = TYPE_LENGTH (field);
4433     }
4434   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4435     {
4436       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4437       if (TYPE_NFIELDS (t) > 1)
4438         {
4439           SET_FIELD_BITPOS (f[0],
4440                             (FIELD_BITPOS (f[-1])
4441                              + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4442                                 * TARGET_CHAR_BIT)));
4443
4444           if (alignment)
4445             {
4446               int left;
4447
4448               alignment *= TARGET_CHAR_BIT;
4449               left = FIELD_BITPOS (f[0]) % alignment;
4450
4451               if (left)
4452                 {
4453                   SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
4454                   TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
4455                 }
4456             }
4457         }
4458     }
4459 }
4460
4461 /* Add new field with name NAME and type FIELD to composite type T.  */
4462
4463 void
4464 append_composite_type_field (struct type *t, char *name,
4465                              struct type *field)
4466 {
4467   append_composite_type_field_aligned (t, name, field, 0);
4468 }
4469
4470 static struct gdbarch_data *gdbtypes_data;
4471
4472 const struct builtin_type *
4473 builtin_type (struct gdbarch *gdbarch)
4474 {
4475   return gdbarch_data (gdbarch, gdbtypes_data);
4476 }
4477
4478 static void *
4479 gdbtypes_post_init (struct gdbarch *gdbarch)
4480 {
4481   struct builtin_type *builtin_type
4482     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4483
4484   /* Basic types.  */
4485   builtin_type->builtin_void
4486     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4487   builtin_type->builtin_char
4488     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4489                          !gdbarch_char_signed (gdbarch), "char");
4490   builtin_type->builtin_signed_char
4491     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4492                          0, "signed char");
4493   builtin_type->builtin_unsigned_char
4494     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4495                          1, "unsigned char");
4496   builtin_type->builtin_short
4497     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4498                          0, "short");
4499   builtin_type->builtin_unsigned_short
4500     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4501                          1, "unsigned short");
4502   builtin_type->builtin_int
4503     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4504                          0, "int");
4505   builtin_type->builtin_unsigned_int
4506     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4507                          1, "unsigned int");
4508   builtin_type->builtin_long
4509     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4510                          0, "long");
4511   builtin_type->builtin_unsigned_long
4512     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4513                          1, "unsigned long");
4514   builtin_type->builtin_long_long
4515     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4516                          0, "long long");
4517   builtin_type->builtin_unsigned_long_long
4518     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4519                          1, "unsigned long long");
4520   builtin_type->builtin_float
4521     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
4522                        "float", gdbarch_float_format (gdbarch));
4523   builtin_type->builtin_double
4524     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
4525                        "double", gdbarch_double_format (gdbarch));
4526   builtin_type->builtin_long_double
4527     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
4528                        "long double", gdbarch_long_double_format (gdbarch));
4529   builtin_type->builtin_complex
4530     = arch_complex_type (gdbarch, "complex",
4531                          builtin_type->builtin_float);
4532   builtin_type->builtin_double_complex
4533     = arch_complex_type (gdbarch, "double complex",
4534                          builtin_type->builtin_double);
4535   builtin_type->builtin_string
4536     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4537   builtin_type->builtin_bool
4538     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
4539
4540   /* The following three are about decimal floating point types, which
4541      are 32-bits, 64-bits and 128-bits respectively.  */
4542   builtin_type->builtin_decfloat
4543     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
4544   builtin_type->builtin_decdouble
4545     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
4546   builtin_type->builtin_declong
4547     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
4548
4549   /* "True" character types.  */
4550   builtin_type->builtin_true_char
4551     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4552   builtin_type->builtin_true_unsigned_char
4553     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
4554
4555   /* Fixed-size integer types.  */
4556   builtin_type->builtin_int0
4557     = arch_integer_type (gdbarch, 0, 0, "int0_t");
4558   builtin_type->builtin_int8
4559     = arch_integer_type (gdbarch, 8, 0, "int8_t");
4560   builtin_type->builtin_uint8
4561     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4562   builtin_type->builtin_int16
4563     = arch_integer_type (gdbarch, 16, 0, "int16_t");
4564   builtin_type->builtin_uint16
4565     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4566   builtin_type->builtin_int32
4567     = arch_integer_type (gdbarch, 32, 0, "int32_t");
4568   builtin_type->builtin_uint32
4569     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4570   builtin_type->builtin_int64
4571     = arch_integer_type (gdbarch, 64, 0, "int64_t");
4572   builtin_type->builtin_uint64
4573     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4574   builtin_type->builtin_int128
4575     = arch_integer_type (gdbarch, 128, 0, "int128_t");
4576   builtin_type->builtin_uint128
4577     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
4578   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4579     TYPE_INSTANCE_FLAG_NOTTEXT;
4580   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4581     TYPE_INSTANCE_FLAG_NOTTEXT;
4582
4583   /* Wide character types.  */
4584   builtin_type->builtin_char16
4585     = arch_integer_type (gdbarch, 16, 0, "char16_t");
4586   builtin_type->builtin_char32
4587     = arch_integer_type (gdbarch, 32, 0, "char32_t");
4588         
4589
4590   /* Default data/code pointer types.  */
4591   builtin_type->builtin_data_ptr
4592     = lookup_pointer_type (builtin_type->builtin_void);
4593   builtin_type->builtin_func_ptr
4594     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
4595   builtin_type->builtin_func_func
4596     = lookup_function_type (builtin_type->builtin_func_ptr);
4597
4598   /* This type represents a GDB internal function.  */
4599   builtin_type->internal_fn
4600     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4601                  "<internal function>");
4602
4603   /* This type represents an xmethod.  */
4604   builtin_type->xmethod
4605     = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
4606
4607   return builtin_type;
4608 }
4609
4610 /* This set of objfile-based types is intended to be used by symbol
4611    readers as basic types.  */
4612
4613 static const struct objfile_data *objfile_type_data;
4614
4615 const struct objfile_type *
4616 objfile_type (struct objfile *objfile)
4617 {
4618   struct gdbarch *gdbarch;
4619   struct objfile_type *objfile_type
4620     = objfile_data (objfile, objfile_type_data);
4621
4622   if (objfile_type)
4623     return objfile_type;
4624
4625   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4626                                  1, struct objfile_type);
4627
4628   /* Use the objfile architecture to determine basic type properties.  */
4629   gdbarch = get_objfile_arch (objfile);
4630
4631   /* Basic types.  */
4632   objfile_type->builtin_void
4633     = init_type (TYPE_CODE_VOID, 1,
4634                  0,
4635                  "void", objfile);
4636
4637   objfile_type->builtin_char
4638     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4639                  (TYPE_FLAG_NOSIGN
4640                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4641                  "char", objfile);
4642   objfile_type->builtin_signed_char
4643     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4644                  0,
4645                  "signed char", objfile);
4646   objfile_type->builtin_unsigned_char
4647     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4648                  TYPE_FLAG_UNSIGNED,
4649                  "unsigned char", objfile);
4650   objfile_type->builtin_short
4651     = init_type (TYPE_CODE_INT,
4652                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4653                  0, "short", objfile);
4654   objfile_type->builtin_unsigned_short
4655     = init_type (TYPE_CODE_INT,
4656                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4657                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4658   objfile_type->builtin_int
4659     = init_type (TYPE_CODE_INT,
4660                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4661                  0, "int", objfile);
4662   objfile_type->builtin_unsigned_int
4663     = init_type (TYPE_CODE_INT,
4664                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4665                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4666   objfile_type->builtin_long
4667     = init_type (TYPE_CODE_INT,
4668                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4669                  0, "long", objfile);
4670   objfile_type->builtin_unsigned_long
4671     = init_type (TYPE_CODE_INT,
4672                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4673                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4674   objfile_type->builtin_long_long
4675     = init_type (TYPE_CODE_INT,
4676                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4677                  0, "long long", objfile);
4678   objfile_type->builtin_unsigned_long_long
4679     = init_type (TYPE_CODE_INT,
4680                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4681                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4682
4683   objfile_type->builtin_float
4684     = init_type (TYPE_CODE_FLT,
4685                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4686                  0, "float", objfile);
4687   TYPE_FLOATFORMAT (objfile_type->builtin_float)
4688     = gdbarch_float_format (gdbarch);
4689   objfile_type->builtin_double
4690     = init_type (TYPE_CODE_FLT,
4691                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4692                  0, "double", objfile);
4693   TYPE_FLOATFORMAT (objfile_type->builtin_double)
4694     = gdbarch_double_format (gdbarch);
4695   objfile_type->builtin_long_double
4696     = init_type (TYPE_CODE_FLT,
4697                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
4698                  0, "long double", objfile);
4699   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
4700     = gdbarch_long_double_format (gdbarch);
4701
4702   /* This type represents a type that was unrecognized in symbol read-in.  */
4703   objfile_type->builtin_error
4704     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
4705
4706   /* The following set of types is used for symbols with no
4707      debug information.  */
4708   objfile_type->nodebug_text_symbol
4709     = init_type (TYPE_CODE_FUNC, 1, 0,
4710                  "<text variable, no debug info>", objfile);
4711   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
4712     = objfile_type->builtin_int;
4713   objfile_type->nodebug_text_gnu_ifunc_symbol
4714     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4715                  "<text gnu-indirect-function variable, no debug info>",
4716                  objfile);
4717   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4718     = objfile_type->nodebug_text_symbol;
4719   objfile_type->nodebug_got_plt_symbol
4720     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4721                  "<text from jump slot in .got.plt, no debug info>",
4722                  objfile);
4723   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4724     = objfile_type->nodebug_text_symbol;
4725   objfile_type->nodebug_data_symbol
4726     = init_type (TYPE_CODE_INT,
4727                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4728                  "<data variable, no debug info>", objfile);
4729   objfile_type->nodebug_unknown_symbol
4730     = init_type (TYPE_CODE_INT, 1, 0,
4731                  "<variable (not text or data), no debug info>", objfile);
4732   objfile_type->nodebug_tls_symbol
4733     = init_type (TYPE_CODE_INT,
4734                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4735                  "<thread local variable, no debug info>", objfile);
4736
4737   /* NOTE: on some targets, addresses and pointers are not necessarily
4738      the same.
4739
4740      The upshot is:
4741      - gdb's `struct type' always describes the target's
4742        representation.
4743      - gdb's `struct value' objects should always hold values in
4744        target form.
4745      - gdb's CORE_ADDR values are addresses in the unified virtual
4746        address space that the assembler and linker work with.  Thus,
4747        since target_read_memory takes a CORE_ADDR as an argument, it
4748        can access any memory on the target, even if the processor has
4749        separate code and data address spaces.
4750
4751      In this context, objfile_type->builtin_core_addr is a bit odd:
4752      it's a target type for a value the target will never see.  It's
4753      only used to hold the values of (typeless) linker symbols, which
4754      are indeed in the unified virtual address space.  */
4755
4756   objfile_type->builtin_core_addr
4757     = init_type (TYPE_CODE_INT,
4758                  gdbarch_addr_bit (gdbarch) / 8,
4759                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
4760
4761   set_objfile_data (objfile, objfile_type_data, objfile_type);
4762   return objfile_type;
4763 }
4764
4765 extern initialize_file_ftype _initialize_gdbtypes;
4766
4767 void
4768 _initialize_gdbtypes (void)
4769 {
4770   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
4771   objfile_type_data = register_objfile_data ();
4772
4773   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4774                              _("Set debugging of C++ overloading."),
4775                              _("Show debugging of C++ overloading."),
4776                              _("When enabled, ranking of the "
4777                                "functions is displayed."),
4778                              NULL,
4779                              show_overload_debug,
4780                              &setdebuglist, &showdebuglist);
4781
4782   /* Add user knob for controlling resolution of opaque types.  */
4783   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
4784                            &opaque_type_resolution,
4785                            _("Set resolution of opaque struct/class/union"
4786                              " types (if set before loading symbols)."),
4787                            _("Show resolution of opaque struct/class/union"
4788                              " types (if set before loading symbols)."),
4789                            NULL, NULL,
4790                            show_opaque_type_resolution,
4791                            &setlist, &showlist);
4792
4793   /* Add an option to permit non-strict type checking.  */
4794   add_setshow_boolean_cmd ("type", class_support,
4795                            &strict_type_checking,
4796                            _("Set strict type checking."),
4797                            _("Show strict type checking."),
4798                            NULL, NULL,
4799                            show_strict_type_checking,
4800                            &setchecklist, &showchecklist);
4801 }