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