1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2014 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/>. */
29 #include "expression.h"
34 #include "complaints.h"
37 #include "gdb_assert.h"
39 #include "exceptions.h"
40 #include "cp-support.h"
42 #include "dwarf2loc.h"
44 /* Initialize BADNESS constants. */
46 const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
48 const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
49 const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
51 const struct rank EXACT_MATCH_BADNESS = {0,0};
53 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
54 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
55 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
56 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
57 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
58 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
59 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
60 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
61 const struct rank BASE_CONVERSION_BADNESS = {2,0};
62 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
63 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
64 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
65 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
67 /* Floatformat pairs. */
68 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
69 &floatformat_ieee_half_big,
70 &floatformat_ieee_half_little
72 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
73 &floatformat_ieee_single_big,
74 &floatformat_ieee_single_little
76 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
77 &floatformat_ieee_double_big,
78 &floatformat_ieee_double_little
80 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
81 &floatformat_ieee_double_big,
82 &floatformat_ieee_double_littlebyte_bigword
84 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
85 &floatformat_i387_ext,
88 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
89 &floatformat_m68881_ext,
90 &floatformat_m68881_ext
92 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
93 &floatformat_arm_ext_big,
94 &floatformat_arm_ext_littlebyte_bigword
96 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
97 &floatformat_ia64_spill_big,
98 &floatformat_ia64_spill_little
100 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
101 &floatformat_ia64_quad_big,
102 &floatformat_ia64_quad_little
104 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
108 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
112 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
113 &floatformat_ibm_long_double_big,
114 &floatformat_ibm_long_double_little
117 /* Should opaque types be resolved? */
119 static int opaque_type_resolution = 1;
121 /* A flag to enable printing of debugging information of C++
124 unsigned int overload_debug = 0;
126 /* A flag to enable strict type checking. */
128 static int strict_type_checking = 1;
130 /* A function to show whether opaque types are resolved. */
133 show_opaque_type_resolution (struct ui_file *file, int from_tty,
134 struct cmd_list_element *c,
137 fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
138 "(if set before loading symbols) is %s.\n"),
142 /* A function to show whether C++ overload debugging is enabled. */
145 show_overload_debug (struct ui_file *file, int from_tty,
146 struct cmd_list_element *c, const char *value)
148 fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
152 /* A function to show the status of strict type checking. */
155 show_strict_type_checking (struct ui_file *file, int from_tty,
156 struct cmd_list_element *c, const char *value)
158 fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
162 /* Allocate a new OBJFILE-associated type structure and fill it
163 with some defaults. Space for the type structure is allocated
164 on the objfile's objfile_obstack. */
167 alloc_type (struct objfile *objfile)
171 gdb_assert (objfile != NULL);
173 /* Alloc the structure and start off with all fields zeroed. */
174 type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
175 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
177 OBJSTAT (objfile, n_types++);
179 TYPE_OBJFILE_OWNED (type) = 1;
180 TYPE_OWNER (type).objfile = objfile;
182 /* Initialize the fields that might not be zero. */
184 TYPE_CODE (type) = TYPE_CODE_UNDEF;
185 TYPE_VPTR_FIELDNO (type) = -1;
186 TYPE_CHAIN (type) = type; /* Chain back to itself. */
191 /* Allocate a new GDBARCH-associated type structure and fill it
192 with some defaults. Space for the type structure is allocated
196 alloc_type_arch (struct gdbarch *gdbarch)
200 gdb_assert (gdbarch != NULL);
202 /* Alloc the structure and start off with all fields zeroed. */
204 type = XCNEW (struct type);
205 TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
207 TYPE_OBJFILE_OWNED (type) = 0;
208 TYPE_OWNER (type).gdbarch = gdbarch;
210 /* Initialize the fields that might not be zero. */
212 TYPE_CODE (type) = TYPE_CODE_UNDEF;
213 TYPE_VPTR_FIELDNO (type) = -1;
214 TYPE_CHAIN (type) = type; /* Chain back to itself. */
219 /* If TYPE is objfile-associated, allocate a new type structure
220 associated with the same objfile. If TYPE is gdbarch-associated,
221 allocate a new type structure associated with the same gdbarch. */
224 alloc_type_copy (const struct type *type)
226 if (TYPE_OBJFILE_OWNED (type))
227 return alloc_type (TYPE_OWNER (type).objfile);
229 return alloc_type_arch (TYPE_OWNER (type).gdbarch);
232 /* If TYPE is gdbarch-associated, return that architecture.
233 If TYPE is objfile-associated, return that objfile's architecture. */
236 get_type_arch (const struct type *type)
238 if (TYPE_OBJFILE_OWNED (type))
239 return get_objfile_arch (TYPE_OWNER (type).objfile);
241 return TYPE_OWNER (type).gdbarch;
244 /* See gdbtypes.h. */
247 get_target_type (struct type *type)
251 type = TYPE_TARGET_TYPE (type);
253 type = check_typedef (type);
259 /* Alloc a new type instance structure, fill it with some defaults,
260 and point it at OLDTYPE. Allocate the new type instance from the
261 same place as OLDTYPE. */
264 alloc_type_instance (struct type *oldtype)
268 /* Allocate the structure. */
270 if (! TYPE_OBJFILE_OWNED (oldtype))
271 type = XCNEW (struct type);
273 type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
276 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
278 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
283 /* Clear all remnants of the previous type at TYPE, in preparation for
284 replacing it with something else. Preserve owner information. */
287 smash_type (struct type *type)
289 int objfile_owned = TYPE_OBJFILE_OWNED (type);
290 union type_owner owner = TYPE_OWNER (type);
292 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
294 /* Restore owner information. */
295 TYPE_OBJFILE_OWNED (type) = objfile_owned;
296 TYPE_OWNER (type) = owner;
298 /* For now, delete the rings. */
299 TYPE_CHAIN (type) = type;
301 /* For now, leave the pointer/reference types alone. */
304 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
305 to a pointer to memory where the pointer type should be stored.
306 If *TYPEPTR is zero, update it to point to the pointer type we return.
307 We allocate new memory if needed. */
310 make_pointer_type (struct type *type, struct type **typeptr)
312 struct type *ntype; /* New type */
315 ntype = TYPE_POINTER_TYPE (type);
320 return ntype; /* Don't care about alloc,
321 and have new type. */
322 else if (*typeptr == 0)
324 *typeptr = ntype; /* Tracking alloc, and have new type. */
329 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
331 ntype = alloc_type_copy (type);
335 else /* We have storage, but need to reset it. */
338 chain = TYPE_CHAIN (ntype);
340 TYPE_CHAIN (ntype) = chain;
343 TYPE_TARGET_TYPE (ntype) = type;
344 TYPE_POINTER_TYPE (type) = ntype;
346 /* FIXME! Assumes the machine has only one representation for pointers! */
349 = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
350 TYPE_CODE (ntype) = TYPE_CODE_PTR;
352 /* Mark pointers as unsigned. The target converts between pointers
353 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
354 gdbarch_address_to_pointer. */
355 TYPE_UNSIGNED (ntype) = 1;
357 /* Update the length of all the other variants of this type. */
358 chain = TYPE_CHAIN (ntype);
359 while (chain != ntype)
361 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
362 chain = TYPE_CHAIN (chain);
368 /* Given a type TYPE, return a type of pointers to that type.
369 May need to construct such a type if this is the first use. */
372 lookup_pointer_type (struct type *type)
374 return make_pointer_type (type, (struct type **) 0);
377 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
378 points to a pointer to memory where the reference type should be
379 stored. If *TYPEPTR is zero, update it to point to the reference
380 type we return. We allocate new memory if needed. */
383 make_reference_type (struct type *type, struct type **typeptr)
385 struct type *ntype; /* New type */
388 ntype = TYPE_REFERENCE_TYPE (type);
393 return ntype; /* Don't care about alloc,
394 and have new type. */
395 else if (*typeptr == 0)
397 *typeptr = ntype; /* Tracking alloc, and have new type. */
402 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
404 ntype = alloc_type_copy (type);
408 else /* We have storage, but need to reset it. */
411 chain = TYPE_CHAIN (ntype);
413 TYPE_CHAIN (ntype) = chain;
416 TYPE_TARGET_TYPE (ntype) = type;
417 TYPE_REFERENCE_TYPE (type) = ntype;
419 /* FIXME! Assume the machine has only one representation for
420 references, and that it matches the (only) representation for
423 TYPE_LENGTH (ntype) =
424 gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
425 TYPE_CODE (ntype) = TYPE_CODE_REF;
427 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
428 TYPE_REFERENCE_TYPE (type) = ntype;
430 /* Update the length of all the other variants of this type. */
431 chain = TYPE_CHAIN (ntype);
432 while (chain != ntype)
434 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
435 chain = TYPE_CHAIN (chain);
441 /* Same as above, but caller doesn't care about memory allocation
445 lookup_reference_type (struct type *type)
447 return make_reference_type (type, (struct type **) 0);
450 /* Lookup a function type that returns type TYPE. TYPEPTR, if
451 nonzero, points to a pointer to memory where the function type
452 should be stored. If *TYPEPTR is zero, update it to point to the
453 function type we return. We allocate new memory if needed. */
456 make_function_type (struct type *type, struct type **typeptr)
458 struct type *ntype; /* New type */
460 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
462 ntype = alloc_type_copy (type);
466 else /* We have storage, but need to reset it. */
472 TYPE_TARGET_TYPE (ntype) = type;
474 TYPE_LENGTH (ntype) = 1;
475 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
477 INIT_FUNC_SPECIFIC (ntype);
482 /* Given a type TYPE, return a type of functions that return that type.
483 May need to construct such a type if this is the first use. */
486 lookup_function_type (struct type *type)
488 return make_function_type (type, (struct type **) 0);
491 /* Given a type TYPE and argument types, return the appropriate
492 function type. If the final type in PARAM_TYPES is NULL, make a
496 lookup_function_type_with_arguments (struct type *type,
498 struct type **param_types)
500 struct type *fn = make_function_type (type, (struct type **) 0);
505 if (param_types[nparams - 1] == NULL)
508 TYPE_VARARGS (fn) = 1;
510 else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
514 /* Caller should have ensured this. */
515 gdb_assert (nparams == 0);
516 TYPE_PROTOTYPED (fn) = 1;
520 TYPE_NFIELDS (fn) = nparams;
521 TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
522 for (i = 0; i < nparams; ++i)
523 TYPE_FIELD_TYPE (fn, i) = param_types[i];
528 /* Identify address space identifier by name --
529 return the integer flag defined in gdbtypes.h. */
532 address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
536 /* Check for known address space delimiters. */
537 if (!strcmp (space_identifier, "code"))
538 return TYPE_INSTANCE_FLAG_CODE_SPACE;
539 else if (!strcmp (space_identifier, "data"))
540 return TYPE_INSTANCE_FLAG_DATA_SPACE;
541 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
542 && gdbarch_address_class_name_to_type_flags (gdbarch,
547 error (_("Unknown address space specifier: \"%s\""), space_identifier);
550 /* Identify address space identifier by integer flag as defined in
551 gdbtypes.h -- return the string version of the adress space name. */
554 address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
556 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
558 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
560 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
561 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
562 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
567 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
569 If STORAGE is non-NULL, create the new type instance there.
570 STORAGE must be in the same obstack as TYPE. */
573 make_qualified_type (struct type *type, int new_flags,
574 struct type *storage)
581 if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
583 ntype = TYPE_CHAIN (ntype);
585 while (ntype != type);
587 /* Create a new type instance. */
589 ntype = alloc_type_instance (type);
592 /* If STORAGE was provided, it had better be in the same objfile
593 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
594 if one objfile is freed and the other kept, we'd have
595 dangling pointers. */
596 gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
599 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
600 TYPE_CHAIN (ntype) = ntype;
603 /* Pointers or references to the original type are not relevant to
605 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
606 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
608 /* Chain the new qualified type to the old type. */
609 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
610 TYPE_CHAIN (type) = ntype;
612 /* Now set the instance flags and return the new type. */
613 TYPE_INSTANCE_FLAGS (ntype) = new_flags;
615 /* Set length of new type to that of the original type. */
616 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
621 /* Make an address-space-delimited variant of a type -- a type that
622 is identical to the one supplied except that it has an address
623 space attribute attached to it (such as "code" or "data").
625 The space attributes "code" and "data" are for Harvard
626 architectures. The address space attributes are for architectures
627 which have alternately sized pointers or pointers with alternate
631 make_type_with_address_space (struct type *type, int space_flag)
633 int new_flags = ((TYPE_INSTANCE_FLAGS (type)
634 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
635 | TYPE_INSTANCE_FLAG_DATA_SPACE
636 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
639 return make_qualified_type (type, new_flags, NULL);
642 /* Make a "c-v" variant of a type -- a type that is identical to the
643 one supplied except that it may have const or volatile attributes
644 CNST is a flag for setting the const attribute
645 VOLTL is a flag for setting the volatile attribute
646 TYPE is the base type whose variant we are creating.
648 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
649 storage to hold the new qualified type; *TYPEPTR and TYPE must be
650 in the same objfile. Otherwise, allocate fresh memory for the new
651 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
652 new type we construct. */
655 make_cv_type (int cnst, int voltl,
657 struct type **typeptr)
659 struct type *ntype; /* New type */
661 int new_flags = (TYPE_INSTANCE_FLAGS (type)
662 & ~(TYPE_INSTANCE_FLAG_CONST
663 | TYPE_INSTANCE_FLAG_VOLATILE));
666 new_flags |= TYPE_INSTANCE_FLAG_CONST;
669 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
671 if (typeptr && *typeptr != NULL)
673 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
674 a C-V variant chain that threads across objfiles: if one
675 objfile gets freed, then the other has a broken C-V chain.
677 This code used to try to copy over the main type from TYPE to
678 *TYPEPTR if they were in different objfiles, but that's
679 wrong, too: TYPE may have a field list or member function
680 lists, which refer to types of their own, etc. etc. The
681 whole shebang would need to be copied over recursively; you
682 can't have inter-objfile pointers. The only thing to do is
683 to leave stub types as stub types, and look them up afresh by
684 name each time you encounter them. */
685 gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
688 ntype = make_qualified_type (type, new_flags,
689 typeptr ? *typeptr : NULL);
697 /* Make a 'restrict'-qualified version of TYPE. */
700 make_restrict_type (struct type *type)
702 return make_qualified_type (type,
703 (TYPE_INSTANCE_FLAGS (type)
704 | TYPE_INSTANCE_FLAG_RESTRICT),
708 /* Replace the contents of ntype with the type *type. This changes the
709 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
710 the changes are propogated to all types in the TYPE_CHAIN.
712 In order to build recursive types, it's inevitable that we'll need
713 to update types in place --- but this sort of indiscriminate
714 smashing is ugly, and needs to be replaced with something more
715 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
716 clear if more steps are needed. */
719 replace_type (struct type *ntype, struct type *type)
723 /* These two types had better be in the same objfile. Otherwise,
724 the assignment of one type's main type structure to the other
725 will produce a type with references to objects (names; field
726 lists; etc.) allocated on an objfile other than its own. */
727 gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
729 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
731 /* The type length is not a part of the main type. Update it for
732 each type on the variant chain. */
736 /* Assert that this element of the chain has no address-class bits
737 set in its flags. Such type variants might have type lengths
738 which are supposed to be different from the non-address-class
739 variants. This assertion shouldn't ever be triggered because
740 symbol readers which do construct address-class variants don't
741 call replace_type(). */
742 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
744 TYPE_LENGTH (chain) = TYPE_LENGTH (type);
745 chain = TYPE_CHAIN (chain);
747 while (ntype != chain);
749 /* Assert that the two types have equivalent instance qualifiers.
750 This should be true for at least all of our debug readers. */
751 gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
754 /* Implement direct support for MEMBER_TYPE in GNU C++.
755 May need to construct such a type if this is the first use.
756 The TYPE is the type of the member. The DOMAIN is the type
757 of the aggregate that the member belongs to. */
760 lookup_memberptr_type (struct type *type, struct type *domain)
764 mtype = alloc_type_copy (type);
765 smash_to_memberptr_type (mtype, domain, type);
769 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
772 lookup_methodptr_type (struct type *to_type)
776 mtype = alloc_type_copy (to_type);
777 smash_to_methodptr_type (mtype, to_type);
781 /* Allocate a stub method whose return type is TYPE. This apparently
782 happens for speed of symbol reading, since parsing out the
783 arguments to the method is cpu-intensive, the way we are doing it.
784 So, we will fill in arguments later. This always returns a fresh
788 allocate_stub_method (struct type *type)
792 mtype = alloc_type_copy (type);
793 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
794 TYPE_LENGTH (mtype) = 1;
795 TYPE_STUB (mtype) = 1;
796 TYPE_TARGET_TYPE (mtype) = type;
797 /* _DOMAIN_TYPE (mtype) = unknown yet */
801 /* Create a range type using either a blank type supplied in
802 RESULT_TYPE, or creating a new type, inheriting the objfile from
805 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
806 to HIGH_BOUND, inclusive.
808 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
809 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
812 create_range_type (struct type *result_type, struct type *index_type,
813 LONGEST low_bound, LONGEST high_bound)
815 if (result_type == NULL)
816 result_type = alloc_type_copy (index_type);
817 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
818 TYPE_TARGET_TYPE (result_type) = index_type;
819 if (TYPE_STUB (index_type))
820 TYPE_TARGET_STUB (result_type) = 1;
822 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
823 TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
824 TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
825 TYPE_LOW_BOUND (result_type) = low_bound;
826 TYPE_HIGH_BOUND (result_type) = high_bound;
829 TYPE_UNSIGNED (result_type) = 1;
834 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
835 TYPE. Return 1 if type is a range type, 0 if it is discrete (and
836 bounds will fit in LONGEST), or -1 otherwise. */
839 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
841 CHECK_TYPEDEF (type);
842 switch (TYPE_CODE (type))
844 case TYPE_CODE_RANGE:
845 *lowp = TYPE_LOW_BOUND (type);
846 *highp = TYPE_HIGH_BOUND (type);
849 if (TYPE_NFIELDS (type) > 0)
851 /* The enums may not be sorted by value, so search all
855 *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
856 for (i = 0; i < TYPE_NFIELDS (type); i++)
858 if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
859 *lowp = TYPE_FIELD_ENUMVAL (type, i);
860 if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
861 *highp = TYPE_FIELD_ENUMVAL (type, i);
864 /* Set unsigned indicator if warranted. */
867 TYPE_UNSIGNED (type) = 1;
881 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
883 if (!TYPE_UNSIGNED (type))
885 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
889 /* ... fall through for unsigned ints ... */
892 /* This round-about calculation is to avoid shifting by
893 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
894 if TYPE_LENGTH (type) == sizeof (LONGEST). */
895 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
896 *highp = (*highp - 1) | *highp;
903 /* Assuming TYPE is a simple, non-empty array type, compute its upper
904 and lower bound. Save the low bound into LOW_BOUND if not NULL.
905 Save the high bound into HIGH_BOUND if not NULL.
907 Return 1 if the operation was successful. Return zero otherwise,
908 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
910 We now simply use get_discrete_bounds call to get the values
911 of the low and high bounds.
912 get_discrete_bounds can return three values:
913 1, meaning that index is a range,
914 0, meaning that index is a discrete type,
915 or -1 for failure. */
918 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
920 struct type *index = TYPE_INDEX_TYPE (type);
928 res = get_discrete_bounds (index, &low, &high);
932 /* Check if the array bounds are undefined. */
934 && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
935 || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
947 /* Create an array type using either a blank type supplied in
948 RESULT_TYPE, or creating a new type, inheriting the objfile from
951 Elements will be of type ELEMENT_TYPE, the indices will be of type
954 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
955 sure it is TYPE_CODE_UNDEF before we bash it into an array
959 create_array_type (struct type *result_type,
960 struct type *element_type,
961 struct type *range_type)
963 LONGEST low_bound, high_bound;
965 if (result_type == NULL)
966 result_type = alloc_type_copy (range_type);
968 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
969 TYPE_TARGET_TYPE (result_type) = element_type;
970 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
971 low_bound = high_bound = 0;
972 CHECK_TYPEDEF (element_type);
973 /* Be careful when setting the array length. Ada arrays can be
974 empty arrays with the high_bound being smaller than the low_bound.
975 In such cases, the array length should be zero. */
976 if (high_bound < low_bound)
977 TYPE_LENGTH (result_type) = 0;
979 TYPE_LENGTH (result_type) =
980 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
981 TYPE_NFIELDS (result_type) = 1;
982 TYPE_FIELDS (result_type) =
983 (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
984 TYPE_INDEX_TYPE (result_type) = range_type;
985 TYPE_VPTR_FIELDNO (result_type) = -1;
987 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays. */
988 if (TYPE_LENGTH (result_type) == 0)
989 TYPE_TARGET_STUB (result_type) = 1;
995 lookup_array_range_type (struct type *element_type,
996 LONGEST low_bound, LONGEST high_bound)
998 struct gdbarch *gdbarch = get_type_arch (element_type);
999 struct type *index_type = builtin_type (gdbarch)->builtin_int;
1000 struct type *range_type
1001 = create_range_type (NULL, index_type, low_bound, high_bound);
1003 return create_array_type (NULL, element_type, range_type);
1006 /* Create a string type using either a blank type supplied in
1007 RESULT_TYPE, or creating a new type. String types are similar
1008 enough to array of char types that we can use create_array_type to
1009 build the basic type and then bash it into a string type.
1011 For fixed length strings, the range type contains 0 as the lower
1012 bound and the length of the string minus one as the upper bound.
1014 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1015 sure it is TYPE_CODE_UNDEF before we bash it into a string
1019 create_string_type (struct type *result_type,
1020 struct type *string_char_type,
1021 struct type *range_type)
1023 result_type = create_array_type (result_type,
1026 TYPE_CODE (result_type) = TYPE_CODE_STRING;
1031 lookup_string_range_type (struct type *string_char_type,
1032 LONGEST low_bound, LONGEST high_bound)
1034 struct type *result_type;
1036 result_type = lookup_array_range_type (string_char_type,
1037 low_bound, high_bound);
1038 TYPE_CODE (result_type) = TYPE_CODE_STRING;
1043 create_set_type (struct type *result_type, struct type *domain_type)
1045 if (result_type == NULL)
1046 result_type = alloc_type_copy (domain_type);
1048 TYPE_CODE (result_type) = TYPE_CODE_SET;
1049 TYPE_NFIELDS (result_type) = 1;
1050 TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
1052 if (!TYPE_STUB (domain_type))
1054 LONGEST low_bound, high_bound, bit_length;
1056 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
1057 low_bound = high_bound = 0;
1058 bit_length = high_bound - low_bound + 1;
1059 TYPE_LENGTH (result_type)
1060 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1062 TYPE_UNSIGNED (result_type) = 1;
1064 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
1069 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1070 and any array types nested inside it. */
1073 make_vector_type (struct type *array_type)
1075 struct type *inner_array, *elt_type;
1078 /* Find the innermost array type, in case the array is
1079 multi-dimensional. */
1080 inner_array = array_type;
1081 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
1082 inner_array = TYPE_TARGET_TYPE (inner_array);
1084 elt_type = TYPE_TARGET_TYPE (inner_array);
1085 if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
1087 flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
1088 elt_type = make_qualified_type (elt_type, flags, NULL);
1089 TYPE_TARGET_TYPE (inner_array) = elt_type;
1092 TYPE_VECTOR (array_type) = 1;
1096 init_vector_type (struct type *elt_type, int n)
1098 struct type *array_type;
1100 array_type = lookup_array_range_type (elt_type, 0, n - 1);
1101 make_vector_type (array_type);
1105 /* Smash TYPE to be a type of pointers to members of DOMAIN with type
1106 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1107 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1108 TYPE doesn't include the offset (that's the value of the MEMBER
1109 itself), but does include the structure type into which it points
1112 When "smashing" the type, we preserve the objfile that the old type
1113 pointed to, since we aren't changing where the type is actually
1117 smash_to_memberptr_type (struct type *type, struct type *domain,
1118 struct type *to_type)
1121 TYPE_TARGET_TYPE (type) = to_type;
1122 TYPE_DOMAIN_TYPE (type) = domain;
1123 /* Assume that a data member pointer is the same size as a normal
1126 = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
1127 TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
1130 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1132 When "smashing" the type, we preserve the objfile that the old type
1133 pointed to, since we aren't changing where the type is actually
1137 smash_to_methodptr_type (struct type *type, struct type *to_type)
1140 TYPE_TARGET_TYPE (type) = to_type;
1141 TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1142 TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1143 TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1146 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
1147 METHOD just means `function that gets an extra "this" argument'.
1149 When "smashing" the type, we preserve the objfile that the old type
1150 pointed to, since we aren't changing where the type is actually
1154 smash_to_method_type (struct type *type, struct type *domain,
1155 struct type *to_type, struct field *args,
1156 int nargs, int varargs)
1159 TYPE_TARGET_TYPE (type) = to_type;
1160 TYPE_DOMAIN_TYPE (type) = domain;
1161 TYPE_FIELDS (type) = args;
1162 TYPE_NFIELDS (type) = nargs;
1164 TYPE_VARARGS (type) = 1;
1165 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
1166 TYPE_CODE (type) = TYPE_CODE_METHOD;
1169 /* Return a typename for a struct/union/enum type without "struct ",
1170 "union ", or "enum ". If the type has a NULL name, return NULL. */
1173 type_name_no_tag (const struct type *type)
1175 if (TYPE_TAG_NAME (type) != NULL)
1176 return TYPE_TAG_NAME (type);
1178 /* Is there code which expects this to return the name if there is
1179 no tag name? My guess is that this is mainly used for C++ in
1180 cases where the two will always be the same. */
1181 return TYPE_NAME (type);
1184 /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1185 Since GCC PR debug/47510 DWARF provides associated information to detect the
1186 anonymous class linkage name from its typedef.
1188 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1192 type_name_no_tag_or_error (struct type *type)
1194 struct type *saved_type = type;
1196 struct objfile *objfile;
1198 CHECK_TYPEDEF (type);
1200 name = type_name_no_tag (type);
1204 name = type_name_no_tag (saved_type);
1205 objfile = TYPE_OBJFILE (saved_type);
1206 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1207 name ? name : "<anonymous>",
1208 objfile ? objfile_name (objfile) : "<arch>");
1211 /* Lookup a typedef or primitive type named NAME, visible in lexical
1212 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1213 suitably defined. */
1216 lookup_typename (const struct language_defn *language,
1217 struct gdbarch *gdbarch, const char *name,
1218 const struct block *block, int noerr)
1223 sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
1224 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1225 return SYMBOL_TYPE (sym);
1227 type = language_lookup_primitive_type_by_name (language, gdbarch, name);
1233 error (_("No type named %s."), name);
1237 lookup_unsigned_typename (const struct language_defn *language,
1238 struct gdbarch *gdbarch, const char *name)
1240 char *uns = alloca (strlen (name) + 10);
1242 strcpy (uns, "unsigned ");
1243 strcpy (uns + 9, name);
1244 return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
1248 lookup_signed_typename (const struct language_defn *language,
1249 struct gdbarch *gdbarch, const char *name)
1252 char *uns = alloca (strlen (name) + 8);
1254 strcpy (uns, "signed ");
1255 strcpy (uns + 7, name);
1256 t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
1257 /* If we don't find "signed FOO" just try again with plain "FOO". */
1260 return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
1263 /* Lookup a structure type named "struct NAME",
1264 visible in lexical block BLOCK. */
1267 lookup_struct (const char *name, const struct block *block)
1271 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1275 error (_("No struct type named %s."), name);
1277 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1279 error (_("This context has class, union or enum %s, not a struct."),
1282 return (SYMBOL_TYPE (sym));
1285 /* Lookup a union type named "union NAME",
1286 visible in lexical block BLOCK. */
1289 lookup_union (const char *name, const struct block *block)
1294 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1297 error (_("No union type named %s."), name);
1299 t = SYMBOL_TYPE (sym);
1301 if (TYPE_CODE (t) == TYPE_CODE_UNION)
1304 /* If we get here, it's not a union. */
1305 error (_("This context has class, struct or enum %s, not a union."),
1309 /* Lookup an enum type named "enum NAME",
1310 visible in lexical block BLOCK. */
1313 lookup_enum (const char *name, const struct block *block)
1317 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1320 error (_("No enum type named %s."), name);
1322 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1324 error (_("This context has class, struct or union %s, not an enum."),
1327 return (SYMBOL_TYPE (sym));
1330 /* Lookup a template type named "template NAME<TYPE>",
1331 visible in lexical block BLOCK. */
1334 lookup_template_type (char *name, struct type *type,
1335 const struct block *block)
1338 char *nam = (char *)
1339 alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1343 strcat (nam, TYPE_NAME (type));
1344 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
1346 sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
1350 error (_("No template type named %s."), name);
1352 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1354 error (_("This context has class, union or enum %s, not a struct."),
1357 return (SYMBOL_TYPE (sym));
1360 /* Given a type TYPE, lookup the type of the component of type named
1363 TYPE can be either a struct or union, or a pointer or reference to
1364 a struct or union. If it is a pointer or reference, its target
1365 type is automatically used. Thus '.' and '->' are interchangable,
1366 as specified for the definitions of the expression element types
1367 STRUCTOP_STRUCT and STRUCTOP_PTR.
1369 If NOERR is nonzero, return zero if NAME is not suitably defined.
1370 If NAME is the name of a baseclass type, return that type. */
1373 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1380 CHECK_TYPEDEF (type);
1381 if (TYPE_CODE (type) != TYPE_CODE_PTR
1382 && TYPE_CODE (type) != TYPE_CODE_REF)
1384 type = TYPE_TARGET_TYPE (type);
1387 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1388 && TYPE_CODE (type) != TYPE_CODE_UNION)
1390 typename = type_to_string (type);
1391 make_cleanup (xfree, typename);
1392 error (_("Type %s is not a structure or union type."), typename);
1396 /* FIXME: This change put in by Michael seems incorrect for the case
1397 where the structure tag name is the same as the member name.
1398 I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
1399 foo; } bell;" Disabled by fnf. */
1403 typename = type_name_no_tag (type);
1404 if (typename != NULL && strcmp (typename, name) == 0)
1409 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1411 const char *t_field_name = TYPE_FIELD_NAME (type, i);
1413 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1415 return TYPE_FIELD_TYPE (type, i);
1417 else if (!t_field_name || *t_field_name == '\0')
1419 struct type *subtype
1420 = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1422 if (subtype != NULL)
1427 /* OK, it's not in this class. Recursively check the baseclasses. */
1428 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1432 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
1444 typename = type_to_string (type);
1445 make_cleanup (xfree, typename);
1446 error (_("Type %s has no component named %s."), typename, name);
1449 /* Lookup the vptr basetype/fieldno values for TYPE.
1450 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1451 vptr_fieldno. Also, if found and basetype is from the same objfile,
1453 If not found, return -1 and ignore BASETYPEP.
1454 Callers should be aware that in some cases (for example,
1455 the type or one of its baseclasses is a stub type and we are
1456 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1457 this function will not be able to find the
1458 virtual function table pointer, and vptr_fieldno will remain -1 and
1459 vptr_basetype will remain NULL or incomplete. */
1462 get_vptr_fieldno (struct type *type, struct type **basetypep)
1464 CHECK_TYPEDEF (type);
1466 if (TYPE_VPTR_FIELDNO (type) < 0)
1470 /* We must start at zero in case the first (and only) baseclass
1471 is virtual (and hence we cannot share the table pointer). */
1472 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1474 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1476 struct type *basetype;
1478 fieldno = get_vptr_fieldno (baseclass, &basetype);
1481 /* If the type comes from a different objfile we can't cache
1482 it, it may have a different lifetime. PR 2384 */
1483 if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
1485 TYPE_VPTR_FIELDNO (type) = fieldno;
1486 TYPE_VPTR_BASETYPE (type) = basetype;
1489 *basetypep = basetype;
1500 *basetypep = TYPE_VPTR_BASETYPE (type);
1501 return TYPE_VPTR_FIELDNO (type);
1506 stub_noname_complaint (void)
1508 complaint (&symfile_complaints, _("stub type has NULL name"));
1511 /* Find the real type of TYPE. This function returns the real type,
1512 after removing all layers of typedefs, and completing opaque or stub
1513 types. Completion changes the TYPE argument, but stripping of
1516 Instance flags (e.g. const/volatile) are preserved as typedefs are
1517 stripped. If necessary a new qualified form of the underlying type
1520 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1521 not been computed and we're either in the middle of reading symbols, or
1522 there was no name for the typedef in the debug info.
1524 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1525 QUITs in the symbol reading code can also throw.
1526 Thus this function can throw an exception.
1528 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1531 If this is a stubbed struct (i.e. declared as struct foo *), see if
1532 we can find a full definition in some other file. If so, copy this
1533 definition, so we can use it in future. There used to be a comment
1534 (but not any code) that if we don't find a full definition, we'd
1535 set a flag so we don't spend time in the future checking the same
1536 type. That would be a mistake, though--we might load in more
1537 symbols which contain a full definition for the type. */
1540 check_typedef (struct type *type)
1542 struct type *orig_type = type;
1543 /* While we're removing typedefs, we don't want to lose qualifiers.
1544 E.g., const/volatile. */
1545 int instance_flags = TYPE_INSTANCE_FLAGS (type);
1549 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1551 if (!TYPE_TARGET_TYPE (type))
1556 /* It is dangerous to call lookup_symbol if we are currently
1557 reading a symtab. Infinite recursion is one danger. */
1558 if (currently_reading_symtab)
1559 return make_qualified_type (type, instance_flags, NULL);
1561 name = type_name_no_tag (type);
1562 /* FIXME: shouldn't we separately check the TYPE_NAME and
1563 the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1564 VAR_DOMAIN as appropriate? (this code was written before
1565 TYPE_NAME and TYPE_TAG_NAME were separate). */
1568 stub_noname_complaint ();
1569 return make_qualified_type (type, instance_flags, NULL);
1571 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1573 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1574 else /* TYPE_CODE_UNDEF */
1575 TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
1577 type = TYPE_TARGET_TYPE (type);
1579 /* Preserve the instance flags as we traverse down the typedef chain.
1581 Handling address spaces/classes is nasty, what do we do if there's a
1583 E.g., what if an outer typedef marks the type as class_1 and an inner
1584 typedef marks the type as class_2?
1585 This is the wrong place to do such error checking. We leave it to
1586 the code that created the typedef in the first place to flag the
1587 error. We just pick the outer address space (akin to letting the
1588 outer cast in a chain of casting win), instead of assuming
1589 "it can't happen". */
1591 const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
1592 | TYPE_INSTANCE_FLAG_DATA_SPACE);
1593 const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
1594 int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
1596 /* Treat code vs data spaces and address classes separately. */
1597 if ((instance_flags & ALL_SPACES) != 0)
1598 new_instance_flags &= ~ALL_SPACES;
1599 if ((instance_flags & ALL_CLASSES) != 0)
1600 new_instance_flags &= ~ALL_CLASSES;
1602 instance_flags |= new_instance_flags;
1606 /* If this is a struct/class/union with no fields, then check
1607 whether a full definition exists somewhere else. This is for
1608 systems where a type definition with no fields is issued for such
1609 types, instead of identifying them as stub types in the first
1612 if (TYPE_IS_OPAQUE (type)
1613 && opaque_type_resolution
1614 && !currently_reading_symtab)
1616 const char *name = type_name_no_tag (type);
1617 struct type *newtype;
1621 stub_noname_complaint ();
1622 return make_qualified_type (type, instance_flags, NULL);
1624 newtype = lookup_transparent_type (name);
1628 /* If the resolved type and the stub are in the same
1629 objfile, then replace the stub type with the real deal.
1630 But if they're in separate objfiles, leave the stub
1631 alone; we'll just look up the transparent type every time
1632 we call check_typedef. We can't create pointers between
1633 types allocated to different objfiles, since they may
1634 have different lifetimes. Trying to copy NEWTYPE over to
1635 TYPE's objfile is pointless, too, since you'll have to
1636 move over any other types NEWTYPE refers to, which could
1637 be an unbounded amount of stuff. */
1638 if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1639 type = make_qualified_type (newtype,
1640 TYPE_INSTANCE_FLAGS (type),
1646 /* Otherwise, rely on the stub flag being set for opaque/stubbed
1648 else if (TYPE_STUB (type) && !currently_reading_symtab)
1650 const char *name = type_name_no_tag (type);
1651 /* FIXME: shouldn't we separately check the TYPE_NAME and the
1652 TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1653 as appropriate? (this code was written before TYPE_NAME and
1654 TYPE_TAG_NAME were separate). */
1659 stub_noname_complaint ();
1660 return make_qualified_type (type, instance_flags, NULL);
1662 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1665 /* Same as above for opaque types, we can replace the stub
1666 with the complete type only if they are in the same
1668 if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1669 type = make_qualified_type (SYMBOL_TYPE (sym),
1670 TYPE_INSTANCE_FLAGS (type),
1673 type = SYMBOL_TYPE (sym);
1677 if (TYPE_TARGET_STUB (type))
1679 struct type *range_type;
1680 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1682 if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1684 /* Nothing we can do. */
1686 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1687 && TYPE_NFIELDS (type) == 1
1688 && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
1689 == TYPE_CODE_RANGE))
1691 /* Now recompute the length of the array type, based on its
1692 number of elements and the target type's length.
1693 Watch out for Ada null Ada arrays where the high bound
1694 is smaller than the low bound. */
1695 const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
1696 const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
1699 if (high_bound < low_bound)
1703 /* For now, we conservatively take the array length to be 0
1704 if its length exceeds UINT_MAX. The code below assumes
1705 that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
1706 which is technically not guaranteed by C, but is usually true
1707 (because it would be true if x were unsigned with its
1708 high-order bit on). It uses the fact that
1709 high_bound-low_bound is always representable in
1710 ULONGEST and that if high_bound-low_bound+1 overflows,
1711 it overflows to 0. We must change these tests if we
1712 decide to increase the representation of TYPE_LENGTH
1713 from unsigned int to ULONGEST. */
1714 ULONGEST ulow = low_bound, uhigh = high_bound;
1715 ULONGEST tlen = TYPE_LENGTH (target_type);
1717 len = tlen * (uhigh - ulow + 1);
1718 if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh
1722 TYPE_LENGTH (type) = len;
1723 TYPE_TARGET_STUB (type) = 0;
1725 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1727 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1728 TYPE_TARGET_STUB (type) = 0;
1732 type = make_qualified_type (type, instance_flags, NULL);
1734 /* Cache TYPE_LENGTH for future use. */
1735 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1740 /* Parse a type expression in the string [P..P+LENGTH). If an error
1741 occurs, silently return a void type. */
1743 static struct type *
1744 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
1746 struct ui_file *saved_gdb_stderr;
1747 struct type *type = NULL; /* Initialize to keep gcc happy. */
1748 volatile struct gdb_exception except;
1750 /* Suppress error messages. */
1751 saved_gdb_stderr = gdb_stderr;
1752 gdb_stderr = ui_file_new ();
1754 /* Call parse_and_eval_type() without fear of longjmp()s. */
1755 TRY_CATCH (except, RETURN_MASK_ERROR)
1757 type = parse_and_eval_type (p, length);
1760 if (except.reason < 0)
1761 type = builtin_type (gdbarch)->builtin_void;
1763 /* Stop suppressing error messages. */
1764 ui_file_delete (gdb_stderr);
1765 gdb_stderr = saved_gdb_stderr;
1770 /* Ugly hack to convert method stubs into method types.
1772 He ain't kiddin'. This demangles the name of the method into a
1773 string including argument types, parses out each argument type,
1774 generates a string casting a zero to that type, evaluates the
1775 string, and stuffs the resulting type into an argtype vector!!!
1776 Then it knows the type of the whole function (including argument
1777 types for overloading), which info used to be in the stab's but was
1778 removed to hack back the space required for them. */
1781 check_stub_method (struct type *type, int method_id, int signature_id)
1783 struct gdbarch *gdbarch = get_type_arch (type);
1785 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1786 char *demangled_name = gdb_demangle (mangled_name,
1787 DMGL_PARAMS | DMGL_ANSI);
1788 char *argtypetext, *p;
1789 int depth = 0, argcount = 1;
1790 struct field *argtypes;
1793 /* Make sure we got back a function string that we can use. */
1795 p = strchr (demangled_name, '(');
1799 if (demangled_name == NULL || p == NULL)
1800 error (_("Internal: Cannot demangle mangled name `%s'."),
1803 /* Now, read in the parameters that define this type. */
1808 if (*p == '(' || *p == '<')
1812 else if (*p == ')' || *p == '>')
1816 else if (*p == ',' && depth == 0)
1824 /* If we read one argument and it was ``void'', don't count it. */
1825 if (strncmp (argtypetext, "(void)", 6) == 0)
1828 /* We need one extra slot, for the THIS pointer. */
1830 argtypes = (struct field *)
1831 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
1834 /* Add THIS pointer for non-static methods. */
1835 f = TYPE_FN_FIELDLIST1 (type, method_id);
1836 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1840 argtypes[0].type = lookup_pointer_type (type);
1844 if (*p != ')') /* () means no args, skip while. */
1849 if (depth <= 0 && (*p == ',' || *p == ')'))
1851 /* Avoid parsing of ellipsis, they will be handled below.
1852 Also avoid ``void'' as above. */
1853 if (strncmp (argtypetext, "...", p - argtypetext) != 0
1854 && strncmp (argtypetext, "void", p - argtypetext) != 0)
1856 argtypes[argcount].type =
1857 safe_parse_type (gdbarch, argtypetext, p - argtypetext);
1860 argtypetext = p + 1;
1863 if (*p == '(' || *p == '<')
1867 else if (*p == ')' || *p == '>')
1876 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1878 /* Now update the old "stub" type into a real type. */
1879 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1880 TYPE_DOMAIN_TYPE (mtype) = type;
1881 TYPE_FIELDS (mtype) = argtypes;
1882 TYPE_NFIELDS (mtype) = argcount;
1883 TYPE_STUB (mtype) = 0;
1884 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1886 TYPE_VARARGS (mtype) = 1;
1888 xfree (demangled_name);
1891 /* This is the external interface to check_stub_method, above. This
1892 function unstubs all of the signatures for TYPE's METHOD_ID method
1893 name. After calling this function TYPE_FN_FIELD_STUB will be
1894 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
1897 This function unfortunately can not die until stabs do. */
1900 check_stub_method_group (struct type *type, int method_id)
1902 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1903 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
1904 int j, found_stub = 0;
1906 for (j = 0; j < len; j++)
1907 if (TYPE_FN_FIELD_STUB (f, j))
1910 check_stub_method (type, method_id, j);
1913 /* GNU v3 methods with incorrect names were corrected when we read
1914 in type information, because it was cheaper to do it then. The
1915 only GNU v2 methods with incorrect method names are operators and
1916 destructors; destructors were also corrected when we read in type
1919 Therefore the only thing we need to handle here are v2 operator
1921 if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1924 char dem_opname[256];
1926 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
1928 dem_opname, DMGL_ANSI);
1930 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
1934 TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
1938 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690. */
1939 const struct cplus_struct_type cplus_struct_default = { };
1942 allocate_cplus_struct_type (struct type *type)
1944 if (HAVE_CPLUS_STRUCT (type))
1945 /* Structure was already allocated. Nothing more to do. */
1948 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
1949 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1950 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1951 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
1954 const struct gnat_aux_type gnat_aux_default =
1957 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
1958 and allocate the associated gnat-specific data. The gnat-specific
1959 data is also initialized to gnat_aux_default. */
1962 allocate_gnat_aux_type (struct type *type)
1964 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
1965 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
1966 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
1967 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
1970 /* Helper function to initialize the standard scalar types.
1972 If NAME is non-NULL, then it is used to initialize the type name.
1973 Note that NAME is not copied; it is required to have a lifetime at
1974 least as long as OBJFILE. */
1977 init_type (enum type_code code, int length, int flags,
1978 const char *name, struct objfile *objfile)
1982 type = alloc_type (objfile);
1983 TYPE_CODE (type) = code;
1984 TYPE_LENGTH (type) = length;
1986 gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
1987 if (flags & TYPE_FLAG_UNSIGNED)
1988 TYPE_UNSIGNED (type) = 1;
1989 if (flags & TYPE_FLAG_NOSIGN)
1990 TYPE_NOSIGN (type) = 1;
1991 if (flags & TYPE_FLAG_STUB)
1992 TYPE_STUB (type) = 1;
1993 if (flags & TYPE_FLAG_TARGET_STUB)
1994 TYPE_TARGET_STUB (type) = 1;
1995 if (flags & TYPE_FLAG_STATIC)
1996 TYPE_STATIC (type) = 1;
1997 if (flags & TYPE_FLAG_PROTOTYPED)
1998 TYPE_PROTOTYPED (type) = 1;
1999 if (flags & TYPE_FLAG_INCOMPLETE)
2000 TYPE_INCOMPLETE (type) = 1;
2001 if (flags & TYPE_FLAG_VARARGS)
2002 TYPE_VARARGS (type) = 1;
2003 if (flags & TYPE_FLAG_VECTOR)
2004 TYPE_VECTOR (type) = 1;
2005 if (flags & TYPE_FLAG_STUB_SUPPORTED)
2006 TYPE_STUB_SUPPORTED (type) = 1;
2007 if (flags & TYPE_FLAG_FIXED_INSTANCE)
2008 TYPE_FIXED_INSTANCE (type) = 1;
2009 if (flags & TYPE_FLAG_GNU_IFUNC)
2010 TYPE_GNU_IFUNC (type) = 1;
2012 TYPE_NAME (type) = name;
2016 if (name && strcmp (name, "char") == 0)
2017 TYPE_NOSIGN (type) = 1;
2021 case TYPE_CODE_STRUCT:
2022 case TYPE_CODE_UNION:
2023 case TYPE_CODE_NAMESPACE:
2024 INIT_CPLUS_SPECIFIC (type);
2027 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2029 case TYPE_CODE_FUNC:
2030 INIT_FUNC_SPECIFIC (type);
2036 /* Queries on types. */
2039 can_dereference (struct type *t)
2041 /* FIXME: Should we return true for references as well as
2046 && TYPE_CODE (t) == TYPE_CODE_PTR
2047 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2051 is_integral_type (struct type *t)
2056 && ((TYPE_CODE (t) == TYPE_CODE_INT)
2057 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
2058 || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
2059 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2060 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2061 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
2064 /* Return true if TYPE is scalar. */
2067 is_scalar_type (struct type *type)
2069 CHECK_TYPEDEF (type);
2071 switch (TYPE_CODE (type))
2073 case TYPE_CODE_ARRAY:
2074 case TYPE_CODE_STRUCT:
2075 case TYPE_CODE_UNION:
2077 case TYPE_CODE_STRING:
2084 /* Return true if T is scalar, or a composite type which in practice has
2085 the memory layout of a scalar type. E.g., an array or struct with only
2086 one scalar element inside it, or a union with only scalar elements. */
2089 is_scalar_type_recursive (struct type *t)
2093 if (is_scalar_type (t))
2095 /* Are we dealing with an array or string of known dimensions? */
2096 else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2097 || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2098 && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2100 LONGEST low_bound, high_bound;
2101 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2103 get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2105 return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2107 /* Are we dealing with a struct with one element? */
2108 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2109 return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2110 else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2112 int i, n = TYPE_NFIELDS (t);
2114 /* If all elements of the union are scalar, then the union is scalar. */
2115 for (i = 0; i < n; i++)
2116 if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2125 /* A helper function which returns true if types A and B represent the
2126 "same" class type. This is true if the types have the same main
2127 type, or the same name. */
2130 class_types_same_p (const struct type *a, const struct type *b)
2132 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2133 || (TYPE_NAME (a) && TYPE_NAME (b)
2134 && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2137 /* If BASE is an ancestor of DCLASS return the distance between them.
2138 otherwise return -1;
2142 class B: public A {};
2143 class C: public B {};
2146 distance_to_ancestor (A, A, 0) = 0
2147 distance_to_ancestor (A, B, 0) = 1
2148 distance_to_ancestor (A, C, 0) = 2
2149 distance_to_ancestor (A, D, 0) = 3
2151 If PUBLIC is 1 then only public ancestors are considered,
2152 and the function returns the distance only if BASE is a public ancestor
2156 distance_to_ancestor (A, D, 1) = -1. */
2159 distance_to_ancestor (struct type *base, struct type *dclass, int public)
2164 CHECK_TYPEDEF (base);
2165 CHECK_TYPEDEF (dclass);
2167 if (class_types_same_p (base, dclass))
2170 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2172 if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2175 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2183 /* Check whether BASE is an ancestor or base class or DCLASS
2184 Return 1 if so, and 0 if not.
2185 Note: If BASE and DCLASS are of the same type, this function
2186 will return 1. So for some class A, is_ancestor (A, A) will
2190 is_ancestor (struct type *base, struct type *dclass)
2192 return distance_to_ancestor (base, dclass, 0) >= 0;
2195 /* Like is_ancestor, but only returns true when BASE is a public
2196 ancestor of DCLASS. */
2199 is_public_ancestor (struct type *base, struct type *dclass)
2201 return distance_to_ancestor (base, dclass, 1) >= 0;
2204 /* A helper function for is_unique_ancestor. */
2207 is_unique_ancestor_worker (struct type *base, struct type *dclass,
2209 const gdb_byte *valaddr, int embedded_offset,
2210 CORE_ADDR address, struct value *val)
2214 CHECK_TYPEDEF (base);
2215 CHECK_TYPEDEF (dclass);
2217 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2222 iter = check_typedef (TYPE_BASECLASS (dclass, i));
2224 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2227 if (class_types_same_p (base, iter))
2229 /* If this is the first subclass, set *OFFSET and set count
2230 to 1. Otherwise, if this is at the same offset as
2231 previous instances, do nothing. Otherwise, increment
2235 *offset = this_offset;
2238 else if (this_offset == *offset)
2246 count += is_unique_ancestor_worker (base, iter, offset,
2248 embedded_offset + this_offset,
2255 /* Like is_ancestor, but only returns true if BASE is a unique base
2256 class of the type of VAL. */
2259 is_unique_ancestor (struct type *base, struct value *val)
2263 return is_unique_ancestor_worker (base, value_type (val), &offset,
2264 value_contents_for_printing (val),
2265 value_embedded_offset (val),
2266 value_address (val), val) == 1;
2270 /* Overload resolution. */
2272 /* Return the sum of the rank of A with the rank of B. */
2275 sum_ranks (struct rank a, struct rank b)
2278 c.rank = a.rank + b.rank;
2279 c.subrank = a.subrank + b.subrank;
2283 /* Compare rank A and B and return:
2285 1 if a is better than b
2286 -1 if b is better than a. */
2289 compare_ranks (struct rank a, struct rank b)
2291 if (a.rank == b.rank)
2293 if (a.subrank == b.subrank)
2295 if (a.subrank < b.subrank)
2297 if (a.subrank > b.subrank)
2301 if (a.rank < b.rank)
2304 /* a.rank > b.rank */
2308 /* Functions for overload resolution begin here. */
2310 /* Compare two badness vectors A and B and return the result.
2311 0 => A and B are identical
2312 1 => A and B are incomparable
2313 2 => A is better than B
2314 3 => A is worse than B */
2317 compare_badness (struct badness_vector *a, struct badness_vector *b)
2321 short found_pos = 0; /* any positives in c? */
2322 short found_neg = 0; /* any negatives in c? */
2324 /* differing lengths => incomparable */
2325 if (a->length != b->length)
2328 /* Subtract b from a */
2329 for (i = 0; i < a->length; i++)
2331 tmp = compare_ranks (b->rank[i], a->rank[i]);
2341 return 1; /* incomparable */
2343 return 3; /* A > B */
2349 return 2; /* A < B */
2351 return 0; /* A == B */
2355 /* Rank a function by comparing its parameter types (PARMS, length
2356 NPARMS), to the types of an argument list (ARGS, length NARGS).
2357 Return a pointer to a badness vector. This has NARGS + 1
2360 struct badness_vector *
2361 rank_function (struct type **parms, int nparms,
2362 struct value **args, int nargs)
2365 struct badness_vector *bv;
2366 int min_len = nparms < nargs ? nparms : nargs;
2368 bv = xmalloc (sizeof (struct badness_vector));
2369 bv->length = nargs + 1; /* add 1 for the length-match rank. */
2370 bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2372 /* First compare the lengths of the supplied lists.
2373 If there is a mismatch, set it to a high value. */
2375 /* pai/1997-06-03 FIXME: when we have debug info about default
2376 arguments and ellipsis parameter lists, we should consider those
2377 and rank the length-match more finely. */
2379 LENGTH_MATCH (bv) = (nargs != nparms)
2380 ? LENGTH_MISMATCH_BADNESS
2381 : EXACT_MATCH_BADNESS;
2383 /* Now rank all the parameters of the candidate function. */
2384 for (i = 1; i <= min_len; i++)
2385 bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2388 /* If more arguments than parameters, add dummy entries. */
2389 for (i = min_len + 1; i <= nargs; i++)
2390 bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2395 /* Compare the names of two integer types, assuming that any sign
2396 qualifiers have been checked already. We do it this way because
2397 there may be an "int" in the name of one of the types. */
2400 integer_types_same_name_p (const char *first, const char *second)
2402 int first_p, second_p;
2404 /* If both are shorts, return 1; if neither is a short, keep
2406 first_p = (strstr (first, "short") != NULL);
2407 second_p = (strstr (second, "short") != NULL);
2408 if (first_p && second_p)
2410 if (first_p || second_p)
2413 /* Likewise for long. */
2414 first_p = (strstr (first, "long") != NULL);
2415 second_p = (strstr (second, "long") != NULL);
2416 if (first_p && second_p)
2418 if (first_p || second_p)
2421 /* Likewise for char. */
2422 first_p = (strstr (first, "char") != NULL);
2423 second_p = (strstr (second, "char") != NULL);
2424 if (first_p && second_p)
2426 if (first_p || second_p)
2429 /* They must both be ints. */
2433 /* Compares type A to type B returns 1 if the represent the same type
2437 types_equal (struct type *a, struct type *b)
2439 /* Identical type pointers. */
2440 /* However, this still doesn't catch all cases of same type for b
2441 and a. The reason is that builtin types are different from
2442 the same ones constructed from the object. */
2446 /* Resolve typedefs */
2447 if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2448 a = check_typedef (a);
2449 if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2450 b = check_typedef (b);
2452 /* If after resolving typedefs a and b are not of the same type
2453 code then they are not equal. */
2454 if (TYPE_CODE (a) != TYPE_CODE (b))
2457 /* If a and b are both pointers types or both reference types then
2458 they are equal of the same type iff the objects they refer to are
2459 of the same type. */
2460 if (TYPE_CODE (a) == TYPE_CODE_PTR
2461 || TYPE_CODE (a) == TYPE_CODE_REF)
2462 return types_equal (TYPE_TARGET_TYPE (a),
2463 TYPE_TARGET_TYPE (b));
2465 /* Well, damnit, if the names are exactly the same, I'll say they
2466 are exactly the same. This happens when we generate method
2467 stubs. The types won't point to the same address, but they
2468 really are the same. */
2470 if (TYPE_NAME (a) && TYPE_NAME (b)
2471 && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2474 /* Check if identical after resolving typedefs. */
2478 /* Two function types are equal if their argument and return types
2480 if (TYPE_CODE (a) == TYPE_CODE_FUNC)
2484 if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
2487 if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
2490 for (i = 0; i < TYPE_NFIELDS (a); ++i)
2491 if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
2500 /* Deep comparison of types. */
2502 /* An entry in the type-equality bcache. */
2504 typedef struct type_equality_entry
2506 struct type *type1, *type2;
2507 } type_equality_entry_d;
2509 DEF_VEC_O (type_equality_entry_d);
2511 /* A helper function to compare two strings. Returns 1 if they are
2512 the same, 0 otherwise. Handles NULLs properly. */
2515 compare_maybe_null_strings (const char *s, const char *t)
2517 if (s == NULL && t != NULL)
2519 else if (s != NULL && t == NULL)
2521 else if (s == NULL && t== NULL)
2523 return strcmp (s, t) == 0;
2526 /* A helper function for check_types_worklist that checks two types for
2527 "deep" equality. Returns non-zero if the types are considered the
2528 same, zero otherwise. */
2531 check_types_equal (struct type *type1, struct type *type2,
2532 VEC (type_equality_entry_d) **worklist)
2534 CHECK_TYPEDEF (type1);
2535 CHECK_TYPEDEF (type2);
2540 if (TYPE_CODE (type1) != TYPE_CODE (type2)
2541 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
2542 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
2543 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
2544 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
2545 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
2546 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
2547 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
2548 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
2551 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
2552 TYPE_TAG_NAME (type2)))
2554 if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
2557 if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
2559 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
2560 sizeof (*TYPE_RANGE_DATA (type1))) != 0)
2567 for (i = 0; i < TYPE_NFIELDS (type1); ++i)
2569 const struct field *field1 = &TYPE_FIELD (type1, i);
2570 const struct field *field2 = &TYPE_FIELD (type2, i);
2571 struct type_equality_entry entry;
2573 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
2574 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
2575 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
2577 if (!compare_maybe_null_strings (FIELD_NAME (*field1),
2578 FIELD_NAME (*field2)))
2580 switch (FIELD_LOC_KIND (*field1))
2582 case FIELD_LOC_KIND_BITPOS:
2583 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
2586 case FIELD_LOC_KIND_ENUMVAL:
2587 if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
2590 case FIELD_LOC_KIND_PHYSADDR:
2591 if (FIELD_STATIC_PHYSADDR (*field1)
2592 != FIELD_STATIC_PHYSADDR (*field2))
2595 case FIELD_LOC_KIND_PHYSNAME:
2596 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
2597 FIELD_STATIC_PHYSNAME (*field2)))
2600 case FIELD_LOC_KIND_DWARF_BLOCK:
2602 struct dwarf2_locexpr_baton *block1, *block2;
2604 block1 = FIELD_DWARF_BLOCK (*field1);
2605 block2 = FIELD_DWARF_BLOCK (*field2);
2606 if (block1->per_cu != block2->per_cu
2607 || block1->size != block2->size
2608 || memcmp (block1->data, block2->data, block1->size) != 0)
2613 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
2614 "%d by check_types_equal"),
2615 FIELD_LOC_KIND (*field1));
2618 entry.type1 = FIELD_TYPE (*field1);
2619 entry.type2 = FIELD_TYPE (*field2);
2620 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2624 if (TYPE_TARGET_TYPE (type1) != NULL)
2626 struct type_equality_entry entry;
2628 if (TYPE_TARGET_TYPE (type2) == NULL)
2631 entry.type1 = TYPE_TARGET_TYPE (type1);
2632 entry.type2 = TYPE_TARGET_TYPE (type2);
2633 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2635 else if (TYPE_TARGET_TYPE (type2) != NULL)
2641 /* Check types on a worklist for equality. Returns zero if any pair
2642 is not equal, non-zero if they are all considered equal. */
2645 check_types_worklist (VEC (type_equality_entry_d) **worklist,
2646 struct bcache *cache)
2648 while (!VEC_empty (type_equality_entry_d, *worklist))
2650 struct type_equality_entry entry;
2653 entry = *VEC_last (type_equality_entry_d, *worklist);
2654 VEC_pop (type_equality_entry_d, *worklist);
2656 /* If the type pair has already been visited, we know it is
2658 bcache_full (&entry, sizeof (entry), cache, &added);
2662 if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
2669 /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
2670 "deep comparison". Otherwise return zero. */
2673 types_deeply_equal (struct type *type1, struct type *type2)
2675 volatile struct gdb_exception except;
2677 struct bcache *cache;
2678 VEC (type_equality_entry_d) *worklist = NULL;
2679 struct type_equality_entry entry;
2681 gdb_assert (type1 != NULL && type2 != NULL);
2683 /* Early exit for the simple case. */
2687 cache = bcache_xmalloc (NULL, NULL);
2689 entry.type1 = type1;
2690 entry.type2 = type2;
2691 VEC_safe_push (type_equality_entry_d, worklist, &entry);
2693 TRY_CATCH (except, RETURN_MASK_ALL)
2695 result = check_types_worklist (&worklist, cache);
2697 /* check_types_worklist calls several nested helper functions,
2698 some of which can raise a GDB Exception, so we just check
2699 and rethrow here. If there is a GDB exception, a comparison
2700 is not capable (or trusted), so exit. */
2701 bcache_xfree (cache);
2702 VEC_free (type_equality_entry_d, worklist);
2703 /* Rethrow if there was a problem. */
2704 if (except.reason < 0)
2705 throw_exception (except);
2710 /* Compare one type (PARM) for compatibility with another (ARG).
2711 * PARM is intended to be the parameter type of a function; and
2712 * ARG is the supplied argument's type. This function tests if
2713 * the latter can be converted to the former.
2714 * VALUE is the argument's value or NULL if none (or called recursively)
2716 * Return 0 if they are identical types;
2717 * Otherwise, return an integer which corresponds to how compatible
2718 * PARM is to ARG. The higher the return value, the worse the match.
2719 * Generally the "bad" conversions are all uniformly assigned a 100. */
2722 rank_one_type (struct type *parm, struct type *arg, struct value *value)
2724 struct rank rank = {0,0};
2726 if (types_equal (parm, arg))
2727 return EXACT_MATCH_BADNESS;
2729 /* Resolve typedefs */
2730 if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2731 parm = check_typedef (parm);
2732 if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2733 arg = check_typedef (arg);
2735 /* See through references, since we can almost make non-references
2737 if (TYPE_CODE (arg) == TYPE_CODE_REF)
2738 return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
2739 REFERENCE_CONVERSION_BADNESS));
2740 if (TYPE_CODE (parm) == TYPE_CODE_REF)
2741 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
2742 REFERENCE_CONVERSION_BADNESS));
2744 /* Debugging only. */
2745 fprintf_filtered (gdb_stderr,
2746 "------ Arg is %s [%d], parm is %s [%d]\n",
2747 TYPE_NAME (arg), TYPE_CODE (arg),
2748 TYPE_NAME (parm), TYPE_CODE (parm));
2750 /* x -> y means arg of type x being supplied for parameter of type y. */
2752 switch (TYPE_CODE (parm))
2755 switch (TYPE_CODE (arg))
2759 /* Allowed pointer conversions are:
2760 (a) pointer to void-pointer conversion. */
2761 if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2762 return VOID_PTR_CONVERSION_BADNESS;
2764 /* (b) pointer to ancestor-pointer conversion. */
2765 rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
2766 TYPE_TARGET_TYPE (arg),
2768 if (rank.subrank >= 0)
2769 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
2771 return INCOMPATIBLE_TYPE_BADNESS;
2772 case TYPE_CODE_ARRAY:
2773 if (types_equal (TYPE_TARGET_TYPE (parm),
2774 TYPE_TARGET_TYPE (arg)))
2775 return EXACT_MATCH_BADNESS;
2776 return INCOMPATIBLE_TYPE_BADNESS;
2777 case TYPE_CODE_FUNC:
2778 return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
2780 if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
2782 if (value_as_long (value) == 0)
2784 /* Null pointer conversion: allow it to be cast to a pointer.
2785 [4.10.1 of C++ standard draft n3290] */
2786 return NULL_POINTER_CONVERSION_BADNESS;
2790 /* If type checking is disabled, allow the conversion. */
2791 if (!strict_type_checking)
2792 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
2796 case TYPE_CODE_ENUM:
2797 case TYPE_CODE_FLAGS:
2798 case TYPE_CODE_CHAR:
2799 case TYPE_CODE_RANGE:
2800 case TYPE_CODE_BOOL:
2802 return INCOMPATIBLE_TYPE_BADNESS;
2804 case TYPE_CODE_ARRAY:
2805 switch (TYPE_CODE (arg))
2808 case TYPE_CODE_ARRAY:
2809 return rank_one_type (TYPE_TARGET_TYPE (parm),
2810 TYPE_TARGET_TYPE (arg), NULL);
2812 return INCOMPATIBLE_TYPE_BADNESS;
2814 case TYPE_CODE_FUNC:
2815 switch (TYPE_CODE (arg))
2817 case TYPE_CODE_PTR: /* funcptr -> func */
2818 return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
2820 return INCOMPATIBLE_TYPE_BADNESS;
2823 switch (TYPE_CODE (arg))
2826 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2828 /* Deal with signed, unsigned, and plain chars and
2829 signed and unsigned ints. */
2830 if (TYPE_NOSIGN (parm))
2832 /* This case only for character types. */
2833 if (TYPE_NOSIGN (arg))
2834 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
2835 else /* signed/unsigned char -> plain char */
2836 return INTEGER_CONVERSION_BADNESS;
2838 else if (TYPE_UNSIGNED (parm))
2840 if (TYPE_UNSIGNED (arg))
2842 /* unsigned int -> unsigned int, or
2843 unsigned long -> unsigned long */
2844 if (integer_types_same_name_p (TYPE_NAME (parm),
2846 return EXACT_MATCH_BADNESS;
2847 else if (integer_types_same_name_p (TYPE_NAME (arg),
2849 && integer_types_same_name_p (TYPE_NAME (parm),
2851 /* unsigned int -> unsigned long */
2852 return INTEGER_PROMOTION_BADNESS;
2854 /* unsigned long -> unsigned int */
2855 return INTEGER_CONVERSION_BADNESS;
2859 if (integer_types_same_name_p (TYPE_NAME (arg),
2861 && integer_types_same_name_p (TYPE_NAME (parm),
2863 /* signed long -> unsigned int */
2864 return INTEGER_CONVERSION_BADNESS;
2866 /* signed int/long -> unsigned int/long */
2867 return INTEGER_CONVERSION_BADNESS;
2870 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2872 if (integer_types_same_name_p (TYPE_NAME (parm),
2874 return EXACT_MATCH_BADNESS;
2875 else if (integer_types_same_name_p (TYPE_NAME (arg),
2877 && integer_types_same_name_p (TYPE_NAME (parm),
2879 return INTEGER_PROMOTION_BADNESS;
2881 return INTEGER_CONVERSION_BADNESS;
2884 return INTEGER_CONVERSION_BADNESS;
2886 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2887 return INTEGER_PROMOTION_BADNESS;
2889 return INTEGER_CONVERSION_BADNESS;
2890 case TYPE_CODE_ENUM:
2891 case TYPE_CODE_FLAGS:
2892 case TYPE_CODE_CHAR:
2893 case TYPE_CODE_RANGE:
2894 case TYPE_CODE_BOOL:
2895 return INTEGER_PROMOTION_BADNESS;
2897 return INT_FLOAT_CONVERSION_BADNESS;
2899 return NS_POINTER_CONVERSION_BADNESS;
2901 return INCOMPATIBLE_TYPE_BADNESS;
2904 case TYPE_CODE_ENUM:
2905 switch (TYPE_CODE (arg))
2908 case TYPE_CODE_CHAR:
2909 case TYPE_CODE_RANGE:
2910 case TYPE_CODE_BOOL:
2911 case TYPE_CODE_ENUM:
2912 return INTEGER_CONVERSION_BADNESS;
2914 return INT_FLOAT_CONVERSION_BADNESS;
2916 return INCOMPATIBLE_TYPE_BADNESS;
2919 case TYPE_CODE_CHAR:
2920 switch (TYPE_CODE (arg))
2922 case TYPE_CODE_RANGE:
2923 case TYPE_CODE_BOOL:
2924 case TYPE_CODE_ENUM:
2925 return INTEGER_CONVERSION_BADNESS;
2927 return INT_FLOAT_CONVERSION_BADNESS;
2929 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2930 return INTEGER_CONVERSION_BADNESS;
2931 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2932 return INTEGER_PROMOTION_BADNESS;
2933 /* >>> !! else fall through !! <<< */
2934 case TYPE_CODE_CHAR:
2935 /* Deal with signed, unsigned, and plain chars for C++ and
2936 with int cases falling through from previous case. */
2937 if (TYPE_NOSIGN (parm))
2939 if (TYPE_NOSIGN (arg))
2940 return EXACT_MATCH_BADNESS;
2942 return INTEGER_CONVERSION_BADNESS;
2944 else if (TYPE_UNSIGNED (parm))
2946 if (TYPE_UNSIGNED (arg))
2947 return EXACT_MATCH_BADNESS;
2949 return INTEGER_PROMOTION_BADNESS;
2951 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2952 return EXACT_MATCH_BADNESS;
2954 return INTEGER_CONVERSION_BADNESS;
2956 return INCOMPATIBLE_TYPE_BADNESS;
2959 case TYPE_CODE_RANGE:
2960 switch (TYPE_CODE (arg))
2963 case TYPE_CODE_CHAR:
2964 case TYPE_CODE_RANGE:
2965 case TYPE_CODE_BOOL:
2966 case TYPE_CODE_ENUM:
2967 return INTEGER_CONVERSION_BADNESS;
2969 return INT_FLOAT_CONVERSION_BADNESS;
2971 return INCOMPATIBLE_TYPE_BADNESS;
2974 case TYPE_CODE_BOOL:
2975 switch (TYPE_CODE (arg))
2977 /* n3290 draft, section 4.12.1 (conv.bool):
2979 "A prvalue of arithmetic, unscoped enumeration, pointer, or
2980 pointer to member type can be converted to a prvalue of type
2981 bool. A zero value, null pointer value, or null member pointer
2982 value is converted to false; any other value is converted to
2983 true. A prvalue of type std::nullptr_t can be converted to a
2984 prvalue of type bool; the resulting value is false." */
2986 case TYPE_CODE_CHAR:
2987 case TYPE_CODE_ENUM:
2989 case TYPE_CODE_MEMBERPTR:
2991 return BOOL_CONVERSION_BADNESS;
2992 case TYPE_CODE_RANGE:
2993 return INCOMPATIBLE_TYPE_BADNESS;
2994 case TYPE_CODE_BOOL:
2995 return EXACT_MATCH_BADNESS;
2997 return INCOMPATIBLE_TYPE_BADNESS;
3001 switch (TYPE_CODE (arg))
3004 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3005 return FLOAT_PROMOTION_BADNESS;
3006 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3007 return EXACT_MATCH_BADNESS;
3009 return FLOAT_CONVERSION_BADNESS;
3011 case TYPE_CODE_BOOL:
3012 case TYPE_CODE_ENUM:
3013 case TYPE_CODE_RANGE:
3014 case TYPE_CODE_CHAR:
3015 return INT_FLOAT_CONVERSION_BADNESS;
3017 return INCOMPATIBLE_TYPE_BADNESS;
3020 case TYPE_CODE_COMPLEX:
3021 switch (TYPE_CODE (arg))
3022 { /* Strictly not needed for C++, but... */
3024 return FLOAT_PROMOTION_BADNESS;
3025 case TYPE_CODE_COMPLEX:
3026 return EXACT_MATCH_BADNESS;
3028 return INCOMPATIBLE_TYPE_BADNESS;
3031 case TYPE_CODE_STRUCT:
3032 /* currently same as TYPE_CODE_CLASS. */
3033 switch (TYPE_CODE (arg))
3035 case TYPE_CODE_STRUCT:
3036 /* Check for derivation */
3037 rank.subrank = distance_to_ancestor (parm, arg, 0);
3038 if (rank.subrank >= 0)
3039 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
3040 /* else fall through */
3042 return INCOMPATIBLE_TYPE_BADNESS;
3045 case TYPE_CODE_UNION:
3046 switch (TYPE_CODE (arg))
3048 case TYPE_CODE_UNION:
3050 return INCOMPATIBLE_TYPE_BADNESS;
3053 case TYPE_CODE_MEMBERPTR:
3054 switch (TYPE_CODE (arg))
3057 return INCOMPATIBLE_TYPE_BADNESS;
3060 case TYPE_CODE_METHOD:
3061 switch (TYPE_CODE (arg))
3065 return INCOMPATIBLE_TYPE_BADNESS;
3069 switch (TYPE_CODE (arg))
3073 return INCOMPATIBLE_TYPE_BADNESS;
3078 switch (TYPE_CODE (arg))
3082 return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
3083 TYPE_FIELD_TYPE (arg, 0), NULL);
3085 return INCOMPATIBLE_TYPE_BADNESS;
3088 case TYPE_CODE_VOID:
3090 return INCOMPATIBLE_TYPE_BADNESS;
3091 } /* switch (TYPE_CODE (arg)) */
3094 /* End of functions for overload resolution. */
3096 /* Routines to pretty-print types. */
3099 print_bit_vector (B_TYPE *bits, int nbits)
3103 for (bitno = 0; bitno < nbits; bitno++)
3105 if ((bitno % 8) == 0)
3107 puts_filtered (" ");
3109 if (B_TST (bits, bitno))
3110 printf_filtered (("1"));
3112 printf_filtered (("0"));
3116 /* Note the first arg should be the "this" pointer, we may not want to
3117 include it since we may get into a infinitely recursive
3121 print_arg_types (struct field *args, int nargs, int spaces)
3127 for (i = 0; i < nargs; i++)
3128 recursive_dump_type (args[i].type, spaces + 2);
3133 field_is_static (struct field *f)
3135 /* "static" fields are the fields whose location is not relative
3136 to the address of the enclosing struct. It would be nice to
3137 have a dedicated flag that would be set for static fields when
3138 the type is being created. But in practice, checking the field
3139 loc_kind should give us an accurate answer. */
3140 return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3141 || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3145 dump_fn_fieldlists (struct type *type, int spaces)
3151 printfi_filtered (spaces, "fn_fieldlists ");
3152 gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
3153 printf_filtered ("\n");
3154 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3156 f = TYPE_FN_FIELDLIST1 (type, method_idx);
3157 printfi_filtered (spaces + 2, "[%d] name '%s' (",
3159 TYPE_FN_FIELDLIST_NAME (type, method_idx));
3160 gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3162 printf_filtered (_(") length %d\n"),
3163 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3164 for (overload_idx = 0;
3165 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3168 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3170 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
3171 gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3173 printf_filtered (")\n");
3174 printfi_filtered (spaces + 8, "type ");
3175 gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
3177 printf_filtered ("\n");
3179 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3182 printfi_filtered (spaces + 8, "args ");
3183 gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
3185 printf_filtered ("\n");
3187 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
3188 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f,
3191 printfi_filtered (spaces + 8, "fcontext ");
3192 gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3194 printf_filtered ("\n");
3196 printfi_filtered (spaces + 8, "is_const %d\n",
3197 TYPE_FN_FIELD_CONST (f, overload_idx));
3198 printfi_filtered (spaces + 8, "is_volatile %d\n",
3199 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3200 printfi_filtered (spaces + 8, "is_private %d\n",
3201 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3202 printfi_filtered (spaces + 8, "is_protected %d\n",
3203 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3204 printfi_filtered (spaces + 8, "is_stub %d\n",
3205 TYPE_FN_FIELD_STUB (f, overload_idx));
3206 printfi_filtered (spaces + 8, "voffset %u\n",
3207 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3213 print_cplus_stuff (struct type *type, int spaces)
3215 printfi_filtered (spaces, "n_baseclasses %d\n",
3216 TYPE_N_BASECLASSES (type));
3217 printfi_filtered (spaces, "nfn_fields %d\n",
3218 TYPE_NFN_FIELDS (type));
3219 if (TYPE_N_BASECLASSES (type) > 0)
3221 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3222 TYPE_N_BASECLASSES (type));
3223 gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
3225 printf_filtered (")");
3227 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3228 TYPE_N_BASECLASSES (type));
3229 puts_filtered ("\n");
3231 if (TYPE_NFIELDS (type) > 0)
3233 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3235 printfi_filtered (spaces,
3236 "private_field_bits (%d bits at *",
3237 TYPE_NFIELDS (type));
3238 gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
3240 printf_filtered (")");
3241 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3242 TYPE_NFIELDS (type));
3243 puts_filtered ("\n");
3245 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3247 printfi_filtered (spaces,
3248 "protected_field_bits (%d bits at *",
3249 TYPE_NFIELDS (type));
3250 gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
3252 printf_filtered (")");
3253 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3254 TYPE_NFIELDS (type));
3255 puts_filtered ("\n");
3258 if (TYPE_NFN_FIELDS (type) > 0)
3260 dump_fn_fieldlists (type, spaces);
3264 /* Print the contents of the TYPE's type_specific union, assuming that
3265 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
3268 print_gnat_stuff (struct type *type, int spaces)
3270 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3272 recursive_dump_type (descriptive_type, spaces + 2);
3275 static struct obstack dont_print_type_obstack;
3278 recursive_dump_type (struct type *type, int spaces)
3283 obstack_begin (&dont_print_type_obstack, 0);
3285 if (TYPE_NFIELDS (type) > 0
3286 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
3288 struct type **first_dont_print
3289 = (struct type **) obstack_base (&dont_print_type_obstack);
3291 int i = (struct type **)
3292 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
3296 if (type == first_dont_print[i])
3298 printfi_filtered (spaces, "type node ");
3299 gdb_print_host_address (type, gdb_stdout);
3300 printf_filtered (_(" <same as already seen type>\n"));
3305 obstack_ptr_grow (&dont_print_type_obstack, type);
3308 printfi_filtered (spaces, "type node ");
3309 gdb_print_host_address (type, gdb_stdout);
3310 printf_filtered ("\n");
3311 printfi_filtered (spaces, "name '%s' (",
3312 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
3313 gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
3314 printf_filtered (")\n");
3315 printfi_filtered (spaces, "tagname '%s' (",
3316 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
3317 gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
3318 printf_filtered (")\n");
3319 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
3320 switch (TYPE_CODE (type))
3322 case TYPE_CODE_UNDEF:
3323 printf_filtered ("(TYPE_CODE_UNDEF)");
3326 printf_filtered ("(TYPE_CODE_PTR)");
3328 case TYPE_CODE_ARRAY:
3329 printf_filtered ("(TYPE_CODE_ARRAY)");
3331 case TYPE_CODE_STRUCT:
3332 printf_filtered ("(TYPE_CODE_STRUCT)");
3334 case TYPE_CODE_UNION:
3335 printf_filtered ("(TYPE_CODE_UNION)");
3337 case TYPE_CODE_ENUM:
3338 printf_filtered ("(TYPE_CODE_ENUM)");
3340 case TYPE_CODE_FLAGS:
3341 printf_filtered ("(TYPE_CODE_FLAGS)");
3343 case TYPE_CODE_FUNC:
3344 printf_filtered ("(TYPE_CODE_FUNC)");
3347 printf_filtered ("(TYPE_CODE_INT)");
3350 printf_filtered ("(TYPE_CODE_FLT)");
3352 case TYPE_CODE_VOID:
3353 printf_filtered ("(TYPE_CODE_VOID)");
3356 printf_filtered ("(TYPE_CODE_SET)");
3358 case TYPE_CODE_RANGE:
3359 printf_filtered ("(TYPE_CODE_RANGE)");
3361 case TYPE_CODE_STRING:
3362 printf_filtered ("(TYPE_CODE_STRING)");
3364 case TYPE_CODE_ERROR:
3365 printf_filtered ("(TYPE_CODE_ERROR)");
3367 case TYPE_CODE_MEMBERPTR:
3368 printf_filtered ("(TYPE_CODE_MEMBERPTR)");
3370 case TYPE_CODE_METHODPTR:
3371 printf_filtered ("(TYPE_CODE_METHODPTR)");
3373 case TYPE_CODE_METHOD:
3374 printf_filtered ("(TYPE_CODE_METHOD)");
3377 printf_filtered ("(TYPE_CODE_REF)");
3379 case TYPE_CODE_CHAR:
3380 printf_filtered ("(TYPE_CODE_CHAR)");
3382 case TYPE_CODE_BOOL:
3383 printf_filtered ("(TYPE_CODE_BOOL)");
3385 case TYPE_CODE_COMPLEX:
3386 printf_filtered ("(TYPE_CODE_COMPLEX)");
3388 case TYPE_CODE_TYPEDEF:
3389 printf_filtered ("(TYPE_CODE_TYPEDEF)");
3391 case TYPE_CODE_NAMESPACE:
3392 printf_filtered ("(TYPE_CODE_NAMESPACE)");
3395 printf_filtered ("(UNKNOWN TYPE CODE)");
3398 puts_filtered ("\n");
3399 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
3400 if (TYPE_OBJFILE_OWNED (type))
3402 printfi_filtered (spaces, "objfile ");
3403 gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
3407 printfi_filtered (spaces, "gdbarch ");
3408 gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
3410 printf_filtered ("\n");
3411 printfi_filtered (spaces, "target_type ");
3412 gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
3413 printf_filtered ("\n");
3414 if (TYPE_TARGET_TYPE (type) != NULL)
3416 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3418 printfi_filtered (spaces, "pointer_type ");
3419 gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
3420 printf_filtered ("\n");
3421 printfi_filtered (spaces, "reference_type ");
3422 gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
3423 printf_filtered ("\n");
3424 printfi_filtered (spaces, "type_chain ");
3425 gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3426 printf_filtered ("\n");
3427 printfi_filtered (spaces, "instance_flags 0x%x",
3428 TYPE_INSTANCE_FLAGS (type));
3429 if (TYPE_CONST (type))
3431 puts_filtered (" TYPE_FLAG_CONST");
3433 if (TYPE_VOLATILE (type))
3435 puts_filtered (" TYPE_FLAG_VOLATILE");
3437 if (TYPE_CODE_SPACE (type))
3439 puts_filtered (" TYPE_FLAG_CODE_SPACE");
3441 if (TYPE_DATA_SPACE (type))
3443 puts_filtered (" TYPE_FLAG_DATA_SPACE");
3445 if (TYPE_ADDRESS_CLASS_1 (type))
3447 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3449 if (TYPE_ADDRESS_CLASS_2 (type))
3451 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3453 if (TYPE_RESTRICT (type))
3455 puts_filtered (" TYPE_FLAG_RESTRICT");
3457 puts_filtered ("\n");
3459 printfi_filtered (spaces, "flags");
3460 if (TYPE_UNSIGNED (type))
3462 puts_filtered (" TYPE_FLAG_UNSIGNED");
3464 if (TYPE_NOSIGN (type))
3466 puts_filtered (" TYPE_FLAG_NOSIGN");
3468 if (TYPE_STUB (type))
3470 puts_filtered (" TYPE_FLAG_STUB");
3472 if (TYPE_TARGET_STUB (type))
3474 puts_filtered (" TYPE_FLAG_TARGET_STUB");
3476 if (TYPE_STATIC (type))
3478 puts_filtered (" TYPE_FLAG_STATIC");
3480 if (TYPE_PROTOTYPED (type))
3482 puts_filtered (" TYPE_FLAG_PROTOTYPED");
3484 if (TYPE_INCOMPLETE (type))
3486 puts_filtered (" TYPE_FLAG_INCOMPLETE");
3488 if (TYPE_VARARGS (type))
3490 puts_filtered (" TYPE_FLAG_VARARGS");
3492 /* This is used for things like AltiVec registers on ppc. Gcc emits
3493 an attribute for the array type, which tells whether or not we
3494 have a vector, instead of a regular array. */
3495 if (TYPE_VECTOR (type))
3497 puts_filtered (" TYPE_FLAG_VECTOR");
3499 if (TYPE_FIXED_INSTANCE (type))
3501 puts_filtered (" TYPE_FIXED_INSTANCE");
3503 if (TYPE_STUB_SUPPORTED (type))
3505 puts_filtered (" TYPE_STUB_SUPPORTED");
3507 if (TYPE_NOTTEXT (type))
3509 puts_filtered (" TYPE_NOTTEXT");
3511 puts_filtered ("\n");
3512 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3513 gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3514 puts_filtered ("\n");
3515 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3517 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3518 printfi_filtered (spaces + 2,
3519 "[%d] enumval %s type ",
3520 idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3522 printfi_filtered (spaces + 2,
3523 "[%d] bitpos %d bitsize %d type ",
3524 idx, TYPE_FIELD_BITPOS (type, idx),
3525 TYPE_FIELD_BITSIZE (type, idx));
3526 gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3527 printf_filtered (" name '%s' (",
3528 TYPE_FIELD_NAME (type, idx) != NULL
3529 ? TYPE_FIELD_NAME (type, idx)
3531 gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3532 printf_filtered (")\n");
3533 if (TYPE_FIELD_TYPE (type, idx) != NULL)
3535 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3538 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3540 printfi_filtered (spaces, "low %s%s high %s%s\n",
3541 plongest (TYPE_LOW_BOUND (type)),
3542 TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3543 plongest (TYPE_HIGH_BOUND (type)),
3544 TYPE_HIGH_BOUND_UNDEFINED (type)
3545 ? " (undefined)" : "");
3547 printfi_filtered (spaces, "vptr_basetype ");
3548 gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3549 puts_filtered ("\n");
3550 if (TYPE_VPTR_BASETYPE (type) != NULL)
3552 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3554 printfi_filtered (spaces, "vptr_fieldno %d\n",
3555 TYPE_VPTR_FIELDNO (type));
3557 switch (TYPE_SPECIFIC_FIELD (type))
3559 case TYPE_SPECIFIC_CPLUS_STUFF:
3560 printfi_filtered (spaces, "cplus_stuff ");
3561 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
3563 puts_filtered ("\n");
3564 print_cplus_stuff (type, spaces);
3567 case TYPE_SPECIFIC_GNAT_STUFF:
3568 printfi_filtered (spaces, "gnat_stuff ");
3569 gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3570 puts_filtered ("\n");
3571 print_gnat_stuff (type, spaces);
3574 case TYPE_SPECIFIC_FLOATFORMAT:
3575 printfi_filtered (spaces, "floatformat ");
3576 if (TYPE_FLOATFORMAT (type) == NULL)
3577 puts_filtered ("(null)");
3580 puts_filtered ("{ ");
3581 if (TYPE_FLOATFORMAT (type)[0] == NULL
3582 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3583 puts_filtered ("(null)");
3585 puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3587 puts_filtered (", ");
3588 if (TYPE_FLOATFORMAT (type)[1] == NULL
3589 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3590 puts_filtered ("(null)");
3592 puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3594 puts_filtered (" }");
3596 puts_filtered ("\n");
3599 case TYPE_SPECIFIC_FUNC:
3600 printfi_filtered (spaces, "calling_convention %d\n",
3601 TYPE_CALLING_CONVENTION (type));
3602 /* tail_call_list is not printed. */
3607 obstack_free (&dont_print_type_obstack, NULL);
3610 /* Trivial helpers for the libiberty hash table, for mapping one
3615 struct type *old, *new;
3619 type_pair_hash (const void *item)
3621 const struct type_pair *pair = item;
3623 return htab_hash_pointer (pair->old);
3627 type_pair_eq (const void *item_lhs, const void *item_rhs)
3629 const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3631 return lhs->old == rhs->old;
3634 /* Allocate the hash table used by copy_type_recursive to walk
3635 types without duplicates. We use OBJFILE's obstack, because
3636 OBJFILE is about to be deleted. */
3639 create_copied_types_hash (struct objfile *objfile)
3641 return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3642 NULL, &objfile->objfile_obstack,
3643 hashtab_obstack_allocate,
3644 dummy_obstack_deallocate);
3647 /* Recursively copy (deep copy) TYPE, if it is associated with
3648 OBJFILE. Return a new type allocated using malloc, a saved type if
3649 we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
3650 not associated with OBJFILE. */
3653 copy_type_recursive (struct objfile *objfile,
3655 htab_t copied_types)
3657 struct type_pair *stored, pair;
3659 struct type *new_type;
3661 if (! TYPE_OBJFILE_OWNED (type))
3664 /* This type shouldn't be pointing to any types in other objfiles;
3665 if it did, the type might disappear unexpectedly. */
3666 gdb_assert (TYPE_OBJFILE (type) == objfile);
3669 slot = htab_find_slot (copied_types, &pair, INSERT);
3671 return ((struct type_pair *) *slot)->new;
3673 new_type = alloc_type_arch (get_type_arch (type));
3675 /* We must add the new type to the hash table immediately, in case
3676 we encounter this type again during a recursive call below. */
3678 = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
3680 stored->new = new_type;
3683 /* Copy the common fields of types. For the main type, we simply
3684 copy the entire thing and then update specific fields as needed. */
3685 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
3686 TYPE_OBJFILE_OWNED (new_type) = 0;
3687 TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
3689 if (TYPE_NAME (type))
3690 TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3691 if (TYPE_TAG_NAME (type))
3692 TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
3694 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3695 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3697 /* Copy the fields. */
3698 if (TYPE_NFIELDS (type))
3702 nfields = TYPE_NFIELDS (type);
3703 TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
3704 for (i = 0; i < nfields; i++)
3706 TYPE_FIELD_ARTIFICIAL (new_type, i) =
3707 TYPE_FIELD_ARTIFICIAL (type, i);
3708 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3709 if (TYPE_FIELD_TYPE (type, i))
3710 TYPE_FIELD_TYPE (new_type, i)
3711 = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3713 if (TYPE_FIELD_NAME (type, i))
3714 TYPE_FIELD_NAME (new_type, i) =
3715 xstrdup (TYPE_FIELD_NAME (type, i));
3716 switch (TYPE_FIELD_LOC_KIND (type, i))
3718 case FIELD_LOC_KIND_BITPOS:
3719 SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
3720 TYPE_FIELD_BITPOS (type, i));
3722 case FIELD_LOC_KIND_ENUMVAL:
3723 SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
3724 TYPE_FIELD_ENUMVAL (type, i));
3726 case FIELD_LOC_KIND_PHYSADDR:
3727 SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3728 TYPE_FIELD_STATIC_PHYSADDR (type, i));
3730 case FIELD_LOC_KIND_PHYSNAME:
3731 SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3732 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
3736 internal_error (__FILE__, __LINE__,
3737 _("Unexpected type field location kind: %d"),
3738 TYPE_FIELD_LOC_KIND (type, i));
3743 /* For range types, copy the bounds information. */
3744 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3746 TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
3747 *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
3750 /* Copy pointers to other types. */
3751 if (TYPE_TARGET_TYPE (type))
3752 TYPE_TARGET_TYPE (new_type) =
3753 copy_type_recursive (objfile,
3754 TYPE_TARGET_TYPE (type),
3756 if (TYPE_VPTR_BASETYPE (type))
3757 TYPE_VPTR_BASETYPE (new_type) =
3758 copy_type_recursive (objfile,
3759 TYPE_VPTR_BASETYPE (type),
3761 /* Maybe copy the type_specific bits.
3763 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3764 base classes and methods. There's no fundamental reason why we
3765 can't, but at the moment it is not needed. */
3767 if (TYPE_CODE (type) == TYPE_CODE_FLT)
3768 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
3769 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3770 || TYPE_CODE (type) == TYPE_CODE_UNION
3771 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3772 INIT_CPLUS_SPECIFIC (new_type);
3777 /* Make a copy of the given TYPE, except that the pointer & reference
3778 types are not preserved.
3780 This function assumes that the given type has an associated objfile.
3781 This objfile is used to allocate the new type. */
3784 copy_type (const struct type *type)
3786 struct type *new_type;
3788 gdb_assert (TYPE_OBJFILE_OWNED (type));
3790 new_type = alloc_type_copy (type);
3791 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3792 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3793 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
3794 sizeof (struct main_type));
3799 /* Helper functions to initialize architecture-specific types. */
3801 /* Allocate a type structure associated with GDBARCH and set its
3802 CODE, LENGTH, and NAME fields. */
3805 arch_type (struct gdbarch *gdbarch,
3806 enum type_code code, int length, char *name)
3810 type = alloc_type_arch (gdbarch);
3811 TYPE_CODE (type) = code;
3812 TYPE_LENGTH (type) = length;
3815 TYPE_NAME (type) = xstrdup (name);
3820 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
3821 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3822 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3825 arch_integer_type (struct gdbarch *gdbarch,
3826 int bit, int unsigned_p, char *name)
3830 t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
3832 TYPE_UNSIGNED (t) = 1;
3833 if (name && strcmp (name, "char") == 0)
3834 TYPE_NOSIGN (t) = 1;
3839 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
3840 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3841 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3844 arch_character_type (struct gdbarch *gdbarch,
3845 int bit, int unsigned_p, char *name)
3849 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
3851 TYPE_UNSIGNED (t) = 1;
3856 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
3857 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3858 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3861 arch_boolean_type (struct gdbarch *gdbarch,
3862 int bit, int unsigned_p, char *name)
3866 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
3868 TYPE_UNSIGNED (t) = 1;
3873 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
3874 BIT is the type size in bits; if BIT equals -1, the size is
3875 determined by the floatformat. NAME is the type name. Set the
3876 TYPE_FLOATFORMAT from FLOATFORMATS. */
3879 arch_float_type (struct gdbarch *gdbarch,
3880 int bit, char *name, const struct floatformat **floatformats)
3886 gdb_assert (floatformats != NULL);
3887 gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3888 bit = floatformats[0]->totalsize;
3890 gdb_assert (bit >= 0);
3892 t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
3893 TYPE_FLOATFORMAT (t) = floatformats;
3897 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
3898 NAME is the type name. TARGET_TYPE is the component float type. */
3901 arch_complex_type (struct gdbarch *gdbarch,
3902 char *name, struct type *target_type)
3906 t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
3907 2 * TYPE_LENGTH (target_type), name);
3908 TYPE_TARGET_TYPE (t) = target_type;
3912 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
3913 NAME is the type name. LENGTH is the size of the flag word in bytes. */
3916 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
3918 int nfields = length * TARGET_CHAR_BIT;
3921 type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
3922 TYPE_UNSIGNED (type) = 1;
3923 TYPE_NFIELDS (type) = nfields;
3924 TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
3929 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
3930 position BITPOS is called NAME. */
3933 append_flags_type_flag (struct type *type, int bitpos, char *name)
3935 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
3936 gdb_assert (bitpos < TYPE_NFIELDS (type));
3937 gdb_assert (bitpos >= 0);
3941 TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
3942 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
3946 /* Don't show this field to the user. */
3947 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
3951 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
3952 specified by CODE) associated with GDBARCH. NAME is the type name. */
3955 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
3959 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
3960 t = arch_type (gdbarch, code, 0, NULL);
3961 TYPE_TAG_NAME (t) = name;
3962 INIT_CPLUS_SPECIFIC (t);
3966 /* Add new field with name NAME and type FIELD to composite type T.
3967 Do not set the field's position or adjust the type's length;
3968 the caller should do so. Return the new field. */
3971 append_composite_type_field_raw (struct type *t, char *name,
3976 TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
3977 TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
3978 sizeof (struct field) * TYPE_NFIELDS (t));
3979 f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
3980 memset (f, 0, sizeof f[0]);
3981 FIELD_TYPE (f[0]) = field;
3982 FIELD_NAME (f[0]) = name;
3986 /* Add new field with name NAME and type FIELD to composite type T.
3987 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
3990 append_composite_type_field_aligned (struct type *t, char *name,
3991 struct type *field, int alignment)
3993 struct field *f = append_composite_type_field_raw (t, name, field);
3995 if (TYPE_CODE (t) == TYPE_CODE_UNION)
3997 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
3998 TYPE_LENGTH (t) = TYPE_LENGTH (field);
4000 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4002 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4003 if (TYPE_NFIELDS (t) > 1)
4005 SET_FIELD_BITPOS (f[0],
4006 (FIELD_BITPOS (f[-1])
4007 + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4008 * TARGET_CHAR_BIT)));
4014 alignment *= TARGET_CHAR_BIT;
4015 left = FIELD_BITPOS (f[0]) % alignment;
4019 SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
4020 TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
4027 /* Add new field with name NAME and type FIELD to composite type T. */
4030 append_composite_type_field (struct type *t, char *name,
4033 append_composite_type_field_aligned (t, name, field, 0);
4036 static struct gdbarch_data *gdbtypes_data;
4038 const struct builtin_type *
4039 builtin_type (struct gdbarch *gdbarch)
4041 return gdbarch_data (gdbarch, gdbtypes_data);
4045 gdbtypes_post_init (struct gdbarch *gdbarch)
4047 struct builtin_type *builtin_type
4048 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4051 builtin_type->builtin_void
4052 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4053 builtin_type->builtin_char
4054 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4055 !gdbarch_char_signed (gdbarch), "char");
4056 builtin_type->builtin_signed_char
4057 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4059 builtin_type->builtin_unsigned_char
4060 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4061 1, "unsigned char");
4062 builtin_type->builtin_short
4063 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4065 builtin_type->builtin_unsigned_short
4066 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4067 1, "unsigned short");
4068 builtin_type->builtin_int
4069 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4071 builtin_type->builtin_unsigned_int
4072 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4074 builtin_type->builtin_long
4075 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4077 builtin_type->builtin_unsigned_long
4078 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4079 1, "unsigned long");
4080 builtin_type->builtin_long_long
4081 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4083 builtin_type->builtin_unsigned_long_long
4084 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4085 1, "unsigned long long");
4086 builtin_type->builtin_float
4087 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
4088 "float", gdbarch_float_format (gdbarch));
4089 builtin_type->builtin_double
4090 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
4091 "double", gdbarch_double_format (gdbarch));
4092 builtin_type->builtin_long_double
4093 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
4094 "long double", gdbarch_long_double_format (gdbarch));
4095 builtin_type->builtin_complex
4096 = arch_complex_type (gdbarch, "complex",
4097 builtin_type->builtin_float);
4098 builtin_type->builtin_double_complex
4099 = arch_complex_type (gdbarch, "double complex",
4100 builtin_type->builtin_double);
4101 builtin_type->builtin_string
4102 = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4103 builtin_type->builtin_bool
4104 = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
4106 /* The following three are about decimal floating point types, which
4107 are 32-bits, 64-bits and 128-bits respectively. */
4108 builtin_type->builtin_decfloat
4109 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
4110 builtin_type->builtin_decdouble
4111 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
4112 builtin_type->builtin_declong
4113 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
4115 /* "True" character types. */
4116 builtin_type->builtin_true_char
4117 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4118 builtin_type->builtin_true_unsigned_char
4119 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
4121 /* Fixed-size integer types. */
4122 builtin_type->builtin_int0
4123 = arch_integer_type (gdbarch, 0, 0, "int0_t");
4124 builtin_type->builtin_int8
4125 = arch_integer_type (gdbarch, 8, 0, "int8_t");
4126 builtin_type->builtin_uint8
4127 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4128 builtin_type->builtin_int16
4129 = arch_integer_type (gdbarch, 16, 0, "int16_t");
4130 builtin_type->builtin_uint16
4131 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4132 builtin_type->builtin_int32
4133 = arch_integer_type (gdbarch, 32, 0, "int32_t");
4134 builtin_type->builtin_uint32
4135 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4136 builtin_type->builtin_int64
4137 = arch_integer_type (gdbarch, 64, 0, "int64_t");
4138 builtin_type->builtin_uint64
4139 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4140 builtin_type->builtin_int128
4141 = arch_integer_type (gdbarch, 128, 0, "int128_t");
4142 builtin_type->builtin_uint128
4143 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
4144 TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4145 TYPE_INSTANCE_FLAG_NOTTEXT;
4146 TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4147 TYPE_INSTANCE_FLAG_NOTTEXT;
4149 /* Wide character types. */
4150 builtin_type->builtin_char16
4151 = arch_integer_type (gdbarch, 16, 0, "char16_t");
4152 builtin_type->builtin_char32
4153 = arch_integer_type (gdbarch, 32, 0, "char32_t");
4156 /* Default data/code pointer types. */
4157 builtin_type->builtin_data_ptr
4158 = lookup_pointer_type (builtin_type->builtin_void);
4159 builtin_type->builtin_func_ptr
4160 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
4161 builtin_type->builtin_func_func
4162 = lookup_function_type (builtin_type->builtin_func_ptr);
4164 /* This type represents a GDB internal function. */
4165 builtin_type->internal_fn
4166 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4167 "<internal function>");
4169 return builtin_type;
4172 /* This set of objfile-based types is intended to be used by symbol
4173 readers as basic types. */
4175 static const struct objfile_data *objfile_type_data;
4177 const struct objfile_type *
4178 objfile_type (struct objfile *objfile)
4180 struct gdbarch *gdbarch;
4181 struct objfile_type *objfile_type
4182 = objfile_data (objfile, objfile_type_data);
4185 return objfile_type;
4187 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4188 1, struct objfile_type);
4190 /* Use the objfile architecture to determine basic type properties. */
4191 gdbarch = get_objfile_arch (objfile);
4194 objfile_type->builtin_void
4195 = init_type (TYPE_CODE_VOID, 1,
4199 objfile_type->builtin_char
4200 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4202 | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4204 objfile_type->builtin_signed_char
4205 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4207 "signed char", objfile);
4208 objfile_type->builtin_unsigned_char
4209 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4211 "unsigned char", objfile);
4212 objfile_type->builtin_short
4213 = init_type (TYPE_CODE_INT,
4214 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4215 0, "short", objfile);
4216 objfile_type->builtin_unsigned_short
4217 = init_type (TYPE_CODE_INT,
4218 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4219 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4220 objfile_type->builtin_int
4221 = init_type (TYPE_CODE_INT,
4222 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4224 objfile_type->builtin_unsigned_int
4225 = init_type (TYPE_CODE_INT,
4226 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4227 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4228 objfile_type->builtin_long
4229 = init_type (TYPE_CODE_INT,
4230 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4231 0, "long", objfile);
4232 objfile_type->builtin_unsigned_long
4233 = init_type (TYPE_CODE_INT,
4234 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4235 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4236 objfile_type->builtin_long_long
4237 = init_type (TYPE_CODE_INT,
4238 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4239 0, "long long", objfile);
4240 objfile_type->builtin_unsigned_long_long
4241 = init_type (TYPE_CODE_INT,
4242 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4243 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4245 objfile_type->builtin_float
4246 = init_type (TYPE_CODE_FLT,
4247 gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4248 0, "float", objfile);
4249 TYPE_FLOATFORMAT (objfile_type->builtin_float)
4250 = gdbarch_float_format (gdbarch);
4251 objfile_type->builtin_double
4252 = init_type (TYPE_CODE_FLT,
4253 gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4254 0, "double", objfile);
4255 TYPE_FLOATFORMAT (objfile_type->builtin_double)
4256 = gdbarch_double_format (gdbarch);
4257 objfile_type->builtin_long_double
4258 = init_type (TYPE_CODE_FLT,
4259 gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
4260 0, "long double", objfile);
4261 TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
4262 = gdbarch_long_double_format (gdbarch);
4264 /* This type represents a type that was unrecognized in symbol read-in. */
4265 objfile_type->builtin_error
4266 = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
4268 /* The following set of types is used for symbols with no
4269 debug information. */
4270 objfile_type->nodebug_text_symbol
4271 = init_type (TYPE_CODE_FUNC, 1, 0,
4272 "<text variable, no debug info>", objfile);
4273 TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
4274 = objfile_type->builtin_int;
4275 objfile_type->nodebug_text_gnu_ifunc_symbol
4276 = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4277 "<text gnu-indirect-function variable, no debug info>",
4279 TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4280 = objfile_type->nodebug_text_symbol;
4281 objfile_type->nodebug_got_plt_symbol
4282 = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4283 "<text from jump slot in .got.plt, no debug info>",
4285 TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4286 = objfile_type->nodebug_text_symbol;
4287 objfile_type->nodebug_data_symbol
4288 = init_type (TYPE_CODE_INT,
4289 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4290 "<data variable, no debug info>", objfile);
4291 objfile_type->nodebug_unknown_symbol
4292 = init_type (TYPE_CODE_INT, 1, 0,
4293 "<variable (not text or data), no debug info>", objfile);
4294 objfile_type->nodebug_tls_symbol
4295 = init_type (TYPE_CODE_INT,
4296 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4297 "<thread local variable, no debug info>", objfile);
4299 /* NOTE: on some targets, addresses and pointers are not necessarily
4303 - gdb's `struct type' always describes the target's
4305 - gdb's `struct value' objects should always hold values in
4307 - gdb's CORE_ADDR values are addresses in the unified virtual
4308 address space that the assembler and linker work with. Thus,
4309 since target_read_memory takes a CORE_ADDR as an argument, it
4310 can access any memory on the target, even if the processor has
4311 separate code and data address spaces.
4313 In this context, objfile_type->builtin_core_addr is a bit odd:
4314 it's a target type for a value the target will never see. It's
4315 only used to hold the values of (typeless) linker symbols, which
4316 are indeed in the unified virtual address space. */
4318 objfile_type->builtin_core_addr
4319 = init_type (TYPE_CODE_INT,
4320 gdbarch_addr_bit (gdbarch) / 8,
4321 TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
4323 set_objfile_data (objfile, objfile_type_data, objfile_type);
4324 return objfile_type;
4327 extern initialize_file_ftype _initialize_gdbtypes;
4330 _initialize_gdbtypes (void)
4332 gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
4333 objfile_type_data = register_objfile_data ();
4335 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4336 _("Set debugging of C++ overloading."),
4337 _("Show debugging of C++ overloading."),
4338 _("When enabled, ranking of the "
4339 "functions is displayed."),
4341 show_overload_debug,
4342 &setdebuglist, &showdebuglist);
4344 /* Add user knob for controlling resolution of opaque types. */
4345 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
4346 &opaque_type_resolution,
4347 _("Set resolution of opaque struct/class/union"
4348 " types (if set before loading symbols)."),
4349 _("Show resolution of opaque struct/class/union"
4350 " types (if set before loading symbols)."),
4352 show_opaque_type_resolution,
4353 &setlist, &showlist);
4355 /* Add an option to permit non-strict type checking. */
4356 add_setshow_boolean_cmd ("type", class_support,
4357 &strict_type_checking,
4358 _("Set strict type checking."),
4359 _("Show strict type checking."),
4361 show_strict_type_checking,
4362 &setchecklist, &showchecklist);