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 static struct dynamic_prop_list
*
51 copy_dynamic_prop_list (struct obstack
*, struct dynamic_prop_list
*);
53 /* Initialize BADNESS constants. */
55 const struct rank LENGTH_MISMATCH_BADNESS
= {INVALID_CONVERSION
,0};
57 const struct rank TOO_FEW_PARAMS_BADNESS
= {INVALID_CONVERSION
,0};
58 const struct rank INCOMPATIBLE_TYPE_BADNESS
= {INVALID_CONVERSION
,0};
60 const struct rank EXACT_MATCH_BADNESS
= {0,0};
62 const struct rank INTEGER_PROMOTION_BADNESS
= {1,0};
63 const struct rank FLOAT_PROMOTION_BADNESS
= {1,0};
64 const struct rank BASE_PTR_CONVERSION_BADNESS
= {1,0};
65 const struct rank CV_CONVERSION_BADNESS
= {1, 0};
66 const struct rank INTEGER_CONVERSION_BADNESS
= {2,0};
67 const struct rank FLOAT_CONVERSION_BADNESS
= {2,0};
68 const struct rank INT_FLOAT_CONVERSION_BADNESS
= {2,0};
69 const struct rank VOID_PTR_CONVERSION_BADNESS
= {2,0};
70 const struct rank BOOL_CONVERSION_BADNESS
= {3,0};
71 const struct rank BASE_CONVERSION_BADNESS
= {2,0};
72 const struct rank REFERENCE_CONVERSION_BADNESS
= {2,0};
73 const struct rank REFERENCE_SEE_THROUGH_BADNESS
= {0,1};
74 const struct rank NULL_POINTER_CONVERSION_BADNESS
= {2,0};
75 const struct rank NS_POINTER_CONVERSION_BADNESS
= {10,0};
76 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS
= {3,0};
78 /* Floatformat pairs. */
79 const struct floatformat
*floatformats_ieee_half
[BFD_ENDIAN_UNKNOWN
] = {
80 &floatformat_ieee_half_big
,
81 &floatformat_ieee_half_little
83 const struct floatformat
*floatformats_ieee_single
[BFD_ENDIAN_UNKNOWN
] = {
84 &floatformat_ieee_single_big
,
85 &floatformat_ieee_single_little
87 const struct floatformat
*floatformats_ieee_double
[BFD_ENDIAN_UNKNOWN
] = {
88 &floatformat_ieee_double_big
,
89 &floatformat_ieee_double_little
91 const struct floatformat
*floatformats_ieee_quad
[BFD_ENDIAN_UNKNOWN
] = {
92 &floatformat_ieee_quad_big
,
93 &floatformat_ieee_quad_little
95 const struct floatformat
*floatformats_ieee_double_littlebyte_bigword
[BFD_ENDIAN_UNKNOWN
] = {
96 &floatformat_ieee_double_big
,
97 &floatformat_ieee_double_littlebyte_bigword
99 const struct floatformat
*floatformats_i387_ext
[BFD_ENDIAN_UNKNOWN
] = {
100 &floatformat_i387_ext
,
101 &floatformat_i387_ext
103 const struct floatformat
*floatformats_m68881_ext
[BFD_ENDIAN_UNKNOWN
] = {
104 &floatformat_m68881_ext
,
105 &floatformat_m68881_ext
107 const struct floatformat
*floatformats_arm_ext
[BFD_ENDIAN_UNKNOWN
] = {
108 &floatformat_arm_ext_big
,
109 &floatformat_arm_ext_littlebyte_bigword
111 const struct floatformat
*floatformats_ia64_spill
[BFD_ENDIAN_UNKNOWN
] = {
112 &floatformat_ia64_spill_big
,
113 &floatformat_ia64_spill_little
115 const struct floatformat
*floatformats_vax_f
[BFD_ENDIAN_UNKNOWN
] = {
119 const struct floatformat
*floatformats_vax_d
[BFD_ENDIAN_UNKNOWN
] = {
123 const struct floatformat
*floatformats_ibm_long_double
[BFD_ENDIAN_UNKNOWN
] = {
124 &floatformat_ibm_long_double_big
,
125 &floatformat_ibm_long_double_little
127 const struct floatformat
*floatformats_bfloat16
[BFD_ENDIAN_UNKNOWN
] = {
128 &floatformat_bfloat16_big
,
129 &floatformat_bfloat16_little
132 /* Should opaque types be resolved? */
134 static bool opaque_type_resolution
= true;
136 /* See gdbtypes.h. */
138 unsigned int overload_debug
= 0;
140 /* A flag to enable strict type checking. */
142 static bool strict_type_checking
= true;
144 /* A function to show whether opaque types are resolved. */
147 show_opaque_type_resolution (struct ui_file
*file
, int from_tty
,
148 struct cmd_list_element
*c
,
151 gdb_printf (file
, _("Resolution of opaque struct/class/union types "
152 "(if set before loading symbols) is %s.\n"),
156 /* A function to show whether C++ overload debugging is enabled. */
159 show_overload_debug (struct ui_file
*file
, int from_tty
,
160 struct cmd_list_element
*c
, const char *value
)
162 gdb_printf (file
, _("Debugging of C++ overloading is %s.\n"),
166 /* A function to show the status of strict type checking. */
169 show_strict_type_checking (struct ui_file
*file
, int from_tty
,
170 struct cmd_list_element
*c
, const char *value
)
172 gdb_printf (file
, _("Strict type checking is %s.\n"), value
);
176 /* Allocate a new OBJFILE-associated type structure and fill it
177 with some defaults. Space for the type structure is allocated
178 on the objfile's objfile_obstack. */
181 alloc_type (struct objfile
*objfile
)
185 gdb_assert (objfile
!= NULL
);
187 /* Alloc the structure and start off with all fields zeroed. */
188 type
= OBSTACK_ZALLOC (&objfile
->objfile_obstack
, struct type
);
189 TYPE_MAIN_TYPE (type
) = OBSTACK_ZALLOC (&objfile
->objfile_obstack
,
191 OBJSTAT (objfile
, n_types
++);
193 type
->set_owner (objfile
);
195 /* Initialize the fields that might not be zero. */
197 type
->set_code (TYPE_CODE_UNDEF
);
198 TYPE_CHAIN (type
) = type
; /* Chain back to itself. */
203 /* Allocate a new GDBARCH-associated type structure and fill it
204 with some defaults. Space for the type structure is allocated
205 on the obstack associated with GDBARCH. */
208 alloc_type_arch (struct gdbarch
*gdbarch
)
212 gdb_assert (gdbarch
!= NULL
);
214 /* Alloc the structure and start off with all fields zeroed. */
216 type
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct type
);
217 TYPE_MAIN_TYPE (type
) = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct main_type
);
219 type
->set_owner (gdbarch
);
221 /* Initialize the fields that might not be zero. */
223 type
->set_code (TYPE_CODE_UNDEF
);
224 TYPE_CHAIN (type
) = type
; /* Chain back to itself. */
229 /* If TYPE is objfile-associated, allocate a new type structure
230 associated with the same objfile. If TYPE is gdbarch-associated,
231 allocate a new type structure associated with the same gdbarch. */
234 alloc_type_copy (const struct type
*type
)
236 if (type
->is_objfile_owned ())
237 return alloc_type (type
->objfile_owner ());
239 return alloc_type_arch (type
->arch_owner ());
242 /* See gdbtypes.h. */
247 struct gdbarch
*arch
;
249 if (this->is_objfile_owned ())
250 arch
= this->objfile_owner ()->arch ();
252 arch
= this->arch_owner ();
254 /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
255 a gdbarch, however, this is very rare, and even then, in most cases
256 that type::arch is called, we assume that a non-NULL value is
258 gdb_assert (arch
!= nullptr);
262 /* See gdbtypes.h. */
265 get_target_type (struct type
*type
)
269 type
= TYPE_TARGET_TYPE (type
);
271 type
= check_typedef (type
);
277 /* See gdbtypes.h. */
280 type_length_units (struct type
*type
)
282 int unit_size
= gdbarch_addressable_memory_unit_size (type
->arch ());
284 return TYPE_LENGTH (type
) / unit_size
;
287 /* Alloc a new type instance structure, fill it with some defaults,
288 and point it at OLDTYPE. Allocate the new type instance from the
289 same place as OLDTYPE. */
292 alloc_type_instance (struct type
*oldtype
)
296 /* Allocate the structure. */
298 if (!oldtype
->is_objfile_owned ())
299 type
= GDBARCH_OBSTACK_ZALLOC (oldtype
->arch_owner (), struct type
);
301 type
= OBSTACK_ZALLOC (&oldtype
->objfile_owner ()->objfile_obstack
,
304 TYPE_MAIN_TYPE (type
) = TYPE_MAIN_TYPE (oldtype
);
306 TYPE_CHAIN (type
) = type
; /* Chain back to itself for now. */
311 /* Clear all remnants of the previous type at TYPE, in preparation for
312 replacing it with something else. Preserve owner information. */
315 smash_type (struct type
*type
)
317 bool objfile_owned
= type
->is_objfile_owned ();
318 objfile
*objfile
= type
->objfile_owner ();
319 gdbarch
*arch
= type
->arch_owner ();
321 memset (TYPE_MAIN_TYPE (type
), 0, sizeof (struct main_type
));
323 /* Restore owner information. */
325 type
->set_owner (objfile
);
327 type
->set_owner (arch
);
329 /* For now, delete the rings. */
330 TYPE_CHAIN (type
) = type
;
332 /* For now, leave the pointer/reference types alone. */
335 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
336 to a pointer to memory where the pointer type should be stored.
337 If *TYPEPTR is zero, update it to point to the pointer type we return.
338 We allocate new memory if needed. */
341 make_pointer_type (struct type
*type
, struct type
**typeptr
)
343 struct type
*ntype
; /* New type */
346 ntype
= TYPE_POINTER_TYPE (type
);
351 return ntype
; /* Don't care about alloc,
352 and have new type. */
353 else if (*typeptr
== 0)
355 *typeptr
= ntype
; /* Tracking alloc, and have new type. */
360 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
362 ntype
= alloc_type_copy (type
);
366 else /* We have storage, but need to reset it. */
369 chain
= TYPE_CHAIN (ntype
);
371 TYPE_CHAIN (ntype
) = chain
;
374 TYPE_TARGET_TYPE (ntype
) = type
;
375 TYPE_POINTER_TYPE (type
) = ntype
;
377 /* FIXME! Assumes the machine has only one representation for pointers! */
379 TYPE_LENGTH (ntype
) = gdbarch_ptr_bit (type
->arch ()) / TARGET_CHAR_BIT
;
380 ntype
->set_code (TYPE_CODE_PTR
);
382 /* Mark pointers as unsigned. The target converts between pointers
383 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
384 gdbarch_address_to_pointer. */
385 ntype
->set_is_unsigned (true);
387 /* Update the length of all the other variants of this type. */
388 chain
= TYPE_CHAIN (ntype
);
389 while (chain
!= ntype
)
391 TYPE_LENGTH (chain
) = TYPE_LENGTH (ntype
);
392 chain
= TYPE_CHAIN (chain
);
398 /* Given a type TYPE, return a type of pointers to that type.
399 May need to construct such a type if this is the first use. */
402 lookup_pointer_type (struct type
*type
)
404 return make_pointer_type (type
, (struct type
**) 0);
407 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
408 points to a pointer to memory where the reference type should be
409 stored. If *TYPEPTR is zero, update it to point to the reference
410 type we return. We allocate new memory if needed. REFCODE denotes
411 the kind of reference type to lookup (lvalue or rvalue reference). */
414 make_reference_type (struct type
*type
, struct type
**typeptr
,
415 enum type_code refcode
)
417 struct type
*ntype
; /* New type */
418 struct type
**reftype
;
421 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
423 ntype
= (refcode
== TYPE_CODE_REF
? TYPE_REFERENCE_TYPE (type
)
424 : TYPE_RVALUE_REFERENCE_TYPE (type
));
429 return ntype
; /* Don't care about alloc,
430 and have new type. */
431 else if (*typeptr
== 0)
433 *typeptr
= ntype
; /* Tracking alloc, and have new type. */
438 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
440 ntype
= alloc_type_copy (type
);
444 else /* We have storage, but need to reset it. */
447 chain
= TYPE_CHAIN (ntype
);
449 TYPE_CHAIN (ntype
) = chain
;
452 TYPE_TARGET_TYPE (ntype
) = type
;
453 reftype
= (refcode
== TYPE_CODE_REF
? &TYPE_REFERENCE_TYPE (type
)
454 : &TYPE_RVALUE_REFERENCE_TYPE (type
));
458 /* FIXME! Assume the machine has only one representation for
459 references, and that it matches the (only) representation for
462 TYPE_LENGTH (ntype
) = gdbarch_ptr_bit (type
->arch ()) / TARGET_CHAR_BIT
;
463 ntype
->set_code (refcode
);
467 /* Update the length of all the other variants of this type. */
468 chain
= TYPE_CHAIN (ntype
);
469 while (chain
!= ntype
)
471 TYPE_LENGTH (chain
) = TYPE_LENGTH (ntype
);
472 chain
= TYPE_CHAIN (chain
);
478 /* Same as above, but caller doesn't care about memory allocation
482 lookup_reference_type (struct type
*type
, enum type_code refcode
)
484 return make_reference_type (type
, (struct type
**) 0, refcode
);
487 /* Lookup the lvalue reference type for the type TYPE. */
490 lookup_lvalue_reference_type (struct type
*type
)
492 return lookup_reference_type (type
, TYPE_CODE_REF
);
495 /* Lookup the rvalue reference type for the type TYPE. */
498 lookup_rvalue_reference_type (struct type
*type
)
500 return lookup_reference_type (type
, TYPE_CODE_RVALUE_REF
);
503 /* Lookup a function type that returns type TYPE. TYPEPTR, if
504 nonzero, points to a pointer to memory where the function type
505 should be stored. If *TYPEPTR is zero, update it to point to the
506 function type we return. We allocate new memory if needed. */
509 make_function_type (struct type
*type
, struct type
**typeptr
)
511 struct type
*ntype
; /* New type */
513 if (typeptr
== 0 || *typeptr
== 0) /* We'll need to allocate one. */
515 ntype
= alloc_type_copy (type
);
519 else /* We have storage, but need to reset it. */
525 TYPE_TARGET_TYPE (ntype
) = type
;
527 TYPE_LENGTH (ntype
) = 1;
528 ntype
->set_code (TYPE_CODE_FUNC
);
530 INIT_FUNC_SPECIFIC (ntype
);
535 /* Given a type TYPE, return a type of functions that return that type.
536 May need to construct such a type if this is the first use. */
539 lookup_function_type (struct type
*type
)
541 return make_function_type (type
, (struct type
**) 0);
544 /* Given a type TYPE and argument types, return the appropriate
545 function type. If the final type in PARAM_TYPES is NULL, make a
549 lookup_function_type_with_arguments (struct type
*type
,
551 struct type
**param_types
)
553 struct type
*fn
= make_function_type (type
, (struct type
**) 0);
558 if (param_types
[nparams
- 1] == NULL
)
561 fn
->set_has_varargs (true);
563 else if (check_typedef (param_types
[nparams
- 1])->code ()
567 /* Caller should have ensured this. */
568 gdb_assert (nparams
== 0);
569 fn
->set_is_prototyped (true);
572 fn
->set_is_prototyped (true);
575 fn
->set_num_fields (nparams
);
577 ((struct field
*) TYPE_ZALLOC (fn
, nparams
* sizeof (struct field
)));
578 for (i
= 0; i
< nparams
; ++i
)
579 fn
->field (i
).set_type (param_types
[i
]);
584 /* Identify address space identifier by name -- return a
585 type_instance_flags. */
588 address_space_name_to_type_instance_flags (struct gdbarch
*gdbarch
,
589 const char *space_identifier
)
591 type_instance_flags type_flags
;
593 /* Check for known address space delimiters. */
594 if (!strcmp (space_identifier
, "code"))
595 return TYPE_INSTANCE_FLAG_CODE_SPACE
;
596 else if (!strcmp (space_identifier
, "data"))
597 return TYPE_INSTANCE_FLAG_DATA_SPACE
;
598 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch
)
599 && gdbarch_address_class_name_to_type_flags (gdbarch
,
604 error (_("Unknown address space specifier: \"%s\""), space_identifier
);
607 /* Identify address space identifier by type_instance_flags and return
608 the string version of the adress space name. */
611 address_space_type_instance_flags_to_name (struct gdbarch
*gdbarch
,
612 type_instance_flags space_flag
)
614 if (space_flag
& TYPE_INSTANCE_FLAG_CODE_SPACE
)
616 else if (space_flag
& TYPE_INSTANCE_FLAG_DATA_SPACE
)
618 else if ((space_flag
& TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
619 && gdbarch_address_class_type_flags_to_name_p (gdbarch
))
620 return gdbarch_address_class_type_flags_to_name (gdbarch
, space_flag
);
625 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
627 If STORAGE is non-NULL, create the new type instance there.
628 STORAGE must be in the same obstack as TYPE. */
631 make_qualified_type (struct type
*type
, type_instance_flags new_flags
,
632 struct type
*storage
)
639 if (ntype
->instance_flags () == new_flags
)
641 ntype
= TYPE_CHAIN (ntype
);
643 while (ntype
!= type
);
645 /* Create a new type instance. */
647 ntype
= alloc_type_instance (type
);
650 /* If STORAGE was provided, it had better be in the same objfile
651 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
652 if one objfile is freed and the other kept, we'd have
653 dangling pointers. */
654 gdb_assert (type
->objfile_owner () == storage
->objfile_owner ());
657 TYPE_MAIN_TYPE (ntype
) = TYPE_MAIN_TYPE (type
);
658 TYPE_CHAIN (ntype
) = ntype
;
661 /* Pointers or references to the original type are not relevant to
663 TYPE_POINTER_TYPE (ntype
) = (struct type
*) 0;
664 TYPE_REFERENCE_TYPE (ntype
) = (struct type
*) 0;
666 /* Chain the new qualified type to the old type. */
667 TYPE_CHAIN (ntype
) = TYPE_CHAIN (type
);
668 TYPE_CHAIN (type
) = ntype
;
670 /* Now set the instance flags and return the new type. */
671 ntype
->set_instance_flags (new_flags
);
673 /* Set length of new type to that of the original type. */
674 TYPE_LENGTH (ntype
) = TYPE_LENGTH (type
);
679 /* Make an address-space-delimited variant of a type -- a type that
680 is identical to the one supplied except that it has an address
681 space attribute attached to it (such as "code" or "data").
683 The space attributes "code" and "data" are for Harvard
684 architectures. The address space attributes are for architectures
685 which have alternately sized pointers or pointers with alternate
689 make_type_with_address_space (struct type
*type
,
690 type_instance_flags space_flag
)
692 type_instance_flags new_flags
= ((type
->instance_flags ()
693 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
694 | TYPE_INSTANCE_FLAG_DATA_SPACE
695 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
))
698 return make_qualified_type (type
, new_flags
, NULL
);
701 /* Make a "c-v" variant of a type -- a type that is identical to the
702 one supplied except that it may have const or volatile attributes
703 CNST is a flag for setting the const attribute
704 VOLTL is a flag for setting the volatile attribute
705 TYPE is the base type whose variant we are creating.
707 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
708 storage to hold the new qualified type; *TYPEPTR and TYPE must be
709 in the same objfile. Otherwise, allocate fresh memory for the new
710 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
711 new type we construct. */
714 make_cv_type (int cnst
, int voltl
,
716 struct type
**typeptr
)
718 struct type
*ntype
; /* New type */
720 type_instance_flags new_flags
= (type
->instance_flags ()
721 & ~(TYPE_INSTANCE_FLAG_CONST
722 | TYPE_INSTANCE_FLAG_VOLATILE
));
725 new_flags
|= TYPE_INSTANCE_FLAG_CONST
;
728 new_flags
|= TYPE_INSTANCE_FLAG_VOLATILE
;
730 if (typeptr
&& *typeptr
!= NULL
)
732 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
733 a C-V variant chain that threads across objfiles: if one
734 objfile gets freed, then the other has a broken C-V chain.
736 This code used to try to copy over the main type from TYPE to
737 *TYPEPTR if they were in different objfiles, but that's
738 wrong, too: TYPE may have a field list or member function
739 lists, which refer to types of their own, etc. etc. The
740 whole shebang would need to be copied over recursively; you
741 can't have inter-objfile pointers. The only thing to do is
742 to leave stub types as stub types, and look them up afresh by
743 name each time you encounter them. */
744 gdb_assert ((*typeptr
)->objfile_owner () == type
->objfile_owner ());
747 ntype
= make_qualified_type (type
, new_flags
,
748 typeptr
? *typeptr
: NULL
);
756 /* Make a 'restrict'-qualified version of TYPE. */
759 make_restrict_type (struct type
*type
)
761 return make_qualified_type (type
,
762 (type
->instance_flags ()
763 | TYPE_INSTANCE_FLAG_RESTRICT
),
767 /* Make a type without const, volatile, or restrict. */
770 make_unqualified_type (struct type
*type
)
772 return make_qualified_type (type
,
773 (type
->instance_flags ()
774 & ~(TYPE_INSTANCE_FLAG_CONST
775 | TYPE_INSTANCE_FLAG_VOLATILE
776 | TYPE_INSTANCE_FLAG_RESTRICT
)),
780 /* Make a '_Atomic'-qualified version of TYPE. */
783 make_atomic_type (struct type
*type
)
785 return make_qualified_type (type
,
786 (type
->instance_flags ()
787 | TYPE_INSTANCE_FLAG_ATOMIC
),
791 /* Replace the contents of ntype with the type *type. This changes the
792 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
793 the changes are propogated to all types in the TYPE_CHAIN.
795 In order to build recursive types, it's inevitable that we'll need
796 to update types in place --- but this sort of indiscriminate
797 smashing is ugly, and needs to be replaced with something more
798 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
799 clear if more steps are needed. */
802 replace_type (struct type
*ntype
, struct type
*type
)
806 /* These two types had better be in the same objfile. Otherwise,
807 the assignment of one type's main type structure to the other
808 will produce a type with references to objects (names; field
809 lists; etc.) allocated on an objfile other than its own. */
810 gdb_assert (ntype
->objfile_owner () == type
->objfile_owner ());
812 *TYPE_MAIN_TYPE (ntype
) = *TYPE_MAIN_TYPE (type
);
814 /* The type length is not a part of the main type. Update it for
815 each type on the variant chain. */
819 /* Assert that this element of the chain has no address-class bits
820 set in its flags. Such type variants might have type lengths
821 which are supposed to be different from the non-address-class
822 variants. This assertion shouldn't ever be triggered because
823 symbol readers which do construct address-class variants don't
824 call replace_type(). */
825 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain
) == 0);
827 TYPE_LENGTH (chain
) = TYPE_LENGTH (type
);
828 chain
= TYPE_CHAIN (chain
);
830 while (ntype
!= chain
);
832 /* Assert that the two types have equivalent instance qualifiers.
833 This should be true for at least all of our debug readers. */
834 gdb_assert (ntype
->instance_flags () == type
->instance_flags ());
837 /* Implement direct support for MEMBER_TYPE in GNU C++.
838 May need to construct such a type if this is the first use.
839 The TYPE is the type of the member. The DOMAIN is the type
840 of the aggregate that the member belongs to. */
843 lookup_memberptr_type (struct type
*type
, struct type
*domain
)
847 mtype
= alloc_type_copy (type
);
848 smash_to_memberptr_type (mtype
, domain
, type
);
852 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
855 lookup_methodptr_type (struct type
*to_type
)
859 mtype
= alloc_type_copy (to_type
);
860 smash_to_methodptr_type (mtype
, to_type
);
864 /* Allocate a stub method whose return type is TYPE. This apparently
865 happens for speed of symbol reading, since parsing out the
866 arguments to the method is cpu-intensive, the way we are doing it.
867 So, we will fill in arguments later. This always returns a fresh
871 allocate_stub_method (struct type
*type
)
875 mtype
= alloc_type_copy (type
);
876 mtype
->set_code (TYPE_CODE_METHOD
);
877 TYPE_LENGTH (mtype
) = 1;
878 mtype
->set_is_stub (true);
879 TYPE_TARGET_TYPE (mtype
) = type
;
880 /* TYPE_SELF_TYPE (mtype) = unknown yet */
884 /* See gdbtypes.h. */
887 operator== (const dynamic_prop
&l
, const dynamic_prop
&r
)
889 if (l
.kind () != r
.kind ())
897 return l
.const_val () == r
.const_val ();
898 case PROP_ADDR_OFFSET
:
901 return l
.baton () == r
.baton ();
902 case PROP_VARIANT_PARTS
:
903 return l
.variant_parts () == r
.variant_parts ();
905 return l
.original_type () == r
.original_type ();
908 gdb_assert_not_reached ("unhandled dynamic_prop kind");
911 /* See gdbtypes.h. */
914 operator== (const range_bounds
&l
, const range_bounds
&r
)
916 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
918 return (FIELD_EQ (low
)
920 && FIELD_EQ (flag_upper_bound_is_count
)
921 && FIELD_EQ (flag_bound_evaluated
)
927 /* Create a range type with a dynamic range from LOW_BOUND to
928 HIGH_BOUND, inclusive. See create_range_type for further details. */
931 create_range_type (struct type
*result_type
, struct type
*index_type
,
932 const struct dynamic_prop
*low_bound
,
933 const struct dynamic_prop
*high_bound
,
936 /* The INDEX_TYPE should be a type capable of holding the upper and lower
937 bounds, as such a zero sized, or void type makes no sense. */
938 gdb_assert (index_type
->code () != TYPE_CODE_VOID
);
939 gdb_assert (TYPE_LENGTH (index_type
) > 0);
941 if (result_type
== NULL
)
942 result_type
= alloc_type_copy (index_type
);
943 result_type
->set_code (TYPE_CODE_RANGE
);
944 TYPE_TARGET_TYPE (result_type
) = index_type
;
945 if (index_type
->is_stub ())
946 result_type
->set_target_is_stub (true);
948 TYPE_LENGTH (result_type
) = TYPE_LENGTH (check_typedef (index_type
));
951 = (struct range_bounds
*) TYPE_ZALLOC (result_type
, sizeof (range_bounds
));
952 bounds
->low
= *low_bound
;
953 bounds
->high
= *high_bound
;
955 bounds
->stride
.set_const_val (0);
957 result_type
->set_bounds (bounds
);
959 if (index_type
->code () == TYPE_CODE_FIXED_POINT
)
960 result_type
->set_is_unsigned (index_type
->is_unsigned ());
961 /* Note that the signed-ness of a range type can't simply be copied
962 from the underlying type. Consider a case where the underlying
963 type is 'int', but the range type can hold 0..65535, and where
964 the range is further specified to fit into 16 bits. In this
965 case, if we copy the underlying type's sign, then reading some
966 range values will cause an unwanted sign extension. So, we have
967 some heuristics here instead. */
968 else if (low_bound
->kind () == PROP_CONST
&& low_bound
->const_val () >= 0)
969 result_type
->set_is_unsigned (true);
970 /* Ada allows the declaration of range types whose upper bound is
971 less than the lower bound, so checking the lower bound is not
972 enough. Make sure we do not mark a range type whose upper bound
973 is negative as unsigned. */
974 if (high_bound
->kind () == PROP_CONST
&& high_bound
->const_val () < 0)
975 result_type
->set_is_unsigned (false);
977 result_type
->set_endianity_is_not_default
978 (index_type
->endianity_is_not_default ());
983 /* See gdbtypes.h. */
986 create_range_type_with_stride (struct type
*result_type
,
987 struct type
*index_type
,
988 const struct dynamic_prop
*low_bound
,
989 const struct dynamic_prop
*high_bound
,
991 const struct dynamic_prop
*stride
,
994 result_type
= create_range_type (result_type
, index_type
, low_bound
,
997 gdb_assert (stride
!= nullptr);
998 result_type
->bounds ()->stride
= *stride
;
999 result_type
->bounds ()->flag_is_byte_stride
= byte_stride_p
;
1006 /* Create a range type using either a blank type supplied in
1007 RESULT_TYPE, or creating a new type, inheriting the objfile from
1010 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
1011 to HIGH_BOUND, inclusive.
1013 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1014 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
1017 create_static_range_type (struct type
*result_type
, struct type
*index_type
,
1018 LONGEST low_bound
, LONGEST high_bound
)
1020 struct dynamic_prop low
, high
;
1022 low
.set_const_val (low_bound
);
1023 high
.set_const_val (high_bound
);
1025 result_type
= create_range_type (result_type
, index_type
, &low
, &high
, 0);
1030 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1031 are static, otherwise returns 0. */
1034 has_static_range (const struct range_bounds
*bounds
)
1036 /* If the range doesn't have a defined stride then its stride field will
1037 be initialized to the constant 0. */
1038 return (bounds
->low
.kind () == PROP_CONST
1039 && bounds
->high
.kind () == PROP_CONST
1040 && bounds
->stride
.kind () == PROP_CONST
);
1043 /* See gdbtypes.h. */
1045 gdb::optional
<LONGEST
>
1046 get_discrete_low_bound (struct type
*type
)
1048 type
= check_typedef (type
);
1049 switch (type
->code ())
1051 case TYPE_CODE_RANGE
:
1053 /* This function only works for ranges with a constant low bound. */
1054 if (type
->bounds ()->low
.kind () != PROP_CONST
)
1057 LONGEST low
= type
->bounds ()->low
.const_val ();
1059 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ENUM
)
1061 gdb::optional
<LONGEST
> low_pos
1062 = discrete_position (TYPE_TARGET_TYPE (type
), low
);
1064 if (low_pos
.has_value ())
1071 case TYPE_CODE_ENUM
:
1073 if (type
->num_fields () > 0)
1075 /* The enums may not be sorted by value, so search all
1077 LONGEST low
= type
->field (0).loc_enumval ();
1079 for (int i
= 0; i
< type
->num_fields (); i
++)
1081 if (type
->field (i
).loc_enumval () < low
)
1082 low
= type
->field (i
).loc_enumval ();
1085 /* Set unsigned indicator if warranted. */
1087 type
->set_is_unsigned (true);
1095 case TYPE_CODE_BOOL
:
1099 if (TYPE_LENGTH (type
) > sizeof (LONGEST
)) /* Too big */
1102 if (!type
->is_unsigned ())
1103 return -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
1106 case TYPE_CODE_CHAR
:
1114 /* See gdbtypes.h. */
1116 gdb::optional
<LONGEST
>
1117 get_discrete_high_bound (struct type
*type
)
1119 type
= check_typedef (type
);
1120 switch (type
->code ())
1122 case TYPE_CODE_RANGE
:
1124 /* This function only works for ranges with a constant high bound. */
1125 if (type
->bounds ()->high
.kind () != PROP_CONST
)
1128 LONGEST high
= type
->bounds ()->high
.const_val ();
1130 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ENUM
)
1132 gdb::optional
<LONGEST
> high_pos
1133 = discrete_position (TYPE_TARGET_TYPE (type
), high
);
1135 if (high_pos
.has_value ())
1142 case TYPE_CODE_ENUM
:
1144 if (type
->num_fields () > 0)
1146 /* The enums may not be sorted by value, so search all
1148 LONGEST high
= type
->field (0).loc_enumval ();
1150 for (int i
= 0; i
< type
->num_fields (); i
++)
1152 if (type
->field (i
).loc_enumval () > high
)
1153 high
= type
->field (i
).loc_enumval ();
1162 case TYPE_CODE_BOOL
:
1166 if (TYPE_LENGTH (type
) > sizeof (LONGEST
)) /* Too big */
1169 if (!type
->is_unsigned ())
1171 LONGEST low
= -(1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1));
1176 case TYPE_CODE_CHAR
:
1178 /* This round-about calculation is to avoid shifting by
1179 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
1180 if TYPE_LENGTH (type) == sizeof (LONGEST). */
1181 LONGEST high
= 1 << (TYPE_LENGTH (type
) * TARGET_CHAR_BIT
- 1);
1182 return (high
- 1) | high
;
1190 /* See gdbtypes.h. */
1193 get_discrete_bounds (struct type
*type
, LONGEST
*lowp
, LONGEST
*highp
)
1195 gdb::optional
<LONGEST
> low
= get_discrete_low_bound (type
);
1196 if (!low
.has_value ())
1199 gdb::optional
<LONGEST
> high
= get_discrete_high_bound (type
);
1200 if (!high
.has_value ())
1209 /* See gdbtypes.h */
1212 get_array_bounds (struct type
*type
, LONGEST
*low_bound
, LONGEST
*high_bound
)
1214 struct type
*index
= type
->index_type ();
1221 if (!get_discrete_bounds (index
, &low
, &high
))
1233 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1234 representation of a value of this type, save the corresponding
1235 position number in POS.
1237 Its differs from VAL only in the case of enumeration types. In
1238 this case, the position number of the value of the first listed
1239 enumeration literal is zero; the position number of the value of
1240 each subsequent enumeration literal is one more than that of its
1241 predecessor in the list.
1243 Return 1 if the operation was successful. Return zero otherwise,
1244 in which case the value of POS is unmodified.
1247 gdb::optional
<LONGEST
>
1248 discrete_position (struct type
*type
, LONGEST val
)
1250 if (type
->code () == TYPE_CODE_RANGE
)
1251 type
= TYPE_TARGET_TYPE (type
);
1253 if (type
->code () == TYPE_CODE_ENUM
)
1257 for (i
= 0; i
< type
->num_fields (); i
+= 1)
1259 if (val
== type
->field (i
).loc_enumval ())
1263 /* Invalid enumeration value. */
1270 /* If the array TYPE has static bounds calculate and update its
1271 size, then return true. Otherwise return false and leave TYPE
1275 update_static_array_size (struct type
*type
)
1277 gdb_assert (type
->code () == TYPE_CODE_ARRAY
);
1279 struct type
*range_type
= type
->index_type ();
1281 if (type
->dyn_prop (DYN_PROP_BYTE_STRIDE
) == nullptr
1282 && has_static_range (range_type
->bounds ())
1283 && (!type_not_associated (type
)
1284 && !type_not_allocated (type
)))
1286 LONGEST low_bound
, high_bound
;
1288 struct type
*element_type
;
1290 stride
= type
->bit_stride ();
1292 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
1293 low_bound
= high_bound
= 0;
1295 element_type
= check_typedef (TYPE_TARGET_TYPE (type
));
1296 /* Be careful when setting the array length. Ada arrays can be
1297 empty arrays with the high_bound being smaller than the low_bound.
1298 In such cases, the array length should be zero. */
1299 if (high_bound
< low_bound
)
1300 TYPE_LENGTH (type
) = 0;
1301 else if (stride
!= 0)
1303 /* Ensure that the type length is always positive, even in the
1304 case where (for example in Fortran) we have a negative
1305 stride. It is possible to have a single element array with a
1306 negative stride in Fortran (this doesn't mean anything
1307 special, it's still just a single element array) so do
1308 consider that case when touching this code. */
1309 LONGEST element_count
= std::abs (high_bound
- low_bound
+ 1);
1311 = ((std::abs (stride
) * element_count
) + 7) / 8;
1314 TYPE_LENGTH (type
) =
1315 TYPE_LENGTH (element_type
) * (high_bound
- low_bound
+ 1);
1317 /* If this array's element is itself an array with a bit stride,
1318 then we want to update this array's bit stride to reflect the
1319 size of the sub-array. Otherwise, we'll end up using the
1320 wrong size when trying to find elements of the outer
1322 if (element_type
->code () == TYPE_CODE_ARRAY
1323 && TYPE_LENGTH (element_type
) != 0
1324 && TYPE_FIELD_BITSIZE (element_type
, 0) != 0
1325 && get_array_bounds (element_type
, &low_bound
, &high_bound
)
1326 && high_bound
>= low_bound
)
1327 TYPE_FIELD_BITSIZE (type
, 0)
1328 = ((high_bound
- low_bound
+ 1)
1329 * TYPE_FIELD_BITSIZE (element_type
, 0));
1337 /* Create an array type using either a blank type supplied in
1338 RESULT_TYPE, or creating a new type, inheriting the objfile from
1341 Elements will be of type ELEMENT_TYPE, the indices will be of type
1344 BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
1345 This byte stride property is added to the resulting array type
1346 as a DYN_PROP_BYTE_STRIDE. As a consequence, the BYTE_STRIDE_PROP
1347 argument can only be used to create types that are objfile-owned
1348 (see add_dyn_prop), meaning that either this function must be called
1349 with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
1351 BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
1352 If BIT_STRIDE is not zero, build a packed array type whose element
1353 size is BIT_STRIDE. Otherwise, ignore this parameter.
1355 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1356 sure it is TYPE_CODE_UNDEF before we bash it into an array
1360 create_array_type_with_stride (struct type
*result_type
,
1361 struct type
*element_type
,
1362 struct type
*range_type
,
1363 struct dynamic_prop
*byte_stride_prop
,
1364 unsigned int bit_stride
)
1366 if (byte_stride_prop
!= NULL
1367 && byte_stride_prop
->kind () == PROP_CONST
)
1369 /* The byte stride is actually not dynamic. Pretend we were
1370 called with bit_stride set instead of byte_stride_prop.
1371 This will give us the same result type, while avoiding
1372 the need to handle this as a special case. */
1373 bit_stride
= byte_stride_prop
->const_val () * 8;
1374 byte_stride_prop
= NULL
;
1377 if (result_type
== NULL
)
1378 result_type
= alloc_type_copy (range_type
);
1380 result_type
->set_code (TYPE_CODE_ARRAY
);
1381 TYPE_TARGET_TYPE (result_type
) = element_type
;
1383 result_type
->set_num_fields (1);
1384 result_type
->set_fields
1385 ((struct field
*) TYPE_ZALLOC (result_type
, sizeof (struct field
)));
1386 result_type
->set_index_type (range_type
);
1387 if (byte_stride_prop
!= NULL
)
1388 result_type
->add_dyn_prop (DYN_PROP_BYTE_STRIDE
, *byte_stride_prop
);
1389 else if (bit_stride
> 0)
1390 TYPE_FIELD_BITSIZE (result_type
, 0) = bit_stride
;
1392 if (!update_static_array_size (result_type
))
1394 /* This type is dynamic and its length needs to be computed
1395 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1396 undefined by setting it to zero. Although we are not expected
1397 to trust TYPE_LENGTH in this case, setting the size to zero
1398 allows us to avoid allocating objects of random sizes in case
1399 we accidently do. */
1400 TYPE_LENGTH (result_type
) = 0;
1403 /* TYPE_TARGET_STUB will take care of zero length arrays. */
1404 if (TYPE_LENGTH (result_type
) == 0)
1405 result_type
->set_target_is_stub (true);
1410 /* Same as create_array_type_with_stride but with no bit_stride
1411 (BIT_STRIDE = 0), thus building an unpacked array. */
1414 create_array_type (struct type
*result_type
,
1415 struct type
*element_type
,
1416 struct type
*range_type
)
1418 return create_array_type_with_stride (result_type
, element_type
,
1419 range_type
, NULL
, 0);
1423 lookup_array_range_type (struct type
*element_type
,
1424 LONGEST low_bound
, LONGEST high_bound
)
1426 struct type
*index_type
;
1427 struct type
*range_type
;
1429 if (element_type
->is_objfile_owned ())
1430 index_type
= objfile_type (element_type
->objfile_owner ())->builtin_int
;
1432 index_type
= builtin_type (element_type
->arch_owner ())->builtin_int
;
1434 range_type
= create_static_range_type (NULL
, index_type
,
1435 low_bound
, high_bound
);
1437 return create_array_type (NULL
, element_type
, range_type
);
1440 /* Create a string type using either a blank type supplied in
1441 RESULT_TYPE, or creating a new type. String types are similar
1442 enough to array of char types that we can use create_array_type to
1443 build the basic type and then bash it into a string type.
1445 For fixed length strings, the range type contains 0 as the lower
1446 bound and the length of the string minus one as the upper bound.
1448 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1449 sure it is TYPE_CODE_UNDEF before we bash it into a string
1453 create_string_type (struct type
*result_type
,
1454 struct type
*string_char_type
,
1455 struct type
*range_type
)
1457 result_type
= create_array_type (result_type
,
1460 result_type
->set_code (TYPE_CODE_STRING
);
1465 lookup_string_range_type (struct type
*string_char_type
,
1466 LONGEST low_bound
, LONGEST high_bound
)
1468 struct type
*result_type
;
1470 result_type
= lookup_array_range_type (string_char_type
,
1471 low_bound
, high_bound
);
1472 result_type
->set_code (TYPE_CODE_STRING
);
1477 create_set_type (struct type
*result_type
, struct type
*domain_type
)
1479 if (result_type
== NULL
)
1480 result_type
= alloc_type_copy (domain_type
);
1482 result_type
->set_code (TYPE_CODE_SET
);
1483 result_type
->set_num_fields (1);
1484 result_type
->set_fields
1485 ((struct field
*) TYPE_ZALLOC (result_type
, sizeof (struct field
)));
1487 if (!domain_type
->is_stub ())
1489 LONGEST low_bound
, high_bound
, bit_length
;
1491 if (!get_discrete_bounds (domain_type
, &low_bound
, &high_bound
))
1492 low_bound
= high_bound
= 0;
1494 bit_length
= high_bound
- low_bound
+ 1;
1495 TYPE_LENGTH (result_type
)
1496 = (bit_length
+ TARGET_CHAR_BIT
- 1) / TARGET_CHAR_BIT
;
1498 result_type
->set_is_unsigned (true);
1500 result_type
->field (0).set_type (domain_type
);
1505 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1506 and any array types nested inside it. */
1509 make_vector_type (struct type
*array_type
)
1511 struct type
*inner_array
, *elt_type
;
1513 /* Find the innermost array type, in case the array is
1514 multi-dimensional. */
1515 inner_array
= array_type
;
1516 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
1517 inner_array
= TYPE_TARGET_TYPE (inner_array
);
1519 elt_type
= TYPE_TARGET_TYPE (inner_array
);
1520 if (elt_type
->code () == TYPE_CODE_INT
)
1522 type_instance_flags flags
1523 = elt_type
->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT
;
1524 elt_type
= make_qualified_type (elt_type
, flags
, NULL
);
1525 TYPE_TARGET_TYPE (inner_array
) = elt_type
;
1528 array_type
->set_is_vector (true);
1532 init_vector_type (struct type
*elt_type
, int n
)
1534 struct type
*array_type
;
1536 array_type
= lookup_array_range_type (elt_type
, 0, n
- 1);
1537 make_vector_type (array_type
);
1541 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1542 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1543 confusing. "self" is a common enough replacement for "this".
1544 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1545 TYPE_CODE_METHOD. */
1548 internal_type_self_type (struct type
*type
)
1550 switch (type
->code ())
1552 case TYPE_CODE_METHODPTR
:
1553 case TYPE_CODE_MEMBERPTR
:
1554 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1556 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_SELF_TYPE
);
1557 return TYPE_MAIN_TYPE (type
)->type_specific
.self_type
;
1558 case TYPE_CODE_METHOD
:
1559 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1561 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
1562 return TYPE_MAIN_TYPE (type
)->type_specific
.func_stuff
->self_type
;
1564 gdb_assert_not_reached ("bad type");
1568 /* Set the type of the class that TYPE belongs to.
1569 In c++ this is the class of "this".
1570 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1571 TYPE_CODE_METHOD. */
1574 set_type_self_type (struct type
*type
, struct type
*self_type
)
1576 switch (type
->code ())
1578 case TYPE_CODE_METHODPTR
:
1579 case TYPE_CODE_MEMBERPTR
:
1580 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1581 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_SELF_TYPE
;
1582 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_SELF_TYPE
);
1583 TYPE_MAIN_TYPE (type
)->type_specific
.self_type
= self_type
;
1585 case TYPE_CODE_METHOD
:
1586 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_NONE
)
1587 INIT_FUNC_SPECIFIC (type
);
1588 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
1589 TYPE_MAIN_TYPE (type
)->type_specific
.func_stuff
->self_type
= self_type
;
1592 gdb_assert_not_reached ("bad type");
1596 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1597 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1598 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1599 TYPE doesn't include the offset (that's the value of the MEMBER
1600 itself), but does include the structure type into which it points
1603 When "smashing" the type, we preserve the objfile that the old type
1604 pointed to, since we aren't changing where the type is actually
1608 smash_to_memberptr_type (struct type
*type
, struct type
*self_type
,
1609 struct type
*to_type
)
1612 type
->set_code (TYPE_CODE_MEMBERPTR
);
1613 TYPE_TARGET_TYPE (type
) = to_type
;
1614 set_type_self_type (type
, self_type
);
1615 /* Assume that a data member pointer is the same size as a normal
1617 TYPE_LENGTH (type
) = gdbarch_ptr_bit (to_type
->arch ()) / TARGET_CHAR_BIT
;
1620 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1622 When "smashing" the type, we preserve the objfile that the old type
1623 pointed to, since we aren't changing where the type is actually
1627 smash_to_methodptr_type (struct type
*type
, struct type
*to_type
)
1630 type
->set_code (TYPE_CODE_METHODPTR
);
1631 TYPE_TARGET_TYPE (type
) = to_type
;
1632 set_type_self_type (type
, TYPE_SELF_TYPE (to_type
));
1633 TYPE_LENGTH (type
) = cplus_method_ptr_size (to_type
);
1636 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1637 METHOD just means `function that gets an extra "this" argument'.
1639 When "smashing" the type, we preserve the objfile that the old type
1640 pointed to, since we aren't changing where the type is actually
1644 smash_to_method_type (struct type
*type
, struct type
*self_type
,
1645 struct type
*to_type
, struct field
*args
,
1646 int nargs
, int varargs
)
1649 type
->set_code (TYPE_CODE_METHOD
);
1650 TYPE_TARGET_TYPE (type
) = to_type
;
1651 set_type_self_type (type
, self_type
);
1652 type
->set_fields (args
);
1653 type
->set_num_fields (nargs
);
1655 type
->set_has_varargs (true);
1656 TYPE_LENGTH (type
) = 1; /* In practice, this is never needed. */
1659 /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
1660 Since GCC PR debug/47510 DWARF provides associated information to detect the
1661 anonymous class linkage name from its typedef.
1663 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1667 type_name_or_error (struct type
*type
)
1669 struct type
*saved_type
= type
;
1671 struct objfile
*objfile
;
1673 type
= check_typedef (type
);
1675 name
= type
->name ();
1679 name
= saved_type
->name ();
1680 objfile
= saved_type
->objfile_owner ();
1681 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1682 name
? name
: "<anonymous>",
1683 objfile
? objfile_name (objfile
) : "<arch>");
1686 /* Lookup a typedef or primitive type named NAME, visible in lexical
1687 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1688 suitably defined. */
1691 lookup_typename (const struct language_defn
*language
,
1693 const struct block
*block
, int noerr
)
1697 sym
= lookup_symbol_in_language (name
, block
, VAR_DOMAIN
,
1698 language
->la_language
, NULL
).symbol
;
1699 if (sym
!= NULL
&& sym
->aclass () == LOC_TYPEDEF
)
1700 return sym
->type ();
1704 error (_("No type named %s."), name
);
1708 lookup_unsigned_typename (const struct language_defn
*language
,
1711 char *uns
= (char *) alloca (strlen (name
) + 10);
1713 strcpy (uns
, "unsigned ");
1714 strcpy (uns
+ 9, name
);
1715 return lookup_typename (language
, uns
, NULL
, 0);
1719 lookup_signed_typename (const struct language_defn
*language
, const char *name
)
1722 char *uns
= (char *) alloca (strlen (name
) + 8);
1724 strcpy (uns
, "signed ");
1725 strcpy (uns
+ 7, name
);
1726 t
= lookup_typename (language
, uns
, NULL
, 1);
1727 /* If we don't find "signed FOO" just try again with plain "FOO". */
1730 return lookup_typename (language
, name
, NULL
, 0);
1733 /* Lookup a structure type named "struct NAME",
1734 visible in lexical block BLOCK. */
1737 lookup_struct (const char *name
, const struct block
*block
)
1741 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1745 error (_("No struct type named %s."), name
);
1747 if (sym
->type ()->code () != TYPE_CODE_STRUCT
)
1749 error (_("This context has class, union or enum %s, not a struct."),
1752 return (sym
->type ());
1755 /* Lookup a union type named "union NAME",
1756 visible in lexical block BLOCK. */
1759 lookup_union (const char *name
, const struct block
*block
)
1764 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1767 error (_("No union type named %s."), name
);
1771 if (t
->code () == TYPE_CODE_UNION
)
1774 /* If we get here, it's not a union. */
1775 error (_("This context has class, struct or enum %s, not a union."),
1779 /* Lookup an enum type named "enum NAME",
1780 visible in lexical block BLOCK. */
1783 lookup_enum (const char *name
, const struct block
*block
)
1787 sym
= lookup_symbol (name
, block
, STRUCT_DOMAIN
, 0).symbol
;
1790 error (_("No enum type named %s."), name
);
1792 if (sym
->type ()->code () != TYPE_CODE_ENUM
)
1794 error (_("This context has class, struct or union %s, not an enum."),
1797 return (sym
->type ());
1800 /* Lookup a template type named "template NAME<TYPE>",
1801 visible in lexical block BLOCK. */
1804 lookup_template_type (const char *name
, struct type
*type
,
1805 const struct block
*block
)
1808 char *nam
= (char *)
1809 alloca (strlen (name
) + strlen (type
->name ()) + 4);
1813 strcat (nam
, type
->name ());
1814 strcat (nam
, " >"); /* FIXME, extra space still introduced in gcc? */
1816 sym
= lookup_symbol (nam
, block
, VAR_DOMAIN
, 0).symbol
;
1820 error (_("No template type named %s."), name
);
1822 if (sym
->type ()->code () != TYPE_CODE_STRUCT
)
1824 error (_("This context has class, union or enum %s, not a struct."),
1827 return (sym
->type ());
1830 /* See gdbtypes.h. */
1833 lookup_struct_elt (struct type
*type
, const char *name
, int noerr
)
1839 type
= check_typedef (type
);
1840 if (type
->code () != TYPE_CODE_PTR
1841 && type
->code () != TYPE_CODE_REF
)
1843 type
= TYPE_TARGET_TYPE (type
);
1846 if (type
->code () != TYPE_CODE_STRUCT
1847 && type
->code () != TYPE_CODE_UNION
)
1849 std::string type_name
= type_to_string (type
);
1850 error (_("Type %s is not a structure or union type."),
1851 type_name
.c_str ());
1854 for (i
= type
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1856 const char *t_field_name
= type
->field (i
).name ();
1858 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1860 return {&type
->field (i
), type
->field (i
).loc_bitpos ()};
1862 else if (!t_field_name
|| *t_field_name
== '\0')
1865 = lookup_struct_elt (type
->field (i
).type (), name
, 1);
1866 if (elt
.field
!= NULL
)
1868 elt
.offset
+= type
->field (i
).loc_bitpos ();
1874 /* OK, it's not in this class. Recursively check the baseclasses. */
1875 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1877 struct_elt elt
= lookup_struct_elt (TYPE_BASECLASS (type
, i
), name
, 1);
1878 if (elt
.field
!= NULL
)
1883 return {nullptr, 0};
1885 std::string type_name
= type_to_string (type
);
1886 error (_("Type %s has no component named %s."), type_name
.c_str (), name
);
1889 /* See gdbtypes.h. */
1892 lookup_struct_elt_type (struct type
*type
, const char *name
, int noerr
)
1894 struct_elt elt
= lookup_struct_elt (type
, name
, noerr
);
1895 if (elt
.field
!= NULL
)
1896 return elt
.field
->type ();
1901 /* Return the largest number representable by unsigned integer type TYPE. */
1904 get_unsigned_type_max (struct type
*type
)
1908 type
= check_typedef (type
);
1909 gdb_assert (type
->code () == TYPE_CODE_INT
&& type
->is_unsigned ());
1910 gdb_assert (TYPE_LENGTH (type
) <= sizeof (ULONGEST
));
1912 /* Written this way to avoid overflow. */
1913 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1914 return ((((ULONGEST
) 1 << (n
- 1)) - 1) << 1) | 1;
1917 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1918 signed integer type TYPE. */
1921 get_signed_type_minmax (struct type
*type
, LONGEST
*min
, LONGEST
*max
)
1925 type
= check_typedef (type
);
1926 gdb_assert (type
->code () == TYPE_CODE_INT
&& !type
->is_unsigned ());
1927 gdb_assert (TYPE_LENGTH (type
) <= sizeof (LONGEST
));
1929 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1930 *min
= -((ULONGEST
) 1 << (n
- 1));
1931 *max
= ((ULONGEST
) 1 << (n
- 1)) - 1;
1934 /* Return the largest value representable by pointer type TYPE. */
1937 get_pointer_type_max (struct type
*type
)
1941 type
= check_typedef (type
);
1942 gdb_assert (type
->code () == TYPE_CODE_PTR
);
1943 gdb_assert (TYPE_LENGTH (type
) <= sizeof (CORE_ADDR
));
1945 n
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
1946 return ((((CORE_ADDR
) 1 << (n
- 1)) - 1) << 1) | 1;
1949 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1950 cplus_stuff.vptr_fieldno.
1952 cplus_stuff is initialized to cplus_struct_default which does not
1953 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1954 designated initializers). We cope with that here. */
1957 internal_type_vptr_fieldno (struct type
*type
)
1959 type
= check_typedef (type
);
1960 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1961 || type
->code () == TYPE_CODE_UNION
);
1962 if (!HAVE_CPLUS_STRUCT (type
))
1964 return TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_fieldno
;
1967 /* Set the value of cplus_stuff.vptr_fieldno. */
1970 set_type_vptr_fieldno (struct type
*type
, int fieldno
)
1972 type
= check_typedef (type
);
1973 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1974 || type
->code () == TYPE_CODE_UNION
);
1975 if (!HAVE_CPLUS_STRUCT (type
))
1976 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
1977 TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_fieldno
= fieldno
;
1980 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1981 cplus_stuff.vptr_basetype. */
1984 internal_type_vptr_basetype (struct type
*type
)
1986 type
= check_typedef (type
);
1987 gdb_assert (type
->code () == TYPE_CODE_STRUCT
1988 || type
->code () == TYPE_CODE_UNION
);
1989 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_CPLUS_STUFF
);
1990 return TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_basetype
;
1993 /* Set the value of cplus_stuff.vptr_basetype. */
1996 set_type_vptr_basetype (struct type
*type
, struct type
*basetype
)
1998 type
= check_typedef (type
);
1999 gdb_assert (type
->code () == TYPE_CODE_STRUCT
2000 || type
->code () == TYPE_CODE_UNION
);
2001 if (!HAVE_CPLUS_STRUCT (type
))
2002 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
2003 TYPE_RAW_CPLUS_SPECIFIC (type
)->vptr_basetype
= basetype
;
2006 /* Lookup the vptr basetype/fieldno values for TYPE.
2007 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
2008 vptr_fieldno. Also, if found and basetype is from the same objfile,
2010 If not found, return -1 and ignore BASETYPEP.
2011 Callers should be aware that in some cases (for example,
2012 the type or one of its baseclasses is a stub type and we are
2013 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
2014 this function will not be able to find the
2015 virtual function table pointer, and vptr_fieldno will remain -1 and
2016 vptr_basetype will remain NULL or incomplete. */
2019 get_vptr_fieldno (struct type
*type
, struct type
**basetypep
)
2021 type
= check_typedef (type
);
2023 if (TYPE_VPTR_FIELDNO (type
) < 0)
2027 /* We must start at zero in case the first (and only) baseclass
2028 is virtual (and hence we cannot share the table pointer). */
2029 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
2031 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
2033 struct type
*basetype
;
2035 fieldno
= get_vptr_fieldno (baseclass
, &basetype
);
2038 /* If the type comes from a different objfile we can't cache
2039 it, it may have a different lifetime. PR 2384 */
2040 if (type
->objfile_owner () == basetype
->objfile_owner ())
2042 set_type_vptr_fieldno (type
, fieldno
);
2043 set_type_vptr_basetype (type
, basetype
);
2046 *basetypep
= basetype
;
2057 *basetypep
= TYPE_VPTR_BASETYPE (type
);
2058 return TYPE_VPTR_FIELDNO (type
);
2063 stub_noname_complaint (void)
2065 complaint (_("stub type has NULL name"));
2068 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2069 attached to it, and that property has a non-constant value. */
2072 array_type_has_dynamic_stride (struct type
*type
)
2074 struct dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2076 return (prop
!= NULL
&& prop
->kind () != PROP_CONST
);
2079 /* Worker for is_dynamic_type. */
2082 is_dynamic_type_internal (struct type
*type
, int top_level
)
2084 type
= check_typedef (type
);
2086 /* We only want to recognize references at the outermost level. */
2087 if (top_level
&& type
->code () == TYPE_CODE_REF
)
2088 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2090 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2091 dynamic, even if the type itself is statically defined.
2092 From a user's point of view, this may appear counter-intuitive;
2093 but it makes sense in this context, because the point is to determine
2094 whether any part of the type needs to be resolved before it can
2096 if (TYPE_DATA_LOCATION (type
) != NULL
2097 && (TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCEXPR
2098 || TYPE_DATA_LOCATION_KIND (type
) == PROP_LOCLIST
))
2101 if (TYPE_ASSOCIATED_PROP (type
))
2104 if (TYPE_ALLOCATED_PROP (type
))
2107 struct dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2108 if (prop
!= nullptr && prop
->kind () != PROP_TYPE
)
2111 if (TYPE_HAS_DYNAMIC_LENGTH (type
))
2114 switch (type
->code ())
2116 case TYPE_CODE_RANGE
:
2118 /* A range type is obviously dynamic if it has at least one
2119 dynamic bound. But also consider the range type to be
2120 dynamic when its subtype is dynamic, even if the bounds
2121 of the range type are static. It allows us to assume that
2122 the subtype of a static range type is also static. */
2123 return (!has_static_range (type
->bounds ())
2124 || is_dynamic_type_internal (TYPE_TARGET_TYPE (type
), 0));
2127 case TYPE_CODE_STRING
:
2128 /* Strings are very much like an array of characters, and can be
2129 treated as one here. */
2130 case TYPE_CODE_ARRAY
:
2132 gdb_assert (type
->num_fields () == 1);
2134 /* The array is dynamic if either the bounds are dynamic... */
2135 if (is_dynamic_type_internal (type
->index_type (), 0))
2137 /* ... or the elements it contains have a dynamic contents... */
2138 if (is_dynamic_type_internal (TYPE_TARGET_TYPE (type
), 0))
2140 /* ... or if it has a dynamic stride... */
2141 if (array_type_has_dynamic_stride (type
))
2146 case TYPE_CODE_STRUCT
:
2147 case TYPE_CODE_UNION
:
2151 bool is_cplus
= HAVE_CPLUS_STRUCT (type
);
2153 for (i
= 0; i
< type
->num_fields (); ++i
)
2155 /* Static fields can be ignored here. */
2156 if (field_is_static (&type
->field (i
)))
2158 /* If the field has dynamic type, then so does TYPE. */
2159 if (is_dynamic_type_internal (type
->field (i
).type (), 0))
2161 /* If the field is at a fixed offset, then it is not
2163 if (type
->field (i
).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK
)
2165 /* Do not consider C++ virtual base types to be dynamic
2166 due to the field's offset being dynamic; these are
2167 handled via other means. */
2168 if (is_cplus
&& BASETYPE_VIA_VIRTUAL (type
, i
))
2179 /* See gdbtypes.h. */
2182 is_dynamic_type (struct type
*type
)
2184 return is_dynamic_type_internal (type
, 1);
2187 static struct type
*resolve_dynamic_type_internal
2188 (struct type
*type
, struct property_addr_info
*addr_stack
, int top_level
);
2190 /* Given a dynamic range type (dyn_range_type) and a stack of
2191 struct property_addr_info elements, return a static version
2194 When RESOLVE_P is true then the returned static range is created by
2195 actually evaluating any dynamic properties within the range type, while
2196 when RESOLVE_P is false the returned static range has all of the bounds
2197 and stride information set to undefined. The RESOLVE_P set to false
2198 case will be used when evaluating a dynamic array that is not
2199 allocated, or not associated, i.e. the bounds information might not be
2202 RANK is the array rank for which we are resolving this range, and is a
2203 zero based count. The rank should never be negative.
2206 static struct type
*
2207 resolve_dynamic_range (struct type
*dyn_range_type
,
2208 struct property_addr_info
*addr_stack
,
2209 int rank
, bool resolve_p
= true)
2212 struct type
*static_range_type
, *static_target_type
;
2213 struct dynamic_prop low_bound
, high_bound
, stride
;
2215 gdb_assert (dyn_range_type
->code () == TYPE_CODE_RANGE
);
2216 gdb_assert (rank
>= 0);
2218 const struct dynamic_prop
*prop
= &dyn_range_type
->bounds ()->low
;
2219 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2220 { (CORE_ADDR
) rank
}))
2221 low_bound
.set_const_val (value
);
2223 low_bound
.set_undefined ();
2225 prop
= &dyn_range_type
->bounds ()->high
;
2226 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2227 { (CORE_ADDR
) rank
}))
2229 high_bound
.set_const_val (value
);
2231 if (dyn_range_type
->bounds ()->flag_upper_bound_is_count
)
2232 high_bound
.set_const_val
2233 (low_bound
.const_val () + high_bound
.const_val () - 1);
2236 high_bound
.set_undefined ();
2238 bool byte_stride_p
= dyn_range_type
->bounds ()->flag_is_byte_stride
;
2239 prop
= &dyn_range_type
->bounds ()->stride
;
2240 if (resolve_p
&& dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
,
2241 { (CORE_ADDR
) rank
}))
2243 stride
.set_const_val (value
);
2245 /* If we have a bit stride that is not an exact number of bytes then
2246 I really don't think this is going to work with current GDB, the
2247 array indexing code in GDB seems to be pretty heavily tied to byte
2248 offsets right now. Assuming 8 bits in a byte. */
2249 struct gdbarch
*gdbarch
= dyn_range_type
->arch ();
2250 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
2251 if (!byte_stride_p
&& (value
% (unit_size
* 8)) != 0)
2252 error (_("bit strides that are not a multiple of the byte size "
2253 "are currently not supported"));
2257 stride
.set_undefined ();
2258 byte_stride_p
= true;
2262 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type
),
2264 LONGEST bias
= dyn_range_type
->bounds ()->bias
;
2265 static_range_type
= create_range_type_with_stride
2266 (copy_type (dyn_range_type
), static_target_type
,
2267 &low_bound
, &high_bound
, bias
, &stride
, byte_stride_p
);
2268 static_range_type
->bounds ()->flag_bound_evaluated
= 1;
2269 return static_range_type
;
2272 /* Helper function for resolve_dynamic_array_or_string. This function
2273 resolves the properties for a single array at RANK within a nested array
2274 of arrays structure. The RANK value is greater than or equal to 0, and
2275 starts at it's maximum value and goes down by 1 for each recursive call
2276 to this function. So, for a 3-dimensional array, the first call to this
2277 function has RANK == 2, then we call ourselves recursively with RANK ==
2278 1, than again with RANK == 0, and at that point we should return.
2280 TYPE is updated as the dynamic properties are resolved, and so, should
2281 be a copy of the dynamic type, rather than the original dynamic type
2284 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2285 during the dynamic resolution.
2287 When RESOLVE_P is true then the dynamic properties of TYPE are
2288 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2289 instead we assume the array is not allocated/associated yet. */
2291 static struct type
*
2292 resolve_dynamic_array_or_string_1 (struct type
*type
,
2293 struct property_addr_info
*addr_stack
,
2294 int rank
, bool resolve_p
)
2297 struct type
*elt_type
;
2298 struct type
*range_type
;
2299 struct type
*ary_dim
;
2300 struct dynamic_prop
*prop
;
2301 unsigned int bit_stride
= 0;
2303 /* For dynamic type resolution strings can be treated like arrays of
2305 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2306 || type
->code () == TYPE_CODE_STRING
);
2308 /* As the rank is a zero based count we expect this to never be
2310 gdb_assert (rank
>= 0);
2312 /* Resolve the allocated and associated properties before doing anything
2313 else. If an array is not allocated or not associated then (at least
2314 for Fortran) there is no guarantee that the data to define the upper
2315 bound, lower bound, or stride will be correct. If RESOLVE_P is
2316 already false at this point then this is not the first dimension of
2317 the array and a more outer dimension has already marked this array as
2318 not allocated/associated, as such we just ignore this property. This
2319 is fine as GDB only checks the allocated/associated on the outer most
2320 dimension of the array. */
2321 prop
= TYPE_ALLOCATED_PROP (type
);
2322 if (prop
!= NULL
&& resolve_p
2323 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2325 prop
->set_const_val (value
);
2330 prop
= TYPE_ASSOCIATED_PROP (type
);
2331 if (prop
!= NULL
&& resolve_p
2332 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2334 prop
->set_const_val (value
);
2339 range_type
= check_typedef (type
->index_type ());
2341 = resolve_dynamic_range (range_type
, addr_stack
, rank
, resolve_p
);
2343 ary_dim
= check_typedef (TYPE_TARGET_TYPE (type
));
2344 if (ary_dim
!= NULL
&& ary_dim
->code () == TYPE_CODE_ARRAY
)
2346 ary_dim
= copy_type (ary_dim
);
2347 elt_type
= resolve_dynamic_array_or_string_1 (ary_dim
, addr_stack
,
2348 rank
- 1, resolve_p
);
2351 elt_type
= TYPE_TARGET_TYPE (type
);
2353 prop
= type
->dyn_prop (DYN_PROP_BYTE_STRIDE
);
2354 if (prop
!= NULL
&& resolve_p
)
2356 if (dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2358 type
->remove_dyn_prop (DYN_PROP_BYTE_STRIDE
);
2359 bit_stride
= (unsigned int) (value
* 8);
2363 /* Could be a bug in our code, but it could also happen
2364 if the DWARF info is not correct. Issue a warning,
2365 and assume no byte/bit stride (leave bit_stride = 0). */
2366 warning (_("cannot determine array stride for type %s"),
2367 type
->name () ? type
->name () : "<no name>");
2371 bit_stride
= TYPE_FIELD_BITSIZE (type
, 0);
2373 return create_array_type_with_stride (type
, elt_type
, range_type
, NULL
,
2377 /* Resolve an array or string type with dynamic properties, return a new
2378 type with the dynamic properties resolved to actual values. The
2379 ADDR_STACK represents the location of the object being resolved. */
2381 static struct type
*
2382 resolve_dynamic_array_or_string (struct type
*type
,
2383 struct property_addr_info
*addr_stack
)
2388 /* For dynamic type resolution strings can be treated like arrays of
2390 gdb_assert (type
->code () == TYPE_CODE_ARRAY
2391 || type
->code () == TYPE_CODE_STRING
);
2393 type
= copy_type (type
);
2395 /* Resolve the rank property to get rank value. */
2396 struct dynamic_prop
*prop
= TYPE_RANK_PROP (type
);
2397 if (dwarf2_evaluate_property (prop
, nullptr, addr_stack
, &value
))
2399 prop
->set_const_val (value
);
2404 /* Rank is zero, if a variable is passed as an argument to a
2405 function. In this case the resolved type should not be an
2406 array, but should instead be that of an array element. */
2407 struct type
*dynamic_array_type
= type
;
2408 type
= copy_type (TYPE_TARGET_TYPE (dynamic_array_type
));
2409 struct dynamic_prop_list
*prop_list
2410 = TYPE_MAIN_TYPE (dynamic_array_type
)->dyn_prop_list
;
2411 if (prop_list
!= nullptr)
2413 struct obstack
*obstack
2414 = &type
->objfile_owner ()->objfile_obstack
;
2415 TYPE_MAIN_TYPE (type
)->dyn_prop_list
2416 = copy_dynamic_prop_list (obstack
, prop_list
);
2420 else if (type
->code () == TYPE_CODE_STRING
&& rank
!= 1)
2422 /* What would this even mean? A string with a dynamic rank
2424 error (_("unable to handle string with dynamic rank greater than 1"));
2428 /* Arrays with dynamic rank are initially just an array type
2429 with a target type that is the array element.
2431 However, now we know the rank of the array we need to build
2432 the array of arrays structure that GDB expects, that is we
2433 need an array type that has a target which is an array type,
2434 and so on, until eventually, we have the element type at the
2435 end of the chain. Create all the additional array types here
2436 by copying the top level array type. */
2437 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2438 struct type
*rank_type
= type
;
2439 for (int i
= 1; i
< rank
; i
++)
2441 TYPE_TARGET_TYPE (rank_type
) = copy_type (rank_type
);
2442 rank_type
= TYPE_TARGET_TYPE (rank_type
);
2444 TYPE_TARGET_TYPE (rank_type
) = element_type
;
2451 for (struct type
*tmp_type
= check_typedef (TYPE_TARGET_TYPE (type
));
2452 tmp_type
->code () == TYPE_CODE_ARRAY
;
2453 tmp_type
= check_typedef (TYPE_TARGET_TYPE (tmp_type
)))
2457 /* The rank that we calculated above is actually a count of the number of
2458 ranks. However, when we resolve the type of each individual array
2459 rank we should actually use a rank "offset", e.g. an array with a rank
2460 count of 1 (calculated above) will use the rank offset 0 in order to
2461 resolve the details of the first array dimension. As a result, we
2462 reduce the rank by 1 here. */
2465 return resolve_dynamic_array_or_string_1 (type
, addr_stack
, rank
, true);
2468 /* Resolve dynamic bounds of members of the union TYPE to static
2469 bounds. ADDR_STACK is a stack of struct property_addr_info
2470 to be used if needed during the dynamic resolution. */
2472 static struct type
*
2473 resolve_dynamic_union (struct type
*type
,
2474 struct property_addr_info
*addr_stack
)
2476 struct type
*resolved_type
;
2478 unsigned int max_len
= 0;
2480 gdb_assert (type
->code () == TYPE_CODE_UNION
);
2482 resolved_type
= copy_type (type
);
2483 resolved_type
->set_fields
2485 TYPE_ALLOC (resolved_type
,
2486 resolved_type
->num_fields () * sizeof (struct field
)));
2487 memcpy (resolved_type
->fields (),
2489 resolved_type
->num_fields () * sizeof (struct field
));
2490 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2494 if (field_is_static (&type
->field (i
)))
2497 t
= resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2499 resolved_type
->field (i
).set_type (t
);
2501 struct type
*real_type
= check_typedef (t
);
2502 if (TYPE_LENGTH (real_type
) > max_len
)
2503 max_len
= TYPE_LENGTH (real_type
);
2506 TYPE_LENGTH (resolved_type
) = max_len
;
2507 return resolved_type
;
2510 /* See gdbtypes.h. */
2513 variant::matches (ULONGEST value
, bool is_unsigned
) const
2515 for (const discriminant_range
&range
: discriminants
)
2516 if (range
.contains (value
, is_unsigned
))
2522 compute_variant_fields_inner (struct type
*type
,
2523 struct property_addr_info
*addr_stack
,
2524 const variant_part
&part
,
2525 std::vector
<bool> &flags
);
2527 /* A helper function to determine which variant fields will be active.
2528 This handles both the variant's direct fields, and any variant
2529 parts embedded in this variant. TYPE is the type we're examining.
2530 ADDR_STACK holds information about the concrete object. VARIANT is
2531 the current variant to be handled. FLAGS is where the results are
2532 stored -- this function sets the Nth element in FLAGS if the
2533 corresponding field is enabled. ENABLED is whether this variant is
2537 compute_variant_fields_recurse (struct type
*type
,
2538 struct property_addr_info
*addr_stack
,
2539 const variant
&variant
,
2540 std::vector
<bool> &flags
,
2543 for (int field
= variant
.first_field
; field
< variant
.last_field
; ++field
)
2544 flags
[field
] = enabled
;
2546 for (const variant_part
&new_part
: variant
.parts
)
2549 compute_variant_fields_inner (type
, addr_stack
, new_part
, flags
);
2552 for (const auto &sub_variant
: new_part
.variants
)
2553 compute_variant_fields_recurse (type
, addr_stack
, sub_variant
,
2559 /* A helper function to determine which variant fields will be active.
2560 This evaluates the discriminant, decides which variant (if any) is
2561 active, and then updates FLAGS to reflect which fields should be
2562 available. TYPE is the type we're examining. ADDR_STACK holds
2563 information about the concrete object. VARIANT is the current
2564 variant to be handled. FLAGS is where the results are stored --
2565 this function sets the Nth element in FLAGS if the corresponding
2566 field is enabled. */
2569 compute_variant_fields_inner (struct type
*type
,
2570 struct property_addr_info
*addr_stack
,
2571 const variant_part
&part
,
2572 std::vector
<bool> &flags
)
2574 /* Evaluate the discriminant. */
2575 gdb::optional
<ULONGEST
> discr_value
;
2576 if (part
.discriminant_index
!= -1)
2578 int idx
= part
.discriminant_index
;
2580 if (type
->field (idx
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2581 error (_("Cannot determine struct field location"
2582 " (invalid location kind)"));
2584 if (addr_stack
->valaddr
.data () != NULL
)
2585 discr_value
= unpack_field_as_long (type
, addr_stack
->valaddr
.data (),
2589 CORE_ADDR addr
= (addr_stack
->addr
2590 + (type
->field (idx
).loc_bitpos ()
2591 / TARGET_CHAR_BIT
));
2593 LONGEST bitsize
= TYPE_FIELD_BITSIZE (type
, idx
);
2594 LONGEST size
= bitsize
/ 8;
2596 size
= TYPE_LENGTH (type
->field (idx
).type ());
2598 gdb_byte bits
[sizeof (ULONGEST
)];
2599 read_memory (addr
, bits
, size
);
2601 LONGEST bitpos
= (type
->field (idx
).loc_bitpos ()
2604 discr_value
= unpack_bits_as_long (type
->field (idx
).type (),
2605 bits
, bitpos
, bitsize
);
2609 /* Go through each variant and see which applies. */
2610 const variant
*default_variant
= nullptr;
2611 const variant
*applied_variant
= nullptr;
2612 for (const auto &variant
: part
.variants
)
2614 if (variant
.is_default ())
2615 default_variant
= &variant
;
2616 else if (discr_value
.has_value ()
2617 && variant
.matches (*discr_value
, part
.is_unsigned
))
2619 applied_variant
= &variant
;
2623 if (applied_variant
== nullptr)
2624 applied_variant
= default_variant
;
2626 for (const auto &variant
: part
.variants
)
2627 compute_variant_fields_recurse (type
, addr_stack
, variant
,
2628 flags
, applied_variant
== &variant
);
2631 /* Determine which variant fields are available in TYPE. The enabled
2632 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2633 about the concrete object. PARTS describes the top-level variant
2634 parts for this type. */
2637 compute_variant_fields (struct type
*type
,
2638 struct type
*resolved_type
,
2639 struct property_addr_info
*addr_stack
,
2640 const gdb::array_view
<variant_part
> &parts
)
2642 /* Assume all fields are included by default. */
2643 std::vector
<bool> flags (resolved_type
->num_fields (), true);
2645 /* Now disable fields based on the variants that control them. */
2646 for (const auto &part
: parts
)
2647 compute_variant_fields_inner (type
, addr_stack
, part
, flags
);
2649 resolved_type
->set_num_fields
2650 (std::count (flags
.begin (), flags
.end (), true));
2651 resolved_type
->set_fields
2653 TYPE_ALLOC (resolved_type
,
2654 resolved_type
->num_fields () * sizeof (struct field
)));
2657 for (int i
= 0; i
< type
->num_fields (); ++i
)
2662 resolved_type
->field (out
) = type
->field (i
);
2667 /* Resolve dynamic bounds of members of the struct TYPE to static
2668 bounds. ADDR_STACK is a stack of struct property_addr_info to
2669 be used if needed during the dynamic resolution. */
2671 static struct type
*
2672 resolve_dynamic_struct (struct type
*type
,
2673 struct property_addr_info
*addr_stack
)
2675 struct type
*resolved_type
;
2677 unsigned resolved_type_bit_length
= 0;
2679 gdb_assert (type
->code () == TYPE_CODE_STRUCT
);
2681 resolved_type
= copy_type (type
);
2683 dynamic_prop
*variant_prop
= resolved_type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
2684 if (variant_prop
!= nullptr && variant_prop
->kind () == PROP_VARIANT_PARTS
)
2686 compute_variant_fields (type
, resolved_type
, addr_stack
,
2687 *variant_prop
->variant_parts ());
2688 /* We want to leave the property attached, so that the Rust code
2689 can tell whether the type was originally an enum. */
2690 variant_prop
->set_original_type (type
);
2694 resolved_type
->set_fields
2696 TYPE_ALLOC (resolved_type
,
2697 resolved_type
->num_fields () * sizeof (struct field
)));
2698 if (type
->num_fields () > 0)
2699 memcpy (resolved_type
->fields (),
2701 resolved_type
->num_fields () * sizeof (struct field
));
2704 for (i
= 0; i
< resolved_type
->num_fields (); ++i
)
2706 unsigned new_bit_length
;
2707 struct property_addr_info pinfo
;
2709 if (field_is_static (&resolved_type
->field (i
)))
2712 if (resolved_type
->field (i
).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK
)
2714 struct dwarf2_property_baton baton
;
2716 = lookup_pointer_type (resolved_type
->field (i
).type ());
2717 baton
.locexpr
= *resolved_type
->field (i
).loc_dwarf_block ();
2719 struct dynamic_prop prop
;
2720 prop
.set_locexpr (&baton
);
2723 if (dwarf2_evaluate_property (&prop
, nullptr, addr_stack
, &addr
,
2724 {addr_stack
->addr
}))
2725 resolved_type
->field (i
).set_loc_bitpos
2726 (TARGET_CHAR_BIT
* (addr
- addr_stack
->addr
));
2729 /* As we know this field is not a static field, the field's
2730 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2731 this is the case, but only trigger a simple error rather
2732 than an internal error if that fails. While failing
2733 that verification indicates a bug in our code, the error
2734 is not severe enough to suggest to the user he stops
2735 his debugging session because of it. */
2736 if (resolved_type
->field (i
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
2737 error (_("Cannot determine struct field location"
2738 " (invalid location kind)"));
2740 pinfo
.type
= check_typedef (resolved_type
->field (i
).type ());
2741 size_t offset
= resolved_type
->field (i
).loc_bitpos () / TARGET_CHAR_BIT
;
2742 pinfo
.valaddr
= addr_stack
->valaddr
;
2743 if (!pinfo
.valaddr
.empty ())
2744 pinfo
.valaddr
= pinfo
.valaddr
.slice (offset
);
2745 pinfo
.addr
= addr_stack
->addr
+ offset
;
2746 pinfo
.next
= addr_stack
;
2748 resolved_type
->field (i
).set_type
2749 (resolve_dynamic_type_internal (resolved_type
->field (i
).type (),
2751 gdb_assert (resolved_type
->field (i
).loc_kind ()
2752 == FIELD_LOC_KIND_BITPOS
);
2754 new_bit_length
= resolved_type
->field (i
).loc_bitpos ();
2755 if (TYPE_FIELD_BITSIZE (resolved_type
, i
) != 0)
2756 new_bit_length
+= TYPE_FIELD_BITSIZE (resolved_type
, i
);
2759 struct type
*real_type
2760 = check_typedef (resolved_type
->field (i
).type ());
2762 new_bit_length
+= (TYPE_LENGTH (real_type
) * TARGET_CHAR_BIT
);
2765 /* Normally, we would use the position and size of the last field
2766 to determine the size of the enclosing structure. But GCC seems
2767 to be encoding the position of some fields incorrectly when
2768 the struct contains a dynamic field that is not placed last.
2769 So we compute the struct size based on the field that has
2770 the highest position + size - probably the best we can do. */
2771 if (new_bit_length
> resolved_type_bit_length
)
2772 resolved_type_bit_length
= new_bit_length
;
2775 /* The length of a type won't change for fortran, but it does for C and Ada.
2776 For fortran the size of dynamic fields might change over time but not the
2777 type length of the structure. If we adapt it, we run into problems
2778 when calculating the element offset for arrays of structs. */
2779 if (current_language
->la_language
!= language_fortran
)
2780 TYPE_LENGTH (resolved_type
)
2781 = (resolved_type_bit_length
+ TARGET_CHAR_BIT
- 1) / TARGET_CHAR_BIT
;
2783 /* The Ada language uses this field as a cache for static fixed types: reset
2784 it as RESOLVED_TYPE must have its own static fixed type. */
2785 TYPE_TARGET_TYPE (resolved_type
) = NULL
;
2787 return resolved_type
;
2790 /* Worker for resolved_dynamic_type. */
2792 static struct type
*
2793 resolve_dynamic_type_internal (struct type
*type
,
2794 struct property_addr_info
*addr_stack
,
2797 struct type
*real_type
= check_typedef (type
);
2798 struct type
*resolved_type
= nullptr;
2799 struct dynamic_prop
*prop
;
2802 if (!is_dynamic_type_internal (real_type
, top_level
))
2805 gdb::optional
<CORE_ADDR
> type_length
;
2806 prop
= TYPE_DYNAMIC_LENGTH (type
);
2808 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2809 type_length
= value
;
2811 if (type
->code () == TYPE_CODE_TYPEDEF
)
2813 resolved_type
= copy_type (type
);
2814 TYPE_TARGET_TYPE (resolved_type
)
2815 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
), addr_stack
,
2820 /* Before trying to resolve TYPE, make sure it is not a stub. */
2823 switch (type
->code ())
2827 struct property_addr_info pinfo
;
2829 pinfo
.type
= check_typedef (TYPE_TARGET_TYPE (type
));
2831 if (addr_stack
->valaddr
.data () != NULL
)
2832 pinfo
.addr
= extract_typed_address (addr_stack
->valaddr
.data (),
2835 pinfo
.addr
= read_memory_typed_address (addr_stack
->addr
, type
);
2836 pinfo
.next
= addr_stack
;
2838 resolved_type
= copy_type (type
);
2839 TYPE_TARGET_TYPE (resolved_type
)
2840 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type
),
2845 case TYPE_CODE_STRING
:
2846 /* Strings are very much like an array of characters, and can be
2847 treated as one here. */
2848 case TYPE_CODE_ARRAY
:
2849 resolved_type
= resolve_dynamic_array_or_string (type
, addr_stack
);
2852 case TYPE_CODE_RANGE
:
2853 /* Pass 0 for the rank value here, which indicates this is a
2854 range for the first rank of an array. The assumption is that
2855 this rank value is not actually required for the resolution of
2856 the dynamic range, otherwise, we'd be resolving this range
2857 within the context of a dynamic array. */
2858 resolved_type
= resolve_dynamic_range (type
, addr_stack
, 0);
2861 case TYPE_CODE_UNION
:
2862 resolved_type
= resolve_dynamic_union (type
, addr_stack
);
2865 case TYPE_CODE_STRUCT
:
2866 resolved_type
= resolve_dynamic_struct (type
, addr_stack
);
2871 if (resolved_type
== nullptr)
2874 if (type_length
.has_value ())
2876 TYPE_LENGTH (resolved_type
) = *type_length
;
2877 resolved_type
->remove_dyn_prop (DYN_PROP_BYTE_SIZE
);
2880 /* Resolve data_location attribute. */
2881 prop
= TYPE_DATA_LOCATION (resolved_type
);
2883 && dwarf2_evaluate_property (prop
, NULL
, addr_stack
, &value
))
2885 /* Start of Fortran hack. See comment in f-lang.h for what is going
2887 if (current_language
->la_language
== language_fortran
2888 && resolved_type
->code () == TYPE_CODE_ARRAY
)
2889 value
= fortran_adjust_dynamic_array_base_address_hack (resolved_type
,
2891 /* End of Fortran hack. */
2892 prop
->set_const_val (value
);
2895 return resolved_type
;
2898 /* See gdbtypes.h */
2901 resolve_dynamic_type (struct type
*type
,
2902 gdb::array_view
<const gdb_byte
> valaddr
,
2905 struct property_addr_info pinfo
2906 = {check_typedef (type
), valaddr
, addr
, NULL
};
2908 return resolve_dynamic_type_internal (type
, &pinfo
, 1);
2911 /* See gdbtypes.h */
2914 type::dyn_prop (dynamic_prop_node_kind prop_kind
) const
2916 dynamic_prop_list
*node
= this->main_type
->dyn_prop_list
;
2918 while (node
!= NULL
)
2920 if (node
->prop_kind
== prop_kind
)
2927 /* See gdbtypes.h */
2930 type::add_dyn_prop (dynamic_prop_node_kind prop_kind
, dynamic_prop prop
)
2932 struct dynamic_prop_list
*temp
;
2934 gdb_assert (this->is_objfile_owned ());
2936 temp
= XOBNEW (&this->objfile_owner ()->objfile_obstack
,
2937 struct dynamic_prop_list
);
2938 temp
->prop_kind
= prop_kind
;
2940 temp
->next
= this->main_type
->dyn_prop_list
;
2942 this->main_type
->dyn_prop_list
= temp
;
2945 /* See gdbtypes.h. */
2948 type::remove_dyn_prop (dynamic_prop_node_kind kind
)
2950 struct dynamic_prop_list
*prev_node
, *curr_node
;
2952 curr_node
= this->main_type
->dyn_prop_list
;
2955 while (NULL
!= curr_node
)
2957 if (curr_node
->prop_kind
== kind
)
2959 /* Update the linked list but don't free anything.
2960 The property was allocated on objstack and it is not known
2961 if we are on top of it. Nevertheless, everything is released
2962 when the complete objstack is freed. */
2963 if (NULL
== prev_node
)
2964 this->main_type
->dyn_prop_list
= curr_node
->next
;
2966 prev_node
->next
= curr_node
->next
;
2971 prev_node
= curr_node
;
2972 curr_node
= curr_node
->next
;
2976 /* Find the real type of TYPE. This function returns the real type,
2977 after removing all layers of typedefs, and completing opaque or stub
2978 types. Completion changes the TYPE argument, but stripping of
2981 Instance flags (e.g. const/volatile) are preserved as typedefs are
2982 stripped. If necessary a new qualified form of the underlying type
2985 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
2986 not been computed and we're either in the middle of reading symbols, or
2987 there was no name for the typedef in the debug info.
2989 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2990 QUITs in the symbol reading code can also throw.
2991 Thus this function can throw an exception.
2993 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2996 If this is a stubbed struct (i.e. declared as struct foo *), see if
2997 we can find a full definition in some other file. If so, copy this
2998 definition, so we can use it in future. There used to be a comment
2999 (but not any code) that if we don't find a full definition, we'd
3000 set a flag so we don't spend time in the future checking the same
3001 type. That would be a mistake, though--we might load in more
3002 symbols which contain a full definition for the type. */
3005 check_typedef (struct type
*type
)
3007 struct type
*orig_type
= type
;
3011 /* While we're removing typedefs, we don't want to lose qualifiers.
3012 E.g., const/volatile. */
3013 type_instance_flags instance_flags
= type
->instance_flags ();
3015 while (type
->code () == TYPE_CODE_TYPEDEF
)
3017 if (!TYPE_TARGET_TYPE (type
))
3022 /* It is dangerous to call lookup_symbol if we are currently
3023 reading a symtab. Infinite recursion is one danger. */
3024 if (currently_reading_symtab
)
3025 return make_qualified_type (type
, instance_flags
, NULL
);
3027 name
= type
->name ();
3028 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
3029 VAR_DOMAIN as appropriate? */
3032 stub_noname_complaint ();
3033 return make_qualified_type (type
, instance_flags
, NULL
);
3035 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3037 TYPE_TARGET_TYPE (type
) = sym
->type ();
3038 else /* TYPE_CODE_UNDEF */
3039 TYPE_TARGET_TYPE (type
) = alloc_type_arch (type
->arch ());
3041 type
= TYPE_TARGET_TYPE (type
);
3043 /* Preserve the instance flags as we traverse down the typedef chain.
3045 Handling address spaces/classes is nasty, what do we do if there's a
3047 E.g., what if an outer typedef marks the type as class_1 and an inner
3048 typedef marks the type as class_2?
3049 This is the wrong place to do such error checking. We leave it to
3050 the code that created the typedef in the first place to flag the
3051 error. We just pick the outer address space (akin to letting the
3052 outer cast in a chain of casting win), instead of assuming
3053 "it can't happen". */
3055 const type_instance_flags ALL_SPACES
3056 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3057 | TYPE_INSTANCE_FLAG_DATA_SPACE
);
3058 const type_instance_flags ALL_CLASSES
3059 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
;
3061 type_instance_flags new_instance_flags
= type
->instance_flags ();
3063 /* Treat code vs data spaces and address classes separately. */
3064 if ((instance_flags
& ALL_SPACES
) != 0)
3065 new_instance_flags
&= ~ALL_SPACES
;
3066 if ((instance_flags
& ALL_CLASSES
) != 0)
3067 new_instance_flags
&= ~ALL_CLASSES
;
3069 instance_flags
|= new_instance_flags
;
3073 /* If this is a struct/class/union with no fields, then check
3074 whether a full definition exists somewhere else. This is for
3075 systems where a type definition with no fields is issued for such
3076 types, instead of identifying them as stub types in the first
3079 if (TYPE_IS_OPAQUE (type
)
3080 && opaque_type_resolution
3081 && !currently_reading_symtab
)
3083 const char *name
= type
->name ();
3084 struct type
*newtype
;
3088 stub_noname_complaint ();
3089 return make_qualified_type (type
, instance_flags
, NULL
);
3091 newtype
= lookup_transparent_type (name
);
3095 /* If the resolved type and the stub are in the same
3096 objfile, then replace the stub type with the real deal.
3097 But if they're in separate objfiles, leave the stub
3098 alone; we'll just look up the transparent type every time
3099 we call check_typedef. We can't create pointers between
3100 types allocated to different objfiles, since they may
3101 have different lifetimes. Trying to copy NEWTYPE over to
3102 TYPE's objfile is pointless, too, since you'll have to
3103 move over any other types NEWTYPE refers to, which could
3104 be an unbounded amount of stuff. */
3105 if (newtype
->objfile_owner () == type
->objfile_owner ())
3106 type
= make_qualified_type (newtype
, type
->instance_flags (), type
);
3111 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3113 else if (type
->is_stub () && !currently_reading_symtab
)
3115 const char *name
= type
->name ();
3116 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3122 stub_noname_complaint ();
3123 return make_qualified_type (type
, instance_flags
, NULL
);
3125 sym
= lookup_symbol (name
, 0, STRUCT_DOMAIN
, 0).symbol
;
3128 /* Same as above for opaque types, we can replace the stub
3129 with the complete type only if they are in the same
3131 if (sym
->type ()->objfile_owner () == type
->objfile_owner ())
3132 type
= make_qualified_type (sym
->type (),
3133 type
->instance_flags (), type
);
3135 type
= sym
->type ();
3139 if (type
->target_is_stub ())
3141 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
3143 if (target_type
->is_stub () || target_type
->target_is_stub ())
3145 /* Nothing we can do. */
3147 else if (type
->code () == TYPE_CODE_RANGE
)
3149 TYPE_LENGTH (type
) = TYPE_LENGTH (target_type
);
3150 type
->set_target_is_stub (false);
3152 else if (type
->code () == TYPE_CODE_ARRAY
3153 && update_static_array_size (type
))
3154 type
->set_target_is_stub (false);
3157 type
= make_qualified_type (type
, instance_flags
, NULL
);
3159 /* Cache TYPE_LENGTH for future use. */
3160 TYPE_LENGTH (orig_type
) = TYPE_LENGTH (type
);
3165 /* Parse a type expression in the string [P..P+LENGTH). If an error
3166 occurs, silently return a void type. */
3168 static struct type
*
3169 safe_parse_type (struct gdbarch
*gdbarch
, const char *p
, int length
)
3171 struct ui_file
*saved_gdb_stderr
;
3172 struct type
*type
= NULL
; /* Initialize to keep gcc happy. */
3174 /* Suppress error messages. */
3175 saved_gdb_stderr
= gdb_stderr
;
3176 gdb_stderr
= &null_stream
;
3178 /* Call parse_and_eval_type() without fear of longjmp()s. */
3181 type
= parse_and_eval_type (p
, length
);
3183 catch (const gdb_exception_error
&except
)
3185 type
= builtin_type (gdbarch
)->builtin_void
;
3188 /* Stop suppressing error messages. */
3189 gdb_stderr
= saved_gdb_stderr
;
3194 /* Ugly hack to convert method stubs into method types.
3196 He ain't kiddin'. This demangles the name of the method into a
3197 string including argument types, parses out each argument type,
3198 generates a string casting a zero to that type, evaluates the
3199 string, and stuffs the resulting type into an argtype vector!!!
3200 Then it knows the type of the whole function (including argument
3201 types for overloading), which info used to be in the stab's but was
3202 removed to hack back the space required for them. */
3205 check_stub_method (struct type
*type
, int method_id
, int signature_id
)
3207 struct gdbarch
*gdbarch
= type
->arch ();
3209 char *mangled_name
= gdb_mangle_name (type
, method_id
, signature_id
);
3210 gdb::unique_xmalloc_ptr
<char> demangled_name
3211 = gdb_demangle (mangled_name
, DMGL_PARAMS
| DMGL_ANSI
);
3212 char *argtypetext
, *p
;
3213 int depth
= 0, argcount
= 1;
3214 struct field
*argtypes
;
3217 /* Make sure we got back a function string that we can use. */
3219 p
= strchr (demangled_name
.get (), '(');
3223 if (demangled_name
== NULL
|| p
== NULL
)
3224 error (_("Internal: Cannot demangle mangled name `%s'."),
3227 /* Now, read in the parameters that define this type. */
3232 if (*p
== '(' || *p
== '<')
3236 else if (*p
== ')' || *p
== '>')
3240 else if (*p
== ',' && depth
== 0)
3248 /* If we read one argument and it was ``void'', don't count it. */
3249 if (startswith (argtypetext
, "(void)"))
3252 /* We need one extra slot, for the THIS pointer. */
3254 argtypes
= (struct field
*)
3255 TYPE_ALLOC (type
, (argcount
+ 1) * sizeof (struct field
));
3258 /* Add THIS pointer for non-static methods. */
3259 f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3260 if (TYPE_FN_FIELD_STATIC_P (f
, signature_id
))
3264 argtypes
[0].set_type (lookup_pointer_type (type
));
3268 if (*p
!= ')') /* () means no args, skip while. */
3273 if (depth
<= 0 && (*p
== ',' || *p
== ')'))
3275 /* Avoid parsing of ellipsis, they will be handled below.
3276 Also avoid ``void'' as above. */
3277 if (strncmp (argtypetext
, "...", p
- argtypetext
) != 0
3278 && strncmp (argtypetext
, "void", p
- argtypetext
) != 0)
3280 argtypes
[argcount
].set_type
3281 (safe_parse_type (gdbarch
, argtypetext
, p
- argtypetext
));
3284 argtypetext
= p
+ 1;
3287 if (*p
== '(' || *p
== '<')
3291 else if (*p
== ')' || *p
== '>')
3300 TYPE_FN_FIELD_PHYSNAME (f
, signature_id
) = mangled_name
;
3302 /* Now update the old "stub" type into a real type. */
3303 mtype
= TYPE_FN_FIELD_TYPE (f
, signature_id
);
3304 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3305 We want a method (TYPE_CODE_METHOD). */
3306 smash_to_method_type (mtype
, type
, TYPE_TARGET_TYPE (mtype
),
3307 argtypes
, argcount
, p
[-2] == '.');
3308 mtype
->set_is_stub (false);
3309 TYPE_FN_FIELD_STUB (f
, signature_id
) = 0;
3312 /* This is the external interface to check_stub_method, above. This
3313 function unstubs all of the signatures for TYPE's METHOD_ID method
3314 name. After calling this function TYPE_FN_FIELD_STUB will be
3315 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3318 This function unfortunately can not die until stabs do. */
3321 check_stub_method_group (struct type
*type
, int method_id
)
3323 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, method_id
);
3324 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
3326 for (int j
= 0; j
< len
; j
++)
3328 if (TYPE_FN_FIELD_STUB (f
, j
))
3329 check_stub_method (type
, method_id
, j
);
3333 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3334 const struct cplus_struct_type cplus_struct_default
= { };
3337 allocate_cplus_struct_type (struct type
*type
)
3339 if (HAVE_CPLUS_STRUCT (type
))
3340 /* Structure was already allocated. Nothing more to do. */
3343 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_CPLUS_STUFF
;
3344 TYPE_RAW_CPLUS_SPECIFIC (type
) = (struct cplus_struct_type
*)
3345 TYPE_ALLOC (type
, sizeof (struct cplus_struct_type
));
3346 *(TYPE_RAW_CPLUS_SPECIFIC (type
)) = cplus_struct_default
;
3347 set_type_vptr_fieldno (type
, -1);
3350 const struct gnat_aux_type gnat_aux_default
=
3353 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3354 and allocate the associated gnat-specific data. The gnat-specific
3355 data is also initialized to gnat_aux_default. */
3358 allocate_gnat_aux_type (struct type
*type
)
3360 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_GNAT_STUFF
;
3361 TYPE_GNAT_SPECIFIC (type
) = (struct gnat_aux_type
*)
3362 TYPE_ALLOC (type
, sizeof (struct gnat_aux_type
));
3363 *(TYPE_GNAT_SPECIFIC (type
)) = gnat_aux_default
;
3366 /* Helper function to initialize a newly allocated type. Set type code
3367 to CODE and initialize the type-specific fields accordingly. */
3370 set_type_code (struct type
*type
, enum type_code code
)
3372 type
->set_code (code
);
3376 case TYPE_CODE_STRUCT
:
3377 case TYPE_CODE_UNION
:
3378 case TYPE_CODE_NAMESPACE
:
3379 INIT_CPLUS_SPECIFIC (type
);
3382 TYPE_SPECIFIC_FIELD (type
) = TYPE_SPECIFIC_FLOATFORMAT
;
3384 case TYPE_CODE_FUNC
:
3385 INIT_FUNC_SPECIFIC (type
);
3387 case TYPE_CODE_FIXED_POINT
:
3388 INIT_FIXED_POINT_SPECIFIC (type
);
3393 /* Helper function to verify floating-point format and size.
3394 BIT is the type size in bits; if BIT equals -1, the size is
3395 determined by the floatformat. Returns size to be used. */
3398 verify_floatformat (int bit
, const struct floatformat
*floatformat
)
3400 gdb_assert (floatformat
!= NULL
);
3403 bit
= floatformat
->totalsize
;
3405 gdb_assert (bit
>= 0);
3406 gdb_assert (bit
>= floatformat
->totalsize
);
3411 /* Return the floating-point format for a floating-point variable of
3414 const struct floatformat
*
3415 floatformat_from_type (const struct type
*type
)
3417 gdb_assert (type
->code () == TYPE_CODE_FLT
);
3418 gdb_assert (TYPE_FLOATFORMAT (type
));
3419 return TYPE_FLOATFORMAT (type
);
3422 /* Helper function to initialize the standard scalar types.
3424 If NAME is non-NULL, then it is used to initialize the type name.
3425 Note that NAME is not copied; it is required to have a lifetime at
3426 least as long as OBJFILE. */
3429 init_type (struct objfile
*objfile
, enum type_code code
, int bit
,
3434 type
= alloc_type (objfile
);
3435 set_type_code (type
, code
);
3436 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
3437 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
3438 type
->set_name (name
);
3443 /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
3444 to use with variables that have no debug info. NAME is the type
3447 static struct type
*
3448 init_nodebug_var_type (struct objfile
*objfile
, const char *name
)
3450 return init_type (objfile
, TYPE_CODE_ERROR
, 0, name
);
3453 /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
3454 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3455 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3458 init_integer_type (struct objfile
*objfile
,
3459 int bit
, int unsigned_p
, const char *name
)
3463 t
= init_type (objfile
, TYPE_CODE_INT
, bit
, name
);
3465 t
->set_is_unsigned (true);
3467 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3468 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3469 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3474 /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
3475 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3476 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3479 init_character_type (struct objfile
*objfile
,
3480 int bit
, int unsigned_p
, const char *name
)
3484 t
= init_type (objfile
, TYPE_CODE_CHAR
, bit
, name
);
3486 t
->set_is_unsigned (true);
3491 /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
3492 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3493 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3496 init_boolean_type (struct objfile
*objfile
,
3497 int bit
, int unsigned_p
, const char *name
)
3501 t
= init_type (objfile
, TYPE_CODE_BOOL
, bit
, name
);
3503 t
->set_is_unsigned (true);
3505 TYPE_SPECIFIC_FIELD (t
) = TYPE_SPECIFIC_INT
;
3506 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_size
= bit
;
3507 TYPE_MAIN_TYPE (t
)->type_specific
.int_stuff
.bit_offset
= 0;
3512 /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
3513 BIT is the type size in bits; if BIT equals -1, the size is
3514 determined by the floatformat. NAME is the type name. Set the
3515 TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
3516 to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
3517 order of the objfile's architecture is used. */
3520 init_float_type (struct objfile
*objfile
,
3521 int bit
, const char *name
,
3522 const struct floatformat
**floatformats
,
3523 enum bfd_endian byte_order
)
3525 if (byte_order
== BFD_ENDIAN_UNKNOWN
)
3527 struct gdbarch
*gdbarch
= objfile
->arch ();
3528 byte_order
= gdbarch_byte_order (gdbarch
);
3530 const struct floatformat
*fmt
= floatformats
[byte_order
];
3533 bit
= verify_floatformat (bit
, fmt
);
3534 t
= init_type (objfile
, TYPE_CODE_FLT
, bit
, name
);
3535 TYPE_FLOATFORMAT (t
) = fmt
;
3540 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
3541 BIT is the type size in bits. NAME is the type name. */
3544 init_decfloat_type (struct objfile
*objfile
, int bit
, const char *name
)
3548 t
= init_type (objfile
, TYPE_CODE_DECFLOAT
, bit
, name
);
3552 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3555 can_create_complex_type (struct type
*target_type
)
3557 return (target_type
->code () == TYPE_CODE_INT
3558 || target_type
->code () == TYPE_CODE_FLT
);
3561 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3562 name. TARGET_TYPE is the component type. */
3565 init_complex_type (const char *name
, struct type
*target_type
)
3569 gdb_assert (can_create_complex_type (target_type
));
3571 if (TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
== nullptr)
3573 if (name
== nullptr && target_type
->name () != nullptr)
3576 = (char *) TYPE_ALLOC (target_type
,
3577 strlen (target_type
->name ())
3578 + strlen ("_Complex ") + 1);
3579 strcpy (new_name
, "_Complex ");
3580 strcat (new_name
, target_type
->name ());
3584 t
= alloc_type_copy (target_type
);
3585 set_type_code (t
, TYPE_CODE_COMPLEX
);
3586 TYPE_LENGTH (t
) = 2 * TYPE_LENGTH (target_type
);
3589 TYPE_TARGET_TYPE (t
) = target_type
;
3590 TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
= t
;
3593 return TYPE_MAIN_TYPE (target_type
)->flds_bnds
.complex_type
;
3596 /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
3597 BIT is the pointer type size in bits. NAME is the type name.
3598 TARGET_TYPE is the pointer target type. Always sets the pointer type's
3599 TYPE_UNSIGNED flag. */
3602 init_pointer_type (struct objfile
*objfile
,
3603 int bit
, const char *name
, struct type
*target_type
)
3607 t
= init_type (objfile
, TYPE_CODE_PTR
, bit
, name
);
3608 TYPE_TARGET_TYPE (t
) = target_type
;
3609 t
->set_is_unsigned (true);
3613 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3614 BIT is the pointer type size in bits.
3615 UNSIGNED_P should be nonzero if the type is unsigned.
3616 NAME is the type name. */
3619 init_fixed_point_type (struct objfile
*objfile
,
3620 int bit
, int unsigned_p
, const char *name
)
3624 t
= init_type (objfile
, TYPE_CODE_FIXED_POINT
, bit
, name
);
3626 t
->set_is_unsigned (true);
3631 /* See gdbtypes.h. */
3634 type_raw_align (struct type
*type
)
3636 if (type
->align_log2
!= 0)
3637 return 1 << (type
->align_log2
- 1);
3641 /* See gdbtypes.h. */
3644 type_align (struct type
*type
)
3646 /* Check alignment provided in the debug information. */
3647 unsigned raw_align
= type_raw_align (type
);
3651 /* Allow the architecture to provide an alignment. */
3652 ULONGEST align
= gdbarch_type_align (type
->arch (), type
);
3656 switch (type
->code ())
3659 case TYPE_CODE_FUNC
:
3660 case TYPE_CODE_FLAGS
:
3662 case TYPE_CODE_RANGE
:
3664 case TYPE_CODE_ENUM
:
3666 case TYPE_CODE_RVALUE_REF
:
3667 case TYPE_CODE_CHAR
:
3668 case TYPE_CODE_BOOL
:
3669 case TYPE_CODE_DECFLOAT
:
3670 case TYPE_CODE_METHODPTR
:
3671 case TYPE_CODE_MEMBERPTR
:
3672 align
= type_length_units (check_typedef (type
));
3675 case TYPE_CODE_ARRAY
:
3676 case TYPE_CODE_COMPLEX
:
3677 case TYPE_CODE_TYPEDEF
:
3678 align
= type_align (TYPE_TARGET_TYPE (type
));
3681 case TYPE_CODE_STRUCT
:
3682 case TYPE_CODE_UNION
:
3684 int number_of_non_static_fields
= 0;
3685 for (unsigned i
= 0; i
< type
->num_fields (); ++i
)
3687 if (!field_is_static (&type
->field (i
)))
3689 number_of_non_static_fields
++;
3690 ULONGEST f_align
= type_align (type
->field (i
).type ());
3693 /* Don't pretend we know something we don't. */
3697 if (f_align
> align
)
3701 /* A struct with no fields, or with only static fields has an
3703 if (number_of_non_static_fields
== 0)
3709 case TYPE_CODE_STRING
:
3710 /* Not sure what to do here, and these can't appear in C or C++
3714 case TYPE_CODE_VOID
:
3718 case TYPE_CODE_ERROR
:
3719 case TYPE_CODE_METHOD
:
3724 if ((align
& (align
- 1)) != 0)
3726 /* Not a power of 2, so pass. */
3733 /* See gdbtypes.h. */
3736 set_type_align (struct type
*type
, ULONGEST align
)
3738 /* Must be a power of 2. Zero is ok. */
3739 gdb_assert ((align
& (align
- 1)) == 0);
3741 unsigned result
= 0;
3748 if (result
>= (1 << TYPE_ALIGN_BITS
))
3751 type
->align_log2
= result
;
3756 /* Queries on types. */
3759 can_dereference (struct type
*t
)
3761 /* FIXME: Should we return true for references as well as
3763 t
= check_typedef (t
);
3766 && t
->code () == TYPE_CODE_PTR
3767 && TYPE_TARGET_TYPE (t
)->code () != TYPE_CODE_VOID
);
3771 is_integral_type (struct type
*t
)
3773 t
= check_typedef (t
);
3776 && !is_fixed_point_type (t
)
3777 && ((t
->code () == TYPE_CODE_INT
)
3778 || (t
->code () == TYPE_CODE_ENUM
)
3779 || (t
->code () == TYPE_CODE_FLAGS
)
3780 || (t
->code () == TYPE_CODE_CHAR
)
3781 || (t
->code () == TYPE_CODE_RANGE
)
3782 || (t
->code () == TYPE_CODE_BOOL
)));
3786 is_floating_type (struct type
*t
)
3788 t
= check_typedef (t
);
3791 && ((t
->code () == TYPE_CODE_FLT
)
3792 || (t
->code () == TYPE_CODE_DECFLOAT
)));
3795 /* Return true if TYPE is scalar. */
3798 is_scalar_type (struct type
*type
)
3800 type
= check_typedef (type
);
3802 if (is_fixed_point_type (type
))
3803 return 0; /* Implemented as a scalar, but more like a floating point. */
3805 switch (type
->code ())
3807 case TYPE_CODE_ARRAY
:
3808 case TYPE_CODE_STRUCT
:
3809 case TYPE_CODE_UNION
:
3811 case TYPE_CODE_STRING
:
3818 /* Return true if T is scalar, or a composite type which in practice has
3819 the memory layout of a scalar type. E.g., an array or struct with only
3820 one scalar element inside it, or a union with only scalar elements. */
3823 is_scalar_type_recursive (struct type
*t
)
3825 t
= check_typedef (t
);
3827 if (is_scalar_type (t
))
3829 /* Are we dealing with an array or string of known dimensions? */
3830 else if ((t
->code () == TYPE_CODE_ARRAY
3831 || t
->code () == TYPE_CODE_STRING
) && t
->num_fields () == 1
3832 && t
->index_type ()->code () == TYPE_CODE_RANGE
)
3834 LONGEST low_bound
, high_bound
;
3835 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (t
));
3837 if (get_discrete_bounds (t
->index_type (), &low_bound
, &high_bound
))
3838 return (high_bound
== low_bound
3839 && is_scalar_type_recursive (elt_type
));
3843 /* Are we dealing with a struct with one element? */
3844 else if (t
->code () == TYPE_CODE_STRUCT
&& t
->num_fields () == 1)
3845 return is_scalar_type_recursive (t
->field (0).type ());
3846 else if (t
->code () == TYPE_CODE_UNION
)
3848 int i
, n
= t
->num_fields ();
3850 /* If all elements of the union are scalar, then the union is scalar. */
3851 for (i
= 0; i
< n
; i
++)
3852 if (!is_scalar_type_recursive (t
->field (i
).type ()))
3861 /* Return true is T is a class or a union. False otherwise. */
3864 class_or_union_p (const struct type
*t
)
3866 return (t
->code () == TYPE_CODE_STRUCT
3867 || t
->code () == TYPE_CODE_UNION
);
3870 /* A helper function which returns true if types A and B represent the
3871 "same" class type. This is true if the types have the same main
3872 type, or the same name. */
3875 class_types_same_p (const struct type
*a
, const struct type
*b
)
3877 return (TYPE_MAIN_TYPE (a
) == TYPE_MAIN_TYPE (b
)
3878 || (a
->name () && b
->name ()
3879 && !strcmp (a
->name (), b
->name ())));
3882 /* If BASE is an ancestor of DCLASS return the distance between them.
3883 otherwise return -1;
3887 class B: public A {};
3888 class C: public B {};
3891 distance_to_ancestor (A, A, 0) = 0
3892 distance_to_ancestor (A, B, 0) = 1
3893 distance_to_ancestor (A, C, 0) = 2
3894 distance_to_ancestor (A, D, 0) = 3
3896 If PUBLIC is 1 then only public ancestors are considered,
3897 and the function returns the distance only if BASE is a public ancestor
3901 distance_to_ancestor (A, D, 1) = -1. */
3904 distance_to_ancestor (struct type
*base
, struct type
*dclass
, int is_public
)
3909 base
= check_typedef (base
);
3910 dclass
= check_typedef (dclass
);
3912 if (class_types_same_p (base
, dclass
))
3915 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
); i
++)
3917 if (is_public
&& ! BASETYPE_VIA_PUBLIC (dclass
, i
))
3920 d
= distance_to_ancestor (base
, TYPE_BASECLASS (dclass
, i
), is_public
);
3928 /* Check whether BASE is an ancestor or base class or DCLASS
3929 Return 1 if so, and 0 if not.
3930 Note: If BASE and DCLASS are of the same type, this function
3931 will return 1. So for some class A, is_ancestor (A, A) will
3935 is_ancestor (struct type
*base
, struct type
*dclass
)
3937 return distance_to_ancestor (base
, dclass
, 0) >= 0;
3940 /* Like is_ancestor, but only returns true when BASE is a public
3941 ancestor of DCLASS. */
3944 is_public_ancestor (struct type
*base
, struct type
*dclass
)
3946 return distance_to_ancestor (base
, dclass
, 1) >= 0;
3949 /* A helper function for is_unique_ancestor. */
3952 is_unique_ancestor_worker (struct type
*base
, struct type
*dclass
,
3954 const gdb_byte
*valaddr
, int embedded_offset
,
3955 CORE_ADDR address
, struct value
*val
)
3959 base
= check_typedef (base
);
3960 dclass
= check_typedef (dclass
);
3962 for (i
= 0; i
< TYPE_N_BASECLASSES (dclass
) && count
< 2; ++i
)
3967 iter
= check_typedef (TYPE_BASECLASS (dclass
, i
));
3969 this_offset
= baseclass_offset (dclass
, i
, valaddr
, embedded_offset
,
3972 if (class_types_same_p (base
, iter
))
3974 /* If this is the first subclass, set *OFFSET and set count
3975 to 1. Otherwise, if this is at the same offset as
3976 previous instances, do nothing. Otherwise, increment
3980 *offset
= this_offset
;
3983 else if (this_offset
== *offset
)
3991 count
+= is_unique_ancestor_worker (base
, iter
, offset
,
3993 embedded_offset
+ this_offset
,
4000 /* Like is_ancestor, but only returns true if BASE is a unique base
4001 class of the type of VAL. */
4004 is_unique_ancestor (struct type
*base
, struct value
*val
)
4008 return is_unique_ancestor_worker (base
, value_type (val
), &offset
,
4009 value_contents_for_printing (val
).data (),
4010 value_embedded_offset (val
),
4011 value_address (val
), val
) == 1;
4014 /* See gdbtypes.h. */
4017 type_byte_order (const struct type
*type
)
4019 bfd_endian byteorder
= gdbarch_byte_order (type
->arch ());
4020 if (type
->endianity_is_not_default ())
4022 if (byteorder
== BFD_ENDIAN_BIG
)
4023 return BFD_ENDIAN_LITTLE
;
4026 gdb_assert (byteorder
== BFD_ENDIAN_LITTLE
);
4027 return BFD_ENDIAN_BIG
;
4034 /* See gdbtypes.h. */
4037 is_nocall_function (const struct type
*type
)
4039 gdb_assert (type
->code () == TYPE_CODE_FUNC
4040 || type
->code () == TYPE_CODE_METHOD
);
4042 return TYPE_CALLING_CONVENTION (type
) == DW_CC_nocall
;
4046 /* Overload resolution. */
4048 /* Return the sum of the rank of A with the rank of B. */
4051 sum_ranks (struct rank a
, struct rank b
)
4054 c
.rank
= a
.rank
+ b
.rank
;
4055 c
.subrank
= a
.subrank
+ b
.subrank
;
4059 /* Compare rank A and B and return:
4061 1 if a is better than b
4062 -1 if b is better than a. */
4065 compare_ranks (struct rank a
, struct rank b
)
4067 if (a
.rank
== b
.rank
)
4069 if (a
.subrank
== b
.subrank
)
4071 if (a
.subrank
< b
.subrank
)
4073 if (a
.subrank
> b
.subrank
)
4077 if (a
.rank
< b
.rank
)
4080 /* a.rank > b.rank */
4084 /* Functions for overload resolution begin here. */
4086 /* Compare two badness vectors A and B and return the result.
4087 0 => A and B are identical
4088 1 => A and B are incomparable
4089 2 => A is better than B
4090 3 => A is worse than B */
4093 compare_badness (const badness_vector
&a
, const badness_vector
&b
)
4097 /* Any positives in comparison? */
4098 bool found_pos
= false;
4099 /* Any negatives in comparison? */
4100 bool found_neg
= false;
4101 /* Did A have any INVALID_CONVERSION entries. */
4102 bool a_invalid
= false;
4103 /* Did B have any INVALID_CONVERSION entries. */
4104 bool b_invalid
= false;
4106 /* differing sizes => incomparable */
4107 if (a
.size () != b
.size ())
4110 /* Subtract b from a */
4111 for (i
= 0; i
< a
.size (); i
++)
4113 tmp
= compare_ranks (b
[i
], a
[i
]);
4118 if (a
[i
].rank
>= INVALID_CONVERSION
)
4120 if (b
[i
].rank
>= INVALID_CONVERSION
)
4124 /* B will only be considered better than or incomparable to A if
4125 they both have invalid entries, or if neither does. That is, if
4126 A has only valid entries, and B has an invalid entry, then A will
4127 be considered better than B, even if B happens to be better for
4129 if (a_invalid
!= b_invalid
)
4132 return 3; /* A > B */
4133 return 2; /* A < B */
4138 return 1; /* incomparable */
4140 return 3; /* A > B */
4146 return 2; /* A < B */
4148 return 0; /* A == B */
4152 /* Rank a function by comparing its parameter types (PARMS), to the
4153 types of an argument list (ARGS). Return the badness vector. This
4154 has ARGS.size() + 1 entries. */
4157 rank_function (gdb::array_view
<type
*> parms
,
4158 gdb::array_view
<value
*> args
)
4160 /* add 1 for the length-match rank. */
4162 bv
.reserve (1 + args
.size ());
4164 /* First compare the lengths of the supplied lists.
4165 If there is a mismatch, set it to a high value. */
4167 /* pai/1997-06-03 FIXME: when we have debug info about default
4168 arguments and ellipsis parameter lists, we should consider those
4169 and rank the length-match more finely. */
4171 bv
.push_back ((args
.size () != parms
.size ())
4172 ? LENGTH_MISMATCH_BADNESS
4173 : EXACT_MATCH_BADNESS
);
4175 /* Now rank all the parameters of the candidate function. */
4176 size_t min_len
= std::min (parms
.size (), args
.size ());
4178 for (size_t i
= 0; i
< min_len
; i
++)
4179 bv
.push_back (rank_one_type (parms
[i
], value_type (args
[i
]),
4182 /* If more arguments than parameters, add dummy entries. */
4183 for (size_t i
= min_len
; i
< args
.size (); i
++)
4184 bv
.push_back (TOO_FEW_PARAMS_BADNESS
);
4189 /* Compare the names of two integer types, assuming that any sign
4190 qualifiers have been checked already. We do it this way because
4191 there may be an "int" in the name of one of the types. */
4194 integer_types_same_name_p (const char *first
, const char *second
)
4196 int first_p
, second_p
;
4198 /* If both are shorts, return 1; if neither is a short, keep
4200 first_p
= (strstr (first
, "short") != NULL
);
4201 second_p
= (strstr (second
, "short") != NULL
);
4202 if (first_p
&& second_p
)
4204 if (first_p
|| second_p
)
4207 /* Likewise for long. */
4208 first_p
= (strstr (first
, "long") != NULL
);
4209 second_p
= (strstr (second
, "long") != NULL
);
4210 if (first_p
&& second_p
)
4212 if (first_p
|| second_p
)
4215 /* Likewise for char. */
4216 first_p
= (strstr (first
, "char") != NULL
);
4217 second_p
= (strstr (second
, "char") != NULL
);
4218 if (first_p
&& second_p
)
4220 if (first_p
|| second_p
)
4223 /* They must both be ints. */
4227 /* Compares type A to type B. Returns true if they represent the same
4228 type, false otherwise. */
4231 types_equal (struct type
*a
, struct type
*b
)
4233 /* Identical type pointers. */
4234 /* However, this still doesn't catch all cases of same type for b
4235 and a. The reason is that builtin types are different from
4236 the same ones constructed from the object. */
4240 /* Resolve typedefs */
4241 if (a
->code () == TYPE_CODE_TYPEDEF
)
4242 a
= check_typedef (a
);
4243 if (b
->code () == TYPE_CODE_TYPEDEF
)
4244 b
= check_typedef (b
);
4246 /* Check if identical after resolving typedefs. */
4250 /* If after resolving typedefs a and b are not of the same type
4251 code then they are not equal. */
4252 if (a
->code () != b
->code ())
4255 /* If a and b are both pointers types or both reference types then
4256 they are equal of the same type iff the objects they refer to are
4257 of the same type. */
4258 if (a
->code () == TYPE_CODE_PTR
4259 || a
->code () == TYPE_CODE_REF
)
4260 return types_equal (TYPE_TARGET_TYPE (a
),
4261 TYPE_TARGET_TYPE (b
));
4263 /* Well, damnit, if the names are exactly the same, I'll say they
4264 are exactly the same. This happens when we generate method
4265 stubs. The types won't point to the same address, but they
4266 really are the same. */
4268 if (a
->name () && b
->name ()
4269 && strcmp (a
->name (), b
->name ()) == 0)
4272 /* Two function types are equal if their argument and return types
4274 if (a
->code () == TYPE_CODE_FUNC
)
4278 if (a
->num_fields () != b
->num_fields ())
4281 if (!types_equal (TYPE_TARGET_TYPE (a
), TYPE_TARGET_TYPE (b
)))
4284 for (i
= 0; i
< a
->num_fields (); ++i
)
4285 if (!types_equal (a
->field (i
).type (), b
->field (i
).type ()))
4294 /* Deep comparison of types. */
4296 /* An entry in the type-equality bcache. */
4298 struct type_equality_entry
4300 type_equality_entry (struct type
*t1
, struct type
*t2
)
4306 struct type
*type1
, *type2
;
4309 /* A helper function to compare two strings. Returns true if they are
4310 the same, false otherwise. Handles NULLs properly. */
4313 compare_maybe_null_strings (const char *s
, const char *t
)
4315 if (s
== NULL
|| t
== NULL
)
4317 return strcmp (s
, t
) == 0;
4320 /* A helper function for check_types_worklist that checks two types for
4321 "deep" equality. Returns true if the types are considered the
4322 same, false otherwise. */
4325 check_types_equal (struct type
*type1
, struct type
*type2
,
4326 std::vector
<type_equality_entry
> *worklist
)
4328 type1
= check_typedef (type1
);
4329 type2
= check_typedef (type2
);
4334 if (type1
->code () != type2
->code ()
4335 || TYPE_LENGTH (type1
) != TYPE_LENGTH (type2
)
4336 || type1
->is_unsigned () != type2
->is_unsigned ()
4337 || type1
->has_no_signedness () != type2
->has_no_signedness ()
4338 || type1
->endianity_is_not_default () != type2
->endianity_is_not_default ()
4339 || type1
->has_varargs () != type2
->has_varargs ()
4340 || type1
->is_vector () != type2
->is_vector ()
4341 || TYPE_NOTTEXT (type1
) != TYPE_NOTTEXT (type2
)
4342 || type1
->instance_flags () != type2
->instance_flags ()
4343 || type1
->num_fields () != type2
->num_fields ())
4346 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4348 if (!compare_maybe_null_strings (type1
->name (), type2
->name ()))
4351 if (type1
->code () == TYPE_CODE_RANGE
)
4353 if (*type1
->bounds () != *type2
->bounds ())
4360 for (i
= 0; i
< type1
->num_fields (); ++i
)
4362 const struct field
*field1
= &type1
->field (i
);
4363 const struct field
*field2
= &type2
->field (i
);
4365 if (FIELD_ARTIFICIAL (*field1
) != FIELD_ARTIFICIAL (*field2
)
4366 || FIELD_BITSIZE (*field1
) != FIELD_BITSIZE (*field2
)
4367 || field1
->loc_kind () != field2
->loc_kind ())
4369 if (!compare_maybe_null_strings (field1
->name (), field2
->name ()))
4371 switch (field1
->loc_kind ())
4373 case FIELD_LOC_KIND_BITPOS
:
4374 if (field1
->loc_bitpos () != field2
->loc_bitpos ())
4377 case FIELD_LOC_KIND_ENUMVAL
:
4378 if (field1
->loc_enumval () != field2
->loc_enumval ())
4380 /* Don't compare types of enum fields, because they don't
4383 case FIELD_LOC_KIND_PHYSADDR
:
4384 if (field1
->loc_physaddr () != field2
->loc_physaddr ())
4387 case FIELD_LOC_KIND_PHYSNAME
:
4388 if (!compare_maybe_null_strings (field1
->loc_physname (),
4389 field2
->loc_physname ()))
4392 case FIELD_LOC_KIND_DWARF_BLOCK
:
4394 struct dwarf2_locexpr_baton
*block1
, *block2
;
4396 block1
= field1
->loc_dwarf_block ();
4397 block2
= field2
->loc_dwarf_block ();
4398 if (block1
->per_cu
!= block2
->per_cu
4399 || block1
->size
!= block2
->size
4400 || memcmp (block1
->data
, block2
->data
, block1
->size
) != 0)
4405 internal_error (__FILE__
, __LINE__
, _("Unsupported field kind "
4406 "%d by check_types_equal"),
4407 field1
->loc_kind ());
4410 worklist
->emplace_back (field1
->type (), field2
->type ());
4414 if (TYPE_TARGET_TYPE (type1
) != NULL
)
4416 if (TYPE_TARGET_TYPE (type2
) == NULL
)
4419 worklist
->emplace_back (TYPE_TARGET_TYPE (type1
),
4420 TYPE_TARGET_TYPE (type2
));
4422 else if (TYPE_TARGET_TYPE (type2
) != NULL
)
4428 /* Check types on a worklist for equality. Returns false if any pair
4429 is not equal, true if they are all considered equal. */
4432 check_types_worklist (std::vector
<type_equality_entry
> *worklist
,
4435 while (!worklist
->empty ())
4439 struct type_equality_entry entry
= std::move (worklist
->back ());
4440 worklist
->pop_back ();
4442 /* If the type pair has already been visited, we know it is
4444 cache
->insert (&entry
, sizeof (entry
), &added
);
4448 if (!check_types_equal (entry
.type1
, entry
.type2
, worklist
))
4455 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4456 "deep comparison". Otherwise return false. */
4459 types_deeply_equal (struct type
*type1
, struct type
*type2
)
4461 std::vector
<type_equality_entry
> worklist
;
4463 gdb_assert (type1
!= NULL
&& type2
!= NULL
);
4465 /* Early exit for the simple case. */
4470 worklist
.emplace_back (type1
, type2
);
4471 return check_types_worklist (&worklist
, &cache
);
4474 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4475 Otherwise return one. */
4478 type_not_allocated (const struct type
*type
)
4480 struct dynamic_prop
*prop
= TYPE_ALLOCATED_PROP (type
);
4482 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4483 && prop
->const_val () == 0);
4486 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4487 Otherwise return one. */
4490 type_not_associated (const struct type
*type
)
4492 struct dynamic_prop
*prop
= TYPE_ASSOCIATED_PROP (type
);
4494 return (prop
!= nullptr && prop
->kind () == PROP_CONST
4495 && prop
->const_val () == 0);
4498 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4501 rank_one_type_parm_ptr (struct type
*parm
, struct type
*arg
, struct value
*value
)
4503 struct rank rank
= {0,0};
4505 switch (arg
->code ())
4509 /* Allowed pointer conversions are:
4510 (a) pointer to void-pointer conversion. */
4511 if (TYPE_TARGET_TYPE (parm
)->code () == TYPE_CODE_VOID
)
4512 return VOID_PTR_CONVERSION_BADNESS
;
4514 /* (b) pointer to ancestor-pointer conversion. */
4515 rank
.subrank
= distance_to_ancestor (TYPE_TARGET_TYPE (parm
),
4516 TYPE_TARGET_TYPE (arg
),
4518 if (rank
.subrank
>= 0)
4519 return sum_ranks (BASE_PTR_CONVERSION_BADNESS
, rank
);
4521 return INCOMPATIBLE_TYPE_BADNESS
;
4522 case TYPE_CODE_ARRAY
:
4524 struct type
*t1
= TYPE_TARGET_TYPE (parm
);
4525 struct type
*t2
= TYPE_TARGET_TYPE (arg
);
4527 if (types_equal (t1
, t2
))
4529 /* Make sure they are CV equal. */
4530 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4531 rank
.subrank
|= CV_CONVERSION_CONST
;
4532 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4533 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4534 if (rank
.subrank
!= 0)
4535 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4536 return EXACT_MATCH_BADNESS
;
4538 return INCOMPATIBLE_TYPE_BADNESS
;
4540 case TYPE_CODE_FUNC
:
4541 return rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
);
4543 if (value
!= NULL
&& value_type (value
)->code () == TYPE_CODE_INT
)
4545 if (value_as_long (value
) == 0)
4547 /* Null pointer conversion: allow it to be cast to a pointer.
4548 [4.10.1 of C++ standard draft n3290] */
4549 return NULL_POINTER_CONVERSION_BADNESS
;
4553 /* If type checking is disabled, allow the conversion. */
4554 if (!strict_type_checking
)
4555 return NS_INTEGER_POINTER_CONVERSION_BADNESS
;
4559 case TYPE_CODE_ENUM
:
4560 case TYPE_CODE_FLAGS
:
4561 case TYPE_CODE_CHAR
:
4562 case TYPE_CODE_RANGE
:
4563 case TYPE_CODE_BOOL
:
4565 return INCOMPATIBLE_TYPE_BADNESS
;
4569 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4572 rank_one_type_parm_array (struct type
*parm
, struct type
*arg
, struct value
*value
)
4574 switch (arg
->code ())
4577 case TYPE_CODE_ARRAY
:
4578 return rank_one_type (TYPE_TARGET_TYPE (parm
),
4579 TYPE_TARGET_TYPE (arg
), NULL
);
4581 return INCOMPATIBLE_TYPE_BADNESS
;
4585 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4588 rank_one_type_parm_func (struct type
*parm
, struct type
*arg
, struct value
*value
)
4590 switch (arg
->code ())
4592 case TYPE_CODE_PTR
: /* funcptr -> func */
4593 return rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
);
4595 return INCOMPATIBLE_TYPE_BADNESS
;
4599 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4602 rank_one_type_parm_int (struct type
*parm
, struct type
*arg
, struct value
*value
)
4604 switch (arg
->code ())
4607 if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4609 /* Deal with signed, unsigned, and plain chars and
4610 signed and unsigned ints. */
4611 if (parm
->has_no_signedness ())
4613 /* This case only for character types. */
4614 if (arg
->has_no_signedness ())
4615 return EXACT_MATCH_BADNESS
; /* plain char -> plain char */
4616 else /* signed/unsigned char -> plain char */
4617 return INTEGER_CONVERSION_BADNESS
;
4619 else if (parm
->is_unsigned ())
4621 if (arg
->is_unsigned ())
4623 /* unsigned int -> unsigned int, or
4624 unsigned long -> unsigned long */
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 /* unsigned int -> unsigned long */
4633 return INTEGER_PROMOTION_BADNESS
;
4635 /* unsigned long -> unsigned int */
4636 return INTEGER_CONVERSION_BADNESS
;
4640 if (integer_types_same_name_p (arg
->name (),
4642 && integer_types_same_name_p (parm
->name (),
4644 /* signed long -> unsigned int */
4645 return INTEGER_CONVERSION_BADNESS
;
4647 /* signed int/long -> unsigned int/long */
4648 return INTEGER_CONVERSION_BADNESS
;
4651 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4653 if (integer_types_same_name_p (parm
->name (),
4655 return EXACT_MATCH_BADNESS
;
4656 else if (integer_types_same_name_p (arg
->name (),
4658 && integer_types_same_name_p (parm
->name (),
4660 return INTEGER_PROMOTION_BADNESS
;
4662 return INTEGER_CONVERSION_BADNESS
;
4665 return INTEGER_CONVERSION_BADNESS
;
4667 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4668 return INTEGER_PROMOTION_BADNESS
;
4670 return INTEGER_CONVERSION_BADNESS
;
4671 case TYPE_CODE_ENUM
:
4672 case TYPE_CODE_FLAGS
:
4673 case TYPE_CODE_CHAR
:
4674 case TYPE_CODE_RANGE
:
4675 case TYPE_CODE_BOOL
:
4676 if (arg
->is_declared_class ())
4677 return INCOMPATIBLE_TYPE_BADNESS
;
4678 return INTEGER_PROMOTION_BADNESS
;
4680 return INT_FLOAT_CONVERSION_BADNESS
;
4682 return NS_POINTER_CONVERSION_BADNESS
;
4684 return INCOMPATIBLE_TYPE_BADNESS
;
4688 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4691 rank_one_type_parm_enum (struct type
*parm
, struct type
*arg
, struct value
*value
)
4693 switch (arg
->code ())
4696 case TYPE_CODE_CHAR
:
4697 case TYPE_CODE_RANGE
:
4698 case TYPE_CODE_BOOL
:
4699 case TYPE_CODE_ENUM
:
4700 if (parm
->is_declared_class () || arg
->is_declared_class ())
4701 return INCOMPATIBLE_TYPE_BADNESS
;
4702 return INTEGER_CONVERSION_BADNESS
;
4704 return INT_FLOAT_CONVERSION_BADNESS
;
4706 return INCOMPATIBLE_TYPE_BADNESS
;
4710 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4713 rank_one_type_parm_char (struct type
*parm
, struct type
*arg
, struct value
*value
)
4715 switch (arg
->code ())
4717 case TYPE_CODE_RANGE
:
4718 case TYPE_CODE_BOOL
:
4719 case TYPE_CODE_ENUM
:
4720 if (arg
->is_declared_class ())
4721 return INCOMPATIBLE_TYPE_BADNESS
;
4722 return INTEGER_CONVERSION_BADNESS
;
4724 return INT_FLOAT_CONVERSION_BADNESS
;
4726 if (TYPE_LENGTH (arg
) > TYPE_LENGTH (parm
))
4727 return INTEGER_CONVERSION_BADNESS
;
4728 else if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4729 return INTEGER_PROMOTION_BADNESS
;
4731 case TYPE_CODE_CHAR
:
4732 /* Deal with signed, unsigned, and plain chars for C++ and
4733 with int cases falling through from previous case. */
4734 if (parm
->has_no_signedness ())
4736 if (arg
->has_no_signedness ())
4737 return EXACT_MATCH_BADNESS
;
4739 return INTEGER_CONVERSION_BADNESS
;
4741 else if (parm
->is_unsigned ())
4743 if (arg
->is_unsigned ())
4744 return EXACT_MATCH_BADNESS
;
4746 return INTEGER_PROMOTION_BADNESS
;
4748 else if (!arg
->has_no_signedness () && !arg
->is_unsigned ())
4749 return EXACT_MATCH_BADNESS
;
4751 return INTEGER_CONVERSION_BADNESS
;
4753 return INCOMPATIBLE_TYPE_BADNESS
;
4757 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4760 rank_one_type_parm_range (struct type
*parm
, struct type
*arg
, struct value
*value
)
4762 switch (arg
->code ())
4765 case TYPE_CODE_CHAR
:
4766 case TYPE_CODE_RANGE
:
4767 case TYPE_CODE_BOOL
:
4768 case TYPE_CODE_ENUM
:
4769 return INTEGER_CONVERSION_BADNESS
;
4771 return INT_FLOAT_CONVERSION_BADNESS
;
4773 return INCOMPATIBLE_TYPE_BADNESS
;
4777 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4780 rank_one_type_parm_bool (struct type
*parm
, struct type
*arg
, struct value
*value
)
4782 switch (arg
->code ())
4784 /* n3290 draft, section 4.12.1 (conv.bool):
4786 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4787 pointer to member type can be converted to a prvalue of type
4788 bool. A zero value, null pointer value, or null member pointer
4789 value is converted to false; any other value is converted to
4790 true. A prvalue of type std::nullptr_t can be converted to a
4791 prvalue of type bool; the resulting value is false." */
4793 case TYPE_CODE_CHAR
:
4794 case TYPE_CODE_ENUM
:
4796 case TYPE_CODE_MEMBERPTR
:
4798 return BOOL_CONVERSION_BADNESS
;
4799 case TYPE_CODE_RANGE
:
4800 return INCOMPATIBLE_TYPE_BADNESS
;
4801 case TYPE_CODE_BOOL
:
4802 return EXACT_MATCH_BADNESS
;
4804 return INCOMPATIBLE_TYPE_BADNESS
;
4808 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4811 rank_one_type_parm_float (struct type
*parm
, struct type
*arg
, struct value
*value
)
4813 switch (arg
->code ())
4816 if (TYPE_LENGTH (arg
) < TYPE_LENGTH (parm
))
4817 return FLOAT_PROMOTION_BADNESS
;
4818 else if (TYPE_LENGTH (arg
) == TYPE_LENGTH (parm
))
4819 return EXACT_MATCH_BADNESS
;
4821 return FLOAT_CONVERSION_BADNESS
;
4823 case TYPE_CODE_BOOL
:
4824 case TYPE_CODE_ENUM
:
4825 case TYPE_CODE_RANGE
:
4826 case TYPE_CODE_CHAR
:
4827 return INT_FLOAT_CONVERSION_BADNESS
;
4829 return INCOMPATIBLE_TYPE_BADNESS
;
4833 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4836 rank_one_type_parm_complex (struct type
*parm
, struct type
*arg
, struct value
*value
)
4838 switch (arg
->code ())
4839 { /* Strictly not needed for C++, but... */
4841 return FLOAT_PROMOTION_BADNESS
;
4842 case TYPE_CODE_COMPLEX
:
4843 return EXACT_MATCH_BADNESS
;
4845 return INCOMPATIBLE_TYPE_BADNESS
;
4849 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4852 rank_one_type_parm_struct (struct type
*parm
, struct type
*arg
, struct value
*value
)
4854 struct rank rank
= {0, 0};
4856 switch (arg
->code ())
4858 case TYPE_CODE_STRUCT
:
4859 /* Check for derivation */
4860 rank
.subrank
= distance_to_ancestor (parm
, arg
, 0);
4861 if (rank
.subrank
>= 0)
4862 return sum_ranks (BASE_CONVERSION_BADNESS
, rank
);
4865 return INCOMPATIBLE_TYPE_BADNESS
;
4869 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4872 rank_one_type_parm_set (struct type
*parm
, struct type
*arg
, struct value
*value
)
4874 switch (arg
->code ())
4878 return rank_one_type (parm
->field (0).type (),
4879 arg
->field (0).type (), NULL
);
4881 return INCOMPATIBLE_TYPE_BADNESS
;
4885 /* Compare one type (PARM) for compatibility with another (ARG).
4886 * PARM is intended to be the parameter type of a function; and
4887 * ARG is the supplied argument's type. This function tests if
4888 * the latter can be converted to the former.
4889 * VALUE is the argument's value or NULL if none (or called recursively)
4891 * Return 0 if they are identical types;
4892 * Otherwise, return an integer which corresponds to how compatible
4893 * PARM is to ARG. The higher the return value, the worse the match.
4894 * Generally the "bad" conversions are all uniformly assigned
4895 * INVALID_CONVERSION. */
4898 rank_one_type (struct type
*parm
, struct type
*arg
, struct value
*value
)
4900 struct rank rank
= {0,0};
4902 /* Resolve typedefs */
4903 if (parm
->code () == TYPE_CODE_TYPEDEF
)
4904 parm
= check_typedef (parm
);
4905 if (arg
->code () == TYPE_CODE_TYPEDEF
)
4906 arg
= check_typedef (arg
);
4908 if (TYPE_IS_REFERENCE (parm
) && value
!= NULL
)
4910 if (VALUE_LVAL (value
) == not_lval
)
4912 /* Rvalues should preferably bind to rvalue references or const
4913 lvalue references. */
4914 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4915 rank
.subrank
= REFERENCE_CONVERSION_RVALUE
;
4916 else if (TYPE_CONST (TYPE_TARGET_TYPE (parm
)))
4917 rank
.subrank
= REFERENCE_CONVERSION_CONST_LVALUE
;
4919 return INCOMPATIBLE_TYPE_BADNESS
;
4920 return sum_ranks (rank
, REFERENCE_CONVERSION_BADNESS
);
4924 /* It's illegal to pass an lvalue as an rvalue. */
4925 if (parm
->code () == TYPE_CODE_RVALUE_REF
)
4926 return INCOMPATIBLE_TYPE_BADNESS
;
4930 if (types_equal (parm
, arg
))
4932 struct type
*t1
= parm
;
4933 struct type
*t2
= arg
;
4935 /* For pointers and references, compare target type. */
4936 if (parm
->is_pointer_or_reference ())
4938 t1
= TYPE_TARGET_TYPE (parm
);
4939 t2
= TYPE_TARGET_TYPE (arg
);
4942 /* Make sure they are CV equal, too. */
4943 if (TYPE_CONST (t1
) != TYPE_CONST (t2
))
4944 rank
.subrank
|= CV_CONVERSION_CONST
;
4945 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
4946 rank
.subrank
|= CV_CONVERSION_VOLATILE
;
4947 if (rank
.subrank
!= 0)
4948 return sum_ranks (CV_CONVERSION_BADNESS
, rank
);
4949 return EXACT_MATCH_BADNESS
;
4952 /* See through references, since we can almost make non-references
4955 if (TYPE_IS_REFERENCE (arg
))
4956 return (sum_ranks (rank_one_type (parm
, TYPE_TARGET_TYPE (arg
), NULL
),
4957 REFERENCE_SEE_THROUGH_BADNESS
));
4958 if (TYPE_IS_REFERENCE (parm
))
4959 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm
), arg
, NULL
),
4960 REFERENCE_SEE_THROUGH_BADNESS
));
4963 /* Debugging only. */
4964 gdb_printf (gdb_stderr
,
4965 "------ Arg is %s [%d], parm is %s [%d]\n",
4966 arg
->name (), arg
->code (),
4967 parm
->name (), parm
->code ());
4970 /* x -> y means arg of type x being supplied for parameter of type y. */
4972 switch (parm
->code ())
4975 return rank_one_type_parm_ptr (parm
, arg
, value
);
4976 case TYPE_CODE_ARRAY
:
4977 return rank_one_type_parm_array (parm
, arg
, value
);
4978 case TYPE_CODE_FUNC
:
4979 return rank_one_type_parm_func (parm
, arg
, value
);
4981 return rank_one_type_parm_int (parm
, arg
, value
);
4982 case TYPE_CODE_ENUM
:
4983 return rank_one_type_parm_enum (parm
, arg
, value
);
4984 case TYPE_CODE_CHAR
:
4985 return rank_one_type_parm_char (parm
, arg
, value
);
4986 case TYPE_CODE_RANGE
:
4987 return rank_one_type_parm_range (parm
, arg
, value
);
4988 case TYPE_CODE_BOOL
:
4989 return rank_one_type_parm_bool (parm
, arg
, value
);
4991 return rank_one_type_parm_float (parm
, arg
, value
);
4992 case TYPE_CODE_COMPLEX
:
4993 return rank_one_type_parm_complex (parm
, arg
, value
);
4994 case TYPE_CODE_STRUCT
:
4995 return rank_one_type_parm_struct (parm
, arg
, value
);
4997 return rank_one_type_parm_set (parm
, arg
, value
);
4999 return INCOMPATIBLE_TYPE_BADNESS
;
5000 } /* switch (arg->code ()) */
5003 /* End of functions for overload resolution. */
5005 /* Routines to pretty-print types. */
5008 print_bit_vector (B_TYPE
*bits
, int nbits
)
5012 for (bitno
= 0; bitno
< nbits
; bitno
++)
5014 if ((bitno
% 8) == 0)
5018 if (B_TST (bits
, bitno
))
5025 /* Note the first arg should be the "this" pointer, we may not want to
5026 include it since we may get into a infinitely recursive
5030 print_args (struct field
*args
, int nargs
, int spaces
)
5036 for (i
= 0; i
< nargs
; i
++)
5039 ("%*s[%d] name '%s'\n", spaces
, "", i
,
5040 args
[i
].name () != NULL
? args
[i
].name () : "<NULL>");
5041 recursive_dump_type (args
[i
].type (), spaces
+ 2);
5047 field_is_static (struct field
*f
)
5049 /* "static" fields are the fields whose location is not relative
5050 to the address of the enclosing struct. It would be nice to
5051 have a dedicated flag that would be set for static fields when
5052 the type is being created. But in practice, checking the field
5053 loc_kind should give us an accurate answer. */
5054 return (f
->loc_kind () == FIELD_LOC_KIND_PHYSNAME
5055 || f
->loc_kind () == FIELD_LOC_KIND_PHYSADDR
);
5059 dump_fn_fieldlists (struct type
*type
, int spaces
)
5065 gdb_printf ("%*sfn_fieldlists %s\n", spaces
, "",
5066 host_address_to_string (TYPE_FN_FIELDLISTS (type
)));
5067 for (method_idx
= 0; method_idx
< TYPE_NFN_FIELDS (type
); method_idx
++)
5069 f
= TYPE_FN_FIELDLIST1 (type
, method_idx
);
5071 ("%*s[%d] name '%s' (%s) length %d\n", spaces
+ 2, "",
5073 TYPE_FN_FIELDLIST_NAME (type
, method_idx
),
5074 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type
, method_idx
)),
5075 TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
));
5076 for (overload_idx
= 0;
5077 overload_idx
< TYPE_FN_FIELDLIST_LENGTH (type
, method_idx
);
5081 ("%*s[%d] physname '%s' (%s)\n",
5082 spaces
+ 4, "", overload_idx
,
5083 TYPE_FN_FIELD_PHYSNAME (f
, overload_idx
),
5084 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f
,
5087 ("%*stype %s\n", spaces
+ 8, "",
5088 host_address_to_string (TYPE_FN_FIELD_TYPE (f
, overload_idx
)));
5090 recursive_dump_type (TYPE_FN_FIELD_TYPE (f
, overload_idx
),
5094 ("%*sargs %s\n", spaces
+ 8, "",
5095 host_address_to_string (TYPE_FN_FIELD_ARGS (f
, overload_idx
)));
5096 print_args (TYPE_FN_FIELD_ARGS (f
, overload_idx
),
5097 TYPE_FN_FIELD_TYPE (f
, overload_idx
)->num_fields (),
5100 ("%*sfcontext %s\n", spaces
+ 8, "",
5101 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f
,
5104 gdb_printf ("%*sis_const %d\n", spaces
+ 8, "",
5105 TYPE_FN_FIELD_CONST (f
, overload_idx
));
5106 gdb_printf ("%*sis_volatile %d\n", spaces
+ 8, "",
5107 TYPE_FN_FIELD_VOLATILE (f
, overload_idx
));
5108 gdb_printf ("%*sis_private %d\n", spaces
+ 8, "",
5109 TYPE_FN_FIELD_PRIVATE (f
, overload_idx
));
5110 gdb_printf ("%*sis_protected %d\n", spaces
+ 8, "",
5111 TYPE_FN_FIELD_PROTECTED (f
, overload_idx
));
5112 gdb_printf ("%*sis_stub %d\n", spaces
+ 8, "",
5113 TYPE_FN_FIELD_STUB (f
, overload_idx
));
5114 gdb_printf ("%*sdefaulted %d\n", spaces
+ 8, "",
5115 TYPE_FN_FIELD_DEFAULTED (f
, overload_idx
));
5116 gdb_printf ("%*sis_deleted %d\n", spaces
+ 8, "",
5117 TYPE_FN_FIELD_DELETED (f
, overload_idx
));
5118 gdb_printf ("%*svoffset %u\n", spaces
+ 8, "",
5119 TYPE_FN_FIELD_VOFFSET (f
, overload_idx
));
5125 print_cplus_stuff (struct type
*type
, int spaces
)
5127 gdb_printf ("%*svptr_fieldno %d\n", spaces
, "",
5128 TYPE_VPTR_FIELDNO (type
));
5129 gdb_printf ("%*svptr_basetype %s\n", spaces
, "",
5130 host_address_to_string (TYPE_VPTR_BASETYPE (type
)));
5131 if (TYPE_VPTR_BASETYPE (type
) != NULL
)
5132 recursive_dump_type (TYPE_VPTR_BASETYPE (type
), spaces
+ 2);
5134 gdb_printf ("%*sn_baseclasses %d\n", spaces
, "",
5135 TYPE_N_BASECLASSES (type
));
5136 gdb_printf ("%*snfn_fields %d\n", spaces
, "",
5137 TYPE_NFN_FIELDS (type
));
5138 if (TYPE_N_BASECLASSES (type
) > 0)
5141 ("%*svirtual_field_bits (%d bits at *%s)",
5142 spaces
, "", TYPE_N_BASECLASSES (type
),
5143 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type
)));
5145 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type
),
5146 TYPE_N_BASECLASSES (type
));
5149 if (type
->num_fields () > 0)
5151 if (TYPE_FIELD_PRIVATE_BITS (type
) != NULL
)
5154 ("%*sprivate_field_bits (%d bits at *%s)",
5155 spaces
, "", type
->num_fields (),
5156 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type
)));
5157 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type
),
5158 type
->num_fields ());
5161 if (TYPE_FIELD_PROTECTED_BITS (type
) != NULL
)
5164 ("%*sprotected_field_bits (%d bits at *%s",
5165 spaces
, "", type
->num_fields (),
5166 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type
)));
5167 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type
),
5168 type
->num_fields ());
5172 if (TYPE_NFN_FIELDS (type
) > 0)
5174 dump_fn_fieldlists (type
, spaces
);
5177 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5178 TYPE_CPLUS_CALLING_CONVENTION (type
));
5181 /* Print the contents of the TYPE's type_specific union, assuming that
5182 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5185 print_gnat_stuff (struct type
*type
, int spaces
)
5187 struct type
*descriptive_type
= TYPE_DESCRIPTIVE_TYPE (type
);
5189 if (descriptive_type
== NULL
)
5190 gdb_printf ("%*sno descriptive type\n", spaces
+ 2, "");
5193 gdb_printf ("%*sdescriptive type\n", spaces
+ 2, "");
5194 recursive_dump_type (descriptive_type
, spaces
+ 4);
5198 /* Print the contents of the TYPE's type_specific union, assuming that
5199 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5202 print_fixed_point_type_info (struct type
*type
, int spaces
)
5204 gdb_printf ("%*sscaling factor: %s\n", spaces
+ 2, "",
5205 type
->fixed_point_scaling_factor ().str ().c_str ());
5208 static struct obstack dont_print_type_obstack
;
5210 /* Print the dynamic_prop PROP. */
5213 dump_dynamic_prop (dynamic_prop
const& prop
)
5215 switch (prop
.kind ())
5218 gdb_printf ("%s", plongest (prop
.const_val ()));
5220 case PROP_UNDEFINED
:
5221 gdb_printf ("(undefined)");
5225 gdb_printf ("(dynamic)");
5228 gdb_assert_not_reached ("unhandled prop kind");
5234 recursive_dump_type (struct type
*type
, int spaces
)
5239 obstack_begin (&dont_print_type_obstack
, 0);
5241 if (type
->num_fields () > 0
5242 || (HAVE_CPLUS_STRUCT (type
) && TYPE_NFN_FIELDS (type
) > 0))
5244 struct type
**first_dont_print
5245 = (struct type
**) obstack_base (&dont_print_type_obstack
);
5247 int i
= (struct type
**)
5248 obstack_next_free (&dont_print_type_obstack
) - first_dont_print
;
5252 if (type
== first_dont_print
[i
])
5254 gdb_printf ("%*stype node %s", spaces
, "",
5255 host_address_to_string (type
));
5256 gdb_printf (_(" <same as already seen type>\n"));
5261 obstack_ptr_grow (&dont_print_type_obstack
, type
);
5264 gdb_printf ("%*stype node %s\n", spaces
, "",
5265 host_address_to_string (type
));
5266 gdb_printf ("%*sname '%s' (%s)\n", spaces
, "",
5267 type
->name () ? type
->name () : "<NULL>",
5268 host_address_to_string (type
->name ()));
5269 gdb_printf ("%*scode 0x%x ", spaces
, "", type
->code ());
5270 switch (type
->code ())
5272 case TYPE_CODE_UNDEF
:
5273 gdb_printf ("(TYPE_CODE_UNDEF)");
5276 gdb_printf ("(TYPE_CODE_PTR)");
5278 case TYPE_CODE_ARRAY
:
5279 gdb_printf ("(TYPE_CODE_ARRAY)");
5281 case TYPE_CODE_STRUCT
:
5282 gdb_printf ("(TYPE_CODE_STRUCT)");
5284 case TYPE_CODE_UNION
:
5285 gdb_printf ("(TYPE_CODE_UNION)");
5287 case TYPE_CODE_ENUM
:
5288 gdb_printf ("(TYPE_CODE_ENUM)");
5290 case TYPE_CODE_FLAGS
:
5291 gdb_printf ("(TYPE_CODE_FLAGS)");
5293 case TYPE_CODE_FUNC
:
5294 gdb_printf ("(TYPE_CODE_FUNC)");
5297 gdb_printf ("(TYPE_CODE_INT)");
5300 gdb_printf ("(TYPE_CODE_FLT)");
5302 case TYPE_CODE_VOID
:
5303 gdb_printf ("(TYPE_CODE_VOID)");
5306 gdb_printf ("(TYPE_CODE_SET)");
5308 case TYPE_CODE_RANGE
:
5309 gdb_printf ("(TYPE_CODE_RANGE)");
5311 case TYPE_CODE_STRING
:
5312 gdb_printf ("(TYPE_CODE_STRING)");
5314 case TYPE_CODE_ERROR
:
5315 gdb_printf ("(TYPE_CODE_ERROR)");
5317 case TYPE_CODE_MEMBERPTR
:
5318 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5320 case TYPE_CODE_METHODPTR
:
5321 gdb_printf ("(TYPE_CODE_METHODPTR)");
5323 case TYPE_CODE_METHOD
:
5324 gdb_printf ("(TYPE_CODE_METHOD)");
5327 gdb_printf ("(TYPE_CODE_REF)");
5329 case TYPE_CODE_CHAR
:
5330 gdb_printf ("(TYPE_CODE_CHAR)");
5332 case TYPE_CODE_BOOL
:
5333 gdb_printf ("(TYPE_CODE_BOOL)");
5335 case TYPE_CODE_COMPLEX
:
5336 gdb_printf ("(TYPE_CODE_COMPLEX)");
5338 case TYPE_CODE_TYPEDEF
:
5339 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5341 case TYPE_CODE_NAMESPACE
:
5342 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5344 case TYPE_CODE_FIXED_POINT
:
5345 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5348 gdb_printf ("(UNKNOWN TYPE CODE)");
5352 gdb_printf ("%*slength %s\n", spaces
, "",
5353 pulongest (TYPE_LENGTH (type
)));
5354 if (type
->is_objfile_owned ())
5355 gdb_printf ("%*sobjfile %s\n", spaces
, "",
5356 host_address_to_string (type
->objfile_owner ()));
5358 gdb_printf ("%*sgdbarch %s\n", spaces
, "",
5359 host_address_to_string (type
->arch_owner ()));
5360 gdb_printf ("%*starget_type %s\n", spaces
, "",
5361 host_address_to_string (TYPE_TARGET_TYPE (type
)));
5362 if (TYPE_TARGET_TYPE (type
) != NULL
)
5364 recursive_dump_type (TYPE_TARGET_TYPE (type
), spaces
+ 2);
5366 gdb_printf ("%*spointer_type %s\n", spaces
, "",
5367 host_address_to_string (TYPE_POINTER_TYPE (type
)));
5368 gdb_printf ("%*sreference_type %s\n", spaces
, "",
5369 host_address_to_string (TYPE_REFERENCE_TYPE (type
)));
5370 gdb_printf ("%*stype_chain %s\n", spaces
, "",
5371 host_address_to_string (TYPE_CHAIN (type
)));
5372 gdb_printf ("%*sinstance_flags 0x%x", spaces
, "",
5373 (unsigned) type
->instance_flags ());
5374 if (TYPE_CONST (type
))
5376 gdb_puts (" TYPE_CONST");
5378 if (TYPE_VOLATILE (type
))
5380 gdb_puts (" TYPE_VOLATILE");
5382 if (TYPE_CODE_SPACE (type
))
5384 gdb_puts (" TYPE_CODE_SPACE");
5386 if (TYPE_DATA_SPACE (type
))
5388 gdb_puts (" TYPE_DATA_SPACE");
5390 if (TYPE_ADDRESS_CLASS_1 (type
))
5392 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5394 if (TYPE_ADDRESS_CLASS_2 (type
))
5396 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5398 if (TYPE_RESTRICT (type
))
5400 gdb_puts (" TYPE_RESTRICT");
5402 if (TYPE_ATOMIC (type
))
5404 gdb_puts (" TYPE_ATOMIC");
5408 gdb_printf ("%*sflags", spaces
, "");
5409 if (type
->is_unsigned ())
5411 gdb_puts (" TYPE_UNSIGNED");
5413 if (type
->has_no_signedness ())
5415 gdb_puts (" TYPE_NOSIGN");
5417 if (type
->endianity_is_not_default ())
5419 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5421 if (type
->is_stub ())
5423 gdb_puts (" TYPE_STUB");
5425 if (type
->target_is_stub ())
5427 gdb_puts (" TYPE_TARGET_STUB");
5429 if (type
->is_prototyped ())
5431 gdb_puts (" TYPE_PROTOTYPED");
5433 if (type
->has_varargs ())
5435 gdb_puts (" TYPE_VARARGS");
5437 /* This is used for things like AltiVec registers on ppc. Gcc emits
5438 an attribute for the array type, which tells whether or not we
5439 have a vector, instead of a regular array. */
5440 if (type
->is_vector ())
5442 gdb_puts (" TYPE_VECTOR");
5444 if (type
->is_fixed_instance ())
5446 gdb_puts (" TYPE_FIXED_INSTANCE");
5448 if (type
->stub_is_supported ())
5450 gdb_puts (" TYPE_STUB_SUPPORTED");
5452 if (TYPE_NOTTEXT (type
))
5454 gdb_puts (" TYPE_NOTTEXT");
5457 gdb_printf ("%*snfields %d ", spaces
, "", type
->num_fields ());
5458 if (TYPE_ASSOCIATED_PROP (type
) != nullptr
5459 || TYPE_ALLOCATED_PROP (type
) != nullptr)
5461 gdb_printf ("%*s", spaces
, "");
5462 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5464 gdb_printf ("associated ");
5465 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type
));
5467 if (TYPE_ALLOCATED_PROP (type
) != nullptr)
5469 if (TYPE_ASSOCIATED_PROP (type
) != nullptr)
5471 gdb_printf ("allocated ");
5472 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type
));
5476 gdb_printf ("%s\n", host_address_to_string (type
->fields ()));
5477 for (idx
= 0; idx
< type
->num_fields (); idx
++)
5479 if (type
->code () == TYPE_CODE_ENUM
)
5480 gdb_printf ("%*s[%d] enumval %s type ", spaces
+ 2, "",
5481 idx
, plongest (type
->field (idx
).loc_enumval ()));
5483 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces
+ 2, "",
5484 idx
, plongest (type
->field (idx
).loc_bitpos ()),
5485 TYPE_FIELD_BITSIZE (type
, idx
));
5486 gdb_printf ("%s name '%s' (%s)\n",
5487 host_address_to_string (type
->field (idx
).type ()),
5488 type
->field (idx
).name () != NULL
5489 ? type
->field (idx
).name ()
5491 host_address_to_string (type
->field (idx
).name ()));
5492 if (type
->field (idx
).type () != NULL
)
5494 recursive_dump_type (type
->field (idx
).type (), spaces
+ 4);
5497 if (type
->code () == TYPE_CODE_RANGE
)
5499 gdb_printf ("%*slow ", spaces
, "");
5500 dump_dynamic_prop (type
->bounds ()->low
);
5501 gdb_printf (" high ");
5502 dump_dynamic_prop (type
->bounds ()->high
);
5506 switch (TYPE_SPECIFIC_FIELD (type
))
5508 case TYPE_SPECIFIC_CPLUS_STUFF
:
5509 gdb_printf ("%*scplus_stuff %s\n", spaces
, "",
5510 host_address_to_string (TYPE_CPLUS_SPECIFIC (type
)));
5511 print_cplus_stuff (type
, spaces
);
5514 case TYPE_SPECIFIC_GNAT_STUFF
:
5515 gdb_printf ("%*sgnat_stuff %s\n", spaces
, "",
5516 host_address_to_string (TYPE_GNAT_SPECIFIC (type
)));
5517 print_gnat_stuff (type
, spaces
);
5520 case TYPE_SPECIFIC_FLOATFORMAT
:
5521 gdb_printf ("%*sfloatformat ", spaces
, "");
5522 if (TYPE_FLOATFORMAT (type
) == NULL
5523 || TYPE_FLOATFORMAT (type
)->name
== NULL
)
5524 gdb_puts ("(null)");
5526 gdb_puts (TYPE_FLOATFORMAT (type
)->name
);
5530 case TYPE_SPECIFIC_FUNC
:
5531 gdb_printf ("%*scalling_convention %d\n", spaces
, "",
5532 TYPE_CALLING_CONVENTION (type
));
5533 /* tail_call_list is not printed. */
5536 case TYPE_SPECIFIC_SELF_TYPE
:
5537 gdb_printf ("%*sself_type %s\n", spaces
, "",
5538 host_address_to_string (TYPE_SELF_TYPE (type
)));
5541 case TYPE_SPECIFIC_FIXED_POINT
:
5542 gdb_printf ("%*sfixed_point_info ", spaces
, "");
5543 print_fixed_point_type_info (type
, spaces
);
5547 case TYPE_SPECIFIC_INT
:
5548 if (type
->bit_size_differs_p ())
5550 unsigned bit_size
= type
->bit_size ();
5551 unsigned bit_off
= type
->bit_offset ();
5552 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces
, "",
5559 obstack_free (&dont_print_type_obstack
, NULL
);
5562 /* Trivial helpers for the libiberty hash table, for mapping one
5565 struct type_pair
: public allocate_on_obstack
5567 type_pair (struct type
*old_
, struct type
*newobj_
)
5568 : old (old_
), newobj (newobj_
)
5571 struct type
* const old
, * const newobj
;
5575 type_pair_hash (const void *item
)
5577 const struct type_pair
*pair
= (const struct type_pair
*) item
;
5579 return htab_hash_pointer (pair
->old
);
5583 type_pair_eq (const void *item_lhs
, const void *item_rhs
)
5585 const struct type_pair
*lhs
= (const struct type_pair
*) item_lhs
;
5586 const struct type_pair
*rhs
= (const struct type_pair
*) item_rhs
;
5588 return lhs
->old
== rhs
->old
;
5591 /* Allocate the hash table used by copy_type_recursive to walk
5592 types without duplicates. We use OBJFILE's obstack, because
5593 OBJFILE is about to be deleted. */
5596 create_copied_types_hash (struct objfile
*objfile
)
5598 return htab_up (htab_create_alloc_ex (1, type_pair_hash
, type_pair_eq
,
5599 NULL
, &objfile
->objfile_obstack
,
5600 hashtab_obstack_allocate
,
5601 dummy_obstack_deallocate
));
5604 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5606 static struct dynamic_prop_list
*
5607 copy_dynamic_prop_list (struct obstack
*objfile_obstack
,
5608 struct dynamic_prop_list
*list
)
5610 struct dynamic_prop_list
*copy
= list
;
5611 struct dynamic_prop_list
**node_ptr
= ©
;
5613 while (*node_ptr
!= NULL
)
5615 struct dynamic_prop_list
*node_copy
;
5617 node_copy
= ((struct dynamic_prop_list
*)
5618 obstack_copy (objfile_obstack
, *node_ptr
,
5619 sizeof (struct dynamic_prop_list
)));
5620 node_copy
->prop
= (*node_ptr
)->prop
;
5621 *node_ptr
= node_copy
;
5623 node_ptr
= &node_copy
->next
;
5629 /* Recursively copy (deep copy) TYPE, if it is associated with
5630 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5631 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5632 it is not associated with OBJFILE. */
5635 copy_type_recursive (struct objfile
*objfile
,
5637 htab_t copied_types
)
5640 struct type
*new_type
;
5642 if (!type
->is_objfile_owned ())
5645 /* This type shouldn't be pointing to any types in other objfiles;
5646 if it did, the type might disappear unexpectedly. */
5647 gdb_assert (type
->objfile_owner () == objfile
);
5649 struct type_pair
pair (type
, nullptr);
5651 slot
= htab_find_slot (copied_types
, &pair
, INSERT
);
5653 return ((struct type_pair
*) *slot
)->newobj
;
5655 new_type
= alloc_type_arch (type
->arch ());
5657 /* We must add the new type to the hash table immediately, in case
5658 we encounter this type again during a recursive call below. */
5659 struct type_pair
*stored
5660 = new (&objfile
->objfile_obstack
) struct type_pair (type
, new_type
);
5664 /* Copy the common fields of types. For the main type, we simply
5665 copy the entire thing and then update specific fields as needed. */
5666 *TYPE_MAIN_TYPE (new_type
) = *TYPE_MAIN_TYPE (type
);
5668 new_type
->set_owner (type
->arch ());
5671 new_type
->set_name (xstrdup (type
->name ()));
5673 new_type
->set_instance_flags (type
->instance_flags ());
5674 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5676 /* Copy the fields. */
5677 if (type
->num_fields ())
5681 nfields
= type
->num_fields ();
5682 new_type
->set_fields
5684 TYPE_ZALLOC (new_type
, nfields
* sizeof (struct field
)));
5686 for (i
= 0; i
< nfields
; i
++)
5688 TYPE_FIELD_ARTIFICIAL (new_type
, i
) =
5689 TYPE_FIELD_ARTIFICIAL (type
, i
);
5690 TYPE_FIELD_BITSIZE (new_type
, i
) = TYPE_FIELD_BITSIZE (type
, i
);
5691 if (type
->field (i
).type ())
5692 new_type
->field (i
).set_type
5693 (copy_type_recursive (objfile
, type
->field (i
).type (),
5695 if (type
->field (i
).name ())
5696 new_type
->field (i
).set_name (xstrdup (type
->field (i
).name ()));
5698 switch (type
->field (i
).loc_kind ())
5700 case FIELD_LOC_KIND_BITPOS
:
5701 new_type
->field (i
).set_loc_bitpos (type
->field (i
).loc_bitpos ());
5703 case FIELD_LOC_KIND_ENUMVAL
:
5704 new_type
->field (i
).set_loc_enumval (type
->field (i
).loc_enumval ());
5706 case FIELD_LOC_KIND_PHYSADDR
:
5707 new_type
->field (i
).set_loc_physaddr
5708 (type
->field (i
).loc_physaddr ());
5710 case FIELD_LOC_KIND_PHYSNAME
:
5711 new_type
->field (i
).set_loc_physname
5712 (xstrdup (type
->field (i
).loc_physname ()));
5714 case FIELD_LOC_KIND_DWARF_BLOCK
:
5715 new_type
->field (i
).set_loc_dwarf_block
5716 (type
->field (i
).loc_dwarf_block ());
5719 internal_error (__FILE__
, __LINE__
,
5720 _("Unexpected type field location kind: %d"),
5721 type
->field (i
).loc_kind ());
5726 /* For range types, copy the bounds information. */
5727 if (type
->code () == TYPE_CODE_RANGE
)
5729 range_bounds
*bounds
5730 = ((struct range_bounds
*) TYPE_ALLOC
5731 (new_type
, sizeof (struct range_bounds
)));
5733 *bounds
= *type
->bounds ();
5734 new_type
->set_bounds (bounds
);
5737 if (type
->main_type
->dyn_prop_list
!= NULL
)
5738 new_type
->main_type
->dyn_prop_list
5739 = copy_dynamic_prop_list (&objfile
->objfile_obstack
,
5740 type
->main_type
->dyn_prop_list
);
5743 /* Copy pointers to other types. */
5744 if (TYPE_TARGET_TYPE (type
))
5745 TYPE_TARGET_TYPE (new_type
) =
5746 copy_type_recursive (objfile
,
5747 TYPE_TARGET_TYPE (type
),
5750 /* Maybe copy the type_specific bits.
5752 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5753 base classes and methods. There's no fundamental reason why we
5754 can't, but at the moment it is not needed. */
5756 switch (TYPE_SPECIFIC_FIELD (type
))
5758 case TYPE_SPECIFIC_NONE
:
5760 case TYPE_SPECIFIC_FUNC
:
5761 INIT_FUNC_SPECIFIC (new_type
);
5762 TYPE_CALLING_CONVENTION (new_type
) = TYPE_CALLING_CONVENTION (type
);
5763 TYPE_NO_RETURN (new_type
) = TYPE_NO_RETURN (type
);
5764 TYPE_TAIL_CALL_LIST (new_type
) = NULL
;
5766 case TYPE_SPECIFIC_FLOATFORMAT
:
5767 TYPE_FLOATFORMAT (new_type
) = TYPE_FLOATFORMAT (type
);
5769 case TYPE_SPECIFIC_CPLUS_STUFF
:
5770 INIT_CPLUS_SPECIFIC (new_type
);
5772 case TYPE_SPECIFIC_GNAT_STUFF
:
5773 INIT_GNAT_SPECIFIC (new_type
);
5775 case TYPE_SPECIFIC_SELF_TYPE
:
5776 set_type_self_type (new_type
,
5777 copy_type_recursive (objfile
, TYPE_SELF_TYPE (type
),
5780 case TYPE_SPECIFIC_FIXED_POINT
:
5781 INIT_FIXED_POINT_SPECIFIC (new_type
);
5782 new_type
->fixed_point_info ().scaling_factor
5783 = type
->fixed_point_info ().scaling_factor
;
5785 case TYPE_SPECIFIC_INT
:
5786 TYPE_SPECIFIC_FIELD (new_type
) = TYPE_SPECIFIC_INT
;
5787 TYPE_MAIN_TYPE (new_type
)->type_specific
.int_stuff
5788 = TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
;
5792 gdb_assert_not_reached ("bad type_specific_kind");
5798 /* Make a copy of the given TYPE, except that the pointer & reference
5799 types are not preserved.
5801 This function assumes that the given type has an associated objfile.
5802 This objfile is used to allocate the new type. */
5805 copy_type (const struct type
*type
)
5807 struct type
*new_type
;
5809 gdb_assert (type
->is_objfile_owned ());
5811 new_type
= alloc_type_copy (type
);
5812 new_type
->set_instance_flags (type
->instance_flags ());
5813 TYPE_LENGTH (new_type
) = TYPE_LENGTH (type
);
5814 memcpy (TYPE_MAIN_TYPE (new_type
), TYPE_MAIN_TYPE (type
),
5815 sizeof (struct main_type
));
5816 if (type
->main_type
->dyn_prop_list
!= NULL
)
5817 new_type
->main_type
->dyn_prop_list
5818 = copy_dynamic_prop_list (&type
->objfile_owner ()->objfile_obstack
,
5819 type
->main_type
->dyn_prop_list
);
5824 /* Helper functions to initialize architecture-specific types. */
5826 /* Allocate a type structure associated with GDBARCH and set its
5827 CODE, LENGTH, and NAME fields. */
5830 arch_type (struct gdbarch
*gdbarch
,
5831 enum type_code code
, int bit
, const char *name
)
5835 type
= alloc_type_arch (gdbarch
);
5836 set_type_code (type
, code
);
5837 gdb_assert ((bit
% TARGET_CHAR_BIT
) == 0);
5838 TYPE_LENGTH (type
) = bit
/ TARGET_CHAR_BIT
;
5841 type
->set_name (gdbarch_obstack_strdup (gdbarch
, name
));
5846 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
5847 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5848 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5851 arch_integer_type (struct gdbarch
*gdbarch
,
5852 int bit
, int unsigned_p
, const char *name
)
5856 t
= arch_type (gdbarch
, TYPE_CODE_INT
, bit
, name
);
5858 t
->set_is_unsigned (true);
5863 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
5864 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5865 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5868 arch_character_type (struct gdbarch
*gdbarch
,
5869 int bit
, int unsigned_p
, const char *name
)
5873 t
= arch_type (gdbarch
, TYPE_CODE_CHAR
, bit
, name
);
5875 t
->set_is_unsigned (true);
5880 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
5881 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5882 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5885 arch_boolean_type (struct gdbarch
*gdbarch
,
5886 int bit
, int unsigned_p
, const char *name
)
5890 t
= arch_type (gdbarch
, TYPE_CODE_BOOL
, bit
, name
);
5892 t
->set_is_unsigned (true);
5897 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
5898 BIT is the type size in bits; if BIT equals -1, the size is
5899 determined by the floatformat. NAME is the type name. Set the
5900 TYPE_FLOATFORMAT from FLOATFORMATS. */
5903 arch_float_type (struct gdbarch
*gdbarch
,
5904 int bit
, const char *name
,
5905 const struct floatformat
**floatformats
)
5907 const struct floatformat
*fmt
= floatformats
[gdbarch_byte_order (gdbarch
)];
5910 bit
= verify_floatformat (bit
, fmt
);
5911 t
= arch_type (gdbarch
, TYPE_CODE_FLT
, bit
, name
);
5912 TYPE_FLOATFORMAT (t
) = fmt
;
5917 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
5918 BIT is the type size in bits. NAME is the type name. */
5921 arch_decfloat_type (struct gdbarch
*gdbarch
, int bit
, const char *name
)
5925 t
= arch_type (gdbarch
, TYPE_CODE_DECFLOAT
, bit
, name
);
5929 /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
5930 BIT is the pointer type size in bits. NAME is the type name.
5931 TARGET_TYPE is the pointer target type. Always sets the pointer type's
5932 TYPE_UNSIGNED flag. */
5935 arch_pointer_type (struct gdbarch
*gdbarch
,
5936 int bit
, const char *name
, struct type
*target_type
)
5940 t
= arch_type (gdbarch
, TYPE_CODE_PTR
, bit
, name
);
5941 TYPE_TARGET_TYPE (t
) = target_type
;
5942 t
->set_is_unsigned (true);
5946 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5947 NAME is the type name. BIT is the size of the flag word in bits. */
5950 arch_flags_type (struct gdbarch
*gdbarch
, const char *name
, int bit
)
5954 type
= arch_type (gdbarch
, TYPE_CODE_FLAGS
, bit
, name
);
5955 type
->set_is_unsigned (true);
5956 type
->set_num_fields (0);
5957 /* Pre-allocate enough space assuming every field is one bit. */
5959 ((struct field
*) TYPE_ZALLOC (type
, bit
* sizeof (struct field
)));
5964 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5965 position BITPOS is called NAME. Pass NAME as "" for fields that
5966 should not be printed. */
5969 append_flags_type_field (struct type
*type
, int start_bitpos
, int nr_bits
,
5970 struct type
*field_type
, const char *name
)
5972 int type_bitsize
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
5973 int field_nr
= type
->num_fields ();
5975 gdb_assert (type
->code () == TYPE_CODE_FLAGS
);
5976 gdb_assert (type
->num_fields () + 1 <= type_bitsize
);
5977 gdb_assert (start_bitpos
>= 0 && start_bitpos
< type_bitsize
);
5978 gdb_assert (nr_bits
>= 1 && (start_bitpos
+ nr_bits
) <= type_bitsize
);
5979 gdb_assert (name
!= NULL
);
5981 type
->set_num_fields (type
->num_fields () + 1);
5982 type
->field (field_nr
).set_name (xstrdup (name
));
5983 type
->field (field_nr
).set_type (field_type
);
5984 type
->field (field_nr
).set_loc_bitpos (start_bitpos
);
5985 TYPE_FIELD_BITSIZE (type
, field_nr
) = nr_bits
;
5988 /* Special version of append_flags_type_field to add a flag field.
5989 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5990 position BITPOS is called NAME. */
5993 append_flags_type_flag (struct type
*type
, int bitpos
, const char *name
)
5995 append_flags_type_field (type
, bitpos
, 1,
5996 builtin_type (type
->arch ())->builtin_bool
,
6000 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
6001 specified by CODE) associated with GDBARCH. NAME is the type name. */
6004 arch_composite_type (struct gdbarch
*gdbarch
, const char *name
,
6005 enum type_code code
)
6009 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
);
6010 t
= arch_type (gdbarch
, code
, 0, NULL
);
6012 INIT_CPLUS_SPECIFIC (t
);
6016 /* Add new field with name NAME and type FIELD to composite type T.
6017 Do not set the field's position or adjust the type's length;
6018 the caller should do so. Return the new field. */
6021 append_composite_type_field_raw (struct type
*t
, const char *name
,
6026 t
->set_num_fields (t
->num_fields () + 1);
6027 t
->set_fields (XRESIZEVEC (struct field
, t
->fields (),
6029 f
= &t
->field (t
->num_fields () - 1);
6030 memset (f
, 0, sizeof f
[0]);
6031 f
[0].set_type (field
);
6032 f
[0].set_name (name
);
6036 /* Add new field with name NAME and type FIELD to composite type T.
6037 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
6040 append_composite_type_field_aligned (struct type
*t
, const char *name
,
6041 struct type
*field
, int alignment
)
6043 struct field
*f
= append_composite_type_field_raw (t
, name
, field
);
6045 if (t
->code () == TYPE_CODE_UNION
)
6047 if (TYPE_LENGTH (t
) < TYPE_LENGTH (field
))
6048 TYPE_LENGTH (t
) = TYPE_LENGTH (field
);
6050 else if (t
->code () == TYPE_CODE_STRUCT
)
6052 TYPE_LENGTH (t
) = TYPE_LENGTH (t
) + TYPE_LENGTH (field
);
6053 if (t
->num_fields () > 1)
6056 (f
[-1].loc_bitpos () + (TYPE_LENGTH (f
[-1].type ()) * TARGET_CHAR_BIT
));
6062 alignment
*= TARGET_CHAR_BIT
;
6063 left
= f
[0].loc_bitpos () % alignment
;
6067 f
->set_loc_bitpos (f
[0].loc_bitpos () + (alignment
- left
));
6068 TYPE_LENGTH (t
) += (alignment
- left
) / TARGET_CHAR_BIT
;
6075 /* Add new field with name NAME and type FIELD to composite type T. */
6078 append_composite_type_field (struct type
*t
, const char *name
,
6081 append_composite_type_field_aligned (t
, name
, field
, 0);
6086 /* We manage the lifetimes of fixed_point_type_info objects by
6087 attaching them to the objfile. Currently, these objects are
6088 modified during construction, and GMP does not provide a way to
6089 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
6090 them. If we did do this, they could be moved to the per-BFD and
6091 shared across objfiles. */
6092 typedef std::vector
<std::unique_ptr
<fixed_point_type_info
>>
6093 fixed_point_type_storage
;
6095 /* Key used for managing the storage of fixed-point type info. */
6096 static const struct objfile_key
<fixed_point_type_storage
>
6097 fixed_point_objfile_key
;
6099 /* See gdbtypes.h. */
6102 allocate_fixed_point_type_info (struct type
*type
)
6104 std::unique_ptr
<fixed_point_type_info
> up (new fixed_point_type_info
);
6105 fixed_point_type_info
*info
;
6107 if (type
->is_objfile_owned ())
6109 fixed_point_type_storage
*storage
6110 = fixed_point_objfile_key
.get (type
->objfile_owner ());
6111 if (storage
== nullptr)
6112 storage
= fixed_point_objfile_key
.emplace (type
->objfile_owner ());
6114 storage
->push_back (std::move (up
));
6118 /* We just leak the memory, because that's what we do generally
6119 for non-objfile-attached types. */
6120 info
= up
.release ();
6123 type
->set_fixed_point_info (info
);
6126 /* See gdbtypes.h. */
6129 is_fixed_point_type (struct type
*type
)
6131 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6132 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6133 type
= check_typedef (type
);
6135 return type
->code () == TYPE_CODE_FIXED_POINT
;
6138 /* See gdbtypes.h. */
6141 type::fixed_point_type_base_type ()
6143 struct type
*type
= this;
6145 while (check_typedef (type
)->code () == TYPE_CODE_RANGE
)
6146 type
= TYPE_TARGET_TYPE (check_typedef (type
));
6147 type
= check_typedef (type
);
6149 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
);
6153 /* See gdbtypes.h. */
6156 type::fixed_point_scaling_factor ()
6158 struct type
*type
= this->fixed_point_type_base_type ();
6160 return type
->fixed_point_info ().scaling_factor
;
6165 static struct gdbarch_data
*gdbtypes_data
;
6167 const struct builtin_type
*
6168 builtin_type (struct gdbarch
*gdbarch
)
6170 return (const struct builtin_type
*) gdbarch_data (gdbarch
, gdbtypes_data
);
6174 gdbtypes_post_init (struct gdbarch
*gdbarch
)
6176 struct builtin_type
*builtin_type
6177 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_type
);
6180 builtin_type
->builtin_void
6181 = arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6182 builtin_type
->builtin_char
6183 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6184 !gdbarch_char_signed (gdbarch
), "char");
6185 builtin_type
->builtin_char
->set_has_no_signedness (true);
6186 builtin_type
->builtin_signed_char
6187 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6189 builtin_type
->builtin_unsigned_char
6190 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
,
6191 1, "unsigned char");
6192 builtin_type
->builtin_short
6193 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6195 builtin_type
->builtin_unsigned_short
6196 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
),
6197 1, "unsigned short");
6198 builtin_type
->builtin_int
6199 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6201 builtin_type
->builtin_unsigned_int
6202 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
6204 builtin_type
->builtin_long
6205 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6207 builtin_type
->builtin_unsigned_long
6208 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
6209 1, "unsigned long");
6210 builtin_type
->builtin_long_long
6211 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6213 builtin_type
->builtin_unsigned_long_long
6214 = arch_integer_type (gdbarch
, gdbarch_long_long_bit (gdbarch
),
6215 1, "unsigned long long");
6216 builtin_type
->builtin_half
6217 = arch_float_type (gdbarch
, gdbarch_half_bit (gdbarch
),
6218 "half", gdbarch_half_format (gdbarch
));
6219 builtin_type
->builtin_float
6220 = arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
),
6221 "float", gdbarch_float_format (gdbarch
));
6222 builtin_type
->builtin_bfloat16
6223 = arch_float_type (gdbarch
, gdbarch_bfloat16_bit (gdbarch
),
6224 "bfloat16", gdbarch_bfloat16_format (gdbarch
));
6225 builtin_type
->builtin_double
6226 = arch_float_type (gdbarch
, gdbarch_double_bit (gdbarch
),
6227 "double", gdbarch_double_format (gdbarch
));
6228 builtin_type
->builtin_long_double
6229 = arch_float_type (gdbarch
, gdbarch_long_double_bit (gdbarch
),
6230 "long double", gdbarch_long_double_format (gdbarch
));
6231 builtin_type
->builtin_complex
6232 = init_complex_type ("complex", builtin_type
->builtin_float
);
6233 builtin_type
->builtin_double_complex
6234 = init_complex_type ("double complex", builtin_type
->builtin_double
);
6235 builtin_type
->builtin_string
6236 = arch_type (gdbarch
, TYPE_CODE_STRING
, TARGET_CHAR_BIT
, "string");
6237 builtin_type
->builtin_bool
6238 = arch_boolean_type (gdbarch
, TARGET_CHAR_BIT
, 1, "bool");
6240 /* The following three are about decimal floating point types, which
6241 are 32-bits, 64-bits and 128-bits respectively. */
6242 builtin_type
->builtin_decfloat
6243 = arch_decfloat_type (gdbarch
, 32, "_Decimal32");
6244 builtin_type
->builtin_decdouble
6245 = arch_decfloat_type (gdbarch
, 64, "_Decimal64");
6246 builtin_type
->builtin_declong
6247 = arch_decfloat_type (gdbarch
, 128, "_Decimal128");
6249 /* "True" character types. */
6250 builtin_type
->builtin_true_char
6251 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 0, "true character");
6252 builtin_type
->builtin_true_unsigned_char
6253 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 1, "true character");
6255 /* Fixed-size integer types. */
6256 builtin_type
->builtin_int0
6257 = arch_integer_type (gdbarch
, 0, 0, "int0_t");
6258 builtin_type
->builtin_int8
6259 = arch_integer_type (gdbarch
, 8, 0, "int8_t");
6260 builtin_type
->builtin_uint8
6261 = arch_integer_type (gdbarch
, 8, 1, "uint8_t");
6262 builtin_type
->builtin_int16
6263 = arch_integer_type (gdbarch
, 16, 0, "int16_t");
6264 builtin_type
->builtin_uint16
6265 = arch_integer_type (gdbarch
, 16, 1, "uint16_t");
6266 builtin_type
->builtin_int24
6267 = arch_integer_type (gdbarch
, 24, 0, "int24_t");
6268 builtin_type
->builtin_uint24
6269 = arch_integer_type (gdbarch
, 24, 1, "uint24_t");
6270 builtin_type
->builtin_int32
6271 = arch_integer_type (gdbarch
, 32, 0, "int32_t");
6272 builtin_type
->builtin_uint32
6273 = arch_integer_type (gdbarch
, 32, 1, "uint32_t");
6274 builtin_type
->builtin_int64
6275 = arch_integer_type (gdbarch
, 64, 0, "int64_t");
6276 builtin_type
->builtin_uint64
6277 = arch_integer_type (gdbarch
, 64, 1, "uint64_t");
6278 builtin_type
->builtin_int128
6279 = arch_integer_type (gdbarch
, 128, 0, "int128_t");
6280 builtin_type
->builtin_uint128
6281 = arch_integer_type (gdbarch
, 128, 1, "uint128_t");
6283 builtin_type
->builtin_int8
->set_instance_flags
6284 (builtin_type
->builtin_int8
->instance_flags ()
6285 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6287 builtin_type
->builtin_uint8
->set_instance_flags
6288 (builtin_type
->builtin_uint8
->instance_flags ()
6289 | TYPE_INSTANCE_FLAG_NOTTEXT
);
6291 /* Wide character types. */
6292 builtin_type
->builtin_char16
6293 = arch_integer_type (gdbarch
, 16, 1, "char16_t");
6294 builtin_type
->builtin_char32
6295 = arch_integer_type (gdbarch
, 32, 1, "char32_t");
6296 builtin_type
->builtin_wchar
6297 = arch_integer_type (gdbarch
, gdbarch_wchar_bit (gdbarch
),
6298 !gdbarch_wchar_signed (gdbarch
), "wchar_t");
6300 /* Default data/code pointer types. */
6301 builtin_type
->builtin_data_ptr
6302 = lookup_pointer_type (builtin_type
->builtin_void
);
6303 builtin_type
->builtin_func_ptr
6304 = lookup_pointer_type (lookup_function_type (builtin_type
->builtin_void
));
6305 builtin_type
->builtin_func_func
6306 = lookup_function_type (builtin_type
->builtin_func_ptr
);
6308 /* This type represents a GDB internal function. */
6309 builtin_type
->internal_fn
6310 = arch_type (gdbarch
, TYPE_CODE_INTERNAL_FUNCTION
, 0,
6311 "<internal function>");
6313 /* This type represents an xmethod. */
6314 builtin_type
->xmethod
6315 = arch_type (gdbarch
, TYPE_CODE_XMETHOD
, 0, "<xmethod>");
6317 return builtin_type
;
6320 /* This set of objfile-based types is intended to be used by symbol
6321 readers as basic types. */
6323 static const struct objfile_key
<struct objfile_type
,
6324 gdb::noop_deleter
<struct objfile_type
>>
6327 const struct objfile_type
*
6328 objfile_type (struct objfile
*objfile
)
6330 struct gdbarch
*gdbarch
;
6331 struct objfile_type
*objfile_type
= objfile_type_data
.get (objfile
);
6334 return objfile_type
;
6336 objfile_type
= OBSTACK_CALLOC (&objfile
->objfile_obstack
,
6337 1, struct objfile_type
);
6339 /* Use the objfile architecture to determine basic type properties. */
6340 gdbarch
= objfile
->arch ();
6343 objfile_type
->builtin_void
6344 = init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
6345 objfile_type
->builtin_char
6346 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6347 !gdbarch_char_signed (gdbarch
), "char");
6348 objfile_type
->builtin_char
->set_has_no_signedness (true);
6349 objfile_type
->builtin_signed_char
6350 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6352 objfile_type
->builtin_unsigned_char
6353 = init_integer_type (objfile
, TARGET_CHAR_BIT
,
6354 1, "unsigned char");
6355 objfile_type
->builtin_short
6356 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6358 objfile_type
->builtin_unsigned_short
6359 = init_integer_type (objfile
, gdbarch_short_bit (gdbarch
),
6360 1, "unsigned short");
6361 objfile_type
->builtin_int
6362 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6364 objfile_type
->builtin_unsigned_int
6365 = init_integer_type (objfile
, gdbarch_int_bit (gdbarch
),
6367 objfile_type
->builtin_long
6368 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6370 objfile_type
->builtin_unsigned_long
6371 = init_integer_type (objfile
, gdbarch_long_bit (gdbarch
),
6372 1, "unsigned long");
6373 objfile_type
->builtin_long_long
6374 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6376 objfile_type
->builtin_unsigned_long_long
6377 = init_integer_type (objfile
, gdbarch_long_long_bit (gdbarch
),
6378 1, "unsigned long long");
6379 objfile_type
->builtin_float
6380 = init_float_type (objfile
, gdbarch_float_bit (gdbarch
),
6381 "float", gdbarch_float_format (gdbarch
));
6382 objfile_type
->builtin_double
6383 = init_float_type (objfile
, gdbarch_double_bit (gdbarch
),
6384 "double", gdbarch_double_format (gdbarch
));
6385 objfile_type
->builtin_long_double
6386 = init_float_type (objfile
, gdbarch_long_double_bit (gdbarch
),
6387 "long double", gdbarch_long_double_format (gdbarch
));
6389 /* This type represents a type that was unrecognized in symbol read-in. */
6390 objfile_type
->builtin_error
6391 = init_type (objfile
, TYPE_CODE_ERROR
, 0, "<unknown type>");
6393 /* The following set of types is used for symbols with no
6394 debug information. */
6395 objfile_type
->nodebug_text_symbol
6396 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6397 "<text variable, no debug info>");
6399 objfile_type
->nodebug_text_gnu_ifunc_symbol
6400 = init_type (objfile
, TYPE_CODE_FUNC
, TARGET_CHAR_BIT
,
6401 "<text gnu-indirect-function variable, no debug info>");
6402 objfile_type
->nodebug_text_gnu_ifunc_symbol
->set_is_gnu_ifunc (true);
6404 objfile_type
->nodebug_got_plt_symbol
6405 = init_pointer_type (objfile
, gdbarch_addr_bit (gdbarch
),
6406 "<text from jump slot in .got.plt, no debug info>",
6407 objfile_type
->nodebug_text_symbol
);
6408 objfile_type
->nodebug_data_symbol
6409 = init_nodebug_var_type (objfile
, "<data variable, no debug info>");
6410 objfile_type
->nodebug_unknown_symbol
6411 = init_nodebug_var_type (objfile
, "<variable (not text or data), no debug info>");
6412 objfile_type
->nodebug_tls_symbol
6413 = init_nodebug_var_type (objfile
, "<thread local variable, no debug info>");
6415 /* NOTE: on some targets, addresses and pointers are not necessarily
6419 - gdb's `struct type' always describes the target's
6421 - gdb's `struct value' objects should always hold values in
6423 - gdb's CORE_ADDR values are addresses in the unified virtual
6424 address space that the assembler and linker work with. Thus,
6425 since target_read_memory takes a CORE_ADDR as an argument, it
6426 can access any memory on the target, even if the processor has
6427 separate code and data address spaces.
6429 In this context, objfile_type->builtin_core_addr is a bit odd:
6430 it's a target type for a value the target will never see. It's
6431 only used to hold the values of (typeless) linker symbols, which
6432 are indeed in the unified virtual address space. */
6434 objfile_type
->builtin_core_addr
6435 = init_integer_type (objfile
, gdbarch_addr_bit (gdbarch
), 1,
6438 objfile_type_data
.set (objfile
, objfile_type
);
6439 return objfile_type
;
6442 /* See gdbtypes.h. */
6445 call_site::pc () const
6447 compunit_symtab
*cust
= this->per_objfile
->get_symtab (this->per_cu
);
6449 = this->per_objfile
->objfile
->section_offsets
[cust
->block_line_section ()];
6450 return m_unrelocated_pc
+ delta
;
6453 void _initialize_gdbtypes ();
6455 _initialize_gdbtypes ()
6457 gdbtypes_data
= gdbarch_data_register_post_init (gdbtypes_post_init
);
6459 add_setshow_zuinteger_cmd ("overload", no_class
, &overload_debug
,
6460 _("Set debugging of C++ overloading."),
6461 _("Show debugging of C++ overloading."),
6462 _("When enabled, ranking of the "
6463 "functions is displayed."),
6465 show_overload_debug
,
6466 &setdebuglist
, &showdebuglist
);
6468 /* Add user knob for controlling resolution of opaque types. */
6469 add_setshow_boolean_cmd ("opaque-type-resolution", class_support
,
6470 &opaque_type_resolution
,
6471 _("Set resolution of opaque struct/class/union"
6472 " types (if set before loading symbols)."),
6473 _("Show resolution of opaque struct/class/union"
6474 " types (if set before loading symbols)."),
6476 show_opaque_type_resolution
,
6477 &setlist
, &showlist
);
6479 /* Add an option to permit non-strict type checking. */
6480 add_setshow_boolean_cmd ("type", class_support
,
6481 &strict_type_checking
,
6482 _("Set strict type checking."),
6483 _("Show strict type checking."),
6485 show_strict_type_checking
,
6486 &setchecklist
, &showchecklist
);