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