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 static struct type
*
2200 resolve_dynamic_range (struct type
*dyn_range_type
,
2201 struct property_addr_info
*addr_stack
,
2202 int rank
, bool resolve_p
= true)
2205 struct type
*static_range_type
, *static_target_type
;
2206 struct dynamic_prop low_bound
, high_bound
, stride
;
2208 gdb_assert (dyn_range_type
->code () == TYPE_CODE_RANGE
);
2210 const struct dynamic_prop
*prop
= &dyn_range_type
->bounds ()->low
;
2211 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2212 { (CORE_ADDR
) rank
}))
2213 low_bound
.set_const_val (value
);
2215 low_bound
.set_undefined ();
2217 prop
= &dyn_range_type
->bounds ()->high
;
2218 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2219 { (CORE_ADDR
) rank
}))
2221 high_bound
.set_const_val (value
);
2223 if (dyn_range_type
->bounds ()->flag_upper_bound_is_count
)
2224 high_bound
.set_const_val
2225 (low_bound
.const_val () + high_bound
.const_val () - 1);
2228 high_bound
.set_undefined ();
2230 bool byte_stride_p
= dyn_range_type
->bounds ()->flag_is_byte_stride
;
2231 prop
= &dyn_range_type
->bounds ()->stride
;
2232 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2233 { (CORE_ADDR
) rank
}))
2235 stride
.set_const_val (value
);
2237 /* If we have a bit stride that is not an exact number of bytes then
2238 I really don't think this is going to work with current GDB, the
2239 array indexing code in GDB seems to be pretty heavily tied to byte
2240 offsets right now. Assuming 8 bits in a byte. */
2241 struct gdbarch
*gdbarch
= dyn_range_type
->arch ();
2242 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
2243 if (!byte_stride_p
&& (value
% (unit_size
* 8)) != 0)
2244 error (_("bit strides that are not a multiple of the byte size "
2245 "are currently not supported"));
2249 stride
.set_undefined ();
2250 byte_stride_p
= true;
2254 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type
),
2256 LONGEST bias
= dyn_range_type
->bounds ()->bias
;
2257 static_range_type
= create_range_type_with_stride
2258 (copy_type (dyn_range_type
), static_target_type
,
2259 &low_bound
, &high_bound
, bias
, &stride
, byte_stride_p
);
2260 static_range_type
->bounds ()->flag_bound_evaluated
= 1;
2261 return static_range_type
;
2264 /* Helper function for resolve_dynamic_array_or_string. This function
2265 resolves the properties for a single array at RANK within a nested array
2266 of arrays structure. The RANK value is always greater than 0, and
2267 starts at it's maximum value and goes down by 1 for each recursive call
2268 to this function. So, for a 3-dimensional array, the first call to this
2269 function has RANK == 3, then we call ourselves recursively with RANK ==
2270 2, than again with RANK == 1, and at that point we should return.
2272 TYPE is updated as the dynamic properties are resolved, and so, should
2273 be a copy of the dynamic type, rather than the original dynamic type
2276 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2277 during the dynamic resolution.
2279 When RESOLVE_P is true then the dynamic properties of TYPE are
2280 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2281 instead we assume the array is not allocated/associated yet. */
2283 static struct type
*
2284 resolve_dynamic_array_or_string_1 (struct type
*type
,
2285 struct property_addr_info
*addr_stack
,
2286 int rank
, bool resolve_p
)
2289 struct type
*elt_type
;
2290 struct type
*range_type
;
2291 struct type
*ary_dim
;
2292 struct dynamic_prop
*prop
;
2293 unsigned int bit_stride
= 0;
2295 /* For dynamic type resolution strings can be treated like arrays of
2297 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2298 || type
->code () == TYPE_CODE_STRING
);
2300 /* The outer resolve_dynamic_array_or_string should ensure we always have
2301 a rank of at least 1 when we get here. */
2302 gdb_assert (rank
> 0);
2304 /* Resolve the allocated and associated properties before doing anything
2305 else. If an array is not allocated or not associated then (at least
2306 for Fortran) there is no guarantee that the data to define the upper
2307 bound, lower bound, or stride will be correct. If RESOLVE_P is
2308 already false at this point then this is not the first dimension of
2309 the array and a more outer dimension has already marked this array as
2310 not allocated/associated, as such we just ignore this property. This
2311 is fine as GDB only checks the allocated/associated on the outer most
2312 dimension of the array. */
2313 prop
= TYPE_ALLOCATED_PROP (type
);
2314 if (prop
!= NULL
&& resolve_p
2315 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2317 prop
->set_const_val (value
);
2322 prop
= TYPE_ASSOCIATED_PROP (type
);
2323 if (prop
!= NULL
&& resolve_p
2324 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2326 prop
->set_const_val (value
);
2331 range_type
= check_typedef (type
->index_type ());
2333 = resolve_dynamic_range (range_type
, addr_stack
, rank
, resolve_p
);
2335 ary_dim
= check_typedef (TYPE_TARGET_TYPE (type
));
2336 if (ary_dim
!= NULL
&& ary_dim
->code () == TYPE_CODE_ARRAY
)
2338 ary_dim
= copy_type (ary_dim
);
2339 elt_type
= resolve_dynamic_array_or_string_1 (ary_dim
, addr_stack
,
2340 rank
- 1, resolve_p
);
2343 elt_type
= TYPE_TARGET_TYPE (type
);
2345 prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2346 if (prop
!= NULL
&& resolve_p
)
2348 if (dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2350 type
->remove_dyn_prop (DYN_PROP_BYTE_STRIDE
);
2351 bit_stride
= (unsigned int) (value
* 8);
2355 /* Could be a bug in our code, but it could also happen
2356 if the DWARF info is not correct. Issue a warning,
2357 and assume no byte/bit stride (leave bit_stride = 0). */
2358 warning (_("cannot determine array stride for type %s"),
2359 type
->name () ? type
->name () : "<no name>");
2363 bit_stride
= TYPE_FIELD_BITSIZE (type
, 0);
2365 return create_array_type_with_stride (type
, elt_type
, range_type
, NULL
,
2369 /* Resolve an array or string type with dynamic properties, return a new
2370 type with the dynamic properties resolved to actual values. The
2371 ADDR_STACK represents the location of the object being resolved. */
2373 static struct type
*
2374 resolve_dynamic_array_or_string (struct type
*type
,
2375 struct property_addr_info
*addr_stack
)
2380 /* For dynamic type resolution strings can be treated like arrays of
2382 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2383 || type
->code () == TYPE_CODE_STRING
);
2385 type
= copy_type (type
);
2387 /* Resolve the rank property to get rank value. */
2388 struct dynamic_prop
*prop
= TYPE_RANK_PROP (type
);
2389 if (dwarf2_evaluate_property (prop
, nullptr, addr_stack
, &value
))
2391 prop
->set_const_val (value
);
2396 /* The dynamic property list juggling below was from the original
2397 patch. I don't understand what this is all about, so I've
2398 commented it out for now and added the following error. */
2399 error (_("failed to resolve dynamic array rank"));
2401 else if (type
->code () == TYPE_CODE_STRING
&& rank
!= 1)
2403 /* What would this even mean? A string with a dynamic rank
2405 error (_("unable to handle string with dynamic rank greater than 1"));
2409 /* Arrays with dynamic rank are initially just an array type
2410 with a target type that is the array element.
2412 However, now we know the rank of the array we need to build
2413 the array of arrays structure that GDB expects, that is we
2414 need an array type that has a target which is an array type,
2415 and so on, until eventually, we have the element type at the
2416 end of the chain. Create all the additional array types here
2417 by copying the top level array type. */
2418 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2419 struct type
*rank_type
= type
;
2420 for (int i
= 1; i
< rank
; i
++)
2422 TYPE_TARGET_TYPE (rank_type
) = copy_type (rank_type
);
2423 rank_type
= TYPE_TARGET_TYPE (rank_type
);
2425 TYPE_TARGET_TYPE (rank_type
) = element_type
;
2432 for (struct type
*tmp_type
= check_typedef (TYPE_TARGET_TYPE (type
));
2433 tmp_type
->code () == TYPE_CODE_ARRAY
;
2434 tmp_type
= check_typedef (TYPE_TARGET_TYPE (tmp_type
)))
2438 return resolve_dynamic_array_or_string_1 (type
, addr_stack
, rank
, true);
2441 /* Resolve dynamic bounds of members of the union TYPE to static
2442 bounds. ADDR_STACK is a stack of struct property_addr_info
2443 to be used if needed during the dynamic resolution. */
2445 static struct type
*
2446 resolve_dynamic_union (struct type
*type
,
2447 struct property_addr_info
*addr_stack
)
2449 struct type
*resolved_type
;
2451 unsigned int max_len
= 0;
2453 gdb_assert (type
->code () == TYPE_CODE_UNION
);
2455 resolved_type
= copy_type (type
);
2456 resolved_type
->set_fields
2458 TYPE_ALLOC (resolved_type
,
2459 resolved_type
->num_fields () * sizeof (struct field
)));
2460 memcpy (resolved_type
->fields (),
2462 resolved_type
->num_fields () * sizeof (struct field
));
2463 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2467 if (field_is_static (&type
->field (i
)))
2470 t
= resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2472 resolved_type
->field (i
).set_type (t
);
2474 struct type
*real_type
= check_typedef (t
);
2475 if (TYPE_LENGTH (real_type
) > max_len
)
2476 max_len
= TYPE_LENGTH (real_type
);
2479 TYPE_LENGTH (resolved_type
) = max_len
;
2480 return resolved_type
;
2483 /* See gdbtypes.h. */
2486 variant::matches (ULONGEST value
, bool is_unsigned
) const
2488 for (const discriminant_range
&range
: discriminants
)
2489 if (range
.contains (value
, is_unsigned
))
2495 compute_variant_fields_inner (struct type
*type
,
2496 struct property_addr_info
*addr_stack
,
2497 const variant_part
&part
,
2498 std::vector
<bool> &flags
);
2500 /* A helper function to determine which variant fields will be active.
2501 This handles both the variant's direct fields, and any variant
2502 parts embedded in this variant. TYPE is the type we're examining.
2503 ADDR_STACK holds information about the concrete object. VARIANT is
2504 the current variant to be handled. FLAGS is where the results are
2505 stored -- this function sets the Nth element in FLAGS if the
2506 corresponding field is enabled. ENABLED is whether this variant is
2510 compute_variant_fields_recurse (struct type
*type
,
2511 struct property_addr_info
*addr_stack
,
2512 const variant
&variant
,
2513 std::vector
<bool> &flags
,
2516 for (int field
= variant
.first_field
; field
< variant
.last_field
; ++field
)
2517 flags
[field
] = enabled
;
2519 for (const variant_part
&new_part
: variant
.parts
)
2522 compute_variant_fields_inner (type
, addr_stack
, new_part
, flags
);
2525 for (const auto &sub_variant
: new_part
.variants
)
2526 compute_variant_fields_recurse (type
, addr_stack
, sub_variant
,
2532 /* A helper function to determine which variant fields will be active.
2533 This evaluates the discriminant, decides which variant (if any) is
2534 active, and then updates FLAGS to reflect which fields should be
2535 available. TYPE is the type we're examining. ADDR_STACK holds
2536 information about the concrete object. VARIANT is the current
2537 variant to be handled. FLAGS is where the results are stored --
2538 this function sets the Nth element in FLAGS if the corresponding
2539 field is enabled. */
2542 compute_variant_fields_inner (struct type
*type
,
2543 struct property_addr_info
*addr_stack
,
2544 const variant_part
&part
,
2545 std::vector
<bool> &flags
)
2547 /* Evaluate the discriminant. */
2548 gdb::optional
<ULONGEST
> discr_value
;
2549 if (part
.discriminant_index
!= -1)
2551 int idx
= part
.discriminant_index
;
2553 if (type
->field (idx
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2554 error (_("Cannot determine struct field location"
2555 " (invalid location kind)"));
2557 if (addr_stack
->valaddr
.data () != NULL
)
2558 discr_value
= unpack_field_as_long (type
, addr_stack
->valaddr
.data (),
2562 CORE_ADDR addr
= (addr_stack
->addr
2563 + (type
->field (idx
).loc_bitpos ()
2564 / TARGET_CHAR_BIT
));
2566 LONGEST bitsize
= TYPE_FIELD_BITSIZE (type
, idx
);
2567 LONGEST size
= bitsize
/ 8;
2569 size
= TYPE_LENGTH (type
->field (idx
).type ());
2571 gdb_byte bits
[sizeof (ULONGEST
)];
2572 read_memory (addr
, bits
, size
);
2574 LONGEST bitpos
= (type
->field (idx
).loc_bitpos ()
2577 discr_value
= unpack_bits_as_long (type
->field (idx
).type (),
2578 bits
, bitpos
, bitsize
);
2582 /* Go through each variant and see which applies. */
2583 const variant
*default_variant
= nullptr;
2584 const variant
*applied_variant
= nullptr;
2585 for (const auto &variant
: part
.variants
)
2587 if (variant
.is_default ())
2588 default_variant
= &variant
;
2589 else if (discr_value
.has_value ()
2590 && variant
.matches (*discr_value
, part
.is_unsigned
))
2592 applied_variant
= &variant
;
2596 if (applied_variant
== nullptr)
2597 applied_variant
= default_variant
;
2599 for (const auto &variant
: part
.variants
)
2600 compute_variant_fields_recurse (type
, addr_stack
, variant
,
2601 flags
, applied_variant
== &variant
);
2604 /* Determine which variant fields are available in TYPE. The enabled
2605 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2606 about the concrete object. PARTS describes the top-level variant
2607 parts for this type. */
2610 compute_variant_fields (struct type
*type
,
2611 struct type
*resolved_type
,
2612 struct property_addr_info
*addr_stack
,
2613 const gdb::array_view
<variant_part
> &parts
)
2615 /* Assume all fields are included by default. */
2616 std::vector
<bool> flags (resolved_type
->num_fields (), true);
2618 /* Now disable fields based on the variants that control them. */
2619 for (const auto &part
: parts
)
2620 compute_variant_fields_inner (type
, addr_stack
, part
, flags
);
2622 resolved_type
->set_num_fields
2623 (std::count (flags
.begin (), flags
.end (), true));
2624 resolved_type
->set_fields
2626 TYPE_ALLOC (resolved_type
,
2627 resolved_type
->num_fields () * sizeof (struct field
)));
2630 for (int i
= 0; i
< type
->num_fields (); ++i
)
2635 resolved_type
->field (out
) = type
->field (i
);
2640 /* Resolve dynamic bounds of members of the struct TYPE to static
2641 bounds. ADDR_STACK is a stack of struct property_addr_info to
2642 be used if needed during the dynamic resolution. */
2644 static struct type
*
2645 resolve_dynamic_struct (struct type
*type
,
2646 struct property_addr_info
*addr_stack
)
2648 struct type
*resolved_type
;
2650 unsigned resolved_type_bit_length
= 0;
2652 gdb_assert (type
->code () == TYPE_CODE_STRUCT
);
2654 resolved_type
= copy_type (type
);
2656 dynamic_prop
*variant_prop
= resolved_type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2657 if (variant_prop
!= nullptr && variant_prop
->kind () == PROP_VARIANT_PARTS
)
2659 compute_variant_fields (type
, resolved_type
, addr_stack
,
2660 *variant_prop
->variant_parts ());
2661 /* We want to leave the property attached, so that the Rust code
2662 can tell whether the type was originally an enum. */
2663 variant_prop
->set_original_type (type
);
2667 resolved_type
->set_fields
2669 TYPE_ALLOC (resolved_type
,
2670 resolved_type
->num_fields () * sizeof (struct field
)));
2671 if (type
->num_fields () > 0)
2672 memcpy (resolved_type
->fields (),
2674 resolved_type
->num_fields () * sizeof (struct field
));
2677 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2679 unsigned new_bit_length
;
2680 struct property_addr_info pinfo
;
2682 if (field_is_static (&resolved_type
->field (i
)))
2685 if (resolved_type
->field (i
).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK
)
2687 struct dwarf2_property_baton baton
;
2689 = lookup_pointer_type (resolved_type
->field (i
).type ());
2690 baton
.locexpr
= *resolved_type
->field (i
).loc_dwarf_block ();
2692 struct dynamic_prop prop
;
2693 prop
.set_locexpr (&baton
);
2696 if (dwarf2_evaluate_property (&prop
, nullptr, addr_stack
, &addr
,
2697 {addr_stack
->addr
}))
2698 resolved_type
->field (i
).set_loc_bitpos
2699 (TARGET_CHAR_BIT
* (addr
- addr_stack
->addr
));
2702 /* As we know this field is not a static field, the field's
2703 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2704 this is the case, but only trigger a simple error rather
2705 than an internal error if that fails. While failing
2706 that verification indicates a bug in our code, the error
2707 is not severe enough to suggest to the user he stops
2708 his debugging session because of it. */
2709 if (resolved_type
->field (i
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2710 error (_("Cannot determine struct field location"
2711 " (invalid location kind)"));
2713 pinfo
.type
= check_typedef (resolved_type
->field (i
).type ());
2714 size_t offset
= resolved_type
->field (i
).loc_bitpos () / TARGET_CHAR_BIT
;
2715 pinfo
.valaddr
= addr_stack
->valaddr
;
2716 if (!pinfo
.valaddr
.empty ())
2717 pinfo
.valaddr
= pinfo
.valaddr
.slice (offset
);
2718 pinfo
.addr
= addr_stack
->addr
+ offset
;
2719 pinfo
.next
= addr_stack
;
2721 resolved_type
->field (i
).set_type
2722 (resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2724 gdb_assert (resolved_type
->field (i
).loc_kind ()
2725 == FIELD_LOC_KIND_BITPOS
);
2727 new_bit_length
= resolved_type
->field (i
).loc_bitpos ();
2728 if (TYPE_FIELD_BITSIZE (resolved_type
, i
) != 0)
2729 new_bit_length
+= TYPE_FIELD_BITSIZE (resolved_type
, i
);
2732 struct type
*real_type
2733 = check_typedef (resolved_type
->field (i
).type ());
2735 new_bit_length
+= (TYPE_LENGTH (real_type
) * TARGET_CHAR_BIT
);
2738 /* Normally, we would use the position and size of the last field
2739 to determine the size of the enclosing structure. But GCC seems
2740 to be encoding the position of some fields incorrectly when
2741 the struct contains a dynamic field that is not placed last.
2742 So we compute the struct size based on the field that has
2743 the highest position + size - probably the best we can do. */
2744 if (new_bit_length
> resolved_type_bit_length
)
2745 resolved_type_bit_length
= new_bit_length
;
2748 /* The length of a type won't change for fortran, but it does for C and Ada.
2749 For fortran the size of dynamic fields might change over time but not the
2750 type length of the structure. If we adapt it, we run into problems
2751 when calculating the element offset for arrays of structs. */
2752 if (current_language
->la_language
!= language_fortran
)
2753 TYPE_LENGTH (resolved_type
)
2754 = (resolved_type_bit_length
+ TARGET_CHAR_BIT
- 1) / TARGET_CHAR_BIT
;
2756 /* The Ada language uses this field as a cache for static fixed types: reset
2757 it as RESOLVED_TYPE must have its own static fixed type. */
2758 TYPE_TARGET_TYPE (resolved_type
) = NULL
;
2760 return resolved_type
;
2763 /* Worker for resolved_dynamic_type. */
2765 static struct type
*
2766 resolve_dynamic_type_internal (struct type
*type
,
2767 struct property_addr_info
*addr_stack
,
2770 struct type
*real_type
= check_typedef (type
);
2771 struct type
*resolved_type
= nullptr;
2772 struct dynamic_prop
*prop
;
2775 if (!is_dynamic_type_internal (real_type
, top_level
))
2778 gdb::optional
<CORE_ADDR
> type_length
;
2779 prop
= TYPE_DYNAMIC_LENGTH (type
);
2781 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2782 type_length
= value
;
2784 if (type
->code () == TYPE_CODE_TYPEDEF
)
2786 resolved_type
= copy_type (type
);
2787 TYPE_TARGET_TYPE (resolved_type
)
2788 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
), addr_stack
,
2793 /* Before trying to resolve TYPE, make sure it is not a stub. */
2796 switch (type
->code ())
2800 struct property_addr_info pinfo
;
2802 pinfo
.type
= check_typedef (TYPE_TARGET_TYPE (type
));
2804 if (addr_stack
->valaddr
.data () != NULL
)
2805 pinfo
.addr
= extract_typed_address (addr_stack
->valaddr
.data (),
2808 pinfo
.addr
= read_memory_typed_address (addr_stack
->addr
, type
);
2809 pinfo
.next
= addr_stack
;
2811 resolved_type
= copy_type (type
);
2812 TYPE_TARGET_TYPE (resolved_type
)
2813 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
),
2818 case TYPE_CODE_STRING
:
2819 /* Strings are very much like an array of characters, and can be
2820 treated as one here. */
2821 case TYPE_CODE_ARRAY
:
2822 resolved_type
= resolve_dynamic_array_or_string (type
, addr_stack
);
2825 case TYPE_CODE_RANGE
:
2826 /* Pass 1 for the rank value here. The assumption is that this
2827 rank value is not actually required for the resolution of the
2828 dynamic range, otherwise, we'd be resolving this range within
2829 the context of a dynamic array. */
2830 resolved_type
= resolve_dynamic_range (type
, addr_stack
, 1);
2833 case TYPE_CODE_UNION
:
2834 resolved_type
= resolve_dynamic_union (type
, addr_stack
);
2837 case TYPE_CODE_STRUCT
:
2838 resolved_type
= resolve_dynamic_struct (type
, addr_stack
);
2843 if (resolved_type
== nullptr)
2846 if (type_length
.has_value ())
2848 TYPE_LENGTH (resolved_type
) = *type_length
;
2849 resolved_type
->remove_dyn_prop (DYN_PROP_BYTE_SIZE
);
2852 /* Resolve data_location attribute. */
2853 prop
= TYPE_DATA_LOCATION (resolved_type
);
2855 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2857 /* Start of Fortran hack. See comment in f-lang.h for what is going
2859 if (current_language
->la_language
== language_fortran
2860 && resolved_type
->code () == TYPE_CODE_ARRAY
)
2861 value
= fortran_adjust_dynamic_array_base_address_hack (resolved_type
,
2863 /* End of Fortran hack. */
2864 prop
->set_const_val (value
);
2867 return resolved_type
;
2870 /* See gdbtypes.h */
2873 resolve_dynamic_type (struct type
*type
,
2874 gdb::array_view
<const gdb_byte
> valaddr
,
2877 struct property_addr_info pinfo
2878 = {check_typedef (type
), valaddr
, addr
, NULL
};
2880 return resolve_dynamic_type_internal (type
, &pinfo
, 1);
2883 /* See gdbtypes.h */
2886 type::dyn_prop (dynamic_prop_node_kind prop_kind
) const
2888 dynamic_prop_list
*node
= this->main_type
->dyn_prop_list
;
2890 while (node
!= NULL
)
2892 if (node
->prop_kind
== prop_kind
)
2899 /* See gdbtypes.h */
2902 type::add_dyn_prop (dynamic_prop_node_kind prop_kind
, dynamic_prop prop
)
2904 struct dynamic_prop_list
*temp
;
2906 gdb_assert (this->is_objfile_owned ());
2908 temp
= XOBNEW (&this->objfile_owner ()->objfile_obstack
,
2909 struct dynamic_prop_list
);
2910 temp
->prop_kind
= prop_kind
;
2912 temp
->next
= this->main_type
->dyn_prop_list
;
2914 this->main_type
->dyn_prop_list
= temp
;
2917 /* See gdbtypes.h. */
2920 type::remove_dyn_prop (dynamic_prop_node_kind kind
)
2922 struct dynamic_prop_list
*prev_node
, *curr_node
;
2924 curr_node
= this->main_type
->dyn_prop_list
;
2927 while (NULL
!= curr_node
)
2929 if (curr_node
->prop_kind
== kind
)
2931 /* Update the linked list but don't free anything.
2932 The property was allocated on objstack and it is not known
2933 if we are on top of it. Nevertheless, everything is released
2934 when the complete objstack is freed. */
2935 if (NULL
== prev_node
)
2936 this->main_type
->dyn_prop_list
= curr_node
->next
;
2938 prev_node
->next
= curr_node
->next
;
2943 prev_node
= curr_node
;
2944 curr_node
= curr_node
->next
;
2948 /* Find the real type of TYPE. This function returns the real type,
2949 after removing all layers of typedefs, and completing opaque or stub
2950 types. Completion changes the TYPE argument, but stripping of
2953 Instance flags (e.g. const/volatile) are preserved as typedefs are
2954 stripped. If necessary a new qualified form of the underlying type
2957 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
2958 not been computed and we're either in the middle of reading symbols, or
2959 there was no name for the typedef in the debug info.
2961 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2962 QUITs in the symbol reading code can also throw.
2963 Thus this function can throw an exception.
2965 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2968 If this is a stubbed struct (i.e. declared as struct foo *), see if
2969 we can find a full definition in some other file. If so, copy this
2970 definition, so we can use it in future. There used to be a comment
2971 (but not any code) that if we don't find a full definition, we'd
2972 set a flag so we don't spend time in the future checking the same
2973 type. That would be a mistake, though--we might load in more
2974 symbols which contain a full definition for the type. */
2977 check_typedef (struct type
*type
)
2979 struct type
*orig_type
= type
;
2983 /* While we're removing typedefs, we don't want to lose qualifiers.
2984 E.g., const/volatile. */
2985 type_instance_flags instance_flags
= type
->instance_flags ();
2987 while (type
->code () == TYPE_CODE_TYPEDEF
)
2989 if (!TYPE_TARGET_TYPE (type
))
2994 /* It is dangerous to call lookup_symbol if we are currently
2995 reading a symtab. Infinite recursion is one danger. */
2996 if (currently_reading_symtab
)
2997 return make_qualified_type (type
, instance_flags
, NULL
);
2999 name
= type
->name ();
3000 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
3001 VAR_DOMAIN as appropriate? */
3004 stub_noname_complaint ();
3005 return make_qualified_type (type
, instance_flags
, NULL
);
3007 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3009 TYPE_TARGET_TYPE (type
) = sym
->type ();
3010 else /* TYPE_CODE_UNDEF */
3011 TYPE_TARGET_TYPE (type
) = alloc_type_arch (type
->arch ());
3013 type
= TYPE_TARGET_TYPE (type
);
3015 /* Preserve the instance flags as we traverse down the typedef chain.
3017 Handling address spaces/classes is nasty, what do we do if there's a
3019 E.g., what if an outer typedef marks the type as class_1 and an inner
3020 typedef marks the type as class_2?
3021 This is the wrong place to do such error checking. We leave it to
3022 the code that created the typedef in the first place to flag the
3023 error. We just pick the outer address space (akin to letting the
3024 outer cast in a chain of casting win), instead of assuming
3025 "it can't happen". */
3027 const type_instance_flags ALL_SPACES
3028 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3029 | TYPE_INSTANCE_FLAG_DATA_SPACE
);
3030 const type_instance_flags ALL_CLASSES
3031 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
;
3033 type_instance_flags new_instance_flags
= type
->instance_flags ();
3035 /* Treat code vs data spaces and address classes separately. */
3036 if ((instance_flags
& ALL_SPACES
) != 0)
3037 new_instance_flags
&= ~ALL_SPACES
;
3038 if ((instance_flags
& ALL_CLASSES
) != 0)
3039 new_instance_flags
&= ~ALL_CLASSES
;
3041 instance_flags
|= new_instance_flags
;
3045 /* If this is a struct/class/union with no fields, then check
3046 whether a full definition exists somewhere else. This is for
3047 systems where a type definition with no fields is issued for such
3048 types, instead of identifying them as stub types in the first
3051 if (TYPE_IS_OPAQUE (type
)
3052 && opaque_type_resolution
3053 && !currently_reading_symtab
)
3055 const char *name
= type
->name ();
3056 struct type
*newtype
;
3060 stub_noname_complaint ();
3061 return make_qualified_type (type
, instance_flags
, NULL
);
3063 newtype
= lookup_transparent_type (name
);
3067 /* If the resolved type and the stub are in the same
3068 objfile, then replace the stub type with the real deal.
3069 But if they're in separate objfiles, leave the stub
3070 alone; we'll just look up the transparent type every time
3071 we call check_typedef. We can't create pointers between
3072 types allocated to different objfiles, since they may
3073 have different lifetimes. Trying to copy NEWTYPE over to
3074 TYPE's objfile is pointless, too, since you'll have to
3075 move over any other types NEWTYPE refers to, which could
3076 be an unbounded amount of stuff. */
3077 if (newtype
->objfile_owner () == type
->objfile_owner ())
3078 type
= make_qualified_type (newtype
, type
->instance_flags (), type
);
3083 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3085 else if (type
->is_stub () && !currently_reading_symtab
)
3087 const char *name
= type
->name ();
3088 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3094 stub_noname_complaint ();
3095 return make_qualified_type (type
, instance_flags
, NULL
);
3097 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3100 /* Same as above for opaque types, we can replace the stub
3101 with the complete type only if they are in the same
3103 if (sym
->type ()->objfile_owner () == type
->objfile_owner ())
3104 type
= make_qualified_type (sym
->type (),
3105 type
->instance_flags (), type
);
3107 type
= sym
->type ();
3111 if (type
->target_is_stub ())
3113 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
3115 if (target_type
->is_stub () || target_type
->target_is_stub ())
3117 /* Nothing we can do. */
3119 else if (type
->code () == TYPE_CODE_RANGE
)
3121 TYPE_LENGTH (type
) = TYPE_LENGTH (target_type
);
3122 type
->set_target_is_stub (false);
3124 else if (type
->code () == TYPE_CODE_ARRAY
3125 && update_static_array_size (type
))
3126 type
->set_target_is_stub (false);
3129 type
= make_qualified_type (type
, instance_flags
, NULL
);
3131 /* Cache TYPE_LENGTH for future use. */
3132 TYPE_LENGTH (orig_type
) = TYPE_LENGTH (type
);
3137 /* Parse a type expression in the string [P..P+LENGTH). If an error
3138 occurs, silently return a void type. */
3140 static struct type
*
3141 safe_parse_type (struct gdbarch
*gdbarch
, const char *p
, int length
)
3143 struct ui_file
*saved_gdb_stderr
;
3144 struct type
*type
= NULL
; /* Initialize to keep gcc happy. */
3146 /* Suppress error messages. */
3147 saved_gdb_stderr
= gdb_stderr
;
3148 gdb_stderr
= &null_stream
;
3150 /* Call parse_and_eval_type() without fear of longjmp()s. */
3153 type
= parse_and_eval_type (p
, length
);
3155 catch (const gdb_exception_error
&except
)
3157 type
= builtin_type (gdbarch
)->builtin_void
;
3160 /* Stop suppressing error messages. */
3161 gdb_stderr
= saved_gdb_stderr
;
3166 /* Ugly hack to convert method stubs into method types.
3168 He ain't kiddin'. This demangles the name of the method into a
3169 string including argument types, parses out each argument type,
3170 generates a string casting a zero to that type, evaluates the
3171 string, and stuffs the resulting type into an argtype vector!!!
3172 Then it knows the type of the whole function (including argument
3173 types for overloading), which info used to be in the stab's but was
3174 removed to hack back the space required for them. */
3177 check_stub_method (struct type
*type
, int method_id
, int signature_id
)
3179 struct gdbarch
*gdbarch
= type
->arch ();
3181 char *mangled_name
= gdb_mangle_name (type
, method_id
, signature_id
);
3182 gdb::unique_xmalloc_ptr
<char> demangled_name
3183 = gdb_demangle (mangled_name
, DMGL_PARAMS
| DMGL_ANSI
);
3184 char *argtypetext
, *p
;
3185 int depth
= 0, argcount
= 1;
3186 struct field
*argtypes
;
3189 /* Make sure we got back a function string that we can use. */
3191 p
= strchr (demangled_name
.get (), '(');
3195 if (demangled_name
== NULL
|| p
== NULL
)
3196 error (_("Internal: Cannot demangle mangled name `%s'."),
3199 /* Now, read in the parameters that define this type. */
3204 if (*p
== '(' || *p
== '<')
3208 else if (*p
== ')' || *p
== '>')
3212 else if (*p
== ',' && depth
== 0)
3220 /* If we read one argument and it was ``void'', don't count it. */
3221 if (startswith (argtypetext
, "(void)"))
3224 /* We need one extra slot, for the THIS pointer. */
3226 argtypes
= (struct field
*)
3227 TYPE_ALLOC (type
, (argcount
+ 1) * sizeof (struct field
));
3230 /* Add THIS pointer for non-static methods. */
3231 f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3232 if (TYPE_FN_FIELD_STATIC_P (f
, signature_id
))
3236 argtypes
[0].set_type (lookup_pointer_type (type
));
3240 if (*p
!= ')') /* () means no args, skip while. */
3245 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
3247 /* Avoid parsing of ellipsis, they will be handled below.
3248 Also avoid ``void'' as above. */
3249 if (strncmp (argtypetext
, "...", p
- argtypetext
) != 0
3250 && strncmp (argtypetext
, "void", p
- argtypetext
) != 0)
3252 argtypes
[argcount
].set_type
3253 (safe_parse_type (gdbarch
, argtypetext
, p
- argtypetext
));
3256 argtypetext
= p
+ 1;
3259 if (*p
== '(' || *p
== '<')
3263 else if (*p
== ')' || *p
== '>')
3272 TYPE_FN_FIELD_PHYSNAME (f
, signature_id
) = mangled_name
;
3274 /* Now update the old "stub" type into a real type. */
3275 mtype
= TYPE_FN_FIELD_TYPE (f
, signature_id
);
3276 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3277 We want a method (TYPE_CODE_METHOD). */
3278 smash_to_method_type (mtype
, type
, TYPE_TARGET_TYPE (mtype
),
3279 argtypes
, argcount
, p
[-2] == '.');
3280 mtype
->set_is_stub (false);
3281 TYPE_FN_FIELD_STUB (f
, signature_id
) = 0;
3284 /* This is the external interface to check_stub_method, above. This
3285 function unstubs all of the signatures for TYPE's METHOD_ID method
3286 name. After calling this function TYPE_FN_FIELD_STUB will be
3287 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3290 This function unfortunately can not die until stabs do. */
3293 check_stub_method_group (struct type
*type
, int method_id
)
3295 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, method_id
);
3296 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3298 for (int j
= 0; j
< len
; j
++)
3300 if (TYPE_FN_FIELD_STUB (f
, j
))
3301 check_stub_method (type
, method_id
, j
);
3305 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3306 const struct cplus_struct_type cplus_struct_default
= { };
3309 allocate_cplus_struct_type (struct type
*type
)
3311 if (HAVE_CPLUS_STRUCT (type
))
3312 /* Structure was already allocated. Nothing more to do. */
3315 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_CPLUS_STUFF
;
3316 TYPE_RAW_CPLUS_SPECIFIC (type
) = (struct cplus_struct_type
*)
3317 TYPE_ALLOC (type
, sizeof (struct cplus_struct_type
));
3318 *(TYPE_RAW_CPLUS_SPECIFIC (type
)) = cplus_struct_default
;
3319 set_type_vptr_fieldno (type
, -1);
3322 const struct gnat_aux_type gnat_aux_default
=
3325 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3326 and allocate the associated gnat-specific data. The gnat-specific
3327 data is also initialized to gnat_aux_default. */
3330 allocate_gnat_aux_type (struct type
*type
)
3332 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_GNAT_STUFF
;
3333 TYPE_GNAT_SPECIFIC (type
) = (struct gnat_aux_type
*)
3334 TYPE_ALLOC (type
, sizeof (struct gnat_aux_type
));
3335 *(TYPE_GNAT_SPECIFIC (type
)) = gnat_aux_default
;
3338 /* Helper function to initialize a newly allocated type. Set type code
3339 to CODE and initialize the type-specific fields accordingly. */
3342 set_type_code (struct type
*type
, enum type_code code
)
3344 type
->set_code (code
);
3348 case TYPE_CODE_STRUCT
:
3349 case TYPE_CODE_UNION
:
3350 case TYPE_CODE_NAMESPACE
:
3351 INIT_CPLUS_SPECIFIC (type
);
3354 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_FLOATFORMAT
;
3356 case TYPE_CODE_FUNC
:
3357 INIT_FUNC_SPECIFIC (type
);
3359 case TYPE_CODE_FIXED_POINT
:
3360 INIT_FIXED_POINT_SPECIFIC (type
);
3365 /* Helper function to verify floating-point format and size.
3366 BIT is the type size in bits; if BIT equals -1, the size is
3367 determined by the floatformat. Returns size to be used. */
3370 verify_floatformat (int bit
, const struct floatformat
*floatformat
)
3372 gdb_assert (floatformat
!= NULL
);
3375 bit
= floatformat
->totalsize
;
3377 gdb_assert (bit
>= 0);
3378 gdb_assert (bit
>= floatformat
->totalsize
);
3383 /* Return the floating-point format for a floating-point variable of
3386 const struct floatformat
*
3387 floatformat_from_type (const struct type
*type
)
3389 gdb_assert (type
->code () == TYPE_CODE_FLT
);
3390 gdb_assert (TYPE_FLOATFORMAT (type
));
3391 return TYPE_FLOATFORMAT (type
);
3394 /* Helper function to initialize the standard scalar types.
3396 If NAME is non-NULL, then it is used to initialize the type name.
3397 Note that NAME is not copied; it is required to have a lifetime at
3398 least as long as OBJFILE. */
3401 init_type (struct objfile
*objfile
, enum type_code code
, int bit
,
3406 type
= alloc_type (objfile
);
3407 set_type_code (type
, code
);
3408 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
3409 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
3410 type
->set_name (name
);
3415 /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
3416 to use with variables that have no debug info. NAME is the type
3419 static struct type
*
3420 init_nodebug_var_type (struct objfile
*objfile
, const char *name
)
3422 return init_type (objfile
, TYPE_CODE_ERROR
, 0, name
);
3425 /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
3426 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3427 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3430 init_integer_type (struct objfile
*objfile
,
3431 int bit
, int unsigned_p
, const char *name
)
3435 t
= init_type (objfile
, TYPE_CODE_INT
, bit
, name
);
3437 t
->set_is_unsigned (true);
3439 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3440 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3441 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3446 /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
3447 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3448 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3451 init_character_type (struct objfile
*objfile
,
3452 int bit
, int unsigned_p
, const char *name
)
3456 t
= init_type (objfile
, TYPE_CODE_CHAR
, bit
, name
);
3458 t
->set_is_unsigned (true);
3463 /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
3464 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3465 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3468 init_boolean_type (struct objfile
*objfile
,
3469 int bit
, int unsigned_p
, const char *name
)
3473 t
= init_type (objfile
, TYPE_CODE_BOOL
, bit
, name
);
3475 t
->set_is_unsigned (true);
3477 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3478 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3479 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3484 /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
3485 BIT is the type size in bits; if BIT equals -1, the size is
3486 determined by the floatformat. NAME is the type name. Set the
3487 TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
3488 to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
3489 order of the objfile's architecture is used. */
3492 init_float_type (struct objfile
*objfile
,
3493 int bit
, const char *name
,
3494 const struct floatformat
**floatformats
,
3495 enum bfd_endian byte_order
)
3497 if (byte_order
== BFD_ENDIAN_UNKNOWN
)
3499 struct gdbarch
*gdbarch
= objfile
->arch ();
3500 byte_order
= gdbarch_byte_order (gdbarch
);
3502 const struct floatformat
*fmt
= floatformats
[byte_order
];
3505 bit
= verify_floatformat (bit
, fmt
);
3506 t
= init_type (objfile
, TYPE_CODE_FLT
, bit
, name
);
3507 TYPE_FLOATFORMAT (t
) = fmt
;
3512 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
3513 BIT is the type size in bits. NAME is the type name. */
3516 init_decfloat_type (struct objfile
*objfile
, int bit
, const char *name
)
3520 t
= init_type (objfile
, TYPE_CODE_DECFLOAT
, bit
, name
);
3524 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3527 can_create_complex_type (struct type
*target_type
)
3529 return (target_type
->code () == TYPE_CODE_INT
3530 || target_type
->code () == TYPE_CODE_FLT
);
3533 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3534 name. TARGET_TYPE is the component type. */
3537 init_complex_type (const char *name
, struct type
*target_type
)
3541 gdb_assert (can_create_complex_type (target_type
));
3543 if (TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
== nullptr)
3545 if (name
== nullptr && target_type
->name () != nullptr)
3548 = (char *) TYPE_ALLOC (target_type
,
3549 strlen (target_type
->name ())
3550 + strlen ("_Complex ") + 1);
3551 strcpy (new_name
, "_Complex ");
3552 strcat (new_name
, target_type
->name ());
3556 t
= alloc_type_copy (target_type
);
3557 set_type_code (t
, TYPE_CODE_COMPLEX
);
3558 TYPE_LENGTH (t
) = 2 * TYPE_LENGTH (target_type
);
3561 TYPE_TARGET_TYPE (t
) = target_type
;
3562 TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
= t
;
3565 return TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
;
3568 /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
3569 BIT is the pointer type size in bits. NAME is the type name.
3570 TARGET_TYPE is the pointer target type. Always sets the pointer type's
3571 TYPE_UNSIGNED flag. */
3574 init_pointer_type (struct objfile
*objfile
,
3575 int bit
, const char *name
, struct type
*target_type
)
3579 t
= init_type (objfile
, TYPE_CODE_PTR
, bit
, name
);
3580 TYPE_TARGET_TYPE (t
) = target_type
;
3581 t
->set_is_unsigned (true);
3585 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3586 BIT is the pointer type size in bits.
3587 UNSIGNED_P should be nonzero if the type is unsigned.
3588 NAME is the type name. */
3591 init_fixed_point_type (struct objfile
*objfile
,
3592 int bit
, int unsigned_p
, const char *name
)
3596 t
= init_type (objfile
, TYPE_CODE_FIXED_POINT
, bit
, name
);
3598 t
->set_is_unsigned (true);
3603 /* See gdbtypes.h. */
3606 type_raw_align (struct type
*type
)
3608 if (type
->align_log2
!= 0)
3609 return 1 << (type
->align_log2
- 1);
3613 /* See gdbtypes.h. */
3616 type_align (struct type
*type
)
3618 /* Check alignment provided in the debug information. */
3619 unsigned raw_align
= type_raw_align (type
);
3623 /* Allow the architecture to provide an alignment. */
3624 ULONGEST align
= gdbarch_type_align (type
->arch (), type
);
3628 switch (type
->code ())
3631 case TYPE_CODE_FUNC
:
3632 case TYPE_CODE_FLAGS
:
3634 case TYPE_CODE_RANGE
:
3636 case TYPE_CODE_ENUM
:
3638 case TYPE_CODE_RVALUE_REF
:
3639 case TYPE_CODE_CHAR
:
3640 case TYPE_CODE_BOOL
:
3641 case TYPE_CODE_DECFLOAT
:
3642 case TYPE_CODE_METHODPTR
:
3643 case TYPE_CODE_MEMBERPTR
:
3644 align
= type_length_units (check_typedef (type
));
3647 case TYPE_CODE_ARRAY
:
3648 case TYPE_CODE_COMPLEX
:
3649 case TYPE_CODE_TYPEDEF
:
3650 align
= type_align (TYPE_TARGET_TYPE (type
));
3653 case TYPE_CODE_STRUCT
:
3654 case TYPE_CODE_UNION
:
3656 int number_of_non_static_fields
= 0;
3657 for (unsigned i
= 0; i
< type
->num_fields (); ++i
)
3659 if (!field_is_static (&type
->field (i
)))
3661 number_of_non_static_fields
++;
3662 ULONGEST f_align
= type_align (type
->field (i
).type ());
3665 /* Don't pretend we know something we don't. */
3669 if (f_align
> align
)
3673 /* A struct with no fields, or with only static fields has an
3675 if (number_of_non_static_fields
== 0)
3681 case TYPE_CODE_STRING
:
3682 /* Not sure what to do here, and these can't appear in C or C++
3686 case TYPE_CODE_VOID
:
3690 case TYPE_CODE_ERROR
:
3691 case TYPE_CODE_METHOD
:
3696 if ((align
& (align
- 1)) != 0)
3698 /* Not a power of 2, so pass. */
3705 /* See gdbtypes.h. */
3708 set_type_align (struct type
*type
, ULONGEST align
)
3710 /* Must be a power of 2. Zero is ok. */
3711 gdb_assert ((align
& (align
- 1)) == 0);
3713 unsigned result
= 0;
3720 if (result
>= (1 << TYPE_ALIGN_BITS
))
3723 type
->align_log2
= result
;
3728 /* Queries on types. */
3731 can_dereference (struct type
*t
)
3733 /* FIXME: Should we return true for references as well as
3735 t
= check_typedef (t
);
3738 && t
->code () == TYPE_CODE_PTR
3739 && TYPE_TARGET_TYPE (t
)->code () != TYPE_CODE_VOID
);
3743 is_integral_type (struct type
*t
)
3745 t
= check_typedef (t
);
3748 && !is_fixed_point_type (t
)
3749 && ((t
->code () == TYPE_CODE_INT
)
3750 || (t
->code () == TYPE_CODE_ENUM
)
3751 || (t
->code () == TYPE_CODE_FLAGS
)
3752 || (t
->code () == TYPE_CODE_CHAR
)
3753 || (t
->code () == TYPE_CODE_RANGE
)
3754 || (t
->code () == TYPE_CODE_BOOL
)));
3758 is_floating_type (struct type
*t
)
3760 t
= check_typedef (t
);
3763 && ((t
->code () == TYPE_CODE_FLT
)
3764 || (t
->code () == TYPE_CODE_DECFLOAT
)));
3767 /* Return true if TYPE is scalar. */
3770 is_scalar_type (struct type
*type
)
3772 type
= check_typedef (type
);
3774 if (is_fixed_point_type (type
))
3775 return 0; /* Implemented as a scalar, but more like a floating point. */
3777 switch (type
->code ())
3779 case TYPE_CODE_ARRAY
:
3780 case TYPE_CODE_STRUCT
:
3781 case TYPE_CODE_UNION
:
3783 case TYPE_CODE_STRING
:
3790 /* Return true if T is scalar, or a composite type which in practice has
3791 the memory layout of a scalar type. E.g., an array or struct with only
3792 one scalar element inside it, or a union with only scalar elements. */
3795 is_scalar_type_recursive (struct type
*t
)
3797 t
= check_typedef (t
);
3799 if (is_scalar_type (t
))
3801 /* Are we dealing with an array or string of known dimensions? */
3802 else if ((t
->code () == TYPE_CODE_ARRAY
3803 || t
->code () == TYPE_CODE_STRING
) && t
->num_fields () == 1
3804 && t
->index_type ()->code () == TYPE_CODE_RANGE
)
3806 LONGEST low_bound
, high_bound
;
3807 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (t
));
3809 if (get_discrete_bounds (t
->index_type (), &low_bound
, &high_bound
))
3810 return (high_bound
== low_bound
3811 && is_scalar_type_recursive (elt_type
));
3815 /* Are we dealing with a struct with one element? */
3816 else if (t
->code () == TYPE_CODE_STRUCT
&& t
->num_fields () == 1)
3817 return is_scalar_type_recursive (t
->field (0).type ());
3818 else if (t
->code () == TYPE_CODE_UNION
)
3820 int i
, n
= t
->num_fields ();
3822 /* If all elements of the union are scalar, then the union is scalar. */
3823 for (i
= 0; i
< n
; i
++)
3824 if (!is_scalar_type_recursive (t
->field (i
).type ()))
3833 /* Return true is T is a class or a union. False otherwise. */
3836 class_or_union_p (const struct type
*t
)
3838 return (t
->code () == TYPE_CODE_STRUCT
3839 || t
->code () == TYPE_CODE_UNION
);
3842 /* A helper function which returns true if types A and B represent the
3843 "same" class type. This is true if the types have the same main
3844 type, or the same name. */
3847 class_types_same_p (const struct type
*a
, const struct type
*b
)
3849 return (TYPE_MAIN_TYPE (a
) == TYPE_MAIN_TYPE (b
)
3850 || (a
->name () && b
->name ()
3851 && !strcmp (a
->name (), b
->name ())));
3854 /* If BASE is an ancestor of DCLASS return the distance between them.
3855 otherwise return -1;
3859 class B: public A {};
3860 class C: public B {};
3863 distance_to_ancestor (A, A, 0) = 0
3864 distance_to_ancestor (A, B, 0) = 1
3865 distance_to_ancestor (A, C, 0) = 2
3866 distance_to_ancestor (A, D, 0) = 3
3868 If PUBLIC is 1 then only public ancestors are considered,
3869 and the function returns the distance only if BASE is a public ancestor
3873 distance_to_ancestor (A, D, 1) = -1. */
3876 distance_to_ancestor (struct type
*base
, struct type
*dclass
, int is_public
)
3881 base
= check_typedef (base
);
3882 dclass
= check_typedef (dclass
);
3884 if (class_types_same_p (base
, dclass
))
3887 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
); i
++)
3889 if (is_public
&& ! BASETYPE_VIA_PUBLIC (dclass
, i
))
3892 d
= distance_to_ancestor (base
, TYPE_BASECLASS (dclass
, i
), is_public
);
3900 /* Check whether BASE is an ancestor or base class or DCLASS
3901 Return 1 if so, and 0 if not.
3902 Note: If BASE and DCLASS are of the same type, this function
3903 will return 1. So for some class A, is_ancestor (A, A) will
3907 is_ancestor (struct type
*base
, struct type
*dclass
)
3909 return distance_to_ancestor (base
, dclass
, 0) >= 0;
3912 /* Like is_ancestor, but only returns true when BASE is a public
3913 ancestor of DCLASS. */
3916 is_public_ancestor (struct type
*base
, struct type
*dclass
)
3918 return distance_to_ancestor (base
, dclass
, 1) >= 0;
3921 /* A helper function for is_unique_ancestor. */
3924 is_unique_ancestor_worker (struct type
*base
, struct type
*dclass
,
3926 const gdb_byte
*valaddr
, int embedded_offset
,
3927 CORE_ADDR address
, struct value
*val
)
3931 base
= check_typedef (base
);
3932 dclass
= check_typedef (dclass
);
3934 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
) && count
< 2; ++i
)
3939 iter
= check_typedef (TYPE_BASECLASS (dclass
, i
));
3941 this_offset
= baseclass_offset (dclass
, i
, valaddr
, embedded_offset
,
3944 if (class_types_same_p (base
, iter
))
3946 /* If this is the first subclass, set *OFFSET and set count
3947 to 1. Otherwise, if this is at the same offset as
3948 previous instances, do nothing. Otherwise, increment
3952 *offset
= this_offset
;
3955 else if (this_offset
== *offset
)
3963 count
+= is_unique_ancestor_worker (base
, iter
, offset
,
3965 embedded_offset
+ this_offset
,
3972 /* Like is_ancestor, but only returns true if BASE is a unique base
3973 class of the type of VAL. */
3976 is_unique_ancestor (struct type
*base
, struct value
*val
)
3980 return is_unique_ancestor_worker (base
, value_type (val
), &offset
,
3981 value_contents_for_printing (val
).data (),
3982 value_embedded_offset (val
),
3983 value_address (val
), val
) == 1;
3986 /* See gdbtypes.h. */
3989 type_byte_order (const struct type
*type
)
3991 bfd_endian byteorder
= gdbarch_byte_order (type
->arch ());
3992 if (type
->endianity_is_not_default ())
3994 if (byteorder
== BFD_ENDIAN_BIG
)
3995 return BFD_ENDIAN_LITTLE
;
3998 gdb_assert (byteorder
== BFD_ENDIAN_LITTLE
);
3999 return BFD_ENDIAN_BIG
;
4006 /* See gdbtypes.h. */
4009 is_nocall_function (const struct type
*type
)
4011 gdb_assert (type
->code () == TYPE_CODE_FUNC
4012 || type
->code () == TYPE_CODE_METHOD
);
4014 return TYPE_CALLING_CONVENTION (type
) == DW_CC_nocall
;
4018 /* Overload resolution. */
4020 /* Return the sum of the rank of A with the rank of B. */
4023 sum_ranks (struct rank a
, struct rank b
)
4026 c
.rank
= a
.rank
+ b
.rank
;
4027 c
.subrank
= a
.subrank
+ b
.subrank
;
4031 /* Compare rank A and B and return:
4033 1 if a is better than b
4034 -1 if b is better than a. */
4037 compare_ranks (struct rank a
, struct rank b
)
4039 if (a
.rank
== b
.rank
)
4041 if (a
.subrank
== b
.subrank
)
4043 if (a
.subrank
< b
.subrank
)
4045 if (a
.subrank
> b
.subrank
)
4049 if (a
.rank
< b
.rank
)
4052 /* a.rank > b.rank */
4056 /* Functions for overload resolution begin here. */
4058 /* Compare two badness vectors A and B and return the result.
4059 0 => A and B are identical
4060 1 => A and B are incomparable
4061 2 => A is better than B
4062 3 => A is worse than B */
4065 compare_badness (const badness_vector
&a
, const badness_vector
&b
)
4069 /* Any positives in comparison? */
4070 bool found_pos
= false;
4071 /* Any negatives in comparison? */
4072 bool found_neg
= false;
4073 /* Did A have any INVALID_CONVERSION entries. */
4074 bool a_invalid
= false;
4075 /* Did B have any INVALID_CONVERSION entries. */
4076 bool b_invalid
= false;
4078 /* differing sizes => incomparable */
4079 if (a
.size () != b
.size ())
4082 /* Subtract b from a */
4083 for (i
= 0; i
< a
.size (); i
++)
4085 tmp
= compare_ranks (b
[i
], a
[i
]);
4090 if (a
[i
].rank
>= INVALID_CONVERSION
)
4092 if (b
[i
].rank
>= INVALID_CONVERSION
)
4096 /* B will only be considered better than or incomparable to A if
4097 they both have invalid entries, or if neither does. That is, if
4098 A has only valid entries, and B has an invalid entry, then A will
4099 be considered better than B, even if B happens to be better for
4101 if (a_invalid
!= b_invalid
)
4104 return 3; /* A > B */
4105 return 2; /* A < B */
4110 return 1; /* incomparable */
4112 return 3; /* A > B */
4118 return 2; /* A < B */
4120 return 0; /* A == B */
4124 /* Rank a function by comparing its parameter types (PARMS), to the
4125 types of an argument list (ARGS). Return the badness vector. This
4126 has ARGS.size() + 1 entries. */
4129 rank_function (gdb::array_view
<type
*> parms
,
4130 gdb::array_view
<value
*> args
)
4132 /* add 1 for the length-match rank. */
4134 bv
.reserve (1 + args
.size ());
4136 /* First compare the lengths of the supplied lists.
4137 If there is a mismatch, set it to a high value. */
4139 /* pai/1997-06-03 FIXME: when we have debug info about default
4140 arguments and ellipsis parameter lists, we should consider those
4141 and rank the length-match more finely. */
4143 bv
.push_back ((args
.size () != parms
.size ())
4144 ? LENGTH_MISMATCH_BADNESS
4145 : EXACT_MATCH_BADNESS
);
4147 /* Now rank all the parameters of the candidate function. */
4148 size_t min_len
= std::min (parms
.size (), args
.size ());
4150 for (size_t i
= 0; i
< min_len
; i
++)
4151 bv
.push_back (rank_one_type (parms
[i
], value_type (args
[i
]),
4154 /* If more arguments than parameters, add dummy entries. */
4155 for (size_t i
= min_len
; i
< args
.size (); i
++)
4156 bv
.push_back (TOO_FEW_PARAMS_BADNESS
);
4161 /* Compare the names of two integer types, assuming that any sign
4162 qualifiers have been checked already. We do it this way because
4163 there may be an "int" in the name of one of the types. */
4166 integer_types_same_name_p (const char *first
, const char *second
)
4168 int first_p
, second_p
;
4170 /* If both are shorts, return 1; if neither is a short, keep
4172 first_p
= (strstr (first
, "short") != NULL
);
4173 second_p
= (strstr (second
, "short") != NULL
);
4174 if (first_p
&& second_p
)
4176 if (first_p
|| second_p
)
4179 /* Likewise for long. */
4180 first_p
= (strstr (first
, "long") != NULL
);
4181 second_p
= (strstr (second
, "long") != NULL
);
4182 if (first_p
&& second_p
)
4184 if (first_p
|| second_p
)
4187 /* Likewise for char. */
4188 first_p
= (strstr (first
, "char") != NULL
);
4189 second_p
= (strstr (second
, "char") != NULL
);
4190 if (first_p
&& second_p
)
4192 if (first_p
|| second_p
)
4195 /* They must both be ints. */
4199 /* Compares type A to type B. Returns true if they represent the same
4200 type, false otherwise. */
4203 types_equal (struct type
*a
, struct type
*b
)
4205 /* Identical type pointers. */
4206 /* However, this still doesn't catch all cases of same type for b
4207 and a. The reason is that builtin types are different from
4208 the same ones constructed from the object. */
4212 /* Resolve typedefs */
4213 if (a
->code () == TYPE_CODE_TYPEDEF
)
4214 a
= check_typedef (a
);
4215 if (b
->code () == TYPE_CODE_TYPEDEF
)
4216 b
= check_typedef (b
);
4218 /* Check if identical after resolving typedefs. */
4222 /* If after resolving typedefs a and b are not of the same type
4223 code then they are not equal. */
4224 if (a
->code () != b
->code ())
4227 /* If a and b are both pointers types or both reference types then
4228 they are equal of the same type iff the objects they refer to are
4229 of the same type. */
4230 if (a
->code () == TYPE_CODE_PTR
4231 || a
->code () == TYPE_CODE_REF
)
4232 return types_equal (TYPE_TARGET_TYPE (a
),
4233 TYPE_TARGET_TYPE (b
));
4235 /* Well, damnit, if the names are exactly the same, I'll say they
4236 are exactly the same. This happens when we generate method
4237 stubs. The types won't point to the same address, but they
4238 really are the same. */
4240 if (a
->name () && b
->name ()
4241 && strcmp (a
->name (), b
->name ()) == 0)
4244 /* Two function types are equal if their argument and return types
4246 if (a
->code () == TYPE_CODE_FUNC
)
4250 if (a
->num_fields () != b
->num_fields ())
4253 if (!types_equal (TYPE_TARGET_TYPE (a
), TYPE_TARGET_TYPE (b
)))
4256 for (i
= 0; i
< a
->num_fields (); ++i
)
4257 if (!types_equal (a
->field (i
).type (), b
->field (i
).type ()))
4266 /* Deep comparison of types. */
4268 /* An entry in the type-equality bcache. */
4270 struct type_equality_entry
4272 type_equality_entry (struct type
*t1
, struct type
*t2
)
4278 struct type
*type1
, *type2
;
4281 /* A helper function to compare two strings. Returns true if they are
4282 the same, false otherwise. Handles NULLs properly. */
4285 compare_maybe_null_strings (const char *s
, const char *t
)
4287 if (s
== NULL
|| t
== NULL
)
4289 return strcmp (s
, t
) == 0;
4292 /* A helper function for check_types_worklist that checks two types for
4293 "deep" equality. Returns true if the types are considered the
4294 same, false otherwise. */
4297 check_types_equal (struct type
*type1
, struct type
*type2
,
4298 std::vector
<type_equality_entry
> *worklist
)
4300 type1
= check_typedef (type1
);
4301 type2
= check_typedef (type2
);
4306 if (type1
->code () != type2
->code ()
4307 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
4308 || type1
->is_unsigned () != type2
->is_unsigned ()
4309 || type1
->has_no_signedness () != type2
->has_no_signedness ()
4310 || type1
->endianity_is_not_default () != type2
->endianity_is_not_default ()
4311 || type1
->has_varargs () != type2
->has_varargs ()
4312 || type1
->is_vector () != type2
->is_vector ()
4313 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
4314 || type1
->instance_flags () != type2
->instance_flags ()
4315 || type1
->num_fields () != type2
->num_fields ())
4318 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4320 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4323 if (type1
->code () == TYPE_CODE_RANGE
)
4325 if (*type1
->bounds () != *type2
->bounds ())
4332 for (i
= 0; i
< type1
->num_fields (); ++i
)
4334 const struct field
*field1
= &type1
->field (i
);
4335 const struct field
*field2
= &type2
->field (i
);
4337 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
4338 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
4339 || field1
->loc_kind () != field2
->loc_kind ())
4341 if (!compare_maybe_null_strings (field1
->name (), field2
->name ()))
4343 switch (field1
->loc_kind ())
4345 case FIELD_LOC_KIND_BITPOS
:
4346 if (field1
->loc_bitpos () != field2
->loc_bitpos ())
4349 case FIELD_LOC_KIND_ENUMVAL
:
4350 if (field1
->loc_enumval () != field2
->loc_enumval ())
4352 /* Don't compare types of enum fields, because they don't
4355 case FIELD_LOC_KIND_PHYSADDR
:
4356 if (field1
->loc_physaddr () != field2
->loc_physaddr ())
4359 case FIELD_LOC_KIND_PHYSNAME
:
4360 if (!compare_maybe_null_strings (field1
->loc_physname (),
4361 field2
->loc_physname ()))
4364 case FIELD_LOC_KIND_DWARF_BLOCK
:
4366 struct dwarf2_locexpr_baton
*block1
, *block2
;
4368 block1
= field1
->loc_dwarf_block ();
4369 block2
= field2
->loc_dwarf_block ();
4370 if (block1
->per_cu
!= block2
->per_cu
4371 || block1
->size
!= block2
->size
4372 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
4377 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
4378 "%d by check_types_equal"),
4379 field1
->loc_kind ());
4382 worklist
->emplace_back (field1
->type (), field2
->type ());
4386 if (TYPE_TARGET_TYPE (type1
) != NULL
)
4388 if (TYPE_TARGET_TYPE (type2
) == NULL
)
4391 worklist
->emplace_back (TYPE_TARGET_TYPE (type1
),
4392 TYPE_TARGET_TYPE (type2
));
4394 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
4400 /* Check types on a worklist for equality. Returns false if any pair
4401 is not equal, true if they are all considered equal. */
4404 check_types_worklist (std::vector
<type_equality_entry
> *worklist
,
4407 while (!worklist
->empty ())
4411 struct type_equality_entry entry
= std::move (worklist
->back ());
4412 worklist
->pop_back ();
4414 /* If the type pair has already been visited, we know it is
4416 cache
->insert (&entry
, sizeof (entry
), &added
);
4420 if (!check_types_equal (entry
.type1
, entry
.type2
, worklist
))
4427 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4428 "deep comparison". Otherwise return false. */
4431 types_deeply_equal (struct type
*type1
, struct type
*type2
)
4433 std::vector
<type_equality_entry
> worklist
;
4435 gdb_assert (type1
!= NULL
&& type2
!= NULL
);
4437 /* Early exit for the simple case. */
4442 worklist
.emplace_back (type1
, type2
);
4443 return check_types_worklist (&worklist
, &cache
);
4446 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4447 Otherwise return one. */
4450 type_not_allocated (const struct type
*type
)
4452 struct dynamic_prop
*prop
= TYPE_ALLOCATED_PROP (type
);
4454 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4455 && prop
->const_val () == 0);
4458 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4459 Otherwise return one. */
4462 type_not_associated (const struct type
*type
)
4464 struct dynamic_prop
*prop
= TYPE_ASSOCIATED_PROP (type
);
4466 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4467 && prop
->const_val () == 0);
4470 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4473 rank_one_type_parm_ptr (struct type
*parm
, struct type
*arg
, struct value
*value
)
4475 struct rank rank
= {0,0};
4477 switch (arg
->code ())
4481 /* Allowed pointer conversions are:
4482 (a) pointer to void-pointer conversion. */
4483 if (TYPE_TARGET_TYPE (parm
)->code () == TYPE_CODE_VOID
)
4484 return VOID_PTR_CONVERSION_BADNESS
;
4486 /* (b) pointer to ancestor-pointer conversion. */
4487 rank
.subrank
= distance_to_ancestor (TYPE_TARGET_TYPE (parm
),
4488 TYPE_TARGET_TYPE (arg
),
4490 if (rank
.subrank
>= 0)
4491 return sum_ranks (BASE_PTR_CONVERSION_BADNESS
, rank
);
4493 return INCOMPATIBLE_TYPE_BADNESS
;
4494 case TYPE_CODE_ARRAY
:
4496 struct type
*t1
= TYPE_TARGET_TYPE (parm
);
4497 struct type
*t2
= TYPE_TARGET_TYPE (arg
);
4499 if (types_equal (t1
, t2
))
4501 /* Make sure they are CV equal. */
4502 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4503 rank
.subrank
|= CV_CONVERSION_CONST
;
4504 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4505 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4506 if (rank
.subrank
!= 0)
4507 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4508 return EXACT_MATCH_BADNESS
;
4510 return INCOMPATIBLE_TYPE_BADNESS
;
4512 case TYPE_CODE_FUNC
:
4513 return rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
);
4515 if (value
!= NULL
&& value_type (value
)->code () == TYPE_CODE_INT
)
4517 if (value_as_long (value
) == 0)
4519 /* Null pointer conversion: allow it to be cast to a pointer.
4520 [4.10.1 of C++ standard draft n3290] */
4521 return NULL_POINTER_CONVERSION_BADNESS
;
4525 /* If type checking is disabled, allow the conversion. */
4526 if (!strict_type_checking
)
4527 return NS_INTEGER_POINTER_CONVERSION_BADNESS
;
4531 case TYPE_CODE_ENUM
:
4532 case TYPE_CODE_FLAGS
:
4533 case TYPE_CODE_CHAR
:
4534 case TYPE_CODE_RANGE
:
4535 case TYPE_CODE_BOOL
:
4537 return INCOMPATIBLE_TYPE_BADNESS
;
4541 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4544 rank_one_type_parm_array (struct type
*parm
, struct type
*arg
, struct value
*value
)
4546 switch (arg
->code ())
4549 case TYPE_CODE_ARRAY
:
4550 return rank_one_type (TYPE_TARGET_TYPE (parm
),
4551 TYPE_TARGET_TYPE (arg
), NULL
);
4553 return INCOMPATIBLE_TYPE_BADNESS
;
4557 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4560 rank_one_type_parm_func (struct type
*parm
, struct type
*arg
, struct value
*value
)
4562 switch (arg
->code ())
4564 case TYPE_CODE_PTR
: /* funcptr -> func */
4565 return rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
);
4567 return INCOMPATIBLE_TYPE_BADNESS
;
4571 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4574 rank_one_type_parm_int (struct type
*parm
, struct type
*arg
, struct value
*value
)
4576 switch (arg
->code ())
4579 if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4581 /* Deal with signed, unsigned, and plain chars and
4582 signed and unsigned ints. */
4583 if (parm
->has_no_signedness ())
4585 /* This case only for character types. */
4586 if (arg
->has_no_signedness ())
4587 return EXACT_MATCH_BADNESS
; /* plain char -> plain char */
4588 else /* signed/unsigned char -> plain char */
4589 return INTEGER_CONVERSION_BADNESS
;
4591 else if (parm
->is_unsigned ())
4593 if (arg
->is_unsigned ())
4595 /* unsigned int -> unsigned int, or
4596 unsigned long -> unsigned long */
4597 if (integer_types_same_name_p (parm
->name (),
4599 return EXACT_MATCH_BADNESS
;
4600 else if (integer_types_same_name_p (arg
->name (),
4602 && integer_types_same_name_p (parm
->name (),
4604 /* unsigned int -> unsigned long */
4605 return INTEGER_PROMOTION_BADNESS
;
4607 /* unsigned long -> unsigned int */
4608 return INTEGER_CONVERSION_BADNESS
;
4612 if (integer_types_same_name_p (arg
->name (),
4614 && integer_types_same_name_p (parm
->name (),
4616 /* signed long -> unsigned int */
4617 return INTEGER_CONVERSION_BADNESS
;
4619 /* signed int/long -> unsigned int/long */
4620 return INTEGER_CONVERSION_BADNESS
;
4623 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4625 if (integer_types_same_name_p (parm
->name (),
4627 return EXACT_MATCH_BADNESS
;
4628 else if (integer_types_same_name_p (arg
->name (),
4630 && integer_types_same_name_p (parm
->name (),
4632 return INTEGER_PROMOTION_BADNESS
;
4634 return INTEGER_CONVERSION_BADNESS
;
4637 return INTEGER_CONVERSION_BADNESS
;
4639 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4640 return INTEGER_PROMOTION_BADNESS
;
4642 return INTEGER_CONVERSION_BADNESS
;
4643 case TYPE_CODE_ENUM
:
4644 case TYPE_CODE_FLAGS
:
4645 case TYPE_CODE_CHAR
:
4646 case TYPE_CODE_RANGE
:
4647 case TYPE_CODE_BOOL
:
4648 if (arg
->is_declared_class ())
4649 return INCOMPATIBLE_TYPE_BADNESS
;
4650 return INTEGER_PROMOTION_BADNESS
;
4652 return INT_FLOAT_CONVERSION_BADNESS
;
4654 return NS_POINTER_CONVERSION_BADNESS
;
4656 return INCOMPATIBLE_TYPE_BADNESS
;
4660 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4663 rank_one_type_parm_enum (struct type
*parm
, struct type
*arg
, struct value
*value
)
4665 switch (arg
->code ())
4668 case TYPE_CODE_CHAR
:
4669 case TYPE_CODE_RANGE
:
4670 case TYPE_CODE_BOOL
:
4671 case TYPE_CODE_ENUM
:
4672 if (parm
->is_declared_class () || arg
->is_declared_class ())
4673 return INCOMPATIBLE_TYPE_BADNESS
;
4674 return INTEGER_CONVERSION_BADNESS
;
4676 return INT_FLOAT_CONVERSION_BADNESS
;
4678 return INCOMPATIBLE_TYPE_BADNESS
;
4682 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4685 rank_one_type_parm_char (struct type
*parm
, struct type
*arg
, struct value
*value
)
4687 switch (arg
->code ())
4689 case TYPE_CODE_RANGE
:
4690 case TYPE_CODE_BOOL
:
4691 case TYPE_CODE_ENUM
:
4692 if (arg
->is_declared_class ())
4693 return INCOMPATIBLE_TYPE_BADNESS
;
4694 return INTEGER_CONVERSION_BADNESS
;
4696 return INT_FLOAT_CONVERSION_BADNESS
;
4698 if (TYPE_LENGTH (arg
) > TYPE_LENGTH (parm
))
4699 return INTEGER_CONVERSION_BADNESS
;
4700 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4701 return INTEGER_PROMOTION_BADNESS
;
4703 case TYPE_CODE_CHAR
:
4704 /* Deal with signed, unsigned, and plain chars for C++ and
4705 with int cases falling through from previous case. */
4706 if (parm
->has_no_signedness ())
4708 if (arg
->has_no_signedness ())
4709 return EXACT_MATCH_BADNESS
;
4711 return INTEGER_CONVERSION_BADNESS
;
4713 else if (parm
->is_unsigned ())
4715 if (arg
->is_unsigned ())
4716 return EXACT_MATCH_BADNESS
;
4718 return INTEGER_PROMOTION_BADNESS
;
4720 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4721 return EXACT_MATCH_BADNESS
;
4723 return INTEGER_CONVERSION_BADNESS
;
4725 return INCOMPATIBLE_TYPE_BADNESS
;
4729 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4732 rank_one_type_parm_range (struct type
*parm
, struct type
*arg
, struct value
*value
)
4734 switch (arg
->code ())
4737 case TYPE_CODE_CHAR
:
4738 case TYPE_CODE_RANGE
:
4739 case TYPE_CODE_BOOL
:
4740 case TYPE_CODE_ENUM
:
4741 return INTEGER_CONVERSION_BADNESS
;
4743 return INT_FLOAT_CONVERSION_BADNESS
;
4745 return INCOMPATIBLE_TYPE_BADNESS
;
4749 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4752 rank_one_type_parm_bool (struct type
*parm
, struct type
*arg
, struct value
*value
)
4754 switch (arg
->code ())
4756 /* n3290 draft, section 4.12.1 (conv.bool):
4758 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4759 pointer to member type can be converted to a prvalue of type
4760 bool. A zero value, null pointer value, or null member pointer
4761 value is converted to false; any other value is converted to
4762 true. A prvalue of type std::nullptr_t can be converted to a
4763 prvalue of type bool; the resulting value is false." */
4765 case TYPE_CODE_CHAR
:
4766 case TYPE_CODE_ENUM
:
4768 case TYPE_CODE_MEMBERPTR
:
4770 return BOOL_CONVERSION_BADNESS
;
4771 case TYPE_CODE_RANGE
:
4772 return INCOMPATIBLE_TYPE_BADNESS
;
4773 case TYPE_CODE_BOOL
:
4774 return EXACT_MATCH_BADNESS
;
4776 return INCOMPATIBLE_TYPE_BADNESS
;
4780 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4783 rank_one_type_parm_float (struct type
*parm
, struct type
*arg
, struct value
*value
)
4785 switch (arg
->code ())
4788 if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4789 return FLOAT_PROMOTION_BADNESS
;
4790 else if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4791 return EXACT_MATCH_BADNESS
;
4793 return FLOAT_CONVERSION_BADNESS
;
4795 case TYPE_CODE_BOOL
:
4796 case TYPE_CODE_ENUM
:
4797 case TYPE_CODE_RANGE
:
4798 case TYPE_CODE_CHAR
:
4799 return INT_FLOAT_CONVERSION_BADNESS
;
4801 return INCOMPATIBLE_TYPE_BADNESS
;
4805 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4808 rank_one_type_parm_complex (struct type
*parm
, struct type
*arg
, struct value
*value
)
4810 switch (arg
->code ())
4811 { /* Strictly not needed for C++, but... */
4813 return FLOAT_PROMOTION_BADNESS
;
4814 case TYPE_CODE_COMPLEX
:
4815 return EXACT_MATCH_BADNESS
;
4817 return INCOMPATIBLE_TYPE_BADNESS
;
4821 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4824 rank_one_type_parm_struct (struct type
*parm
, struct type
*arg
, struct value
*value
)
4826 struct rank rank
= {0, 0};
4828 switch (arg
->code ())
4830 case TYPE_CODE_STRUCT
:
4831 /* Check for derivation */
4832 rank
.subrank
= distance_to_ancestor (parm
, arg
, 0);
4833 if (rank
.subrank
>= 0)
4834 return sum_ranks (BASE_CONVERSION_BADNESS
, rank
);
4837 return INCOMPATIBLE_TYPE_BADNESS
;
4841 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4844 rank_one_type_parm_set (struct type
*parm
, struct type
*arg
, struct value
*value
)
4846 switch (arg
->code ())
4850 return rank_one_type (parm
->field (0).type (),
4851 arg
->field (0).type (), NULL
);
4853 return INCOMPATIBLE_TYPE_BADNESS
;
4857 /* Compare one type (PARM) for compatibility with another (ARG).
4858 * PARM is intended to be the parameter type of a function; and
4859 * ARG is the supplied argument's type. This function tests if
4860 * the latter can be converted to the former.
4861 * VALUE is the argument's value or NULL if none (or called recursively)
4863 * Return 0 if they are identical types;
4864 * Otherwise, return an integer which corresponds to how compatible
4865 * PARM is to ARG. The higher the return value, the worse the match.
4866 * Generally the "bad" conversions are all uniformly assigned
4867 * INVALID_CONVERSION. */
4870 rank_one_type (struct type
*parm
, struct type
*arg
, struct value
*value
)
4872 struct rank rank
= {0,0};
4874 /* Resolve typedefs */
4875 if (parm
->code () == TYPE_CODE_TYPEDEF
)
4876 parm
= check_typedef (parm
);
4877 if (arg
->code () == TYPE_CODE_TYPEDEF
)
4878 arg
= check_typedef (arg
);
4880 if (TYPE_IS_REFERENCE (parm
) && value
!= NULL
)
4882 if (VALUE_LVAL (value
) == not_lval
)
4884 /* Rvalues should preferably bind to rvalue references or const
4885 lvalue references. */
4886 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4887 rank
.subrank
= REFERENCE_CONVERSION_RVALUE
;
4888 else if (TYPE_CONST (TYPE_TARGET_TYPE (parm
)))
4889 rank
.subrank
= REFERENCE_CONVERSION_CONST_LVALUE
;
4891 return INCOMPATIBLE_TYPE_BADNESS
;
4892 return sum_ranks (rank
, REFERENCE_CONVERSION_BADNESS
);
4896 /* It's illegal to pass an lvalue as an rvalue. */
4897 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4898 return INCOMPATIBLE_TYPE_BADNESS
;
4902 if (types_equal (parm
, arg
))
4904 struct type
*t1
= parm
;
4905 struct type
*t2
= arg
;
4907 /* For pointers and references, compare target type. */
4908 if (parm
->is_pointer_or_reference ())
4910 t1
= TYPE_TARGET_TYPE (parm
);
4911 t2
= TYPE_TARGET_TYPE (arg
);
4914 /* Make sure they are CV equal, too. */
4915 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4916 rank
.subrank
|= CV_CONVERSION_CONST
;
4917 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4918 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4919 if (rank
.subrank
!= 0)
4920 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4921 return EXACT_MATCH_BADNESS
;
4924 /* See through references, since we can almost make non-references
4927 if (TYPE_IS_REFERENCE (arg
))
4928 return (sum_ranks (rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
),
4929 REFERENCE_SEE_THROUGH_BADNESS
));
4930 if (TYPE_IS_REFERENCE (parm
))
4931 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
),
4932 REFERENCE_SEE_THROUGH_BADNESS
));
4935 /* Debugging only. */
4936 gdb_printf (gdb_stderr
,
4937 "------ Arg is %s [%d], parm is %s [%d]\n",
4938 arg
->name (), arg
->code (),
4939 parm
->name (), parm
->code ());
4942 /* x -> y means arg of type x being supplied for parameter of type y. */
4944 switch (parm
->code ())
4947 return rank_one_type_parm_ptr (parm
, arg
, value
);
4948 case TYPE_CODE_ARRAY
:
4949 return rank_one_type_parm_array (parm
, arg
, value
);
4950 case TYPE_CODE_FUNC
:
4951 return rank_one_type_parm_func (parm
, arg
, value
);
4953 return rank_one_type_parm_int (parm
, arg
, value
);
4954 case TYPE_CODE_ENUM
:
4955 return rank_one_type_parm_enum (parm
, arg
, value
);
4956 case TYPE_CODE_CHAR
:
4957 return rank_one_type_parm_char (parm
, arg
, value
);
4958 case TYPE_CODE_RANGE
:
4959 return rank_one_type_parm_range (parm
, arg
, value
);
4960 case TYPE_CODE_BOOL
:
4961 return rank_one_type_parm_bool (parm
, arg
, value
);
4963 return rank_one_type_parm_float (parm
, arg
, value
);
4964 case TYPE_CODE_COMPLEX
:
4965 return rank_one_type_parm_complex (parm
, arg
, value
);
4966 case TYPE_CODE_STRUCT
:
4967 return rank_one_type_parm_struct (parm
, arg
, value
);
4969 return rank_one_type_parm_set (parm
, arg
, value
);
4971 return INCOMPATIBLE_TYPE_BADNESS
;
4972 } /* switch (arg->code ()) */
4975 /* End of functions for overload resolution. */
4977 /* Routines to pretty-print types. */
4980 print_bit_vector (B_TYPE
*bits
, int nbits
)
4984 for (bitno
= 0; bitno
< nbits
; bitno
++)
4986 if ((bitno
% 8) == 0)
4990 if (B_TST (bits
, bitno
))
4997 /* Note the first arg should be the "this" pointer, we may not want to
4998 include it since we may get into a infinitely recursive
5002 print_args (struct field
*args
, int nargs
, int spaces
)
5008 for (i
= 0; i
< nargs
; i
++)
5011 ("%*s[%d] name '%s'\n", spaces
, "", i
,
5012 args
[i
].name () != NULL
? args
[i
].name () : "<NULL>");
5013 recursive_dump_type (args
[i
].type (), spaces
+ 2);
5019 field_is_static (struct field
*f
)
5021 /* "static" fields are the fields whose location is not relative
5022 to the address of the enclosing struct. It would be nice to
5023 have a dedicated flag that would be set for static fields when
5024 the type is being created. But in practice, checking the field
5025 loc_kind should give us an accurate answer. */
5026 return (f
->loc_kind () == FIELD_LOC_KIND_PHYSNAME
5027 || f
->loc_kind () == FIELD_LOC_KIND_PHYSADDR
);
5031 dump_fn_fieldlists (struct type
*type
, int spaces
)
5037 gdb_printf ("%*sfn_fieldlists %s\n", spaces
, "",
5038 host_address_to_string (TYPE_FN_FIELDLISTS (type
)));
5039 for (method_idx
= 0; method_idx
< TYPE_NFN_FIELDS (type
); method_idx
++)
5041 f
= TYPE_FN_FIELDLIST1 (type
, method_idx
);
5043 ("%*s[%d] name '%s' (%s) length %d\n", spaces
+ 2, "",
5045 TYPE_FN_FIELDLIST_NAME (type
, method_idx
),
5046 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type
, method_idx
)),
5047 TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
));
5048 for (overload_idx
= 0;
5049 overload_idx
< TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
);
5053 ("%*s[%d] physname '%s' (%s)\n",
5054 spaces
+ 4, "", overload_idx
,
5055 TYPE_FN_FIELD_PHYSNAME (f
, overload_idx
),
5056 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f
,
5059 ("%*stype %s\n", spaces
+ 8, "",
5060 host_address_to_string (TYPE_FN_FIELD_TYPE (f
, overload_idx
)));
5062 recursive_dump_type (TYPE_FN_FIELD_TYPE (f
, overload_idx
),
5066 ("%*sargs %s\n", spaces
+ 8, "",
5067 host_address_to_string (TYPE_FN_FIELD_ARGS (f
, overload_idx
)));
5068 print_args (TYPE_FN_FIELD_ARGS (f
, overload_idx
),
5069 TYPE_FN_FIELD_TYPE (f
, overload_idx
)->num_fields (),
5072 ("%*sfcontext %s\n", spaces
+ 8, "",
5073 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f
,
5076 gdb_printf ("%*sis_const %d\n", spaces
+ 8, "",
5077 TYPE_FN_FIELD_CONST (f
, overload_idx
));
5078 gdb_printf ("%*sis_volatile %d\n", spaces
+ 8, "",
5079 TYPE_FN_FIELD_VOLATILE (f
, overload_idx
));
5080 gdb_printf ("%*sis_private %d\n", spaces
+ 8, "",
5081 TYPE_FN_FIELD_PRIVATE (f
, overload_idx
));
5082 gdb_printf ("%*sis_protected %d\n", spaces
+ 8, "",
5083 TYPE_FN_FIELD_PROTECTED (f
, overload_idx
));
5084 gdb_printf ("%*sis_stub %d\n", spaces
+ 8, "",
5085 TYPE_FN_FIELD_STUB (f
, overload_idx
));
5086 gdb_printf ("%*sdefaulted %d\n", spaces
+ 8, "",
5087 TYPE_FN_FIELD_DEFAULTED (f
, overload_idx
));
5088 gdb_printf ("%*sis_deleted %d\n", spaces
+ 8, "",
5089 TYPE_FN_FIELD_DELETED (f
, overload_idx
));
5090 gdb_printf ("%*svoffset %u\n", spaces
+ 8, "",
5091 TYPE_FN_FIELD_VOFFSET (f
, overload_idx
));
5097 print_cplus_stuff (struct type
*type
, int spaces
)
5099 gdb_printf ("%*svptr_fieldno %d\n", spaces
, "",
5100 TYPE_VPTR_FIELDNO (type
));
5101 gdb_printf ("%*svptr_basetype %s\n", spaces
, "",
5102 host_address_to_string (TYPE_VPTR_BASETYPE (type
)));
5103 if (TYPE_VPTR_BASETYPE (type
) != NULL
)
5104 recursive_dump_type (TYPE_VPTR_BASETYPE (type
), spaces
+ 2);
5106 gdb_printf ("%*sn_baseclasses %d\n", spaces
, "",
5107 TYPE_N_BASECLASSES (type
));
5108 gdb_printf ("%*snfn_fields %d\n", spaces
, "",
5109 TYPE_NFN_FIELDS (type
));
5110 if (TYPE_N_BASECLASSES (type
) > 0)
5113 ("%*svirtual_field_bits (%d bits at *%s)",
5114 spaces
, "", TYPE_N_BASECLASSES (type
),
5115 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type
)));
5117 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type
),
5118 TYPE_N_BASECLASSES (type
));
5121 if (type
->num_fields () > 0)
5123 if (TYPE_FIELD_PRIVATE_BITS (type
) != NULL
)
5126 ("%*sprivate_field_bits (%d bits at *%s)",
5127 spaces
, "", type
->num_fields (),
5128 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type
)));
5129 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type
),
5130 type
->num_fields ());
5133 if (TYPE_FIELD_PROTECTED_BITS (type
) != NULL
)
5136 ("%*sprotected_field_bits (%d bits at *%s",
5137 spaces
, "", type
->num_fields (),
5138 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type
)));
5139 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type
),
5140 type
->num_fields ());
5144 if (TYPE_NFN_FIELDS (type
) > 0)
5146 dump_fn_fieldlists (type
, spaces
);
5149 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5150 TYPE_CPLUS_CALLING_CONVENTION (type
));
5153 /* Print the contents of the TYPE's type_specific union, assuming that
5154 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5157 print_gnat_stuff (struct type
*type
, int spaces
)
5159 struct type
*descriptive_type
= TYPE_DESCRIPTIVE_TYPE (type
);
5161 if (descriptive_type
== NULL
)
5162 gdb_printf ("%*sno descriptive type\n", spaces
+ 2, "");
5165 gdb_printf ("%*sdescriptive type\n", spaces
+ 2, "");
5166 recursive_dump_type (descriptive_type
, spaces
+ 4);
5170 /* Print the contents of the TYPE's type_specific union, assuming that
5171 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5174 print_fixed_point_type_info (struct type
*type
, int spaces
)
5176 gdb_printf ("%*sscaling factor: %s\n", spaces
+ 2, "",
5177 type
->fixed_point_scaling_factor ().str ().c_str ());
5180 static struct obstack dont_print_type_obstack
;
5182 /* Print the dynamic_prop PROP. */
5185 dump_dynamic_prop (dynamic_prop
const& prop
)
5187 switch (prop
.kind ())
5190 gdb_printf ("%s", plongest (prop
.const_val ()));
5192 case PROP_UNDEFINED
:
5193 gdb_printf ("(undefined)");
5197 gdb_printf ("(dynamic)");
5200 gdb_assert_not_reached ("unhandled prop kind");
5206 recursive_dump_type (struct type
*type
, int spaces
)
5211 obstack_begin (&dont_print_type_obstack
, 0);
5213 if (type
->num_fields () > 0
5214 || (HAVE_CPLUS_STRUCT (type
) && TYPE_NFN_FIELDS (type
) > 0))
5216 struct type
**first_dont_print
5217 = (struct type
**) obstack_base (&dont_print_type_obstack
);
5219 int i
= (struct type
**)
5220 obstack_next_free (&dont_print_type_obstack
) - first_dont_print
;
5224 if (type
== first_dont_print
[i
])
5226 gdb_printf ("%*stype node %s", spaces
, "",
5227 host_address_to_string (type
));
5228 gdb_printf (_(" <same as already seen type>\n"));
5233 obstack_ptr_grow (&dont_print_type_obstack
, type
);
5236 gdb_printf ("%*stype node %s\n", spaces
, "",
5237 host_address_to_string (type
));
5238 gdb_printf ("%*sname '%s' (%s)\n", spaces
, "",
5239 type
->name () ? type
->name () : "<NULL>",
5240 host_address_to_string (type
->name ()));
5241 gdb_printf ("%*scode 0x%x ", spaces
, "", type
->code ());
5242 switch (type
->code ())
5244 case TYPE_CODE_UNDEF
:
5245 gdb_printf ("(TYPE_CODE_UNDEF)");
5248 gdb_printf ("(TYPE_CODE_PTR)");
5250 case TYPE_CODE_ARRAY
:
5251 gdb_printf ("(TYPE_CODE_ARRAY)");
5253 case TYPE_CODE_STRUCT
:
5254 gdb_printf ("(TYPE_CODE_STRUCT)");
5256 case TYPE_CODE_UNION
:
5257 gdb_printf ("(TYPE_CODE_UNION)");
5259 case TYPE_CODE_ENUM
:
5260 gdb_printf ("(TYPE_CODE_ENUM)");
5262 case TYPE_CODE_FLAGS
:
5263 gdb_printf ("(TYPE_CODE_FLAGS)");
5265 case TYPE_CODE_FUNC
:
5266 gdb_printf ("(TYPE_CODE_FUNC)");
5269 gdb_printf ("(TYPE_CODE_INT)");
5272 gdb_printf ("(TYPE_CODE_FLT)");
5274 case TYPE_CODE_VOID
:
5275 gdb_printf ("(TYPE_CODE_VOID)");
5278 gdb_printf ("(TYPE_CODE_SET)");
5280 case TYPE_CODE_RANGE
:
5281 gdb_printf ("(TYPE_CODE_RANGE)");
5283 case TYPE_CODE_STRING
:
5284 gdb_printf ("(TYPE_CODE_STRING)");
5286 case TYPE_CODE_ERROR
:
5287 gdb_printf ("(TYPE_CODE_ERROR)");
5289 case TYPE_CODE_MEMBERPTR
:
5290 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5292 case TYPE_CODE_METHODPTR
:
5293 gdb_printf ("(TYPE_CODE_METHODPTR)");
5295 case TYPE_CODE_METHOD
:
5296 gdb_printf ("(TYPE_CODE_METHOD)");
5299 gdb_printf ("(TYPE_CODE_REF)");
5301 case TYPE_CODE_CHAR
:
5302 gdb_printf ("(TYPE_CODE_CHAR)");
5304 case TYPE_CODE_BOOL
:
5305 gdb_printf ("(TYPE_CODE_BOOL)");
5307 case TYPE_CODE_COMPLEX
:
5308 gdb_printf ("(TYPE_CODE_COMPLEX)");
5310 case TYPE_CODE_TYPEDEF
:
5311 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5313 case TYPE_CODE_NAMESPACE
:
5314 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5316 case TYPE_CODE_FIXED_POINT
:
5317 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5320 gdb_printf ("(UNKNOWN TYPE CODE)");
5324 gdb_printf ("%*slength %s\n", spaces
, "",
5325 pulongest (TYPE_LENGTH (type
)));
5326 if (type
->is_objfile_owned ())
5327 gdb_printf ("%*sobjfile %s\n", spaces
, "",
5328 host_address_to_string (type
->objfile_owner ()));
5330 gdb_printf ("%*sgdbarch %s\n", spaces
, "",
5331 host_address_to_string (type
->arch_owner ()));
5332 gdb_printf ("%*starget_type %s\n", spaces
, "",
5333 host_address_to_string (TYPE_TARGET_TYPE (type
)));
5334 if (TYPE_TARGET_TYPE (type
) != NULL
)
5336 recursive_dump_type (TYPE_TARGET_TYPE (type
), spaces
+ 2);
5338 gdb_printf ("%*spointer_type %s\n", spaces
, "",
5339 host_address_to_string (TYPE_POINTER_TYPE (type
)));
5340 gdb_printf ("%*sreference_type %s\n", spaces
, "",
5341 host_address_to_string (TYPE_REFERENCE_TYPE (type
)));
5342 gdb_printf ("%*stype_chain %s\n", spaces
, "",
5343 host_address_to_string (TYPE_CHAIN (type
)));
5344 gdb_printf ("%*sinstance_flags 0x%x", spaces
, "",
5345 (unsigned) type
->instance_flags ());
5346 if (TYPE_CONST (type
))
5348 gdb_puts (" TYPE_CONST");
5350 if (TYPE_VOLATILE (type
))
5352 gdb_puts (" TYPE_VOLATILE");
5354 if (TYPE_CODE_SPACE (type
))
5356 gdb_puts (" TYPE_CODE_SPACE");
5358 if (TYPE_DATA_SPACE (type
))
5360 gdb_puts (" TYPE_DATA_SPACE");
5362 if (TYPE_ADDRESS_CLASS_1 (type
))
5364 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5366 if (TYPE_ADDRESS_CLASS_2 (type
))
5368 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5370 if (TYPE_RESTRICT (type
))
5372 gdb_puts (" TYPE_RESTRICT");
5374 if (TYPE_ATOMIC (type
))
5376 gdb_puts (" TYPE_ATOMIC");
5380 gdb_printf ("%*sflags", spaces
, "");
5381 if (type
->is_unsigned ())
5383 gdb_puts (" TYPE_UNSIGNED");
5385 if (type
->has_no_signedness ())
5387 gdb_puts (" TYPE_NOSIGN");
5389 if (type
->endianity_is_not_default ())
5391 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5393 if (type
->is_stub ())
5395 gdb_puts (" TYPE_STUB");
5397 if (type
->target_is_stub ())
5399 gdb_puts (" TYPE_TARGET_STUB");
5401 if (type
->is_prototyped ())
5403 gdb_puts (" TYPE_PROTOTYPED");
5405 if (type
->has_varargs ())
5407 gdb_puts (" TYPE_VARARGS");
5409 /* This is used for things like AltiVec registers on ppc. Gcc emits
5410 an attribute for the array type, which tells whether or not we
5411 have a vector, instead of a regular array. */
5412 if (type
->is_vector ())
5414 gdb_puts (" TYPE_VECTOR");
5416 if (type
->is_fixed_instance ())
5418 gdb_puts (" TYPE_FIXED_INSTANCE");
5420 if (type
->stub_is_supported ())
5422 gdb_puts (" TYPE_STUB_SUPPORTED");
5424 if (TYPE_NOTTEXT (type
))
5426 gdb_puts (" TYPE_NOTTEXT");
5429 gdb_printf ("%*snfields %d ", spaces
, "", type
->num_fields ());
5430 if (TYPE_ASSOCIATED_PROP (type
) != nullptr
5431 || TYPE_ALLOCATED_PROP (type
) != nullptr)
5433 gdb_printf ("%*s", spaces
, "");
5434 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5436 gdb_printf ("associated ");
5437 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type
));
5439 if (TYPE_ALLOCATED_PROP (type
) != nullptr)
5441 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5443 gdb_printf ("allocated ");
5444 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type
));
5448 gdb_printf ("%s\n", host_address_to_string (type
->fields ()));
5449 for (idx
= 0; idx
< type
->num_fields (); idx
++)
5451 if (type
->code () == TYPE_CODE_ENUM
)
5452 gdb_printf ("%*s[%d] enumval %s type ", spaces
+ 2, "",
5453 idx
, plongest (type
->field (idx
).loc_enumval ()));
5455 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces
+ 2, "",
5456 idx
, plongest (type
->field (idx
).loc_bitpos ()),
5457 TYPE_FIELD_BITSIZE (type
, idx
));
5458 gdb_printf ("%s name '%s' (%s)\n",
5459 host_address_to_string (type
->field (idx
).type ()),
5460 type
->field (idx
).name () != NULL
5461 ? type
->field (idx
).name ()
5463 host_address_to_string (type
->field (idx
).name ()));
5464 if (type
->field (idx
).type () != NULL
)
5466 recursive_dump_type (type
->field (idx
).type (), spaces
+ 4);
5469 if (type
->code () == TYPE_CODE_RANGE
)
5471 gdb_printf ("%*slow ", spaces
, "");
5472 dump_dynamic_prop (type
->bounds ()->low
);
5473 gdb_printf (" high ");
5474 dump_dynamic_prop (type
->bounds ()->high
);
5478 switch (TYPE_SPECIFIC_FIELD (type
))
5480 case TYPE_SPECIFIC_CPLUS_STUFF
:
5481 gdb_printf ("%*scplus_stuff %s\n", spaces
, "",
5482 host_address_to_string (TYPE_CPLUS_SPECIFIC (type
)));
5483 print_cplus_stuff (type
, spaces
);
5486 case TYPE_SPECIFIC_GNAT_STUFF
:
5487 gdb_printf ("%*sgnat_stuff %s\n", spaces
, "",
5488 host_address_to_string (TYPE_GNAT_SPECIFIC (type
)));
5489 print_gnat_stuff (type
, spaces
);
5492 case TYPE_SPECIFIC_FLOATFORMAT
:
5493 gdb_printf ("%*sfloatformat ", spaces
, "");
5494 if (TYPE_FLOATFORMAT (type
) == NULL
5495 || TYPE_FLOATFORMAT (type
)->name
== NULL
)
5496 gdb_puts ("(null)");
5498 gdb_puts (TYPE_FLOATFORMAT (type
)->name
);
5502 case TYPE_SPECIFIC_FUNC
:
5503 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5504 TYPE_CALLING_CONVENTION (type
));
5505 /* tail_call_list is not printed. */
5508 case TYPE_SPECIFIC_SELF_TYPE
:
5509 gdb_printf ("%*sself_type %s\n", spaces
, "",
5510 host_address_to_string (TYPE_SELF_TYPE (type
)));
5513 case TYPE_SPECIFIC_FIXED_POINT
:
5514 gdb_printf ("%*sfixed_point_info ", spaces
, "");
5515 print_fixed_point_type_info (type
, spaces
);
5519 case TYPE_SPECIFIC_INT
:
5520 if (type
->bit_size_differs_p ())
5522 unsigned bit_size
= type
->bit_size ();
5523 unsigned bit_off
= type
->bit_offset ();
5524 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces
, "",
5531 obstack_free (&dont_print_type_obstack
, NULL
);
5534 /* Trivial helpers for the libiberty hash table, for mapping one
5537 struct type_pair
: public allocate_on_obstack
5539 type_pair (struct type
*old_
, struct type
*newobj_
)
5540 : old (old_
), newobj (newobj_
)
5543 struct type
* const old
, * const newobj
;
5547 type_pair_hash (const void *item
)
5549 const struct type_pair
*pair
= (const struct type_pair
*) item
;
5551 return htab_hash_pointer (pair
->old
);
5555 type_pair_eq (const void *item_lhs
, const void *item_rhs
)
5557 const struct type_pair
*lhs
= (const struct type_pair
*) item_lhs
;
5558 const struct type_pair
*rhs
= (const struct type_pair
*) item_rhs
;
5560 return lhs
->old
== rhs
->old
;
5563 /* Allocate the hash table used by copy_type_recursive to walk
5564 types without duplicates. We use OBJFILE's obstack, because
5565 OBJFILE is about to be deleted. */
5568 create_copied_types_hash (struct objfile
*objfile
)
5570 return htab_up (htab_create_alloc_ex (1, type_pair_hash
, type_pair_eq
,
5571 NULL
, &objfile
->objfile_obstack
,
5572 hashtab_obstack_allocate
,
5573 dummy_obstack_deallocate
));
5576 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5578 static struct dynamic_prop_list
*
5579 copy_dynamic_prop_list (struct obstack
*objfile_obstack
,
5580 struct dynamic_prop_list
*list
)
5582 struct dynamic_prop_list
*copy
= list
;
5583 struct dynamic_prop_list
**node_ptr
= ©
;
5585 while (*node_ptr
!= NULL
)
5587 struct dynamic_prop_list
*node_copy
;
5589 node_copy
= ((struct dynamic_prop_list
*)
5590 obstack_copy (objfile_obstack
, *node_ptr
,
5591 sizeof (struct dynamic_prop_list
)));
5592 node_copy
->prop
= (*node_ptr
)->prop
;
5593 *node_ptr
= node_copy
;
5595 node_ptr
= &node_copy
->next
;
5601 /* Recursively copy (deep copy) TYPE, if it is associated with
5602 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5603 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5604 it is not associated with OBJFILE. */
5607 copy_type_recursive (struct objfile
*objfile
,
5609 htab_t copied_types
)
5612 struct type
*new_type
;
5614 if (!type
->is_objfile_owned ())
5617 /* This type shouldn't be pointing to any types in other objfiles;
5618 if it did, the type might disappear unexpectedly. */
5619 gdb_assert (type
->objfile_owner () == objfile
);
5621 struct type_pair
pair (type
, nullptr);
5623 slot
= htab_find_slot (copied_types
, &pair
, INSERT
);
5625 return ((struct type_pair
*) *slot
)->newobj
;
5627 new_type
= alloc_type_arch (type
->arch ());
5629 /* We must add the new type to the hash table immediately, in case
5630 we encounter this type again during a recursive call below. */
5631 struct type_pair
*stored
5632 = new (&objfile
->objfile_obstack
) struct type_pair (type
, new_type
);
5636 /* Copy the common fields of types. For the main type, we simply
5637 copy the entire thing and then update specific fields as needed. */
5638 *TYPE_MAIN_TYPE (new_type
) = *TYPE_MAIN_TYPE (type
);
5640 new_type
->set_owner (type
->arch ());
5643 new_type
->set_name (xstrdup (type
->name ()));
5645 new_type
->set_instance_flags (type
->instance_flags ());
5646 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5648 /* Copy the fields. */
5649 if (type
->num_fields ())
5653 nfields
= type
->num_fields ();
5654 new_type
->set_fields
5656 TYPE_ZALLOC (new_type
, nfields
* sizeof (struct field
)));
5658 for (i
= 0; i
< nfields
; i
++)
5660 TYPE_FIELD_ARTIFICIAL (new_type
, i
) =
5661 TYPE_FIELD_ARTIFICIAL (type
, i
);
5662 TYPE_FIELD_BITSIZE (new_type
, i
) = TYPE_FIELD_BITSIZE (type
, i
);
5663 if (type
->field (i
).type ())
5664 new_type
->field (i
).set_type
5665 (copy_type_recursive (objfile
, type
->field (i
).type (),
5667 if (type
->field (i
).name ())
5668 new_type
->field (i
).set_name (xstrdup (type
->field (i
).name ()));
5670 switch (type
->field (i
).loc_kind ())
5672 case FIELD_LOC_KIND_BITPOS
:
5673 new_type
->field (i
).set_loc_bitpos (type
->field (i
).loc_bitpos ());
5675 case FIELD_LOC_KIND_ENUMVAL
:
5676 new_type
->field (i
).set_loc_enumval (type
->field (i
).loc_enumval ());
5678 case FIELD_LOC_KIND_PHYSADDR
:
5679 new_type
->field (i
).set_loc_physaddr
5680 (type
->field (i
).loc_physaddr ());
5682 case FIELD_LOC_KIND_PHYSNAME
:
5683 new_type
->field (i
).set_loc_physname
5684 (xstrdup (type
->field (i
).loc_physname ()));
5686 case FIELD_LOC_KIND_DWARF_BLOCK
:
5687 new_type
->field (i
).set_loc_dwarf_block
5688 (type
->field (i
).loc_dwarf_block ());
5691 internal_error (__FILE__
, __LINE__
,
5692 _("Unexpected type field location kind: %d"),
5693 type
->field (i
).loc_kind ());
5698 /* For range types, copy the bounds information. */
5699 if (type
->code () == TYPE_CODE_RANGE
)
5701 range_bounds
*bounds
5702 = ((struct range_bounds
*) TYPE_ALLOC
5703 (new_type
, sizeof (struct range_bounds
)));
5705 *bounds
= *type
->bounds ();
5706 new_type
->set_bounds (bounds
);
5709 if (type
->main_type
->dyn_prop_list
!= NULL
)
5710 new_type
->main_type
->dyn_prop_list
5711 = copy_dynamic_prop_list (&objfile
->objfile_obstack
,
5712 type
->main_type
->dyn_prop_list
);
5715 /* Copy pointers to other types. */
5716 if (TYPE_TARGET_TYPE (type
))
5717 TYPE_TARGET_TYPE (new_type
) =
5718 copy_type_recursive (objfile
,
5719 TYPE_TARGET_TYPE (type
),
5722 /* Maybe copy the type_specific bits.
5724 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5725 base classes and methods. There's no fundamental reason why we
5726 can't, but at the moment it is not needed. */
5728 switch (TYPE_SPECIFIC_FIELD (type
))
5730 case TYPE_SPECIFIC_NONE
:
5732 case TYPE_SPECIFIC_FUNC
:
5733 INIT_FUNC_SPECIFIC (new_type
);
5734 TYPE_CALLING_CONVENTION (new_type
) = TYPE_CALLING_CONVENTION (type
);
5735 TYPE_NO_RETURN (new_type
) = TYPE_NO_RETURN (type
);
5736 TYPE_TAIL_CALL_LIST (new_type
) = NULL
;
5738 case TYPE_SPECIFIC_FLOATFORMAT
:
5739 TYPE_FLOATFORMAT (new_type
) = TYPE_FLOATFORMAT (type
);
5741 case TYPE_SPECIFIC_CPLUS_STUFF
:
5742 INIT_CPLUS_SPECIFIC (new_type
);
5744 case TYPE_SPECIFIC_GNAT_STUFF
:
5745 INIT_GNAT_SPECIFIC (new_type
);
5747 case TYPE_SPECIFIC_SELF_TYPE
:
5748 set_type_self_type (new_type
,
5749 copy_type_recursive (objfile
, TYPE_SELF_TYPE (type
),
5752 case TYPE_SPECIFIC_FIXED_POINT
:
5753 INIT_FIXED_POINT_SPECIFIC (new_type
);
5754 new_type
->fixed_point_info ().scaling_factor
5755 = type
->fixed_point_info ().scaling_factor
;
5757 case TYPE_SPECIFIC_INT
:
5758 TYPE_SPECIFIC_FIELD (new_type
) = TYPE_SPECIFIC_INT
;
5759 TYPE_MAIN_TYPE (new_type
)->type_specific
.int_stuff
5760 = TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
;
5764 gdb_assert_not_reached ("bad type_specific_kind");
5770 /* Make a copy of the given TYPE, except that the pointer & reference
5771 types are not preserved.
5773 This function assumes that the given type has an associated objfile.
5774 This objfile is used to allocate the new type. */
5777 copy_type (const struct type
*type
)
5779 struct type
*new_type
;
5781 gdb_assert (type
->is_objfile_owned ());
5783 new_type
= alloc_type_copy (type
);
5784 new_type
->set_instance_flags (type
->instance_flags ());
5785 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5786 memcpy (TYPE_MAIN_TYPE (new_type
), TYPE_MAIN_TYPE (type
),
5787 sizeof (struct main_type
));
5788 if (type
->main_type
->dyn_prop_list
!= NULL
)
5789 new_type
->main_type
->dyn_prop_list
5790 = copy_dynamic_prop_list (&type
->objfile_owner ()->objfile_obstack
,
5791 type
->main_type
->dyn_prop_list
);
5796 /* Helper functions to initialize architecture-specific types. */
5798 /* Allocate a type structure associated with GDBARCH and set its
5799 CODE, LENGTH, and NAME fields. */
5802 arch_type (struct gdbarch
*gdbarch
,
5803 enum type_code code
, int bit
, const char *name
)
5807 type
= alloc_type_arch (gdbarch
);
5808 set_type_code (type
, code
);
5809 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
5810 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
5813 type
->set_name (gdbarch_obstack_strdup (gdbarch
, name
));
5818 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
5819 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5820 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5823 arch_integer_type (struct gdbarch
*gdbarch
,
5824 int bit
, int unsigned_p
, const char *name
)
5828 t
= arch_type (gdbarch
, TYPE_CODE_INT
, bit
, name
);
5830 t
->set_is_unsigned (true);
5835 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
5836 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5837 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5840 arch_character_type (struct gdbarch
*gdbarch
,
5841 int bit
, int unsigned_p
, const char *name
)
5845 t
= arch_type (gdbarch
, TYPE_CODE_CHAR
, bit
, name
);
5847 t
->set_is_unsigned (true);
5852 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
5853 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5854 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5857 arch_boolean_type (struct gdbarch
*gdbarch
,
5858 int bit
, int unsigned_p
, const char *name
)
5862 t
= arch_type (gdbarch
, TYPE_CODE_BOOL
, bit
, name
);
5864 t
->set_is_unsigned (true);
5869 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
5870 BIT is the type size in bits; if BIT equals -1, the size is
5871 determined by the floatformat. NAME is the type name. Set the
5872 TYPE_FLOATFORMAT from FLOATFORMATS. */
5875 arch_float_type (struct gdbarch
*gdbarch
,
5876 int bit
, const char *name
,
5877 const struct floatformat
**floatformats
)
5879 const struct floatformat
*fmt
= floatformats
[gdbarch_byte_order (gdbarch
)];
5882 bit
= verify_floatformat (bit
, fmt
);
5883 t
= arch_type (gdbarch
, TYPE_CODE_FLT
, bit
, name
);
5884 TYPE_FLOATFORMAT (t
) = fmt
;
5889 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
5890 BIT is the type size in bits. NAME is the type name. */
5893 arch_decfloat_type (struct gdbarch
*gdbarch
, int bit
, const char *name
)
5897 t
= arch_type (gdbarch
, TYPE_CODE_DECFLOAT
, bit
, name
);
5901 /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
5902 BIT is the pointer type size in bits. NAME is the type name.
5903 TARGET_TYPE is the pointer target type. Always sets the pointer type's
5904 TYPE_UNSIGNED flag. */
5907 arch_pointer_type (struct gdbarch
*gdbarch
,
5908 int bit
, const char *name
, struct type
*target_type
)
5912 t
= arch_type (gdbarch
, TYPE_CODE_PTR
, bit
, name
);
5913 TYPE_TARGET_TYPE (t
) = target_type
;
5914 t
->set_is_unsigned (true);
5918 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5919 NAME is the type name. BIT is the size of the flag word in bits. */
5922 arch_flags_type (struct gdbarch
*gdbarch
, const char *name
, int bit
)
5926 type
= arch_type (gdbarch
, TYPE_CODE_FLAGS
, bit
, name
);
5927 type
->set_is_unsigned (true);
5928 type
->set_num_fields (0);
5929 /* Pre-allocate enough space assuming every field is one bit. */
5931 ((struct field
*) TYPE_ZALLOC (type
, bit
* sizeof (struct field
)));
5936 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5937 position BITPOS is called NAME. Pass NAME as "" for fields that
5938 should not be printed. */
5941 append_flags_type_field (struct type
*type
, int start_bitpos
, int nr_bits
,
5942 struct type
*field_type
, const char *name
)
5944 int type_bitsize
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
5945 int field_nr
= type
->num_fields ();
5947 gdb_assert (type
->code () == TYPE_CODE_FLAGS
);
5948 gdb_assert (type
->num_fields () + 1 <= type_bitsize
);
5949 gdb_assert (start_bitpos
>= 0 && start_bitpos
< type_bitsize
);
5950 gdb_assert (nr_bits
>= 1 && (start_bitpos
+ nr_bits
) <= type_bitsize
);
5951 gdb_assert (name
!= NULL
);
5953 type
->set_num_fields (type
->num_fields () + 1);
5954 type
->field (field_nr
).set_name (xstrdup (name
));
5955 type
->field (field_nr
).set_type (field_type
);
5956 type
->field (field_nr
).set_loc_bitpos (start_bitpos
);
5957 TYPE_FIELD_BITSIZE (type
, field_nr
) = nr_bits
;
5960 /* Special version of append_flags_type_field to add a flag field.
5961 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5962 position BITPOS is called NAME. */
5965 append_flags_type_flag (struct type
*type
, int bitpos
, const char *name
)
5967 append_flags_type_field (type
, bitpos
, 1,
5968 builtin_type (type
->arch ())->builtin_bool
,
5972 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5973 specified by CODE) associated with GDBARCH. NAME is the type name. */
5976 arch_composite_type (struct gdbarch
*gdbarch
, const char *name
,
5977 enum type_code code
)
5981 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
);
5982 t
= arch_type (gdbarch
, code
, 0, NULL
);
5984 INIT_CPLUS_SPECIFIC (t
);
5988 /* Add new field with name NAME and type FIELD to composite type T.
5989 Do not set the field's position or adjust the type's length;
5990 the caller should do so. Return the new field. */
5993 append_composite_type_field_raw (struct type
*t
, const char *name
,
5998 t
->set_num_fields (t
->num_fields () + 1);
5999 t
->set_fields (XRESIZEVEC (struct field
, t
->fields (),
6001 f
= &t
->field (t
->num_fields () - 1);
6002 memset (f
, 0, sizeof f
[0]);
6003 f
[0].set_type (field
);
6004 f
[0].set_name (name
);
6008 /* Add new field with name NAME and type FIELD to composite type T.
6009 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
6012 append_composite_type_field_aligned (struct type
*t
, const char *name
,
6013 struct type
*field
, int alignment
)
6015 struct field
*f
= append_composite_type_field_raw (t
, name
, field
);
6017 if (t
->code () == TYPE_CODE_UNION
)
6019 if (TYPE_LENGTH (t
) < TYPE_LENGTH (field
))
6020 TYPE_LENGTH (t
) = TYPE_LENGTH (field
);
6022 else if (t
->code () == TYPE_CODE_STRUCT
)
6024 TYPE_LENGTH (t
) = TYPE_LENGTH (t
) + TYPE_LENGTH (field
);
6025 if (t
->num_fields () > 1)
6028 (f
[-1].loc_bitpos () + (TYPE_LENGTH (f
[-1].type ()) * TARGET_CHAR_BIT
));
6034 alignment
*= TARGET_CHAR_BIT
;
6035 left
= f
[0].loc_bitpos () % alignment
;
6039 f
->set_loc_bitpos (f
[0].loc_bitpos () + (alignment
- left
));
6040 TYPE_LENGTH (t
) += (alignment
- left
) / TARGET_CHAR_BIT
;
6047 /* Add new field with name NAME and type FIELD to composite type T. */
6050 append_composite_type_field (struct type
*t
, const char *name
,
6053 append_composite_type_field_aligned (t
, name
, field
, 0);
6058 /* We manage the lifetimes of fixed_point_type_info objects by
6059 attaching them to the objfile. Currently, these objects are
6060 modified during construction, and GMP does not provide a way to
6061 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
6062 them. If we did do this, they could be moved to the per-BFD and
6063 shared across objfiles. */
6064 typedef std::vector
<std::unique_ptr
<fixed_point_type_info
>>
6065 fixed_point_type_storage
;
6067 /* Key used for managing the storage of fixed-point type info. */
6068 static const struct objfile_key
<fixed_point_type_storage
>
6069 fixed_point_objfile_key
;
6071 /* See gdbtypes.h. */
6074 allocate_fixed_point_type_info (struct type
*type
)
6076 std::unique_ptr
<fixed_point_type_info
> up (new fixed_point_type_info
);
6077 fixed_point_type_info
*info
;
6079 if (type
->is_objfile_owned ())
6081 fixed_point_type_storage
*storage
6082 = fixed_point_objfile_key
.get (type
->objfile_owner ());
6083 if (storage
== nullptr)
6084 storage
= fixed_point_objfile_key
.emplace (type
->objfile_owner ());
6086 storage
->push_back (std::move (up
));
6090 /* We just leak the memory, because that's what we do generally
6091 for non-objfile-attached types. */
6092 info
= up
.release ();
6095 type
->set_fixed_point_info (info
);
6098 /* See gdbtypes.h. */
6101 is_fixed_point_type (struct type
*type
)
6103 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6104 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6105 type
= check_typedef (type
);
6107 return type
->code () == TYPE_CODE_FIXED_POINT
;
6110 /* See gdbtypes.h. */
6113 type::fixed_point_type_base_type ()
6115 struct type
*type
= this;
6117 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6118 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6119 type
= check_typedef (type
);
6121 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
);
6125 /* See gdbtypes.h. */
6128 type::fixed_point_scaling_factor ()
6130 struct type
*type
= this->fixed_point_type_base_type ();
6132 return type
->fixed_point_info ().scaling_factor
;
6137 static struct gdbarch_data
*gdbtypes_data
;
6139 const struct builtin_type
*
6140 builtin_type (struct gdbarch
*gdbarch
)
6142 return (const struct builtin_type
*) gdbarch_data (gdbarch
, gdbtypes_data
);
6146 gdbtypes_post_init (struct gdbarch
*gdbarch
)
6148 struct builtin_type
*builtin_type
6149 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_type
);
6152 builtin_type
->builtin_void
6153 = arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6154 builtin_type
->builtin_char
6155 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6156 !gdbarch_char_signed (gdbarch
), "char");
6157 builtin_type
->builtin_char
->set_has_no_signedness (true);
6158 builtin_type
->builtin_signed_char
6159 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6161 builtin_type
->builtin_unsigned_char
6162 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6163 1, "unsigned char");
6164 builtin_type
->builtin_short
6165 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6167 builtin_type
->builtin_unsigned_short
6168 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6169 1, "unsigned short");
6170 builtin_type
->builtin_int
6171 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6173 builtin_type
->builtin_unsigned_int
6174 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6176 builtin_type
->builtin_long
6177 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6179 builtin_type
->builtin_unsigned_long
6180 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6181 1, "unsigned long");
6182 builtin_type
->builtin_long_long
6183 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6185 builtin_type
->builtin_unsigned_long_long
6186 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6187 1, "unsigned long long");
6188 builtin_type
->builtin_half
6189 = arch_float_type (gdbarch
, gdbarch_half_bit (gdbarch
),
6190 "half", gdbarch_half_format (gdbarch
));
6191 builtin_type
->builtin_float
6192 = arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
),
6193 "float", gdbarch_float_format (gdbarch
));
6194 builtin_type
->builtin_bfloat16
6195 = arch_float_type (gdbarch
, gdbarch_bfloat16_bit (gdbarch
),
6196 "bfloat16", gdbarch_bfloat16_format (gdbarch
));
6197 builtin_type
->builtin_double
6198 = arch_float_type (gdbarch
, gdbarch_double_bit (gdbarch
),
6199 "double", gdbarch_double_format (gdbarch
));
6200 builtin_type
->builtin_long_double
6201 = arch_float_type (gdbarch
, gdbarch_long_double_bit (gdbarch
),
6202 "long double", gdbarch_long_double_format (gdbarch
));
6203 builtin_type
->builtin_complex
6204 = init_complex_type ("complex", builtin_type
->builtin_float
);
6205 builtin_type
->builtin_double_complex
6206 = init_complex_type ("double complex", builtin_type
->builtin_double
);
6207 builtin_type
->builtin_string
6208 = arch_type (gdbarch
, TYPE_CODE_STRING
, TARGET_CHAR_BIT
, "string");
6209 builtin_type
->builtin_bool
6210 = arch_boolean_type (gdbarch
, TARGET_CHAR_BIT
, 1, "bool");
6212 /* The following three are about decimal floating point types, which
6213 are 32-bits, 64-bits and 128-bits respectively. */
6214 builtin_type
->builtin_decfloat
6215 = arch_decfloat_type (gdbarch
, 32, "_Decimal32");
6216 builtin_type
->builtin_decdouble
6217 = arch_decfloat_type (gdbarch
, 64, "_Decimal64");
6218 builtin_type
->builtin_declong
6219 = arch_decfloat_type (gdbarch
, 128, "_Decimal128");
6221 /* "True" character types. */
6222 builtin_type
->builtin_true_char
6223 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 0, "true character");
6224 builtin_type
->builtin_true_unsigned_char
6225 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 1, "true character");
6227 /* Fixed-size integer types. */
6228 builtin_type
->builtin_int0
6229 = arch_integer_type (gdbarch
, 0, 0, "int0_t");
6230 builtin_type
->builtin_int8
6231 = arch_integer_type (gdbarch
, 8, 0, "int8_t");
6232 builtin_type
->builtin_uint8
6233 = arch_integer_type (gdbarch
, 8, 1, "uint8_t");
6234 builtin_type
->builtin_int16
6235 = arch_integer_type (gdbarch
, 16, 0, "int16_t");
6236 builtin_type
->builtin_uint16
6237 = arch_integer_type (gdbarch
, 16, 1, "uint16_t");
6238 builtin_type
->builtin_int24
6239 = arch_integer_type (gdbarch
, 24, 0, "int24_t");
6240 builtin_type
->builtin_uint24
6241 = arch_integer_type (gdbarch
, 24, 1, "uint24_t");
6242 builtin_type
->builtin_int32
6243 = arch_integer_type (gdbarch
, 32, 0, "int32_t");
6244 builtin_type
->builtin_uint32
6245 = arch_integer_type (gdbarch
, 32, 1, "uint32_t");
6246 builtin_type
->builtin_int64
6247 = arch_integer_type (gdbarch
, 64, 0, "int64_t");
6248 builtin_type
->builtin_uint64
6249 = arch_integer_type (gdbarch
, 64, 1, "uint64_t");
6250 builtin_type
->builtin_int128
6251 = arch_integer_type (gdbarch
, 128, 0, "int128_t");
6252 builtin_type
->builtin_uint128
6253 = arch_integer_type (gdbarch
, 128, 1, "uint128_t");
6255 builtin_type
->builtin_int8
->set_instance_flags
6256 (builtin_type
->builtin_int8
->instance_flags ()
6257 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6259 builtin_type
->builtin_uint8
->set_instance_flags
6260 (builtin_type
->builtin_uint8
->instance_flags ()
6261 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6263 /* Wide character types. */
6264 builtin_type
->builtin_char16
6265 = arch_integer_type (gdbarch
, 16, 1, "char16_t");
6266 builtin_type
->builtin_char32
6267 = arch_integer_type (gdbarch
, 32, 1, "char32_t");
6268 builtin_type
->builtin_wchar
6269 = arch_integer_type (gdbarch
, gdbarch_wchar_bit (gdbarch
),
6270 !gdbarch_wchar_signed (gdbarch
), "wchar_t");
6272 /* Default data/code pointer types. */
6273 builtin_type
->builtin_data_ptr
6274 = lookup_pointer_type (builtin_type
->builtin_void
);
6275 builtin_type
->builtin_func_ptr
6276 = lookup_pointer_type (lookup_function_type (builtin_type
->builtin_void
));
6277 builtin_type
->builtin_func_func
6278 = lookup_function_type (builtin_type
->builtin_func_ptr
);
6280 /* This type represents a GDB internal function. */
6281 builtin_type
->internal_fn
6282 = arch_type (gdbarch
, TYPE_CODE_INTERNAL_FUNCTION
, 0,
6283 "<internal function>");
6285 /* This type represents an xmethod. */
6286 builtin_type
->xmethod
6287 = arch_type (gdbarch
, TYPE_CODE_XMETHOD
, 0, "<xmethod>");
6289 return builtin_type
;
6292 /* This set of objfile-based types is intended to be used by symbol
6293 readers as basic types. */
6295 static const struct objfile_key
<struct objfile_type
,
6296 gdb::noop_deleter
<struct objfile_type
>>
6299 const struct objfile_type
*
6300 objfile_type (struct objfile
*objfile
)
6302 struct gdbarch
*gdbarch
;
6303 struct objfile_type
*objfile_type
= objfile_type_data
.get (objfile
);
6306 return objfile_type
;
6308 objfile_type
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
6309 1, struct objfile_type
);
6311 /* Use the objfile architecture to determine basic type properties. */
6312 gdbarch
= objfile
->arch ();
6315 objfile_type
->builtin_void
6316 = init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6317 objfile_type
->builtin_char
6318 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6319 !gdbarch_char_signed (gdbarch
), "char");
6320 objfile_type
->builtin_char
->set_has_no_signedness (true);
6321 objfile_type
->builtin_signed_char
6322 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6324 objfile_type
->builtin_unsigned_char
6325 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6326 1, "unsigned char");
6327 objfile_type
->builtin_short
6328 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6330 objfile_type
->builtin_unsigned_short
6331 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6332 1, "unsigned short");
6333 objfile_type
->builtin_int
6334 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6336 objfile_type
->builtin_unsigned_int
6337 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6339 objfile_type
->builtin_long
6340 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6342 objfile_type
->builtin_unsigned_long
6343 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6344 1, "unsigned long");
6345 objfile_type
->builtin_long_long
6346 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6348 objfile_type
->builtin_unsigned_long_long
6349 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6350 1, "unsigned long long");
6351 objfile_type
->builtin_float
6352 = init_float_type (objfile
, gdbarch_float_bit (gdbarch
),
6353 "float", gdbarch_float_format (gdbarch
));
6354 objfile_type
->builtin_double
6355 = init_float_type (objfile
, gdbarch_double_bit (gdbarch
),
6356 "double", gdbarch_double_format (gdbarch
));
6357 objfile_type
->builtin_long_double
6358 = init_float_type (objfile
, gdbarch_long_double_bit (gdbarch
),
6359 "long double", gdbarch_long_double_format (gdbarch
));
6361 /* This type represents a type that was unrecognized in symbol read-in. */
6362 objfile_type
->builtin_error
6363 = init_type (objfile
, TYPE_CODE_ERROR
, 0, "<unknown type>");
6365 /* The following set of types is used for symbols with no
6366 debug information. */
6367 objfile_type
->nodebug_text_symbol
6368 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6369 "<text variable, no debug info>");
6371 objfile_type
->nodebug_text_gnu_ifunc_symbol
6372 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6373 "<text gnu-indirect-function variable, no debug info>");
6374 objfile_type
->nodebug_text_gnu_ifunc_symbol
->set_is_gnu_ifunc (true);
6376 objfile_type
->nodebug_got_plt_symbol
6377 = init_pointer_type (objfile
, gdbarch_addr_bit (gdbarch
),
6378 "<text from jump slot in .got.plt, no debug info>",
6379 objfile_type
->nodebug_text_symbol
);
6380 objfile_type
->nodebug_data_symbol
6381 = init_nodebug_var_type (objfile
, "<data variable, no debug info>");
6382 objfile_type
->nodebug_unknown_symbol
6383 = init_nodebug_var_type (objfile
, "<variable (not text or data), no debug info>");
6384 objfile_type
->nodebug_tls_symbol
6385 = init_nodebug_var_type (objfile
, "<thread local variable, no debug info>");
6387 /* NOTE: on some targets, addresses and pointers are not necessarily
6391 - gdb's `struct type' always describes the target's
6393 - gdb's `struct value' objects should always hold values in
6395 - gdb's CORE_ADDR values are addresses in the unified virtual
6396 address space that the assembler and linker work with. Thus,
6397 since target_read_memory takes a CORE_ADDR as an argument, it
6398 can access any memory on the target, even if the processor has
6399 separate code and data address spaces.
6401 In this context, objfile_type->builtin_core_addr is a bit odd:
6402 it's a target type for a value the target will never see. It's
6403 only used to hold the values of (typeless) linker symbols, which
6404 are indeed in the unified virtual address space. */
6406 objfile_type
->builtin_core_addr
6407 = init_integer_type (objfile
, gdbarch_addr_bit (gdbarch
), 1,
6410 objfile_type_data
.set (objfile
, objfile_type
);
6411 return objfile_type
;
6414 /* See gdbtypes.h. */
6417 call_site::pc () const
6419 compunit_symtab
*cust
= this->per_objfile
->get_symtab (this->per_cu
);
6421 = this->per_objfile
->objfile
->section_offsets
[cust
->block_line_section ()];
6422 return m_unrelocated_pc
+ delta
;
6425 void _initialize_gdbtypes ();
6427 _initialize_gdbtypes ()
6429 gdbtypes_data
= gdbarch_data_register_post_init (gdbtypes_post_init
);
6431 add_setshow_zuinteger_cmd ("overload", no_class
, &overload_debug
,
6432 _("Set debugging of C++ overloading."),
6433 _("Show debugging of C++ overloading."),
6434 _("When enabled, ranking of the "
6435 "functions is displayed."),
6437 show_overload_debug
,
6438 &setdebuglist
, &showdebuglist
);
6440 /* Add user knob for controlling resolution of opaque types. */
6441 add_setshow_boolean_cmd ("opaque-type-resolution", class_support
,
6442 &opaque_type_resolution
,
6443 _("Set resolution of opaque struct/class/union"
6444 " types (if set before loading symbols)."),
6445 _("Show resolution of opaque struct/class/union"
6446 " types (if set before loading symbols)."),
6448 show_opaque_type_resolution
,
6449 &setlist
, &showlist
);
6451 /* Add an option to permit non-strict type checking. */
6452 add_setshow_boolean_cmd ("type", class_support
,
6453 &strict_type_checking
,
6454 _("Set strict type checking."),
6455 _("Show strict type checking."),
6457 show_strict_type_checking
,
6458 &setchecklist
, &showchecklist
);