[gdb/symtab] Workaround PR gas/31115
[binutils-gdb.git] / gdb / gdbtypes.c
blob599a696839ef46520085b109c481c82e40c614d8
1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2024 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, SEARCH_TYPE_DOMAIN,
1659 language->la_language, NULL).symbol;
1660 if (sym != nullptr)
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, SEARCH_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, SEARCH_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, SEARCH_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,
1776 SEARCH_STRUCT_DOMAIN, 0).symbol;
1778 if (sym == NULL)
1780 error (_("No template type named %s."), name);
1782 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1784 error (_("This context has class, union or enum %s, not a struct."),
1785 name);
1787 return (sym->type ());
1790 /* See gdbtypes.h. */
1792 struct_elt
1793 lookup_struct_elt (struct type *type, const char *name, int noerr)
1795 int i;
1797 for (;;)
1799 type = check_typedef (type);
1800 if (type->code () != TYPE_CODE_PTR
1801 && type->code () != TYPE_CODE_REF)
1802 break;
1803 type = type->target_type ();
1806 if (type->code () != TYPE_CODE_STRUCT
1807 && type->code () != TYPE_CODE_UNION)
1809 std::string type_name = type_to_string (type);
1810 error (_("Type %s is not a structure or union type."),
1811 type_name.c_str ());
1814 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
1816 const char *t_field_name = type->field (i).name ();
1818 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1820 return {&type->field (i), type->field (i).loc_bitpos ()};
1822 else if (!t_field_name || *t_field_name == '\0')
1824 struct_elt elt
1825 = lookup_struct_elt (type->field (i).type (), name, 1);
1826 if (elt.field != NULL)
1828 elt.offset += type->field (i).loc_bitpos ();
1829 return elt;
1834 /* OK, it's not in this class. Recursively check the baseclasses. */
1835 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1837 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1838 if (elt.field != NULL)
1839 return elt;
1842 if (noerr)
1843 return {nullptr, 0};
1845 std::string type_name = type_to_string (type);
1846 error (_("Type %s has no component named %s."), type_name.c_str (), name);
1849 /* See gdbtypes.h. */
1851 struct type *
1852 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1854 struct_elt elt = lookup_struct_elt (type, name, noerr);
1855 if (elt.field != NULL)
1856 return elt.field->type ();
1857 else
1858 return NULL;
1861 /* Return the largest number representable by unsigned integer type TYPE. */
1863 ULONGEST
1864 get_unsigned_type_max (struct type *type)
1866 unsigned int n;
1868 type = check_typedef (type);
1869 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
1870 gdb_assert (type->length () <= sizeof (ULONGEST));
1872 /* Written this way to avoid overflow. */
1873 n = type->length () * TARGET_CHAR_BIT;
1874 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1877 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1878 signed integer type TYPE. */
1880 void
1881 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1883 unsigned int n;
1885 type = check_typedef (type);
1886 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
1887 gdb_assert (type->length () <= sizeof (LONGEST));
1889 n = type->length () * TARGET_CHAR_BIT;
1890 *min = -((ULONGEST) 1 << (n - 1));
1891 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1894 /* Return the largest value representable by pointer type TYPE. */
1896 CORE_ADDR
1897 get_pointer_type_max (struct type *type)
1899 unsigned int n;
1901 type = check_typedef (type);
1902 gdb_assert (type->code () == TYPE_CODE_PTR);
1903 gdb_assert (type->length () <= sizeof (CORE_ADDR));
1905 n = type->length () * TARGET_CHAR_BIT;
1906 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1909 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1910 cplus_stuff.vptr_fieldno.
1912 cplus_stuff is initialized to cplus_struct_default which does not
1913 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1914 designated initializers). We cope with that here. */
1917 internal_type_vptr_fieldno (struct type *type)
1919 type = check_typedef (type);
1920 gdb_assert (type->code () == TYPE_CODE_STRUCT
1921 || type->code () == TYPE_CODE_UNION);
1922 if (!HAVE_CPLUS_STRUCT (type))
1923 return -1;
1924 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1927 /* Set the value of cplus_stuff.vptr_fieldno. */
1929 void
1930 set_type_vptr_fieldno (struct type *type, int fieldno)
1932 type = check_typedef (type);
1933 gdb_assert (type->code () == TYPE_CODE_STRUCT
1934 || type->code () == TYPE_CODE_UNION);
1935 if (!HAVE_CPLUS_STRUCT (type))
1936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1937 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1940 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1941 cplus_stuff.vptr_basetype. */
1943 struct type *
1944 internal_type_vptr_basetype (struct type *type)
1946 type = check_typedef (type);
1947 gdb_assert (type->code () == TYPE_CODE_STRUCT
1948 || type->code () == TYPE_CODE_UNION);
1949 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1950 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1953 /* Set the value of cplus_stuff.vptr_basetype. */
1955 void
1956 set_type_vptr_basetype (struct type *type, struct type *basetype)
1958 type = check_typedef (type);
1959 gdb_assert (type->code () == TYPE_CODE_STRUCT
1960 || type->code () == TYPE_CODE_UNION);
1961 if (!HAVE_CPLUS_STRUCT (type))
1962 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1963 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1966 /* Lookup the vptr basetype/fieldno values for TYPE.
1967 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1968 vptr_fieldno. Also, if found and basetype is from the same objfile,
1969 cache the results.
1970 If not found, return -1 and ignore BASETYPEP.
1971 Callers should be aware that in some cases (for example,
1972 the type or one of its baseclasses is a stub type and we are
1973 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1974 this function will not be able to find the
1975 virtual function table pointer, and vptr_fieldno will remain -1 and
1976 vptr_basetype will remain NULL or incomplete. */
1979 get_vptr_fieldno (struct type *type, struct type **basetypep)
1981 type = check_typedef (type);
1983 if (TYPE_VPTR_FIELDNO (type) < 0)
1985 int i;
1987 /* We must start at zero in case the first (and only) baseclass
1988 is virtual (and hence we cannot share the table pointer). */
1989 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1991 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1992 int fieldno;
1993 struct type *basetype;
1995 fieldno = get_vptr_fieldno (baseclass, &basetype);
1996 if (fieldno >= 0)
1998 /* If the type comes from a different objfile we can't cache
1999 it, it may have a different lifetime. PR 2384 */
2000 if (type->objfile_owner () == basetype->objfile_owner ())
2002 set_type_vptr_fieldno (type, fieldno);
2003 set_type_vptr_basetype (type, basetype);
2005 if (basetypep)
2006 *basetypep = basetype;
2007 return fieldno;
2011 /* Not found. */
2012 return -1;
2014 else
2016 if (basetypep)
2017 *basetypep = TYPE_VPTR_BASETYPE (type);
2018 return TYPE_VPTR_FIELDNO (type);
2022 static void
2023 stub_noname_complaint (void)
2025 complaint (_("stub type has NULL name"));
2028 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2029 attached to it, and that property has a non-constant value. */
2031 static int
2032 array_type_has_dynamic_stride (struct type *type)
2034 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2036 return prop != nullptr && prop->is_constant ();
2039 /* Worker for is_dynamic_type. */
2041 static bool
2042 is_dynamic_type_internal (struct type *type, bool top_level)
2044 type = check_typedef (type);
2046 /* We only want to recognize references and pointers at the outermost
2047 level. */
2048 if (top_level && type->is_pointer_or_reference ())
2049 type = check_typedef (type->target_type ());
2051 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2052 dynamic, even if the type itself is statically defined.
2053 From a user's point of view, this may appear counter-intuitive;
2054 but it makes sense in this context, because the point is to determine
2055 whether any part of the type needs to be resolved before it can
2056 be exploited. */
2057 if (TYPE_DATA_LOCATION (type) != NULL
2058 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2059 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
2060 return true;
2062 if (TYPE_ASSOCIATED_PROP (type))
2063 return true;
2065 if (TYPE_ALLOCATED_PROP (type))
2066 return true;
2068 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2069 if (prop != nullptr && prop->kind () != PROP_TYPE)
2070 return true;
2072 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2073 return true;
2075 switch (type->code ())
2077 case TYPE_CODE_RANGE:
2079 /* A range type is obviously dynamic if it has at least one
2080 dynamic bound. But also consider the range type to be
2081 dynamic when its subtype is dynamic, even if the bounds
2082 of the range type are static. It allows us to assume that
2083 the subtype of a static range type is also static. */
2084 return (!has_static_range (type->bounds ())
2085 || is_dynamic_type_internal (type->target_type (), false));
2088 case TYPE_CODE_STRING:
2089 /* Strings are very much like an array of characters, and can be
2090 treated as one here. */
2091 case TYPE_CODE_ARRAY:
2093 gdb_assert (type->num_fields () == 1);
2095 /* The array is dynamic if either the bounds are dynamic... */
2096 if (is_dynamic_type_internal (type->index_type (), false))
2097 return true;
2098 /* ... or the elements it contains have a dynamic contents... */
2099 if (is_dynamic_type_internal (type->target_type (), false))
2100 return true;
2101 /* ... or if it has a dynamic stride... */
2102 if (array_type_has_dynamic_stride (type))
2103 return true;
2104 return false;
2107 case TYPE_CODE_STRUCT:
2108 case TYPE_CODE_UNION:
2110 int i;
2112 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2114 for (i = 0; i < type->num_fields (); ++i)
2116 /* Static fields can be ignored here. */
2117 if (type->field (i).is_static ())
2118 continue;
2119 /* If the field has dynamic type, then so does TYPE. */
2120 if (is_dynamic_type_internal (type->field (i).type (), false))
2121 return true;
2122 /* If the field is at a fixed offset, then it is not
2123 dynamic. */
2124 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
2125 continue;
2126 /* Do not consider C++ virtual base types to be dynamic
2127 due to the field's offset being dynamic; these are
2128 handled via other means. */
2129 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2130 continue;
2131 return true;
2134 break;
2137 return false;
2140 /* See gdbtypes.h. */
2142 bool
2143 is_dynamic_type (struct type *type)
2145 return is_dynamic_type_internal (type, true);
2148 static struct type *resolve_dynamic_type_internal
2149 (struct type *type, struct property_addr_info *addr_stack,
2150 const frame_info_ptr &frame, bool top_level);
2152 /* Given a dynamic range type (dyn_range_type) and a stack of
2153 struct property_addr_info elements, return a static version
2154 of that type.
2156 When RESOLVE_P is true then the returned static range is created by
2157 actually evaluating any dynamic properties within the range type, while
2158 when RESOLVE_P is false the returned static range has all of the bounds
2159 and stride information set to undefined. The RESOLVE_P set to false
2160 case will be used when evaluating a dynamic array that is not
2161 allocated, or not associated, i.e. the bounds information might not be
2162 initialized yet.
2164 RANK is the array rank for which we are resolving this range, and is a
2165 zero based count. The rank should never be negative.
2168 static struct type *
2169 resolve_dynamic_range (struct type *dyn_range_type,
2170 struct property_addr_info *addr_stack,
2171 const frame_info_ptr &frame,
2172 int rank, bool resolve_p = true)
2174 CORE_ADDR value;
2175 struct type *static_range_type, *static_target_type;
2176 struct dynamic_prop low_bound, high_bound, stride;
2178 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
2179 gdb_assert (rank >= 0);
2181 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
2182 if (resolve_p)
2184 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2185 { (CORE_ADDR) rank }))
2186 low_bound.set_const_val (value);
2187 else if (prop->kind () == PROP_UNDEFINED)
2188 low_bound.set_undefined ();
2189 else
2190 low_bound.set_optimized_out ();
2192 else
2193 low_bound.set_undefined ();
2195 prop = &dyn_range_type->bounds ()->high;
2196 if (resolve_p)
2198 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2199 { (CORE_ADDR) rank }))
2201 high_bound.set_const_val (value);
2203 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2204 high_bound.set_const_val
2205 (low_bound.const_val () + high_bound.const_val () - 1);
2207 else if (prop->kind () == PROP_UNDEFINED)
2208 high_bound.set_undefined ();
2209 else
2210 high_bound.set_optimized_out ();
2212 else
2213 high_bound.set_undefined ();
2215 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2216 prop = &dyn_range_type->bounds ()->stride;
2217 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2218 { (CORE_ADDR) rank }))
2220 stride.set_const_val (value);
2222 /* If we have a bit stride that is not an exact number of bytes then
2223 I really don't think this is going to work with current GDB, the
2224 array indexing code in GDB seems to be pretty heavily tied to byte
2225 offsets right now. Assuming 8 bits in a byte. */
2226 struct gdbarch *gdbarch = dyn_range_type->arch ();
2227 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2228 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2229 error (_("bit strides that are not a multiple of the byte size "
2230 "are currently not supported"));
2232 else
2234 stride.set_undefined ();
2235 byte_stride_p = true;
2238 static_target_type
2239 = resolve_dynamic_type_internal (dyn_range_type->target_type (),
2240 addr_stack, frame, false);
2241 LONGEST bias = dyn_range_type->bounds ()->bias;
2242 type_allocator alloc (dyn_range_type);
2243 static_range_type = create_range_type_with_stride
2244 (alloc, static_target_type,
2245 &low_bound, &high_bound, bias, &stride, byte_stride_p);
2246 static_range_type->set_name (dyn_range_type->name ());
2247 static_range_type->bounds ()->flag_bound_evaluated = 1;
2248 return static_range_type;
2251 /* Helper function for resolve_dynamic_array_or_string. This function
2252 resolves the properties for a single array at RANK within a nested array
2253 of arrays structure. The RANK value is greater than or equal to 0, and
2254 starts at it's maximum value and goes down by 1 for each recursive call
2255 to this function. So, for a 3-dimensional array, the first call to this
2256 function has RANK == 2, then we call ourselves recursively with RANK ==
2257 1, than again with RANK == 0, and at that point we should return.
2259 TYPE is updated as the dynamic properties are resolved, and so, should
2260 be a copy of the dynamic type, rather than the original dynamic type
2261 itself.
2263 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2264 during the dynamic resolution.
2266 When RESOLVE_P is true then the dynamic properties of TYPE are
2267 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2268 instead we assume the array is not allocated/associated yet. */
2270 static struct type *
2271 resolve_dynamic_array_or_string_1 (struct type *type,
2272 struct property_addr_info *addr_stack,
2273 const frame_info_ptr &frame,
2274 int rank, bool resolve_p)
2276 CORE_ADDR value;
2277 struct type *elt_type;
2278 struct type *range_type;
2279 struct type *ary_dim;
2280 struct dynamic_prop *prop;
2281 unsigned int bit_stride = 0;
2283 /* For dynamic type resolution strings can be treated like arrays of
2284 characters. */
2285 gdb_assert (type->code () == TYPE_CODE_ARRAY
2286 || type->code () == TYPE_CODE_STRING);
2288 /* As the rank is a zero based count we expect this to never be
2289 negative. */
2290 gdb_assert (rank >= 0);
2292 /* Resolve the allocated and associated properties before doing anything
2293 else. If an array is not allocated or not associated then (at least
2294 for Fortran) there is no guarantee that the data to define the upper
2295 bound, lower bound, or stride will be correct. If RESOLVE_P is
2296 already false at this point then this is not the first dimension of
2297 the array and a more outer dimension has already marked this array as
2298 not allocated/associated, as such we just ignore this property. This
2299 is fine as GDB only checks the allocated/associated on the outer most
2300 dimension of the array. */
2301 prop = TYPE_ALLOCATED_PROP (type);
2302 if (prop != NULL && resolve_p
2303 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2305 prop->set_const_val (value);
2306 if (value == 0)
2307 resolve_p = false;
2310 prop = TYPE_ASSOCIATED_PROP (type);
2311 if (prop != NULL && resolve_p
2312 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2314 prop->set_const_val (value);
2315 if (value == 0)
2316 resolve_p = false;
2319 range_type = check_typedef (type->index_type ());
2320 range_type
2321 = resolve_dynamic_range (range_type, addr_stack, frame, rank, resolve_p);
2323 ary_dim = check_typedef (type->target_type ());
2324 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
2326 ary_dim = copy_type (ary_dim);
2327 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
2328 frame, rank - 1,
2329 resolve_p);
2331 else
2332 elt_type = type->target_type ();
2334 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2335 if (prop != NULL && resolve_p)
2337 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2339 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
2340 bit_stride = (unsigned int) (value * 8);
2342 else
2344 /* Could be a bug in our code, but it could also happen
2345 if the DWARF info is not correct. Issue a warning,
2346 and assume no byte/bit stride (leave bit_stride = 0). */
2347 warning (_("cannot determine array stride for type %s"),
2348 type->name () ? type->name () : "<no name>");
2351 else
2352 bit_stride = type->field (0).bitsize ();
2354 type_allocator alloc (type, type_allocator::SMASH);
2355 return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
2356 bit_stride);
2359 /* Resolve an array or string type with dynamic properties, return a new
2360 type with the dynamic properties resolved to actual values. The
2361 ADDR_STACK represents the location of the object being resolved. */
2363 static struct type *
2364 resolve_dynamic_array_or_string (struct type *type,
2365 struct property_addr_info *addr_stack,
2366 const frame_info_ptr &frame)
2368 CORE_ADDR value;
2369 int rank = 0;
2371 /* For dynamic type resolution strings can be treated like arrays of
2372 characters. */
2373 gdb_assert (type->code () == TYPE_CODE_ARRAY
2374 || type->code () == TYPE_CODE_STRING);
2376 type = copy_type (type);
2378 /* Resolve the rank property to get rank value. */
2379 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
2380 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2382 prop->set_const_val (value);
2383 rank = value;
2385 if (rank == 0)
2387 /* Rank is zero, if a variable is passed as an argument to a
2388 function. In this case the resolved type should not be an
2389 array, but should instead be that of an array element. */
2390 struct type *dynamic_array_type = type;
2391 type = copy_type (dynamic_array_type->target_type ());
2392 struct dynamic_prop_list *prop_list
2393 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2394 if (prop_list != nullptr)
2396 struct obstack *obstack
2397 = &type->objfile_owner ()->objfile_obstack;
2398 TYPE_MAIN_TYPE (type)->dyn_prop_list
2399 = copy_dynamic_prop_list (obstack, prop_list);
2401 return type;
2403 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2405 /* What would this even mean? A string with a dynamic rank
2406 greater than 1. */
2407 error (_("unable to handle string with dynamic rank greater than 1"));
2409 else if (rank > 1)
2411 /* Arrays with dynamic rank are initially just an array type
2412 with a target type that is the array element.
2414 However, now we know the rank of the array we need to build
2415 the array of arrays structure that GDB expects, that is we
2416 need an array type that has a target which is an array type,
2417 and so on, until eventually, we have the element type at the
2418 end of the chain. Create all the additional array types here
2419 by copying the top level array type. */
2420 struct type *element_type = type->target_type ();
2421 struct type *rank_type = type;
2422 for (int i = 1; i < rank; i++)
2424 rank_type->set_target_type (copy_type (rank_type));
2425 rank_type = rank_type->target_type ();
2427 rank_type->set_target_type (element_type);
2430 else
2432 rank = 1;
2434 for (struct type *tmp_type = check_typedef (type->target_type ());
2435 tmp_type->code () == TYPE_CODE_ARRAY;
2436 tmp_type = check_typedef (tmp_type->target_type ()))
2437 ++rank;
2440 /* The rank that we calculated above is actually a count of the number of
2441 ranks. However, when we resolve the type of each individual array
2442 rank we should actually use a rank "offset", e.g. an array with a rank
2443 count of 1 (calculated above) will use the rank offset 0 in order to
2444 resolve the details of the first array dimension. As a result, we
2445 reduce the rank by 1 here. */
2446 --rank;
2448 return resolve_dynamic_array_or_string_1 (type, addr_stack, frame, rank,
2449 true);
2452 /* Resolve dynamic bounds of members of the union TYPE to static
2453 bounds. ADDR_STACK is a stack of struct property_addr_info
2454 to be used if needed during the dynamic resolution. */
2456 static struct type *
2457 resolve_dynamic_union (struct type *type,
2458 struct property_addr_info *addr_stack,
2459 const frame_info_ptr &frame)
2461 struct type *resolved_type;
2462 int i;
2463 unsigned int max_len = 0;
2465 gdb_assert (type->code () == TYPE_CODE_UNION);
2467 resolved_type = copy_type (type);
2468 resolved_type->copy_fields (type);
2469 for (i = 0; i < resolved_type->num_fields (); ++i)
2471 struct type *t;
2473 if (type->field (i).is_static ())
2474 continue;
2476 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
2477 addr_stack, frame, false);
2478 resolved_type->field (i).set_type (t);
2480 struct type *real_type = check_typedef (t);
2481 if (real_type->length () > max_len)
2482 max_len = real_type->length ();
2485 resolved_type->set_length (max_len);
2486 return resolved_type;
2489 /* See gdbtypes.h. */
2491 bool
2492 variant::matches (ULONGEST value, bool is_unsigned) const
2494 for (const discriminant_range &range : discriminants)
2495 if (range.contains (value, is_unsigned))
2496 return true;
2497 return false;
2500 static void
2501 compute_variant_fields_inner (struct type *type,
2502 struct property_addr_info *addr_stack,
2503 const variant_part &part,
2504 std::vector<bool> &flags);
2506 /* A helper function to determine which variant fields will be active.
2507 This handles both the variant's direct fields, and any variant
2508 parts embedded in this variant. TYPE is the type we're examining.
2509 ADDR_STACK holds information about the concrete object. VARIANT is
2510 the current variant to be handled. FLAGS is where the results are
2511 stored -- this function sets the Nth element in FLAGS if the
2512 corresponding field is enabled. ENABLED is whether this variant is
2513 enabled or not. */
2515 static void
2516 compute_variant_fields_recurse (struct type *type,
2517 struct property_addr_info *addr_stack,
2518 const variant &variant,
2519 std::vector<bool> &flags,
2520 bool enabled)
2522 for (int field = variant.first_field; field < variant.last_field; ++field)
2523 flags[field] = enabled;
2525 for (const variant_part &new_part : variant.parts)
2527 if (enabled)
2528 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2529 else
2531 for (const auto &sub_variant : new_part.variants)
2532 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2533 flags, enabled);
2538 /* A helper function to determine which variant fields will be active.
2539 This evaluates the discriminant, decides which variant (if any) is
2540 active, and then updates FLAGS to reflect which fields should be
2541 available. TYPE is the type we're examining. ADDR_STACK holds
2542 information about the concrete object. VARIANT is the current
2543 variant to be handled. FLAGS is where the results are stored --
2544 this function sets the Nth element in FLAGS if the corresponding
2545 field is enabled. */
2547 static void
2548 compute_variant_fields_inner (struct type *type,
2549 struct property_addr_info *addr_stack,
2550 const variant_part &part,
2551 std::vector<bool> &flags)
2553 /* Evaluate the discriminant. */
2554 std::optional<ULONGEST> discr_value;
2555 if (part.discriminant_index != -1)
2557 int idx = part.discriminant_index;
2559 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
2560 error (_("Cannot determine struct field location"
2561 " (invalid location kind)"));
2563 if (addr_stack->valaddr.data () != NULL)
2564 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2565 idx);
2566 else
2568 CORE_ADDR addr = (addr_stack->addr
2569 + (type->field (idx).loc_bitpos ()
2570 / TARGET_CHAR_BIT));
2572 LONGEST bitsize = type->field (idx).bitsize ();
2573 LONGEST size = bitsize / 8;
2574 if (size == 0)
2575 size = type->field (idx).type ()->length ();
2577 gdb_byte bits[sizeof (ULONGEST)];
2578 read_memory (addr, bits, size);
2580 LONGEST bitpos = (type->field (idx).loc_bitpos ()
2581 % TARGET_CHAR_BIT);
2583 discr_value = unpack_bits_as_long (type->field (idx).type (),
2584 bits, bitpos, bitsize);
2588 /* Go through each variant and see which applies. */
2589 const variant *default_variant = nullptr;
2590 const variant *applied_variant = nullptr;
2591 for (const auto &variant : part.variants)
2593 if (variant.is_default ())
2594 default_variant = &variant;
2595 else if (discr_value.has_value ()
2596 && variant.matches (*discr_value, part.is_unsigned))
2598 applied_variant = &variant;
2599 break;
2602 if (applied_variant == nullptr)
2603 applied_variant = default_variant;
2605 for (const auto &variant : part.variants)
2606 compute_variant_fields_recurse (type, addr_stack, variant,
2607 flags, applied_variant == &variant);
2610 /* Determine which variant fields are available in TYPE. The enabled
2611 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2612 about the concrete object. PARTS describes the top-level variant
2613 parts for this type. */
2615 static void
2616 compute_variant_fields (struct type *type,
2617 struct type *resolved_type,
2618 struct property_addr_info *addr_stack,
2619 const gdb::array_view<variant_part> &parts)
2621 /* Assume all fields are included by default. */
2622 std::vector<bool> flags (resolved_type->num_fields (), true);
2624 /* Now disable fields based on the variants that control them. */
2625 for (const auto &part : parts)
2626 compute_variant_fields_inner (type, addr_stack, part, flags);
2628 unsigned int nfields = std::count (flags.begin (), flags.end (), true);
2629 /* No need to zero-initialize the newly allocated fields, they'll be
2630 initialized by the copy in the loop below. */
2631 resolved_type->alloc_fields (nfields, false);
2633 int out = 0;
2634 for (int i = 0; i < type->num_fields (); ++i)
2636 if (!flags[i])
2637 continue;
2639 resolved_type->field (out) = type->field (i);
2640 ++out;
2644 /* Resolve dynamic bounds of members of the struct TYPE to static
2645 bounds. ADDR_STACK is a stack of struct property_addr_info to
2646 be used if needed during the dynamic resolution. */
2648 static struct type *
2649 resolve_dynamic_struct (struct type *type,
2650 struct property_addr_info *addr_stack,
2651 const frame_info_ptr &frame)
2653 struct type *resolved_type;
2654 int i;
2655 unsigned resolved_type_bit_length = 0;
2657 gdb_assert (type->code () == TYPE_CODE_STRUCT);
2659 resolved_type = copy_type (type);
2661 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2662 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
2664 compute_variant_fields (type, resolved_type, addr_stack,
2665 *variant_prop->variant_parts ());
2666 /* We want to leave the property attached, so that the Rust code
2667 can tell whether the type was originally an enum. */
2668 variant_prop->set_original_type (type);
2670 else
2672 resolved_type->copy_fields (type);
2675 for (i = 0; i < resolved_type->num_fields (); ++i)
2677 unsigned new_bit_length;
2678 struct property_addr_info pinfo;
2680 if (resolved_type->field (i).is_static ())
2681 continue;
2683 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
2685 struct dwarf2_property_baton baton;
2686 baton.property_type
2687 = lookup_pointer_type (resolved_type->field (i).type ());
2688 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
2690 struct dynamic_prop prop;
2691 prop.set_locexpr (&baton);
2693 CORE_ADDR addr;
2694 if (dwarf2_evaluate_property (&prop, frame, addr_stack, &addr,
2695 {addr_stack->addr}))
2696 resolved_type->field (i).set_loc_bitpos
2697 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
2700 /* As we know this field is not a static field, the field's
2701 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2702 this is the case, but only trigger a simple error rather
2703 than an internal error if that fails. While failing
2704 that verification indicates a bug in our code, the error
2705 is not severe enough to suggest to the user he stops
2706 his debugging session because of it. */
2707 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
2708 error (_("Cannot determine struct field location"
2709 " (invalid location kind)"));
2711 pinfo.type = check_typedef (resolved_type->field (i).type ());
2712 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
2713 pinfo.valaddr = addr_stack->valaddr;
2714 if (!pinfo.valaddr.empty ())
2715 pinfo.valaddr = pinfo.valaddr.slice (offset);
2716 pinfo.addr = addr_stack->addr + offset;
2717 pinfo.next = addr_stack;
2719 resolved_type->field (i).set_type
2720 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
2721 &pinfo, frame, false));
2722 gdb_assert (resolved_type->field (i).loc_kind ()
2723 == FIELD_LOC_KIND_BITPOS);
2725 new_bit_length = resolved_type->field (i).loc_bitpos ();
2726 if (resolved_type->field (i).bitsize () != 0)
2727 new_bit_length += resolved_type->field (i).bitsize ();
2728 else
2730 struct type *real_type
2731 = check_typedef (resolved_type->field (i).type ());
2733 new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
2736 /* Normally, we would use the position and size of the last field
2737 to determine the size of the enclosing structure. But GCC seems
2738 to be encoding the position of some fields incorrectly when
2739 the struct contains a dynamic field that is not placed last.
2740 So we compute the struct size based on the field that has
2741 the highest position + size - probably the best we can do. */
2742 if (new_bit_length > resolved_type_bit_length)
2743 resolved_type_bit_length = new_bit_length;
2746 /* The length of a type won't change for fortran, but it does for C and Ada.
2747 For fortran the size of dynamic fields might change over time but not the
2748 type length of the structure. If we adapt it, we run into problems
2749 when calculating the element offset for arrays of structs. */
2750 if (current_language->la_language != language_fortran)
2751 resolved_type->set_length ((resolved_type_bit_length + TARGET_CHAR_BIT - 1)
2752 / TARGET_CHAR_BIT);
2754 /* The Ada language uses this field as a cache for static fixed types: reset
2755 it as RESOLVED_TYPE must have its own static fixed type. */
2756 resolved_type->set_target_type (nullptr);
2758 return resolved_type;
2761 /* Worker for resolved_dynamic_type. */
2763 static struct type *
2764 resolve_dynamic_type_internal (struct type *type,
2765 struct property_addr_info *addr_stack,
2766 const frame_info_ptr &frame,
2767 bool top_level)
2769 struct type *real_type = check_typedef (type);
2770 struct type *resolved_type = nullptr;
2771 struct dynamic_prop *prop;
2772 CORE_ADDR value;
2774 if (!is_dynamic_type_internal (real_type, top_level))
2775 return type;
2777 std::optional<CORE_ADDR> type_length;
2778 prop = TYPE_DYNAMIC_LENGTH (type);
2779 if (prop != NULL
2780 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2781 type_length = value;
2783 if (type->code () == TYPE_CODE_TYPEDEF)
2785 resolved_type = copy_type (type);
2786 resolved_type->set_target_type
2787 (resolve_dynamic_type_internal (type->target_type (), addr_stack,
2788 frame, top_level));
2790 else
2792 /* Before trying to resolve TYPE, make sure it is not a stub. */
2793 type = real_type;
2795 switch (type->code ())
2797 case TYPE_CODE_REF:
2798 case TYPE_CODE_PTR:
2799 case TYPE_CODE_RVALUE_REF:
2801 struct property_addr_info pinfo;
2803 pinfo.type = check_typedef (type->target_type ());
2804 pinfo.valaddr = {};
2805 if (addr_stack->valaddr.data () != NULL)
2806 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2807 type);
2808 else
2809 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2810 pinfo.next = addr_stack;
2812 /* Special case a NULL pointer here -- we don't want to
2813 dereference it. */
2814 if (pinfo.addr != 0)
2816 resolved_type = copy_type (type);
2817 resolved_type->set_target_type
2818 (resolve_dynamic_type_internal (type->target_type (),
2819 &pinfo, frame, true));
2821 break;
2824 case TYPE_CODE_STRING:
2825 /* Strings are very much like an array of characters, and can be
2826 treated as one here. */
2827 case TYPE_CODE_ARRAY:
2828 resolved_type = resolve_dynamic_array_or_string (type, addr_stack,
2829 frame);
2830 break;
2832 case TYPE_CODE_RANGE:
2833 /* Pass 0 for the rank value here, which indicates this is a
2834 range for the first rank of an array. The assumption is that
2835 this rank value is not actually required for the resolution of
2836 the dynamic range, otherwise, we'd be resolving this range
2837 within the context of a dynamic array. */
2838 resolved_type = resolve_dynamic_range (type, addr_stack, frame, 0);
2839 break;
2841 case TYPE_CODE_UNION:
2842 resolved_type = resolve_dynamic_union (type, addr_stack, frame);
2843 break;
2845 case TYPE_CODE_STRUCT:
2846 resolved_type = resolve_dynamic_struct (type, addr_stack, frame);
2847 break;
2851 if (resolved_type == nullptr)
2852 return type;
2854 if (type_length.has_value ())
2856 resolved_type->set_length (*type_length);
2857 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
2860 /* Resolve data_location attribute. */
2861 prop = TYPE_DATA_LOCATION (resolved_type);
2862 if (prop != NULL
2863 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2865 /* Start of Fortran hack. See comment in f-lang.h for what is going
2866 on here.*/
2867 if (current_language->la_language == language_fortran
2868 && resolved_type->code () == TYPE_CODE_ARRAY)
2869 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2870 value);
2871 /* End of Fortran hack. */
2872 prop->set_const_val (value);
2875 return resolved_type;
2878 /* See gdbtypes.h */
2880 struct type *
2881 resolve_dynamic_type (struct type *type,
2882 gdb::array_view<const gdb_byte> valaddr,
2883 CORE_ADDR addr,
2884 const frame_info_ptr *in_frame)
2886 struct property_addr_info pinfo
2887 = {check_typedef (type), valaddr, addr, NULL};
2889 frame_info_ptr frame;
2890 if (in_frame != nullptr)
2891 frame = *in_frame;
2893 return resolve_dynamic_type_internal (type, &pinfo, frame, true);
2896 /* See gdbtypes.h */
2898 dynamic_prop *
2899 type::dyn_prop (dynamic_prop_node_kind prop_kind) const
2901 dynamic_prop_list *node = this->main_type->dyn_prop_list;
2903 while (node != NULL)
2905 if (node->prop_kind == prop_kind)
2906 return &node->prop;
2907 node = node->next;
2909 return NULL;
2912 /* See gdbtypes.h */
2914 void
2915 type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
2917 struct dynamic_prop_list *temp;
2919 gdb_assert (this->is_objfile_owned ());
2921 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
2922 struct dynamic_prop_list);
2923 temp->prop_kind = prop_kind;
2924 temp->prop = prop;
2925 temp->next = this->main_type->dyn_prop_list;
2927 this->main_type->dyn_prop_list = temp;
2930 /* See gdbtypes.h. */
2932 void
2933 type::remove_dyn_prop (dynamic_prop_node_kind kind)
2935 struct dynamic_prop_list *prev_node, *curr_node;
2937 curr_node = this->main_type->dyn_prop_list;
2938 prev_node = NULL;
2940 while (NULL != curr_node)
2942 if (curr_node->prop_kind == kind)
2944 /* Update the linked list but don't free anything.
2945 The property was allocated on obstack and it is not known
2946 if we are on top of it. Nevertheless, everything is released
2947 when the complete obstack is freed. */
2948 if (NULL == prev_node)
2949 this->main_type->dyn_prop_list = curr_node->next;
2950 else
2951 prev_node->next = curr_node->next;
2953 return;
2956 prev_node = curr_node;
2957 curr_node = curr_node->next;
2961 /* Find the real type of TYPE. This function returns the real type,
2962 after removing all layers of typedefs, and completing opaque or stub
2963 types. Completion changes the TYPE argument, but stripping of
2964 typedefs does not.
2966 Instance flags (e.g. const/volatile) are preserved as typedefs are
2967 stripped. If necessary a new qualified form of the underlying type
2968 is created.
2970 NOTE: This will return a typedef if type::target_type for the typedef has
2971 not been computed and we're either in the middle of reading symbols, or
2972 there was no name for the typedef in the debug info.
2974 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2975 QUITs in the symbol reading code can also throw.
2976 Thus this function can throw an exception.
2978 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2979 the target type.
2981 If this is a stubbed struct (i.e. declared as struct foo *), see if
2982 we can find a full definition in some other file. If so, copy this
2983 definition, so we can use it in future. There used to be a comment
2984 (but not any code) that if we don't find a full definition, we'd
2985 set a flag so we don't spend time in the future checking the same
2986 type. That would be a mistake, though--we might load in more
2987 symbols which contain a full definition for the type. */
2989 struct type *
2990 check_typedef (struct type *type)
2992 struct type *orig_type = type;
2994 gdb_assert (type);
2996 /* While we're removing typedefs, we don't want to lose qualifiers.
2997 E.g., const/volatile. */
2998 type_instance_flags instance_flags = type->instance_flags ();
3000 while (type->code () == TYPE_CODE_TYPEDEF)
3002 if (!type->target_type ())
3004 const char *name;
3005 struct symbol *sym;
3007 /* It is dangerous to call lookup_symbol if we are currently
3008 reading a symtab. Infinite recursion is one danger. */
3009 if (currently_reading_symtab)
3010 return make_qualified_type (type, instance_flags, NULL);
3012 name = type->name ();
3013 if (name == NULL)
3015 stub_noname_complaint ();
3016 return make_qualified_type (type, instance_flags, NULL);
3018 domain_search_flag flag
3019 = ((type->language () == language_c
3020 || type->language () == language_objc
3021 || type->language () == language_opencl
3022 || type->language () == language_minimal)
3023 ? SEARCH_STRUCT_DOMAIN
3024 : SEARCH_TYPE_DOMAIN);
3025 sym = lookup_symbol (name, nullptr, flag, nullptr).symbol;
3026 if (sym)
3027 type->set_target_type (sym->type ());
3028 else /* TYPE_CODE_UNDEF */
3029 type->set_target_type (type_allocator (type->arch ()).new_type ());
3031 type = type->target_type ();
3033 /* Preserve the instance flags as we traverse down the typedef chain.
3035 Handling address spaces/classes is nasty, what do we do if there's a
3036 conflict?
3037 E.g., what if an outer typedef marks the type as class_1 and an inner
3038 typedef marks the type as class_2?
3039 This is the wrong place to do such error checking. We leave it to
3040 the code that created the typedef in the first place to flag the
3041 error. We just pick the outer address space (akin to letting the
3042 outer cast in a chain of casting win), instead of assuming
3043 "it can't happen". */
3045 const type_instance_flags ALL_SPACES
3046 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3047 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3048 const type_instance_flags ALL_CLASSES
3049 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3051 type_instance_flags new_instance_flags = type->instance_flags ();
3053 /* Treat code vs data spaces and address classes separately. */
3054 if ((instance_flags & ALL_SPACES) != 0)
3055 new_instance_flags &= ~ALL_SPACES;
3056 if ((instance_flags & ALL_CLASSES) != 0)
3057 new_instance_flags &= ~ALL_CLASSES;
3059 instance_flags |= new_instance_flags;
3063 /* If this is a struct/class/union with no fields, then check
3064 whether a full definition exists somewhere else. This is for
3065 systems where a type definition with no fields is issued for such
3066 types, instead of identifying them as stub types in the first
3067 place. */
3069 if (TYPE_IS_OPAQUE (type)
3070 && opaque_type_resolution
3071 && !currently_reading_symtab)
3073 const char *name = type->name ();
3074 struct type *newtype;
3076 if (name == NULL)
3078 stub_noname_complaint ();
3079 return make_qualified_type (type, instance_flags, NULL);
3081 newtype = lookup_transparent_type (name);
3083 if (newtype)
3085 /* If the resolved type and the stub are in the same
3086 objfile, then replace the stub type with the real deal.
3087 But if they're in separate objfiles, leave the stub
3088 alone; we'll just look up the transparent type every time
3089 we call check_typedef. We can't create pointers between
3090 types allocated to different objfiles, since they may
3091 have different lifetimes. Trying to copy NEWTYPE over to
3092 TYPE's objfile is pointless, too, since you'll have to
3093 move over any other types NEWTYPE refers to, which could
3094 be an unbounded amount of stuff. */
3095 if (newtype->objfile_owner () == type->objfile_owner ())
3096 type = make_qualified_type (newtype, type->instance_flags (), type);
3097 else
3098 type = newtype;
3101 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3102 types. */
3103 else if (type->is_stub () && !currently_reading_symtab)
3105 const char *name = type->name ();
3106 struct symbol *sym;
3108 if (name == NULL)
3110 stub_noname_complaint ();
3111 return make_qualified_type (type, instance_flags, NULL);
3113 domain_search_flag flag
3114 = ((type->language () == language_c
3115 || type->language () == language_objc
3116 || type->language () == language_opencl
3117 || type->language () == language_minimal)
3118 ? SEARCH_STRUCT_DOMAIN
3119 : SEARCH_TYPE_DOMAIN);
3120 sym = lookup_symbol (name, nullptr, flag, nullptr).symbol;
3121 if (sym)
3123 /* Same as above for opaque types, we can replace the stub
3124 with the complete type only if they are in the same
3125 objfile. */
3126 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3127 type = make_qualified_type (sym->type (),
3128 type->instance_flags (), type);
3129 else
3130 type = sym->type ();
3134 if (type->target_is_stub ())
3136 struct type *target_type = check_typedef (type->target_type ());
3138 if (target_type->is_stub () || target_type->target_is_stub ())
3140 /* Nothing we can do. */
3142 else if (type->code () == TYPE_CODE_RANGE)
3144 type->set_length (target_type->length ());
3145 type->set_target_is_stub (false);
3147 else if (type->code () == TYPE_CODE_ARRAY
3148 && update_static_array_size (type))
3149 type->set_target_is_stub (false);
3152 type = make_qualified_type (type, instance_flags, NULL);
3154 /* Cache TYPE_LENGTH for future use. */
3155 orig_type->set_length (type->length ());
3157 return type;
3160 /* Parse a type expression in the string [P..P+LENGTH). If an error
3161 occurs, silently return a void type. */
3163 static struct type *
3164 safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
3166 struct type *type = NULL; /* Initialize to keep gcc happy. */
3168 /* Suppress error messages. */
3169 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3170 &null_stream);
3172 /* Call parse_and_eval_type() without fear of longjmp()s. */
3175 type = parse_and_eval_type (p, length);
3177 catch (const gdb_exception_error &except)
3179 type = builtin_type (gdbarch)->builtin_void;
3182 return type;
3185 /* Ugly hack to convert method stubs into method types.
3187 He ain't kiddin'. This demangles the name of the method into a
3188 string including argument types, parses out each argument type,
3189 generates a string casting a zero to that type, evaluates the
3190 string, and stuffs the resulting type into an argtype vector!!!
3191 Then it knows the type of the whole function (including argument
3192 types for overloading), which info used to be in the stab's but was
3193 removed to hack back the space required for them. */
3195 static void
3196 check_stub_method (struct type *type, int method_id, int signature_id)
3198 struct gdbarch *gdbarch = type->arch ();
3199 struct fn_field *f;
3200 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3201 gdb::unique_xmalloc_ptr<char> demangled_name
3202 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3203 char *argtypetext, *p;
3204 int depth = 0, argcount = 1;
3205 struct field *argtypes;
3206 struct type *mtype;
3208 /* Make sure we got back a function string that we can use. */
3209 if (demangled_name)
3210 p = strchr (demangled_name.get (), '(');
3211 else
3212 p = NULL;
3214 if (demangled_name == NULL || p == NULL)
3215 error (_("Internal: Cannot demangle mangled name `%s'."),
3216 mangled_name);
3218 /* Now, read in the parameters that define this type. */
3219 p += 1;
3220 argtypetext = p;
3221 while (*p)
3223 if (*p == '(' || *p == '<')
3225 depth += 1;
3227 else if (*p == ')' || *p == '>')
3229 depth -= 1;
3231 else if (*p == ',' && depth == 0)
3233 argcount += 1;
3236 p += 1;
3239 /* If we read one argument and it was ``void'', don't count it. */
3240 if (startswith (argtypetext, "(void)"))
3241 argcount -= 1;
3243 /* We need one extra slot, for the THIS pointer. */
3245 argtypes = (struct field *)
3246 TYPE_ZALLOC (type, (argcount + 1) * sizeof (struct field));
3247 p = argtypetext;
3249 /* Add THIS pointer for non-static methods. */
3250 f = TYPE_FN_FIELDLIST1 (type, method_id);
3251 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3252 argcount = 0;
3253 else
3255 argtypes[0].set_type (lookup_pointer_type (type));
3256 argcount = 1;
3259 if (*p != ')') /* () means no args, skip while. */
3261 depth = 0;
3262 while (*p)
3264 if (depth <= 0 && (*p == ',' || *p == ')'))
3266 /* Avoid parsing of ellipsis, they will be handled below.
3267 Also avoid ``void'' as above. */
3268 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3269 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3271 argtypes[argcount].set_type
3272 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3273 argcount += 1;
3275 argtypetext = p + 1;
3278 if (*p == '(' || *p == '<')
3280 depth += 1;
3282 else if (*p == ')' || *p == '>')
3284 depth -= 1;
3287 p += 1;
3291 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3293 /* Now update the old "stub" type into a real type. */
3294 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3295 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3296 We want a method (TYPE_CODE_METHOD). */
3297 smash_to_method_type (mtype, type, mtype->target_type (),
3298 argtypes, argcount, p[-2] == '.');
3299 mtype->set_is_stub (false);
3300 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3303 /* This is the external interface to check_stub_method, above. This
3304 function unstubs all of the signatures for TYPE's METHOD_ID method
3305 name. After calling this function TYPE_FN_FIELD_STUB will be
3306 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3307 correct.
3309 This function unfortunately can not die until stabs do. */
3311 void
3312 check_stub_method_group (struct type *type, int method_id)
3314 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3315 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3317 for (int j = 0; j < len; j++)
3319 if (TYPE_FN_FIELD_STUB (f, j))
3320 check_stub_method (type, method_id, j);
3324 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3325 const struct cplus_struct_type cplus_struct_default = { };
3327 void
3328 allocate_cplus_struct_type (struct type *type)
3330 if (HAVE_CPLUS_STRUCT (type))
3331 /* Structure was already allocated. Nothing more to do. */
3332 return;
3334 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3335 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3336 TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
3337 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3338 set_type_vptr_fieldno (type, -1);
3341 const struct gnat_aux_type gnat_aux_default =
3342 { NULL };
3344 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3345 and allocate the associated gnat-specific data. The gnat-specific
3346 data is also initialized to gnat_aux_default. */
3348 void
3349 allocate_gnat_aux_type (struct type *type)
3351 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3352 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3353 TYPE_ZALLOC (type, sizeof (struct gnat_aux_type));
3354 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3357 /* Helper function to verify floating-point format and size.
3358 BIT is the type size in bits; if BIT equals -1, the size is
3359 determined by the floatformat. Returns size to be used. */
3361 static int
3362 verify_floatformat (int bit, const struct floatformat *floatformat)
3364 gdb_assert (floatformat != NULL);
3366 if (bit == -1)
3367 bit = floatformat->totalsize;
3369 gdb_assert (bit >= 0);
3370 gdb_assert (bit >= floatformat->totalsize);
3372 return bit;
3375 /* Return the floating-point format for a floating-point variable of
3376 type TYPE. */
3378 const struct floatformat *
3379 floatformat_from_type (const struct type *type)
3381 gdb_assert (type->code () == TYPE_CODE_FLT);
3382 gdb_assert (TYPE_FLOATFORMAT (type));
3383 return TYPE_FLOATFORMAT (type);
3386 /* See gdbtypes.h. */
3388 struct type *
3389 init_integer_type (type_allocator &alloc,
3390 int bit, int unsigned_p, const char *name)
3392 struct type *t;
3394 t = alloc.new_type (TYPE_CODE_INT, bit, name);
3395 if (unsigned_p)
3396 t->set_is_unsigned (true);
3398 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3399 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3400 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3402 return t;
3405 /* See gdbtypes.h. */
3407 struct type *
3408 init_character_type (type_allocator &alloc,
3409 int bit, int unsigned_p, const char *name)
3411 struct type *t;
3413 t = alloc.new_type (TYPE_CODE_CHAR, bit, name);
3414 if (unsigned_p)
3415 t->set_is_unsigned (true);
3417 return t;
3420 /* See gdbtypes.h. */
3422 struct type *
3423 init_boolean_type (type_allocator &alloc,
3424 int bit, int unsigned_p, const char *name)
3426 struct type *t;
3428 t = alloc.new_type (TYPE_CODE_BOOL, bit, name);
3429 if (unsigned_p)
3430 t->set_is_unsigned (true);
3432 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3433 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3434 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3436 return t;
3439 /* See gdbtypes.h. */
3441 struct type *
3442 init_float_type (type_allocator &alloc,
3443 int bit, const char *name,
3444 const struct floatformat **floatformats,
3445 enum bfd_endian byte_order)
3447 if (byte_order == BFD_ENDIAN_UNKNOWN)
3449 struct gdbarch *gdbarch = alloc.arch ();
3450 byte_order = gdbarch_byte_order (gdbarch);
3452 const struct floatformat *fmt = floatformats[byte_order];
3453 struct type *t;
3455 bit = verify_floatformat (bit, fmt);
3456 t = alloc.new_type (TYPE_CODE_FLT, bit, name);
3457 TYPE_FLOATFORMAT (t) = fmt;
3459 return t;
3462 /* See gdbtypes.h. */
3464 struct type *
3465 init_decfloat_type (type_allocator &alloc, int bit, const char *name)
3467 return alloc.new_type (TYPE_CODE_DECFLOAT, bit, name);
3470 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3472 bool
3473 can_create_complex_type (struct type *target_type)
3475 return (target_type->code () == TYPE_CODE_INT
3476 || target_type->code () == TYPE_CODE_FLT);
3479 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3480 name. TARGET_TYPE is the component type. */
3482 struct type *
3483 init_complex_type (const char *name, struct type *target_type)
3485 struct type *t;
3487 gdb_assert (can_create_complex_type (target_type));
3489 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3491 if (name == nullptr && target_type->name () != nullptr)
3493 /* No zero-initialization required, initialized by strcpy/strcat
3494 below. */
3495 char *new_name
3496 = (char *) TYPE_ALLOC (target_type,
3497 strlen (target_type->name ())
3498 + strlen ("_Complex ") + 1);
3499 strcpy (new_name, "_Complex ");
3500 strcat (new_name, target_type->name ());
3501 name = new_name;
3504 t = type_allocator (target_type).new_type ();
3505 set_type_code (t, TYPE_CODE_COMPLEX);
3506 t->set_length (2 * target_type->length ());
3507 t->set_name (name);
3509 t->set_target_type (target_type);
3510 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3513 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3516 /* See gdbtypes.h. */
3518 struct type *
3519 init_pointer_type (type_allocator &alloc,
3520 int bit, const char *name, struct type *target_type)
3522 struct type *t;
3524 t = alloc.new_type (TYPE_CODE_PTR, bit, name);
3525 t->set_target_type (target_type);
3526 t->set_is_unsigned (true);
3527 return t;
3530 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3531 BIT is the pointer type size in bits.
3532 UNSIGNED_P should be nonzero if the type is unsigned.
3533 NAME is the type name. */
3535 struct type *
3536 init_fixed_point_type (type_allocator &alloc,
3537 int bit, int unsigned_p, const char *name)
3539 struct type *t;
3541 t = alloc.new_type (TYPE_CODE_FIXED_POINT, bit, name);
3542 if (unsigned_p)
3543 t->set_is_unsigned (true);
3545 return t;
3548 /* See gdbtypes.h. */
3550 unsigned
3551 type_raw_align (struct type *type)
3553 if (type->align_log2 != 0)
3554 return 1 << (type->align_log2 - 1);
3555 return 0;
3558 /* See gdbtypes.h. */
3560 unsigned
3561 type_align (struct type *type)
3563 /* Check alignment provided in the debug information. */
3564 unsigned raw_align = type_raw_align (type);
3565 if (raw_align != 0)
3566 return raw_align;
3568 /* Allow the architecture to provide an alignment. */
3569 ULONGEST align = gdbarch_type_align (type->arch (), type);
3570 if (align != 0)
3571 return align;
3573 switch (type->code ())
3575 case TYPE_CODE_PTR:
3576 case TYPE_CODE_FUNC:
3577 case TYPE_CODE_FLAGS:
3578 case TYPE_CODE_INT:
3579 case TYPE_CODE_RANGE:
3580 case TYPE_CODE_FLT:
3581 case TYPE_CODE_ENUM:
3582 case TYPE_CODE_REF:
3583 case TYPE_CODE_RVALUE_REF:
3584 case TYPE_CODE_CHAR:
3585 case TYPE_CODE_BOOL:
3586 case TYPE_CODE_DECFLOAT:
3587 case TYPE_CODE_METHODPTR:
3588 case TYPE_CODE_MEMBERPTR:
3589 align = type_length_units (check_typedef (type));
3590 break;
3592 case TYPE_CODE_ARRAY:
3593 case TYPE_CODE_COMPLEX:
3594 case TYPE_CODE_TYPEDEF:
3595 align = type_align (type->target_type ());
3596 break;
3598 case TYPE_CODE_STRUCT:
3599 case TYPE_CODE_UNION:
3601 int number_of_non_static_fields = 0;
3602 for (unsigned i = 0; i < type->num_fields (); ++i)
3604 if (!type->field (i).is_static ())
3606 number_of_non_static_fields++;
3607 ULONGEST f_align = type_align (type->field (i).type ());
3608 if (f_align == 0)
3610 /* Don't pretend we know something we don't. */
3611 align = 0;
3612 break;
3614 if (f_align > align)
3615 align = f_align;
3618 /* A struct with no fields, or with only static fields has an
3619 alignment of 1. */
3620 if (number_of_non_static_fields == 0)
3621 align = 1;
3623 break;
3625 case TYPE_CODE_SET:
3626 case TYPE_CODE_STRING:
3627 /* Not sure what to do here, and these can't appear in C or C++
3628 anyway. */
3629 break;
3631 case TYPE_CODE_VOID:
3632 align = 1;
3633 break;
3635 case TYPE_CODE_ERROR:
3636 case TYPE_CODE_METHOD:
3637 default:
3638 break;
3641 if ((align & (align - 1)) != 0)
3643 /* Not a power of 2, so pass. */
3644 align = 0;
3647 return align;
3650 /* See gdbtypes.h. */
3652 bool
3653 set_type_align (struct type *type, ULONGEST align)
3655 /* Must be a power of 2. Zero is ok. */
3656 gdb_assert ((align & (align - 1)) == 0);
3658 unsigned result = 0;
3659 while (align != 0)
3661 ++result;
3662 align >>= 1;
3665 if (result >= (1 << TYPE_ALIGN_BITS))
3666 return false;
3668 type->align_log2 = result;
3669 return true;
3673 /* Queries on types. */
3676 can_dereference (struct type *t)
3678 /* FIXME: Should we return true for references as well as
3679 pointers? */
3680 t = check_typedef (t);
3681 return
3682 (t != NULL
3683 && t->code () == TYPE_CODE_PTR
3684 && t->target_type ()->code () != TYPE_CODE_VOID);
3688 is_integral_type (struct type *t)
3690 t = check_typedef (t);
3691 return
3692 ((t != NULL)
3693 && !is_fixed_point_type (t)
3694 && ((t->code () == TYPE_CODE_INT)
3695 || (t->code () == TYPE_CODE_ENUM)
3696 || (t->code () == TYPE_CODE_FLAGS)
3697 || (t->code () == TYPE_CODE_CHAR)
3698 || (t->code () == TYPE_CODE_RANGE)
3699 || (t->code () == TYPE_CODE_BOOL)));
3703 is_floating_type (struct type *t)
3705 t = check_typedef (t);
3706 return
3707 ((t != NULL)
3708 && ((t->code () == TYPE_CODE_FLT)
3709 || (t->code () == TYPE_CODE_DECFLOAT)));
3712 /* Return true if TYPE is scalar. */
3715 is_scalar_type (struct type *type)
3717 type = check_typedef (type);
3719 if (is_fixed_point_type (type))
3720 return 0; /* Implemented as a scalar, but more like a floating point. */
3722 switch (type->code ())
3724 case TYPE_CODE_ARRAY:
3725 case TYPE_CODE_STRUCT:
3726 case TYPE_CODE_UNION:
3727 case TYPE_CODE_SET:
3728 case TYPE_CODE_STRING:
3729 return 0;
3730 default:
3731 return 1;
3735 /* Return true if T is scalar, or a composite type which in practice has
3736 the memory layout of a scalar type. E.g., an array or struct with only
3737 one scalar element inside it, or a union with only scalar elements. */
3740 is_scalar_type_recursive (struct type *t)
3742 t = check_typedef (t);
3744 if (is_scalar_type (t))
3745 return 1;
3746 /* Are we dealing with an array or string of known dimensions? */
3747 else if ((t->code () == TYPE_CODE_ARRAY
3748 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3749 && t->index_type ()->code () == TYPE_CODE_RANGE)
3751 LONGEST low_bound, high_bound;
3752 struct type *elt_type = check_typedef (t->target_type ());
3754 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3755 return (high_bound == low_bound
3756 && is_scalar_type_recursive (elt_type));
3757 else
3758 return 0;
3760 /* Are we dealing with a struct with one element? */
3761 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3762 return is_scalar_type_recursive (t->field (0).type ());
3763 else if (t->code () == TYPE_CODE_UNION)
3765 int i, n = t->num_fields ();
3767 /* If all elements of the union are scalar, then the union is scalar. */
3768 for (i = 0; i < n; i++)
3769 if (!is_scalar_type_recursive (t->field (i).type ()))
3770 return 0;
3772 return 1;
3775 return 0;
3778 /* Return true is T is a class or a union. False otherwise. */
3781 class_or_union_p (const struct type *t)
3783 return (t->code () == TYPE_CODE_STRUCT
3784 || t->code () == TYPE_CODE_UNION);
3787 /* A helper function which returns true if types A and B represent the
3788 "same" class type. This is true if the types have the same main
3789 type, or the same name. */
3792 class_types_same_p (const struct type *a, const struct type *b)
3794 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3795 || (a->name () && b->name ()
3796 && !strcmp (a->name (), b->name ())));
3799 /* If BASE is an ancestor of DCLASS return the distance between them.
3800 otherwise return -1;
3803 class A {};
3804 class B: public A {};
3805 class C: public B {};
3806 class D: C {};
3808 distance_to_ancestor (A, A, 0) = 0
3809 distance_to_ancestor (A, B, 0) = 1
3810 distance_to_ancestor (A, C, 0) = 2
3811 distance_to_ancestor (A, D, 0) = 3
3813 If PUBLIC is 1 then only public ancestors are considered,
3814 and the function returns the distance only if BASE is a public ancestor
3815 of DCLASS.
3818 distance_to_ancestor (A, D, 1) = -1. */
3820 static int
3821 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3823 int i;
3824 int d;
3826 base = check_typedef (base);
3827 dclass = check_typedef (dclass);
3829 if (class_types_same_p (base, dclass))
3830 return 0;
3832 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3834 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3835 continue;
3837 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3838 if (d >= 0)
3839 return 1 + d;
3842 return -1;
3845 /* Check whether BASE is an ancestor or base class or DCLASS
3846 Return 1 if so, and 0 if not.
3847 Note: If BASE and DCLASS are of the same type, this function
3848 will return 1. So for some class A, is_ancestor (A, A) will
3849 return 1. */
3852 is_ancestor (struct type *base, struct type *dclass)
3854 return distance_to_ancestor (base, dclass, 0) >= 0;
3857 /* Like is_ancestor, but only returns true when BASE is a public
3858 ancestor of DCLASS. */
3861 is_public_ancestor (struct type *base, struct type *dclass)
3863 return distance_to_ancestor (base, dclass, 1) >= 0;
3866 /* A helper function for is_unique_ancestor. */
3868 static int
3869 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3870 int *offset,
3871 const gdb_byte *valaddr, int embedded_offset,
3872 CORE_ADDR address, struct value *val)
3874 int i, count = 0;
3876 base = check_typedef (base);
3877 dclass = check_typedef (dclass);
3879 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3881 struct type *iter;
3882 int this_offset;
3884 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3886 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3887 address, val);
3889 if (class_types_same_p (base, iter))
3891 /* If this is the first subclass, set *OFFSET and set count
3892 to 1. Otherwise, if this is at the same offset as
3893 previous instances, do nothing. Otherwise, increment
3894 count. */
3895 if (*offset == -1)
3897 *offset = this_offset;
3898 count = 1;
3900 else if (this_offset == *offset)
3902 /* Nothing. */
3904 else
3905 ++count;
3907 else
3908 count += is_unique_ancestor_worker (base, iter, offset,
3909 valaddr,
3910 embedded_offset + this_offset,
3911 address, val);
3914 return count;
3917 /* Like is_ancestor, but only returns true if BASE is a unique base
3918 class of the type of VAL. */
3921 is_unique_ancestor (struct type *base, struct value *val)
3923 int offset = -1;
3925 return is_unique_ancestor_worker (base, val->type (), &offset,
3926 val->contents_for_printing ().data (),
3927 val->embedded_offset (),
3928 val->address (), val) == 1;
3931 /* See gdbtypes.h. */
3933 enum bfd_endian
3934 type_byte_order (const struct type *type)
3936 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
3937 if (type->endianity_is_not_default ())
3939 if (byteorder == BFD_ENDIAN_BIG)
3940 return BFD_ENDIAN_LITTLE;
3941 else
3943 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
3944 return BFD_ENDIAN_BIG;
3948 return byteorder;
3951 /* See gdbtypes.h. */
3953 bool
3954 is_nocall_function (const struct type *type)
3956 if (type->code () != TYPE_CODE_FUNC && type->code () != TYPE_CODE_METHOD)
3957 return false;
3959 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
3963 /* Overload resolution. */
3965 /* Return the sum of the rank of A with the rank of B. */
3967 struct rank
3968 sum_ranks (struct rank a, struct rank b)
3970 struct rank c;
3971 c.rank = a.rank + b.rank;
3972 c.subrank = a.subrank + b.subrank;
3973 return c;
3976 /* Compare rank A and B and return:
3977 0 if a = b
3978 1 if a is better than b
3979 -1 if b is better than a. */
3982 compare_ranks (struct rank a, struct rank b)
3984 if (a.rank == b.rank)
3986 if (a.subrank == b.subrank)
3987 return 0;
3988 if (a.subrank < b.subrank)
3989 return 1;
3990 if (a.subrank > b.subrank)
3991 return -1;
3994 if (a.rank < b.rank)
3995 return 1;
3997 /* a.rank > b.rank */
3998 return -1;
4001 /* Functions for overload resolution begin here. */
4003 /* Compare two badness vectors A and B and return the result.
4004 0 => A and B are identical
4005 1 => A and B are incomparable
4006 2 => A is better than B
4007 3 => A is worse than B */
4010 compare_badness (const badness_vector &a, const badness_vector &b)
4012 int i;
4013 int tmp;
4014 /* Any positives in comparison? */
4015 bool found_pos = false;
4016 /* Any negatives in comparison? */
4017 bool found_neg = false;
4018 /* Did A have any INVALID_CONVERSION entries. */
4019 bool a_invalid = false;
4020 /* Did B have any INVALID_CONVERSION entries. */
4021 bool b_invalid = false;
4023 /* differing sizes => incomparable */
4024 if (a.size () != b.size ())
4025 return 1;
4027 /* Subtract b from a */
4028 for (i = 0; i < a.size (); i++)
4030 tmp = compare_ranks (b[i], a[i]);
4031 if (tmp > 0)
4032 found_pos = true;
4033 else if (tmp < 0)
4034 found_neg = true;
4035 if (a[i].rank >= INVALID_CONVERSION)
4036 a_invalid = true;
4037 if (b[i].rank >= INVALID_CONVERSION)
4038 b_invalid = true;
4041 /* B will only be considered better than or incomparable to A if
4042 they both have invalid entries, or if neither does. That is, if
4043 A has only valid entries, and B has an invalid entry, then A will
4044 be considered better than B, even if B happens to be better for
4045 some parameter. */
4046 if (a_invalid != b_invalid)
4048 if (a_invalid)
4049 return 3; /* A > B */
4050 return 2; /* A < B */
4052 else if (found_pos)
4054 if (found_neg)
4055 return 1; /* incomparable */
4056 else
4057 return 3; /* A > B */
4059 else
4060 /* no positives */
4062 if (found_neg)
4063 return 2; /* A < B */
4064 else
4065 return 0; /* A == B */
4069 /* Rank a function by comparing its parameter types (PARMS), to the
4070 types of an argument list (ARGS). Return the badness vector. This
4071 has ARGS.size() + 1 entries. */
4073 badness_vector
4074 rank_function (gdb::array_view<type *> parms,
4075 gdb::array_view<value *> args,
4076 bool varargs)
4078 /* add 1 for the length-match rank. */
4079 badness_vector bv;
4080 bv.reserve (1 + args.size ());
4082 /* First compare the lengths of the supplied lists.
4083 If there is a mismatch, set it to a high value. */
4085 /* pai/1997-06-03 FIXME: when we have debug info about default
4086 arguments and ellipsis parameter lists, we should consider those
4087 and rank the length-match more finely. */
4089 bv.push_back ((args.size () != parms.size ()
4090 && (! varargs || args.size () < parms.size ()))
4091 ? LENGTH_MISMATCH_BADNESS
4092 : EXACT_MATCH_BADNESS);
4094 /* Now rank all the parameters of the candidate function. */
4095 size_t min_len = std::min (parms.size (), args.size ());
4097 for (size_t i = 0; i < min_len; i++)
4098 bv.push_back (rank_one_type (parms[i], args[i]->type (),
4099 args[i]));
4101 /* If more arguments than parameters, add dummy entries. */
4102 for (size_t i = min_len; i < args.size (); i++)
4103 bv.push_back (varargs ? VARARG_BADNESS : TOO_FEW_PARAMS_BADNESS);
4105 return bv;
4108 /* Compare the names of two integer types, assuming that any sign
4109 qualifiers have been checked already. We do it this way because
4110 there may be an "int" in the name of one of the types. */
4112 static int
4113 integer_types_same_name_p (const char *first, const char *second)
4115 int first_p, second_p;
4117 /* If both are shorts, return 1; if neither is a short, keep
4118 checking. */
4119 first_p = (strstr (first, "short") != NULL);
4120 second_p = (strstr (second, "short") != NULL);
4121 if (first_p && second_p)
4122 return 1;
4123 if (first_p || second_p)
4124 return 0;
4126 /* Likewise for long. */
4127 first_p = (strstr (first, "long") != NULL);
4128 second_p = (strstr (second, "long") != NULL);
4129 if (first_p && second_p)
4130 return 1;
4131 if (first_p || second_p)
4132 return 0;
4134 /* Likewise for char. */
4135 first_p = (strstr (first, "char") != NULL);
4136 second_p = (strstr (second, "char") != NULL);
4137 if (first_p && second_p)
4138 return 1;
4139 if (first_p || second_p)
4140 return 0;
4142 /* They must both be ints. */
4143 return 1;
4146 /* Compares type A to type B. Returns true if they represent the same
4147 type, false otherwise. */
4149 bool
4150 types_equal (struct type *a, struct type *b)
4152 /* Identical type pointers. */
4153 /* However, this still doesn't catch all cases of same type for b
4154 and a. The reason is that builtin types are different from
4155 the same ones constructed from the object. */
4156 if (a == b)
4157 return true;
4159 /* Resolve typedefs */
4160 if (a->code () == TYPE_CODE_TYPEDEF)
4161 a = check_typedef (a);
4162 if (b->code () == TYPE_CODE_TYPEDEF)
4163 b = check_typedef (b);
4165 /* Check if identical after resolving typedefs. */
4166 if (a == b)
4167 return true;
4169 /* If after resolving typedefs a and b are not of the same type
4170 code then they are not equal. */
4171 if (a->code () != b->code ())
4172 return false;
4174 /* If a and b are both pointers types or both reference types then
4175 they are equal of the same type iff the objects they refer to are
4176 of the same type. */
4177 if (a->code () == TYPE_CODE_PTR
4178 || a->code () == TYPE_CODE_REF)
4179 return types_equal (a->target_type (),
4180 b->target_type ());
4182 /* Well, damnit, if the names are exactly the same, I'll say they
4183 are exactly the same. This happens when we generate method
4184 stubs. The types won't point to the same address, but they
4185 really are the same. */
4187 if (a->name () && b->name ()
4188 && strcmp (a->name (), b->name ()) == 0)
4189 return true;
4191 /* Two function types are equal if their argument and return types
4192 are equal. */
4193 if (a->code () == TYPE_CODE_FUNC)
4195 int i;
4197 if (a->num_fields () != b->num_fields ())
4198 return false;
4200 if (!types_equal (a->target_type (), b->target_type ()))
4201 return false;
4203 for (i = 0; i < a->num_fields (); ++i)
4204 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4205 return false;
4207 return true;
4210 return false;
4213 /* Deep comparison of types. */
4215 /* An entry in the type-equality bcache. */
4217 struct type_equality_entry
4219 type_equality_entry (struct type *t1, struct type *t2)
4220 : type1 (t1),
4221 type2 (t2)
4225 struct type *type1, *type2;
4228 /* A helper function to compare two strings. Returns true if they are
4229 the same, false otherwise. Handles NULLs properly. */
4231 static bool
4232 compare_maybe_null_strings (const char *s, const char *t)
4234 if (s == NULL || t == NULL)
4235 return s == t;
4236 return strcmp (s, t) == 0;
4239 /* A helper function for check_types_worklist that checks two types for
4240 "deep" equality. Returns true if the types are considered the
4241 same, false otherwise. */
4243 static bool
4244 check_types_equal (struct type *type1, struct type *type2,
4245 std::vector<type_equality_entry> *worklist)
4247 type1 = check_typedef (type1);
4248 type2 = check_typedef (type2);
4250 if (type1 == type2)
4251 return true;
4253 if (type1->code () != type2->code ()
4254 || type1->length () != type2->length ()
4255 || type1->is_unsigned () != type2->is_unsigned ()
4256 || type1->has_no_signedness () != type2->has_no_signedness ()
4257 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4258 || type1->has_varargs () != type2->has_varargs ()
4259 || type1->is_vector () != type2->is_vector ()
4260 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4261 || type1->instance_flags () != type2->instance_flags ()
4262 || type1->num_fields () != type2->num_fields ())
4263 return false;
4265 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4266 return false;
4267 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4268 return false;
4270 if (type1->code () == TYPE_CODE_RANGE)
4272 if (*type1->bounds () != *type2->bounds ())
4273 return false;
4275 else
4277 int i;
4279 for (i = 0; i < type1->num_fields (); ++i)
4281 const struct field *field1 = &type1->field (i);
4282 const struct field *field2 = &type2->field (i);
4284 if (field1->is_artificial () != field2->is_artificial ()
4285 || field1->bitsize () != field2->bitsize ()
4286 || field1->loc_kind () != field2->loc_kind ())
4287 return false;
4288 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4289 return false;
4290 switch (field1->loc_kind ())
4292 case FIELD_LOC_KIND_BITPOS:
4293 if (field1->loc_bitpos () != field2->loc_bitpos ())
4294 return false;
4295 break;
4296 case FIELD_LOC_KIND_ENUMVAL:
4297 if (field1->loc_enumval () != field2->loc_enumval ())
4298 return false;
4299 /* Don't compare types of enum fields, because they don't
4300 have a type. */
4301 continue;
4302 case FIELD_LOC_KIND_PHYSADDR:
4303 if (field1->loc_physaddr () != field2->loc_physaddr ())
4304 return false;
4305 break;
4306 case FIELD_LOC_KIND_PHYSNAME:
4307 if (!compare_maybe_null_strings (field1->loc_physname (),
4308 field2->loc_physname ()))
4309 return false;
4310 break;
4311 case FIELD_LOC_KIND_DWARF_BLOCK:
4313 struct dwarf2_locexpr_baton *block1, *block2;
4315 block1 = field1->loc_dwarf_block ();
4316 block2 = field2->loc_dwarf_block ();
4317 if (block1->per_cu != block2->per_cu
4318 || block1->size != block2->size
4319 || memcmp (block1->data, block2->data, block1->size) != 0)
4320 return false;
4322 break;
4323 default:
4324 internal_error (_("Unsupported field kind "
4325 "%d by check_types_equal"),
4326 field1->loc_kind ());
4329 worklist->emplace_back (field1->type (), field2->type ());
4333 if (type1->target_type () != NULL)
4335 if (type2->target_type () == NULL)
4336 return false;
4338 worklist->emplace_back (type1->target_type (),
4339 type2->target_type ());
4341 else if (type2->target_type () != NULL)
4342 return false;
4344 return true;
4347 /* Check types on a worklist for equality. Returns false if any pair
4348 is not equal, true if they are all considered equal. */
4350 static bool
4351 check_types_worklist (std::vector<type_equality_entry> *worklist,
4352 gdb::bcache *cache)
4354 while (!worklist->empty ())
4356 bool added;
4358 struct type_equality_entry entry = std::move (worklist->back ());
4359 worklist->pop_back ();
4361 /* If the type pair has already been visited, we know it is
4362 ok. */
4363 cache->insert (&entry, sizeof (entry), &added);
4364 if (!added)
4365 continue;
4367 if (!check_types_equal (entry.type1, entry.type2, worklist))
4368 return false;
4371 return true;
4374 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4375 "deep comparison". Otherwise return false. */
4377 bool
4378 types_deeply_equal (struct type *type1, struct type *type2)
4380 std::vector<type_equality_entry> worklist;
4382 gdb_assert (type1 != NULL && type2 != NULL);
4384 /* Early exit for the simple case. */
4385 if (type1 == type2)
4386 return true;
4388 gdb::bcache cache;
4389 worklist.emplace_back (type1, type2);
4390 return check_types_worklist (&worklist, &cache);
4393 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4394 Otherwise return one. */
4397 type_not_allocated (const struct type *type)
4399 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4401 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4404 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4405 Otherwise return one. */
4408 type_not_associated (const struct type *type)
4410 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4412 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4415 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4417 static struct rank
4418 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4420 struct rank rank = {0,0};
4422 switch (arg->code ())
4424 case TYPE_CODE_PTR:
4426 /* Allowed pointer conversions are:
4427 (a) pointer to void-pointer conversion. */
4428 if (parm->target_type ()->code () == TYPE_CODE_VOID)
4429 return VOID_PTR_CONVERSION_BADNESS;
4431 /* (b) pointer to ancestor-pointer conversion. */
4432 rank.subrank = distance_to_ancestor (parm->target_type (),
4433 arg->target_type (),
4435 if (rank.subrank >= 0)
4436 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4438 return INCOMPATIBLE_TYPE_BADNESS;
4439 case TYPE_CODE_ARRAY:
4441 struct type *t1 = parm->target_type ();
4442 struct type *t2 = arg->target_type ();
4444 if (types_equal (t1, t2))
4446 /* Make sure they are CV equal. */
4447 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4448 rank.subrank |= CV_CONVERSION_CONST;
4449 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4450 rank.subrank |= CV_CONVERSION_VOLATILE;
4451 if (rank.subrank != 0)
4452 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4453 return EXACT_MATCH_BADNESS;
4455 return INCOMPATIBLE_TYPE_BADNESS;
4457 case TYPE_CODE_FUNC:
4458 return rank_one_type (parm->target_type (), arg, NULL);
4459 case TYPE_CODE_INT:
4460 if (value != NULL && value->type ()->code () == TYPE_CODE_INT)
4462 if (value_as_long (value) == 0)
4464 /* Null pointer conversion: allow it to be cast to a pointer.
4465 [4.10.1 of C++ standard draft n3290] */
4466 return NULL_POINTER_CONVERSION_BADNESS;
4468 else
4470 /* If type checking is disabled, allow the conversion. */
4471 if (!strict_type_checking)
4472 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4475 [[fallthrough]];
4476 case TYPE_CODE_ENUM:
4477 case TYPE_CODE_FLAGS:
4478 case TYPE_CODE_CHAR:
4479 case TYPE_CODE_RANGE:
4480 case TYPE_CODE_BOOL:
4481 default:
4482 return INCOMPATIBLE_TYPE_BADNESS;
4486 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4488 static struct rank
4489 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4491 switch (arg->code ())
4493 case TYPE_CODE_PTR:
4494 case TYPE_CODE_ARRAY:
4495 return rank_one_type (parm->target_type (),
4496 arg->target_type (), NULL);
4497 default:
4498 return INCOMPATIBLE_TYPE_BADNESS;
4502 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4504 static struct rank
4505 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4507 switch (arg->code ())
4509 case TYPE_CODE_PTR: /* funcptr -> func */
4510 return rank_one_type (parm, arg->target_type (), NULL);
4511 default:
4512 return INCOMPATIBLE_TYPE_BADNESS;
4516 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4518 static struct rank
4519 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4521 switch (arg->code ())
4523 case TYPE_CODE_INT:
4524 if (arg->length () == parm->length ())
4526 /* Deal with signed, unsigned, and plain chars and
4527 signed and unsigned ints. */
4528 if (parm->has_no_signedness ())
4530 /* This case only for character types. */
4531 if (arg->has_no_signedness ())
4532 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4533 else /* signed/unsigned char -> plain char */
4534 return INTEGER_CONVERSION_BADNESS;
4536 else if (parm->is_unsigned ())
4538 if (arg->is_unsigned ())
4540 /* unsigned int -> unsigned int, or
4541 unsigned long -> unsigned long */
4542 if (integer_types_same_name_p (parm->name (),
4543 arg->name ()))
4544 return EXACT_MATCH_BADNESS;
4545 else if (integer_types_same_name_p (arg->name (),
4546 "int")
4547 && integer_types_same_name_p (parm->name (),
4548 "long"))
4549 /* unsigned int -> unsigned long */
4550 return INTEGER_PROMOTION_BADNESS;
4551 else
4552 /* unsigned long -> unsigned int */
4553 return INTEGER_CONVERSION_BADNESS;
4555 else
4557 if (integer_types_same_name_p (arg->name (),
4558 "long")
4559 && integer_types_same_name_p (parm->name (),
4560 "int"))
4561 /* signed long -> unsigned int */
4562 return INTEGER_CONVERSION_BADNESS;
4563 else
4564 /* signed int/long -> unsigned int/long */
4565 return INTEGER_CONVERSION_BADNESS;
4568 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4570 if (integer_types_same_name_p (parm->name (),
4571 arg->name ()))
4572 return EXACT_MATCH_BADNESS;
4573 else if (integer_types_same_name_p (arg->name (),
4574 "int")
4575 && integer_types_same_name_p (parm->name (),
4576 "long"))
4577 return INTEGER_PROMOTION_BADNESS;
4578 else
4579 return INTEGER_CONVERSION_BADNESS;
4581 else
4582 return INTEGER_CONVERSION_BADNESS;
4584 else if (arg->length () < parm->length ())
4585 return INTEGER_PROMOTION_BADNESS;
4586 else
4587 return INTEGER_CONVERSION_BADNESS;
4588 case TYPE_CODE_ENUM:
4589 case TYPE_CODE_FLAGS:
4590 case TYPE_CODE_CHAR:
4591 case TYPE_CODE_RANGE:
4592 case TYPE_CODE_BOOL:
4593 if (arg->is_declared_class ())
4594 return INCOMPATIBLE_TYPE_BADNESS;
4595 return INTEGER_PROMOTION_BADNESS;
4596 case TYPE_CODE_FLT:
4597 return INT_FLOAT_CONVERSION_BADNESS;
4598 case TYPE_CODE_PTR:
4599 return NS_POINTER_CONVERSION_BADNESS;
4600 default:
4601 return INCOMPATIBLE_TYPE_BADNESS;
4605 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4607 static struct rank
4608 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4610 switch (arg->code ())
4612 case TYPE_CODE_INT:
4613 case TYPE_CODE_CHAR:
4614 case TYPE_CODE_RANGE:
4615 case TYPE_CODE_BOOL:
4616 case TYPE_CODE_ENUM:
4617 if (parm->is_declared_class () || arg->is_declared_class ())
4618 return INCOMPATIBLE_TYPE_BADNESS;
4619 return INTEGER_CONVERSION_BADNESS;
4620 case TYPE_CODE_FLT:
4621 return INT_FLOAT_CONVERSION_BADNESS;
4622 default:
4623 return INCOMPATIBLE_TYPE_BADNESS;
4627 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4629 static struct rank
4630 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4632 switch (arg->code ())
4634 case TYPE_CODE_RANGE:
4635 case TYPE_CODE_BOOL:
4636 case TYPE_CODE_ENUM:
4637 if (arg->is_declared_class ())
4638 return INCOMPATIBLE_TYPE_BADNESS;
4639 return INTEGER_CONVERSION_BADNESS;
4640 case TYPE_CODE_FLT:
4641 return INT_FLOAT_CONVERSION_BADNESS;
4642 case TYPE_CODE_INT:
4643 if (arg->length () > parm->length ())
4644 return INTEGER_CONVERSION_BADNESS;
4645 else if (arg->length () < parm->length ())
4646 return INTEGER_PROMOTION_BADNESS;
4647 [[fallthrough]];
4648 case TYPE_CODE_CHAR:
4649 /* Deal with signed, unsigned, and plain chars for C++ and
4650 with int cases falling through from previous case. */
4651 if (parm->has_no_signedness ())
4653 if (arg->has_no_signedness ())
4654 return EXACT_MATCH_BADNESS;
4655 else
4656 return INTEGER_CONVERSION_BADNESS;
4658 else if (parm->is_unsigned ())
4660 if (arg->is_unsigned ())
4661 return EXACT_MATCH_BADNESS;
4662 else
4663 return INTEGER_PROMOTION_BADNESS;
4665 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4666 return EXACT_MATCH_BADNESS;
4667 else
4668 return INTEGER_CONVERSION_BADNESS;
4669 default:
4670 return INCOMPATIBLE_TYPE_BADNESS;
4674 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4676 static struct rank
4677 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4679 switch (arg->code ())
4681 case TYPE_CODE_INT:
4682 case TYPE_CODE_CHAR:
4683 case TYPE_CODE_RANGE:
4684 case TYPE_CODE_BOOL:
4685 case TYPE_CODE_ENUM:
4686 return INTEGER_CONVERSION_BADNESS;
4687 case TYPE_CODE_FLT:
4688 return INT_FLOAT_CONVERSION_BADNESS;
4689 default:
4690 return INCOMPATIBLE_TYPE_BADNESS;
4694 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4696 static struct rank
4697 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4699 switch (arg->code ())
4701 /* n3290 draft, section 4.12.1 (conv.bool):
4703 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4704 pointer to member type can be converted to a prvalue of type
4705 bool. A zero value, null pointer value, or null member pointer
4706 value is converted to false; any other value is converted to
4707 true. A prvalue of type std::nullptr_t can be converted to a
4708 prvalue of type bool; the resulting value is false." */
4709 case TYPE_CODE_INT:
4710 case TYPE_CODE_CHAR:
4711 case TYPE_CODE_ENUM:
4712 case TYPE_CODE_FLT:
4713 case TYPE_CODE_MEMBERPTR:
4714 case TYPE_CODE_PTR:
4715 return BOOL_CONVERSION_BADNESS;
4716 case TYPE_CODE_RANGE:
4717 return INCOMPATIBLE_TYPE_BADNESS;
4718 case TYPE_CODE_BOOL:
4719 return EXACT_MATCH_BADNESS;
4720 default:
4721 return INCOMPATIBLE_TYPE_BADNESS;
4725 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4727 static struct rank
4728 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4730 switch (arg->code ())
4732 case TYPE_CODE_FLT:
4733 if (arg->length () < parm->length ())
4734 return FLOAT_PROMOTION_BADNESS;
4735 else if (arg->length () == parm->length ())
4736 return EXACT_MATCH_BADNESS;
4737 else
4738 return FLOAT_CONVERSION_BADNESS;
4739 case TYPE_CODE_INT:
4740 case TYPE_CODE_BOOL:
4741 case TYPE_CODE_ENUM:
4742 case TYPE_CODE_RANGE:
4743 case TYPE_CODE_CHAR:
4744 return INT_FLOAT_CONVERSION_BADNESS;
4745 default:
4746 return INCOMPATIBLE_TYPE_BADNESS;
4750 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4752 static struct rank
4753 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4755 switch (arg->code ())
4756 { /* Strictly not needed for C++, but... */
4757 case TYPE_CODE_FLT:
4758 return FLOAT_PROMOTION_BADNESS;
4759 case TYPE_CODE_COMPLEX:
4760 return EXACT_MATCH_BADNESS;
4761 default:
4762 return INCOMPATIBLE_TYPE_BADNESS;
4766 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4768 static struct rank
4769 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4771 struct rank rank = {0, 0};
4773 switch (arg->code ())
4775 case TYPE_CODE_STRUCT:
4776 /* Check for derivation */
4777 rank.subrank = distance_to_ancestor (parm, arg, 0);
4778 if (rank.subrank >= 0)
4779 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4780 [[fallthrough]];
4781 default:
4782 return INCOMPATIBLE_TYPE_BADNESS;
4786 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4788 static struct rank
4789 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4791 switch (arg->code ())
4793 /* Not in C++ */
4794 case TYPE_CODE_SET:
4795 return rank_one_type (parm->field (0).type (),
4796 arg->field (0).type (), NULL);
4797 default:
4798 return INCOMPATIBLE_TYPE_BADNESS;
4802 /* Compare one type (PARM) for compatibility with another (ARG).
4803 * PARM is intended to be the parameter type of a function; and
4804 * ARG is the supplied argument's type. This function tests if
4805 * the latter can be converted to the former.
4806 * VALUE is the argument's value or NULL if none (or called recursively)
4808 * Return 0 if they are identical types;
4809 * Otherwise, return an integer which corresponds to how compatible
4810 * PARM is to ARG. The higher the return value, the worse the match.
4811 * Generally the "bad" conversions are all uniformly assigned
4812 * INVALID_CONVERSION. */
4814 struct rank
4815 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4817 struct rank rank = {0,0};
4819 /* Resolve typedefs */
4820 if (parm->code () == TYPE_CODE_TYPEDEF)
4821 parm = check_typedef (parm);
4822 if (arg->code () == TYPE_CODE_TYPEDEF)
4823 arg = check_typedef (arg);
4825 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4827 if (value->lval () == not_lval)
4829 /* Rvalues should preferably bind to rvalue references or const
4830 lvalue references. */
4831 if (parm->code () == TYPE_CODE_RVALUE_REF)
4832 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4833 else if (TYPE_CONST (parm->target_type ()))
4834 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4835 else
4836 return INCOMPATIBLE_TYPE_BADNESS;
4837 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4839 else
4841 /* It's illegal to pass an lvalue as an rvalue. */
4842 if (parm->code () == TYPE_CODE_RVALUE_REF)
4843 return INCOMPATIBLE_TYPE_BADNESS;
4847 if (types_equal (parm, arg))
4849 struct type *t1 = parm;
4850 struct type *t2 = arg;
4852 /* For pointers and references, compare target type. */
4853 if (parm->is_pointer_or_reference ())
4855 t1 = parm->target_type ();
4856 t2 = arg->target_type ();
4859 /* Make sure they are CV equal, too. */
4860 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4861 rank.subrank |= CV_CONVERSION_CONST;
4862 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4863 rank.subrank |= CV_CONVERSION_VOLATILE;
4864 if (rank.subrank != 0)
4865 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4866 return EXACT_MATCH_BADNESS;
4869 /* See through references, since we can almost make non-references
4870 references. */
4872 if (TYPE_IS_REFERENCE (arg))
4873 return (sum_ranks (rank_one_type (parm, arg->target_type (), NULL),
4874 REFERENCE_SEE_THROUGH_BADNESS));
4875 if (TYPE_IS_REFERENCE (parm))
4876 return (sum_ranks (rank_one_type (parm->target_type (), arg, NULL),
4877 REFERENCE_SEE_THROUGH_BADNESS));
4878 if (overload_debug)
4880 /* Debugging only. */
4881 gdb_printf (gdb_stderr,
4882 "------ Arg is %s [%d], parm is %s [%d]\n",
4883 arg->name (), arg->code (),
4884 parm->name (), parm->code ());
4887 /* x -> y means arg of type x being supplied for parameter of type y. */
4889 switch (parm->code ())
4891 case TYPE_CODE_PTR:
4892 return rank_one_type_parm_ptr (parm, arg, value);
4893 case TYPE_CODE_ARRAY:
4894 return rank_one_type_parm_array (parm, arg, value);
4895 case TYPE_CODE_FUNC:
4896 return rank_one_type_parm_func (parm, arg, value);
4897 case TYPE_CODE_INT:
4898 return rank_one_type_parm_int (parm, arg, value);
4899 case TYPE_CODE_ENUM:
4900 return rank_one_type_parm_enum (parm, arg, value);
4901 case TYPE_CODE_CHAR:
4902 return rank_one_type_parm_char (parm, arg, value);
4903 case TYPE_CODE_RANGE:
4904 return rank_one_type_parm_range (parm, arg, value);
4905 case TYPE_CODE_BOOL:
4906 return rank_one_type_parm_bool (parm, arg, value);
4907 case TYPE_CODE_FLT:
4908 return rank_one_type_parm_float (parm, arg, value);
4909 case TYPE_CODE_COMPLEX:
4910 return rank_one_type_parm_complex (parm, arg, value);
4911 case TYPE_CODE_STRUCT:
4912 return rank_one_type_parm_struct (parm, arg, value);
4913 case TYPE_CODE_SET:
4914 return rank_one_type_parm_set (parm, arg, value);
4915 default:
4916 return INCOMPATIBLE_TYPE_BADNESS;
4917 } /* switch (arg->code ()) */
4920 /* End of functions for overload resolution. */
4923 /* Note the first arg should be the "this" pointer, we may not want to
4924 include it since we may get into a infinitely recursive
4925 situation. */
4927 static void
4928 print_args (struct field *args, int nargs, int spaces)
4930 if (args != NULL)
4932 int i;
4934 for (i = 0; i < nargs; i++)
4936 gdb_printf
4937 ("%*s[%d] name '%s'\n", spaces, "", i,
4938 args[i].name () != NULL ? args[i].name () : "<NULL>");
4939 recursive_dump_type (args[i].type (), spaces + 2);
4944 static void
4945 dump_fn_fieldlists (struct type *type, int spaces)
4947 int method_idx;
4948 int overload_idx;
4949 struct fn_field *f;
4951 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
4952 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
4953 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
4955 f = TYPE_FN_FIELDLIST1 (type, method_idx);
4956 gdb_printf
4957 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
4958 method_idx,
4959 TYPE_FN_FIELDLIST_NAME (type, method_idx),
4960 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
4961 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
4962 for (overload_idx = 0;
4963 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
4964 overload_idx++)
4966 gdb_printf
4967 ("%*s[%d] physname '%s' (%s)\n",
4968 spaces + 4, "", overload_idx,
4969 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
4970 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
4971 overload_idx)));
4972 gdb_printf
4973 ("%*stype %s\n", spaces + 8, "",
4974 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
4976 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
4977 spaces + 8 + 2);
4979 gdb_printf
4980 ("%*sargs %s\n", spaces + 8, "",
4981 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
4982 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
4983 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
4984 spaces + 8 + 2);
4985 gdb_printf
4986 ("%*sfcontext %s\n", spaces + 8, "",
4987 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
4988 overload_idx)));
4990 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
4991 TYPE_FN_FIELD_CONST (f, overload_idx));
4992 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
4993 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
4994 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
4995 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
4996 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
4997 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
4998 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
4999 TYPE_FN_FIELD_STUB (f, overload_idx));
5000 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
5001 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
5002 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
5003 TYPE_FN_FIELD_DELETED (f, overload_idx));
5004 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
5005 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
5010 static void
5011 print_cplus_stuff (struct type *type, int spaces)
5013 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
5014 TYPE_VPTR_FIELDNO (type));
5015 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
5016 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
5017 if (TYPE_VPTR_BASETYPE (type) != NULL)
5018 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
5020 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
5021 TYPE_N_BASECLASSES (type));
5022 gdb_printf ("%*snfn_fields %d\n", spaces, "",
5023 TYPE_NFN_FIELDS (type));
5024 if (TYPE_NFN_FIELDS (type) > 0)
5026 dump_fn_fieldlists (type, spaces);
5029 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5030 TYPE_CPLUS_CALLING_CONVENTION (type));
5033 /* Print the contents of the TYPE's type_specific union, assuming that
5034 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5036 static void
5037 print_gnat_stuff (struct type *type, int spaces)
5039 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5041 if (descriptive_type == NULL)
5042 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5043 else
5045 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5046 recursive_dump_type (descriptive_type, spaces + 4);
5050 /* Print the contents of the TYPE's type_specific union, assuming that
5051 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5053 static void
5054 print_fixed_point_type_info (struct type *type, int spaces)
5056 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5057 type->fixed_point_scaling_factor ().str ().c_str ());
5060 static struct obstack dont_print_type_obstack;
5062 /* Print the dynamic_prop PROP. */
5064 static void
5065 dump_dynamic_prop (dynamic_prop const& prop)
5067 switch (prop.kind ())
5069 case PROP_CONST:
5070 gdb_printf ("%s", plongest (prop.const_val ()));
5071 break;
5072 case PROP_UNDEFINED:
5073 gdb_printf ("(undefined)");
5074 break;
5075 case PROP_LOCEXPR:
5076 case PROP_LOCLIST:
5077 gdb_printf ("(dynamic)");
5078 break;
5079 default:
5080 gdb_assert_not_reached ("unhandled prop kind");
5081 break;
5085 /* Return a string that represents a type code. */
5086 static const char *
5087 type_code_name (type_code code)
5089 switch (code)
5091 #define OP(X) case X: return # X;
5092 #include "type-codes.def"
5093 #undef OP
5095 case TYPE_CODE_UNDEF:
5096 return "TYPE_CODE_UNDEF";
5099 gdb_assert_not_reached ("unhandled type_code");
5102 void
5103 recursive_dump_type (struct type *type, int spaces)
5105 int idx;
5107 if (spaces == 0)
5108 obstack_begin (&dont_print_type_obstack, 0);
5110 if (type->num_fields () > 0
5111 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5113 struct type **first_dont_print
5114 = (struct type **) obstack_base (&dont_print_type_obstack);
5116 int i = (struct type **)
5117 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5119 while (--i >= 0)
5121 if (type == first_dont_print[i])
5123 gdb_printf ("%*stype node %s", spaces, "",
5124 host_address_to_string (type));
5125 gdb_printf (_(" <same as already seen type>\n"));
5126 return;
5130 obstack_ptr_grow (&dont_print_type_obstack, type);
5133 gdb_printf ("%*stype node %s\n", spaces, "",
5134 host_address_to_string (type));
5135 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5136 type->name () ? type->name () : "<NULL>",
5137 host_address_to_string (type->name ()));
5138 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5139 gdb_printf ("(%s)", type_code_name (type->code ()));
5140 gdb_puts ("\n");
5141 gdb_printf ("%*slength %s\n", spaces, "",
5142 pulongest (type->length ()));
5143 if (type->is_objfile_owned ())
5144 gdb_printf ("%*sobjfile %s\n", spaces, "",
5145 host_address_to_string (type->objfile_owner ()));
5146 else
5147 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5148 host_address_to_string (type->arch_owner ()));
5149 gdb_printf ("%*starget_type %s\n", spaces, "",
5150 host_address_to_string (type->target_type ()));
5151 if (type->target_type () != NULL)
5153 recursive_dump_type (type->target_type (), spaces + 2);
5155 gdb_printf ("%*spointer_type %s\n", spaces, "",
5156 host_address_to_string (TYPE_POINTER_TYPE (type)));
5157 gdb_printf ("%*sreference_type %s\n", spaces, "",
5158 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5159 gdb_printf ("%*stype_chain %s\n", spaces, "",
5160 host_address_to_string (TYPE_CHAIN (type)));
5161 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5162 (unsigned) type->instance_flags ());
5163 if (TYPE_CONST (type))
5165 gdb_puts (" TYPE_CONST");
5167 if (TYPE_VOLATILE (type))
5169 gdb_puts (" TYPE_VOLATILE");
5171 if (TYPE_CODE_SPACE (type))
5173 gdb_puts (" TYPE_CODE_SPACE");
5175 if (TYPE_DATA_SPACE (type))
5177 gdb_puts (" TYPE_DATA_SPACE");
5179 if (TYPE_ADDRESS_CLASS_1 (type))
5181 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5183 if (TYPE_ADDRESS_CLASS_2 (type))
5185 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5187 if (TYPE_RESTRICT (type))
5189 gdb_puts (" TYPE_RESTRICT");
5191 if (TYPE_ATOMIC (type))
5193 gdb_puts (" TYPE_ATOMIC");
5195 gdb_puts ("\n");
5197 gdb_printf ("%*sflags", spaces, "");
5198 if (type->is_unsigned ())
5200 gdb_puts (" TYPE_UNSIGNED");
5202 if (type->has_no_signedness ())
5204 gdb_puts (" TYPE_NOSIGN");
5206 if (type->endianity_is_not_default ())
5208 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5210 if (type->is_stub ())
5212 gdb_puts (" TYPE_STUB");
5214 if (type->target_is_stub ())
5216 gdb_puts (" TYPE_TARGET_STUB");
5218 if (type->is_prototyped ())
5220 gdb_puts (" TYPE_PROTOTYPED");
5222 if (type->has_varargs ())
5224 gdb_puts (" TYPE_VARARGS");
5226 /* This is used for things like AltiVec registers on ppc. Gcc emits
5227 an attribute for the array type, which tells whether or not we
5228 have a vector, instead of a regular array. */
5229 if (type->is_vector ())
5231 gdb_puts (" TYPE_VECTOR");
5233 if (type->is_fixed_instance ())
5235 gdb_puts (" TYPE_FIXED_INSTANCE");
5237 if (type->stub_is_supported ())
5239 gdb_puts (" TYPE_STUB_SUPPORTED");
5241 if (TYPE_NOTTEXT (type))
5243 gdb_puts (" TYPE_NOTTEXT");
5245 gdb_puts ("\n");
5246 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5247 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5248 || TYPE_ALLOCATED_PROP (type) != nullptr)
5250 gdb_printf ("%*s", spaces, "");
5251 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5253 gdb_printf ("associated ");
5254 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5256 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5258 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5259 gdb_printf (" ");
5260 gdb_printf ("allocated ");
5261 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5263 gdb_printf ("\n");
5265 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5266 for (idx = 0; idx < type->num_fields (); idx++)
5268 field &fld = type->field (idx);
5269 if (type->code () == TYPE_CODE_ENUM)
5270 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5271 idx, plongest (fld.loc_enumval ()));
5272 else
5273 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5274 idx, plongest (fld.loc_bitpos ()),
5275 fld.bitsize ());
5276 gdb_printf ("%s name '%s' (%s)",
5277 host_address_to_string (fld.type ()),
5278 fld.name () != NULL
5279 ? fld.name ()
5280 : "<NULL>",
5281 host_address_to_string (fld.name ()));
5282 if (fld.is_virtual ())
5283 gdb_printf (" virtual");
5285 if (fld.is_private ())
5286 gdb_printf (" private");
5287 else if (fld.is_protected ())
5288 gdb_printf (" protected");
5289 else if (fld.is_ignored ())
5290 gdb_printf (" ignored");
5292 gdb_printf ("\n");
5293 if (fld.type () != NULL)
5295 recursive_dump_type (fld.type (), spaces + 4);
5298 if (type->code () == TYPE_CODE_RANGE)
5300 gdb_printf ("%*slow ", spaces, "");
5301 dump_dynamic_prop (type->bounds ()->low);
5302 gdb_printf (" high ");
5303 dump_dynamic_prop (type->bounds ()->high);
5304 gdb_printf ("\n");
5307 switch (TYPE_SPECIFIC_FIELD (type))
5309 case TYPE_SPECIFIC_CPLUS_STUFF:
5310 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5311 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5312 print_cplus_stuff (type, spaces);
5313 break;
5315 case TYPE_SPECIFIC_GNAT_STUFF:
5316 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5317 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5318 print_gnat_stuff (type, spaces);
5319 break;
5321 case TYPE_SPECIFIC_FLOATFORMAT:
5322 gdb_printf ("%*sfloatformat ", spaces, "");
5323 if (TYPE_FLOATFORMAT (type) == NULL
5324 || TYPE_FLOATFORMAT (type)->name == NULL)
5325 gdb_puts ("(null)");
5326 else
5327 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5328 gdb_puts ("\n");
5329 break;
5331 case TYPE_SPECIFIC_FUNC:
5332 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5333 TYPE_CALLING_CONVENTION (type));
5334 /* tail_call_list is not printed. */
5335 break;
5337 case TYPE_SPECIFIC_SELF_TYPE:
5338 gdb_printf ("%*sself_type %s\n", spaces, "",
5339 host_address_to_string (TYPE_SELF_TYPE (type)));
5340 break;
5342 case TYPE_SPECIFIC_FIXED_POINT:
5343 gdb_printf ("%*sfixed_point_info ", spaces, "");
5344 print_fixed_point_type_info (type, spaces);
5345 gdb_puts ("\n");
5346 break;
5348 case TYPE_SPECIFIC_INT:
5349 if (type->bit_size_differs_p ())
5351 unsigned bit_size = type->bit_size ();
5352 unsigned bit_off = type->bit_offset ();
5353 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5354 bit_size, bit_off);
5356 break;
5359 if (spaces == 0)
5360 obstack_free (&dont_print_type_obstack, NULL);
5363 /* Trivial helpers for the libiberty hash table, for mapping one
5364 type to another. */
5366 struct type_pair
5368 type_pair (struct type *old_, struct type *newobj_)
5369 : old (old_), newobj (newobj_)
5372 struct type * const old, * const newobj;
5375 static hashval_t
5376 type_pair_hash (const void *item)
5378 const struct type_pair *pair = (const struct type_pair *) item;
5380 return htab_hash_pointer (pair->old);
5383 static int
5384 type_pair_eq (const void *item_lhs, const void *item_rhs)
5386 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5387 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5389 return lhs->old == rhs->old;
5392 /* Allocate the hash table used by copy_type_recursive to walk
5393 types without duplicates. */
5395 htab_up
5396 create_copied_types_hash ()
5398 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5399 htab_delete_entry<type_pair>,
5400 xcalloc, xfree));
5403 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5405 static struct dynamic_prop_list *
5406 copy_dynamic_prop_list (struct obstack *storage,
5407 struct dynamic_prop_list *list)
5409 struct dynamic_prop_list *copy = list;
5410 struct dynamic_prop_list **node_ptr = &copy;
5412 while (*node_ptr != NULL)
5414 struct dynamic_prop_list *node_copy;
5416 node_copy = ((struct dynamic_prop_list *)
5417 obstack_copy (storage, *node_ptr,
5418 sizeof (struct dynamic_prop_list)));
5419 node_copy->prop = (*node_ptr)->prop;
5420 *node_ptr = node_copy;
5422 node_ptr = &node_copy->next;
5425 return copy;
5428 /* Recursively copy (deep copy) TYPE, if it is associated with
5429 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5430 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5431 it is not associated with OBJFILE. */
5433 struct type *
5434 copy_type_recursive (struct type *type, htab_t copied_types)
5436 void **slot;
5437 struct type *new_type;
5439 if (!type->is_objfile_owned ())
5440 return type;
5442 struct type_pair pair (type, nullptr);
5444 slot = htab_find_slot (copied_types, &pair, INSERT);
5445 if (*slot != NULL)
5446 return ((struct type_pair *) *slot)->newobj;
5448 new_type = type_allocator (type->arch ()).new_type ();
5450 /* We must add the new type to the hash table immediately, in case
5451 we encounter this type again during a recursive call below. */
5452 struct type_pair *stored = new type_pair (type, new_type);
5454 *slot = stored;
5456 /* Copy the common fields of types. For the main type, we simply
5457 copy the entire thing and then update specific fields as needed. */
5458 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5460 new_type->set_owner (type->arch ());
5462 if (type->name ())
5463 new_type->set_name (xstrdup (type->name ()));
5465 new_type->set_instance_flags (type->instance_flags ());
5466 new_type->set_length (type->length ());
5468 /* Copy the fields. */
5469 if (type->num_fields ())
5471 int i, nfields;
5473 nfields = type->num_fields ();
5474 new_type->alloc_fields (type->num_fields ());
5476 for (i = 0; i < nfields; i++)
5478 new_type->field (i).set_is_artificial
5479 (type->field (i).is_artificial ());
5480 new_type->field (i).set_bitsize (type->field (i).bitsize ());
5481 if (type->field (i).type ())
5482 new_type->field (i).set_type
5483 (copy_type_recursive (type->field (i).type (), copied_types));
5484 if (type->field (i).name ())
5485 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5487 switch (type->field (i).loc_kind ())
5489 case FIELD_LOC_KIND_BITPOS:
5490 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5491 break;
5492 case FIELD_LOC_KIND_ENUMVAL:
5493 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5494 break;
5495 case FIELD_LOC_KIND_PHYSADDR:
5496 new_type->field (i).set_loc_physaddr
5497 (type->field (i).loc_physaddr ());
5498 break;
5499 case FIELD_LOC_KIND_PHYSNAME:
5500 new_type->field (i).set_loc_physname
5501 (xstrdup (type->field (i).loc_physname ()));
5502 break;
5503 case FIELD_LOC_KIND_DWARF_BLOCK:
5504 new_type->field (i).set_loc_dwarf_block
5505 (type->field (i).loc_dwarf_block ());
5506 break;
5507 default:
5508 internal_error (_("Unexpected type field location kind: %d"),
5509 type->field (i).loc_kind ());
5514 /* For range types, copy the bounds information. */
5515 if (type->code () == TYPE_CODE_RANGE)
5517 range_bounds *bounds
5518 = ((struct range_bounds *) TYPE_ALLOC
5519 (new_type, sizeof (struct range_bounds)));
5521 *bounds = *type->bounds ();
5522 new_type->set_bounds (bounds);
5525 if (type->main_type->dyn_prop_list != NULL)
5526 new_type->main_type->dyn_prop_list
5527 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5528 type->main_type->dyn_prop_list);
5531 /* Copy pointers to other types. */
5532 if (type->target_type ())
5533 new_type->set_target_type
5534 (copy_type_recursive (type->target_type (), copied_types));
5536 /* Maybe copy the type_specific bits.
5538 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5539 base classes and methods. There's no fundamental reason why we
5540 can't, but at the moment it is not needed. */
5542 switch (TYPE_SPECIFIC_FIELD (type))
5544 case TYPE_SPECIFIC_NONE:
5545 break;
5546 case TYPE_SPECIFIC_FUNC:
5547 INIT_FUNC_SPECIFIC (new_type);
5548 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5549 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5550 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5551 break;
5552 case TYPE_SPECIFIC_FLOATFORMAT:
5553 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5554 break;
5555 case TYPE_SPECIFIC_CPLUS_STUFF:
5556 INIT_CPLUS_SPECIFIC (new_type);
5557 break;
5558 case TYPE_SPECIFIC_GNAT_STUFF:
5559 INIT_GNAT_SPECIFIC (new_type);
5560 break;
5561 case TYPE_SPECIFIC_SELF_TYPE:
5562 set_type_self_type (new_type,
5563 copy_type_recursive (TYPE_SELF_TYPE (type),
5564 copied_types));
5565 break;
5566 case TYPE_SPECIFIC_FIXED_POINT:
5567 INIT_FIXED_POINT_SPECIFIC (new_type);
5568 new_type->fixed_point_info ().scaling_factor
5569 = type->fixed_point_info ().scaling_factor;
5570 break;
5571 case TYPE_SPECIFIC_INT:
5572 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5573 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5574 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5575 break;
5577 default:
5578 gdb_assert_not_reached ("bad type_specific_kind");
5581 return new_type;
5584 /* Make a copy of the given TYPE, except that the pointer & reference
5585 types are not preserved. */
5587 struct type *
5588 copy_type (const struct type *type)
5590 struct type *new_type = type_allocator (type).new_type ();
5591 new_type->set_instance_flags (type->instance_flags ());
5592 new_type->set_length (type->length ());
5593 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5594 sizeof (struct main_type));
5595 if (type->main_type->dyn_prop_list != NULL)
5597 struct obstack *storage = (type->is_objfile_owned ()
5598 ? &type->objfile_owner ()->objfile_obstack
5599 : gdbarch_obstack (type->arch_owner ()));
5600 new_type->main_type->dyn_prop_list
5601 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5604 return new_type;
5607 /* Helper functions to initialize architecture-specific types. */
5609 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5610 NAME is the type name. BIT is the size of the flag word in bits. */
5612 struct type *
5613 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5615 struct type *type;
5617 type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
5618 type->set_is_unsigned (true);
5619 /* Pre-allocate enough space assuming every field is one bit. */
5620 type->alloc_fields (bit);
5621 type->set_num_fields (0);
5623 return type;
5626 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5627 position BITPOS is called NAME. Pass NAME as "" for fields that
5628 should not be printed. */
5630 void
5631 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5632 struct type *field_type, const char *name)
5634 int type_bitsize = type->length () * TARGET_CHAR_BIT;
5635 int field_nr = type->num_fields ();
5637 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5638 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5639 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5640 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5641 gdb_assert (name != NULL);
5643 type->set_num_fields (type->num_fields () + 1);
5644 type->field (field_nr).set_name (xstrdup (name));
5645 type->field (field_nr).set_type (field_type);
5646 type->field (field_nr).set_loc_bitpos (start_bitpos);
5647 type->field (field_nr).set_bitsize (nr_bits);
5650 /* Special version of append_flags_type_field to add a flag field.
5651 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5652 position BITPOS is called NAME. */
5654 void
5655 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5657 append_flags_type_field (type, bitpos, 1,
5658 builtin_type (type->arch ())->builtin_bool,
5659 name);
5662 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5663 specified by CODE) associated with GDBARCH. NAME is the type name. */
5665 struct type *
5666 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5667 enum type_code code)
5669 struct type *t;
5671 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5672 t = type_allocator (gdbarch).new_type (code, 0, NULL);
5673 t->set_name (name);
5674 INIT_CPLUS_SPECIFIC (t);
5675 return t;
5678 /* Add new field with name NAME and type FIELD to composite type T.
5679 Do not set the field's position or adjust the type's length;
5680 the caller should do so. Return the new field. */
5682 struct field *
5683 append_composite_type_field_raw (struct type *t, const char *name,
5684 struct type *field)
5686 struct field *f;
5688 t->set_num_fields (t->num_fields () + 1);
5689 t->set_fields (XRESIZEVEC (struct field, t->fields (),
5690 t->num_fields ()));
5691 f = &t->field (t->num_fields () - 1);
5692 memset (f, 0, sizeof f[0]);
5693 f[0].set_type (field);
5694 f[0].set_name (name);
5695 return f;
5698 /* Add new field with name NAME and type FIELD to composite type T.
5699 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5701 void
5702 append_composite_type_field_aligned (struct type *t, const char *name,
5703 struct type *field, int alignment)
5705 struct field *f = append_composite_type_field_raw (t, name, field);
5707 if (t->code () == TYPE_CODE_UNION)
5709 if (t->length () < field->length ())
5710 t->set_length (field->length ());
5712 else if (t->code () == TYPE_CODE_STRUCT)
5714 t->set_length (t->length () + field->length ());
5715 if (t->num_fields () > 1)
5717 f->set_loc_bitpos
5718 (f[-1].loc_bitpos ()
5719 + (f[-1].type ()->length () * TARGET_CHAR_BIT));
5721 if (alignment)
5723 int left;
5725 alignment *= TARGET_CHAR_BIT;
5726 left = f[0].loc_bitpos () % alignment;
5728 if (left)
5730 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
5731 t->set_length
5732 (t->length () + (alignment - left) / TARGET_CHAR_BIT);
5739 /* Add new field with name NAME and type FIELD to composite type T. */
5741 void
5742 append_composite_type_field (struct type *t, const char *name,
5743 struct type *field)
5745 append_composite_type_field_aligned (t, name, field, 0);
5750 /* We manage the lifetimes of fixed_point_type_info objects by
5751 attaching them to the objfile. Currently, these objects are
5752 modified during construction, and GMP does not provide a way to
5753 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5754 them. If we did do this, they could be moved to the per-BFD and
5755 shared across objfiles. */
5756 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
5757 fixed_point_type_storage;
5759 /* Key used for managing the storage of fixed-point type info. */
5760 static const struct registry<objfile>::key<fixed_point_type_storage>
5761 fixed_point_objfile_key;
5763 /* See gdbtypes.h. */
5765 void
5766 allocate_fixed_point_type_info (struct type *type)
5768 auto up = std::make_unique<fixed_point_type_info> ();
5769 fixed_point_type_info *info;
5771 if (type->is_objfile_owned ())
5773 fixed_point_type_storage *storage
5774 = fixed_point_objfile_key.get (type->objfile_owner ());
5775 if (storage == nullptr)
5776 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
5777 info = up.get ();
5778 storage->push_back (std::move (up));
5780 else
5782 /* We just leak the memory, because that's what we do generally
5783 for non-objfile-attached types. */
5784 info = up.release ();
5787 type->set_fixed_point_info (info);
5790 /* See gdbtypes.h. */
5792 bool
5793 is_fixed_point_type (struct type *type)
5795 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5796 type = check_typedef (type)->target_type ();
5797 type = check_typedef (type);
5799 return type->code () == TYPE_CODE_FIXED_POINT;
5802 /* See gdbtypes.h. */
5804 struct type *
5805 type::fixed_point_type_base_type ()
5807 struct type *type = this;
5809 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5810 type = check_typedef (type)->target_type ();
5811 type = check_typedef (type);
5813 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
5814 return type;
5817 /* See gdbtypes.h. */
5819 const gdb_mpq &
5820 type::fixed_point_scaling_factor ()
5822 struct type *type = this->fixed_point_type_base_type ();
5824 return type->fixed_point_info ().scaling_factor;
5827 /* See gdbtypes.h. */
5829 void
5830 type::alloc_fields (unsigned int nfields, bool init)
5832 this->set_num_fields (nfields);
5834 if (nfields == 0)
5836 this->main_type->flds_bnds.fields = nullptr;
5837 return;
5840 size_t size = nfields * sizeof (*this->fields ());
5841 struct field *fields
5842 = (struct field *) (init
5843 ? TYPE_ZALLOC (this, size)
5844 : TYPE_ALLOC (this, size));
5846 this->main_type->flds_bnds.fields = fields;
5849 /* See gdbtypes.h. */
5851 void
5852 type::copy_fields (struct type *src)
5854 unsigned int nfields = src->num_fields ();
5855 alloc_fields (nfields, false);
5856 if (nfields == 0)
5857 return;
5859 size_t size = nfields * sizeof (*this->fields ());
5860 memcpy (this->fields (), src->fields (), size);
5863 /* See gdbtypes.h. */
5865 void
5866 type::copy_fields (std::vector<struct field> &src)
5868 unsigned int nfields = src.size ();
5869 alloc_fields (nfields, false);
5870 if (nfields == 0)
5871 return;
5873 size_t size = nfields * sizeof (*this->fields ());
5874 memcpy (this->fields (), src.data (), size);
5877 /* See gdbtypes.h. */
5879 bool
5880 type::is_string_like ()
5882 const language_defn *defn = language_def (this->language ());
5883 return defn->is_string_type_p (this);
5886 /* See gdbtypes.h. */
5888 bool
5889 type::is_array_like ()
5891 if (code () == TYPE_CODE_ARRAY)
5892 return true;
5893 const language_defn *defn = language_def (this->language ());
5894 return defn->is_array_like (this);
5899 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
5901 static struct builtin_type *
5902 create_gdbtypes_data (struct gdbarch *gdbarch)
5904 struct builtin_type *builtin_type = new struct builtin_type;
5906 type_allocator alloc (gdbarch);
5908 /* Basic types. */
5909 builtin_type->builtin_void
5910 = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
5911 builtin_type->builtin_char
5912 = init_integer_type (alloc, TARGET_CHAR_BIT,
5913 !gdbarch_char_signed (gdbarch), "char");
5914 builtin_type->builtin_char->set_has_no_signedness (true);
5915 builtin_type->builtin_signed_char
5916 = init_integer_type (alloc, TARGET_CHAR_BIT,
5917 0, "signed char");
5918 builtin_type->builtin_unsigned_char
5919 = init_integer_type (alloc, TARGET_CHAR_BIT,
5920 1, "unsigned char");
5921 builtin_type->builtin_short
5922 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5923 0, "short");
5924 builtin_type->builtin_unsigned_short
5925 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5926 1, "unsigned short");
5927 builtin_type->builtin_int
5928 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5929 0, "int");
5930 builtin_type->builtin_unsigned_int
5931 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5932 1, "unsigned int");
5933 builtin_type->builtin_long
5934 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5935 0, "long");
5936 builtin_type->builtin_unsigned_long
5937 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5938 1, "unsigned long");
5939 builtin_type->builtin_long_long
5940 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5941 0, "long long");
5942 builtin_type->builtin_unsigned_long_long
5943 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5944 1, "unsigned long long");
5945 builtin_type->builtin_half
5946 = init_float_type (alloc, gdbarch_half_bit (gdbarch),
5947 "half", gdbarch_half_format (gdbarch));
5948 builtin_type->builtin_float
5949 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
5950 "float", gdbarch_float_format (gdbarch));
5951 builtin_type->builtin_bfloat16
5952 = init_float_type (alloc, gdbarch_bfloat16_bit (gdbarch),
5953 "bfloat16", gdbarch_bfloat16_format (gdbarch));
5954 builtin_type->builtin_double
5955 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
5956 "double", gdbarch_double_format (gdbarch));
5957 builtin_type->builtin_long_double
5958 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
5959 "long double", gdbarch_long_double_format (gdbarch));
5960 builtin_type->builtin_complex
5961 = init_complex_type ("complex", builtin_type->builtin_float);
5962 builtin_type->builtin_double_complex
5963 = init_complex_type ("double complex", builtin_type->builtin_double);
5964 builtin_type->builtin_string
5965 = alloc.new_type (TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
5966 builtin_type->builtin_bool
5967 = init_boolean_type (alloc, TARGET_CHAR_BIT, 1, "bool");
5969 /* The following three are about decimal floating point types, which
5970 are 32-bits, 64-bits and 128-bits respectively. */
5971 builtin_type->builtin_decfloat
5972 = init_decfloat_type (alloc, 32, "_Decimal32");
5973 builtin_type->builtin_decdouble
5974 = init_decfloat_type (alloc, 64, "_Decimal64");
5975 builtin_type->builtin_declong
5976 = init_decfloat_type (alloc, 128, "_Decimal128");
5978 /* "True" character types. */
5979 builtin_type->builtin_true_char
5980 = init_character_type (alloc, TARGET_CHAR_BIT, 0, "true character");
5981 builtin_type->builtin_true_unsigned_char
5982 = init_character_type (alloc, TARGET_CHAR_BIT, 1, "true character");
5984 /* Fixed-size integer types. */
5985 builtin_type->builtin_int0
5986 = init_integer_type (alloc, 0, 0, "int0_t");
5987 builtin_type->builtin_int8
5988 = init_integer_type (alloc, 8, 0, "int8_t");
5989 builtin_type->builtin_uint8
5990 = init_integer_type (alloc, 8, 1, "uint8_t");
5991 builtin_type->builtin_int16
5992 = init_integer_type (alloc, 16, 0, "int16_t");
5993 builtin_type->builtin_uint16
5994 = init_integer_type (alloc, 16, 1, "uint16_t");
5995 builtin_type->builtin_int24
5996 = init_integer_type (alloc, 24, 0, "int24_t");
5997 builtin_type->builtin_uint24
5998 = init_integer_type (alloc, 24, 1, "uint24_t");
5999 builtin_type->builtin_int32
6000 = init_integer_type (alloc, 32, 0, "int32_t");
6001 builtin_type->builtin_uint32
6002 = init_integer_type (alloc, 32, 1, "uint32_t");
6003 builtin_type->builtin_int64
6004 = init_integer_type (alloc, 64, 0, "int64_t");
6005 builtin_type->builtin_uint64
6006 = init_integer_type (alloc, 64, 1, "uint64_t");
6007 builtin_type->builtin_int128
6008 = init_integer_type (alloc, 128, 0, "int128_t");
6009 builtin_type->builtin_uint128
6010 = init_integer_type (alloc, 128, 1, "uint128_t");
6012 builtin_type->builtin_int8->set_instance_flags
6013 (builtin_type->builtin_int8->instance_flags ()
6014 | TYPE_INSTANCE_FLAG_NOTTEXT);
6016 builtin_type->builtin_uint8->set_instance_flags
6017 (builtin_type->builtin_uint8->instance_flags ()
6018 | TYPE_INSTANCE_FLAG_NOTTEXT);
6020 /* Wide character types. */
6021 builtin_type->builtin_char16
6022 = init_integer_type (alloc, 16, 1, "char16_t");
6023 builtin_type->builtin_char32
6024 = init_integer_type (alloc, 32, 1, "char32_t");
6025 builtin_type->builtin_wchar
6026 = init_integer_type (alloc, gdbarch_wchar_bit (gdbarch),
6027 !gdbarch_wchar_signed (gdbarch), "wchar_t");
6029 /* Default data/code pointer types. */
6030 builtin_type->builtin_data_ptr
6031 = lookup_pointer_type (builtin_type->builtin_void);
6032 builtin_type->builtin_func_ptr
6033 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6034 builtin_type->builtin_func_func
6035 = lookup_function_type (builtin_type->builtin_func_ptr);
6037 /* This type represents a GDB internal function. */
6038 builtin_type->internal_fn
6039 = alloc.new_type (TYPE_CODE_INTERNAL_FUNCTION, 0,
6040 "<internal function>");
6042 /* This type represents an xmethod. */
6043 builtin_type->xmethod
6044 = alloc.new_type (TYPE_CODE_XMETHOD, 0, "<xmethod>");
6046 /* This type represents a type that was unrecognized in symbol read-in. */
6047 builtin_type->builtin_error
6048 = alloc.new_type (TYPE_CODE_ERROR, 0, "<unknown type>");
6050 /* The following set of types is used for symbols with no
6051 debug information. */
6052 builtin_type->nodebug_text_symbol
6053 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6054 "<text variable, no debug info>");
6056 builtin_type->nodebug_text_gnu_ifunc_symbol
6057 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6058 "<text gnu-indirect-function variable, no debug info>");
6059 builtin_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6061 builtin_type->nodebug_got_plt_symbol
6062 = init_pointer_type (alloc, gdbarch_addr_bit (gdbarch),
6063 "<text from jump slot in .got.plt, no debug info>",
6064 builtin_type->nodebug_text_symbol);
6065 builtin_type->nodebug_data_symbol
6066 = alloc.new_type (TYPE_CODE_ERROR, 0, "<data variable, no debug info>");
6067 builtin_type->nodebug_unknown_symbol
6068 = alloc.new_type (TYPE_CODE_ERROR, 0,
6069 "<variable (not text or data), no debug info>");
6070 builtin_type->nodebug_tls_symbol
6071 = alloc.new_type (TYPE_CODE_ERROR, 0,
6072 "<thread local variable, no debug info>");
6074 /* NOTE: on some targets, addresses and pointers are not necessarily
6075 the same.
6077 The upshot is:
6078 - gdb's `struct type' always describes the target's
6079 representation.
6080 - gdb's `struct value' objects should always hold values in
6081 target form.
6082 - gdb's CORE_ADDR values are addresses in the unified virtual
6083 address space that the assembler and linker work with. Thus,
6084 since target_read_memory takes a CORE_ADDR as an argument, it
6085 can access any memory on the target, even if the processor has
6086 separate code and data address spaces.
6088 In this context, builtin_type->builtin_core_addr is a bit odd:
6089 it's a target type for a value the target will never see. It's
6090 only used to hold the values of (typeless) linker symbols, which
6091 are indeed in the unified virtual address space. */
6093 builtin_type->builtin_core_addr
6094 = init_integer_type (alloc, gdbarch_addr_bit (gdbarch), 1,
6095 "__CORE_ADDR");
6096 return builtin_type;
6099 const struct builtin_type *
6100 builtin_type (struct gdbarch *gdbarch)
6102 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6103 if (result == nullptr)
6105 result = create_gdbtypes_data (gdbarch);
6106 gdbtypes_data.set (gdbarch, result);
6108 return result;
6111 const struct builtin_type *
6112 builtin_type (struct objfile *objfile)
6114 return builtin_type (objfile->arch ());
6117 /* See gdbtypes.h. */
6119 CORE_ADDR
6120 call_site::pc () const
6122 return per_objfile->relocate (m_unrelocated_pc);
6125 void _initialize_gdbtypes ();
6126 void
6127 _initialize_gdbtypes ()
6129 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6130 _("Set debugging of C++ overloading."),
6131 _("Show debugging of C++ overloading."),
6132 _("When enabled, ranking of the "
6133 "functions is displayed."),
6134 NULL,
6135 show_overload_debug,
6136 &setdebuglist, &showdebuglist);
6138 /* Add user knob for controlling resolution of opaque types. */
6139 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6140 &opaque_type_resolution,
6141 _("Set resolution of opaque struct/class/union"
6142 " types (if set before loading symbols)."),
6143 _("Show resolution of opaque struct/class/union"
6144 " types (if set before loading symbols)."),
6145 NULL, NULL,
6146 show_opaque_type_resolution,
6147 &setlist, &showlist);
6149 /* Add an option to permit non-strict type checking. */
6150 add_setshow_boolean_cmd ("type", class_support,
6151 &strict_type_checking,
6152 _("Set strict type checking."),
6153 _("Show strict type checking."),
6154 NULL, NULL,
6155 show_strict_type_checking,
6156 &setchecklist, &showchecklist);