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