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