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 ntype->set_target_type (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 ntype->set_target_type (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 ntype->set_target_type (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 mtype->set_target_type (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 result_type->set_target_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 result_type->set_target_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 inner_array->set_target_type (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->set_target_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->set_target_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->set_target_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 rank_type->set_target_type (copy_type (rank_type));
2442 rank_type = TYPE_TARGET_TYPE (rank_type);
2444 rank_type->set_target_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 resolved_type->set_target_type (nullptr);
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 resolved_type->set_target_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 resolved_type->set_target_type
2840 (resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
2841 &pinfo, top_level));
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->set_target_type (sym->type ());
3038 else /* TYPE_CODE_UNDEF */
3039 type->set_target_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 type *type = NULL; /* Initialize to keep gcc happy. */
3173 /* Suppress error messages. */
3174 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3177 /* Call parse_and_eval_type() without fear of longjmp()s. */
3180 type = parse_and_eval_type (p, length);
3182 catch (const gdb_exception_error &except)
3184 type = builtin_type (gdbarch)->builtin_void;
3190 /* Ugly hack to convert method stubs into method types.
3192 He ain't kiddin'. This demangles the name of the method into a
3193 string including argument types, parses out each argument type,
3194 generates a string casting a zero to that type, evaluates the
3195 string, and stuffs the resulting type into an argtype vector!!!
3196 Then it knows the type of the whole function (including argument
3197 types for overloading), which info used to be in the stab's but was
3198 removed to hack back the space required for them. */
3201 check_stub_method (struct type *type, int method_id, int signature_id)
3203 struct gdbarch *gdbarch = type->arch ();
3205 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3206 gdb::unique_xmalloc_ptr<char> demangled_name
3207 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3208 char *argtypetext, *p;
3209 int depth = 0, argcount = 1;
3210 struct field *argtypes;
3213 /* Make sure we got back a function string that we can use. */
3215 p = strchr (demangled_name.get (), '(');
3219 if (demangled_name == NULL || p == NULL)
3220 error (_("Internal: Cannot demangle mangled name `%s'."),
3223 /* Now, read in the parameters that define this type. */
3228 if (*p == '(' || *p == '<')
3232 else if (*p == ')' || *p == '>')
3236 else if (*p == ',' && depth == 0)
3244 /* If we read one argument and it was ``void'', don't count it. */
3245 if (startswith (argtypetext, "(void)"))
3248 /* We need one extra slot, for the THIS pointer. */
3250 argtypes = (struct field *)
3251 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
3254 /* Add THIS pointer for non-static methods. */
3255 f = TYPE_FN_FIELDLIST1 (type, method_id);
3256 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3260 argtypes[0].set_type (lookup_pointer_type (type));
3264 if (*p != ')') /* () means no args, skip while. */
3269 if (depth <= 0 && (*p == ',' || *p == ')'))
3271 /* Avoid parsing of ellipsis, they will be handled below.
3272 Also avoid ``void'' as above. */
3273 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3274 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3276 argtypes[argcount].set_type
3277 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3280 argtypetext = p + 1;
3283 if (*p == '(' || *p == '<')
3287 else if (*p == ')' || *p == '>')
3296 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3298 /* Now update the old "stub" type into a real type. */
3299 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3300 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3301 We want a method (TYPE_CODE_METHOD). */
3302 smash_to_method_type (mtype, type, TYPE_TARGET_TYPE (mtype),
3303 argtypes, argcount, p[-2] == '.');
3304 mtype->set_is_stub (false);
3305 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3308 /* This is the external interface to check_stub_method, above. This
3309 function unstubs all of the signatures for TYPE's METHOD_ID method
3310 name. After calling this function TYPE_FN_FIELD_STUB will be
3311 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3314 This function unfortunately can not die until stabs do. */
3317 check_stub_method_group (struct type *type, int method_id)
3319 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3320 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3322 for (int j = 0; j < len; j++)
3324 if (TYPE_FN_FIELD_STUB (f, j))
3325 check_stub_method (type, method_id, j);
3329 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3330 const struct cplus_struct_type cplus_struct_default = { };
3333 allocate_cplus_struct_type (struct type *type)
3335 if (HAVE_CPLUS_STRUCT (type))
3336 /* Structure was already allocated. Nothing more to do. */
3339 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3340 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3341 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
3342 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3343 set_type_vptr_fieldno (type, -1);
3346 const struct gnat_aux_type gnat_aux_default =
3349 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3350 and allocate the associated gnat-specific data. The gnat-specific
3351 data is also initialized to gnat_aux_default. */
3354 allocate_gnat_aux_type (struct type *type)
3356 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3357 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3358 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
3359 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3362 /* Helper function to initialize a newly allocated type. Set type code
3363 to CODE and initialize the type-specific fields accordingly. */
3366 set_type_code (struct type *type, enum type_code code)
3368 type->set_code (code);
3372 case TYPE_CODE_STRUCT:
3373 case TYPE_CODE_UNION:
3374 case TYPE_CODE_NAMESPACE:
3375 INIT_CPLUS_SPECIFIC (type);
3378 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
3380 case TYPE_CODE_FUNC:
3381 INIT_FUNC_SPECIFIC (type);
3383 case TYPE_CODE_FIXED_POINT:
3384 INIT_FIXED_POINT_SPECIFIC (type);
3389 /* Helper function to verify floating-point format and size.
3390 BIT is the type size in bits; if BIT equals -1, the size is
3391 determined by the floatformat. Returns size to be used. */
3394 verify_floatformat (int bit, const struct floatformat *floatformat)
3396 gdb_assert (floatformat != NULL);
3399 bit = floatformat->totalsize;
3401 gdb_assert (bit >= 0);
3402 gdb_assert (bit >= floatformat->totalsize);
3407 /* Return the floating-point format for a floating-point variable of
3410 const struct floatformat *
3411 floatformat_from_type (const struct type *type)
3413 gdb_assert (type->code () == TYPE_CODE_FLT);
3414 gdb_assert (TYPE_FLOATFORMAT (type));
3415 return TYPE_FLOATFORMAT (type);
3418 /* Helper function to initialize the standard scalar types.
3420 If NAME is non-NULL, then it is used to initialize the type name.
3421 Note that NAME is not copied; it is required to have a lifetime at
3422 least as long as OBJFILE. */
3425 init_type (struct objfile *objfile, enum type_code code, int bit,
3430 type = alloc_type (objfile);
3431 set_type_code (type, code);
3432 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
3433 TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
3434 type->set_name (name);
3439 /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
3440 to use with variables that have no debug info. NAME is the type
3443 static struct type *
3444 init_nodebug_var_type (struct objfile *objfile, const char *name)
3446 return init_type (objfile, TYPE_CODE_ERROR, 0, name);
3449 /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
3450 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3451 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3454 init_integer_type (struct objfile *objfile,
3455 int bit, int unsigned_p, const char *name)
3459 t = init_type (objfile, TYPE_CODE_INT, bit, name);
3461 t->set_is_unsigned (true);
3463 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3464 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3465 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3470 /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
3471 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3472 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3475 init_character_type (struct objfile *objfile,
3476 int bit, int unsigned_p, const char *name)
3480 t = init_type (objfile, TYPE_CODE_CHAR, bit, name);
3482 t->set_is_unsigned (true);
3487 /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
3488 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3489 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3492 init_boolean_type (struct objfile *objfile,
3493 int bit, int unsigned_p, const char *name)
3497 t = init_type (objfile, TYPE_CODE_BOOL, bit, name);
3499 t->set_is_unsigned (true);
3501 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3502 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3503 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3508 /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
3509 BIT is the type size in bits; if BIT equals -1, the size is
3510 determined by the floatformat. NAME is the type name. Set the
3511 TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
3512 to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
3513 order of the objfile's architecture is used. */
3516 init_float_type (struct objfile *objfile,
3517 int bit, const char *name,
3518 const struct floatformat **floatformats,
3519 enum bfd_endian byte_order)
3521 if (byte_order == BFD_ENDIAN_UNKNOWN)
3523 struct gdbarch *gdbarch = objfile->arch ();
3524 byte_order = gdbarch_byte_order (gdbarch);
3526 const struct floatformat *fmt = floatformats[byte_order];
3529 bit = verify_floatformat (bit, fmt);
3530 t = init_type (objfile, TYPE_CODE_FLT, bit, name);
3531 TYPE_FLOATFORMAT (t) = fmt;
3536 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
3537 BIT is the type size in bits. NAME is the type name. */
3540 init_decfloat_type (struct objfile *objfile, int bit, const char *name)
3544 t = init_type (objfile, TYPE_CODE_DECFLOAT, bit, name);
3548 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3551 can_create_complex_type (struct type *target_type)
3553 return (target_type->code () == TYPE_CODE_INT
3554 || target_type->code () == TYPE_CODE_FLT);
3557 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3558 name. TARGET_TYPE is the component type. */
3561 init_complex_type (const char *name, struct type *target_type)
3565 gdb_assert (can_create_complex_type (target_type));
3567 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3569 if (name == nullptr && target_type->name () != nullptr)
3572 = (char *) TYPE_ALLOC (target_type,
3573 strlen (target_type->name ())
3574 + strlen ("_Complex ") + 1);
3575 strcpy (new_name, "_Complex ");
3576 strcat (new_name, target_type->name ());
3580 t = alloc_type_copy (target_type);
3581 set_type_code (t, TYPE_CODE_COMPLEX);
3582 TYPE_LENGTH (t) = 2 * TYPE_LENGTH (target_type);
3585 t->set_target_type (target_type);
3586 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3589 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3592 /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
3593 BIT is the pointer type size in bits. NAME is the type name.
3594 TARGET_TYPE is the pointer target type. Always sets the pointer type's
3595 TYPE_UNSIGNED flag. */
3598 init_pointer_type (struct objfile *objfile,
3599 int bit, const char *name, struct type *target_type)
3603 t = init_type (objfile, TYPE_CODE_PTR, bit, name);
3604 t->set_target_type (target_type);
3605 t->set_is_unsigned (true);
3609 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3610 BIT is the pointer type size in bits.
3611 UNSIGNED_P should be nonzero if the type is unsigned.
3612 NAME is the type name. */
3615 init_fixed_point_type (struct objfile *objfile,
3616 int bit, int unsigned_p, const char *name)
3620 t = init_type (objfile, TYPE_CODE_FIXED_POINT, bit, name);
3622 t->set_is_unsigned (true);
3627 /* See gdbtypes.h. */
3630 type_raw_align (struct type *type)
3632 if (type->align_log2 != 0)
3633 return 1 << (type->align_log2 - 1);
3637 /* See gdbtypes.h. */
3640 type_align (struct type *type)
3642 /* Check alignment provided in the debug information. */
3643 unsigned raw_align = type_raw_align (type);
3647 /* Allow the architecture to provide an alignment. */
3648 ULONGEST align = gdbarch_type_align (type->arch (), type);
3652 switch (type->code ())
3655 case TYPE_CODE_FUNC:
3656 case TYPE_CODE_FLAGS:
3658 case TYPE_CODE_RANGE:
3660 case TYPE_CODE_ENUM:
3662 case TYPE_CODE_RVALUE_REF:
3663 case TYPE_CODE_CHAR:
3664 case TYPE_CODE_BOOL:
3665 case TYPE_CODE_DECFLOAT:
3666 case TYPE_CODE_METHODPTR:
3667 case TYPE_CODE_MEMBERPTR:
3668 align = type_length_units (check_typedef (type));
3671 case TYPE_CODE_ARRAY:
3672 case TYPE_CODE_COMPLEX:
3673 case TYPE_CODE_TYPEDEF:
3674 align = type_align (TYPE_TARGET_TYPE (type));
3677 case TYPE_CODE_STRUCT:
3678 case TYPE_CODE_UNION:
3680 int number_of_non_static_fields = 0;
3681 for (unsigned i = 0; i < type->num_fields (); ++i)
3683 if (!field_is_static (&type->field (i)))
3685 number_of_non_static_fields++;
3686 ULONGEST f_align = type_align (type->field (i).type ());
3689 /* Don't pretend we know something we don't. */
3693 if (f_align > align)
3697 /* A struct with no fields, or with only static fields has an
3699 if (number_of_non_static_fields == 0)
3705 case TYPE_CODE_STRING:
3706 /* Not sure what to do here, and these can't appear in C or C++
3710 case TYPE_CODE_VOID:
3714 case TYPE_CODE_ERROR:
3715 case TYPE_CODE_METHOD:
3720 if ((align & (align - 1)) != 0)
3722 /* Not a power of 2, so pass. */
3729 /* See gdbtypes.h. */
3732 set_type_align (struct type *type, ULONGEST align)
3734 /* Must be a power of 2. Zero is ok. */
3735 gdb_assert ((align & (align - 1)) == 0);
3737 unsigned result = 0;
3744 if (result >= (1 << TYPE_ALIGN_BITS))
3747 type->align_log2 = result;
3752 /* Queries on types. */
3755 can_dereference (struct type *t)
3757 /* FIXME: Should we return true for references as well as
3759 t = check_typedef (t);
3762 && t->code () == TYPE_CODE_PTR
3763 && TYPE_TARGET_TYPE (t)->code () != TYPE_CODE_VOID);
3767 is_integral_type (struct type *t)
3769 t = check_typedef (t);
3772 && !is_fixed_point_type (t)
3773 && ((t->code () == TYPE_CODE_INT)
3774 || (t->code () == TYPE_CODE_ENUM)
3775 || (t->code () == TYPE_CODE_FLAGS)
3776 || (t->code () == TYPE_CODE_CHAR)
3777 || (t->code () == TYPE_CODE_RANGE)
3778 || (t->code () == TYPE_CODE_BOOL)));
3782 is_floating_type (struct type *t)
3784 t = check_typedef (t);
3787 && ((t->code () == TYPE_CODE_FLT)
3788 || (t->code () == TYPE_CODE_DECFLOAT)));
3791 /* Return true if TYPE is scalar. */
3794 is_scalar_type (struct type *type)
3796 type = check_typedef (type);
3798 if (is_fixed_point_type (type))
3799 return 0; /* Implemented as a scalar, but more like a floating point. */
3801 switch (type->code ())
3803 case TYPE_CODE_ARRAY:
3804 case TYPE_CODE_STRUCT:
3805 case TYPE_CODE_UNION:
3807 case TYPE_CODE_STRING:
3814 /* Return true if T is scalar, or a composite type which in practice has
3815 the memory layout of a scalar type. E.g., an array or struct with only
3816 one scalar element inside it, or a union with only scalar elements. */
3819 is_scalar_type_recursive (struct type *t)
3821 t = check_typedef (t);
3823 if (is_scalar_type (t))
3825 /* Are we dealing with an array or string of known dimensions? */
3826 else if ((t->code () == TYPE_CODE_ARRAY
3827 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3828 && t->index_type ()->code () == TYPE_CODE_RANGE)
3830 LONGEST low_bound, high_bound;
3831 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
3833 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3834 return (high_bound == low_bound
3835 && is_scalar_type_recursive (elt_type));
3839 /* Are we dealing with a struct with one element? */
3840 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3841 return is_scalar_type_recursive (t->field (0).type ());
3842 else if (t->code () == TYPE_CODE_UNION)
3844 int i, n = t->num_fields ();
3846 /* If all elements of the union are scalar, then the union is scalar. */
3847 for (i = 0; i < n; i++)
3848 if (!is_scalar_type_recursive (t->field (i).type ()))
3857 /* Return true is T is a class or a union. False otherwise. */
3860 class_or_union_p (const struct type *t)
3862 return (t->code () == TYPE_CODE_STRUCT
3863 || t->code () == TYPE_CODE_UNION);
3866 /* A helper function which returns true if types A and B represent the
3867 "same" class type. This is true if the types have the same main
3868 type, or the same name. */
3871 class_types_same_p (const struct type *a, const struct type *b)
3873 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3874 || (a->name () && b->name ()
3875 && !strcmp (a->name (), b->name ())));
3878 /* If BASE is an ancestor of DCLASS return the distance between them.
3879 otherwise return -1;
3883 class B: public A {};
3884 class C: public B {};
3887 distance_to_ancestor (A, A, 0) = 0
3888 distance_to_ancestor (A, B, 0) = 1
3889 distance_to_ancestor (A, C, 0) = 2
3890 distance_to_ancestor (A, D, 0) = 3
3892 If PUBLIC is 1 then only public ancestors are considered,
3893 and the function returns the distance only if BASE is a public ancestor
3897 distance_to_ancestor (A, D, 1) = -1. */
3900 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3905 base = check_typedef (base);
3906 dclass = check_typedef (dclass);
3908 if (class_types_same_p (base, dclass))
3911 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3913 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3916 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3924 /* Check whether BASE is an ancestor or base class or DCLASS
3925 Return 1 if so, and 0 if not.
3926 Note: If BASE and DCLASS are of the same type, this function
3927 will return 1. So for some class A, is_ancestor (A, A) will
3931 is_ancestor (struct type *base, struct type *dclass)
3933 return distance_to_ancestor (base, dclass, 0) >= 0;
3936 /* Like is_ancestor, but only returns true when BASE is a public
3937 ancestor of DCLASS. */
3940 is_public_ancestor (struct type *base, struct type *dclass)
3942 return distance_to_ancestor (base, dclass, 1) >= 0;
3945 /* A helper function for is_unique_ancestor. */
3948 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3950 const gdb_byte *valaddr, int embedded_offset,
3951 CORE_ADDR address, struct value *val)
3955 base = check_typedef (base);
3956 dclass = check_typedef (dclass);
3958 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3963 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3965 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3968 if (class_types_same_p (base, iter))
3970 /* If this is the first subclass, set *OFFSET and set count
3971 to 1. Otherwise, if this is at the same offset as
3972 previous instances, do nothing. Otherwise, increment
3976 *offset = this_offset;
3979 else if (this_offset == *offset)
3987 count += is_unique_ancestor_worker (base, iter, offset,
3989 embedded_offset + this_offset,
3996 /* Like is_ancestor, but only returns true if BASE is a unique base
3997 class of the type of VAL. */
4000 is_unique_ancestor (struct type *base, struct value *val)
4004 return is_unique_ancestor_worker (base, value_type (val), &offset,
4005 value_contents_for_printing (val).data (),
4006 value_embedded_offset (val),
4007 value_address (val), val) == 1;
4010 /* See gdbtypes.h. */
4013 type_byte_order (const struct type *type)
4015 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
4016 if (type->endianity_is_not_default ())
4018 if (byteorder == BFD_ENDIAN_BIG)
4019 return BFD_ENDIAN_LITTLE;
4022 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
4023 return BFD_ENDIAN_BIG;
4030 /* See gdbtypes.h. */
4033 is_nocall_function (const struct type *type)
4035 gdb_assert (type->code () == TYPE_CODE_FUNC
4036 || type->code () == TYPE_CODE_METHOD);
4038 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
4042 /* Overload resolution. */
4044 /* Return the sum of the rank of A with the rank of B. */
4047 sum_ranks (struct rank a, struct rank b)
4050 c.rank = a.rank + b.rank;
4051 c.subrank = a.subrank + b.subrank;
4055 /* Compare rank A and B and return:
4057 1 if a is better than b
4058 -1 if b is better than a. */
4061 compare_ranks (struct rank a, struct rank b)
4063 if (a.rank == b.rank)
4065 if (a.subrank == b.subrank)
4067 if (a.subrank < b.subrank)
4069 if (a.subrank > b.subrank)
4073 if (a.rank < b.rank)
4076 /* a.rank > b.rank */
4080 /* Functions for overload resolution begin here. */
4082 /* Compare two badness vectors A and B and return the result.
4083 0 => A and B are identical
4084 1 => A and B are incomparable
4085 2 => A is better than B
4086 3 => A is worse than B */
4089 compare_badness (const badness_vector &a, const badness_vector &b)
4093 /* Any positives in comparison? */
4094 bool found_pos = false;
4095 /* Any negatives in comparison? */
4096 bool found_neg = false;
4097 /* Did A have any INVALID_CONVERSION entries. */
4098 bool a_invalid = false;
4099 /* Did B have any INVALID_CONVERSION entries. */
4100 bool b_invalid = false;
4102 /* differing sizes => incomparable */
4103 if (a.size () != b.size ())
4106 /* Subtract b from a */
4107 for (i = 0; i < a.size (); i++)
4109 tmp = compare_ranks (b[i], a[i]);
4114 if (a[i].rank >= INVALID_CONVERSION)
4116 if (b[i].rank >= INVALID_CONVERSION)
4120 /* B will only be considered better than or incomparable to A if
4121 they both have invalid entries, or if neither does. That is, if
4122 A has only valid entries, and B has an invalid entry, then A will
4123 be considered better than B, even if B happens to be better for
4125 if (a_invalid != b_invalid)
4128 return 3; /* A > B */
4129 return 2; /* A < B */
4134 return 1; /* incomparable */
4136 return 3; /* A > B */
4142 return 2; /* A < B */
4144 return 0; /* A == B */
4148 /* Rank a function by comparing its parameter types (PARMS), to the
4149 types of an argument list (ARGS). Return the badness vector. This
4150 has ARGS.size() + 1 entries. */
4153 rank_function (gdb::array_view<type *> parms,
4154 gdb::array_view<value *> args)
4156 /* add 1 for the length-match rank. */
4158 bv.reserve (1 + args.size ());
4160 /* First compare the lengths of the supplied lists.
4161 If there is a mismatch, set it to a high value. */
4163 /* pai/1997-06-03 FIXME: when we have debug info about default
4164 arguments and ellipsis parameter lists, we should consider those
4165 and rank the length-match more finely. */
4167 bv.push_back ((args.size () != parms.size ())
4168 ? LENGTH_MISMATCH_BADNESS
4169 : EXACT_MATCH_BADNESS);
4171 /* Now rank all the parameters of the candidate function. */
4172 size_t min_len = std::min (parms.size (), args.size ());
4174 for (size_t i = 0; i < min_len; i++)
4175 bv.push_back (rank_one_type (parms[i], value_type (args[i]),
4178 /* If more arguments than parameters, add dummy entries. */
4179 for (size_t i = min_len; i < args.size (); i++)
4180 bv.push_back (TOO_FEW_PARAMS_BADNESS);
4185 /* Compare the names of two integer types, assuming that any sign
4186 qualifiers have been checked already. We do it this way because
4187 there may be an "int" in the name of one of the types. */
4190 integer_types_same_name_p (const char *first, const char *second)
4192 int first_p, second_p;
4194 /* If both are shorts, return 1; if neither is a short, keep
4196 first_p = (strstr (first, "short") != NULL);
4197 second_p = (strstr (second, "short") != NULL);
4198 if (first_p && second_p)
4200 if (first_p || second_p)
4203 /* Likewise for long. */
4204 first_p = (strstr (first, "long") != NULL);
4205 second_p = (strstr (second, "long") != NULL);
4206 if (first_p && second_p)
4208 if (first_p || second_p)
4211 /* Likewise for char. */
4212 first_p = (strstr (first, "char") != NULL);
4213 second_p = (strstr (second, "char") != NULL);
4214 if (first_p && second_p)
4216 if (first_p || second_p)
4219 /* They must both be ints. */
4223 /* Compares type A to type B. Returns true if they represent the same
4224 type, false otherwise. */
4227 types_equal (struct type *a, struct type *b)
4229 /* Identical type pointers. */
4230 /* However, this still doesn't catch all cases of same type for b
4231 and a. The reason is that builtin types are different from
4232 the same ones constructed from the object. */
4236 /* Resolve typedefs */
4237 if (a->code () == TYPE_CODE_TYPEDEF)
4238 a = check_typedef (a);
4239 if (b->code () == TYPE_CODE_TYPEDEF)
4240 b = check_typedef (b);
4242 /* Check if identical after resolving typedefs. */
4246 /* If after resolving typedefs a and b are not of the same type
4247 code then they are not equal. */
4248 if (a->code () != b->code ())
4251 /* If a and b are both pointers types or both reference types then
4252 they are equal of the same type iff the objects they refer to are
4253 of the same type. */
4254 if (a->code () == TYPE_CODE_PTR
4255 || a->code () == TYPE_CODE_REF)
4256 return types_equal (TYPE_TARGET_TYPE (a),
4257 TYPE_TARGET_TYPE (b));
4259 /* Well, damnit, if the names are exactly the same, I'll say they
4260 are exactly the same. This happens when we generate method
4261 stubs. The types won't point to the same address, but they
4262 really are the same. */
4264 if (a->name () && b->name ()
4265 && strcmp (a->name (), b->name ()) == 0)
4268 /* Two function types are equal if their argument and return types
4270 if (a->code () == TYPE_CODE_FUNC)
4274 if (a->num_fields () != b->num_fields ())
4277 if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
4280 for (i = 0; i < a->num_fields (); ++i)
4281 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4290 /* Deep comparison of types. */
4292 /* An entry in the type-equality bcache. */
4294 struct type_equality_entry
4296 type_equality_entry (struct type *t1, struct type *t2)
4302 struct type *type1, *type2;
4305 /* A helper function to compare two strings. Returns true if they are
4306 the same, false otherwise. Handles NULLs properly. */
4309 compare_maybe_null_strings (const char *s, const char *t)
4311 if (s == NULL || t == NULL)
4313 return strcmp (s, t) == 0;
4316 /* A helper function for check_types_worklist that checks two types for
4317 "deep" equality. Returns true if the types are considered the
4318 same, false otherwise. */
4321 check_types_equal (struct type *type1, struct type *type2,
4322 std::vector<type_equality_entry> *worklist)
4324 type1 = check_typedef (type1);
4325 type2 = check_typedef (type2);
4330 if (type1->code () != type2->code ()
4331 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
4332 || type1->is_unsigned () != type2->is_unsigned ()
4333 || type1->has_no_signedness () != type2->has_no_signedness ()
4334 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4335 || type1->has_varargs () != type2->has_varargs ()
4336 || type1->is_vector () != type2->is_vector ()
4337 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4338 || type1->instance_flags () != type2->instance_flags ()
4339 || type1->num_fields () != type2->num_fields ())
4342 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4344 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4347 if (type1->code () == TYPE_CODE_RANGE)
4349 if (*type1->bounds () != *type2->bounds ())
4356 for (i = 0; i < type1->num_fields (); ++i)
4358 const struct field *field1 = &type1->field (i);
4359 const struct field *field2 = &type2->field (i);
4361 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
4362 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
4363 || field1->loc_kind () != field2->loc_kind ())
4365 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4367 switch (field1->loc_kind ())
4369 case FIELD_LOC_KIND_BITPOS:
4370 if (field1->loc_bitpos () != field2->loc_bitpos ())
4373 case FIELD_LOC_KIND_ENUMVAL:
4374 if (field1->loc_enumval () != field2->loc_enumval ())
4376 /* Don't compare types of enum fields, because they don't
4379 case FIELD_LOC_KIND_PHYSADDR:
4380 if (field1->loc_physaddr () != field2->loc_physaddr ())
4383 case FIELD_LOC_KIND_PHYSNAME:
4384 if (!compare_maybe_null_strings (field1->loc_physname (),
4385 field2->loc_physname ()))
4388 case FIELD_LOC_KIND_DWARF_BLOCK:
4390 struct dwarf2_locexpr_baton *block1, *block2;
4392 block1 = field1->loc_dwarf_block ();
4393 block2 = field2->loc_dwarf_block ();
4394 if (block1->per_cu != block2->per_cu
4395 || block1->size != block2->size
4396 || memcmp (block1->data, block2->data, block1->size) != 0)
4401 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
4402 "%d by check_types_equal"),
4403 field1->loc_kind ());
4406 worklist->emplace_back (field1->type (), field2->type ());
4410 if (TYPE_TARGET_TYPE (type1) != NULL)
4412 if (TYPE_TARGET_TYPE (type2) == NULL)
4415 worklist->emplace_back (TYPE_TARGET_TYPE (type1),
4416 TYPE_TARGET_TYPE (type2));
4418 else if (TYPE_TARGET_TYPE (type2) != NULL)
4424 /* Check types on a worklist for equality. Returns false if any pair
4425 is not equal, true if they are all considered equal. */
4428 check_types_worklist (std::vector<type_equality_entry> *worklist,
4431 while (!worklist->empty ())
4435 struct type_equality_entry entry = std::move (worklist->back ());
4436 worklist->pop_back ();
4438 /* If the type pair has already been visited, we know it is
4440 cache->insert (&entry, sizeof (entry), &added);
4444 if (!check_types_equal (entry.type1, entry.type2, worklist))
4451 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4452 "deep comparison". Otherwise return false. */
4455 types_deeply_equal (struct type *type1, struct type *type2)
4457 std::vector<type_equality_entry> worklist;
4459 gdb_assert (type1 != NULL && type2 != NULL);
4461 /* Early exit for the simple case. */
4466 worklist.emplace_back (type1, type2);
4467 return check_types_worklist (&worklist, &cache);
4470 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4471 Otherwise return one. */
4474 type_not_allocated (const struct type *type)
4476 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4478 return (prop != nullptr && prop->kind () == PROP_CONST
4479 && prop->const_val () == 0);
4482 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4483 Otherwise return one. */
4486 type_not_associated (const struct type *type)
4488 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4490 return (prop != nullptr && prop->kind () == PROP_CONST
4491 && prop->const_val () == 0);
4494 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4497 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4499 struct rank rank = {0,0};
4501 switch (arg->code ())
4505 /* Allowed pointer conversions are:
4506 (a) pointer to void-pointer conversion. */
4507 if (TYPE_TARGET_TYPE (parm)->code () == TYPE_CODE_VOID)
4508 return VOID_PTR_CONVERSION_BADNESS;
4510 /* (b) pointer to ancestor-pointer conversion. */
4511 rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
4512 TYPE_TARGET_TYPE (arg),
4514 if (rank.subrank >= 0)
4515 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4517 return INCOMPATIBLE_TYPE_BADNESS;
4518 case TYPE_CODE_ARRAY:
4520 struct type *t1 = TYPE_TARGET_TYPE (parm);
4521 struct type *t2 = TYPE_TARGET_TYPE (arg);
4523 if (types_equal (t1, t2))
4525 /* Make sure they are CV equal. */
4526 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4527 rank.subrank |= CV_CONVERSION_CONST;
4528 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4529 rank.subrank |= CV_CONVERSION_VOLATILE;
4530 if (rank.subrank != 0)
4531 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4532 return EXACT_MATCH_BADNESS;
4534 return INCOMPATIBLE_TYPE_BADNESS;
4536 case TYPE_CODE_FUNC:
4537 return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
4539 if (value != NULL && value_type (value)->code () == TYPE_CODE_INT)
4541 if (value_as_long (value) == 0)
4543 /* Null pointer conversion: allow it to be cast to a pointer.
4544 [4.10.1 of C++ standard draft n3290] */
4545 return NULL_POINTER_CONVERSION_BADNESS;
4549 /* If type checking is disabled, allow the conversion. */
4550 if (!strict_type_checking)
4551 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4555 case TYPE_CODE_ENUM:
4556 case TYPE_CODE_FLAGS:
4557 case TYPE_CODE_CHAR:
4558 case TYPE_CODE_RANGE:
4559 case TYPE_CODE_BOOL:
4561 return INCOMPATIBLE_TYPE_BADNESS;
4565 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4568 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4570 switch (arg->code ())
4573 case TYPE_CODE_ARRAY:
4574 return rank_one_type (TYPE_TARGET_TYPE (parm),
4575 TYPE_TARGET_TYPE (arg), NULL);
4577 return INCOMPATIBLE_TYPE_BADNESS;
4581 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4584 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4586 switch (arg->code ())
4588 case TYPE_CODE_PTR: /* funcptr -> func */
4589 return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
4591 return INCOMPATIBLE_TYPE_BADNESS;
4595 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4598 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4600 switch (arg->code ())
4603 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
4605 /* Deal with signed, unsigned, and plain chars and
4606 signed and unsigned ints. */
4607 if (parm->has_no_signedness ())
4609 /* This case only for character types. */
4610 if (arg->has_no_signedness ())
4611 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4612 else /* signed/unsigned char -> plain char */
4613 return INTEGER_CONVERSION_BADNESS;
4615 else if (parm->is_unsigned ())
4617 if (arg->is_unsigned ())
4619 /* unsigned int -> unsigned int, or
4620 unsigned long -> unsigned long */
4621 if (integer_types_same_name_p (parm->name (),
4623 return EXACT_MATCH_BADNESS;
4624 else if (integer_types_same_name_p (arg->name (),
4626 && integer_types_same_name_p (parm->name (),
4628 /* unsigned int -> unsigned long */
4629 return INTEGER_PROMOTION_BADNESS;
4631 /* unsigned long -> unsigned int */
4632 return INTEGER_CONVERSION_BADNESS;
4636 if (integer_types_same_name_p (arg->name (),
4638 && integer_types_same_name_p (parm->name (),
4640 /* signed long -> unsigned int */
4641 return INTEGER_CONVERSION_BADNESS;
4643 /* signed int/long -> unsigned int/long */
4644 return INTEGER_CONVERSION_BADNESS;
4647 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4649 if (integer_types_same_name_p (parm->name (),
4651 return EXACT_MATCH_BADNESS;
4652 else if (integer_types_same_name_p (arg->name (),
4654 && integer_types_same_name_p (parm->name (),
4656 return INTEGER_PROMOTION_BADNESS;
4658 return INTEGER_CONVERSION_BADNESS;
4661 return INTEGER_CONVERSION_BADNESS;
4663 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4664 return INTEGER_PROMOTION_BADNESS;
4666 return INTEGER_CONVERSION_BADNESS;
4667 case TYPE_CODE_ENUM:
4668 case TYPE_CODE_FLAGS:
4669 case TYPE_CODE_CHAR:
4670 case TYPE_CODE_RANGE:
4671 case TYPE_CODE_BOOL:
4672 if (arg->is_declared_class ())
4673 return INCOMPATIBLE_TYPE_BADNESS;
4674 return INTEGER_PROMOTION_BADNESS;
4676 return INT_FLOAT_CONVERSION_BADNESS;
4678 return NS_POINTER_CONVERSION_BADNESS;
4680 return INCOMPATIBLE_TYPE_BADNESS;
4684 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4687 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4689 switch (arg->code ())
4692 case TYPE_CODE_CHAR:
4693 case TYPE_CODE_RANGE:
4694 case TYPE_CODE_BOOL:
4695 case TYPE_CODE_ENUM:
4696 if (parm->is_declared_class () || arg->is_declared_class ())
4697 return INCOMPATIBLE_TYPE_BADNESS;
4698 return INTEGER_CONVERSION_BADNESS;
4700 return INT_FLOAT_CONVERSION_BADNESS;
4702 return INCOMPATIBLE_TYPE_BADNESS;
4706 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4709 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4711 switch (arg->code ())
4713 case TYPE_CODE_RANGE:
4714 case TYPE_CODE_BOOL:
4715 case TYPE_CODE_ENUM:
4716 if (arg->is_declared_class ())
4717 return INCOMPATIBLE_TYPE_BADNESS;
4718 return INTEGER_CONVERSION_BADNESS;
4720 return INT_FLOAT_CONVERSION_BADNESS;
4722 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
4723 return INTEGER_CONVERSION_BADNESS;
4724 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4725 return INTEGER_PROMOTION_BADNESS;
4727 case TYPE_CODE_CHAR:
4728 /* Deal with signed, unsigned, and plain chars for C++ and
4729 with int cases falling through from previous case. */
4730 if (parm->has_no_signedness ())
4732 if (arg->has_no_signedness ())
4733 return EXACT_MATCH_BADNESS;
4735 return INTEGER_CONVERSION_BADNESS;
4737 else if (parm->is_unsigned ())
4739 if (arg->is_unsigned ())
4740 return EXACT_MATCH_BADNESS;
4742 return INTEGER_PROMOTION_BADNESS;
4744 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4745 return EXACT_MATCH_BADNESS;
4747 return INTEGER_CONVERSION_BADNESS;
4749 return INCOMPATIBLE_TYPE_BADNESS;
4753 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4756 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4758 switch (arg->code ())
4761 case TYPE_CODE_CHAR:
4762 case TYPE_CODE_RANGE:
4763 case TYPE_CODE_BOOL:
4764 case TYPE_CODE_ENUM:
4765 return INTEGER_CONVERSION_BADNESS;
4767 return INT_FLOAT_CONVERSION_BADNESS;
4769 return INCOMPATIBLE_TYPE_BADNESS;
4773 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4776 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4778 switch (arg->code ())
4780 /* n3290 draft, section 4.12.1 (conv.bool):
4782 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4783 pointer to member type can be converted to a prvalue of type
4784 bool. A zero value, null pointer value, or null member pointer
4785 value is converted to false; any other value is converted to
4786 true. A prvalue of type std::nullptr_t can be converted to a
4787 prvalue of type bool; the resulting value is false." */
4789 case TYPE_CODE_CHAR:
4790 case TYPE_CODE_ENUM:
4792 case TYPE_CODE_MEMBERPTR:
4794 return BOOL_CONVERSION_BADNESS;
4795 case TYPE_CODE_RANGE:
4796 return INCOMPATIBLE_TYPE_BADNESS;
4797 case TYPE_CODE_BOOL:
4798 return EXACT_MATCH_BADNESS;
4800 return INCOMPATIBLE_TYPE_BADNESS;
4804 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4807 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4809 switch (arg->code ())
4812 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4813 return FLOAT_PROMOTION_BADNESS;
4814 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
4815 return EXACT_MATCH_BADNESS;
4817 return FLOAT_CONVERSION_BADNESS;
4819 case TYPE_CODE_BOOL:
4820 case TYPE_CODE_ENUM:
4821 case TYPE_CODE_RANGE:
4822 case TYPE_CODE_CHAR:
4823 return INT_FLOAT_CONVERSION_BADNESS;
4825 return INCOMPATIBLE_TYPE_BADNESS;
4829 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4832 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4834 switch (arg->code ())
4835 { /* Strictly not needed for C++, but... */
4837 return FLOAT_PROMOTION_BADNESS;
4838 case TYPE_CODE_COMPLEX:
4839 return EXACT_MATCH_BADNESS;
4841 return INCOMPATIBLE_TYPE_BADNESS;
4845 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4848 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4850 struct rank rank = {0, 0};
4852 switch (arg->code ())
4854 case TYPE_CODE_STRUCT:
4855 /* Check for derivation */
4856 rank.subrank = distance_to_ancestor (parm, arg, 0);
4857 if (rank.subrank >= 0)
4858 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4861 return INCOMPATIBLE_TYPE_BADNESS;
4865 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4868 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4870 switch (arg->code ())
4874 return rank_one_type (parm->field (0).type (),
4875 arg->field (0).type (), NULL);
4877 return INCOMPATIBLE_TYPE_BADNESS;
4881 /* Compare one type (PARM) for compatibility with another (ARG).
4882 * PARM is intended to be the parameter type of a function; and
4883 * ARG is the supplied argument's type. This function tests if
4884 * the latter can be converted to the former.
4885 * VALUE is the argument's value or NULL if none (or called recursively)
4887 * Return 0 if they are identical types;
4888 * Otherwise, return an integer which corresponds to how compatible
4889 * PARM is to ARG. The higher the return value, the worse the match.
4890 * Generally the "bad" conversions are all uniformly assigned
4891 * INVALID_CONVERSION. */
4894 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4896 struct rank rank = {0,0};
4898 /* Resolve typedefs */
4899 if (parm->code () == TYPE_CODE_TYPEDEF)
4900 parm = check_typedef (parm);
4901 if (arg->code () == TYPE_CODE_TYPEDEF)
4902 arg = check_typedef (arg);
4904 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4906 if (VALUE_LVAL (value) == not_lval)
4908 /* Rvalues should preferably bind to rvalue references or const
4909 lvalue references. */
4910 if (parm->code () == TYPE_CODE_RVALUE_REF)
4911 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4912 else if (TYPE_CONST (TYPE_TARGET_TYPE (parm)))
4913 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4915 return INCOMPATIBLE_TYPE_BADNESS;
4916 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4920 /* It's illegal to pass an lvalue as an rvalue. */
4921 if (parm->code () == TYPE_CODE_RVALUE_REF)
4922 return INCOMPATIBLE_TYPE_BADNESS;
4926 if (types_equal (parm, arg))
4928 struct type *t1 = parm;
4929 struct type *t2 = arg;
4931 /* For pointers and references, compare target type. */
4932 if (parm->is_pointer_or_reference ())
4934 t1 = TYPE_TARGET_TYPE (parm);
4935 t2 = TYPE_TARGET_TYPE (arg);
4938 /* Make sure they are CV equal, too. */
4939 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4940 rank.subrank |= CV_CONVERSION_CONST;
4941 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4942 rank.subrank |= CV_CONVERSION_VOLATILE;
4943 if (rank.subrank != 0)
4944 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4945 return EXACT_MATCH_BADNESS;
4948 /* See through references, since we can almost make non-references
4951 if (TYPE_IS_REFERENCE (arg))
4952 return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
4953 REFERENCE_SEE_THROUGH_BADNESS));
4954 if (TYPE_IS_REFERENCE (parm))
4955 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
4956 REFERENCE_SEE_THROUGH_BADNESS));
4959 /* Debugging only. */
4960 gdb_printf (gdb_stderr,
4961 "------ Arg is %s [%d], parm is %s [%d]\n",
4962 arg->name (), arg->code (),
4963 parm->name (), parm->code ());
4966 /* x -> y means arg of type x being supplied for parameter of type y. */
4968 switch (parm->code ())
4971 return rank_one_type_parm_ptr (parm, arg, value);
4972 case TYPE_CODE_ARRAY:
4973 return rank_one_type_parm_array (parm, arg, value);
4974 case TYPE_CODE_FUNC:
4975 return rank_one_type_parm_func (parm, arg, value);
4977 return rank_one_type_parm_int (parm, arg, value);
4978 case TYPE_CODE_ENUM:
4979 return rank_one_type_parm_enum (parm, arg, value);
4980 case TYPE_CODE_CHAR:
4981 return rank_one_type_parm_char (parm, arg, value);
4982 case TYPE_CODE_RANGE:
4983 return rank_one_type_parm_range (parm, arg, value);
4984 case TYPE_CODE_BOOL:
4985 return rank_one_type_parm_bool (parm, arg, value);
4987 return rank_one_type_parm_float (parm, arg, value);
4988 case TYPE_CODE_COMPLEX:
4989 return rank_one_type_parm_complex (parm, arg, value);
4990 case TYPE_CODE_STRUCT:
4991 return rank_one_type_parm_struct (parm, arg, value);
4993 return rank_one_type_parm_set (parm, arg, value);
4995 return INCOMPATIBLE_TYPE_BADNESS;
4996 } /* switch (arg->code ()) */
4999 /* End of functions for overload resolution. */
5001 /* Routines to pretty-print types. */
5004 print_bit_vector (B_TYPE *bits, int nbits)
5008 for (bitno = 0; bitno < nbits; bitno++)
5010 if ((bitno % 8) == 0)
5014 if (B_TST (bits, bitno))
5021 /* Note the first arg should be the "this" pointer, we may not want to
5022 include it since we may get into a infinitely recursive
5026 print_args (struct field *args, int nargs, int spaces)
5032 for (i = 0; i < nargs; i++)
5035 ("%*s[%d] name '%s'\n", spaces, "", i,
5036 args[i].name () != NULL ? args[i].name () : "<NULL>");
5037 recursive_dump_type (args[i].type (), spaces + 2);
5043 field_is_static (struct field *f)
5045 /* "static" fields are the fields whose location is not relative
5046 to the address of the enclosing struct. It would be nice to
5047 have a dedicated flag that would be set for static fields when
5048 the type is being created. But in practice, checking the field
5049 loc_kind should give us an accurate answer. */
5050 return (f->loc_kind () == FIELD_LOC_KIND_PHYSNAME
5051 || f->loc_kind () == FIELD_LOC_KIND_PHYSADDR);
5055 dump_fn_fieldlists (struct type *type, int spaces)
5061 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
5062 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
5063 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
5065 f = TYPE_FN_FIELDLIST1 (type, method_idx);
5067 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
5069 TYPE_FN_FIELDLIST_NAME (type, method_idx),
5070 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
5071 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
5072 for (overload_idx = 0;
5073 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
5077 ("%*s[%d] physname '%s' (%s)\n",
5078 spaces + 4, "", overload_idx,
5079 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
5080 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
5083 ("%*stype %s\n", spaces + 8, "",
5084 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
5086 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
5090 ("%*sargs %s\n", spaces + 8, "",
5091 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
5092 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
5093 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
5096 ("%*sfcontext %s\n", spaces + 8, "",
5097 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
5100 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
5101 TYPE_FN_FIELD_CONST (f, overload_idx));
5102 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
5103 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
5104 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
5105 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
5106 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
5107 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
5108 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
5109 TYPE_FN_FIELD_STUB (f, overload_idx));
5110 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
5111 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
5112 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
5113 TYPE_FN_FIELD_DELETED (f, overload_idx));
5114 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
5115 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
5121 print_cplus_stuff (struct type *type, int spaces)
5123 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
5124 TYPE_VPTR_FIELDNO (type));
5125 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
5126 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
5127 if (TYPE_VPTR_BASETYPE (type) != NULL)
5128 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
5130 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
5131 TYPE_N_BASECLASSES (type));
5132 gdb_printf ("%*snfn_fields %d\n", spaces, "",
5133 TYPE_NFN_FIELDS (type));
5134 if (TYPE_N_BASECLASSES (type) > 0)
5137 ("%*svirtual_field_bits (%d bits at *%s)",
5138 spaces, "", TYPE_N_BASECLASSES (type),
5139 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type)));
5141 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
5142 TYPE_N_BASECLASSES (type));
5145 if (type->num_fields () > 0)
5147 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
5150 ("%*sprivate_field_bits (%d bits at *%s)",
5151 spaces, "", type->num_fields (),
5152 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type)));
5153 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
5154 type->num_fields ());
5157 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
5160 ("%*sprotected_field_bits (%d bits at *%s",
5161 spaces, "", type->num_fields (),
5162 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type)));
5163 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
5164 type->num_fields ());
5168 if (TYPE_NFN_FIELDS (type) > 0)
5170 dump_fn_fieldlists (type, spaces);
5173 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5174 TYPE_CPLUS_CALLING_CONVENTION (type));
5177 /* Print the contents of the TYPE's type_specific union, assuming that
5178 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5181 print_gnat_stuff (struct type *type, int spaces)
5183 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5185 if (descriptive_type == NULL)
5186 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5189 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5190 recursive_dump_type (descriptive_type, spaces + 4);
5194 /* Print the contents of the TYPE's type_specific union, assuming that
5195 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5198 print_fixed_point_type_info (struct type *type, int spaces)
5200 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5201 type->fixed_point_scaling_factor ().str ().c_str ());
5204 static struct obstack dont_print_type_obstack;
5206 /* Print the dynamic_prop PROP. */
5209 dump_dynamic_prop (dynamic_prop const& prop)
5211 switch (prop.kind ())
5214 gdb_printf ("%s", plongest (prop.const_val ()));
5216 case PROP_UNDEFINED:
5217 gdb_printf ("(undefined)");
5221 gdb_printf ("(dynamic)");
5224 gdb_assert_not_reached ("unhandled prop kind");
5230 recursive_dump_type (struct type *type, int spaces)
5235 obstack_begin (&dont_print_type_obstack, 0);
5237 if (type->num_fields () > 0
5238 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5240 struct type **first_dont_print
5241 = (struct type **) obstack_base (&dont_print_type_obstack);
5243 int i = (struct type **)
5244 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5248 if (type == first_dont_print[i])
5250 gdb_printf ("%*stype node %s", spaces, "",
5251 host_address_to_string (type));
5252 gdb_printf (_(" <same as already seen type>\n"));
5257 obstack_ptr_grow (&dont_print_type_obstack, type);
5260 gdb_printf ("%*stype node %s\n", spaces, "",
5261 host_address_to_string (type));
5262 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5263 type->name () ? type->name () : "<NULL>",
5264 host_address_to_string (type->name ()));
5265 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5266 switch (type->code ())
5268 case TYPE_CODE_UNDEF:
5269 gdb_printf ("(TYPE_CODE_UNDEF)");
5272 gdb_printf ("(TYPE_CODE_PTR)");
5274 case TYPE_CODE_ARRAY:
5275 gdb_printf ("(TYPE_CODE_ARRAY)");
5277 case TYPE_CODE_STRUCT:
5278 gdb_printf ("(TYPE_CODE_STRUCT)");
5280 case TYPE_CODE_UNION:
5281 gdb_printf ("(TYPE_CODE_UNION)");
5283 case TYPE_CODE_ENUM:
5284 gdb_printf ("(TYPE_CODE_ENUM)");
5286 case TYPE_CODE_FLAGS:
5287 gdb_printf ("(TYPE_CODE_FLAGS)");
5289 case TYPE_CODE_FUNC:
5290 gdb_printf ("(TYPE_CODE_FUNC)");
5293 gdb_printf ("(TYPE_CODE_INT)");
5296 gdb_printf ("(TYPE_CODE_FLT)");
5298 case TYPE_CODE_VOID:
5299 gdb_printf ("(TYPE_CODE_VOID)");
5302 gdb_printf ("(TYPE_CODE_SET)");
5304 case TYPE_CODE_RANGE:
5305 gdb_printf ("(TYPE_CODE_RANGE)");
5307 case TYPE_CODE_STRING:
5308 gdb_printf ("(TYPE_CODE_STRING)");
5310 case TYPE_CODE_ERROR:
5311 gdb_printf ("(TYPE_CODE_ERROR)");
5313 case TYPE_CODE_MEMBERPTR:
5314 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5316 case TYPE_CODE_METHODPTR:
5317 gdb_printf ("(TYPE_CODE_METHODPTR)");
5319 case TYPE_CODE_METHOD:
5320 gdb_printf ("(TYPE_CODE_METHOD)");
5323 gdb_printf ("(TYPE_CODE_REF)");
5325 case TYPE_CODE_CHAR:
5326 gdb_printf ("(TYPE_CODE_CHAR)");
5328 case TYPE_CODE_BOOL:
5329 gdb_printf ("(TYPE_CODE_BOOL)");
5331 case TYPE_CODE_COMPLEX:
5332 gdb_printf ("(TYPE_CODE_COMPLEX)");
5334 case TYPE_CODE_TYPEDEF:
5335 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5337 case TYPE_CODE_NAMESPACE:
5338 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5340 case TYPE_CODE_FIXED_POINT:
5341 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5344 gdb_printf ("(UNKNOWN TYPE CODE)");
5348 gdb_printf ("%*slength %s\n", spaces, "",
5349 pulongest (TYPE_LENGTH (type)));
5350 if (type->is_objfile_owned ())
5351 gdb_printf ("%*sobjfile %s\n", spaces, "",
5352 host_address_to_string (type->objfile_owner ()));
5354 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5355 host_address_to_string (type->arch_owner ()));
5356 gdb_printf ("%*starget_type %s\n", spaces, "",
5357 host_address_to_string (TYPE_TARGET_TYPE (type)));
5358 if (TYPE_TARGET_TYPE (type) != NULL)
5360 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
5362 gdb_printf ("%*spointer_type %s\n", spaces, "",
5363 host_address_to_string (TYPE_POINTER_TYPE (type)));
5364 gdb_printf ("%*sreference_type %s\n", spaces, "",
5365 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5366 gdb_printf ("%*stype_chain %s\n", spaces, "",
5367 host_address_to_string (TYPE_CHAIN (type)));
5368 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5369 (unsigned) type->instance_flags ());
5370 if (TYPE_CONST (type))
5372 gdb_puts (" TYPE_CONST");
5374 if (TYPE_VOLATILE (type))
5376 gdb_puts (" TYPE_VOLATILE");
5378 if (TYPE_CODE_SPACE (type))
5380 gdb_puts (" TYPE_CODE_SPACE");
5382 if (TYPE_DATA_SPACE (type))
5384 gdb_puts (" TYPE_DATA_SPACE");
5386 if (TYPE_ADDRESS_CLASS_1 (type))
5388 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5390 if (TYPE_ADDRESS_CLASS_2 (type))
5392 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5394 if (TYPE_RESTRICT (type))
5396 gdb_puts (" TYPE_RESTRICT");
5398 if (TYPE_ATOMIC (type))
5400 gdb_puts (" TYPE_ATOMIC");
5404 gdb_printf ("%*sflags", spaces, "");
5405 if (type->is_unsigned ())
5407 gdb_puts (" TYPE_UNSIGNED");
5409 if (type->has_no_signedness ())
5411 gdb_puts (" TYPE_NOSIGN");
5413 if (type->endianity_is_not_default ())
5415 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5417 if (type->is_stub ())
5419 gdb_puts (" TYPE_STUB");
5421 if (type->target_is_stub ())
5423 gdb_puts (" TYPE_TARGET_STUB");
5425 if (type->is_prototyped ())
5427 gdb_puts (" TYPE_PROTOTYPED");
5429 if (type->has_varargs ())
5431 gdb_puts (" TYPE_VARARGS");
5433 /* This is used for things like AltiVec registers on ppc. Gcc emits
5434 an attribute for the array type, which tells whether or not we
5435 have a vector, instead of a regular array. */
5436 if (type->is_vector ())
5438 gdb_puts (" TYPE_VECTOR");
5440 if (type->is_fixed_instance ())
5442 gdb_puts (" TYPE_FIXED_INSTANCE");
5444 if (type->stub_is_supported ())
5446 gdb_puts (" TYPE_STUB_SUPPORTED");
5448 if (TYPE_NOTTEXT (type))
5450 gdb_puts (" TYPE_NOTTEXT");
5453 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5454 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5455 || TYPE_ALLOCATED_PROP (type) != nullptr)
5457 gdb_printf ("%*s", spaces, "");
5458 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5460 gdb_printf ("associated ");
5461 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5463 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5465 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5467 gdb_printf ("allocated ");
5468 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5472 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5473 for (idx = 0; idx < type->num_fields (); idx++)
5475 if (type->code () == TYPE_CODE_ENUM)
5476 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5477 idx, plongest (type->field (idx).loc_enumval ()));
5479 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5480 idx, plongest (type->field (idx).loc_bitpos ()),
5481 TYPE_FIELD_BITSIZE (type, idx));
5482 gdb_printf ("%s name '%s' (%s)\n",
5483 host_address_to_string (type->field (idx).type ()),
5484 type->field (idx).name () != NULL
5485 ? type->field (idx).name ()
5487 host_address_to_string (type->field (idx).name ()));
5488 if (type->field (idx).type () != NULL)
5490 recursive_dump_type (type->field (idx).type (), spaces + 4);
5493 if (type->code () == TYPE_CODE_RANGE)
5495 gdb_printf ("%*slow ", spaces, "");
5496 dump_dynamic_prop (type->bounds ()->low);
5497 gdb_printf (" high ");
5498 dump_dynamic_prop (type->bounds ()->high);
5502 switch (TYPE_SPECIFIC_FIELD (type))
5504 case TYPE_SPECIFIC_CPLUS_STUFF:
5505 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5506 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5507 print_cplus_stuff (type, spaces);
5510 case TYPE_SPECIFIC_GNAT_STUFF:
5511 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5512 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5513 print_gnat_stuff (type, spaces);
5516 case TYPE_SPECIFIC_FLOATFORMAT:
5517 gdb_printf ("%*sfloatformat ", spaces, "");
5518 if (TYPE_FLOATFORMAT (type) == NULL
5519 || TYPE_FLOATFORMAT (type)->name == NULL)
5520 gdb_puts ("(null)");
5522 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5526 case TYPE_SPECIFIC_FUNC:
5527 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5528 TYPE_CALLING_CONVENTION (type));
5529 /* tail_call_list is not printed. */
5532 case TYPE_SPECIFIC_SELF_TYPE:
5533 gdb_printf ("%*sself_type %s\n", spaces, "",
5534 host_address_to_string (TYPE_SELF_TYPE (type)));
5537 case TYPE_SPECIFIC_FIXED_POINT:
5538 gdb_printf ("%*sfixed_point_info ", spaces, "");
5539 print_fixed_point_type_info (type, spaces);
5543 case TYPE_SPECIFIC_INT:
5544 if (type->bit_size_differs_p ())
5546 unsigned bit_size = type->bit_size ();
5547 unsigned bit_off = type->bit_offset ();
5548 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5555 obstack_free (&dont_print_type_obstack, NULL);
5558 /* Trivial helpers for the libiberty hash table, for mapping one
5563 type_pair (struct type *old_, struct type *newobj_)
5564 : old (old_), newobj (newobj_)
5567 struct type * const old, * const newobj;
5571 type_pair_hash (const void *item)
5573 const struct type_pair *pair = (const struct type_pair *) item;
5575 return htab_hash_pointer (pair->old);
5579 type_pair_eq (const void *item_lhs, const void *item_rhs)
5581 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5582 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5584 return lhs->old == rhs->old;
5587 /* Allocate the hash table used by copy_type_recursive to walk
5588 types without duplicates. */
5591 create_copied_types_hash ()
5593 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5594 htab_delete_entry<type_pair>,
5598 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5600 static struct dynamic_prop_list *
5601 copy_dynamic_prop_list (struct obstack *storage,
5602 struct dynamic_prop_list *list)
5604 struct dynamic_prop_list *copy = list;
5605 struct dynamic_prop_list **node_ptr = ©
5607 while (*node_ptr != NULL)
5609 struct dynamic_prop_list *node_copy;
5611 node_copy = ((struct dynamic_prop_list *)
5612 obstack_copy (storage, *node_ptr,
5613 sizeof (struct dynamic_prop_list)));
5614 node_copy->prop = (*node_ptr)->prop;
5615 *node_ptr = node_copy;
5617 node_ptr = &node_copy->next;
5623 /* Recursively copy (deep copy) TYPE, if it is associated with
5624 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5625 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5626 it is not associated with OBJFILE. */
5629 copy_type_recursive (struct type *type, htab_t copied_types)
5632 struct type *new_type;
5634 if (!type->is_objfile_owned ())
5637 struct type_pair pair (type, nullptr);
5639 slot = htab_find_slot (copied_types, &pair, INSERT);
5641 return ((struct type_pair *) *slot)->newobj;
5643 new_type = alloc_type_arch (type->arch ());
5645 /* We must add the new type to the hash table immediately, in case
5646 we encounter this type again during a recursive call below. */
5647 struct type_pair *stored = new type_pair (type, new_type);
5651 /* Copy the common fields of types. For the main type, we simply
5652 copy the entire thing and then update specific fields as needed. */
5653 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5655 new_type->set_owner (type->arch ());
5658 new_type->set_name (xstrdup (type->name ()));
5660 new_type->set_instance_flags (type->instance_flags ());
5661 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
5663 /* Copy the fields. */
5664 if (type->num_fields ())
5668 nfields = type->num_fields ();
5669 new_type->set_fields
5671 TYPE_ZALLOC (new_type, nfields * sizeof (struct field)));
5673 for (i = 0; i < nfields; i++)
5675 TYPE_FIELD_ARTIFICIAL (new_type, i) =
5676 TYPE_FIELD_ARTIFICIAL (type, i);
5677 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
5678 if (type->field (i).type ())
5679 new_type->field (i).set_type
5680 (copy_type_recursive (type->field (i).type (), copied_types));
5681 if (type->field (i).name ())
5682 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5684 switch (type->field (i).loc_kind ())
5686 case FIELD_LOC_KIND_BITPOS:
5687 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5689 case FIELD_LOC_KIND_ENUMVAL:
5690 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5692 case FIELD_LOC_KIND_PHYSADDR:
5693 new_type->field (i).set_loc_physaddr
5694 (type->field (i).loc_physaddr ());
5696 case FIELD_LOC_KIND_PHYSNAME:
5697 new_type->field (i).set_loc_physname
5698 (xstrdup (type->field (i).loc_physname ()));
5700 case FIELD_LOC_KIND_DWARF_BLOCK:
5701 new_type->field (i).set_loc_dwarf_block
5702 (type->field (i).loc_dwarf_block ());
5705 internal_error (__FILE__, __LINE__,
5706 _("Unexpected type field location kind: %d"),
5707 type->field (i).loc_kind ());
5712 /* For range types, copy the bounds information. */
5713 if (type->code () == TYPE_CODE_RANGE)
5715 range_bounds *bounds
5716 = ((struct range_bounds *) TYPE_ALLOC
5717 (new_type, sizeof (struct range_bounds)));
5719 *bounds = *type->bounds ();
5720 new_type->set_bounds (bounds);
5723 if (type->main_type->dyn_prop_list != NULL)
5724 new_type->main_type->dyn_prop_list
5725 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5726 type->main_type->dyn_prop_list);
5729 /* Copy pointers to other types. */
5730 if (TYPE_TARGET_TYPE (type))
5731 new_type->set_target_type
5732 (copy_type_recursive (TYPE_TARGET_TYPE (type), copied_types));
5734 /* Maybe copy the type_specific bits.
5736 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5737 base classes and methods. There's no fundamental reason why we
5738 can't, but at the moment it is not needed. */
5740 switch (TYPE_SPECIFIC_FIELD (type))
5742 case TYPE_SPECIFIC_NONE:
5744 case TYPE_SPECIFIC_FUNC:
5745 INIT_FUNC_SPECIFIC (new_type);
5746 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5747 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5748 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5750 case TYPE_SPECIFIC_FLOATFORMAT:
5751 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5753 case TYPE_SPECIFIC_CPLUS_STUFF:
5754 INIT_CPLUS_SPECIFIC (new_type);
5756 case TYPE_SPECIFIC_GNAT_STUFF:
5757 INIT_GNAT_SPECIFIC (new_type);
5759 case TYPE_SPECIFIC_SELF_TYPE:
5760 set_type_self_type (new_type,
5761 copy_type_recursive (TYPE_SELF_TYPE (type),
5764 case TYPE_SPECIFIC_FIXED_POINT:
5765 INIT_FIXED_POINT_SPECIFIC (new_type);
5766 new_type->fixed_point_info ().scaling_factor
5767 = type->fixed_point_info ().scaling_factor;
5769 case TYPE_SPECIFIC_INT:
5770 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5771 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5772 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5776 gdb_assert_not_reached ("bad type_specific_kind");
5782 /* Make a copy of the given TYPE, except that the pointer & reference
5783 types are not preserved. */
5786 copy_type (const struct type *type)
5788 struct type *new_type = alloc_type_copy (type);
5789 new_type->set_instance_flags (type->instance_flags ());
5790 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
5791 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5792 sizeof (struct main_type));
5793 if (type->main_type->dyn_prop_list != NULL)
5795 struct obstack *storage = (type->is_objfile_owned ()
5796 ? &type->objfile_owner ()->objfile_obstack
5797 : gdbarch_obstack (type->arch_owner ()));
5798 new_type->main_type->dyn_prop_list
5799 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5805 /* Helper functions to initialize architecture-specific types. */
5807 /* Allocate a type structure associated with GDBARCH and set its
5808 CODE, LENGTH, and NAME fields. */
5811 arch_type (struct gdbarch *gdbarch,
5812 enum type_code code, int bit, const char *name)
5816 type = alloc_type_arch (gdbarch);
5817 set_type_code (type, code);
5818 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
5819 TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
5822 type->set_name (gdbarch_obstack_strdup (gdbarch, name));
5827 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
5828 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5829 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5832 arch_integer_type (struct gdbarch *gdbarch,
5833 int bit, int unsigned_p, const char *name)
5837 t = arch_type (gdbarch, TYPE_CODE_INT, bit, name);
5839 t->set_is_unsigned (true);
5844 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
5845 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5846 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5849 arch_character_type (struct gdbarch *gdbarch,
5850 int bit, int unsigned_p, const char *name)
5854 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit, name);
5856 t->set_is_unsigned (true);
5861 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
5862 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5863 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5866 arch_boolean_type (struct gdbarch *gdbarch,
5867 int bit, int unsigned_p, const char *name)
5871 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit, name);
5873 t->set_is_unsigned (true);
5878 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
5879 BIT is the type size in bits; if BIT equals -1, the size is
5880 determined by the floatformat. NAME is the type name. Set the
5881 TYPE_FLOATFORMAT from FLOATFORMATS. */
5884 arch_float_type (struct gdbarch *gdbarch,
5885 int bit, const char *name,
5886 const struct floatformat **floatformats)
5888 const struct floatformat *fmt = floatformats[gdbarch_byte_order (gdbarch)];
5891 bit = verify_floatformat (bit, fmt);
5892 t = arch_type (gdbarch, TYPE_CODE_FLT, bit, name);
5893 TYPE_FLOATFORMAT (t) = fmt;
5898 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
5899 BIT is the type size in bits. NAME is the type name. */
5902 arch_decfloat_type (struct gdbarch *gdbarch, int bit, const char *name)
5906 t = arch_type (gdbarch, TYPE_CODE_DECFLOAT, bit, name);
5910 /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
5911 BIT is the pointer type size in bits. NAME is the type name.
5912 TARGET_TYPE is the pointer target type. Always sets the pointer type's
5913 TYPE_UNSIGNED flag. */
5916 arch_pointer_type (struct gdbarch *gdbarch,
5917 int bit, const char *name, struct type *target_type)
5921 t = arch_type (gdbarch, TYPE_CODE_PTR, bit, name);
5922 t->set_target_type (target_type);
5923 t->set_is_unsigned (true);
5927 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5928 NAME is the type name. BIT is the size of the flag word in bits. */
5931 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5935 type = arch_type (gdbarch, TYPE_CODE_FLAGS, bit, name);
5936 type->set_is_unsigned (true);
5937 type->set_num_fields (0);
5938 /* Pre-allocate enough space assuming every field is one bit. */
5940 ((struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field)));
5945 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5946 position BITPOS is called NAME. Pass NAME as "" for fields that
5947 should not be printed. */
5950 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5951 struct type *field_type, const char *name)
5953 int type_bitsize = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
5954 int field_nr = type->num_fields ();
5956 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5957 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5958 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5959 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5960 gdb_assert (name != NULL);
5962 type->set_num_fields (type->num_fields () + 1);
5963 type->field (field_nr).set_name (xstrdup (name));
5964 type->field (field_nr).set_type (field_type);
5965 type->field (field_nr).set_loc_bitpos (start_bitpos);
5966 TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits;
5969 /* Special version of append_flags_type_field to add a flag field.
5970 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5971 position BITPOS is called NAME. */
5974 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5976 append_flags_type_field (type, bitpos, 1,
5977 builtin_type (type->arch ())->builtin_bool,
5981 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5982 specified by CODE) associated with GDBARCH. NAME is the type name. */
5985 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5986 enum type_code code)
5990 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5991 t = arch_type (gdbarch, code, 0, NULL);
5993 INIT_CPLUS_SPECIFIC (t);
5997 /* Add new field with name NAME and type FIELD to composite type T.
5998 Do not set the field's position or adjust the type's length;
5999 the caller should do so. Return the new field. */
6002 append_composite_type_field_raw (struct type *t, const char *name,
6007 t->set_num_fields (t->num_fields () + 1);
6008 t->set_fields (XRESIZEVEC (struct field, t->fields (),
6010 f = &t->field (t->num_fields () - 1);
6011 memset (f, 0, sizeof f[0]);
6012 f[0].set_type (field);
6013 f[0].set_name (name);
6017 /* Add new field with name NAME and type FIELD to composite type T.
6018 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
6021 append_composite_type_field_aligned (struct type *t, const char *name,
6022 struct type *field, int alignment)
6024 struct field *f = append_composite_type_field_raw (t, name, field);
6026 if (t->code () == TYPE_CODE_UNION)
6028 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
6029 TYPE_LENGTH (t) = TYPE_LENGTH (field);
6031 else if (t->code () == TYPE_CODE_STRUCT)
6033 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
6034 if (t->num_fields () > 1)
6037 (f[-1].loc_bitpos () + (TYPE_LENGTH (f[-1].type ()) * TARGET_CHAR_BIT));
6043 alignment *= TARGET_CHAR_BIT;
6044 left = f[0].loc_bitpos () % alignment;
6048 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
6049 TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
6056 /* Add new field with name NAME and type FIELD to composite type T. */
6059 append_composite_type_field (struct type *t, const char *name,
6062 append_composite_type_field_aligned (t, name, field, 0);
6067 /* We manage the lifetimes of fixed_point_type_info objects by
6068 attaching them to the objfile. Currently, these objects are
6069 modified during construction, and GMP does not provide a way to
6070 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
6071 them. If we did do this, they could be moved to the per-BFD and
6072 shared across objfiles. */
6073 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
6074 fixed_point_type_storage;
6076 /* Key used for managing the storage of fixed-point type info. */
6077 static const struct registry<objfile>::key<fixed_point_type_storage>
6078 fixed_point_objfile_key;
6080 /* See gdbtypes.h. */
6083 allocate_fixed_point_type_info (struct type *type)
6085 std::unique_ptr<fixed_point_type_info> up (new fixed_point_type_info);
6086 fixed_point_type_info *info;
6088 if (type->is_objfile_owned ())
6090 fixed_point_type_storage *storage
6091 = fixed_point_objfile_key.get (type->objfile_owner ());
6092 if (storage == nullptr)
6093 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
6095 storage->push_back (std::move (up));
6099 /* We just leak the memory, because that's what we do generally
6100 for non-objfile-attached types. */
6101 info = up.release ();
6104 type->set_fixed_point_info (info);
6107 /* See gdbtypes.h. */
6110 is_fixed_point_type (struct type *type)
6112 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
6113 type = TYPE_TARGET_TYPE (check_typedef (type));
6114 type = check_typedef (type);
6116 return type->code () == TYPE_CODE_FIXED_POINT;
6119 /* See gdbtypes.h. */
6122 type::fixed_point_type_base_type ()
6124 struct type *type = this;
6126 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
6127 type = TYPE_TARGET_TYPE (check_typedef (type));
6128 type = check_typedef (type);
6130 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
6134 /* See gdbtypes.h. */
6137 type::fixed_point_scaling_factor ()
6139 struct type *type = this->fixed_point_type_base_type ();
6141 return type->fixed_point_info ().scaling_factor;
6146 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
6148 static struct builtin_type *
6149 create_gdbtypes_data (struct gdbarch *gdbarch)
6151 struct builtin_type *builtin_type = new struct builtin_type;
6154 builtin_type->builtin_void
6155 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
6156 builtin_type->builtin_char
6157 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6158 !gdbarch_char_signed (gdbarch), "char");
6159 builtin_type->builtin_char->set_has_no_signedness (true);
6160 builtin_type->builtin_signed_char
6161 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6163 builtin_type->builtin_unsigned_char
6164 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6165 1, "unsigned char");
6166 builtin_type->builtin_short
6167 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
6169 builtin_type->builtin_unsigned_short
6170 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
6171 1, "unsigned short");
6172 builtin_type->builtin_int
6173 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
6175 builtin_type->builtin_unsigned_int
6176 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
6178 builtin_type->builtin_long
6179 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
6181 builtin_type->builtin_unsigned_long
6182 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
6183 1, "unsigned long");
6184 builtin_type->builtin_long_long
6185 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
6187 builtin_type->builtin_unsigned_long_long
6188 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
6189 1, "unsigned long long");
6190 builtin_type->builtin_half
6191 = arch_float_type (gdbarch, gdbarch_half_bit (gdbarch),
6192 "half", gdbarch_half_format (gdbarch));
6193 builtin_type->builtin_float
6194 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
6195 "float", gdbarch_float_format (gdbarch));
6196 builtin_type->builtin_bfloat16
6197 = arch_float_type (gdbarch, gdbarch_bfloat16_bit (gdbarch),
6198 "bfloat16", gdbarch_bfloat16_format (gdbarch));
6199 builtin_type->builtin_double
6200 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
6201 "double", gdbarch_double_format (gdbarch));
6202 builtin_type->builtin_long_double
6203 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
6204 "long double", gdbarch_long_double_format (gdbarch));
6205 builtin_type->builtin_complex
6206 = init_complex_type ("complex", builtin_type->builtin_float);
6207 builtin_type->builtin_double_complex
6208 = init_complex_type ("double complex", builtin_type->builtin_double);
6209 builtin_type->builtin_string
6210 = arch_type (gdbarch, TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
6211 builtin_type->builtin_bool
6212 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "bool");
6214 /* The following three are about decimal floating point types, which
6215 are 32-bits, 64-bits and 128-bits respectively. */
6216 builtin_type->builtin_decfloat
6217 = arch_decfloat_type (gdbarch, 32, "_Decimal32");
6218 builtin_type->builtin_decdouble
6219 = arch_decfloat_type (gdbarch, 64, "_Decimal64");
6220 builtin_type->builtin_declong
6221 = arch_decfloat_type (gdbarch, 128, "_Decimal128");
6223 /* "True" character types. */
6224 builtin_type->builtin_true_char
6225 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
6226 builtin_type->builtin_true_unsigned_char
6227 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
6229 /* Fixed-size integer types. */
6230 builtin_type->builtin_int0
6231 = arch_integer_type (gdbarch, 0, 0, "int0_t");
6232 builtin_type->builtin_int8
6233 = arch_integer_type (gdbarch, 8, 0, "int8_t");
6234 builtin_type->builtin_uint8
6235 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
6236 builtin_type->builtin_int16
6237 = arch_integer_type (gdbarch, 16, 0, "int16_t");
6238 builtin_type->builtin_uint16
6239 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
6240 builtin_type->builtin_int24
6241 = arch_integer_type (gdbarch, 24, 0, "int24_t");
6242 builtin_type->builtin_uint24
6243 = arch_integer_type (gdbarch, 24, 1, "uint24_t");
6244 builtin_type->builtin_int32
6245 = arch_integer_type (gdbarch, 32, 0, "int32_t");
6246 builtin_type->builtin_uint32
6247 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
6248 builtin_type->builtin_int64
6249 = arch_integer_type (gdbarch, 64, 0, "int64_t");
6250 builtin_type->builtin_uint64
6251 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
6252 builtin_type->builtin_int128
6253 = arch_integer_type (gdbarch, 128, 0, "int128_t");
6254 builtin_type->builtin_uint128
6255 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
6257 builtin_type->builtin_int8->set_instance_flags
6258 (builtin_type->builtin_int8->instance_flags ()
6259 | TYPE_INSTANCE_FLAG_NOTTEXT);
6261 builtin_type->builtin_uint8->set_instance_flags
6262 (builtin_type->builtin_uint8->instance_flags ()
6263 | TYPE_INSTANCE_FLAG_NOTTEXT);
6265 /* Wide character types. */
6266 builtin_type->builtin_char16
6267 = arch_integer_type (gdbarch, 16, 1, "char16_t");
6268 builtin_type->builtin_char32
6269 = arch_integer_type (gdbarch, 32, 1, "char32_t");
6270 builtin_type->builtin_wchar
6271 = arch_integer_type (gdbarch, gdbarch_wchar_bit (gdbarch),
6272 !gdbarch_wchar_signed (gdbarch), "wchar_t");
6274 /* Default data/code pointer types. */
6275 builtin_type->builtin_data_ptr
6276 = lookup_pointer_type (builtin_type->builtin_void);
6277 builtin_type->builtin_func_ptr
6278 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6279 builtin_type->builtin_func_func
6280 = lookup_function_type (builtin_type->builtin_func_ptr);
6282 /* This type represents a GDB internal function. */
6283 builtin_type->internal_fn
6284 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
6285 "<internal function>");
6287 /* This type represents an xmethod. */
6288 builtin_type->xmethod
6289 = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
6291 return builtin_type;
6294 const struct builtin_type *
6295 builtin_type (struct gdbarch *gdbarch)
6297 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6298 if (result == nullptr)
6300 result = create_gdbtypes_data (gdbarch);
6301 gdbtypes_data.set (gdbarch, result);
6306 /* This set of objfile-based types is intended to be used by symbol
6307 readers as basic types. */
6309 static const registry<objfile>::key<struct objfile_type,
6310 gdb::noop_deleter<struct objfile_type>>
6313 const struct objfile_type *
6314 objfile_type (struct objfile *objfile)
6316 struct gdbarch *gdbarch;
6317 struct objfile_type *objfile_type = objfile_type_data.get (objfile);
6320 return objfile_type;
6322 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
6323 1, struct objfile_type);
6325 /* Use the objfile architecture to determine basic type properties. */
6326 gdbarch = objfile->arch ();
6329 objfile_type->builtin_void
6330 = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
6331 objfile_type->builtin_char
6332 = init_integer_type (objfile, TARGET_CHAR_BIT,
6333 !gdbarch_char_signed (gdbarch), "char");
6334 objfile_type->builtin_char->set_has_no_signedness (true);
6335 objfile_type->builtin_signed_char
6336 = init_integer_type (objfile, TARGET_CHAR_BIT,
6338 objfile_type->builtin_unsigned_char
6339 = init_integer_type (objfile, TARGET_CHAR_BIT,
6340 1, "unsigned char");
6341 objfile_type->builtin_short
6342 = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
6344 objfile_type->builtin_unsigned_short
6345 = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
6346 1, "unsigned short");
6347 objfile_type->builtin_int
6348 = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
6350 objfile_type->builtin_unsigned_int
6351 = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
6353 objfile_type->builtin_long
6354 = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
6356 objfile_type->builtin_unsigned_long
6357 = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
6358 1, "unsigned long");
6359 objfile_type->builtin_long_long
6360 = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
6362 objfile_type->builtin_unsigned_long_long
6363 = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
6364 1, "unsigned long long");
6365 objfile_type->builtin_float
6366 = init_float_type (objfile, gdbarch_float_bit (gdbarch),
6367 "float", gdbarch_float_format (gdbarch));
6368 objfile_type->builtin_double
6369 = init_float_type (objfile, gdbarch_double_bit (gdbarch),
6370 "double", gdbarch_double_format (gdbarch));
6371 objfile_type->builtin_long_double
6372 = init_float_type (objfile, gdbarch_long_double_bit (gdbarch),
6373 "long double", gdbarch_long_double_format (gdbarch));
6375 /* This type represents a type that was unrecognized in symbol read-in. */
6376 objfile_type->builtin_error
6377 = init_type (objfile, TYPE_CODE_ERROR, 0, "<unknown type>");
6379 /* The following set of types is used for symbols with no
6380 debug information. */
6381 objfile_type->nodebug_text_symbol
6382 = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6383 "<text variable, no debug info>");
6385 objfile_type->nodebug_text_gnu_ifunc_symbol
6386 = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6387 "<text gnu-indirect-function variable, no debug info>");
6388 objfile_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6390 objfile_type->nodebug_got_plt_symbol
6391 = init_pointer_type (objfile, gdbarch_addr_bit (gdbarch),
6392 "<text from jump slot in .got.plt, no debug info>",
6393 objfile_type->nodebug_text_symbol);
6394 objfile_type->nodebug_data_symbol
6395 = init_nodebug_var_type (objfile, "<data variable, no debug info>");
6396 objfile_type->nodebug_unknown_symbol
6397 = init_nodebug_var_type (objfile, "<variable (not text or data), no debug info>");
6398 objfile_type->nodebug_tls_symbol
6399 = init_nodebug_var_type (objfile, "<thread local variable, no debug info>");
6401 /* NOTE: on some targets, addresses and pointers are not necessarily
6405 - gdb's `struct type' always describes the target's
6407 - gdb's `struct value' objects should always hold values in
6409 - gdb's CORE_ADDR values are addresses in the unified virtual
6410 address space that the assembler and linker work with. Thus,
6411 since target_read_memory takes a CORE_ADDR as an argument, it
6412 can access any memory on the target, even if the processor has
6413 separate code and data address spaces.
6415 In this context, objfile_type->builtin_core_addr is a bit odd:
6416 it's a target type for a value the target will never see. It's
6417 only used to hold the values of (typeless) linker symbols, which
6418 are indeed in the unified virtual address space. */
6420 objfile_type->builtin_core_addr
6421 = init_integer_type (objfile, gdbarch_addr_bit (gdbarch), 1,
6424 objfile_type_data.set (objfile, objfile_type);
6425 return objfile_type;
6428 /* See gdbtypes.h. */
6431 call_site::pc () const
6433 compunit_symtab *cust = this->per_objfile->get_symtab (this->per_cu);
6435 = this->per_objfile->objfile->section_offsets[cust->block_line_section ()];
6436 return m_unrelocated_pc + delta;
6439 void _initialize_gdbtypes ();
6441 _initialize_gdbtypes ()
6443 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6444 _("Set debugging of C++ overloading."),
6445 _("Show debugging of C++ overloading."),
6446 _("When enabled, ranking of the "
6447 "functions is displayed."),
6449 show_overload_debug,
6450 &setdebuglist, &showdebuglist);
6452 /* Add user knob for controlling resolution of opaque types. */
6453 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6454 &opaque_type_resolution,
6455 _("Set resolution of opaque struct/class/union"
6456 " types (if set before loading symbols)."),
6457 _("Show resolution of opaque struct/class/union"
6458 " types (if set before loading symbols)."),
6460 show_opaque_type_resolution,
6461 &setlist, &showlist);
6463 /* Add an option to permit non-strict type checking. */
6464 add_setshow_boolean_cmd ("type", class_support,
6465 &strict_type_checking,
6466 _("Set strict type checking."),
6467 _("Show strict type checking."),
6469 show_strict_type_checking,
6470 &setchecklist, &showchecklist);