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