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/>. */
27 #include "expression.h"
32 #include "complaints.h"
33 #include "cli/cli-cmds.h"
36 #include "cp-support.h"
38 #include "dwarf2/loc.h"
39 #include "dwarf2/read.h"
41 #include "floatformat.h"
44 #include "gmp-utils.h"
45 #include "rust-lang.h"
47 #include "extract-store-integer.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
] = {
122 const struct floatformat
*floatformats_vax_d
[BFD_ENDIAN_UNKNOWN
] = {
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. */
150 show_opaque_type_resolution (struct ui_file
*file
, int from_tty
,
151 struct cmd_list_element
*c
,
154 gdb_printf (file
, _("Resolution of opaque struct/class/union types "
155 "(if set before loading symbols) is %s.\n"),
159 /* A function to show whether C++ overload debugging is enabled. */
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"),
169 /* A function to show the status of strict type checking. */
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. */
183 set_type_code (struct type
*type
, enum type_code code
)
185 type
->set_code (code
);
189 case TYPE_CODE_STRUCT
:
190 case TYPE_CODE_UNION
:
191 case TYPE_CODE_NAMESPACE
:
192 INIT_CPLUS_SPECIFIC (type
);
195 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_FLOATFORMAT
;
198 INIT_FUNC_SPECIFIC (type
);
200 case TYPE_CODE_FIXED_POINT
:
201 INIT_FIXED_POINT_SPECIFIC (type
);
206 /* See gdbtypes.h. */
209 type_allocator::new_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
;
225 OBJSTAT (m_data
.objfile
, n_types
++);
226 type
->set_owner (m_data
.objfile
);
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. */
238 /* See gdbtypes.h. */
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
);
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
));
259 /* See gdbtypes.h. */
262 type_allocator::arch ()
265 return m_data
.type
->arch ();
267 return m_data
.objfile
->arch ();
268 return m_data
.gdbarch
;
271 /* See gdbtypes.h. */
276 struct gdbarch
*arch
;
278 if (this->is_objfile_owned ())
279 arch
= this->objfile_owner ()->arch ();
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
287 gdb_assert (arch
!= nullptr);
291 /* See gdbtypes.h. */
294 get_target_type (struct type
*type
)
298 type
= type
->target_type ();
300 type
= check_typedef (type
);
306 /* See gdbtypes.h. */
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. */
321 alloc_type_instance (struct type
*oldtype
)
325 /* Allocate the structure. */
327 if (!oldtype
->is_objfile_owned ())
328 type
= GDBARCH_OBSTACK_ZALLOC (oldtype
->arch_owner (), struct type
);
330 type
= OBSTACK_ZALLOC (&oldtype
->objfile_owner ()->objfile_obstack
,
333 TYPE_MAIN_TYPE (type
) = TYPE_MAIN_TYPE (oldtype
);
335 TYPE_CHAIN (type
) = type
; /* Chain back to itself for now. */
340 /* Clear all remnants of the previous type at TYPE, in preparation for
341 replacing it with something else. Preserve owner information. */
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. */
354 type
->set_owner (objfile
);
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. */
370 make_pointer_type (struct type
*type
, struct type
**typeptr
)
372 struct type
*ntype
; /* New type */
375 ntype
= TYPE_POINTER_TYPE (type
);
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. */
389 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
391 ntype
= type_allocator (type
).new_type ();
395 else /* We have storage, but need to reset it. */
398 chain
= TYPE_CHAIN (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
);
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. */
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). */
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
;
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
));
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. */
467 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
469 ntype
= type_allocator (type
).new_type ();
473 else /* We have storage, but need to reset it. */
476 chain
= TYPE_CHAIN (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
));
487 /* FIXME! Assume the machine has only one representation for
488 references, and that it matches the (only) representation for
491 ntype
->set_length (gdbarch_ptr_bit (type
->arch ()) / TARGET_CHAR_BIT
);
492 ntype
->set_code (refcode
);
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
);
507 /* Same as above, but caller doesn't care about memory allocation
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. */
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. */
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. */
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 ();
548 else /* We have storage, but need to reset it. */
554 ntype
->set_target_type (type
);
556 ntype
->set_length (1);
557 ntype
->set_code (TYPE_CODE_FUNC
);
559 INIT_FUNC_SPECIFIC (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. */
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
578 lookup_function_type_with_arguments (struct type
*type
,
580 struct type
**param_types
)
582 struct type
*fn
= make_function_type (type
, (struct type
**) 0);
587 if (param_types
[nparams
- 1] == NULL
)
590 fn
->set_has_varargs (true);
592 else if (check_typedef (param_types
[nparams
- 1])->code ()
596 /* Caller should have ensured this. */
597 gdb_assert (nparams
== 0);
598 fn
->set_is_prototyped (true);
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
]);
611 /* Identify address space identifier by name -- return a
612 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
,
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 address space name. */
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
)
643 else if (space_flag
& TYPE_INSTANCE_FLAG_DATA_SPACE
)
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
);
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. */
658 make_qualified_type (struct type
*type
, type_instance_flags new_flags
,
659 struct type
*storage
)
666 if (ntype
->instance_flags () == new_flags
)
668 ntype
= TYPE_CHAIN (ntype
);
670 while (ntype
!= type
);
672 /* Create a new type instance. */
674 ntype
= alloc_type_instance (type
);
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 ());
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
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 ());
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
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
))
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 wherever TYPE lives. If TYPEPTR is non-zero, set it to the
738 new type we construct. */
741 make_cv_type (int cnst
, int voltl
,
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
));
752 new_flags
|= TYPE_INSTANCE_FLAG_CONST
;
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
);
783 /* Make a 'restrict'-qualified version of TYPE. */
786 make_restrict_type (struct type
*type
)
788 return make_qualified_type (type
,
789 (type
->instance_flags ()
790 | TYPE_INSTANCE_FLAG_RESTRICT
),
794 /* Make a type without const, volatile, or restrict. */
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
)),
807 /* Make a '_Atomic'-qualified version of TYPE. */
810 make_atomic_type (struct type
*type
)
812 return make_qualified_type (type
,
813 (type
->instance_flags ()
814 | TYPE_INSTANCE_FLAG_ATOMIC
),
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 propagated 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. */
829 replace_type (struct type
*ntype
, struct type
*type
)
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. */
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. */
870 lookup_memberptr_type (struct type
*type
, struct type
*domain
)
874 mtype
= type_allocator (type
).new_type ();
875 smash_to_memberptr_type (mtype
, domain
, type
);
879 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
882 lookup_methodptr_type (struct type
*to_type
)
886 mtype
= type_allocator (to_type
).new_type ();
887 smash_to_methodptr_type (mtype
, to_type
);
891 /* See gdbtypes.h. */
894 operator== (const dynamic_prop
&l
, const dynamic_prop
&r
)
896 if (l
.kind () != r
.kind ())
904 return l
.const_val () == r
.const_val ();
905 case PROP_ADDR_OFFSET
:
908 return l
.baton () == r
.baton ();
909 case PROP_VARIANT_PARTS
:
910 return l
.variant_parts () == r
.variant_parts ();
912 return l
.original_type () == r
.original_type ();
915 gdb_assert_not_reached ("unhandled dynamic_prop kind");
918 /* See gdbtypes.h. */
921 operator== (const range_bounds
&l
, const range_bounds
&r
)
923 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
925 return (FIELD_EQ (low
)
927 && FIELD_EQ (flag_upper_bound_is_count
)
928 && FIELD_EQ (flag_bound_evaluated
)
934 /* See gdbtypes.h. */
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
,
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);
953 result_type
->set_length (check_typedef (index_type
)->length ());
956 = (struct range_bounds
*) TYPE_ZALLOC (result_type
, sizeof (range_bounds
));
957 bounds
->low
= *low_bound
;
958 bounds
->high
= *high_bound
;
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
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 ());
996 /* See gdbtypes.h. */
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
,
1004 const struct dynamic_prop
*stride
,
1007 struct type
*result_type
= create_range_type (alloc
, index_type
, low_bound
,
1010 gdb_assert (stride
!= nullptr);
1011 result_type
->bounds ()->stride
= *stride
;
1012 result_type
->bounds ()->flag_is_byte_stride
= byte_stride_p
;
1017 /* See gdbtypes.h. */
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
,
1034 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1035 are static, otherwise returns 0. */
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 ())
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 ())
1075 case TYPE_CODE_ENUM
:
1077 if (type
->num_fields () > 0)
1079 /* The enums may not be sorted by value, so search all
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 ();
1095 case TYPE_CODE_BOOL
:
1099 if (type
->length () > sizeof (LONGEST
)) /* Too big */
1102 if (!type
->is_unsigned ())
1103 return -(1 << (type
->length () * TARGET_CHAR_BIT
- 1));
1106 case TYPE_CODE_CHAR
:
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 ())
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 ())
1142 case TYPE_CODE_ENUM
:
1144 if (type
->num_fields () > 0)
1146 /* The enums may not be sorted by value, so search all
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 ();
1162 case TYPE_CODE_BOOL
:
1166 if (type
->length () > sizeof (LONGEST
)) /* Too big */
1169 if (!type
->is_unsigned ())
1171 LONGEST low
= -(1 << (type
->length () * TARGET_CHAR_BIT
- 1));
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
;
1190 /* See gdbtypes.h. */
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 ())
1199 std::optional
<LONGEST
> high
= get_discrete_high_bound (type
);
1200 if (!high
.has_value ())
1209 /* See gdbtypes.h */
1212 get_array_bounds (struct type
*type
, LONGEST
*low_bound
, LONGEST
*high_bound
)
1214 struct type
*index
= type
->index_type ();
1221 if (!get_discrete_bounds (index
, &low
, &high
))
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
)
1257 for (i
= 0; i
< type
->num_fields (); i
+= 1)
1259 if (val
== type
->field (i
).loc_enumval ())
1263 /* Invalid enumeration value. */
1270 /* If the array TYPE has static bounds calculate and update its
1271 size, then return true. Otherwise return false and leave TYPE
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
;
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);
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
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 ());
1337 /* See gdbtypes.h. */
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 accidentally 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);
1386 /* See gdbtypes.h. */
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);
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. */
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
,
1423 result_type
->set_code (TYPE_CODE_STRING
);
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
);
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)
1458 result_type
->set_is_unsigned (true);
1460 result_type
->field (0).set_type (domain_type
);
1465 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1466 and any array types nested inside it. */
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);
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
);
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. */
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
)
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
)
1521 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
1522 return TYPE_MAIN_TYPE (type
)->type_specific
.func_stuff
->self_type
;
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. */
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
;
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
;
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 weird 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
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
1568 smash_to_memberptr_type (struct type
*type
, struct type
*self_type
,
1569 struct type
*to_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
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
1587 smash_to_methodptr_type (struct type
*type
, struct type
*to_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
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
)
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
);
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
1630 type_name_or_error (struct type
*type
)
1632 struct type
*saved_type
= type
;
1634 struct objfile
*objfile
;
1636 type
= check_typedef (type
);
1638 name
= type
->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. */
1652 lookup_typename (const struct language_defn
*language
,
1654 const struct block
*block
, int noerr
)
1658 sym
= lookup_symbol_in_language (name
, block
, SEARCH_TYPE_DOMAIN
,
1659 language
->la_language
, NULL
).symbol
;
1662 struct type
*type
= sym
->type ();
1663 /* Ensure the length of TYPE is valid. */
1664 check_typedef (type
);
1670 error (_("No type named %s."), name
);
1674 lookup_unsigned_typename (const struct language_defn
*language
,
1678 uns
.reserve (strlen (name
) + strlen ("unsigned "));
1682 return lookup_typename (language
, uns
.c_str (), NULL
, 0);
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. */
1698 lookup_struct (const char *name
, const struct block
*block
)
1702 sym
= lookup_symbol (name
, block
, SEARCH_STRUCT_DOMAIN
, 0).symbol
;
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."),
1713 return (sym
->type ());
1716 /* Lookup a union type named "union NAME",
1717 visible in lexical block BLOCK. */
1720 lookup_union (const char *name
, const struct block
*block
)
1725 sym
= lookup_symbol (name
, block
, SEARCH_STRUCT_DOMAIN
, 0).symbol
;
1728 error (_("No union type named %s."), name
);
1732 if (t
->code () == TYPE_CODE_UNION
)
1735 /* If we get here, it's not a union. */
1736 error (_("This context has class, struct or enum %s, not a union."),
1740 /* Lookup an enum type named "enum NAME",
1741 visible in lexical block BLOCK. */
1744 lookup_enum (const char *name
, const struct block
*block
)
1748 sym
= lookup_symbol (name
, block
, SEARCH_STRUCT_DOMAIN
, 0).symbol
;
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."),
1758 return (sym
->type ());
1761 /* Lookup a template type named "template NAME<TYPE>",
1762 visible in lexical block BLOCK. */
1765 lookup_template_type (const char *name
, struct type
*type
,
1766 const struct block
*block
)
1769 nam
.reserve (strlen (name
) + strlen (type
->name ()) + strlen ("< >"));
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
;
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."),
1787 return (sym
->type ());
1790 /* See gdbtypes.h. */
1793 lookup_struct_elt (struct type
*type
, const char *name
, int noerr
)
1799 type
= check_typedef (type
);
1800 if (type
->code () != TYPE_CODE_PTR
1801 && type
->code () != TYPE_CODE_REF
)
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')
1825 = lookup_struct_elt (type
->field (i
).type (), name
, 1);
1826 if (elt
.field
!= NULL
)
1828 elt
.offset
+= type
->field (i
).loc_bitpos ();
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
)
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. */
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 ();
1861 /* Return the largest number representable by unsigned integer type TYPE. */
1864 get_unsigned_type_max (struct type
*type
)
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. */
1881 get_signed_type_minmax (struct type
*type
, LONGEST
*min
, LONGEST
*max
)
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. */
1897 get_pointer_type_max (struct type
*type
)
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
))
1924 return TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_fieldno
;
1927 /* Set the value of cplus_stuff.vptr_fieldno. */
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. */
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. */
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,
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)
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
));
1993 struct type
*basetype
;
1995 fieldno
= get_vptr_fieldno (baseclass
, &basetype
);
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
);
2006 *basetypep
= basetype
;
2017 *basetypep
= TYPE_VPTR_BASETYPE (type
);
2018 return TYPE_VPTR_FIELDNO (type
);
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. */
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. */
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
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
2057 if (TYPE_DATA_LOCATION (type
) != NULL
2058 && (TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCEXPR
2059 || TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCLIST
))
2062 if (TYPE_ASSOCIATED_PROP (type
))
2065 if (TYPE_ALLOCATED_PROP (type
))
2068 struct dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2069 if (prop
!= nullptr && prop
->kind () != PROP_TYPE
)
2072 if (TYPE_HAS_DYNAMIC_LENGTH (type
))
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))
2098 /* ... or the elements it contains have a dynamic contents... */
2099 if (is_dynamic_type_internal (type
->target_type (), false))
2101 /* ... or if it has a dynamic stride... */
2102 if (array_type_has_dynamic_stride (type
))
2107 case TYPE_CODE_STRUCT
:
2108 case TYPE_CODE_UNION
:
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 ())
2119 /* If the field has dynamic type, then so does TYPE. */
2120 if (is_dynamic_type_internal (type
->field (i
).type (), false))
2122 /* If the field is at a fixed offset, then it is not
2124 if (type
->field (i
).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK
)
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
))
2140 /* See gdbtypes.h. */
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
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
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)
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
;
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 ();
2190 low_bound
.set_optimized_out ();
2193 low_bound
.set_undefined ();
2195 prop
= &dyn_range_type
->bounds ()->high
;
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 ();
2210 high_bound
.set_optimized_out ();
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"));
2234 stride
.set_undefined ();
2235 byte_stride_p
= true;
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
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
)
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
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
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
);
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
);
2319 range_type
= check_typedef (type
->index_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
,
2331 else if (ary_dim
!= nullptr && ary_dim
->code () == TYPE_CODE_STRING
)
2333 /* The following special case for TYPE_CODE_STRING should not be
2334 needed, ideally we would defer resolving the dynamic type of the
2335 array elements until needed later, and indeed, the resolved type
2336 of each array element might be different, so attempting to resolve
2337 the type here makes no sense.
2339 However, in Fortran, for arrays of strings, each element must be
2340 the same type, as such, the DWARF for the string length relies on
2341 the object address of the array itself.
2343 The problem here is that, when we create values from the dynamic
2344 array type, we resolve the data location, and use that as the
2345 value address, this completely discards the original value
2346 address, and it is this original value address that is the
2347 descriptor for the dynamic array, the very address that the DWARF
2348 needs us to push in order to resolve the dynamic string length.
2350 What this means then, is that, given the current state of GDB, if
2351 we don't resolve the string length now, then we will have lost
2352 access to the address of the dynamic object descriptor, and so we
2353 will not be able to resolve the dynamic string later.
2355 For now then, we handle special case TYPE_CODE_STRING on behalf of
2356 Fortran, and hope that this doesn't cause problems for anyone
2358 elt_type
= resolve_dynamic_type_internal (type
->target_type (),
2359 addr_stack
, frame
, 0);
2362 elt_type
= type
->target_type ();
2364 prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2365 if (prop
!= nullptr && type
->code () == TYPE_CODE_STRING
)
2367 if (prop
!= NULL
&& resolve_p
)
2369 if (dwarf2_evaluate_property (prop
, frame
, addr_stack
, &value
))
2371 type
->remove_dyn_prop (DYN_PROP_BYTE_STRIDE
);
2372 bit_stride
= (unsigned int) (value
* 8);
2376 /* Could be a bug in our code, but it could also happen
2377 if the DWARF info is not correct. Issue a warning,
2378 and assume no byte/bit stride (leave bit_stride = 0). */
2379 warning (_("cannot determine array stride for type %s"),
2380 type
->name () ? type
->name () : "<no name>");
2384 bit_stride
= type
->field (0).bitsize ();
2386 type_allocator
alloc (type
, type_allocator::SMASH
);
2387 if (type
->code () == TYPE_CODE_STRING
)
2388 return create_string_type (alloc
, elt_type
, range_type
);
2390 return create_array_type_with_stride (alloc
, elt_type
, range_type
, NULL
,
2394 /* Resolve an array or string type with dynamic properties, return a new
2395 type with the dynamic properties resolved to actual values. The
2396 ADDR_STACK represents the location of the object being resolved. */
2398 static struct type
*
2399 resolve_dynamic_array_or_string (struct type
*type
,
2400 struct property_addr_info
*addr_stack
,
2401 const frame_info_ptr
&frame
)
2406 /* For dynamic type resolution strings can be treated like arrays of
2408 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2409 || type
->code () == TYPE_CODE_STRING
);
2411 type
= copy_type (type
);
2413 /* Resolve the rank property to get rank value. */
2414 struct dynamic_prop
*prop
= TYPE_RANK_PROP (type
);
2415 if (dwarf2_evaluate_property (prop
, frame
, addr_stack
, &value
))
2417 prop
->set_const_val (value
);
2422 /* Rank is zero, if a variable is passed as an argument to a
2423 function. In this case the resolved type should not be an
2424 array, but should instead be that of an array element. */
2425 struct type
*dynamic_array_type
= type
;
2426 type
= copy_type (dynamic_array_type
->target_type ());
2427 struct dynamic_prop_list
*prop_list
2428 = TYPE_MAIN_TYPE (dynamic_array_type
)->dyn_prop_list
;
2429 if (prop_list
!= nullptr)
2431 struct obstack
*obstack
2432 = &type
->objfile_owner ()->objfile_obstack
;
2433 TYPE_MAIN_TYPE (type
)->dyn_prop_list
2434 = copy_dynamic_prop_list (obstack
, prop_list
);
2438 else if (type
->code () == TYPE_CODE_STRING
&& rank
!= 1)
2440 /* What would this even mean? A string with a dynamic rank
2442 error (_("unable to handle string with dynamic rank greater than 1"));
2446 /* Arrays with dynamic rank are initially just an array type
2447 with a target type that is the array element.
2449 However, now we know the rank of the array we need to build
2450 the array of arrays structure that GDB expects, that is we
2451 need an array type that has a target which is an array type,
2452 and so on, until eventually, we have the element type at the
2453 end of the chain. Create all the additional array types here
2454 by copying the top level array type. */
2455 struct type
*element_type
= type
->target_type ();
2456 struct type
*rank_type
= type
;
2457 for (int i
= 1; i
< rank
; i
++)
2459 rank_type
->set_target_type (copy_type (rank_type
));
2460 rank_type
= rank_type
->target_type ();
2462 rank_type
->set_target_type (element_type
);
2469 for (struct type
*tmp_type
= check_typedef (type
->target_type ());
2470 tmp_type
->code () == TYPE_CODE_ARRAY
;
2471 tmp_type
= check_typedef (tmp_type
->target_type ()))
2475 /* The rank that we calculated above is actually a count of the number of
2476 ranks. However, when we resolve the type of each individual array
2477 rank we should actually use a rank "offset", e.g. an array with a rank
2478 count of 1 (calculated above) will use the rank offset 0 in order to
2479 resolve the details of the first array dimension. As a result, we
2480 reduce the rank by 1 here. */
2483 return resolve_dynamic_array_or_string_1 (type
, addr_stack
, frame
, rank
,
2487 /* Resolve dynamic bounds of members of the union TYPE to static
2488 bounds. ADDR_STACK is a stack of struct property_addr_info
2489 to be used if needed during the dynamic resolution. */
2491 static struct type
*
2492 resolve_dynamic_union (struct type
*type
,
2493 struct property_addr_info
*addr_stack
,
2494 const frame_info_ptr
&frame
)
2496 struct type
*resolved_type
;
2498 unsigned int max_len
= 0;
2500 gdb_assert (type
->code () == TYPE_CODE_UNION
);
2502 resolved_type
= copy_type (type
);
2503 resolved_type
->copy_fields (type
);
2504 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2508 if (type
->field (i
).is_static ())
2511 t
= resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2512 addr_stack
, frame
, false);
2513 resolved_type
->field (i
).set_type (t
);
2515 struct type
*real_type
= check_typedef (t
);
2516 if (real_type
->length () > max_len
)
2517 max_len
= real_type
->length ();
2520 resolved_type
->set_length (max_len
);
2521 return resolved_type
;
2524 /* See gdbtypes.h. */
2527 variant::matches (ULONGEST value
, bool is_unsigned
) const
2529 for (const discriminant_range
&range
: discriminants
)
2530 if (range
.contains (value
, is_unsigned
))
2536 compute_variant_fields_inner (struct type
*type
,
2537 struct property_addr_info
*addr_stack
,
2538 const variant_part
&part
,
2539 std::vector
<bool> &flags
);
2541 /* A helper function to determine which variant fields will be active.
2542 This handles both the variant's direct fields, and any variant
2543 parts embedded in this variant. TYPE is the type we're examining.
2544 ADDR_STACK holds information about the concrete object. VARIANT is
2545 the current variant to be handled. FLAGS is where the results are
2546 stored -- this function sets the Nth element in FLAGS if the
2547 corresponding field is enabled. ENABLED is whether this variant is
2551 compute_variant_fields_recurse (struct type
*type
,
2552 struct property_addr_info
*addr_stack
,
2553 const variant
&variant
,
2554 std::vector
<bool> &flags
,
2557 for (int field
= variant
.first_field
; field
< variant
.last_field
; ++field
)
2558 flags
[field
] = enabled
;
2560 for (const variant_part
&new_part
: variant
.parts
)
2563 compute_variant_fields_inner (type
, addr_stack
, new_part
, flags
);
2566 for (const auto &sub_variant
: new_part
.variants
)
2567 compute_variant_fields_recurse (type
, addr_stack
, sub_variant
,
2573 /* A helper function to determine which variant fields will be active.
2574 This evaluates the discriminant, decides which variant (if any) is
2575 active, and then updates FLAGS to reflect which fields should be
2576 available. TYPE is the type we're examining. ADDR_STACK holds
2577 information about the concrete object. VARIANT is the current
2578 variant to be handled. FLAGS is where the results are stored --
2579 this function sets the Nth element in FLAGS if the corresponding
2580 field is enabled. */
2583 compute_variant_fields_inner (struct type
*type
,
2584 struct property_addr_info
*addr_stack
,
2585 const variant_part
&part
,
2586 std::vector
<bool> &flags
)
2588 /* Evaluate the discriminant. */
2589 std::optional
<ULONGEST
> discr_value
;
2590 if (part
.discriminant_index
!= -1)
2592 int idx
= part
.discriminant_index
;
2594 if (type
->field (idx
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2595 error (_("Cannot determine struct field location"
2596 " (invalid location kind)"));
2598 if (addr_stack
->valaddr
.data () != NULL
)
2599 discr_value
= unpack_field_as_long (type
, addr_stack
->valaddr
.data (),
2603 CORE_ADDR addr
= (addr_stack
->addr
2604 + (type
->field (idx
).loc_bitpos ()
2605 / TARGET_CHAR_BIT
));
2607 LONGEST bitsize
= type
->field (idx
).bitsize ();
2608 LONGEST size
= bitsize
/ 8;
2610 size
= type
->field (idx
).type ()->length ();
2612 gdb_byte bits
[sizeof (ULONGEST
)];
2613 read_memory (addr
, bits
, size
);
2615 LONGEST bitpos
= (type
->field (idx
).loc_bitpos ()
2618 discr_value
= unpack_bits_as_long (type
->field (idx
).type (),
2619 bits
, bitpos
, bitsize
);
2623 /* Go through each variant and see which applies. */
2624 const variant
*default_variant
= nullptr;
2625 const variant
*applied_variant
= nullptr;
2626 for (const auto &variant
: part
.variants
)
2628 if (variant
.is_default ())
2629 default_variant
= &variant
;
2630 else if (discr_value
.has_value ()
2631 && variant
.matches (*discr_value
, part
.is_unsigned
))
2633 applied_variant
= &variant
;
2637 if (applied_variant
== nullptr)
2638 applied_variant
= default_variant
;
2640 for (const auto &variant
: part
.variants
)
2641 compute_variant_fields_recurse (type
, addr_stack
, variant
,
2642 flags
, applied_variant
== &variant
);
2645 /* Determine which variant fields are available in TYPE. The enabled
2646 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2647 about the concrete object. PARTS describes the top-level variant
2648 parts for this type. */
2651 compute_variant_fields (struct type
*type
,
2652 struct type
*resolved_type
,
2653 struct property_addr_info
*addr_stack
,
2654 const gdb::array_view
<variant_part
> &parts
)
2656 /* Assume all fields are included by default. */
2657 std::vector
<bool> flags (resolved_type
->num_fields (), true);
2659 /* Now disable fields based on the variants that control them. */
2660 for (const auto &part
: parts
)
2661 compute_variant_fields_inner (type
, addr_stack
, part
, flags
);
2663 unsigned int nfields
= std::count (flags
.begin (), flags
.end (), true);
2664 /* No need to zero-initialize the newly allocated fields, they'll be
2665 initialized by the copy in the loop below. */
2666 resolved_type
->alloc_fields (nfields
, false);
2669 for (int i
= 0; i
< type
->num_fields (); ++i
)
2674 resolved_type
->field (out
) = type
->field (i
);
2679 /* Resolve dynamic bounds of members of the struct TYPE to static
2680 bounds. ADDR_STACK is a stack of struct property_addr_info to
2681 be used if needed during the dynamic resolution. */
2683 static struct type
*
2684 resolve_dynamic_struct (struct type
*type
,
2685 struct property_addr_info
*addr_stack
,
2686 const frame_info_ptr
&frame
)
2688 struct type
*resolved_type
;
2690 unsigned resolved_type_bit_length
= 0;
2692 gdb_assert (type
->code () == TYPE_CODE_STRUCT
);
2694 resolved_type
= copy_type (type
);
2696 dynamic_prop
*variant_prop
= resolved_type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2697 if (variant_prop
!= nullptr && variant_prop
->kind () == PROP_VARIANT_PARTS
)
2699 compute_variant_fields (type
, resolved_type
, addr_stack
,
2700 *variant_prop
->variant_parts ());
2701 /* We want to leave the property attached, so that the Rust code
2702 can tell whether the type was originally an enum. */
2703 variant_prop
->set_original_type (type
);
2707 resolved_type
->copy_fields (type
);
2710 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2712 unsigned new_bit_length
;
2713 struct property_addr_info pinfo
;
2715 if (resolved_type
->field (i
).is_static ())
2718 if (resolved_type
->field (i
).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK
)
2720 struct dwarf2_property_baton baton
;
2722 = lookup_pointer_type (resolved_type
->field (i
).type ());
2723 baton
.locexpr
= *resolved_type
->field (i
).loc_dwarf_block ();
2725 struct dynamic_prop prop
;
2726 prop
.set_locexpr (&baton
);
2729 if (dwarf2_evaluate_property (&prop
, frame
, addr_stack
, &addr
,
2730 {addr_stack
->addr
}))
2731 resolved_type
->field (i
).set_loc_bitpos
2732 (TARGET_CHAR_BIT
* (addr
- addr_stack
->addr
));
2735 /* As we know this field is not a static field, the field's
2736 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2737 this is the case, but only trigger a simple error rather
2738 than an internal error if that fails. While failing
2739 that verification indicates a bug in our code, the error
2740 is not severe enough to suggest to the user he stops
2741 his debugging session because of it. */
2742 if (resolved_type
->field (i
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2743 error (_("Cannot determine struct field location"
2744 " (invalid location kind)"));
2746 pinfo
.type
= check_typedef (resolved_type
->field (i
).type ());
2747 size_t offset
= resolved_type
->field (i
).loc_bitpos () / TARGET_CHAR_BIT
;
2748 pinfo
.valaddr
= addr_stack
->valaddr
;
2749 if (!pinfo
.valaddr
.empty ())
2750 pinfo
.valaddr
= pinfo
.valaddr
.slice (offset
);
2751 pinfo
.addr
= addr_stack
->addr
+ offset
;
2752 pinfo
.next
= addr_stack
;
2754 resolved_type
->field (i
).set_type
2755 (resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2756 &pinfo
, frame
, false));
2757 gdb_assert (resolved_type
->field (i
).loc_kind ()
2758 == FIELD_LOC_KIND_BITPOS
);
2760 new_bit_length
= resolved_type
->field (i
).loc_bitpos ();
2761 if (resolved_type
->field (i
).bitsize () != 0)
2762 new_bit_length
+= resolved_type
->field (i
).bitsize ();
2765 struct type
*real_type
2766 = check_typedef (resolved_type
->field (i
).type ());
2768 new_bit_length
+= (real_type
->length () * TARGET_CHAR_BIT
);
2771 /* Normally, we would use the position and size of the last field
2772 to determine the size of the enclosing structure. But GCC seems
2773 to be encoding the position of some fields incorrectly when
2774 the struct contains a dynamic field that is not placed last.
2775 So we compute the struct size based on the field that has
2776 the highest position + size - probably the best we can do. */
2777 if (new_bit_length
> resolved_type_bit_length
)
2778 resolved_type_bit_length
= new_bit_length
;
2781 /* The length of a type won't change for fortran, but it does for C and Ada.
2782 For fortran the size of dynamic fields might change over time but not the
2783 type length of the structure. If we adapt it, we run into problems
2784 when calculating the element offset for arrays of structs. */
2785 if (current_language
->la_language
!= language_fortran
)
2786 resolved_type
->set_length ((resolved_type_bit_length
+ TARGET_CHAR_BIT
- 1)
2789 /* The Ada language uses this field as a cache for static fixed types: reset
2790 it as RESOLVED_TYPE must have its own static fixed type. */
2791 resolved_type
->set_target_type (nullptr);
2793 return resolved_type
;
2796 /* Worker for resolved_dynamic_type. */
2798 static struct type
*
2799 resolve_dynamic_type_internal (struct type
*type
,
2800 struct property_addr_info
*addr_stack
,
2801 const frame_info_ptr
&frame
,
2804 struct type
*real_type
= check_typedef (type
);
2805 struct type
*resolved_type
= nullptr;
2806 struct dynamic_prop
*prop
;
2809 if (!is_dynamic_type_internal (real_type
, top_level
))
2812 std::optional
<CORE_ADDR
> type_length
;
2813 prop
= TYPE_DYNAMIC_LENGTH (type
);
2815 && dwarf2_evaluate_property (prop
, frame
, addr_stack
, &value
))
2816 type_length
= value
;
2818 if (type
->code () == TYPE_CODE_TYPEDEF
)
2820 resolved_type
= copy_type (type
);
2821 resolved_type
->set_target_type
2822 (resolve_dynamic_type_internal (type
->target_type (), addr_stack
,
2827 /* Before trying to resolve TYPE, make sure it is not a stub. */
2830 switch (type
->code ())
2834 case TYPE_CODE_RVALUE_REF
:
2836 struct property_addr_info pinfo
;
2838 pinfo
.type
= check_typedef (type
->target_type ());
2840 if (addr_stack
->valaddr
.data () != NULL
)
2841 pinfo
.addr
= extract_typed_address (addr_stack
->valaddr
.data (),
2844 pinfo
.addr
= read_memory_typed_address (addr_stack
->addr
, type
);
2845 pinfo
.next
= addr_stack
;
2847 /* Special case a NULL pointer here -- we don't want to
2849 if (pinfo
.addr
!= 0)
2851 resolved_type
= copy_type (type
);
2852 resolved_type
->set_target_type
2853 (resolve_dynamic_type_internal (type
->target_type (),
2854 &pinfo
, frame
, true));
2859 case TYPE_CODE_STRING
:
2860 /* Strings are very much like an array of characters, and can be
2861 treated as one here. */
2862 case TYPE_CODE_ARRAY
:
2863 resolved_type
= resolve_dynamic_array_or_string (type
, addr_stack
,
2867 case TYPE_CODE_RANGE
:
2868 /* Pass 0 for the rank value here, which indicates this is a
2869 range for the first rank of an array. The assumption is that
2870 this rank value is not actually required for the resolution of
2871 the dynamic range, otherwise, we'd be resolving this range
2872 within the context of a dynamic array. */
2873 resolved_type
= resolve_dynamic_range (type
, addr_stack
, frame
, 0);
2876 case TYPE_CODE_UNION
:
2877 resolved_type
= resolve_dynamic_union (type
, addr_stack
, frame
);
2880 case TYPE_CODE_STRUCT
:
2881 resolved_type
= resolve_dynamic_struct (type
, addr_stack
, frame
);
2886 if (resolved_type
== nullptr)
2889 if (type_length
.has_value ())
2891 resolved_type
->set_length (*type_length
);
2892 resolved_type
->remove_dyn_prop (DYN_PROP_BYTE_SIZE
);
2895 /* Resolve data_location attribute. */
2896 prop
= TYPE_DATA_LOCATION (resolved_type
);
2898 && dwarf2_evaluate_property (prop
, frame
, addr_stack
, &value
))
2900 /* Start of Fortran hack. See comment in f-lang.h for what is going
2902 if (current_language
->la_language
== language_fortran
2903 && resolved_type
->code () == TYPE_CODE_ARRAY
)
2904 value
= fortran_adjust_dynamic_array_base_address_hack (resolved_type
,
2906 /* End of Fortran hack. */
2907 prop
->set_const_val (value
);
2910 return resolved_type
;
2913 /* See gdbtypes.h */
2916 resolve_dynamic_type (struct type
*type
,
2917 gdb::array_view
<const gdb_byte
> valaddr
,
2919 const frame_info_ptr
*in_frame
)
2921 struct property_addr_info pinfo
2922 = {check_typedef (type
), valaddr
, addr
, NULL
};
2924 frame_info_ptr frame
;
2925 if (in_frame
!= nullptr)
2928 return resolve_dynamic_type_internal (type
, &pinfo
, frame
, true);
2931 /* See gdbtypes.h */
2934 type::dyn_prop (dynamic_prop_node_kind prop_kind
) const
2936 dynamic_prop_list
*node
= this->main_type
->dyn_prop_list
;
2938 while (node
!= NULL
)
2940 if (node
->prop_kind
== prop_kind
)
2947 /* See gdbtypes.h */
2950 type::add_dyn_prop (dynamic_prop_node_kind prop_kind
, dynamic_prop prop
)
2952 struct dynamic_prop_list
*temp
;
2954 gdb_assert (this->is_objfile_owned ());
2956 temp
= XOBNEW (&this->objfile_owner ()->objfile_obstack
,
2957 struct dynamic_prop_list
);
2958 temp
->prop_kind
= prop_kind
;
2960 temp
->next
= this->main_type
->dyn_prop_list
;
2962 this->main_type
->dyn_prop_list
= temp
;
2965 /* See gdbtypes.h. */
2968 type::remove_dyn_prop (dynamic_prop_node_kind kind
)
2970 struct dynamic_prop_list
*prev_node
, *curr_node
;
2972 curr_node
= this->main_type
->dyn_prop_list
;
2975 while (NULL
!= curr_node
)
2977 if (curr_node
->prop_kind
== kind
)
2979 /* Update the linked list but don't free anything.
2980 The property was allocated on obstack and it is not known
2981 if we are on top of it. Nevertheless, everything is released
2982 when the complete obstack is freed. */
2983 if (NULL
== prev_node
)
2984 this->main_type
->dyn_prop_list
= curr_node
->next
;
2986 prev_node
->next
= curr_node
->next
;
2991 prev_node
= curr_node
;
2992 curr_node
= curr_node
->next
;
2996 /* Find the real type of TYPE. This function returns the real type,
2997 after removing all layers of typedefs, and completing opaque or stub
2998 types. Completion changes the TYPE argument, but stripping of
3001 Instance flags (e.g. const/volatile) are preserved as typedefs are
3002 stripped. If necessary a new qualified form of the underlying type
3005 NOTE: This will return a typedef if type::target_type for the typedef has
3006 not been computed and we're either in the middle of reading symbols, or
3007 there was no name for the typedef in the debug info.
3009 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
3010 QUITs in the symbol reading code can also throw.
3011 Thus this function can throw an exception.
3013 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
3016 If this is a stubbed struct (i.e. declared as struct foo *), see if
3017 we can find a full definition in some other file. If so, copy this
3018 definition, so we can use it in future. There used to be a comment
3019 (but not any code) that if we don't find a full definition, we'd
3020 set a flag so we don't spend time in the future checking the same
3021 type. That would be a mistake, though--we might load in more
3022 symbols which contain a full definition for the type. */
3025 check_typedef (struct type
*type
)
3027 struct type
*orig_type
= type
;
3031 /* While we're removing typedefs, we don't want to lose qualifiers.
3032 E.g., const/volatile. */
3033 type_instance_flags instance_flags
= type
->instance_flags ();
3035 while (type
->code () == TYPE_CODE_TYPEDEF
)
3037 if (!type
->target_type ())
3042 /* It is dangerous to call lookup_symbol if we are currently
3043 reading a symtab. Infinite recursion is one danger. */
3044 if (currently_reading_symtab
)
3045 return make_qualified_type (type
, instance_flags
, NULL
);
3047 name
= type
->name ();
3050 stub_noname_complaint ();
3051 return make_qualified_type (type
, instance_flags
, NULL
);
3053 domain_search_flag flag
3054 = ((type
->language () == language_c
3055 || type
->language () == language_objc
3056 || type
->language () == language_opencl
3057 || type
->language () == language_minimal
)
3058 ? SEARCH_STRUCT_DOMAIN
3059 : SEARCH_TYPE_DOMAIN
);
3060 sym
= lookup_symbol (name
, nullptr, flag
, nullptr).symbol
;
3062 type
->set_target_type (sym
->type ());
3063 else /* TYPE_CODE_UNDEF */
3064 type
->set_target_type (type_allocator (type
->arch ()).new_type ());
3066 type
= type
->target_type ();
3068 /* Preserve the instance flags as we traverse down the typedef chain.
3070 Handling address spaces/classes is nasty, what do we do if there's a
3072 E.g., what if an outer typedef marks the type as class_1 and an inner
3073 typedef marks the type as class_2?
3074 This is the wrong place to do such error checking. We leave it to
3075 the code that created the typedef in the first place to flag the
3076 error. We just pick the outer address space (akin to letting the
3077 outer cast in a chain of casting win), instead of assuming
3078 "it can't happen". */
3080 const type_instance_flags ALL_SPACES
3081 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3082 | TYPE_INSTANCE_FLAG_DATA_SPACE
);
3083 const type_instance_flags ALL_CLASSES
3084 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
;
3086 type_instance_flags new_instance_flags
= type
->instance_flags ();
3088 /* Treat code vs data spaces and address classes separately. */
3089 if ((instance_flags
& ALL_SPACES
) != 0)
3090 new_instance_flags
&= ~ALL_SPACES
;
3091 if ((instance_flags
& ALL_CLASSES
) != 0)
3092 new_instance_flags
&= ~ALL_CLASSES
;
3094 instance_flags
|= new_instance_flags
;
3098 /* If this is a struct/class/union with no fields, then check
3099 whether a full definition exists somewhere else. This is for
3100 systems where a type definition with no fields is issued for such
3101 types, instead of identifying them as stub types in the first
3104 if (TYPE_IS_OPAQUE (type
)
3105 && opaque_type_resolution
3106 && !currently_reading_symtab
)
3108 const char *name
= type
->name ();
3109 struct type
*newtype
;
3113 stub_noname_complaint ();
3114 return make_qualified_type (type
, instance_flags
, NULL
);
3116 newtype
= lookup_transparent_type (name
);
3120 /* If the resolved type and the stub are in the same
3121 objfile, then replace the stub type with the real deal.
3122 But if they're in separate objfiles, leave the stub
3123 alone; we'll just look up the transparent type every time
3124 we call check_typedef. We can't create pointers between
3125 types allocated to different objfiles, since they may
3126 have different lifetimes. Trying to copy NEWTYPE over to
3127 TYPE's objfile is pointless, too, since you'll have to
3128 move over any other types NEWTYPE refers to, which could
3129 be an unbounded amount of stuff. */
3130 if (newtype
->objfile_owner () == type
->objfile_owner ())
3131 type
= make_qualified_type (newtype
, type
->instance_flags (), type
);
3136 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3138 else if (type
->is_stub () && !currently_reading_symtab
)
3140 const char *name
= type
->name ();
3145 stub_noname_complaint ();
3146 return make_qualified_type (type
, instance_flags
, NULL
);
3148 domain_search_flag flag
3149 = ((type
->language () == language_c
3150 || type
->language () == language_objc
3151 || type
->language () == language_opencl
3152 || type
->language () == language_minimal
)
3153 ? SEARCH_STRUCT_DOMAIN
3154 : SEARCH_TYPE_DOMAIN
);
3155 sym
= lookup_symbol (name
, nullptr, flag
, nullptr).symbol
;
3158 /* Same as above for opaque types, we can replace the stub
3159 with the complete type only if they are in the same
3161 if (sym
->type ()->objfile_owner () == type
->objfile_owner ())
3162 type
= make_qualified_type (sym
->type (),
3163 type
->instance_flags (), type
);
3165 type
= sym
->type ();
3169 if (type
->target_is_stub ())
3171 struct type
*target_type
= check_typedef (type
->target_type ());
3173 if (target_type
->is_stub () || target_type
->target_is_stub ())
3175 /* Nothing we can do. */
3177 else if (type
->code () == TYPE_CODE_RANGE
)
3179 type
->set_length (target_type
->length ());
3180 type
->set_target_is_stub (false);
3182 else if (type
->code () == TYPE_CODE_ARRAY
3183 && update_static_array_size (type
))
3184 type
->set_target_is_stub (false);
3187 type
= make_qualified_type (type
, instance_flags
, NULL
);
3189 /* Cache TYPE_LENGTH for future use. */
3190 orig_type
->set_length (type
->length ());
3195 /* Parse a type expression in the string [P..P+LENGTH). If an error
3196 occurs, silently return a void type. */
3198 static struct type
*
3199 safe_parse_type (struct gdbarch
*gdbarch
, const char *p
, int length
)
3201 struct type
*type
= NULL
; /* Initialize to keep gcc happy. */
3203 /* Suppress error messages. */
3204 scoped_restore saved_gdb_stderr
= make_scoped_restore (&gdb_stderr
,
3207 /* Call parse_and_eval_type() without fear of longjmp()s. */
3210 type
= parse_and_eval_type (p
, length
);
3212 catch (const gdb_exception_error
&except
)
3214 type
= builtin_type (gdbarch
)->builtin_void
;
3220 /* Ugly hack to convert method stubs into method types.
3222 He ain't kiddin'. This demangles the name of the method into a
3223 string including argument types, parses out each argument type,
3224 generates a string casting a zero to that type, evaluates the
3225 string, and stuffs the resulting type into an argtype vector!!!
3226 Then it knows the type of the whole function (including argument
3227 types for overloading), which info used to be in the stab's but was
3228 removed to hack back the space required for them. */
3231 check_stub_method (struct type
*type
, int method_id
, int signature_id
)
3233 struct gdbarch
*gdbarch
= type
->arch ();
3235 char *mangled_name
= gdb_mangle_name (type
, method_id
, signature_id
);
3236 gdb::unique_xmalloc_ptr
<char> demangled_name
3237 = gdb_demangle (mangled_name
, DMGL_PARAMS
| DMGL_ANSI
);
3238 char *argtypetext
, *p
;
3239 int depth
= 0, argcount
= 1;
3240 struct field
*argtypes
;
3243 /* Make sure we got back a function string that we can use. */
3245 p
= strchr (demangled_name
.get (), '(');
3249 if (demangled_name
== NULL
|| p
== NULL
)
3250 error (_("Internal: Cannot demangle mangled name `%s'."),
3253 /* Now, read in the parameters that define this type. */
3258 if (*p
== '(' || *p
== '<')
3262 else if (*p
== ')' || *p
== '>')
3266 else if (*p
== ',' && depth
== 0)
3274 /* If we read one argument and it was ``void'', don't count it. */
3275 if (startswith (argtypetext
, "(void)"))
3278 /* We need one extra slot, for the THIS pointer. */
3280 argtypes
= (struct field
*)
3281 TYPE_ZALLOC (type
, (argcount
+ 1) * sizeof (struct field
));
3284 /* Add THIS pointer for non-static methods. */
3285 f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3286 if (TYPE_FN_FIELD_STATIC_P (f
, signature_id
))
3290 argtypes
[0].set_type (lookup_pointer_type (type
));
3294 if (*p
!= ')') /* () means no args, skip while. */
3299 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
3301 /* Avoid parsing of ellipsis, they will be handled below.
3302 Also avoid ``void'' as above. */
3303 if (strncmp (argtypetext
, "...", p
- argtypetext
) != 0
3304 && strncmp (argtypetext
, "void", p
- argtypetext
) != 0)
3306 argtypes
[argcount
].set_type
3307 (safe_parse_type (gdbarch
, argtypetext
, p
- argtypetext
));
3310 argtypetext
= p
+ 1;
3313 if (*p
== '(' || *p
== '<')
3317 else if (*p
== ')' || *p
== '>')
3326 TYPE_FN_FIELD_PHYSNAME (f
, signature_id
) = mangled_name
;
3328 /* Now update the old "stub" type into a real type. */
3329 mtype
= TYPE_FN_FIELD_TYPE (f
, signature_id
);
3330 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3331 We want a method (TYPE_CODE_METHOD). */
3332 smash_to_method_type (mtype
, type
, mtype
->target_type (),
3333 argtypes
, argcount
, p
[-2] == '.');
3334 mtype
->set_is_stub (false);
3335 TYPE_FN_FIELD_STUB (f
, signature_id
) = 0;
3338 /* This is the external interface to check_stub_method, above. This
3339 function unstubs all of the signatures for TYPE's METHOD_ID method
3340 name. After calling this function TYPE_FN_FIELD_STUB will be
3341 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3344 This function unfortunately can not die until stabs do. */
3347 check_stub_method_group (struct type
*type
, int method_id
)
3349 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, method_id
);
3350 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3352 for (int j
= 0; j
< len
; j
++)
3354 if (TYPE_FN_FIELD_STUB (f
, j
))
3355 check_stub_method (type
, method_id
, j
);
3359 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3360 const struct cplus_struct_type cplus_struct_default
= { };
3363 allocate_cplus_struct_type (struct type
*type
)
3365 if (HAVE_CPLUS_STRUCT (type
))
3366 /* Structure was already allocated. Nothing more to do. */
3369 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_CPLUS_STUFF
;
3370 TYPE_RAW_CPLUS_SPECIFIC (type
) = (struct cplus_struct_type
*)
3371 TYPE_ZALLOC (type
, sizeof (struct cplus_struct_type
));
3372 *(TYPE_RAW_CPLUS_SPECIFIC (type
)) = cplus_struct_default
;
3373 set_type_vptr_fieldno (type
, -1);
3376 const struct gnat_aux_type gnat_aux_default
=
3379 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3380 and allocate the associated gnat-specific data. The gnat-specific
3381 data is also initialized to gnat_aux_default. */
3384 allocate_gnat_aux_type (struct type
*type
)
3386 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_GNAT_STUFF
;
3387 TYPE_GNAT_SPECIFIC (type
) = (struct gnat_aux_type
*)
3388 TYPE_ZALLOC (type
, sizeof (struct gnat_aux_type
));
3389 *(TYPE_GNAT_SPECIFIC (type
)) = gnat_aux_default
;
3392 /* Helper function to verify floating-point format and size.
3393 BIT is the type size in bits; if BIT equals -1, the size is
3394 determined by the floatformat. Returns size to be used. */
3397 verify_floatformat (int bit
, const struct floatformat
*floatformat
)
3399 gdb_assert (floatformat
!= NULL
);
3402 bit
= floatformat
->totalsize
;
3404 gdb_assert (bit
>= 0);
3405 gdb_assert (bit
>= floatformat
->totalsize
);
3410 /* Return the floating-point format for a floating-point variable of
3413 const struct floatformat
*
3414 floatformat_from_type (const struct type
*type
)
3416 gdb_assert (type
->code () == TYPE_CODE_FLT
);
3417 gdb_assert (TYPE_FLOATFORMAT (type
));
3418 return TYPE_FLOATFORMAT (type
);
3421 /* See gdbtypes.h. */
3424 init_integer_type (type_allocator
&alloc
,
3425 int bit
, int unsigned_p
, const char *name
)
3429 t
= alloc
.new_type (TYPE_CODE_INT
, bit
, name
);
3431 t
->set_is_unsigned (true);
3433 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3434 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3435 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3440 /* See gdbtypes.h. */
3443 init_character_type (type_allocator
&alloc
,
3444 int bit
, int unsigned_p
, const char *name
)
3448 t
= alloc
.new_type (TYPE_CODE_CHAR
, bit
, name
);
3450 t
->set_is_unsigned (true);
3455 /* See gdbtypes.h. */
3458 init_boolean_type (type_allocator
&alloc
,
3459 int bit
, int unsigned_p
, const char *name
)
3463 t
= alloc
.new_type (TYPE_CODE_BOOL
, bit
, name
);
3465 t
->set_is_unsigned (true);
3467 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3468 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3469 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3474 /* See gdbtypes.h. */
3477 init_float_type (type_allocator
&alloc
,
3478 int bit
, const char *name
,
3479 const struct floatformat
**floatformats
,
3480 enum bfd_endian byte_order
)
3482 if (byte_order
== BFD_ENDIAN_UNKNOWN
)
3484 struct gdbarch
*gdbarch
= alloc
.arch ();
3485 byte_order
= gdbarch_byte_order (gdbarch
);
3487 const struct floatformat
*fmt
= floatformats
[byte_order
];
3490 bit
= verify_floatformat (bit
, fmt
);
3491 t
= alloc
.new_type (TYPE_CODE_FLT
, bit
, name
);
3492 TYPE_FLOATFORMAT (t
) = fmt
;
3497 /* See gdbtypes.h. */
3500 init_decfloat_type (type_allocator
&alloc
, int bit
, const char *name
)
3502 return alloc
.new_type (TYPE_CODE_DECFLOAT
, bit
, name
);
3505 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3508 can_create_complex_type (struct type
*target_type
)
3510 return (target_type
->code () == TYPE_CODE_INT
3511 || target_type
->code () == TYPE_CODE_FLT
);
3514 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3515 name. TARGET_TYPE is the component type. */
3518 init_complex_type (const char *name
, struct type
*target_type
)
3522 gdb_assert (can_create_complex_type (target_type
));
3524 if (TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
== nullptr)
3526 if (name
== nullptr && target_type
->name () != nullptr)
3528 /* No zero-initialization required, initialized by strcpy/strcat
3531 = (char *) TYPE_ALLOC (target_type
,
3532 strlen (target_type
->name ())
3533 + strlen ("_Complex ") + 1);
3534 strcpy (new_name
, "_Complex ");
3535 strcat (new_name
, target_type
->name ());
3539 t
= type_allocator (target_type
).new_type ();
3540 set_type_code (t
, TYPE_CODE_COMPLEX
);
3541 t
->set_length (2 * target_type
->length ());
3544 t
->set_target_type (target_type
);
3545 TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
= t
;
3548 return TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
;
3551 /* See gdbtypes.h. */
3554 init_pointer_type (type_allocator
&alloc
,
3555 int bit
, const char *name
, struct type
*target_type
)
3559 t
= alloc
.new_type (TYPE_CODE_PTR
, bit
, name
);
3560 t
->set_target_type (target_type
);
3561 t
->set_is_unsigned (true);
3565 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3566 BIT is the pointer type size in bits.
3567 UNSIGNED_P should be nonzero if the type is unsigned.
3568 NAME is the type name. */
3571 init_fixed_point_type (type_allocator
&alloc
,
3572 int bit
, int unsigned_p
, const char *name
)
3576 t
= alloc
.new_type (TYPE_CODE_FIXED_POINT
, bit
, name
);
3578 t
->set_is_unsigned (true);
3583 /* See gdbtypes.h. */
3586 type_raw_align (struct type
*type
)
3588 if (type
->align_log2
!= 0)
3589 return 1 << (type
->align_log2
- 1);
3593 /* See gdbtypes.h. */
3596 type_align (struct type
*type
)
3598 /* Check alignment provided in the debug information. */
3599 unsigned raw_align
= type_raw_align (type
);
3603 /* Allow the architecture to provide an alignment. */
3604 ULONGEST align
= gdbarch_type_align (type
->arch (), type
);
3608 switch (type
->code ())
3611 case TYPE_CODE_FUNC
:
3612 case TYPE_CODE_FLAGS
:
3614 case TYPE_CODE_RANGE
:
3616 case TYPE_CODE_ENUM
:
3618 case TYPE_CODE_RVALUE_REF
:
3619 case TYPE_CODE_CHAR
:
3620 case TYPE_CODE_BOOL
:
3621 case TYPE_CODE_DECFLOAT
:
3622 case TYPE_CODE_METHODPTR
:
3623 case TYPE_CODE_MEMBERPTR
:
3624 align
= type_length_units (check_typedef (type
));
3627 case TYPE_CODE_ARRAY
:
3628 case TYPE_CODE_COMPLEX
:
3629 case TYPE_CODE_TYPEDEF
:
3630 align
= type_align (type
->target_type ());
3633 case TYPE_CODE_STRUCT
:
3634 case TYPE_CODE_UNION
:
3636 int number_of_non_static_fields
= 0;
3637 for (unsigned i
= 0; i
< type
->num_fields (); ++i
)
3639 if (!type
->field (i
).is_static ())
3641 number_of_non_static_fields
++;
3642 ULONGEST f_align
= type_align (type
->field (i
).type ());
3645 /* Don't pretend we know something we don't. */
3649 if (f_align
> align
)
3653 /* A struct with no fields, or with only static fields has an
3655 if (number_of_non_static_fields
== 0)
3661 case TYPE_CODE_STRING
:
3662 /* Not sure what to do here, and these can't appear in C or C++
3666 case TYPE_CODE_VOID
:
3670 case TYPE_CODE_ERROR
:
3671 case TYPE_CODE_METHOD
:
3676 if ((align
& (align
- 1)) != 0)
3678 /* Not a power of 2, so pass. */
3685 /* See gdbtypes.h. */
3688 set_type_align (struct type
*type
, ULONGEST align
)
3690 /* Must be a power of 2. Zero is ok. */
3691 gdb_assert ((align
& (align
- 1)) == 0);
3693 unsigned result
= 0;
3700 if (result
>= (1 << TYPE_ALIGN_BITS
))
3703 type
->align_log2
= result
;
3708 /* Queries on types. */
3711 can_dereference (struct type
*t
)
3713 /* FIXME: Should we return true for references as well as
3715 t
= check_typedef (t
);
3718 && t
->code () == TYPE_CODE_PTR
3719 && t
->target_type ()->code () != TYPE_CODE_VOID
);
3723 is_integral_type (struct type
*t
)
3725 t
= check_typedef (t
);
3728 && !is_fixed_point_type (t
)
3729 && ((t
->code () == TYPE_CODE_INT
)
3730 || (t
->code () == TYPE_CODE_ENUM
)
3731 || (t
->code () == TYPE_CODE_FLAGS
)
3732 || (t
->code () == TYPE_CODE_CHAR
)
3733 || (t
->code () == TYPE_CODE_RANGE
)
3734 || (t
->code () == TYPE_CODE_BOOL
)));
3738 is_floating_type (struct type
*t
)
3740 t
= check_typedef (t
);
3743 && ((t
->code () == TYPE_CODE_FLT
)
3744 || (t
->code () == TYPE_CODE_DECFLOAT
)));
3747 /* Return true if TYPE is scalar. */
3750 is_scalar_type (struct type
*type
)
3752 type
= check_typedef (type
);
3754 if (is_fixed_point_type (type
))
3755 return 0; /* Implemented as a scalar, but more like a floating point. */
3757 switch (type
->code ())
3759 case TYPE_CODE_ARRAY
:
3760 case TYPE_CODE_STRUCT
:
3761 case TYPE_CODE_UNION
:
3763 case TYPE_CODE_STRING
:
3770 /* Return true if T is scalar, or a composite type which in practice has
3771 the memory layout of a scalar type. E.g., an array or struct with only
3772 one scalar element inside it, or a union with only scalar elements. */
3775 is_scalar_type_recursive (struct type
*t
)
3777 t
= check_typedef (t
);
3779 if (is_scalar_type (t
))
3781 /* Are we dealing with an array or string of known dimensions? */
3782 else if ((t
->code () == TYPE_CODE_ARRAY
3783 || t
->code () == TYPE_CODE_STRING
) && t
->num_fields () == 1
3784 && t
->index_type ()->code () == TYPE_CODE_RANGE
)
3786 LONGEST low_bound
, high_bound
;
3787 struct type
*elt_type
= check_typedef (t
->target_type ());
3789 if (get_discrete_bounds (t
->index_type (), &low_bound
, &high_bound
))
3790 return (high_bound
== low_bound
3791 && is_scalar_type_recursive (elt_type
));
3795 /* Are we dealing with a struct with one element? */
3796 else if (t
->code () == TYPE_CODE_STRUCT
&& t
->num_fields () == 1)
3797 return is_scalar_type_recursive (t
->field (0).type ());
3798 else if (t
->code () == TYPE_CODE_UNION
)
3800 int i
, n
= t
->num_fields ();
3802 /* If all elements of the union are scalar, then the union is scalar. */
3803 for (i
= 0; i
< n
; i
++)
3804 if (!is_scalar_type_recursive (t
->field (i
).type ()))
3813 /* Return true is T is a class or a union. False otherwise. */
3816 class_or_union_p (const struct type
*t
)
3818 return (t
->code () == TYPE_CODE_STRUCT
3819 || t
->code () == TYPE_CODE_UNION
);
3822 /* A helper function which returns true if types A and B represent the
3823 "same" class type. This is true if the types have the same main
3824 type, or the same name. */
3827 class_types_same_p (const struct type
*a
, const struct type
*b
)
3829 return (TYPE_MAIN_TYPE (a
) == TYPE_MAIN_TYPE (b
)
3830 || (a
->name () && b
->name ()
3831 && !strcmp (a
->name (), b
->name ())));
3834 /* If BASE is an ancestor of DCLASS return the distance between them.
3835 otherwise return -1;
3839 class B: public A {};
3840 class C: public B {};
3843 distance_to_ancestor (A, A, 0) = 0
3844 distance_to_ancestor (A, B, 0) = 1
3845 distance_to_ancestor (A, C, 0) = 2
3846 distance_to_ancestor (A, D, 0) = 3
3848 If PUBLIC is 1 then only public ancestors are considered,
3849 and the function returns the distance only if BASE is a public ancestor
3853 distance_to_ancestor (A, D, 1) = -1. */
3856 distance_to_ancestor (struct type
*base
, struct type
*dclass
, int is_public
)
3861 base
= check_typedef (base
);
3862 dclass
= check_typedef (dclass
);
3864 if (class_types_same_p (base
, dclass
))
3867 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
); i
++)
3869 if (is_public
&& ! BASETYPE_VIA_PUBLIC (dclass
, i
))
3872 d
= distance_to_ancestor (base
, TYPE_BASECLASS (dclass
, i
), is_public
);
3880 /* Check whether BASE is an ancestor or base class or DCLASS
3881 Return 1 if so, and 0 if not.
3882 Note: If BASE and DCLASS are of the same type, this function
3883 will return 1. So for some class A, is_ancestor (A, A) will
3887 is_ancestor (struct type
*base
, struct type
*dclass
)
3889 return distance_to_ancestor (base
, dclass
, 0) >= 0;
3892 /* Like is_ancestor, but only returns true when BASE is a public
3893 ancestor of DCLASS. */
3896 is_public_ancestor (struct type
*base
, struct type
*dclass
)
3898 return distance_to_ancestor (base
, dclass
, 1) >= 0;
3901 /* A helper function for is_unique_ancestor. */
3904 is_unique_ancestor_worker (struct type
*base
, struct type
*dclass
,
3906 const gdb_byte
*valaddr
, int embedded_offset
,
3907 CORE_ADDR address
, struct value
*val
)
3911 base
= check_typedef (base
);
3912 dclass
= check_typedef (dclass
);
3914 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
) && count
< 2; ++i
)
3919 iter
= check_typedef (TYPE_BASECLASS (dclass
, i
));
3921 this_offset
= baseclass_offset (dclass
, i
, valaddr
, embedded_offset
,
3924 if (class_types_same_p (base
, iter
))
3926 /* If this is the first subclass, set *OFFSET and set count
3927 to 1. Otherwise, if this is at the same offset as
3928 previous instances, do nothing. Otherwise, increment
3932 *offset
= this_offset
;
3935 else if (this_offset
== *offset
)
3943 count
+= is_unique_ancestor_worker (base
, iter
, offset
,
3945 embedded_offset
+ this_offset
,
3952 /* Like is_ancestor, but only returns true if BASE is a unique base
3953 class of the type of VAL. */
3956 is_unique_ancestor (struct type
*base
, struct value
*val
)
3960 return is_unique_ancestor_worker (base
, val
->type (), &offset
,
3961 val
->contents_for_printing ().data (),
3962 val
->embedded_offset (),
3963 val
->address (), val
) == 1;
3966 /* See gdbtypes.h. */
3969 type_byte_order (const struct type
*type
)
3971 bfd_endian byteorder
= gdbarch_byte_order (type
->arch ());
3972 if (type
->endianity_is_not_default ())
3974 if (byteorder
== BFD_ENDIAN_BIG
)
3975 return BFD_ENDIAN_LITTLE
;
3978 gdb_assert (byteorder
== BFD_ENDIAN_LITTLE
);
3979 return BFD_ENDIAN_BIG
;
3986 /* See gdbtypes.h. */
3989 is_nocall_function (const struct type
*type
)
3991 if (type
->code () != TYPE_CODE_FUNC
&& type
->code () != TYPE_CODE_METHOD
)
3994 return TYPE_CALLING_CONVENTION (type
) == DW_CC_nocall
;
3998 /* Overload resolution. */
4000 /* Return the sum of the rank of A with the rank of B. */
4003 sum_ranks (struct rank a
, struct rank b
)
4006 c
.rank
= a
.rank
+ b
.rank
;
4007 c
.subrank
= a
.subrank
+ b
.subrank
;
4011 /* Compare rank A and B and return:
4013 1 if a is better than b
4014 -1 if b is better than a. */
4017 compare_ranks (struct rank a
, struct rank b
)
4019 if (a
.rank
== b
.rank
)
4021 if (a
.subrank
== b
.subrank
)
4023 if (a
.subrank
< b
.subrank
)
4025 if (a
.subrank
> b
.subrank
)
4029 if (a
.rank
< b
.rank
)
4032 /* a.rank > b.rank */
4036 /* Functions for overload resolution begin here. */
4038 /* Compare two badness vectors A and B and return the result.
4039 0 => A and B are identical
4040 1 => A and B are incomparable
4041 2 => A is better than B
4042 3 => A is worse than B */
4045 compare_badness (const badness_vector
&a
, const badness_vector
&b
)
4049 /* Any positives in comparison? */
4050 bool found_pos
= false;
4051 /* Any negatives in comparison? */
4052 bool found_neg
= false;
4053 /* Did A have any INVALID_CONVERSION entries. */
4054 bool a_invalid
= false;
4055 /* Did B have any INVALID_CONVERSION entries. */
4056 bool b_invalid
= false;
4058 /* differing sizes => incomparable */
4059 if (a
.size () != b
.size ())
4062 /* Subtract b from a */
4063 for (i
= 0; i
< a
.size (); i
++)
4065 tmp
= compare_ranks (b
[i
], a
[i
]);
4070 if (a
[i
].rank
>= INVALID_CONVERSION
)
4072 if (b
[i
].rank
>= INVALID_CONVERSION
)
4076 /* B will only be considered better than or incomparable to A if
4077 they both have invalid entries, or if neither does. That is, if
4078 A has only valid entries, and B has an invalid entry, then A will
4079 be considered better than B, even if B happens to be better for
4081 if (a_invalid
!= b_invalid
)
4084 return 3; /* A > B */
4085 return 2; /* A < B */
4090 return 1; /* incomparable */
4092 return 3; /* A > B */
4098 return 2; /* A < B */
4100 return 0; /* A == B */
4104 /* Rank a function by comparing its parameter types (PARMS), to the
4105 types of an argument list (ARGS). Return the badness vector. This
4106 has ARGS.size() + 1 entries. */
4109 rank_function (gdb::array_view
<type
*> parms
,
4110 gdb::array_view
<value
*> args
,
4113 /* add 1 for the length-match rank. */
4115 bv
.reserve (1 + args
.size ());
4117 /* First compare the lengths of the supplied lists.
4118 If there is a mismatch, set it to a high value. */
4120 /* pai/1997-06-03 FIXME: when we have debug info about default
4121 arguments and ellipsis parameter lists, we should consider those
4122 and rank the length-match more finely. */
4124 bv
.push_back ((args
.size () != parms
.size ()
4125 && (! varargs
|| args
.size () < parms
.size ()))
4126 ? LENGTH_MISMATCH_BADNESS
4127 : EXACT_MATCH_BADNESS
);
4129 /* Now rank all the parameters of the candidate function. */
4130 size_t min_len
= std::min (parms
.size (), args
.size ());
4132 for (size_t i
= 0; i
< min_len
; i
++)
4133 bv
.push_back (rank_one_type (parms
[i
], args
[i
]->type (),
4136 /* If more arguments than parameters, add dummy entries. */
4137 for (size_t i
= min_len
; i
< args
.size (); i
++)
4138 bv
.push_back (varargs
? VARARG_BADNESS
: TOO_FEW_PARAMS_BADNESS
);
4143 /* Compare the names of two integer types, assuming that any sign
4144 qualifiers have been checked already. We do it this way because
4145 there may be an "int" in the name of one of the types. */
4148 integer_types_same_name_p (const char *first
, const char *second
)
4150 int first_p
, second_p
;
4152 /* If both are shorts, return 1; if neither is a short, keep
4154 first_p
= (strstr (first
, "short") != NULL
);
4155 second_p
= (strstr (second
, "short") != NULL
);
4156 if (first_p
&& second_p
)
4158 if (first_p
|| second_p
)
4161 /* Likewise for long. */
4162 first_p
= (strstr (first
, "long") != NULL
);
4163 second_p
= (strstr (second
, "long") != NULL
);
4164 if (first_p
&& second_p
)
4166 if (first_p
|| second_p
)
4169 /* Likewise for char. */
4170 first_p
= (strstr (first
, "char") != NULL
);
4171 second_p
= (strstr (second
, "char") != NULL
);
4172 if (first_p
&& second_p
)
4174 if (first_p
|| second_p
)
4177 /* They must both be ints. */
4181 /* Compares type A to type B. Returns true if they represent the same
4182 type, false otherwise. */
4185 types_equal (struct type
*a
, struct type
*b
)
4187 /* Identical type pointers. */
4188 /* However, this still doesn't catch all cases of same type for b
4189 and a. The reason is that builtin types are different from
4190 the same ones constructed from the object. */
4194 /* Resolve typedefs */
4195 if (a
->code () == TYPE_CODE_TYPEDEF
)
4196 a
= check_typedef (a
);
4197 if (b
->code () == TYPE_CODE_TYPEDEF
)
4198 b
= check_typedef (b
);
4200 /* Check if identical after resolving typedefs. */
4204 /* If after resolving typedefs a and b are not of the same type
4205 code then they are not equal. */
4206 if (a
->code () != b
->code ())
4209 /* If a and b are both pointers types or both reference types then
4210 they are equal of the same type iff the objects they refer to are
4211 of the same type. */
4212 if (a
->code () == TYPE_CODE_PTR
4213 || a
->code () == TYPE_CODE_REF
)
4214 return types_equal (a
->target_type (),
4217 /* Well, damnit, if the names are exactly the same, I'll say they
4218 are exactly the same. This happens when we generate method
4219 stubs. The types won't point to the same address, but they
4220 really are the same. */
4222 if (a
->name () && b
->name ()
4223 && strcmp (a
->name (), b
->name ()) == 0)
4226 /* Two function types are equal if their argument and return types
4228 if (a
->code () == TYPE_CODE_FUNC
)
4232 if (a
->num_fields () != b
->num_fields ())
4235 if (!types_equal (a
->target_type (), b
->target_type ()))
4238 for (i
= 0; i
< a
->num_fields (); ++i
)
4239 if (!types_equal (a
->field (i
).type (), b
->field (i
).type ()))
4245 /* Two array types are the same if they have the same element types
4246 and array bounds. */
4247 if (a
->code () == TYPE_CODE_ARRAY
)
4249 if (!types_equal (a
->target_type (), b
->target_type ()))
4252 if (*a
->bounds () != *b
->bounds ())
4261 /* Deep comparison of types. */
4263 /* An entry in the type-equality bcache. */
4265 struct type_equality_entry
4267 type_equality_entry (struct type
*t1
, struct type
*t2
)
4273 struct type
*type1
, *type2
;
4276 /* A helper function to compare two strings. Returns true if they are
4277 the same, false otherwise. Handles NULLs properly. */
4280 compare_maybe_null_strings (const char *s
, const char *t
)
4282 if (s
== NULL
|| t
== NULL
)
4284 return strcmp (s
, t
) == 0;
4287 /* A helper function for check_types_worklist that checks two types for
4288 "deep" equality. Returns true if the types are considered the
4289 same, false otherwise. */
4292 check_types_equal (struct type
*type1
, struct type
*type2
,
4293 std::vector
<type_equality_entry
> *worklist
)
4295 type1
= check_typedef (type1
);
4296 type2
= check_typedef (type2
);
4301 if (type1
->code () != type2
->code ()
4302 || type1
->length () != type2
->length ()
4303 || type1
->is_unsigned () != type2
->is_unsigned ()
4304 || type1
->has_no_signedness () != type2
->has_no_signedness ()
4305 || type1
->endianity_is_not_default () != type2
->endianity_is_not_default ()
4306 || type1
->has_varargs () != type2
->has_varargs ()
4307 || type1
->is_vector () != type2
->is_vector ()
4308 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
4309 || type1
->instance_flags () != type2
->instance_flags ()
4310 || type1
->num_fields () != type2
->num_fields ())
4313 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4315 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4318 if (type1
->code () == TYPE_CODE_RANGE
)
4320 if (*type1
->bounds () != *type2
->bounds ())
4327 for (i
= 0; i
< type1
->num_fields (); ++i
)
4329 const struct field
*field1
= &type1
->field (i
);
4330 const struct field
*field2
= &type2
->field (i
);
4332 if (field1
->is_artificial () != field2
->is_artificial ()
4333 || field1
->bitsize () != field2
->bitsize ()
4334 || field1
->loc_kind () != field2
->loc_kind ())
4336 if (!compare_maybe_null_strings (field1
->name (), field2
->name ()))
4338 switch (field1
->loc_kind ())
4340 case FIELD_LOC_KIND_BITPOS
:
4341 if (field1
->loc_bitpos () != field2
->loc_bitpos ())
4344 case FIELD_LOC_KIND_ENUMVAL
:
4345 if (field1
->loc_enumval () != field2
->loc_enumval ())
4347 /* Don't compare types of enum fields, because they don't
4350 case FIELD_LOC_KIND_PHYSADDR
:
4351 if (field1
->loc_physaddr () != field2
->loc_physaddr ())
4354 case FIELD_LOC_KIND_PHYSNAME
:
4355 if (!compare_maybe_null_strings (field1
->loc_physname (),
4356 field2
->loc_physname ()))
4359 case FIELD_LOC_KIND_DWARF_BLOCK
:
4361 struct dwarf2_locexpr_baton
*block1
, *block2
;
4363 block1
= field1
->loc_dwarf_block ();
4364 block2
= field2
->loc_dwarf_block ();
4365 if (block1
->per_cu
!= block2
->per_cu
4366 || block1
->size
!= block2
->size
4367 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
4372 internal_error (_("Unsupported field kind "
4373 "%d by check_types_equal"),
4374 field1
->loc_kind ());
4377 worklist
->emplace_back (field1
->type (), field2
->type ());
4381 if (type1
->target_type () != NULL
)
4383 if (type2
->target_type () == NULL
)
4386 worklist
->emplace_back (type1
->target_type (),
4387 type2
->target_type ());
4389 else if (type2
->target_type () != NULL
)
4395 /* Check types on a worklist for equality. Returns false if any pair
4396 is not equal, true if they are all considered equal. */
4399 check_types_worklist (std::vector
<type_equality_entry
> *worklist
,
4402 while (!worklist
->empty ())
4406 struct type_equality_entry entry
= std::move (worklist
->back ());
4407 worklist
->pop_back ();
4409 /* If the type pair has already been visited, we know it is
4411 cache
->insert (entry
, &added
);
4415 if (!check_types_equal (entry
.type1
, entry
.type2
, worklist
))
4422 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4423 "deep comparison". Otherwise return false. */
4426 types_deeply_equal (struct type
*type1
, struct type
*type2
)
4428 std::vector
<type_equality_entry
> worklist
;
4430 gdb_assert (type1
!= NULL
&& type2
!= NULL
);
4432 /* Early exit for the simple case. */
4437 worklist
.emplace_back (type1
, type2
);
4438 return check_types_worklist (&worklist
, &cache
);
4441 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4442 Otherwise return one. */
4445 type_not_allocated (const struct type
*type
)
4447 struct dynamic_prop
*prop
= TYPE_ALLOCATED_PROP (type
);
4449 return prop
!= nullptr && prop
->is_constant () && prop
->const_val () == 0;
4452 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4453 Otherwise return one. */
4456 type_not_associated (const struct type
*type
)
4458 struct dynamic_prop
*prop
= TYPE_ASSOCIATED_PROP (type
);
4460 return prop
!= nullptr && prop
->is_constant () && prop
->const_val () == 0;
4463 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4466 rank_one_type_parm_ptr (struct type
*parm
, struct type
*arg
, struct value
*value
)
4468 struct rank rank
= {0,0};
4470 switch (arg
->code ())
4474 /* Allowed pointer conversions are:
4475 (a) pointer to void-pointer conversion. */
4476 if (parm
->target_type ()->code () == TYPE_CODE_VOID
)
4477 return VOID_PTR_CONVERSION_BADNESS
;
4479 /* (b) pointer to ancestor-pointer conversion. */
4480 rank
.subrank
= distance_to_ancestor (parm
->target_type (),
4481 arg
->target_type (),
4483 if (rank
.subrank
>= 0)
4484 return sum_ranks (BASE_PTR_CONVERSION_BADNESS
, rank
);
4486 return INCOMPATIBLE_TYPE_BADNESS
;
4487 case TYPE_CODE_ARRAY
:
4489 struct type
*t1
= parm
->target_type ();
4490 struct type
*t2
= arg
->target_type ();
4492 if (types_equal (t1
, t2
))
4494 /* Make sure they are CV equal. */
4495 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4496 rank
.subrank
|= CV_CONVERSION_CONST
;
4497 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4498 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4499 if (rank
.subrank
!= 0)
4500 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4501 return EXACT_MATCH_BADNESS
;
4503 return INCOMPATIBLE_TYPE_BADNESS
;
4505 case TYPE_CODE_FUNC
:
4506 return rank_one_type (parm
->target_type (), arg
, NULL
);
4508 if (value
!= NULL
&& value
->type ()->code () == TYPE_CODE_INT
)
4510 if (value_as_long (value
) == 0)
4512 /* Null pointer conversion: allow it to be cast to a pointer.
4513 [4.10.1 of C++ standard draft n3290] */
4514 return NULL_POINTER_CONVERSION_BADNESS
;
4518 /* If type checking is disabled, allow the conversion. */
4519 if (!strict_type_checking
)
4520 return NS_INTEGER_POINTER_CONVERSION_BADNESS
;
4524 case TYPE_CODE_ENUM
:
4525 case TYPE_CODE_FLAGS
:
4526 case TYPE_CODE_CHAR
:
4527 case TYPE_CODE_RANGE
:
4528 case TYPE_CODE_BOOL
:
4530 return INCOMPATIBLE_TYPE_BADNESS
;
4534 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4537 rank_one_type_parm_array (struct type
*parm
, struct type
*arg
, struct value
*value
)
4539 switch (arg
->code ())
4542 case TYPE_CODE_ARRAY
:
4543 return rank_one_type (parm
->target_type (),
4544 arg
->target_type (), NULL
);
4546 return INCOMPATIBLE_TYPE_BADNESS
;
4550 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4553 rank_one_type_parm_func (struct type
*parm
, struct type
*arg
, struct value
*value
)
4555 switch (arg
->code ())
4557 case TYPE_CODE_PTR
: /* funcptr -> func */
4558 return rank_one_type (parm
, arg
->target_type (), NULL
);
4560 return INCOMPATIBLE_TYPE_BADNESS
;
4564 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4567 rank_one_type_parm_int (struct type
*parm
, struct type
*arg
, struct value
*value
)
4569 switch (arg
->code ())
4572 if (arg
->length () == parm
->length ())
4574 /* Deal with signed, unsigned, and plain chars and
4575 signed and unsigned ints. */
4576 if (parm
->has_no_signedness ())
4578 /* This case only for character types. */
4579 if (arg
->has_no_signedness ())
4580 return EXACT_MATCH_BADNESS
; /* plain char -> plain char */
4581 else /* signed/unsigned char -> plain char */
4582 return INTEGER_CONVERSION_BADNESS
;
4584 else if (parm
->is_unsigned ())
4586 if (arg
->is_unsigned ())
4588 /* unsigned int -> unsigned int, or
4589 unsigned long -> unsigned long */
4590 if (integer_types_same_name_p (parm
->name (),
4592 return EXACT_MATCH_BADNESS
;
4593 else if (integer_types_same_name_p (arg
->name (),
4595 && integer_types_same_name_p (parm
->name (),
4597 /* unsigned int -> unsigned long */
4598 return INTEGER_PROMOTION_BADNESS
;
4600 /* unsigned long -> unsigned int */
4601 return INTEGER_CONVERSION_BADNESS
;
4605 if (integer_types_same_name_p (arg
->name (),
4607 && integer_types_same_name_p (parm
->name (),
4609 /* signed long -> unsigned int */
4610 return INTEGER_CONVERSION_BADNESS
;
4612 /* signed int/long -> unsigned int/long */
4613 return INTEGER_CONVERSION_BADNESS
;
4616 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4618 if (integer_types_same_name_p (parm
->name (),
4620 return EXACT_MATCH_BADNESS
;
4621 else if (integer_types_same_name_p (arg
->name (),
4623 && integer_types_same_name_p (parm
->name (),
4625 return INTEGER_PROMOTION_BADNESS
;
4627 return INTEGER_CONVERSION_BADNESS
;
4630 return INTEGER_CONVERSION_BADNESS
;
4632 else if (arg
->length () < parm
->length ())
4633 return INTEGER_PROMOTION_BADNESS
;
4635 return INTEGER_CONVERSION_BADNESS
;
4636 case TYPE_CODE_ENUM
:
4637 case TYPE_CODE_FLAGS
:
4638 case TYPE_CODE_CHAR
:
4639 case TYPE_CODE_RANGE
:
4640 case TYPE_CODE_BOOL
:
4641 if (arg
->is_declared_class ())
4642 return INCOMPATIBLE_TYPE_BADNESS
;
4643 return INTEGER_PROMOTION_BADNESS
;
4645 return INT_FLOAT_CONVERSION_BADNESS
;
4647 return NS_POINTER_CONVERSION_BADNESS
;
4649 return INCOMPATIBLE_TYPE_BADNESS
;
4653 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4656 rank_one_type_parm_enum (struct type
*parm
, struct type
*arg
, struct value
*value
)
4658 switch (arg
->code ())
4661 case TYPE_CODE_CHAR
:
4662 case TYPE_CODE_RANGE
:
4663 case TYPE_CODE_BOOL
:
4664 case TYPE_CODE_ENUM
:
4665 if (parm
->is_declared_class () || arg
->is_declared_class ())
4666 return INCOMPATIBLE_TYPE_BADNESS
;
4667 return INTEGER_CONVERSION_BADNESS
;
4669 return INT_FLOAT_CONVERSION_BADNESS
;
4671 return INCOMPATIBLE_TYPE_BADNESS
;
4675 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4678 rank_one_type_parm_char (struct type
*parm
, struct type
*arg
, struct value
*value
)
4680 switch (arg
->code ())
4682 case TYPE_CODE_RANGE
:
4683 case TYPE_CODE_BOOL
:
4684 case TYPE_CODE_ENUM
:
4685 if (arg
->is_declared_class ())
4686 return INCOMPATIBLE_TYPE_BADNESS
;
4687 return INTEGER_CONVERSION_BADNESS
;
4689 return INT_FLOAT_CONVERSION_BADNESS
;
4691 if (arg
->length () > parm
->length ())
4692 return INTEGER_CONVERSION_BADNESS
;
4693 else if (arg
->length () < parm
->length ())
4694 return INTEGER_PROMOTION_BADNESS
;
4696 case TYPE_CODE_CHAR
:
4697 /* Deal with signed, unsigned, and plain chars for C++ and
4698 with int cases falling through from previous case. */
4699 if (parm
->has_no_signedness ())
4701 if (arg
->has_no_signedness ())
4702 return EXACT_MATCH_BADNESS
;
4704 return INTEGER_CONVERSION_BADNESS
;
4706 else if (parm
->is_unsigned ())
4708 if (arg
->is_unsigned ())
4709 return EXACT_MATCH_BADNESS
;
4711 return INTEGER_PROMOTION_BADNESS
;
4713 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4714 return EXACT_MATCH_BADNESS
;
4716 return INTEGER_CONVERSION_BADNESS
;
4718 return INCOMPATIBLE_TYPE_BADNESS
;
4722 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4725 rank_one_type_parm_range (struct type
*parm
, struct type
*arg
, struct value
*value
)
4727 switch (arg
->code ())
4730 case TYPE_CODE_CHAR
:
4731 case TYPE_CODE_RANGE
:
4732 case TYPE_CODE_BOOL
:
4733 case TYPE_CODE_ENUM
:
4734 return INTEGER_CONVERSION_BADNESS
;
4736 return INT_FLOAT_CONVERSION_BADNESS
;
4738 return INCOMPATIBLE_TYPE_BADNESS
;
4742 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4745 rank_one_type_parm_bool (struct type
*parm
, struct type
*arg
, struct value
*value
)
4747 switch (arg
->code ())
4749 /* n3290 draft, section 4.12.1 (conv.bool):
4751 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4752 pointer to member type can be converted to a prvalue of type
4753 bool. A zero value, null pointer value, or null member pointer
4754 value is converted to false; any other value is converted to
4755 true. A prvalue of type std::nullptr_t can be converted to a
4756 prvalue of type bool; the resulting value is false." */
4758 case TYPE_CODE_CHAR
:
4759 case TYPE_CODE_ENUM
:
4761 case TYPE_CODE_MEMBERPTR
:
4763 return BOOL_CONVERSION_BADNESS
;
4764 case TYPE_CODE_RANGE
:
4765 return INCOMPATIBLE_TYPE_BADNESS
;
4766 case TYPE_CODE_BOOL
:
4767 return EXACT_MATCH_BADNESS
;
4769 return INCOMPATIBLE_TYPE_BADNESS
;
4773 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4776 rank_one_type_parm_float (struct type
*parm
, struct type
*arg
, struct value
*value
)
4778 switch (arg
->code ())
4781 if (arg
->length () < parm
->length ())
4782 return FLOAT_PROMOTION_BADNESS
;
4783 else if (arg
->length () == parm
->length ())
4784 return EXACT_MATCH_BADNESS
;
4786 return FLOAT_CONVERSION_BADNESS
;
4788 case TYPE_CODE_BOOL
:
4789 case TYPE_CODE_ENUM
:
4790 case TYPE_CODE_RANGE
:
4791 case TYPE_CODE_CHAR
:
4792 return INT_FLOAT_CONVERSION_BADNESS
;
4794 return INCOMPATIBLE_TYPE_BADNESS
;
4798 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4801 rank_one_type_parm_complex (struct type
*parm
, struct type
*arg
, struct value
*value
)
4803 switch (arg
->code ())
4804 { /* Strictly not needed for C++, but... */
4806 return FLOAT_PROMOTION_BADNESS
;
4807 case TYPE_CODE_COMPLEX
:
4808 return EXACT_MATCH_BADNESS
;
4810 return INCOMPATIBLE_TYPE_BADNESS
;
4814 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4817 rank_one_type_parm_struct (struct type
*parm
, struct type
*arg
, struct value
*value
)
4819 struct rank rank
= {0, 0};
4821 switch (arg
->code ())
4823 case TYPE_CODE_STRUCT
:
4824 /* Check for derivation */
4825 rank
.subrank
= distance_to_ancestor (parm
, arg
, 0);
4826 if (rank
.subrank
>= 0)
4827 return sum_ranks (BASE_CONVERSION_BADNESS
, rank
);
4830 return INCOMPATIBLE_TYPE_BADNESS
;
4834 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4837 rank_one_type_parm_set (struct type
*parm
, struct type
*arg
, struct value
*value
)
4839 switch (arg
->code ())
4843 return rank_one_type (parm
->field (0).type (),
4844 arg
->field (0).type (), NULL
);
4846 return INCOMPATIBLE_TYPE_BADNESS
;
4850 /* Compare one type (PARM) for compatibility with another (ARG).
4851 * PARM is intended to be the parameter type of a function; and
4852 * ARG is the supplied argument's type. This function tests if
4853 * the latter can be converted to the former.
4854 * VALUE is the argument's value or NULL if none (or called recursively)
4856 * Return 0 if they are identical types;
4857 * Otherwise, return an integer which corresponds to how compatible
4858 * PARM is to ARG. The higher the return value, the worse the match.
4859 * Generally the "bad" conversions are all uniformly assigned
4860 * INVALID_CONVERSION. */
4863 rank_one_type (struct type
*parm
, struct type
*arg
, struct value
*value
)
4865 struct rank rank
= {0,0};
4867 /* Resolve typedefs */
4868 if (parm
->code () == TYPE_CODE_TYPEDEF
)
4869 parm
= check_typedef (parm
);
4870 if (arg
->code () == TYPE_CODE_TYPEDEF
)
4871 arg
= check_typedef (arg
);
4873 if (TYPE_IS_REFERENCE (parm
) && value
!= NULL
)
4875 if (value
->lval () == not_lval
)
4877 /* Rvalues should preferably bind to rvalue references or const
4878 lvalue references. */
4879 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4880 rank
.subrank
= REFERENCE_CONVERSION_RVALUE
;
4881 else if (TYPE_CONST (parm
->target_type ()))
4882 rank
.subrank
= REFERENCE_CONVERSION_CONST_LVALUE
;
4884 return INCOMPATIBLE_TYPE_BADNESS
;
4885 return sum_ranks (rank
, REFERENCE_CONVERSION_BADNESS
);
4889 /* It's illegal to pass an lvalue as an rvalue. */
4890 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4891 return INCOMPATIBLE_TYPE_BADNESS
;
4895 if (types_equal (parm
, arg
))
4897 struct type
*t1
= parm
;
4898 struct type
*t2
= arg
;
4900 /* For pointers and references, compare target type. */
4901 if (parm
->is_pointer_or_reference ())
4903 t1
= parm
->target_type ();
4904 t2
= arg
->target_type ();
4907 /* Make sure they are CV equal, too. */
4908 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4909 rank
.subrank
|= CV_CONVERSION_CONST
;
4910 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4911 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4912 if (rank
.subrank
!= 0)
4913 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4914 return EXACT_MATCH_BADNESS
;
4917 /* See through references, since we can almost make non-references
4920 if (TYPE_IS_REFERENCE (arg
))
4921 return (sum_ranks (rank_one_type (parm
, arg
->target_type (), NULL
),
4922 REFERENCE_SEE_THROUGH_BADNESS
));
4923 if (TYPE_IS_REFERENCE (parm
))
4924 return (sum_ranks (rank_one_type (parm
->target_type (), arg
, NULL
),
4925 REFERENCE_SEE_THROUGH_BADNESS
));
4928 /* Debugging only. */
4929 gdb_printf (gdb_stderr
,
4930 "------ Arg is %s [%d], parm is %s [%d]\n",
4931 arg
->name (), arg
->code (),
4932 parm
->name (), parm
->code ());
4935 /* x -> y means arg of type x being supplied for parameter of type y. */
4937 switch (parm
->code ())
4940 return rank_one_type_parm_ptr (parm
, arg
, value
);
4941 case TYPE_CODE_ARRAY
:
4942 return rank_one_type_parm_array (parm
, arg
, value
);
4943 case TYPE_CODE_FUNC
:
4944 return rank_one_type_parm_func (parm
, arg
, value
);
4946 return rank_one_type_parm_int (parm
, arg
, value
);
4947 case TYPE_CODE_ENUM
:
4948 return rank_one_type_parm_enum (parm
, arg
, value
);
4949 case TYPE_CODE_CHAR
:
4950 return rank_one_type_parm_char (parm
, arg
, value
);
4951 case TYPE_CODE_RANGE
:
4952 return rank_one_type_parm_range (parm
, arg
, value
);
4953 case TYPE_CODE_BOOL
:
4954 return rank_one_type_parm_bool (parm
, arg
, value
);
4956 return rank_one_type_parm_float (parm
, arg
, value
);
4957 case TYPE_CODE_COMPLEX
:
4958 return rank_one_type_parm_complex (parm
, arg
, value
);
4959 case TYPE_CODE_STRUCT
:
4960 return rank_one_type_parm_struct (parm
, arg
, value
);
4962 return rank_one_type_parm_set (parm
, arg
, value
);
4964 return INCOMPATIBLE_TYPE_BADNESS
;
4965 } /* switch (arg->code ()) */
4968 /* End of functions for overload resolution. */
4971 /* Note the first arg should be the "this" pointer, we may not want to
4972 include it since we may get into a infinitely recursive
4976 print_args (struct field
*args
, int nargs
, int spaces
)
4982 for (i
= 0; i
< nargs
; i
++)
4985 ("%*s[%d] name '%s'\n", spaces
, "", i
,
4986 args
[i
].name () != NULL
? args
[i
].name () : "<NULL>");
4987 recursive_dump_type (args
[i
].type (), spaces
+ 2);
4993 dump_fn_fieldlists (struct type
*type
, int spaces
)
4999 gdb_printf ("%*sfn_fieldlists %s\n", spaces
, "",
5000 host_address_to_string (TYPE_FN_FIELDLISTS (type
)));
5001 for (method_idx
= 0; method_idx
< TYPE_NFN_FIELDS (type
); method_idx
++)
5003 f
= TYPE_FN_FIELDLIST1 (type
, method_idx
);
5005 ("%*s[%d] name '%s' (%s) length %d\n", spaces
+ 2, "",
5007 TYPE_FN_FIELDLIST_NAME (type
, method_idx
),
5008 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type
, method_idx
)),
5009 TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
));
5010 for (overload_idx
= 0;
5011 overload_idx
< TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
);
5015 ("%*s[%d] physname '%s' (%s)\n",
5016 spaces
+ 4, "", overload_idx
,
5017 TYPE_FN_FIELD_PHYSNAME (f
, overload_idx
),
5018 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f
,
5021 ("%*stype %s\n", spaces
+ 8, "",
5022 host_address_to_string (TYPE_FN_FIELD_TYPE (f
, overload_idx
)));
5024 recursive_dump_type (TYPE_FN_FIELD_TYPE (f
, overload_idx
),
5028 ("%*sargs %s\n", spaces
+ 8, "",
5029 host_address_to_string (TYPE_FN_FIELD_ARGS (f
, overload_idx
)));
5030 print_args (TYPE_FN_FIELD_ARGS (f
, overload_idx
),
5031 TYPE_FN_FIELD_TYPE (f
, overload_idx
)->num_fields (),
5034 ("%*sfcontext %s\n", spaces
+ 8, "",
5035 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f
,
5038 gdb_printf ("%*sis_const %d\n", spaces
+ 8, "",
5039 TYPE_FN_FIELD_CONST (f
, overload_idx
));
5040 gdb_printf ("%*sis_volatile %d\n", spaces
+ 8, "",
5041 TYPE_FN_FIELD_VOLATILE (f
, overload_idx
));
5042 gdb_printf ("%*sis_private %d\n", spaces
+ 8, "",
5043 TYPE_FN_FIELD_PRIVATE (f
, overload_idx
));
5044 gdb_printf ("%*sis_protected %d\n", spaces
+ 8, "",
5045 TYPE_FN_FIELD_PROTECTED (f
, overload_idx
));
5046 gdb_printf ("%*sis_stub %d\n", spaces
+ 8, "",
5047 TYPE_FN_FIELD_STUB (f
, overload_idx
));
5048 gdb_printf ("%*sdefaulted %d\n", spaces
+ 8, "",
5049 TYPE_FN_FIELD_DEFAULTED (f
, overload_idx
));
5050 gdb_printf ("%*sis_deleted %d\n", spaces
+ 8, "",
5051 TYPE_FN_FIELD_DELETED (f
, overload_idx
));
5052 gdb_printf ("%*svoffset %u\n", spaces
+ 8, "",
5053 TYPE_FN_FIELD_VOFFSET (f
, overload_idx
));
5059 print_cplus_stuff (struct type
*type
, int spaces
)
5061 gdb_printf ("%*svptr_fieldno %d\n", spaces
, "",
5062 TYPE_VPTR_FIELDNO (type
));
5063 gdb_printf ("%*svptr_basetype %s\n", spaces
, "",
5064 host_address_to_string (TYPE_VPTR_BASETYPE (type
)));
5065 if (TYPE_VPTR_BASETYPE (type
) != NULL
)
5066 recursive_dump_type (TYPE_VPTR_BASETYPE (type
), spaces
+ 2);
5068 gdb_printf ("%*sn_baseclasses %d\n", spaces
, "",
5069 TYPE_N_BASECLASSES (type
));
5070 gdb_printf ("%*snfn_fields %d\n", spaces
, "",
5071 TYPE_NFN_FIELDS (type
));
5072 if (TYPE_NFN_FIELDS (type
) > 0)
5074 dump_fn_fieldlists (type
, spaces
);
5077 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5078 TYPE_CPLUS_CALLING_CONVENTION (type
));
5081 /* Print the contents of the TYPE's type_specific union, assuming that
5082 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5085 print_gnat_stuff (struct type
*type
, int spaces
)
5087 struct type
*descriptive_type
= TYPE_DESCRIPTIVE_TYPE (type
);
5089 if (descriptive_type
== NULL
)
5090 gdb_printf ("%*sno descriptive type\n", spaces
+ 2, "");
5093 gdb_printf ("%*sdescriptive type\n", spaces
+ 2, "");
5094 recursive_dump_type (descriptive_type
, spaces
+ 4);
5098 /* Print the contents of the TYPE's type_specific union, assuming that
5099 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5102 print_fixed_point_type_info (struct type
*type
, int spaces
)
5104 gdb_printf ("%*sscaling factor: %s\n", spaces
+ 2, "",
5105 type
->fixed_point_scaling_factor ().str ().c_str ());
5108 static struct obstack dont_print_type_obstack
;
5110 /* Print the dynamic_prop PROP. */
5113 dump_dynamic_prop (dynamic_prop
const& prop
)
5115 switch (prop
.kind ())
5118 gdb_printf ("%s", plongest (prop
.const_val ()));
5120 case PROP_UNDEFINED
:
5121 gdb_printf ("(undefined)");
5125 gdb_printf ("(dynamic)");
5128 gdb_assert_not_reached ("unhandled prop kind");
5133 /* Return a string that represents a type code. */
5135 type_code_name (type_code code
)
5139 #define OP(X) case X: return # X;
5140 #include "type-codes.def"
5143 case TYPE_CODE_UNDEF
:
5144 return "TYPE_CODE_UNDEF";
5147 gdb_assert_not_reached ("unhandled type_code");
5151 recursive_dump_type (struct type
*type
, int spaces
)
5156 obstack_begin (&dont_print_type_obstack
, 0);
5158 if (type
->num_fields () > 0
5159 || (HAVE_CPLUS_STRUCT (type
) && TYPE_NFN_FIELDS (type
) > 0))
5161 struct type
**first_dont_print
5162 = (struct type
**) obstack_base (&dont_print_type_obstack
);
5164 int i
= (struct type
**)
5165 obstack_next_free (&dont_print_type_obstack
) - first_dont_print
;
5169 if (type
== first_dont_print
[i
])
5171 gdb_printf ("%*stype node %s", spaces
, "",
5172 host_address_to_string (type
));
5173 gdb_printf (_(" <same as already seen type>\n"));
5178 obstack_ptr_grow (&dont_print_type_obstack
, type
);
5181 gdb_printf ("%*stype node %s\n", spaces
, "",
5182 host_address_to_string (type
));
5183 gdb_printf ("%*sname '%s' (%s)\n", spaces
, "",
5184 type
->name () ? type
->name () : "<NULL>",
5185 host_address_to_string (type
->name ()));
5186 gdb_printf ("%*scode 0x%x ", spaces
, "", type
->code ());
5187 gdb_printf ("(%s)", type_code_name (type
->code ()));
5189 gdb_printf ("%*slength %s\n", spaces
, "",
5190 pulongest (type
->length ()));
5191 if (type
->is_objfile_owned ())
5192 gdb_printf ("%*sobjfile %s\n", spaces
, "",
5193 host_address_to_string (type
->objfile_owner ()));
5195 gdb_printf ("%*sgdbarch %s\n", spaces
, "",
5196 host_address_to_string (type
->arch_owner ()));
5197 gdb_printf ("%*starget_type %s\n", spaces
, "",
5198 host_address_to_string (type
->target_type ()));
5199 if (type
->target_type () != NULL
)
5201 recursive_dump_type (type
->target_type (), spaces
+ 2);
5203 gdb_printf ("%*spointer_type %s\n", spaces
, "",
5204 host_address_to_string (TYPE_POINTER_TYPE (type
)));
5205 gdb_printf ("%*sreference_type %s\n", spaces
, "",
5206 host_address_to_string (TYPE_REFERENCE_TYPE (type
)));
5207 gdb_printf ("%*stype_chain %s\n", spaces
, "",
5208 host_address_to_string (TYPE_CHAIN (type
)));
5209 gdb_printf ("%*sinstance_flags 0x%x", spaces
, "",
5210 (unsigned) type
->instance_flags ());
5211 if (TYPE_CONST (type
))
5213 gdb_puts (" TYPE_CONST");
5215 if (TYPE_VOLATILE (type
))
5217 gdb_puts (" TYPE_VOLATILE");
5219 if (TYPE_CODE_SPACE (type
))
5221 gdb_puts (" TYPE_CODE_SPACE");
5223 if (TYPE_DATA_SPACE (type
))
5225 gdb_puts (" TYPE_DATA_SPACE");
5227 if (TYPE_ADDRESS_CLASS_1 (type
))
5229 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5231 if (TYPE_ADDRESS_CLASS_2 (type
))
5233 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5235 if (TYPE_RESTRICT (type
))
5237 gdb_puts (" TYPE_RESTRICT");
5239 if (TYPE_ATOMIC (type
))
5241 gdb_puts (" TYPE_ATOMIC");
5245 gdb_printf ("%*sflags", spaces
, "");
5246 if (type
->is_unsigned ())
5248 gdb_puts (" TYPE_UNSIGNED");
5250 if (type
->has_no_signedness ())
5252 gdb_puts (" TYPE_NOSIGN");
5254 if (type
->endianity_is_not_default ())
5256 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5258 if (type
->is_stub ())
5260 gdb_puts (" TYPE_STUB");
5262 if (type
->target_is_stub ())
5264 gdb_puts (" TYPE_TARGET_STUB");
5266 if (type
->is_prototyped ())
5268 gdb_puts (" TYPE_PROTOTYPED");
5270 if (type
->has_varargs ())
5272 gdb_puts (" TYPE_VARARGS");
5274 /* This is used for things like AltiVec registers on ppc. Gcc emits
5275 an attribute for the array type, which tells whether or not we
5276 have a vector, instead of a regular array. */
5277 if (type
->is_vector ())
5279 gdb_puts (" TYPE_VECTOR");
5281 if (type
->is_fixed_instance ())
5283 gdb_puts (" TYPE_FIXED_INSTANCE");
5285 if (type
->stub_is_supported ())
5287 gdb_puts (" TYPE_STUB_SUPPORTED");
5289 if (TYPE_NOTTEXT (type
))
5291 gdb_puts (" TYPE_NOTTEXT");
5294 gdb_printf ("%*snfields %d ", spaces
, "", type
->num_fields ());
5295 if (TYPE_ASSOCIATED_PROP (type
) != nullptr
5296 || TYPE_ALLOCATED_PROP (type
) != nullptr)
5298 gdb_printf ("%*s", spaces
, "");
5299 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5301 gdb_printf ("associated ");
5302 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type
));
5304 if (TYPE_ALLOCATED_PROP (type
) != nullptr)
5306 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5308 gdb_printf ("allocated ");
5309 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type
));
5313 gdb_printf ("%s\n", host_address_to_string (type
->fields ()));
5314 for (idx
= 0; idx
< type
->num_fields (); idx
++)
5316 field
&fld
= type
->field (idx
);
5317 if (type
->code () == TYPE_CODE_ENUM
)
5318 gdb_printf ("%*s[%d] enumval %s type ", spaces
+ 2, "",
5319 idx
, plongest (fld
.loc_enumval ()));
5321 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces
+ 2, "",
5322 idx
, plongest (fld
.loc_bitpos ()),
5324 gdb_printf ("%s name '%s' (%s)",
5325 host_address_to_string (fld
.type ()),
5329 host_address_to_string (fld
.name ()));
5330 if (fld
.is_virtual ())
5331 gdb_printf (" virtual");
5333 if (fld
.is_private ())
5334 gdb_printf (" private");
5335 else if (fld
.is_protected ())
5336 gdb_printf (" protected");
5337 else if (fld
.is_ignored ())
5338 gdb_printf (" ignored");
5341 if (fld
.type () != NULL
)
5343 recursive_dump_type (fld
.type (), spaces
+ 4);
5346 if (type
->code () == TYPE_CODE_RANGE
)
5348 gdb_printf ("%*slow ", spaces
, "");
5349 dump_dynamic_prop (type
->bounds ()->low
);
5350 gdb_printf (" high ");
5351 dump_dynamic_prop (type
->bounds ()->high
);
5355 switch (TYPE_SPECIFIC_FIELD (type
))
5357 case TYPE_SPECIFIC_CPLUS_STUFF
:
5358 gdb_printf ("%*scplus_stuff %s\n", spaces
, "",
5359 host_address_to_string (TYPE_CPLUS_SPECIFIC (type
)));
5360 print_cplus_stuff (type
, spaces
);
5363 case TYPE_SPECIFIC_GNAT_STUFF
:
5364 gdb_printf ("%*sgnat_stuff %s\n", spaces
, "",
5365 host_address_to_string (TYPE_GNAT_SPECIFIC (type
)));
5366 print_gnat_stuff (type
, spaces
);
5369 case TYPE_SPECIFIC_FLOATFORMAT
:
5370 gdb_printf ("%*sfloatformat ", spaces
, "");
5371 if (TYPE_FLOATFORMAT (type
) == NULL
5372 || TYPE_FLOATFORMAT (type
)->name
== NULL
)
5373 gdb_puts ("(null)");
5375 gdb_puts (TYPE_FLOATFORMAT (type
)->name
);
5379 case TYPE_SPECIFIC_FUNC
:
5380 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5381 TYPE_CALLING_CONVENTION (type
));
5382 /* tail_call_list is not printed. */
5385 case TYPE_SPECIFIC_SELF_TYPE
:
5386 gdb_printf ("%*sself_type %s\n", spaces
, "",
5387 host_address_to_string (TYPE_SELF_TYPE (type
)));
5390 case TYPE_SPECIFIC_FIXED_POINT
:
5391 gdb_printf ("%*sfixed_point_info ", spaces
, "");
5392 print_fixed_point_type_info (type
, spaces
);
5396 case TYPE_SPECIFIC_INT
:
5397 if (type
->bit_size_differs_p ())
5399 unsigned bit_size
= type
->bit_size ();
5400 unsigned bit_off
= type
->bit_offset ();
5401 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces
, "",
5408 obstack_free (&dont_print_type_obstack
, NULL
);
5411 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5413 static struct dynamic_prop_list
*
5414 copy_dynamic_prop_list (struct obstack
*storage
,
5415 struct dynamic_prop_list
*list
)
5417 struct dynamic_prop_list
*copy
= list
;
5418 struct dynamic_prop_list
**node_ptr
= ©
;
5420 while (*node_ptr
!= NULL
)
5422 struct dynamic_prop_list
*node_copy
;
5424 node_copy
= ((struct dynamic_prop_list
*)
5425 obstack_copy (storage
, *node_ptr
,
5426 sizeof (struct dynamic_prop_list
)));
5427 node_copy
->prop
= (*node_ptr
)->prop
;
5428 *node_ptr
= node_copy
;
5430 node_ptr
= &node_copy
->next
;
5436 /* Recursively copy (deep copy) TYPE, if it is associated with
5437 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5438 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5439 it is not associated with OBJFILE. */
5442 copy_type_recursive (struct type
*type
, copied_types_hash_t
&copied_types
)
5444 if (!type
->is_objfile_owned ())
5447 if (auto iter
= copied_types
.find (type
);
5448 iter
!= copied_types
.end ())
5449 return iter
->second
;
5451 struct type
*new_type
= type_allocator (type
->arch ()).new_type ();
5453 /* We must add the new type to the hash table immediately, in case
5454 we encounter this type again during a recursive call below. */
5455 copied_types
.emplace (type
, new_type
);
5457 /* Copy the common fields of types. For the main type, we simply
5458 copy the entire thing and then update specific fields as needed. */
5459 *TYPE_MAIN_TYPE (new_type
) = *TYPE_MAIN_TYPE (type
);
5461 new_type
->set_owner (type
->arch ());
5464 new_type
->set_name (xstrdup (type
->name ()));
5466 new_type
->set_instance_flags (type
->instance_flags ());
5467 new_type
->set_length (type
->length ());
5469 /* Copy the fields. */
5470 if (type
->num_fields ())
5474 nfields
= type
->num_fields ();
5475 new_type
->alloc_fields (type
->num_fields ());
5477 for (i
= 0; i
< nfields
; i
++)
5479 new_type
->field (i
).set_is_artificial
5480 (type
->field (i
).is_artificial ());
5481 new_type
->field (i
).set_bitsize (type
->field (i
).bitsize ());
5482 if (type
->field (i
).type ())
5483 new_type
->field (i
).set_type
5484 (copy_type_recursive (type
->field (i
).type (), copied_types
));
5485 if (type
->field (i
).name ())
5486 new_type
->field (i
).set_name (xstrdup (type
->field (i
).name ()));
5488 switch (type
->field (i
).loc_kind ())
5490 case FIELD_LOC_KIND_BITPOS
:
5491 new_type
->field (i
).set_loc_bitpos (type
->field (i
).loc_bitpos ());
5493 case FIELD_LOC_KIND_ENUMVAL
:
5494 new_type
->field (i
).set_loc_enumval (type
->field (i
).loc_enumval ());
5496 case FIELD_LOC_KIND_PHYSADDR
:
5497 new_type
->field (i
).set_loc_physaddr
5498 (type
->field (i
).loc_physaddr ());
5500 case FIELD_LOC_KIND_PHYSNAME
:
5501 new_type
->field (i
).set_loc_physname
5502 (xstrdup (type
->field (i
).loc_physname ()));
5504 case FIELD_LOC_KIND_DWARF_BLOCK
:
5505 new_type
->field (i
).set_loc_dwarf_block
5506 (type
->field (i
).loc_dwarf_block ());
5509 internal_error (_("Unexpected type field location kind: %d"),
5510 type
->field (i
).loc_kind ());
5515 /* For range types, copy the bounds information. */
5516 if (type
->code () == TYPE_CODE_RANGE
)
5518 range_bounds
*bounds
5519 = ((struct range_bounds
*) TYPE_ALLOC
5520 (new_type
, sizeof (struct range_bounds
)));
5522 *bounds
= *type
->bounds ();
5523 new_type
->set_bounds (bounds
);
5526 if (type
->main_type
->dyn_prop_list
!= NULL
)
5527 new_type
->main_type
->dyn_prop_list
5528 = copy_dynamic_prop_list (gdbarch_obstack (new_type
->arch_owner ()),
5529 type
->main_type
->dyn_prop_list
);
5532 /* Copy pointers to other types. */
5533 if (type
->target_type ())
5534 new_type
->set_target_type
5535 (copy_type_recursive (type
->target_type (), copied_types
));
5537 /* Maybe copy the type_specific bits.
5539 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5540 base classes and methods. There's no fundamental reason why we
5541 can't, but at the moment it is not needed. */
5543 switch (TYPE_SPECIFIC_FIELD (type
))
5545 case TYPE_SPECIFIC_NONE
:
5547 case TYPE_SPECIFIC_FUNC
:
5548 INIT_FUNC_SPECIFIC (new_type
);
5549 TYPE_CALLING_CONVENTION (new_type
) = TYPE_CALLING_CONVENTION (type
);
5550 TYPE_NO_RETURN (new_type
) = TYPE_NO_RETURN (type
);
5551 TYPE_TAIL_CALL_LIST (new_type
) = NULL
;
5553 case TYPE_SPECIFIC_FLOATFORMAT
:
5554 TYPE_FLOATFORMAT (new_type
) = TYPE_FLOATFORMAT (type
);
5556 case TYPE_SPECIFIC_CPLUS_STUFF
:
5557 INIT_CPLUS_SPECIFIC (new_type
);
5559 case TYPE_SPECIFIC_GNAT_STUFF
:
5560 INIT_GNAT_SPECIFIC (new_type
);
5562 case TYPE_SPECIFIC_SELF_TYPE
:
5563 set_type_self_type (new_type
,
5564 copy_type_recursive (TYPE_SELF_TYPE (type
),
5567 case TYPE_SPECIFIC_FIXED_POINT
:
5568 INIT_FIXED_POINT_SPECIFIC (new_type
);
5569 new_type
->fixed_point_info ().scaling_factor
5570 = type
->fixed_point_info ().scaling_factor
;
5572 case TYPE_SPECIFIC_INT
:
5573 TYPE_SPECIFIC_FIELD (new_type
) = TYPE_SPECIFIC_INT
;
5574 TYPE_MAIN_TYPE (new_type
)->type_specific
.int_stuff
5575 = TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
;
5579 gdb_assert_not_reached ("bad type_specific_kind");
5585 /* Make a copy of the given TYPE, except that the pointer & reference
5586 types are not preserved. */
5589 copy_type (const struct type
*type
)
5591 struct type
*new_type
= type_allocator (type
).new_type ();
5592 new_type
->set_instance_flags (type
->instance_flags ());
5593 new_type
->set_length (type
->length ());
5594 memcpy (TYPE_MAIN_TYPE (new_type
), TYPE_MAIN_TYPE (type
),
5595 sizeof (struct main_type
));
5596 if (type
->main_type
->dyn_prop_list
!= NULL
)
5598 struct obstack
*storage
= (type
->is_objfile_owned ()
5599 ? &type
->objfile_owner ()->objfile_obstack
5600 : gdbarch_obstack (type
->arch_owner ()));
5601 new_type
->main_type
->dyn_prop_list
5602 = copy_dynamic_prop_list (storage
, type
->main_type
->dyn_prop_list
);
5608 /* Helper functions to initialize architecture-specific types. */
5610 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5611 NAME is the type name. BIT is the size of the flag word in bits. */
5614 arch_flags_type (struct gdbarch
*gdbarch
, const char *name
, int bit
)
5618 type
= type_allocator (gdbarch
).new_type (TYPE_CODE_FLAGS
, bit
, name
);
5619 type
->set_is_unsigned (true);
5620 /* Pre-allocate enough space assuming every field is one bit. */
5621 type
->alloc_fields (bit
);
5622 type
->set_num_fields (0);
5627 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5628 position BITPOS is called NAME. Pass NAME as "" for fields that
5629 should not be printed. */
5632 append_flags_type_field (struct type
*type
, int start_bitpos
, int nr_bits
,
5633 struct type
*field_type
, const char *name
)
5635 int type_bitsize
= type
->length () * TARGET_CHAR_BIT
;
5636 int field_nr
= type
->num_fields ();
5638 gdb_assert (type
->code () == TYPE_CODE_FLAGS
);
5639 gdb_assert (type
->num_fields () + 1 <= type_bitsize
);
5640 gdb_assert (start_bitpos
>= 0 && start_bitpos
< type_bitsize
);
5641 gdb_assert (nr_bits
>= 1 && (start_bitpos
+ nr_bits
) <= type_bitsize
);
5642 gdb_assert (name
!= NULL
);
5644 type
->set_num_fields (type
->num_fields () + 1);
5645 type
->field (field_nr
).set_name (xstrdup (name
));
5646 type
->field (field_nr
).set_type (field_type
);
5647 type
->field (field_nr
).set_loc_bitpos (start_bitpos
);
5648 type
->field (field_nr
).set_bitsize (nr_bits
);
5651 /* Special version of append_flags_type_field to add a flag field.
5652 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5653 position BITPOS is called NAME. */
5656 append_flags_type_flag (struct type
*type
, int bitpos
, const char *name
)
5658 append_flags_type_field (type
, bitpos
, 1,
5659 builtin_type (type
->arch ())->builtin_bool
,
5663 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5664 specified by CODE) associated with GDBARCH. NAME is the type name. */
5667 arch_composite_type (struct gdbarch
*gdbarch
, const char *name
,
5668 enum type_code code
)
5672 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
);
5673 t
= type_allocator (gdbarch
).new_type (code
, 0, NULL
);
5675 INIT_CPLUS_SPECIFIC (t
);
5679 /* Add new field with name NAME and type FIELD to composite type T.
5680 Do not set the field's position or adjust the type's length;
5681 the caller should do so. Return the new field. */
5684 append_composite_type_field_raw (struct type
*t
, const char *name
,
5689 t
->set_num_fields (t
->num_fields () + 1);
5690 t
->set_fields (XRESIZEVEC (struct field
, t
->fields (),
5692 f
= &t
->field (t
->num_fields () - 1);
5693 memset (f
, 0, sizeof f
[0]);
5694 f
[0].set_type (field
);
5695 f
[0].set_name (name
);
5699 /* Add new field with name NAME and type FIELD to composite type T.
5700 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5703 append_composite_type_field_aligned (struct type
*t
, const char *name
,
5704 struct type
*field
, int alignment
)
5706 struct field
*f
= append_composite_type_field_raw (t
, name
, field
);
5708 if (t
->code () == TYPE_CODE_UNION
)
5710 if (t
->length () < field
->length ())
5711 t
->set_length (field
->length ());
5713 else if (t
->code () == TYPE_CODE_STRUCT
)
5715 t
->set_length (t
->length () + field
->length ());
5716 if (t
->num_fields () > 1)
5719 (f
[-1].loc_bitpos ()
5720 + (f
[-1].type ()->length () * TARGET_CHAR_BIT
));
5726 alignment
*= TARGET_CHAR_BIT
;
5727 left
= f
[0].loc_bitpos () % alignment
;
5731 f
->set_loc_bitpos (f
[0].loc_bitpos () + (alignment
- left
));
5733 (t
->length () + (alignment
- left
) / TARGET_CHAR_BIT
);
5740 /* Add new field with name NAME and type FIELD to composite type T. */
5743 append_composite_type_field (struct type
*t
, const char *name
,
5746 append_composite_type_field_aligned (t
, name
, field
, 0);
5751 /* We manage the lifetimes of fixed_point_type_info objects by
5752 attaching them to the objfile. Currently, these objects are
5753 modified during construction, and GMP does not provide a way to
5754 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5755 them. If we did do this, they could be moved to the per-BFD and
5756 shared across objfiles. */
5757 typedef std::vector
<std::unique_ptr
<fixed_point_type_info
>>
5758 fixed_point_type_storage
;
5760 /* Key used for managing the storage of fixed-point type info. */
5761 static const struct registry
<objfile
>::key
<fixed_point_type_storage
>
5762 fixed_point_objfile_key
;
5764 /* See gdbtypes.h. */
5767 allocate_fixed_point_type_info (struct type
*type
)
5769 auto up
= std::make_unique
<fixed_point_type_info
> ();
5770 fixed_point_type_info
*info
;
5772 if (type
->is_objfile_owned ())
5774 fixed_point_type_storage
*storage
5775 = fixed_point_objfile_key
.get (type
->objfile_owner ());
5776 if (storage
== nullptr)
5777 storage
= fixed_point_objfile_key
.emplace (type
->objfile_owner ());
5779 storage
->push_back (std::move (up
));
5783 /* We just leak the memory, because that's what we do generally
5784 for non-objfile-attached types. */
5785 info
= up
.release ();
5788 type
->set_fixed_point_info (info
);
5791 /* See gdbtypes.h. */
5794 is_fixed_point_type (struct type
*type
)
5796 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
5797 type
= check_typedef (type
)->target_type ();
5798 type
= check_typedef (type
);
5800 return type
->code () == TYPE_CODE_FIXED_POINT
;
5803 /* See gdbtypes.h. */
5806 type::fixed_point_type_base_type ()
5808 struct type
*type
= this;
5810 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
5811 type
= check_typedef (type
)->target_type ();
5812 type
= check_typedef (type
);
5814 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
);
5818 /* See gdbtypes.h. */
5821 type::fixed_point_scaling_factor ()
5823 struct type
*type
= this->fixed_point_type_base_type ();
5825 return type
->fixed_point_info ().scaling_factor
;
5828 /* See gdbtypes.h. */
5831 type::alloc_fields (unsigned int nfields
, bool init
)
5833 this->set_num_fields (nfields
);
5837 this->main_type
->flds_bnds
.fields
= nullptr;
5841 size_t size
= nfields
* sizeof (*this->fields ());
5842 struct field
*fields
5843 = (struct field
*) (init
5844 ? TYPE_ZALLOC (this, size
)
5845 : TYPE_ALLOC (this, size
));
5847 this->main_type
->flds_bnds
.fields
= fields
;
5850 /* See gdbtypes.h. */
5853 type::copy_fields (struct type
*src
)
5855 unsigned int nfields
= src
->num_fields ();
5856 alloc_fields (nfields
, false);
5860 size_t size
= nfields
* sizeof (*this->fields ());
5861 memcpy (this->fields (), src
->fields (), size
);
5864 /* See gdbtypes.h. */
5867 type::copy_fields (std::vector
<struct field
> &src
)
5869 unsigned int nfields
= src
.size ();
5870 alloc_fields (nfields
, false);
5874 size_t size
= nfields
* sizeof (*this->fields ());
5875 memcpy (this->fields (), src
.data (), size
);
5878 /* See gdbtypes.h. */
5881 type::is_string_like ()
5883 const language_defn
*defn
= language_def (this->language ());
5884 return defn
->is_string_type_p (this);
5887 /* See gdbtypes.h. */
5890 type::is_array_like ()
5892 if (code () == TYPE_CODE_ARRAY
)
5894 const language_defn
*defn
= language_def (this->language ());
5895 return defn
->is_array_like (this);
5900 static const registry
<gdbarch
>::key
<struct builtin_type
> gdbtypes_data
;
5902 static struct builtin_type
*
5903 create_gdbtypes_data (struct gdbarch
*gdbarch
)
5905 struct builtin_type
*builtin_type
= new struct builtin_type
;
5907 type_allocator
alloc (gdbarch
);
5910 builtin_type
->builtin_void
5911 = alloc
.new_type (TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
5912 builtin_type
->builtin_char
5913 = init_integer_type (alloc
, TARGET_CHAR_BIT
,
5914 !gdbarch_char_signed (gdbarch
), "char");
5915 builtin_type
->builtin_char
->set_has_no_signedness (true);
5916 builtin_type
->builtin_signed_char
5917 = init_integer_type (alloc
, TARGET_CHAR_BIT
,
5919 builtin_type
->builtin_unsigned_char
5920 = init_integer_type (alloc
, TARGET_CHAR_BIT
,
5921 1, "unsigned char");
5922 builtin_type
->builtin_short
5923 = init_integer_type (alloc
, gdbarch_short_bit (gdbarch
),
5925 builtin_type
->builtin_unsigned_short
5926 = init_integer_type (alloc
, gdbarch_short_bit (gdbarch
),
5927 1, "unsigned short");
5928 builtin_type
->builtin_int
5929 = init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
5931 builtin_type
->builtin_unsigned_int
5932 = init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
5934 builtin_type
->builtin_long
5935 = init_integer_type (alloc
, gdbarch_long_bit (gdbarch
),
5937 builtin_type
->builtin_unsigned_long
5938 = init_integer_type (alloc
, gdbarch_long_bit (gdbarch
),
5939 1, "unsigned long");
5940 builtin_type
->builtin_long_long
5941 = init_integer_type (alloc
, gdbarch_long_long_bit (gdbarch
),
5943 builtin_type
->builtin_unsigned_long_long
5944 = init_integer_type (alloc
, gdbarch_long_long_bit (gdbarch
),
5945 1, "unsigned long long");
5946 builtin_type
->builtin_half
5947 = init_float_type (alloc
, gdbarch_half_bit (gdbarch
),
5948 "half", gdbarch_half_format (gdbarch
));
5949 builtin_type
->builtin_float
5950 = init_float_type (alloc
, gdbarch_float_bit (gdbarch
),
5951 "float", gdbarch_float_format (gdbarch
));
5952 builtin_type
->builtin_bfloat16
5953 = init_float_type (alloc
, gdbarch_bfloat16_bit (gdbarch
),
5954 "bfloat16", gdbarch_bfloat16_format (gdbarch
));
5955 builtin_type
->builtin_double
5956 = init_float_type (alloc
, gdbarch_double_bit (gdbarch
),
5957 "double", gdbarch_double_format (gdbarch
));
5958 builtin_type
->builtin_long_double
5959 = init_float_type (alloc
, gdbarch_long_double_bit (gdbarch
),
5960 "long double", gdbarch_long_double_format (gdbarch
));
5961 builtin_type
->builtin_complex
5962 = init_complex_type ("complex", builtin_type
->builtin_float
);
5963 builtin_type
->builtin_double_complex
5964 = init_complex_type ("double complex", builtin_type
->builtin_double
);
5965 builtin_type
->builtin_string
5966 = alloc
.new_type (TYPE_CODE_STRING
, TARGET_CHAR_BIT
, "string");
5967 builtin_type
->builtin_bool
5968 = init_boolean_type (alloc
, TARGET_CHAR_BIT
, 1, "bool");
5970 /* The following three are about decimal floating point types, which
5971 are 32-bits, 64-bits and 128-bits respectively. */
5972 builtin_type
->builtin_decfloat
5973 = init_decfloat_type (alloc
, 32, "_Decimal32");
5974 builtin_type
->builtin_decdouble
5975 = init_decfloat_type (alloc
, 64, "_Decimal64");
5976 builtin_type
->builtin_declong
5977 = init_decfloat_type (alloc
, 128, "_Decimal128");
5979 /* "True" character types. */
5980 builtin_type
->builtin_true_char
5981 = init_character_type (alloc
, TARGET_CHAR_BIT
, 0, "true character");
5982 builtin_type
->builtin_true_unsigned_char
5983 = init_character_type (alloc
, TARGET_CHAR_BIT
, 1, "true character");
5985 /* Fixed-size integer types. */
5986 builtin_type
->builtin_int0
5987 = init_integer_type (alloc
, 0, 0, "int0_t");
5988 builtin_type
->builtin_int8
5989 = init_integer_type (alloc
, 8, 0, "int8_t");
5990 builtin_type
->builtin_uint8
5991 = init_integer_type (alloc
, 8, 1, "uint8_t");
5992 builtin_type
->builtin_int16
5993 = init_integer_type (alloc
, 16, 0, "int16_t");
5994 builtin_type
->builtin_uint16
5995 = init_integer_type (alloc
, 16, 1, "uint16_t");
5996 builtin_type
->builtin_int24
5997 = init_integer_type (alloc
, 24, 0, "int24_t");
5998 builtin_type
->builtin_uint24
5999 = init_integer_type (alloc
, 24, 1, "uint24_t");
6000 builtin_type
->builtin_int32
6001 = init_integer_type (alloc
, 32, 0, "int32_t");
6002 builtin_type
->builtin_uint32
6003 = init_integer_type (alloc
, 32, 1, "uint32_t");
6004 builtin_type
->builtin_int64
6005 = init_integer_type (alloc
, 64, 0, "int64_t");
6006 builtin_type
->builtin_uint64
6007 = init_integer_type (alloc
, 64, 1, "uint64_t");
6008 builtin_type
->builtin_int128
6009 = init_integer_type (alloc
, 128, 0, "int128_t");
6010 builtin_type
->builtin_uint128
6011 = init_integer_type (alloc
, 128, 1, "uint128_t");
6013 builtin_type
->builtin_int8
->set_instance_flags
6014 (builtin_type
->builtin_int8
->instance_flags ()
6015 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6017 builtin_type
->builtin_uint8
->set_instance_flags
6018 (builtin_type
->builtin_uint8
->instance_flags ()
6019 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6021 /* Wide character types. */
6022 builtin_type
->builtin_char16
6023 = init_integer_type (alloc
, 16, 1, "char16_t");
6024 builtin_type
->builtin_char32
6025 = init_integer_type (alloc
, 32, 1, "char32_t");
6026 builtin_type
->builtin_wchar
6027 = init_integer_type (alloc
, gdbarch_wchar_bit (gdbarch
),
6028 !gdbarch_wchar_signed (gdbarch
), "wchar_t");
6030 /* Default data/code pointer types. */
6031 builtin_type
->builtin_data_ptr
6032 = lookup_pointer_type (builtin_type
->builtin_void
);
6033 builtin_type
->builtin_func_ptr
6034 = lookup_pointer_type (lookup_function_type (builtin_type
->builtin_void
));
6035 builtin_type
->builtin_func_func
6036 = lookup_function_type (builtin_type
->builtin_func_ptr
);
6038 /* This type represents a GDB internal function. */
6039 builtin_type
->internal_fn
6040 = alloc
.new_type (TYPE_CODE_INTERNAL_FUNCTION
, 0,
6041 "<internal function>");
6043 /* This type represents an xmethod. */
6044 builtin_type
->xmethod
6045 = alloc
.new_type (TYPE_CODE_XMETHOD
, 0, "<xmethod>");
6047 /* This type represents a type that was unrecognized in symbol read-in. */
6048 builtin_type
->builtin_error
6049 = alloc
.new_type (TYPE_CODE_ERROR
, 0, "<unknown type>");
6051 /* The following set of types is used for symbols with no
6052 debug information. */
6053 builtin_type
->nodebug_text_symbol
6054 = alloc
.new_type (TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6055 "<text variable, no debug info>");
6057 builtin_type
->nodebug_text_gnu_ifunc_symbol
6058 = alloc
.new_type (TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6059 "<text gnu-indirect-function variable, no debug info>");
6060 builtin_type
->nodebug_text_gnu_ifunc_symbol
->set_is_gnu_ifunc (true);
6062 builtin_type
->nodebug_got_plt_symbol
6063 = init_pointer_type (alloc
, gdbarch_addr_bit (gdbarch
),
6064 "<text from jump slot in .got.plt, no debug info>",
6065 builtin_type
->nodebug_text_symbol
);
6066 builtin_type
->nodebug_data_symbol
6067 = alloc
.new_type (TYPE_CODE_ERROR
, 0, "<data variable, no debug info>");
6068 builtin_type
->nodebug_unknown_symbol
6069 = alloc
.new_type (TYPE_CODE_ERROR
, 0,
6070 "<variable (not text or data), no debug info>");
6071 builtin_type
->nodebug_tls_symbol
6072 = alloc
.new_type (TYPE_CODE_ERROR
, 0,
6073 "<thread local variable, no debug info>");
6075 /* NOTE: on some targets, addresses and pointers are not necessarily
6079 - gdb's `struct type' always describes the target's
6081 - gdb's `struct value' objects should always hold values in
6083 - gdb's CORE_ADDR values are addresses in the unified virtual
6084 address space that the assembler and linker work with. Thus,
6085 since target_read_memory takes a CORE_ADDR as an argument, it
6086 can access any memory on the target, even if the processor has
6087 separate code and data address spaces.
6089 In this context, builtin_type->builtin_core_addr is a bit odd:
6090 it's a target type for a value the target will never see. It's
6091 only used to hold the values of (typeless) linker symbols, which
6092 are indeed in the unified virtual address space. */
6094 builtin_type
->builtin_core_addr
6095 = init_integer_type (alloc
, gdbarch_addr_bit (gdbarch
), 1,
6097 return builtin_type
;
6100 const struct builtin_type
*
6101 builtin_type (struct gdbarch
*gdbarch
)
6103 struct builtin_type
*result
= gdbtypes_data
.get (gdbarch
);
6104 if (result
== nullptr)
6106 result
= create_gdbtypes_data (gdbarch
);
6107 gdbtypes_data
.set (gdbarch
, result
);
6112 const struct builtin_type
*
6113 builtin_type (struct objfile
*objfile
)
6115 return builtin_type (objfile
->arch ());
6118 /* See gdbtypes.h. */
6121 call_site::pc () const
6123 return per_objfile
->relocate (m_unrelocated_pc
);
6126 void _initialize_gdbtypes ();
6128 _initialize_gdbtypes ()
6130 add_setshow_zuinteger_cmd ("overload", no_class
, &overload_debug
,
6131 _("Set debugging of C++ overloading."),
6132 _("Show debugging of C++ overloading."),
6133 _("When enabled, ranking of the "
6134 "functions is displayed."),
6136 show_overload_debug
,
6137 &setdebuglist
, &showdebuglist
);
6139 /* Add user knob for controlling resolution of opaque types. */
6140 add_setshow_boolean_cmd ("opaque-type-resolution", class_support
,
6141 &opaque_type_resolution
,
6143 Set resolution of opaque struct/class/union types."),
6145 Show resolution of opaque struct/class/union types."),
6147 show_opaque_type_resolution
,
6148 &setlist
, &showlist
);
6150 /* Add an option to permit non-strict type checking. */
6151 add_setshow_boolean_cmd ("type", class_support
,
6152 &strict_type_checking
,
6153 _("Set strict type checking."),
6154 _("Show strict type checking."),
6156 show_strict_type_checking
,
6157 &setchecklist
, &showchecklist
);