1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2015 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 "dwarf2loc.h"
42 /* Initialize BADNESS constants. */
44 const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
46 const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
47 const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
49 const struct rank EXACT_MATCH_BADNESS = {0,0};
51 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
52 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
53 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
54 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
55 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
56 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
57 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
58 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
59 const struct rank BASE_CONVERSION_BADNESS = {2,0};
60 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
61 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
62 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
63 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
65 /* Floatformat pairs. */
66 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
67 &floatformat_ieee_half_big,
68 &floatformat_ieee_half_little
70 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
71 &floatformat_ieee_single_big,
72 &floatformat_ieee_single_little
74 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
75 &floatformat_ieee_double_big,
76 &floatformat_ieee_double_little
78 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
79 &floatformat_ieee_double_big,
80 &floatformat_ieee_double_littlebyte_bigword
82 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
83 &floatformat_i387_ext,
86 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
87 &floatformat_m68881_ext,
88 &floatformat_m68881_ext
90 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
91 &floatformat_arm_ext_big,
92 &floatformat_arm_ext_littlebyte_bigword
94 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
95 &floatformat_ia64_spill_big,
96 &floatformat_ia64_spill_little
98 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
99 &floatformat_ia64_quad_big,
100 &floatformat_ia64_quad_little
102 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
106 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
110 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
111 &floatformat_ibm_long_double_big,
112 &floatformat_ibm_long_double_little
115 /* Should opaque types be resolved? */
117 static int opaque_type_resolution = 1;
119 /* A flag to enable printing of debugging information of C++
122 unsigned int overload_debug = 0;
124 /* A flag to enable strict type checking. */
126 static int strict_type_checking = 1;
128 /* A function to show whether opaque types are resolved. */
131 show_opaque_type_resolution (struct ui_file *file, int from_tty,
132 struct cmd_list_element *c,
135 fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
136 "(if set before loading symbols) is %s.\n"),
140 /* A function to show whether C++ overload debugging is enabled. */
143 show_overload_debug (struct ui_file *file, int from_tty,
144 struct cmd_list_element *c, const char *value)
146 fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
150 /* A function to show the status of strict type checking. */
153 show_strict_type_checking (struct ui_file *file, int from_tty,
154 struct cmd_list_element *c, const char *value)
156 fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
160 /* Allocate a new OBJFILE-associated type structure and fill it
161 with some defaults. Space for the type structure is allocated
162 on the objfile's objfile_obstack. */
165 alloc_type (struct objfile *objfile)
169 gdb_assert (objfile != NULL);
171 /* Alloc the structure and start off with all fields zeroed. */
172 type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
173 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
175 OBJSTAT (objfile, n_types++);
177 TYPE_OBJFILE_OWNED (type) = 1;
178 TYPE_OWNER (type).objfile = objfile;
180 /* Initialize the fields that might not be zero. */
182 TYPE_CODE (type) = TYPE_CODE_UNDEF;
183 TYPE_CHAIN (type) = type; /* Chain back to itself. */
188 /* Allocate a new GDBARCH-associated type structure and fill it
189 with some defaults. Space for the type structure is allocated
193 alloc_type_arch (struct gdbarch *gdbarch)
197 gdb_assert (gdbarch != NULL);
199 /* Alloc the structure and start off with all fields zeroed. */
201 type = XCNEW (struct type);
202 TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
204 TYPE_OBJFILE_OWNED (type) = 0;
205 TYPE_OWNER (type).gdbarch = gdbarch;
207 /* Initialize the fields that might not be zero. */
209 TYPE_CODE (type) = TYPE_CODE_UNDEF;
210 TYPE_CHAIN (type) = type; /* Chain back to itself. */
215 /* If TYPE is objfile-associated, allocate a new type structure
216 associated with the same objfile. If TYPE is gdbarch-associated,
217 allocate a new type structure associated with the same gdbarch. */
220 alloc_type_copy (const struct type *type)
222 if (TYPE_OBJFILE_OWNED (type))
223 return alloc_type (TYPE_OWNER (type).objfile);
225 return alloc_type_arch (TYPE_OWNER (type).gdbarch);
228 /* If TYPE is gdbarch-associated, return that architecture.
229 If TYPE is objfile-associated, return that objfile's architecture. */
232 get_type_arch (const struct type *type)
234 if (TYPE_OBJFILE_OWNED (type))
235 return get_objfile_arch (TYPE_OWNER (type).objfile);
237 return TYPE_OWNER (type).gdbarch;
240 /* See gdbtypes.h. */
243 get_target_type (struct type *type)
247 type = TYPE_TARGET_TYPE (type);
249 type = check_typedef (type);
255 /* See gdbtypes.h. */
258 type_length_units (struct type *type)
260 struct gdbarch *arch = get_type_arch (type);
261 int unit_size = gdbarch_addressable_memory_unit_size (arch);
263 return TYPE_LENGTH (type) / unit_size;
266 /* Alloc a new type instance structure, fill it with some defaults,
267 and point it at OLDTYPE. Allocate the new type instance from the
268 same place as OLDTYPE. */
271 alloc_type_instance (struct type *oldtype)
275 /* Allocate the structure. */
277 if (! TYPE_OBJFILE_OWNED (oldtype))
278 type = XCNEW (struct type);
280 type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
283 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
285 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
290 /* Clear all remnants of the previous type at TYPE, in preparation for
291 replacing it with something else. Preserve owner information. */
294 smash_type (struct type *type)
296 int objfile_owned = TYPE_OBJFILE_OWNED (type);
297 union type_owner owner = TYPE_OWNER (type);
299 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
301 /* Restore owner information. */
302 TYPE_OBJFILE_OWNED (type) = objfile_owned;
303 TYPE_OWNER (type) = owner;
305 /* For now, delete the rings. */
306 TYPE_CHAIN (type) = type;
308 /* For now, leave the pointer/reference types alone. */
311 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
312 to a pointer to memory where the pointer type should be stored.
313 If *TYPEPTR is zero, update it to point to the pointer type we return.
314 We allocate new memory if needed. */
317 make_pointer_type (struct type *type, struct type **typeptr)
319 struct type *ntype; /* New type */
322 ntype = TYPE_POINTER_TYPE (type);
327 return ntype; /* Don't care about alloc,
328 and have new type. */
329 else if (*typeptr == 0)
331 *typeptr = ntype; /* Tracking alloc, and have new type. */
336 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
338 ntype = alloc_type_copy (type);
342 else /* We have storage, but need to reset it. */
345 chain = TYPE_CHAIN (ntype);
347 TYPE_CHAIN (ntype) = chain;
350 TYPE_TARGET_TYPE (ntype) = type;
351 TYPE_POINTER_TYPE (type) = ntype;
353 /* FIXME! Assumes the machine has only one representation for pointers! */
356 = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
357 TYPE_CODE (ntype) = TYPE_CODE_PTR;
359 /* Mark pointers as unsigned. The target converts between pointers
360 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
361 gdbarch_address_to_pointer. */
362 TYPE_UNSIGNED (ntype) = 1;
364 /* Update the length of all the other variants of this type. */
365 chain = TYPE_CHAIN (ntype);
366 while (chain != ntype)
368 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
369 chain = TYPE_CHAIN (chain);
375 /* Given a type TYPE, return a type of pointers to that type.
376 May need to construct such a type if this is the first use. */
379 lookup_pointer_type (struct type *type)
381 return make_pointer_type (type, (struct type **) 0);
384 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
385 points to a pointer to memory where the reference type should be
386 stored. If *TYPEPTR is zero, update it to point to the reference
387 type we return. We allocate new memory if needed. */
390 make_reference_type (struct type *type, struct type **typeptr)
392 struct type *ntype; /* New type */
395 ntype = TYPE_REFERENCE_TYPE (type);
400 return ntype; /* Don't care about alloc,
401 and have new type. */
402 else if (*typeptr == 0)
404 *typeptr = ntype; /* Tracking alloc, and have new type. */
409 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
411 ntype = alloc_type_copy (type);
415 else /* We have storage, but need to reset it. */
418 chain = TYPE_CHAIN (ntype);
420 TYPE_CHAIN (ntype) = chain;
423 TYPE_TARGET_TYPE (ntype) = type;
424 TYPE_REFERENCE_TYPE (type) = ntype;
426 /* FIXME! Assume the machine has only one representation for
427 references, and that it matches the (only) representation for
430 TYPE_LENGTH (ntype) =
431 gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
432 TYPE_CODE (ntype) = TYPE_CODE_REF;
434 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
435 TYPE_REFERENCE_TYPE (type) = ntype;
437 /* Update the length of all the other variants of this type. */
438 chain = TYPE_CHAIN (ntype);
439 while (chain != ntype)
441 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
442 chain = TYPE_CHAIN (chain);
448 /* Same as above, but caller doesn't care about memory allocation
452 lookup_reference_type (struct type *type)
454 return make_reference_type (type, (struct type **) 0);
457 /* Lookup a function type that returns type TYPE. TYPEPTR, if
458 nonzero, points to a pointer to memory where the function type
459 should be stored. If *TYPEPTR is zero, update it to point to the
460 function type we return. We allocate new memory if needed. */
463 make_function_type (struct type *type, struct type **typeptr)
465 struct type *ntype; /* New type */
467 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
469 ntype = alloc_type_copy (type);
473 else /* We have storage, but need to reset it. */
479 TYPE_TARGET_TYPE (ntype) = type;
481 TYPE_LENGTH (ntype) = 1;
482 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
484 INIT_FUNC_SPECIFIC (ntype);
489 /* Given a type TYPE, return a type of functions that return that type.
490 May need to construct such a type if this is the first use. */
493 lookup_function_type (struct type *type)
495 return make_function_type (type, (struct type **) 0);
498 /* Given a type TYPE and argument types, return the appropriate
499 function type. If the final type in PARAM_TYPES is NULL, make a
503 lookup_function_type_with_arguments (struct type *type,
505 struct type **param_types)
507 struct type *fn = make_function_type (type, (struct type **) 0);
512 if (param_types[nparams - 1] == NULL)
515 TYPE_VARARGS (fn) = 1;
517 else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
521 /* Caller should have ensured this. */
522 gdb_assert (nparams == 0);
523 TYPE_PROTOTYPED (fn) = 1;
527 TYPE_NFIELDS (fn) = nparams;
528 TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
529 for (i = 0; i < nparams; ++i)
530 TYPE_FIELD_TYPE (fn, i) = param_types[i];
535 /* Identify address space identifier by name --
536 return the integer flag defined in gdbtypes.h. */
539 address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
543 /* Check for known address space delimiters. */
544 if (!strcmp (space_identifier, "code"))
545 return TYPE_INSTANCE_FLAG_CODE_SPACE;
546 else if (!strcmp (space_identifier, "data"))
547 return TYPE_INSTANCE_FLAG_DATA_SPACE;
548 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
549 && gdbarch_address_class_name_to_type_flags (gdbarch,
554 error (_("Unknown address space specifier: \"%s\""), space_identifier);
557 /* Identify address space identifier by integer flag as defined in
558 gdbtypes.h -- return the string version of the adress space name. */
561 address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
563 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
565 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
567 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
568 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
569 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
574 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
576 If STORAGE is non-NULL, create the new type instance there.
577 STORAGE must be in the same obstack as TYPE. */
580 make_qualified_type (struct type *type, int new_flags,
581 struct type *storage)
588 if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
590 ntype = TYPE_CHAIN (ntype);
592 while (ntype != type);
594 /* Create a new type instance. */
596 ntype = alloc_type_instance (type);
599 /* If STORAGE was provided, it had better be in the same objfile
600 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
601 if one objfile is freed and the other kept, we'd have
602 dangling pointers. */
603 gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
606 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
607 TYPE_CHAIN (ntype) = ntype;
610 /* Pointers or references to the original type are not relevant to
612 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
613 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
615 /* Chain the new qualified type to the old type. */
616 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
617 TYPE_CHAIN (type) = ntype;
619 /* Now set the instance flags and return the new type. */
620 TYPE_INSTANCE_FLAGS (ntype) = new_flags;
622 /* Set length of new type to that of the original type. */
623 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
628 /* Make an address-space-delimited variant of a type -- a type that
629 is identical to the one supplied except that it has an address
630 space attribute attached to it (such as "code" or "data").
632 The space attributes "code" and "data" are for Harvard
633 architectures. The address space attributes are for architectures
634 which have alternately sized pointers or pointers with alternate
638 make_type_with_address_space (struct type *type, int space_flag)
640 int new_flags = ((TYPE_INSTANCE_FLAGS (type)
641 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
642 | TYPE_INSTANCE_FLAG_DATA_SPACE
643 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
646 return make_qualified_type (type, new_flags, NULL);
649 /* Make a "c-v" variant of a type -- a type that is identical to the
650 one supplied except that it may have const or volatile attributes
651 CNST is a flag for setting the const attribute
652 VOLTL is a flag for setting the volatile attribute
653 TYPE is the base type whose variant we are creating.
655 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
656 storage to hold the new qualified type; *TYPEPTR and TYPE must be
657 in the same objfile. Otherwise, allocate fresh memory for the new
658 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
659 new type we construct. */
662 make_cv_type (int cnst, int voltl,
664 struct type **typeptr)
666 struct type *ntype; /* New type */
668 int new_flags = (TYPE_INSTANCE_FLAGS (type)
669 & ~(TYPE_INSTANCE_FLAG_CONST
670 | TYPE_INSTANCE_FLAG_VOLATILE));
673 new_flags |= TYPE_INSTANCE_FLAG_CONST;
676 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
678 if (typeptr && *typeptr != NULL)
680 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
681 a C-V variant chain that threads across objfiles: if one
682 objfile gets freed, then the other has a broken C-V chain.
684 This code used to try to copy over the main type from TYPE to
685 *TYPEPTR if they were in different objfiles, but that's
686 wrong, too: TYPE may have a field list or member function
687 lists, which refer to types of their own, etc. etc. The
688 whole shebang would need to be copied over recursively; you
689 can't have inter-objfile pointers. The only thing to do is
690 to leave stub types as stub types, and look them up afresh by
691 name each time you encounter them. */
692 gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
695 ntype = make_qualified_type (type, new_flags,
696 typeptr ? *typeptr : NULL);
704 /* Make a 'restrict'-qualified version of TYPE. */
707 make_restrict_type (struct type *type)
709 return make_qualified_type (type,
710 (TYPE_INSTANCE_FLAGS (type)
711 | TYPE_INSTANCE_FLAG_RESTRICT),
715 /* Make a type without const, volatile, or restrict. */
718 make_unqualified_type (struct type *type)
720 return make_qualified_type (type,
721 (TYPE_INSTANCE_FLAGS (type)
722 & ~(TYPE_INSTANCE_FLAG_CONST
723 | TYPE_INSTANCE_FLAG_VOLATILE
724 | TYPE_INSTANCE_FLAG_RESTRICT)),
728 /* Make a '_Atomic'-qualified version of TYPE. */
731 make_atomic_type (struct type *type)
733 return make_qualified_type (type,
734 (TYPE_INSTANCE_FLAGS (type)
735 | TYPE_INSTANCE_FLAG_ATOMIC),
739 /* Replace the contents of ntype with the type *type. This changes the
740 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
741 the changes are propogated to all types in the TYPE_CHAIN.
743 In order to build recursive types, it's inevitable that we'll need
744 to update types in place --- but this sort of indiscriminate
745 smashing is ugly, and needs to be replaced with something more
746 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
747 clear if more steps are needed. */
750 replace_type (struct type *ntype, struct type *type)
754 /* These two types had better be in the same objfile. Otherwise,
755 the assignment of one type's main type structure to the other
756 will produce a type with references to objects (names; field
757 lists; etc.) allocated on an objfile other than its own. */
758 gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
760 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
762 /* The type length is not a part of the main type. Update it for
763 each type on the variant chain. */
767 /* Assert that this element of the chain has no address-class bits
768 set in its flags. Such type variants might have type lengths
769 which are supposed to be different from the non-address-class
770 variants. This assertion shouldn't ever be triggered because
771 symbol readers which do construct address-class variants don't
772 call replace_type(). */
773 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
775 TYPE_LENGTH (chain) = TYPE_LENGTH (type);
776 chain = TYPE_CHAIN (chain);
778 while (ntype != chain);
780 /* Assert that the two types have equivalent instance qualifiers.
781 This should be true for at least all of our debug readers. */
782 gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
785 /* Implement direct support for MEMBER_TYPE in GNU C++.
786 May need to construct such a type if this is the first use.
787 The TYPE is the type of the member. The DOMAIN is the type
788 of the aggregate that the member belongs to. */
791 lookup_memberptr_type (struct type *type, struct type *domain)
795 mtype = alloc_type_copy (type);
796 smash_to_memberptr_type (mtype, domain, type);
800 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
803 lookup_methodptr_type (struct type *to_type)
807 mtype = alloc_type_copy (to_type);
808 smash_to_methodptr_type (mtype, to_type);
812 /* Allocate a stub method whose return type is TYPE. This apparently
813 happens for speed of symbol reading, since parsing out the
814 arguments to the method is cpu-intensive, the way we are doing it.
815 So, we will fill in arguments later. This always returns a fresh
819 allocate_stub_method (struct type *type)
823 mtype = alloc_type_copy (type);
824 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
825 TYPE_LENGTH (mtype) = 1;
826 TYPE_STUB (mtype) = 1;
827 TYPE_TARGET_TYPE (mtype) = type;
828 /* TYPE_SELF_TYPE (mtype) = unknown yet */
832 /* Create a range type with a dynamic range from LOW_BOUND to
833 HIGH_BOUND, inclusive. See create_range_type for further details. */
836 create_range_type (struct type *result_type, struct type *index_type,
837 const struct dynamic_prop *low_bound,
838 const struct dynamic_prop *high_bound)
840 if (result_type == NULL)
841 result_type = alloc_type_copy (index_type);
842 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
843 TYPE_TARGET_TYPE (result_type) = index_type;
844 if (TYPE_STUB (index_type))
845 TYPE_TARGET_STUB (result_type) = 1;
847 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
849 TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
850 TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
851 TYPE_RANGE_DATA (result_type)->low = *low_bound;
852 TYPE_RANGE_DATA (result_type)->high = *high_bound;
854 if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
855 TYPE_UNSIGNED (result_type) = 1;
857 /* Ada allows the declaration of range types whose upper bound is
858 less than the lower bound, so checking the lower bound is not
859 enough. Make sure we do not mark a range type whose upper bound
860 is negative as unsigned. */
861 if (high_bound->kind == PROP_CONST && high_bound->data.const_val < 0)
862 TYPE_UNSIGNED (result_type) = 0;
867 /* Create a range type using either a blank type supplied in
868 RESULT_TYPE, or creating a new type, inheriting the objfile from
871 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
872 to HIGH_BOUND, inclusive.
874 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
875 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
878 create_static_range_type (struct type *result_type, struct type *index_type,
879 LONGEST low_bound, LONGEST high_bound)
881 struct dynamic_prop low, high;
883 low.kind = PROP_CONST;
884 low.data.const_val = low_bound;
886 high.kind = PROP_CONST;
887 high.data.const_val = high_bound;
889 result_type = create_range_type (result_type, index_type, &low, &high);
894 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
895 are static, otherwise returns 0. */
898 has_static_range (const struct range_bounds *bounds)
900 return (bounds->low.kind == PROP_CONST
901 && bounds->high.kind == PROP_CONST);
905 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
906 TYPE. Return 1 if type is a range type, 0 if it is discrete (and
907 bounds will fit in LONGEST), or -1 otherwise. */
910 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
912 type = check_typedef (type);
913 switch (TYPE_CODE (type))
915 case TYPE_CODE_RANGE:
916 *lowp = TYPE_LOW_BOUND (type);
917 *highp = TYPE_HIGH_BOUND (type);
920 if (TYPE_NFIELDS (type) > 0)
922 /* The enums may not be sorted by value, so search all
926 *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
927 for (i = 0; i < TYPE_NFIELDS (type); i++)
929 if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
930 *lowp = TYPE_FIELD_ENUMVAL (type, i);
931 if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
932 *highp = TYPE_FIELD_ENUMVAL (type, i);
935 /* Set unsigned indicator if warranted. */
938 TYPE_UNSIGNED (type) = 1;
952 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
954 if (!TYPE_UNSIGNED (type))
956 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
960 /* ... fall through for unsigned ints ... */
963 /* This round-about calculation is to avoid shifting by
964 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
965 if TYPE_LENGTH (type) == sizeof (LONGEST). */
966 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
967 *highp = (*highp - 1) | *highp;
974 /* Assuming TYPE is a simple, non-empty array type, compute its upper
975 and lower bound. Save the low bound into LOW_BOUND if not NULL.
976 Save the high bound into HIGH_BOUND if not NULL.
978 Return 1 if the operation was successful. Return zero otherwise,
979 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
981 We now simply use get_discrete_bounds call to get the values
982 of the low and high bounds.
983 get_discrete_bounds can return three values:
984 1, meaning that index is a range,
985 0, meaning that index is a discrete type,
986 or -1 for failure. */
989 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
991 struct type *index = TYPE_INDEX_TYPE (type);
999 res = get_discrete_bounds (index, &low, &high);
1003 /* Check if the array bounds are undefined. */
1005 && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
1006 || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
1018 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1019 representation of a value of this type, save the corresponding
1020 position number in POS.
1022 Its differs from VAL only in the case of enumeration types. In
1023 this case, the position number of the value of the first listed
1024 enumeration literal is zero; the position number of the value of
1025 each subsequent enumeration literal is one more than that of its
1026 predecessor in the list.
1028 Return 1 if the operation was successful. Return zero otherwise,
1029 in which case the value of POS is unmodified.
1033 discrete_position (struct type *type, LONGEST val, LONGEST *pos)
1035 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
1039 for (i = 0; i < TYPE_NFIELDS (type); i += 1)
1041 if (val == TYPE_FIELD_ENUMVAL (type, i))
1047 /* Invalid enumeration value. */
1057 /* Create an array type using either a blank type supplied in
1058 RESULT_TYPE, or creating a new type, inheriting the objfile from
1061 Elements will be of type ELEMENT_TYPE, the indices will be of type
1064 If BIT_STRIDE is not zero, build a packed array type whose element
1065 size is BIT_STRIDE. Otherwise, ignore this parameter.
1067 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1068 sure it is TYPE_CODE_UNDEF before we bash it into an array
1072 create_array_type_with_stride (struct type *result_type,
1073 struct type *element_type,
1074 struct type *range_type,
1075 unsigned int bit_stride)
1077 if (result_type == NULL)
1078 result_type = alloc_type_copy (range_type);
1080 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
1081 TYPE_TARGET_TYPE (result_type) = element_type;
1082 if (has_static_range (TYPE_RANGE_DATA (range_type)))
1084 LONGEST low_bound, high_bound;
1086 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
1087 low_bound = high_bound = 0;
1088 element_type = check_typedef (element_type);
1089 /* Be careful when setting the array length. Ada arrays can be
1090 empty arrays with the high_bound being smaller than the low_bound.
1091 In such cases, the array length should be zero. */
1092 if (high_bound < low_bound)
1093 TYPE_LENGTH (result_type) = 0;
1094 else if (bit_stride > 0)
1095 TYPE_LENGTH (result_type) =
1096 (bit_stride * (high_bound - low_bound + 1) + 7) / 8;
1098 TYPE_LENGTH (result_type) =
1099 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
1103 /* This type is dynamic and its length needs to be computed
1104 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1105 undefined by setting it to zero. Although we are not expected
1106 to trust TYPE_LENGTH in this case, setting the size to zero
1107 allows us to avoid allocating objects of random sizes in case
1108 we accidently do. */
1109 TYPE_LENGTH (result_type) = 0;
1112 TYPE_NFIELDS (result_type) = 1;
1113 TYPE_FIELDS (result_type) =
1114 (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
1115 TYPE_INDEX_TYPE (result_type) = range_type;
1117 TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
1119 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays. */
1120 if (TYPE_LENGTH (result_type) == 0)
1121 TYPE_TARGET_STUB (result_type) = 1;
1126 /* Same as create_array_type_with_stride but with no bit_stride
1127 (BIT_STRIDE = 0), thus building an unpacked array. */
1130 create_array_type (struct type *result_type,
1131 struct type *element_type,
1132 struct type *range_type)
1134 return create_array_type_with_stride (result_type, element_type,
1139 lookup_array_range_type (struct type *element_type,
1140 LONGEST low_bound, LONGEST high_bound)
1142 struct gdbarch *gdbarch = get_type_arch (element_type);
1143 struct type *index_type = builtin_type (gdbarch)->builtin_int;
1144 struct type *range_type
1145 = create_static_range_type (NULL, index_type, low_bound, high_bound);
1147 return create_array_type (NULL, element_type, range_type);
1150 /* Create a string type using either a blank type supplied in
1151 RESULT_TYPE, or creating a new type. String types are similar
1152 enough to array of char types that we can use create_array_type to
1153 build the basic type and then bash it into a string type.
1155 For fixed length strings, the range type contains 0 as the lower
1156 bound and the length of the string minus one as the upper bound.
1158 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1159 sure it is TYPE_CODE_UNDEF before we bash it into a string
1163 create_string_type (struct type *result_type,
1164 struct type *string_char_type,
1165 struct type *range_type)
1167 result_type = create_array_type (result_type,
1170 TYPE_CODE (result_type) = TYPE_CODE_STRING;
1175 lookup_string_range_type (struct type *string_char_type,
1176 LONGEST low_bound, LONGEST high_bound)
1178 struct type *result_type;
1180 result_type = lookup_array_range_type (string_char_type,
1181 low_bound, high_bound);
1182 TYPE_CODE (result_type) = TYPE_CODE_STRING;
1187 create_set_type (struct type *result_type, struct type *domain_type)
1189 if (result_type == NULL)
1190 result_type = alloc_type_copy (domain_type);
1192 TYPE_CODE (result_type) = TYPE_CODE_SET;
1193 TYPE_NFIELDS (result_type) = 1;
1194 TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
1196 if (!TYPE_STUB (domain_type))
1198 LONGEST low_bound, high_bound, bit_length;
1200 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
1201 low_bound = high_bound = 0;
1202 bit_length = high_bound - low_bound + 1;
1203 TYPE_LENGTH (result_type)
1204 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1206 TYPE_UNSIGNED (result_type) = 1;
1208 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
1213 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1214 and any array types nested inside it. */
1217 make_vector_type (struct type *array_type)
1219 struct type *inner_array, *elt_type;
1222 /* Find the innermost array type, in case the array is
1223 multi-dimensional. */
1224 inner_array = array_type;
1225 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
1226 inner_array = TYPE_TARGET_TYPE (inner_array);
1228 elt_type = TYPE_TARGET_TYPE (inner_array);
1229 if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
1231 flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
1232 elt_type = make_qualified_type (elt_type, flags, NULL);
1233 TYPE_TARGET_TYPE (inner_array) = elt_type;
1236 TYPE_VECTOR (array_type) = 1;
1240 init_vector_type (struct type *elt_type, int n)
1242 struct type *array_type;
1244 array_type = lookup_array_range_type (elt_type, 0, n - 1);
1245 make_vector_type (array_type);
1249 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1250 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1251 confusing. "self" is a common enough replacement for "this".
1252 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1253 TYPE_CODE_METHOD. */
1256 internal_type_self_type (struct type *type)
1258 switch (TYPE_CODE (type))
1260 case TYPE_CODE_METHODPTR:
1261 case TYPE_CODE_MEMBERPTR:
1262 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1264 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1265 return TYPE_MAIN_TYPE (type)->type_specific.self_type;
1266 case TYPE_CODE_METHOD:
1267 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1269 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1270 return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type;
1272 gdb_assert_not_reached ("bad type");
1276 /* Set the type of the class that TYPE belongs to.
1277 In c++ this is the class of "this".
1278 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1279 TYPE_CODE_METHOD. */
1282 set_type_self_type (struct type *type, struct type *self_type)
1284 switch (TYPE_CODE (type))
1286 case TYPE_CODE_METHODPTR:
1287 case TYPE_CODE_MEMBERPTR:
1288 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1289 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE;
1290 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1291 TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type;
1293 case TYPE_CODE_METHOD:
1294 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1295 INIT_FUNC_SPECIFIC (type);
1296 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1297 TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type;
1300 gdb_assert_not_reached ("bad type");
1304 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1305 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1306 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1307 TYPE doesn't include the offset (that's the value of the MEMBER
1308 itself), but does include the structure type into which it points
1311 When "smashing" the type, we preserve the objfile that the old type
1312 pointed to, since we aren't changing where the type is actually
1316 smash_to_memberptr_type (struct type *type, struct type *self_type,
1317 struct type *to_type)
1320 TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
1321 TYPE_TARGET_TYPE (type) = to_type;
1322 set_type_self_type (type, self_type);
1323 /* Assume that a data member pointer is the same size as a normal
1326 = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
1329 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1331 When "smashing" the type, we preserve the objfile that the old type
1332 pointed to, since we aren't changing where the type is actually
1336 smash_to_methodptr_type (struct type *type, struct type *to_type)
1339 TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1340 TYPE_TARGET_TYPE (type) = to_type;
1341 set_type_self_type (type, TYPE_SELF_TYPE (to_type));
1342 TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1345 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1346 METHOD just means `function that gets an extra "this" argument'.
1348 When "smashing" the type, we preserve the objfile that the old type
1349 pointed to, since we aren't changing where the type is actually
1353 smash_to_method_type (struct type *type, struct type *self_type,
1354 struct type *to_type, struct field *args,
1355 int nargs, int varargs)
1358 TYPE_CODE (type) = TYPE_CODE_METHOD;
1359 TYPE_TARGET_TYPE (type) = to_type;
1360 set_type_self_type (type, self_type);
1361 TYPE_FIELDS (type) = args;
1362 TYPE_NFIELDS (type) = nargs;
1364 TYPE_VARARGS (type) = 1;
1365 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
1368 /* Return a typename for a struct/union/enum type without "struct ",
1369 "union ", or "enum ". If the type has a NULL name, return NULL. */
1372 type_name_no_tag (const struct type *type)
1374 if (TYPE_TAG_NAME (type) != NULL)
1375 return TYPE_TAG_NAME (type);
1377 /* Is there code which expects this to return the name if there is
1378 no tag name? My guess is that this is mainly used for C++ in
1379 cases where the two will always be the same. */
1380 return TYPE_NAME (type);
1383 /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1384 Since GCC PR debug/47510 DWARF provides associated information to detect the
1385 anonymous class linkage name from its typedef.
1387 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1391 type_name_no_tag_or_error (struct type *type)
1393 struct type *saved_type = type;
1395 struct objfile *objfile;
1397 type = check_typedef (type);
1399 name = type_name_no_tag (type);
1403 name = type_name_no_tag (saved_type);
1404 objfile = TYPE_OBJFILE (saved_type);
1405 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1406 name ? name : "<anonymous>",
1407 objfile ? objfile_name (objfile) : "<arch>");
1410 /* Lookup a typedef or primitive type named NAME, visible in lexical
1411 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1412 suitably defined. */
1415 lookup_typename (const struct language_defn *language,
1416 struct gdbarch *gdbarch, const char *name,
1417 const struct block *block, int noerr)
1422 sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
1423 language->la_language, NULL).symbol;
1424 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1425 return SYMBOL_TYPE (sym);
1429 error (_("No type named %s."), name);
1433 lookup_unsigned_typename (const struct language_defn *language,
1434 struct gdbarch *gdbarch, const char *name)
1436 char *uns = alloca (strlen (name) + 10);
1438 strcpy (uns, "unsigned ");
1439 strcpy (uns + 9, name);
1440 return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
1444 lookup_signed_typename (const struct language_defn *language,
1445 struct gdbarch *gdbarch, const char *name)
1448 char *uns = alloca (strlen (name) + 8);
1450 strcpy (uns, "signed ");
1451 strcpy (uns + 7, name);
1452 t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
1453 /* If we don't find "signed FOO" just try again with plain "FOO". */
1456 return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
1459 /* Lookup a structure type named "struct NAME",
1460 visible in lexical block BLOCK. */
1463 lookup_struct (const char *name, const struct block *block)
1467 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1471 error (_("No struct type named %s."), name);
1473 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1475 error (_("This context has class, union or enum %s, not a struct."),
1478 return (SYMBOL_TYPE (sym));
1481 /* Lookup a union type named "union NAME",
1482 visible in lexical block BLOCK. */
1485 lookup_union (const char *name, const struct block *block)
1490 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1493 error (_("No union type named %s."), name);
1495 t = SYMBOL_TYPE (sym);
1497 if (TYPE_CODE (t) == TYPE_CODE_UNION)
1500 /* If we get here, it's not a union. */
1501 error (_("This context has class, struct or enum %s, not a union."),
1505 /* Lookup an enum type named "enum NAME",
1506 visible in lexical block BLOCK. */
1509 lookup_enum (const char *name, const struct block *block)
1513 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1516 error (_("No enum type named %s."), name);
1518 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1520 error (_("This context has class, struct or union %s, not an enum."),
1523 return (SYMBOL_TYPE (sym));
1526 /* Lookup a template type named "template NAME<TYPE>",
1527 visible in lexical block BLOCK. */
1530 lookup_template_type (char *name, struct type *type,
1531 const struct block *block)
1534 char *nam = (char *)
1535 alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1539 strcat (nam, TYPE_NAME (type));
1540 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
1542 sym = lookup_symbol (nam, block, VAR_DOMAIN, 0).symbol;
1546 error (_("No template type named %s."), name);
1548 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1550 error (_("This context has class, union or enum %s, not a struct."),
1553 return (SYMBOL_TYPE (sym));
1556 /* Given a type TYPE, lookup the type of the component of type named
1559 TYPE can be either a struct or union, or a pointer or reference to
1560 a struct or union. If it is a pointer or reference, its target
1561 type is automatically used. Thus '.' and '->' are interchangable,
1562 as specified for the definitions of the expression element types
1563 STRUCTOP_STRUCT and STRUCTOP_PTR.
1565 If NOERR is nonzero, return zero if NAME is not suitably defined.
1566 If NAME is the name of a baseclass type, return that type. */
1569 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1576 type = check_typedef (type);
1577 if (TYPE_CODE (type) != TYPE_CODE_PTR
1578 && TYPE_CODE (type) != TYPE_CODE_REF)
1580 type = TYPE_TARGET_TYPE (type);
1583 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1584 && TYPE_CODE (type) != TYPE_CODE_UNION)
1586 type_name = type_to_string (type);
1587 make_cleanup (xfree, type_name);
1588 error (_("Type %s is not a structure or union type."), type_name);
1592 /* FIXME: This change put in by Michael seems incorrect for the case
1593 where the structure tag name is the same as the member name.
1594 I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
1595 foo; } bell;" Disabled by fnf. */
1599 type_name = type_name_no_tag (type);
1600 if (type_name != NULL && strcmp (type_name, name) == 0)
1605 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1607 const char *t_field_name = TYPE_FIELD_NAME (type, i);
1609 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1611 return TYPE_FIELD_TYPE (type, i);
1613 else if (!t_field_name || *t_field_name == '\0')
1615 struct type *subtype
1616 = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1618 if (subtype != NULL)
1623 /* OK, it's not in this class. Recursively check the baseclasses. */
1624 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1628 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
1640 type_name = type_to_string (type);
1641 make_cleanup (xfree, type_name);
1642 error (_("Type %s has no component named %s."), type_name, name);
1645 /* Store in *MAX the largest number representable by unsigned integer type
1649 get_unsigned_type_max (struct type *type, ULONGEST *max)
1653 type = check_typedef (type);
1654 gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
1655 gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
1657 /* Written this way to avoid overflow. */
1658 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1659 *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1662 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1663 signed integer type TYPE. */
1666 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1670 type = check_typedef (type);
1671 gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
1672 gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
1674 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1675 *min = -((ULONGEST) 1 << (n - 1));
1676 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1679 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1680 cplus_stuff.vptr_fieldno.
1682 cplus_stuff is initialized to cplus_struct_default which does not
1683 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1684 designated initializers). We cope with that here. */
1687 internal_type_vptr_fieldno (struct type *type)
1689 type = check_typedef (type);
1690 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1691 || TYPE_CODE (type) == TYPE_CODE_UNION);
1692 if (!HAVE_CPLUS_STRUCT (type))
1694 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1697 /* Set the value of cplus_stuff.vptr_fieldno. */
1700 set_type_vptr_fieldno (struct type *type, int fieldno)
1702 type = check_typedef (type);
1703 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1704 || TYPE_CODE (type) == TYPE_CODE_UNION);
1705 if (!HAVE_CPLUS_STRUCT (type))
1706 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1707 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1710 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1711 cplus_stuff.vptr_basetype. */
1714 internal_type_vptr_basetype (struct type *type)
1716 type = check_typedef (type);
1717 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1718 || TYPE_CODE (type) == TYPE_CODE_UNION);
1719 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1720 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1723 /* Set the value of cplus_stuff.vptr_basetype. */
1726 set_type_vptr_basetype (struct type *type, struct type *basetype)
1728 type = check_typedef (type);
1729 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1730 || TYPE_CODE (type) == TYPE_CODE_UNION);
1731 if (!HAVE_CPLUS_STRUCT (type))
1732 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1733 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1736 /* Lookup the vptr basetype/fieldno values for TYPE.
1737 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1738 vptr_fieldno. Also, if found and basetype is from the same objfile,
1740 If not found, return -1 and ignore BASETYPEP.
1741 Callers should be aware that in some cases (for example,
1742 the type or one of its baseclasses is a stub type and we are
1743 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1744 this function will not be able to find the
1745 virtual function table pointer, and vptr_fieldno will remain -1 and
1746 vptr_basetype will remain NULL or incomplete. */
1749 get_vptr_fieldno (struct type *type, struct type **basetypep)
1751 type = check_typedef (type);
1753 if (TYPE_VPTR_FIELDNO (type) < 0)
1757 /* We must start at zero in case the first (and only) baseclass
1758 is virtual (and hence we cannot share the table pointer). */
1759 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1761 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1763 struct type *basetype;
1765 fieldno = get_vptr_fieldno (baseclass, &basetype);
1768 /* If the type comes from a different objfile we can't cache
1769 it, it may have a different lifetime. PR 2384 */
1770 if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
1772 set_type_vptr_fieldno (type, fieldno);
1773 set_type_vptr_basetype (type, basetype);
1776 *basetypep = basetype;
1787 *basetypep = TYPE_VPTR_BASETYPE (type);
1788 return TYPE_VPTR_FIELDNO (type);
1793 stub_noname_complaint (void)
1795 complaint (&symfile_complaints, _("stub type has NULL name"));
1798 /* Worker for is_dynamic_type. */
1801 is_dynamic_type_internal (struct type *type, int top_level)
1803 type = check_typedef (type);
1805 /* We only want to recognize references at the outermost level. */
1806 if (top_level && TYPE_CODE (type) == TYPE_CODE_REF)
1807 type = check_typedef (TYPE_TARGET_TYPE (type));
1809 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
1810 dynamic, even if the type itself is statically defined.
1811 From a user's point of view, this may appear counter-intuitive;
1812 but it makes sense in this context, because the point is to determine
1813 whether any part of the type needs to be resolved before it can
1815 if (TYPE_DATA_LOCATION (type) != NULL
1816 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
1817 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
1820 switch (TYPE_CODE (type))
1822 case TYPE_CODE_RANGE:
1824 /* A range type is obviously dynamic if it has at least one
1825 dynamic bound. But also consider the range type to be
1826 dynamic when its subtype is dynamic, even if the bounds
1827 of the range type are static. It allows us to assume that
1828 the subtype of a static range type is also static. */
1829 return (!has_static_range (TYPE_RANGE_DATA (type))
1830 || is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0));
1833 case TYPE_CODE_ARRAY:
1835 gdb_assert (TYPE_NFIELDS (type) == 1);
1837 /* The array is dynamic if either the bounds are dynamic,
1838 or the elements it contains have a dynamic contents. */
1839 if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
1841 return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
1844 case TYPE_CODE_STRUCT:
1845 case TYPE_CODE_UNION:
1849 for (i = 0; i < TYPE_NFIELDS (type); ++i)
1850 if (!field_is_static (&TYPE_FIELD (type, i))
1851 && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
1860 /* See gdbtypes.h. */
1863 is_dynamic_type (struct type *type)
1865 return is_dynamic_type_internal (type, 1);
1868 static struct type *resolve_dynamic_type_internal
1869 (struct type *type, struct property_addr_info *addr_stack, int top_level);
1871 /* Given a dynamic range type (dyn_range_type) and a stack of
1872 struct property_addr_info elements, return a static version
1875 static struct type *
1876 resolve_dynamic_range (struct type *dyn_range_type,
1877 struct property_addr_info *addr_stack)
1880 struct type *static_range_type, *static_target_type;
1881 const struct dynamic_prop *prop;
1882 const struct dwarf2_locexpr_baton *baton;
1883 struct dynamic_prop low_bound, high_bound;
1885 gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);
1887 prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
1888 if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
1890 low_bound.kind = PROP_CONST;
1891 low_bound.data.const_val = value;
1895 low_bound.kind = PROP_UNDEFINED;
1896 low_bound.data.const_val = 0;
1899 prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
1900 if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
1902 high_bound.kind = PROP_CONST;
1903 high_bound.data.const_val = value;
1905 if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count)
1906 high_bound.data.const_val
1907 = low_bound.data.const_val + high_bound.data.const_val - 1;
1911 high_bound.kind = PROP_UNDEFINED;
1912 high_bound.data.const_val = 0;
1916 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type),
1918 static_range_type = create_range_type (copy_type (dyn_range_type),
1920 &low_bound, &high_bound);
1921 TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1;
1922 return static_range_type;
1925 /* Resolves dynamic bound values of an array type TYPE to static ones.
1926 ADDR_STACK is a stack of struct property_addr_info to be used
1927 if needed during the dynamic resolution. */
1929 static struct type *
1930 resolve_dynamic_array (struct type *type,
1931 struct property_addr_info *addr_stack)
1934 struct type *elt_type;
1935 struct type *range_type;
1936 struct type *ary_dim;
1938 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
1941 range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
1942 range_type = resolve_dynamic_range (range_type, addr_stack);
1944 ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));
1946 if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
1947 elt_type = resolve_dynamic_array (ary_dim, addr_stack);
1949 elt_type = TYPE_TARGET_TYPE (type);
1951 return create_array_type_with_stride (copy_type (type),
1952 elt_type, range_type,
1953 TYPE_FIELD_BITSIZE (type, 0));
1956 /* Resolve dynamic bounds of members of the union TYPE to static
1957 bounds. ADDR_STACK is a stack of struct property_addr_info
1958 to be used if needed during the dynamic resolution. */
1960 static struct type *
1961 resolve_dynamic_union (struct type *type,
1962 struct property_addr_info *addr_stack)
1964 struct type *resolved_type;
1966 unsigned int max_len = 0;
1968 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
1970 resolved_type = copy_type (type);
1971 TYPE_FIELDS (resolved_type)
1972 = TYPE_ALLOC (resolved_type,
1973 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1974 memcpy (TYPE_FIELDS (resolved_type),
1976 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1977 for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
1981 if (field_is_static (&TYPE_FIELD (type, i)))
1984 t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
1986 TYPE_FIELD_TYPE (resolved_type, i) = t;
1987 if (TYPE_LENGTH (t) > max_len)
1988 max_len = TYPE_LENGTH (t);
1991 TYPE_LENGTH (resolved_type) = max_len;
1992 return resolved_type;
1995 /* Resolve dynamic bounds of members of the struct TYPE to static
1996 bounds. ADDR_STACK is a stack of struct property_addr_info to
1997 be used if needed during the dynamic resolution. */
1999 static struct type *
2000 resolve_dynamic_struct (struct type *type,
2001 struct property_addr_info *addr_stack)
2003 struct type *resolved_type;
2005 unsigned resolved_type_bit_length = 0;
2007 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
2008 gdb_assert (TYPE_NFIELDS (type) > 0);
2010 resolved_type = copy_type (type);
2011 TYPE_FIELDS (resolved_type)
2012 = TYPE_ALLOC (resolved_type,
2013 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
2014 memcpy (TYPE_FIELDS (resolved_type),
2016 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
2017 for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
2019 unsigned new_bit_length;
2020 struct property_addr_info pinfo;
2022 if (field_is_static (&TYPE_FIELD (type, i)))
2025 /* As we know this field is not a static field, the field's
2026 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2027 this is the case, but only trigger a simple error rather
2028 than an internal error if that fails. While failing
2029 that verification indicates a bug in our code, the error
2030 is not severe enough to suggest to the user he stops
2031 his debugging session because of it. */
2032 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_BITPOS)
2033 error (_("Cannot determine struct field location"
2034 " (invalid location kind)"));
2036 pinfo.type = check_typedef (TYPE_FIELD_TYPE (type, i));
2037 pinfo.valaddr = addr_stack->valaddr;
2038 pinfo.addr = addr_stack->addr;
2039 pinfo.next = addr_stack;
2041 TYPE_FIELD_TYPE (resolved_type, i)
2042 = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
2044 gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
2045 == FIELD_LOC_KIND_BITPOS);
2047 new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
2048 if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
2049 new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
2051 new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
2054 /* Normally, we would use the position and size of the last field
2055 to determine the size of the enclosing structure. But GCC seems
2056 to be encoding the position of some fields incorrectly when
2057 the struct contains a dynamic field that is not placed last.
2058 So we compute the struct size based on the field that has
2059 the highest position + size - probably the best we can do. */
2060 if (new_bit_length > resolved_type_bit_length)
2061 resolved_type_bit_length = new_bit_length;
2064 TYPE_LENGTH (resolved_type)
2065 = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
2067 /* The Ada language uses this field as a cache for static fixed types: reset
2068 it as RESOLVED_TYPE must have its own static fixed type. */
2069 TYPE_TARGET_TYPE (resolved_type) = NULL;
2071 return resolved_type;
2074 /* Worker for resolved_dynamic_type. */
2076 static struct type *
2077 resolve_dynamic_type_internal (struct type *type,
2078 struct property_addr_info *addr_stack,
2081 struct type *real_type = check_typedef (type);
2082 struct type *resolved_type = type;
2083 struct dynamic_prop *prop;
2086 if (!is_dynamic_type_internal (real_type, top_level))
2089 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2091 resolved_type = copy_type (type);
2092 TYPE_TARGET_TYPE (resolved_type)
2093 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack,
2098 /* Before trying to resolve TYPE, make sure it is not a stub. */
2101 switch (TYPE_CODE (type))
2105 struct property_addr_info pinfo;
2107 pinfo.type = check_typedef (TYPE_TARGET_TYPE (type));
2108 pinfo.valaddr = NULL;
2109 if (addr_stack->valaddr != NULL)
2110 pinfo.addr = extract_typed_address (addr_stack->valaddr, type);
2112 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2113 pinfo.next = addr_stack;
2115 resolved_type = copy_type (type);
2116 TYPE_TARGET_TYPE (resolved_type)
2117 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
2122 case TYPE_CODE_ARRAY:
2123 resolved_type = resolve_dynamic_array (type, addr_stack);
2126 case TYPE_CODE_RANGE:
2127 resolved_type = resolve_dynamic_range (type, addr_stack);
2130 case TYPE_CODE_UNION:
2131 resolved_type = resolve_dynamic_union (type, addr_stack);
2134 case TYPE_CODE_STRUCT:
2135 resolved_type = resolve_dynamic_struct (type, addr_stack);
2140 /* Resolve data_location attribute. */
2141 prop = TYPE_DATA_LOCATION (resolved_type);
2143 && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2145 TYPE_DYN_PROP_ADDR (prop) = value;
2146 TYPE_DYN_PROP_KIND (prop) = PROP_CONST;
2149 return resolved_type;
2152 /* See gdbtypes.h */
2155 resolve_dynamic_type (struct type *type, const gdb_byte *valaddr,
2158 struct property_addr_info pinfo
2159 = {check_typedef (type), valaddr, addr, NULL};
2161 return resolve_dynamic_type_internal (type, &pinfo, 1);
2164 /* See gdbtypes.h */
2166 struct dynamic_prop *
2167 get_dyn_prop (enum dynamic_prop_node_kind prop_kind, const struct type *type)
2169 struct dynamic_prop_list *node = TYPE_DYN_PROP_LIST (type);
2171 while (node != NULL)
2173 if (node->prop_kind == prop_kind)
2180 /* See gdbtypes.h */
2183 add_dyn_prop (enum dynamic_prop_node_kind prop_kind, struct dynamic_prop prop,
2184 struct type *type, struct objfile *objfile)
2186 struct dynamic_prop_list *temp;
2188 gdb_assert (TYPE_OBJFILE_OWNED (type));
2190 temp = obstack_alloc (&objfile->objfile_obstack,
2191 sizeof (struct dynamic_prop_list));
2192 temp->prop_kind = prop_kind;
2194 temp->next = TYPE_DYN_PROP_LIST (type);
2196 TYPE_DYN_PROP_LIST (type) = temp;
2200 /* Find the real type of TYPE. This function returns the real type,
2201 after removing all layers of typedefs, and completing opaque or stub
2202 types. Completion changes the TYPE argument, but stripping of
2205 Instance flags (e.g. const/volatile) are preserved as typedefs are
2206 stripped. If necessary a new qualified form of the underlying type
2209 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
2210 not been computed and we're either in the middle of reading symbols, or
2211 there was no name for the typedef in the debug info.
2213 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2214 QUITs in the symbol reading code can also throw.
2215 Thus this function can throw an exception.
2217 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2220 If this is a stubbed struct (i.e. declared as struct foo *), see if
2221 we can find a full definition in some other file. If so, copy this
2222 definition, so we can use it in future. There used to be a comment
2223 (but not any code) that if we don't find a full definition, we'd
2224 set a flag so we don't spend time in the future checking the same
2225 type. That would be a mistake, though--we might load in more
2226 symbols which contain a full definition for the type. */
2229 check_typedef (struct type *type)
2231 struct type *orig_type = type;
2232 /* While we're removing typedefs, we don't want to lose qualifiers.
2233 E.g., const/volatile. */
2234 int instance_flags = TYPE_INSTANCE_FLAGS (type);
2238 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2240 if (!TYPE_TARGET_TYPE (type))
2245 /* It is dangerous to call lookup_symbol if we are currently
2246 reading a symtab. Infinite recursion is one danger. */
2247 if (currently_reading_symtab)
2248 return make_qualified_type (type, instance_flags, NULL);
2250 name = type_name_no_tag (type);
2251 /* FIXME: shouldn't we separately check the TYPE_NAME and
2252 the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
2253 VAR_DOMAIN as appropriate? (this code was written before
2254 TYPE_NAME and TYPE_TAG_NAME were separate). */
2257 stub_noname_complaint ();
2258 return make_qualified_type (type, instance_flags, NULL);
2260 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
2262 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
2263 else /* TYPE_CODE_UNDEF */
2264 TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
2266 type = TYPE_TARGET_TYPE (type);
2268 /* Preserve the instance flags as we traverse down the typedef chain.
2270 Handling address spaces/classes is nasty, what do we do if there's a
2272 E.g., what if an outer typedef marks the type as class_1 and an inner
2273 typedef marks the type as class_2?
2274 This is the wrong place to do such error checking. We leave it to
2275 the code that created the typedef in the first place to flag the
2276 error. We just pick the outer address space (akin to letting the
2277 outer cast in a chain of casting win), instead of assuming
2278 "it can't happen". */
2280 const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
2281 | TYPE_INSTANCE_FLAG_DATA_SPACE);
2282 const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
2283 int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
2285 /* Treat code vs data spaces and address classes separately. */
2286 if ((instance_flags & ALL_SPACES) != 0)
2287 new_instance_flags &= ~ALL_SPACES;
2288 if ((instance_flags & ALL_CLASSES) != 0)
2289 new_instance_flags &= ~ALL_CLASSES;
2291 instance_flags |= new_instance_flags;
2295 /* If this is a struct/class/union with no fields, then check
2296 whether a full definition exists somewhere else. This is for
2297 systems where a type definition with no fields is issued for such
2298 types, instead of identifying them as stub types in the first
2301 if (TYPE_IS_OPAQUE (type)
2302 && opaque_type_resolution
2303 && !currently_reading_symtab)
2305 const char *name = type_name_no_tag (type);
2306 struct type *newtype;
2310 stub_noname_complaint ();
2311 return make_qualified_type (type, instance_flags, NULL);
2313 newtype = lookup_transparent_type (name);
2317 /* If the resolved type and the stub are in the same
2318 objfile, then replace the stub type with the real deal.
2319 But if they're in separate objfiles, leave the stub
2320 alone; we'll just look up the transparent type every time
2321 we call check_typedef. We can't create pointers between
2322 types allocated to different objfiles, since they may
2323 have different lifetimes. Trying to copy NEWTYPE over to
2324 TYPE's objfile is pointless, too, since you'll have to
2325 move over any other types NEWTYPE refers to, which could
2326 be an unbounded amount of stuff. */
2327 if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
2328 type = make_qualified_type (newtype,
2329 TYPE_INSTANCE_FLAGS (type),
2335 /* Otherwise, rely on the stub flag being set for opaque/stubbed
2337 else if (TYPE_STUB (type) && !currently_reading_symtab)
2339 const char *name = type_name_no_tag (type);
2340 /* FIXME: shouldn't we separately check the TYPE_NAME and the
2341 TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
2342 as appropriate? (this code was written before TYPE_NAME and
2343 TYPE_TAG_NAME were separate). */
2348 stub_noname_complaint ();
2349 return make_qualified_type (type, instance_flags, NULL);
2351 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
2354 /* Same as above for opaque types, we can replace the stub
2355 with the complete type only if they are in the same
2357 if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
2358 type = make_qualified_type (SYMBOL_TYPE (sym),
2359 TYPE_INSTANCE_FLAGS (type),
2362 type = SYMBOL_TYPE (sym);
2366 if (TYPE_TARGET_STUB (type))
2368 struct type *range_type;
2369 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
2371 if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
2373 /* Nothing we can do. */
2375 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
2377 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
2378 TYPE_TARGET_STUB (type) = 0;
2382 type = make_qualified_type (type, instance_flags, NULL);
2384 /* Cache TYPE_LENGTH for future use. */
2385 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
2390 /* Parse a type expression in the string [P..P+LENGTH). If an error
2391 occurs, silently return a void type. */
2393 static struct type *
2394 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
2396 struct ui_file *saved_gdb_stderr;
2397 struct type *type = NULL; /* Initialize to keep gcc happy. */
2399 /* Suppress error messages. */
2400 saved_gdb_stderr = gdb_stderr;
2401 gdb_stderr = ui_file_new ();
2403 /* Call parse_and_eval_type() without fear of longjmp()s. */
2406 type = parse_and_eval_type (p, length);
2408 CATCH (except, RETURN_MASK_ERROR)
2410 type = builtin_type (gdbarch)->builtin_void;
2414 /* Stop suppressing error messages. */
2415 ui_file_delete (gdb_stderr);
2416 gdb_stderr = saved_gdb_stderr;
2421 /* Ugly hack to convert method stubs into method types.
2423 He ain't kiddin'. This demangles the name of the method into a
2424 string including argument types, parses out each argument type,
2425 generates a string casting a zero to that type, evaluates the
2426 string, and stuffs the resulting type into an argtype vector!!!
2427 Then it knows the type of the whole function (including argument
2428 types for overloading), which info used to be in the stab's but was
2429 removed to hack back the space required for them. */
2432 check_stub_method (struct type *type, int method_id, int signature_id)
2434 struct gdbarch *gdbarch = get_type_arch (type);
2436 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
2437 char *demangled_name = gdb_demangle (mangled_name,
2438 DMGL_PARAMS | DMGL_ANSI);
2439 char *argtypetext, *p;
2440 int depth = 0, argcount = 1;
2441 struct field *argtypes;
2444 /* Make sure we got back a function string that we can use. */
2446 p = strchr (demangled_name, '(');
2450 if (demangled_name == NULL || p == NULL)
2451 error (_("Internal: Cannot demangle mangled name `%s'."),
2454 /* Now, read in the parameters that define this type. */
2459 if (*p == '(' || *p == '<')
2463 else if (*p == ')' || *p == '>')
2467 else if (*p == ',' && depth == 0)
2475 /* If we read one argument and it was ``void'', don't count it. */
2476 if (startswith (argtypetext, "(void)"))
2479 /* We need one extra slot, for the THIS pointer. */
2481 argtypes = (struct field *)
2482 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
2485 /* Add THIS pointer for non-static methods. */
2486 f = TYPE_FN_FIELDLIST1 (type, method_id);
2487 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
2491 argtypes[0].type = lookup_pointer_type (type);
2495 if (*p != ')') /* () means no args, skip while. */
2500 if (depth <= 0 && (*p == ',' || *p == ')'))
2502 /* Avoid parsing of ellipsis, they will be handled below.
2503 Also avoid ``void'' as above. */
2504 if (strncmp (argtypetext, "...", p - argtypetext) != 0
2505 && strncmp (argtypetext, "void", p - argtypetext) != 0)
2507 argtypes[argcount].type =
2508 safe_parse_type (gdbarch, argtypetext, p - argtypetext);
2511 argtypetext = p + 1;
2514 if (*p == '(' || *p == '<')
2518 else if (*p == ')' || *p == '>')
2527 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
2529 /* Now update the old "stub" type into a real type. */
2530 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
2531 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
2532 We want a method (TYPE_CODE_METHOD). */
2533 smash_to_method_type (mtype, type, TYPE_TARGET_TYPE (mtype),
2534 argtypes, argcount, p[-2] == '.');
2535 TYPE_STUB (mtype) = 0;
2536 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
2538 xfree (demangled_name);
2541 /* This is the external interface to check_stub_method, above. This
2542 function unstubs all of the signatures for TYPE's METHOD_ID method
2543 name. After calling this function TYPE_FN_FIELD_STUB will be
2544 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
2547 This function unfortunately can not die until stabs do. */
2550 check_stub_method_group (struct type *type, int method_id)
2552 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
2553 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
2554 int j, found_stub = 0;
2556 for (j = 0; j < len; j++)
2557 if (TYPE_FN_FIELD_STUB (f, j))
2560 check_stub_method (type, method_id, j);
2563 /* GNU v3 methods with incorrect names were corrected when we read
2564 in type information, because it was cheaper to do it then. The
2565 only GNU v2 methods with incorrect method names are operators and
2566 destructors; destructors were also corrected when we read in type
2569 Therefore the only thing we need to handle here are v2 operator
2571 if (found_stub && !startswith (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z"))
2574 char dem_opname[256];
2576 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
2578 dem_opname, DMGL_ANSI);
2580 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
2584 TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
2588 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690. */
2589 const struct cplus_struct_type cplus_struct_default = { };
2592 allocate_cplus_struct_type (struct type *type)
2594 if (HAVE_CPLUS_STRUCT (type))
2595 /* Structure was already allocated. Nothing more to do. */
2598 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
2599 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2600 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
2601 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
2602 set_type_vptr_fieldno (type, -1);
2605 const struct gnat_aux_type gnat_aux_default =
2608 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
2609 and allocate the associated gnat-specific data. The gnat-specific
2610 data is also initialized to gnat_aux_default. */
2613 allocate_gnat_aux_type (struct type *type)
2615 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
2616 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
2617 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
2618 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
2621 /* Helper function to initialize the standard scalar types.
2623 If NAME is non-NULL, then it is used to initialize the type name.
2624 Note that NAME is not copied; it is required to have a lifetime at
2625 least as long as OBJFILE. */
2628 init_type (enum type_code code, int length, int flags,
2629 const char *name, struct objfile *objfile)
2633 type = alloc_type (objfile);
2634 TYPE_CODE (type) = code;
2635 TYPE_LENGTH (type) = length;
2637 gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
2638 if (flags & TYPE_FLAG_UNSIGNED)
2639 TYPE_UNSIGNED (type) = 1;
2640 if (flags & TYPE_FLAG_NOSIGN)
2641 TYPE_NOSIGN (type) = 1;
2642 if (flags & TYPE_FLAG_STUB)
2643 TYPE_STUB (type) = 1;
2644 if (flags & TYPE_FLAG_TARGET_STUB)
2645 TYPE_TARGET_STUB (type) = 1;
2646 if (flags & TYPE_FLAG_STATIC)
2647 TYPE_STATIC (type) = 1;
2648 if (flags & TYPE_FLAG_PROTOTYPED)
2649 TYPE_PROTOTYPED (type) = 1;
2650 if (flags & TYPE_FLAG_INCOMPLETE)
2651 TYPE_INCOMPLETE (type) = 1;
2652 if (flags & TYPE_FLAG_VARARGS)
2653 TYPE_VARARGS (type) = 1;
2654 if (flags & TYPE_FLAG_VECTOR)
2655 TYPE_VECTOR (type) = 1;
2656 if (flags & TYPE_FLAG_STUB_SUPPORTED)
2657 TYPE_STUB_SUPPORTED (type) = 1;
2658 if (flags & TYPE_FLAG_FIXED_INSTANCE)
2659 TYPE_FIXED_INSTANCE (type) = 1;
2660 if (flags & TYPE_FLAG_GNU_IFUNC)
2661 TYPE_GNU_IFUNC (type) = 1;
2663 TYPE_NAME (type) = name;
2667 if (name && strcmp (name, "char") == 0)
2668 TYPE_NOSIGN (type) = 1;
2672 case TYPE_CODE_STRUCT:
2673 case TYPE_CODE_UNION:
2674 case TYPE_CODE_NAMESPACE:
2675 INIT_CPLUS_SPECIFIC (type);
2678 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2680 case TYPE_CODE_FUNC:
2681 INIT_FUNC_SPECIFIC (type);
2687 /* Queries on types. */
2690 can_dereference (struct type *t)
2692 /* FIXME: Should we return true for references as well as
2694 t = check_typedef (t);
2697 && TYPE_CODE (t) == TYPE_CODE_PTR
2698 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2702 is_integral_type (struct type *t)
2704 t = check_typedef (t);
2707 && ((TYPE_CODE (t) == TYPE_CODE_INT)
2708 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
2709 || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
2710 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2711 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2712 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
2715 /* Return true if TYPE is scalar. */
2718 is_scalar_type (struct type *type)
2720 type = check_typedef (type);
2722 switch (TYPE_CODE (type))
2724 case TYPE_CODE_ARRAY:
2725 case TYPE_CODE_STRUCT:
2726 case TYPE_CODE_UNION:
2728 case TYPE_CODE_STRING:
2735 /* Return true if T is scalar, or a composite type which in practice has
2736 the memory layout of a scalar type. E.g., an array or struct with only
2737 one scalar element inside it, or a union with only scalar elements. */
2740 is_scalar_type_recursive (struct type *t)
2742 t = check_typedef (t);
2744 if (is_scalar_type (t))
2746 /* Are we dealing with an array or string of known dimensions? */
2747 else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2748 || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2749 && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2751 LONGEST low_bound, high_bound;
2752 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2754 get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2756 return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2758 /* Are we dealing with a struct with one element? */
2759 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2760 return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2761 else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2763 int i, n = TYPE_NFIELDS (t);
2765 /* If all elements of the union are scalar, then the union is scalar. */
2766 for (i = 0; i < n; i++)
2767 if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2776 /* Return true is T is a class or a union. False otherwise. */
2779 class_or_union_p (const struct type *t)
2781 return (TYPE_CODE (t) == TYPE_CODE_STRUCT
2782 || TYPE_CODE (t) == TYPE_CODE_UNION);
2785 /* A helper function which returns true if types A and B represent the
2786 "same" class type. This is true if the types have the same main
2787 type, or the same name. */
2790 class_types_same_p (const struct type *a, const struct type *b)
2792 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2793 || (TYPE_NAME (a) && TYPE_NAME (b)
2794 && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2797 /* If BASE is an ancestor of DCLASS return the distance between them.
2798 otherwise return -1;
2802 class B: public A {};
2803 class C: public B {};
2806 distance_to_ancestor (A, A, 0) = 0
2807 distance_to_ancestor (A, B, 0) = 1
2808 distance_to_ancestor (A, C, 0) = 2
2809 distance_to_ancestor (A, D, 0) = 3
2811 If PUBLIC is 1 then only public ancestors are considered,
2812 and the function returns the distance only if BASE is a public ancestor
2816 distance_to_ancestor (A, D, 1) = -1. */
2819 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
2824 base = check_typedef (base);
2825 dclass = check_typedef (dclass);
2827 if (class_types_same_p (base, dclass))
2830 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2832 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2835 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
2843 /* Check whether BASE is an ancestor or base class or DCLASS
2844 Return 1 if so, and 0 if not.
2845 Note: If BASE and DCLASS are of the same type, this function
2846 will return 1. So for some class A, is_ancestor (A, A) will
2850 is_ancestor (struct type *base, struct type *dclass)
2852 return distance_to_ancestor (base, dclass, 0) >= 0;
2855 /* Like is_ancestor, but only returns true when BASE is a public
2856 ancestor of DCLASS. */
2859 is_public_ancestor (struct type *base, struct type *dclass)
2861 return distance_to_ancestor (base, dclass, 1) >= 0;
2864 /* A helper function for is_unique_ancestor. */
2867 is_unique_ancestor_worker (struct type *base, struct type *dclass,
2869 const gdb_byte *valaddr, int embedded_offset,
2870 CORE_ADDR address, struct value *val)
2874 base = check_typedef (base);
2875 dclass = check_typedef (dclass);
2877 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2882 iter = check_typedef (TYPE_BASECLASS (dclass, i));
2884 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2887 if (class_types_same_p (base, iter))
2889 /* If this is the first subclass, set *OFFSET and set count
2890 to 1. Otherwise, if this is at the same offset as
2891 previous instances, do nothing. Otherwise, increment
2895 *offset = this_offset;
2898 else if (this_offset == *offset)
2906 count += is_unique_ancestor_worker (base, iter, offset,
2908 embedded_offset + this_offset,
2915 /* Like is_ancestor, but only returns true if BASE is a unique base
2916 class of the type of VAL. */
2919 is_unique_ancestor (struct type *base, struct value *val)
2923 return is_unique_ancestor_worker (base, value_type (val), &offset,
2924 value_contents_for_printing (val),
2925 value_embedded_offset (val),
2926 value_address (val), val) == 1;
2930 /* Overload resolution. */
2932 /* Return the sum of the rank of A with the rank of B. */
2935 sum_ranks (struct rank a, struct rank b)
2938 c.rank = a.rank + b.rank;
2939 c.subrank = a.subrank + b.subrank;
2943 /* Compare rank A and B and return:
2945 1 if a is better than b
2946 -1 if b is better than a. */
2949 compare_ranks (struct rank a, struct rank b)
2951 if (a.rank == b.rank)
2953 if (a.subrank == b.subrank)
2955 if (a.subrank < b.subrank)
2957 if (a.subrank > b.subrank)
2961 if (a.rank < b.rank)
2964 /* a.rank > b.rank */
2968 /* Functions for overload resolution begin here. */
2970 /* Compare two badness vectors A and B and return the result.
2971 0 => A and B are identical
2972 1 => A and B are incomparable
2973 2 => A is better than B
2974 3 => A is worse than B */
2977 compare_badness (struct badness_vector *a, struct badness_vector *b)
2981 short found_pos = 0; /* any positives in c? */
2982 short found_neg = 0; /* any negatives in c? */
2984 /* differing lengths => incomparable */
2985 if (a->length != b->length)
2988 /* Subtract b from a */
2989 for (i = 0; i < a->length; i++)
2991 tmp = compare_ranks (b->rank[i], a->rank[i]);
3001 return 1; /* incomparable */
3003 return 3; /* A > B */
3009 return 2; /* A < B */
3011 return 0; /* A == B */
3015 /* Rank a function by comparing its parameter types (PARMS, length
3016 NPARMS), to the types of an argument list (ARGS, length NARGS).
3017 Return a pointer to a badness vector. This has NARGS + 1
3020 struct badness_vector *
3021 rank_function (struct type **parms, int nparms,
3022 struct value **args, int nargs)
3025 struct badness_vector *bv;
3026 int min_len = nparms < nargs ? nparms : nargs;
3028 bv = xmalloc (sizeof (struct badness_vector));
3029 bv->length = nargs + 1; /* add 1 for the length-match rank. */
3030 bv->rank = XNEWVEC (struct rank, nargs + 1);
3032 /* First compare the lengths of the supplied lists.
3033 If there is a mismatch, set it to a high value. */
3035 /* pai/1997-06-03 FIXME: when we have debug info about default
3036 arguments and ellipsis parameter lists, we should consider those
3037 and rank the length-match more finely. */
3039 LENGTH_MATCH (bv) = (nargs != nparms)
3040 ? LENGTH_MISMATCH_BADNESS
3041 : EXACT_MATCH_BADNESS;
3043 /* Now rank all the parameters of the candidate function. */
3044 for (i = 1; i <= min_len; i++)
3045 bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
3048 /* If more arguments than parameters, add dummy entries. */
3049 for (i = min_len + 1; i <= nargs; i++)
3050 bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
3055 /* Compare the names of two integer types, assuming that any sign
3056 qualifiers have been checked already. We do it this way because
3057 there may be an "int" in the name of one of the types. */
3060 integer_types_same_name_p (const char *first, const char *second)
3062 int first_p, second_p;
3064 /* If both are shorts, return 1; if neither is a short, keep
3066 first_p = (strstr (first, "short") != NULL);
3067 second_p = (strstr (second, "short") != NULL);
3068 if (first_p && second_p)
3070 if (first_p || second_p)
3073 /* Likewise for long. */
3074 first_p = (strstr (first, "long") != NULL);
3075 second_p = (strstr (second, "long") != NULL);
3076 if (first_p && second_p)
3078 if (first_p || second_p)
3081 /* Likewise for char. */
3082 first_p = (strstr (first, "char") != NULL);
3083 second_p = (strstr (second, "char") != NULL);
3084 if (first_p && second_p)
3086 if (first_p || second_p)
3089 /* They must both be ints. */
3093 /* Compares type A to type B returns 1 if the represent the same type
3097 types_equal (struct type *a, struct type *b)
3099 /* Identical type pointers. */
3100 /* However, this still doesn't catch all cases of same type for b
3101 and a. The reason is that builtin types are different from
3102 the same ones constructed from the object. */
3106 /* Resolve typedefs */
3107 if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
3108 a = check_typedef (a);
3109 if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
3110 b = check_typedef (b);
3112 /* If after resolving typedefs a and b are not of the same type
3113 code then they are not equal. */
3114 if (TYPE_CODE (a) != TYPE_CODE (b))
3117 /* If a and b are both pointers types or both reference types then
3118 they are equal of the same type iff the objects they refer to are
3119 of the same type. */
3120 if (TYPE_CODE (a) == TYPE_CODE_PTR
3121 || TYPE_CODE (a) == TYPE_CODE_REF)
3122 return types_equal (TYPE_TARGET_TYPE (a),
3123 TYPE_TARGET_TYPE (b));
3125 /* Well, damnit, if the names are exactly the same, I'll say they
3126 are exactly the same. This happens when we generate method
3127 stubs. The types won't point to the same address, but they
3128 really are the same. */
3130 if (TYPE_NAME (a) && TYPE_NAME (b)
3131 && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
3134 /* Check if identical after resolving typedefs. */
3138 /* Two function types are equal if their argument and return types
3140 if (TYPE_CODE (a) == TYPE_CODE_FUNC)
3144 if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
3147 if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
3150 for (i = 0; i < TYPE_NFIELDS (a); ++i)
3151 if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
3160 /* Deep comparison of types. */
3162 /* An entry in the type-equality bcache. */
3164 typedef struct type_equality_entry
3166 struct type *type1, *type2;
3167 } type_equality_entry_d;
3169 DEF_VEC_O (type_equality_entry_d);
3171 /* A helper function to compare two strings. Returns 1 if they are
3172 the same, 0 otherwise. Handles NULLs properly. */
3175 compare_maybe_null_strings (const char *s, const char *t)
3177 if (s == NULL && t != NULL)
3179 else if (s != NULL && t == NULL)
3181 else if (s == NULL && t== NULL)
3183 return strcmp (s, t) == 0;
3186 /* A helper function for check_types_worklist that checks two types for
3187 "deep" equality. Returns non-zero if the types are considered the
3188 same, zero otherwise. */
3191 check_types_equal (struct type *type1, struct type *type2,
3192 VEC (type_equality_entry_d) **worklist)
3194 type1 = check_typedef (type1);
3195 type2 = check_typedef (type2);
3200 if (TYPE_CODE (type1) != TYPE_CODE (type2)
3201 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
3202 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
3203 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
3204 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
3205 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
3206 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
3207 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
3208 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
3211 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
3212 TYPE_TAG_NAME (type2)))
3214 if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
3217 if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
3219 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
3220 sizeof (*TYPE_RANGE_DATA (type1))) != 0)
3227 for (i = 0; i < TYPE_NFIELDS (type1); ++i)
3229 const struct field *field1 = &TYPE_FIELD (type1, i);
3230 const struct field *field2 = &TYPE_FIELD (type2, i);
3231 struct type_equality_entry entry;
3233 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
3234 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
3235 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
3237 if (!compare_maybe_null_strings (FIELD_NAME (*field1),
3238 FIELD_NAME (*field2)))
3240 switch (FIELD_LOC_KIND (*field1))
3242 case FIELD_LOC_KIND_BITPOS:
3243 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
3246 case FIELD_LOC_KIND_ENUMVAL:
3247 if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
3250 case FIELD_LOC_KIND_PHYSADDR:
3251 if (FIELD_STATIC_PHYSADDR (*field1)
3252 != FIELD_STATIC_PHYSADDR (*field2))
3255 case FIELD_LOC_KIND_PHYSNAME:
3256 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
3257 FIELD_STATIC_PHYSNAME (*field2)))
3260 case FIELD_LOC_KIND_DWARF_BLOCK:
3262 struct dwarf2_locexpr_baton *block1, *block2;
3264 block1 = FIELD_DWARF_BLOCK (*field1);
3265 block2 = FIELD_DWARF_BLOCK (*field2);
3266 if (block1->per_cu != block2->per_cu
3267 || block1->size != block2->size
3268 || memcmp (block1->data, block2->data, block1->size) != 0)
3273 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
3274 "%d by check_types_equal"),
3275 FIELD_LOC_KIND (*field1));
3278 entry.type1 = FIELD_TYPE (*field1);
3279 entry.type2 = FIELD_TYPE (*field2);
3280 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3284 if (TYPE_TARGET_TYPE (type1) != NULL)
3286 struct type_equality_entry entry;
3288 if (TYPE_TARGET_TYPE (type2) == NULL)
3291 entry.type1 = TYPE_TARGET_TYPE (type1);
3292 entry.type2 = TYPE_TARGET_TYPE (type2);
3293 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3295 else if (TYPE_TARGET_TYPE (type2) != NULL)
3301 /* Check types on a worklist for equality. Returns zero if any pair
3302 is not equal, non-zero if they are all considered equal. */
3305 check_types_worklist (VEC (type_equality_entry_d) **worklist,
3306 struct bcache *cache)
3308 while (!VEC_empty (type_equality_entry_d, *worklist))
3310 struct type_equality_entry entry;
3313 entry = *VEC_last (type_equality_entry_d, *worklist);
3314 VEC_pop (type_equality_entry_d, *worklist);
3316 /* If the type pair has already been visited, we know it is
3318 bcache_full (&entry, sizeof (entry), cache, &added);
3322 if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
3329 /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
3330 "deep comparison". Otherwise return zero. */
3333 types_deeply_equal (struct type *type1, struct type *type2)
3335 struct gdb_exception except = exception_none;
3337 struct bcache *cache;
3338 VEC (type_equality_entry_d) *worklist = NULL;
3339 struct type_equality_entry entry;
3341 gdb_assert (type1 != NULL && type2 != NULL);
3343 /* Early exit for the simple case. */
3347 cache = bcache_xmalloc (NULL, NULL);
3349 entry.type1 = type1;
3350 entry.type2 = type2;
3351 VEC_safe_push (type_equality_entry_d, worklist, &entry);
3353 /* check_types_worklist calls several nested helper functions, some
3354 of which can raise a GDB exception, so we just check and rethrow
3355 here. If there is a GDB exception, a comparison is not capable
3356 (or trusted), so exit. */
3359 result = check_types_worklist (&worklist, cache);
3361 CATCH (ex, RETURN_MASK_ALL)
3367 bcache_xfree (cache);
3368 VEC_free (type_equality_entry_d, worklist);
3370 /* Rethrow if there was a problem. */
3371 if (except.reason < 0)
3372 throw_exception (except);
3377 /* Compare one type (PARM) for compatibility with another (ARG).
3378 * PARM is intended to be the parameter type of a function; and
3379 * ARG is the supplied argument's type. This function tests if
3380 * the latter can be converted to the former.
3381 * VALUE is the argument's value or NULL if none (or called recursively)
3383 * Return 0 if they are identical types;
3384 * Otherwise, return an integer which corresponds to how compatible
3385 * PARM is to ARG. The higher the return value, the worse the match.
3386 * Generally the "bad" conversions are all uniformly assigned a 100. */
3389 rank_one_type (struct type *parm, struct type *arg, struct value *value)
3391 struct rank rank = {0,0};
3393 if (types_equal (parm, arg))
3394 return EXACT_MATCH_BADNESS;
3396 /* Resolve typedefs */
3397 if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
3398 parm = check_typedef (parm);
3399 if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
3400 arg = check_typedef (arg);
3402 /* See through references, since we can almost make non-references
3404 if (TYPE_CODE (arg) == TYPE_CODE_REF)
3405 return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
3406 REFERENCE_CONVERSION_BADNESS));
3407 if (TYPE_CODE (parm) == TYPE_CODE_REF)
3408 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
3409 REFERENCE_CONVERSION_BADNESS));
3411 /* Debugging only. */
3412 fprintf_filtered (gdb_stderr,
3413 "------ Arg is %s [%d], parm is %s [%d]\n",
3414 TYPE_NAME (arg), TYPE_CODE (arg),
3415 TYPE_NAME (parm), TYPE_CODE (parm));
3417 /* x -> y means arg of type x being supplied for parameter of type y. */
3419 switch (TYPE_CODE (parm))
3422 switch (TYPE_CODE (arg))
3426 /* Allowed pointer conversions are:
3427 (a) pointer to void-pointer conversion. */
3428 if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
3429 return VOID_PTR_CONVERSION_BADNESS;
3431 /* (b) pointer to ancestor-pointer conversion. */
3432 rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
3433 TYPE_TARGET_TYPE (arg),
3435 if (rank.subrank >= 0)
3436 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
3438 return INCOMPATIBLE_TYPE_BADNESS;
3439 case TYPE_CODE_ARRAY:
3440 if (types_equal (TYPE_TARGET_TYPE (parm),
3441 TYPE_TARGET_TYPE (arg)))
3442 return EXACT_MATCH_BADNESS;
3443 return INCOMPATIBLE_TYPE_BADNESS;
3444 case TYPE_CODE_FUNC:
3445 return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
3447 if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
3449 if (value_as_long (value) == 0)
3451 /* Null pointer conversion: allow it to be cast to a pointer.
3452 [4.10.1 of C++ standard draft n3290] */
3453 return NULL_POINTER_CONVERSION_BADNESS;
3457 /* If type checking is disabled, allow the conversion. */
3458 if (!strict_type_checking)
3459 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
3463 case TYPE_CODE_ENUM:
3464 case TYPE_CODE_FLAGS:
3465 case TYPE_CODE_CHAR:
3466 case TYPE_CODE_RANGE:
3467 case TYPE_CODE_BOOL:
3469 return INCOMPATIBLE_TYPE_BADNESS;
3471 case TYPE_CODE_ARRAY:
3472 switch (TYPE_CODE (arg))
3475 case TYPE_CODE_ARRAY:
3476 return rank_one_type (TYPE_TARGET_TYPE (parm),
3477 TYPE_TARGET_TYPE (arg), NULL);
3479 return INCOMPATIBLE_TYPE_BADNESS;
3481 case TYPE_CODE_FUNC:
3482 switch (TYPE_CODE (arg))
3484 case TYPE_CODE_PTR: /* funcptr -> func */
3485 return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
3487 return INCOMPATIBLE_TYPE_BADNESS;
3490 switch (TYPE_CODE (arg))
3493 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3495 /* Deal with signed, unsigned, and plain chars and
3496 signed and unsigned ints. */
3497 if (TYPE_NOSIGN (parm))
3499 /* This case only for character types. */
3500 if (TYPE_NOSIGN (arg))
3501 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
3502 else /* signed/unsigned char -> plain char */
3503 return INTEGER_CONVERSION_BADNESS;
3505 else if (TYPE_UNSIGNED (parm))
3507 if (TYPE_UNSIGNED (arg))
3509 /* unsigned int -> unsigned int, or
3510 unsigned long -> unsigned long */
3511 if (integer_types_same_name_p (TYPE_NAME (parm),
3513 return EXACT_MATCH_BADNESS;
3514 else if (integer_types_same_name_p (TYPE_NAME (arg),
3516 && integer_types_same_name_p (TYPE_NAME (parm),
3518 /* unsigned int -> unsigned long */
3519 return INTEGER_PROMOTION_BADNESS;
3521 /* unsigned long -> unsigned int */
3522 return INTEGER_CONVERSION_BADNESS;
3526 if (integer_types_same_name_p (TYPE_NAME (arg),
3528 && integer_types_same_name_p (TYPE_NAME (parm),
3530 /* signed long -> unsigned int */
3531 return INTEGER_CONVERSION_BADNESS;
3533 /* signed int/long -> unsigned int/long */
3534 return INTEGER_CONVERSION_BADNESS;
3537 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3539 if (integer_types_same_name_p (TYPE_NAME (parm),
3541 return EXACT_MATCH_BADNESS;
3542 else if (integer_types_same_name_p (TYPE_NAME (arg),
3544 && integer_types_same_name_p (TYPE_NAME (parm),
3546 return INTEGER_PROMOTION_BADNESS;
3548 return INTEGER_CONVERSION_BADNESS;
3551 return INTEGER_CONVERSION_BADNESS;
3553 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3554 return INTEGER_PROMOTION_BADNESS;
3556 return INTEGER_CONVERSION_BADNESS;
3557 case TYPE_CODE_ENUM:
3558 case TYPE_CODE_FLAGS:
3559 case TYPE_CODE_CHAR:
3560 case TYPE_CODE_RANGE:
3561 case TYPE_CODE_BOOL:
3562 if (TYPE_DECLARED_CLASS (arg))
3563 return INCOMPATIBLE_TYPE_BADNESS;
3564 return INTEGER_PROMOTION_BADNESS;
3566 return INT_FLOAT_CONVERSION_BADNESS;
3568 return NS_POINTER_CONVERSION_BADNESS;
3570 return INCOMPATIBLE_TYPE_BADNESS;
3573 case TYPE_CODE_ENUM:
3574 switch (TYPE_CODE (arg))
3577 case TYPE_CODE_CHAR:
3578 case TYPE_CODE_RANGE:
3579 case TYPE_CODE_BOOL:
3580 case TYPE_CODE_ENUM:
3581 if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg))
3582 return INCOMPATIBLE_TYPE_BADNESS;
3583 return INTEGER_CONVERSION_BADNESS;
3585 return INT_FLOAT_CONVERSION_BADNESS;
3587 return INCOMPATIBLE_TYPE_BADNESS;
3590 case TYPE_CODE_CHAR:
3591 switch (TYPE_CODE (arg))
3593 case TYPE_CODE_RANGE:
3594 case TYPE_CODE_BOOL:
3595 case TYPE_CODE_ENUM:
3596 if (TYPE_DECLARED_CLASS (arg))
3597 return INCOMPATIBLE_TYPE_BADNESS;
3598 return INTEGER_CONVERSION_BADNESS;
3600 return INT_FLOAT_CONVERSION_BADNESS;
3602 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
3603 return INTEGER_CONVERSION_BADNESS;
3604 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3605 return INTEGER_PROMOTION_BADNESS;
3606 /* >>> !! else fall through !! <<< */
3607 case TYPE_CODE_CHAR:
3608 /* Deal with signed, unsigned, and plain chars for C++ and
3609 with int cases falling through from previous case. */
3610 if (TYPE_NOSIGN (parm))
3612 if (TYPE_NOSIGN (arg))
3613 return EXACT_MATCH_BADNESS;
3615 return INTEGER_CONVERSION_BADNESS;
3617 else if (TYPE_UNSIGNED (parm))
3619 if (TYPE_UNSIGNED (arg))
3620 return EXACT_MATCH_BADNESS;
3622 return INTEGER_PROMOTION_BADNESS;
3624 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3625 return EXACT_MATCH_BADNESS;
3627 return INTEGER_CONVERSION_BADNESS;
3629 return INCOMPATIBLE_TYPE_BADNESS;
3632 case TYPE_CODE_RANGE:
3633 switch (TYPE_CODE (arg))
3636 case TYPE_CODE_CHAR:
3637 case TYPE_CODE_RANGE:
3638 case TYPE_CODE_BOOL:
3639 case TYPE_CODE_ENUM:
3640 return INTEGER_CONVERSION_BADNESS;
3642 return INT_FLOAT_CONVERSION_BADNESS;
3644 return INCOMPATIBLE_TYPE_BADNESS;
3647 case TYPE_CODE_BOOL:
3648 switch (TYPE_CODE (arg))
3650 /* n3290 draft, section 4.12.1 (conv.bool):
3652 "A prvalue of arithmetic, unscoped enumeration, pointer, or
3653 pointer to member type can be converted to a prvalue of type
3654 bool. A zero value, null pointer value, or null member pointer
3655 value is converted to false; any other value is converted to
3656 true. A prvalue of type std::nullptr_t can be converted to a
3657 prvalue of type bool; the resulting value is false." */
3659 case TYPE_CODE_CHAR:
3660 case TYPE_CODE_ENUM:
3662 case TYPE_CODE_MEMBERPTR:
3664 return BOOL_CONVERSION_BADNESS;
3665 case TYPE_CODE_RANGE:
3666 return INCOMPATIBLE_TYPE_BADNESS;
3667 case TYPE_CODE_BOOL:
3668 return EXACT_MATCH_BADNESS;
3670 return INCOMPATIBLE_TYPE_BADNESS;
3674 switch (TYPE_CODE (arg))
3677 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3678 return FLOAT_PROMOTION_BADNESS;
3679 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3680 return EXACT_MATCH_BADNESS;
3682 return FLOAT_CONVERSION_BADNESS;
3684 case TYPE_CODE_BOOL:
3685 case TYPE_CODE_ENUM:
3686 case TYPE_CODE_RANGE:
3687 case TYPE_CODE_CHAR:
3688 return INT_FLOAT_CONVERSION_BADNESS;
3690 return INCOMPATIBLE_TYPE_BADNESS;
3693 case TYPE_CODE_COMPLEX:
3694 switch (TYPE_CODE (arg))
3695 { /* Strictly not needed for C++, but... */
3697 return FLOAT_PROMOTION_BADNESS;
3698 case TYPE_CODE_COMPLEX:
3699 return EXACT_MATCH_BADNESS;
3701 return INCOMPATIBLE_TYPE_BADNESS;
3704 case TYPE_CODE_STRUCT:
3705 switch (TYPE_CODE (arg))
3707 case TYPE_CODE_STRUCT:
3708 /* Check for derivation */
3709 rank.subrank = distance_to_ancestor (parm, arg, 0);
3710 if (rank.subrank >= 0)
3711 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
3712 /* else fall through */
3714 return INCOMPATIBLE_TYPE_BADNESS;
3717 case TYPE_CODE_UNION:
3718 switch (TYPE_CODE (arg))
3720 case TYPE_CODE_UNION:
3722 return INCOMPATIBLE_TYPE_BADNESS;
3725 case TYPE_CODE_MEMBERPTR:
3726 switch (TYPE_CODE (arg))
3729 return INCOMPATIBLE_TYPE_BADNESS;
3732 case TYPE_CODE_METHOD:
3733 switch (TYPE_CODE (arg))
3737 return INCOMPATIBLE_TYPE_BADNESS;
3741 switch (TYPE_CODE (arg))
3745 return INCOMPATIBLE_TYPE_BADNESS;
3750 switch (TYPE_CODE (arg))
3754 return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
3755 TYPE_FIELD_TYPE (arg, 0), NULL);
3757 return INCOMPATIBLE_TYPE_BADNESS;
3760 case TYPE_CODE_VOID:
3762 return INCOMPATIBLE_TYPE_BADNESS;
3763 } /* switch (TYPE_CODE (arg)) */
3766 /* End of functions for overload resolution. */
3768 /* Routines to pretty-print types. */
3771 print_bit_vector (B_TYPE *bits, int nbits)
3775 for (bitno = 0; bitno < nbits; bitno++)
3777 if ((bitno % 8) == 0)
3779 puts_filtered (" ");
3781 if (B_TST (bits, bitno))
3782 printf_filtered (("1"));
3784 printf_filtered (("0"));
3788 /* Note the first arg should be the "this" pointer, we may not want to
3789 include it since we may get into a infinitely recursive
3793 print_args (struct field *args, int nargs, int spaces)
3799 for (i = 0; i < nargs; i++)
3801 printfi_filtered (spaces, "[%d] name '%s'\n", i,
3802 args[i].name != NULL ? args[i].name : "<NULL>");
3803 recursive_dump_type (args[i].type, spaces + 2);
3809 field_is_static (struct field *f)
3811 /* "static" fields are the fields whose location is not relative
3812 to the address of the enclosing struct. It would be nice to
3813 have a dedicated flag that would be set for static fields when
3814 the type is being created. But in practice, checking the field
3815 loc_kind should give us an accurate answer. */
3816 return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3817 || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3821 dump_fn_fieldlists (struct type *type, int spaces)
3827 printfi_filtered (spaces, "fn_fieldlists ");
3828 gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
3829 printf_filtered ("\n");
3830 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3832 f = TYPE_FN_FIELDLIST1 (type, method_idx);
3833 printfi_filtered (spaces + 2, "[%d] name '%s' (",
3835 TYPE_FN_FIELDLIST_NAME (type, method_idx));
3836 gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3838 printf_filtered (_(") length %d\n"),
3839 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3840 for (overload_idx = 0;
3841 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3844 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3846 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
3847 gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3849 printf_filtered (")\n");
3850 printfi_filtered (spaces + 8, "type ");
3851 gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
3853 printf_filtered ("\n");
3855 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3858 printfi_filtered (spaces + 8, "args ");
3859 gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
3861 printf_filtered ("\n");
3862 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
3863 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)),
3865 printfi_filtered (spaces + 8, "fcontext ");
3866 gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3868 printf_filtered ("\n");
3870 printfi_filtered (spaces + 8, "is_const %d\n",
3871 TYPE_FN_FIELD_CONST (f, overload_idx));
3872 printfi_filtered (spaces + 8, "is_volatile %d\n",
3873 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3874 printfi_filtered (spaces + 8, "is_private %d\n",
3875 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3876 printfi_filtered (spaces + 8, "is_protected %d\n",
3877 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3878 printfi_filtered (spaces + 8, "is_stub %d\n",
3879 TYPE_FN_FIELD_STUB (f, overload_idx));
3880 printfi_filtered (spaces + 8, "voffset %u\n",
3881 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3887 print_cplus_stuff (struct type *type, int spaces)
3889 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
3890 printfi_filtered (spaces, "vptr_basetype ");
3891 gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3892 puts_filtered ("\n");
3893 if (TYPE_VPTR_BASETYPE (type) != NULL)
3894 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3896 printfi_filtered (spaces, "n_baseclasses %d\n",
3897 TYPE_N_BASECLASSES (type));
3898 printfi_filtered (spaces, "nfn_fields %d\n",
3899 TYPE_NFN_FIELDS (type));
3900 if (TYPE_N_BASECLASSES (type) > 0)
3902 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3903 TYPE_N_BASECLASSES (type));
3904 gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
3906 printf_filtered (")");
3908 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3909 TYPE_N_BASECLASSES (type));
3910 puts_filtered ("\n");
3912 if (TYPE_NFIELDS (type) > 0)
3914 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3916 printfi_filtered (spaces,
3917 "private_field_bits (%d bits at *",
3918 TYPE_NFIELDS (type));
3919 gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
3921 printf_filtered (")");
3922 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3923 TYPE_NFIELDS (type));
3924 puts_filtered ("\n");
3926 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3928 printfi_filtered (spaces,
3929 "protected_field_bits (%d bits at *",
3930 TYPE_NFIELDS (type));
3931 gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
3933 printf_filtered (")");
3934 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3935 TYPE_NFIELDS (type));
3936 puts_filtered ("\n");
3939 if (TYPE_NFN_FIELDS (type) > 0)
3941 dump_fn_fieldlists (type, spaces);
3945 /* Print the contents of the TYPE's type_specific union, assuming that
3946 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
3949 print_gnat_stuff (struct type *type, int spaces)
3951 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3953 if (descriptive_type == NULL)
3954 printfi_filtered (spaces + 2, "no descriptive type\n");
3957 printfi_filtered (spaces + 2, "descriptive type\n");
3958 recursive_dump_type (descriptive_type, spaces + 4);
3962 static struct obstack dont_print_type_obstack;
3965 recursive_dump_type (struct type *type, int spaces)
3970 obstack_begin (&dont_print_type_obstack, 0);
3972 if (TYPE_NFIELDS (type) > 0
3973 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
3975 struct type **first_dont_print
3976 = (struct type **) obstack_base (&dont_print_type_obstack);
3978 int i = (struct type **)
3979 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
3983 if (type == first_dont_print[i])
3985 printfi_filtered (spaces, "type node ");
3986 gdb_print_host_address (type, gdb_stdout);
3987 printf_filtered (_(" <same as already seen type>\n"));
3992 obstack_ptr_grow (&dont_print_type_obstack, type);
3995 printfi_filtered (spaces, "type node ");
3996 gdb_print_host_address (type, gdb_stdout);
3997 printf_filtered ("\n");
3998 printfi_filtered (spaces, "name '%s' (",
3999 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
4000 gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
4001 printf_filtered (")\n");
4002 printfi_filtered (spaces, "tagname '%s' (",
4003 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
4004 gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
4005 printf_filtered (")\n");
4006 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
4007 switch (TYPE_CODE (type))
4009 case TYPE_CODE_UNDEF:
4010 printf_filtered ("(TYPE_CODE_UNDEF)");
4013 printf_filtered ("(TYPE_CODE_PTR)");
4015 case TYPE_CODE_ARRAY:
4016 printf_filtered ("(TYPE_CODE_ARRAY)");
4018 case TYPE_CODE_STRUCT:
4019 printf_filtered ("(TYPE_CODE_STRUCT)");
4021 case TYPE_CODE_UNION:
4022 printf_filtered ("(TYPE_CODE_UNION)");
4024 case TYPE_CODE_ENUM:
4025 printf_filtered ("(TYPE_CODE_ENUM)");
4027 case TYPE_CODE_FLAGS:
4028 printf_filtered ("(TYPE_CODE_FLAGS)");
4030 case TYPE_CODE_FUNC:
4031 printf_filtered ("(TYPE_CODE_FUNC)");
4034 printf_filtered ("(TYPE_CODE_INT)");
4037 printf_filtered ("(TYPE_CODE_FLT)");
4039 case TYPE_CODE_VOID:
4040 printf_filtered ("(TYPE_CODE_VOID)");
4043 printf_filtered ("(TYPE_CODE_SET)");
4045 case TYPE_CODE_RANGE:
4046 printf_filtered ("(TYPE_CODE_RANGE)");
4048 case TYPE_CODE_STRING:
4049 printf_filtered ("(TYPE_CODE_STRING)");
4051 case TYPE_CODE_ERROR:
4052 printf_filtered ("(TYPE_CODE_ERROR)");
4054 case TYPE_CODE_MEMBERPTR:
4055 printf_filtered ("(TYPE_CODE_MEMBERPTR)");
4057 case TYPE_CODE_METHODPTR:
4058 printf_filtered ("(TYPE_CODE_METHODPTR)");
4060 case TYPE_CODE_METHOD:
4061 printf_filtered ("(TYPE_CODE_METHOD)");
4064 printf_filtered ("(TYPE_CODE_REF)");
4066 case TYPE_CODE_CHAR:
4067 printf_filtered ("(TYPE_CODE_CHAR)");
4069 case TYPE_CODE_BOOL:
4070 printf_filtered ("(TYPE_CODE_BOOL)");
4072 case TYPE_CODE_COMPLEX:
4073 printf_filtered ("(TYPE_CODE_COMPLEX)");
4075 case TYPE_CODE_TYPEDEF:
4076 printf_filtered ("(TYPE_CODE_TYPEDEF)");
4078 case TYPE_CODE_NAMESPACE:
4079 printf_filtered ("(TYPE_CODE_NAMESPACE)");
4082 printf_filtered ("(UNKNOWN TYPE CODE)");
4085 puts_filtered ("\n");
4086 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
4087 if (TYPE_OBJFILE_OWNED (type))
4089 printfi_filtered (spaces, "objfile ");
4090 gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
4094 printfi_filtered (spaces, "gdbarch ");
4095 gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
4097 printf_filtered ("\n");
4098 printfi_filtered (spaces, "target_type ");
4099 gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
4100 printf_filtered ("\n");
4101 if (TYPE_TARGET_TYPE (type) != NULL)
4103 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
4105 printfi_filtered (spaces, "pointer_type ");
4106 gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
4107 printf_filtered ("\n");
4108 printfi_filtered (spaces, "reference_type ");
4109 gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
4110 printf_filtered ("\n");
4111 printfi_filtered (spaces, "type_chain ");
4112 gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
4113 printf_filtered ("\n");
4114 printfi_filtered (spaces, "instance_flags 0x%x",
4115 TYPE_INSTANCE_FLAGS (type));
4116 if (TYPE_CONST (type))
4118 puts_filtered (" TYPE_FLAG_CONST");
4120 if (TYPE_VOLATILE (type))
4122 puts_filtered (" TYPE_FLAG_VOLATILE");
4124 if (TYPE_CODE_SPACE (type))
4126 puts_filtered (" TYPE_FLAG_CODE_SPACE");
4128 if (TYPE_DATA_SPACE (type))
4130 puts_filtered (" TYPE_FLAG_DATA_SPACE");
4132 if (TYPE_ADDRESS_CLASS_1 (type))
4134 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
4136 if (TYPE_ADDRESS_CLASS_2 (type))
4138 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
4140 if (TYPE_RESTRICT (type))
4142 puts_filtered (" TYPE_FLAG_RESTRICT");
4144 if (TYPE_ATOMIC (type))
4146 puts_filtered (" TYPE_FLAG_ATOMIC");
4148 puts_filtered ("\n");
4150 printfi_filtered (spaces, "flags");
4151 if (TYPE_UNSIGNED (type))
4153 puts_filtered (" TYPE_FLAG_UNSIGNED");
4155 if (TYPE_NOSIGN (type))
4157 puts_filtered (" TYPE_FLAG_NOSIGN");
4159 if (TYPE_STUB (type))
4161 puts_filtered (" TYPE_FLAG_STUB");
4163 if (TYPE_TARGET_STUB (type))
4165 puts_filtered (" TYPE_FLAG_TARGET_STUB");
4167 if (TYPE_STATIC (type))
4169 puts_filtered (" TYPE_FLAG_STATIC");
4171 if (TYPE_PROTOTYPED (type))
4173 puts_filtered (" TYPE_FLAG_PROTOTYPED");
4175 if (TYPE_INCOMPLETE (type))
4177 puts_filtered (" TYPE_FLAG_INCOMPLETE");
4179 if (TYPE_VARARGS (type))
4181 puts_filtered (" TYPE_FLAG_VARARGS");
4183 /* This is used for things like AltiVec registers on ppc. Gcc emits
4184 an attribute for the array type, which tells whether or not we
4185 have a vector, instead of a regular array. */
4186 if (TYPE_VECTOR (type))
4188 puts_filtered (" TYPE_FLAG_VECTOR");
4190 if (TYPE_FIXED_INSTANCE (type))
4192 puts_filtered (" TYPE_FIXED_INSTANCE");
4194 if (TYPE_STUB_SUPPORTED (type))
4196 puts_filtered (" TYPE_STUB_SUPPORTED");
4198 if (TYPE_NOTTEXT (type))
4200 puts_filtered (" TYPE_NOTTEXT");
4202 puts_filtered ("\n");
4203 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
4204 gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
4205 puts_filtered ("\n");
4206 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
4208 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
4209 printfi_filtered (spaces + 2,
4210 "[%d] enumval %s type ",
4211 idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
4213 printfi_filtered (spaces + 2,
4214 "[%d] bitpos %d bitsize %d type ",
4215 idx, TYPE_FIELD_BITPOS (type, idx),
4216 TYPE_FIELD_BITSIZE (type, idx));
4217 gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
4218 printf_filtered (" name '%s' (",
4219 TYPE_FIELD_NAME (type, idx) != NULL
4220 ? TYPE_FIELD_NAME (type, idx)
4222 gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
4223 printf_filtered (")\n");
4224 if (TYPE_FIELD_TYPE (type, idx) != NULL)
4226 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
4229 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
4231 printfi_filtered (spaces, "low %s%s high %s%s\n",
4232 plongest (TYPE_LOW_BOUND (type)),
4233 TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
4234 plongest (TYPE_HIGH_BOUND (type)),
4235 TYPE_HIGH_BOUND_UNDEFINED (type)
4236 ? " (undefined)" : "");
4239 switch (TYPE_SPECIFIC_FIELD (type))
4241 case TYPE_SPECIFIC_CPLUS_STUFF:
4242 printfi_filtered (spaces, "cplus_stuff ");
4243 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
4245 puts_filtered ("\n");
4246 print_cplus_stuff (type, spaces);
4249 case TYPE_SPECIFIC_GNAT_STUFF:
4250 printfi_filtered (spaces, "gnat_stuff ");
4251 gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
4252 puts_filtered ("\n");
4253 print_gnat_stuff (type, spaces);
4256 case TYPE_SPECIFIC_FLOATFORMAT:
4257 printfi_filtered (spaces, "floatformat ");
4258 if (TYPE_FLOATFORMAT (type) == NULL)
4259 puts_filtered ("(null)");
4262 puts_filtered ("{ ");
4263 if (TYPE_FLOATFORMAT (type)[0] == NULL
4264 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
4265 puts_filtered ("(null)");
4267 puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
4269 puts_filtered (", ");
4270 if (TYPE_FLOATFORMAT (type)[1] == NULL
4271 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
4272 puts_filtered ("(null)");
4274 puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
4276 puts_filtered (" }");
4278 puts_filtered ("\n");
4281 case TYPE_SPECIFIC_FUNC:
4282 printfi_filtered (spaces, "calling_convention %d\n",
4283 TYPE_CALLING_CONVENTION (type));
4284 /* tail_call_list is not printed. */
4287 case TYPE_SPECIFIC_SELF_TYPE:
4288 printfi_filtered (spaces, "self_type ");
4289 gdb_print_host_address (TYPE_SELF_TYPE (type), gdb_stdout);
4290 puts_filtered ("\n");
4295 obstack_free (&dont_print_type_obstack, NULL);
4298 /* Trivial helpers for the libiberty hash table, for mapping one
4303 struct type *old, *newobj;
4307 type_pair_hash (const void *item)
4309 const struct type_pair *pair = item;
4311 return htab_hash_pointer (pair->old);
4315 type_pair_eq (const void *item_lhs, const void *item_rhs)
4317 const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
4319 return lhs->old == rhs->old;
4322 /* Allocate the hash table used by copy_type_recursive to walk
4323 types without duplicates. We use OBJFILE's obstack, because
4324 OBJFILE is about to be deleted. */
4327 create_copied_types_hash (struct objfile *objfile)
4329 return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
4330 NULL, &objfile->objfile_obstack,
4331 hashtab_obstack_allocate,
4332 dummy_obstack_deallocate);
4335 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
4337 static struct dynamic_prop_list *
4338 copy_dynamic_prop_list (struct obstack *objfile_obstack,
4339 struct dynamic_prop_list *list)
4341 struct dynamic_prop_list *copy = list;
4342 struct dynamic_prop_list **node_ptr = ©
4344 while (*node_ptr != NULL)
4346 struct dynamic_prop_list *node_copy;
4348 node_copy = obstack_copy (objfile_obstack, *node_ptr,
4349 sizeof (struct dynamic_prop_list));
4350 node_copy->prop = (*node_ptr)->prop;
4351 *node_ptr = node_copy;
4353 node_ptr = &node_copy->next;
4359 /* Recursively copy (deep copy) TYPE, if it is associated with
4360 OBJFILE. Return a new type allocated using malloc, a saved type if
4361 we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
4362 not associated with OBJFILE. */
4365 copy_type_recursive (struct objfile *objfile,
4367 htab_t copied_types)
4369 struct type_pair *stored, pair;
4371 struct type *new_type;
4373 if (! TYPE_OBJFILE_OWNED (type))
4376 /* This type shouldn't be pointing to any types in other objfiles;
4377 if it did, the type might disappear unexpectedly. */
4378 gdb_assert (TYPE_OBJFILE (type) == objfile);
4381 slot = htab_find_slot (copied_types, &pair, INSERT);
4383 return ((struct type_pair *) *slot)->newobj;
4385 new_type = alloc_type_arch (get_type_arch (type));
4387 /* We must add the new type to the hash table immediately, in case
4388 we encounter this type again during a recursive call below. */
4390 = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
4392 stored->newobj = new_type;
4395 /* Copy the common fields of types. For the main type, we simply
4396 copy the entire thing and then update specific fields as needed. */
4397 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
4398 TYPE_OBJFILE_OWNED (new_type) = 0;
4399 TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
4401 if (TYPE_NAME (type))
4402 TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
4403 if (TYPE_TAG_NAME (type))
4404 TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
4406 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4407 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4409 /* Copy the fields. */
4410 if (TYPE_NFIELDS (type))
4414 nfields = TYPE_NFIELDS (type);
4415 TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
4416 for (i = 0; i < nfields; i++)
4418 TYPE_FIELD_ARTIFICIAL (new_type, i) =
4419 TYPE_FIELD_ARTIFICIAL (type, i);
4420 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
4421 if (TYPE_FIELD_TYPE (type, i))
4422 TYPE_FIELD_TYPE (new_type, i)
4423 = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
4425 if (TYPE_FIELD_NAME (type, i))
4426 TYPE_FIELD_NAME (new_type, i) =
4427 xstrdup (TYPE_FIELD_NAME (type, i));
4428 switch (TYPE_FIELD_LOC_KIND (type, i))
4430 case FIELD_LOC_KIND_BITPOS:
4431 SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
4432 TYPE_FIELD_BITPOS (type, i));
4434 case FIELD_LOC_KIND_ENUMVAL:
4435 SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
4436 TYPE_FIELD_ENUMVAL (type, i));
4438 case FIELD_LOC_KIND_PHYSADDR:
4439 SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
4440 TYPE_FIELD_STATIC_PHYSADDR (type, i));
4442 case FIELD_LOC_KIND_PHYSNAME:
4443 SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
4444 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
4448 internal_error (__FILE__, __LINE__,
4449 _("Unexpected type field location kind: %d"),
4450 TYPE_FIELD_LOC_KIND (type, i));
4455 /* For range types, copy the bounds information. */
4456 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
4458 TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
4459 *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
4462 if (TYPE_DYN_PROP_LIST (type) != NULL)
4463 TYPE_DYN_PROP_LIST (new_type)
4464 = copy_dynamic_prop_list (&objfile->objfile_obstack,
4465 TYPE_DYN_PROP_LIST (type));
4468 /* Copy pointers to other types. */
4469 if (TYPE_TARGET_TYPE (type))
4470 TYPE_TARGET_TYPE (new_type) =
4471 copy_type_recursive (objfile,
4472 TYPE_TARGET_TYPE (type),
4475 /* Maybe copy the type_specific bits.
4477 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
4478 base classes and methods. There's no fundamental reason why we
4479 can't, but at the moment it is not needed. */
4481 switch (TYPE_SPECIFIC_FIELD (type))
4483 case TYPE_SPECIFIC_NONE:
4485 case TYPE_SPECIFIC_FUNC:
4486 INIT_FUNC_SPECIFIC (new_type);
4487 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
4488 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
4489 TYPE_TAIL_CALL_LIST (new_type) = NULL;
4491 case TYPE_SPECIFIC_FLOATFORMAT:
4492 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
4494 case TYPE_SPECIFIC_CPLUS_STUFF:
4495 INIT_CPLUS_SPECIFIC (new_type);
4497 case TYPE_SPECIFIC_GNAT_STUFF:
4498 INIT_GNAT_SPECIFIC (new_type);
4500 case TYPE_SPECIFIC_SELF_TYPE:
4501 set_type_self_type (new_type,
4502 copy_type_recursive (objfile, TYPE_SELF_TYPE (type),
4506 gdb_assert_not_reached ("bad type_specific_kind");
4512 /* Make a copy of the given TYPE, except that the pointer & reference
4513 types are not preserved.
4515 This function assumes that the given type has an associated objfile.
4516 This objfile is used to allocate the new type. */
4519 copy_type (const struct type *type)
4521 struct type *new_type;
4523 gdb_assert (TYPE_OBJFILE_OWNED (type));
4525 new_type = alloc_type_copy (type);
4526 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4527 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4528 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
4529 sizeof (struct main_type));
4530 if (TYPE_DYN_PROP_LIST (type) != NULL)
4531 TYPE_DYN_PROP_LIST (new_type)
4532 = copy_dynamic_prop_list (&TYPE_OBJFILE (type) -> objfile_obstack,
4533 TYPE_DYN_PROP_LIST (type));
4538 /* Helper functions to initialize architecture-specific types. */
4540 /* Allocate a type structure associated with GDBARCH and set its
4541 CODE, LENGTH, and NAME fields. */
4544 arch_type (struct gdbarch *gdbarch,
4545 enum type_code code, int length, char *name)
4549 type = alloc_type_arch (gdbarch);
4550 TYPE_CODE (type) = code;
4551 TYPE_LENGTH (type) = length;
4554 TYPE_NAME (type) = xstrdup (name);
4559 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
4560 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4561 the type's TYPE_UNSIGNED flag. NAME is the type name. */
4564 arch_integer_type (struct gdbarch *gdbarch,
4565 int bit, int unsigned_p, char *name)
4569 t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
4571 TYPE_UNSIGNED (t) = 1;
4572 if (name && strcmp (name, "char") == 0)
4573 TYPE_NOSIGN (t) = 1;
4578 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
4579 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4580 the type's TYPE_UNSIGNED flag. NAME is the type name. */
4583 arch_character_type (struct gdbarch *gdbarch,
4584 int bit, int unsigned_p, char *name)
4588 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
4590 TYPE_UNSIGNED (t) = 1;
4595 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
4596 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4597 the type's TYPE_UNSIGNED flag. NAME is the type name. */
4600 arch_boolean_type (struct gdbarch *gdbarch,
4601 int bit, int unsigned_p, char *name)
4605 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
4607 TYPE_UNSIGNED (t) = 1;
4612 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
4613 BIT is the type size in bits; if BIT equals -1, the size is
4614 determined by the floatformat. NAME is the type name. Set the
4615 TYPE_FLOATFORMAT from FLOATFORMATS. */
4618 arch_float_type (struct gdbarch *gdbarch,
4619 int bit, char *name, const struct floatformat **floatformats)
4625 gdb_assert (floatformats != NULL);
4626 gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
4627 bit = floatformats[0]->totalsize;
4629 gdb_assert (bit >= 0);
4631 t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
4632 TYPE_FLOATFORMAT (t) = floatformats;
4636 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
4637 NAME is the type name. TARGET_TYPE is the component float type. */
4640 arch_complex_type (struct gdbarch *gdbarch,
4641 char *name, struct type *target_type)
4645 t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
4646 2 * TYPE_LENGTH (target_type), name);
4647 TYPE_TARGET_TYPE (t) = target_type;
4651 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
4652 NAME is the type name. LENGTH is the size of the flag word in bytes. */
4655 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
4657 int nfields = length * TARGET_CHAR_BIT;
4660 type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
4661 TYPE_UNSIGNED (type) = 1;
4662 TYPE_NFIELDS (type) = nfields;
4663 TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
4668 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
4669 position BITPOS is called NAME. */
4672 append_flags_type_flag (struct type *type, int bitpos, char *name)
4674 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
4675 gdb_assert (bitpos < TYPE_NFIELDS (type));
4676 gdb_assert (bitpos >= 0);
4680 TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
4681 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
4685 /* Don't show this field to the user. */
4686 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
4690 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
4691 specified by CODE) associated with GDBARCH. NAME is the type name. */
4694 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
4698 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
4699 t = arch_type (gdbarch, code, 0, NULL);
4700 TYPE_TAG_NAME (t) = name;
4701 INIT_CPLUS_SPECIFIC (t);
4705 /* Add new field with name NAME and type FIELD to composite type T.
4706 Do not set the field's position or adjust the type's length;
4707 the caller should do so. Return the new field. */
4710 append_composite_type_field_raw (struct type *t, char *name,
4715 TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
4716 TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
4717 sizeof (struct field) * TYPE_NFIELDS (t));
4718 f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
4719 memset (f, 0, sizeof f[0]);
4720 FIELD_TYPE (f[0]) = field;
4721 FIELD_NAME (f[0]) = name;
4725 /* Add new field with name NAME and type FIELD to composite type T.
4726 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
4729 append_composite_type_field_aligned (struct type *t, char *name,
4730 struct type *field, int alignment)
4732 struct field *f = append_composite_type_field_raw (t, name, field);
4734 if (TYPE_CODE (t) == TYPE_CODE_UNION)
4736 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
4737 TYPE_LENGTH (t) = TYPE_LENGTH (field);
4739 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4741 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4742 if (TYPE_NFIELDS (t) > 1)
4744 SET_FIELD_BITPOS (f[0],
4745 (FIELD_BITPOS (f[-1])
4746 + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4747 * TARGET_CHAR_BIT)));
4753 alignment *= TARGET_CHAR_BIT;
4754 left = FIELD_BITPOS (f[0]) % alignment;
4758 SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
4759 TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
4766 /* Add new field with name NAME and type FIELD to composite type T. */
4769 append_composite_type_field (struct type *t, char *name,
4772 append_composite_type_field_aligned (t, name, field, 0);
4775 static struct gdbarch_data *gdbtypes_data;
4777 const struct builtin_type *
4778 builtin_type (struct gdbarch *gdbarch)
4780 return gdbarch_data (gdbarch, gdbtypes_data);
4784 gdbtypes_post_init (struct gdbarch *gdbarch)
4786 struct builtin_type *builtin_type
4787 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4790 builtin_type->builtin_void
4791 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4792 builtin_type->builtin_char
4793 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4794 !gdbarch_char_signed (gdbarch), "char");
4795 builtin_type->builtin_signed_char
4796 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4798 builtin_type->builtin_unsigned_char
4799 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4800 1, "unsigned char");
4801 builtin_type->builtin_short
4802 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4804 builtin_type->builtin_unsigned_short
4805 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4806 1, "unsigned short");
4807 builtin_type->builtin_int
4808 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4810 builtin_type->builtin_unsigned_int
4811 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4813 builtin_type->builtin_long
4814 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4816 builtin_type->builtin_unsigned_long
4817 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4818 1, "unsigned long");
4819 builtin_type->builtin_long_long
4820 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4822 builtin_type->builtin_unsigned_long_long
4823 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4824 1, "unsigned long long");
4825 builtin_type->builtin_float
4826 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
4827 "float", gdbarch_float_format (gdbarch));
4828 builtin_type->builtin_double
4829 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
4830 "double", gdbarch_double_format (gdbarch));
4831 builtin_type->builtin_long_double
4832 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
4833 "long double", gdbarch_long_double_format (gdbarch));
4834 builtin_type->builtin_complex
4835 = arch_complex_type (gdbarch, "complex",
4836 builtin_type->builtin_float);
4837 builtin_type->builtin_double_complex
4838 = arch_complex_type (gdbarch, "double complex",
4839 builtin_type->builtin_double);
4840 builtin_type->builtin_string
4841 = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4842 builtin_type->builtin_bool
4843 = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
4845 /* The following three are about decimal floating point types, which
4846 are 32-bits, 64-bits and 128-bits respectively. */
4847 builtin_type->builtin_decfloat
4848 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
4849 builtin_type->builtin_decdouble
4850 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
4851 builtin_type->builtin_declong
4852 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
4854 /* "True" character types. */
4855 builtin_type->builtin_true_char
4856 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4857 builtin_type->builtin_true_unsigned_char
4858 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
4860 /* Fixed-size integer types. */
4861 builtin_type->builtin_int0
4862 = arch_integer_type (gdbarch, 0, 0, "int0_t");
4863 builtin_type->builtin_int8
4864 = arch_integer_type (gdbarch, 8, 0, "int8_t");
4865 builtin_type->builtin_uint8
4866 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4867 builtin_type->builtin_int16
4868 = arch_integer_type (gdbarch, 16, 0, "int16_t");
4869 builtin_type->builtin_uint16
4870 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4871 builtin_type->builtin_int32
4872 = arch_integer_type (gdbarch, 32, 0, "int32_t");
4873 builtin_type->builtin_uint32
4874 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4875 builtin_type->builtin_int64
4876 = arch_integer_type (gdbarch, 64, 0, "int64_t");
4877 builtin_type->builtin_uint64
4878 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4879 builtin_type->builtin_int128
4880 = arch_integer_type (gdbarch, 128, 0, "int128_t");
4881 builtin_type->builtin_uint128
4882 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
4883 TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4884 TYPE_INSTANCE_FLAG_NOTTEXT;
4885 TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4886 TYPE_INSTANCE_FLAG_NOTTEXT;
4888 /* Wide character types. */
4889 builtin_type->builtin_char16
4890 = arch_integer_type (gdbarch, 16, 0, "char16_t");
4891 builtin_type->builtin_char32
4892 = arch_integer_type (gdbarch, 32, 0, "char32_t");
4895 /* Default data/code pointer types. */
4896 builtin_type->builtin_data_ptr
4897 = lookup_pointer_type (builtin_type->builtin_void);
4898 builtin_type->builtin_func_ptr
4899 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
4900 builtin_type->builtin_func_func
4901 = lookup_function_type (builtin_type->builtin_func_ptr);
4903 /* This type represents a GDB internal function. */
4904 builtin_type->internal_fn
4905 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4906 "<internal function>");
4908 /* This type represents an xmethod. */
4909 builtin_type->xmethod
4910 = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
4912 return builtin_type;
4915 /* This set of objfile-based types is intended to be used by symbol
4916 readers as basic types. */
4918 static const struct objfile_data *objfile_type_data;
4920 const struct objfile_type *
4921 objfile_type (struct objfile *objfile)
4923 struct gdbarch *gdbarch;
4924 struct objfile_type *objfile_type
4925 = objfile_data (objfile, objfile_type_data);
4928 return objfile_type;
4930 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4931 1, struct objfile_type);
4933 /* Use the objfile architecture to determine basic type properties. */
4934 gdbarch = get_objfile_arch (objfile);
4937 objfile_type->builtin_void
4938 = init_type (TYPE_CODE_VOID, 1,
4942 objfile_type->builtin_char
4943 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4945 | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4947 objfile_type->builtin_signed_char
4948 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4950 "signed char", objfile);
4951 objfile_type->builtin_unsigned_char
4952 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4954 "unsigned char", objfile);
4955 objfile_type->builtin_short
4956 = init_type (TYPE_CODE_INT,
4957 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4958 0, "short", objfile);
4959 objfile_type->builtin_unsigned_short
4960 = init_type (TYPE_CODE_INT,
4961 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4962 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4963 objfile_type->builtin_int
4964 = init_type (TYPE_CODE_INT,
4965 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4967 objfile_type->builtin_unsigned_int
4968 = init_type (TYPE_CODE_INT,
4969 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4970 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4971 objfile_type->builtin_long
4972 = init_type (TYPE_CODE_INT,
4973 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4974 0, "long", objfile);
4975 objfile_type->builtin_unsigned_long
4976 = init_type (TYPE_CODE_INT,
4977 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4978 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4979 objfile_type->builtin_long_long
4980 = init_type (TYPE_CODE_INT,
4981 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4982 0, "long long", objfile);
4983 objfile_type->builtin_unsigned_long_long
4984 = init_type (TYPE_CODE_INT,
4985 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4986 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4988 objfile_type->builtin_float
4989 = init_type (TYPE_CODE_FLT,
4990 gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4991 0, "float", objfile);
4992 TYPE_FLOATFORMAT (objfile_type->builtin_float)
4993 = gdbarch_float_format (gdbarch);
4994 objfile_type->builtin_double
4995 = init_type (TYPE_CODE_FLT,
4996 gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4997 0, "double", objfile);
4998 TYPE_FLOATFORMAT (objfile_type->builtin_double)
4999 = gdbarch_double_format (gdbarch);
5000 objfile_type->builtin_long_double
5001 = init_type (TYPE_CODE_FLT,
5002 gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
5003 0, "long double", objfile);
5004 TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
5005 = gdbarch_long_double_format (gdbarch);
5007 /* This type represents a type that was unrecognized in symbol read-in. */
5008 objfile_type->builtin_error
5009 = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
5011 /* The following set of types is used for symbols with no
5012 debug information. */
5013 objfile_type->nodebug_text_symbol
5014 = init_type (TYPE_CODE_FUNC, 1, 0,
5015 "<text variable, no debug info>", objfile);
5016 TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
5017 = objfile_type->builtin_int;
5018 objfile_type->nodebug_text_gnu_ifunc_symbol
5019 = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
5020 "<text gnu-indirect-function variable, no debug info>",
5022 TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
5023 = objfile_type->nodebug_text_symbol;
5024 objfile_type->nodebug_got_plt_symbol
5025 = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
5026 "<text from jump slot in .got.plt, no debug info>",
5028 TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
5029 = objfile_type->nodebug_text_symbol;
5030 objfile_type->nodebug_data_symbol
5031 = init_type (TYPE_CODE_INT,
5032 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
5033 "<data variable, no debug info>", objfile);
5034 objfile_type->nodebug_unknown_symbol
5035 = init_type (TYPE_CODE_INT, 1, 0,
5036 "<variable (not text or data), no debug info>", objfile);
5037 objfile_type->nodebug_tls_symbol
5038 = init_type (TYPE_CODE_INT,
5039 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
5040 "<thread local variable, no debug info>", objfile);
5042 /* NOTE: on some targets, addresses and pointers are not necessarily
5046 - gdb's `struct type' always describes the target's
5048 - gdb's `struct value' objects should always hold values in
5050 - gdb's CORE_ADDR values are addresses in the unified virtual
5051 address space that the assembler and linker work with. Thus,
5052 since target_read_memory takes a CORE_ADDR as an argument, it
5053 can access any memory on the target, even if the processor has
5054 separate code and data address spaces.
5056 In this context, objfile_type->builtin_core_addr is a bit odd:
5057 it's a target type for a value the target will never see. It's
5058 only used to hold the values of (typeless) linker symbols, which
5059 are indeed in the unified virtual address space. */
5061 objfile_type->builtin_core_addr
5062 = init_type (TYPE_CODE_INT,
5063 gdbarch_addr_bit (gdbarch) / 8,
5064 TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
5066 set_objfile_data (objfile, objfile_type_data, objfile_type);
5067 return objfile_type;
5070 extern initialize_file_ftype _initialize_gdbtypes;
5073 _initialize_gdbtypes (void)
5075 gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
5076 objfile_type_data = register_objfile_data ();
5078 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
5079 _("Set debugging of C++ overloading."),
5080 _("Show debugging of C++ overloading."),
5081 _("When enabled, ranking of the "
5082 "functions is displayed."),
5084 show_overload_debug,
5085 &setdebuglist, &showdebuglist);
5087 /* Add user knob for controlling resolution of opaque types. */
5088 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
5089 &opaque_type_resolution,
5090 _("Set resolution of opaque struct/class/union"
5091 " types (if set before loading symbols)."),
5092 _("Show resolution of opaque struct/class/union"
5093 " types (if set before loading symbols)."),
5095 show_opaque_type_resolution,
5096 &setlist, &showlist);
5098 /* Add an option to permit non-strict type checking. */
5099 add_setshow_boolean_cmd ("type", class_support,
5100 &strict_type_checking,
5101 _("Set strict type checking."),
5102 _("Show strict type checking."),
5104 show_strict_type_checking,
5105 &setchecklist, &showchecklist);