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