1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2022 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/>. */
28 #include "expression.h"
33 #include "complaints.h"
37 #include "cp-support.h"
39 #include "dwarf2/loc.h"
40 #include "dwarf2/read.h"
42 #include "floatformat.h"
45 #include "gmp-utils.h"
47 /* The value of an invalid conversion badness. */
48 #define INVALID_CONVERSION 100
50 /* Initialize BADNESS constants. */
52 const struct rank LENGTH_MISMATCH_BADNESS
= {INVALID_CONVERSION
,0};
54 const struct rank TOO_FEW_PARAMS_BADNESS
= {INVALID_CONVERSION
,0};
55 const struct rank INCOMPATIBLE_TYPE_BADNESS
= {INVALID_CONVERSION
,0};
57 const struct rank EXACT_MATCH_BADNESS
= {0,0};
59 const struct rank INTEGER_PROMOTION_BADNESS
= {1,0};
60 const struct rank FLOAT_PROMOTION_BADNESS
= {1,0};
61 const struct rank BASE_PTR_CONVERSION_BADNESS
= {1,0};
62 const struct rank CV_CONVERSION_BADNESS
= {1, 0};
63 const struct rank INTEGER_CONVERSION_BADNESS
= {2,0};
64 const struct rank FLOAT_CONVERSION_BADNESS
= {2,0};
65 const struct rank INT_FLOAT_CONVERSION_BADNESS
= {2,0};
66 const struct rank VOID_PTR_CONVERSION_BADNESS
= {2,0};
67 const struct rank BOOL_CONVERSION_BADNESS
= {3,0};
68 const struct rank BASE_CONVERSION_BADNESS
= {2,0};
69 const struct rank REFERENCE_CONVERSION_BADNESS
= {2,0};
70 const struct rank REFERENCE_SEE_THROUGH_BADNESS
= {0,1};
71 const struct rank NULL_POINTER_CONVERSION_BADNESS
= {2,0};
72 const struct rank NS_POINTER_CONVERSION_BADNESS
= {10,0};
73 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS
= {3,0};
75 /* Floatformat pairs. */
76 const struct floatformat
*floatformats_ieee_half
[BFD_ENDIAN_UNKNOWN
] = {
77 &floatformat_ieee_half_big
,
78 &floatformat_ieee_half_little
80 const struct floatformat
*floatformats_ieee_single
[BFD_ENDIAN_UNKNOWN
] = {
81 &floatformat_ieee_single_big
,
82 &floatformat_ieee_single_little
84 const struct floatformat
*floatformats_ieee_double
[BFD_ENDIAN_UNKNOWN
] = {
85 &floatformat_ieee_double_big
,
86 &floatformat_ieee_double_little
88 const struct floatformat
*floatformats_ieee_quad
[BFD_ENDIAN_UNKNOWN
] = {
89 &floatformat_ieee_quad_big
,
90 &floatformat_ieee_quad_little
92 const struct floatformat
*floatformats_ieee_double_littlebyte_bigword
[BFD_ENDIAN_UNKNOWN
] = {
93 &floatformat_ieee_double_big
,
94 &floatformat_ieee_double_littlebyte_bigword
96 const struct floatformat
*floatformats_i387_ext
[BFD_ENDIAN_UNKNOWN
] = {
97 &floatformat_i387_ext
,
100 const struct floatformat
*floatformats_m68881_ext
[BFD_ENDIAN_UNKNOWN
] = {
101 &floatformat_m68881_ext
,
102 &floatformat_m68881_ext
104 const struct floatformat
*floatformats_arm_ext
[BFD_ENDIAN_UNKNOWN
] = {
105 &floatformat_arm_ext_big
,
106 &floatformat_arm_ext_littlebyte_bigword
108 const struct floatformat
*floatformats_ia64_spill
[BFD_ENDIAN_UNKNOWN
] = {
109 &floatformat_ia64_spill_big
,
110 &floatformat_ia64_spill_little
112 const struct floatformat
*floatformats_vax_f
[BFD_ENDIAN_UNKNOWN
] = {
116 const struct floatformat
*floatformats_vax_d
[BFD_ENDIAN_UNKNOWN
] = {
120 const struct floatformat
*floatformats_ibm_long_double
[BFD_ENDIAN_UNKNOWN
] = {
121 &floatformat_ibm_long_double_big
,
122 &floatformat_ibm_long_double_little
124 const struct floatformat
*floatformats_bfloat16
[BFD_ENDIAN_UNKNOWN
] = {
125 &floatformat_bfloat16_big
,
126 &floatformat_bfloat16_little
129 /* Should opaque types be resolved? */
131 static bool opaque_type_resolution
= true;
133 /* See gdbtypes.h. */
135 unsigned int overload_debug
= 0;
137 /* A flag to enable strict type checking. */
139 static bool strict_type_checking
= true;
141 /* A function to show whether opaque types are resolved. */
144 show_opaque_type_resolution (struct ui_file
*file
, int from_tty
,
145 struct cmd_list_element
*c
,
148 gdb_printf (file
, _("Resolution of opaque struct/class/union types "
149 "(if set before loading symbols) is %s.\n"),
153 /* A function to show whether C++ overload debugging is enabled. */
156 show_overload_debug (struct ui_file
*file
, int from_tty
,
157 struct cmd_list_element
*c
, const char *value
)
159 gdb_printf (file
, _("Debugging of C++ overloading is %s.\n"),
163 /* A function to show the status of strict type checking. */
166 show_strict_type_checking (struct ui_file
*file
, int from_tty
,
167 struct cmd_list_element
*c
, const char *value
)
169 gdb_printf (file
, _("Strict type checking is %s.\n"), value
);
173 /* Allocate a new OBJFILE-associated type structure and fill it
174 with some defaults. Space for the type structure is allocated
175 on the objfile's objfile_obstack. */
178 alloc_type (struct objfile
*objfile
)
182 gdb_assert (objfile
!= NULL
);
184 /* Alloc the structure and start off with all fields zeroed. */
185 type
= OBSTACK_ZALLOC (&objfile
->objfile_obstack
, struct type
);
186 TYPE_MAIN_TYPE (type
) = OBSTACK_ZALLOC (&objfile
->objfile_obstack
,
188 OBJSTAT (objfile
, n_types
++);
190 type
->set_owner (objfile
);
192 /* Initialize the fields that might not be zero. */
194 type
->set_code (TYPE_CODE_UNDEF
);
195 TYPE_CHAIN (type
) = type
; /* Chain back to itself. */
200 /* Allocate a new GDBARCH-associated type structure and fill it
201 with some defaults. Space for the type structure is allocated
202 on the obstack associated with GDBARCH. */
205 alloc_type_arch (struct gdbarch
*gdbarch
)
209 gdb_assert (gdbarch
!= NULL
);
211 /* Alloc the structure and start off with all fields zeroed. */
213 type
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct type
);
214 TYPE_MAIN_TYPE (type
) = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct main_type
);
216 type
->set_owner (gdbarch
);
218 /* Initialize the fields that might not be zero. */
220 type
->set_code (TYPE_CODE_UNDEF
);
221 TYPE_CHAIN (type
) = type
; /* Chain back to itself. */
226 /* If TYPE is objfile-associated, allocate a new type structure
227 associated with the same objfile. If TYPE is gdbarch-associated,
228 allocate a new type structure associated with the same gdbarch. */
231 alloc_type_copy (const struct type
*type
)
233 if (type
->is_objfile_owned ())
234 return alloc_type (type
->objfile_owner ());
236 return alloc_type_arch (type
->arch_owner ());
239 /* See gdbtypes.h. */
244 struct gdbarch
*arch
;
246 if (this->is_objfile_owned ())
247 arch
= this->objfile_owner ()->arch ();
249 arch
= this->arch_owner ();
251 /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
252 a gdbarch, however, this is very rare, and even then, in most cases
253 that type::arch is called, we assume that a non-NULL value is
255 gdb_assert (arch
!= nullptr);
259 /* See gdbtypes.h. */
262 get_target_type (struct type
*type
)
266 type
= TYPE_TARGET_TYPE (type
);
268 type
= check_typedef (type
);
274 /* See gdbtypes.h. */
277 type_length_units (struct type
*type
)
279 int unit_size
= gdbarch_addressable_memory_unit_size (type
->arch ());
281 return TYPE_LENGTH (type
) / unit_size
;
284 /* Alloc a new type instance structure, fill it with some defaults,
285 and point it at OLDTYPE. Allocate the new type instance from the
286 same place as OLDTYPE. */
289 alloc_type_instance (struct type
*oldtype
)
293 /* Allocate the structure. */
295 if (!oldtype
->is_objfile_owned ())
296 type
= GDBARCH_OBSTACK_ZALLOC (oldtype
->arch_owner (), struct type
);
298 type
= OBSTACK_ZALLOC (&oldtype
->objfile_owner ()->objfile_obstack
,
301 TYPE_MAIN_TYPE (type
) = TYPE_MAIN_TYPE (oldtype
);
303 TYPE_CHAIN (type
) = type
; /* Chain back to itself for now. */
308 /* Clear all remnants of the previous type at TYPE, in preparation for
309 replacing it with something else. Preserve owner information. */
312 smash_type (struct type
*type
)
314 bool objfile_owned
= type
->is_objfile_owned ();
315 objfile
*objfile
= type
->objfile_owner ();
316 gdbarch
*arch
= type
->arch_owner ();
318 memset (TYPE_MAIN_TYPE (type
), 0, sizeof (struct main_type
));
320 /* Restore owner information. */
322 type
->set_owner (objfile
);
324 type
->set_owner (arch
);
326 /* For now, delete the rings. */
327 TYPE_CHAIN (type
) = type
;
329 /* For now, leave the pointer/reference types alone. */
332 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
333 to a pointer to memory where the pointer type should be stored.
334 If *TYPEPTR is zero, update it to point to the pointer type we return.
335 We allocate new memory if needed. */
338 make_pointer_type (struct type
*type
, struct type
**typeptr
)
340 struct type
*ntype
; /* New type */
343 ntype
= TYPE_POINTER_TYPE (type
);
348 return ntype
; /* Don't care about alloc,
349 and have new type. */
350 else if (*typeptr
== 0)
352 *typeptr
= ntype
; /* Tracking alloc, and have new type. */
357 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
359 ntype
= alloc_type_copy (type
);
363 else /* We have storage, but need to reset it. */
366 chain
= TYPE_CHAIN (ntype
);
368 TYPE_CHAIN (ntype
) = chain
;
371 TYPE_TARGET_TYPE (ntype
) = type
;
372 TYPE_POINTER_TYPE (type
) = ntype
;
374 /* FIXME! Assumes the machine has only one representation for pointers! */
376 TYPE_LENGTH (ntype
) = gdbarch_ptr_bit (type
->arch ()) / TARGET_CHAR_BIT
;
377 ntype
->set_code (TYPE_CODE_PTR
);
379 /* Mark pointers as unsigned. The target converts between pointers
380 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
381 gdbarch_address_to_pointer. */
382 ntype
->set_is_unsigned (true);
384 /* Update the length of all the other variants of this type. */
385 chain
= TYPE_CHAIN (ntype
);
386 while (chain
!= ntype
)
388 TYPE_LENGTH (chain
) = TYPE_LENGTH (ntype
);
389 chain
= TYPE_CHAIN (chain
);
395 /* Given a type TYPE, return a type of pointers to that type.
396 May need to construct such a type if this is the first use. */
399 lookup_pointer_type (struct type
*type
)
401 return make_pointer_type (type
, (struct type
**) 0);
404 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
405 points to a pointer to memory where the reference type should be
406 stored. If *TYPEPTR is zero, update it to point to the reference
407 type we return. We allocate new memory if needed. REFCODE denotes
408 the kind of reference type to lookup (lvalue or rvalue reference). */
411 make_reference_type (struct type
*type
, struct type
**typeptr
,
412 enum type_code refcode
)
414 struct type
*ntype
; /* New type */
415 struct type
**reftype
;
418 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
420 ntype
= (refcode
== TYPE_CODE_REF
? TYPE_REFERENCE_TYPE (type
)
421 : TYPE_RVALUE_REFERENCE_TYPE (type
));
426 return ntype
; /* Don't care about alloc,
427 and have new type. */
428 else if (*typeptr
== 0)
430 *typeptr
= ntype
; /* Tracking alloc, and have new type. */
435 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
437 ntype
= alloc_type_copy (type
);
441 else /* We have storage, but need to reset it. */
444 chain
= TYPE_CHAIN (ntype
);
446 TYPE_CHAIN (ntype
) = chain
;
449 TYPE_TARGET_TYPE (ntype
) = type
;
450 reftype
= (refcode
== TYPE_CODE_REF
? &TYPE_REFERENCE_TYPE (type
)
451 : &TYPE_RVALUE_REFERENCE_TYPE (type
));
455 /* FIXME! Assume the machine has only one representation for
456 references, and that it matches the (only) representation for
459 TYPE_LENGTH (ntype
) = gdbarch_ptr_bit (type
->arch ()) / TARGET_CHAR_BIT
;
460 ntype
->set_code (refcode
);
464 /* Update the length of all the other variants of this type. */
465 chain
= TYPE_CHAIN (ntype
);
466 while (chain
!= ntype
)
468 TYPE_LENGTH (chain
) = TYPE_LENGTH (ntype
);
469 chain
= TYPE_CHAIN (chain
);
475 /* Same as above, but caller doesn't care about memory allocation
479 lookup_reference_type (struct type
*type
, enum type_code refcode
)
481 return make_reference_type (type
, (struct type
**) 0, refcode
);
484 /* Lookup the lvalue reference type for the type TYPE. */
487 lookup_lvalue_reference_type (struct type
*type
)
489 return lookup_reference_type (type
, TYPE_CODE_REF
);
492 /* Lookup the rvalue reference type for the type TYPE. */
495 lookup_rvalue_reference_type (struct type
*type
)
497 return lookup_reference_type (type
, TYPE_CODE_RVALUE_REF
);
500 /* Lookup a function type that returns type TYPE. TYPEPTR, if
501 nonzero, points to a pointer to memory where the function type
502 should be stored. If *TYPEPTR is zero, update it to point to the
503 function type we return. We allocate new memory if needed. */
506 make_function_type (struct type
*type
, struct type
**typeptr
)
508 struct type
*ntype
; /* New type */
510 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
512 ntype
= alloc_type_copy (type
);
516 else /* We have storage, but need to reset it. */
522 TYPE_TARGET_TYPE (ntype
) = type
;
524 TYPE_LENGTH (ntype
) = 1;
525 ntype
->set_code (TYPE_CODE_FUNC
);
527 INIT_FUNC_SPECIFIC (ntype
);
532 /* Given a type TYPE, return a type of functions that return that type.
533 May need to construct such a type if this is the first use. */
536 lookup_function_type (struct type
*type
)
538 return make_function_type (type
, (struct type
**) 0);
541 /* Given a type TYPE and argument types, return the appropriate
542 function type. If the final type in PARAM_TYPES is NULL, make a
546 lookup_function_type_with_arguments (struct type
*type
,
548 struct type
**param_types
)
550 struct type
*fn
= make_function_type (type
, (struct type
**) 0);
555 if (param_types
[nparams
- 1] == NULL
)
558 fn
->set_has_varargs (true);
560 else if (check_typedef (param_types
[nparams
- 1])->code ()
564 /* Caller should have ensured this. */
565 gdb_assert (nparams
== 0);
566 fn
->set_is_prototyped (true);
569 fn
->set_is_prototyped (true);
572 fn
->set_num_fields (nparams
);
574 ((struct field
*) TYPE_ZALLOC (fn
, nparams
* sizeof (struct field
)));
575 for (i
= 0; i
< nparams
; ++i
)
576 fn
->field (i
).set_type (param_types
[i
]);
581 /* Identify address space identifier by name -- return a
582 type_instance_flags. */
585 address_space_name_to_type_instance_flags (struct gdbarch
*gdbarch
,
586 const char *space_identifier
)
588 type_instance_flags type_flags
;
590 /* Check for known address space delimiters. */
591 if (!strcmp (space_identifier
, "code"))
592 return TYPE_INSTANCE_FLAG_CODE_SPACE
;
593 else if (!strcmp (space_identifier
, "data"))
594 return TYPE_INSTANCE_FLAG_DATA_SPACE
;
595 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch
)
596 && gdbarch_address_class_name_to_type_flags (gdbarch
,
601 error (_("Unknown address space specifier: \"%s\""), space_identifier
);
604 /* Identify address space identifier by type_instance_flags and return
605 the string version of the adress space name. */
608 address_space_type_instance_flags_to_name (struct gdbarch
*gdbarch
,
609 type_instance_flags space_flag
)
611 if (space_flag
& TYPE_INSTANCE_FLAG_CODE_SPACE
)
613 else if (space_flag
& TYPE_INSTANCE_FLAG_DATA_SPACE
)
615 else if ((space_flag
& TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
616 && gdbarch_address_class_type_flags_to_name_p (gdbarch
))
617 return gdbarch_address_class_type_flags_to_name (gdbarch
, space_flag
);
622 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
624 If STORAGE is non-NULL, create the new type instance there.
625 STORAGE must be in the same obstack as TYPE. */
628 make_qualified_type (struct type
*type
, type_instance_flags new_flags
,
629 struct type
*storage
)
636 if (ntype
->instance_flags () == new_flags
)
638 ntype
= TYPE_CHAIN (ntype
);
640 while (ntype
!= type
);
642 /* Create a new type instance. */
644 ntype
= alloc_type_instance (type
);
647 /* If STORAGE was provided, it had better be in the same objfile
648 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
649 if one objfile is freed and the other kept, we'd have
650 dangling pointers. */
651 gdb_assert (type
->objfile_owner () == storage
->objfile_owner ());
654 TYPE_MAIN_TYPE (ntype
) = TYPE_MAIN_TYPE (type
);
655 TYPE_CHAIN (ntype
) = ntype
;
658 /* Pointers or references to the original type are not relevant to
660 TYPE_POINTER_TYPE (ntype
) = (struct type
*) 0;
661 TYPE_REFERENCE_TYPE (ntype
) = (struct type
*) 0;
663 /* Chain the new qualified type to the old type. */
664 TYPE_CHAIN (ntype
) = TYPE_CHAIN (type
);
665 TYPE_CHAIN (type
) = ntype
;
667 /* Now set the instance flags and return the new type. */
668 ntype
->set_instance_flags (new_flags
);
670 /* Set length of new type to that of the original type. */
671 TYPE_LENGTH (ntype
) = TYPE_LENGTH (type
);
676 /* Make an address-space-delimited variant of a type -- a type that
677 is identical to the one supplied except that it has an address
678 space attribute attached to it (such as "code" or "data").
680 The space attributes "code" and "data" are for Harvard
681 architectures. The address space attributes are for architectures
682 which have alternately sized pointers or pointers with alternate
686 make_type_with_address_space (struct type
*type
,
687 type_instance_flags space_flag
)
689 type_instance_flags new_flags
= ((type
->instance_flags ()
690 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
691 | TYPE_INSTANCE_FLAG_DATA_SPACE
692 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
))
695 return make_qualified_type (type
, new_flags
, NULL
);
698 /* Make a "c-v" variant of a type -- a type that is identical to the
699 one supplied except that it may have const or volatile attributes
700 CNST is a flag for setting the const attribute
701 VOLTL is a flag for setting the volatile attribute
702 TYPE is the base type whose variant we are creating.
704 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
705 storage to hold the new qualified type; *TYPEPTR and TYPE must be
706 in the same objfile. Otherwise, allocate fresh memory for the new
707 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
708 new type we construct. */
711 make_cv_type (int cnst
, int voltl
,
713 struct type
**typeptr
)
715 struct type
*ntype
; /* New type */
717 type_instance_flags new_flags
= (type
->instance_flags ()
718 & ~(TYPE_INSTANCE_FLAG_CONST
719 | TYPE_INSTANCE_FLAG_VOLATILE
));
722 new_flags
|= TYPE_INSTANCE_FLAG_CONST
;
725 new_flags
|= TYPE_INSTANCE_FLAG_VOLATILE
;
727 if (typeptr
&& *typeptr
!= NULL
)
729 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
730 a C-V variant chain that threads across objfiles: if one
731 objfile gets freed, then the other has a broken C-V chain.
733 This code used to try to copy over the main type from TYPE to
734 *TYPEPTR if they were in different objfiles, but that's
735 wrong, too: TYPE may have a field list or member function
736 lists, which refer to types of their own, etc. etc. The
737 whole shebang would need to be copied over recursively; you
738 can't have inter-objfile pointers. The only thing to do is
739 to leave stub types as stub types, and look them up afresh by
740 name each time you encounter them. */
741 gdb_assert ((*typeptr
)->objfile_owner () == type
->objfile_owner ());
744 ntype
= make_qualified_type (type
, new_flags
,
745 typeptr
? *typeptr
: NULL
);
753 /* Make a 'restrict'-qualified version of TYPE. */
756 make_restrict_type (struct type
*type
)
758 return make_qualified_type (type
,
759 (type
->instance_flags ()
760 | TYPE_INSTANCE_FLAG_RESTRICT
),
764 /* Make a type without const, volatile, or restrict. */
767 make_unqualified_type (struct type
*type
)
769 return make_qualified_type (type
,
770 (type
->instance_flags ()
771 & ~(TYPE_INSTANCE_FLAG_CONST
772 | TYPE_INSTANCE_FLAG_VOLATILE
773 | TYPE_INSTANCE_FLAG_RESTRICT
)),
777 /* Make a '_Atomic'-qualified version of TYPE. */
780 make_atomic_type (struct type
*type
)
782 return make_qualified_type (type
,
783 (type
->instance_flags ()
784 | TYPE_INSTANCE_FLAG_ATOMIC
),
788 /* Replace the contents of ntype with the type *type. This changes the
789 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
790 the changes are propogated to all types in the TYPE_CHAIN.
792 In order to build recursive types, it's inevitable that we'll need
793 to update types in place --- but this sort of indiscriminate
794 smashing is ugly, and needs to be replaced with something more
795 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
796 clear if more steps are needed. */
799 replace_type (struct type
*ntype
, struct type
*type
)
803 /* These two types had better be in the same objfile. Otherwise,
804 the assignment of one type's main type structure to the other
805 will produce a type with references to objects (names; field
806 lists; etc.) allocated on an objfile other than its own. */
807 gdb_assert (ntype
->objfile_owner () == type
->objfile_owner ());
809 *TYPE_MAIN_TYPE (ntype
) = *TYPE_MAIN_TYPE (type
);
811 /* The type length is not a part of the main type. Update it for
812 each type on the variant chain. */
816 /* Assert that this element of the chain has no address-class bits
817 set in its flags. Such type variants might have type lengths
818 which are supposed to be different from the non-address-class
819 variants. This assertion shouldn't ever be triggered because
820 symbol readers which do construct address-class variants don't
821 call replace_type(). */
822 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain
) == 0);
824 TYPE_LENGTH (chain
) = TYPE_LENGTH (type
);
825 chain
= TYPE_CHAIN (chain
);
827 while (ntype
!= chain
);
829 /* Assert that the two types have equivalent instance qualifiers.
830 This should be true for at least all of our debug readers. */
831 gdb_assert (ntype
->instance_flags () == type
->instance_flags ());
834 /* Implement direct support for MEMBER_TYPE in GNU C++.
835 May need to construct such a type if this is the first use.
836 The TYPE is the type of the member. The DOMAIN is the type
837 of the aggregate that the member belongs to. */
840 lookup_memberptr_type (struct type
*type
, struct type
*domain
)
844 mtype
= alloc_type_copy (type
);
845 smash_to_memberptr_type (mtype
, domain
, type
);
849 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
852 lookup_methodptr_type (struct type
*to_type
)
856 mtype
= alloc_type_copy (to_type
);
857 smash_to_methodptr_type (mtype
, to_type
);
861 /* Allocate a stub method whose return type is TYPE. This apparently
862 happens for speed of symbol reading, since parsing out the
863 arguments to the method is cpu-intensive, the way we are doing it.
864 So, we will fill in arguments later. This always returns a fresh
868 allocate_stub_method (struct type
*type
)
872 mtype
= alloc_type_copy (type
);
873 mtype
->set_code (TYPE_CODE_METHOD
);
874 TYPE_LENGTH (mtype
) = 1;
875 mtype
->set_is_stub (true);
876 TYPE_TARGET_TYPE (mtype
) = type
;
877 /* TYPE_SELF_TYPE (mtype) = unknown yet */
881 /* See gdbtypes.h. */
884 operator== (const dynamic_prop
&l
, const dynamic_prop
&r
)
886 if (l
.kind () != r
.kind ())
894 return l
.const_val () == r
.const_val ();
895 case PROP_ADDR_OFFSET
:
898 return l
.baton () == r
.baton ();
899 case PROP_VARIANT_PARTS
:
900 return l
.variant_parts () == r
.variant_parts ();
902 return l
.original_type () == r
.original_type ();
905 gdb_assert_not_reached ("unhandled dynamic_prop kind");
908 /* See gdbtypes.h. */
911 operator== (const range_bounds
&l
, const range_bounds
&r
)
913 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
915 return (FIELD_EQ (low
)
917 && FIELD_EQ (flag_upper_bound_is_count
)
918 && FIELD_EQ (flag_bound_evaluated
)
924 /* Create a range type with a dynamic range from LOW_BOUND to
925 HIGH_BOUND, inclusive. See create_range_type for further details. */
928 create_range_type (struct type
*result_type
, struct type
*index_type
,
929 const struct dynamic_prop
*low_bound
,
930 const struct dynamic_prop
*high_bound
,
933 /* The INDEX_TYPE should be a type capable of holding the upper and lower
934 bounds, as such a zero sized, or void type makes no sense. */
935 gdb_assert (index_type
->code () != TYPE_CODE_VOID
);
936 gdb_assert (TYPE_LENGTH (index_type
) > 0);
938 if (result_type
== NULL
)
939 result_type
= alloc_type_copy (index_type
);
940 result_type
->set_code (TYPE_CODE_RANGE
);
941 TYPE_TARGET_TYPE (result_type
) = index_type
;
942 if (index_type
->is_stub ())
943 result_type
->set_target_is_stub (true);
945 TYPE_LENGTH (result_type
) = TYPE_LENGTH (check_typedef (index_type
));
948 = (struct range_bounds
*) TYPE_ZALLOC (result_type
, sizeof (range_bounds
));
949 bounds
->low
= *low_bound
;
950 bounds
->high
= *high_bound
;
952 bounds
->stride
.set_const_val (0);
954 result_type
->set_bounds (bounds
);
956 if (index_type
->code () == TYPE_CODE_FIXED_POINT
)
957 result_type
->set_is_unsigned (index_type
->is_unsigned ());
958 /* Note that the signed-ness of a range type can't simply be copied
959 from the underlying type. Consider a case where the underlying
960 type is 'int', but the range type can hold 0..65535, and where
961 the range is further specified to fit into 16 bits. In this
962 case, if we copy the underlying type's sign, then reading some
963 range values will cause an unwanted sign extension. So, we have
964 some heuristics here instead. */
965 else if (low_bound
->kind () == PROP_CONST
&& low_bound
->const_val () >= 0)
966 result_type
->set_is_unsigned (true);
967 /* Ada allows the declaration of range types whose upper bound is
968 less than the lower bound, so checking the lower bound is not
969 enough. Make sure we do not mark a range type whose upper bound
970 is negative as unsigned. */
971 if (high_bound
->kind () == PROP_CONST
&& high_bound
->const_val () < 0)
972 result_type
->set_is_unsigned (false);
974 result_type
->set_endianity_is_not_default
975 (index_type
->endianity_is_not_default ());
980 /* See gdbtypes.h. */
983 create_range_type_with_stride (struct type
*result_type
,
984 struct type
*index_type
,
985 const struct dynamic_prop
*low_bound
,
986 const struct dynamic_prop
*high_bound
,
988 const struct dynamic_prop
*stride
,
991 result_type
= create_range_type (result_type
, index_type
, low_bound
,
994 gdb_assert (stride
!= nullptr);
995 result_type
->bounds ()->stride
= *stride
;
996 result_type
->bounds ()->flag_is_byte_stride
= byte_stride_p
;
1003 /* Create a range type using either a blank type supplied in
1004 RESULT_TYPE, or creating a new type, inheriting the objfile from
1007 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
1008 to HIGH_BOUND, inclusive.
1010 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1011 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
1014 create_static_range_type (struct type
*result_type
, struct type
*index_type
,
1015 LONGEST low_bound
, LONGEST high_bound
)
1017 struct dynamic_prop low
, high
;
1019 low
.set_const_val (low_bound
);
1020 high
.set_const_val (high_bound
);
1022 result_type
= create_range_type (result_type
, index_type
, &low
, &high
, 0);
1027 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1028 are static, otherwise returns 0. */
1031 has_static_range (const struct range_bounds
*bounds
)
1033 /* If the range doesn't have a defined stride then its stride field will
1034 be initialized to the constant 0. */
1035 return (bounds
->low
.kind () == PROP_CONST
1036 && bounds
->high
.kind () == PROP_CONST
1037 && bounds
->stride
.kind () == PROP_CONST
);
1040 /* See gdbtypes.h. */
1042 gdb::optional
<LONGEST
>
1043 get_discrete_low_bound (struct type
*type
)
1045 type
= check_typedef (type
);
1046 switch (type
->code ())
1048 case TYPE_CODE_RANGE
:
1050 /* This function only works for ranges with a constant low bound. */
1051 if (type
->bounds ()->low
.kind () != PROP_CONST
)
1054 LONGEST low
= type
->bounds ()->low
.const_val ();
1056 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ENUM
)
1058 gdb::optional
<LONGEST
> low_pos
1059 = discrete_position (TYPE_TARGET_TYPE (type
), low
);
1061 if (low_pos
.has_value ())
1068 case TYPE_CODE_ENUM
:
1070 if (type
->num_fields () > 0)
1072 /* The enums may not be sorted by value, so search all
1074 LONGEST low
= type
->field (0).loc_enumval ();
1076 for (int i
= 0; i
< type
->num_fields (); i
++)
1078 if (type
->field (i
).loc_enumval () < low
)
1079 low
= type
->field (i
).loc_enumval ();
1082 /* Set unsigned indicator if warranted. */
1084 type
->set_is_unsigned (true);
1092 case TYPE_CODE_BOOL
:
1096 if (TYPE_LENGTH (type
) > sizeof (LONGEST
)) /* Too big */
1099 if (!type
->is_unsigned ())
1100 return -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
1103 case TYPE_CODE_CHAR
:
1111 /* See gdbtypes.h. */
1113 gdb::optional
<LONGEST
>
1114 get_discrete_high_bound (struct type
*type
)
1116 type
= check_typedef (type
);
1117 switch (type
->code ())
1119 case TYPE_CODE_RANGE
:
1121 /* This function only works for ranges with a constant high bound. */
1122 if (type
->bounds ()->high
.kind () != PROP_CONST
)
1125 LONGEST high
= type
->bounds ()->high
.const_val ();
1127 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ENUM
)
1129 gdb::optional
<LONGEST
> high_pos
1130 = discrete_position (TYPE_TARGET_TYPE (type
), high
);
1132 if (high_pos
.has_value ())
1139 case TYPE_CODE_ENUM
:
1141 if (type
->num_fields () > 0)
1143 /* The enums may not be sorted by value, so search all
1145 LONGEST high
= type
->field (0).loc_enumval ();
1147 for (int i
= 0; i
< type
->num_fields (); i
++)
1149 if (type
->field (i
).loc_enumval () > high
)
1150 high
= type
->field (i
).loc_enumval ();
1159 case TYPE_CODE_BOOL
:
1163 if (TYPE_LENGTH (type
) > sizeof (LONGEST
)) /* Too big */
1166 if (!type
->is_unsigned ())
1168 LONGEST low
= -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
1173 case TYPE_CODE_CHAR
:
1175 /* This round-about calculation is to avoid shifting by
1176 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
1177 if TYPE_LENGTH (type) == sizeof (LONGEST). */
1178 LONGEST high
= 1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1);
1179 return (high
- 1) | high
;
1187 /* See gdbtypes.h. */
1190 get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
1192 gdb::optional
<LONGEST
> low
= get_discrete_low_bound (type
);
1193 if (!low
.has_value ())
1196 gdb::optional
<LONGEST
> high
= get_discrete_high_bound (type
);
1197 if (!high
.has_value ())
1206 /* See gdbtypes.h */
1209 get_array_bounds (struct type
*type
, LONGEST
*low_bound
, LONGEST
*high_bound
)
1211 struct type
*index
= type
->index_type ();
1218 if (!get_discrete_bounds (index
, &low
, &high
))
1230 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1231 representation of a value of this type, save the corresponding
1232 position number in POS.
1234 Its differs from VAL only in the case of enumeration types. In
1235 this case, the position number of the value of the first listed
1236 enumeration literal is zero; the position number of the value of
1237 each subsequent enumeration literal is one more than that of its
1238 predecessor in the list.
1240 Return 1 if the operation was successful. Return zero otherwise,
1241 in which case the value of POS is unmodified.
1244 gdb::optional
<LONGEST
>
1245 discrete_position (struct type
*type
, LONGEST val
)
1247 if (type
->code () == TYPE_CODE_RANGE
)
1248 type
= TYPE_TARGET_TYPE (type
);
1250 if (type
->code () == TYPE_CODE_ENUM
)
1254 for (i
= 0; i
< type
->num_fields (); i
+= 1)
1256 if (val
== type
->field (i
).loc_enumval ())
1260 /* Invalid enumeration value. */
1267 /* If the array TYPE has static bounds calculate and update its
1268 size, then return true. Otherwise return false and leave TYPE
1272 update_static_array_size (struct type
*type
)
1274 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
1276 struct type
*range_type
= type
->index_type ();
1278 if (type
->dyn_prop (DYN_PROP_BYTE_STRIDE
) == nullptr
1279 && has_static_range (range_type
->bounds ())
1280 && (!type_not_associated (type
)
1281 && !type_not_allocated (type
)))
1283 LONGEST low_bound
, high_bound
;
1285 struct type
*element_type
;
1287 stride
= type
->bit_stride ();
1289 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
1290 low_bound
= high_bound
= 0;
1292 element_type
= check_typedef (TYPE_TARGET_TYPE (type
));
1293 /* Be careful when setting the array length. Ada arrays can be
1294 empty arrays with the high_bound being smaller than the low_bound.
1295 In such cases, the array length should be zero. */
1296 if (high_bound
< low_bound
)
1297 TYPE_LENGTH (type
) = 0;
1298 else if (stride
!= 0)
1300 /* Ensure that the type length is always positive, even in the
1301 case where (for example in Fortran) we have a negative
1302 stride. It is possible to have a single element array with a
1303 negative stride in Fortran (this doesn't mean anything
1304 special, it's still just a single element array) so do
1305 consider that case when touching this code. */
1306 LONGEST element_count
= std::abs (high_bound
- low_bound
+ 1);
1308 = ((std::abs (stride
) * element_count
) + 7) / 8;
1311 TYPE_LENGTH (type
) =
1312 TYPE_LENGTH (element_type
) * (high_bound
- low_bound
+ 1);
1314 /* If this array's element is itself an array with a bit stride,
1315 then we want to update this array's bit stride to reflect the
1316 size of the sub-array. Otherwise, we'll end up using the
1317 wrong size when trying to find elements of the outer
1319 if (element_type
->code () == TYPE_CODE_ARRAY
1320 && TYPE_LENGTH (element_type
) != 0
1321 && TYPE_FIELD_BITSIZE (element_type
, 0) != 0
1322 && get_array_bounds (element_type
, &low_bound
, &high_bound
)
1323 && high_bound
>= low_bound
)
1324 TYPE_FIELD_BITSIZE (type
, 0)
1325 = ((high_bound
- low_bound
+ 1)
1326 * TYPE_FIELD_BITSIZE (element_type
, 0));
1334 /* Create an array type using either a blank type supplied in
1335 RESULT_TYPE, or creating a new type, inheriting the objfile from
1338 Elements will be of type ELEMENT_TYPE, the indices will be of type
1341 BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
1342 This byte stride property is added to the resulting array type
1343 as a DYN_PROP_BYTE_STRIDE. As a consequence, the BYTE_STRIDE_PROP
1344 argument can only be used to create types that are objfile-owned
1345 (see add_dyn_prop), meaning that either this function must be called
1346 with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
1348 BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
1349 If BIT_STRIDE is not zero, build a packed array type whose element
1350 size is BIT_STRIDE. Otherwise, ignore this parameter.
1352 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1353 sure it is TYPE_CODE_UNDEF before we bash it into an array
1357 create_array_type_with_stride (struct type
*result_type
,
1358 struct type
*element_type
,
1359 struct type
*range_type
,
1360 struct dynamic_prop
*byte_stride_prop
,
1361 unsigned int bit_stride
)
1363 if (byte_stride_prop
!= NULL
1364 && byte_stride_prop
->kind () == PROP_CONST
)
1366 /* The byte stride is actually not dynamic. Pretend we were
1367 called with bit_stride set instead of byte_stride_prop.
1368 This will give us the same result type, while avoiding
1369 the need to handle this as a special case. */
1370 bit_stride
= byte_stride_prop
->const_val () * 8;
1371 byte_stride_prop
= NULL
;
1374 if (result_type
== NULL
)
1375 result_type
= alloc_type_copy (range_type
);
1377 result_type
->set_code (TYPE_CODE_ARRAY
);
1378 TYPE_TARGET_TYPE (result_type
) = element_type
;
1380 result_type
->set_num_fields (1);
1381 result_type
->set_fields
1382 ((struct field
*) TYPE_ZALLOC (result_type
, sizeof (struct field
)));
1383 result_type
->set_index_type (range_type
);
1384 if (byte_stride_prop
!= NULL
)
1385 result_type
->add_dyn_prop (DYN_PROP_BYTE_STRIDE
, *byte_stride_prop
);
1386 else if (bit_stride
> 0)
1387 TYPE_FIELD_BITSIZE (result_type
, 0) = bit_stride
;
1389 if (!update_static_array_size (result_type
))
1391 /* This type is dynamic and its length needs to be computed
1392 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1393 undefined by setting it to zero. Although we are not expected
1394 to trust TYPE_LENGTH in this case, setting the size to zero
1395 allows us to avoid allocating objects of random sizes in case
1396 we accidently do. */
1397 TYPE_LENGTH (result_type
) = 0;
1400 /* TYPE_TARGET_STUB will take care of zero length arrays. */
1401 if (TYPE_LENGTH (result_type
) == 0)
1402 result_type
->set_target_is_stub (true);
1407 /* Same as create_array_type_with_stride but with no bit_stride
1408 (BIT_STRIDE = 0), thus building an unpacked array. */
1411 create_array_type (struct type
*result_type
,
1412 struct type
*element_type
,
1413 struct type
*range_type
)
1415 return create_array_type_with_stride (result_type
, element_type
,
1416 range_type
, NULL
, 0);
1420 lookup_array_range_type (struct type
*element_type
,
1421 LONGEST low_bound
, LONGEST high_bound
)
1423 struct type
*index_type
;
1424 struct type
*range_type
;
1426 if (element_type
->is_objfile_owned ())
1427 index_type
= objfile_type (element_type
->objfile_owner ())->builtin_int
;
1429 index_type
= builtin_type (element_type
->arch_owner ())->builtin_int
;
1431 range_type
= create_static_range_type (NULL
, index_type
,
1432 low_bound
, high_bound
);
1434 return create_array_type (NULL
, element_type
, range_type
);
1437 /* Create a string type using either a blank type supplied in
1438 RESULT_TYPE, or creating a new type. String types are similar
1439 enough to array of char types that we can use create_array_type to
1440 build the basic type and then bash it into a string type.
1442 For fixed length strings, the range type contains 0 as the lower
1443 bound and the length of the string minus one as the upper bound.
1445 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1446 sure it is TYPE_CODE_UNDEF before we bash it into a string
1450 create_string_type (struct type
*result_type
,
1451 struct type
*string_char_type
,
1452 struct type
*range_type
)
1454 result_type
= create_array_type (result_type
,
1457 result_type
->set_code (TYPE_CODE_STRING
);
1462 lookup_string_range_type (struct type
*string_char_type
,
1463 LONGEST low_bound
, LONGEST high_bound
)
1465 struct type
*result_type
;
1467 result_type
= lookup_array_range_type (string_char_type
,
1468 low_bound
, high_bound
);
1469 result_type
->set_code (TYPE_CODE_STRING
);
1474 create_set_type (struct type
*result_type
, struct type
*domain_type
)
1476 if (result_type
== NULL
)
1477 result_type
= alloc_type_copy (domain_type
);
1479 result_type
->set_code (TYPE_CODE_SET
);
1480 result_type
->set_num_fields (1);
1481 result_type
->set_fields
1482 ((struct field
*) TYPE_ZALLOC (result_type
, sizeof (struct field
)));
1484 if (!domain_type
->is_stub ())
1486 LONGEST low_bound
, high_bound
, bit_length
;
1488 if (!get_discrete_bounds (domain_type
, &low_bound
, &high_bound
))
1489 low_bound
= high_bound
= 0;
1491 bit_length
= high_bound
- low_bound
+ 1;
1492 TYPE_LENGTH (result_type
)
1493 = (bit_length
+ TARGET_CHAR_BIT
- 1) / TARGET_CHAR_BIT
;
1495 result_type
->set_is_unsigned (true);
1497 result_type
->field (0).set_type (domain_type
);
1502 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1503 and any array types nested inside it. */
1506 make_vector_type (struct type
*array_type
)
1508 struct type
*inner_array
, *elt_type
;
1510 /* Find the innermost array type, in case the array is
1511 multi-dimensional. */
1512 inner_array
= array_type
;
1513 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
1514 inner_array
= TYPE_TARGET_TYPE (inner_array
);
1516 elt_type
= TYPE_TARGET_TYPE (inner_array
);
1517 if (elt_type
->code () == TYPE_CODE_INT
)
1519 type_instance_flags flags
1520 = elt_type
->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT
;
1521 elt_type
= make_qualified_type (elt_type
, flags
, NULL
);
1522 TYPE_TARGET_TYPE (inner_array
) = elt_type
;
1525 array_type
->set_is_vector (true);
1529 init_vector_type (struct type
*elt_type
, int n
)
1531 struct type
*array_type
;
1533 array_type
= lookup_array_range_type (elt_type
, 0, n
- 1);
1534 make_vector_type (array_type
);
1538 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1539 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1540 confusing. "self" is a common enough replacement for "this".
1541 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1542 TYPE_CODE_METHOD. */
1545 internal_type_self_type (struct type
*type
)
1547 switch (type
->code ())
1549 case TYPE_CODE_METHODPTR
:
1550 case TYPE_CODE_MEMBERPTR
:
1551 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1553 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_SELF_TYPE
);
1554 return TYPE_MAIN_TYPE (type
)->type_specific
.self_type
;
1555 case TYPE_CODE_METHOD
:
1556 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1558 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
1559 return TYPE_MAIN_TYPE (type
)->type_specific
.func_stuff
->self_type
;
1561 gdb_assert_not_reached ("bad type");
1565 /* Set the type of the class that TYPE belongs to.
1566 In c++ this is the class of "this".
1567 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1568 TYPE_CODE_METHOD. */
1571 set_type_self_type (struct type
*type
, struct type
*self_type
)
1573 switch (type
->code ())
1575 case TYPE_CODE_METHODPTR
:
1576 case TYPE_CODE_MEMBERPTR
:
1577 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1578 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_SELF_TYPE
;
1579 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_SELF_TYPE
);
1580 TYPE_MAIN_TYPE (type
)->type_specific
.self_type
= self_type
;
1582 case TYPE_CODE_METHOD
:
1583 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1584 INIT_FUNC_SPECIFIC (type
);
1585 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
1586 TYPE_MAIN_TYPE (type
)->type_specific
.func_stuff
->self_type
= self_type
;
1589 gdb_assert_not_reached ("bad type");
1593 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1594 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1595 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1596 TYPE doesn't include the offset (that's the value of the MEMBER
1597 itself), but does include the structure type into which it points
1600 When "smashing" the type, we preserve the objfile that the old type
1601 pointed to, since we aren't changing where the type is actually
1605 smash_to_memberptr_type (struct type
*type
, struct type
*self_type
,
1606 struct type
*to_type
)
1609 type
->set_code (TYPE_CODE_MEMBERPTR
);
1610 TYPE_TARGET_TYPE (type
) = to_type
;
1611 set_type_self_type (type
, self_type
);
1612 /* Assume that a data member pointer is the same size as a normal
1614 TYPE_LENGTH (type
) = gdbarch_ptr_bit (to_type
->arch ()) / TARGET_CHAR_BIT
;
1617 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1619 When "smashing" the type, we preserve the objfile that the old type
1620 pointed to, since we aren't changing where the type is actually
1624 smash_to_methodptr_type (struct type
*type
, struct type
*to_type
)
1627 type
->set_code (TYPE_CODE_METHODPTR
);
1628 TYPE_TARGET_TYPE (type
) = to_type
;
1629 set_type_self_type (type
, TYPE_SELF_TYPE (to_type
));
1630 TYPE_LENGTH (type
) = cplus_method_ptr_size (to_type
);
1633 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1634 METHOD just means `function that gets an extra "this" argument'.
1636 When "smashing" the type, we preserve the objfile that the old type
1637 pointed to, since we aren't changing where the type is actually
1641 smash_to_method_type (struct type
*type
, struct type
*self_type
,
1642 struct type
*to_type
, struct field
*args
,
1643 int nargs
, int varargs
)
1646 type
->set_code (TYPE_CODE_METHOD
);
1647 TYPE_TARGET_TYPE (type
) = to_type
;
1648 set_type_self_type (type
, self_type
);
1649 type
->set_fields (args
);
1650 type
->set_num_fields (nargs
);
1652 type
->set_has_varargs (true);
1653 TYPE_LENGTH (type
) = 1; /* In practice, this is never needed. */
1656 /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
1657 Since GCC PR debug/47510 DWARF provides associated information to detect the
1658 anonymous class linkage name from its typedef.
1660 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1664 type_name_or_error (struct type
*type
)
1666 struct type
*saved_type
= type
;
1668 struct objfile
*objfile
;
1670 type
= check_typedef (type
);
1672 name
= type
->name ();
1676 name
= saved_type
->name ();
1677 objfile
= saved_type
->objfile_owner ();
1678 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1679 name
? name
: "<anonymous>",
1680 objfile
? objfile_name (objfile
) : "<arch>");
1683 /* Lookup a typedef or primitive type named NAME, visible in lexical
1684 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1685 suitably defined. */
1688 lookup_typename (const struct language_defn
*language
,
1690 const struct block
*block
, int noerr
)
1694 sym
= lookup_symbol_in_language (name
, block
, VAR_DOMAIN
,
1695 language
->la_language
, NULL
).symbol
;
1696 if (sym
!= NULL
&& sym
->aclass () == LOC_TYPEDEF
)
1697 return sym
->type ();
1701 error (_("No type named %s."), name
);
1705 lookup_unsigned_typename (const struct language_defn
*language
,
1708 char *uns
= (char *) alloca (strlen (name
) + 10);
1710 strcpy (uns
, "unsigned ");
1711 strcpy (uns
+ 9, name
);
1712 return lookup_typename (language
, uns
, NULL
, 0);
1716 lookup_signed_typename (const struct language_defn
*language
, const char *name
)
1719 char *uns
= (char *) alloca (strlen (name
) + 8);
1721 strcpy (uns
, "signed ");
1722 strcpy (uns
+ 7, name
);
1723 t
= lookup_typename (language
, uns
, NULL
, 1);
1724 /* If we don't find "signed FOO" just try again with plain "FOO". */
1727 return lookup_typename (language
, name
, NULL
, 0);
1730 /* Lookup a structure type named "struct NAME",
1731 visible in lexical block BLOCK. */
1734 lookup_struct (const char *name
, const struct block
*block
)
1738 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1742 error (_("No struct type named %s."), name
);
1744 if (sym
->type ()->code () != TYPE_CODE_STRUCT
)
1746 error (_("This context has class, union or enum %s, not a struct."),
1749 return (sym
->type ());
1752 /* Lookup a union type named "union NAME",
1753 visible in lexical block BLOCK. */
1756 lookup_union (const char *name
, const struct block
*block
)
1761 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1764 error (_("No union type named %s."), name
);
1768 if (t
->code () == TYPE_CODE_UNION
)
1771 /* If we get here, it's not a union. */
1772 error (_("This context has class, struct or enum %s, not a union."),
1776 /* Lookup an enum type named "enum NAME",
1777 visible in lexical block BLOCK. */
1780 lookup_enum (const char *name
, const struct block
*block
)
1784 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1787 error (_("No enum type named %s."), name
);
1789 if (sym
->type ()->code () != TYPE_CODE_ENUM
)
1791 error (_("This context has class, struct or union %s, not an enum."),
1794 return (sym
->type ());
1797 /* Lookup a template type named "template NAME<TYPE>",
1798 visible in lexical block BLOCK. */
1801 lookup_template_type (const char *name
, struct type
*type
,
1802 const struct block
*block
)
1805 char *nam
= (char *)
1806 alloca (strlen (name
) + strlen (type
->name ()) + 4);
1810 strcat (nam
, type
->name ());
1811 strcat (nam
, " >"); /* FIXME, extra space still introduced in gcc? */
1813 sym
= lookup_symbol (nam
, block
, VAR_DOMAIN
, 0).symbol
;
1817 error (_("No template type named %s."), name
);
1819 if (sym
->type ()->code () != TYPE_CODE_STRUCT
)
1821 error (_("This context has class, union or enum %s, not a struct."),
1824 return (sym
->type ());
1827 /* See gdbtypes.h. */
1830 lookup_struct_elt (struct type
*type
, const char *name
, int noerr
)
1836 type
= check_typedef (type
);
1837 if (type
->code () != TYPE_CODE_PTR
1838 && type
->code () != TYPE_CODE_REF
)
1840 type
= TYPE_TARGET_TYPE (type
);
1843 if (type
->code () != TYPE_CODE_STRUCT
1844 && type
->code () != TYPE_CODE_UNION
)
1846 std::string type_name
= type_to_string (type
);
1847 error (_("Type %s is not a structure or union type."),
1848 type_name
.c_str ());
1851 for (i
= type
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1853 const char *t_field_name
= type
->field (i
).name ();
1855 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1857 return {&type
->field (i
), type
->field (i
).loc_bitpos ()};
1859 else if (!t_field_name
|| *t_field_name
== '\0')
1862 = lookup_struct_elt (type
->field (i
).type (), name
, 1);
1863 if (elt
.field
!= NULL
)
1865 elt
.offset
+= type
->field (i
).loc_bitpos ();
1871 /* OK, it's not in this class. Recursively check the baseclasses. */
1872 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1874 struct_elt elt
= lookup_struct_elt (TYPE_BASECLASS (type
, i
), name
, 1);
1875 if (elt
.field
!= NULL
)
1880 return {nullptr, 0};
1882 std::string type_name
= type_to_string (type
);
1883 error (_("Type %s has no component named %s."), type_name
.c_str (), name
);
1886 /* See gdbtypes.h. */
1889 lookup_struct_elt_type (struct type
*type
, const char *name
, int noerr
)
1891 struct_elt elt
= lookup_struct_elt (type
, name
, noerr
);
1892 if (elt
.field
!= NULL
)
1893 return elt
.field
->type ();
1898 /* Return the largest number representable by unsigned integer type TYPE. */
1901 get_unsigned_type_max (struct type
*type
)
1905 type
= check_typedef (type
);
1906 gdb_assert (type
->code () == TYPE_CODE_INT
&& type
->is_unsigned ());
1907 gdb_assert (TYPE_LENGTH (type
) <= sizeof (ULONGEST
));
1909 /* Written this way to avoid overflow. */
1910 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1911 return ((((ULONGEST
) 1 << (n
- 1)) - 1) << 1) | 1;
1914 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1915 signed integer type TYPE. */
1918 get_signed_type_minmax (struct type
*type
, LONGEST
*min
, LONGEST
*max
)
1922 type
= check_typedef (type
);
1923 gdb_assert (type
->code () == TYPE_CODE_INT
&& !type
->is_unsigned ());
1924 gdb_assert (TYPE_LENGTH (type
) <= sizeof (LONGEST
));
1926 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1927 *min
= -((ULONGEST
) 1 << (n
- 1));
1928 *max
= ((ULONGEST
) 1 << (n
- 1)) - 1;
1931 /* Return the largest value representable by pointer type TYPE. */
1934 get_pointer_type_max (struct type
*type
)
1938 type
= check_typedef (type
);
1939 gdb_assert (type
->code () == TYPE_CODE_PTR
);
1940 gdb_assert (TYPE_LENGTH (type
) <= sizeof (CORE_ADDR
));
1942 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1943 return ((((CORE_ADDR
) 1 << (n
- 1)) - 1) << 1) | 1;
1946 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1947 cplus_stuff.vptr_fieldno.
1949 cplus_stuff is initialized to cplus_struct_default which does not
1950 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1951 designated initializers). We cope with that here. */
1954 internal_type_vptr_fieldno (struct type
*type
)
1956 type
= check_typedef (type
);
1957 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1958 || type
->code () == TYPE_CODE_UNION
);
1959 if (!HAVE_CPLUS_STRUCT (type
))
1961 return TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_fieldno
;
1964 /* Set the value of cplus_stuff.vptr_fieldno. */
1967 set_type_vptr_fieldno (struct type
*type
, int fieldno
)
1969 type
= check_typedef (type
);
1970 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1971 || type
->code () == TYPE_CODE_UNION
);
1972 if (!HAVE_CPLUS_STRUCT (type
))
1973 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
1974 TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_fieldno
= fieldno
;
1977 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1978 cplus_stuff.vptr_basetype. */
1981 internal_type_vptr_basetype (struct type
*type
)
1983 type
= check_typedef (type
);
1984 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1985 || type
->code () == TYPE_CODE_UNION
);
1986 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_CPLUS_STUFF
);
1987 return TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_basetype
;
1990 /* Set the value of cplus_stuff.vptr_basetype. */
1993 set_type_vptr_basetype (struct type
*type
, struct type
*basetype
)
1995 type
= check_typedef (type
);
1996 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1997 || type
->code () == TYPE_CODE_UNION
);
1998 if (!HAVE_CPLUS_STRUCT (type
))
1999 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
2000 TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_basetype
= basetype
;
2003 /* Lookup the vptr basetype/fieldno values for TYPE.
2004 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
2005 vptr_fieldno. Also, if found and basetype is from the same objfile,
2007 If not found, return -1 and ignore BASETYPEP.
2008 Callers should be aware that in some cases (for example,
2009 the type or one of its baseclasses is a stub type and we are
2010 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
2011 this function will not be able to find the
2012 virtual function table pointer, and vptr_fieldno will remain -1 and
2013 vptr_basetype will remain NULL or incomplete. */
2016 get_vptr_fieldno (struct type
*type
, struct type
**basetypep
)
2018 type
= check_typedef (type
);
2020 if (TYPE_VPTR_FIELDNO (type
) < 0)
2024 /* We must start at zero in case the first (and only) baseclass
2025 is virtual (and hence we cannot share the table pointer). */
2026 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
2028 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
2030 struct type
*basetype
;
2032 fieldno
= get_vptr_fieldno (baseclass
, &basetype
);
2035 /* If the type comes from a different objfile we can't cache
2036 it, it may have a different lifetime. PR 2384 */
2037 if (type
->objfile_owner () == basetype
->objfile_owner ())
2039 set_type_vptr_fieldno (type
, fieldno
);
2040 set_type_vptr_basetype (type
, basetype
);
2043 *basetypep
= basetype
;
2054 *basetypep
= TYPE_VPTR_BASETYPE (type
);
2055 return TYPE_VPTR_FIELDNO (type
);
2060 stub_noname_complaint (void)
2062 complaint (_("stub type has NULL name"));
2065 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2066 attached to it, and that property has a non-constant value. */
2069 array_type_has_dynamic_stride (struct type
*type
)
2071 struct dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2073 return (prop
!= NULL
&& prop
->kind () != PROP_CONST
);
2076 /* Worker for is_dynamic_type. */
2079 is_dynamic_type_internal (struct type
*type
, int top_level
)
2081 type
= check_typedef (type
);
2083 /* We only want to recognize references at the outermost level. */
2084 if (top_level
&& type
->code () == TYPE_CODE_REF
)
2085 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2087 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2088 dynamic, even if the type itself is statically defined.
2089 From a user's point of view, this may appear counter-intuitive;
2090 but it makes sense in this context, because the point is to determine
2091 whether any part of the type needs to be resolved before it can
2093 if (TYPE_DATA_LOCATION (type
) != NULL
2094 && (TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCEXPR
2095 || TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCLIST
))
2098 if (TYPE_ASSOCIATED_PROP (type
))
2101 if (TYPE_ALLOCATED_PROP (type
))
2104 struct dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2105 if (prop
!= nullptr && prop
->kind () != PROP_TYPE
)
2108 if (TYPE_HAS_DYNAMIC_LENGTH (type
))
2111 switch (type
->code ())
2113 case TYPE_CODE_RANGE
:
2115 /* A range type is obviously dynamic if it has at least one
2116 dynamic bound. But also consider the range type to be
2117 dynamic when its subtype is dynamic, even if the bounds
2118 of the range type are static. It allows us to assume that
2119 the subtype of a static range type is also static. */
2120 return (!has_static_range (type
->bounds ())
2121 || is_dynamic_type_internal (TYPE_TARGET_TYPE (type
), 0));
2124 case TYPE_CODE_STRING
:
2125 /* Strings are very much like an array of characters, and can be
2126 treated as one here. */
2127 case TYPE_CODE_ARRAY
:
2129 gdb_assert (type
->num_fields () == 1);
2131 /* The array is dynamic if either the bounds are dynamic... */
2132 if (is_dynamic_type_internal (type
->index_type (), 0))
2134 /* ... or the elements it contains have a dynamic contents... */
2135 if (is_dynamic_type_internal (TYPE_TARGET_TYPE (type
), 0))
2137 /* ... or if it has a dynamic stride... */
2138 if (array_type_has_dynamic_stride (type
))
2143 case TYPE_CODE_STRUCT
:
2144 case TYPE_CODE_UNION
:
2148 bool is_cplus
= HAVE_CPLUS_STRUCT (type
);
2150 for (i
= 0; i
< type
->num_fields (); ++i
)
2152 /* Static fields can be ignored here. */
2153 if (field_is_static (&type
->field (i
)))
2155 /* If the field has dynamic type, then so does TYPE. */
2156 if (is_dynamic_type_internal (type
->field (i
).type (), 0))
2158 /* If the field is at a fixed offset, then it is not
2160 if (type
->field (i
).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK
)
2162 /* Do not consider C++ virtual base types to be dynamic
2163 due to the field's offset being dynamic; these are
2164 handled via other means. */
2165 if (is_cplus
&& BASETYPE_VIA_VIRTUAL (type
, i
))
2176 /* See gdbtypes.h. */
2179 is_dynamic_type (struct type
*type
)
2181 return is_dynamic_type_internal (type
, 1);
2184 static struct type
*resolve_dynamic_type_internal
2185 (struct type
*type
, struct property_addr_info
*addr_stack
, int top_level
);
2187 /* Given a dynamic range type (dyn_range_type) and a stack of
2188 struct property_addr_info elements, return a static version
2191 When RESOLVE_P is true then the returned static range is created by
2192 actually evaluating any dynamic properties within the range type, while
2193 when RESOLVE_P is false the returned static range has all of the bounds
2194 and stride information set to undefined. The RESOLVE_P set to false
2195 case will be used when evaluating a dynamic array that is not
2196 allocated, or not associated, i.e. the bounds information might not be
2199 RANK is the array rank for which we are resolving this range, and is a
2200 zero based count. The rank should never be negative.
2203 static struct type
*
2204 resolve_dynamic_range (struct type
*dyn_range_type
,
2205 struct property_addr_info
*addr_stack
,
2206 int rank
, bool resolve_p
= true)
2209 struct type
*static_range_type
, *static_target_type
;
2210 struct dynamic_prop low_bound
, high_bound
, stride
;
2212 gdb_assert (dyn_range_type
->code () == TYPE_CODE_RANGE
);
2213 gdb_assert (rank
>= 0);
2215 const struct dynamic_prop
*prop
= &dyn_range_type
->bounds ()->low
;
2216 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2217 { (CORE_ADDR
) rank
}))
2218 low_bound
.set_const_val (value
);
2220 low_bound
.set_undefined ();
2222 prop
= &dyn_range_type
->bounds ()->high
;
2223 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2224 { (CORE_ADDR
) rank
}))
2226 high_bound
.set_const_val (value
);
2228 if (dyn_range_type
->bounds ()->flag_upper_bound_is_count
)
2229 high_bound
.set_const_val
2230 (low_bound
.const_val () + high_bound
.const_val () - 1);
2233 high_bound
.set_undefined ();
2235 bool byte_stride_p
= dyn_range_type
->bounds ()->flag_is_byte_stride
;
2236 prop
= &dyn_range_type
->bounds ()->stride
;
2237 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2238 { (CORE_ADDR
) rank
}))
2240 stride
.set_const_val (value
);
2242 /* If we have a bit stride that is not an exact number of bytes then
2243 I really don't think this is going to work with current GDB, the
2244 array indexing code in GDB seems to be pretty heavily tied to byte
2245 offsets right now. Assuming 8 bits in a byte. */
2246 struct gdbarch
*gdbarch
= dyn_range_type
->arch ();
2247 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
2248 if (!byte_stride_p
&& (value
% (unit_size
* 8)) != 0)
2249 error (_("bit strides that are not a multiple of the byte size "
2250 "are currently not supported"));
2254 stride
.set_undefined ();
2255 byte_stride_p
= true;
2259 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type
),
2261 LONGEST bias
= dyn_range_type
->bounds ()->bias
;
2262 static_range_type
= create_range_type_with_stride
2263 (copy_type (dyn_range_type
), static_target_type
,
2264 &low_bound
, &high_bound
, bias
, &stride
, byte_stride_p
);
2265 static_range_type
->bounds ()->flag_bound_evaluated
= 1;
2266 return static_range_type
;
2269 /* Helper function for resolve_dynamic_array_or_string. This function
2270 resolves the properties for a single array at RANK within a nested array
2271 of arrays structure. The RANK value is greater than or equal to 0, and
2272 starts at it's maximum value and goes down by 1 for each recursive call
2273 to this function. So, for a 3-dimensional array, the first call to this
2274 function has RANK == 2, then we call ourselves recursively with RANK ==
2275 1, than again with RANK == 0, and at that point we should return.
2277 TYPE is updated as the dynamic properties are resolved, and so, should
2278 be a copy of the dynamic type, rather than the original dynamic type
2281 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2282 during the dynamic resolution.
2284 When RESOLVE_P is true then the dynamic properties of TYPE are
2285 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2286 instead we assume the array is not allocated/associated yet. */
2288 static struct type
*
2289 resolve_dynamic_array_or_string_1 (struct type
*type
,
2290 struct property_addr_info
*addr_stack
,
2291 int rank
, bool resolve_p
)
2294 struct type
*elt_type
;
2295 struct type
*range_type
;
2296 struct type
*ary_dim
;
2297 struct dynamic_prop
*prop
;
2298 unsigned int bit_stride
= 0;
2300 /* For dynamic type resolution strings can be treated like arrays of
2302 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2303 || type
->code () == TYPE_CODE_STRING
);
2305 /* As the rank is a zero based count we expect this to never be
2307 gdb_assert (rank
>= 0);
2309 /* Resolve the allocated and associated properties before doing anything
2310 else. If an array is not allocated or not associated then (at least
2311 for Fortran) there is no guarantee that the data to define the upper
2312 bound, lower bound, or stride will be correct. If RESOLVE_P is
2313 already false at this point then this is not the first dimension of
2314 the array and a more outer dimension has already marked this array as
2315 not allocated/associated, as such we just ignore this property. This
2316 is fine as GDB only checks the allocated/associated on the outer most
2317 dimension of the array. */
2318 prop
= TYPE_ALLOCATED_PROP (type
);
2319 if (prop
!= NULL
&& resolve_p
2320 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2322 prop
->set_const_val (value
);
2327 prop
= TYPE_ASSOCIATED_PROP (type
);
2328 if (prop
!= NULL
&& resolve_p
2329 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2331 prop
->set_const_val (value
);
2336 range_type
= check_typedef (type
->index_type ());
2338 = resolve_dynamic_range (range_type
, addr_stack
, rank
, resolve_p
);
2340 ary_dim
= check_typedef (TYPE_TARGET_TYPE (type
));
2341 if (ary_dim
!= NULL
&& ary_dim
->code () == TYPE_CODE_ARRAY
)
2343 ary_dim
= copy_type (ary_dim
);
2344 elt_type
= resolve_dynamic_array_or_string_1 (ary_dim
, addr_stack
,
2345 rank
- 1, resolve_p
);
2348 elt_type
= TYPE_TARGET_TYPE (type
);
2350 prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2351 if (prop
!= NULL
&& resolve_p
)
2353 if (dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2355 type
->remove_dyn_prop (DYN_PROP_BYTE_STRIDE
);
2356 bit_stride
= (unsigned int) (value
* 8);
2360 /* Could be a bug in our code, but it could also happen
2361 if the DWARF info is not correct. Issue a warning,
2362 and assume no byte/bit stride (leave bit_stride = 0). */
2363 warning (_("cannot determine array stride for type %s"),
2364 type
->name () ? type
->name () : "<no name>");
2368 bit_stride
= TYPE_FIELD_BITSIZE (type
, 0);
2370 return create_array_type_with_stride (type
, elt_type
, range_type
, NULL
,
2374 /* Resolve an array or string type with dynamic properties, return a new
2375 type with the dynamic properties resolved to actual values. The
2376 ADDR_STACK represents the location of the object being resolved. */
2378 static struct type
*
2379 resolve_dynamic_array_or_string (struct type
*type
,
2380 struct property_addr_info
*addr_stack
)
2385 /* For dynamic type resolution strings can be treated like arrays of
2387 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2388 || type
->code () == TYPE_CODE_STRING
);
2390 type
= copy_type (type
);
2392 /* Resolve the rank property to get rank value. */
2393 struct dynamic_prop
*prop
= TYPE_RANK_PROP (type
);
2394 if (dwarf2_evaluate_property (prop
, nullptr, addr_stack
, &value
))
2396 prop
->set_const_val (value
);
2401 /* The dynamic property list juggling below was from the original
2402 patch. I don't understand what this is all about, so I've
2403 commented it out for now and added the following error. */
2404 error (_("failed to resolve dynamic array rank"));
2406 else if (type
->code () == TYPE_CODE_STRING
&& rank
!= 1)
2408 /* What would this even mean? A string with a dynamic rank
2410 error (_("unable to handle string with dynamic rank greater than 1"));
2414 /* Arrays with dynamic rank are initially just an array type
2415 with a target type that is the array element.
2417 However, now we know the rank of the array we need to build
2418 the array of arrays structure that GDB expects, that is we
2419 need an array type that has a target which is an array type,
2420 and so on, until eventually, we have the element type at the
2421 end of the chain. Create all the additional array types here
2422 by copying the top level array type. */
2423 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2424 struct type
*rank_type
= type
;
2425 for (int i
= 1; i
< rank
; i
++)
2427 TYPE_TARGET_TYPE (rank_type
) = copy_type (rank_type
);
2428 rank_type
= TYPE_TARGET_TYPE (rank_type
);
2430 TYPE_TARGET_TYPE (rank_type
) = element_type
;
2437 for (struct type
*tmp_type
= check_typedef (TYPE_TARGET_TYPE (type
));
2438 tmp_type
->code () == TYPE_CODE_ARRAY
;
2439 tmp_type
= check_typedef (TYPE_TARGET_TYPE (tmp_type
)))
2443 /* The rank that we calculated above is actually a count of the number of
2444 ranks. However, when we resolve the type of each individual array
2445 rank we should actually use a rank "offset", e.g. an array with a rank
2446 count of 1 (calculated above) will use the rank offset 0 in order to
2447 resolve the details of the first array dimension. As a result, we
2448 reduce the rank by 1 here. */
2451 return resolve_dynamic_array_or_string_1 (type
, addr_stack
, rank
, true);
2454 /* Resolve dynamic bounds of members of the union TYPE to static
2455 bounds. ADDR_STACK is a stack of struct property_addr_info
2456 to be used if needed during the dynamic resolution. */
2458 static struct type
*
2459 resolve_dynamic_union (struct type
*type
,
2460 struct property_addr_info
*addr_stack
)
2462 struct type
*resolved_type
;
2464 unsigned int max_len
= 0;
2466 gdb_assert (type
->code () == TYPE_CODE_UNION
);
2468 resolved_type
= copy_type (type
);
2469 resolved_type
->set_fields
2471 TYPE_ALLOC (resolved_type
,
2472 resolved_type
->num_fields () * sizeof (struct field
)));
2473 memcpy (resolved_type
->fields (),
2475 resolved_type
->num_fields () * sizeof (struct field
));
2476 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2480 if (field_is_static (&type
->field (i
)))
2483 t
= resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2485 resolved_type
->field (i
).set_type (t
);
2487 struct type
*real_type
= check_typedef (t
);
2488 if (TYPE_LENGTH (real_type
) > max_len
)
2489 max_len
= TYPE_LENGTH (real_type
);
2492 TYPE_LENGTH (resolved_type
) = max_len
;
2493 return resolved_type
;
2496 /* See gdbtypes.h. */
2499 variant::matches (ULONGEST value
, bool is_unsigned
) const
2501 for (const discriminant_range
&range
: discriminants
)
2502 if (range
.contains (value
, is_unsigned
))
2508 compute_variant_fields_inner (struct type
*type
,
2509 struct property_addr_info
*addr_stack
,
2510 const variant_part
&part
,
2511 std::vector
<bool> &flags
);
2513 /* A helper function to determine which variant fields will be active.
2514 This handles both the variant's direct fields, and any variant
2515 parts embedded in this variant. TYPE is the type we're examining.
2516 ADDR_STACK holds information about the concrete object. VARIANT is
2517 the current variant to be handled. FLAGS is where the results are
2518 stored -- this function sets the Nth element in FLAGS if the
2519 corresponding field is enabled. ENABLED is whether this variant is
2523 compute_variant_fields_recurse (struct type
*type
,
2524 struct property_addr_info
*addr_stack
,
2525 const variant
&variant
,
2526 std::vector
<bool> &flags
,
2529 for (int field
= variant
.first_field
; field
< variant
.last_field
; ++field
)
2530 flags
[field
] = enabled
;
2532 for (const variant_part
&new_part
: variant
.parts
)
2535 compute_variant_fields_inner (type
, addr_stack
, new_part
, flags
);
2538 for (const auto &sub_variant
: new_part
.variants
)
2539 compute_variant_fields_recurse (type
, addr_stack
, sub_variant
,
2545 /* A helper function to determine which variant fields will be active.
2546 This evaluates the discriminant, decides which variant (if any) is
2547 active, and then updates FLAGS to reflect which fields should be
2548 available. TYPE is the type we're examining. ADDR_STACK holds
2549 information about the concrete object. VARIANT is the current
2550 variant to be handled. FLAGS is where the results are stored --
2551 this function sets the Nth element in FLAGS if the corresponding
2552 field is enabled. */
2555 compute_variant_fields_inner (struct type
*type
,
2556 struct property_addr_info
*addr_stack
,
2557 const variant_part
&part
,
2558 std::vector
<bool> &flags
)
2560 /* Evaluate the discriminant. */
2561 gdb::optional
<ULONGEST
> discr_value
;
2562 if (part
.discriminant_index
!= -1)
2564 int idx
= part
.discriminant_index
;
2566 if (type
->field (idx
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2567 error (_("Cannot determine struct field location"
2568 " (invalid location kind)"));
2570 if (addr_stack
->valaddr
.data () != NULL
)
2571 discr_value
= unpack_field_as_long (type
, addr_stack
->valaddr
.data (),
2575 CORE_ADDR addr
= (addr_stack
->addr
2576 + (type
->field (idx
).loc_bitpos ()
2577 / TARGET_CHAR_BIT
));
2579 LONGEST bitsize
= TYPE_FIELD_BITSIZE (type
, idx
);
2580 LONGEST size
= bitsize
/ 8;
2582 size
= TYPE_LENGTH (type
->field (idx
).type ());
2584 gdb_byte bits
[sizeof (ULONGEST
)];
2585 read_memory (addr
, bits
, size
);
2587 LONGEST bitpos
= (type
->field (idx
).loc_bitpos ()
2590 discr_value
= unpack_bits_as_long (type
->field (idx
).type (),
2591 bits
, bitpos
, bitsize
);
2595 /* Go through each variant and see which applies. */
2596 const variant
*default_variant
= nullptr;
2597 const variant
*applied_variant
= nullptr;
2598 for (const auto &variant
: part
.variants
)
2600 if (variant
.is_default ())
2601 default_variant
= &variant
;
2602 else if (discr_value
.has_value ()
2603 && variant
.matches (*discr_value
, part
.is_unsigned
))
2605 applied_variant
= &variant
;
2609 if (applied_variant
== nullptr)
2610 applied_variant
= default_variant
;
2612 for (const auto &variant
: part
.variants
)
2613 compute_variant_fields_recurse (type
, addr_stack
, variant
,
2614 flags
, applied_variant
== &variant
);
2617 /* Determine which variant fields are available in TYPE. The enabled
2618 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2619 about the concrete object. PARTS describes the top-level variant
2620 parts for this type. */
2623 compute_variant_fields (struct type
*type
,
2624 struct type
*resolved_type
,
2625 struct property_addr_info
*addr_stack
,
2626 const gdb::array_view
<variant_part
> &parts
)
2628 /* Assume all fields are included by default. */
2629 std::vector
<bool> flags (resolved_type
->num_fields (), true);
2631 /* Now disable fields based on the variants that control them. */
2632 for (const auto &part
: parts
)
2633 compute_variant_fields_inner (type
, addr_stack
, part
, flags
);
2635 resolved_type
->set_num_fields
2636 (std::count (flags
.begin (), flags
.end (), true));
2637 resolved_type
->set_fields
2639 TYPE_ALLOC (resolved_type
,
2640 resolved_type
->num_fields () * sizeof (struct field
)));
2643 for (int i
= 0; i
< type
->num_fields (); ++i
)
2648 resolved_type
->field (out
) = type
->field (i
);
2653 /* Resolve dynamic bounds of members of the struct TYPE to static
2654 bounds. ADDR_STACK is a stack of struct property_addr_info to
2655 be used if needed during the dynamic resolution. */
2657 static struct type
*
2658 resolve_dynamic_struct (struct type
*type
,
2659 struct property_addr_info
*addr_stack
)
2661 struct type
*resolved_type
;
2663 unsigned resolved_type_bit_length
= 0;
2665 gdb_assert (type
->code () == TYPE_CODE_STRUCT
);
2667 resolved_type
= copy_type (type
);
2669 dynamic_prop
*variant_prop
= resolved_type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2670 if (variant_prop
!= nullptr && variant_prop
->kind () == PROP_VARIANT_PARTS
)
2672 compute_variant_fields (type
, resolved_type
, addr_stack
,
2673 *variant_prop
->variant_parts ());
2674 /* We want to leave the property attached, so that the Rust code
2675 can tell whether the type was originally an enum. */
2676 variant_prop
->set_original_type (type
);
2680 resolved_type
->set_fields
2682 TYPE_ALLOC (resolved_type
,
2683 resolved_type
->num_fields () * sizeof (struct field
)));
2684 if (type
->num_fields () > 0)
2685 memcpy (resolved_type
->fields (),
2687 resolved_type
->num_fields () * sizeof (struct field
));
2690 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2692 unsigned new_bit_length
;
2693 struct property_addr_info pinfo
;
2695 if (field_is_static (&resolved_type
->field (i
)))
2698 if (resolved_type
->field (i
).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK
)
2700 struct dwarf2_property_baton baton
;
2702 = lookup_pointer_type (resolved_type
->field (i
).type ());
2703 baton
.locexpr
= *resolved_type
->field (i
).loc_dwarf_block ();
2705 struct dynamic_prop prop
;
2706 prop
.set_locexpr (&baton
);
2709 if (dwarf2_evaluate_property (&prop
, nullptr, addr_stack
, &addr
,
2710 {addr_stack
->addr
}))
2711 resolved_type
->field (i
).set_loc_bitpos
2712 (TARGET_CHAR_BIT
* (addr
- addr_stack
->addr
));
2715 /* As we know this field is not a static field, the field's
2716 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2717 this is the case, but only trigger a simple error rather
2718 than an internal error if that fails. While failing
2719 that verification indicates a bug in our code, the error
2720 is not severe enough to suggest to the user he stops
2721 his debugging session because of it. */
2722 if (resolved_type
->field (i
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2723 error (_("Cannot determine struct field location"
2724 " (invalid location kind)"));
2726 pinfo
.type
= check_typedef (resolved_type
->field (i
).type ());
2727 size_t offset
= resolved_type
->field (i
).loc_bitpos () / TARGET_CHAR_BIT
;
2728 pinfo
.valaddr
= addr_stack
->valaddr
;
2729 if (!pinfo
.valaddr
.empty ())
2730 pinfo
.valaddr
= pinfo
.valaddr
.slice (offset
);
2731 pinfo
.addr
= addr_stack
->addr
+ offset
;
2732 pinfo
.next
= addr_stack
;
2734 resolved_type
->field (i
).set_type
2735 (resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2737 gdb_assert (resolved_type
->field (i
).loc_kind ()
2738 == FIELD_LOC_KIND_BITPOS
);
2740 new_bit_length
= resolved_type
->field (i
).loc_bitpos ();
2741 if (TYPE_FIELD_BITSIZE (resolved_type
, i
) != 0)
2742 new_bit_length
+= TYPE_FIELD_BITSIZE (resolved_type
, i
);
2745 struct type
*real_type
2746 = check_typedef (resolved_type
->field (i
).type ());
2748 new_bit_length
+= (TYPE_LENGTH (real_type
) * TARGET_CHAR_BIT
);
2751 /* Normally, we would use the position and size of the last field
2752 to determine the size of the enclosing structure. But GCC seems
2753 to be encoding the position of some fields incorrectly when
2754 the struct contains a dynamic field that is not placed last.
2755 So we compute the struct size based on the field that has
2756 the highest position + size - probably the best we can do. */
2757 if (new_bit_length
> resolved_type_bit_length
)
2758 resolved_type_bit_length
= new_bit_length
;
2761 /* The length of a type won't change for fortran, but it does for C and Ada.
2762 For fortran the size of dynamic fields might change over time but not the
2763 type length of the structure. If we adapt it, we run into problems
2764 when calculating the element offset for arrays of structs. */
2765 if (current_language
->la_language
!= language_fortran
)
2766 TYPE_LENGTH (resolved_type
)
2767 = (resolved_type_bit_length
+ TARGET_CHAR_BIT
- 1) / TARGET_CHAR_BIT
;
2769 /* The Ada language uses this field as a cache for static fixed types: reset
2770 it as RESOLVED_TYPE must have its own static fixed type. */
2771 TYPE_TARGET_TYPE (resolved_type
) = NULL
;
2773 return resolved_type
;
2776 /* Worker for resolved_dynamic_type. */
2778 static struct type
*
2779 resolve_dynamic_type_internal (struct type
*type
,
2780 struct property_addr_info
*addr_stack
,
2783 struct type
*real_type
= check_typedef (type
);
2784 struct type
*resolved_type
= nullptr;
2785 struct dynamic_prop
*prop
;
2788 if (!is_dynamic_type_internal (real_type
, top_level
))
2791 gdb::optional
<CORE_ADDR
> type_length
;
2792 prop
= TYPE_DYNAMIC_LENGTH (type
);
2794 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2795 type_length
= value
;
2797 if (type
->code () == TYPE_CODE_TYPEDEF
)
2799 resolved_type
= copy_type (type
);
2800 TYPE_TARGET_TYPE (resolved_type
)
2801 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
), addr_stack
,
2806 /* Before trying to resolve TYPE, make sure it is not a stub. */
2809 switch (type
->code ())
2813 struct property_addr_info pinfo
;
2815 pinfo
.type
= check_typedef (TYPE_TARGET_TYPE (type
));
2817 if (addr_stack
->valaddr
.data () != NULL
)
2818 pinfo
.addr
= extract_typed_address (addr_stack
->valaddr
.data (),
2821 pinfo
.addr
= read_memory_typed_address (addr_stack
->addr
, type
);
2822 pinfo
.next
= addr_stack
;
2824 resolved_type
= copy_type (type
);
2825 TYPE_TARGET_TYPE (resolved_type
)
2826 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
),
2831 case TYPE_CODE_STRING
:
2832 /* Strings are very much like an array of characters, and can be
2833 treated as one here. */
2834 case TYPE_CODE_ARRAY
:
2835 resolved_type
= resolve_dynamic_array_or_string (type
, addr_stack
);
2838 case TYPE_CODE_RANGE
:
2839 /* Pass 0 for the rank value here, which indicates this is a
2840 range for the first rank of an array. The assumption is that
2841 this rank value is not actually required for the resolution of
2842 the dynamic range, otherwise, we'd be resolving this range
2843 within the context of a dynamic array. */
2844 resolved_type
= resolve_dynamic_range (type
, addr_stack
, 0);
2847 case TYPE_CODE_UNION
:
2848 resolved_type
= resolve_dynamic_union (type
, addr_stack
);
2851 case TYPE_CODE_STRUCT
:
2852 resolved_type
= resolve_dynamic_struct (type
, addr_stack
);
2857 if (resolved_type
== nullptr)
2860 if (type_length
.has_value ())
2862 TYPE_LENGTH (resolved_type
) = *type_length
;
2863 resolved_type
->remove_dyn_prop (DYN_PROP_BYTE_SIZE
);
2866 /* Resolve data_location attribute. */
2867 prop
= TYPE_DATA_LOCATION (resolved_type
);
2869 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2871 /* Start of Fortran hack. See comment in f-lang.h for what is going
2873 if (current_language
->la_language
== language_fortran
2874 && resolved_type
->code () == TYPE_CODE_ARRAY
)
2875 value
= fortran_adjust_dynamic_array_base_address_hack (resolved_type
,
2877 /* End of Fortran hack. */
2878 prop
->set_const_val (value
);
2881 return resolved_type
;
2884 /* See gdbtypes.h */
2887 resolve_dynamic_type (struct type
*type
,
2888 gdb::array_view
<const gdb_byte
> valaddr
,
2891 struct property_addr_info pinfo
2892 = {check_typedef (type
), valaddr
, addr
, NULL
};
2894 return resolve_dynamic_type_internal (type
, &pinfo
, 1);
2897 /* See gdbtypes.h */
2900 type::dyn_prop (dynamic_prop_node_kind prop_kind
) const
2902 dynamic_prop_list
*node
= this->main_type
->dyn_prop_list
;
2904 while (node
!= NULL
)
2906 if (node
->prop_kind
== prop_kind
)
2913 /* See gdbtypes.h */
2916 type::add_dyn_prop (dynamic_prop_node_kind prop_kind
, dynamic_prop prop
)
2918 struct dynamic_prop_list
*temp
;
2920 gdb_assert (this->is_objfile_owned ());
2922 temp
= XOBNEW (&this->objfile_owner ()->objfile_obstack
,
2923 struct dynamic_prop_list
);
2924 temp
->prop_kind
= prop_kind
;
2926 temp
->next
= this->main_type
->dyn_prop_list
;
2928 this->main_type
->dyn_prop_list
= temp
;
2931 /* See gdbtypes.h. */
2934 type::remove_dyn_prop (dynamic_prop_node_kind kind
)
2936 struct dynamic_prop_list
*prev_node
, *curr_node
;
2938 curr_node
= this->main_type
->dyn_prop_list
;
2941 while (NULL
!= curr_node
)
2943 if (curr_node
->prop_kind
== kind
)
2945 /* Update the linked list but don't free anything.
2946 The property was allocated on objstack and it is not known
2947 if we are on top of it. Nevertheless, everything is released
2948 when the complete objstack is freed. */
2949 if (NULL
== prev_node
)
2950 this->main_type
->dyn_prop_list
= curr_node
->next
;
2952 prev_node
->next
= curr_node
->next
;
2957 prev_node
= curr_node
;
2958 curr_node
= curr_node
->next
;
2962 /* Find the real type of TYPE. This function returns the real type,
2963 after removing all layers of typedefs, and completing opaque or stub
2964 types. Completion changes the TYPE argument, but stripping of
2967 Instance flags (e.g. const/volatile) are preserved as typedefs are
2968 stripped. If necessary a new qualified form of the underlying type
2971 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
2972 not been computed and we're either in the middle of reading symbols, or
2973 there was no name for the typedef in the debug info.
2975 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2976 QUITs in the symbol reading code can also throw.
2977 Thus this function can throw an exception.
2979 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2982 If this is a stubbed struct (i.e. declared as struct foo *), see if
2983 we can find a full definition in some other file. If so, copy this
2984 definition, so we can use it in future. There used to be a comment
2985 (but not any code) that if we don't find a full definition, we'd
2986 set a flag so we don't spend time in the future checking the same
2987 type. That would be a mistake, though--we might load in more
2988 symbols which contain a full definition for the type. */
2991 check_typedef (struct type
*type
)
2993 struct type
*orig_type
= type
;
2997 /* While we're removing typedefs, we don't want to lose qualifiers.
2998 E.g., const/volatile. */
2999 type_instance_flags instance_flags
= type
->instance_flags ();
3001 while (type
->code () == TYPE_CODE_TYPEDEF
)
3003 if (!TYPE_TARGET_TYPE (type
))
3008 /* It is dangerous to call lookup_symbol if we are currently
3009 reading a symtab. Infinite recursion is one danger. */
3010 if (currently_reading_symtab
)
3011 return make_qualified_type (type
, instance_flags
, NULL
);
3013 name
= type
->name ();
3014 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
3015 VAR_DOMAIN as appropriate? */
3018 stub_noname_complaint ();
3019 return make_qualified_type (type
, instance_flags
, NULL
);
3021 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3023 TYPE_TARGET_TYPE (type
) = sym
->type ();
3024 else /* TYPE_CODE_UNDEF */
3025 TYPE_TARGET_TYPE (type
) = alloc_type_arch (type
->arch ());
3027 type
= TYPE_TARGET_TYPE (type
);
3029 /* Preserve the instance flags as we traverse down the typedef chain.
3031 Handling address spaces/classes is nasty, what do we do if there's a
3033 E.g., what if an outer typedef marks the type as class_1 and an inner
3034 typedef marks the type as class_2?
3035 This is the wrong place to do such error checking. We leave it to
3036 the code that created the typedef in the first place to flag the
3037 error. We just pick the outer address space (akin to letting the
3038 outer cast in a chain of casting win), instead of assuming
3039 "it can't happen". */
3041 const type_instance_flags ALL_SPACES
3042 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3043 | TYPE_INSTANCE_FLAG_DATA_SPACE
);
3044 const type_instance_flags ALL_CLASSES
3045 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
;
3047 type_instance_flags new_instance_flags
= type
->instance_flags ();
3049 /* Treat code vs data spaces and address classes separately. */
3050 if ((instance_flags
& ALL_SPACES
) != 0)
3051 new_instance_flags
&= ~ALL_SPACES
;
3052 if ((instance_flags
& ALL_CLASSES
) != 0)
3053 new_instance_flags
&= ~ALL_CLASSES
;
3055 instance_flags
|= new_instance_flags
;
3059 /* If this is a struct/class/union with no fields, then check
3060 whether a full definition exists somewhere else. This is for
3061 systems where a type definition with no fields is issued for such
3062 types, instead of identifying them as stub types in the first
3065 if (TYPE_IS_OPAQUE (type
)
3066 && opaque_type_resolution
3067 && !currently_reading_symtab
)
3069 const char *name
= type
->name ();
3070 struct type
*newtype
;
3074 stub_noname_complaint ();
3075 return make_qualified_type (type
, instance_flags
, NULL
);
3077 newtype
= lookup_transparent_type (name
);
3081 /* If the resolved type and the stub are in the same
3082 objfile, then replace the stub type with the real deal.
3083 But if they're in separate objfiles, leave the stub
3084 alone; we'll just look up the transparent type every time
3085 we call check_typedef. We can't create pointers between
3086 types allocated to different objfiles, since they may
3087 have different lifetimes. Trying to copy NEWTYPE over to
3088 TYPE's objfile is pointless, too, since you'll have to
3089 move over any other types NEWTYPE refers to, which could
3090 be an unbounded amount of stuff. */
3091 if (newtype
->objfile_owner () == type
->objfile_owner ())
3092 type
= make_qualified_type (newtype
, type
->instance_flags (), type
);
3097 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3099 else if (type
->is_stub () && !currently_reading_symtab
)
3101 const char *name
= type
->name ();
3102 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3108 stub_noname_complaint ();
3109 return make_qualified_type (type
, instance_flags
, NULL
);
3111 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3114 /* Same as above for opaque types, we can replace the stub
3115 with the complete type only if they are in the same
3117 if (sym
->type ()->objfile_owner () == type
->objfile_owner ())
3118 type
= make_qualified_type (sym
->type (),
3119 type
->instance_flags (), type
);
3121 type
= sym
->type ();
3125 if (type
->target_is_stub ())
3127 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
3129 if (target_type
->is_stub () || target_type
->target_is_stub ())
3131 /* Nothing we can do. */
3133 else if (type
->code () == TYPE_CODE_RANGE
)
3135 TYPE_LENGTH (type
) = TYPE_LENGTH (target_type
);
3136 type
->set_target_is_stub (false);
3138 else if (type
->code () == TYPE_CODE_ARRAY
3139 && update_static_array_size (type
))
3140 type
->set_target_is_stub (false);
3143 type
= make_qualified_type (type
, instance_flags
, NULL
);
3145 /* Cache TYPE_LENGTH for future use. */
3146 TYPE_LENGTH (orig_type
) = TYPE_LENGTH (type
);
3151 /* Parse a type expression in the string [P..P+LENGTH). If an error
3152 occurs, silently return a void type. */
3154 static struct type
*
3155 safe_parse_type (struct gdbarch
*gdbarch
, const char *p
, int length
)
3157 struct ui_file
*saved_gdb_stderr
;
3158 struct type
*type
= NULL
; /* Initialize to keep gcc happy. */
3160 /* Suppress error messages. */
3161 saved_gdb_stderr
= gdb_stderr
;
3162 gdb_stderr
= &null_stream
;
3164 /* Call parse_and_eval_type() without fear of longjmp()s. */
3167 type
= parse_and_eval_type (p
, length
);
3169 catch (const gdb_exception_error
&except
)
3171 type
= builtin_type (gdbarch
)->builtin_void
;
3174 /* Stop suppressing error messages. */
3175 gdb_stderr
= saved_gdb_stderr
;
3180 /* Ugly hack to convert method stubs into method types.
3182 He ain't kiddin'. This demangles the name of the method into a
3183 string including argument types, parses out each argument type,
3184 generates a string casting a zero to that type, evaluates the
3185 string, and stuffs the resulting type into an argtype vector!!!
3186 Then it knows the type of the whole function (including argument
3187 types for overloading), which info used to be in the stab's but was
3188 removed to hack back the space required for them. */
3191 check_stub_method (struct type
*type
, int method_id
, int signature_id
)
3193 struct gdbarch
*gdbarch
= type
->arch ();
3195 char *mangled_name
= gdb_mangle_name (type
, method_id
, signature_id
);
3196 gdb::unique_xmalloc_ptr
<char> demangled_name
3197 = gdb_demangle (mangled_name
, DMGL_PARAMS
| DMGL_ANSI
);
3198 char *argtypetext
, *p
;
3199 int depth
= 0, argcount
= 1;
3200 struct field
*argtypes
;
3203 /* Make sure we got back a function string that we can use. */
3205 p
= strchr (demangled_name
.get (), '(');
3209 if (demangled_name
== NULL
|| p
== NULL
)
3210 error (_("Internal: Cannot demangle mangled name `%s'."),
3213 /* Now, read in the parameters that define this type. */
3218 if (*p
== '(' || *p
== '<')
3222 else if (*p
== ')' || *p
== '>')
3226 else if (*p
== ',' && depth
== 0)
3234 /* If we read one argument and it was ``void'', don't count it. */
3235 if (startswith (argtypetext
, "(void)"))
3238 /* We need one extra slot, for the THIS pointer. */
3240 argtypes
= (struct field
*)
3241 TYPE_ALLOC (type
, (argcount
+ 1) * sizeof (struct field
));
3244 /* Add THIS pointer for non-static methods. */
3245 f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3246 if (TYPE_FN_FIELD_STATIC_P (f
, signature_id
))
3250 argtypes
[0].set_type (lookup_pointer_type (type
));
3254 if (*p
!= ')') /* () means no args, skip while. */
3259 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
3261 /* Avoid parsing of ellipsis, they will be handled below.
3262 Also avoid ``void'' as above. */
3263 if (strncmp (argtypetext
, "...", p
- argtypetext
) != 0
3264 && strncmp (argtypetext
, "void", p
- argtypetext
) != 0)
3266 argtypes
[argcount
].set_type
3267 (safe_parse_type (gdbarch
, argtypetext
, p
- argtypetext
));
3270 argtypetext
= p
+ 1;
3273 if (*p
== '(' || *p
== '<')
3277 else if (*p
== ')' || *p
== '>')
3286 TYPE_FN_FIELD_PHYSNAME (f
, signature_id
) = mangled_name
;
3288 /* Now update the old "stub" type into a real type. */
3289 mtype
= TYPE_FN_FIELD_TYPE (f
, signature_id
);
3290 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3291 We want a method (TYPE_CODE_METHOD). */
3292 smash_to_method_type (mtype
, type
, TYPE_TARGET_TYPE (mtype
),
3293 argtypes
, argcount
, p
[-2] == '.');
3294 mtype
->set_is_stub (false);
3295 TYPE_FN_FIELD_STUB (f
, signature_id
) = 0;
3298 /* This is the external interface to check_stub_method, above. This
3299 function unstubs all of the signatures for TYPE's METHOD_ID method
3300 name. After calling this function TYPE_FN_FIELD_STUB will be
3301 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3304 This function unfortunately can not die until stabs do. */
3307 check_stub_method_group (struct type
*type
, int method_id
)
3309 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, method_id
);
3310 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3312 for (int j
= 0; j
< len
; j
++)
3314 if (TYPE_FN_FIELD_STUB (f
, j
))
3315 check_stub_method (type
, method_id
, j
);
3319 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3320 const struct cplus_struct_type cplus_struct_default
= { };
3323 allocate_cplus_struct_type (struct type
*type
)
3325 if (HAVE_CPLUS_STRUCT (type
))
3326 /* Structure was already allocated. Nothing more to do. */
3329 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_CPLUS_STUFF
;
3330 TYPE_RAW_CPLUS_SPECIFIC (type
) = (struct cplus_struct_type
*)
3331 TYPE_ALLOC (type
, sizeof (struct cplus_struct_type
));
3332 *(TYPE_RAW_CPLUS_SPECIFIC (type
)) = cplus_struct_default
;
3333 set_type_vptr_fieldno (type
, -1);
3336 const struct gnat_aux_type gnat_aux_default
=
3339 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3340 and allocate the associated gnat-specific data. The gnat-specific
3341 data is also initialized to gnat_aux_default. */
3344 allocate_gnat_aux_type (struct type
*type
)
3346 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_GNAT_STUFF
;
3347 TYPE_GNAT_SPECIFIC (type
) = (struct gnat_aux_type
*)
3348 TYPE_ALLOC (type
, sizeof (struct gnat_aux_type
));
3349 *(TYPE_GNAT_SPECIFIC (type
)) = gnat_aux_default
;
3352 /* Helper function to initialize a newly allocated type. Set type code
3353 to CODE and initialize the type-specific fields accordingly. */
3356 set_type_code (struct type
*type
, enum type_code code
)
3358 type
->set_code (code
);
3362 case TYPE_CODE_STRUCT
:
3363 case TYPE_CODE_UNION
:
3364 case TYPE_CODE_NAMESPACE
:
3365 INIT_CPLUS_SPECIFIC (type
);
3368 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_FLOATFORMAT
;
3370 case TYPE_CODE_FUNC
:
3371 INIT_FUNC_SPECIFIC (type
);
3373 case TYPE_CODE_FIXED_POINT
:
3374 INIT_FIXED_POINT_SPECIFIC (type
);
3379 /* Helper function to verify floating-point format and size.
3380 BIT is the type size in bits; if BIT equals -1, the size is
3381 determined by the floatformat. Returns size to be used. */
3384 verify_floatformat (int bit
, const struct floatformat
*floatformat
)
3386 gdb_assert (floatformat
!= NULL
);
3389 bit
= floatformat
->totalsize
;
3391 gdb_assert (bit
>= 0);
3392 gdb_assert (bit
>= floatformat
->totalsize
);
3397 /* Return the floating-point format for a floating-point variable of
3400 const struct floatformat
*
3401 floatformat_from_type (const struct type
*type
)
3403 gdb_assert (type
->code () == TYPE_CODE_FLT
);
3404 gdb_assert (TYPE_FLOATFORMAT (type
));
3405 return TYPE_FLOATFORMAT (type
);
3408 /* Helper function to initialize the standard scalar types.
3410 If NAME is non-NULL, then it is used to initialize the type name.
3411 Note that NAME is not copied; it is required to have a lifetime at
3412 least as long as OBJFILE. */
3415 init_type (struct objfile
*objfile
, enum type_code code
, int bit
,
3420 type
= alloc_type (objfile
);
3421 set_type_code (type
, code
);
3422 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
3423 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
3424 type
->set_name (name
);
3429 /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
3430 to use with variables that have no debug info. NAME is the type
3433 static struct type
*
3434 init_nodebug_var_type (struct objfile
*objfile
, const char *name
)
3436 return init_type (objfile
, TYPE_CODE_ERROR
, 0, name
);
3439 /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
3440 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3441 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3444 init_integer_type (struct objfile
*objfile
,
3445 int bit
, int unsigned_p
, const char *name
)
3449 t
= init_type (objfile
, TYPE_CODE_INT
, bit
, name
);
3451 t
->set_is_unsigned (true);
3453 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3454 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3455 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3460 /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
3461 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3462 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3465 init_character_type (struct objfile
*objfile
,
3466 int bit
, int unsigned_p
, const char *name
)
3470 t
= init_type (objfile
, TYPE_CODE_CHAR
, bit
, name
);
3472 t
->set_is_unsigned (true);
3477 /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
3478 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3479 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3482 init_boolean_type (struct objfile
*objfile
,
3483 int bit
, int unsigned_p
, const char *name
)
3487 t
= init_type (objfile
, TYPE_CODE_BOOL
, bit
, name
);
3489 t
->set_is_unsigned (true);
3491 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3492 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3493 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3498 /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
3499 BIT is the type size in bits; if BIT equals -1, the size is
3500 determined by the floatformat. NAME is the type name. Set the
3501 TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
3502 to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
3503 order of the objfile's architecture is used. */
3506 init_float_type (struct objfile
*objfile
,
3507 int bit
, const char *name
,
3508 const struct floatformat
**floatformats
,
3509 enum bfd_endian byte_order
)
3511 if (byte_order
== BFD_ENDIAN_UNKNOWN
)
3513 struct gdbarch
*gdbarch
= objfile
->arch ();
3514 byte_order
= gdbarch_byte_order (gdbarch
);
3516 const struct floatformat
*fmt
= floatformats
[byte_order
];
3519 bit
= verify_floatformat (bit
, fmt
);
3520 t
= init_type (objfile
, TYPE_CODE_FLT
, bit
, name
);
3521 TYPE_FLOATFORMAT (t
) = fmt
;
3526 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
3527 BIT is the type size in bits. NAME is the type name. */
3530 init_decfloat_type (struct objfile
*objfile
, int bit
, const char *name
)
3534 t
= init_type (objfile
, TYPE_CODE_DECFLOAT
, bit
, name
);
3538 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3541 can_create_complex_type (struct type
*target_type
)
3543 return (target_type
->code () == TYPE_CODE_INT
3544 || target_type
->code () == TYPE_CODE_FLT
);
3547 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3548 name. TARGET_TYPE is the component type. */
3551 init_complex_type (const char *name
, struct type
*target_type
)
3555 gdb_assert (can_create_complex_type (target_type
));
3557 if (TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
== nullptr)
3559 if (name
== nullptr && target_type
->name () != nullptr)
3562 = (char *) TYPE_ALLOC (target_type
,
3563 strlen (target_type
->name ())
3564 + strlen ("_Complex ") + 1);
3565 strcpy (new_name
, "_Complex ");
3566 strcat (new_name
, target_type
->name ());
3570 t
= alloc_type_copy (target_type
);
3571 set_type_code (t
, TYPE_CODE_COMPLEX
);
3572 TYPE_LENGTH (t
) = 2 * TYPE_LENGTH (target_type
);
3575 TYPE_TARGET_TYPE (t
) = target_type
;
3576 TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
= t
;
3579 return TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
;
3582 /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
3583 BIT is the pointer type size in bits. NAME is the type name.
3584 TARGET_TYPE is the pointer target type. Always sets the pointer type's
3585 TYPE_UNSIGNED flag. */
3588 init_pointer_type (struct objfile
*objfile
,
3589 int bit
, const char *name
, struct type
*target_type
)
3593 t
= init_type (objfile
, TYPE_CODE_PTR
, bit
, name
);
3594 TYPE_TARGET_TYPE (t
) = target_type
;
3595 t
->set_is_unsigned (true);
3599 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3600 BIT is the pointer type size in bits.
3601 UNSIGNED_P should be nonzero if the type is unsigned.
3602 NAME is the type name. */
3605 init_fixed_point_type (struct objfile
*objfile
,
3606 int bit
, int unsigned_p
, const char *name
)
3610 t
= init_type (objfile
, TYPE_CODE_FIXED_POINT
, bit
, name
);
3612 t
->set_is_unsigned (true);
3617 /* See gdbtypes.h. */
3620 type_raw_align (struct type
*type
)
3622 if (type
->align_log2
!= 0)
3623 return 1 << (type
->align_log2
- 1);
3627 /* See gdbtypes.h. */
3630 type_align (struct type
*type
)
3632 /* Check alignment provided in the debug information. */
3633 unsigned raw_align
= type_raw_align (type
);
3637 /* Allow the architecture to provide an alignment. */
3638 ULONGEST align
= gdbarch_type_align (type
->arch (), type
);
3642 switch (type
->code ())
3645 case TYPE_CODE_FUNC
:
3646 case TYPE_CODE_FLAGS
:
3648 case TYPE_CODE_RANGE
:
3650 case TYPE_CODE_ENUM
:
3652 case TYPE_CODE_RVALUE_REF
:
3653 case TYPE_CODE_CHAR
:
3654 case TYPE_CODE_BOOL
:
3655 case TYPE_CODE_DECFLOAT
:
3656 case TYPE_CODE_METHODPTR
:
3657 case TYPE_CODE_MEMBERPTR
:
3658 align
= type_length_units (check_typedef (type
));
3661 case TYPE_CODE_ARRAY
:
3662 case TYPE_CODE_COMPLEX
:
3663 case TYPE_CODE_TYPEDEF
:
3664 align
= type_align (TYPE_TARGET_TYPE (type
));
3667 case TYPE_CODE_STRUCT
:
3668 case TYPE_CODE_UNION
:
3670 int number_of_non_static_fields
= 0;
3671 for (unsigned i
= 0; i
< type
->num_fields (); ++i
)
3673 if (!field_is_static (&type
->field (i
)))
3675 number_of_non_static_fields
++;
3676 ULONGEST f_align
= type_align (type
->field (i
).type ());
3679 /* Don't pretend we know something we don't. */
3683 if (f_align
> align
)
3687 /* A struct with no fields, or with only static fields has an
3689 if (number_of_non_static_fields
== 0)
3695 case TYPE_CODE_STRING
:
3696 /* Not sure what to do here, and these can't appear in C or C++
3700 case TYPE_CODE_VOID
:
3704 case TYPE_CODE_ERROR
:
3705 case TYPE_CODE_METHOD
:
3710 if ((align
& (align
- 1)) != 0)
3712 /* Not a power of 2, so pass. */
3719 /* See gdbtypes.h. */
3722 set_type_align (struct type
*type
, ULONGEST align
)
3724 /* Must be a power of 2. Zero is ok. */
3725 gdb_assert ((align
& (align
- 1)) == 0);
3727 unsigned result
= 0;
3734 if (result
>= (1 << TYPE_ALIGN_BITS
))
3737 type
->align_log2
= result
;
3742 /* Queries on types. */
3745 can_dereference (struct type
*t
)
3747 /* FIXME: Should we return true for references as well as
3749 t
= check_typedef (t
);
3752 && t
->code () == TYPE_CODE_PTR
3753 && TYPE_TARGET_TYPE (t
)->code () != TYPE_CODE_VOID
);
3757 is_integral_type (struct type
*t
)
3759 t
= check_typedef (t
);
3762 && !is_fixed_point_type (t
)
3763 && ((t
->code () == TYPE_CODE_INT
)
3764 || (t
->code () == TYPE_CODE_ENUM
)
3765 || (t
->code () == TYPE_CODE_FLAGS
)
3766 || (t
->code () == TYPE_CODE_CHAR
)
3767 || (t
->code () == TYPE_CODE_RANGE
)
3768 || (t
->code () == TYPE_CODE_BOOL
)));
3772 is_floating_type (struct type
*t
)
3774 t
= check_typedef (t
);
3777 && ((t
->code () == TYPE_CODE_FLT
)
3778 || (t
->code () == TYPE_CODE_DECFLOAT
)));
3781 /* Return true if TYPE is scalar. */
3784 is_scalar_type (struct type
*type
)
3786 type
= check_typedef (type
);
3788 if (is_fixed_point_type (type
))
3789 return 0; /* Implemented as a scalar, but more like a floating point. */
3791 switch (type
->code ())
3793 case TYPE_CODE_ARRAY
:
3794 case TYPE_CODE_STRUCT
:
3795 case TYPE_CODE_UNION
:
3797 case TYPE_CODE_STRING
:
3804 /* Return true if T is scalar, or a composite type which in practice has
3805 the memory layout of a scalar type. E.g., an array or struct with only
3806 one scalar element inside it, or a union with only scalar elements. */
3809 is_scalar_type_recursive (struct type
*t
)
3811 t
= check_typedef (t
);
3813 if (is_scalar_type (t
))
3815 /* Are we dealing with an array or string of known dimensions? */
3816 else if ((t
->code () == TYPE_CODE_ARRAY
3817 || t
->code () == TYPE_CODE_STRING
) && t
->num_fields () == 1
3818 && t
->index_type ()->code () == TYPE_CODE_RANGE
)
3820 LONGEST low_bound
, high_bound
;
3821 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (t
));
3823 if (get_discrete_bounds (t
->index_type (), &low_bound
, &high_bound
))
3824 return (high_bound
== low_bound
3825 && is_scalar_type_recursive (elt_type
));
3829 /* Are we dealing with a struct with one element? */
3830 else if (t
->code () == TYPE_CODE_STRUCT
&& t
->num_fields () == 1)
3831 return is_scalar_type_recursive (t
->field (0).type ());
3832 else if (t
->code () == TYPE_CODE_UNION
)
3834 int i
, n
= t
->num_fields ();
3836 /* If all elements of the union are scalar, then the union is scalar. */
3837 for (i
= 0; i
< n
; i
++)
3838 if (!is_scalar_type_recursive (t
->field (i
).type ()))
3847 /* Return true is T is a class or a union. False otherwise. */
3850 class_or_union_p (const struct type
*t
)
3852 return (t
->code () == TYPE_CODE_STRUCT
3853 || t
->code () == TYPE_CODE_UNION
);
3856 /* A helper function which returns true if types A and B represent the
3857 "same" class type. This is true if the types have the same main
3858 type, or the same name. */
3861 class_types_same_p (const struct type
*a
, const struct type
*b
)
3863 return (TYPE_MAIN_TYPE (a
) == TYPE_MAIN_TYPE (b
)
3864 || (a
->name () && b
->name ()
3865 && !strcmp (a
->name (), b
->name ())));
3868 /* If BASE is an ancestor of DCLASS return the distance between them.
3869 otherwise return -1;
3873 class B: public A {};
3874 class C: public B {};
3877 distance_to_ancestor (A, A, 0) = 0
3878 distance_to_ancestor (A, B, 0) = 1
3879 distance_to_ancestor (A, C, 0) = 2
3880 distance_to_ancestor (A, D, 0) = 3
3882 If PUBLIC is 1 then only public ancestors are considered,
3883 and the function returns the distance only if BASE is a public ancestor
3887 distance_to_ancestor (A, D, 1) = -1. */
3890 distance_to_ancestor (struct type
*base
, struct type
*dclass
, int is_public
)
3895 base
= check_typedef (base
);
3896 dclass
= check_typedef (dclass
);
3898 if (class_types_same_p (base
, dclass
))
3901 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
); i
++)
3903 if (is_public
&& ! BASETYPE_VIA_PUBLIC (dclass
, i
))
3906 d
= distance_to_ancestor (base
, TYPE_BASECLASS (dclass
, i
), is_public
);
3914 /* Check whether BASE is an ancestor or base class or DCLASS
3915 Return 1 if so, and 0 if not.
3916 Note: If BASE and DCLASS are of the same type, this function
3917 will return 1. So for some class A, is_ancestor (A, A) will
3921 is_ancestor (struct type
*base
, struct type
*dclass
)
3923 return distance_to_ancestor (base
, dclass
, 0) >= 0;
3926 /* Like is_ancestor, but only returns true when BASE is a public
3927 ancestor of DCLASS. */
3930 is_public_ancestor (struct type
*base
, struct type
*dclass
)
3932 return distance_to_ancestor (base
, dclass
, 1) >= 0;
3935 /* A helper function for is_unique_ancestor. */
3938 is_unique_ancestor_worker (struct type
*base
, struct type
*dclass
,
3940 const gdb_byte
*valaddr
, int embedded_offset
,
3941 CORE_ADDR address
, struct value
*val
)
3945 base
= check_typedef (base
);
3946 dclass
= check_typedef (dclass
);
3948 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
) && count
< 2; ++i
)
3953 iter
= check_typedef (TYPE_BASECLASS (dclass
, i
));
3955 this_offset
= baseclass_offset (dclass
, i
, valaddr
, embedded_offset
,
3958 if (class_types_same_p (base
, iter
))
3960 /* If this is the first subclass, set *OFFSET and set count
3961 to 1. Otherwise, if this is at the same offset as
3962 previous instances, do nothing. Otherwise, increment
3966 *offset
= this_offset
;
3969 else if (this_offset
== *offset
)
3977 count
+= is_unique_ancestor_worker (base
, iter
, offset
,
3979 embedded_offset
+ this_offset
,
3986 /* Like is_ancestor, but only returns true if BASE is a unique base
3987 class of the type of VAL. */
3990 is_unique_ancestor (struct type
*base
, struct value
*val
)
3994 return is_unique_ancestor_worker (base
, value_type (val
), &offset
,
3995 value_contents_for_printing (val
).data (),
3996 value_embedded_offset (val
),
3997 value_address (val
), val
) == 1;
4000 /* See gdbtypes.h. */
4003 type_byte_order (const struct type
*type
)
4005 bfd_endian byteorder
= gdbarch_byte_order (type
->arch ());
4006 if (type
->endianity_is_not_default ())
4008 if (byteorder
== BFD_ENDIAN_BIG
)
4009 return BFD_ENDIAN_LITTLE
;
4012 gdb_assert (byteorder
== BFD_ENDIAN_LITTLE
);
4013 return BFD_ENDIAN_BIG
;
4020 /* See gdbtypes.h. */
4023 is_nocall_function (const struct type
*type
)
4025 gdb_assert (type
->code () == TYPE_CODE_FUNC
4026 || type
->code () == TYPE_CODE_METHOD
);
4028 return TYPE_CALLING_CONVENTION (type
) == DW_CC_nocall
;
4032 /* Overload resolution. */
4034 /* Return the sum of the rank of A with the rank of B. */
4037 sum_ranks (struct rank a
, struct rank b
)
4040 c
.rank
= a
.rank
+ b
.rank
;
4041 c
.subrank
= a
.subrank
+ b
.subrank
;
4045 /* Compare rank A and B and return:
4047 1 if a is better than b
4048 -1 if b is better than a. */
4051 compare_ranks (struct rank a
, struct rank b
)
4053 if (a
.rank
== b
.rank
)
4055 if (a
.subrank
== b
.subrank
)
4057 if (a
.subrank
< b
.subrank
)
4059 if (a
.subrank
> b
.subrank
)
4063 if (a
.rank
< b
.rank
)
4066 /* a.rank > b.rank */
4070 /* Functions for overload resolution begin here. */
4072 /* Compare two badness vectors A and B and return the result.
4073 0 => A and B are identical
4074 1 => A and B are incomparable
4075 2 => A is better than B
4076 3 => A is worse than B */
4079 compare_badness (const badness_vector
&a
, const badness_vector
&b
)
4083 /* Any positives in comparison? */
4084 bool found_pos
= false;
4085 /* Any negatives in comparison? */
4086 bool found_neg
= false;
4087 /* Did A have any INVALID_CONVERSION entries. */
4088 bool a_invalid
= false;
4089 /* Did B have any INVALID_CONVERSION entries. */
4090 bool b_invalid
= false;
4092 /* differing sizes => incomparable */
4093 if (a
.size () != b
.size ())
4096 /* Subtract b from a */
4097 for (i
= 0; i
< a
.size (); i
++)
4099 tmp
= compare_ranks (b
[i
], a
[i
]);
4104 if (a
[i
].rank
>= INVALID_CONVERSION
)
4106 if (b
[i
].rank
>= INVALID_CONVERSION
)
4110 /* B will only be considered better than or incomparable to A if
4111 they both have invalid entries, or if neither does. That is, if
4112 A has only valid entries, and B has an invalid entry, then A will
4113 be considered better than B, even if B happens to be better for
4115 if (a_invalid
!= b_invalid
)
4118 return 3; /* A > B */
4119 return 2; /* A < B */
4124 return 1; /* incomparable */
4126 return 3; /* A > B */
4132 return 2; /* A < B */
4134 return 0; /* A == B */
4138 /* Rank a function by comparing its parameter types (PARMS), to the
4139 types of an argument list (ARGS). Return the badness vector. This
4140 has ARGS.size() + 1 entries. */
4143 rank_function (gdb::array_view
<type
*> parms
,
4144 gdb::array_view
<value
*> args
)
4146 /* add 1 for the length-match rank. */
4148 bv
.reserve (1 + args
.size ());
4150 /* First compare the lengths of the supplied lists.
4151 If there is a mismatch, set it to a high value. */
4153 /* pai/1997-06-03 FIXME: when we have debug info about default
4154 arguments and ellipsis parameter lists, we should consider those
4155 and rank the length-match more finely. */
4157 bv
.push_back ((args
.size () != parms
.size ())
4158 ? LENGTH_MISMATCH_BADNESS
4159 : EXACT_MATCH_BADNESS
);
4161 /* Now rank all the parameters of the candidate function. */
4162 size_t min_len
= std::min (parms
.size (), args
.size ());
4164 for (size_t i
= 0; i
< min_len
; i
++)
4165 bv
.push_back (rank_one_type (parms
[i
], value_type (args
[i
]),
4168 /* If more arguments than parameters, add dummy entries. */
4169 for (size_t i
= min_len
; i
< args
.size (); i
++)
4170 bv
.push_back (TOO_FEW_PARAMS_BADNESS
);
4175 /* Compare the names of two integer types, assuming that any sign
4176 qualifiers have been checked already. We do it this way because
4177 there may be an "int" in the name of one of the types. */
4180 integer_types_same_name_p (const char *first
, const char *second
)
4182 int first_p
, second_p
;
4184 /* If both are shorts, return 1; if neither is a short, keep
4186 first_p
= (strstr (first
, "short") != NULL
);
4187 second_p
= (strstr (second
, "short") != NULL
);
4188 if (first_p
&& second_p
)
4190 if (first_p
|| second_p
)
4193 /* Likewise for long. */
4194 first_p
= (strstr (first
, "long") != NULL
);
4195 second_p
= (strstr (second
, "long") != NULL
);
4196 if (first_p
&& second_p
)
4198 if (first_p
|| second_p
)
4201 /* Likewise for char. */
4202 first_p
= (strstr (first
, "char") != NULL
);
4203 second_p
= (strstr (second
, "char") != NULL
);
4204 if (first_p
&& second_p
)
4206 if (first_p
|| second_p
)
4209 /* They must both be ints. */
4213 /* Compares type A to type B. Returns true if they represent the same
4214 type, false otherwise. */
4217 types_equal (struct type
*a
, struct type
*b
)
4219 /* Identical type pointers. */
4220 /* However, this still doesn't catch all cases of same type for b
4221 and a. The reason is that builtin types are different from
4222 the same ones constructed from the object. */
4226 /* Resolve typedefs */
4227 if (a
->code () == TYPE_CODE_TYPEDEF
)
4228 a
= check_typedef (a
);
4229 if (b
->code () == TYPE_CODE_TYPEDEF
)
4230 b
= check_typedef (b
);
4232 /* Check if identical after resolving typedefs. */
4236 /* If after resolving typedefs a and b are not of the same type
4237 code then they are not equal. */
4238 if (a
->code () != b
->code ())
4241 /* If a and b are both pointers types or both reference types then
4242 they are equal of the same type iff the objects they refer to are
4243 of the same type. */
4244 if (a
->code () == TYPE_CODE_PTR
4245 || a
->code () == TYPE_CODE_REF
)
4246 return types_equal (TYPE_TARGET_TYPE (a
),
4247 TYPE_TARGET_TYPE (b
));
4249 /* Well, damnit, if the names are exactly the same, I'll say they
4250 are exactly the same. This happens when we generate method
4251 stubs. The types won't point to the same address, but they
4252 really are the same. */
4254 if (a
->name () && b
->name ()
4255 && strcmp (a
->name (), b
->name ()) == 0)
4258 /* Two function types are equal if their argument and return types
4260 if (a
->code () == TYPE_CODE_FUNC
)
4264 if (a
->num_fields () != b
->num_fields ())
4267 if (!types_equal (TYPE_TARGET_TYPE (a
), TYPE_TARGET_TYPE (b
)))
4270 for (i
= 0; i
< a
->num_fields (); ++i
)
4271 if (!types_equal (a
->field (i
).type (), b
->field (i
).type ()))
4280 /* Deep comparison of types. */
4282 /* An entry in the type-equality bcache. */
4284 struct type_equality_entry
4286 type_equality_entry (struct type
*t1
, struct type
*t2
)
4292 struct type
*type1
, *type2
;
4295 /* A helper function to compare two strings. Returns true if they are
4296 the same, false otherwise. Handles NULLs properly. */
4299 compare_maybe_null_strings (const char *s
, const char *t
)
4301 if (s
== NULL
|| t
== NULL
)
4303 return strcmp (s
, t
) == 0;
4306 /* A helper function for check_types_worklist that checks two types for
4307 "deep" equality. Returns true if the types are considered the
4308 same, false otherwise. */
4311 check_types_equal (struct type
*type1
, struct type
*type2
,
4312 std::vector
<type_equality_entry
> *worklist
)
4314 type1
= check_typedef (type1
);
4315 type2
= check_typedef (type2
);
4320 if (type1
->code () != type2
->code ()
4321 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
4322 || type1
->is_unsigned () != type2
->is_unsigned ()
4323 || type1
->has_no_signedness () != type2
->has_no_signedness ()
4324 || type1
->endianity_is_not_default () != type2
->endianity_is_not_default ()
4325 || type1
->has_varargs () != type2
->has_varargs ()
4326 || type1
->is_vector () != type2
->is_vector ()
4327 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
4328 || type1
->instance_flags () != type2
->instance_flags ()
4329 || type1
->num_fields () != type2
->num_fields ())
4332 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4334 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4337 if (type1
->code () == TYPE_CODE_RANGE
)
4339 if (*type1
->bounds () != *type2
->bounds ())
4346 for (i
= 0; i
< type1
->num_fields (); ++i
)
4348 const struct field
*field1
= &type1
->field (i
);
4349 const struct field
*field2
= &type2
->field (i
);
4351 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
4352 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
4353 || field1
->loc_kind () != field2
->loc_kind ())
4355 if (!compare_maybe_null_strings (field1
->name (), field2
->name ()))
4357 switch (field1
->loc_kind ())
4359 case FIELD_LOC_KIND_BITPOS
:
4360 if (field1
->loc_bitpos () != field2
->loc_bitpos ())
4363 case FIELD_LOC_KIND_ENUMVAL
:
4364 if (field1
->loc_enumval () != field2
->loc_enumval ())
4366 /* Don't compare types of enum fields, because they don't
4369 case FIELD_LOC_KIND_PHYSADDR
:
4370 if (field1
->loc_physaddr () != field2
->loc_physaddr ())
4373 case FIELD_LOC_KIND_PHYSNAME
:
4374 if (!compare_maybe_null_strings (field1
->loc_physname (),
4375 field2
->loc_physname ()))
4378 case FIELD_LOC_KIND_DWARF_BLOCK
:
4380 struct dwarf2_locexpr_baton
*block1
, *block2
;
4382 block1
= field1
->loc_dwarf_block ();
4383 block2
= field2
->loc_dwarf_block ();
4384 if (block1
->per_cu
!= block2
->per_cu
4385 || block1
->size
!= block2
->size
4386 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
4391 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
4392 "%d by check_types_equal"),
4393 field1
->loc_kind ());
4396 worklist
->emplace_back (field1
->type (), field2
->type ());
4400 if (TYPE_TARGET_TYPE (type1
) != NULL
)
4402 if (TYPE_TARGET_TYPE (type2
) == NULL
)
4405 worklist
->emplace_back (TYPE_TARGET_TYPE (type1
),
4406 TYPE_TARGET_TYPE (type2
));
4408 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
4414 /* Check types on a worklist for equality. Returns false if any pair
4415 is not equal, true if they are all considered equal. */
4418 check_types_worklist (std::vector
<type_equality_entry
> *worklist
,
4421 while (!worklist
->empty ())
4425 struct type_equality_entry entry
= std::move (worklist
->back ());
4426 worklist
->pop_back ();
4428 /* If the type pair has already been visited, we know it is
4430 cache
->insert (&entry
, sizeof (entry
), &added
);
4434 if (!check_types_equal (entry
.type1
, entry
.type2
, worklist
))
4441 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4442 "deep comparison". Otherwise return false. */
4445 types_deeply_equal (struct type
*type1
, struct type
*type2
)
4447 std::vector
<type_equality_entry
> worklist
;
4449 gdb_assert (type1
!= NULL
&& type2
!= NULL
);
4451 /* Early exit for the simple case. */
4456 worklist
.emplace_back (type1
, type2
);
4457 return check_types_worklist (&worklist
, &cache
);
4460 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4461 Otherwise return one. */
4464 type_not_allocated (const struct type
*type
)
4466 struct dynamic_prop
*prop
= TYPE_ALLOCATED_PROP (type
);
4468 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4469 && prop
->const_val () == 0);
4472 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4473 Otherwise return one. */
4476 type_not_associated (const struct type
*type
)
4478 struct dynamic_prop
*prop
= TYPE_ASSOCIATED_PROP (type
);
4480 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4481 && prop
->const_val () == 0);
4484 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4487 rank_one_type_parm_ptr (struct type
*parm
, struct type
*arg
, struct value
*value
)
4489 struct rank rank
= {0,0};
4491 switch (arg
->code ())
4495 /* Allowed pointer conversions are:
4496 (a) pointer to void-pointer conversion. */
4497 if (TYPE_TARGET_TYPE (parm
)->code () == TYPE_CODE_VOID
)
4498 return VOID_PTR_CONVERSION_BADNESS
;
4500 /* (b) pointer to ancestor-pointer conversion. */
4501 rank
.subrank
= distance_to_ancestor (TYPE_TARGET_TYPE (parm
),
4502 TYPE_TARGET_TYPE (arg
),
4504 if (rank
.subrank
>= 0)
4505 return sum_ranks (BASE_PTR_CONVERSION_BADNESS
, rank
);
4507 return INCOMPATIBLE_TYPE_BADNESS
;
4508 case TYPE_CODE_ARRAY
:
4510 struct type
*t1
= TYPE_TARGET_TYPE (parm
);
4511 struct type
*t2
= TYPE_TARGET_TYPE (arg
);
4513 if (types_equal (t1
, t2
))
4515 /* Make sure they are CV equal. */
4516 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4517 rank
.subrank
|= CV_CONVERSION_CONST
;
4518 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4519 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4520 if (rank
.subrank
!= 0)
4521 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4522 return EXACT_MATCH_BADNESS
;
4524 return INCOMPATIBLE_TYPE_BADNESS
;
4526 case TYPE_CODE_FUNC
:
4527 return rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
);
4529 if (value
!= NULL
&& value_type (value
)->code () == TYPE_CODE_INT
)
4531 if (value_as_long (value
) == 0)
4533 /* Null pointer conversion: allow it to be cast to a pointer.
4534 [4.10.1 of C++ standard draft n3290] */
4535 return NULL_POINTER_CONVERSION_BADNESS
;
4539 /* If type checking is disabled, allow the conversion. */
4540 if (!strict_type_checking
)
4541 return NS_INTEGER_POINTER_CONVERSION_BADNESS
;
4545 case TYPE_CODE_ENUM
:
4546 case TYPE_CODE_FLAGS
:
4547 case TYPE_CODE_CHAR
:
4548 case TYPE_CODE_RANGE
:
4549 case TYPE_CODE_BOOL
:
4551 return INCOMPATIBLE_TYPE_BADNESS
;
4555 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4558 rank_one_type_parm_array (struct type
*parm
, struct type
*arg
, struct value
*value
)
4560 switch (arg
->code ())
4563 case TYPE_CODE_ARRAY
:
4564 return rank_one_type (TYPE_TARGET_TYPE (parm
),
4565 TYPE_TARGET_TYPE (arg
), NULL
);
4567 return INCOMPATIBLE_TYPE_BADNESS
;
4571 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4574 rank_one_type_parm_func (struct type
*parm
, struct type
*arg
, struct value
*value
)
4576 switch (arg
->code ())
4578 case TYPE_CODE_PTR
: /* funcptr -> func */
4579 return rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
);
4581 return INCOMPATIBLE_TYPE_BADNESS
;
4585 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4588 rank_one_type_parm_int (struct type
*parm
, struct type
*arg
, struct value
*value
)
4590 switch (arg
->code ())
4593 if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4595 /* Deal with signed, unsigned, and plain chars and
4596 signed and unsigned ints. */
4597 if (parm
->has_no_signedness ())
4599 /* This case only for character types. */
4600 if (arg
->has_no_signedness ())
4601 return EXACT_MATCH_BADNESS
; /* plain char -> plain char */
4602 else /* signed/unsigned char -> plain char */
4603 return INTEGER_CONVERSION_BADNESS
;
4605 else if (parm
->is_unsigned ())
4607 if (arg
->is_unsigned ())
4609 /* unsigned int -> unsigned int, or
4610 unsigned long -> unsigned long */
4611 if (integer_types_same_name_p (parm
->name (),
4613 return EXACT_MATCH_BADNESS
;
4614 else if (integer_types_same_name_p (arg
->name (),
4616 && integer_types_same_name_p (parm
->name (),
4618 /* unsigned int -> unsigned long */
4619 return INTEGER_PROMOTION_BADNESS
;
4621 /* unsigned long -> unsigned int */
4622 return INTEGER_CONVERSION_BADNESS
;
4626 if (integer_types_same_name_p (arg
->name (),
4628 && integer_types_same_name_p (parm
->name (),
4630 /* signed long -> unsigned int */
4631 return INTEGER_CONVERSION_BADNESS
;
4633 /* signed int/long -> unsigned int/long */
4634 return INTEGER_CONVERSION_BADNESS
;
4637 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4639 if (integer_types_same_name_p (parm
->name (),
4641 return EXACT_MATCH_BADNESS
;
4642 else if (integer_types_same_name_p (arg
->name (),
4644 && integer_types_same_name_p (parm
->name (),
4646 return INTEGER_PROMOTION_BADNESS
;
4648 return INTEGER_CONVERSION_BADNESS
;
4651 return INTEGER_CONVERSION_BADNESS
;
4653 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4654 return INTEGER_PROMOTION_BADNESS
;
4656 return INTEGER_CONVERSION_BADNESS
;
4657 case TYPE_CODE_ENUM
:
4658 case TYPE_CODE_FLAGS
:
4659 case TYPE_CODE_CHAR
:
4660 case TYPE_CODE_RANGE
:
4661 case TYPE_CODE_BOOL
:
4662 if (arg
->is_declared_class ())
4663 return INCOMPATIBLE_TYPE_BADNESS
;
4664 return INTEGER_PROMOTION_BADNESS
;
4666 return INT_FLOAT_CONVERSION_BADNESS
;
4668 return NS_POINTER_CONVERSION_BADNESS
;
4670 return INCOMPATIBLE_TYPE_BADNESS
;
4674 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4677 rank_one_type_parm_enum (struct type
*parm
, struct type
*arg
, struct value
*value
)
4679 switch (arg
->code ())
4682 case TYPE_CODE_CHAR
:
4683 case TYPE_CODE_RANGE
:
4684 case TYPE_CODE_BOOL
:
4685 case TYPE_CODE_ENUM
:
4686 if (parm
->is_declared_class () || arg
->is_declared_class ())
4687 return INCOMPATIBLE_TYPE_BADNESS
;
4688 return INTEGER_CONVERSION_BADNESS
;
4690 return INT_FLOAT_CONVERSION_BADNESS
;
4692 return INCOMPATIBLE_TYPE_BADNESS
;
4696 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4699 rank_one_type_parm_char (struct type
*parm
, struct type
*arg
, struct value
*value
)
4701 switch (arg
->code ())
4703 case TYPE_CODE_RANGE
:
4704 case TYPE_CODE_BOOL
:
4705 case TYPE_CODE_ENUM
:
4706 if (arg
->is_declared_class ())
4707 return INCOMPATIBLE_TYPE_BADNESS
;
4708 return INTEGER_CONVERSION_BADNESS
;
4710 return INT_FLOAT_CONVERSION_BADNESS
;
4712 if (TYPE_LENGTH (arg
) > TYPE_LENGTH (parm
))
4713 return INTEGER_CONVERSION_BADNESS
;
4714 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4715 return INTEGER_PROMOTION_BADNESS
;
4717 case TYPE_CODE_CHAR
:
4718 /* Deal with signed, unsigned, and plain chars for C++ and
4719 with int cases falling through from previous case. */
4720 if (parm
->has_no_signedness ())
4722 if (arg
->has_no_signedness ())
4723 return EXACT_MATCH_BADNESS
;
4725 return INTEGER_CONVERSION_BADNESS
;
4727 else if (parm
->is_unsigned ())
4729 if (arg
->is_unsigned ())
4730 return EXACT_MATCH_BADNESS
;
4732 return INTEGER_PROMOTION_BADNESS
;
4734 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4735 return EXACT_MATCH_BADNESS
;
4737 return INTEGER_CONVERSION_BADNESS
;
4739 return INCOMPATIBLE_TYPE_BADNESS
;
4743 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4746 rank_one_type_parm_range (struct type
*parm
, struct type
*arg
, struct value
*value
)
4748 switch (arg
->code ())
4751 case TYPE_CODE_CHAR
:
4752 case TYPE_CODE_RANGE
:
4753 case TYPE_CODE_BOOL
:
4754 case TYPE_CODE_ENUM
:
4755 return INTEGER_CONVERSION_BADNESS
;
4757 return INT_FLOAT_CONVERSION_BADNESS
;
4759 return INCOMPATIBLE_TYPE_BADNESS
;
4763 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4766 rank_one_type_parm_bool (struct type
*parm
, struct type
*arg
, struct value
*value
)
4768 switch (arg
->code ())
4770 /* n3290 draft, section 4.12.1 (conv.bool):
4772 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4773 pointer to member type can be converted to a prvalue of type
4774 bool. A zero value, null pointer value, or null member pointer
4775 value is converted to false; any other value is converted to
4776 true. A prvalue of type std::nullptr_t can be converted to a
4777 prvalue of type bool; the resulting value is false." */
4779 case TYPE_CODE_CHAR
:
4780 case TYPE_CODE_ENUM
:
4782 case TYPE_CODE_MEMBERPTR
:
4784 return BOOL_CONVERSION_BADNESS
;
4785 case TYPE_CODE_RANGE
:
4786 return INCOMPATIBLE_TYPE_BADNESS
;
4787 case TYPE_CODE_BOOL
:
4788 return EXACT_MATCH_BADNESS
;
4790 return INCOMPATIBLE_TYPE_BADNESS
;
4794 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4797 rank_one_type_parm_float (struct type
*parm
, struct type
*arg
, struct value
*value
)
4799 switch (arg
->code ())
4802 if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4803 return FLOAT_PROMOTION_BADNESS
;
4804 else if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4805 return EXACT_MATCH_BADNESS
;
4807 return FLOAT_CONVERSION_BADNESS
;
4809 case TYPE_CODE_BOOL
:
4810 case TYPE_CODE_ENUM
:
4811 case TYPE_CODE_RANGE
:
4812 case TYPE_CODE_CHAR
:
4813 return INT_FLOAT_CONVERSION_BADNESS
;
4815 return INCOMPATIBLE_TYPE_BADNESS
;
4819 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4822 rank_one_type_parm_complex (struct type
*parm
, struct type
*arg
, struct value
*value
)
4824 switch (arg
->code ())
4825 { /* Strictly not needed for C++, but... */
4827 return FLOAT_PROMOTION_BADNESS
;
4828 case TYPE_CODE_COMPLEX
:
4829 return EXACT_MATCH_BADNESS
;
4831 return INCOMPATIBLE_TYPE_BADNESS
;
4835 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4838 rank_one_type_parm_struct (struct type
*parm
, struct type
*arg
, struct value
*value
)
4840 struct rank rank
= {0, 0};
4842 switch (arg
->code ())
4844 case TYPE_CODE_STRUCT
:
4845 /* Check for derivation */
4846 rank
.subrank
= distance_to_ancestor (parm
, arg
, 0);
4847 if (rank
.subrank
>= 0)
4848 return sum_ranks (BASE_CONVERSION_BADNESS
, rank
);
4851 return INCOMPATIBLE_TYPE_BADNESS
;
4855 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4858 rank_one_type_parm_set (struct type
*parm
, struct type
*arg
, struct value
*value
)
4860 switch (arg
->code ())
4864 return rank_one_type (parm
->field (0).type (),
4865 arg
->field (0).type (), NULL
);
4867 return INCOMPATIBLE_TYPE_BADNESS
;
4871 /* Compare one type (PARM) for compatibility with another (ARG).
4872 * PARM is intended to be the parameter type of a function; and
4873 * ARG is the supplied argument's type. This function tests if
4874 * the latter can be converted to the former.
4875 * VALUE is the argument's value or NULL if none (or called recursively)
4877 * Return 0 if they are identical types;
4878 * Otherwise, return an integer which corresponds to how compatible
4879 * PARM is to ARG. The higher the return value, the worse the match.
4880 * Generally the "bad" conversions are all uniformly assigned
4881 * INVALID_CONVERSION. */
4884 rank_one_type (struct type
*parm
, struct type
*arg
, struct value
*value
)
4886 struct rank rank
= {0,0};
4888 /* Resolve typedefs */
4889 if (parm
->code () == TYPE_CODE_TYPEDEF
)
4890 parm
= check_typedef (parm
);
4891 if (arg
->code () == TYPE_CODE_TYPEDEF
)
4892 arg
= check_typedef (arg
);
4894 if (TYPE_IS_REFERENCE (parm
) && value
!= NULL
)
4896 if (VALUE_LVAL (value
) == not_lval
)
4898 /* Rvalues should preferably bind to rvalue references or const
4899 lvalue references. */
4900 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4901 rank
.subrank
= REFERENCE_CONVERSION_RVALUE
;
4902 else if (TYPE_CONST (TYPE_TARGET_TYPE (parm
)))
4903 rank
.subrank
= REFERENCE_CONVERSION_CONST_LVALUE
;
4905 return INCOMPATIBLE_TYPE_BADNESS
;
4906 return sum_ranks (rank
, REFERENCE_CONVERSION_BADNESS
);
4910 /* It's illegal to pass an lvalue as an rvalue. */
4911 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4912 return INCOMPATIBLE_TYPE_BADNESS
;
4916 if (types_equal (parm
, arg
))
4918 struct type
*t1
= parm
;
4919 struct type
*t2
= arg
;
4921 /* For pointers and references, compare target type. */
4922 if (parm
->is_pointer_or_reference ())
4924 t1
= TYPE_TARGET_TYPE (parm
);
4925 t2
= TYPE_TARGET_TYPE (arg
);
4928 /* Make sure they are CV equal, too. */
4929 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4930 rank
.subrank
|= CV_CONVERSION_CONST
;
4931 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4932 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4933 if (rank
.subrank
!= 0)
4934 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4935 return EXACT_MATCH_BADNESS
;
4938 /* See through references, since we can almost make non-references
4941 if (TYPE_IS_REFERENCE (arg
))
4942 return (sum_ranks (rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
),
4943 REFERENCE_SEE_THROUGH_BADNESS
));
4944 if (TYPE_IS_REFERENCE (parm
))
4945 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
),
4946 REFERENCE_SEE_THROUGH_BADNESS
));
4949 /* Debugging only. */
4950 gdb_printf (gdb_stderr
,
4951 "------ Arg is %s [%d], parm is %s [%d]\n",
4952 arg
->name (), arg
->code (),
4953 parm
->name (), parm
->code ());
4956 /* x -> y means arg of type x being supplied for parameter of type y. */
4958 switch (parm
->code ())
4961 return rank_one_type_parm_ptr (parm
, arg
, value
);
4962 case TYPE_CODE_ARRAY
:
4963 return rank_one_type_parm_array (parm
, arg
, value
);
4964 case TYPE_CODE_FUNC
:
4965 return rank_one_type_parm_func (parm
, arg
, value
);
4967 return rank_one_type_parm_int (parm
, arg
, value
);
4968 case TYPE_CODE_ENUM
:
4969 return rank_one_type_parm_enum (parm
, arg
, value
);
4970 case TYPE_CODE_CHAR
:
4971 return rank_one_type_parm_char (parm
, arg
, value
);
4972 case TYPE_CODE_RANGE
:
4973 return rank_one_type_parm_range (parm
, arg
, value
);
4974 case TYPE_CODE_BOOL
:
4975 return rank_one_type_parm_bool (parm
, arg
, value
);
4977 return rank_one_type_parm_float (parm
, arg
, value
);
4978 case TYPE_CODE_COMPLEX
:
4979 return rank_one_type_parm_complex (parm
, arg
, value
);
4980 case TYPE_CODE_STRUCT
:
4981 return rank_one_type_parm_struct (parm
, arg
, value
);
4983 return rank_one_type_parm_set (parm
, arg
, value
);
4985 return INCOMPATIBLE_TYPE_BADNESS
;
4986 } /* switch (arg->code ()) */
4989 /* End of functions for overload resolution. */
4991 /* Routines to pretty-print types. */
4994 print_bit_vector (B_TYPE
*bits
, int nbits
)
4998 for (bitno
= 0; bitno
< nbits
; bitno
++)
5000 if ((bitno
% 8) == 0)
5004 if (B_TST (bits
, bitno
))
5011 /* Note the first arg should be the "this" pointer, we may not want to
5012 include it since we may get into a infinitely recursive
5016 print_args (struct field
*args
, int nargs
, int spaces
)
5022 for (i
= 0; i
< nargs
; i
++)
5025 ("%*s[%d] name '%s'\n", spaces
, "", i
,
5026 args
[i
].name () != NULL
? args
[i
].name () : "<NULL>");
5027 recursive_dump_type (args
[i
].type (), spaces
+ 2);
5033 field_is_static (struct field
*f
)
5035 /* "static" fields are the fields whose location is not relative
5036 to the address of the enclosing struct. It would be nice to
5037 have a dedicated flag that would be set for static fields when
5038 the type is being created. But in practice, checking the field
5039 loc_kind should give us an accurate answer. */
5040 return (f
->loc_kind () == FIELD_LOC_KIND_PHYSNAME
5041 || f
->loc_kind () == FIELD_LOC_KIND_PHYSADDR
);
5045 dump_fn_fieldlists (struct type
*type
, int spaces
)
5051 gdb_printf ("%*sfn_fieldlists %s\n", spaces
, "",
5052 host_address_to_string (TYPE_FN_FIELDLISTS (type
)));
5053 for (method_idx
= 0; method_idx
< TYPE_NFN_FIELDS (type
); method_idx
++)
5055 f
= TYPE_FN_FIELDLIST1 (type
, method_idx
);
5057 ("%*s[%d] name '%s' (%s) length %d\n", spaces
+ 2, "",
5059 TYPE_FN_FIELDLIST_NAME (type
, method_idx
),
5060 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type
, method_idx
)),
5061 TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
));
5062 for (overload_idx
= 0;
5063 overload_idx
< TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
);
5067 ("%*s[%d] physname '%s' (%s)\n",
5068 spaces
+ 4, "", overload_idx
,
5069 TYPE_FN_FIELD_PHYSNAME (f
, overload_idx
),
5070 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f
,
5073 ("%*stype %s\n", spaces
+ 8, "",
5074 host_address_to_string (TYPE_FN_FIELD_TYPE (f
, overload_idx
)));
5076 recursive_dump_type (TYPE_FN_FIELD_TYPE (f
, overload_idx
),
5080 ("%*sargs %s\n", spaces
+ 8, "",
5081 host_address_to_string (TYPE_FN_FIELD_ARGS (f
, overload_idx
)));
5082 print_args (TYPE_FN_FIELD_ARGS (f
, overload_idx
),
5083 TYPE_FN_FIELD_TYPE (f
, overload_idx
)->num_fields (),
5086 ("%*sfcontext %s\n", spaces
+ 8, "",
5087 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f
,
5090 gdb_printf ("%*sis_const %d\n", spaces
+ 8, "",
5091 TYPE_FN_FIELD_CONST (f
, overload_idx
));
5092 gdb_printf ("%*sis_volatile %d\n", spaces
+ 8, "",
5093 TYPE_FN_FIELD_VOLATILE (f
, overload_idx
));
5094 gdb_printf ("%*sis_private %d\n", spaces
+ 8, "",
5095 TYPE_FN_FIELD_PRIVATE (f
, overload_idx
));
5096 gdb_printf ("%*sis_protected %d\n", spaces
+ 8, "",
5097 TYPE_FN_FIELD_PROTECTED (f
, overload_idx
));
5098 gdb_printf ("%*sis_stub %d\n", spaces
+ 8, "",
5099 TYPE_FN_FIELD_STUB (f
, overload_idx
));
5100 gdb_printf ("%*sdefaulted %d\n", spaces
+ 8, "",
5101 TYPE_FN_FIELD_DEFAULTED (f
, overload_idx
));
5102 gdb_printf ("%*sis_deleted %d\n", spaces
+ 8, "",
5103 TYPE_FN_FIELD_DELETED (f
, overload_idx
));
5104 gdb_printf ("%*svoffset %u\n", spaces
+ 8, "",
5105 TYPE_FN_FIELD_VOFFSET (f
, overload_idx
));
5111 print_cplus_stuff (struct type
*type
, int spaces
)
5113 gdb_printf ("%*svptr_fieldno %d\n", spaces
, "",
5114 TYPE_VPTR_FIELDNO (type
));
5115 gdb_printf ("%*svptr_basetype %s\n", spaces
, "",
5116 host_address_to_string (TYPE_VPTR_BASETYPE (type
)));
5117 if (TYPE_VPTR_BASETYPE (type
) != NULL
)
5118 recursive_dump_type (TYPE_VPTR_BASETYPE (type
), spaces
+ 2);
5120 gdb_printf ("%*sn_baseclasses %d\n", spaces
, "",
5121 TYPE_N_BASECLASSES (type
));
5122 gdb_printf ("%*snfn_fields %d\n", spaces
, "",
5123 TYPE_NFN_FIELDS (type
));
5124 if (TYPE_N_BASECLASSES (type
) > 0)
5127 ("%*svirtual_field_bits (%d bits at *%s)",
5128 spaces
, "", TYPE_N_BASECLASSES (type
),
5129 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type
)));
5131 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type
),
5132 TYPE_N_BASECLASSES (type
));
5135 if (type
->num_fields () > 0)
5137 if (TYPE_FIELD_PRIVATE_BITS (type
) != NULL
)
5140 ("%*sprivate_field_bits (%d bits at *%s)",
5141 spaces
, "", type
->num_fields (),
5142 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type
)));
5143 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type
),
5144 type
->num_fields ());
5147 if (TYPE_FIELD_PROTECTED_BITS (type
) != NULL
)
5150 ("%*sprotected_field_bits (%d bits at *%s",
5151 spaces
, "", type
->num_fields (),
5152 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type
)));
5153 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type
),
5154 type
->num_fields ());
5158 if (TYPE_NFN_FIELDS (type
) > 0)
5160 dump_fn_fieldlists (type
, spaces
);
5163 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5164 TYPE_CPLUS_CALLING_CONVENTION (type
));
5167 /* Print the contents of the TYPE's type_specific union, assuming that
5168 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5171 print_gnat_stuff (struct type
*type
, int spaces
)
5173 struct type
*descriptive_type
= TYPE_DESCRIPTIVE_TYPE (type
);
5175 if (descriptive_type
== NULL
)
5176 gdb_printf ("%*sno descriptive type\n", spaces
+ 2, "");
5179 gdb_printf ("%*sdescriptive type\n", spaces
+ 2, "");
5180 recursive_dump_type (descriptive_type
, spaces
+ 4);
5184 /* Print the contents of the TYPE's type_specific union, assuming that
5185 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5188 print_fixed_point_type_info (struct type
*type
, int spaces
)
5190 gdb_printf ("%*sscaling factor: %s\n", spaces
+ 2, "",
5191 type
->fixed_point_scaling_factor ().str ().c_str ());
5194 static struct obstack dont_print_type_obstack
;
5196 /* Print the dynamic_prop PROP. */
5199 dump_dynamic_prop (dynamic_prop
const& prop
)
5201 switch (prop
.kind ())
5204 gdb_printf ("%s", plongest (prop
.const_val ()));
5206 case PROP_UNDEFINED
:
5207 gdb_printf ("(undefined)");
5211 gdb_printf ("(dynamic)");
5214 gdb_assert_not_reached ("unhandled prop kind");
5220 recursive_dump_type (struct type
*type
, int spaces
)
5225 obstack_begin (&dont_print_type_obstack
, 0);
5227 if (type
->num_fields () > 0
5228 || (HAVE_CPLUS_STRUCT (type
) && TYPE_NFN_FIELDS (type
) > 0))
5230 struct type
**first_dont_print
5231 = (struct type
**) obstack_base (&dont_print_type_obstack
);
5233 int i
= (struct type
**)
5234 obstack_next_free (&dont_print_type_obstack
) - first_dont_print
;
5238 if (type
== first_dont_print
[i
])
5240 gdb_printf ("%*stype node %s", spaces
, "",
5241 host_address_to_string (type
));
5242 gdb_printf (_(" <same as already seen type>\n"));
5247 obstack_ptr_grow (&dont_print_type_obstack
, type
);
5250 gdb_printf ("%*stype node %s\n", spaces
, "",
5251 host_address_to_string (type
));
5252 gdb_printf ("%*sname '%s' (%s)\n", spaces
, "",
5253 type
->name () ? type
->name () : "<NULL>",
5254 host_address_to_string (type
->name ()));
5255 gdb_printf ("%*scode 0x%x ", spaces
, "", type
->code ());
5256 switch (type
->code ())
5258 case TYPE_CODE_UNDEF
:
5259 gdb_printf ("(TYPE_CODE_UNDEF)");
5262 gdb_printf ("(TYPE_CODE_PTR)");
5264 case TYPE_CODE_ARRAY
:
5265 gdb_printf ("(TYPE_CODE_ARRAY)");
5267 case TYPE_CODE_STRUCT
:
5268 gdb_printf ("(TYPE_CODE_STRUCT)");
5270 case TYPE_CODE_UNION
:
5271 gdb_printf ("(TYPE_CODE_UNION)");
5273 case TYPE_CODE_ENUM
:
5274 gdb_printf ("(TYPE_CODE_ENUM)");
5276 case TYPE_CODE_FLAGS
:
5277 gdb_printf ("(TYPE_CODE_FLAGS)");
5279 case TYPE_CODE_FUNC
:
5280 gdb_printf ("(TYPE_CODE_FUNC)");
5283 gdb_printf ("(TYPE_CODE_INT)");
5286 gdb_printf ("(TYPE_CODE_FLT)");
5288 case TYPE_CODE_VOID
:
5289 gdb_printf ("(TYPE_CODE_VOID)");
5292 gdb_printf ("(TYPE_CODE_SET)");
5294 case TYPE_CODE_RANGE
:
5295 gdb_printf ("(TYPE_CODE_RANGE)");
5297 case TYPE_CODE_STRING
:
5298 gdb_printf ("(TYPE_CODE_STRING)");
5300 case TYPE_CODE_ERROR
:
5301 gdb_printf ("(TYPE_CODE_ERROR)");
5303 case TYPE_CODE_MEMBERPTR
:
5304 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5306 case TYPE_CODE_METHODPTR
:
5307 gdb_printf ("(TYPE_CODE_METHODPTR)");
5309 case TYPE_CODE_METHOD
:
5310 gdb_printf ("(TYPE_CODE_METHOD)");
5313 gdb_printf ("(TYPE_CODE_REF)");
5315 case TYPE_CODE_CHAR
:
5316 gdb_printf ("(TYPE_CODE_CHAR)");
5318 case TYPE_CODE_BOOL
:
5319 gdb_printf ("(TYPE_CODE_BOOL)");
5321 case TYPE_CODE_COMPLEX
:
5322 gdb_printf ("(TYPE_CODE_COMPLEX)");
5324 case TYPE_CODE_TYPEDEF
:
5325 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5327 case TYPE_CODE_NAMESPACE
:
5328 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5330 case TYPE_CODE_FIXED_POINT
:
5331 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5334 gdb_printf ("(UNKNOWN TYPE CODE)");
5338 gdb_printf ("%*slength %s\n", spaces
, "",
5339 pulongest (TYPE_LENGTH (type
)));
5340 if (type
->is_objfile_owned ())
5341 gdb_printf ("%*sobjfile %s\n", spaces
, "",
5342 host_address_to_string (type
->objfile_owner ()));
5344 gdb_printf ("%*sgdbarch %s\n", spaces
, "",
5345 host_address_to_string (type
->arch_owner ()));
5346 gdb_printf ("%*starget_type %s\n", spaces
, "",
5347 host_address_to_string (TYPE_TARGET_TYPE (type
)));
5348 if (TYPE_TARGET_TYPE (type
) != NULL
)
5350 recursive_dump_type (TYPE_TARGET_TYPE (type
), spaces
+ 2);
5352 gdb_printf ("%*spointer_type %s\n", spaces
, "",
5353 host_address_to_string (TYPE_POINTER_TYPE (type
)));
5354 gdb_printf ("%*sreference_type %s\n", spaces
, "",
5355 host_address_to_string (TYPE_REFERENCE_TYPE (type
)));
5356 gdb_printf ("%*stype_chain %s\n", spaces
, "",
5357 host_address_to_string (TYPE_CHAIN (type
)));
5358 gdb_printf ("%*sinstance_flags 0x%x", spaces
, "",
5359 (unsigned) type
->instance_flags ());
5360 if (TYPE_CONST (type
))
5362 gdb_puts (" TYPE_CONST");
5364 if (TYPE_VOLATILE (type
))
5366 gdb_puts (" TYPE_VOLATILE");
5368 if (TYPE_CODE_SPACE (type
))
5370 gdb_puts (" TYPE_CODE_SPACE");
5372 if (TYPE_DATA_SPACE (type
))
5374 gdb_puts (" TYPE_DATA_SPACE");
5376 if (TYPE_ADDRESS_CLASS_1 (type
))
5378 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5380 if (TYPE_ADDRESS_CLASS_2 (type
))
5382 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5384 if (TYPE_RESTRICT (type
))
5386 gdb_puts (" TYPE_RESTRICT");
5388 if (TYPE_ATOMIC (type
))
5390 gdb_puts (" TYPE_ATOMIC");
5394 gdb_printf ("%*sflags", spaces
, "");
5395 if (type
->is_unsigned ())
5397 gdb_puts (" TYPE_UNSIGNED");
5399 if (type
->has_no_signedness ())
5401 gdb_puts (" TYPE_NOSIGN");
5403 if (type
->endianity_is_not_default ())
5405 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5407 if (type
->is_stub ())
5409 gdb_puts (" TYPE_STUB");
5411 if (type
->target_is_stub ())
5413 gdb_puts (" TYPE_TARGET_STUB");
5415 if (type
->is_prototyped ())
5417 gdb_puts (" TYPE_PROTOTYPED");
5419 if (type
->has_varargs ())
5421 gdb_puts (" TYPE_VARARGS");
5423 /* This is used for things like AltiVec registers on ppc. Gcc emits
5424 an attribute for the array type, which tells whether or not we
5425 have a vector, instead of a regular array. */
5426 if (type
->is_vector ())
5428 gdb_puts (" TYPE_VECTOR");
5430 if (type
->is_fixed_instance ())
5432 gdb_puts (" TYPE_FIXED_INSTANCE");
5434 if (type
->stub_is_supported ())
5436 gdb_puts (" TYPE_STUB_SUPPORTED");
5438 if (TYPE_NOTTEXT (type
))
5440 gdb_puts (" TYPE_NOTTEXT");
5443 gdb_printf ("%*snfields %d ", spaces
, "", type
->num_fields ());
5444 if (TYPE_ASSOCIATED_PROP (type
) != nullptr
5445 || TYPE_ALLOCATED_PROP (type
) != nullptr)
5447 gdb_printf ("%*s", spaces
, "");
5448 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5450 gdb_printf ("associated ");
5451 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type
));
5453 if (TYPE_ALLOCATED_PROP (type
) != nullptr)
5455 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5457 gdb_printf ("allocated ");
5458 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type
));
5462 gdb_printf ("%s\n", host_address_to_string (type
->fields ()));
5463 for (idx
= 0; idx
< type
->num_fields (); idx
++)
5465 if (type
->code () == TYPE_CODE_ENUM
)
5466 gdb_printf ("%*s[%d] enumval %s type ", spaces
+ 2, "",
5467 idx
, plongest (type
->field (idx
).loc_enumval ()));
5469 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces
+ 2, "",
5470 idx
, plongest (type
->field (idx
).loc_bitpos ()),
5471 TYPE_FIELD_BITSIZE (type
, idx
));
5472 gdb_printf ("%s name '%s' (%s)\n",
5473 host_address_to_string (type
->field (idx
).type ()),
5474 type
->field (idx
).name () != NULL
5475 ? type
->field (idx
).name ()
5477 host_address_to_string (type
->field (idx
).name ()));
5478 if (type
->field (idx
).type () != NULL
)
5480 recursive_dump_type (type
->field (idx
).type (), spaces
+ 4);
5483 if (type
->code () == TYPE_CODE_RANGE
)
5485 gdb_printf ("%*slow ", spaces
, "");
5486 dump_dynamic_prop (type
->bounds ()->low
);
5487 gdb_printf (" high ");
5488 dump_dynamic_prop (type
->bounds ()->high
);
5492 switch (TYPE_SPECIFIC_FIELD (type
))
5494 case TYPE_SPECIFIC_CPLUS_STUFF
:
5495 gdb_printf ("%*scplus_stuff %s\n", spaces
, "",
5496 host_address_to_string (TYPE_CPLUS_SPECIFIC (type
)));
5497 print_cplus_stuff (type
, spaces
);
5500 case TYPE_SPECIFIC_GNAT_STUFF
:
5501 gdb_printf ("%*sgnat_stuff %s\n", spaces
, "",
5502 host_address_to_string (TYPE_GNAT_SPECIFIC (type
)));
5503 print_gnat_stuff (type
, spaces
);
5506 case TYPE_SPECIFIC_FLOATFORMAT
:
5507 gdb_printf ("%*sfloatformat ", spaces
, "");
5508 if (TYPE_FLOATFORMAT (type
) == NULL
5509 || TYPE_FLOATFORMAT (type
)->name
== NULL
)
5510 gdb_puts ("(null)");
5512 gdb_puts (TYPE_FLOATFORMAT (type
)->name
);
5516 case TYPE_SPECIFIC_FUNC
:
5517 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5518 TYPE_CALLING_CONVENTION (type
));
5519 /* tail_call_list is not printed. */
5522 case TYPE_SPECIFIC_SELF_TYPE
:
5523 gdb_printf ("%*sself_type %s\n", spaces
, "",
5524 host_address_to_string (TYPE_SELF_TYPE (type
)));
5527 case TYPE_SPECIFIC_FIXED_POINT
:
5528 gdb_printf ("%*sfixed_point_info ", spaces
, "");
5529 print_fixed_point_type_info (type
, spaces
);
5533 case TYPE_SPECIFIC_INT
:
5534 if (type
->bit_size_differs_p ())
5536 unsigned bit_size
= type
->bit_size ();
5537 unsigned bit_off
= type
->bit_offset ();
5538 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces
, "",
5545 obstack_free (&dont_print_type_obstack
, NULL
);
5548 /* Trivial helpers for the libiberty hash table, for mapping one
5551 struct type_pair
: public allocate_on_obstack
5553 type_pair (struct type
*old_
, struct type
*newobj_
)
5554 : old (old_
), newobj (newobj_
)
5557 struct type
* const old
, * const newobj
;
5561 type_pair_hash (const void *item
)
5563 const struct type_pair
*pair
= (const struct type_pair
*) item
;
5565 return htab_hash_pointer (pair
->old
);
5569 type_pair_eq (const void *item_lhs
, const void *item_rhs
)
5571 const struct type_pair
*lhs
= (const struct type_pair
*) item_lhs
;
5572 const struct type_pair
*rhs
= (const struct type_pair
*) item_rhs
;
5574 return lhs
->old
== rhs
->old
;
5577 /* Allocate the hash table used by copy_type_recursive to walk
5578 types without duplicates. We use OBJFILE's obstack, because
5579 OBJFILE is about to be deleted. */
5582 create_copied_types_hash (struct objfile
*objfile
)
5584 return htab_up (htab_create_alloc_ex (1, type_pair_hash
, type_pair_eq
,
5585 NULL
, &objfile
->objfile_obstack
,
5586 hashtab_obstack_allocate
,
5587 dummy_obstack_deallocate
));
5590 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5592 static struct dynamic_prop_list
*
5593 copy_dynamic_prop_list (struct obstack
*objfile_obstack
,
5594 struct dynamic_prop_list
*list
)
5596 struct dynamic_prop_list
*copy
= list
;
5597 struct dynamic_prop_list
**node_ptr
= ©
;
5599 while (*node_ptr
!= NULL
)
5601 struct dynamic_prop_list
*node_copy
;
5603 node_copy
= ((struct dynamic_prop_list
*)
5604 obstack_copy (objfile_obstack
, *node_ptr
,
5605 sizeof (struct dynamic_prop_list
)));
5606 node_copy
->prop
= (*node_ptr
)->prop
;
5607 *node_ptr
= node_copy
;
5609 node_ptr
= &node_copy
->next
;
5615 /* Recursively copy (deep copy) TYPE, if it is associated with
5616 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5617 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5618 it is not associated with OBJFILE. */
5621 copy_type_recursive (struct objfile
*objfile
,
5623 htab_t copied_types
)
5626 struct type
*new_type
;
5628 if (!type
->is_objfile_owned ())
5631 /* This type shouldn't be pointing to any types in other objfiles;
5632 if it did, the type might disappear unexpectedly. */
5633 gdb_assert (type
->objfile_owner () == objfile
);
5635 struct type_pair
pair (type
, nullptr);
5637 slot
= htab_find_slot (copied_types
, &pair
, INSERT
);
5639 return ((struct type_pair
*) *slot
)->newobj
;
5641 new_type
= alloc_type_arch (type
->arch ());
5643 /* We must add the new type to the hash table immediately, in case
5644 we encounter this type again during a recursive call below. */
5645 struct type_pair
*stored
5646 = new (&objfile
->objfile_obstack
) struct type_pair (type
, new_type
);
5650 /* Copy the common fields of types. For the main type, we simply
5651 copy the entire thing and then update specific fields as needed. */
5652 *TYPE_MAIN_TYPE (new_type
) = *TYPE_MAIN_TYPE (type
);
5654 new_type
->set_owner (type
->arch ());
5657 new_type
->set_name (xstrdup (type
->name ()));
5659 new_type
->set_instance_flags (type
->instance_flags ());
5660 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5662 /* Copy the fields. */
5663 if (type
->num_fields ())
5667 nfields
= type
->num_fields ();
5668 new_type
->set_fields
5670 TYPE_ZALLOC (new_type
, nfields
* sizeof (struct field
)));
5672 for (i
= 0; i
< nfields
; i
++)
5674 TYPE_FIELD_ARTIFICIAL (new_type
, i
) =
5675 TYPE_FIELD_ARTIFICIAL (type
, i
);
5676 TYPE_FIELD_BITSIZE (new_type
, i
) = TYPE_FIELD_BITSIZE (type
, i
);
5677 if (type
->field (i
).type ())
5678 new_type
->field (i
).set_type
5679 (copy_type_recursive (objfile
, type
->field (i
).type (),
5681 if (type
->field (i
).name ())
5682 new_type
->field (i
).set_name (xstrdup (type
->field (i
).name ()));
5684 switch (type
->field (i
).loc_kind ())
5686 case FIELD_LOC_KIND_BITPOS
:
5687 new_type
->field (i
).set_loc_bitpos (type
->field (i
).loc_bitpos ());
5689 case FIELD_LOC_KIND_ENUMVAL
:
5690 new_type
->field (i
).set_loc_enumval (type
->field (i
).loc_enumval ());
5692 case FIELD_LOC_KIND_PHYSADDR
:
5693 new_type
->field (i
).set_loc_physaddr
5694 (type
->field (i
).loc_physaddr ());
5696 case FIELD_LOC_KIND_PHYSNAME
:
5697 new_type
->field (i
).set_loc_physname
5698 (xstrdup (type
->field (i
).loc_physname ()));
5700 case FIELD_LOC_KIND_DWARF_BLOCK
:
5701 new_type
->field (i
).set_loc_dwarf_block
5702 (type
->field (i
).loc_dwarf_block ());
5705 internal_error (__FILE__
, __LINE__
,
5706 _("Unexpected type field location kind: %d"),
5707 type
->field (i
).loc_kind ());
5712 /* For range types, copy the bounds information. */
5713 if (type
->code () == TYPE_CODE_RANGE
)
5715 range_bounds
*bounds
5716 = ((struct range_bounds
*) TYPE_ALLOC
5717 (new_type
, sizeof (struct range_bounds
)));
5719 *bounds
= *type
->bounds ();
5720 new_type
->set_bounds (bounds
);
5723 if (type
->main_type
->dyn_prop_list
!= NULL
)
5724 new_type
->main_type
->dyn_prop_list
5725 = copy_dynamic_prop_list (&objfile
->objfile_obstack
,
5726 type
->main_type
->dyn_prop_list
);
5729 /* Copy pointers to other types. */
5730 if (TYPE_TARGET_TYPE (type
))
5731 TYPE_TARGET_TYPE (new_type
) =
5732 copy_type_recursive (objfile
,
5733 TYPE_TARGET_TYPE (type
),
5736 /* Maybe copy the type_specific bits.
5738 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5739 base classes and methods. There's no fundamental reason why we
5740 can't, but at the moment it is not needed. */
5742 switch (TYPE_SPECIFIC_FIELD (type
))
5744 case TYPE_SPECIFIC_NONE
:
5746 case TYPE_SPECIFIC_FUNC
:
5747 INIT_FUNC_SPECIFIC (new_type
);
5748 TYPE_CALLING_CONVENTION (new_type
) = TYPE_CALLING_CONVENTION (type
);
5749 TYPE_NO_RETURN (new_type
) = TYPE_NO_RETURN (type
);
5750 TYPE_TAIL_CALL_LIST (new_type
) = NULL
;
5752 case TYPE_SPECIFIC_FLOATFORMAT
:
5753 TYPE_FLOATFORMAT (new_type
) = TYPE_FLOATFORMAT (type
);
5755 case TYPE_SPECIFIC_CPLUS_STUFF
:
5756 INIT_CPLUS_SPECIFIC (new_type
);
5758 case TYPE_SPECIFIC_GNAT_STUFF
:
5759 INIT_GNAT_SPECIFIC (new_type
);
5761 case TYPE_SPECIFIC_SELF_TYPE
:
5762 set_type_self_type (new_type
,
5763 copy_type_recursive (objfile
, TYPE_SELF_TYPE (type
),
5766 case TYPE_SPECIFIC_FIXED_POINT
:
5767 INIT_FIXED_POINT_SPECIFIC (new_type
);
5768 new_type
->fixed_point_info ().scaling_factor
5769 = type
->fixed_point_info ().scaling_factor
;
5771 case TYPE_SPECIFIC_INT
:
5772 TYPE_SPECIFIC_FIELD (new_type
) = TYPE_SPECIFIC_INT
;
5773 TYPE_MAIN_TYPE (new_type
)->type_specific
.int_stuff
5774 = TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
;
5778 gdb_assert_not_reached ("bad type_specific_kind");
5784 /* Make a copy of the given TYPE, except that the pointer & reference
5785 types are not preserved.
5787 This function assumes that the given type has an associated objfile.
5788 This objfile is used to allocate the new type. */
5791 copy_type (const struct type
*type
)
5793 struct type
*new_type
;
5795 gdb_assert (type
->is_objfile_owned ());
5797 new_type
= alloc_type_copy (type
);
5798 new_type
->set_instance_flags (type
->instance_flags ());
5799 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5800 memcpy (TYPE_MAIN_TYPE (new_type
), TYPE_MAIN_TYPE (type
),
5801 sizeof (struct main_type
));
5802 if (type
->main_type
->dyn_prop_list
!= NULL
)
5803 new_type
->main_type
->dyn_prop_list
5804 = copy_dynamic_prop_list (&type
->objfile_owner ()->objfile_obstack
,
5805 type
->main_type
->dyn_prop_list
);
5810 /* Helper functions to initialize architecture-specific types. */
5812 /* Allocate a type structure associated with GDBARCH and set its
5813 CODE, LENGTH, and NAME fields. */
5816 arch_type (struct gdbarch
*gdbarch
,
5817 enum type_code code
, int bit
, const char *name
)
5821 type
= alloc_type_arch (gdbarch
);
5822 set_type_code (type
, code
);
5823 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
5824 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
5827 type
->set_name (gdbarch_obstack_strdup (gdbarch
, name
));
5832 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
5833 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5834 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5837 arch_integer_type (struct gdbarch
*gdbarch
,
5838 int bit
, int unsigned_p
, const char *name
)
5842 t
= arch_type (gdbarch
, TYPE_CODE_INT
, bit
, name
);
5844 t
->set_is_unsigned (true);
5849 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
5850 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5851 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5854 arch_character_type (struct gdbarch
*gdbarch
,
5855 int bit
, int unsigned_p
, const char *name
)
5859 t
= arch_type (gdbarch
, TYPE_CODE_CHAR
, bit
, name
);
5861 t
->set_is_unsigned (true);
5866 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
5867 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5868 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5871 arch_boolean_type (struct gdbarch
*gdbarch
,
5872 int bit
, int unsigned_p
, const char *name
)
5876 t
= arch_type (gdbarch
, TYPE_CODE_BOOL
, bit
, name
);
5878 t
->set_is_unsigned (true);
5883 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
5884 BIT is the type size in bits; if BIT equals -1, the size is
5885 determined by the floatformat. NAME is the type name. Set the
5886 TYPE_FLOATFORMAT from FLOATFORMATS. */
5889 arch_float_type (struct gdbarch
*gdbarch
,
5890 int bit
, const char *name
,
5891 const struct floatformat
**floatformats
)
5893 const struct floatformat
*fmt
= floatformats
[gdbarch_byte_order (gdbarch
)];
5896 bit
= verify_floatformat (bit
, fmt
);
5897 t
= arch_type (gdbarch
, TYPE_CODE_FLT
, bit
, name
);
5898 TYPE_FLOATFORMAT (t
) = fmt
;
5903 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
5904 BIT is the type size in bits. NAME is the type name. */
5907 arch_decfloat_type (struct gdbarch
*gdbarch
, int bit
, const char *name
)
5911 t
= arch_type (gdbarch
, TYPE_CODE_DECFLOAT
, bit
, name
);
5915 /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
5916 BIT is the pointer type size in bits. NAME is the type name.
5917 TARGET_TYPE is the pointer target type. Always sets the pointer type's
5918 TYPE_UNSIGNED flag. */
5921 arch_pointer_type (struct gdbarch
*gdbarch
,
5922 int bit
, const char *name
, struct type
*target_type
)
5926 t
= arch_type (gdbarch
, TYPE_CODE_PTR
, bit
, name
);
5927 TYPE_TARGET_TYPE (t
) = target_type
;
5928 t
->set_is_unsigned (true);
5932 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5933 NAME is the type name. BIT is the size of the flag word in bits. */
5936 arch_flags_type (struct gdbarch
*gdbarch
, const char *name
, int bit
)
5940 type
= arch_type (gdbarch
, TYPE_CODE_FLAGS
, bit
, name
);
5941 type
->set_is_unsigned (true);
5942 type
->set_num_fields (0);
5943 /* Pre-allocate enough space assuming every field is one bit. */
5945 ((struct field
*) TYPE_ZALLOC (type
, bit
* sizeof (struct field
)));
5950 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5951 position BITPOS is called NAME. Pass NAME as "" for fields that
5952 should not be printed. */
5955 append_flags_type_field (struct type
*type
, int start_bitpos
, int nr_bits
,
5956 struct type
*field_type
, const char *name
)
5958 int type_bitsize
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
5959 int field_nr
= type
->num_fields ();
5961 gdb_assert (type
->code () == TYPE_CODE_FLAGS
);
5962 gdb_assert (type
->num_fields () + 1 <= type_bitsize
);
5963 gdb_assert (start_bitpos
>= 0 && start_bitpos
< type_bitsize
);
5964 gdb_assert (nr_bits
>= 1 && (start_bitpos
+ nr_bits
) <= type_bitsize
);
5965 gdb_assert (name
!= NULL
);
5967 type
->set_num_fields (type
->num_fields () + 1);
5968 type
->field (field_nr
).set_name (xstrdup (name
));
5969 type
->field (field_nr
).set_type (field_type
);
5970 type
->field (field_nr
).set_loc_bitpos (start_bitpos
);
5971 TYPE_FIELD_BITSIZE (type
, field_nr
) = nr_bits
;
5974 /* Special version of append_flags_type_field to add a flag field.
5975 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5976 position BITPOS is called NAME. */
5979 append_flags_type_flag (struct type
*type
, int bitpos
, const char *name
)
5981 append_flags_type_field (type
, bitpos
, 1,
5982 builtin_type (type
->arch ())->builtin_bool
,
5986 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5987 specified by CODE) associated with GDBARCH. NAME is the type name. */
5990 arch_composite_type (struct gdbarch
*gdbarch
, const char *name
,
5991 enum type_code code
)
5995 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
);
5996 t
= arch_type (gdbarch
, code
, 0, NULL
);
5998 INIT_CPLUS_SPECIFIC (t
);
6002 /* Add new field with name NAME and type FIELD to composite type T.
6003 Do not set the field's position or adjust the type's length;
6004 the caller should do so. Return the new field. */
6007 append_composite_type_field_raw (struct type
*t
, const char *name
,
6012 t
->set_num_fields (t
->num_fields () + 1);
6013 t
->set_fields (XRESIZEVEC (struct field
, t
->fields (),
6015 f
= &t
->field (t
->num_fields () - 1);
6016 memset (f
, 0, sizeof f
[0]);
6017 f
[0].set_type (field
);
6018 f
[0].set_name (name
);
6022 /* Add new field with name NAME and type FIELD to composite type T.
6023 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
6026 append_composite_type_field_aligned (struct type
*t
, const char *name
,
6027 struct type
*field
, int alignment
)
6029 struct field
*f
= append_composite_type_field_raw (t
, name
, field
);
6031 if (t
->code () == TYPE_CODE_UNION
)
6033 if (TYPE_LENGTH (t
) < TYPE_LENGTH (field
))
6034 TYPE_LENGTH (t
) = TYPE_LENGTH (field
);
6036 else if (t
->code () == TYPE_CODE_STRUCT
)
6038 TYPE_LENGTH (t
) = TYPE_LENGTH (t
) + TYPE_LENGTH (field
);
6039 if (t
->num_fields () > 1)
6042 (f
[-1].loc_bitpos () + (TYPE_LENGTH (f
[-1].type ()) * TARGET_CHAR_BIT
));
6048 alignment
*= TARGET_CHAR_BIT
;
6049 left
= f
[0].loc_bitpos () % alignment
;
6053 f
->set_loc_bitpos (f
[0].loc_bitpos () + (alignment
- left
));
6054 TYPE_LENGTH (t
) += (alignment
- left
) / TARGET_CHAR_BIT
;
6061 /* Add new field with name NAME and type FIELD to composite type T. */
6064 append_composite_type_field (struct type
*t
, const char *name
,
6067 append_composite_type_field_aligned (t
, name
, field
, 0);
6072 /* We manage the lifetimes of fixed_point_type_info objects by
6073 attaching them to the objfile. Currently, these objects are
6074 modified during construction, and GMP does not provide a way to
6075 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
6076 them. If we did do this, they could be moved to the per-BFD and
6077 shared across objfiles. */
6078 typedef std::vector
<std::unique_ptr
<fixed_point_type_info
>>
6079 fixed_point_type_storage
;
6081 /* Key used for managing the storage of fixed-point type info. */
6082 static const struct objfile_key
<fixed_point_type_storage
>
6083 fixed_point_objfile_key
;
6085 /* See gdbtypes.h. */
6088 allocate_fixed_point_type_info (struct type
*type
)
6090 std::unique_ptr
<fixed_point_type_info
> up (new fixed_point_type_info
);
6091 fixed_point_type_info
*info
;
6093 if (type
->is_objfile_owned ())
6095 fixed_point_type_storage
*storage
6096 = fixed_point_objfile_key
.get (type
->objfile_owner ());
6097 if (storage
== nullptr)
6098 storage
= fixed_point_objfile_key
.emplace (type
->objfile_owner ());
6100 storage
->push_back (std::move (up
));
6104 /* We just leak the memory, because that's what we do generally
6105 for non-objfile-attached types. */
6106 info
= up
.release ();
6109 type
->set_fixed_point_info (info
);
6112 /* See gdbtypes.h. */
6115 is_fixed_point_type (struct type
*type
)
6117 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6118 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6119 type
= check_typedef (type
);
6121 return type
->code () == TYPE_CODE_FIXED_POINT
;
6124 /* See gdbtypes.h. */
6127 type::fixed_point_type_base_type ()
6129 struct type
*type
= this;
6131 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6132 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6133 type
= check_typedef (type
);
6135 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
);
6139 /* See gdbtypes.h. */
6142 type::fixed_point_scaling_factor ()
6144 struct type
*type
= this->fixed_point_type_base_type ();
6146 return type
->fixed_point_info ().scaling_factor
;
6151 static struct gdbarch_data
*gdbtypes_data
;
6153 const struct builtin_type
*
6154 builtin_type (struct gdbarch
*gdbarch
)
6156 return (const struct builtin_type
*) gdbarch_data (gdbarch
, gdbtypes_data
);
6160 gdbtypes_post_init (struct gdbarch
*gdbarch
)
6162 struct builtin_type
*builtin_type
6163 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_type
);
6166 builtin_type
->builtin_void
6167 = arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6168 builtin_type
->builtin_char
6169 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6170 !gdbarch_char_signed (gdbarch
), "char");
6171 builtin_type
->builtin_char
->set_has_no_signedness (true);
6172 builtin_type
->builtin_signed_char
6173 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6175 builtin_type
->builtin_unsigned_char
6176 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6177 1, "unsigned char");
6178 builtin_type
->builtin_short
6179 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6181 builtin_type
->builtin_unsigned_short
6182 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6183 1, "unsigned short");
6184 builtin_type
->builtin_int
6185 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6187 builtin_type
->builtin_unsigned_int
6188 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6190 builtin_type
->builtin_long
6191 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6193 builtin_type
->builtin_unsigned_long
6194 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6195 1, "unsigned long");
6196 builtin_type
->builtin_long_long
6197 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6199 builtin_type
->builtin_unsigned_long_long
6200 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6201 1, "unsigned long long");
6202 builtin_type
->builtin_half
6203 = arch_float_type (gdbarch
, gdbarch_half_bit (gdbarch
),
6204 "half", gdbarch_half_format (gdbarch
));
6205 builtin_type
->builtin_float
6206 = arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
),
6207 "float", gdbarch_float_format (gdbarch
));
6208 builtin_type
->builtin_bfloat16
6209 = arch_float_type (gdbarch
, gdbarch_bfloat16_bit (gdbarch
),
6210 "bfloat16", gdbarch_bfloat16_format (gdbarch
));
6211 builtin_type
->builtin_double
6212 = arch_float_type (gdbarch
, gdbarch_double_bit (gdbarch
),
6213 "double", gdbarch_double_format (gdbarch
));
6214 builtin_type
->builtin_long_double
6215 = arch_float_type (gdbarch
, gdbarch_long_double_bit (gdbarch
),
6216 "long double", gdbarch_long_double_format (gdbarch
));
6217 builtin_type
->builtin_complex
6218 = init_complex_type ("complex", builtin_type
->builtin_float
);
6219 builtin_type
->builtin_double_complex
6220 = init_complex_type ("double complex", builtin_type
->builtin_double
);
6221 builtin_type
->builtin_string
6222 = arch_type (gdbarch
, TYPE_CODE_STRING
, TARGET_CHAR_BIT
, "string");
6223 builtin_type
->builtin_bool
6224 = arch_boolean_type (gdbarch
, TARGET_CHAR_BIT
, 1, "bool");
6226 /* The following three are about decimal floating point types, which
6227 are 32-bits, 64-bits and 128-bits respectively. */
6228 builtin_type
->builtin_decfloat
6229 = arch_decfloat_type (gdbarch
, 32, "_Decimal32");
6230 builtin_type
->builtin_decdouble
6231 = arch_decfloat_type (gdbarch
, 64, "_Decimal64");
6232 builtin_type
->builtin_declong
6233 = arch_decfloat_type (gdbarch
, 128, "_Decimal128");
6235 /* "True" character types. */
6236 builtin_type
->builtin_true_char
6237 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 0, "true character");
6238 builtin_type
->builtin_true_unsigned_char
6239 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 1, "true character");
6241 /* Fixed-size integer types. */
6242 builtin_type
->builtin_int0
6243 = arch_integer_type (gdbarch
, 0, 0, "int0_t");
6244 builtin_type
->builtin_int8
6245 = arch_integer_type (gdbarch
, 8, 0, "int8_t");
6246 builtin_type
->builtin_uint8
6247 = arch_integer_type (gdbarch
, 8, 1, "uint8_t");
6248 builtin_type
->builtin_int16
6249 = arch_integer_type (gdbarch
, 16, 0, "int16_t");
6250 builtin_type
->builtin_uint16
6251 = arch_integer_type (gdbarch
, 16, 1, "uint16_t");
6252 builtin_type
->builtin_int24
6253 = arch_integer_type (gdbarch
, 24, 0, "int24_t");
6254 builtin_type
->builtin_uint24
6255 = arch_integer_type (gdbarch
, 24, 1, "uint24_t");
6256 builtin_type
->builtin_int32
6257 = arch_integer_type (gdbarch
, 32, 0, "int32_t");
6258 builtin_type
->builtin_uint32
6259 = arch_integer_type (gdbarch
, 32, 1, "uint32_t");
6260 builtin_type
->builtin_int64
6261 = arch_integer_type (gdbarch
, 64, 0, "int64_t");
6262 builtin_type
->builtin_uint64
6263 = arch_integer_type (gdbarch
, 64, 1, "uint64_t");
6264 builtin_type
->builtin_int128
6265 = arch_integer_type (gdbarch
, 128, 0, "int128_t");
6266 builtin_type
->builtin_uint128
6267 = arch_integer_type (gdbarch
, 128, 1, "uint128_t");
6269 builtin_type
->builtin_int8
->set_instance_flags
6270 (builtin_type
->builtin_int8
->instance_flags ()
6271 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6273 builtin_type
->builtin_uint8
->set_instance_flags
6274 (builtin_type
->builtin_uint8
->instance_flags ()
6275 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6277 /* Wide character types. */
6278 builtin_type
->builtin_char16
6279 = arch_integer_type (gdbarch
, 16, 1, "char16_t");
6280 builtin_type
->builtin_char32
6281 = arch_integer_type (gdbarch
, 32, 1, "char32_t");
6282 builtin_type
->builtin_wchar
6283 = arch_integer_type (gdbarch
, gdbarch_wchar_bit (gdbarch
),
6284 !gdbarch_wchar_signed (gdbarch
), "wchar_t");
6286 /* Default data/code pointer types. */
6287 builtin_type
->builtin_data_ptr
6288 = lookup_pointer_type (builtin_type
->builtin_void
);
6289 builtin_type
->builtin_func_ptr
6290 = lookup_pointer_type (lookup_function_type (builtin_type
->builtin_void
));
6291 builtin_type
->builtin_func_func
6292 = lookup_function_type (builtin_type
->builtin_func_ptr
);
6294 /* This type represents a GDB internal function. */
6295 builtin_type
->internal_fn
6296 = arch_type (gdbarch
, TYPE_CODE_INTERNAL_FUNCTION
, 0,
6297 "<internal function>");
6299 /* This type represents an xmethod. */
6300 builtin_type
->xmethod
6301 = arch_type (gdbarch
, TYPE_CODE_XMETHOD
, 0, "<xmethod>");
6303 return builtin_type
;
6306 /* This set of objfile-based types is intended to be used by symbol
6307 readers as basic types. */
6309 static const struct objfile_key
<struct objfile_type
,
6310 gdb::noop_deleter
<struct objfile_type
>>
6313 const struct objfile_type
*
6314 objfile_type (struct objfile
*objfile
)
6316 struct gdbarch
*gdbarch
;
6317 struct objfile_type
*objfile_type
= objfile_type_data
.get (objfile
);
6320 return objfile_type
;
6322 objfile_type
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
6323 1, struct objfile_type
);
6325 /* Use the objfile architecture to determine basic type properties. */
6326 gdbarch
= objfile
->arch ();
6329 objfile_type
->builtin_void
6330 = init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6331 objfile_type
->builtin_char
6332 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6333 !gdbarch_char_signed (gdbarch
), "char");
6334 objfile_type
->builtin_char
->set_has_no_signedness (true);
6335 objfile_type
->builtin_signed_char
6336 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6338 objfile_type
->builtin_unsigned_char
6339 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6340 1, "unsigned char");
6341 objfile_type
->builtin_short
6342 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6344 objfile_type
->builtin_unsigned_short
6345 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6346 1, "unsigned short");
6347 objfile_type
->builtin_int
6348 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6350 objfile_type
->builtin_unsigned_int
6351 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6353 objfile_type
->builtin_long
6354 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6356 objfile_type
->builtin_unsigned_long
6357 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6358 1, "unsigned long");
6359 objfile_type
->builtin_long_long
6360 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6362 objfile_type
->builtin_unsigned_long_long
6363 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6364 1, "unsigned long long");
6365 objfile_type
->builtin_float
6366 = init_float_type (objfile
, gdbarch_float_bit (gdbarch
),
6367 "float", gdbarch_float_format (gdbarch
));
6368 objfile_type
->builtin_double
6369 = init_float_type (objfile
, gdbarch_double_bit (gdbarch
),
6370 "double", gdbarch_double_format (gdbarch
));
6371 objfile_type
->builtin_long_double
6372 = init_float_type (objfile
, gdbarch_long_double_bit (gdbarch
),
6373 "long double", gdbarch_long_double_format (gdbarch
));
6375 /* This type represents a type that was unrecognized in symbol read-in. */
6376 objfile_type
->builtin_error
6377 = init_type (objfile
, TYPE_CODE_ERROR
, 0, "<unknown type>");
6379 /* The following set of types is used for symbols with no
6380 debug information. */
6381 objfile_type
->nodebug_text_symbol
6382 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6383 "<text variable, no debug info>");
6385 objfile_type
->nodebug_text_gnu_ifunc_symbol
6386 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6387 "<text gnu-indirect-function variable, no debug info>");
6388 objfile_type
->nodebug_text_gnu_ifunc_symbol
->set_is_gnu_ifunc (true);
6390 objfile_type
->nodebug_got_plt_symbol
6391 = init_pointer_type (objfile
, gdbarch_addr_bit (gdbarch
),
6392 "<text from jump slot in .got.plt, no debug info>",
6393 objfile_type
->nodebug_text_symbol
);
6394 objfile_type
->nodebug_data_symbol
6395 = init_nodebug_var_type (objfile
, "<data variable, no debug info>");
6396 objfile_type
->nodebug_unknown_symbol
6397 = init_nodebug_var_type (objfile
, "<variable (not text or data), no debug info>");
6398 objfile_type
->nodebug_tls_symbol
6399 = init_nodebug_var_type (objfile
, "<thread local variable, no debug info>");
6401 /* NOTE: on some targets, addresses and pointers are not necessarily
6405 - gdb's `struct type' always describes the target's
6407 - gdb's `struct value' objects should always hold values in
6409 - gdb's CORE_ADDR values are addresses in the unified virtual
6410 address space that the assembler and linker work with. Thus,
6411 since target_read_memory takes a CORE_ADDR as an argument, it
6412 can access any memory on the target, even if the processor has
6413 separate code and data address spaces.
6415 In this context, objfile_type->builtin_core_addr is a bit odd:
6416 it's a target type for a value the target will never see. It's
6417 only used to hold the values of (typeless) linker symbols, which
6418 are indeed in the unified virtual address space. */
6420 objfile_type
->builtin_core_addr
6421 = init_integer_type (objfile
, gdbarch_addr_bit (gdbarch
), 1,
6424 objfile_type_data
.set (objfile
, objfile_type
);
6425 return objfile_type
;
6428 /* See gdbtypes.h. */
6431 call_site::pc () const
6433 compunit_symtab
*cust
= this->per_objfile
->get_symtab (this->per_cu
);
6435 = this->per_objfile
->objfile
->section_offsets
[cust
->block_line_section ()];
6436 return m_unrelocated_pc
+ delta
;
6439 void _initialize_gdbtypes ();
6441 _initialize_gdbtypes ()
6443 gdbtypes_data
= gdbarch_data_register_post_init (gdbtypes_post_init
);
6445 add_setshow_zuinteger_cmd ("overload", no_class
, &overload_debug
,
6446 _("Set debugging of C++ overloading."),
6447 _("Show debugging of C++ overloading."),
6448 _("When enabled, ranking of the "
6449 "functions is displayed."),
6451 show_overload_debug
,
6452 &setdebuglist
, &showdebuglist
);
6454 /* Add user knob for controlling resolution of opaque types. */
6455 add_setshow_boolean_cmd ("opaque-type-resolution", class_support
,
6456 &opaque_type_resolution
,
6457 _("Set resolution of opaque struct/class/union"
6458 " types (if set before loading symbols)."),
6459 _("Show resolution of opaque struct/class/union"
6460 " types (if set before loading symbols)."),
6462 show_opaque_type_resolution
,
6463 &setlist
, &showlist
);
6465 /* Add an option to permit non-strict type checking. */
6466 add_setshow_boolean_cmd ("type", class_support
,
6467 &strict_type_checking
,
6468 _("Set strict type checking."),
6469 _("Show strict type checking."),
6471 show_strict_type_checking
,
6472 &setchecklist
, &showchecklist
);