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