Automatic date update in version.in
[binutils-gdb.git] / gdb / gdbtypes.c
blob8c2558dd7ac85f67926b0edce53ee11388558210
1 /* Support routines for manipulating internal types for GDB.
3 Copyright (C) 1992-2022 Free Software Foundation, Inc.
5 Contributed by Cygnus Support, using pieces from other GDB modules.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "defs.h"
23 #include "bfd.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "language.h"
30 #include "target.h"
31 #include "value.h"
32 #include "demangle.h"
33 #include "complaints.h"
34 #include "gdbcmd.h"
35 #include "cp-abi.h"
36 #include "hashtab.h"
37 #include "cp-support.h"
38 #include "bcache.h"
39 #include "dwarf2/loc.h"
40 #include "dwarf2/read.h"
41 #include "gdbcore.h"
42 #include "floatformat.h"
43 #include "f-lang.h"
44 #include <algorithm>
45 #include "gmp-utils.h"
47 /* The value of an invalid conversion badness. */
48 #define INVALID_CONVERSION 100
50 static struct dynamic_prop_list *
51 copy_dynamic_prop_list (struct obstack *, struct dynamic_prop_list *);
53 /* Initialize BADNESS constants. */
55 const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
57 const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
58 const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
60 const struct rank EXACT_MATCH_BADNESS = {0,0};
62 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
63 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
64 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
65 const struct rank CV_CONVERSION_BADNESS = {1, 0};
66 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
67 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
68 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
69 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
70 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
71 const struct rank BASE_CONVERSION_BADNESS = {2,0};
72 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
73 const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
74 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
75 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
76 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
78 /* Floatformat pairs. */
79 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
80 &floatformat_ieee_half_big,
81 &floatformat_ieee_half_little
83 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
84 &floatformat_ieee_single_big,
85 &floatformat_ieee_single_little
87 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
88 &floatformat_ieee_double_big,
89 &floatformat_ieee_double_little
91 const struct floatformat *floatformats_ieee_quad[BFD_ENDIAN_UNKNOWN] = {
92 &floatformat_ieee_quad_big,
93 &floatformat_ieee_quad_little
95 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
96 &floatformat_ieee_double_big,
97 &floatformat_ieee_double_littlebyte_bigword
99 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
100 &floatformat_i387_ext,
101 &floatformat_i387_ext
103 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
104 &floatformat_m68881_ext,
105 &floatformat_m68881_ext
107 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
108 &floatformat_arm_ext_big,
109 &floatformat_arm_ext_littlebyte_bigword
111 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
112 &floatformat_ia64_spill_big,
113 &floatformat_ia64_spill_little
115 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
116 &floatformat_vax_f,
117 &floatformat_vax_f
119 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
120 &floatformat_vax_d,
121 &floatformat_vax_d
123 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
124 &floatformat_ibm_long_double_big,
125 &floatformat_ibm_long_double_little
127 const struct floatformat *floatformats_bfloat16[BFD_ENDIAN_UNKNOWN] = {
128 &floatformat_bfloat16_big,
129 &floatformat_bfloat16_little
132 /* Should opaque types be resolved? */
134 static bool opaque_type_resolution = true;
136 /* See gdbtypes.h. */
138 unsigned int overload_debug = 0;
140 /* A flag to enable strict type checking. */
142 static bool strict_type_checking = true;
144 /* A function to show whether opaque types are resolved. */
146 static void
147 show_opaque_type_resolution (struct ui_file *file, int from_tty,
148 struct cmd_list_element *c,
149 const char *value)
151 gdb_printf (file, _("Resolution of opaque struct/class/union types "
152 "(if set before loading symbols) is %s.\n"),
153 value);
156 /* A function to show whether C++ overload debugging is enabled. */
158 static void
159 show_overload_debug (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
162 gdb_printf (file, _("Debugging of C++ overloading is %s.\n"),
163 value);
166 /* A function to show the status of strict type checking. */
168 static void
169 show_strict_type_checking (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
172 gdb_printf (file, _("Strict type checking is %s.\n"), value);
176 /* Allocate a new OBJFILE-associated type structure and fill it
177 with some defaults. Space for the type structure is allocated
178 on the objfile's objfile_obstack. */
180 struct type *
181 alloc_type (struct objfile *objfile)
183 struct type *type;
185 gdb_assert (objfile != NULL);
187 /* Alloc the structure and start off with all fields zeroed. */
188 type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
189 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
190 struct main_type);
191 OBJSTAT (objfile, n_types++);
193 type->set_owner (objfile);
195 /* Initialize the fields that might not be zero. */
197 type->set_code (TYPE_CODE_UNDEF);
198 TYPE_CHAIN (type) = type; /* Chain back to itself. */
200 return type;
203 /* Allocate a new GDBARCH-associated type structure and fill it
204 with some defaults. Space for the type structure is allocated
205 on the obstack associated with GDBARCH. */
207 struct type *
208 alloc_type_arch (struct gdbarch *gdbarch)
210 struct type *type;
212 gdb_assert (gdbarch != NULL);
214 /* Alloc the structure and start off with all fields zeroed. */
216 type = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct type);
217 TYPE_MAIN_TYPE (type) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct main_type);
219 type->set_owner (gdbarch);
221 /* Initialize the fields that might not be zero. */
223 type->set_code (TYPE_CODE_UNDEF);
224 TYPE_CHAIN (type) = type; /* Chain back to itself. */
226 return type;
229 /* If TYPE is objfile-associated, allocate a new type structure
230 associated with the same objfile. If TYPE is gdbarch-associated,
231 allocate a new type structure associated with the same gdbarch. */
233 struct type *
234 alloc_type_copy (const struct type *type)
236 if (type->is_objfile_owned ())
237 return alloc_type (type->objfile_owner ());
238 else
239 return alloc_type_arch (type->arch_owner ());
242 /* See gdbtypes.h. */
244 gdbarch *
245 type::arch () const
247 struct gdbarch *arch;
249 if (this->is_objfile_owned ())
250 arch = this->objfile_owner ()->arch ();
251 else
252 arch = this->arch_owner ();
254 /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
255 a gdbarch, however, this is very rare, and even then, in most cases
256 that type::arch is called, we assume that a non-NULL value is
257 returned. */
258 gdb_assert (arch != nullptr);
259 return arch;
262 /* See gdbtypes.h. */
264 struct type *
265 get_target_type (struct type *type)
267 if (type != NULL)
269 type = TYPE_TARGET_TYPE (type);
270 if (type != NULL)
271 type = check_typedef (type);
274 return type;
277 /* See gdbtypes.h. */
279 unsigned int
280 type_length_units (struct type *type)
282 int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
284 return TYPE_LENGTH (type) / unit_size;
287 /* Alloc a new type instance structure, fill it with some defaults,
288 and point it at OLDTYPE. Allocate the new type instance from the
289 same place as OLDTYPE. */
291 static struct type *
292 alloc_type_instance (struct type *oldtype)
294 struct type *type;
296 /* Allocate the structure. */
298 if (!oldtype->is_objfile_owned ())
299 type = GDBARCH_OBSTACK_ZALLOC (oldtype->arch_owner (), struct type);
300 else
301 type = OBSTACK_ZALLOC (&oldtype->objfile_owner ()->objfile_obstack,
302 struct type);
304 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
306 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
308 return type;
311 /* Clear all remnants of the previous type at TYPE, in preparation for
312 replacing it with something else. Preserve owner information. */
314 static void
315 smash_type (struct type *type)
317 bool objfile_owned = type->is_objfile_owned ();
318 objfile *objfile = type->objfile_owner ();
319 gdbarch *arch = type->arch_owner ();
321 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
323 /* Restore owner information. */
324 if (objfile_owned)
325 type->set_owner (objfile);
326 else
327 type->set_owner (arch);
329 /* For now, delete the rings. */
330 TYPE_CHAIN (type) = type;
332 /* For now, leave the pointer/reference types alone. */
335 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
336 to a pointer to memory where the pointer type should be stored.
337 If *TYPEPTR is zero, update it to point to the pointer type we return.
338 We allocate new memory if needed. */
340 struct type *
341 make_pointer_type (struct type *type, struct type **typeptr)
343 struct type *ntype; /* New type */
344 struct type *chain;
346 ntype = TYPE_POINTER_TYPE (type);
348 if (ntype)
350 if (typeptr == 0)
351 return ntype; /* Don't care about alloc,
352 and have new type. */
353 else if (*typeptr == 0)
355 *typeptr = ntype; /* Tracking alloc, and have new type. */
356 return ntype;
360 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
362 ntype = alloc_type_copy (type);
363 if (typeptr)
364 *typeptr = ntype;
366 else /* We have storage, but need to reset it. */
368 ntype = *typeptr;
369 chain = TYPE_CHAIN (ntype);
370 smash_type (ntype);
371 TYPE_CHAIN (ntype) = chain;
374 TYPE_TARGET_TYPE (ntype) = type;
375 TYPE_POINTER_TYPE (type) = ntype;
377 /* FIXME! Assumes the machine has only one representation for pointers! */
379 TYPE_LENGTH (ntype) = gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT;
380 ntype->set_code (TYPE_CODE_PTR);
382 /* Mark pointers as unsigned. The target converts between pointers
383 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
384 gdbarch_address_to_pointer. */
385 ntype->set_is_unsigned (true);
387 /* Update the length of all the other variants of this type. */
388 chain = TYPE_CHAIN (ntype);
389 while (chain != ntype)
391 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
392 chain = TYPE_CHAIN (chain);
395 return ntype;
398 /* Given a type TYPE, return a type of pointers to that type.
399 May need to construct such a type if this is the first use. */
401 struct type *
402 lookup_pointer_type (struct type *type)
404 return make_pointer_type (type, (struct type **) 0);
407 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
408 points to a pointer to memory where the reference type should be
409 stored. If *TYPEPTR is zero, update it to point to the reference
410 type we return. We allocate new memory if needed. REFCODE denotes
411 the kind of reference type to lookup (lvalue or rvalue reference). */
413 struct type *
414 make_reference_type (struct type *type, struct type **typeptr,
415 enum type_code refcode)
417 struct type *ntype; /* New type */
418 struct type **reftype;
419 struct type *chain;
421 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
423 ntype = (refcode == TYPE_CODE_REF ? TYPE_REFERENCE_TYPE (type)
424 : TYPE_RVALUE_REFERENCE_TYPE (type));
426 if (ntype)
428 if (typeptr == 0)
429 return ntype; /* Don't care about alloc,
430 and have new type. */
431 else if (*typeptr == 0)
433 *typeptr = ntype; /* Tracking alloc, and have new type. */
434 return ntype;
438 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
440 ntype = alloc_type_copy (type);
441 if (typeptr)
442 *typeptr = ntype;
444 else /* We have storage, but need to reset it. */
446 ntype = *typeptr;
447 chain = TYPE_CHAIN (ntype);
448 smash_type (ntype);
449 TYPE_CHAIN (ntype) = chain;
452 TYPE_TARGET_TYPE (ntype) = type;
453 reftype = (refcode == TYPE_CODE_REF ? &TYPE_REFERENCE_TYPE (type)
454 : &TYPE_RVALUE_REFERENCE_TYPE (type));
456 *reftype = ntype;
458 /* FIXME! Assume the machine has only one representation for
459 references, and that it matches the (only) representation for
460 pointers! */
462 TYPE_LENGTH (ntype) = gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT;
463 ntype->set_code (refcode);
465 *reftype = ntype;
467 /* Update the length of all the other variants of this type. */
468 chain = TYPE_CHAIN (ntype);
469 while (chain != ntype)
471 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
472 chain = TYPE_CHAIN (chain);
475 return ntype;
478 /* Same as above, but caller doesn't care about memory allocation
479 details. */
481 struct type *
482 lookup_reference_type (struct type *type, enum type_code refcode)
484 return make_reference_type (type, (struct type **) 0, refcode);
487 /* Lookup the lvalue reference type for the type TYPE. */
489 struct type *
490 lookup_lvalue_reference_type (struct type *type)
492 return lookup_reference_type (type, TYPE_CODE_REF);
495 /* Lookup the rvalue reference type for the type TYPE. */
497 struct type *
498 lookup_rvalue_reference_type (struct type *type)
500 return lookup_reference_type (type, TYPE_CODE_RVALUE_REF);
503 /* Lookup a function type that returns type TYPE. TYPEPTR, if
504 nonzero, points to a pointer to memory where the function type
505 should be stored. If *TYPEPTR is zero, update it to point to the
506 function type we return. We allocate new memory if needed. */
508 struct type *
509 make_function_type (struct type *type, struct type **typeptr)
511 struct type *ntype; /* New type */
513 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
515 ntype = alloc_type_copy (type);
516 if (typeptr)
517 *typeptr = ntype;
519 else /* We have storage, but need to reset it. */
521 ntype = *typeptr;
522 smash_type (ntype);
525 TYPE_TARGET_TYPE (ntype) = type;
527 TYPE_LENGTH (ntype) = 1;
528 ntype->set_code (TYPE_CODE_FUNC);
530 INIT_FUNC_SPECIFIC (ntype);
532 return ntype;
535 /* Given a type TYPE, return a type of functions that return that type.
536 May need to construct such a type if this is the first use. */
538 struct type *
539 lookup_function_type (struct type *type)
541 return make_function_type (type, (struct type **) 0);
544 /* Given a type TYPE and argument types, return the appropriate
545 function type. If the final type in PARAM_TYPES is NULL, make a
546 varargs function. */
548 struct type *
549 lookup_function_type_with_arguments (struct type *type,
550 int nparams,
551 struct type **param_types)
553 struct type *fn = make_function_type (type, (struct type **) 0);
554 int i;
556 if (nparams > 0)
558 if (param_types[nparams - 1] == NULL)
560 --nparams;
561 fn->set_has_varargs (true);
563 else if (check_typedef (param_types[nparams - 1])->code ()
564 == TYPE_CODE_VOID)
566 --nparams;
567 /* Caller should have ensured this. */
568 gdb_assert (nparams == 0);
569 fn->set_is_prototyped (true);
571 else
572 fn->set_is_prototyped (true);
575 fn->set_num_fields (nparams);
576 fn->set_fields
577 ((struct field *) TYPE_ZALLOC (fn, nparams * sizeof (struct field)));
578 for (i = 0; i < nparams; ++i)
579 fn->field (i).set_type (param_types[i]);
581 return fn;
584 /* Identify address space identifier by name -- return a
585 type_instance_flags. */
587 type_instance_flags
588 address_space_name_to_type_instance_flags (struct gdbarch *gdbarch,
589 const char *space_identifier)
591 type_instance_flags type_flags;
593 /* Check for known address space delimiters. */
594 if (!strcmp (space_identifier, "code"))
595 return TYPE_INSTANCE_FLAG_CODE_SPACE;
596 else if (!strcmp (space_identifier, "data"))
597 return TYPE_INSTANCE_FLAG_DATA_SPACE;
598 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
599 && gdbarch_address_class_name_to_type_flags (gdbarch,
600 space_identifier,
601 &type_flags))
602 return type_flags;
603 else
604 error (_("Unknown address space specifier: \"%s\""), space_identifier);
607 /* Identify address space identifier by type_instance_flags and return
608 the string version of the adress space name. */
610 const char *
611 address_space_type_instance_flags_to_name (struct gdbarch *gdbarch,
612 type_instance_flags space_flag)
614 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
615 return "code";
616 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
617 return "data";
618 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
619 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
620 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
621 else
622 return NULL;
625 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
627 If STORAGE is non-NULL, create the new type instance there.
628 STORAGE must be in the same obstack as TYPE. */
630 static struct type *
631 make_qualified_type (struct type *type, type_instance_flags new_flags,
632 struct type *storage)
634 struct type *ntype;
636 ntype = type;
639 if (ntype->instance_flags () == new_flags)
640 return ntype;
641 ntype = TYPE_CHAIN (ntype);
643 while (ntype != type);
645 /* Create a new type instance. */
646 if (storage == NULL)
647 ntype = alloc_type_instance (type);
648 else
650 /* If STORAGE was provided, it had better be in the same objfile
651 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
652 if one objfile is freed and the other kept, we'd have
653 dangling pointers. */
654 gdb_assert (type->objfile_owner () == storage->objfile_owner ());
656 ntype = storage;
657 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
658 TYPE_CHAIN (ntype) = ntype;
661 /* Pointers or references to the original type are not relevant to
662 the new type. */
663 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
664 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
666 /* Chain the new qualified type to the old type. */
667 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
668 TYPE_CHAIN (type) = ntype;
670 /* Now set the instance flags and return the new type. */
671 ntype->set_instance_flags (new_flags);
673 /* Set length of new type to that of the original type. */
674 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
676 return ntype;
679 /* Make an address-space-delimited variant of a type -- a type that
680 is identical to the one supplied except that it has an address
681 space attribute attached to it (such as "code" or "data").
683 The space attributes "code" and "data" are for Harvard
684 architectures. The address space attributes are for architectures
685 which have alternately sized pointers or pointers with alternate
686 representations. */
688 struct type *
689 make_type_with_address_space (struct type *type,
690 type_instance_flags space_flag)
692 type_instance_flags new_flags = ((type->instance_flags ()
693 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
694 | TYPE_INSTANCE_FLAG_DATA_SPACE
695 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
696 | space_flag);
698 return make_qualified_type (type, new_flags, NULL);
701 /* Make a "c-v" variant of a type -- a type that is identical to the
702 one supplied except that it may have const or volatile attributes
703 CNST is a flag for setting the const attribute
704 VOLTL is a flag for setting the volatile attribute
705 TYPE is the base type whose variant we are creating.
707 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
708 storage to hold the new qualified type; *TYPEPTR and TYPE must be
709 in the same objfile. Otherwise, allocate fresh memory for the new
710 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
711 new type we construct. */
713 struct type *
714 make_cv_type (int cnst, int voltl,
715 struct type *type,
716 struct type **typeptr)
718 struct type *ntype; /* New type */
720 type_instance_flags new_flags = (type->instance_flags ()
721 & ~(TYPE_INSTANCE_FLAG_CONST
722 | TYPE_INSTANCE_FLAG_VOLATILE));
724 if (cnst)
725 new_flags |= TYPE_INSTANCE_FLAG_CONST;
727 if (voltl)
728 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
730 if (typeptr && *typeptr != NULL)
732 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
733 a C-V variant chain that threads across objfiles: if one
734 objfile gets freed, then the other has a broken C-V chain.
736 This code used to try to copy over the main type from TYPE to
737 *TYPEPTR if they were in different objfiles, but that's
738 wrong, too: TYPE may have a field list or member function
739 lists, which refer to types of their own, etc. etc. The
740 whole shebang would need to be copied over recursively; you
741 can't have inter-objfile pointers. The only thing to do is
742 to leave stub types as stub types, and look them up afresh by
743 name each time you encounter them. */
744 gdb_assert ((*typeptr)->objfile_owner () == type->objfile_owner ());
747 ntype = make_qualified_type (type, new_flags,
748 typeptr ? *typeptr : NULL);
750 if (typeptr != NULL)
751 *typeptr = ntype;
753 return ntype;
756 /* Make a 'restrict'-qualified version of TYPE. */
758 struct type *
759 make_restrict_type (struct type *type)
761 return make_qualified_type (type,
762 (type->instance_flags ()
763 | TYPE_INSTANCE_FLAG_RESTRICT),
764 NULL);
767 /* Make a type without const, volatile, or restrict. */
769 struct type *
770 make_unqualified_type (struct type *type)
772 return make_qualified_type (type,
773 (type->instance_flags ()
774 & ~(TYPE_INSTANCE_FLAG_CONST
775 | TYPE_INSTANCE_FLAG_VOLATILE
776 | TYPE_INSTANCE_FLAG_RESTRICT)),
777 NULL);
780 /* Make a '_Atomic'-qualified version of TYPE. */
782 struct type *
783 make_atomic_type (struct type *type)
785 return make_qualified_type (type,
786 (type->instance_flags ()
787 | TYPE_INSTANCE_FLAG_ATOMIC),
788 NULL);
791 /* Replace the contents of ntype with the type *type. This changes the
792 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
793 the changes are propogated to all types in the TYPE_CHAIN.
795 In order to build recursive types, it's inevitable that we'll need
796 to update types in place --- but this sort of indiscriminate
797 smashing is ugly, and needs to be replaced with something more
798 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
799 clear if more steps are needed. */
801 void
802 replace_type (struct type *ntype, struct type *type)
804 struct type *chain;
806 /* These two types had better be in the same objfile. Otherwise,
807 the assignment of one type's main type structure to the other
808 will produce a type with references to objects (names; field
809 lists; etc.) allocated on an objfile other than its own. */
810 gdb_assert (ntype->objfile_owner () == type->objfile_owner ());
812 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
814 /* The type length is not a part of the main type. Update it for
815 each type on the variant chain. */
816 chain = ntype;
819 /* Assert that this element of the chain has no address-class bits
820 set in its flags. Such type variants might have type lengths
821 which are supposed to be different from the non-address-class
822 variants. This assertion shouldn't ever be triggered because
823 symbol readers which do construct address-class variants don't
824 call replace_type(). */
825 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
827 TYPE_LENGTH (chain) = TYPE_LENGTH (type);
828 chain = TYPE_CHAIN (chain);
830 while (ntype != chain);
832 /* Assert that the two types have equivalent instance qualifiers.
833 This should be true for at least all of our debug readers. */
834 gdb_assert (ntype->instance_flags () == type->instance_flags ());
837 /* Implement direct support for MEMBER_TYPE in GNU C++.
838 May need to construct such a type if this is the first use.
839 The TYPE is the type of the member. The DOMAIN is the type
840 of the aggregate that the member belongs to. */
842 struct type *
843 lookup_memberptr_type (struct type *type, struct type *domain)
845 struct type *mtype;
847 mtype = alloc_type_copy (type);
848 smash_to_memberptr_type (mtype, domain, type);
849 return mtype;
852 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
854 struct type *
855 lookup_methodptr_type (struct type *to_type)
857 struct type *mtype;
859 mtype = alloc_type_copy (to_type);
860 smash_to_methodptr_type (mtype, to_type);
861 return mtype;
864 /* Allocate a stub method whose return type is TYPE. This apparently
865 happens for speed of symbol reading, since parsing out the
866 arguments to the method is cpu-intensive, the way we are doing it.
867 So, we will fill in arguments later. This always returns a fresh
868 type. */
870 struct type *
871 allocate_stub_method (struct type *type)
873 struct type *mtype;
875 mtype = alloc_type_copy (type);
876 mtype->set_code (TYPE_CODE_METHOD);
877 TYPE_LENGTH (mtype) = 1;
878 mtype->set_is_stub (true);
879 TYPE_TARGET_TYPE (mtype) = type;
880 /* TYPE_SELF_TYPE (mtype) = unknown yet */
881 return mtype;
884 /* See gdbtypes.h. */
886 bool
887 operator== (const dynamic_prop &l, const dynamic_prop &r)
889 if (l.kind () != r.kind ())
890 return false;
892 switch (l.kind ())
894 case PROP_UNDEFINED:
895 return true;
896 case PROP_CONST:
897 return l.const_val () == r.const_val ();
898 case PROP_ADDR_OFFSET:
899 case PROP_LOCEXPR:
900 case PROP_LOCLIST:
901 return l.baton () == r.baton ();
902 case PROP_VARIANT_PARTS:
903 return l.variant_parts () == r.variant_parts ();
904 case PROP_TYPE:
905 return l.original_type () == r.original_type ();
908 gdb_assert_not_reached ("unhandled dynamic_prop kind");
911 /* See gdbtypes.h. */
913 bool
914 operator== (const range_bounds &l, const range_bounds &r)
916 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
918 return (FIELD_EQ (low)
919 && FIELD_EQ (high)
920 && FIELD_EQ (flag_upper_bound_is_count)
921 && FIELD_EQ (flag_bound_evaluated)
922 && FIELD_EQ (bias));
924 #undef FIELD_EQ
927 /* Create a range type with a dynamic range from LOW_BOUND to
928 HIGH_BOUND, inclusive. See create_range_type for further details. */
930 struct type *
931 create_range_type (struct type *result_type, struct type *index_type,
932 const struct dynamic_prop *low_bound,
933 const struct dynamic_prop *high_bound,
934 LONGEST bias)
936 /* The INDEX_TYPE should be a type capable of holding the upper and lower
937 bounds, as such a zero sized, or void type makes no sense. */
938 gdb_assert (index_type->code () != TYPE_CODE_VOID);
939 gdb_assert (TYPE_LENGTH (index_type) > 0);
941 if (result_type == NULL)
942 result_type = alloc_type_copy (index_type);
943 result_type->set_code (TYPE_CODE_RANGE);
944 TYPE_TARGET_TYPE (result_type) = index_type;
945 if (index_type->is_stub ())
946 result_type->set_target_is_stub (true);
947 else
948 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
950 range_bounds *bounds
951 = (struct range_bounds *) TYPE_ZALLOC (result_type, sizeof (range_bounds));
952 bounds->low = *low_bound;
953 bounds->high = *high_bound;
954 bounds->bias = bias;
955 bounds->stride.set_const_val (0);
957 result_type->set_bounds (bounds);
959 if (index_type->code () == TYPE_CODE_FIXED_POINT)
960 result_type->set_is_unsigned (index_type->is_unsigned ());
961 /* Note that the signed-ness of a range type can't simply be copied
962 from the underlying type. Consider a case where the underlying
963 type is 'int', but the range type can hold 0..65535, and where
964 the range is further specified to fit into 16 bits. In this
965 case, if we copy the underlying type's sign, then reading some
966 range values will cause an unwanted sign extension. So, we have
967 some heuristics here instead. */
968 else if (low_bound->kind () == PROP_CONST && low_bound->const_val () >= 0)
969 result_type->set_is_unsigned (true);
970 /* Ada allows the declaration of range types whose upper bound is
971 less than the lower bound, so checking the lower bound is not
972 enough. Make sure we do not mark a range type whose upper bound
973 is negative as unsigned. */
974 if (high_bound->kind () == PROP_CONST && high_bound->const_val () < 0)
975 result_type->set_is_unsigned (false);
977 result_type->set_endianity_is_not_default
978 (index_type->endianity_is_not_default ());
980 return result_type;
983 /* See gdbtypes.h. */
985 struct type *
986 create_range_type_with_stride (struct type *result_type,
987 struct type *index_type,
988 const struct dynamic_prop *low_bound,
989 const struct dynamic_prop *high_bound,
990 LONGEST bias,
991 const struct dynamic_prop *stride,
992 bool byte_stride_p)
994 result_type = create_range_type (result_type, index_type, low_bound,
995 high_bound, bias);
997 gdb_assert (stride != nullptr);
998 result_type->bounds ()->stride = *stride;
999 result_type->bounds ()->flag_is_byte_stride = byte_stride_p;
1001 return result_type;
1006 /* Create a range type using either a blank type supplied in
1007 RESULT_TYPE, or creating a new type, inheriting the objfile from
1008 INDEX_TYPE.
1010 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
1011 to HIGH_BOUND, inclusive.
1013 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1014 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
1016 struct type *
1017 create_static_range_type (struct type *result_type, struct type *index_type,
1018 LONGEST low_bound, LONGEST high_bound)
1020 struct dynamic_prop low, high;
1022 low.set_const_val (low_bound);
1023 high.set_const_val (high_bound);
1025 result_type = create_range_type (result_type, index_type, &low, &high, 0);
1027 return result_type;
1030 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1031 are static, otherwise returns 0. */
1033 static bool
1034 has_static_range (const struct range_bounds *bounds)
1036 /* If the range doesn't have a defined stride then its stride field will
1037 be initialized to the constant 0. */
1038 return (bounds->low.kind () == PROP_CONST
1039 && bounds->high.kind () == PROP_CONST
1040 && bounds->stride.kind () == PROP_CONST);
1043 /* See gdbtypes.h. */
1045 gdb::optional<LONGEST>
1046 get_discrete_low_bound (struct type *type)
1048 type = check_typedef (type);
1049 switch (type->code ())
1051 case TYPE_CODE_RANGE:
1053 /* This function only works for ranges with a constant low bound. */
1054 if (type->bounds ()->low.kind () != PROP_CONST)
1055 return {};
1057 LONGEST low = type->bounds ()->low.const_val ();
1059 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM)
1061 gdb::optional<LONGEST> low_pos
1062 = discrete_position (TYPE_TARGET_TYPE (type), low);
1064 if (low_pos.has_value ())
1065 low = *low_pos;
1068 return low;
1071 case TYPE_CODE_ENUM:
1073 if (type->num_fields () > 0)
1075 /* The enums may not be sorted by value, so search all
1076 entries. */
1077 LONGEST low = type->field (0).loc_enumval ();
1079 for (int i = 0; i < type->num_fields (); i++)
1081 if (type->field (i).loc_enumval () < low)
1082 low = type->field (i).loc_enumval ();
1085 /* Set unsigned indicator if warranted. */
1086 if (low >= 0)
1087 type->set_is_unsigned (true);
1089 return low;
1091 else
1092 return 0;
1095 case TYPE_CODE_BOOL:
1096 return 0;
1098 case TYPE_CODE_INT:
1099 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
1100 return {};
1102 if (!type->is_unsigned ())
1103 return -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
1105 /* fall through */
1106 case TYPE_CODE_CHAR:
1107 return 0;
1109 default:
1110 return {};
1114 /* See gdbtypes.h. */
1116 gdb::optional<LONGEST>
1117 get_discrete_high_bound (struct type *type)
1119 type = check_typedef (type);
1120 switch (type->code ())
1122 case TYPE_CODE_RANGE:
1124 /* This function only works for ranges with a constant high bound. */
1125 if (type->bounds ()->high.kind () != PROP_CONST)
1126 return {};
1128 LONGEST high = type->bounds ()->high.const_val ();
1130 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM)
1132 gdb::optional<LONGEST> high_pos
1133 = discrete_position (TYPE_TARGET_TYPE (type), high);
1135 if (high_pos.has_value ())
1136 high = *high_pos;
1139 return high;
1142 case TYPE_CODE_ENUM:
1144 if (type->num_fields () > 0)
1146 /* The enums may not be sorted by value, so search all
1147 entries. */
1148 LONGEST high = type->field (0).loc_enumval ();
1150 for (int i = 0; i < type->num_fields (); i++)
1152 if (type->field (i).loc_enumval () > high)
1153 high = type->field (i).loc_enumval ();
1156 return high;
1158 else
1159 return -1;
1162 case TYPE_CODE_BOOL:
1163 return 1;
1165 case TYPE_CODE_INT:
1166 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
1167 return {};
1169 if (!type->is_unsigned ())
1171 LONGEST low = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
1172 return -low - 1;
1175 /* fall through */
1176 case TYPE_CODE_CHAR:
1178 /* This round-about calculation is to avoid shifting by
1179 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
1180 if TYPE_LENGTH (type) == sizeof (LONGEST). */
1181 LONGEST high = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
1182 return (high - 1) | high;
1185 default:
1186 return {};
1190 /* See gdbtypes.h. */
1192 bool
1193 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
1195 gdb::optional<LONGEST> low = get_discrete_low_bound (type);
1196 if (!low.has_value ())
1197 return false;
1199 gdb::optional<LONGEST> high = get_discrete_high_bound (type);
1200 if (!high.has_value ())
1201 return false;
1203 *lowp = *low;
1204 *highp = *high;
1206 return true;
1209 /* See gdbtypes.h */
1211 bool
1212 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
1214 struct type *index = type->index_type ();
1215 LONGEST low = 0;
1216 LONGEST high = 0;
1218 if (index == NULL)
1219 return false;
1221 if (!get_discrete_bounds (index, &low, &high))
1222 return false;
1224 if (low_bound)
1225 *low_bound = low;
1227 if (high_bound)
1228 *high_bound = high;
1230 return true;
1233 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1234 representation of a value of this type, save the corresponding
1235 position number in POS.
1237 Its differs from VAL only in the case of enumeration types. In
1238 this case, the position number of the value of the first listed
1239 enumeration literal is zero; the position number of the value of
1240 each subsequent enumeration literal is one more than that of its
1241 predecessor in the list.
1243 Return 1 if the operation was successful. Return zero otherwise,
1244 in which case the value of POS is unmodified.
1247 gdb::optional<LONGEST>
1248 discrete_position (struct type *type, LONGEST val)
1250 if (type->code () == TYPE_CODE_RANGE)
1251 type = TYPE_TARGET_TYPE (type);
1253 if (type->code () == TYPE_CODE_ENUM)
1255 int i;
1257 for (i = 0; i < type->num_fields (); i += 1)
1259 if (val == type->field (i).loc_enumval ())
1260 return i;
1263 /* Invalid enumeration value. */
1264 return {};
1266 else
1267 return val;
1270 /* If the array TYPE has static bounds calculate and update its
1271 size, then return true. Otherwise return false and leave TYPE
1272 unchanged. */
1274 static bool
1275 update_static_array_size (struct type *type)
1277 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1279 struct type *range_type = type->index_type ();
1281 if (type->dyn_prop (DYN_PROP_BYTE_STRIDE) == nullptr
1282 && has_static_range (range_type->bounds ())
1283 && (!type_not_associated (type)
1284 && !type_not_allocated (type)))
1286 LONGEST low_bound, high_bound;
1287 int stride;
1288 struct type *element_type;
1290 stride = type->bit_stride ();
1292 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
1293 low_bound = high_bound = 0;
1295 element_type = check_typedef (TYPE_TARGET_TYPE (type));
1296 /* Be careful when setting the array length. Ada arrays can be
1297 empty arrays with the high_bound being smaller than the low_bound.
1298 In such cases, the array length should be zero. */
1299 if (high_bound < low_bound)
1300 TYPE_LENGTH (type) = 0;
1301 else if (stride != 0)
1303 /* Ensure that the type length is always positive, even in the
1304 case where (for example in Fortran) we have a negative
1305 stride. It is possible to have a single element array with a
1306 negative stride in Fortran (this doesn't mean anything
1307 special, it's still just a single element array) so do
1308 consider that case when touching this code. */
1309 LONGEST element_count = std::abs (high_bound - low_bound + 1);
1310 TYPE_LENGTH (type)
1311 = ((std::abs (stride) * element_count) + 7) / 8;
1313 else
1314 TYPE_LENGTH (type) =
1315 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
1317 /* If this array's element is itself an array with a bit stride,
1318 then we want to update this array's bit stride to reflect the
1319 size of the sub-array. Otherwise, we'll end up using the
1320 wrong size when trying to find elements of the outer
1321 array. */
1322 if (element_type->code () == TYPE_CODE_ARRAY
1323 && TYPE_LENGTH (element_type) != 0
1324 && TYPE_FIELD_BITSIZE (element_type, 0) != 0
1325 && get_array_bounds (element_type, &low_bound, &high_bound)
1326 && high_bound >= low_bound)
1327 TYPE_FIELD_BITSIZE (type, 0)
1328 = ((high_bound - low_bound + 1)
1329 * TYPE_FIELD_BITSIZE (element_type, 0));
1331 return true;
1334 return false;
1337 /* Create an array type using either a blank type supplied in
1338 RESULT_TYPE, or creating a new type, inheriting the objfile from
1339 RANGE_TYPE.
1341 Elements will be of type ELEMENT_TYPE, the indices will be of type
1342 RANGE_TYPE.
1344 BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
1345 This byte stride property is added to the resulting array type
1346 as a DYN_PROP_BYTE_STRIDE. As a consequence, the BYTE_STRIDE_PROP
1347 argument can only be used to create types that are objfile-owned
1348 (see add_dyn_prop), meaning that either this function must be called
1349 with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
1351 BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
1352 If BIT_STRIDE is not zero, build a packed array type whose element
1353 size is BIT_STRIDE. Otherwise, ignore this parameter.
1355 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1356 sure it is TYPE_CODE_UNDEF before we bash it into an array
1357 type? */
1359 struct type *
1360 create_array_type_with_stride (struct type *result_type,
1361 struct type *element_type,
1362 struct type *range_type,
1363 struct dynamic_prop *byte_stride_prop,
1364 unsigned int bit_stride)
1366 if (byte_stride_prop != NULL
1367 && byte_stride_prop->kind () == PROP_CONST)
1369 /* The byte stride is actually not dynamic. Pretend we were
1370 called with bit_stride set instead of byte_stride_prop.
1371 This will give us the same result type, while avoiding
1372 the need to handle this as a special case. */
1373 bit_stride = byte_stride_prop->const_val () * 8;
1374 byte_stride_prop = NULL;
1377 if (result_type == NULL)
1378 result_type = alloc_type_copy (range_type);
1380 result_type->set_code (TYPE_CODE_ARRAY);
1381 TYPE_TARGET_TYPE (result_type) = element_type;
1383 result_type->set_num_fields (1);
1384 result_type->set_fields
1385 ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
1386 result_type->set_index_type (range_type);
1387 if (byte_stride_prop != NULL)
1388 result_type->add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop);
1389 else if (bit_stride > 0)
1390 TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
1392 if (!update_static_array_size (result_type))
1394 /* This type is dynamic and its length needs to be computed
1395 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1396 undefined by setting it to zero. Although we are not expected
1397 to trust TYPE_LENGTH in this case, setting the size to zero
1398 allows us to avoid allocating objects of random sizes in case
1399 we accidently do. */
1400 TYPE_LENGTH (result_type) = 0;
1403 /* TYPE_TARGET_STUB will take care of zero length arrays. */
1404 if (TYPE_LENGTH (result_type) == 0)
1405 result_type->set_target_is_stub (true);
1407 return result_type;
1410 /* Same as create_array_type_with_stride but with no bit_stride
1411 (BIT_STRIDE = 0), thus building an unpacked array. */
1413 struct type *
1414 create_array_type (struct type *result_type,
1415 struct type *element_type,
1416 struct type *range_type)
1418 return create_array_type_with_stride (result_type, element_type,
1419 range_type, NULL, 0);
1422 struct type *
1423 lookup_array_range_type (struct type *element_type,
1424 LONGEST low_bound, LONGEST high_bound)
1426 struct type *index_type;
1427 struct type *range_type;
1429 if (element_type->is_objfile_owned ())
1430 index_type = objfile_type (element_type->objfile_owner ())->builtin_int;
1431 else
1432 index_type = builtin_type (element_type->arch_owner ())->builtin_int;
1434 range_type = create_static_range_type (NULL, index_type,
1435 low_bound, high_bound);
1437 return create_array_type (NULL, element_type, range_type);
1440 /* Create a string type using either a blank type supplied in
1441 RESULT_TYPE, or creating a new type. String types are similar
1442 enough to array of char types that we can use create_array_type to
1443 build the basic type and then bash it into a string type.
1445 For fixed length strings, the range type contains 0 as the lower
1446 bound and the length of the string minus one as the upper bound.
1448 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1449 sure it is TYPE_CODE_UNDEF before we bash it into a string
1450 type? */
1452 struct type *
1453 create_string_type (struct type *result_type,
1454 struct type *string_char_type,
1455 struct type *range_type)
1457 result_type = create_array_type (result_type,
1458 string_char_type,
1459 range_type);
1460 result_type->set_code (TYPE_CODE_STRING);
1461 return result_type;
1464 struct type *
1465 lookup_string_range_type (struct type *string_char_type,
1466 LONGEST low_bound, LONGEST high_bound)
1468 struct type *result_type;
1470 result_type = lookup_array_range_type (string_char_type,
1471 low_bound, high_bound);
1472 result_type->set_code (TYPE_CODE_STRING);
1473 return result_type;
1476 struct type *
1477 create_set_type (struct type *result_type, struct type *domain_type)
1479 if (result_type == NULL)
1480 result_type = alloc_type_copy (domain_type);
1482 result_type->set_code (TYPE_CODE_SET);
1483 result_type->set_num_fields (1);
1484 result_type->set_fields
1485 ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
1487 if (!domain_type->is_stub ())
1489 LONGEST low_bound, high_bound, bit_length;
1491 if (!get_discrete_bounds (domain_type, &low_bound, &high_bound))
1492 low_bound = high_bound = 0;
1494 bit_length = high_bound - low_bound + 1;
1495 TYPE_LENGTH (result_type)
1496 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1497 if (low_bound >= 0)
1498 result_type->set_is_unsigned (true);
1500 result_type->field (0).set_type (domain_type);
1502 return result_type;
1505 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1506 and any array types nested inside it. */
1508 void
1509 make_vector_type (struct type *array_type)
1511 struct type *inner_array, *elt_type;
1513 /* Find the innermost array type, in case the array is
1514 multi-dimensional. */
1515 inner_array = array_type;
1516 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
1517 inner_array = TYPE_TARGET_TYPE (inner_array);
1519 elt_type = TYPE_TARGET_TYPE (inner_array);
1520 if (elt_type->code () == TYPE_CODE_INT)
1522 type_instance_flags flags
1523 = elt_type->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT;
1524 elt_type = make_qualified_type (elt_type, flags, NULL);
1525 TYPE_TARGET_TYPE (inner_array) = elt_type;
1528 array_type->set_is_vector (true);
1531 struct type *
1532 init_vector_type (struct type *elt_type, int n)
1534 struct type *array_type;
1536 array_type = lookup_array_range_type (elt_type, 0, n - 1);
1537 make_vector_type (array_type);
1538 return array_type;
1541 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1542 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1543 confusing. "self" is a common enough replacement for "this".
1544 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1545 TYPE_CODE_METHOD. */
1547 struct type *
1548 internal_type_self_type (struct type *type)
1550 switch (type->code ())
1552 case TYPE_CODE_METHODPTR:
1553 case TYPE_CODE_MEMBERPTR:
1554 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1555 return NULL;
1556 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1557 return TYPE_MAIN_TYPE (type)->type_specific.self_type;
1558 case TYPE_CODE_METHOD:
1559 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1560 return NULL;
1561 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1562 return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type;
1563 default:
1564 gdb_assert_not_reached ("bad type");
1568 /* Set the type of the class that TYPE belongs to.
1569 In c++ this is the class of "this".
1570 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1571 TYPE_CODE_METHOD. */
1573 void
1574 set_type_self_type (struct type *type, struct type *self_type)
1576 switch (type->code ())
1578 case TYPE_CODE_METHODPTR:
1579 case TYPE_CODE_MEMBERPTR:
1580 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1581 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE;
1582 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1583 TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type;
1584 break;
1585 case TYPE_CODE_METHOD:
1586 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1587 INIT_FUNC_SPECIFIC (type);
1588 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1589 TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type;
1590 break;
1591 default:
1592 gdb_assert_not_reached ("bad type");
1596 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1597 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1598 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1599 TYPE doesn't include the offset (that's the value of the MEMBER
1600 itself), but does include the structure type into which it points
1601 (for some reason).
1603 When "smashing" the type, we preserve the objfile that the old type
1604 pointed to, since we aren't changing where the type is actually
1605 allocated. */
1607 void
1608 smash_to_memberptr_type (struct type *type, struct type *self_type,
1609 struct type *to_type)
1611 smash_type (type);
1612 type->set_code (TYPE_CODE_MEMBERPTR);
1613 TYPE_TARGET_TYPE (type) = to_type;
1614 set_type_self_type (type, self_type);
1615 /* Assume that a data member pointer is the same size as a normal
1616 pointer. */
1617 TYPE_LENGTH (type) = gdbarch_ptr_bit (to_type->arch ()) / TARGET_CHAR_BIT;
1620 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1622 When "smashing" the type, we preserve the objfile that the old type
1623 pointed to, since we aren't changing where the type is actually
1624 allocated. */
1626 void
1627 smash_to_methodptr_type (struct type *type, struct type *to_type)
1629 smash_type (type);
1630 type->set_code (TYPE_CODE_METHODPTR);
1631 TYPE_TARGET_TYPE (type) = to_type;
1632 set_type_self_type (type, TYPE_SELF_TYPE (to_type));
1633 TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1636 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1637 METHOD just means `function that gets an extra "this" argument'.
1639 When "smashing" the type, we preserve the objfile that the old type
1640 pointed to, since we aren't changing where the type is actually
1641 allocated. */
1643 void
1644 smash_to_method_type (struct type *type, struct type *self_type,
1645 struct type *to_type, struct field *args,
1646 int nargs, int varargs)
1648 smash_type (type);
1649 type->set_code (TYPE_CODE_METHOD);
1650 TYPE_TARGET_TYPE (type) = to_type;
1651 set_type_self_type (type, self_type);
1652 type->set_fields (args);
1653 type->set_num_fields (nargs);
1654 if (varargs)
1655 type->set_has_varargs (true);
1656 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
1659 /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
1660 Since GCC PR debug/47510 DWARF provides associated information to detect the
1661 anonymous class linkage name from its typedef.
1663 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1664 apply it itself. */
1666 const char *
1667 type_name_or_error (struct type *type)
1669 struct type *saved_type = type;
1670 const char *name;
1671 struct objfile *objfile;
1673 type = check_typedef (type);
1675 name = type->name ();
1676 if (name != NULL)
1677 return name;
1679 name = saved_type->name ();
1680 objfile = saved_type->objfile_owner ();
1681 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1682 name ? name : "<anonymous>",
1683 objfile ? objfile_name (objfile) : "<arch>");
1686 /* Lookup a typedef or primitive type named NAME, visible in lexical
1687 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1688 suitably defined. */
1690 struct type *
1691 lookup_typename (const struct language_defn *language,
1692 const char *name,
1693 const struct block *block, int noerr)
1695 struct symbol *sym;
1697 sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
1698 language->la_language, NULL).symbol;
1699 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
1700 return sym->type ();
1702 if (noerr)
1703 return NULL;
1704 error (_("No type named %s."), name);
1707 struct type *
1708 lookup_unsigned_typename (const struct language_defn *language,
1709 const char *name)
1711 char *uns = (char *) alloca (strlen (name) + 10);
1713 strcpy (uns, "unsigned ");
1714 strcpy (uns + 9, name);
1715 return lookup_typename (language, uns, NULL, 0);
1718 struct type *
1719 lookup_signed_typename (const struct language_defn *language, const char *name)
1721 struct type *t;
1722 char *uns = (char *) alloca (strlen (name) + 8);
1724 strcpy (uns, "signed ");
1725 strcpy (uns + 7, name);
1726 t = lookup_typename (language, uns, NULL, 1);
1727 /* If we don't find "signed FOO" just try again with plain "FOO". */
1728 if (t != NULL)
1729 return t;
1730 return lookup_typename (language, name, NULL, 0);
1733 /* Lookup a structure type named "struct NAME",
1734 visible in lexical block BLOCK. */
1736 struct type *
1737 lookup_struct (const char *name, const struct block *block)
1739 struct symbol *sym;
1741 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1743 if (sym == NULL)
1745 error (_("No struct type named %s."), name);
1747 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1749 error (_("This context has class, union or enum %s, not a struct."),
1750 name);
1752 return (sym->type ());
1755 /* Lookup a union type named "union NAME",
1756 visible in lexical block BLOCK. */
1758 struct type *
1759 lookup_union (const char *name, const struct block *block)
1761 struct symbol *sym;
1762 struct type *t;
1764 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1766 if (sym == NULL)
1767 error (_("No union type named %s."), name);
1769 t = sym->type ();
1771 if (t->code () == TYPE_CODE_UNION)
1772 return t;
1774 /* If we get here, it's not a union. */
1775 error (_("This context has class, struct or enum %s, not a union."),
1776 name);
1779 /* Lookup an enum type named "enum NAME",
1780 visible in lexical block BLOCK. */
1782 struct type *
1783 lookup_enum (const char *name, const struct block *block)
1785 struct symbol *sym;
1787 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1788 if (sym == NULL)
1790 error (_("No enum type named %s."), name);
1792 if (sym->type ()->code () != TYPE_CODE_ENUM)
1794 error (_("This context has class, struct or union %s, not an enum."),
1795 name);
1797 return (sym->type ());
1800 /* Lookup a template type named "template NAME<TYPE>",
1801 visible in lexical block BLOCK. */
1803 struct type *
1804 lookup_template_type (const char *name, struct type *type,
1805 const struct block *block)
1807 struct symbol *sym;
1808 char *nam = (char *)
1809 alloca (strlen (name) + strlen (type->name ()) + 4);
1811 strcpy (nam, name);
1812 strcat (nam, "<");
1813 strcat (nam, type->name ());
1814 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
1816 sym = lookup_symbol (nam, block, VAR_DOMAIN, 0).symbol;
1818 if (sym == NULL)
1820 error (_("No template type named %s."), name);
1822 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1824 error (_("This context has class, union or enum %s, not a struct."),
1825 name);
1827 return (sym->type ());
1830 /* See gdbtypes.h. */
1832 struct_elt
1833 lookup_struct_elt (struct type *type, const char *name, int noerr)
1835 int i;
1837 for (;;)
1839 type = check_typedef (type);
1840 if (type->code () != TYPE_CODE_PTR
1841 && type->code () != TYPE_CODE_REF)
1842 break;
1843 type = TYPE_TARGET_TYPE (type);
1846 if (type->code () != TYPE_CODE_STRUCT
1847 && type->code () != TYPE_CODE_UNION)
1849 std::string type_name = type_to_string (type);
1850 error (_("Type %s is not a structure or union type."),
1851 type_name.c_str ());
1854 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
1856 const char *t_field_name = type->field (i).name ();
1858 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1860 return {&type->field (i), type->field (i).loc_bitpos ()};
1862 else if (!t_field_name || *t_field_name == '\0')
1864 struct_elt elt
1865 = lookup_struct_elt (type->field (i).type (), name, 1);
1866 if (elt.field != NULL)
1868 elt.offset += type->field (i).loc_bitpos ();
1869 return elt;
1874 /* OK, it's not in this class. Recursively check the baseclasses. */
1875 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1877 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1878 if (elt.field != NULL)
1879 return elt;
1882 if (noerr)
1883 return {nullptr, 0};
1885 std::string type_name = type_to_string (type);
1886 error (_("Type %s has no component named %s."), type_name.c_str (), name);
1889 /* See gdbtypes.h. */
1891 struct type *
1892 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1894 struct_elt elt = lookup_struct_elt (type, name, noerr);
1895 if (elt.field != NULL)
1896 return elt.field->type ();
1897 else
1898 return NULL;
1901 /* Return the largest number representable by unsigned integer type TYPE. */
1903 ULONGEST
1904 get_unsigned_type_max (struct type *type)
1906 unsigned int n;
1908 type = check_typedef (type);
1909 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
1910 gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
1912 /* Written this way to avoid overflow. */
1913 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1914 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1917 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1918 signed integer type TYPE. */
1920 void
1921 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1923 unsigned int n;
1925 type = check_typedef (type);
1926 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
1927 gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
1929 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1930 *min = -((ULONGEST) 1 << (n - 1));
1931 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1934 /* Return the largest value representable by pointer type TYPE. */
1936 CORE_ADDR
1937 get_pointer_type_max (struct type *type)
1939 unsigned int n;
1941 type = check_typedef (type);
1942 gdb_assert (type->code () == TYPE_CODE_PTR);
1943 gdb_assert (TYPE_LENGTH (type) <= sizeof (CORE_ADDR));
1945 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1946 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1949 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1950 cplus_stuff.vptr_fieldno.
1952 cplus_stuff is initialized to cplus_struct_default which does not
1953 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1954 designated initializers). We cope with that here. */
1957 internal_type_vptr_fieldno (struct type *type)
1959 type = check_typedef (type);
1960 gdb_assert (type->code () == TYPE_CODE_STRUCT
1961 || type->code () == TYPE_CODE_UNION);
1962 if (!HAVE_CPLUS_STRUCT (type))
1963 return -1;
1964 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1967 /* Set the value of cplus_stuff.vptr_fieldno. */
1969 void
1970 set_type_vptr_fieldno (struct type *type, int fieldno)
1972 type = check_typedef (type);
1973 gdb_assert (type->code () == TYPE_CODE_STRUCT
1974 || type->code () == TYPE_CODE_UNION);
1975 if (!HAVE_CPLUS_STRUCT (type))
1976 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1977 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1980 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1981 cplus_stuff.vptr_basetype. */
1983 struct type *
1984 internal_type_vptr_basetype (struct type *type)
1986 type = check_typedef (type);
1987 gdb_assert (type->code () == TYPE_CODE_STRUCT
1988 || type->code () == TYPE_CODE_UNION);
1989 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1990 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1993 /* Set the value of cplus_stuff.vptr_basetype. */
1995 void
1996 set_type_vptr_basetype (struct type *type, struct type *basetype)
1998 type = check_typedef (type);
1999 gdb_assert (type->code () == TYPE_CODE_STRUCT
2000 || type->code () == TYPE_CODE_UNION);
2001 if (!HAVE_CPLUS_STRUCT (type))
2002 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2003 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
2006 /* Lookup the vptr basetype/fieldno values for TYPE.
2007 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
2008 vptr_fieldno. Also, if found and basetype is from the same objfile,
2009 cache the results.
2010 If not found, return -1 and ignore BASETYPEP.
2011 Callers should be aware that in some cases (for example,
2012 the type or one of its baseclasses is a stub type and we are
2013 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
2014 this function will not be able to find the
2015 virtual function table pointer, and vptr_fieldno will remain -1 and
2016 vptr_basetype will remain NULL or incomplete. */
2019 get_vptr_fieldno (struct type *type, struct type **basetypep)
2021 type = check_typedef (type);
2023 if (TYPE_VPTR_FIELDNO (type) < 0)
2025 int i;
2027 /* We must start at zero in case the first (and only) baseclass
2028 is virtual (and hence we cannot share the table pointer). */
2029 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
2031 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2032 int fieldno;
2033 struct type *basetype;
2035 fieldno = get_vptr_fieldno (baseclass, &basetype);
2036 if (fieldno >= 0)
2038 /* If the type comes from a different objfile we can't cache
2039 it, it may have a different lifetime. PR 2384 */
2040 if (type->objfile_owner () == basetype->objfile_owner ())
2042 set_type_vptr_fieldno (type, fieldno);
2043 set_type_vptr_basetype (type, basetype);
2045 if (basetypep)
2046 *basetypep = basetype;
2047 return fieldno;
2051 /* Not found. */
2052 return -1;
2054 else
2056 if (basetypep)
2057 *basetypep = TYPE_VPTR_BASETYPE (type);
2058 return TYPE_VPTR_FIELDNO (type);
2062 static void
2063 stub_noname_complaint (void)
2065 complaint (_("stub type has NULL name"));
2068 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2069 attached to it, and that property has a non-constant value. */
2071 static int
2072 array_type_has_dynamic_stride (struct type *type)
2074 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2076 return (prop != NULL && prop->kind () != PROP_CONST);
2079 /* Worker for is_dynamic_type. */
2081 static int
2082 is_dynamic_type_internal (struct type *type, int top_level)
2084 type = check_typedef (type);
2086 /* We only want to recognize references at the outermost level. */
2087 if (top_level && type->code () == TYPE_CODE_REF)
2088 type = check_typedef (TYPE_TARGET_TYPE (type));
2090 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2091 dynamic, even if the type itself is statically defined.
2092 From a user's point of view, this may appear counter-intuitive;
2093 but it makes sense in this context, because the point is to determine
2094 whether any part of the type needs to be resolved before it can
2095 be exploited. */
2096 if (TYPE_DATA_LOCATION (type) != NULL
2097 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2098 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
2099 return 1;
2101 if (TYPE_ASSOCIATED_PROP (type))
2102 return 1;
2104 if (TYPE_ALLOCATED_PROP (type))
2105 return 1;
2107 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2108 if (prop != nullptr && prop->kind () != PROP_TYPE)
2109 return 1;
2111 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2112 return 1;
2114 switch (type->code ())
2116 case TYPE_CODE_RANGE:
2118 /* A range type is obviously dynamic if it has at least one
2119 dynamic bound. But also consider the range type to be
2120 dynamic when its subtype is dynamic, even if the bounds
2121 of the range type are static. It allows us to assume that
2122 the subtype of a static range type is also static. */
2123 return (!has_static_range (type->bounds ())
2124 || is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0));
2127 case TYPE_CODE_STRING:
2128 /* Strings are very much like an array of characters, and can be
2129 treated as one here. */
2130 case TYPE_CODE_ARRAY:
2132 gdb_assert (type->num_fields () == 1);
2134 /* The array is dynamic if either the bounds are dynamic... */
2135 if (is_dynamic_type_internal (type->index_type (), 0))
2136 return 1;
2137 /* ... or the elements it contains have a dynamic contents... */
2138 if (is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0))
2139 return 1;
2140 /* ... or if it has a dynamic stride... */
2141 if (array_type_has_dynamic_stride (type))
2142 return 1;
2143 return 0;
2146 case TYPE_CODE_STRUCT:
2147 case TYPE_CODE_UNION:
2149 int i;
2151 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2153 for (i = 0; i < type->num_fields (); ++i)
2155 /* Static fields can be ignored here. */
2156 if (field_is_static (&type->field (i)))
2157 continue;
2158 /* If the field has dynamic type, then so does TYPE. */
2159 if (is_dynamic_type_internal (type->field (i).type (), 0))
2160 return 1;
2161 /* If the field is at a fixed offset, then it is not
2162 dynamic. */
2163 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
2164 continue;
2165 /* Do not consider C++ virtual base types to be dynamic
2166 due to the field's offset being dynamic; these are
2167 handled via other means. */
2168 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2169 continue;
2170 return 1;
2173 break;
2176 return 0;
2179 /* See gdbtypes.h. */
2182 is_dynamic_type (struct type *type)
2184 return is_dynamic_type_internal (type, 1);
2187 static struct type *resolve_dynamic_type_internal
2188 (struct type *type, struct property_addr_info *addr_stack, int top_level);
2190 /* Given a dynamic range type (dyn_range_type) and a stack of
2191 struct property_addr_info elements, return a static version
2192 of that type.
2194 When RESOLVE_P is true then the returned static range is created by
2195 actually evaluating any dynamic properties within the range type, while
2196 when RESOLVE_P is false the returned static range has all of the bounds
2197 and stride information set to undefined. The RESOLVE_P set to false
2198 case will be used when evaluating a dynamic array that is not
2199 allocated, or not associated, i.e. the bounds information might not be
2200 initialized yet.
2202 RANK is the array rank for which we are resolving this range, and is a
2203 zero based count. The rank should never be negative.
2206 static struct type *
2207 resolve_dynamic_range (struct type *dyn_range_type,
2208 struct property_addr_info *addr_stack,
2209 int rank, bool resolve_p = true)
2211 CORE_ADDR value;
2212 struct type *static_range_type, *static_target_type;
2213 struct dynamic_prop low_bound, high_bound, stride;
2215 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
2216 gdb_assert (rank >= 0);
2218 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
2219 if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
2220 { (CORE_ADDR) rank }))
2221 low_bound.set_const_val (value);
2222 else
2223 low_bound.set_undefined ();
2225 prop = &dyn_range_type->bounds ()->high;
2226 if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
2227 { (CORE_ADDR) rank }))
2229 high_bound.set_const_val (value);
2231 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2232 high_bound.set_const_val
2233 (low_bound.const_val () + high_bound.const_val () - 1);
2235 else
2236 high_bound.set_undefined ();
2238 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2239 prop = &dyn_range_type->bounds ()->stride;
2240 if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
2241 { (CORE_ADDR) rank }))
2243 stride.set_const_val (value);
2245 /* If we have a bit stride that is not an exact number of bytes then
2246 I really don't think this is going to work with current GDB, the
2247 array indexing code in GDB seems to be pretty heavily tied to byte
2248 offsets right now. Assuming 8 bits in a byte. */
2249 struct gdbarch *gdbarch = dyn_range_type->arch ();
2250 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2251 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2252 error (_("bit strides that are not a multiple of the byte size "
2253 "are currently not supported"));
2255 else
2257 stride.set_undefined ();
2258 byte_stride_p = true;
2261 static_target_type
2262 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type),
2263 addr_stack, 0);
2264 LONGEST bias = dyn_range_type->bounds ()->bias;
2265 static_range_type = create_range_type_with_stride
2266 (copy_type (dyn_range_type), static_target_type,
2267 &low_bound, &high_bound, bias, &stride, byte_stride_p);
2268 static_range_type->bounds ()->flag_bound_evaluated = 1;
2269 return static_range_type;
2272 /* Helper function for resolve_dynamic_array_or_string. This function
2273 resolves the properties for a single array at RANK within a nested array
2274 of arrays structure. The RANK value is greater than or equal to 0, and
2275 starts at it's maximum value and goes down by 1 for each recursive call
2276 to this function. So, for a 3-dimensional array, the first call to this
2277 function has RANK == 2, then we call ourselves recursively with RANK ==
2278 1, than again with RANK == 0, and at that point we should return.
2280 TYPE is updated as the dynamic properties are resolved, and so, should
2281 be a copy of the dynamic type, rather than the original dynamic type
2282 itself.
2284 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2285 during the dynamic resolution.
2287 When RESOLVE_P is true then the dynamic properties of TYPE are
2288 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2289 instead we assume the array is not allocated/associated yet. */
2291 static struct type *
2292 resolve_dynamic_array_or_string_1 (struct type *type,
2293 struct property_addr_info *addr_stack,
2294 int rank, bool resolve_p)
2296 CORE_ADDR value;
2297 struct type *elt_type;
2298 struct type *range_type;
2299 struct type *ary_dim;
2300 struct dynamic_prop *prop;
2301 unsigned int bit_stride = 0;
2303 /* For dynamic type resolution strings can be treated like arrays of
2304 characters. */
2305 gdb_assert (type->code () == TYPE_CODE_ARRAY
2306 || type->code () == TYPE_CODE_STRING);
2308 /* As the rank is a zero based count we expect this to never be
2309 negative. */
2310 gdb_assert (rank >= 0);
2312 /* Resolve the allocated and associated properties before doing anything
2313 else. If an array is not allocated or not associated then (at least
2314 for Fortran) there is no guarantee that the data to define the upper
2315 bound, lower bound, or stride will be correct. If RESOLVE_P is
2316 already false at this point then this is not the first dimension of
2317 the array and a more outer dimension has already marked this array as
2318 not allocated/associated, as such we just ignore this property. This
2319 is fine as GDB only checks the allocated/associated on the outer most
2320 dimension of the array. */
2321 prop = TYPE_ALLOCATED_PROP (type);
2322 if (prop != NULL && resolve_p
2323 && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2325 prop->set_const_val (value);
2326 if (value == 0)
2327 resolve_p = false;
2330 prop = TYPE_ASSOCIATED_PROP (type);
2331 if (prop != NULL && resolve_p
2332 && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2334 prop->set_const_val (value);
2335 if (value == 0)
2336 resolve_p = false;
2339 range_type = check_typedef (type->index_type ());
2340 range_type
2341 = resolve_dynamic_range (range_type, addr_stack, rank, resolve_p);
2343 ary_dim = check_typedef (TYPE_TARGET_TYPE (type));
2344 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
2346 ary_dim = copy_type (ary_dim);
2347 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
2348 rank - 1, resolve_p);
2350 else
2351 elt_type = TYPE_TARGET_TYPE (type);
2353 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2354 if (prop != NULL && resolve_p)
2356 if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2358 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
2359 bit_stride = (unsigned int) (value * 8);
2361 else
2363 /* Could be a bug in our code, but it could also happen
2364 if the DWARF info is not correct. Issue a warning,
2365 and assume no byte/bit stride (leave bit_stride = 0). */
2366 warning (_("cannot determine array stride for type %s"),
2367 type->name () ? type->name () : "<no name>");
2370 else
2371 bit_stride = TYPE_FIELD_BITSIZE (type, 0);
2373 return create_array_type_with_stride (type, elt_type, range_type, NULL,
2374 bit_stride);
2377 /* Resolve an array or string type with dynamic properties, return a new
2378 type with the dynamic properties resolved to actual values. The
2379 ADDR_STACK represents the location of the object being resolved. */
2381 static struct type *
2382 resolve_dynamic_array_or_string (struct type *type,
2383 struct property_addr_info *addr_stack)
2385 CORE_ADDR value;
2386 int rank = 0;
2388 /* For dynamic type resolution strings can be treated like arrays of
2389 characters. */
2390 gdb_assert (type->code () == TYPE_CODE_ARRAY
2391 || type->code () == TYPE_CODE_STRING);
2393 type = copy_type (type);
2395 /* Resolve the rank property to get rank value. */
2396 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
2397 if (dwarf2_evaluate_property (prop, nullptr, addr_stack, &value))
2399 prop->set_const_val (value);
2400 rank = value;
2402 if (rank == 0)
2404 /* Rank is zero, if a variable is passed as an argument to a
2405 function. In this case the resolved type should not be an
2406 array, but should instead be that of an array element. */
2407 struct type *dynamic_array_type = type;
2408 type = copy_type (TYPE_TARGET_TYPE (dynamic_array_type));
2409 struct dynamic_prop_list *prop_list
2410 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2411 if (prop_list != nullptr)
2413 struct obstack *obstack
2414 = &type->objfile_owner ()->objfile_obstack;
2415 TYPE_MAIN_TYPE (type)->dyn_prop_list
2416 = copy_dynamic_prop_list (obstack, prop_list);
2418 return type;
2420 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2422 /* What would this even mean? A string with a dynamic rank
2423 greater than 1. */
2424 error (_("unable to handle string with dynamic rank greater than 1"));
2426 else if (rank > 1)
2428 /* Arrays with dynamic rank are initially just an array type
2429 with a target type that is the array element.
2431 However, now we know the rank of the array we need to build
2432 the array of arrays structure that GDB expects, that is we
2433 need an array type that has a target which is an array type,
2434 and so on, until eventually, we have the element type at the
2435 end of the chain. Create all the additional array types here
2436 by copying the top level array type. */
2437 struct type *element_type = TYPE_TARGET_TYPE (type);
2438 struct type *rank_type = type;
2439 for (int i = 1; i < rank; i++)
2441 TYPE_TARGET_TYPE (rank_type) = copy_type (rank_type);
2442 rank_type = TYPE_TARGET_TYPE (rank_type);
2444 TYPE_TARGET_TYPE (rank_type) = element_type;
2447 else
2449 rank = 1;
2451 for (struct type *tmp_type = check_typedef (TYPE_TARGET_TYPE (type));
2452 tmp_type->code () == TYPE_CODE_ARRAY;
2453 tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type)))
2454 ++rank;
2457 /* The rank that we calculated above is actually a count of the number of
2458 ranks. However, when we resolve the type of each individual array
2459 rank we should actually use a rank "offset", e.g. an array with a rank
2460 count of 1 (calculated above) will use the rank offset 0 in order to
2461 resolve the details of the first array dimension. As a result, we
2462 reduce the rank by 1 here. */
2463 --rank;
2465 return resolve_dynamic_array_or_string_1 (type, addr_stack, rank, true);
2468 /* Resolve dynamic bounds of members of the union TYPE to static
2469 bounds. ADDR_STACK is a stack of struct property_addr_info
2470 to be used if needed during the dynamic resolution. */
2472 static struct type *
2473 resolve_dynamic_union (struct type *type,
2474 struct property_addr_info *addr_stack)
2476 struct type *resolved_type;
2477 int i;
2478 unsigned int max_len = 0;
2480 gdb_assert (type->code () == TYPE_CODE_UNION);
2482 resolved_type = copy_type (type);
2483 resolved_type->set_fields
2484 ((struct field *)
2485 TYPE_ALLOC (resolved_type,
2486 resolved_type->num_fields () * sizeof (struct field)));
2487 memcpy (resolved_type->fields (),
2488 type->fields (),
2489 resolved_type->num_fields () * sizeof (struct field));
2490 for (i = 0; i < resolved_type->num_fields (); ++i)
2492 struct type *t;
2494 if (field_is_static (&type->field (i)))
2495 continue;
2497 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
2498 addr_stack, 0);
2499 resolved_type->field (i).set_type (t);
2501 struct type *real_type = check_typedef (t);
2502 if (TYPE_LENGTH (real_type) > max_len)
2503 max_len = TYPE_LENGTH (real_type);
2506 TYPE_LENGTH (resolved_type) = max_len;
2507 return resolved_type;
2510 /* See gdbtypes.h. */
2512 bool
2513 variant::matches (ULONGEST value, bool is_unsigned) const
2515 for (const discriminant_range &range : discriminants)
2516 if (range.contains (value, is_unsigned))
2517 return true;
2518 return false;
2521 static void
2522 compute_variant_fields_inner (struct type *type,
2523 struct property_addr_info *addr_stack,
2524 const variant_part &part,
2525 std::vector<bool> &flags);
2527 /* A helper function to determine which variant fields will be active.
2528 This handles both the variant's direct fields, and any variant
2529 parts embedded in this variant. TYPE is the type we're examining.
2530 ADDR_STACK holds information about the concrete object. VARIANT is
2531 the current variant to be handled. FLAGS is where the results are
2532 stored -- this function sets the Nth element in FLAGS if the
2533 corresponding field is enabled. ENABLED is whether this variant is
2534 enabled or not. */
2536 static void
2537 compute_variant_fields_recurse (struct type *type,
2538 struct property_addr_info *addr_stack,
2539 const variant &variant,
2540 std::vector<bool> &flags,
2541 bool enabled)
2543 for (int field = variant.first_field; field < variant.last_field; ++field)
2544 flags[field] = enabled;
2546 for (const variant_part &new_part : variant.parts)
2548 if (enabled)
2549 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2550 else
2552 for (const auto &sub_variant : new_part.variants)
2553 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2554 flags, enabled);
2559 /* A helper function to determine which variant fields will be active.
2560 This evaluates the discriminant, decides which variant (if any) is
2561 active, and then updates FLAGS to reflect which fields should be
2562 available. TYPE is the type we're examining. ADDR_STACK holds
2563 information about the concrete object. VARIANT is the current
2564 variant to be handled. FLAGS is where the results are stored --
2565 this function sets the Nth element in FLAGS if the corresponding
2566 field is enabled. */
2568 static void
2569 compute_variant_fields_inner (struct type *type,
2570 struct property_addr_info *addr_stack,
2571 const variant_part &part,
2572 std::vector<bool> &flags)
2574 /* Evaluate the discriminant. */
2575 gdb::optional<ULONGEST> discr_value;
2576 if (part.discriminant_index != -1)
2578 int idx = part.discriminant_index;
2580 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
2581 error (_("Cannot determine struct field location"
2582 " (invalid location kind)"));
2584 if (addr_stack->valaddr.data () != NULL)
2585 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2586 idx);
2587 else
2589 CORE_ADDR addr = (addr_stack->addr
2590 + (type->field (idx).loc_bitpos ()
2591 / TARGET_CHAR_BIT));
2593 LONGEST bitsize = TYPE_FIELD_BITSIZE (type, idx);
2594 LONGEST size = bitsize / 8;
2595 if (size == 0)
2596 size = TYPE_LENGTH (type->field (idx).type ());
2598 gdb_byte bits[sizeof (ULONGEST)];
2599 read_memory (addr, bits, size);
2601 LONGEST bitpos = (type->field (idx).loc_bitpos ()
2602 % TARGET_CHAR_BIT);
2604 discr_value = unpack_bits_as_long (type->field (idx).type (),
2605 bits, bitpos, bitsize);
2609 /* Go through each variant and see which applies. */
2610 const variant *default_variant = nullptr;
2611 const variant *applied_variant = nullptr;
2612 for (const auto &variant : part.variants)
2614 if (variant.is_default ())
2615 default_variant = &variant;
2616 else if (discr_value.has_value ()
2617 && variant.matches (*discr_value, part.is_unsigned))
2619 applied_variant = &variant;
2620 break;
2623 if (applied_variant == nullptr)
2624 applied_variant = default_variant;
2626 for (const auto &variant : part.variants)
2627 compute_variant_fields_recurse (type, addr_stack, variant,
2628 flags, applied_variant == &variant);
2631 /* Determine which variant fields are available in TYPE. The enabled
2632 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2633 about the concrete object. PARTS describes the top-level variant
2634 parts for this type. */
2636 static void
2637 compute_variant_fields (struct type *type,
2638 struct type *resolved_type,
2639 struct property_addr_info *addr_stack,
2640 const gdb::array_view<variant_part> &parts)
2642 /* Assume all fields are included by default. */
2643 std::vector<bool> flags (resolved_type->num_fields (), true);
2645 /* Now disable fields based on the variants that control them. */
2646 for (const auto &part : parts)
2647 compute_variant_fields_inner (type, addr_stack, part, flags);
2649 resolved_type->set_num_fields
2650 (std::count (flags.begin (), flags.end (), true));
2651 resolved_type->set_fields
2652 ((struct field *)
2653 TYPE_ALLOC (resolved_type,
2654 resolved_type->num_fields () * sizeof (struct field)));
2656 int out = 0;
2657 for (int i = 0; i < type->num_fields (); ++i)
2659 if (!flags[i])
2660 continue;
2662 resolved_type->field (out) = type->field (i);
2663 ++out;
2667 /* Resolve dynamic bounds of members of the struct TYPE to static
2668 bounds. ADDR_STACK is a stack of struct property_addr_info to
2669 be used if needed during the dynamic resolution. */
2671 static struct type *
2672 resolve_dynamic_struct (struct type *type,
2673 struct property_addr_info *addr_stack)
2675 struct type *resolved_type;
2676 int i;
2677 unsigned resolved_type_bit_length = 0;
2679 gdb_assert (type->code () == TYPE_CODE_STRUCT);
2681 resolved_type = copy_type (type);
2683 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2684 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
2686 compute_variant_fields (type, resolved_type, addr_stack,
2687 *variant_prop->variant_parts ());
2688 /* We want to leave the property attached, so that the Rust code
2689 can tell whether the type was originally an enum. */
2690 variant_prop->set_original_type (type);
2692 else
2694 resolved_type->set_fields
2695 ((struct field *)
2696 TYPE_ALLOC (resolved_type,
2697 resolved_type->num_fields () * sizeof (struct field)));
2698 if (type->num_fields () > 0)
2699 memcpy (resolved_type->fields (),
2700 type->fields (),
2701 resolved_type->num_fields () * sizeof (struct field));
2704 for (i = 0; i < resolved_type->num_fields (); ++i)
2706 unsigned new_bit_length;
2707 struct property_addr_info pinfo;
2709 if (field_is_static (&resolved_type->field (i)))
2710 continue;
2712 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
2714 struct dwarf2_property_baton baton;
2715 baton.property_type
2716 = lookup_pointer_type (resolved_type->field (i).type ());
2717 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
2719 struct dynamic_prop prop;
2720 prop.set_locexpr (&baton);
2722 CORE_ADDR addr;
2723 if (dwarf2_evaluate_property (&prop, nullptr, addr_stack, &addr,
2724 {addr_stack->addr}))
2725 resolved_type->field (i).set_loc_bitpos
2726 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
2729 /* As we know this field is not a static field, the field's
2730 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2731 this is the case, but only trigger a simple error rather
2732 than an internal error if that fails. While failing
2733 that verification indicates a bug in our code, the error
2734 is not severe enough to suggest to the user he stops
2735 his debugging session because of it. */
2736 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
2737 error (_("Cannot determine struct field location"
2738 " (invalid location kind)"));
2740 pinfo.type = check_typedef (resolved_type->field (i).type ());
2741 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
2742 pinfo.valaddr = addr_stack->valaddr;
2743 if (!pinfo.valaddr.empty ())
2744 pinfo.valaddr = pinfo.valaddr.slice (offset);
2745 pinfo.addr = addr_stack->addr + offset;
2746 pinfo.next = addr_stack;
2748 resolved_type->field (i).set_type
2749 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
2750 &pinfo, 0));
2751 gdb_assert (resolved_type->field (i).loc_kind ()
2752 == FIELD_LOC_KIND_BITPOS);
2754 new_bit_length = resolved_type->field (i).loc_bitpos ();
2755 if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
2756 new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
2757 else
2759 struct type *real_type
2760 = check_typedef (resolved_type->field (i).type ());
2762 new_bit_length += (TYPE_LENGTH (real_type) * TARGET_CHAR_BIT);
2765 /* Normally, we would use the position and size of the last field
2766 to determine the size of the enclosing structure. But GCC seems
2767 to be encoding the position of some fields incorrectly when
2768 the struct contains a dynamic field that is not placed last.
2769 So we compute the struct size based on the field that has
2770 the highest position + size - probably the best we can do. */
2771 if (new_bit_length > resolved_type_bit_length)
2772 resolved_type_bit_length = new_bit_length;
2775 /* The length of a type won't change for fortran, but it does for C and Ada.
2776 For fortran the size of dynamic fields might change over time but not the
2777 type length of the structure. If we adapt it, we run into problems
2778 when calculating the element offset for arrays of structs. */
2779 if (current_language->la_language != language_fortran)
2780 TYPE_LENGTH (resolved_type)
2781 = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
2783 /* The Ada language uses this field as a cache for static fixed types: reset
2784 it as RESOLVED_TYPE must have its own static fixed type. */
2785 TYPE_TARGET_TYPE (resolved_type) = NULL;
2787 return resolved_type;
2790 /* Worker for resolved_dynamic_type. */
2792 static struct type *
2793 resolve_dynamic_type_internal (struct type *type,
2794 struct property_addr_info *addr_stack,
2795 int top_level)
2797 struct type *real_type = check_typedef (type);
2798 struct type *resolved_type = nullptr;
2799 struct dynamic_prop *prop;
2800 CORE_ADDR value;
2802 if (!is_dynamic_type_internal (real_type, top_level))
2803 return type;
2805 gdb::optional<CORE_ADDR> type_length;
2806 prop = TYPE_DYNAMIC_LENGTH (type);
2807 if (prop != NULL
2808 && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2809 type_length = value;
2811 if (type->code () == TYPE_CODE_TYPEDEF)
2813 resolved_type = copy_type (type);
2814 TYPE_TARGET_TYPE (resolved_type)
2815 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack,
2816 top_level);
2818 else
2820 /* Before trying to resolve TYPE, make sure it is not a stub. */
2821 type = real_type;
2823 switch (type->code ())
2825 case TYPE_CODE_REF:
2827 struct property_addr_info pinfo;
2829 pinfo.type = check_typedef (TYPE_TARGET_TYPE (type));
2830 pinfo.valaddr = {};
2831 if (addr_stack->valaddr.data () != NULL)
2832 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2833 type);
2834 else
2835 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2836 pinfo.next = addr_stack;
2838 resolved_type = copy_type (type);
2839 TYPE_TARGET_TYPE (resolved_type)
2840 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
2841 &pinfo, top_level);
2842 break;
2845 case TYPE_CODE_STRING:
2846 /* Strings are very much like an array of characters, and can be
2847 treated as one here. */
2848 case TYPE_CODE_ARRAY:
2849 resolved_type = resolve_dynamic_array_or_string (type, addr_stack);
2850 break;
2852 case TYPE_CODE_RANGE:
2853 /* Pass 0 for the rank value here, which indicates this is a
2854 range for the first rank of an array. The assumption is that
2855 this rank value is not actually required for the resolution of
2856 the dynamic range, otherwise, we'd be resolving this range
2857 within the context of a dynamic array. */
2858 resolved_type = resolve_dynamic_range (type, addr_stack, 0);
2859 break;
2861 case TYPE_CODE_UNION:
2862 resolved_type = resolve_dynamic_union (type, addr_stack);
2863 break;
2865 case TYPE_CODE_STRUCT:
2866 resolved_type = resolve_dynamic_struct (type, addr_stack);
2867 break;
2871 if (resolved_type == nullptr)
2872 return type;
2874 if (type_length.has_value ())
2876 TYPE_LENGTH (resolved_type) = *type_length;
2877 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
2880 /* Resolve data_location attribute. */
2881 prop = TYPE_DATA_LOCATION (resolved_type);
2882 if (prop != NULL
2883 && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
2885 /* Start of Fortran hack. See comment in f-lang.h for what is going
2886 on here.*/
2887 if (current_language->la_language == language_fortran
2888 && resolved_type->code () == TYPE_CODE_ARRAY)
2889 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2890 value);
2891 /* End of Fortran hack. */
2892 prop->set_const_val (value);
2895 return resolved_type;
2898 /* See gdbtypes.h */
2900 struct type *
2901 resolve_dynamic_type (struct type *type,
2902 gdb::array_view<const gdb_byte> valaddr,
2903 CORE_ADDR addr)
2905 struct property_addr_info pinfo
2906 = {check_typedef (type), valaddr, addr, NULL};
2908 return resolve_dynamic_type_internal (type, &pinfo, 1);
2911 /* See gdbtypes.h */
2913 dynamic_prop *
2914 type::dyn_prop (dynamic_prop_node_kind prop_kind) const
2916 dynamic_prop_list *node = this->main_type->dyn_prop_list;
2918 while (node != NULL)
2920 if (node->prop_kind == prop_kind)
2921 return &node->prop;
2922 node = node->next;
2924 return NULL;
2927 /* See gdbtypes.h */
2929 void
2930 type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
2932 struct dynamic_prop_list *temp;
2934 gdb_assert (this->is_objfile_owned ());
2936 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
2937 struct dynamic_prop_list);
2938 temp->prop_kind = prop_kind;
2939 temp->prop = prop;
2940 temp->next = this->main_type->dyn_prop_list;
2942 this->main_type->dyn_prop_list = temp;
2945 /* See gdbtypes.h. */
2947 void
2948 type::remove_dyn_prop (dynamic_prop_node_kind kind)
2950 struct dynamic_prop_list *prev_node, *curr_node;
2952 curr_node = this->main_type->dyn_prop_list;
2953 prev_node = NULL;
2955 while (NULL != curr_node)
2957 if (curr_node->prop_kind == kind)
2959 /* Update the linked list but don't free anything.
2960 The property was allocated on objstack and it is not known
2961 if we are on top of it. Nevertheless, everything is released
2962 when the complete objstack is freed. */
2963 if (NULL == prev_node)
2964 this->main_type->dyn_prop_list = curr_node->next;
2965 else
2966 prev_node->next = curr_node->next;
2968 return;
2971 prev_node = curr_node;
2972 curr_node = curr_node->next;
2976 /* Find the real type of TYPE. This function returns the real type,
2977 after removing all layers of typedefs, and completing opaque or stub
2978 types. Completion changes the TYPE argument, but stripping of
2979 typedefs does not.
2981 Instance flags (e.g. const/volatile) are preserved as typedefs are
2982 stripped. If necessary a new qualified form of the underlying type
2983 is created.
2985 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
2986 not been computed and we're either in the middle of reading symbols, or
2987 there was no name for the typedef in the debug info.
2989 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2990 QUITs in the symbol reading code can also throw.
2991 Thus this function can throw an exception.
2993 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2994 the target type.
2996 If this is a stubbed struct (i.e. declared as struct foo *), see if
2997 we can find a full definition in some other file. If so, copy this
2998 definition, so we can use it in future. There used to be a comment
2999 (but not any code) that if we don't find a full definition, we'd
3000 set a flag so we don't spend time in the future checking the same
3001 type. That would be a mistake, though--we might load in more
3002 symbols which contain a full definition for the type. */
3004 struct type *
3005 check_typedef (struct type *type)
3007 struct type *orig_type = type;
3009 gdb_assert (type);
3011 /* While we're removing typedefs, we don't want to lose qualifiers.
3012 E.g., const/volatile. */
3013 type_instance_flags instance_flags = type->instance_flags ();
3015 while (type->code () == TYPE_CODE_TYPEDEF)
3017 if (!TYPE_TARGET_TYPE (type))
3019 const char *name;
3020 struct symbol *sym;
3022 /* It is dangerous to call lookup_symbol if we are currently
3023 reading a symtab. Infinite recursion is one danger. */
3024 if (currently_reading_symtab)
3025 return make_qualified_type (type, instance_flags, NULL);
3027 name = type->name ();
3028 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
3029 VAR_DOMAIN as appropriate? */
3030 if (name == NULL)
3032 stub_noname_complaint ();
3033 return make_qualified_type (type, instance_flags, NULL);
3035 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
3036 if (sym)
3037 TYPE_TARGET_TYPE (type) = sym->type ();
3038 else /* TYPE_CODE_UNDEF */
3039 TYPE_TARGET_TYPE (type) = alloc_type_arch (type->arch ());
3041 type = TYPE_TARGET_TYPE (type);
3043 /* Preserve the instance flags as we traverse down the typedef chain.
3045 Handling address spaces/classes is nasty, what do we do if there's a
3046 conflict?
3047 E.g., what if an outer typedef marks the type as class_1 and an inner
3048 typedef marks the type as class_2?
3049 This is the wrong place to do such error checking. We leave it to
3050 the code that created the typedef in the first place to flag the
3051 error. We just pick the outer address space (akin to letting the
3052 outer cast in a chain of casting win), instead of assuming
3053 "it can't happen". */
3055 const type_instance_flags ALL_SPACES
3056 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3057 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3058 const type_instance_flags ALL_CLASSES
3059 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3061 type_instance_flags new_instance_flags = type->instance_flags ();
3063 /* Treat code vs data spaces and address classes separately. */
3064 if ((instance_flags & ALL_SPACES) != 0)
3065 new_instance_flags &= ~ALL_SPACES;
3066 if ((instance_flags & ALL_CLASSES) != 0)
3067 new_instance_flags &= ~ALL_CLASSES;
3069 instance_flags |= new_instance_flags;
3073 /* If this is a struct/class/union with no fields, then check
3074 whether a full definition exists somewhere else. This is for
3075 systems where a type definition with no fields is issued for such
3076 types, instead of identifying them as stub types in the first
3077 place. */
3079 if (TYPE_IS_OPAQUE (type)
3080 && opaque_type_resolution
3081 && !currently_reading_symtab)
3083 const char *name = type->name ();
3084 struct type *newtype;
3086 if (name == NULL)
3088 stub_noname_complaint ();
3089 return make_qualified_type (type, instance_flags, NULL);
3091 newtype = lookup_transparent_type (name);
3093 if (newtype)
3095 /* If the resolved type and the stub are in the same
3096 objfile, then replace the stub type with the real deal.
3097 But if they're in separate objfiles, leave the stub
3098 alone; we'll just look up the transparent type every time
3099 we call check_typedef. We can't create pointers between
3100 types allocated to different objfiles, since they may
3101 have different lifetimes. Trying to copy NEWTYPE over to
3102 TYPE's objfile is pointless, too, since you'll have to
3103 move over any other types NEWTYPE refers to, which could
3104 be an unbounded amount of stuff. */
3105 if (newtype->objfile_owner () == type->objfile_owner ())
3106 type = make_qualified_type (newtype, type->instance_flags (), type);
3107 else
3108 type = newtype;
3111 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3112 types. */
3113 else if (type->is_stub () && !currently_reading_symtab)
3115 const char *name = type->name ();
3116 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3117 as appropriate? */
3118 struct symbol *sym;
3120 if (name == NULL)
3122 stub_noname_complaint ();
3123 return make_qualified_type (type, instance_flags, NULL);
3125 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
3126 if (sym)
3128 /* Same as above for opaque types, we can replace the stub
3129 with the complete type only if they are in the same
3130 objfile. */
3131 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3132 type = make_qualified_type (sym->type (),
3133 type->instance_flags (), type);
3134 else
3135 type = sym->type ();
3139 if (type->target_is_stub ())
3141 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
3143 if (target_type->is_stub () || target_type->target_is_stub ())
3145 /* Nothing we can do. */
3147 else if (type->code () == TYPE_CODE_RANGE)
3149 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
3150 type->set_target_is_stub (false);
3152 else if (type->code () == TYPE_CODE_ARRAY
3153 && update_static_array_size (type))
3154 type->set_target_is_stub (false);
3157 type = make_qualified_type (type, instance_flags, NULL);
3159 /* Cache TYPE_LENGTH for future use. */
3160 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
3162 return type;
3165 /* Parse a type expression in the string [P..P+LENGTH). If an error
3166 occurs, silently return a void type. */
3168 static struct type *
3169 safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
3171 struct ui_file *saved_gdb_stderr;
3172 struct type *type = NULL; /* Initialize to keep gcc happy. */
3174 /* Suppress error messages. */
3175 saved_gdb_stderr = gdb_stderr;
3176 gdb_stderr = &null_stream;
3178 /* Call parse_and_eval_type() without fear of longjmp()s. */
3181 type = parse_and_eval_type (p, length);
3183 catch (const gdb_exception_error &except)
3185 type = builtin_type (gdbarch)->builtin_void;
3188 /* Stop suppressing error messages. */
3189 gdb_stderr = saved_gdb_stderr;
3191 return type;
3194 /* Ugly hack to convert method stubs into method types.
3196 He ain't kiddin'. This demangles the name of the method into a
3197 string including argument types, parses out each argument type,
3198 generates a string casting a zero to that type, evaluates the
3199 string, and stuffs the resulting type into an argtype vector!!!
3200 Then it knows the type of the whole function (including argument
3201 types for overloading), which info used to be in the stab's but was
3202 removed to hack back the space required for them. */
3204 static void
3205 check_stub_method (struct type *type, int method_id, int signature_id)
3207 struct gdbarch *gdbarch = type->arch ();
3208 struct fn_field *f;
3209 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3210 gdb::unique_xmalloc_ptr<char> demangled_name
3211 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3212 char *argtypetext, *p;
3213 int depth = 0, argcount = 1;
3214 struct field *argtypes;
3215 struct type *mtype;
3217 /* Make sure we got back a function string that we can use. */
3218 if (demangled_name)
3219 p = strchr (demangled_name.get (), '(');
3220 else
3221 p = NULL;
3223 if (demangled_name == NULL || p == NULL)
3224 error (_("Internal: Cannot demangle mangled name `%s'."),
3225 mangled_name);
3227 /* Now, read in the parameters that define this type. */
3228 p += 1;
3229 argtypetext = p;
3230 while (*p)
3232 if (*p == '(' || *p == '<')
3234 depth += 1;
3236 else if (*p == ')' || *p == '>')
3238 depth -= 1;
3240 else if (*p == ',' && depth == 0)
3242 argcount += 1;
3245 p += 1;
3248 /* If we read one argument and it was ``void'', don't count it. */
3249 if (startswith (argtypetext, "(void)"))
3250 argcount -= 1;
3252 /* We need one extra slot, for the THIS pointer. */
3254 argtypes = (struct field *)
3255 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
3256 p = argtypetext;
3258 /* Add THIS pointer for non-static methods. */
3259 f = TYPE_FN_FIELDLIST1 (type, method_id);
3260 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3261 argcount = 0;
3262 else
3264 argtypes[0].set_type (lookup_pointer_type (type));
3265 argcount = 1;
3268 if (*p != ')') /* () means no args, skip while. */
3270 depth = 0;
3271 while (*p)
3273 if (depth <= 0 && (*p == ',' || *p == ')'))
3275 /* Avoid parsing of ellipsis, they will be handled below.
3276 Also avoid ``void'' as above. */
3277 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3278 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3280 argtypes[argcount].set_type
3281 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3282 argcount += 1;
3284 argtypetext = p + 1;
3287 if (*p == '(' || *p == '<')
3289 depth += 1;
3291 else if (*p == ')' || *p == '>')
3293 depth -= 1;
3296 p += 1;
3300 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3302 /* Now update the old "stub" type into a real type. */
3303 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3304 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3305 We want a method (TYPE_CODE_METHOD). */
3306 smash_to_method_type (mtype, type, TYPE_TARGET_TYPE (mtype),
3307 argtypes, argcount, p[-2] == '.');
3308 mtype->set_is_stub (false);
3309 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3312 /* This is the external interface to check_stub_method, above. This
3313 function unstubs all of the signatures for TYPE's METHOD_ID method
3314 name. After calling this function TYPE_FN_FIELD_STUB will be
3315 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3316 correct.
3318 This function unfortunately can not die until stabs do. */
3320 void
3321 check_stub_method_group (struct type *type, int method_id)
3323 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3324 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3326 for (int j = 0; j < len; j++)
3328 if (TYPE_FN_FIELD_STUB (f, j))
3329 check_stub_method (type, method_id, j);
3333 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3334 const struct cplus_struct_type cplus_struct_default = { };
3336 void
3337 allocate_cplus_struct_type (struct type *type)
3339 if (HAVE_CPLUS_STRUCT (type))
3340 /* Structure was already allocated. Nothing more to do. */
3341 return;
3343 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3344 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3345 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
3346 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3347 set_type_vptr_fieldno (type, -1);
3350 const struct gnat_aux_type gnat_aux_default =
3351 { NULL };
3353 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3354 and allocate the associated gnat-specific data. The gnat-specific
3355 data is also initialized to gnat_aux_default. */
3357 void
3358 allocate_gnat_aux_type (struct type *type)
3360 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3361 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3362 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
3363 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3366 /* Helper function to initialize a newly allocated type. Set type code
3367 to CODE and initialize the type-specific fields accordingly. */
3369 static void
3370 set_type_code (struct type *type, enum type_code code)
3372 type->set_code (code);
3374 switch (code)
3376 case TYPE_CODE_STRUCT:
3377 case TYPE_CODE_UNION:
3378 case TYPE_CODE_NAMESPACE:
3379 INIT_CPLUS_SPECIFIC (type);
3380 break;
3381 case TYPE_CODE_FLT:
3382 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
3383 break;
3384 case TYPE_CODE_FUNC:
3385 INIT_FUNC_SPECIFIC (type);
3386 break;
3387 case TYPE_CODE_FIXED_POINT:
3388 INIT_FIXED_POINT_SPECIFIC (type);
3389 break;
3393 /* Helper function to verify floating-point format and size.
3394 BIT is the type size in bits; if BIT equals -1, the size is
3395 determined by the floatformat. Returns size to be used. */
3397 static int
3398 verify_floatformat (int bit, const struct floatformat *floatformat)
3400 gdb_assert (floatformat != NULL);
3402 if (bit == -1)
3403 bit = floatformat->totalsize;
3405 gdb_assert (bit >= 0);
3406 gdb_assert (bit >= floatformat->totalsize);
3408 return bit;
3411 /* Return the floating-point format for a floating-point variable of
3412 type TYPE. */
3414 const struct floatformat *
3415 floatformat_from_type (const struct type *type)
3417 gdb_assert (type->code () == TYPE_CODE_FLT);
3418 gdb_assert (TYPE_FLOATFORMAT (type));
3419 return TYPE_FLOATFORMAT (type);
3422 /* Helper function to initialize the standard scalar types.
3424 If NAME is non-NULL, then it is used to initialize the type name.
3425 Note that NAME is not copied; it is required to have a lifetime at
3426 least as long as OBJFILE. */
3428 struct type *
3429 init_type (struct objfile *objfile, enum type_code code, int bit,
3430 const char *name)
3432 struct type *type;
3434 type = alloc_type (objfile);
3435 set_type_code (type, code);
3436 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
3437 TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
3438 type->set_name (name);
3440 return type;
3443 /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
3444 to use with variables that have no debug info. NAME is the type
3445 name. */
3447 static struct type *
3448 init_nodebug_var_type (struct objfile *objfile, const char *name)
3450 return init_type (objfile, TYPE_CODE_ERROR, 0, name);
3453 /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
3454 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3455 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3457 struct type *
3458 init_integer_type (struct objfile *objfile,
3459 int bit, int unsigned_p, const char *name)
3461 struct type *t;
3463 t = init_type (objfile, TYPE_CODE_INT, bit, name);
3464 if (unsigned_p)
3465 t->set_is_unsigned (true);
3467 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3468 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3469 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3471 return t;
3474 /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
3475 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3476 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3478 struct type *
3479 init_character_type (struct objfile *objfile,
3480 int bit, int unsigned_p, const char *name)
3482 struct type *t;
3484 t = init_type (objfile, TYPE_CODE_CHAR, bit, name);
3485 if (unsigned_p)
3486 t->set_is_unsigned (true);
3488 return t;
3491 /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
3492 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3493 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3495 struct type *
3496 init_boolean_type (struct objfile *objfile,
3497 int bit, int unsigned_p, const char *name)
3499 struct type *t;
3501 t = init_type (objfile, TYPE_CODE_BOOL, bit, name);
3502 if (unsigned_p)
3503 t->set_is_unsigned (true);
3505 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3506 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3507 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3509 return t;
3512 /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
3513 BIT is the type size in bits; if BIT equals -1, the size is
3514 determined by the floatformat. NAME is the type name. Set the
3515 TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
3516 to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
3517 order of the objfile's architecture is used. */
3519 struct type *
3520 init_float_type (struct objfile *objfile,
3521 int bit, const char *name,
3522 const struct floatformat **floatformats,
3523 enum bfd_endian byte_order)
3525 if (byte_order == BFD_ENDIAN_UNKNOWN)
3527 struct gdbarch *gdbarch = objfile->arch ();
3528 byte_order = gdbarch_byte_order (gdbarch);
3530 const struct floatformat *fmt = floatformats[byte_order];
3531 struct type *t;
3533 bit = verify_floatformat (bit, fmt);
3534 t = init_type (objfile, TYPE_CODE_FLT, bit, name);
3535 TYPE_FLOATFORMAT (t) = fmt;
3537 return t;
3540 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
3541 BIT is the type size in bits. NAME is the type name. */
3543 struct type *
3544 init_decfloat_type (struct objfile *objfile, int bit, const char *name)
3546 struct type *t;
3548 t = init_type (objfile, TYPE_CODE_DECFLOAT, bit, name);
3549 return t;
3552 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3554 bool
3555 can_create_complex_type (struct type *target_type)
3557 return (target_type->code () == TYPE_CODE_INT
3558 || target_type->code () == TYPE_CODE_FLT);
3561 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3562 name. TARGET_TYPE is the component type. */
3564 struct type *
3565 init_complex_type (const char *name, struct type *target_type)
3567 struct type *t;
3569 gdb_assert (can_create_complex_type (target_type));
3571 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3573 if (name == nullptr && target_type->name () != nullptr)
3575 char *new_name
3576 = (char *) TYPE_ALLOC (target_type,
3577 strlen (target_type->name ())
3578 + strlen ("_Complex ") + 1);
3579 strcpy (new_name, "_Complex ");
3580 strcat (new_name, target_type->name ());
3581 name = new_name;
3584 t = alloc_type_copy (target_type);
3585 set_type_code (t, TYPE_CODE_COMPLEX);
3586 TYPE_LENGTH (t) = 2 * TYPE_LENGTH (target_type);
3587 t->set_name (name);
3589 TYPE_TARGET_TYPE (t) = target_type;
3590 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3593 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3596 /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
3597 BIT is the pointer type size in bits. NAME is the type name.
3598 TARGET_TYPE is the pointer target type. Always sets the pointer type's
3599 TYPE_UNSIGNED flag. */
3601 struct type *
3602 init_pointer_type (struct objfile *objfile,
3603 int bit, const char *name, struct type *target_type)
3605 struct type *t;
3607 t = init_type (objfile, TYPE_CODE_PTR, bit, name);
3608 TYPE_TARGET_TYPE (t) = target_type;
3609 t->set_is_unsigned (true);
3610 return t;
3613 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3614 BIT is the pointer type size in bits.
3615 UNSIGNED_P should be nonzero if the type is unsigned.
3616 NAME is the type name. */
3618 struct type *
3619 init_fixed_point_type (struct objfile *objfile,
3620 int bit, int unsigned_p, const char *name)
3622 struct type *t;
3624 t = init_type (objfile, TYPE_CODE_FIXED_POINT, bit, name);
3625 if (unsigned_p)
3626 t->set_is_unsigned (true);
3628 return t;
3631 /* See gdbtypes.h. */
3633 unsigned
3634 type_raw_align (struct type *type)
3636 if (type->align_log2 != 0)
3637 return 1 << (type->align_log2 - 1);
3638 return 0;
3641 /* See gdbtypes.h. */
3643 unsigned
3644 type_align (struct type *type)
3646 /* Check alignment provided in the debug information. */
3647 unsigned raw_align = type_raw_align (type);
3648 if (raw_align != 0)
3649 return raw_align;
3651 /* Allow the architecture to provide an alignment. */
3652 ULONGEST align = gdbarch_type_align (type->arch (), type);
3653 if (align != 0)
3654 return align;
3656 switch (type->code ())
3658 case TYPE_CODE_PTR:
3659 case TYPE_CODE_FUNC:
3660 case TYPE_CODE_FLAGS:
3661 case TYPE_CODE_INT:
3662 case TYPE_CODE_RANGE:
3663 case TYPE_CODE_FLT:
3664 case TYPE_CODE_ENUM:
3665 case TYPE_CODE_REF:
3666 case TYPE_CODE_RVALUE_REF:
3667 case TYPE_CODE_CHAR:
3668 case TYPE_CODE_BOOL:
3669 case TYPE_CODE_DECFLOAT:
3670 case TYPE_CODE_METHODPTR:
3671 case TYPE_CODE_MEMBERPTR:
3672 align = type_length_units (check_typedef (type));
3673 break;
3675 case TYPE_CODE_ARRAY:
3676 case TYPE_CODE_COMPLEX:
3677 case TYPE_CODE_TYPEDEF:
3678 align = type_align (TYPE_TARGET_TYPE (type));
3679 break;
3681 case TYPE_CODE_STRUCT:
3682 case TYPE_CODE_UNION:
3684 int number_of_non_static_fields = 0;
3685 for (unsigned i = 0; i < type->num_fields (); ++i)
3687 if (!field_is_static (&type->field (i)))
3689 number_of_non_static_fields++;
3690 ULONGEST f_align = type_align (type->field (i).type ());
3691 if (f_align == 0)
3693 /* Don't pretend we know something we don't. */
3694 align = 0;
3695 break;
3697 if (f_align > align)
3698 align = f_align;
3701 /* A struct with no fields, or with only static fields has an
3702 alignment of 1. */
3703 if (number_of_non_static_fields == 0)
3704 align = 1;
3706 break;
3708 case TYPE_CODE_SET:
3709 case TYPE_CODE_STRING:
3710 /* Not sure what to do here, and these can't appear in C or C++
3711 anyway. */
3712 break;
3714 case TYPE_CODE_VOID:
3715 align = 1;
3716 break;
3718 case TYPE_CODE_ERROR:
3719 case TYPE_CODE_METHOD:
3720 default:
3721 break;
3724 if ((align & (align - 1)) != 0)
3726 /* Not a power of 2, so pass. */
3727 align = 0;
3730 return align;
3733 /* See gdbtypes.h. */
3735 bool
3736 set_type_align (struct type *type, ULONGEST align)
3738 /* Must be a power of 2. Zero is ok. */
3739 gdb_assert ((align & (align - 1)) == 0);
3741 unsigned result = 0;
3742 while (align != 0)
3744 ++result;
3745 align >>= 1;
3748 if (result >= (1 << TYPE_ALIGN_BITS))
3749 return false;
3751 type->align_log2 = result;
3752 return true;
3756 /* Queries on types. */
3759 can_dereference (struct type *t)
3761 /* FIXME: Should we return true for references as well as
3762 pointers? */
3763 t = check_typedef (t);
3764 return
3765 (t != NULL
3766 && t->code () == TYPE_CODE_PTR
3767 && TYPE_TARGET_TYPE (t)->code () != TYPE_CODE_VOID);
3771 is_integral_type (struct type *t)
3773 t = check_typedef (t);
3774 return
3775 ((t != NULL)
3776 && !is_fixed_point_type (t)
3777 && ((t->code () == TYPE_CODE_INT)
3778 || (t->code () == TYPE_CODE_ENUM)
3779 || (t->code () == TYPE_CODE_FLAGS)
3780 || (t->code () == TYPE_CODE_CHAR)
3781 || (t->code () == TYPE_CODE_RANGE)
3782 || (t->code () == TYPE_CODE_BOOL)));
3786 is_floating_type (struct type *t)
3788 t = check_typedef (t);
3789 return
3790 ((t != NULL)
3791 && ((t->code () == TYPE_CODE_FLT)
3792 || (t->code () == TYPE_CODE_DECFLOAT)));
3795 /* Return true if TYPE is scalar. */
3798 is_scalar_type (struct type *type)
3800 type = check_typedef (type);
3802 if (is_fixed_point_type (type))
3803 return 0; /* Implemented as a scalar, but more like a floating point. */
3805 switch (type->code ())
3807 case TYPE_CODE_ARRAY:
3808 case TYPE_CODE_STRUCT:
3809 case TYPE_CODE_UNION:
3810 case TYPE_CODE_SET:
3811 case TYPE_CODE_STRING:
3812 return 0;
3813 default:
3814 return 1;
3818 /* Return true if T is scalar, or a composite type which in practice has
3819 the memory layout of a scalar type. E.g., an array or struct with only
3820 one scalar element inside it, or a union with only scalar elements. */
3823 is_scalar_type_recursive (struct type *t)
3825 t = check_typedef (t);
3827 if (is_scalar_type (t))
3828 return 1;
3829 /* Are we dealing with an array or string of known dimensions? */
3830 else if ((t->code () == TYPE_CODE_ARRAY
3831 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3832 && t->index_type ()->code () == TYPE_CODE_RANGE)
3834 LONGEST low_bound, high_bound;
3835 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
3837 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3838 return (high_bound == low_bound
3839 && is_scalar_type_recursive (elt_type));
3840 else
3841 return 0;
3843 /* Are we dealing with a struct with one element? */
3844 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3845 return is_scalar_type_recursive (t->field (0).type ());
3846 else if (t->code () == TYPE_CODE_UNION)
3848 int i, n = t->num_fields ();
3850 /* If all elements of the union are scalar, then the union is scalar. */
3851 for (i = 0; i < n; i++)
3852 if (!is_scalar_type_recursive (t->field (i).type ()))
3853 return 0;
3855 return 1;
3858 return 0;
3861 /* Return true is T is a class or a union. False otherwise. */
3864 class_or_union_p (const struct type *t)
3866 return (t->code () == TYPE_CODE_STRUCT
3867 || t->code () == TYPE_CODE_UNION);
3870 /* A helper function which returns true if types A and B represent the
3871 "same" class type. This is true if the types have the same main
3872 type, or the same name. */
3875 class_types_same_p (const struct type *a, const struct type *b)
3877 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3878 || (a->name () && b->name ()
3879 && !strcmp (a->name (), b->name ())));
3882 /* If BASE is an ancestor of DCLASS return the distance between them.
3883 otherwise return -1;
3886 class A {};
3887 class B: public A {};
3888 class C: public B {};
3889 class D: C {};
3891 distance_to_ancestor (A, A, 0) = 0
3892 distance_to_ancestor (A, B, 0) = 1
3893 distance_to_ancestor (A, C, 0) = 2
3894 distance_to_ancestor (A, D, 0) = 3
3896 If PUBLIC is 1 then only public ancestors are considered,
3897 and the function returns the distance only if BASE is a public ancestor
3898 of DCLASS.
3901 distance_to_ancestor (A, D, 1) = -1. */
3903 static int
3904 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3906 int i;
3907 int d;
3909 base = check_typedef (base);
3910 dclass = check_typedef (dclass);
3912 if (class_types_same_p (base, dclass))
3913 return 0;
3915 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3917 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3918 continue;
3920 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3921 if (d >= 0)
3922 return 1 + d;
3925 return -1;
3928 /* Check whether BASE is an ancestor or base class or DCLASS
3929 Return 1 if so, and 0 if not.
3930 Note: If BASE and DCLASS are of the same type, this function
3931 will return 1. So for some class A, is_ancestor (A, A) will
3932 return 1. */
3935 is_ancestor (struct type *base, struct type *dclass)
3937 return distance_to_ancestor (base, dclass, 0) >= 0;
3940 /* Like is_ancestor, but only returns true when BASE is a public
3941 ancestor of DCLASS. */
3944 is_public_ancestor (struct type *base, struct type *dclass)
3946 return distance_to_ancestor (base, dclass, 1) >= 0;
3949 /* A helper function for is_unique_ancestor. */
3951 static int
3952 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3953 int *offset,
3954 const gdb_byte *valaddr, int embedded_offset,
3955 CORE_ADDR address, struct value *val)
3957 int i, count = 0;
3959 base = check_typedef (base);
3960 dclass = check_typedef (dclass);
3962 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3964 struct type *iter;
3965 int this_offset;
3967 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3969 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3970 address, val);
3972 if (class_types_same_p (base, iter))
3974 /* If this is the first subclass, set *OFFSET and set count
3975 to 1. Otherwise, if this is at the same offset as
3976 previous instances, do nothing. Otherwise, increment
3977 count. */
3978 if (*offset == -1)
3980 *offset = this_offset;
3981 count = 1;
3983 else if (this_offset == *offset)
3985 /* Nothing. */
3987 else
3988 ++count;
3990 else
3991 count += is_unique_ancestor_worker (base, iter, offset,
3992 valaddr,
3993 embedded_offset + this_offset,
3994 address, val);
3997 return count;
4000 /* Like is_ancestor, but only returns true if BASE is a unique base
4001 class of the type of VAL. */
4004 is_unique_ancestor (struct type *base, struct value *val)
4006 int offset = -1;
4008 return is_unique_ancestor_worker (base, value_type (val), &offset,
4009 value_contents_for_printing (val).data (),
4010 value_embedded_offset (val),
4011 value_address (val), val) == 1;
4014 /* See gdbtypes.h. */
4016 enum bfd_endian
4017 type_byte_order (const struct type *type)
4019 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
4020 if (type->endianity_is_not_default ())
4022 if (byteorder == BFD_ENDIAN_BIG)
4023 return BFD_ENDIAN_LITTLE;
4024 else
4026 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
4027 return BFD_ENDIAN_BIG;
4031 return byteorder;
4034 /* See gdbtypes.h. */
4036 bool
4037 is_nocall_function (const struct type *type)
4039 gdb_assert (type->code () == TYPE_CODE_FUNC
4040 || type->code () == TYPE_CODE_METHOD);
4042 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
4046 /* Overload resolution. */
4048 /* Return the sum of the rank of A with the rank of B. */
4050 struct rank
4051 sum_ranks (struct rank a, struct rank b)
4053 struct rank c;
4054 c.rank = a.rank + b.rank;
4055 c.subrank = a.subrank + b.subrank;
4056 return c;
4059 /* Compare rank A and B and return:
4060 0 if a = b
4061 1 if a is better than b
4062 -1 if b is better than a. */
4065 compare_ranks (struct rank a, struct rank b)
4067 if (a.rank == b.rank)
4069 if (a.subrank == b.subrank)
4070 return 0;
4071 if (a.subrank < b.subrank)
4072 return 1;
4073 if (a.subrank > b.subrank)
4074 return -1;
4077 if (a.rank < b.rank)
4078 return 1;
4080 /* a.rank > b.rank */
4081 return -1;
4084 /* Functions for overload resolution begin here. */
4086 /* Compare two badness vectors A and B and return the result.
4087 0 => A and B are identical
4088 1 => A and B are incomparable
4089 2 => A is better than B
4090 3 => A is worse than B */
4093 compare_badness (const badness_vector &a, const badness_vector &b)
4095 int i;
4096 int tmp;
4097 /* Any positives in comparison? */
4098 bool found_pos = false;
4099 /* Any negatives in comparison? */
4100 bool found_neg = false;
4101 /* Did A have any INVALID_CONVERSION entries. */
4102 bool a_invalid = false;
4103 /* Did B have any INVALID_CONVERSION entries. */
4104 bool b_invalid = false;
4106 /* differing sizes => incomparable */
4107 if (a.size () != b.size ())
4108 return 1;
4110 /* Subtract b from a */
4111 for (i = 0; i < a.size (); i++)
4113 tmp = compare_ranks (b[i], a[i]);
4114 if (tmp > 0)
4115 found_pos = true;
4116 else if (tmp < 0)
4117 found_neg = true;
4118 if (a[i].rank >= INVALID_CONVERSION)
4119 a_invalid = true;
4120 if (b[i].rank >= INVALID_CONVERSION)
4121 b_invalid = true;
4124 /* B will only be considered better than or incomparable to A if
4125 they both have invalid entries, or if neither does. That is, if
4126 A has only valid entries, and B has an invalid entry, then A will
4127 be considered better than B, even if B happens to be better for
4128 some parameter. */
4129 if (a_invalid != b_invalid)
4131 if (a_invalid)
4132 return 3; /* A > B */
4133 return 2; /* A < B */
4135 else if (found_pos)
4137 if (found_neg)
4138 return 1; /* incomparable */
4139 else
4140 return 3; /* A > B */
4142 else
4143 /* no positives */
4145 if (found_neg)
4146 return 2; /* A < B */
4147 else
4148 return 0; /* A == B */
4152 /* Rank a function by comparing its parameter types (PARMS), to the
4153 types of an argument list (ARGS). Return the badness vector. This
4154 has ARGS.size() + 1 entries. */
4156 badness_vector
4157 rank_function (gdb::array_view<type *> parms,
4158 gdb::array_view<value *> args)
4160 /* add 1 for the length-match rank. */
4161 badness_vector bv;
4162 bv.reserve (1 + args.size ());
4164 /* First compare the lengths of the supplied lists.
4165 If there is a mismatch, set it to a high value. */
4167 /* pai/1997-06-03 FIXME: when we have debug info about default
4168 arguments and ellipsis parameter lists, we should consider those
4169 and rank the length-match more finely. */
4171 bv.push_back ((args.size () != parms.size ())
4172 ? LENGTH_MISMATCH_BADNESS
4173 : EXACT_MATCH_BADNESS);
4175 /* Now rank all the parameters of the candidate function. */
4176 size_t min_len = std::min (parms.size (), args.size ());
4178 for (size_t i = 0; i < min_len; i++)
4179 bv.push_back (rank_one_type (parms[i], value_type (args[i]),
4180 args[i]));
4182 /* If more arguments than parameters, add dummy entries. */
4183 for (size_t i = min_len; i < args.size (); i++)
4184 bv.push_back (TOO_FEW_PARAMS_BADNESS);
4186 return bv;
4189 /* Compare the names of two integer types, assuming that any sign
4190 qualifiers have been checked already. We do it this way because
4191 there may be an "int" in the name of one of the types. */
4193 static int
4194 integer_types_same_name_p (const char *first, const char *second)
4196 int first_p, second_p;
4198 /* If both are shorts, return 1; if neither is a short, keep
4199 checking. */
4200 first_p = (strstr (first, "short") != NULL);
4201 second_p = (strstr (second, "short") != NULL);
4202 if (first_p && second_p)
4203 return 1;
4204 if (first_p || second_p)
4205 return 0;
4207 /* Likewise for long. */
4208 first_p = (strstr (first, "long") != NULL);
4209 second_p = (strstr (second, "long") != NULL);
4210 if (first_p && second_p)
4211 return 1;
4212 if (first_p || second_p)
4213 return 0;
4215 /* Likewise for char. */
4216 first_p = (strstr (first, "char") != NULL);
4217 second_p = (strstr (second, "char") != NULL);
4218 if (first_p && second_p)
4219 return 1;
4220 if (first_p || second_p)
4221 return 0;
4223 /* They must both be ints. */
4224 return 1;
4227 /* Compares type A to type B. Returns true if they represent the same
4228 type, false otherwise. */
4230 bool
4231 types_equal (struct type *a, struct type *b)
4233 /* Identical type pointers. */
4234 /* However, this still doesn't catch all cases of same type for b
4235 and a. The reason is that builtin types are different from
4236 the same ones constructed from the object. */
4237 if (a == b)
4238 return true;
4240 /* Resolve typedefs */
4241 if (a->code () == TYPE_CODE_TYPEDEF)
4242 a = check_typedef (a);
4243 if (b->code () == TYPE_CODE_TYPEDEF)
4244 b = check_typedef (b);
4246 /* Check if identical after resolving typedefs. */
4247 if (a == b)
4248 return true;
4250 /* If after resolving typedefs a and b are not of the same type
4251 code then they are not equal. */
4252 if (a->code () != b->code ())
4253 return false;
4255 /* If a and b are both pointers types or both reference types then
4256 they are equal of the same type iff the objects they refer to are
4257 of the same type. */
4258 if (a->code () == TYPE_CODE_PTR
4259 || a->code () == TYPE_CODE_REF)
4260 return types_equal (TYPE_TARGET_TYPE (a),
4261 TYPE_TARGET_TYPE (b));
4263 /* Well, damnit, if the names are exactly the same, I'll say they
4264 are exactly the same. This happens when we generate method
4265 stubs. The types won't point to the same address, but they
4266 really are the same. */
4268 if (a->name () && b->name ()
4269 && strcmp (a->name (), b->name ()) == 0)
4270 return true;
4272 /* Two function types are equal if their argument and return types
4273 are equal. */
4274 if (a->code () == TYPE_CODE_FUNC)
4276 int i;
4278 if (a->num_fields () != b->num_fields ())
4279 return false;
4281 if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
4282 return false;
4284 for (i = 0; i < a->num_fields (); ++i)
4285 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4286 return false;
4288 return true;
4291 return false;
4294 /* Deep comparison of types. */
4296 /* An entry in the type-equality bcache. */
4298 struct type_equality_entry
4300 type_equality_entry (struct type *t1, struct type *t2)
4301 : type1 (t1),
4302 type2 (t2)
4306 struct type *type1, *type2;
4309 /* A helper function to compare two strings. Returns true if they are
4310 the same, false otherwise. Handles NULLs properly. */
4312 static bool
4313 compare_maybe_null_strings (const char *s, const char *t)
4315 if (s == NULL || t == NULL)
4316 return s == t;
4317 return strcmp (s, t) == 0;
4320 /* A helper function for check_types_worklist that checks two types for
4321 "deep" equality. Returns true if the types are considered the
4322 same, false otherwise. */
4324 static bool
4325 check_types_equal (struct type *type1, struct type *type2,
4326 std::vector<type_equality_entry> *worklist)
4328 type1 = check_typedef (type1);
4329 type2 = check_typedef (type2);
4331 if (type1 == type2)
4332 return true;
4334 if (type1->code () != type2->code ()
4335 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
4336 || type1->is_unsigned () != type2->is_unsigned ()
4337 || type1->has_no_signedness () != type2->has_no_signedness ()
4338 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4339 || type1->has_varargs () != type2->has_varargs ()
4340 || type1->is_vector () != type2->is_vector ()
4341 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4342 || type1->instance_flags () != type2->instance_flags ()
4343 || type1->num_fields () != type2->num_fields ())
4344 return false;
4346 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4347 return false;
4348 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4349 return false;
4351 if (type1->code () == TYPE_CODE_RANGE)
4353 if (*type1->bounds () != *type2->bounds ())
4354 return false;
4356 else
4358 int i;
4360 for (i = 0; i < type1->num_fields (); ++i)
4362 const struct field *field1 = &type1->field (i);
4363 const struct field *field2 = &type2->field (i);
4365 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
4366 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
4367 || field1->loc_kind () != field2->loc_kind ())
4368 return false;
4369 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4370 return false;
4371 switch (field1->loc_kind ())
4373 case FIELD_LOC_KIND_BITPOS:
4374 if (field1->loc_bitpos () != field2->loc_bitpos ())
4375 return false;
4376 break;
4377 case FIELD_LOC_KIND_ENUMVAL:
4378 if (field1->loc_enumval () != field2->loc_enumval ())
4379 return false;
4380 /* Don't compare types of enum fields, because they don't
4381 have a type. */
4382 continue;
4383 case FIELD_LOC_KIND_PHYSADDR:
4384 if (field1->loc_physaddr () != field2->loc_physaddr ())
4385 return false;
4386 break;
4387 case FIELD_LOC_KIND_PHYSNAME:
4388 if (!compare_maybe_null_strings (field1->loc_physname (),
4389 field2->loc_physname ()))
4390 return false;
4391 break;
4392 case FIELD_LOC_KIND_DWARF_BLOCK:
4394 struct dwarf2_locexpr_baton *block1, *block2;
4396 block1 = field1->loc_dwarf_block ();
4397 block2 = field2->loc_dwarf_block ();
4398 if (block1->per_cu != block2->per_cu
4399 || block1->size != block2->size
4400 || memcmp (block1->data, block2->data, block1->size) != 0)
4401 return false;
4403 break;
4404 default:
4405 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
4406 "%d by check_types_equal"),
4407 field1->loc_kind ());
4410 worklist->emplace_back (field1->type (), field2->type ());
4414 if (TYPE_TARGET_TYPE (type1) != NULL)
4416 if (TYPE_TARGET_TYPE (type2) == NULL)
4417 return false;
4419 worklist->emplace_back (TYPE_TARGET_TYPE (type1),
4420 TYPE_TARGET_TYPE (type2));
4422 else if (TYPE_TARGET_TYPE (type2) != NULL)
4423 return false;
4425 return true;
4428 /* Check types on a worklist for equality. Returns false if any pair
4429 is not equal, true if they are all considered equal. */
4431 static bool
4432 check_types_worklist (std::vector<type_equality_entry> *worklist,
4433 gdb::bcache *cache)
4435 while (!worklist->empty ())
4437 bool added;
4439 struct type_equality_entry entry = std::move (worklist->back ());
4440 worklist->pop_back ();
4442 /* If the type pair has already been visited, we know it is
4443 ok. */
4444 cache->insert (&entry, sizeof (entry), &added);
4445 if (!added)
4446 continue;
4448 if (!check_types_equal (entry.type1, entry.type2, worklist))
4449 return false;
4452 return true;
4455 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4456 "deep comparison". Otherwise return false. */
4458 bool
4459 types_deeply_equal (struct type *type1, struct type *type2)
4461 std::vector<type_equality_entry> worklist;
4463 gdb_assert (type1 != NULL && type2 != NULL);
4465 /* Early exit for the simple case. */
4466 if (type1 == type2)
4467 return true;
4469 gdb::bcache cache;
4470 worklist.emplace_back (type1, type2);
4471 return check_types_worklist (&worklist, &cache);
4474 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4475 Otherwise return one. */
4478 type_not_allocated (const struct type *type)
4480 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4482 return (prop != nullptr && prop->kind () == PROP_CONST
4483 && prop->const_val () == 0);
4486 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4487 Otherwise return one. */
4490 type_not_associated (const struct type *type)
4492 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4494 return (prop != nullptr && prop->kind () == PROP_CONST
4495 && prop->const_val () == 0);
4498 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4500 static struct rank
4501 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4503 struct rank rank = {0,0};
4505 switch (arg->code ())
4507 case TYPE_CODE_PTR:
4509 /* Allowed pointer conversions are:
4510 (a) pointer to void-pointer conversion. */
4511 if (TYPE_TARGET_TYPE (parm)->code () == TYPE_CODE_VOID)
4512 return VOID_PTR_CONVERSION_BADNESS;
4514 /* (b) pointer to ancestor-pointer conversion. */
4515 rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
4516 TYPE_TARGET_TYPE (arg),
4518 if (rank.subrank >= 0)
4519 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4521 return INCOMPATIBLE_TYPE_BADNESS;
4522 case TYPE_CODE_ARRAY:
4524 struct type *t1 = TYPE_TARGET_TYPE (parm);
4525 struct type *t2 = TYPE_TARGET_TYPE (arg);
4527 if (types_equal (t1, t2))
4529 /* Make sure they are CV equal. */
4530 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4531 rank.subrank |= CV_CONVERSION_CONST;
4532 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4533 rank.subrank |= CV_CONVERSION_VOLATILE;
4534 if (rank.subrank != 0)
4535 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4536 return EXACT_MATCH_BADNESS;
4538 return INCOMPATIBLE_TYPE_BADNESS;
4540 case TYPE_CODE_FUNC:
4541 return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
4542 case TYPE_CODE_INT:
4543 if (value != NULL && value_type (value)->code () == TYPE_CODE_INT)
4545 if (value_as_long (value) == 0)
4547 /* Null pointer conversion: allow it to be cast to a pointer.
4548 [4.10.1 of C++ standard draft n3290] */
4549 return NULL_POINTER_CONVERSION_BADNESS;
4551 else
4553 /* If type checking is disabled, allow the conversion. */
4554 if (!strict_type_checking)
4555 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4558 /* fall through */
4559 case TYPE_CODE_ENUM:
4560 case TYPE_CODE_FLAGS:
4561 case TYPE_CODE_CHAR:
4562 case TYPE_CODE_RANGE:
4563 case TYPE_CODE_BOOL:
4564 default:
4565 return INCOMPATIBLE_TYPE_BADNESS;
4569 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4571 static struct rank
4572 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4574 switch (arg->code ())
4576 case TYPE_CODE_PTR:
4577 case TYPE_CODE_ARRAY:
4578 return rank_one_type (TYPE_TARGET_TYPE (parm),
4579 TYPE_TARGET_TYPE (arg), NULL);
4580 default:
4581 return INCOMPATIBLE_TYPE_BADNESS;
4585 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4587 static struct rank
4588 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4590 switch (arg->code ())
4592 case TYPE_CODE_PTR: /* funcptr -> func */
4593 return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
4594 default:
4595 return INCOMPATIBLE_TYPE_BADNESS;
4599 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4601 static struct rank
4602 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4604 switch (arg->code ())
4606 case TYPE_CODE_INT:
4607 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
4609 /* Deal with signed, unsigned, and plain chars and
4610 signed and unsigned ints. */
4611 if (parm->has_no_signedness ())
4613 /* This case only for character types. */
4614 if (arg->has_no_signedness ())
4615 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4616 else /* signed/unsigned char -> plain char */
4617 return INTEGER_CONVERSION_BADNESS;
4619 else if (parm->is_unsigned ())
4621 if (arg->is_unsigned ())
4623 /* unsigned int -> unsigned int, or
4624 unsigned long -> unsigned long */
4625 if (integer_types_same_name_p (parm->name (),
4626 arg->name ()))
4627 return EXACT_MATCH_BADNESS;
4628 else if (integer_types_same_name_p (arg->name (),
4629 "int")
4630 && integer_types_same_name_p (parm->name (),
4631 "long"))
4632 /* unsigned int -> unsigned long */
4633 return INTEGER_PROMOTION_BADNESS;
4634 else
4635 /* unsigned long -> unsigned int */
4636 return INTEGER_CONVERSION_BADNESS;
4638 else
4640 if (integer_types_same_name_p (arg->name (),
4641 "long")
4642 && integer_types_same_name_p (parm->name (),
4643 "int"))
4644 /* signed long -> unsigned int */
4645 return INTEGER_CONVERSION_BADNESS;
4646 else
4647 /* signed int/long -> unsigned int/long */
4648 return INTEGER_CONVERSION_BADNESS;
4651 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4653 if (integer_types_same_name_p (parm->name (),
4654 arg->name ()))
4655 return EXACT_MATCH_BADNESS;
4656 else if (integer_types_same_name_p (arg->name (),
4657 "int")
4658 && integer_types_same_name_p (parm->name (),
4659 "long"))
4660 return INTEGER_PROMOTION_BADNESS;
4661 else
4662 return INTEGER_CONVERSION_BADNESS;
4664 else
4665 return INTEGER_CONVERSION_BADNESS;
4667 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4668 return INTEGER_PROMOTION_BADNESS;
4669 else
4670 return INTEGER_CONVERSION_BADNESS;
4671 case TYPE_CODE_ENUM:
4672 case TYPE_CODE_FLAGS:
4673 case TYPE_CODE_CHAR:
4674 case TYPE_CODE_RANGE:
4675 case TYPE_CODE_BOOL:
4676 if (arg->is_declared_class ())
4677 return INCOMPATIBLE_TYPE_BADNESS;
4678 return INTEGER_PROMOTION_BADNESS;
4679 case TYPE_CODE_FLT:
4680 return INT_FLOAT_CONVERSION_BADNESS;
4681 case TYPE_CODE_PTR:
4682 return NS_POINTER_CONVERSION_BADNESS;
4683 default:
4684 return INCOMPATIBLE_TYPE_BADNESS;
4688 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4690 static struct rank
4691 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4693 switch (arg->code ())
4695 case TYPE_CODE_INT:
4696 case TYPE_CODE_CHAR:
4697 case TYPE_CODE_RANGE:
4698 case TYPE_CODE_BOOL:
4699 case TYPE_CODE_ENUM:
4700 if (parm->is_declared_class () || arg->is_declared_class ())
4701 return INCOMPATIBLE_TYPE_BADNESS;
4702 return INTEGER_CONVERSION_BADNESS;
4703 case TYPE_CODE_FLT:
4704 return INT_FLOAT_CONVERSION_BADNESS;
4705 default:
4706 return INCOMPATIBLE_TYPE_BADNESS;
4710 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4712 static struct rank
4713 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4715 switch (arg->code ())
4717 case TYPE_CODE_RANGE:
4718 case TYPE_CODE_BOOL:
4719 case TYPE_CODE_ENUM:
4720 if (arg->is_declared_class ())
4721 return INCOMPATIBLE_TYPE_BADNESS;
4722 return INTEGER_CONVERSION_BADNESS;
4723 case TYPE_CODE_FLT:
4724 return INT_FLOAT_CONVERSION_BADNESS;
4725 case TYPE_CODE_INT:
4726 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
4727 return INTEGER_CONVERSION_BADNESS;
4728 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4729 return INTEGER_PROMOTION_BADNESS;
4730 /* fall through */
4731 case TYPE_CODE_CHAR:
4732 /* Deal with signed, unsigned, and plain chars for C++ and
4733 with int cases falling through from previous case. */
4734 if (parm->has_no_signedness ())
4736 if (arg->has_no_signedness ())
4737 return EXACT_MATCH_BADNESS;
4738 else
4739 return INTEGER_CONVERSION_BADNESS;
4741 else if (parm->is_unsigned ())
4743 if (arg->is_unsigned ())
4744 return EXACT_MATCH_BADNESS;
4745 else
4746 return INTEGER_PROMOTION_BADNESS;
4748 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4749 return EXACT_MATCH_BADNESS;
4750 else
4751 return INTEGER_CONVERSION_BADNESS;
4752 default:
4753 return INCOMPATIBLE_TYPE_BADNESS;
4757 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4759 static struct rank
4760 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4762 switch (arg->code ())
4764 case TYPE_CODE_INT:
4765 case TYPE_CODE_CHAR:
4766 case TYPE_CODE_RANGE:
4767 case TYPE_CODE_BOOL:
4768 case TYPE_CODE_ENUM:
4769 return INTEGER_CONVERSION_BADNESS;
4770 case TYPE_CODE_FLT:
4771 return INT_FLOAT_CONVERSION_BADNESS;
4772 default:
4773 return INCOMPATIBLE_TYPE_BADNESS;
4777 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4779 static struct rank
4780 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4782 switch (arg->code ())
4784 /* n3290 draft, section 4.12.1 (conv.bool):
4786 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4787 pointer to member type can be converted to a prvalue of type
4788 bool. A zero value, null pointer value, or null member pointer
4789 value is converted to false; any other value is converted to
4790 true. A prvalue of type std::nullptr_t can be converted to a
4791 prvalue of type bool; the resulting value is false." */
4792 case TYPE_CODE_INT:
4793 case TYPE_CODE_CHAR:
4794 case TYPE_CODE_ENUM:
4795 case TYPE_CODE_FLT:
4796 case TYPE_CODE_MEMBERPTR:
4797 case TYPE_CODE_PTR:
4798 return BOOL_CONVERSION_BADNESS;
4799 case TYPE_CODE_RANGE:
4800 return INCOMPATIBLE_TYPE_BADNESS;
4801 case TYPE_CODE_BOOL:
4802 return EXACT_MATCH_BADNESS;
4803 default:
4804 return INCOMPATIBLE_TYPE_BADNESS;
4808 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4810 static struct rank
4811 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4813 switch (arg->code ())
4815 case TYPE_CODE_FLT:
4816 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
4817 return FLOAT_PROMOTION_BADNESS;
4818 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
4819 return EXACT_MATCH_BADNESS;
4820 else
4821 return FLOAT_CONVERSION_BADNESS;
4822 case TYPE_CODE_INT:
4823 case TYPE_CODE_BOOL:
4824 case TYPE_CODE_ENUM:
4825 case TYPE_CODE_RANGE:
4826 case TYPE_CODE_CHAR:
4827 return INT_FLOAT_CONVERSION_BADNESS;
4828 default:
4829 return INCOMPATIBLE_TYPE_BADNESS;
4833 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4835 static struct rank
4836 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4838 switch (arg->code ())
4839 { /* Strictly not needed for C++, but... */
4840 case TYPE_CODE_FLT:
4841 return FLOAT_PROMOTION_BADNESS;
4842 case TYPE_CODE_COMPLEX:
4843 return EXACT_MATCH_BADNESS;
4844 default:
4845 return INCOMPATIBLE_TYPE_BADNESS;
4849 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4851 static struct rank
4852 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4854 struct rank rank = {0, 0};
4856 switch (arg->code ())
4858 case TYPE_CODE_STRUCT:
4859 /* Check for derivation */
4860 rank.subrank = distance_to_ancestor (parm, arg, 0);
4861 if (rank.subrank >= 0)
4862 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4863 /* fall through */
4864 default:
4865 return INCOMPATIBLE_TYPE_BADNESS;
4869 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4871 static struct rank
4872 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4874 switch (arg->code ())
4876 /* Not in C++ */
4877 case TYPE_CODE_SET:
4878 return rank_one_type (parm->field (0).type (),
4879 arg->field (0).type (), NULL);
4880 default:
4881 return INCOMPATIBLE_TYPE_BADNESS;
4885 /* Compare one type (PARM) for compatibility with another (ARG).
4886 * PARM is intended to be the parameter type of a function; and
4887 * ARG is the supplied argument's type. This function tests if
4888 * the latter can be converted to the former.
4889 * VALUE is the argument's value or NULL if none (or called recursively)
4891 * Return 0 if they are identical types;
4892 * Otherwise, return an integer which corresponds to how compatible
4893 * PARM is to ARG. The higher the return value, the worse the match.
4894 * Generally the "bad" conversions are all uniformly assigned
4895 * INVALID_CONVERSION. */
4897 struct rank
4898 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4900 struct rank rank = {0,0};
4902 /* Resolve typedefs */
4903 if (parm->code () == TYPE_CODE_TYPEDEF)
4904 parm = check_typedef (parm);
4905 if (arg->code () == TYPE_CODE_TYPEDEF)
4906 arg = check_typedef (arg);
4908 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4910 if (VALUE_LVAL (value) == not_lval)
4912 /* Rvalues should preferably bind to rvalue references or const
4913 lvalue references. */
4914 if (parm->code () == TYPE_CODE_RVALUE_REF)
4915 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4916 else if (TYPE_CONST (TYPE_TARGET_TYPE (parm)))
4917 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4918 else
4919 return INCOMPATIBLE_TYPE_BADNESS;
4920 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4922 else
4924 /* It's illegal to pass an lvalue as an rvalue. */
4925 if (parm->code () == TYPE_CODE_RVALUE_REF)
4926 return INCOMPATIBLE_TYPE_BADNESS;
4930 if (types_equal (parm, arg))
4932 struct type *t1 = parm;
4933 struct type *t2 = arg;
4935 /* For pointers and references, compare target type. */
4936 if (parm->is_pointer_or_reference ())
4938 t1 = TYPE_TARGET_TYPE (parm);
4939 t2 = TYPE_TARGET_TYPE (arg);
4942 /* Make sure they are CV equal, too. */
4943 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4944 rank.subrank |= CV_CONVERSION_CONST;
4945 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4946 rank.subrank |= CV_CONVERSION_VOLATILE;
4947 if (rank.subrank != 0)
4948 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4949 return EXACT_MATCH_BADNESS;
4952 /* See through references, since we can almost make non-references
4953 references. */
4955 if (TYPE_IS_REFERENCE (arg))
4956 return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
4957 REFERENCE_SEE_THROUGH_BADNESS));
4958 if (TYPE_IS_REFERENCE (parm))
4959 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
4960 REFERENCE_SEE_THROUGH_BADNESS));
4961 if (overload_debug)
4963 /* Debugging only. */
4964 gdb_printf (gdb_stderr,
4965 "------ Arg is %s [%d], parm is %s [%d]\n",
4966 arg->name (), arg->code (),
4967 parm->name (), parm->code ());
4970 /* x -> y means arg of type x being supplied for parameter of type y. */
4972 switch (parm->code ())
4974 case TYPE_CODE_PTR:
4975 return rank_one_type_parm_ptr (parm, arg, value);
4976 case TYPE_CODE_ARRAY:
4977 return rank_one_type_parm_array (parm, arg, value);
4978 case TYPE_CODE_FUNC:
4979 return rank_one_type_parm_func (parm, arg, value);
4980 case TYPE_CODE_INT:
4981 return rank_one_type_parm_int (parm, arg, value);
4982 case TYPE_CODE_ENUM:
4983 return rank_one_type_parm_enum (parm, arg, value);
4984 case TYPE_CODE_CHAR:
4985 return rank_one_type_parm_char (parm, arg, value);
4986 case TYPE_CODE_RANGE:
4987 return rank_one_type_parm_range (parm, arg, value);
4988 case TYPE_CODE_BOOL:
4989 return rank_one_type_parm_bool (parm, arg, value);
4990 case TYPE_CODE_FLT:
4991 return rank_one_type_parm_float (parm, arg, value);
4992 case TYPE_CODE_COMPLEX:
4993 return rank_one_type_parm_complex (parm, arg, value);
4994 case TYPE_CODE_STRUCT:
4995 return rank_one_type_parm_struct (parm, arg, value);
4996 case TYPE_CODE_SET:
4997 return rank_one_type_parm_set (parm, arg, value);
4998 default:
4999 return INCOMPATIBLE_TYPE_BADNESS;
5000 } /* switch (arg->code ()) */
5003 /* End of functions for overload resolution. */
5005 /* Routines to pretty-print types. */
5007 static void
5008 print_bit_vector (B_TYPE *bits, int nbits)
5010 int bitno;
5012 for (bitno = 0; bitno < nbits; bitno++)
5014 if ((bitno % 8) == 0)
5016 gdb_puts (" ");
5018 if (B_TST (bits, bitno))
5019 gdb_printf (("1"));
5020 else
5021 gdb_printf (("0"));
5025 /* Note the first arg should be the "this" pointer, we may not want to
5026 include it since we may get into a infinitely recursive
5027 situation. */
5029 static void
5030 print_args (struct field *args, int nargs, int spaces)
5032 if (args != NULL)
5034 int i;
5036 for (i = 0; i < nargs; i++)
5038 gdb_printf
5039 ("%*s[%d] name '%s'\n", spaces, "", i,
5040 args[i].name () != NULL ? args[i].name () : "<NULL>");
5041 recursive_dump_type (args[i].type (), spaces + 2);
5047 field_is_static (struct field *f)
5049 /* "static" fields are the fields whose location is not relative
5050 to the address of the enclosing struct. It would be nice to
5051 have a dedicated flag that would be set for static fields when
5052 the type is being created. But in practice, checking the field
5053 loc_kind should give us an accurate answer. */
5054 return (f->loc_kind () == FIELD_LOC_KIND_PHYSNAME
5055 || f->loc_kind () == FIELD_LOC_KIND_PHYSADDR);
5058 static void
5059 dump_fn_fieldlists (struct type *type, int spaces)
5061 int method_idx;
5062 int overload_idx;
5063 struct fn_field *f;
5065 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
5066 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
5067 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
5069 f = TYPE_FN_FIELDLIST1 (type, method_idx);
5070 gdb_printf
5071 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
5072 method_idx,
5073 TYPE_FN_FIELDLIST_NAME (type, method_idx),
5074 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
5075 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
5076 for (overload_idx = 0;
5077 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
5078 overload_idx++)
5080 gdb_printf
5081 ("%*s[%d] physname '%s' (%s)\n",
5082 spaces + 4, "", overload_idx,
5083 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
5084 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
5085 overload_idx)));
5086 gdb_printf
5087 ("%*stype %s\n", spaces + 8, "",
5088 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
5090 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
5091 spaces + 8 + 2);
5093 gdb_printf
5094 ("%*sargs %s\n", spaces + 8, "",
5095 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
5096 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
5097 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
5098 spaces + 8 + 2);
5099 gdb_printf
5100 ("%*sfcontext %s\n", spaces + 8, "",
5101 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
5102 overload_idx)));
5104 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
5105 TYPE_FN_FIELD_CONST (f, overload_idx));
5106 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
5107 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
5108 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
5109 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
5110 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
5111 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
5112 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
5113 TYPE_FN_FIELD_STUB (f, overload_idx));
5114 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
5115 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
5116 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
5117 TYPE_FN_FIELD_DELETED (f, overload_idx));
5118 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
5119 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
5124 static void
5125 print_cplus_stuff (struct type *type, int spaces)
5127 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
5128 TYPE_VPTR_FIELDNO (type));
5129 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
5130 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
5131 if (TYPE_VPTR_BASETYPE (type) != NULL)
5132 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
5134 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
5135 TYPE_N_BASECLASSES (type));
5136 gdb_printf ("%*snfn_fields %d\n", spaces, "",
5137 TYPE_NFN_FIELDS (type));
5138 if (TYPE_N_BASECLASSES (type) > 0)
5140 gdb_printf
5141 ("%*svirtual_field_bits (%d bits at *%s)",
5142 spaces, "", TYPE_N_BASECLASSES (type),
5143 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type)));
5145 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
5146 TYPE_N_BASECLASSES (type));
5147 gdb_puts ("\n");
5149 if (type->num_fields () > 0)
5151 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
5153 gdb_printf
5154 ("%*sprivate_field_bits (%d bits at *%s)",
5155 spaces, "", type->num_fields (),
5156 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type)));
5157 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
5158 type->num_fields ());
5159 gdb_puts ("\n");
5161 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
5163 gdb_printf
5164 ("%*sprotected_field_bits (%d bits at *%s",
5165 spaces, "", type->num_fields (),
5166 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type)));
5167 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
5168 type->num_fields ());
5169 gdb_puts ("\n");
5172 if (TYPE_NFN_FIELDS (type) > 0)
5174 dump_fn_fieldlists (type, spaces);
5177 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5178 TYPE_CPLUS_CALLING_CONVENTION (type));
5181 /* Print the contents of the TYPE's type_specific union, assuming that
5182 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5184 static void
5185 print_gnat_stuff (struct type *type, int spaces)
5187 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5189 if (descriptive_type == NULL)
5190 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5191 else
5193 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5194 recursive_dump_type (descriptive_type, spaces + 4);
5198 /* Print the contents of the TYPE's type_specific union, assuming that
5199 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5201 static void
5202 print_fixed_point_type_info (struct type *type, int spaces)
5204 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5205 type->fixed_point_scaling_factor ().str ().c_str ());
5208 static struct obstack dont_print_type_obstack;
5210 /* Print the dynamic_prop PROP. */
5212 static void
5213 dump_dynamic_prop (dynamic_prop const& prop)
5215 switch (prop.kind ())
5217 case PROP_CONST:
5218 gdb_printf ("%s", plongest (prop.const_val ()));
5219 break;
5220 case PROP_UNDEFINED:
5221 gdb_printf ("(undefined)");
5222 break;
5223 case PROP_LOCEXPR:
5224 case PROP_LOCLIST:
5225 gdb_printf ("(dynamic)");
5226 break;
5227 default:
5228 gdb_assert_not_reached ("unhandled prop kind");
5229 break;
5233 void
5234 recursive_dump_type (struct type *type, int spaces)
5236 int idx;
5238 if (spaces == 0)
5239 obstack_begin (&dont_print_type_obstack, 0);
5241 if (type->num_fields () > 0
5242 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5244 struct type **first_dont_print
5245 = (struct type **) obstack_base (&dont_print_type_obstack);
5247 int i = (struct type **)
5248 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5250 while (--i >= 0)
5252 if (type == first_dont_print[i])
5254 gdb_printf ("%*stype node %s", spaces, "",
5255 host_address_to_string (type));
5256 gdb_printf (_(" <same as already seen type>\n"));
5257 return;
5261 obstack_ptr_grow (&dont_print_type_obstack, type);
5264 gdb_printf ("%*stype node %s\n", spaces, "",
5265 host_address_to_string (type));
5266 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5267 type->name () ? type->name () : "<NULL>",
5268 host_address_to_string (type->name ()));
5269 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5270 switch (type->code ())
5272 case TYPE_CODE_UNDEF:
5273 gdb_printf ("(TYPE_CODE_UNDEF)");
5274 break;
5275 case TYPE_CODE_PTR:
5276 gdb_printf ("(TYPE_CODE_PTR)");
5277 break;
5278 case TYPE_CODE_ARRAY:
5279 gdb_printf ("(TYPE_CODE_ARRAY)");
5280 break;
5281 case TYPE_CODE_STRUCT:
5282 gdb_printf ("(TYPE_CODE_STRUCT)");
5283 break;
5284 case TYPE_CODE_UNION:
5285 gdb_printf ("(TYPE_CODE_UNION)");
5286 break;
5287 case TYPE_CODE_ENUM:
5288 gdb_printf ("(TYPE_CODE_ENUM)");
5289 break;
5290 case TYPE_CODE_FLAGS:
5291 gdb_printf ("(TYPE_CODE_FLAGS)");
5292 break;
5293 case TYPE_CODE_FUNC:
5294 gdb_printf ("(TYPE_CODE_FUNC)");
5295 break;
5296 case TYPE_CODE_INT:
5297 gdb_printf ("(TYPE_CODE_INT)");
5298 break;
5299 case TYPE_CODE_FLT:
5300 gdb_printf ("(TYPE_CODE_FLT)");
5301 break;
5302 case TYPE_CODE_VOID:
5303 gdb_printf ("(TYPE_CODE_VOID)");
5304 break;
5305 case TYPE_CODE_SET:
5306 gdb_printf ("(TYPE_CODE_SET)");
5307 break;
5308 case TYPE_CODE_RANGE:
5309 gdb_printf ("(TYPE_CODE_RANGE)");
5310 break;
5311 case TYPE_CODE_STRING:
5312 gdb_printf ("(TYPE_CODE_STRING)");
5313 break;
5314 case TYPE_CODE_ERROR:
5315 gdb_printf ("(TYPE_CODE_ERROR)");
5316 break;
5317 case TYPE_CODE_MEMBERPTR:
5318 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5319 break;
5320 case TYPE_CODE_METHODPTR:
5321 gdb_printf ("(TYPE_CODE_METHODPTR)");
5322 break;
5323 case TYPE_CODE_METHOD:
5324 gdb_printf ("(TYPE_CODE_METHOD)");
5325 break;
5326 case TYPE_CODE_REF:
5327 gdb_printf ("(TYPE_CODE_REF)");
5328 break;
5329 case TYPE_CODE_CHAR:
5330 gdb_printf ("(TYPE_CODE_CHAR)");
5331 break;
5332 case TYPE_CODE_BOOL:
5333 gdb_printf ("(TYPE_CODE_BOOL)");
5334 break;
5335 case TYPE_CODE_COMPLEX:
5336 gdb_printf ("(TYPE_CODE_COMPLEX)");
5337 break;
5338 case TYPE_CODE_TYPEDEF:
5339 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5340 break;
5341 case TYPE_CODE_NAMESPACE:
5342 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5343 break;
5344 case TYPE_CODE_FIXED_POINT:
5345 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5346 break;
5347 default:
5348 gdb_printf ("(UNKNOWN TYPE CODE)");
5349 break;
5351 gdb_puts ("\n");
5352 gdb_printf ("%*slength %s\n", spaces, "",
5353 pulongest (TYPE_LENGTH (type)));
5354 if (type->is_objfile_owned ())
5355 gdb_printf ("%*sobjfile %s\n", spaces, "",
5356 host_address_to_string (type->objfile_owner ()));
5357 else
5358 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5359 host_address_to_string (type->arch_owner ()));
5360 gdb_printf ("%*starget_type %s\n", spaces, "",
5361 host_address_to_string (TYPE_TARGET_TYPE (type)));
5362 if (TYPE_TARGET_TYPE (type) != NULL)
5364 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
5366 gdb_printf ("%*spointer_type %s\n", spaces, "",
5367 host_address_to_string (TYPE_POINTER_TYPE (type)));
5368 gdb_printf ("%*sreference_type %s\n", spaces, "",
5369 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5370 gdb_printf ("%*stype_chain %s\n", spaces, "",
5371 host_address_to_string (TYPE_CHAIN (type)));
5372 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5373 (unsigned) type->instance_flags ());
5374 if (TYPE_CONST (type))
5376 gdb_puts (" TYPE_CONST");
5378 if (TYPE_VOLATILE (type))
5380 gdb_puts (" TYPE_VOLATILE");
5382 if (TYPE_CODE_SPACE (type))
5384 gdb_puts (" TYPE_CODE_SPACE");
5386 if (TYPE_DATA_SPACE (type))
5388 gdb_puts (" TYPE_DATA_SPACE");
5390 if (TYPE_ADDRESS_CLASS_1 (type))
5392 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5394 if (TYPE_ADDRESS_CLASS_2 (type))
5396 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5398 if (TYPE_RESTRICT (type))
5400 gdb_puts (" TYPE_RESTRICT");
5402 if (TYPE_ATOMIC (type))
5404 gdb_puts (" TYPE_ATOMIC");
5406 gdb_puts ("\n");
5408 gdb_printf ("%*sflags", spaces, "");
5409 if (type->is_unsigned ())
5411 gdb_puts (" TYPE_UNSIGNED");
5413 if (type->has_no_signedness ())
5415 gdb_puts (" TYPE_NOSIGN");
5417 if (type->endianity_is_not_default ())
5419 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5421 if (type->is_stub ())
5423 gdb_puts (" TYPE_STUB");
5425 if (type->target_is_stub ())
5427 gdb_puts (" TYPE_TARGET_STUB");
5429 if (type->is_prototyped ())
5431 gdb_puts (" TYPE_PROTOTYPED");
5433 if (type->has_varargs ())
5435 gdb_puts (" TYPE_VARARGS");
5437 /* This is used for things like AltiVec registers on ppc. Gcc emits
5438 an attribute for the array type, which tells whether or not we
5439 have a vector, instead of a regular array. */
5440 if (type->is_vector ())
5442 gdb_puts (" TYPE_VECTOR");
5444 if (type->is_fixed_instance ())
5446 gdb_puts (" TYPE_FIXED_INSTANCE");
5448 if (type->stub_is_supported ())
5450 gdb_puts (" TYPE_STUB_SUPPORTED");
5452 if (TYPE_NOTTEXT (type))
5454 gdb_puts (" TYPE_NOTTEXT");
5456 gdb_puts ("\n");
5457 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5458 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5459 || TYPE_ALLOCATED_PROP (type) != nullptr)
5461 gdb_printf ("%*s", spaces, "");
5462 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5464 gdb_printf ("associated ");
5465 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5467 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5469 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5470 gdb_printf (" ");
5471 gdb_printf ("allocated ");
5472 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5474 gdb_printf ("\n");
5476 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5477 for (idx = 0; idx < type->num_fields (); idx++)
5479 if (type->code () == TYPE_CODE_ENUM)
5480 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5481 idx, plongest (type->field (idx).loc_enumval ()));
5482 else
5483 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5484 idx, plongest (type->field (idx).loc_bitpos ()),
5485 TYPE_FIELD_BITSIZE (type, idx));
5486 gdb_printf ("%s name '%s' (%s)\n",
5487 host_address_to_string (type->field (idx).type ()),
5488 type->field (idx).name () != NULL
5489 ? type->field (idx).name ()
5490 : "<NULL>",
5491 host_address_to_string (type->field (idx).name ()));
5492 if (type->field (idx).type () != NULL)
5494 recursive_dump_type (type->field (idx).type (), spaces + 4);
5497 if (type->code () == TYPE_CODE_RANGE)
5499 gdb_printf ("%*slow ", spaces, "");
5500 dump_dynamic_prop (type->bounds ()->low);
5501 gdb_printf (" high ");
5502 dump_dynamic_prop (type->bounds ()->high);
5503 gdb_printf ("\n");
5506 switch (TYPE_SPECIFIC_FIELD (type))
5508 case TYPE_SPECIFIC_CPLUS_STUFF:
5509 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5510 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5511 print_cplus_stuff (type, spaces);
5512 break;
5514 case TYPE_SPECIFIC_GNAT_STUFF:
5515 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5516 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5517 print_gnat_stuff (type, spaces);
5518 break;
5520 case TYPE_SPECIFIC_FLOATFORMAT:
5521 gdb_printf ("%*sfloatformat ", spaces, "");
5522 if (TYPE_FLOATFORMAT (type) == NULL
5523 || TYPE_FLOATFORMAT (type)->name == NULL)
5524 gdb_puts ("(null)");
5525 else
5526 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5527 gdb_puts ("\n");
5528 break;
5530 case TYPE_SPECIFIC_FUNC:
5531 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5532 TYPE_CALLING_CONVENTION (type));
5533 /* tail_call_list is not printed. */
5534 break;
5536 case TYPE_SPECIFIC_SELF_TYPE:
5537 gdb_printf ("%*sself_type %s\n", spaces, "",
5538 host_address_to_string (TYPE_SELF_TYPE (type)));
5539 break;
5541 case TYPE_SPECIFIC_FIXED_POINT:
5542 gdb_printf ("%*sfixed_point_info ", spaces, "");
5543 print_fixed_point_type_info (type, spaces);
5544 gdb_puts ("\n");
5545 break;
5547 case TYPE_SPECIFIC_INT:
5548 if (type->bit_size_differs_p ())
5550 unsigned bit_size = type->bit_size ();
5551 unsigned bit_off = type->bit_offset ();
5552 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5553 bit_size, bit_off);
5555 break;
5558 if (spaces == 0)
5559 obstack_free (&dont_print_type_obstack, NULL);
5562 /* Trivial helpers for the libiberty hash table, for mapping one
5563 type to another. */
5565 struct type_pair
5567 type_pair (struct type *old_, struct type *newobj_)
5568 : old (old_), newobj (newobj_)
5571 struct type * const old, * const newobj;
5574 static hashval_t
5575 type_pair_hash (const void *item)
5577 const struct type_pair *pair = (const struct type_pair *) item;
5579 return htab_hash_pointer (pair->old);
5582 static int
5583 type_pair_eq (const void *item_lhs, const void *item_rhs)
5585 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5586 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5588 return lhs->old == rhs->old;
5591 /* Allocate the hash table used by copy_type_recursive to walk
5592 types without duplicates. */
5594 htab_up
5595 create_copied_types_hash ()
5597 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5598 htab_delete_entry<type_pair>,
5599 xcalloc, xfree));
5602 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5604 static struct dynamic_prop_list *
5605 copy_dynamic_prop_list (struct obstack *storage,
5606 struct dynamic_prop_list *list)
5608 struct dynamic_prop_list *copy = list;
5609 struct dynamic_prop_list **node_ptr = &copy;
5611 while (*node_ptr != NULL)
5613 struct dynamic_prop_list *node_copy;
5615 node_copy = ((struct dynamic_prop_list *)
5616 obstack_copy (storage, *node_ptr,
5617 sizeof (struct dynamic_prop_list)));
5618 node_copy->prop = (*node_ptr)->prop;
5619 *node_ptr = node_copy;
5621 node_ptr = &node_copy->next;
5624 return copy;
5627 /* Recursively copy (deep copy) TYPE, if it is associated with
5628 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5629 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5630 it is not associated with OBJFILE. */
5632 struct type *
5633 copy_type_recursive (struct type *type, htab_t copied_types)
5635 void **slot;
5636 struct type *new_type;
5638 if (!type->is_objfile_owned ())
5639 return type;
5641 struct type_pair pair (type, nullptr);
5643 slot = htab_find_slot (copied_types, &pair, INSERT);
5644 if (*slot != NULL)
5645 return ((struct type_pair *) *slot)->newobj;
5647 new_type = alloc_type_arch (type->arch ());
5649 /* We must add the new type to the hash table immediately, in case
5650 we encounter this type again during a recursive call below. */
5651 struct type_pair *stored = new type_pair (type, new_type);
5653 *slot = stored;
5655 /* Copy the common fields of types. For the main type, we simply
5656 copy the entire thing and then update specific fields as needed. */
5657 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5659 new_type->set_owner (type->arch ());
5661 if (type->name ())
5662 new_type->set_name (xstrdup (type->name ()));
5664 new_type->set_instance_flags (type->instance_flags ());
5665 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
5667 /* Copy the fields. */
5668 if (type->num_fields ())
5670 int i, nfields;
5672 nfields = type->num_fields ();
5673 new_type->set_fields
5674 ((struct field *)
5675 TYPE_ZALLOC (new_type, nfields * sizeof (struct field)));
5677 for (i = 0; i < nfields; i++)
5679 TYPE_FIELD_ARTIFICIAL (new_type, i) =
5680 TYPE_FIELD_ARTIFICIAL (type, i);
5681 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
5682 if (type->field (i).type ())
5683 new_type->field (i).set_type
5684 (copy_type_recursive (type->field (i).type (), copied_types));
5685 if (type->field (i).name ())
5686 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5688 switch (type->field (i).loc_kind ())
5690 case FIELD_LOC_KIND_BITPOS:
5691 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5692 break;
5693 case FIELD_LOC_KIND_ENUMVAL:
5694 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5695 break;
5696 case FIELD_LOC_KIND_PHYSADDR:
5697 new_type->field (i).set_loc_physaddr
5698 (type->field (i).loc_physaddr ());
5699 break;
5700 case FIELD_LOC_KIND_PHYSNAME:
5701 new_type->field (i).set_loc_physname
5702 (xstrdup (type->field (i).loc_physname ()));
5703 break;
5704 case FIELD_LOC_KIND_DWARF_BLOCK:
5705 new_type->field (i).set_loc_dwarf_block
5706 (type->field (i).loc_dwarf_block ());
5707 break;
5708 default:
5709 internal_error (__FILE__, __LINE__,
5710 _("Unexpected type field location kind: %d"),
5711 type->field (i).loc_kind ());
5716 /* For range types, copy the bounds information. */
5717 if (type->code () == TYPE_CODE_RANGE)
5719 range_bounds *bounds
5720 = ((struct range_bounds *) TYPE_ALLOC
5721 (new_type, sizeof (struct range_bounds)));
5723 *bounds = *type->bounds ();
5724 new_type->set_bounds (bounds);
5727 if (type->main_type->dyn_prop_list != NULL)
5728 new_type->main_type->dyn_prop_list
5729 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5730 type->main_type->dyn_prop_list);
5733 /* Copy pointers to other types. */
5734 if (TYPE_TARGET_TYPE (type))
5735 TYPE_TARGET_TYPE (new_type) =
5736 copy_type_recursive (TYPE_TARGET_TYPE (type), copied_types);
5738 /* Maybe copy the type_specific bits.
5740 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5741 base classes and methods. There's no fundamental reason why we
5742 can't, but at the moment it is not needed. */
5744 switch (TYPE_SPECIFIC_FIELD (type))
5746 case TYPE_SPECIFIC_NONE:
5747 break;
5748 case TYPE_SPECIFIC_FUNC:
5749 INIT_FUNC_SPECIFIC (new_type);
5750 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5751 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5752 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5753 break;
5754 case TYPE_SPECIFIC_FLOATFORMAT:
5755 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5756 break;
5757 case TYPE_SPECIFIC_CPLUS_STUFF:
5758 INIT_CPLUS_SPECIFIC (new_type);
5759 break;
5760 case TYPE_SPECIFIC_GNAT_STUFF:
5761 INIT_GNAT_SPECIFIC (new_type);
5762 break;
5763 case TYPE_SPECIFIC_SELF_TYPE:
5764 set_type_self_type (new_type,
5765 copy_type_recursive (TYPE_SELF_TYPE (type),
5766 copied_types));
5767 break;
5768 case TYPE_SPECIFIC_FIXED_POINT:
5769 INIT_FIXED_POINT_SPECIFIC (new_type);
5770 new_type->fixed_point_info ().scaling_factor
5771 = type->fixed_point_info ().scaling_factor;
5772 break;
5773 case TYPE_SPECIFIC_INT:
5774 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5775 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5776 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5777 break;
5779 default:
5780 gdb_assert_not_reached ("bad type_specific_kind");
5783 return new_type;
5786 /* Make a copy of the given TYPE, except that the pointer & reference
5787 types are not preserved. */
5789 struct type *
5790 copy_type (const struct type *type)
5792 struct type *new_type = alloc_type_copy (type);
5793 new_type->set_instance_flags (type->instance_flags ());
5794 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
5795 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5796 sizeof (struct main_type));
5797 if (type->main_type->dyn_prop_list != NULL)
5799 struct obstack *storage = (type->is_objfile_owned ()
5800 ? &type->objfile_owner ()->objfile_obstack
5801 : gdbarch_obstack (type->arch_owner ()));
5802 new_type->main_type->dyn_prop_list
5803 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5806 return new_type;
5809 /* Helper functions to initialize architecture-specific types. */
5811 /* Allocate a type structure associated with GDBARCH and set its
5812 CODE, LENGTH, and NAME fields. */
5814 struct type *
5815 arch_type (struct gdbarch *gdbarch,
5816 enum type_code code, int bit, const char *name)
5818 struct type *type;
5820 type = alloc_type_arch (gdbarch);
5821 set_type_code (type, code);
5822 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
5823 TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
5825 if (name)
5826 type->set_name (gdbarch_obstack_strdup (gdbarch, name));
5828 return type;
5831 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
5832 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5833 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5835 struct type *
5836 arch_integer_type (struct gdbarch *gdbarch,
5837 int bit, int unsigned_p, const char *name)
5839 struct type *t;
5841 t = arch_type (gdbarch, TYPE_CODE_INT, bit, name);
5842 if (unsigned_p)
5843 t->set_is_unsigned (true);
5845 return t;
5848 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
5849 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5850 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5852 struct type *
5853 arch_character_type (struct gdbarch *gdbarch,
5854 int bit, int unsigned_p, const char *name)
5856 struct type *t;
5858 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit, name);
5859 if (unsigned_p)
5860 t->set_is_unsigned (true);
5862 return t;
5865 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
5866 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
5867 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5869 struct type *
5870 arch_boolean_type (struct gdbarch *gdbarch,
5871 int bit, int unsigned_p, const char *name)
5873 struct type *t;
5875 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit, name);
5876 if (unsigned_p)
5877 t->set_is_unsigned (true);
5879 return t;
5882 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
5883 BIT is the type size in bits; if BIT equals -1, the size is
5884 determined by the floatformat. NAME is the type name. Set the
5885 TYPE_FLOATFORMAT from FLOATFORMATS. */
5887 struct type *
5888 arch_float_type (struct gdbarch *gdbarch,
5889 int bit, const char *name,
5890 const struct floatformat **floatformats)
5892 const struct floatformat *fmt = floatformats[gdbarch_byte_order (gdbarch)];
5893 struct type *t;
5895 bit = verify_floatformat (bit, fmt);
5896 t = arch_type (gdbarch, TYPE_CODE_FLT, bit, name);
5897 TYPE_FLOATFORMAT (t) = fmt;
5899 return t;
5902 /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
5903 BIT is the type size in bits. NAME is the type name. */
5905 struct type *
5906 arch_decfloat_type (struct gdbarch *gdbarch, int bit, const char *name)
5908 struct type *t;
5910 t = arch_type (gdbarch, TYPE_CODE_DECFLOAT, bit, name);
5911 return t;
5914 /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
5915 BIT is the pointer type size in bits. NAME is the type name.
5916 TARGET_TYPE is the pointer target type. Always sets the pointer type's
5917 TYPE_UNSIGNED flag. */
5919 struct type *
5920 arch_pointer_type (struct gdbarch *gdbarch,
5921 int bit, const char *name, struct type *target_type)
5923 struct type *t;
5925 t = arch_type (gdbarch, TYPE_CODE_PTR, bit, name);
5926 TYPE_TARGET_TYPE (t) = target_type;
5927 t->set_is_unsigned (true);
5928 return t;
5931 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5932 NAME is the type name. BIT is the size of the flag word in bits. */
5934 struct type *
5935 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5937 struct type *type;
5939 type = arch_type (gdbarch, TYPE_CODE_FLAGS, bit, name);
5940 type->set_is_unsigned (true);
5941 type->set_num_fields (0);
5942 /* Pre-allocate enough space assuming every field is one bit. */
5943 type->set_fields
5944 ((struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field)));
5946 return type;
5949 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5950 position BITPOS is called NAME. Pass NAME as "" for fields that
5951 should not be printed. */
5953 void
5954 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5955 struct type *field_type, const char *name)
5957 int type_bitsize = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
5958 int field_nr = type->num_fields ();
5960 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5961 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5962 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5963 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5964 gdb_assert (name != NULL);
5966 type->set_num_fields (type->num_fields () + 1);
5967 type->field (field_nr).set_name (xstrdup (name));
5968 type->field (field_nr).set_type (field_type);
5969 type->field (field_nr).set_loc_bitpos (start_bitpos);
5970 TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits;
5973 /* Special version of append_flags_type_field to add a flag field.
5974 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5975 position BITPOS is called NAME. */
5977 void
5978 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5980 append_flags_type_field (type, bitpos, 1,
5981 builtin_type (type->arch ())->builtin_bool,
5982 name);
5985 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5986 specified by CODE) associated with GDBARCH. NAME is the type name. */
5988 struct type *
5989 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5990 enum type_code code)
5992 struct type *t;
5994 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5995 t = arch_type (gdbarch, code, 0, NULL);
5996 t->set_name (name);
5997 INIT_CPLUS_SPECIFIC (t);
5998 return t;
6001 /* Add new field with name NAME and type FIELD to composite type T.
6002 Do not set the field's position or adjust the type's length;
6003 the caller should do so. Return the new field. */
6005 struct field *
6006 append_composite_type_field_raw (struct type *t, const char *name,
6007 struct type *field)
6009 struct field *f;
6011 t->set_num_fields (t->num_fields () + 1);
6012 t->set_fields (XRESIZEVEC (struct field, t->fields (),
6013 t->num_fields ()));
6014 f = &t->field (t->num_fields () - 1);
6015 memset (f, 0, sizeof f[0]);
6016 f[0].set_type (field);
6017 f[0].set_name (name);
6018 return f;
6021 /* Add new field with name NAME and type FIELD to composite type T.
6022 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
6024 void
6025 append_composite_type_field_aligned (struct type *t, const char *name,
6026 struct type *field, int alignment)
6028 struct field *f = append_composite_type_field_raw (t, name, field);
6030 if (t->code () == TYPE_CODE_UNION)
6032 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
6033 TYPE_LENGTH (t) = TYPE_LENGTH (field);
6035 else if (t->code () == TYPE_CODE_STRUCT)
6037 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
6038 if (t->num_fields () > 1)
6040 f->set_loc_bitpos
6041 (f[-1].loc_bitpos () + (TYPE_LENGTH (f[-1].type ()) * TARGET_CHAR_BIT));
6043 if (alignment)
6045 int left;
6047 alignment *= TARGET_CHAR_BIT;
6048 left = f[0].loc_bitpos () % alignment;
6050 if (left)
6052 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
6053 TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
6060 /* Add new field with name NAME and type FIELD to composite type T. */
6062 void
6063 append_composite_type_field (struct type *t, const char *name,
6064 struct type *field)
6066 append_composite_type_field_aligned (t, name, field, 0);
6071 /* We manage the lifetimes of fixed_point_type_info objects by
6072 attaching them to the objfile. Currently, these objects are
6073 modified during construction, and GMP does not provide a way to
6074 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
6075 them. If we did do this, they could be moved to the per-BFD and
6076 shared across objfiles. */
6077 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
6078 fixed_point_type_storage;
6080 /* Key used for managing the storage of fixed-point type info. */
6081 static const struct registry<objfile>::key<fixed_point_type_storage>
6082 fixed_point_objfile_key;
6084 /* See gdbtypes.h. */
6086 void
6087 allocate_fixed_point_type_info (struct type *type)
6089 std::unique_ptr<fixed_point_type_info> up (new fixed_point_type_info);
6090 fixed_point_type_info *info;
6092 if (type->is_objfile_owned ())
6094 fixed_point_type_storage *storage
6095 = fixed_point_objfile_key.get (type->objfile_owner ());
6096 if (storage == nullptr)
6097 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
6098 info = up.get ();
6099 storage->push_back (std::move (up));
6101 else
6103 /* We just leak the memory, because that's what we do generally
6104 for non-objfile-attached types. */
6105 info = up.release ();
6108 type->set_fixed_point_info (info);
6111 /* See gdbtypes.h. */
6113 bool
6114 is_fixed_point_type (struct type *type)
6116 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
6117 type = TYPE_TARGET_TYPE (check_typedef (type));
6118 type = check_typedef (type);
6120 return type->code () == TYPE_CODE_FIXED_POINT;
6123 /* See gdbtypes.h. */
6125 struct type *
6126 type::fixed_point_type_base_type ()
6128 struct type *type = this;
6130 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
6131 type = TYPE_TARGET_TYPE (check_typedef (type));
6132 type = check_typedef (type);
6134 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
6135 return type;
6138 /* See gdbtypes.h. */
6140 const gdb_mpq &
6141 type::fixed_point_scaling_factor ()
6143 struct type *type = this->fixed_point_type_base_type ();
6145 return type->fixed_point_info ().scaling_factor;
6150 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
6152 static struct builtin_type *
6153 create_gdbtypes_data (struct gdbarch *gdbarch)
6155 struct builtin_type *builtin_type = new struct builtin_type;
6157 /* Basic types. */
6158 builtin_type->builtin_void
6159 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
6160 builtin_type->builtin_char
6161 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6162 !gdbarch_char_signed (gdbarch), "char");
6163 builtin_type->builtin_char->set_has_no_signedness (true);
6164 builtin_type->builtin_signed_char
6165 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6166 0, "signed char");
6167 builtin_type->builtin_unsigned_char
6168 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
6169 1, "unsigned char");
6170 builtin_type->builtin_short
6171 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
6172 0, "short");
6173 builtin_type->builtin_unsigned_short
6174 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
6175 1, "unsigned short");
6176 builtin_type->builtin_int
6177 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
6178 0, "int");
6179 builtin_type->builtin_unsigned_int
6180 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
6181 1, "unsigned int");
6182 builtin_type->builtin_long
6183 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
6184 0, "long");
6185 builtin_type->builtin_unsigned_long
6186 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
6187 1, "unsigned long");
6188 builtin_type->builtin_long_long
6189 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
6190 0, "long long");
6191 builtin_type->builtin_unsigned_long_long
6192 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
6193 1, "unsigned long long");
6194 builtin_type->builtin_half
6195 = arch_float_type (gdbarch, gdbarch_half_bit (gdbarch),
6196 "half", gdbarch_half_format (gdbarch));
6197 builtin_type->builtin_float
6198 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
6199 "float", gdbarch_float_format (gdbarch));
6200 builtin_type->builtin_bfloat16
6201 = arch_float_type (gdbarch, gdbarch_bfloat16_bit (gdbarch),
6202 "bfloat16", gdbarch_bfloat16_format (gdbarch));
6203 builtin_type->builtin_double
6204 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
6205 "double", gdbarch_double_format (gdbarch));
6206 builtin_type->builtin_long_double
6207 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
6208 "long double", gdbarch_long_double_format (gdbarch));
6209 builtin_type->builtin_complex
6210 = init_complex_type ("complex", builtin_type->builtin_float);
6211 builtin_type->builtin_double_complex
6212 = init_complex_type ("double complex", builtin_type->builtin_double);
6213 builtin_type->builtin_string
6214 = arch_type (gdbarch, TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
6215 builtin_type->builtin_bool
6216 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "bool");
6218 /* The following three are about decimal floating point types, which
6219 are 32-bits, 64-bits and 128-bits respectively. */
6220 builtin_type->builtin_decfloat
6221 = arch_decfloat_type (gdbarch, 32, "_Decimal32");
6222 builtin_type->builtin_decdouble
6223 = arch_decfloat_type (gdbarch, 64, "_Decimal64");
6224 builtin_type->builtin_declong
6225 = arch_decfloat_type (gdbarch, 128, "_Decimal128");
6227 /* "True" character types. */
6228 builtin_type->builtin_true_char
6229 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
6230 builtin_type->builtin_true_unsigned_char
6231 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
6233 /* Fixed-size integer types. */
6234 builtin_type->builtin_int0
6235 = arch_integer_type (gdbarch, 0, 0, "int0_t");
6236 builtin_type->builtin_int8
6237 = arch_integer_type (gdbarch, 8, 0, "int8_t");
6238 builtin_type->builtin_uint8
6239 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
6240 builtin_type->builtin_int16
6241 = arch_integer_type (gdbarch, 16, 0, "int16_t");
6242 builtin_type->builtin_uint16
6243 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
6244 builtin_type->builtin_int24
6245 = arch_integer_type (gdbarch, 24, 0, "int24_t");
6246 builtin_type->builtin_uint24
6247 = arch_integer_type (gdbarch, 24, 1, "uint24_t");
6248 builtin_type->builtin_int32
6249 = arch_integer_type (gdbarch, 32, 0, "int32_t");
6250 builtin_type->builtin_uint32
6251 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
6252 builtin_type->builtin_int64
6253 = arch_integer_type (gdbarch, 64, 0, "int64_t");
6254 builtin_type->builtin_uint64
6255 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
6256 builtin_type->builtin_int128
6257 = arch_integer_type (gdbarch, 128, 0, "int128_t");
6258 builtin_type->builtin_uint128
6259 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
6261 builtin_type->builtin_int8->set_instance_flags
6262 (builtin_type->builtin_int8->instance_flags ()
6263 | TYPE_INSTANCE_FLAG_NOTTEXT);
6265 builtin_type->builtin_uint8->set_instance_flags
6266 (builtin_type->builtin_uint8->instance_flags ()
6267 | TYPE_INSTANCE_FLAG_NOTTEXT);
6269 /* Wide character types. */
6270 builtin_type->builtin_char16
6271 = arch_integer_type (gdbarch, 16, 1, "char16_t");
6272 builtin_type->builtin_char32
6273 = arch_integer_type (gdbarch, 32, 1, "char32_t");
6274 builtin_type->builtin_wchar
6275 = arch_integer_type (gdbarch, gdbarch_wchar_bit (gdbarch),
6276 !gdbarch_wchar_signed (gdbarch), "wchar_t");
6278 /* Default data/code pointer types. */
6279 builtin_type->builtin_data_ptr
6280 = lookup_pointer_type (builtin_type->builtin_void);
6281 builtin_type->builtin_func_ptr
6282 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6283 builtin_type->builtin_func_func
6284 = lookup_function_type (builtin_type->builtin_func_ptr);
6286 /* This type represents a GDB internal function. */
6287 builtin_type->internal_fn
6288 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
6289 "<internal function>");
6291 /* This type represents an xmethod. */
6292 builtin_type->xmethod
6293 = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
6295 return builtin_type;
6298 const struct builtin_type *
6299 builtin_type (struct gdbarch *gdbarch)
6301 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6302 if (result == nullptr)
6304 result = create_gdbtypes_data (gdbarch);
6305 gdbtypes_data.set (gdbarch, result);
6307 return result;
6310 /* This set of objfile-based types is intended to be used by symbol
6311 readers as basic types. */
6313 static const registry<objfile>::key<struct objfile_type,
6314 gdb::noop_deleter<struct objfile_type>>
6315 objfile_type_data;
6317 const struct objfile_type *
6318 objfile_type (struct objfile *objfile)
6320 struct gdbarch *gdbarch;
6321 struct objfile_type *objfile_type = objfile_type_data.get (objfile);
6323 if (objfile_type)
6324 return objfile_type;
6326 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
6327 1, struct objfile_type);
6329 /* Use the objfile architecture to determine basic type properties. */
6330 gdbarch = objfile->arch ();
6332 /* Basic types. */
6333 objfile_type->builtin_void
6334 = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
6335 objfile_type->builtin_char
6336 = init_integer_type (objfile, TARGET_CHAR_BIT,
6337 !gdbarch_char_signed (gdbarch), "char");
6338 objfile_type->builtin_char->set_has_no_signedness (true);
6339 objfile_type->builtin_signed_char
6340 = init_integer_type (objfile, TARGET_CHAR_BIT,
6341 0, "signed char");
6342 objfile_type->builtin_unsigned_char
6343 = init_integer_type (objfile, TARGET_CHAR_BIT,
6344 1, "unsigned char");
6345 objfile_type->builtin_short
6346 = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
6347 0, "short");
6348 objfile_type->builtin_unsigned_short
6349 = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
6350 1, "unsigned short");
6351 objfile_type->builtin_int
6352 = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
6353 0, "int");
6354 objfile_type->builtin_unsigned_int
6355 = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
6356 1, "unsigned int");
6357 objfile_type->builtin_long
6358 = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
6359 0, "long");
6360 objfile_type->builtin_unsigned_long
6361 = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
6362 1, "unsigned long");
6363 objfile_type->builtin_long_long
6364 = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
6365 0, "long long");
6366 objfile_type->builtin_unsigned_long_long
6367 = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
6368 1, "unsigned long long");
6369 objfile_type->builtin_float
6370 = init_float_type (objfile, gdbarch_float_bit (gdbarch),
6371 "float", gdbarch_float_format (gdbarch));
6372 objfile_type->builtin_double
6373 = init_float_type (objfile, gdbarch_double_bit (gdbarch),
6374 "double", gdbarch_double_format (gdbarch));
6375 objfile_type->builtin_long_double
6376 = init_float_type (objfile, gdbarch_long_double_bit (gdbarch),
6377 "long double", gdbarch_long_double_format (gdbarch));
6379 /* This type represents a type that was unrecognized in symbol read-in. */
6380 objfile_type->builtin_error
6381 = init_type (objfile, TYPE_CODE_ERROR, 0, "<unknown type>");
6383 /* The following set of types is used for symbols with no
6384 debug information. */
6385 objfile_type->nodebug_text_symbol
6386 = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6387 "<text variable, no debug info>");
6389 objfile_type->nodebug_text_gnu_ifunc_symbol
6390 = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6391 "<text gnu-indirect-function variable, no debug info>");
6392 objfile_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6394 objfile_type->nodebug_got_plt_symbol
6395 = init_pointer_type (objfile, gdbarch_addr_bit (gdbarch),
6396 "<text from jump slot in .got.plt, no debug info>",
6397 objfile_type->nodebug_text_symbol);
6398 objfile_type->nodebug_data_symbol
6399 = init_nodebug_var_type (objfile, "<data variable, no debug info>");
6400 objfile_type->nodebug_unknown_symbol
6401 = init_nodebug_var_type (objfile, "<variable (not text or data), no debug info>");
6402 objfile_type->nodebug_tls_symbol
6403 = init_nodebug_var_type (objfile, "<thread local variable, no debug info>");
6405 /* NOTE: on some targets, addresses and pointers are not necessarily
6406 the same.
6408 The upshot is:
6409 - gdb's `struct type' always describes the target's
6410 representation.
6411 - gdb's `struct value' objects should always hold values in
6412 target form.
6413 - gdb's CORE_ADDR values are addresses in the unified virtual
6414 address space that the assembler and linker work with. Thus,
6415 since target_read_memory takes a CORE_ADDR as an argument, it
6416 can access any memory on the target, even if the processor has
6417 separate code and data address spaces.
6419 In this context, objfile_type->builtin_core_addr is a bit odd:
6420 it's a target type for a value the target will never see. It's
6421 only used to hold the values of (typeless) linker symbols, which
6422 are indeed in the unified virtual address space. */
6424 objfile_type->builtin_core_addr
6425 = init_integer_type (objfile, gdbarch_addr_bit (gdbarch), 1,
6426 "__CORE_ADDR");
6428 objfile_type_data.set (objfile, objfile_type);
6429 return objfile_type;
6432 /* See gdbtypes.h. */
6434 CORE_ADDR
6435 call_site::pc () const
6437 compunit_symtab *cust = this->per_objfile->get_symtab (this->per_cu);
6438 CORE_ADDR delta
6439 = this->per_objfile->objfile->section_offsets[cust->block_line_section ()];
6440 return m_unrelocated_pc + delta;
6443 void _initialize_gdbtypes ();
6444 void
6445 _initialize_gdbtypes ()
6447 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6448 _("Set debugging of C++ overloading."),
6449 _("Show debugging of C++ overloading."),
6450 _("When enabled, ranking of the "
6451 "functions is displayed."),
6452 NULL,
6453 show_overload_debug,
6454 &setdebuglist, &showdebuglist);
6456 /* Add user knob for controlling resolution of opaque types. */
6457 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6458 &opaque_type_resolution,
6459 _("Set resolution of opaque struct/class/union"
6460 " types (if set before loading symbols)."),
6461 _("Show resolution of opaque struct/class/union"
6462 " types (if set before loading symbols)."),
6463 NULL, NULL,
6464 show_opaque_type_resolution,
6465 &setlist, &showlist);
6467 /* Add an option to permit non-strict type checking. */
6468 add_setshow_boolean_cmd ("type", class_support,
6469 &strict_type_checking,
6470 _("Set strict type checking."),
6471 _("Show strict type checking."),
6472 NULL, NULL,
6473 show_strict_type_checking,
6474 &setchecklist, &showchecklist);