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