s390: Fix build when using EXEEXT_FOR_BUILD
[binutils-gdb.git] / gdb / gdbtypes.c
blob2580b4f19eebd09f636d47bab400fb82853ba7be
1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2023 Free Software Foundation, Inc.
5 Contributed by Cygnus Support, using pieces from other GDB modules.
7 This file is part of GDB.
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.
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.
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/>. */
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 #include "rust-lang.h"
47 #include "ada-lang.h"
49 /* The value of an invalid conversion badness. */
50 #define INVALID_CONVERSION 100
52 static struct dynamic_prop_list *
53 copy_dynamic_prop_list (struct obstack *, struct dynamic_prop_list *);
55 /* Initialize BADNESS constants. */
57 const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
59 const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
60 const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
62 const struct rank EXACT_MATCH_BADNESS = {0,0};
64 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
65 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
66 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
67 const struct rank CV_CONVERSION_BADNESS = {1, 0};
68 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
69 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
70 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
71 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
72 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
73 const struct rank BASE_CONVERSION_BADNESS = {2,0};
74 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
75 const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
76 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
77 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
78 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
79 const struct rank VARARG_BADNESS = {4, 0};
81 /* Floatformat pairs. */
82 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
83 &floatformat_ieee_half_big,
84 &floatformat_ieee_half_little
86 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
87 &floatformat_ieee_single_big,
88 &floatformat_ieee_single_little
90 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
91 &floatformat_ieee_double_big,
92 &floatformat_ieee_double_little
94 const struct floatformat *floatformats_ieee_quad[BFD_ENDIAN_UNKNOWN] = {
95 &floatformat_ieee_quad_big,
96 &floatformat_ieee_quad_little
98 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
99 &floatformat_ieee_double_big,
100 &floatformat_ieee_double_littlebyte_bigword
102 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
103 &floatformat_i387_ext,
104 &floatformat_i387_ext
106 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
107 &floatformat_m68881_ext,
108 &floatformat_m68881_ext
110 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
111 &floatformat_arm_ext_big,
112 &floatformat_arm_ext_littlebyte_bigword
114 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
115 &floatformat_ia64_spill_big,
116 &floatformat_ia64_spill_little
118 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
119 &floatformat_vax_f,
120 &floatformat_vax_f
122 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
123 &floatformat_vax_d,
124 &floatformat_vax_d
126 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
127 &floatformat_ibm_long_double_big,
128 &floatformat_ibm_long_double_little
130 const struct floatformat *floatformats_bfloat16[BFD_ENDIAN_UNKNOWN] = {
131 &floatformat_bfloat16_big,
132 &floatformat_bfloat16_little
135 /* Should opaque types be resolved? */
137 static bool opaque_type_resolution = true;
139 /* See gdbtypes.h. */
141 unsigned int overload_debug = 0;
143 /* A flag to enable strict type checking. */
145 static bool strict_type_checking = true;
147 /* A function to show whether opaque types are resolved. */
149 static void
150 show_opaque_type_resolution (struct ui_file *file, int from_tty,
151 struct cmd_list_element *c,
152 const char *value)
154 gdb_printf (file, _("Resolution of opaque struct/class/union types "
155 "(if set before loading symbols) is %s.\n"),
156 value);
159 /* A function to show whether C++ overload debugging is enabled. */
161 static void
162 show_overload_debug (struct ui_file *file, int from_tty,
163 struct cmd_list_element *c, const char *value)
165 gdb_printf (file, _("Debugging of C++ overloading is %s.\n"),
166 value);
169 /* A function to show the status of strict type checking. */
171 static void
172 show_strict_type_checking (struct ui_file *file, int from_tty,
173 struct cmd_list_element *c, const char *value)
175 gdb_printf (file, _("Strict type checking is %s.\n"), value);
179 /* Helper function to initialize a newly allocated type. Set type code
180 to CODE and initialize the type-specific fields accordingly. */
182 static void
183 set_type_code (struct type *type, enum type_code code)
185 type->set_code (code);
187 switch (code)
189 case TYPE_CODE_STRUCT:
190 case TYPE_CODE_UNION:
191 case TYPE_CODE_NAMESPACE:
192 INIT_CPLUS_SPECIFIC (type);
193 break;
194 case TYPE_CODE_FLT:
195 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
196 break;
197 case TYPE_CODE_FUNC:
198 INIT_FUNC_SPECIFIC (type);
199 break;
200 case TYPE_CODE_FIXED_POINT:
201 INIT_FIXED_POINT_SPECIFIC (type);
202 break;
206 /* See gdbtypes.h. */
208 type *
209 type_allocator::new_type ()
211 if (m_smash)
212 return m_data.type;
214 obstack *obstack = (m_is_objfile
215 ? &m_data.objfile->objfile_obstack
216 : gdbarch_obstack (m_data.gdbarch));
218 /* Alloc the structure and start off with all fields zeroed. */
219 struct type *type = OBSTACK_ZALLOC (obstack, struct type);
220 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (obstack, struct main_type);
221 TYPE_MAIN_TYPE (type)->m_lang = m_lang;
223 if (m_is_objfile)
225 OBJSTAT (m_data.objfile, n_types++);
226 type->set_owner (m_data.objfile);
228 else
229 type->set_owner (m_data.gdbarch);
231 /* Initialize the fields that might not be zero. */
232 type->set_code (TYPE_CODE_UNDEF);
233 TYPE_CHAIN (type) = type; /* Chain back to itself. */
235 return type;
238 /* See gdbtypes.h. */
240 type *
241 type_allocator::new_type (enum type_code code, int bit, const char *name)
243 struct type *type = new_type ();
244 set_type_code (type, code);
245 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
246 type->set_length (bit / TARGET_CHAR_BIT);
248 if (name != nullptr)
250 obstack *obstack = (m_is_objfile
251 ? &m_data.objfile->objfile_obstack
252 : gdbarch_obstack (m_data.gdbarch));
253 type->set_name (obstack_strdup (obstack, name));
256 return type;
259 /* See gdbtypes.h. */
261 gdbarch *
262 type_allocator::arch ()
264 if (m_smash)
265 return m_data.type->arch ();
266 if (m_is_objfile)
267 return m_data.objfile->arch ();
268 return m_data.gdbarch;
271 /* See gdbtypes.h. */
273 gdbarch *
274 type::arch () const
276 struct gdbarch *arch;
278 if (this->is_objfile_owned ())
279 arch = this->objfile_owner ()->arch ();
280 else
281 arch = this->arch_owner ();
283 /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
284 a gdbarch, however, this is very rare, and even then, in most cases
285 that type::arch is called, we assume that a non-NULL value is
286 returned. */
287 gdb_assert (arch != nullptr);
288 return arch;
291 /* See gdbtypes.h. */
293 struct type *
294 get_target_type (struct type *type)
296 if (type != NULL)
298 type = type->target_type ();
299 if (type != NULL)
300 type = check_typedef (type);
303 return type;
306 /* See gdbtypes.h. */
308 unsigned int
309 type_length_units (struct type *type)
311 int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
313 return type->length () / unit_size;
316 /* Alloc a new type instance structure, fill it with some defaults,
317 and point it at OLDTYPE. Allocate the new type instance from the
318 same place as OLDTYPE. */
320 static struct type *
321 alloc_type_instance (struct type *oldtype)
323 struct type *type;
325 /* Allocate the structure. */
327 if (!oldtype->is_objfile_owned ())
328 type = GDBARCH_OBSTACK_ZALLOC (oldtype->arch_owner (), struct type);
329 else
330 type = OBSTACK_ZALLOC (&oldtype->objfile_owner ()->objfile_obstack,
331 struct type);
333 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
335 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
337 return type;
340 /* Clear all remnants of the previous type at TYPE, in preparation for
341 replacing it with something else. Preserve owner information. */
343 static void
344 smash_type (struct type *type)
346 bool objfile_owned = type->is_objfile_owned ();
347 objfile *objfile = type->objfile_owner ();
348 gdbarch *arch = type->arch_owner ();
350 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
352 /* Restore owner information. */
353 if (objfile_owned)
354 type->set_owner (objfile);
355 else
356 type->set_owner (arch);
358 /* For now, delete the rings. */
359 TYPE_CHAIN (type) = type;
361 /* For now, leave the pointer/reference types alone. */
364 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
365 to a pointer to memory where the pointer type should be stored.
366 If *TYPEPTR is zero, update it to point to the pointer type we return.
367 We allocate new memory if needed. */
369 struct type *
370 make_pointer_type (struct type *type, struct type **typeptr)
372 struct type *ntype; /* New type */
373 struct type *chain;
375 ntype = TYPE_POINTER_TYPE (type);
377 if (ntype)
379 if (typeptr == 0)
380 return ntype; /* Don't care about alloc,
381 and have new type. */
382 else if (*typeptr == 0)
384 *typeptr = ntype; /* Tracking alloc, and have new type. */
385 return ntype;
389 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
391 ntype = type_allocator (type).new_type ();
392 if (typeptr)
393 *typeptr = ntype;
395 else /* We have storage, but need to reset it. */
397 ntype = *typeptr;
398 chain = TYPE_CHAIN (ntype);
399 smash_type (ntype);
400 TYPE_CHAIN (ntype) = chain;
403 ntype->set_target_type (type);
404 TYPE_POINTER_TYPE (type) = ntype;
406 /* FIXME! Assumes the machine has only one representation for pointers! */
408 ntype->set_length (gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT);
409 ntype->set_code (TYPE_CODE_PTR);
411 /* Mark pointers as unsigned. The target converts between pointers
412 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
413 gdbarch_address_to_pointer. */
414 ntype->set_is_unsigned (true);
416 /* Update the length of all the other variants of this type. */
417 chain = TYPE_CHAIN (ntype);
418 while (chain != ntype)
420 chain->set_length (ntype->length ());
421 chain = TYPE_CHAIN (chain);
424 return ntype;
427 /* Given a type TYPE, return a type of pointers to that type.
428 May need to construct such a type if this is the first use. */
430 struct type *
431 lookup_pointer_type (struct type *type)
433 return make_pointer_type (type, (struct type **) 0);
436 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
437 points to a pointer to memory where the reference type should be
438 stored. If *TYPEPTR is zero, update it to point to the reference
439 type we return. We allocate new memory if needed. REFCODE denotes
440 the kind of reference type to lookup (lvalue or rvalue reference). */
442 struct type *
443 make_reference_type (struct type *type, struct type **typeptr,
444 enum type_code refcode)
446 struct type *ntype; /* New type */
447 struct type **reftype;
448 struct type *chain;
450 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
452 ntype = (refcode == TYPE_CODE_REF ? TYPE_REFERENCE_TYPE (type)
453 : TYPE_RVALUE_REFERENCE_TYPE (type));
455 if (ntype)
457 if (typeptr == 0)
458 return ntype; /* Don't care about alloc,
459 and have new type. */
460 else if (*typeptr == 0)
462 *typeptr = ntype; /* Tracking alloc, and have new type. */
463 return ntype;
467 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
469 ntype = type_allocator (type).new_type ();
470 if (typeptr)
471 *typeptr = ntype;
473 else /* We have storage, but need to reset it. */
475 ntype = *typeptr;
476 chain = TYPE_CHAIN (ntype);
477 smash_type (ntype);
478 TYPE_CHAIN (ntype) = chain;
481 ntype->set_target_type (type);
482 reftype = (refcode == TYPE_CODE_REF ? &TYPE_REFERENCE_TYPE (type)
483 : &TYPE_RVALUE_REFERENCE_TYPE (type));
485 *reftype = ntype;
487 /* FIXME! Assume the machine has only one representation for
488 references, and that it matches the (only) representation for
489 pointers! */
491 ntype->set_length (gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT);
492 ntype->set_code (refcode);
494 *reftype = ntype;
496 /* Update the length of all the other variants of this type. */
497 chain = TYPE_CHAIN (ntype);
498 while (chain != ntype)
500 chain->set_length (ntype->length ());
501 chain = TYPE_CHAIN (chain);
504 return ntype;
507 /* Same as above, but caller doesn't care about memory allocation
508 details. */
510 struct type *
511 lookup_reference_type (struct type *type, enum type_code refcode)
513 return make_reference_type (type, (struct type **) 0, refcode);
516 /* Lookup the lvalue reference type for the type TYPE. */
518 struct type *
519 lookup_lvalue_reference_type (struct type *type)
521 return lookup_reference_type (type, TYPE_CODE_REF);
524 /* Lookup the rvalue reference type for the type TYPE. */
526 struct type *
527 lookup_rvalue_reference_type (struct type *type)
529 return lookup_reference_type (type, TYPE_CODE_RVALUE_REF);
532 /* Lookup a function type that returns type TYPE. TYPEPTR, if
533 nonzero, points to a pointer to memory where the function type
534 should be stored. If *TYPEPTR is zero, update it to point to the
535 function type we return. We allocate new memory if needed. */
537 struct type *
538 make_function_type (struct type *type, struct type **typeptr)
540 struct type *ntype; /* New type */
542 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
544 ntype = type_allocator (type).new_type ();
545 if (typeptr)
546 *typeptr = ntype;
548 else /* We have storage, but need to reset it. */
550 ntype = *typeptr;
551 smash_type (ntype);
554 ntype->set_target_type (type);
556 ntype->set_length (1);
557 ntype->set_code (TYPE_CODE_FUNC);
559 INIT_FUNC_SPECIFIC (ntype);
561 return ntype;
564 /* Given a type TYPE, return a type of functions that return that type.
565 May need to construct such a type if this is the first use. */
567 struct type *
568 lookup_function_type (struct type *type)
570 return make_function_type (type, (struct type **) 0);
573 /* Given a type TYPE and argument types, return the appropriate
574 function type. If the final type in PARAM_TYPES is NULL, make a
575 varargs function. */
577 struct type *
578 lookup_function_type_with_arguments (struct type *type,
579 int nparams,
580 struct type **param_types)
582 struct type *fn = make_function_type (type, (struct type **) 0);
583 int i;
585 if (nparams > 0)
587 if (param_types[nparams - 1] == NULL)
589 --nparams;
590 fn->set_has_varargs (true);
592 else if (check_typedef (param_types[nparams - 1])->code ()
593 == TYPE_CODE_VOID)
595 --nparams;
596 /* Caller should have ensured this. */
597 gdb_assert (nparams == 0);
598 fn->set_is_prototyped (true);
600 else
601 fn->set_is_prototyped (true);
604 fn->alloc_fields (nparams);
605 for (i = 0; i < nparams; ++i)
606 fn->field (i).set_type (param_types[i]);
608 return fn;
611 /* Identify address space identifier by name -- return a
612 type_instance_flags. */
614 type_instance_flags
615 address_space_name_to_type_instance_flags (struct gdbarch *gdbarch,
616 const char *space_identifier)
618 type_instance_flags type_flags;
620 /* Check for known address space delimiters. */
621 if (!strcmp (space_identifier, "code"))
622 return TYPE_INSTANCE_FLAG_CODE_SPACE;
623 else if (!strcmp (space_identifier, "data"))
624 return TYPE_INSTANCE_FLAG_DATA_SPACE;
625 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
626 && gdbarch_address_class_name_to_type_flags (gdbarch,
627 space_identifier,
628 &type_flags))
629 return type_flags;
630 else
631 error (_("Unknown address space specifier: \"%s\""), space_identifier);
634 /* Identify address space identifier by type_instance_flags and return
635 the string version of the adress space name. */
637 const char *
638 address_space_type_instance_flags_to_name (struct gdbarch *gdbarch,
639 type_instance_flags space_flag)
641 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
642 return "code";
643 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
644 return "data";
645 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
646 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
647 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
648 else
649 return NULL;
652 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
654 If STORAGE is non-NULL, create the new type instance there.
655 STORAGE must be in the same obstack as TYPE. */
657 static struct type *
658 make_qualified_type (struct type *type, type_instance_flags new_flags,
659 struct type *storage)
661 struct type *ntype;
663 ntype = type;
666 if (ntype->instance_flags () == new_flags)
667 return ntype;
668 ntype = TYPE_CHAIN (ntype);
670 while (ntype != type);
672 /* Create a new type instance. */
673 if (storage == NULL)
674 ntype = alloc_type_instance (type);
675 else
677 /* If STORAGE was provided, it had better be in the same objfile
678 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
679 if one objfile is freed and the other kept, we'd have
680 dangling pointers. */
681 gdb_assert (type->objfile_owner () == storage->objfile_owner ());
683 ntype = storage;
684 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
685 TYPE_CHAIN (ntype) = ntype;
688 /* Pointers or references to the original type are not relevant to
689 the new type. */
690 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
691 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
693 /* Chain the new qualified type to the old type. */
694 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
695 TYPE_CHAIN (type) = ntype;
697 /* Now set the instance flags and return the new type. */
698 ntype->set_instance_flags (new_flags);
700 /* Set length of new type to that of the original type. */
701 ntype->set_length (type->length ());
703 return ntype;
706 /* Make an address-space-delimited variant of a type -- a type that
707 is identical to the one supplied except that it has an address
708 space attribute attached to it (such as "code" or "data").
710 The space attributes "code" and "data" are for Harvard
711 architectures. The address space attributes are for architectures
712 which have alternately sized pointers or pointers with alternate
713 representations. */
715 struct type *
716 make_type_with_address_space (struct type *type,
717 type_instance_flags space_flag)
719 type_instance_flags new_flags = ((type->instance_flags ()
720 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
721 | TYPE_INSTANCE_FLAG_DATA_SPACE
722 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
723 | space_flag);
725 return make_qualified_type (type, new_flags, NULL);
728 /* Make a "c-v" variant of a type -- a type that is identical to the
729 one supplied except that it may have const or volatile attributes
730 CNST is a flag for setting the const attribute
731 VOLTL is a flag for setting the volatile attribute
732 TYPE is the base type whose variant we are creating.
734 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
735 storage to hold the new qualified type; *TYPEPTR and TYPE must be
736 in the same objfile. Otherwise, allocate fresh memory for the new
737 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
738 new type we construct. */
740 struct type *
741 make_cv_type (int cnst, int voltl,
742 struct type *type,
743 struct type **typeptr)
745 struct type *ntype; /* New type */
747 type_instance_flags new_flags = (type->instance_flags ()
748 & ~(TYPE_INSTANCE_FLAG_CONST
749 | TYPE_INSTANCE_FLAG_VOLATILE));
751 if (cnst)
752 new_flags |= TYPE_INSTANCE_FLAG_CONST;
754 if (voltl)
755 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
757 if (typeptr && *typeptr != NULL)
759 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
760 a C-V variant chain that threads across objfiles: if one
761 objfile gets freed, then the other has a broken C-V chain.
763 This code used to try to copy over the main type from TYPE to
764 *TYPEPTR if they were in different objfiles, but that's
765 wrong, too: TYPE may have a field list or member function
766 lists, which refer to types of their own, etc. etc. The
767 whole shebang would need to be copied over recursively; you
768 can't have inter-objfile pointers. The only thing to do is
769 to leave stub types as stub types, and look them up afresh by
770 name each time you encounter them. */
771 gdb_assert ((*typeptr)->objfile_owner () == type->objfile_owner ());
774 ntype = make_qualified_type (type, new_flags,
775 typeptr ? *typeptr : NULL);
777 if (typeptr != NULL)
778 *typeptr = ntype;
780 return ntype;
783 /* Make a 'restrict'-qualified version of TYPE. */
785 struct type *
786 make_restrict_type (struct type *type)
788 return make_qualified_type (type,
789 (type->instance_flags ()
790 | TYPE_INSTANCE_FLAG_RESTRICT),
791 NULL);
794 /* Make a type without const, volatile, or restrict. */
796 struct type *
797 make_unqualified_type (struct type *type)
799 return make_qualified_type (type,
800 (type->instance_flags ()
801 & ~(TYPE_INSTANCE_FLAG_CONST
802 | TYPE_INSTANCE_FLAG_VOLATILE
803 | TYPE_INSTANCE_FLAG_RESTRICT)),
804 NULL);
807 /* Make a '_Atomic'-qualified version of TYPE. */
809 struct type *
810 make_atomic_type (struct type *type)
812 return make_qualified_type (type,
813 (type->instance_flags ()
814 | TYPE_INSTANCE_FLAG_ATOMIC),
815 NULL);
818 /* Replace the contents of ntype with the type *type. This changes the
819 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
820 the changes are propogated to all types in the TYPE_CHAIN.
822 In order to build recursive types, it's inevitable that we'll need
823 to update types in place --- but this sort of indiscriminate
824 smashing is ugly, and needs to be replaced with something more
825 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
826 clear if more steps are needed. */
828 void
829 replace_type (struct type *ntype, struct type *type)
831 struct type *chain;
833 /* These two types had better be in the same objfile. Otherwise,
834 the assignment of one type's main type structure to the other
835 will produce a type with references to objects (names; field
836 lists; etc.) allocated on an objfile other than its own. */
837 gdb_assert (ntype->objfile_owner () == type->objfile_owner ());
839 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
841 /* The type length is not a part of the main type. Update it for
842 each type on the variant chain. */
843 chain = ntype;
846 /* Assert that this element of the chain has no address-class bits
847 set in its flags. Such type variants might have type lengths
848 which are supposed to be different from the non-address-class
849 variants. This assertion shouldn't ever be triggered because
850 symbol readers which do construct address-class variants don't
851 call replace_type(). */
852 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
854 chain->set_length (type->length ());
855 chain = TYPE_CHAIN (chain);
857 while (ntype != chain);
859 /* Assert that the two types have equivalent instance qualifiers.
860 This should be true for at least all of our debug readers. */
861 gdb_assert (ntype->instance_flags () == type->instance_flags ());
864 /* Implement direct support for MEMBER_TYPE in GNU C++.
865 May need to construct such a type if this is the first use.
866 The TYPE is the type of the member. The DOMAIN is the type
867 of the aggregate that the member belongs to. */
869 struct type *
870 lookup_memberptr_type (struct type *type, struct type *domain)
872 struct type *mtype;
874 mtype = type_allocator (type).new_type ();
875 smash_to_memberptr_type (mtype, domain, type);
876 return mtype;
879 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
881 struct type *
882 lookup_methodptr_type (struct type *to_type)
884 struct type *mtype;
886 mtype = type_allocator (to_type).new_type ();
887 smash_to_methodptr_type (mtype, to_type);
888 return mtype;
891 /* See gdbtypes.h. */
893 bool
894 operator== (const dynamic_prop &l, const dynamic_prop &r)
896 if (l.kind () != r.kind ())
897 return false;
899 switch (l.kind ())
901 case PROP_UNDEFINED:
902 return true;
903 case PROP_CONST:
904 return l.const_val () == r.const_val ();
905 case PROP_ADDR_OFFSET:
906 case PROP_LOCEXPR:
907 case PROP_LOCLIST:
908 return l.baton () == r.baton ();
909 case PROP_VARIANT_PARTS:
910 return l.variant_parts () == r.variant_parts ();
911 case PROP_TYPE:
912 return l.original_type () == r.original_type ();
915 gdb_assert_not_reached ("unhandled dynamic_prop kind");
918 /* See gdbtypes.h. */
920 bool
921 operator== (const range_bounds &l, const range_bounds &r)
923 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
925 return (FIELD_EQ (low)
926 && FIELD_EQ (high)
927 && FIELD_EQ (flag_upper_bound_is_count)
928 && FIELD_EQ (flag_bound_evaluated)
929 && FIELD_EQ (bias));
931 #undef FIELD_EQ
934 /* See gdbtypes.h. */
936 struct type *
937 create_range_type (type_allocator &alloc, struct type *index_type,
938 const struct dynamic_prop *low_bound,
939 const struct dynamic_prop *high_bound,
940 LONGEST bias)
942 /* The INDEX_TYPE should be a type capable of holding the upper and lower
943 bounds, as such a zero sized, or void type makes no sense. */
944 gdb_assert (index_type->code () != TYPE_CODE_VOID);
945 gdb_assert (index_type->length () > 0);
947 struct type *result_type = alloc.new_type ();
948 result_type->set_code (TYPE_CODE_RANGE);
949 result_type->set_target_type (index_type);
950 if (index_type->is_stub ())
951 result_type->set_target_is_stub (true);
952 else
953 result_type->set_length (check_typedef (index_type)->length ());
955 range_bounds *bounds
956 = (struct range_bounds *) TYPE_ZALLOC (result_type, sizeof (range_bounds));
957 bounds->low = *low_bound;
958 bounds->high = *high_bound;
959 bounds->bias = bias;
960 bounds->stride.set_const_val (0);
962 result_type->set_bounds (bounds);
964 if (index_type->code () == TYPE_CODE_FIXED_POINT)
965 result_type->set_is_unsigned (index_type->is_unsigned ());
966 else if (index_type->is_unsigned ())
968 /* If the underlying type is unsigned, then the range
969 necessarily is. */
970 result_type->set_is_unsigned (true);
972 /* Otherwise, the signed-ness of a range type can't simply be copied
973 from the underlying type. Consider a case where the underlying
974 type is 'int', but the range type can hold 0..65535, and where
975 the range is further specified to fit into 16 bits. In this
976 case, if we copy the underlying type's sign, then reading some
977 range values will cause an unwanted sign extension. So, we have
978 some heuristics here instead. */
979 else if (low_bound->is_constant () && low_bound->const_val () >= 0)
981 result_type->set_is_unsigned (true);
982 /* Ada allows the declaration of range types whose upper bound is
983 less than the lower bound, so checking the lower bound is not
984 enough. Make sure we do not mark a range type whose upper bound
985 is negative as unsigned. */
986 if (high_bound->is_constant () && high_bound->const_val () < 0)
987 result_type->set_is_unsigned (false);
990 result_type->set_endianity_is_not_default
991 (index_type->endianity_is_not_default ());
993 return result_type;
996 /* See gdbtypes.h. */
998 struct type *
999 create_range_type_with_stride (type_allocator &alloc,
1000 struct type *index_type,
1001 const struct dynamic_prop *low_bound,
1002 const struct dynamic_prop *high_bound,
1003 LONGEST bias,
1004 const struct dynamic_prop *stride,
1005 bool byte_stride_p)
1007 struct type *result_type = create_range_type (alloc, index_type, low_bound,
1008 high_bound, bias);
1010 gdb_assert (stride != nullptr);
1011 result_type->bounds ()->stride = *stride;
1012 result_type->bounds ()->flag_is_byte_stride = byte_stride_p;
1014 return result_type;
1017 /* See gdbtypes.h. */
1019 struct type *
1020 create_static_range_type (type_allocator &alloc, struct type *index_type,
1021 LONGEST low_bound, LONGEST high_bound)
1023 struct dynamic_prop low, high;
1025 low.set_const_val (low_bound);
1026 high.set_const_val (high_bound);
1028 struct type *result_type = create_range_type (alloc, index_type,
1029 &low, &high, 0);
1031 return result_type;
1034 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1035 are static, otherwise returns 0. */
1037 static bool
1038 has_static_range (const struct range_bounds *bounds)
1040 /* If the range doesn't have a defined stride then its stride field will
1041 be initialized to the constant 0. */
1042 return (bounds->low.is_constant ()
1043 && bounds->high.is_constant ()
1044 && bounds->stride.is_constant ());
1047 /* See gdbtypes.h. */
1049 std::optional<LONGEST>
1050 get_discrete_low_bound (struct type *type)
1052 type = check_typedef (type);
1053 switch (type->code ())
1055 case TYPE_CODE_RANGE:
1057 /* This function only works for ranges with a constant low bound. */
1058 if (!type->bounds ()->low.is_constant ())
1059 return {};
1061 LONGEST low = type->bounds ()->low.const_val ();
1063 if (type->target_type ()->code () == TYPE_CODE_ENUM)
1065 std::optional<LONGEST> low_pos
1066 = discrete_position (type->target_type (), low);
1068 if (low_pos.has_value ())
1069 low = *low_pos;
1072 return low;
1075 case TYPE_CODE_ENUM:
1077 if (type->num_fields () > 0)
1079 /* The enums may not be sorted by value, so search all
1080 entries. */
1081 LONGEST low = type->field (0).loc_enumval ();
1083 for (int i = 0; i < type->num_fields (); i++)
1085 if (type->field (i).loc_enumval () < low)
1086 low = type->field (i).loc_enumval ();
1089 return low;
1091 else
1092 return 0;
1095 case TYPE_CODE_BOOL:
1096 return 0;
1098 case TYPE_CODE_INT:
1099 if (type->length () > sizeof (LONGEST)) /* Too big */
1100 return {};
1102 if (!type->is_unsigned ())
1103 return -(1 << (type->length () * TARGET_CHAR_BIT - 1));
1105 [[fallthrough]];
1106 case TYPE_CODE_CHAR:
1107 return 0;
1109 default:
1110 return {};
1114 /* See gdbtypes.h. */
1116 std::optional<LONGEST>
1117 get_discrete_high_bound (struct type *type)
1119 type = check_typedef (type);
1120 switch (type->code ())
1122 case TYPE_CODE_RANGE:
1124 /* This function only works for ranges with a constant high bound. */
1125 if (!type->bounds ()->high.is_constant ())
1126 return {};
1128 LONGEST high = type->bounds ()->high.const_val ();
1130 if (type->target_type ()->code () == TYPE_CODE_ENUM)
1132 std::optional<LONGEST> high_pos
1133 = discrete_position (type->target_type (), high);
1135 if (high_pos.has_value ())
1136 high = *high_pos;
1139 return high;
1142 case TYPE_CODE_ENUM:
1144 if (type->num_fields () > 0)
1146 /* The enums may not be sorted by value, so search all
1147 entries. */
1148 LONGEST high = type->field (0).loc_enumval ();
1150 for (int i = 0; i < type->num_fields (); i++)
1152 if (type->field (i).loc_enumval () > high)
1153 high = type->field (i).loc_enumval ();
1156 return high;
1158 else
1159 return -1;
1162 case TYPE_CODE_BOOL:
1163 return 1;
1165 case TYPE_CODE_INT:
1166 if (type->length () > sizeof (LONGEST)) /* Too big */
1167 return {};
1169 if (!type->is_unsigned ())
1171 LONGEST low = -(1 << (type->length () * TARGET_CHAR_BIT - 1));
1172 return -low - 1;
1175 [[fallthrough]];
1176 case TYPE_CODE_CHAR:
1178 /* This round-about calculation is to avoid shifting by
1179 type->length () * TARGET_CHAR_BIT, which will not work
1180 if type->length () == sizeof (LONGEST). */
1181 LONGEST high = 1 << (type->length () * TARGET_CHAR_BIT - 1);
1182 return (high - 1) | high;
1185 default:
1186 return {};
1190 /* See gdbtypes.h. */
1192 bool
1193 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
1195 std::optional<LONGEST> low = get_discrete_low_bound (type);
1196 if (!low.has_value ())
1197 return false;
1199 std::optional<LONGEST> high = get_discrete_high_bound (type);
1200 if (!high.has_value ())
1201 return false;
1203 *lowp = *low;
1204 *highp = *high;
1206 return true;
1209 /* See gdbtypes.h */
1211 bool
1212 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
1214 struct type *index = type->index_type ();
1215 LONGEST low = 0;
1216 LONGEST high = 0;
1218 if (index == NULL)
1219 return false;
1221 if (!get_discrete_bounds (index, &low, &high))
1222 return false;
1224 if (low_bound)
1225 *low_bound = low;
1227 if (high_bound)
1228 *high_bound = high;
1230 return true;
1233 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1234 representation of a value of this type, save the corresponding
1235 position number in POS.
1237 Its differs from VAL only in the case of enumeration types. In
1238 this case, the position number of the value of the first listed
1239 enumeration literal is zero; the position number of the value of
1240 each subsequent enumeration literal is one more than that of its
1241 predecessor in the list.
1243 Return 1 if the operation was successful. Return zero otherwise,
1244 in which case the value of POS is unmodified.
1247 std::optional<LONGEST>
1248 discrete_position (struct type *type, LONGEST val)
1250 if (type->code () == TYPE_CODE_RANGE)
1251 type = type->target_type ();
1253 if (type->code () == TYPE_CODE_ENUM)
1255 int i;
1257 for (i = 0; i < type->num_fields (); i += 1)
1259 if (val == type->field (i).loc_enumval ())
1260 return i;
1263 /* Invalid enumeration value. */
1264 return {};
1266 else
1267 return val;
1270 /* If the array TYPE has static bounds calculate and update its
1271 size, then return true. Otherwise return false and leave TYPE
1272 unchanged. */
1274 static bool
1275 update_static_array_size (struct type *type)
1277 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1279 struct type *range_type = type->index_type ();
1281 if (type->dyn_prop (DYN_PROP_BYTE_STRIDE) == nullptr
1282 && has_static_range (range_type->bounds ())
1283 && (!type_not_associated (type)
1284 && !type_not_allocated (type)))
1286 LONGEST low_bound, high_bound;
1287 int stride;
1288 struct type *element_type;
1290 stride = type->bit_stride ();
1292 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
1293 low_bound = high_bound = 0;
1295 element_type = check_typedef (type->target_type ());
1296 /* Be careful when setting the array length. Ada arrays can be
1297 empty arrays with the high_bound being smaller than the low_bound.
1298 In such cases, the array length should be zero. */
1299 if (high_bound < low_bound)
1300 type->set_length (0);
1301 else if (stride != 0)
1303 /* Ensure that the type length is always positive, even in the
1304 case where (for example in Fortran) we have a negative
1305 stride. It is possible to have a single element array with a
1306 negative stride in Fortran (this doesn't mean anything
1307 special, it's still just a single element array) so do
1308 consider that case when touching this code. */
1309 LONGEST element_count = std::abs (high_bound - low_bound + 1);
1310 type->set_length (((std::abs (stride) * element_count) + 7) / 8);
1312 else
1313 type->set_length (element_type->length ()
1314 * (high_bound - low_bound + 1));
1316 /* If this array's element is itself an array with a bit stride,
1317 then we want to update this array's bit stride to reflect the
1318 size of the sub-array. Otherwise, we'll end up using the
1319 wrong size when trying to find elements of the outer
1320 array. */
1321 if (element_type->code () == TYPE_CODE_ARRAY
1322 && (stride != 0 || element_type->is_multi_dimensional ())
1323 && element_type->length () != 0
1324 && element_type->field (0).bitsize () != 0
1325 && get_array_bounds (element_type, &low_bound, &high_bound)
1326 && high_bound >= low_bound)
1327 type->field (0).set_bitsize
1328 ((high_bound - low_bound + 1)
1329 * element_type->field (0).bitsize ());
1331 return true;
1334 return false;
1337 /* See gdbtypes.h. */
1339 struct type *
1340 create_array_type_with_stride (type_allocator &alloc,
1341 struct type *element_type,
1342 struct type *range_type,
1343 struct dynamic_prop *byte_stride_prop,
1344 unsigned int bit_stride)
1346 if (byte_stride_prop != nullptr && byte_stride_prop->is_constant ())
1348 /* The byte stride is actually not dynamic. Pretend we were
1349 called with bit_stride set instead of byte_stride_prop.
1350 This will give us the same result type, while avoiding
1351 the need to handle this as a special case. */
1352 bit_stride = byte_stride_prop->const_val () * 8;
1353 byte_stride_prop = NULL;
1356 struct type *result_type = alloc.new_type ();
1358 result_type->set_code (TYPE_CODE_ARRAY);
1359 result_type->set_target_type (element_type);
1361 result_type->alloc_fields (1);
1362 result_type->set_index_type (range_type);
1363 if (byte_stride_prop != NULL)
1364 result_type->add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop);
1365 else if (bit_stride > 0)
1366 result_type->field (0).set_bitsize (bit_stride);
1368 if (!update_static_array_size (result_type))
1370 /* This type is dynamic and its length needs to be computed
1371 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1372 undefined by setting it to zero. Although we are not expected
1373 to trust TYPE_LENGTH in this case, setting the size to zero
1374 allows us to avoid allocating objects of random sizes in case
1375 we accidently do. */
1376 result_type->set_length (0);
1379 /* TYPE_TARGET_STUB will take care of zero length arrays. */
1380 if (result_type->length () == 0)
1381 result_type->set_target_is_stub (true);
1383 return result_type;
1386 /* See gdbtypes.h. */
1388 struct type *
1389 create_array_type (type_allocator &alloc,
1390 struct type *element_type,
1391 struct type *range_type)
1393 return create_array_type_with_stride (alloc, element_type,
1394 range_type, NULL, 0);
1397 struct type *
1398 lookup_array_range_type (struct type *element_type,
1399 LONGEST low_bound, LONGEST high_bound)
1401 struct type *index_type;
1402 struct type *range_type;
1404 type_allocator alloc (element_type);
1405 index_type = builtin_type (element_type->arch ())->builtin_int;
1407 range_type = create_static_range_type (alloc, index_type,
1408 low_bound, high_bound);
1410 return create_array_type (alloc, element_type, range_type);
1413 /* See gdbtypes.h. */
1415 struct type *
1416 create_string_type (type_allocator &alloc,
1417 struct type *string_char_type,
1418 struct type *range_type)
1420 struct type *result_type = create_array_type (alloc,
1421 string_char_type,
1422 range_type);
1423 result_type->set_code (TYPE_CODE_STRING);
1424 return result_type;
1427 struct type *
1428 lookup_string_range_type (struct type *string_char_type,
1429 LONGEST low_bound, LONGEST high_bound)
1431 struct type *result_type;
1433 result_type = lookup_array_range_type (string_char_type,
1434 low_bound, high_bound);
1435 result_type->set_code (TYPE_CODE_STRING);
1436 return result_type;
1439 struct type *
1440 create_set_type (type_allocator &alloc, struct type *domain_type)
1442 struct type *result_type = alloc.new_type ();
1444 result_type->set_code (TYPE_CODE_SET);
1445 result_type->alloc_fields (1);
1447 if (!domain_type->is_stub ())
1449 LONGEST low_bound, high_bound, bit_length;
1451 if (!get_discrete_bounds (domain_type, &low_bound, &high_bound))
1452 low_bound = high_bound = 0;
1454 bit_length = high_bound - low_bound + 1;
1455 result_type->set_length ((bit_length + TARGET_CHAR_BIT - 1)
1456 / TARGET_CHAR_BIT);
1457 if (low_bound >= 0)
1458 result_type->set_is_unsigned (true);
1460 result_type->field (0).set_type (domain_type);
1462 return result_type;
1465 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1466 and any array types nested inside it. */
1468 void
1469 make_vector_type (struct type *array_type)
1471 struct type *inner_array, *elt_type;
1473 /* Find the innermost array type, in case the array is
1474 multi-dimensional. */
1475 inner_array = array_type;
1476 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
1477 inner_array = inner_array->target_type ();
1479 elt_type = inner_array->target_type ();
1480 if (elt_type->code () == TYPE_CODE_INT)
1482 type_instance_flags flags
1483 = elt_type->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT;
1484 elt_type = make_qualified_type (elt_type, flags, NULL);
1485 inner_array->set_target_type (elt_type);
1488 array_type->set_is_vector (true);
1491 struct type *
1492 init_vector_type (struct type *elt_type, int n)
1494 struct type *array_type;
1496 array_type = lookup_array_range_type (elt_type, 0, n - 1);
1497 make_vector_type (array_type);
1498 return array_type;
1501 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1502 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1503 confusing. "self" is a common enough replacement for "this".
1504 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1505 TYPE_CODE_METHOD. */
1507 struct type *
1508 internal_type_self_type (struct type *type)
1510 switch (type->code ())
1512 case TYPE_CODE_METHODPTR:
1513 case TYPE_CODE_MEMBERPTR:
1514 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1515 return NULL;
1516 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1517 return TYPE_MAIN_TYPE (type)->type_specific.self_type;
1518 case TYPE_CODE_METHOD:
1519 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1520 return NULL;
1521 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1522 return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type;
1523 default:
1524 gdb_assert_not_reached ("bad type");
1528 /* Set the type of the class that TYPE belongs to.
1529 In c++ this is the class of "this".
1530 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1531 TYPE_CODE_METHOD. */
1533 void
1534 set_type_self_type (struct type *type, struct type *self_type)
1536 switch (type->code ())
1538 case TYPE_CODE_METHODPTR:
1539 case TYPE_CODE_MEMBERPTR:
1540 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1541 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE;
1542 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1543 TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type;
1544 break;
1545 case TYPE_CODE_METHOD:
1546 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1547 INIT_FUNC_SPECIFIC (type);
1548 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1549 TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type;
1550 break;
1551 default:
1552 gdb_assert_not_reached ("bad type");
1556 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1557 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1558 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1559 TYPE doesn't include the offset (that's the value of the MEMBER
1560 itself), but does include the structure type into which it points
1561 (for some reason).
1563 When "smashing" the type, we preserve the objfile that the old type
1564 pointed to, since we aren't changing where the type is actually
1565 allocated. */
1567 void
1568 smash_to_memberptr_type (struct type *type, struct type *self_type,
1569 struct type *to_type)
1571 smash_type (type);
1572 type->set_code (TYPE_CODE_MEMBERPTR);
1573 type->set_target_type (to_type);
1574 set_type_self_type (type, self_type);
1575 /* Assume that a data member pointer is the same size as a normal
1576 pointer. */
1577 type->set_length (gdbarch_ptr_bit (to_type->arch ()) / TARGET_CHAR_BIT);
1580 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1582 When "smashing" the type, we preserve the objfile that the old type
1583 pointed to, since we aren't changing where the type is actually
1584 allocated. */
1586 void
1587 smash_to_methodptr_type (struct type *type, struct type *to_type)
1589 smash_type (type);
1590 type->set_code (TYPE_CODE_METHODPTR);
1591 type->set_target_type (to_type);
1592 set_type_self_type (type, TYPE_SELF_TYPE (to_type));
1593 type->set_length (cplus_method_ptr_size (to_type));
1596 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1597 METHOD just means `function that gets an extra "this" argument'.
1599 When "smashing" the type, we preserve the objfile that the old type
1600 pointed to, since we aren't changing where the type is actually
1601 allocated. */
1603 void
1604 smash_to_method_type (struct type *type, struct type *self_type,
1605 struct type *to_type, struct field *args,
1606 int nargs, int varargs)
1608 smash_type (type);
1609 type->set_code (TYPE_CODE_METHOD);
1610 type->set_target_type (to_type);
1611 set_type_self_type (type, self_type);
1612 type->set_fields (args);
1613 type->set_num_fields (nargs);
1615 if (varargs)
1616 type->set_has_varargs (true);
1618 /* In practice, this is never needed. */
1619 type->set_length (1);
1622 /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
1623 Since GCC PR debug/47510 DWARF provides associated information to detect the
1624 anonymous class linkage name from its typedef.
1626 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1627 apply it itself. */
1629 const char *
1630 type_name_or_error (struct type *type)
1632 struct type *saved_type = type;
1633 const char *name;
1634 struct objfile *objfile;
1636 type = check_typedef (type);
1638 name = type->name ();
1639 if (name != NULL)
1640 return name;
1642 name = saved_type->name ();
1643 objfile = saved_type->objfile_owner ();
1644 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1645 name ? name : "<anonymous>",
1646 objfile ? objfile_name (objfile) : "<arch>");
1649 /* See gdbtypes.h. */
1651 struct type *
1652 lookup_typename (const struct language_defn *language,
1653 const char *name,
1654 const struct block *block, int noerr)
1656 struct symbol *sym;
1658 sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
1659 language->la_language, NULL).symbol;
1660 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
1662 struct type *type = sym->type ();
1663 /* Ensure the length of TYPE is valid. */
1664 check_typedef (type);
1665 return type;
1668 if (noerr)
1669 return NULL;
1670 error (_("No type named %s."), name);
1673 struct type *
1674 lookup_unsigned_typename (const struct language_defn *language,
1675 const char *name)
1677 std::string uns;
1678 uns.reserve (strlen (name) + strlen ("unsigned "));
1679 uns = "unsigned ";
1680 uns += name;
1682 return lookup_typename (language, uns.c_str (), NULL, 0);
1685 struct type *
1686 lookup_signed_typename (const struct language_defn *language, const char *name)
1688 /* In C and C++, "char" and "signed char" are distinct types. */
1689 if (streq (name, "char"))
1690 name = "signed char";
1691 return lookup_typename (language, name, NULL, 0);
1694 /* Lookup a structure type named "struct NAME",
1695 visible in lexical block BLOCK. */
1697 struct type *
1698 lookup_struct (const char *name, const struct block *block)
1700 struct symbol *sym;
1702 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1704 if (sym == NULL)
1706 error (_("No struct type named %s."), name);
1708 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1710 error (_("This context has class, union or enum %s, not a struct."),
1711 name);
1713 return (sym->type ());
1716 /* Lookup a union type named "union NAME",
1717 visible in lexical block BLOCK. */
1719 struct type *
1720 lookup_union (const char *name, const struct block *block)
1722 struct symbol *sym;
1723 struct type *t;
1725 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1727 if (sym == NULL)
1728 error (_("No union type named %s."), name);
1730 t = sym->type ();
1732 if (t->code () == TYPE_CODE_UNION)
1733 return t;
1735 /* If we get here, it's not a union. */
1736 error (_("This context has class, struct or enum %s, not a union."),
1737 name);
1740 /* Lookup an enum type named "enum NAME",
1741 visible in lexical block BLOCK. */
1743 struct type *
1744 lookup_enum (const char *name, const struct block *block)
1746 struct symbol *sym;
1748 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1749 if (sym == NULL)
1751 error (_("No enum type named %s."), name);
1753 if (sym->type ()->code () != TYPE_CODE_ENUM)
1755 error (_("This context has class, struct or union %s, not an enum."),
1756 name);
1758 return (sym->type ());
1761 /* Lookup a template type named "template NAME<TYPE>",
1762 visible in lexical block BLOCK. */
1764 struct type *
1765 lookup_template_type (const char *name, struct type *type,
1766 const struct block *block)
1768 std::string nam;
1769 nam.reserve (strlen (name) + strlen (type->name ()) + strlen ("< >"));
1770 nam = name;
1771 nam += "<";
1772 nam += type->name ();
1773 nam += " >"; /* FIXME, extra space still introduced in gcc? */
1775 symbol *sym = lookup_symbol (nam.c_str (), block, VAR_DOMAIN, 0).symbol;
1777 if (sym == NULL)
1779 error (_("No template type named %s."), name);
1781 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1783 error (_("This context has class, union or enum %s, not a struct."),
1784 name);
1786 return (sym->type ());
1789 /* See gdbtypes.h. */
1791 struct_elt
1792 lookup_struct_elt (struct type *type, const char *name, int noerr)
1794 int i;
1796 for (;;)
1798 type = check_typedef (type);
1799 if (type->code () != TYPE_CODE_PTR
1800 && type->code () != TYPE_CODE_REF)
1801 break;
1802 type = type->target_type ();
1805 if (type->code () != TYPE_CODE_STRUCT
1806 && type->code () != TYPE_CODE_UNION)
1808 std::string type_name = type_to_string (type);
1809 error (_("Type %s is not a structure or union type."),
1810 type_name.c_str ());
1813 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
1815 const char *t_field_name = type->field (i).name ();
1817 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1819 return {&type->field (i), type->field (i).loc_bitpos ()};
1821 else if (!t_field_name || *t_field_name == '\0')
1823 struct_elt elt
1824 = lookup_struct_elt (type->field (i).type (), name, 1);
1825 if (elt.field != NULL)
1827 elt.offset += type->field (i).loc_bitpos ();
1828 return elt;
1833 /* OK, it's not in this class. Recursively check the baseclasses. */
1834 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1836 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1837 if (elt.field != NULL)
1838 return elt;
1841 if (noerr)
1842 return {nullptr, 0};
1844 std::string type_name = type_to_string (type);
1845 error (_("Type %s has no component named %s."), type_name.c_str (), name);
1848 /* See gdbtypes.h. */
1850 struct type *
1851 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1853 struct_elt elt = lookup_struct_elt (type, name, noerr);
1854 if (elt.field != NULL)
1855 return elt.field->type ();
1856 else
1857 return NULL;
1860 /* Return the largest number representable by unsigned integer type TYPE. */
1862 ULONGEST
1863 get_unsigned_type_max (struct type *type)
1865 unsigned int n;
1867 type = check_typedef (type);
1868 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
1869 gdb_assert (type->length () <= sizeof (ULONGEST));
1871 /* Written this way to avoid overflow. */
1872 n = type->length () * TARGET_CHAR_BIT;
1873 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1876 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1877 signed integer type TYPE. */
1879 void
1880 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1882 unsigned int n;
1884 type = check_typedef (type);
1885 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
1886 gdb_assert (type->length () <= sizeof (LONGEST));
1888 n = type->length () * TARGET_CHAR_BIT;
1889 *min = -((ULONGEST) 1 << (n - 1));
1890 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1893 /* Return the largest value representable by pointer type TYPE. */
1895 CORE_ADDR
1896 get_pointer_type_max (struct type *type)
1898 unsigned int n;
1900 type = check_typedef (type);
1901 gdb_assert (type->code () == TYPE_CODE_PTR);
1902 gdb_assert (type->length () <= sizeof (CORE_ADDR));
1904 n = type->length () * TARGET_CHAR_BIT;
1905 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1908 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1909 cplus_stuff.vptr_fieldno.
1911 cplus_stuff is initialized to cplus_struct_default which does not
1912 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1913 designated initializers). We cope with that here. */
1916 internal_type_vptr_fieldno (struct type *type)
1918 type = check_typedef (type);
1919 gdb_assert (type->code () == TYPE_CODE_STRUCT
1920 || type->code () == TYPE_CODE_UNION);
1921 if (!HAVE_CPLUS_STRUCT (type))
1922 return -1;
1923 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1926 /* Set the value of cplus_stuff.vptr_fieldno. */
1928 void
1929 set_type_vptr_fieldno (struct type *type, int fieldno)
1931 type = check_typedef (type);
1932 gdb_assert (type->code () == TYPE_CODE_STRUCT
1933 || type->code () == TYPE_CODE_UNION);
1934 if (!HAVE_CPLUS_STRUCT (type))
1935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1936 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1939 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1940 cplus_stuff.vptr_basetype. */
1942 struct type *
1943 internal_type_vptr_basetype (struct type *type)
1945 type = check_typedef (type);
1946 gdb_assert (type->code () == TYPE_CODE_STRUCT
1947 || type->code () == TYPE_CODE_UNION);
1948 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1949 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1952 /* Set the value of cplus_stuff.vptr_basetype. */
1954 void
1955 set_type_vptr_basetype (struct type *type, struct type *basetype)
1957 type = check_typedef (type);
1958 gdb_assert (type->code () == TYPE_CODE_STRUCT
1959 || type->code () == TYPE_CODE_UNION);
1960 if (!HAVE_CPLUS_STRUCT (type))
1961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1962 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1965 /* Lookup the vptr basetype/fieldno values for TYPE.
1966 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1967 vptr_fieldno. Also, if found and basetype is from the same objfile,
1968 cache the results.
1969 If not found, return -1 and ignore BASETYPEP.
1970 Callers should be aware that in some cases (for example,
1971 the type or one of its baseclasses is a stub type and we are
1972 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1973 this function will not be able to find the
1974 virtual function table pointer, and vptr_fieldno will remain -1 and
1975 vptr_basetype will remain NULL or incomplete. */
1978 get_vptr_fieldno (struct type *type, struct type **basetypep)
1980 type = check_typedef (type);
1982 if (TYPE_VPTR_FIELDNO (type) < 0)
1984 int i;
1986 /* We must start at zero in case the first (and only) baseclass
1987 is virtual (and hence we cannot share the table pointer). */
1988 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1990 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1991 int fieldno;
1992 struct type *basetype;
1994 fieldno = get_vptr_fieldno (baseclass, &basetype);
1995 if (fieldno >= 0)
1997 /* If the type comes from a different objfile we can't cache
1998 it, it may have a different lifetime. PR 2384 */
1999 if (type->objfile_owner () == basetype->objfile_owner ())
2001 set_type_vptr_fieldno (type, fieldno);
2002 set_type_vptr_basetype (type, basetype);
2004 if (basetypep)
2005 *basetypep = basetype;
2006 return fieldno;
2010 /* Not found. */
2011 return -1;
2013 else
2015 if (basetypep)
2016 *basetypep = TYPE_VPTR_BASETYPE (type);
2017 return TYPE_VPTR_FIELDNO (type);
2021 static void
2022 stub_noname_complaint (void)
2024 complaint (_("stub type has NULL name"));
2027 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2028 attached to it, and that property has a non-constant value. */
2030 static int
2031 array_type_has_dynamic_stride (struct type *type)
2033 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2035 return prop != nullptr && prop->is_constant ();
2038 /* Worker for is_dynamic_type. */
2040 static int
2041 is_dynamic_type_internal (struct type *type, int top_level)
2043 type = check_typedef (type);
2045 /* We only want to recognize references at the outermost level. */
2046 if (top_level && type->code () == TYPE_CODE_REF)
2047 type = check_typedef (type->target_type ());
2049 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2050 dynamic, even if the type itself is statically defined.
2051 From a user's point of view, this may appear counter-intuitive;
2052 but it makes sense in this context, because the point is to determine
2053 whether any part of the type needs to be resolved before it can
2054 be exploited. */
2055 if (TYPE_DATA_LOCATION (type) != NULL
2056 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2057 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
2058 return 1;
2060 if (TYPE_ASSOCIATED_PROP (type))
2061 return 1;
2063 if (TYPE_ALLOCATED_PROP (type))
2064 return 1;
2066 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2067 if (prop != nullptr && prop->kind () != PROP_TYPE)
2068 return 1;
2070 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2071 return 1;
2073 switch (type->code ())
2075 case TYPE_CODE_RANGE:
2077 /* A range type is obviously dynamic if it has at least one
2078 dynamic bound. But also consider the range type to be
2079 dynamic when its subtype is dynamic, even if the bounds
2080 of the range type are static. It allows us to assume that
2081 the subtype of a static range type is also static. */
2082 return (!has_static_range (type->bounds ())
2083 || is_dynamic_type_internal (type->target_type (), 0));
2086 case TYPE_CODE_STRING:
2087 /* Strings are very much like an array of characters, and can be
2088 treated as one here. */
2089 case TYPE_CODE_ARRAY:
2091 gdb_assert (type->num_fields () == 1);
2093 /* The array is dynamic if either the bounds are dynamic... */
2094 if (is_dynamic_type_internal (type->index_type (), 0))
2095 return 1;
2096 /* ... or the elements it contains have a dynamic contents... */
2097 if (is_dynamic_type_internal (type->target_type (), 0))
2098 return 1;
2099 /* ... or if it has a dynamic stride... */
2100 if (array_type_has_dynamic_stride (type))
2101 return 1;
2102 return 0;
2105 case TYPE_CODE_STRUCT:
2106 case TYPE_CODE_UNION:
2108 int i;
2110 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2112 for (i = 0; i < type->num_fields (); ++i)
2114 /* Static fields can be ignored here. */
2115 if (type->field (i).is_static ())
2116 continue;
2117 /* If the field has dynamic type, then so does TYPE. */
2118 if (is_dynamic_type_internal (type->field (i).type (), 0))
2119 return 1;
2120 /* If the field is at a fixed offset, then it is not
2121 dynamic. */
2122 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
2123 continue;
2124 /* Do not consider C++ virtual base types to be dynamic
2125 due to the field's offset being dynamic; these are
2126 handled via other means. */
2127 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2128 continue;
2129 return 1;
2132 break;
2135 return 0;
2138 /* See gdbtypes.h. */
2141 is_dynamic_type (struct type *type)
2143 return is_dynamic_type_internal (type, 1);
2146 static struct type *resolve_dynamic_type_internal
2147 (struct type *type, struct property_addr_info *addr_stack,
2148 const frame_info_ptr &frame, int top_level);
2150 /* Given a dynamic range type (dyn_range_type) and a stack of
2151 struct property_addr_info elements, return a static version
2152 of that type.
2154 When RESOLVE_P is true then the returned static range is created by
2155 actually evaluating any dynamic properties within the range type, while
2156 when RESOLVE_P is false the returned static range has all of the bounds
2157 and stride information set to undefined. The RESOLVE_P set to false
2158 case will be used when evaluating a dynamic array that is not
2159 allocated, or not associated, i.e. the bounds information might not be
2160 initialized yet.
2162 RANK is the array rank for which we are resolving this range, and is a
2163 zero based count. The rank should never be negative.
2166 static struct type *
2167 resolve_dynamic_range (struct type *dyn_range_type,
2168 struct property_addr_info *addr_stack,
2169 const frame_info_ptr &frame,
2170 int rank, bool resolve_p = true)
2172 CORE_ADDR value;
2173 struct type *static_range_type, *static_target_type;
2174 struct dynamic_prop low_bound, high_bound, stride;
2176 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
2177 gdb_assert (rank >= 0);
2179 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
2180 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2181 { (CORE_ADDR) rank }))
2182 low_bound.set_const_val (value);
2183 else
2184 low_bound.set_undefined ();
2186 prop = &dyn_range_type->bounds ()->high;
2187 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2188 { (CORE_ADDR) rank }))
2190 high_bound.set_const_val (value);
2192 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2193 high_bound.set_const_val
2194 (low_bound.const_val () + high_bound.const_val () - 1);
2196 else
2197 high_bound.set_undefined ();
2199 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2200 prop = &dyn_range_type->bounds ()->stride;
2201 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2202 { (CORE_ADDR) rank }))
2204 stride.set_const_val (value);
2206 /* If we have a bit stride that is not an exact number of bytes then
2207 I really don't think this is going to work with current GDB, the
2208 array indexing code in GDB seems to be pretty heavily tied to byte
2209 offsets right now. Assuming 8 bits in a byte. */
2210 struct gdbarch *gdbarch = dyn_range_type->arch ();
2211 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2212 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2213 error (_("bit strides that are not a multiple of the byte size "
2214 "are currently not supported"));
2216 else
2218 stride.set_undefined ();
2219 byte_stride_p = true;
2222 static_target_type
2223 = resolve_dynamic_type_internal (dyn_range_type->target_type (),
2224 addr_stack, frame, 0);
2225 LONGEST bias = dyn_range_type->bounds ()->bias;
2226 type_allocator alloc (dyn_range_type);
2227 static_range_type = create_range_type_with_stride
2228 (alloc, static_target_type,
2229 &low_bound, &high_bound, bias, &stride, byte_stride_p);
2230 static_range_type->set_name (dyn_range_type->name ());
2231 static_range_type->bounds ()->flag_bound_evaluated = 1;
2232 return static_range_type;
2235 /* Helper function for resolve_dynamic_array_or_string. This function
2236 resolves the properties for a single array at RANK within a nested array
2237 of arrays structure. The RANK value is greater than or equal to 0, and
2238 starts at it's maximum value and goes down by 1 for each recursive call
2239 to this function. So, for a 3-dimensional array, the first call to this
2240 function has RANK == 2, then we call ourselves recursively with RANK ==
2241 1, than again with RANK == 0, and at that point we should return.
2243 TYPE is updated as the dynamic properties are resolved, and so, should
2244 be a copy of the dynamic type, rather than the original dynamic type
2245 itself.
2247 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2248 during the dynamic resolution.
2250 When RESOLVE_P is true then the dynamic properties of TYPE are
2251 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2252 instead we assume the array is not allocated/associated yet. */
2254 static struct type *
2255 resolve_dynamic_array_or_string_1 (struct type *type,
2256 struct property_addr_info *addr_stack,
2257 const frame_info_ptr &frame,
2258 int rank, bool resolve_p)
2260 CORE_ADDR value;
2261 struct type *elt_type;
2262 struct type *range_type;
2263 struct type *ary_dim;
2264 struct dynamic_prop *prop;
2265 unsigned int bit_stride = 0;
2267 /* For dynamic type resolution strings can be treated like arrays of
2268 characters. */
2269 gdb_assert (type->code () == TYPE_CODE_ARRAY
2270 || type->code () == TYPE_CODE_STRING);
2272 /* As the rank is a zero based count we expect this to never be
2273 negative. */
2274 gdb_assert (rank >= 0);
2276 /* Resolve the allocated and associated properties before doing anything
2277 else. If an array is not allocated or not associated then (at least
2278 for Fortran) there is no guarantee that the data to define the upper
2279 bound, lower bound, or stride will be correct. If RESOLVE_P is
2280 already false at this point then this is not the first dimension of
2281 the array and a more outer dimension has already marked this array as
2282 not allocated/associated, as such we just ignore this property. This
2283 is fine as GDB only checks the allocated/associated on the outer most
2284 dimension of the array. */
2285 prop = TYPE_ALLOCATED_PROP (type);
2286 if (prop != NULL && resolve_p
2287 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2289 prop->set_const_val (value);
2290 if (value == 0)
2291 resolve_p = false;
2294 prop = TYPE_ASSOCIATED_PROP (type);
2295 if (prop != NULL && resolve_p
2296 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2298 prop->set_const_val (value);
2299 if (value == 0)
2300 resolve_p = false;
2303 range_type = check_typedef (type->index_type ());
2304 range_type
2305 = resolve_dynamic_range (range_type, addr_stack, frame, rank, resolve_p);
2307 ary_dim = check_typedef (type->target_type ());
2308 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
2310 ary_dim = copy_type (ary_dim);
2311 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
2312 frame, rank - 1,
2313 resolve_p);
2315 else
2316 elt_type = type->target_type ();
2318 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2319 if (prop != NULL && resolve_p)
2321 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2323 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
2324 bit_stride = (unsigned int) (value * 8);
2326 else
2328 /* Could be a bug in our code, but it could also happen
2329 if the DWARF info is not correct. Issue a warning,
2330 and assume no byte/bit stride (leave bit_stride = 0). */
2331 warning (_("cannot determine array stride for type %s"),
2332 type->name () ? type->name () : "<no name>");
2335 else
2336 bit_stride = type->field (0).bitsize ();
2338 type_allocator alloc (type, type_allocator::SMASH);
2339 return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
2340 bit_stride);
2343 /* Resolve an array or string type with dynamic properties, return a new
2344 type with the dynamic properties resolved to actual values. The
2345 ADDR_STACK represents the location of the object being resolved. */
2347 static struct type *
2348 resolve_dynamic_array_or_string (struct type *type,
2349 struct property_addr_info *addr_stack,
2350 const frame_info_ptr &frame)
2352 CORE_ADDR value;
2353 int rank = 0;
2355 /* For dynamic type resolution strings can be treated like arrays of
2356 characters. */
2357 gdb_assert (type->code () == TYPE_CODE_ARRAY
2358 || type->code () == TYPE_CODE_STRING);
2360 type = copy_type (type);
2362 /* Resolve the rank property to get rank value. */
2363 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
2364 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2366 prop->set_const_val (value);
2367 rank = value;
2369 if (rank == 0)
2371 /* Rank is zero, if a variable is passed as an argument to a
2372 function. In this case the resolved type should not be an
2373 array, but should instead be that of an array element. */
2374 struct type *dynamic_array_type = type;
2375 type = copy_type (dynamic_array_type->target_type ());
2376 struct dynamic_prop_list *prop_list
2377 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2378 if (prop_list != nullptr)
2380 struct obstack *obstack
2381 = &type->objfile_owner ()->objfile_obstack;
2382 TYPE_MAIN_TYPE (type)->dyn_prop_list
2383 = copy_dynamic_prop_list (obstack, prop_list);
2385 return type;
2387 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2389 /* What would this even mean? A string with a dynamic rank
2390 greater than 1. */
2391 error (_("unable to handle string with dynamic rank greater than 1"));
2393 else if (rank > 1)
2395 /* Arrays with dynamic rank are initially just an array type
2396 with a target type that is the array element.
2398 However, now we know the rank of the array we need to build
2399 the array of arrays structure that GDB expects, that is we
2400 need an array type that has a target which is an array type,
2401 and so on, until eventually, we have the element type at the
2402 end of the chain. Create all the additional array types here
2403 by copying the top level array type. */
2404 struct type *element_type = type->target_type ();
2405 struct type *rank_type = type;
2406 for (int i = 1; i < rank; i++)
2408 rank_type->set_target_type (copy_type (rank_type));
2409 rank_type = rank_type->target_type ();
2411 rank_type->set_target_type (element_type);
2414 else
2416 rank = 1;
2418 for (struct type *tmp_type = check_typedef (type->target_type ());
2419 tmp_type->code () == TYPE_CODE_ARRAY;
2420 tmp_type = check_typedef (tmp_type->target_type ()))
2421 ++rank;
2424 /* The rank that we calculated above is actually a count of the number of
2425 ranks. However, when we resolve the type of each individual array
2426 rank we should actually use a rank "offset", e.g. an array with a rank
2427 count of 1 (calculated above) will use the rank offset 0 in order to
2428 resolve the details of the first array dimension. As a result, we
2429 reduce the rank by 1 here. */
2430 --rank;
2432 return resolve_dynamic_array_or_string_1 (type, addr_stack, frame, rank,
2433 true);
2436 /* Resolve dynamic bounds of members of the union TYPE to static
2437 bounds. ADDR_STACK is a stack of struct property_addr_info
2438 to be used if needed during the dynamic resolution. */
2440 static struct type *
2441 resolve_dynamic_union (struct type *type,
2442 struct property_addr_info *addr_stack,
2443 const frame_info_ptr &frame)
2445 struct type *resolved_type;
2446 int i;
2447 unsigned int max_len = 0;
2449 gdb_assert (type->code () == TYPE_CODE_UNION);
2451 resolved_type = copy_type (type);
2452 resolved_type->copy_fields (type);
2453 for (i = 0; i < resolved_type->num_fields (); ++i)
2455 struct type *t;
2457 if (type->field (i).is_static ())
2458 continue;
2460 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
2461 addr_stack, frame, 0);
2462 resolved_type->field (i).set_type (t);
2464 struct type *real_type = check_typedef (t);
2465 if (real_type->length () > max_len)
2466 max_len = real_type->length ();
2469 resolved_type->set_length (max_len);
2470 return resolved_type;
2473 /* See gdbtypes.h. */
2475 bool
2476 variant::matches (ULONGEST value, bool is_unsigned) const
2478 for (const discriminant_range &range : discriminants)
2479 if (range.contains (value, is_unsigned))
2480 return true;
2481 return false;
2484 static void
2485 compute_variant_fields_inner (struct type *type,
2486 struct property_addr_info *addr_stack,
2487 const variant_part &part,
2488 std::vector<bool> &flags);
2490 /* A helper function to determine which variant fields will be active.
2491 This handles both the variant's direct fields, and any variant
2492 parts embedded in this variant. TYPE is the type we're examining.
2493 ADDR_STACK holds information about the concrete object. VARIANT is
2494 the current variant to be handled. FLAGS is where the results are
2495 stored -- this function sets the Nth element in FLAGS if the
2496 corresponding field is enabled. ENABLED is whether this variant is
2497 enabled or not. */
2499 static void
2500 compute_variant_fields_recurse (struct type *type,
2501 struct property_addr_info *addr_stack,
2502 const variant &variant,
2503 std::vector<bool> &flags,
2504 bool enabled)
2506 for (int field = variant.first_field; field < variant.last_field; ++field)
2507 flags[field] = enabled;
2509 for (const variant_part &new_part : variant.parts)
2511 if (enabled)
2512 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2513 else
2515 for (const auto &sub_variant : new_part.variants)
2516 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2517 flags, enabled);
2522 /* A helper function to determine which variant fields will be active.
2523 This evaluates the discriminant, decides which variant (if any) is
2524 active, and then updates FLAGS to reflect which fields should be
2525 available. TYPE is the type we're examining. ADDR_STACK holds
2526 information about the concrete object. VARIANT is the current
2527 variant to be handled. FLAGS is where the results are stored --
2528 this function sets the Nth element in FLAGS if the corresponding
2529 field is enabled. */
2531 static void
2532 compute_variant_fields_inner (struct type *type,
2533 struct property_addr_info *addr_stack,
2534 const variant_part &part,
2535 std::vector<bool> &flags)
2537 /* Evaluate the discriminant. */
2538 std::optional<ULONGEST> discr_value;
2539 if (part.discriminant_index != -1)
2541 int idx = part.discriminant_index;
2543 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
2544 error (_("Cannot determine struct field location"
2545 " (invalid location kind)"));
2547 if (addr_stack->valaddr.data () != NULL)
2548 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2549 idx);
2550 else
2552 CORE_ADDR addr = (addr_stack->addr
2553 + (type->field (idx).loc_bitpos ()
2554 / TARGET_CHAR_BIT));
2556 LONGEST bitsize = type->field (idx).bitsize ();
2557 LONGEST size = bitsize / 8;
2558 if (size == 0)
2559 size = type->field (idx).type ()->length ();
2561 gdb_byte bits[sizeof (ULONGEST)];
2562 read_memory (addr, bits, size);
2564 LONGEST bitpos = (type->field (idx).loc_bitpos ()
2565 % TARGET_CHAR_BIT);
2567 discr_value = unpack_bits_as_long (type->field (idx).type (),
2568 bits, bitpos, bitsize);
2572 /* Go through each variant and see which applies. */
2573 const variant *default_variant = nullptr;
2574 const variant *applied_variant = nullptr;
2575 for (const auto &variant : part.variants)
2577 if (variant.is_default ())
2578 default_variant = &variant;
2579 else if (discr_value.has_value ()
2580 && variant.matches (*discr_value, part.is_unsigned))
2582 applied_variant = &variant;
2583 break;
2586 if (applied_variant == nullptr)
2587 applied_variant = default_variant;
2589 for (const auto &variant : part.variants)
2590 compute_variant_fields_recurse (type, addr_stack, variant,
2591 flags, applied_variant == &variant);
2594 /* Determine which variant fields are available in TYPE. The enabled
2595 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2596 about the concrete object. PARTS describes the top-level variant
2597 parts for this type. */
2599 static void
2600 compute_variant_fields (struct type *type,
2601 struct type *resolved_type,
2602 struct property_addr_info *addr_stack,
2603 const gdb::array_view<variant_part> &parts)
2605 /* Assume all fields are included by default. */
2606 std::vector<bool> flags (resolved_type->num_fields (), true);
2608 /* Now disable fields based on the variants that control them. */
2609 for (const auto &part : parts)
2610 compute_variant_fields_inner (type, addr_stack, part, flags);
2612 unsigned int nfields = std::count (flags.begin (), flags.end (), true);
2613 /* No need to zero-initialize the newly allocated fields, they'll be
2614 initialized by the copy in the loop below. */
2615 resolved_type->alloc_fields (nfields, false);
2617 int out = 0;
2618 for (int i = 0; i < type->num_fields (); ++i)
2620 if (!flags[i])
2621 continue;
2623 resolved_type->field (out) = type->field (i);
2624 ++out;
2628 /* Resolve dynamic bounds of members of the struct TYPE to static
2629 bounds. ADDR_STACK is a stack of struct property_addr_info to
2630 be used if needed during the dynamic resolution. */
2632 static struct type *
2633 resolve_dynamic_struct (struct type *type,
2634 struct property_addr_info *addr_stack,
2635 const frame_info_ptr &frame)
2637 struct type *resolved_type;
2638 int i;
2639 unsigned resolved_type_bit_length = 0;
2641 gdb_assert (type->code () == TYPE_CODE_STRUCT);
2643 resolved_type = copy_type (type);
2645 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2646 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
2648 compute_variant_fields (type, resolved_type, addr_stack,
2649 *variant_prop->variant_parts ());
2650 /* We want to leave the property attached, so that the Rust code
2651 can tell whether the type was originally an enum. */
2652 variant_prop->set_original_type (type);
2654 else
2656 resolved_type->copy_fields (type);
2659 for (i = 0; i < resolved_type->num_fields (); ++i)
2661 unsigned new_bit_length;
2662 struct property_addr_info pinfo;
2664 if (resolved_type->field (i).is_static ())
2665 continue;
2667 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
2669 struct dwarf2_property_baton baton;
2670 baton.property_type
2671 = lookup_pointer_type (resolved_type->field (i).type ());
2672 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
2674 struct dynamic_prop prop;
2675 prop.set_locexpr (&baton);
2677 CORE_ADDR addr;
2678 if (dwarf2_evaluate_property (&prop, frame, addr_stack, &addr,
2679 {addr_stack->addr}))
2680 resolved_type->field (i).set_loc_bitpos
2681 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
2684 /* As we know this field is not a static field, the field's
2685 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2686 this is the case, but only trigger a simple error rather
2687 than an internal error if that fails. While failing
2688 that verification indicates a bug in our code, the error
2689 is not severe enough to suggest to the user he stops
2690 his debugging session because of it. */
2691 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
2692 error (_("Cannot determine struct field location"
2693 " (invalid location kind)"));
2695 pinfo.type = check_typedef (resolved_type->field (i).type ());
2696 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
2697 pinfo.valaddr = addr_stack->valaddr;
2698 if (!pinfo.valaddr.empty ())
2699 pinfo.valaddr = pinfo.valaddr.slice (offset);
2700 pinfo.addr = addr_stack->addr + offset;
2701 pinfo.next = addr_stack;
2703 resolved_type->field (i).set_type
2704 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
2705 &pinfo, frame, 0));
2706 gdb_assert (resolved_type->field (i).loc_kind ()
2707 == FIELD_LOC_KIND_BITPOS);
2709 new_bit_length = resolved_type->field (i).loc_bitpos ();
2710 if (resolved_type->field (i).bitsize () != 0)
2711 new_bit_length += resolved_type->field (i).bitsize ();
2712 else
2714 struct type *real_type
2715 = check_typedef (resolved_type->field (i).type ());
2717 new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
2720 /* Normally, we would use the position and size of the last field
2721 to determine the size of the enclosing structure. But GCC seems
2722 to be encoding the position of some fields incorrectly when
2723 the struct contains a dynamic field that is not placed last.
2724 So we compute the struct size based on the field that has
2725 the highest position + size - probably the best we can do. */
2726 if (new_bit_length > resolved_type_bit_length)
2727 resolved_type_bit_length = new_bit_length;
2730 /* The length of a type won't change for fortran, but it does for C and Ada.
2731 For fortran the size of dynamic fields might change over time but not the
2732 type length of the structure. If we adapt it, we run into problems
2733 when calculating the element offset for arrays of structs. */
2734 if (current_language->la_language != language_fortran)
2735 resolved_type->set_length ((resolved_type_bit_length + TARGET_CHAR_BIT - 1)
2736 / TARGET_CHAR_BIT);
2738 /* The Ada language uses this field as a cache for static fixed types: reset
2739 it as RESOLVED_TYPE must have its own static fixed type. */
2740 resolved_type->set_target_type (nullptr);
2742 return resolved_type;
2745 /* Worker for resolved_dynamic_type. */
2747 static struct type *
2748 resolve_dynamic_type_internal (struct type *type,
2749 struct property_addr_info *addr_stack,
2750 const frame_info_ptr &frame,
2751 int top_level)
2753 struct type *real_type = check_typedef (type);
2754 struct type *resolved_type = nullptr;
2755 struct dynamic_prop *prop;
2756 CORE_ADDR value;
2758 if (!is_dynamic_type_internal (real_type, top_level))
2759 return type;
2761 std::optional<CORE_ADDR> type_length;
2762 prop = TYPE_DYNAMIC_LENGTH (type);
2763 if (prop != NULL
2764 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2765 type_length = value;
2767 if (type->code () == TYPE_CODE_TYPEDEF)
2769 resolved_type = copy_type (type);
2770 resolved_type->set_target_type
2771 (resolve_dynamic_type_internal (type->target_type (), addr_stack,
2772 frame, top_level));
2774 else
2776 /* Before trying to resolve TYPE, make sure it is not a stub. */
2777 type = real_type;
2779 switch (type->code ())
2781 case TYPE_CODE_REF:
2783 struct property_addr_info pinfo;
2785 pinfo.type = check_typedef (type->target_type ());
2786 pinfo.valaddr = {};
2787 if (addr_stack->valaddr.data () != NULL)
2788 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2789 type);
2790 else
2791 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2792 pinfo.next = addr_stack;
2794 resolved_type = copy_type (type);
2795 resolved_type->set_target_type
2796 (resolve_dynamic_type_internal (type->target_type (),
2797 &pinfo, frame, top_level));
2798 break;
2801 case TYPE_CODE_STRING:
2802 /* Strings are very much like an array of characters, and can be
2803 treated as one here. */
2804 case TYPE_CODE_ARRAY:
2805 resolved_type = resolve_dynamic_array_or_string (type, addr_stack,
2806 frame);
2807 break;
2809 case TYPE_CODE_RANGE:
2810 /* Pass 0 for the rank value here, which indicates this is a
2811 range for the first rank of an array. The assumption is that
2812 this rank value is not actually required for the resolution of
2813 the dynamic range, otherwise, we'd be resolving this range
2814 within the context of a dynamic array. */
2815 resolved_type = resolve_dynamic_range (type, addr_stack, frame, 0);
2816 break;
2818 case TYPE_CODE_UNION:
2819 resolved_type = resolve_dynamic_union (type, addr_stack, frame);
2820 break;
2822 case TYPE_CODE_STRUCT:
2823 resolved_type = resolve_dynamic_struct (type, addr_stack, frame);
2824 break;
2828 if (resolved_type == nullptr)
2829 return type;
2831 if (type_length.has_value ())
2833 resolved_type->set_length (*type_length);
2834 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
2837 /* Resolve data_location attribute. */
2838 prop = TYPE_DATA_LOCATION (resolved_type);
2839 if (prop != NULL
2840 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2842 /* Start of Fortran hack. See comment in f-lang.h for what is going
2843 on here.*/
2844 if (current_language->la_language == language_fortran
2845 && resolved_type->code () == TYPE_CODE_ARRAY)
2846 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2847 value);
2848 /* End of Fortran hack. */
2849 prop->set_const_val (value);
2852 return resolved_type;
2855 /* See gdbtypes.h */
2857 struct type *
2858 resolve_dynamic_type (struct type *type,
2859 gdb::array_view<const gdb_byte> valaddr,
2860 CORE_ADDR addr,
2861 const frame_info_ptr *in_frame)
2863 struct property_addr_info pinfo
2864 = {check_typedef (type), valaddr, addr, NULL};
2866 frame_info_ptr frame;
2867 if (in_frame != nullptr)
2868 frame = *in_frame;
2870 return resolve_dynamic_type_internal (type, &pinfo, frame, 1);
2873 /* See gdbtypes.h */
2875 dynamic_prop *
2876 type::dyn_prop (dynamic_prop_node_kind prop_kind) const
2878 dynamic_prop_list *node = this->main_type->dyn_prop_list;
2880 while (node != NULL)
2882 if (node->prop_kind == prop_kind)
2883 return &node->prop;
2884 node = node->next;
2886 return NULL;
2889 /* See gdbtypes.h */
2891 void
2892 type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
2894 struct dynamic_prop_list *temp;
2896 gdb_assert (this->is_objfile_owned ());
2898 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
2899 struct dynamic_prop_list);
2900 temp->prop_kind = prop_kind;
2901 temp->prop = prop;
2902 temp->next = this->main_type->dyn_prop_list;
2904 this->main_type->dyn_prop_list = temp;
2907 /* See gdbtypes.h. */
2909 void
2910 type::remove_dyn_prop (dynamic_prop_node_kind kind)
2912 struct dynamic_prop_list *prev_node, *curr_node;
2914 curr_node = this->main_type->dyn_prop_list;
2915 prev_node = NULL;
2917 while (NULL != curr_node)
2919 if (curr_node->prop_kind == kind)
2921 /* Update the linked list but don't free anything.
2922 The property was allocated on objstack and it is not known
2923 if we are on top of it. Nevertheless, everything is released
2924 when the complete objstack is freed. */
2925 if (NULL == prev_node)
2926 this->main_type->dyn_prop_list = curr_node->next;
2927 else
2928 prev_node->next = curr_node->next;
2930 return;
2933 prev_node = curr_node;
2934 curr_node = curr_node->next;
2938 /* Find the real type of TYPE. This function returns the real type,
2939 after removing all layers of typedefs, and completing opaque or stub
2940 types. Completion changes the TYPE argument, but stripping of
2941 typedefs does not.
2943 Instance flags (e.g. const/volatile) are preserved as typedefs are
2944 stripped. If necessary a new qualified form of the underlying type
2945 is created.
2947 NOTE: This will return a typedef if type::target_type for the typedef has
2948 not been computed and we're either in the middle of reading symbols, or
2949 there was no name for the typedef in the debug info.
2951 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2952 QUITs in the symbol reading code can also throw.
2953 Thus this function can throw an exception.
2955 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2956 the target type.
2958 If this is a stubbed struct (i.e. declared as struct foo *), see if
2959 we can find a full definition in some other file. If so, copy this
2960 definition, so we can use it in future. There used to be a comment
2961 (but not any code) that if we don't find a full definition, we'd
2962 set a flag so we don't spend time in the future checking the same
2963 type. That would be a mistake, though--we might load in more
2964 symbols which contain a full definition for the type. */
2966 struct type *
2967 check_typedef (struct type *type)
2969 struct type *orig_type = type;
2971 gdb_assert (type);
2973 /* While we're removing typedefs, we don't want to lose qualifiers.
2974 E.g., const/volatile. */
2975 type_instance_flags instance_flags = type->instance_flags ();
2977 while (type->code () == TYPE_CODE_TYPEDEF)
2979 if (!type->target_type ())
2981 const char *name;
2982 struct symbol *sym;
2984 /* It is dangerous to call lookup_symbol if we are currently
2985 reading a symtab. Infinite recursion is one danger. */
2986 if (currently_reading_symtab)
2987 return make_qualified_type (type, instance_flags, NULL);
2989 name = type->name ();
2990 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
2991 VAR_DOMAIN as appropriate? */
2992 if (name == NULL)
2994 stub_noname_complaint ();
2995 return make_qualified_type (type, instance_flags, NULL);
2997 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
2998 if (sym)
2999 type->set_target_type (sym->type ());
3000 else /* TYPE_CODE_UNDEF */
3001 type->set_target_type (type_allocator (type->arch ()).new_type ());
3003 type = type->target_type ();
3005 /* Preserve the instance flags as we traverse down the typedef chain.
3007 Handling address spaces/classes is nasty, what do we do if there's a
3008 conflict?
3009 E.g., what if an outer typedef marks the type as class_1 and an inner
3010 typedef marks the type as class_2?
3011 This is the wrong place to do such error checking. We leave it to
3012 the code that created the typedef in the first place to flag the
3013 error. We just pick the outer address space (akin to letting the
3014 outer cast in a chain of casting win), instead of assuming
3015 "it can't happen". */
3017 const type_instance_flags ALL_SPACES
3018 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3019 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3020 const type_instance_flags ALL_CLASSES
3021 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3023 type_instance_flags new_instance_flags = type->instance_flags ();
3025 /* Treat code vs data spaces and address classes separately. */
3026 if ((instance_flags & ALL_SPACES) != 0)
3027 new_instance_flags &= ~ALL_SPACES;
3028 if ((instance_flags & ALL_CLASSES) != 0)
3029 new_instance_flags &= ~ALL_CLASSES;
3031 instance_flags |= new_instance_flags;
3035 /* If this is a struct/class/union with no fields, then check
3036 whether a full definition exists somewhere else. This is for
3037 systems where a type definition with no fields is issued for such
3038 types, instead of identifying them as stub types in the first
3039 place. */
3041 if (TYPE_IS_OPAQUE (type)
3042 && opaque_type_resolution
3043 && !currently_reading_symtab)
3045 const char *name = type->name ();
3046 struct type *newtype;
3048 if (name == NULL)
3050 stub_noname_complaint ();
3051 return make_qualified_type (type, instance_flags, NULL);
3053 newtype = lookup_transparent_type (name);
3055 if (newtype)
3057 /* If the resolved type and the stub are in the same
3058 objfile, then replace the stub type with the real deal.
3059 But if they're in separate objfiles, leave the stub
3060 alone; we'll just look up the transparent type every time
3061 we call check_typedef. We can't create pointers between
3062 types allocated to different objfiles, since they may
3063 have different lifetimes. Trying to copy NEWTYPE over to
3064 TYPE's objfile is pointless, too, since you'll have to
3065 move over any other types NEWTYPE refers to, which could
3066 be an unbounded amount of stuff. */
3067 if (newtype->objfile_owner () == type->objfile_owner ())
3068 type = make_qualified_type (newtype, type->instance_flags (), type);
3069 else
3070 type = newtype;
3073 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3074 types. */
3075 else if (type->is_stub () && !currently_reading_symtab)
3077 const char *name = type->name ();
3078 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3079 as appropriate? */
3080 struct symbol *sym;
3082 if (name == NULL)
3084 stub_noname_complaint ();
3085 return make_qualified_type (type, instance_flags, NULL);
3087 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
3088 if (sym)
3090 /* Same as above for opaque types, we can replace the stub
3091 with the complete type only if they are in the same
3092 objfile. */
3093 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3094 type = make_qualified_type (sym->type (),
3095 type->instance_flags (), type);
3096 else
3097 type = sym->type ();
3101 if (type->target_is_stub ())
3103 struct type *target_type = check_typedef (type->target_type ());
3105 if (target_type->is_stub () || target_type->target_is_stub ())
3107 /* Nothing we can do. */
3109 else if (type->code () == TYPE_CODE_RANGE)
3111 type->set_length (target_type->length ());
3112 type->set_target_is_stub (false);
3114 else if (type->code () == TYPE_CODE_ARRAY
3115 && update_static_array_size (type))
3116 type->set_target_is_stub (false);
3119 type = make_qualified_type (type, instance_flags, NULL);
3121 /* Cache TYPE_LENGTH for future use. */
3122 orig_type->set_length (type->length ());
3124 return type;
3127 /* Parse a type expression in the string [P..P+LENGTH). If an error
3128 occurs, silently return a void type. */
3130 static struct type *
3131 safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
3133 struct type *type = NULL; /* Initialize to keep gcc happy. */
3135 /* Suppress error messages. */
3136 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3137 &null_stream);
3139 /* Call parse_and_eval_type() without fear of longjmp()s. */
3142 type = parse_and_eval_type (p, length);
3144 catch (const gdb_exception_error &except)
3146 type = builtin_type (gdbarch)->builtin_void;
3149 return type;
3152 /* Ugly hack to convert method stubs into method types.
3154 He ain't kiddin'. This demangles the name of the method into a
3155 string including argument types, parses out each argument type,
3156 generates a string casting a zero to that type, evaluates the
3157 string, and stuffs the resulting type into an argtype vector!!!
3158 Then it knows the type of the whole function (including argument
3159 types for overloading), which info used to be in the stab's but was
3160 removed to hack back the space required for them. */
3162 static void
3163 check_stub_method (struct type *type, int method_id, int signature_id)
3165 struct gdbarch *gdbarch = type->arch ();
3166 struct fn_field *f;
3167 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3168 gdb::unique_xmalloc_ptr<char> demangled_name
3169 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3170 char *argtypetext, *p;
3171 int depth = 0, argcount = 1;
3172 struct field *argtypes;
3173 struct type *mtype;
3175 /* Make sure we got back a function string that we can use. */
3176 if (demangled_name)
3177 p = strchr (demangled_name.get (), '(');
3178 else
3179 p = NULL;
3181 if (demangled_name == NULL || p == NULL)
3182 error (_("Internal: Cannot demangle mangled name `%s'."),
3183 mangled_name);
3185 /* Now, read in the parameters that define this type. */
3186 p += 1;
3187 argtypetext = p;
3188 while (*p)
3190 if (*p == '(' || *p == '<')
3192 depth += 1;
3194 else if (*p == ')' || *p == '>')
3196 depth -= 1;
3198 else if (*p == ',' && depth == 0)
3200 argcount += 1;
3203 p += 1;
3206 /* If we read one argument and it was ``void'', don't count it. */
3207 if (startswith (argtypetext, "(void)"))
3208 argcount -= 1;
3210 /* We need one extra slot, for the THIS pointer. */
3212 argtypes = (struct field *)
3213 TYPE_ZALLOC (type, (argcount + 1) * sizeof (struct field));
3214 p = argtypetext;
3216 /* Add THIS pointer for non-static methods. */
3217 f = TYPE_FN_FIELDLIST1 (type, method_id);
3218 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3219 argcount = 0;
3220 else
3222 argtypes[0].set_type (lookup_pointer_type (type));
3223 argcount = 1;
3226 if (*p != ')') /* () means no args, skip while. */
3228 depth = 0;
3229 while (*p)
3231 if (depth <= 0 && (*p == ',' || *p == ')'))
3233 /* Avoid parsing of ellipsis, they will be handled below.
3234 Also avoid ``void'' as above. */
3235 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3236 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3238 argtypes[argcount].set_type
3239 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3240 argcount += 1;
3242 argtypetext = p + 1;
3245 if (*p == '(' || *p == '<')
3247 depth += 1;
3249 else if (*p == ')' || *p == '>')
3251 depth -= 1;
3254 p += 1;
3258 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3260 /* Now update the old "stub" type into a real type. */
3261 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3262 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3263 We want a method (TYPE_CODE_METHOD). */
3264 smash_to_method_type (mtype, type, mtype->target_type (),
3265 argtypes, argcount, p[-2] == '.');
3266 mtype->set_is_stub (false);
3267 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3270 /* This is the external interface to check_stub_method, above. This
3271 function unstubs all of the signatures for TYPE's METHOD_ID method
3272 name. After calling this function TYPE_FN_FIELD_STUB will be
3273 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3274 correct.
3276 This function unfortunately can not die until stabs do. */
3278 void
3279 check_stub_method_group (struct type *type, int method_id)
3281 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3282 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3284 for (int j = 0; j < len; j++)
3286 if (TYPE_FN_FIELD_STUB (f, j))
3287 check_stub_method (type, method_id, j);
3291 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3292 const struct cplus_struct_type cplus_struct_default = { };
3294 void
3295 allocate_cplus_struct_type (struct type *type)
3297 if (HAVE_CPLUS_STRUCT (type))
3298 /* Structure was already allocated. Nothing more to do. */
3299 return;
3301 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3302 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3303 TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
3304 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3305 set_type_vptr_fieldno (type, -1);
3308 const struct gnat_aux_type gnat_aux_default =
3309 { NULL };
3311 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3312 and allocate the associated gnat-specific data. The gnat-specific
3313 data is also initialized to gnat_aux_default. */
3315 void
3316 allocate_gnat_aux_type (struct type *type)
3318 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3319 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3320 TYPE_ZALLOC (type, sizeof (struct gnat_aux_type));
3321 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3324 /* Helper function to verify floating-point format and size.
3325 BIT is the type size in bits; if BIT equals -1, the size is
3326 determined by the floatformat. Returns size to be used. */
3328 static int
3329 verify_floatformat (int bit, const struct floatformat *floatformat)
3331 gdb_assert (floatformat != NULL);
3333 if (bit == -1)
3334 bit = floatformat->totalsize;
3336 gdb_assert (bit >= 0);
3337 gdb_assert (bit >= floatformat->totalsize);
3339 return bit;
3342 /* Return the floating-point format for a floating-point variable of
3343 type TYPE. */
3345 const struct floatformat *
3346 floatformat_from_type (const struct type *type)
3348 gdb_assert (type->code () == TYPE_CODE_FLT);
3349 gdb_assert (TYPE_FLOATFORMAT (type));
3350 return TYPE_FLOATFORMAT (type);
3353 /* See gdbtypes.h. */
3355 struct type *
3356 init_integer_type (type_allocator &alloc,
3357 int bit, int unsigned_p, const char *name)
3359 struct type *t;
3361 t = alloc.new_type (TYPE_CODE_INT, bit, name);
3362 if (unsigned_p)
3363 t->set_is_unsigned (true);
3365 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3366 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3367 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3369 return t;
3372 /* See gdbtypes.h. */
3374 struct type *
3375 init_character_type (type_allocator &alloc,
3376 int bit, int unsigned_p, const char *name)
3378 struct type *t;
3380 t = alloc.new_type (TYPE_CODE_CHAR, bit, name);
3381 if (unsigned_p)
3382 t->set_is_unsigned (true);
3384 return t;
3387 /* See gdbtypes.h. */
3389 struct type *
3390 init_boolean_type (type_allocator &alloc,
3391 int bit, int unsigned_p, const char *name)
3393 struct type *t;
3395 t = alloc.new_type (TYPE_CODE_BOOL, bit, name);
3396 if (unsigned_p)
3397 t->set_is_unsigned (true);
3399 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3400 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3401 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3403 return t;
3406 /* See gdbtypes.h. */
3408 struct type *
3409 init_float_type (type_allocator &alloc,
3410 int bit, const char *name,
3411 const struct floatformat **floatformats,
3412 enum bfd_endian byte_order)
3414 if (byte_order == BFD_ENDIAN_UNKNOWN)
3416 struct gdbarch *gdbarch = alloc.arch ();
3417 byte_order = gdbarch_byte_order (gdbarch);
3419 const struct floatformat *fmt = floatformats[byte_order];
3420 struct type *t;
3422 bit = verify_floatformat (bit, fmt);
3423 t = alloc.new_type (TYPE_CODE_FLT, bit, name);
3424 TYPE_FLOATFORMAT (t) = fmt;
3426 return t;
3429 /* See gdbtypes.h. */
3431 struct type *
3432 init_decfloat_type (type_allocator &alloc, int bit, const char *name)
3434 return alloc.new_type (TYPE_CODE_DECFLOAT, bit, name);
3437 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3439 bool
3440 can_create_complex_type (struct type *target_type)
3442 return (target_type->code () == TYPE_CODE_INT
3443 || target_type->code () == TYPE_CODE_FLT);
3446 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3447 name. TARGET_TYPE is the component type. */
3449 struct type *
3450 init_complex_type (const char *name, struct type *target_type)
3452 struct type *t;
3454 gdb_assert (can_create_complex_type (target_type));
3456 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3458 if (name == nullptr && target_type->name () != nullptr)
3460 /* No zero-initialization required, initialized by strcpy/strcat
3461 below. */
3462 char *new_name
3463 = (char *) TYPE_ALLOC (target_type,
3464 strlen (target_type->name ())
3465 + strlen ("_Complex ") + 1);
3466 strcpy (new_name, "_Complex ");
3467 strcat (new_name, target_type->name ());
3468 name = new_name;
3471 t = type_allocator (target_type).new_type ();
3472 set_type_code (t, TYPE_CODE_COMPLEX);
3473 t->set_length (2 * target_type->length ());
3474 t->set_name (name);
3476 t->set_target_type (target_type);
3477 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3480 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3483 /* See gdbtypes.h. */
3485 struct type *
3486 init_pointer_type (type_allocator &alloc,
3487 int bit, const char *name, struct type *target_type)
3489 struct type *t;
3491 t = alloc.new_type (TYPE_CODE_PTR, bit, name);
3492 t->set_target_type (target_type);
3493 t->set_is_unsigned (true);
3494 return t;
3497 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3498 BIT is the pointer type size in bits.
3499 UNSIGNED_P should be nonzero if the type is unsigned.
3500 NAME is the type name. */
3502 struct type *
3503 init_fixed_point_type (type_allocator &alloc,
3504 int bit, int unsigned_p, const char *name)
3506 struct type *t;
3508 t = alloc.new_type (TYPE_CODE_FIXED_POINT, bit, name);
3509 if (unsigned_p)
3510 t->set_is_unsigned (true);
3512 return t;
3515 /* See gdbtypes.h. */
3517 unsigned
3518 type_raw_align (struct type *type)
3520 if (type->align_log2 != 0)
3521 return 1 << (type->align_log2 - 1);
3522 return 0;
3525 /* See gdbtypes.h. */
3527 unsigned
3528 type_align (struct type *type)
3530 /* Check alignment provided in the debug information. */
3531 unsigned raw_align = type_raw_align (type);
3532 if (raw_align != 0)
3533 return raw_align;
3535 /* Allow the architecture to provide an alignment. */
3536 ULONGEST align = gdbarch_type_align (type->arch (), type);
3537 if (align != 0)
3538 return align;
3540 switch (type->code ())
3542 case TYPE_CODE_PTR:
3543 case TYPE_CODE_FUNC:
3544 case TYPE_CODE_FLAGS:
3545 case TYPE_CODE_INT:
3546 case TYPE_CODE_RANGE:
3547 case TYPE_CODE_FLT:
3548 case TYPE_CODE_ENUM:
3549 case TYPE_CODE_REF:
3550 case TYPE_CODE_RVALUE_REF:
3551 case TYPE_CODE_CHAR:
3552 case TYPE_CODE_BOOL:
3553 case TYPE_CODE_DECFLOAT:
3554 case TYPE_CODE_METHODPTR:
3555 case TYPE_CODE_MEMBERPTR:
3556 align = type_length_units (check_typedef (type));
3557 break;
3559 case TYPE_CODE_ARRAY:
3560 case TYPE_CODE_COMPLEX:
3561 case TYPE_CODE_TYPEDEF:
3562 align = type_align (type->target_type ());
3563 break;
3565 case TYPE_CODE_STRUCT:
3566 case TYPE_CODE_UNION:
3568 int number_of_non_static_fields = 0;
3569 for (unsigned i = 0; i < type->num_fields (); ++i)
3571 if (!type->field (i).is_static ())
3573 number_of_non_static_fields++;
3574 ULONGEST f_align = type_align (type->field (i).type ());
3575 if (f_align == 0)
3577 /* Don't pretend we know something we don't. */
3578 align = 0;
3579 break;
3581 if (f_align > align)
3582 align = f_align;
3585 /* A struct with no fields, or with only static fields has an
3586 alignment of 1. */
3587 if (number_of_non_static_fields == 0)
3588 align = 1;
3590 break;
3592 case TYPE_CODE_SET:
3593 case TYPE_CODE_STRING:
3594 /* Not sure what to do here, and these can't appear in C or C++
3595 anyway. */
3596 break;
3598 case TYPE_CODE_VOID:
3599 align = 1;
3600 break;
3602 case TYPE_CODE_ERROR:
3603 case TYPE_CODE_METHOD:
3604 default:
3605 break;
3608 if ((align & (align - 1)) != 0)
3610 /* Not a power of 2, so pass. */
3611 align = 0;
3614 return align;
3617 /* See gdbtypes.h. */
3619 bool
3620 set_type_align (struct type *type, ULONGEST align)
3622 /* Must be a power of 2. Zero is ok. */
3623 gdb_assert ((align & (align - 1)) == 0);
3625 unsigned result = 0;
3626 while (align != 0)
3628 ++result;
3629 align >>= 1;
3632 if (result >= (1 << TYPE_ALIGN_BITS))
3633 return false;
3635 type->align_log2 = result;
3636 return true;
3640 /* Queries on types. */
3643 can_dereference (struct type *t)
3645 /* FIXME: Should we return true for references as well as
3646 pointers? */
3647 t = check_typedef (t);
3648 return
3649 (t != NULL
3650 && t->code () == TYPE_CODE_PTR
3651 && t->target_type ()->code () != TYPE_CODE_VOID);
3655 is_integral_type (struct type *t)
3657 t = check_typedef (t);
3658 return
3659 ((t != NULL)
3660 && !is_fixed_point_type (t)
3661 && ((t->code () == TYPE_CODE_INT)
3662 || (t->code () == TYPE_CODE_ENUM)
3663 || (t->code () == TYPE_CODE_FLAGS)
3664 || (t->code () == TYPE_CODE_CHAR)
3665 || (t->code () == TYPE_CODE_RANGE)
3666 || (t->code () == TYPE_CODE_BOOL)));
3670 is_floating_type (struct type *t)
3672 t = check_typedef (t);
3673 return
3674 ((t != NULL)
3675 && ((t->code () == TYPE_CODE_FLT)
3676 || (t->code () == TYPE_CODE_DECFLOAT)));
3679 /* Return true if TYPE is scalar. */
3682 is_scalar_type (struct type *type)
3684 type = check_typedef (type);
3686 if (is_fixed_point_type (type))
3687 return 0; /* Implemented as a scalar, but more like a floating point. */
3689 switch (type->code ())
3691 case TYPE_CODE_ARRAY:
3692 case TYPE_CODE_STRUCT:
3693 case TYPE_CODE_UNION:
3694 case TYPE_CODE_SET:
3695 case TYPE_CODE_STRING:
3696 return 0;
3697 default:
3698 return 1;
3702 /* Return true if T is scalar, or a composite type which in practice has
3703 the memory layout of a scalar type. E.g., an array or struct with only
3704 one scalar element inside it, or a union with only scalar elements. */
3707 is_scalar_type_recursive (struct type *t)
3709 t = check_typedef (t);
3711 if (is_scalar_type (t))
3712 return 1;
3713 /* Are we dealing with an array or string of known dimensions? */
3714 else if ((t->code () == TYPE_CODE_ARRAY
3715 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3716 && t->index_type ()->code () == TYPE_CODE_RANGE)
3718 LONGEST low_bound, high_bound;
3719 struct type *elt_type = check_typedef (t->target_type ());
3721 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3722 return (high_bound == low_bound
3723 && is_scalar_type_recursive (elt_type));
3724 else
3725 return 0;
3727 /* Are we dealing with a struct with one element? */
3728 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3729 return is_scalar_type_recursive (t->field (0).type ());
3730 else if (t->code () == TYPE_CODE_UNION)
3732 int i, n = t->num_fields ();
3734 /* If all elements of the union are scalar, then the union is scalar. */
3735 for (i = 0; i < n; i++)
3736 if (!is_scalar_type_recursive (t->field (i).type ()))
3737 return 0;
3739 return 1;
3742 return 0;
3745 /* Return true is T is a class or a union. False otherwise. */
3748 class_or_union_p (const struct type *t)
3750 return (t->code () == TYPE_CODE_STRUCT
3751 || t->code () == TYPE_CODE_UNION);
3754 /* A helper function which returns true if types A and B represent the
3755 "same" class type. This is true if the types have the same main
3756 type, or the same name. */
3759 class_types_same_p (const struct type *a, const struct type *b)
3761 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3762 || (a->name () && b->name ()
3763 && !strcmp (a->name (), b->name ())));
3766 /* If BASE is an ancestor of DCLASS return the distance between them.
3767 otherwise return -1;
3770 class A {};
3771 class B: public A {};
3772 class C: public B {};
3773 class D: C {};
3775 distance_to_ancestor (A, A, 0) = 0
3776 distance_to_ancestor (A, B, 0) = 1
3777 distance_to_ancestor (A, C, 0) = 2
3778 distance_to_ancestor (A, D, 0) = 3
3780 If PUBLIC is 1 then only public ancestors are considered,
3781 and the function returns the distance only if BASE is a public ancestor
3782 of DCLASS.
3785 distance_to_ancestor (A, D, 1) = -1. */
3787 static int
3788 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3790 int i;
3791 int d;
3793 base = check_typedef (base);
3794 dclass = check_typedef (dclass);
3796 if (class_types_same_p (base, dclass))
3797 return 0;
3799 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3801 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3802 continue;
3804 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3805 if (d >= 0)
3806 return 1 + d;
3809 return -1;
3812 /* Check whether BASE is an ancestor or base class or DCLASS
3813 Return 1 if so, and 0 if not.
3814 Note: If BASE and DCLASS are of the same type, this function
3815 will return 1. So for some class A, is_ancestor (A, A) will
3816 return 1. */
3819 is_ancestor (struct type *base, struct type *dclass)
3821 return distance_to_ancestor (base, dclass, 0) >= 0;
3824 /* Like is_ancestor, but only returns true when BASE is a public
3825 ancestor of DCLASS. */
3828 is_public_ancestor (struct type *base, struct type *dclass)
3830 return distance_to_ancestor (base, dclass, 1) >= 0;
3833 /* A helper function for is_unique_ancestor. */
3835 static int
3836 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3837 int *offset,
3838 const gdb_byte *valaddr, int embedded_offset,
3839 CORE_ADDR address, struct value *val)
3841 int i, count = 0;
3843 base = check_typedef (base);
3844 dclass = check_typedef (dclass);
3846 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3848 struct type *iter;
3849 int this_offset;
3851 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3853 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3854 address, val);
3856 if (class_types_same_p (base, iter))
3858 /* If this is the first subclass, set *OFFSET and set count
3859 to 1. Otherwise, if this is at the same offset as
3860 previous instances, do nothing. Otherwise, increment
3861 count. */
3862 if (*offset == -1)
3864 *offset = this_offset;
3865 count = 1;
3867 else if (this_offset == *offset)
3869 /* Nothing. */
3871 else
3872 ++count;
3874 else
3875 count += is_unique_ancestor_worker (base, iter, offset,
3876 valaddr,
3877 embedded_offset + this_offset,
3878 address, val);
3881 return count;
3884 /* Like is_ancestor, but only returns true if BASE is a unique base
3885 class of the type of VAL. */
3888 is_unique_ancestor (struct type *base, struct value *val)
3890 int offset = -1;
3892 return is_unique_ancestor_worker (base, val->type (), &offset,
3893 val->contents_for_printing ().data (),
3894 val->embedded_offset (),
3895 val->address (), val) == 1;
3898 /* See gdbtypes.h. */
3900 enum bfd_endian
3901 type_byte_order (const struct type *type)
3903 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
3904 if (type->endianity_is_not_default ())
3906 if (byteorder == BFD_ENDIAN_BIG)
3907 return BFD_ENDIAN_LITTLE;
3908 else
3910 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
3911 return BFD_ENDIAN_BIG;
3915 return byteorder;
3918 /* See gdbtypes.h. */
3920 bool
3921 is_nocall_function (const struct type *type)
3923 if (type->code () != TYPE_CODE_FUNC && type->code () != TYPE_CODE_METHOD)
3924 return false;
3926 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
3930 /* Overload resolution. */
3932 /* Return the sum of the rank of A with the rank of B. */
3934 struct rank
3935 sum_ranks (struct rank a, struct rank b)
3937 struct rank c;
3938 c.rank = a.rank + b.rank;
3939 c.subrank = a.subrank + b.subrank;
3940 return c;
3943 /* Compare rank A and B and return:
3944 0 if a = b
3945 1 if a is better than b
3946 -1 if b is better than a. */
3949 compare_ranks (struct rank a, struct rank b)
3951 if (a.rank == b.rank)
3953 if (a.subrank == b.subrank)
3954 return 0;
3955 if (a.subrank < b.subrank)
3956 return 1;
3957 if (a.subrank > b.subrank)
3958 return -1;
3961 if (a.rank < b.rank)
3962 return 1;
3964 /* a.rank > b.rank */
3965 return -1;
3968 /* Functions for overload resolution begin here. */
3970 /* Compare two badness vectors A and B and return the result.
3971 0 => A and B are identical
3972 1 => A and B are incomparable
3973 2 => A is better than B
3974 3 => A is worse than B */
3977 compare_badness (const badness_vector &a, const badness_vector &b)
3979 int i;
3980 int tmp;
3981 /* Any positives in comparison? */
3982 bool found_pos = false;
3983 /* Any negatives in comparison? */
3984 bool found_neg = false;
3985 /* Did A have any INVALID_CONVERSION entries. */
3986 bool a_invalid = false;
3987 /* Did B have any INVALID_CONVERSION entries. */
3988 bool b_invalid = false;
3990 /* differing sizes => incomparable */
3991 if (a.size () != b.size ())
3992 return 1;
3994 /* Subtract b from a */
3995 for (i = 0; i < a.size (); i++)
3997 tmp = compare_ranks (b[i], a[i]);
3998 if (tmp > 0)
3999 found_pos = true;
4000 else if (tmp < 0)
4001 found_neg = true;
4002 if (a[i].rank >= INVALID_CONVERSION)
4003 a_invalid = true;
4004 if (b[i].rank >= INVALID_CONVERSION)
4005 b_invalid = true;
4008 /* B will only be considered better than or incomparable to A if
4009 they both have invalid entries, or if neither does. That is, if
4010 A has only valid entries, and B has an invalid entry, then A will
4011 be considered better than B, even if B happens to be better for
4012 some parameter. */
4013 if (a_invalid != b_invalid)
4015 if (a_invalid)
4016 return 3; /* A > B */
4017 return 2; /* A < B */
4019 else if (found_pos)
4021 if (found_neg)
4022 return 1; /* incomparable */
4023 else
4024 return 3; /* A > B */
4026 else
4027 /* no positives */
4029 if (found_neg)
4030 return 2; /* A < B */
4031 else
4032 return 0; /* A == B */
4036 /* Rank a function by comparing its parameter types (PARMS), to the
4037 types of an argument list (ARGS). Return the badness vector. This
4038 has ARGS.size() + 1 entries. */
4040 badness_vector
4041 rank_function (gdb::array_view<type *> parms,
4042 gdb::array_view<value *> args,
4043 bool varargs)
4045 /* add 1 for the length-match rank. */
4046 badness_vector bv;
4047 bv.reserve (1 + args.size ());
4049 /* First compare the lengths of the supplied lists.
4050 If there is a mismatch, set it to a high value. */
4052 /* pai/1997-06-03 FIXME: when we have debug info about default
4053 arguments and ellipsis parameter lists, we should consider those
4054 and rank the length-match more finely. */
4056 bv.push_back ((args.size () != parms.size ()
4057 && (! varargs || args.size () < parms.size ()))
4058 ? LENGTH_MISMATCH_BADNESS
4059 : EXACT_MATCH_BADNESS);
4061 /* Now rank all the parameters of the candidate function. */
4062 size_t min_len = std::min (parms.size (), args.size ());
4064 for (size_t i = 0; i < min_len; i++)
4065 bv.push_back (rank_one_type (parms[i], args[i]->type (),
4066 args[i]));
4068 /* If more arguments than parameters, add dummy entries. */
4069 for (size_t i = min_len; i < args.size (); i++)
4070 bv.push_back (varargs ? VARARG_BADNESS : TOO_FEW_PARAMS_BADNESS);
4072 return bv;
4075 /* Compare the names of two integer types, assuming that any sign
4076 qualifiers have been checked already. We do it this way because
4077 there may be an "int" in the name of one of the types. */
4079 static int
4080 integer_types_same_name_p (const char *first, const char *second)
4082 int first_p, second_p;
4084 /* If both are shorts, return 1; if neither is a short, keep
4085 checking. */
4086 first_p = (strstr (first, "short") != NULL);
4087 second_p = (strstr (second, "short") != NULL);
4088 if (first_p && second_p)
4089 return 1;
4090 if (first_p || second_p)
4091 return 0;
4093 /* Likewise for long. */
4094 first_p = (strstr (first, "long") != NULL);
4095 second_p = (strstr (second, "long") != NULL);
4096 if (first_p && second_p)
4097 return 1;
4098 if (first_p || second_p)
4099 return 0;
4101 /* Likewise for char. */
4102 first_p = (strstr (first, "char") != NULL);
4103 second_p = (strstr (second, "char") != NULL);
4104 if (first_p && second_p)
4105 return 1;
4106 if (first_p || second_p)
4107 return 0;
4109 /* They must both be ints. */
4110 return 1;
4113 /* Compares type A to type B. Returns true if they represent the same
4114 type, false otherwise. */
4116 bool
4117 types_equal (struct type *a, struct type *b)
4119 /* Identical type pointers. */
4120 /* However, this still doesn't catch all cases of same type for b
4121 and a. The reason is that builtin types are different from
4122 the same ones constructed from the object. */
4123 if (a == b)
4124 return true;
4126 /* Resolve typedefs */
4127 if (a->code () == TYPE_CODE_TYPEDEF)
4128 a = check_typedef (a);
4129 if (b->code () == TYPE_CODE_TYPEDEF)
4130 b = check_typedef (b);
4132 /* Check if identical after resolving typedefs. */
4133 if (a == b)
4134 return true;
4136 /* If after resolving typedefs a and b are not of the same type
4137 code then they are not equal. */
4138 if (a->code () != b->code ())
4139 return false;
4141 /* If a and b are both pointers types or both reference types then
4142 they are equal of the same type iff the objects they refer to are
4143 of the same type. */
4144 if (a->code () == TYPE_CODE_PTR
4145 || a->code () == TYPE_CODE_REF)
4146 return types_equal (a->target_type (),
4147 b->target_type ());
4149 /* Well, damnit, if the names are exactly the same, I'll say they
4150 are exactly the same. This happens when we generate method
4151 stubs. The types won't point to the same address, but they
4152 really are the same. */
4154 if (a->name () && b->name ()
4155 && strcmp (a->name (), b->name ()) == 0)
4156 return true;
4158 /* Two function types are equal if their argument and return types
4159 are equal. */
4160 if (a->code () == TYPE_CODE_FUNC)
4162 int i;
4164 if (a->num_fields () != b->num_fields ())
4165 return false;
4167 if (!types_equal (a->target_type (), b->target_type ()))
4168 return false;
4170 for (i = 0; i < a->num_fields (); ++i)
4171 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4172 return false;
4174 return true;
4177 return false;
4180 /* Deep comparison of types. */
4182 /* An entry in the type-equality bcache. */
4184 struct type_equality_entry
4186 type_equality_entry (struct type *t1, struct type *t2)
4187 : type1 (t1),
4188 type2 (t2)
4192 struct type *type1, *type2;
4195 /* A helper function to compare two strings. Returns true if they are
4196 the same, false otherwise. Handles NULLs properly. */
4198 static bool
4199 compare_maybe_null_strings (const char *s, const char *t)
4201 if (s == NULL || t == NULL)
4202 return s == t;
4203 return strcmp (s, t) == 0;
4206 /* A helper function for check_types_worklist that checks two types for
4207 "deep" equality. Returns true if the types are considered the
4208 same, false otherwise. */
4210 static bool
4211 check_types_equal (struct type *type1, struct type *type2,
4212 std::vector<type_equality_entry> *worklist)
4214 type1 = check_typedef (type1);
4215 type2 = check_typedef (type2);
4217 if (type1 == type2)
4218 return true;
4220 if (type1->code () != type2->code ()
4221 || type1->length () != type2->length ()
4222 || type1->is_unsigned () != type2->is_unsigned ()
4223 || type1->has_no_signedness () != type2->has_no_signedness ()
4224 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4225 || type1->has_varargs () != type2->has_varargs ()
4226 || type1->is_vector () != type2->is_vector ()
4227 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4228 || type1->instance_flags () != type2->instance_flags ()
4229 || type1->num_fields () != type2->num_fields ())
4230 return false;
4232 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4233 return false;
4234 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4235 return false;
4237 if (type1->code () == TYPE_CODE_RANGE)
4239 if (*type1->bounds () != *type2->bounds ())
4240 return false;
4242 else
4244 int i;
4246 for (i = 0; i < type1->num_fields (); ++i)
4248 const struct field *field1 = &type1->field (i);
4249 const struct field *field2 = &type2->field (i);
4251 if (field1->is_artificial () != field2->is_artificial ()
4252 || field1->bitsize () != field2->bitsize ()
4253 || field1->loc_kind () != field2->loc_kind ())
4254 return false;
4255 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4256 return false;
4257 switch (field1->loc_kind ())
4259 case FIELD_LOC_KIND_BITPOS:
4260 if (field1->loc_bitpos () != field2->loc_bitpos ())
4261 return false;
4262 break;
4263 case FIELD_LOC_KIND_ENUMVAL:
4264 if (field1->loc_enumval () != field2->loc_enumval ())
4265 return false;
4266 /* Don't compare types of enum fields, because they don't
4267 have a type. */
4268 continue;
4269 case FIELD_LOC_KIND_PHYSADDR:
4270 if (field1->loc_physaddr () != field2->loc_physaddr ())
4271 return false;
4272 break;
4273 case FIELD_LOC_KIND_PHYSNAME:
4274 if (!compare_maybe_null_strings (field1->loc_physname (),
4275 field2->loc_physname ()))
4276 return false;
4277 break;
4278 case FIELD_LOC_KIND_DWARF_BLOCK:
4280 struct dwarf2_locexpr_baton *block1, *block2;
4282 block1 = field1->loc_dwarf_block ();
4283 block2 = field2->loc_dwarf_block ();
4284 if (block1->per_cu != block2->per_cu
4285 || block1->size != block2->size
4286 || memcmp (block1->data, block2->data, block1->size) != 0)
4287 return false;
4289 break;
4290 default:
4291 internal_error (_("Unsupported field kind "
4292 "%d by check_types_equal"),
4293 field1->loc_kind ());
4296 worklist->emplace_back (field1->type (), field2->type ());
4300 if (type1->target_type () != NULL)
4302 if (type2->target_type () == NULL)
4303 return false;
4305 worklist->emplace_back (type1->target_type (),
4306 type2->target_type ());
4308 else if (type2->target_type () != NULL)
4309 return false;
4311 return true;
4314 /* Check types on a worklist for equality. Returns false if any pair
4315 is not equal, true if they are all considered equal. */
4317 static bool
4318 check_types_worklist (std::vector<type_equality_entry> *worklist,
4319 gdb::bcache *cache)
4321 while (!worklist->empty ())
4323 bool added;
4325 struct type_equality_entry entry = std::move (worklist->back ());
4326 worklist->pop_back ();
4328 /* If the type pair has already been visited, we know it is
4329 ok. */
4330 cache->insert (&entry, sizeof (entry), &added);
4331 if (!added)
4332 continue;
4334 if (!check_types_equal (entry.type1, entry.type2, worklist))
4335 return false;
4338 return true;
4341 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4342 "deep comparison". Otherwise return false. */
4344 bool
4345 types_deeply_equal (struct type *type1, struct type *type2)
4347 std::vector<type_equality_entry> worklist;
4349 gdb_assert (type1 != NULL && type2 != NULL);
4351 /* Early exit for the simple case. */
4352 if (type1 == type2)
4353 return true;
4355 gdb::bcache cache;
4356 worklist.emplace_back (type1, type2);
4357 return check_types_worklist (&worklist, &cache);
4360 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4361 Otherwise return one. */
4364 type_not_allocated (const struct type *type)
4366 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4368 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4371 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4372 Otherwise return one. */
4375 type_not_associated (const struct type *type)
4377 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4379 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4382 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4384 static struct rank
4385 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4387 struct rank rank = {0,0};
4389 switch (arg->code ())
4391 case TYPE_CODE_PTR:
4393 /* Allowed pointer conversions are:
4394 (a) pointer to void-pointer conversion. */
4395 if (parm->target_type ()->code () == TYPE_CODE_VOID)
4396 return VOID_PTR_CONVERSION_BADNESS;
4398 /* (b) pointer to ancestor-pointer conversion. */
4399 rank.subrank = distance_to_ancestor (parm->target_type (),
4400 arg->target_type (),
4402 if (rank.subrank >= 0)
4403 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4405 return INCOMPATIBLE_TYPE_BADNESS;
4406 case TYPE_CODE_ARRAY:
4408 struct type *t1 = parm->target_type ();
4409 struct type *t2 = arg->target_type ();
4411 if (types_equal (t1, t2))
4413 /* Make sure they are CV equal. */
4414 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4415 rank.subrank |= CV_CONVERSION_CONST;
4416 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4417 rank.subrank |= CV_CONVERSION_VOLATILE;
4418 if (rank.subrank != 0)
4419 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4420 return EXACT_MATCH_BADNESS;
4422 return INCOMPATIBLE_TYPE_BADNESS;
4424 case TYPE_CODE_FUNC:
4425 return rank_one_type (parm->target_type (), arg, NULL);
4426 case TYPE_CODE_INT:
4427 if (value != NULL && value->type ()->code () == TYPE_CODE_INT)
4429 if (value_as_long (value) == 0)
4431 /* Null pointer conversion: allow it to be cast to a pointer.
4432 [4.10.1 of C++ standard draft n3290] */
4433 return NULL_POINTER_CONVERSION_BADNESS;
4435 else
4437 /* If type checking is disabled, allow the conversion. */
4438 if (!strict_type_checking)
4439 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4442 [[fallthrough]];
4443 case TYPE_CODE_ENUM:
4444 case TYPE_CODE_FLAGS:
4445 case TYPE_CODE_CHAR:
4446 case TYPE_CODE_RANGE:
4447 case TYPE_CODE_BOOL:
4448 default:
4449 return INCOMPATIBLE_TYPE_BADNESS;
4453 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4455 static struct rank
4456 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4458 switch (arg->code ())
4460 case TYPE_CODE_PTR:
4461 case TYPE_CODE_ARRAY:
4462 return rank_one_type (parm->target_type (),
4463 arg->target_type (), NULL);
4464 default:
4465 return INCOMPATIBLE_TYPE_BADNESS;
4469 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4471 static struct rank
4472 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4474 switch (arg->code ())
4476 case TYPE_CODE_PTR: /* funcptr -> func */
4477 return rank_one_type (parm, arg->target_type (), NULL);
4478 default:
4479 return INCOMPATIBLE_TYPE_BADNESS;
4483 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4485 static struct rank
4486 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4488 switch (arg->code ())
4490 case TYPE_CODE_INT:
4491 if (arg->length () == parm->length ())
4493 /* Deal with signed, unsigned, and plain chars and
4494 signed and unsigned ints. */
4495 if (parm->has_no_signedness ())
4497 /* This case only for character types. */
4498 if (arg->has_no_signedness ())
4499 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4500 else /* signed/unsigned char -> plain char */
4501 return INTEGER_CONVERSION_BADNESS;
4503 else if (parm->is_unsigned ())
4505 if (arg->is_unsigned ())
4507 /* unsigned int -> unsigned int, or
4508 unsigned long -> unsigned long */
4509 if (integer_types_same_name_p (parm->name (),
4510 arg->name ()))
4511 return EXACT_MATCH_BADNESS;
4512 else if (integer_types_same_name_p (arg->name (),
4513 "int")
4514 && integer_types_same_name_p (parm->name (),
4515 "long"))
4516 /* unsigned int -> unsigned long */
4517 return INTEGER_PROMOTION_BADNESS;
4518 else
4519 /* unsigned long -> unsigned int */
4520 return INTEGER_CONVERSION_BADNESS;
4522 else
4524 if (integer_types_same_name_p (arg->name (),
4525 "long")
4526 && integer_types_same_name_p (parm->name (),
4527 "int"))
4528 /* signed long -> unsigned int */
4529 return INTEGER_CONVERSION_BADNESS;
4530 else
4531 /* signed int/long -> unsigned int/long */
4532 return INTEGER_CONVERSION_BADNESS;
4535 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4537 if (integer_types_same_name_p (parm->name (),
4538 arg->name ()))
4539 return EXACT_MATCH_BADNESS;
4540 else if (integer_types_same_name_p (arg->name (),
4541 "int")
4542 && integer_types_same_name_p (parm->name (),
4543 "long"))
4544 return INTEGER_PROMOTION_BADNESS;
4545 else
4546 return INTEGER_CONVERSION_BADNESS;
4548 else
4549 return INTEGER_CONVERSION_BADNESS;
4551 else if (arg->length () < parm->length ())
4552 return INTEGER_PROMOTION_BADNESS;
4553 else
4554 return INTEGER_CONVERSION_BADNESS;
4555 case TYPE_CODE_ENUM:
4556 case TYPE_CODE_FLAGS:
4557 case TYPE_CODE_CHAR:
4558 case TYPE_CODE_RANGE:
4559 case TYPE_CODE_BOOL:
4560 if (arg->is_declared_class ())
4561 return INCOMPATIBLE_TYPE_BADNESS;
4562 return INTEGER_PROMOTION_BADNESS;
4563 case TYPE_CODE_FLT:
4564 return INT_FLOAT_CONVERSION_BADNESS;
4565 case TYPE_CODE_PTR:
4566 return NS_POINTER_CONVERSION_BADNESS;
4567 default:
4568 return INCOMPATIBLE_TYPE_BADNESS;
4572 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4574 static struct rank
4575 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4577 switch (arg->code ())
4579 case TYPE_CODE_INT:
4580 case TYPE_CODE_CHAR:
4581 case TYPE_CODE_RANGE:
4582 case TYPE_CODE_BOOL:
4583 case TYPE_CODE_ENUM:
4584 if (parm->is_declared_class () || arg->is_declared_class ())
4585 return INCOMPATIBLE_TYPE_BADNESS;
4586 return INTEGER_CONVERSION_BADNESS;
4587 case TYPE_CODE_FLT:
4588 return INT_FLOAT_CONVERSION_BADNESS;
4589 default:
4590 return INCOMPATIBLE_TYPE_BADNESS;
4594 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4596 static struct rank
4597 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4599 switch (arg->code ())
4601 case TYPE_CODE_RANGE:
4602 case TYPE_CODE_BOOL:
4603 case TYPE_CODE_ENUM:
4604 if (arg->is_declared_class ())
4605 return INCOMPATIBLE_TYPE_BADNESS;
4606 return INTEGER_CONVERSION_BADNESS;
4607 case TYPE_CODE_FLT:
4608 return INT_FLOAT_CONVERSION_BADNESS;
4609 case TYPE_CODE_INT:
4610 if (arg->length () > parm->length ())
4611 return INTEGER_CONVERSION_BADNESS;
4612 else if (arg->length () < parm->length ())
4613 return INTEGER_PROMOTION_BADNESS;
4614 [[fallthrough]];
4615 case TYPE_CODE_CHAR:
4616 /* Deal with signed, unsigned, and plain chars for C++ and
4617 with int cases falling through from previous case. */
4618 if (parm->has_no_signedness ())
4620 if (arg->has_no_signedness ())
4621 return EXACT_MATCH_BADNESS;
4622 else
4623 return INTEGER_CONVERSION_BADNESS;
4625 else if (parm->is_unsigned ())
4627 if (arg->is_unsigned ())
4628 return EXACT_MATCH_BADNESS;
4629 else
4630 return INTEGER_PROMOTION_BADNESS;
4632 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4633 return EXACT_MATCH_BADNESS;
4634 else
4635 return INTEGER_CONVERSION_BADNESS;
4636 default:
4637 return INCOMPATIBLE_TYPE_BADNESS;
4641 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4643 static struct rank
4644 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4646 switch (arg->code ())
4648 case TYPE_CODE_INT:
4649 case TYPE_CODE_CHAR:
4650 case TYPE_CODE_RANGE:
4651 case TYPE_CODE_BOOL:
4652 case TYPE_CODE_ENUM:
4653 return INTEGER_CONVERSION_BADNESS;
4654 case TYPE_CODE_FLT:
4655 return INT_FLOAT_CONVERSION_BADNESS;
4656 default:
4657 return INCOMPATIBLE_TYPE_BADNESS;
4661 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4663 static struct rank
4664 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4666 switch (arg->code ())
4668 /* n3290 draft, section 4.12.1 (conv.bool):
4670 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4671 pointer to member type can be converted to a prvalue of type
4672 bool. A zero value, null pointer value, or null member pointer
4673 value is converted to false; any other value is converted to
4674 true. A prvalue of type std::nullptr_t can be converted to a
4675 prvalue of type bool; the resulting value is false." */
4676 case TYPE_CODE_INT:
4677 case TYPE_CODE_CHAR:
4678 case TYPE_CODE_ENUM:
4679 case TYPE_CODE_FLT:
4680 case TYPE_CODE_MEMBERPTR:
4681 case TYPE_CODE_PTR:
4682 return BOOL_CONVERSION_BADNESS;
4683 case TYPE_CODE_RANGE:
4684 return INCOMPATIBLE_TYPE_BADNESS;
4685 case TYPE_CODE_BOOL:
4686 return EXACT_MATCH_BADNESS;
4687 default:
4688 return INCOMPATIBLE_TYPE_BADNESS;
4692 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4694 static struct rank
4695 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4697 switch (arg->code ())
4699 case TYPE_CODE_FLT:
4700 if (arg->length () < parm->length ())
4701 return FLOAT_PROMOTION_BADNESS;
4702 else if (arg->length () == parm->length ())
4703 return EXACT_MATCH_BADNESS;
4704 else
4705 return FLOAT_CONVERSION_BADNESS;
4706 case TYPE_CODE_INT:
4707 case TYPE_CODE_BOOL:
4708 case TYPE_CODE_ENUM:
4709 case TYPE_CODE_RANGE:
4710 case TYPE_CODE_CHAR:
4711 return INT_FLOAT_CONVERSION_BADNESS;
4712 default:
4713 return INCOMPATIBLE_TYPE_BADNESS;
4717 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4719 static struct rank
4720 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4722 switch (arg->code ())
4723 { /* Strictly not needed for C++, but... */
4724 case TYPE_CODE_FLT:
4725 return FLOAT_PROMOTION_BADNESS;
4726 case TYPE_CODE_COMPLEX:
4727 return EXACT_MATCH_BADNESS;
4728 default:
4729 return INCOMPATIBLE_TYPE_BADNESS;
4733 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4735 static struct rank
4736 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4738 struct rank rank = {0, 0};
4740 switch (arg->code ())
4742 case TYPE_CODE_STRUCT:
4743 /* Check for derivation */
4744 rank.subrank = distance_to_ancestor (parm, arg, 0);
4745 if (rank.subrank >= 0)
4746 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4747 [[fallthrough]];
4748 default:
4749 return INCOMPATIBLE_TYPE_BADNESS;
4753 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4755 static struct rank
4756 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4758 switch (arg->code ())
4760 /* Not in C++ */
4761 case TYPE_CODE_SET:
4762 return rank_one_type (parm->field (0).type (),
4763 arg->field (0).type (), NULL);
4764 default:
4765 return INCOMPATIBLE_TYPE_BADNESS;
4769 /* Compare one type (PARM) for compatibility with another (ARG).
4770 * PARM is intended to be the parameter type of a function; and
4771 * ARG is the supplied argument's type. This function tests if
4772 * the latter can be converted to the former.
4773 * VALUE is the argument's value or NULL if none (or called recursively)
4775 * Return 0 if they are identical types;
4776 * Otherwise, return an integer which corresponds to how compatible
4777 * PARM is to ARG. The higher the return value, the worse the match.
4778 * Generally the "bad" conversions are all uniformly assigned
4779 * INVALID_CONVERSION. */
4781 struct rank
4782 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4784 struct rank rank = {0,0};
4786 /* Resolve typedefs */
4787 if (parm->code () == TYPE_CODE_TYPEDEF)
4788 parm = check_typedef (parm);
4789 if (arg->code () == TYPE_CODE_TYPEDEF)
4790 arg = check_typedef (arg);
4792 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4794 if (value->lval () == not_lval)
4796 /* Rvalues should preferably bind to rvalue references or const
4797 lvalue references. */
4798 if (parm->code () == TYPE_CODE_RVALUE_REF)
4799 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4800 else if (TYPE_CONST (parm->target_type ()))
4801 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4802 else
4803 return INCOMPATIBLE_TYPE_BADNESS;
4804 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4806 else
4808 /* It's illegal to pass an lvalue as an rvalue. */
4809 if (parm->code () == TYPE_CODE_RVALUE_REF)
4810 return INCOMPATIBLE_TYPE_BADNESS;
4814 if (types_equal (parm, arg))
4816 struct type *t1 = parm;
4817 struct type *t2 = arg;
4819 /* For pointers and references, compare target type. */
4820 if (parm->is_pointer_or_reference ())
4822 t1 = parm->target_type ();
4823 t2 = arg->target_type ();
4826 /* Make sure they are CV equal, too. */
4827 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4828 rank.subrank |= CV_CONVERSION_CONST;
4829 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4830 rank.subrank |= CV_CONVERSION_VOLATILE;
4831 if (rank.subrank != 0)
4832 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4833 return EXACT_MATCH_BADNESS;
4836 /* See through references, since we can almost make non-references
4837 references. */
4839 if (TYPE_IS_REFERENCE (arg))
4840 return (sum_ranks (rank_one_type (parm, arg->target_type (), NULL),
4841 REFERENCE_SEE_THROUGH_BADNESS));
4842 if (TYPE_IS_REFERENCE (parm))
4843 return (sum_ranks (rank_one_type (parm->target_type (), arg, NULL),
4844 REFERENCE_SEE_THROUGH_BADNESS));
4845 if (overload_debug)
4847 /* Debugging only. */
4848 gdb_printf (gdb_stderr,
4849 "------ Arg is %s [%d], parm is %s [%d]\n",
4850 arg->name (), arg->code (),
4851 parm->name (), parm->code ());
4854 /* x -> y means arg of type x being supplied for parameter of type y. */
4856 switch (parm->code ())
4858 case TYPE_CODE_PTR:
4859 return rank_one_type_parm_ptr (parm, arg, value);
4860 case TYPE_CODE_ARRAY:
4861 return rank_one_type_parm_array (parm, arg, value);
4862 case TYPE_CODE_FUNC:
4863 return rank_one_type_parm_func (parm, arg, value);
4864 case TYPE_CODE_INT:
4865 return rank_one_type_parm_int (parm, arg, value);
4866 case TYPE_CODE_ENUM:
4867 return rank_one_type_parm_enum (parm, arg, value);
4868 case TYPE_CODE_CHAR:
4869 return rank_one_type_parm_char (parm, arg, value);
4870 case TYPE_CODE_RANGE:
4871 return rank_one_type_parm_range (parm, arg, value);
4872 case TYPE_CODE_BOOL:
4873 return rank_one_type_parm_bool (parm, arg, value);
4874 case TYPE_CODE_FLT:
4875 return rank_one_type_parm_float (parm, arg, value);
4876 case TYPE_CODE_COMPLEX:
4877 return rank_one_type_parm_complex (parm, arg, value);
4878 case TYPE_CODE_STRUCT:
4879 return rank_one_type_parm_struct (parm, arg, value);
4880 case TYPE_CODE_SET:
4881 return rank_one_type_parm_set (parm, arg, value);
4882 default:
4883 return INCOMPATIBLE_TYPE_BADNESS;
4884 } /* switch (arg->code ()) */
4887 /* End of functions for overload resolution. */
4890 /* Note the first arg should be the "this" pointer, we may not want to
4891 include it since we may get into a infinitely recursive
4892 situation. */
4894 static void
4895 print_args (struct field *args, int nargs, int spaces)
4897 if (args != NULL)
4899 int i;
4901 for (i = 0; i < nargs; i++)
4903 gdb_printf
4904 ("%*s[%d] name '%s'\n", spaces, "", i,
4905 args[i].name () != NULL ? args[i].name () : "<NULL>");
4906 recursive_dump_type (args[i].type (), spaces + 2);
4911 static void
4912 dump_fn_fieldlists (struct type *type, int spaces)
4914 int method_idx;
4915 int overload_idx;
4916 struct fn_field *f;
4918 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
4919 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
4920 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
4922 f = TYPE_FN_FIELDLIST1 (type, method_idx);
4923 gdb_printf
4924 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
4925 method_idx,
4926 TYPE_FN_FIELDLIST_NAME (type, method_idx),
4927 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
4928 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
4929 for (overload_idx = 0;
4930 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
4931 overload_idx++)
4933 gdb_printf
4934 ("%*s[%d] physname '%s' (%s)\n",
4935 spaces + 4, "", overload_idx,
4936 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
4937 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
4938 overload_idx)));
4939 gdb_printf
4940 ("%*stype %s\n", spaces + 8, "",
4941 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
4943 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
4944 spaces + 8 + 2);
4946 gdb_printf
4947 ("%*sargs %s\n", spaces + 8, "",
4948 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
4949 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
4950 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
4951 spaces + 8 + 2);
4952 gdb_printf
4953 ("%*sfcontext %s\n", spaces + 8, "",
4954 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
4955 overload_idx)));
4957 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
4958 TYPE_FN_FIELD_CONST (f, overload_idx));
4959 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
4960 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
4961 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
4962 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
4963 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
4964 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
4965 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
4966 TYPE_FN_FIELD_STUB (f, overload_idx));
4967 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
4968 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
4969 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
4970 TYPE_FN_FIELD_DELETED (f, overload_idx));
4971 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
4972 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
4977 static void
4978 print_cplus_stuff (struct type *type, int spaces)
4980 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
4981 TYPE_VPTR_FIELDNO (type));
4982 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
4983 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
4984 if (TYPE_VPTR_BASETYPE (type) != NULL)
4985 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
4987 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
4988 TYPE_N_BASECLASSES (type));
4989 gdb_printf ("%*snfn_fields %d\n", spaces, "",
4990 TYPE_NFN_FIELDS (type));
4991 if (TYPE_NFN_FIELDS (type) > 0)
4993 dump_fn_fieldlists (type, spaces);
4996 gdb_printf ("%*scalling_convention %d\n", spaces, "",
4997 TYPE_CPLUS_CALLING_CONVENTION (type));
5000 /* Print the contents of the TYPE's type_specific union, assuming that
5001 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5003 static void
5004 print_gnat_stuff (struct type *type, int spaces)
5006 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5008 if (descriptive_type == NULL)
5009 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5010 else
5012 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5013 recursive_dump_type (descriptive_type, spaces + 4);
5017 /* Print the contents of the TYPE's type_specific union, assuming that
5018 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5020 static void
5021 print_fixed_point_type_info (struct type *type, int spaces)
5023 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5024 type->fixed_point_scaling_factor ().str ().c_str ());
5027 static struct obstack dont_print_type_obstack;
5029 /* Print the dynamic_prop PROP. */
5031 static void
5032 dump_dynamic_prop (dynamic_prop const& prop)
5034 switch (prop.kind ())
5036 case PROP_CONST:
5037 gdb_printf ("%s", plongest (prop.const_val ()));
5038 break;
5039 case PROP_UNDEFINED:
5040 gdb_printf ("(undefined)");
5041 break;
5042 case PROP_LOCEXPR:
5043 case PROP_LOCLIST:
5044 gdb_printf ("(dynamic)");
5045 break;
5046 default:
5047 gdb_assert_not_reached ("unhandled prop kind");
5048 break;
5052 /* Return a string that represents a type code. */
5053 static const char *
5054 type_code_name (type_code code)
5056 switch (code)
5058 #define OP(X) case X: return # X;
5059 #include "type-codes.def"
5060 #undef OP
5062 case TYPE_CODE_UNDEF:
5063 return "TYPE_CODE_UNDEF";
5066 gdb_assert_not_reached ("unhandled type_code");
5069 void
5070 recursive_dump_type (struct type *type, int spaces)
5072 int idx;
5074 if (spaces == 0)
5075 obstack_begin (&dont_print_type_obstack, 0);
5077 if (type->num_fields () > 0
5078 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5080 struct type **first_dont_print
5081 = (struct type **) obstack_base (&dont_print_type_obstack);
5083 int i = (struct type **)
5084 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5086 while (--i >= 0)
5088 if (type == first_dont_print[i])
5090 gdb_printf ("%*stype node %s", spaces, "",
5091 host_address_to_string (type));
5092 gdb_printf (_(" <same as already seen type>\n"));
5093 return;
5097 obstack_ptr_grow (&dont_print_type_obstack, type);
5100 gdb_printf ("%*stype node %s\n", spaces, "",
5101 host_address_to_string (type));
5102 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5103 type->name () ? type->name () : "<NULL>",
5104 host_address_to_string (type->name ()));
5105 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5106 gdb_printf ("(%s)", type_code_name (type->code ()));
5107 gdb_puts ("\n");
5108 gdb_printf ("%*slength %s\n", spaces, "",
5109 pulongest (type->length ()));
5110 if (type->is_objfile_owned ())
5111 gdb_printf ("%*sobjfile %s\n", spaces, "",
5112 host_address_to_string (type->objfile_owner ()));
5113 else
5114 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5115 host_address_to_string (type->arch_owner ()));
5116 gdb_printf ("%*starget_type %s\n", spaces, "",
5117 host_address_to_string (type->target_type ()));
5118 if (type->target_type () != NULL)
5120 recursive_dump_type (type->target_type (), spaces + 2);
5122 gdb_printf ("%*spointer_type %s\n", spaces, "",
5123 host_address_to_string (TYPE_POINTER_TYPE (type)));
5124 gdb_printf ("%*sreference_type %s\n", spaces, "",
5125 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5126 gdb_printf ("%*stype_chain %s\n", spaces, "",
5127 host_address_to_string (TYPE_CHAIN (type)));
5128 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5129 (unsigned) type->instance_flags ());
5130 if (TYPE_CONST (type))
5132 gdb_puts (" TYPE_CONST");
5134 if (TYPE_VOLATILE (type))
5136 gdb_puts (" TYPE_VOLATILE");
5138 if (TYPE_CODE_SPACE (type))
5140 gdb_puts (" TYPE_CODE_SPACE");
5142 if (TYPE_DATA_SPACE (type))
5144 gdb_puts (" TYPE_DATA_SPACE");
5146 if (TYPE_ADDRESS_CLASS_1 (type))
5148 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5150 if (TYPE_ADDRESS_CLASS_2 (type))
5152 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5154 if (TYPE_RESTRICT (type))
5156 gdb_puts (" TYPE_RESTRICT");
5158 if (TYPE_ATOMIC (type))
5160 gdb_puts (" TYPE_ATOMIC");
5162 gdb_puts ("\n");
5164 gdb_printf ("%*sflags", spaces, "");
5165 if (type->is_unsigned ())
5167 gdb_puts (" TYPE_UNSIGNED");
5169 if (type->has_no_signedness ())
5171 gdb_puts (" TYPE_NOSIGN");
5173 if (type->endianity_is_not_default ())
5175 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5177 if (type->is_stub ())
5179 gdb_puts (" TYPE_STUB");
5181 if (type->target_is_stub ())
5183 gdb_puts (" TYPE_TARGET_STUB");
5185 if (type->is_prototyped ())
5187 gdb_puts (" TYPE_PROTOTYPED");
5189 if (type->has_varargs ())
5191 gdb_puts (" TYPE_VARARGS");
5193 /* This is used for things like AltiVec registers on ppc. Gcc emits
5194 an attribute for the array type, which tells whether or not we
5195 have a vector, instead of a regular array. */
5196 if (type->is_vector ())
5198 gdb_puts (" TYPE_VECTOR");
5200 if (type->is_fixed_instance ())
5202 gdb_puts (" TYPE_FIXED_INSTANCE");
5204 if (type->stub_is_supported ())
5206 gdb_puts (" TYPE_STUB_SUPPORTED");
5208 if (TYPE_NOTTEXT (type))
5210 gdb_puts (" TYPE_NOTTEXT");
5212 gdb_puts ("\n");
5213 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5214 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5215 || TYPE_ALLOCATED_PROP (type) != nullptr)
5217 gdb_printf ("%*s", spaces, "");
5218 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5220 gdb_printf ("associated ");
5221 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5223 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5225 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5226 gdb_printf (" ");
5227 gdb_printf ("allocated ");
5228 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5230 gdb_printf ("\n");
5232 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5233 for (idx = 0; idx < type->num_fields (); idx++)
5235 field &fld = type->field (idx);
5236 if (type->code () == TYPE_CODE_ENUM)
5237 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5238 idx, plongest (fld.loc_enumval ()));
5239 else
5240 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5241 idx, plongest (fld.loc_bitpos ()),
5242 fld.bitsize ());
5243 gdb_printf ("%s name '%s' (%s)",
5244 host_address_to_string (fld.type ()),
5245 fld.name () != NULL
5246 ? fld.name ()
5247 : "<NULL>",
5248 host_address_to_string (fld.name ()));
5249 if (fld.is_virtual ())
5250 gdb_printf (" virtual");
5252 if (fld.is_private ())
5253 gdb_printf (" private");
5254 else if (fld.is_protected ())
5255 gdb_printf (" protected");
5256 else if (fld.is_ignored ())
5257 gdb_printf (" ignored");
5259 gdb_printf ("\n");
5260 if (fld.type () != NULL)
5262 recursive_dump_type (fld.type (), spaces + 4);
5265 if (type->code () == TYPE_CODE_RANGE)
5267 gdb_printf ("%*slow ", spaces, "");
5268 dump_dynamic_prop (type->bounds ()->low);
5269 gdb_printf (" high ");
5270 dump_dynamic_prop (type->bounds ()->high);
5271 gdb_printf ("\n");
5274 switch (TYPE_SPECIFIC_FIELD (type))
5276 case TYPE_SPECIFIC_CPLUS_STUFF:
5277 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5278 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5279 print_cplus_stuff (type, spaces);
5280 break;
5282 case TYPE_SPECIFIC_GNAT_STUFF:
5283 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5284 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5285 print_gnat_stuff (type, spaces);
5286 break;
5288 case TYPE_SPECIFIC_FLOATFORMAT:
5289 gdb_printf ("%*sfloatformat ", spaces, "");
5290 if (TYPE_FLOATFORMAT (type) == NULL
5291 || TYPE_FLOATFORMAT (type)->name == NULL)
5292 gdb_puts ("(null)");
5293 else
5294 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5295 gdb_puts ("\n");
5296 break;
5298 case TYPE_SPECIFIC_FUNC:
5299 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5300 TYPE_CALLING_CONVENTION (type));
5301 /* tail_call_list is not printed. */
5302 break;
5304 case TYPE_SPECIFIC_SELF_TYPE:
5305 gdb_printf ("%*sself_type %s\n", spaces, "",
5306 host_address_to_string (TYPE_SELF_TYPE (type)));
5307 break;
5309 case TYPE_SPECIFIC_FIXED_POINT:
5310 gdb_printf ("%*sfixed_point_info ", spaces, "");
5311 print_fixed_point_type_info (type, spaces);
5312 gdb_puts ("\n");
5313 break;
5315 case TYPE_SPECIFIC_INT:
5316 if (type->bit_size_differs_p ())
5318 unsigned bit_size = type->bit_size ();
5319 unsigned bit_off = type->bit_offset ();
5320 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5321 bit_size, bit_off);
5323 break;
5326 if (spaces == 0)
5327 obstack_free (&dont_print_type_obstack, NULL);
5330 /* Trivial helpers for the libiberty hash table, for mapping one
5331 type to another. */
5333 struct type_pair
5335 type_pair (struct type *old_, struct type *newobj_)
5336 : old (old_), newobj (newobj_)
5339 struct type * const old, * const newobj;
5342 static hashval_t
5343 type_pair_hash (const void *item)
5345 const struct type_pair *pair = (const struct type_pair *) item;
5347 return htab_hash_pointer (pair->old);
5350 static int
5351 type_pair_eq (const void *item_lhs, const void *item_rhs)
5353 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5354 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5356 return lhs->old == rhs->old;
5359 /* Allocate the hash table used by copy_type_recursive to walk
5360 types without duplicates. */
5362 htab_up
5363 create_copied_types_hash ()
5365 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5366 htab_delete_entry<type_pair>,
5367 xcalloc, xfree));
5370 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5372 static struct dynamic_prop_list *
5373 copy_dynamic_prop_list (struct obstack *storage,
5374 struct dynamic_prop_list *list)
5376 struct dynamic_prop_list *copy = list;
5377 struct dynamic_prop_list **node_ptr = &copy;
5379 while (*node_ptr != NULL)
5381 struct dynamic_prop_list *node_copy;
5383 node_copy = ((struct dynamic_prop_list *)
5384 obstack_copy (storage, *node_ptr,
5385 sizeof (struct dynamic_prop_list)));
5386 node_copy->prop = (*node_ptr)->prop;
5387 *node_ptr = node_copy;
5389 node_ptr = &node_copy->next;
5392 return copy;
5395 /* Recursively copy (deep copy) TYPE, if it is associated with
5396 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5397 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5398 it is not associated with OBJFILE. */
5400 struct type *
5401 copy_type_recursive (struct type *type, htab_t copied_types)
5403 void **slot;
5404 struct type *new_type;
5406 if (!type->is_objfile_owned ())
5407 return type;
5409 struct type_pair pair (type, nullptr);
5411 slot = htab_find_slot (copied_types, &pair, INSERT);
5412 if (*slot != NULL)
5413 return ((struct type_pair *) *slot)->newobj;
5415 new_type = type_allocator (type->arch ()).new_type ();
5417 /* We must add the new type to the hash table immediately, in case
5418 we encounter this type again during a recursive call below. */
5419 struct type_pair *stored = new type_pair (type, new_type);
5421 *slot = stored;
5423 /* Copy the common fields of types. For the main type, we simply
5424 copy the entire thing and then update specific fields as needed. */
5425 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5427 new_type->set_owner (type->arch ());
5429 if (type->name ())
5430 new_type->set_name (xstrdup (type->name ()));
5432 new_type->set_instance_flags (type->instance_flags ());
5433 new_type->set_length (type->length ());
5435 /* Copy the fields. */
5436 if (type->num_fields ())
5438 int i, nfields;
5440 nfields = type->num_fields ();
5441 new_type->alloc_fields (type->num_fields ());
5443 for (i = 0; i < nfields; i++)
5445 new_type->field (i).set_is_artificial
5446 (type->field (i).is_artificial ());
5447 new_type->field (i).set_bitsize (type->field (i).bitsize ());
5448 if (type->field (i).type ())
5449 new_type->field (i).set_type
5450 (copy_type_recursive (type->field (i).type (), copied_types));
5451 if (type->field (i).name ())
5452 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5454 switch (type->field (i).loc_kind ())
5456 case FIELD_LOC_KIND_BITPOS:
5457 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5458 break;
5459 case FIELD_LOC_KIND_ENUMVAL:
5460 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5461 break;
5462 case FIELD_LOC_KIND_PHYSADDR:
5463 new_type->field (i).set_loc_physaddr
5464 (type->field (i).loc_physaddr ());
5465 break;
5466 case FIELD_LOC_KIND_PHYSNAME:
5467 new_type->field (i).set_loc_physname
5468 (xstrdup (type->field (i).loc_physname ()));
5469 break;
5470 case FIELD_LOC_KIND_DWARF_BLOCK:
5471 new_type->field (i).set_loc_dwarf_block
5472 (type->field (i).loc_dwarf_block ());
5473 break;
5474 default:
5475 internal_error (_("Unexpected type field location kind: %d"),
5476 type->field (i).loc_kind ());
5481 /* For range types, copy the bounds information. */
5482 if (type->code () == TYPE_CODE_RANGE)
5484 range_bounds *bounds
5485 = ((struct range_bounds *) TYPE_ALLOC
5486 (new_type, sizeof (struct range_bounds)));
5488 *bounds = *type->bounds ();
5489 new_type->set_bounds (bounds);
5492 if (type->main_type->dyn_prop_list != NULL)
5493 new_type->main_type->dyn_prop_list
5494 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5495 type->main_type->dyn_prop_list);
5498 /* Copy pointers to other types. */
5499 if (type->target_type ())
5500 new_type->set_target_type
5501 (copy_type_recursive (type->target_type (), copied_types));
5503 /* Maybe copy the type_specific bits.
5505 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5506 base classes and methods. There's no fundamental reason why we
5507 can't, but at the moment it is not needed. */
5509 switch (TYPE_SPECIFIC_FIELD (type))
5511 case TYPE_SPECIFIC_NONE:
5512 break;
5513 case TYPE_SPECIFIC_FUNC:
5514 INIT_FUNC_SPECIFIC (new_type);
5515 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5516 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5517 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5518 break;
5519 case TYPE_SPECIFIC_FLOATFORMAT:
5520 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5521 break;
5522 case TYPE_SPECIFIC_CPLUS_STUFF:
5523 INIT_CPLUS_SPECIFIC (new_type);
5524 break;
5525 case TYPE_SPECIFIC_GNAT_STUFF:
5526 INIT_GNAT_SPECIFIC (new_type);
5527 break;
5528 case TYPE_SPECIFIC_SELF_TYPE:
5529 set_type_self_type (new_type,
5530 copy_type_recursive (TYPE_SELF_TYPE (type),
5531 copied_types));
5532 break;
5533 case TYPE_SPECIFIC_FIXED_POINT:
5534 INIT_FIXED_POINT_SPECIFIC (new_type);
5535 new_type->fixed_point_info ().scaling_factor
5536 = type->fixed_point_info ().scaling_factor;
5537 break;
5538 case TYPE_SPECIFIC_INT:
5539 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5540 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5541 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5542 break;
5544 default:
5545 gdb_assert_not_reached ("bad type_specific_kind");
5548 return new_type;
5551 /* Make a copy of the given TYPE, except that the pointer & reference
5552 types are not preserved. */
5554 struct type *
5555 copy_type (const struct type *type)
5557 struct type *new_type = type_allocator (type).new_type ();
5558 new_type->set_instance_flags (type->instance_flags ());
5559 new_type->set_length (type->length ());
5560 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5561 sizeof (struct main_type));
5562 if (type->main_type->dyn_prop_list != NULL)
5564 struct obstack *storage = (type->is_objfile_owned ()
5565 ? &type->objfile_owner ()->objfile_obstack
5566 : gdbarch_obstack (type->arch_owner ()));
5567 new_type->main_type->dyn_prop_list
5568 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5571 return new_type;
5574 /* Helper functions to initialize architecture-specific types. */
5576 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5577 NAME is the type name. BIT is the size of the flag word in bits. */
5579 struct type *
5580 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5582 struct type *type;
5584 type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
5585 type->set_is_unsigned (true);
5586 /* Pre-allocate enough space assuming every field is one bit. */
5587 type->alloc_fields (bit);
5588 type->set_num_fields (0);
5590 return type;
5593 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5594 position BITPOS is called NAME. Pass NAME as "" for fields that
5595 should not be printed. */
5597 void
5598 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5599 struct type *field_type, const char *name)
5601 int type_bitsize = type->length () * TARGET_CHAR_BIT;
5602 int field_nr = type->num_fields ();
5604 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5605 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5606 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5607 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5608 gdb_assert (name != NULL);
5610 type->set_num_fields (type->num_fields () + 1);
5611 type->field (field_nr).set_name (xstrdup (name));
5612 type->field (field_nr).set_type (field_type);
5613 type->field (field_nr).set_loc_bitpos (start_bitpos);
5614 type->field (field_nr).set_bitsize (nr_bits);
5617 /* Special version of append_flags_type_field to add a flag field.
5618 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5619 position BITPOS is called NAME. */
5621 void
5622 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5624 append_flags_type_field (type, bitpos, 1,
5625 builtin_type (type->arch ())->builtin_bool,
5626 name);
5629 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5630 specified by CODE) associated with GDBARCH. NAME is the type name. */
5632 struct type *
5633 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5634 enum type_code code)
5636 struct type *t;
5638 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5639 t = type_allocator (gdbarch).new_type (code, 0, NULL);
5640 t->set_name (name);
5641 INIT_CPLUS_SPECIFIC (t);
5642 return t;
5645 /* Add new field with name NAME and type FIELD to composite type T.
5646 Do not set the field's position or adjust the type's length;
5647 the caller should do so. Return the new field. */
5649 struct field *
5650 append_composite_type_field_raw (struct type *t, const char *name,
5651 struct type *field)
5653 struct field *f;
5655 t->set_num_fields (t->num_fields () + 1);
5656 t->set_fields (XRESIZEVEC (struct field, t->fields (),
5657 t->num_fields ()));
5658 f = &t->field (t->num_fields () - 1);
5659 memset (f, 0, sizeof f[0]);
5660 f[0].set_type (field);
5661 f[0].set_name (name);
5662 return f;
5665 /* Add new field with name NAME and type FIELD to composite type T.
5666 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5668 void
5669 append_composite_type_field_aligned (struct type *t, const char *name,
5670 struct type *field, int alignment)
5672 struct field *f = append_composite_type_field_raw (t, name, field);
5674 if (t->code () == TYPE_CODE_UNION)
5676 if (t->length () < field->length ())
5677 t->set_length (field->length ());
5679 else if (t->code () == TYPE_CODE_STRUCT)
5681 t->set_length (t->length () + field->length ());
5682 if (t->num_fields () > 1)
5684 f->set_loc_bitpos
5685 (f[-1].loc_bitpos ()
5686 + (f[-1].type ()->length () * TARGET_CHAR_BIT));
5688 if (alignment)
5690 int left;
5692 alignment *= TARGET_CHAR_BIT;
5693 left = f[0].loc_bitpos () % alignment;
5695 if (left)
5697 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
5698 t->set_length
5699 (t->length () + (alignment - left) / TARGET_CHAR_BIT);
5706 /* Add new field with name NAME and type FIELD to composite type T. */
5708 void
5709 append_composite_type_field (struct type *t, const char *name,
5710 struct type *field)
5712 append_composite_type_field_aligned (t, name, field, 0);
5717 /* We manage the lifetimes of fixed_point_type_info objects by
5718 attaching them to the objfile. Currently, these objects are
5719 modified during construction, and GMP does not provide a way to
5720 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5721 them. If we did do this, they could be moved to the per-BFD and
5722 shared across objfiles. */
5723 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
5724 fixed_point_type_storage;
5726 /* Key used for managing the storage of fixed-point type info. */
5727 static const struct registry<objfile>::key<fixed_point_type_storage>
5728 fixed_point_objfile_key;
5730 /* See gdbtypes.h. */
5732 void
5733 allocate_fixed_point_type_info (struct type *type)
5735 auto up = std::make_unique<fixed_point_type_info> ();
5736 fixed_point_type_info *info;
5738 if (type->is_objfile_owned ())
5740 fixed_point_type_storage *storage
5741 = fixed_point_objfile_key.get (type->objfile_owner ());
5742 if (storage == nullptr)
5743 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
5744 info = up.get ();
5745 storage->push_back (std::move (up));
5747 else
5749 /* We just leak the memory, because that's what we do generally
5750 for non-objfile-attached types. */
5751 info = up.release ();
5754 type->set_fixed_point_info (info);
5757 /* See gdbtypes.h. */
5759 bool
5760 is_fixed_point_type (struct type *type)
5762 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5763 type = check_typedef (type)->target_type ();
5764 type = check_typedef (type);
5766 return type->code () == TYPE_CODE_FIXED_POINT;
5769 /* See gdbtypes.h. */
5771 struct type *
5772 type::fixed_point_type_base_type ()
5774 struct type *type = this;
5776 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5777 type = check_typedef (type)->target_type ();
5778 type = check_typedef (type);
5780 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
5781 return type;
5784 /* See gdbtypes.h. */
5786 const gdb_mpq &
5787 type::fixed_point_scaling_factor ()
5789 struct type *type = this->fixed_point_type_base_type ();
5791 return type->fixed_point_info ().scaling_factor;
5794 /* See gdbtypes.h. */
5796 void
5797 type::alloc_fields (unsigned int nfields, bool init)
5799 this->set_num_fields (nfields);
5801 if (nfields == 0)
5803 this->main_type->flds_bnds.fields = nullptr;
5804 return;
5807 size_t size = nfields * sizeof (*this->fields ());
5808 struct field *fields
5809 = (struct field *) (init
5810 ? TYPE_ZALLOC (this, size)
5811 : TYPE_ALLOC (this, size));
5813 this->main_type->flds_bnds.fields = fields;
5816 /* See gdbtypes.h. */
5818 void
5819 type::copy_fields (struct type *src)
5821 unsigned int nfields = src->num_fields ();
5822 alloc_fields (nfields, false);
5823 if (nfields == 0)
5824 return;
5826 size_t size = nfields * sizeof (*this->fields ());
5827 memcpy (this->fields (), src->fields (), size);
5830 /* See gdbtypes.h. */
5832 void
5833 type::copy_fields (std::vector<struct field> &src)
5835 unsigned int nfields = src.size ();
5836 alloc_fields (nfields, false);
5837 if (nfields == 0)
5838 return;
5840 size_t size = nfields * sizeof (*this->fields ());
5841 memcpy (this->fields (), src.data (), size);
5844 /* See gdbtypes.h. */
5846 bool
5847 type::is_string_like ()
5849 const language_defn *defn = language_def (this->language ());
5850 return defn->is_string_type_p (this);
5853 /* See gdbtypes.h. */
5855 bool
5856 type::is_array_like ()
5858 if (code () == TYPE_CODE_ARRAY)
5859 return true;
5860 const language_defn *defn = language_def (this->language ());
5861 return defn->is_array_like (this);
5866 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
5868 static struct builtin_type *
5869 create_gdbtypes_data (struct gdbarch *gdbarch)
5871 struct builtin_type *builtin_type = new struct builtin_type;
5873 type_allocator alloc (gdbarch);
5875 /* Basic types. */
5876 builtin_type->builtin_void
5877 = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
5878 builtin_type->builtin_char
5879 = init_integer_type (alloc, TARGET_CHAR_BIT,
5880 !gdbarch_char_signed (gdbarch), "char");
5881 builtin_type->builtin_char->set_has_no_signedness (true);
5882 builtin_type->builtin_signed_char
5883 = init_integer_type (alloc, TARGET_CHAR_BIT,
5884 0, "signed char");
5885 builtin_type->builtin_unsigned_char
5886 = init_integer_type (alloc, TARGET_CHAR_BIT,
5887 1, "unsigned char");
5888 builtin_type->builtin_short
5889 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5890 0, "short");
5891 builtin_type->builtin_unsigned_short
5892 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5893 1, "unsigned short");
5894 builtin_type->builtin_int
5895 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5896 0, "int");
5897 builtin_type->builtin_unsigned_int
5898 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5899 1, "unsigned int");
5900 builtin_type->builtin_long
5901 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5902 0, "long");
5903 builtin_type->builtin_unsigned_long
5904 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5905 1, "unsigned long");
5906 builtin_type->builtin_long_long
5907 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5908 0, "long long");
5909 builtin_type->builtin_unsigned_long_long
5910 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5911 1, "unsigned long long");
5912 builtin_type->builtin_half
5913 = init_float_type (alloc, gdbarch_half_bit (gdbarch),
5914 "half", gdbarch_half_format (gdbarch));
5915 builtin_type->builtin_float
5916 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
5917 "float", gdbarch_float_format (gdbarch));
5918 builtin_type->builtin_bfloat16
5919 = init_float_type (alloc, gdbarch_bfloat16_bit (gdbarch),
5920 "bfloat16", gdbarch_bfloat16_format (gdbarch));
5921 builtin_type->builtin_double
5922 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
5923 "double", gdbarch_double_format (gdbarch));
5924 builtin_type->builtin_long_double
5925 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
5926 "long double", gdbarch_long_double_format (gdbarch));
5927 builtin_type->builtin_complex
5928 = init_complex_type ("complex", builtin_type->builtin_float);
5929 builtin_type->builtin_double_complex
5930 = init_complex_type ("double complex", builtin_type->builtin_double);
5931 builtin_type->builtin_string
5932 = alloc.new_type (TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
5933 builtin_type->builtin_bool
5934 = init_boolean_type (alloc, TARGET_CHAR_BIT, 1, "bool");
5936 /* The following three are about decimal floating point types, which
5937 are 32-bits, 64-bits and 128-bits respectively. */
5938 builtin_type->builtin_decfloat
5939 = init_decfloat_type (alloc, 32, "_Decimal32");
5940 builtin_type->builtin_decdouble
5941 = init_decfloat_type (alloc, 64, "_Decimal64");
5942 builtin_type->builtin_declong
5943 = init_decfloat_type (alloc, 128, "_Decimal128");
5945 /* "True" character types. */
5946 builtin_type->builtin_true_char
5947 = init_character_type (alloc, TARGET_CHAR_BIT, 0, "true character");
5948 builtin_type->builtin_true_unsigned_char
5949 = init_character_type (alloc, TARGET_CHAR_BIT, 1, "true character");
5951 /* Fixed-size integer types. */
5952 builtin_type->builtin_int0
5953 = init_integer_type (alloc, 0, 0, "int0_t");
5954 builtin_type->builtin_int8
5955 = init_integer_type (alloc, 8, 0, "int8_t");
5956 builtin_type->builtin_uint8
5957 = init_integer_type (alloc, 8, 1, "uint8_t");
5958 builtin_type->builtin_int16
5959 = init_integer_type (alloc, 16, 0, "int16_t");
5960 builtin_type->builtin_uint16
5961 = init_integer_type (alloc, 16, 1, "uint16_t");
5962 builtin_type->builtin_int24
5963 = init_integer_type (alloc, 24, 0, "int24_t");
5964 builtin_type->builtin_uint24
5965 = init_integer_type (alloc, 24, 1, "uint24_t");
5966 builtin_type->builtin_int32
5967 = init_integer_type (alloc, 32, 0, "int32_t");
5968 builtin_type->builtin_uint32
5969 = init_integer_type (alloc, 32, 1, "uint32_t");
5970 builtin_type->builtin_int64
5971 = init_integer_type (alloc, 64, 0, "int64_t");
5972 builtin_type->builtin_uint64
5973 = init_integer_type (alloc, 64, 1, "uint64_t");
5974 builtin_type->builtin_int128
5975 = init_integer_type (alloc, 128, 0, "int128_t");
5976 builtin_type->builtin_uint128
5977 = init_integer_type (alloc, 128, 1, "uint128_t");
5979 builtin_type->builtin_int8->set_instance_flags
5980 (builtin_type->builtin_int8->instance_flags ()
5981 | TYPE_INSTANCE_FLAG_NOTTEXT);
5983 builtin_type->builtin_uint8->set_instance_flags
5984 (builtin_type->builtin_uint8->instance_flags ()
5985 | TYPE_INSTANCE_FLAG_NOTTEXT);
5987 /* Wide character types. */
5988 builtin_type->builtin_char16
5989 = init_integer_type (alloc, 16, 1, "char16_t");
5990 builtin_type->builtin_char32
5991 = init_integer_type (alloc, 32, 1, "char32_t");
5992 builtin_type->builtin_wchar
5993 = init_integer_type (alloc, gdbarch_wchar_bit (gdbarch),
5994 !gdbarch_wchar_signed (gdbarch), "wchar_t");
5996 /* Default data/code pointer types. */
5997 builtin_type->builtin_data_ptr
5998 = lookup_pointer_type (builtin_type->builtin_void);
5999 builtin_type->builtin_func_ptr
6000 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6001 builtin_type->builtin_func_func
6002 = lookup_function_type (builtin_type->builtin_func_ptr);
6004 /* This type represents a GDB internal function. */
6005 builtin_type->internal_fn
6006 = alloc.new_type (TYPE_CODE_INTERNAL_FUNCTION, 0,
6007 "<internal function>");
6009 /* This type represents an xmethod. */
6010 builtin_type->xmethod
6011 = alloc.new_type (TYPE_CODE_XMETHOD, 0, "<xmethod>");
6013 /* This type represents a type that was unrecognized in symbol read-in. */
6014 builtin_type->builtin_error
6015 = alloc.new_type (TYPE_CODE_ERROR, 0, "<unknown type>");
6017 /* The following set of types is used for symbols with no
6018 debug information. */
6019 builtin_type->nodebug_text_symbol
6020 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6021 "<text variable, no debug info>");
6023 builtin_type->nodebug_text_gnu_ifunc_symbol
6024 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6025 "<text gnu-indirect-function variable, no debug info>");
6026 builtin_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6028 builtin_type->nodebug_got_plt_symbol
6029 = init_pointer_type (alloc, gdbarch_addr_bit (gdbarch),
6030 "<text from jump slot in .got.plt, no debug info>",
6031 builtin_type->nodebug_text_symbol);
6032 builtin_type->nodebug_data_symbol
6033 = alloc.new_type (TYPE_CODE_ERROR, 0, "<data variable, no debug info>");
6034 builtin_type->nodebug_unknown_symbol
6035 = alloc.new_type (TYPE_CODE_ERROR, 0,
6036 "<variable (not text or data), no debug info>");
6037 builtin_type->nodebug_tls_symbol
6038 = alloc.new_type (TYPE_CODE_ERROR, 0,
6039 "<thread local variable, no debug info>");
6041 /* NOTE: on some targets, addresses and pointers are not necessarily
6042 the same.
6044 The upshot is:
6045 - gdb's `struct type' always describes the target's
6046 representation.
6047 - gdb's `struct value' objects should always hold values in
6048 target form.
6049 - gdb's CORE_ADDR values are addresses in the unified virtual
6050 address space that the assembler and linker work with. Thus,
6051 since target_read_memory takes a CORE_ADDR as an argument, it
6052 can access any memory on the target, even if the processor has
6053 separate code and data address spaces.
6055 In this context, builtin_type->builtin_core_addr is a bit odd:
6056 it's a target type for a value the target will never see. It's
6057 only used to hold the values of (typeless) linker symbols, which
6058 are indeed in the unified virtual address space. */
6060 builtin_type->builtin_core_addr
6061 = init_integer_type (alloc, gdbarch_addr_bit (gdbarch), 1,
6062 "__CORE_ADDR");
6063 return builtin_type;
6066 const struct builtin_type *
6067 builtin_type (struct gdbarch *gdbarch)
6069 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6070 if (result == nullptr)
6072 result = create_gdbtypes_data (gdbarch);
6073 gdbtypes_data.set (gdbarch, result);
6075 return result;
6078 const struct builtin_type *
6079 builtin_type (struct objfile *objfile)
6081 return builtin_type (objfile->arch ());
6084 /* See gdbtypes.h. */
6086 CORE_ADDR
6087 call_site::pc () const
6089 return per_objfile->relocate (m_unrelocated_pc);
6092 void _initialize_gdbtypes ();
6093 void
6094 _initialize_gdbtypes ()
6096 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6097 _("Set debugging of C++ overloading."),
6098 _("Show debugging of C++ overloading."),
6099 _("When enabled, ranking of the "
6100 "functions is displayed."),
6101 NULL,
6102 show_overload_debug,
6103 &setdebuglist, &showdebuglist);
6105 /* Add user knob for controlling resolution of opaque types. */
6106 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6107 &opaque_type_resolution,
6108 _("Set resolution of opaque struct/class/union"
6109 " types (if set before loading symbols)."),
6110 _("Show resolution of opaque struct/class/union"
6111 " types (if set before loading symbols)."),
6112 NULL, NULL,
6113 show_opaque_type_resolution,
6114 &setlist, &showlist);
6116 /* Add an option to permit non-strict type checking. */
6117 add_setshow_boolean_cmd ("type", class_support,
6118 &strict_type_checking,
6119 _("Set strict type checking."),
6120 _("Show strict type checking."),
6121 NULL, NULL,
6122 show_strict_type_checking,
6123 &setchecklist, &showchecklist);