Remove path name from test case
[binutils-gdb.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3 Copyright (C) 1992-2023 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
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"
46 #include "rust-lang.h"
47 #include "ada-lang.h"
48
49 /* The value of an invalid conversion badness. */
50 #define INVALID_CONVERSION 100
51
52 static struct dynamic_prop_list *
53 copy_dynamic_prop_list (struct obstack *, struct dynamic_prop_list *);
54
55 /* Initialize BADNESS constants. */
56
57 const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
58
59 const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
60 const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
61
62 const struct rank EXACT_MATCH_BADNESS = {0,0};
63
64 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
65 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
66 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
67 const struct rank CV_CONVERSION_BADNESS = {1, 0};
68 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
69 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
70 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
71 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
72 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
73 const struct rank BASE_CONVERSION_BADNESS = {2,0};
74 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
75 const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
76 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
77 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
78 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
79
80 /* Floatformat pairs. */
81 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
82 &floatformat_ieee_half_big,
83 &floatformat_ieee_half_little
84 };
85 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
86 &floatformat_ieee_single_big,
87 &floatformat_ieee_single_little
88 };
89 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
90 &floatformat_ieee_double_big,
91 &floatformat_ieee_double_little
92 };
93 const struct floatformat *floatformats_ieee_quad[BFD_ENDIAN_UNKNOWN] = {
94 &floatformat_ieee_quad_big,
95 &floatformat_ieee_quad_little
96 };
97 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
98 &floatformat_ieee_double_big,
99 &floatformat_ieee_double_littlebyte_bigword
100 };
101 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
102 &floatformat_i387_ext,
103 &floatformat_i387_ext
104 };
105 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
106 &floatformat_m68881_ext,
107 &floatformat_m68881_ext
108 };
109 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
110 &floatformat_arm_ext_big,
111 &floatformat_arm_ext_littlebyte_bigword
112 };
113 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
114 &floatformat_ia64_spill_big,
115 &floatformat_ia64_spill_little
116 };
117 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
118 &floatformat_vax_f,
119 &floatformat_vax_f
120 };
121 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
122 &floatformat_vax_d,
123 &floatformat_vax_d
124 };
125 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
126 &floatformat_ibm_long_double_big,
127 &floatformat_ibm_long_double_little
128 };
129 const struct floatformat *floatformats_bfloat16[BFD_ENDIAN_UNKNOWN] = {
130 &floatformat_bfloat16_big,
131 &floatformat_bfloat16_little
132 };
133
134 /* Should opaque types be resolved? */
135
136 static bool opaque_type_resolution = true;
137
138 /* See gdbtypes.h. */
139
140 unsigned int overload_debug = 0;
141
142 /* A flag to enable strict type checking. */
143
144 static bool strict_type_checking = true;
145
146 /* A function to show whether opaque types are resolved. */
147
148 static void
149 show_opaque_type_resolution (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c,
151 const char *value)
152 {
153 gdb_printf (file, _("Resolution of opaque struct/class/union types "
154 "(if set before loading symbols) is %s.\n"),
155 value);
156 }
157
158 /* A function to show whether C++ overload debugging is enabled. */
159
160 static void
161 show_overload_debug (struct ui_file *file, int from_tty,
162 struct cmd_list_element *c, const char *value)
163 {
164 gdb_printf (file, _("Debugging of C++ overloading is %s.\n"),
165 value);
166 }
167
168 /* A function to show the status of strict type checking. */
169
170 static void
171 show_strict_type_checking (struct ui_file *file, int from_tty,
172 struct cmd_list_element *c, const char *value)
173 {
174 gdb_printf (file, _("Strict type checking is %s.\n"), value);
175 }
176
177 \f
178 /* Helper function to initialize a newly allocated type. Set type code
179 to CODE and initialize the type-specific fields accordingly. */
180
181 static void
182 set_type_code (struct type *type, enum type_code code)
183 {
184 type->set_code (code);
185
186 switch (code)
187 {
188 case TYPE_CODE_STRUCT:
189 case TYPE_CODE_UNION:
190 case TYPE_CODE_NAMESPACE:
191 INIT_CPLUS_SPECIFIC (type);
192 break;
193 case TYPE_CODE_FLT:
194 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
195 break;
196 case TYPE_CODE_FUNC:
197 INIT_FUNC_SPECIFIC (type);
198 break;
199 case TYPE_CODE_FIXED_POINT:
200 INIT_FIXED_POINT_SPECIFIC (type);
201 break;
202 }
203 }
204
205 /* See gdbtypes.h. */
206
207 type *
208 type_allocator::new_type ()
209 {
210 if (m_smash)
211 return m_data.type;
212
213 obstack *obstack = (m_is_objfile
214 ? &m_data.objfile->objfile_obstack
215 : gdbarch_obstack (m_data.gdbarch));
216
217 /* Alloc the structure and start off with all fields zeroed. */
218 struct type *type = OBSTACK_ZALLOC (obstack, struct type);
219 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (obstack, struct main_type);
220 TYPE_MAIN_TYPE (type)->m_lang = m_lang;
221
222 if (m_is_objfile)
223 {
224 OBJSTAT (m_data.objfile, n_types++);
225 type->set_owner (m_data.objfile);
226 }
227 else
228 type->set_owner (m_data.gdbarch);
229
230 /* Initialize the fields that might not be zero. */
231 type->set_code (TYPE_CODE_UNDEF);
232 TYPE_CHAIN (type) = type; /* Chain back to itself. */
233
234 return type;
235 }
236
237 /* See gdbtypes.h. */
238
239 type *
240 type_allocator::new_type (enum type_code code, int bit, const char *name)
241 {
242 struct type *type = new_type ();
243 set_type_code (type, code);
244 gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
245 type->set_length (bit / TARGET_CHAR_BIT);
246
247 if (name != nullptr)
248 {
249 obstack *obstack = (m_is_objfile
250 ? &m_data.objfile->objfile_obstack
251 : gdbarch_obstack (m_data.gdbarch));
252 type->set_name (obstack_strdup (obstack, name));
253 }
254
255 return type;
256 }
257
258 /* See gdbtypes.h. */
259
260 gdbarch *
261 type_allocator::arch ()
262 {
263 if (m_smash)
264 return m_data.type->arch ();
265 if (m_is_objfile)
266 return m_data.objfile->arch ();
267 return m_data.gdbarch;
268 }
269
270 /* See gdbtypes.h. */
271
272 gdbarch *
273 type::arch () const
274 {
275 struct gdbarch *arch;
276
277 if (this->is_objfile_owned ())
278 arch = this->objfile_owner ()->arch ();
279 else
280 arch = this->arch_owner ();
281
282 /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
283 a gdbarch, however, this is very rare, and even then, in most cases
284 that type::arch is called, we assume that a non-NULL value is
285 returned. */
286 gdb_assert (arch != nullptr);
287 return arch;
288 }
289
290 /* See gdbtypes.h. */
291
292 struct type *
293 get_target_type (struct type *type)
294 {
295 if (type != NULL)
296 {
297 type = type->target_type ();
298 if (type != NULL)
299 type = check_typedef (type);
300 }
301
302 return type;
303 }
304
305 /* See gdbtypes.h. */
306
307 unsigned int
308 type_length_units (struct type *type)
309 {
310 int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
311
312 return type->length () / unit_size;
313 }
314
315 /* Alloc a new type instance structure, fill it with some defaults,
316 and point it at OLDTYPE. Allocate the new type instance from the
317 same place as OLDTYPE. */
318
319 static struct type *
320 alloc_type_instance (struct type *oldtype)
321 {
322 struct type *type;
323
324 /* Allocate the structure. */
325
326 if (!oldtype->is_objfile_owned ())
327 type = GDBARCH_OBSTACK_ZALLOC (oldtype->arch_owner (), struct type);
328 else
329 type = OBSTACK_ZALLOC (&oldtype->objfile_owner ()->objfile_obstack,
330 struct type);
331
332 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
333
334 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
335
336 return type;
337 }
338
339 /* Clear all remnants of the previous type at TYPE, in preparation for
340 replacing it with something else. Preserve owner information. */
341
342 static void
343 smash_type (struct type *type)
344 {
345 bool objfile_owned = type->is_objfile_owned ();
346 objfile *objfile = type->objfile_owner ();
347 gdbarch *arch = type->arch_owner ();
348
349 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
350
351 /* Restore owner information. */
352 if (objfile_owned)
353 type->set_owner (objfile);
354 else
355 type->set_owner (arch);
356
357 /* For now, delete the rings. */
358 TYPE_CHAIN (type) = type;
359
360 /* For now, leave the pointer/reference types alone. */
361 }
362
363 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
364 to a pointer to memory where the pointer type should be stored.
365 If *TYPEPTR is zero, update it to point to the pointer type we return.
366 We allocate new memory if needed. */
367
368 struct type *
369 make_pointer_type (struct type *type, struct type **typeptr)
370 {
371 struct type *ntype; /* New type */
372 struct type *chain;
373
374 ntype = TYPE_POINTER_TYPE (type);
375
376 if (ntype)
377 {
378 if (typeptr == 0)
379 return ntype; /* Don't care about alloc,
380 and have new type. */
381 else if (*typeptr == 0)
382 {
383 *typeptr = ntype; /* Tracking alloc, and have new type. */
384 return ntype;
385 }
386 }
387
388 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
389 {
390 ntype = type_allocator (type).new_type ();
391 if (typeptr)
392 *typeptr = ntype;
393 }
394 else /* We have storage, but need to reset it. */
395 {
396 ntype = *typeptr;
397 chain = TYPE_CHAIN (ntype);
398 smash_type (ntype);
399 TYPE_CHAIN (ntype) = chain;
400 }
401
402 ntype->set_target_type (type);
403 TYPE_POINTER_TYPE (type) = ntype;
404
405 /* FIXME! Assumes the machine has only one representation for pointers! */
406
407 ntype->set_length (gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT);
408 ntype->set_code (TYPE_CODE_PTR);
409
410 /* Mark pointers as unsigned. The target converts between pointers
411 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
412 gdbarch_address_to_pointer. */
413 ntype->set_is_unsigned (true);
414
415 /* Update the length of all the other variants of this type. */
416 chain = TYPE_CHAIN (ntype);
417 while (chain != ntype)
418 {
419 chain->set_length (ntype->length ());
420 chain = TYPE_CHAIN (chain);
421 }
422
423 return ntype;
424 }
425
426 /* Given a type TYPE, return a type of pointers to that type.
427 May need to construct such a type if this is the first use. */
428
429 struct type *
430 lookup_pointer_type (struct type *type)
431 {
432 return make_pointer_type (type, (struct type **) 0);
433 }
434
435 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
436 points to a pointer to memory where the reference type should be
437 stored. If *TYPEPTR is zero, update it to point to the reference
438 type we return. We allocate new memory if needed. REFCODE denotes
439 the kind of reference type to lookup (lvalue or rvalue reference). */
440
441 struct type *
442 make_reference_type (struct type *type, struct type **typeptr,
443 enum type_code refcode)
444 {
445 struct type *ntype; /* New type */
446 struct type **reftype;
447 struct type *chain;
448
449 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
450
451 ntype = (refcode == TYPE_CODE_REF ? TYPE_REFERENCE_TYPE (type)
452 : TYPE_RVALUE_REFERENCE_TYPE (type));
453
454 if (ntype)
455 {
456 if (typeptr == 0)
457 return ntype; /* Don't care about alloc,
458 and have new type. */
459 else if (*typeptr == 0)
460 {
461 *typeptr = ntype; /* Tracking alloc, and have new type. */
462 return ntype;
463 }
464 }
465
466 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
467 {
468 ntype = type_allocator (type).new_type ();
469 if (typeptr)
470 *typeptr = ntype;
471 }
472 else /* We have storage, but need to reset it. */
473 {
474 ntype = *typeptr;
475 chain = TYPE_CHAIN (ntype);
476 smash_type (ntype);
477 TYPE_CHAIN (ntype) = chain;
478 }
479
480 ntype->set_target_type (type);
481 reftype = (refcode == TYPE_CODE_REF ? &TYPE_REFERENCE_TYPE (type)
482 : &TYPE_RVALUE_REFERENCE_TYPE (type));
483
484 *reftype = ntype;
485
486 /* FIXME! Assume the machine has only one representation for
487 references, and that it matches the (only) representation for
488 pointers! */
489
490 ntype->set_length (gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT);
491 ntype->set_code (refcode);
492
493 *reftype = ntype;
494
495 /* Update the length of all the other variants of this type. */
496 chain = TYPE_CHAIN (ntype);
497 while (chain != ntype)
498 {
499 chain->set_length (ntype->length ());
500 chain = TYPE_CHAIN (chain);
501 }
502
503 return ntype;
504 }
505
506 /* Same as above, but caller doesn't care about memory allocation
507 details. */
508
509 struct type *
510 lookup_reference_type (struct type *type, enum type_code refcode)
511 {
512 return make_reference_type (type, (struct type **) 0, refcode);
513 }
514
515 /* Lookup the lvalue reference type for the type TYPE. */
516
517 struct type *
518 lookup_lvalue_reference_type (struct type *type)
519 {
520 return lookup_reference_type (type, TYPE_CODE_REF);
521 }
522
523 /* Lookup the rvalue reference type for the type TYPE. */
524
525 struct type *
526 lookup_rvalue_reference_type (struct type *type)
527 {
528 return lookup_reference_type (type, TYPE_CODE_RVALUE_REF);
529 }
530
531 /* Lookup a function type that returns type TYPE. TYPEPTR, if
532 nonzero, points to a pointer to memory where the function type
533 should be stored. If *TYPEPTR is zero, update it to point to the
534 function type we return. We allocate new memory if needed. */
535
536 struct type *
537 make_function_type (struct type *type, struct type **typeptr)
538 {
539 struct type *ntype; /* New type */
540
541 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
542 {
543 ntype = type_allocator (type).new_type ();
544 if (typeptr)
545 *typeptr = ntype;
546 }
547 else /* We have storage, but need to reset it. */
548 {
549 ntype = *typeptr;
550 smash_type (ntype);
551 }
552
553 ntype->set_target_type (type);
554
555 ntype->set_length (1);
556 ntype->set_code (TYPE_CODE_FUNC);
557
558 INIT_FUNC_SPECIFIC (ntype);
559
560 return ntype;
561 }
562
563 /* Given a type TYPE, return a type of functions that return that type.
564 May need to construct such a type if this is the first use. */
565
566 struct type *
567 lookup_function_type (struct type *type)
568 {
569 return make_function_type (type, (struct type **) 0);
570 }
571
572 /* Given a type TYPE and argument types, return the appropriate
573 function type. If the final type in PARAM_TYPES is NULL, make a
574 varargs function. */
575
576 struct type *
577 lookup_function_type_with_arguments (struct type *type,
578 int nparams,
579 struct type **param_types)
580 {
581 struct type *fn = make_function_type (type, (struct type **) 0);
582 int i;
583
584 if (nparams > 0)
585 {
586 if (param_types[nparams - 1] == NULL)
587 {
588 --nparams;
589 fn->set_has_varargs (true);
590 }
591 else if (check_typedef (param_types[nparams - 1])->code ()
592 == TYPE_CODE_VOID)
593 {
594 --nparams;
595 /* Caller should have ensured this. */
596 gdb_assert (nparams == 0);
597 fn->set_is_prototyped (true);
598 }
599 else
600 fn->set_is_prototyped (true);
601 }
602
603 fn->alloc_fields (nparams);
604 for (i = 0; i < nparams; ++i)
605 fn->field (i).set_type (param_types[i]);
606
607 return fn;
608 }
609
610 /* Identify address space identifier by name -- return a
611 type_instance_flags. */
612
613 type_instance_flags
614 address_space_name_to_type_instance_flags (struct gdbarch *gdbarch,
615 const char *space_identifier)
616 {
617 type_instance_flags type_flags;
618
619 /* Check for known address space delimiters. */
620 if (!strcmp (space_identifier, "code"))
621 return TYPE_INSTANCE_FLAG_CODE_SPACE;
622 else if (!strcmp (space_identifier, "data"))
623 return TYPE_INSTANCE_FLAG_DATA_SPACE;
624 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
625 && gdbarch_address_class_name_to_type_flags (gdbarch,
626 space_identifier,
627 &type_flags))
628 return type_flags;
629 else
630 error (_("Unknown address space specifier: \"%s\""), space_identifier);
631 }
632
633 /* Identify address space identifier by type_instance_flags and return
634 the string version of the adress space name. */
635
636 const char *
637 address_space_type_instance_flags_to_name (struct gdbarch *gdbarch,
638 type_instance_flags space_flag)
639 {
640 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
641 return "code";
642 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
643 return "data";
644 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
645 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
646 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
647 else
648 return NULL;
649 }
650
651 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
652
653 If STORAGE is non-NULL, create the new type instance there.
654 STORAGE must be in the same obstack as TYPE. */
655
656 static struct type *
657 make_qualified_type (struct type *type, type_instance_flags new_flags,
658 struct type *storage)
659 {
660 struct type *ntype;
661
662 ntype = type;
663 do
664 {
665 if (ntype->instance_flags () == new_flags)
666 return ntype;
667 ntype = TYPE_CHAIN (ntype);
668 }
669 while (ntype != type);
670
671 /* Create a new type instance. */
672 if (storage == NULL)
673 ntype = alloc_type_instance (type);
674 else
675 {
676 /* If STORAGE was provided, it had better be in the same objfile
677 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
678 if one objfile is freed and the other kept, we'd have
679 dangling pointers. */
680 gdb_assert (type->objfile_owner () == storage->objfile_owner ());
681
682 ntype = storage;
683 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
684 TYPE_CHAIN (ntype) = ntype;
685 }
686
687 /* Pointers or references to the original type are not relevant to
688 the new type. */
689 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
690 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
691
692 /* Chain the new qualified type to the old type. */
693 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
694 TYPE_CHAIN (type) = ntype;
695
696 /* Now set the instance flags and return the new type. */
697 ntype->set_instance_flags (new_flags);
698
699 /* Set length of new type to that of the original type. */
700 ntype->set_length (type->length ());
701
702 return ntype;
703 }
704
705 /* Make an address-space-delimited variant of a type -- a type that
706 is identical to the one supplied except that it has an address
707 space attribute attached to it (such as "code" or "data").
708
709 The space attributes "code" and "data" are for Harvard
710 architectures. The address space attributes are for architectures
711 which have alternately sized pointers or pointers with alternate
712 representations. */
713
714 struct type *
715 make_type_with_address_space (struct type *type,
716 type_instance_flags space_flag)
717 {
718 type_instance_flags new_flags = ((type->instance_flags ()
719 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
720 | TYPE_INSTANCE_FLAG_DATA_SPACE
721 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
722 | space_flag);
723
724 return make_qualified_type (type, new_flags, NULL);
725 }
726
727 /* Make a "c-v" variant of a type -- a type that is identical to the
728 one supplied except that it may have const or volatile attributes
729 CNST is a flag for setting the const attribute
730 VOLTL is a flag for setting the volatile attribute
731 TYPE is the base type whose variant we are creating.
732
733 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
734 storage to hold the new qualified type; *TYPEPTR and TYPE must be
735 in the same objfile. Otherwise, allocate fresh memory for the new
736 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
737 new type we construct. */
738
739 struct type *
740 make_cv_type (int cnst, int voltl,
741 struct type *type,
742 struct type **typeptr)
743 {
744 struct type *ntype; /* New type */
745
746 type_instance_flags new_flags = (type->instance_flags ()
747 & ~(TYPE_INSTANCE_FLAG_CONST
748 | TYPE_INSTANCE_FLAG_VOLATILE));
749
750 if (cnst)
751 new_flags |= TYPE_INSTANCE_FLAG_CONST;
752
753 if (voltl)
754 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
755
756 if (typeptr && *typeptr != NULL)
757 {
758 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
759 a C-V variant chain that threads across objfiles: if one
760 objfile gets freed, then the other has a broken C-V chain.
761
762 This code used to try to copy over the main type from TYPE to
763 *TYPEPTR if they were in different objfiles, but that's
764 wrong, too: TYPE may have a field list or member function
765 lists, which refer to types of their own, etc. etc. The
766 whole shebang would need to be copied over recursively; you
767 can't have inter-objfile pointers. The only thing to do is
768 to leave stub types as stub types, and look them up afresh by
769 name each time you encounter them. */
770 gdb_assert ((*typeptr)->objfile_owner () == type->objfile_owner ());
771 }
772
773 ntype = make_qualified_type (type, new_flags,
774 typeptr ? *typeptr : NULL);
775
776 if (typeptr != NULL)
777 *typeptr = ntype;
778
779 return ntype;
780 }
781
782 /* Make a 'restrict'-qualified version of TYPE. */
783
784 struct type *
785 make_restrict_type (struct type *type)
786 {
787 return make_qualified_type (type,
788 (type->instance_flags ()
789 | TYPE_INSTANCE_FLAG_RESTRICT),
790 NULL);
791 }
792
793 /* Make a type without const, volatile, or restrict. */
794
795 struct type *
796 make_unqualified_type (struct type *type)
797 {
798 return make_qualified_type (type,
799 (type->instance_flags ()
800 & ~(TYPE_INSTANCE_FLAG_CONST
801 | TYPE_INSTANCE_FLAG_VOLATILE
802 | TYPE_INSTANCE_FLAG_RESTRICT)),
803 NULL);
804 }
805
806 /* Make a '_Atomic'-qualified version of TYPE. */
807
808 struct type *
809 make_atomic_type (struct type *type)
810 {
811 return make_qualified_type (type,
812 (type->instance_flags ()
813 | TYPE_INSTANCE_FLAG_ATOMIC),
814 NULL);
815 }
816
817 /* Replace the contents of ntype with the type *type. This changes the
818 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
819 the changes are propogated to all types in the TYPE_CHAIN.
820
821 In order to build recursive types, it's inevitable that we'll need
822 to update types in place --- but this sort of indiscriminate
823 smashing is ugly, and needs to be replaced with something more
824 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
825 clear if more steps are needed. */
826
827 void
828 replace_type (struct type *ntype, struct type *type)
829 {
830 struct type *chain;
831
832 /* These two types had better be in the same objfile. Otherwise,
833 the assignment of one type's main type structure to the other
834 will produce a type with references to objects (names; field
835 lists; etc.) allocated on an objfile other than its own. */
836 gdb_assert (ntype->objfile_owner () == type->objfile_owner ());
837
838 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
839
840 /* The type length is not a part of the main type. Update it for
841 each type on the variant chain. */
842 chain = ntype;
843 do
844 {
845 /* Assert that this element of the chain has no address-class bits
846 set in its flags. Such type variants might have type lengths
847 which are supposed to be different from the non-address-class
848 variants. This assertion shouldn't ever be triggered because
849 symbol readers which do construct address-class variants don't
850 call replace_type(). */
851 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
852
853 chain->set_length (type->length ());
854 chain = TYPE_CHAIN (chain);
855 }
856 while (ntype != chain);
857
858 /* Assert that the two types have equivalent instance qualifiers.
859 This should be true for at least all of our debug readers. */
860 gdb_assert (ntype->instance_flags () == type->instance_flags ());
861 }
862
863 /* Implement direct support for MEMBER_TYPE in GNU C++.
864 May need to construct such a type if this is the first use.
865 The TYPE is the type of the member. The DOMAIN is the type
866 of the aggregate that the member belongs to. */
867
868 struct type *
869 lookup_memberptr_type (struct type *type, struct type *domain)
870 {
871 struct type *mtype;
872
873 mtype = type_allocator (type).new_type ();
874 smash_to_memberptr_type (mtype, domain, type);
875 return mtype;
876 }
877
878 /* Return a pointer-to-method type, for a method of type TO_TYPE. */
879
880 struct type *
881 lookup_methodptr_type (struct type *to_type)
882 {
883 struct type *mtype;
884
885 mtype = type_allocator (to_type).new_type ();
886 smash_to_methodptr_type (mtype, to_type);
887 return mtype;
888 }
889
890 /* See gdbtypes.h. */
891
892 bool
893 operator== (const dynamic_prop &l, const dynamic_prop &r)
894 {
895 if (l.kind () != r.kind ())
896 return false;
897
898 switch (l.kind ())
899 {
900 case PROP_UNDEFINED:
901 return true;
902 case PROP_CONST:
903 return l.const_val () == r.const_val ();
904 case PROP_ADDR_OFFSET:
905 case PROP_LOCEXPR:
906 case PROP_LOCLIST:
907 return l.baton () == r.baton ();
908 case PROP_VARIANT_PARTS:
909 return l.variant_parts () == r.variant_parts ();
910 case PROP_TYPE:
911 return l.original_type () == r.original_type ();
912 }
913
914 gdb_assert_not_reached ("unhandled dynamic_prop kind");
915 }
916
917 /* See gdbtypes.h. */
918
919 bool
920 operator== (const range_bounds &l, const range_bounds &r)
921 {
922 #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
923
924 return (FIELD_EQ (low)
925 && FIELD_EQ (high)
926 && FIELD_EQ (flag_upper_bound_is_count)
927 && FIELD_EQ (flag_bound_evaluated)
928 && FIELD_EQ (bias));
929
930 #undef FIELD_EQ
931 }
932
933 /* See gdbtypes.h. */
934
935 struct type *
936 create_range_type (type_allocator &alloc, struct type *index_type,
937 const struct dynamic_prop *low_bound,
938 const struct dynamic_prop *high_bound,
939 LONGEST bias)
940 {
941 /* The INDEX_TYPE should be a type capable of holding the upper and lower
942 bounds, as such a zero sized, or void type makes no sense. */
943 gdb_assert (index_type->code () != TYPE_CODE_VOID);
944 gdb_assert (index_type->length () > 0);
945
946 struct type *result_type = alloc.new_type ();
947 result_type->set_code (TYPE_CODE_RANGE);
948 result_type->set_target_type (index_type);
949 if (index_type->is_stub ())
950 result_type->set_target_is_stub (true);
951 else
952 result_type->set_length (check_typedef (index_type)->length ());
953
954 range_bounds *bounds
955 = (struct range_bounds *) TYPE_ZALLOC (result_type, sizeof (range_bounds));
956 bounds->low = *low_bound;
957 bounds->high = *high_bound;
958 bounds->bias = bias;
959 bounds->stride.set_const_val (0);
960
961 result_type->set_bounds (bounds);
962
963 if (index_type->code () == TYPE_CODE_FIXED_POINT)
964 result_type->set_is_unsigned (index_type->is_unsigned ());
965 else if (index_type->is_unsigned ())
966 {
967 /* If the underlying type is unsigned, then the range
968 necessarily is. */
969 result_type->set_is_unsigned (true);
970 }
971 /* Otherwise, the signed-ness of a range type can't simply be copied
972 from the underlying type. Consider a case where the underlying
973 type is 'int', but the range type can hold 0..65535, and where
974 the range is further specified to fit into 16 bits. In this
975 case, if we copy the underlying type's sign, then reading some
976 range values will cause an unwanted sign extension. So, we have
977 some heuristics here instead. */
978 else if (low_bound->is_constant () && low_bound->const_val () >= 0)
979 {
980 result_type->set_is_unsigned (true);
981 /* Ada allows the declaration of range types whose upper bound is
982 less than the lower bound, so checking the lower bound is not
983 enough. Make sure we do not mark a range type whose upper bound
984 is negative as unsigned. */
985 if (high_bound->is_constant () && high_bound->const_val () < 0)
986 result_type->set_is_unsigned (false);
987 }
988
989 result_type->set_endianity_is_not_default
990 (index_type->endianity_is_not_default ());
991
992 return result_type;
993 }
994
995 /* See gdbtypes.h. */
996
997 struct type *
998 create_range_type_with_stride (type_allocator &alloc,
999 struct type *index_type,
1000 const struct dynamic_prop *low_bound,
1001 const struct dynamic_prop *high_bound,
1002 LONGEST bias,
1003 const struct dynamic_prop *stride,
1004 bool byte_stride_p)
1005 {
1006 struct type *result_type = create_range_type (alloc, index_type, low_bound,
1007 high_bound, bias);
1008
1009 gdb_assert (stride != nullptr);
1010 result_type->bounds ()->stride = *stride;
1011 result_type->bounds ()->flag_is_byte_stride = byte_stride_p;
1012
1013 return result_type;
1014 }
1015
1016 /* See gdbtypes.h. */
1017
1018 struct type *
1019 create_static_range_type (type_allocator &alloc, struct type *index_type,
1020 LONGEST low_bound, LONGEST high_bound)
1021 {
1022 struct dynamic_prop low, high;
1023
1024 low.set_const_val (low_bound);
1025 high.set_const_val (high_bound);
1026
1027 struct type *result_type = create_range_type (alloc, index_type,
1028 &low, &high, 0);
1029
1030 return result_type;
1031 }
1032
1033 /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
1034 are static, otherwise returns 0. */
1035
1036 static bool
1037 has_static_range (const struct range_bounds *bounds)
1038 {
1039 /* If the range doesn't have a defined stride then its stride field will
1040 be initialized to the constant 0. */
1041 return (bounds->low.is_constant ()
1042 && bounds->high.is_constant ()
1043 && bounds->stride.is_constant ());
1044 }
1045
1046 /* See gdbtypes.h. */
1047
1048 gdb::optional<LONGEST>
1049 get_discrete_low_bound (struct type *type)
1050 {
1051 type = check_typedef (type);
1052 switch (type->code ())
1053 {
1054 case TYPE_CODE_RANGE:
1055 {
1056 /* This function only works for ranges with a constant low bound. */
1057 if (!type->bounds ()->low.is_constant ())
1058 return {};
1059
1060 LONGEST low = type->bounds ()->low.const_val ();
1061
1062 if (type->target_type ()->code () == TYPE_CODE_ENUM)
1063 {
1064 gdb::optional<LONGEST> low_pos
1065 = discrete_position (type->target_type (), low);
1066
1067 if (low_pos.has_value ())
1068 low = *low_pos;
1069 }
1070
1071 return low;
1072 }
1073
1074 case TYPE_CODE_ENUM:
1075 {
1076 if (type->num_fields () > 0)
1077 {
1078 /* The enums may not be sorted by value, so search all
1079 entries. */
1080 LONGEST low = type->field (0).loc_enumval ();
1081
1082 for (int i = 0; i < type->num_fields (); i++)
1083 {
1084 if (type->field (i).loc_enumval () < low)
1085 low = type->field (i).loc_enumval ();
1086 }
1087
1088 return low;
1089 }
1090 else
1091 return 0;
1092 }
1093
1094 case TYPE_CODE_BOOL:
1095 return 0;
1096
1097 case TYPE_CODE_INT:
1098 if (type->length () > sizeof (LONGEST)) /* Too big */
1099 return {};
1100
1101 if (!type->is_unsigned ())
1102 return -(1 << (type->length () * TARGET_CHAR_BIT - 1));
1103
1104 /* fall through */
1105 case TYPE_CODE_CHAR:
1106 return 0;
1107
1108 default:
1109 return {};
1110 }
1111 }
1112
1113 /* See gdbtypes.h. */
1114
1115 gdb::optional<LONGEST>
1116 get_discrete_high_bound (struct type *type)
1117 {
1118 type = check_typedef (type);
1119 switch (type->code ())
1120 {
1121 case TYPE_CODE_RANGE:
1122 {
1123 /* This function only works for ranges with a constant high bound. */
1124 if (!type->bounds ()->high.is_constant ())
1125 return {};
1126
1127 LONGEST high = type->bounds ()->high.const_val ();
1128
1129 if (type->target_type ()->code () == TYPE_CODE_ENUM)
1130 {
1131 gdb::optional<LONGEST> high_pos
1132 = discrete_position (type->target_type (), high);
1133
1134 if (high_pos.has_value ())
1135 high = *high_pos;
1136 }
1137
1138 return high;
1139 }
1140
1141 case TYPE_CODE_ENUM:
1142 {
1143 if (type->num_fields () > 0)
1144 {
1145 /* The enums may not be sorted by value, so search all
1146 entries. */
1147 LONGEST high = type->field (0).loc_enumval ();
1148
1149 for (int i = 0; i < type->num_fields (); i++)
1150 {
1151 if (type->field (i).loc_enumval () > high)
1152 high = type->field (i).loc_enumval ();
1153 }
1154
1155 return high;
1156 }
1157 else
1158 return -1;
1159 }
1160
1161 case TYPE_CODE_BOOL:
1162 return 1;
1163
1164 case TYPE_CODE_INT:
1165 if (type->length () > sizeof (LONGEST)) /* Too big */
1166 return {};
1167
1168 if (!type->is_unsigned ())
1169 {
1170 LONGEST low = -(1 << (type->length () * TARGET_CHAR_BIT - 1));
1171 return -low - 1;
1172 }
1173
1174 /* fall through */
1175 case TYPE_CODE_CHAR:
1176 {
1177 /* This round-about calculation is to avoid shifting by
1178 type->length () * TARGET_CHAR_BIT, which will not work
1179 if type->length () == sizeof (LONGEST). */
1180 LONGEST high = 1 << (type->length () * TARGET_CHAR_BIT - 1);
1181 return (high - 1) | high;
1182 }
1183
1184 default:
1185 return {};
1186 }
1187 }
1188
1189 /* See gdbtypes.h. */
1190
1191 bool
1192 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
1193 {
1194 gdb::optional<LONGEST> low = get_discrete_low_bound (type);
1195 if (!low.has_value ())
1196 return false;
1197
1198 gdb::optional<LONGEST> high = get_discrete_high_bound (type);
1199 if (!high.has_value ())
1200 return false;
1201
1202 *lowp = *low;
1203 *highp = *high;
1204
1205 return true;
1206 }
1207
1208 /* See gdbtypes.h */
1209
1210 bool
1211 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
1212 {
1213 struct type *index = type->index_type ();
1214 LONGEST low = 0;
1215 LONGEST high = 0;
1216
1217 if (index == NULL)
1218 return false;
1219
1220 if (!get_discrete_bounds (index, &low, &high))
1221 return false;
1222
1223 if (low_bound)
1224 *low_bound = low;
1225
1226 if (high_bound)
1227 *high_bound = high;
1228
1229 return true;
1230 }
1231
1232 /* Assuming that TYPE is a discrete type and VAL is a valid integer
1233 representation of a value of this type, save the corresponding
1234 position number in POS.
1235
1236 Its differs from VAL only in the case of enumeration types. In
1237 this case, the position number of the value of the first listed
1238 enumeration literal is zero; the position number of the value of
1239 each subsequent enumeration literal is one more than that of its
1240 predecessor in the list.
1241
1242 Return 1 if the operation was successful. Return zero otherwise,
1243 in which case the value of POS is unmodified.
1244 */
1245
1246 gdb::optional<LONGEST>
1247 discrete_position (struct type *type, LONGEST val)
1248 {
1249 if (type->code () == TYPE_CODE_RANGE)
1250 type = type->target_type ();
1251
1252 if (type->code () == TYPE_CODE_ENUM)
1253 {
1254 int i;
1255
1256 for (i = 0; i < type->num_fields (); i += 1)
1257 {
1258 if (val == type->field (i).loc_enumval ())
1259 return i;
1260 }
1261
1262 /* Invalid enumeration value. */
1263 return {};
1264 }
1265 else
1266 return val;
1267 }
1268
1269 /* If the array TYPE has static bounds calculate and update its
1270 size, then return true. Otherwise return false and leave TYPE
1271 unchanged. */
1272
1273 static bool
1274 update_static_array_size (struct type *type)
1275 {
1276 gdb_assert (type->code () == TYPE_CODE_ARRAY);
1277
1278 struct type *range_type = type->index_type ();
1279
1280 if (type->dyn_prop (DYN_PROP_BYTE_STRIDE) == nullptr
1281 && has_static_range (range_type->bounds ())
1282 && (!type_not_associated (type)
1283 && !type_not_allocated (type)))
1284 {
1285 LONGEST low_bound, high_bound;
1286 int stride;
1287 struct type *element_type;
1288
1289 stride = type->bit_stride ();
1290
1291 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
1292 low_bound = high_bound = 0;
1293
1294 element_type = check_typedef (type->target_type ());
1295 /* Be careful when setting the array length. Ada arrays can be
1296 empty arrays with the high_bound being smaller than the low_bound.
1297 In such cases, the array length should be zero. */
1298 if (high_bound < low_bound)
1299 type->set_length (0);
1300 else if (stride != 0)
1301 {
1302 /* Ensure that the type length is always positive, even in the
1303 case where (for example in Fortran) we have a negative
1304 stride. It is possible to have a single element array with a
1305 negative stride in Fortran (this doesn't mean anything
1306 special, it's still just a single element array) so do
1307 consider that case when touching this code. */
1308 LONGEST element_count = std::abs (high_bound - low_bound + 1);
1309 type->set_length (((std::abs (stride) * element_count) + 7) / 8);
1310 }
1311 else
1312 type->set_length (element_type->length ()
1313 * (high_bound - low_bound + 1));
1314
1315 /* If this array's element is itself an array with a bit stride,
1316 then we want to update this array's bit stride to reflect the
1317 size of the sub-array. Otherwise, we'll end up using the
1318 wrong size when trying to find elements of the outer
1319 array. */
1320 if (element_type->code () == TYPE_CODE_ARRAY
1321 && (stride != 0 || element_type->is_multi_dimensional ())
1322 && element_type->length () != 0
1323 && element_type->field (0).bitsize () != 0
1324 && get_array_bounds (element_type, &low_bound, &high_bound)
1325 && high_bound >= low_bound)
1326 type->field (0).set_bitsize
1327 ((high_bound - low_bound + 1)
1328 * element_type->field (0).bitsize ());
1329
1330 return true;
1331 }
1332
1333 return false;
1334 }
1335
1336 /* See gdbtypes.h. */
1337
1338 struct type *
1339 create_array_type_with_stride (type_allocator &alloc,
1340 struct type *element_type,
1341 struct type *range_type,
1342 struct dynamic_prop *byte_stride_prop,
1343 unsigned int bit_stride)
1344 {
1345 if (byte_stride_prop != nullptr && byte_stride_prop->is_constant ())
1346 {
1347 /* The byte stride is actually not dynamic. Pretend we were
1348 called with bit_stride set instead of byte_stride_prop.
1349 This will give us the same result type, while avoiding
1350 the need to handle this as a special case. */
1351 bit_stride = byte_stride_prop->const_val () * 8;
1352 byte_stride_prop = NULL;
1353 }
1354
1355 struct type *result_type = alloc.new_type ();
1356
1357 result_type->set_code (TYPE_CODE_ARRAY);
1358 result_type->set_target_type (element_type);
1359
1360 result_type->alloc_fields (1);
1361 result_type->set_index_type (range_type);
1362 if (byte_stride_prop != NULL)
1363 result_type->add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop);
1364 else if (bit_stride > 0)
1365 result_type->field (0).set_bitsize (bit_stride);
1366
1367 if (!update_static_array_size (result_type))
1368 {
1369 /* This type is dynamic and its length needs to be computed
1370 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1371 undefined by setting it to zero. Although we are not expected
1372 to trust TYPE_LENGTH in this case, setting the size to zero
1373 allows us to avoid allocating objects of random sizes in case
1374 we accidently do. */
1375 result_type->set_length (0);
1376 }
1377
1378 /* TYPE_TARGET_STUB will take care of zero length arrays. */
1379 if (result_type->length () == 0)
1380 result_type->set_target_is_stub (true);
1381
1382 return result_type;
1383 }
1384
1385 /* See gdbtypes.h. */
1386
1387 struct type *
1388 create_array_type (type_allocator &alloc,
1389 struct type *element_type,
1390 struct type *range_type)
1391 {
1392 return create_array_type_with_stride (alloc, element_type,
1393 range_type, NULL, 0);
1394 }
1395
1396 struct type *
1397 lookup_array_range_type (struct type *element_type,
1398 LONGEST low_bound, LONGEST high_bound)
1399 {
1400 struct type *index_type;
1401 struct type *range_type;
1402
1403 type_allocator alloc (element_type);
1404 index_type = builtin_type (element_type->arch ())->builtin_int;
1405
1406 range_type = create_static_range_type (alloc, index_type,
1407 low_bound, high_bound);
1408
1409 return create_array_type (alloc, element_type, range_type);
1410 }
1411
1412 /* See gdbtypes.h. */
1413
1414 struct type *
1415 create_string_type (type_allocator &alloc,
1416 struct type *string_char_type,
1417 struct type *range_type)
1418 {
1419 struct type *result_type = create_array_type (alloc,
1420 string_char_type,
1421 range_type);
1422 result_type->set_code (TYPE_CODE_STRING);
1423 return result_type;
1424 }
1425
1426 struct type *
1427 lookup_string_range_type (struct type *string_char_type,
1428 LONGEST low_bound, LONGEST high_bound)
1429 {
1430 struct type *result_type;
1431
1432 result_type = lookup_array_range_type (string_char_type,
1433 low_bound, high_bound);
1434 result_type->set_code (TYPE_CODE_STRING);
1435 return result_type;
1436 }
1437
1438 struct type *
1439 create_set_type (type_allocator &alloc, struct type *domain_type)
1440 {
1441 struct type *result_type = alloc.new_type ();
1442
1443 result_type->set_code (TYPE_CODE_SET);
1444 result_type->alloc_fields (1);
1445
1446 if (!domain_type->is_stub ())
1447 {
1448 LONGEST low_bound, high_bound, bit_length;
1449
1450 if (!get_discrete_bounds (domain_type, &low_bound, &high_bound))
1451 low_bound = high_bound = 0;
1452
1453 bit_length = high_bound - low_bound + 1;
1454 result_type->set_length ((bit_length + TARGET_CHAR_BIT - 1)
1455 / TARGET_CHAR_BIT);
1456 if (low_bound >= 0)
1457 result_type->set_is_unsigned (true);
1458 }
1459 result_type->field (0).set_type (domain_type);
1460
1461 return result_type;
1462 }
1463
1464 /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1465 and any array types nested inside it. */
1466
1467 void
1468 make_vector_type (struct type *array_type)
1469 {
1470 struct type *inner_array, *elt_type;
1471
1472 /* Find the innermost array type, in case the array is
1473 multi-dimensional. */
1474 inner_array = array_type;
1475 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
1476 inner_array = inner_array->target_type ();
1477
1478 elt_type = inner_array->target_type ();
1479 if (elt_type->code () == TYPE_CODE_INT)
1480 {
1481 type_instance_flags flags
1482 = elt_type->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT;
1483 elt_type = make_qualified_type (elt_type, flags, NULL);
1484 inner_array->set_target_type (elt_type);
1485 }
1486
1487 array_type->set_is_vector (true);
1488 }
1489
1490 struct type *
1491 init_vector_type (struct type *elt_type, int n)
1492 {
1493 struct type *array_type;
1494
1495 array_type = lookup_array_range_type (elt_type, 0, n - 1);
1496 make_vector_type (array_type);
1497 return array_type;
1498 }
1499
1500 /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
1501 belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
1502 confusing. "self" is a common enough replacement for "this".
1503 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1504 TYPE_CODE_METHOD. */
1505
1506 struct type *
1507 internal_type_self_type (struct type *type)
1508 {
1509 switch (type->code ())
1510 {
1511 case TYPE_CODE_METHODPTR:
1512 case TYPE_CODE_MEMBERPTR:
1513 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1514 return NULL;
1515 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1516 return TYPE_MAIN_TYPE (type)->type_specific.self_type;
1517 case TYPE_CODE_METHOD:
1518 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1519 return NULL;
1520 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1521 return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type;
1522 default:
1523 gdb_assert_not_reached ("bad type");
1524 }
1525 }
1526
1527 /* Set the type of the class that TYPE belongs to.
1528 In c++ this is the class of "this".
1529 TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
1530 TYPE_CODE_METHOD. */
1531
1532 void
1533 set_type_self_type (struct type *type, struct type *self_type)
1534 {
1535 switch (type->code ())
1536 {
1537 case TYPE_CODE_METHODPTR:
1538 case TYPE_CODE_MEMBERPTR:
1539 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1540 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE;
1541 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
1542 TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type;
1543 break;
1544 case TYPE_CODE_METHOD:
1545 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
1546 INIT_FUNC_SPECIFIC (type);
1547 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
1548 TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type;
1549 break;
1550 default:
1551 gdb_assert_not_reached ("bad type");
1552 }
1553 }
1554
1555 /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
1556 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1557 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1558 TYPE doesn't include the offset (that's the value of the MEMBER
1559 itself), but does include the structure type into which it points
1560 (for some reason).
1561
1562 When "smashing" the type, we preserve the objfile that the old type
1563 pointed to, since we aren't changing where the type is actually
1564 allocated. */
1565
1566 void
1567 smash_to_memberptr_type (struct type *type, struct type *self_type,
1568 struct type *to_type)
1569 {
1570 smash_type (type);
1571 type->set_code (TYPE_CODE_MEMBERPTR);
1572 type->set_target_type (to_type);
1573 set_type_self_type (type, self_type);
1574 /* Assume that a data member pointer is the same size as a normal
1575 pointer. */
1576 type->set_length (gdbarch_ptr_bit (to_type->arch ()) / TARGET_CHAR_BIT);
1577 }
1578
1579 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1580
1581 When "smashing" the type, we preserve the objfile that the old type
1582 pointed to, since we aren't changing where the type is actually
1583 allocated. */
1584
1585 void
1586 smash_to_methodptr_type (struct type *type, struct type *to_type)
1587 {
1588 smash_type (type);
1589 type->set_code (TYPE_CODE_METHODPTR);
1590 type->set_target_type (to_type);
1591 set_type_self_type (type, TYPE_SELF_TYPE (to_type));
1592 type->set_length (cplus_method_ptr_size (to_type));
1593 }
1594
1595 /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
1596 METHOD just means `function that gets an extra "this" argument'.
1597
1598 When "smashing" the type, we preserve the objfile that the old type
1599 pointed to, since we aren't changing where the type is actually
1600 allocated. */
1601
1602 void
1603 smash_to_method_type (struct type *type, struct type *self_type,
1604 struct type *to_type, struct field *args,
1605 int nargs, int varargs)
1606 {
1607 smash_type (type);
1608 type->set_code (TYPE_CODE_METHOD);
1609 type->set_target_type (to_type);
1610 set_type_self_type (type, self_type);
1611 type->set_fields (args);
1612 type->set_num_fields (nargs);
1613
1614 if (varargs)
1615 type->set_has_varargs (true);
1616
1617 /* In practice, this is never needed. */
1618 type->set_length (1);
1619 }
1620
1621 /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
1622 Since GCC PR debug/47510 DWARF provides associated information to detect the
1623 anonymous class linkage name from its typedef.
1624
1625 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1626 apply it itself. */
1627
1628 const char *
1629 type_name_or_error (struct type *type)
1630 {
1631 struct type *saved_type = type;
1632 const char *name;
1633 struct objfile *objfile;
1634
1635 type = check_typedef (type);
1636
1637 name = type->name ();
1638 if (name != NULL)
1639 return name;
1640
1641 name = saved_type->name ();
1642 objfile = saved_type->objfile_owner ();
1643 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1644 name ? name : "<anonymous>",
1645 objfile ? objfile_name (objfile) : "<arch>");
1646 }
1647
1648 /* See gdbtypes.h. */
1649
1650 struct type *
1651 lookup_typename (const struct language_defn *language,
1652 const char *name,
1653 const struct block *block, int noerr)
1654 {
1655 struct symbol *sym;
1656
1657 sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
1658 language->la_language, NULL).symbol;
1659 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
1660 {
1661 struct type *type = sym->type ();
1662 /* Ensure the length of TYPE is valid. */
1663 check_typedef (type);
1664 return type;
1665 }
1666
1667 if (noerr)
1668 return NULL;
1669 error (_("No type named %s."), name);
1670 }
1671
1672 struct type *
1673 lookup_unsigned_typename (const struct language_defn *language,
1674 const char *name)
1675 {
1676 std::string uns;
1677 uns.reserve (strlen (name) + strlen ("unsigned "));
1678 uns = "unsigned ";
1679 uns += name;
1680
1681 return lookup_typename (language, uns.c_str (), NULL, 0);
1682 }
1683
1684 struct type *
1685 lookup_signed_typename (const struct language_defn *language, const char *name)
1686 {
1687 /* In C and C++, "char" and "signed char" are distinct types. */
1688 if (streq (name, "char"))
1689 name = "signed char";
1690 return lookup_typename (language, name, NULL, 0);
1691 }
1692
1693 /* Lookup a structure type named "struct NAME",
1694 visible in lexical block BLOCK. */
1695
1696 struct type *
1697 lookup_struct (const char *name, const struct block *block)
1698 {
1699 struct symbol *sym;
1700
1701 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1702
1703 if (sym == NULL)
1704 {
1705 error (_("No struct type named %s."), name);
1706 }
1707 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1708 {
1709 error (_("This context has class, union or enum %s, not a struct."),
1710 name);
1711 }
1712 return (sym->type ());
1713 }
1714
1715 /* Lookup a union type named "union NAME",
1716 visible in lexical block BLOCK. */
1717
1718 struct type *
1719 lookup_union (const char *name, const struct block *block)
1720 {
1721 struct symbol *sym;
1722 struct type *t;
1723
1724 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1725
1726 if (sym == NULL)
1727 error (_("No union type named %s."), name);
1728
1729 t = sym->type ();
1730
1731 if (t->code () == TYPE_CODE_UNION)
1732 return t;
1733
1734 /* If we get here, it's not a union. */
1735 error (_("This context has class, struct or enum %s, not a union."),
1736 name);
1737 }
1738
1739 /* Lookup an enum type named "enum NAME",
1740 visible in lexical block BLOCK. */
1741
1742 struct type *
1743 lookup_enum (const char *name, const struct block *block)
1744 {
1745 struct symbol *sym;
1746
1747 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
1748 if (sym == NULL)
1749 {
1750 error (_("No enum type named %s."), name);
1751 }
1752 if (sym->type ()->code () != TYPE_CODE_ENUM)
1753 {
1754 error (_("This context has class, struct or union %s, not an enum."),
1755 name);
1756 }
1757 return (sym->type ());
1758 }
1759
1760 /* Lookup a template type named "template NAME<TYPE>",
1761 visible in lexical block BLOCK. */
1762
1763 struct type *
1764 lookup_template_type (const char *name, struct type *type,
1765 const struct block *block)
1766 {
1767 std::string nam;
1768 nam.reserve (strlen (name) + strlen (type->name ()) + strlen ("< >"));
1769 nam = name;
1770 nam += "<";
1771 nam += type->name ();
1772 nam += " >"; /* FIXME, extra space still introduced in gcc? */
1773
1774 symbol *sym = lookup_symbol (nam.c_str (), block, VAR_DOMAIN, 0).symbol;
1775
1776 if (sym == NULL)
1777 {
1778 error (_("No template type named %s."), name);
1779 }
1780 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1781 {
1782 error (_("This context has class, union or enum %s, not a struct."),
1783 name);
1784 }
1785 return (sym->type ());
1786 }
1787
1788 /* See gdbtypes.h. */
1789
1790 struct_elt
1791 lookup_struct_elt (struct type *type, const char *name, int noerr)
1792 {
1793 int i;
1794
1795 for (;;)
1796 {
1797 type = check_typedef (type);
1798 if (type->code () != TYPE_CODE_PTR
1799 && type->code () != TYPE_CODE_REF)
1800 break;
1801 type = type->target_type ();
1802 }
1803
1804 if (type->code () != TYPE_CODE_STRUCT
1805 && type->code () != TYPE_CODE_UNION)
1806 {
1807 std::string type_name = type_to_string (type);
1808 error (_("Type %s is not a structure or union type."),
1809 type_name.c_str ());
1810 }
1811
1812 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
1813 {
1814 const char *t_field_name = type->field (i).name ();
1815
1816 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1817 {
1818 return {&type->field (i), type->field (i).loc_bitpos ()};
1819 }
1820 else if (!t_field_name || *t_field_name == '\0')
1821 {
1822 struct_elt elt
1823 = lookup_struct_elt (type->field (i).type (), name, 1);
1824 if (elt.field != NULL)
1825 {
1826 elt.offset += type->field (i).loc_bitpos ();
1827 return elt;
1828 }
1829 }
1830 }
1831
1832 /* OK, it's not in this class. Recursively check the baseclasses. */
1833 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1834 {
1835 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1836 if (elt.field != NULL)
1837 return elt;
1838 }
1839
1840 if (noerr)
1841 return {nullptr, 0};
1842
1843 std::string type_name = type_to_string (type);
1844 error (_("Type %s has no component named %s."), type_name.c_str (), name);
1845 }
1846
1847 /* See gdbtypes.h. */
1848
1849 struct type *
1850 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1851 {
1852 struct_elt elt = lookup_struct_elt (type, name, noerr);
1853 if (elt.field != NULL)
1854 return elt.field->type ();
1855 else
1856 return NULL;
1857 }
1858
1859 /* Return the largest number representable by unsigned integer type TYPE. */
1860
1861 ULONGEST
1862 get_unsigned_type_max (struct type *type)
1863 {
1864 unsigned int n;
1865
1866 type = check_typedef (type);
1867 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
1868 gdb_assert (type->length () <= sizeof (ULONGEST));
1869
1870 /* Written this way to avoid overflow. */
1871 n = type->length () * TARGET_CHAR_BIT;
1872 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1873 }
1874
1875 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1876 signed integer type TYPE. */
1877
1878 void
1879 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1880 {
1881 unsigned int n;
1882
1883 type = check_typedef (type);
1884 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
1885 gdb_assert (type->length () <= sizeof (LONGEST));
1886
1887 n = type->length () * TARGET_CHAR_BIT;
1888 *min = -((ULONGEST) 1 << (n - 1));
1889 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1890 }
1891
1892 /* Return the largest value representable by pointer type TYPE. */
1893
1894 CORE_ADDR
1895 get_pointer_type_max (struct type *type)
1896 {
1897 unsigned int n;
1898
1899 type = check_typedef (type);
1900 gdb_assert (type->code () == TYPE_CODE_PTR);
1901 gdb_assert (type->length () <= sizeof (CORE_ADDR));
1902
1903 n = type->length () * TARGET_CHAR_BIT;
1904 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1905 }
1906
1907 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1908 cplus_stuff.vptr_fieldno.
1909
1910 cplus_stuff is initialized to cplus_struct_default which does not
1911 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1912 designated initializers). We cope with that here. */
1913
1914 int
1915 internal_type_vptr_fieldno (struct type *type)
1916 {
1917 type = check_typedef (type);
1918 gdb_assert (type->code () == TYPE_CODE_STRUCT
1919 || type->code () == TYPE_CODE_UNION);
1920 if (!HAVE_CPLUS_STRUCT (type))
1921 return -1;
1922 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1923 }
1924
1925 /* Set the value of cplus_stuff.vptr_fieldno. */
1926
1927 void
1928 set_type_vptr_fieldno (struct type *type, int fieldno)
1929 {
1930 type = check_typedef (type);
1931 gdb_assert (type->code () == TYPE_CODE_STRUCT
1932 || type->code () == TYPE_CODE_UNION);
1933 if (!HAVE_CPLUS_STRUCT (type))
1934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1935 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1936 }
1937
1938 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1939 cplus_stuff.vptr_basetype. */
1940
1941 struct type *
1942 internal_type_vptr_basetype (struct type *type)
1943 {
1944 type = check_typedef (type);
1945 gdb_assert (type->code () == TYPE_CODE_STRUCT
1946 || type->code () == TYPE_CODE_UNION);
1947 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1948 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1949 }
1950
1951 /* Set the value of cplus_stuff.vptr_basetype. */
1952
1953 void
1954 set_type_vptr_basetype (struct type *type, struct type *basetype)
1955 {
1956 type = check_typedef (type);
1957 gdb_assert (type->code () == TYPE_CODE_STRUCT
1958 || type->code () == TYPE_CODE_UNION);
1959 if (!HAVE_CPLUS_STRUCT (type))
1960 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1961 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1962 }
1963
1964 /* Lookup the vptr basetype/fieldno values for TYPE.
1965 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1966 vptr_fieldno. Also, if found and basetype is from the same objfile,
1967 cache the results.
1968 If not found, return -1 and ignore BASETYPEP.
1969 Callers should be aware that in some cases (for example,
1970 the type or one of its baseclasses is a stub type and we are
1971 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1972 this function will not be able to find the
1973 virtual function table pointer, and vptr_fieldno will remain -1 and
1974 vptr_basetype will remain NULL or incomplete. */
1975
1976 int
1977 get_vptr_fieldno (struct type *type, struct type **basetypep)
1978 {
1979 type = check_typedef (type);
1980
1981 if (TYPE_VPTR_FIELDNO (type) < 0)
1982 {
1983 int i;
1984
1985 /* We must start at zero in case the first (and only) baseclass
1986 is virtual (and hence we cannot share the table pointer). */
1987 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1988 {
1989 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1990 int fieldno;
1991 struct type *basetype;
1992
1993 fieldno = get_vptr_fieldno (baseclass, &basetype);
1994 if (fieldno >= 0)
1995 {
1996 /* If the type comes from a different objfile we can't cache
1997 it, it may have a different lifetime. PR 2384 */
1998 if (type->objfile_owner () == basetype->objfile_owner ())
1999 {
2000 set_type_vptr_fieldno (type, fieldno);
2001 set_type_vptr_basetype (type, basetype);
2002 }
2003 if (basetypep)
2004 *basetypep = basetype;
2005 return fieldno;
2006 }
2007 }
2008
2009 /* Not found. */
2010 return -1;
2011 }
2012 else
2013 {
2014 if (basetypep)
2015 *basetypep = TYPE_VPTR_BASETYPE (type);
2016 return TYPE_VPTR_FIELDNO (type);
2017 }
2018 }
2019
2020 static void
2021 stub_noname_complaint (void)
2022 {
2023 complaint (_("stub type has NULL name"));
2024 }
2025
2026 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2027 attached to it, and that property has a non-constant value. */
2028
2029 static int
2030 array_type_has_dynamic_stride (struct type *type)
2031 {
2032 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2033
2034 return prop != nullptr && prop->is_constant ();
2035 }
2036
2037 /* Worker for is_dynamic_type. */
2038
2039 static int
2040 is_dynamic_type_internal (struct type *type, int top_level)
2041 {
2042 type = check_typedef (type);
2043
2044 /* We only want to recognize references at the outermost level. */
2045 if (top_level && type->code () == TYPE_CODE_REF)
2046 type = check_typedef (type->target_type ());
2047
2048 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2049 dynamic, even if the type itself is statically defined.
2050 From a user's point of view, this may appear counter-intuitive;
2051 but it makes sense in this context, because the point is to determine
2052 whether any part of the type needs to be resolved before it can
2053 be exploited. */
2054 if (TYPE_DATA_LOCATION (type) != NULL
2055 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2056 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
2057 return 1;
2058
2059 if (TYPE_ASSOCIATED_PROP (type))
2060 return 1;
2061
2062 if (TYPE_ALLOCATED_PROP (type))
2063 return 1;
2064
2065 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2066 if (prop != nullptr && prop->kind () != PROP_TYPE)
2067 return 1;
2068
2069 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2070 return 1;
2071
2072 switch (type->code ())
2073 {
2074 case TYPE_CODE_RANGE:
2075 {
2076 /* A range type is obviously dynamic if it has at least one
2077 dynamic bound. But also consider the range type to be
2078 dynamic when its subtype is dynamic, even if the bounds
2079 of the range type are static. It allows us to assume that
2080 the subtype of a static range type is also static. */
2081 return (!has_static_range (type->bounds ())
2082 || is_dynamic_type_internal (type->target_type (), 0));
2083 }
2084
2085 case TYPE_CODE_STRING:
2086 /* Strings are very much like an array of characters, and can be
2087 treated as one here. */
2088 case TYPE_CODE_ARRAY:
2089 {
2090 gdb_assert (type->num_fields () == 1);
2091
2092 /* The array is dynamic if either the bounds are dynamic... */
2093 if (is_dynamic_type_internal (type->index_type (), 0))
2094 return 1;
2095 /* ... or the elements it contains have a dynamic contents... */
2096 if (is_dynamic_type_internal (type->target_type (), 0))
2097 return 1;
2098 /* ... or if it has a dynamic stride... */
2099 if (array_type_has_dynamic_stride (type))
2100 return 1;
2101 return 0;
2102 }
2103
2104 case TYPE_CODE_STRUCT:
2105 case TYPE_CODE_UNION:
2106 {
2107 int i;
2108
2109 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2110
2111 for (i = 0; i < type->num_fields (); ++i)
2112 {
2113 /* Static fields can be ignored here. */
2114 if (type->field (i).is_static ())
2115 continue;
2116 /* If the field has dynamic type, then so does TYPE. */
2117 if (is_dynamic_type_internal (type->field (i).type (), 0))
2118 return 1;
2119 /* If the field is at a fixed offset, then it is not
2120 dynamic. */
2121 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
2122 continue;
2123 /* Do not consider C++ virtual base types to be dynamic
2124 due to the field's offset being dynamic; these are
2125 handled via other means. */
2126 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2127 continue;
2128 return 1;
2129 }
2130 }
2131 break;
2132 }
2133
2134 return 0;
2135 }
2136
2137 /* See gdbtypes.h. */
2138
2139 int
2140 is_dynamic_type (struct type *type)
2141 {
2142 return is_dynamic_type_internal (type, 1);
2143 }
2144
2145 static struct type *resolve_dynamic_type_internal
2146 (struct type *type, struct property_addr_info *addr_stack,
2147 const frame_info_ptr &frame, int top_level);
2148
2149 /* Given a dynamic range type (dyn_range_type) and a stack of
2150 struct property_addr_info elements, return a static version
2151 of that type.
2152
2153 When RESOLVE_P is true then the returned static range is created by
2154 actually evaluating any dynamic properties within the range type, while
2155 when RESOLVE_P is false the returned static range has all of the bounds
2156 and stride information set to undefined. The RESOLVE_P set to false
2157 case will be used when evaluating a dynamic array that is not
2158 allocated, or not associated, i.e. the bounds information might not be
2159 initialized yet.
2160
2161 RANK is the array rank for which we are resolving this range, and is a
2162 zero based count. The rank should never be negative.
2163 */
2164
2165 static struct type *
2166 resolve_dynamic_range (struct type *dyn_range_type,
2167 struct property_addr_info *addr_stack,
2168 const frame_info_ptr &frame,
2169 int rank, bool resolve_p = true)
2170 {
2171 CORE_ADDR value;
2172 struct type *static_range_type, *static_target_type;
2173 struct dynamic_prop low_bound, high_bound, stride;
2174
2175 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
2176 gdb_assert (rank >= 0);
2177
2178 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
2179 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2180 { (CORE_ADDR) rank }))
2181 low_bound.set_const_val (value);
2182 else
2183 low_bound.set_undefined ();
2184
2185 prop = &dyn_range_type->bounds ()->high;
2186 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2187 { (CORE_ADDR) rank }))
2188 {
2189 high_bound.set_const_val (value);
2190
2191 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2192 high_bound.set_const_val
2193 (low_bound.const_val () + high_bound.const_val () - 1);
2194 }
2195 else
2196 high_bound.set_undefined ();
2197
2198 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2199 prop = &dyn_range_type->bounds ()->stride;
2200 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2201 { (CORE_ADDR) rank }))
2202 {
2203 stride.set_const_val (value);
2204
2205 /* If we have a bit stride that is not an exact number of bytes then
2206 I really don't think this is going to work with current GDB, the
2207 array indexing code in GDB seems to be pretty heavily tied to byte
2208 offsets right now. Assuming 8 bits in a byte. */
2209 struct gdbarch *gdbarch = dyn_range_type->arch ();
2210 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2211 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2212 error (_("bit strides that are not a multiple of the byte size "
2213 "are currently not supported"));
2214 }
2215 else
2216 {
2217 stride.set_undefined ();
2218 byte_stride_p = true;
2219 }
2220
2221 static_target_type
2222 = resolve_dynamic_type_internal (dyn_range_type->target_type (),
2223 addr_stack, frame, 0);
2224 LONGEST bias = dyn_range_type->bounds ()->bias;
2225 type_allocator alloc (dyn_range_type);
2226 static_range_type = create_range_type_with_stride
2227 (alloc, static_target_type,
2228 &low_bound, &high_bound, bias, &stride, byte_stride_p);
2229 static_range_type->set_name (dyn_range_type->name ());
2230 static_range_type->bounds ()->flag_bound_evaluated = 1;
2231 return static_range_type;
2232 }
2233
2234 /* Helper function for resolve_dynamic_array_or_string. This function
2235 resolves the properties for a single array at RANK within a nested array
2236 of arrays structure. The RANK value is greater than or equal to 0, and
2237 starts at it's maximum value and goes down by 1 for each recursive call
2238 to this function. So, for a 3-dimensional array, the first call to this
2239 function has RANK == 2, then we call ourselves recursively with RANK ==
2240 1, than again with RANK == 0, and at that point we should return.
2241
2242 TYPE is updated as the dynamic properties are resolved, and so, should
2243 be a copy of the dynamic type, rather than the original dynamic type
2244 itself.
2245
2246 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2247 during the dynamic resolution.
2248
2249 When RESOLVE_P is true then the dynamic properties of TYPE are
2250 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2251 instead we assume the array is not allocated/associated yet. */
2252
2253 static struct type *
2254 resolve_dynamic_array_or_string_1 (struct type *type,
2255 struct property_addr_info *addr_stack,
2256 const frame_info_ptr &frame,
2257 int rank, bool resolve_p)
2258 {
2259 CORE_ADDR value;
2260 struct type *elt_type;
2261 struct type *range_type;
2262 struct type *ary_dim;
2263 struct dynamic_prop *prop;
2264 unsigned int bit_stride = 0;
2265
2266 /* For dynamic type resolution strings can be treated like arrays of
2267 characters. */
2268 gdb_assert (type->code () == TYPE_CODE_ARRAY
2269 || type->code () == TYPE_CODE_STRING);
2270
2271 /* As the rank is a zero based count we expect this to never be
2272 negative. */
2273 gdb_assert (rank >= 0);
2274
2275 /* Resolve the allocated and associated properties before doing anything
2276 else. If an array is not allocated or not associated then (at least
2277 for Fortran) there is no guarantee that the data to define the upper
2278 bound, lower bound, or stride will be correct. If RESOLVE_P is
2279 already false at this point then this is not the first dimension of
2280 the array and a more outer dimension has already marked this array as
2281 not allocated/associated, as such we just ignore this property. This
2282 is fine as GDB only checks the allocated/associated on the outer most
2283 dimension of the array. */
2284 prop = TYPE_ALLOCATED_PROP (type);
2285 if (prop != NULL && resolve_p
2286 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2287 {
2288 prop->set_const_val (value);
2289 if (value == 0)
2290 resolve_p = false;
2291 }
2292
2293 prop = TYPE_ASSOCIATED_PROP (type);
2294 if (prop != NULL && resolve_p
2295 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2296 {
2297 prop->set_const_val (value);
2298 if (value == 0)
2299 resolve_p = false;
2300 }
2301
2302 range_type = check_typedef (type->index_type ());
2303 range_type
2304 = resolve_dynamic_range (range_type, addr_stack, frame, rank, resolve_p);
2305
2306 ary_dim = check_typedef (type->target_type ());
2307 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
2308 {
2309 ary_dim = copy_type (ary_dim);
2310 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
2311 frame, rank - 1,
2312 resolve_p);
2313 }
2314 else
2315 elt_type = type->target_type ();
2316
2317 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2318 if (prop != NULL && resolve_p)
2319 {
2320 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2321 {
2322 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
2323 bit_stride = (unsigned int) (value * 8);
2324 }
2325 else
2326 {
2327 /* Could be a bug in our code, but it could also happen
2328 if the DWARF info is not correct. Issue a warning,
2329 and assume no byte/bit stride (leave bit_stride = 0). */
2330 warning (_("cannot determine array stride for type %s"),
2331 type->name () ? type->name () : "<no name>");
2332 }
2333 }
2334 else
2335 bit_stride = type->field (0).bitsize ();
2336
2337 type_allocator alloc (type, type_allocator::SMASH);
2338 return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
2339 bit_stride);
2340 }
2341
2342 /* Resolve an array or string type with dynamic properties, return a new
2343 type with the dynamic properties resolved to actual values. The
2344 ADDR_STACK represents the location of the object being resolved. */
2345
2346 static struct type *
2347 resolve_dynamic_array_or_string (struct type *type,
2348 struct property_addr_info *addr_stack,
2349 const frame_info_ptr &frame)
2350 {
2351 CORE_ADDR value;
2352 int rank = 0;
2353
2354 /* For dynamic type resolution strings can be treated like arrays of
2355 characters. */
2356 gdb_assert (type->code () == TYPE_CODE_ARRAY
2357 || type->code () == TYPE_CODE_STRING);
2358
2359 type = copy_type (type);
2360
2361 /* Resolve the rank property to get rank value. */
2362 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
2363 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2364 {
2365 prop->set_const_val (value);
2366 rank = value;
2367
2368 if (rank == 0)
2369 {
2370 /* Rank is zero, if a variable is passed as an argument to a
2371 function. In this case the resolved type should not be an
2372 array, but should instead be that of an array element. */
2373 struct type *dynamic_array_type = type;
2374 type = copy_type (dynamic_array_type->target_type ());
2375 struct dynamic_prop_list *prop_list
2376 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2377 if (prop_list != nullptr)
2378 {
2379 struct obstack *obstack
2380 = &type->objfile_owner ()->objfile_obstack;
2381 TYPE_MAIN_TYPE (type)->dyn_prop_list
2382 = copy_dynamic_prop_list (obstack, prop_list);
2383 }
2384 return type;
2385 }
2386 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2387 {
2388 /* What would this even mean? A string with a dynamic rank
2389 greater than 1. */
2390 error (_("unable to handle string with dynamic rank greater than 1"));
2391 }
2392 else if (rank > 1)
2393 {
2394 /* Arrays with dynamic rank are initially just an array type
2395 with a target type that is the array element.
2396
2397 However, now we know the rank of the array we need to build
2398 the array of arrays structure that GDB expects, that is we
2399 need an array type that has a target which is an array type,
2400 and so on, until eventually, we have the element type at the
2401 end of the chain. Create all the additional array types here
2402 by copying the top level array type. */
2403 struct type *element_type = type->target_type ();
2404 struct type *rank_type = type;
2405 for (int i = 1; i < rank; i++)
2406 {
2407 rank_type->set_target_type (copy_type (rank_type));
2408 rank_type = rank_type->target_type ();
2409 }
2410 rank_type->set_target_type (element_type);
2411 }
2412 }
2413 else
2414 {
2415 rank = 1;
2416
2417 for (struct type *tmp_type = check_typedef (type->target_type ());
2418 tmp_type->code () == TYPE_CODE_ARRAY;
2419 tmp_type = check_typedef (tmp_type->target_type ()))
2420 ++rank;
2421 }
2422
2423 /* The rank that we calculated above is actually a count of the number of
2424 ranks. However, when we resolve the type of each individual array
2425 rank we should actually use a rank "offset", e.g. an array with a rank
2426 count of 1 (calculated above) will use the rank offset 0 in order to
2427 resolve the details of the first array dimension. As a result, we
2428 reduce the rank by 1 here. */
2429 --rank;
2430
2431 return resolve_dynamic_array_or_string_1 (type, addr_stack, frame, rank,
2432 true);
2433 }
2434
2435 /* Resolve dynamic bounds of members of the union TYPE to static
2436 bounds. ADDR_STACK is a stack of struct property_addr_info
2437 to be used if needed during the dynamic resolution. */
2438
2439 static struct type *
2440 resolve_dynamic_union (struct type *type,
2441 struct property_addr_info *addr_stack,
2442 const frame_info_ptr &frame)
2443 {
2444 struct type *resolved_type;
2445 int i;
2446 unsigned int max_len = 0;
2447
2448 gdb_assert (type->code () == TYPE_CODE_UNION);
2449
2450 resolved_type = copy_type (type);
2451 resolved_type->copy_fields (type);
2452 for (i = 0; i < resolved_type->num_fields (); ++i)
2453 {
2454 struct type *t;
2455
2456 if (type->field (i).is_static ())
2457 continue;
2458
2459 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
2460 addr_stack, frame, 0);
2461 resolved_type->field (i).set_type (t);
2462
2463 struct type *real_type = check_typedef (t);
2464 if (real_type->length () > max_len)
2465 max_len = real_type->length ();
2466 }
2467
2468 resolved_type->set_length (max_len);
2469 return resolved_type;
2470 }
2471
2472 /* See gdbtypes.h. */
2473
2474 bool
2475 variant::matches (ULONGEST value, bool is_unsigned) const
2476 {
2477 for (const discriminant_range &range : discriminants)
2478 if (range.contains (value, is_unsigned))
2479 return true;
2480 return false;
2481 }
2482
2483 static void
2484 compute_variant_fields_inner (struct type *type,
2485 struct property_addr_info *addr_stack,
2486 const variant_part &part,
2487 std::vector<bool> &flags);
2488
2489 /* A helper function to determine which variant fields will be active.
2490 This handles both the variant's direct fields, and any variant
2491 parts embedded in this variant. TYPE is the type we're examining.
2492 ADDR_STACK holds information about the concrete object. VARIANT is
2493 the current variant to be handled. FLAGS is where the results are
2494 stored -- this function sets the Nth element in FLAGS if the
2495 corresponding field is enabled. ENABLED is whether this variant is
2496 enabled or not. */
2497
2498 static void
2499 compute_variant_fields_recurse (struct type *type,
2500 struct property_addr_info *addr_stack,
2501 const variant &variant,
2502 std::vector<bool> &flags,
2503 bool enabled)
2504 {
2505 for (int field = variant.first_field; field < variant.last_field; ++field)
2506 flags[field] = enabled;
2507
2508 for (const variant_part &new_part : variant.parts)
2509 {
2510 if (enabled)
2511 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2512 else
2513 {
2514 for (const auto &sub_variant : new_part.variants)
2515 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2516 flags, enabled);
2517 }
2518 }
2519 }
2520
2521 /* A helper function to determine which variant fields will be active.
2522 This evaluates the discriminant, decides which variant (if any) is
2523 active, and then updates FLAGS to reflect which fields should be
2524 available. TYPE is the type we're examining. ADDR_STACK holds
2525 information about the concrete object. VARIANT is the current
2526 variant to be handled. FLAGS is where the results are stored --
2527 this function sets the Nth element in FLAGS if the corresponding
2528 field is enabled. */
2529
2530 static void
2531 compute_variant_fields_inner (struct type *type,
2532 struct property_addr_info *addr_stack,
2533 const variant_part &part,
2534 std::vector<bool> &flags)
2535 {
2536 /* Evaluate the discriminant. */
2537 gdb::optional<ULONGEST> discr_value;
2538 if (part.discriminant_index != -1)
2539 {
2540 int idx = part.discriminant_index;
2541
2542 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
2543 error (_("Cannot determine struct field location"
2544 " (invalid location kind)"));
2545
2546 if (addr_stack->valaddr.data () != NULL)
2547 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2548 idx);
2549 else
2550 {
2551 CORE_ADDR addr = (addr_stack->addr
2552 + (type->field (idx).loc_bitpos ()
2553 / TARGET_CHAR_BIT));
2554
2555 LONGEST bitsize = type->field (idx).bitsize ();
2556 LONGEST size = bitsize / 8;
2557 if (size == 0)
2558 size = type->field (idx).type ()->length ();
2559
2560 gdb_byte bits[sizeof (ULONGEST)];
2561 read_memory (addr, bits, size);
2562
2563 LONGEST bitpos = (type->field (idx).loc_bitpos ()
2564 % TARGET_CHAR_BIT);
2565
2566 discr_value = unpack_bits_as_long (type->field (idx).type (),
2567 bits, bitpos, bitsize);
2568 }
2569 }
2570
2571 /* Go through each variant and see which applies. */
2572 const variant *default_variant = nullptr;
2573 const variant *applied_variant = nullptr;
2574 for (const auto &variant : part.variants)
2575 {
2576 if (variant.is_default ())
2577 default_variant = &variant;
2578 else if (discr_value.has_value ()
2579 && variant.matches (*discr_value, part.is_unsigned))
2580 {
2581 applied_variant = &variant;
2582 break;
2583 }
2584 }
2585 if (applied_variant == nullptr)
2586 applied_variant = default_variant;
2587
2588 for (const auto &variant : part.variants)
2589 compute_variant_fields_recurse (type, addr_stack, variant,
2590 flags, applied_variant == &variant);
2591 }
2592
2593 /* Determine which variant fields are available in TYPE. The enabled
2594 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2595 about the concrete object. PARTS describes the top-level variant
2596 parts for this type. */
2597
2598 static void
2599 compute_variant_fields (struct type *type,
2600 struct type *resolved_type,
2601 struct property_addr_info *addr_stack,
2602 const gdb::array_view<variant_part> &parts)
2603 {
2604 /* Assume all fields are included by default. */
2605 std::vector<bool> flags (resolved_type->num_fields (), true);
2606
2607 /* Now disable fields based on the variants that control them. */
2608 for (const auto &part : parts)
2609 compute_variant_fields_inner (type, addr_stack, part, flags);
2610
2611 unsigned int nfields = std::count (flags.begin (), flags.end (), true);
2612 /* No need to zero-initialize the newly allocated fields, they'll be
2613 initialized by the copy in the loop below. */
2614 resolved_type->alloc_fields (nfields, false);
2615
2616 int out = 0;
2617 for (int i = 0; i < type->num_fields (); ++i)
2618 {
2619 if (!flags[i])
2620 continue;
2621
2622 resolved_type->field (out) = type->field (i);
2623 ++out;
2624 }
2625 }
2626
2627 /* Resolve dynamic bounds of members of the struct TYPE to static
2628 bounds. ADDR_STACK is a stack of struct property_addr_info to
2629 be used if needed during the dynamic resolution. */
2630
2631 static struct type *
2632 resolve_dynamic_struct (struct type *type,
2633 struct property_addr_info *addr_stack,
2634 const frame_info_ptr &frame)
2635 {
2636 struct type *resolved_type;
2637 int i;
2638 unsigned resolved_type_bit_length = 0;
2639
2640 gdb_assert (type->code () == TYPE_CODE_STRUCT);
2641
2642 resolved_type = copy_type (type);
2643
2644 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2645 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
2646 {
2647 compute_variant_fields (type, resolved_type, addr_stack,
2648 *variant_prop->variant_parts ());
2649 /* We want to leave the property attached, so that the Rust code
2650 can tell whether the type was originally an enum. */
2651 variant_prop->set_original_type (type);
2652 }
2653 else
2654 {
2655 resolved_type->copy_fields (type);
2656 }
2657
2658 for (i = 0; i < resolved_type->num_fields (); ++i)
2659 {
2660 unsigned new_bit_length;
2661 struct property_addr_info pinfo;
2662
2663 if (resolved_type->field (i).is_static ())
2664 continue;
2665
2666 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
2667 {
2668 struct dwarf2_property_baton baton;
2669 baton.property_type
2670 = lookup_pointer_type (resolved_type->field (i).type ());
2671 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
2672
2673 struct dynamic_prop prop;
2674 prop.set_locexpr (&baton);
2675
2676 CORE_ADDR addr;
2677 if (dwarf2_evaluate_property (&prop, frame, addr_stack, &addr,
2678 {addr_stack->addr}))
2679 resolved_type->field (i).set_loc_bitpos
2680 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
2681 }
2682
2683 /* As we know this field is not a static field, the field's
2684 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2685 this is the case, but only trigger a simple error rather
2686 than an internal error if that fails. While failing
2687 that verification indicates a bug in our code, the error
2688 is not severe enough to suggest to the user he stops
2689 his debugging session because of it. */
2690 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
2691 error (_("Cannot determine struct field location"
2692 " (invalid location kind)"));
2693
2694 pinfo.type = check_typedef (resolved_type->field (i).type ());
2695 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
2696 pinfo.valaddr = addr_stack->valaddr;
2697 if (!pinfo.valaddr.empty ())
2698 pinfo.valaddr = pinfo.valaddr.slice (offset);
2699 pinfo.addr = addr_stack->addr + offset;
2700 pinfo.next = addr_stack;
2701
2702 resolved_type->field (i).set_type
2703 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
2704 &pinfo, frame, 0));
2705 gdb_assert (resolved_type->field (i).loc_kind ()
2706 == FIELD_LOC_KIND_BITPOS);
2707
2708 new_bit_length = resolved_type->field (i).loc_bitpos ();
2709 if (resolved_type->field (i).bitsize () != 0)
2710 new_bit_length += resolved_type->field (i).bitsize ();
2711 else
2712 {
2713 struct type *real_type
2714 = check_typedef (resolved_type->field (i).type ());
2715
2716 new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
2717 }
2718
2719 /* Normally, we would use the position and size of the last field
2720 to determine the size of the enclosing structure. But GCC seems
2721 to be encoding the position of some fields incorrectly when
2722 the struct contains a dynamic field that is not placed last.
2723 So we compute the struct size based on the field that has
2724 the highest position + size - probably the best we can do. */
2725 if (new_bit_length > resolved_type_bit_length)
2726 resolved_type_bit_length = new_bit_length;
2727 }
2728
2729 /* The length of a type won't change for fortran, but it does for C and Ada.
2730 For fortran the size of dynamic fields might change over time but not the
2731 type length of the structure. If we adapt it, we run into problems
2732 when calculating the element offset for arrays of structs. */
2733 if (current_language->la_language != language_fortran)
2734 resolved_type->set_length ((resolved_type_bit_length + TARGET_CHAR_BIT - 1)
2735 / TARGET_CHAR_BIT);
2736
2737 /* The Ada language uses this field as a cache for static fixed types: reset
2738 it as RESOLVED_TYPE must have its own static fixed type. */
2739 resolved_type->set_target_type (nullptr);
2740
2741 return resolved_type;
2742 }
2743
2744 /* Worker for resolved_dynamic_type. */
2745
2746 static struct type *
2747 resolve_dynamic_type_internal (struct type *type,
2748 struct property_addr_info *addr_stack,
2749 const frame_info_ptr &frame,
2750 int top_level)
2751 {
2752 struct type *real_type = check_typedef (type);
2753 struct type *resolved_type = nullptr;
2754 struct dynamic_prop *prop;
2755 CORE_ADDR value;
2756
2757 if (!is_dynamic_type_internal (real_type, top_level))
2758 return type;
2759
2760 gdb::optional<CORE_ADDR> type_length;
2761 prop = TYPE_DYNAMIC_LENGTH (type);
2762 if (prop != NULL
2763 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2764 type_length = value;
2765
2766 if (type->code () == TYPE_CODE_TYPEDEF)
2767 {
2768 resolved_type = copy_type (type);
2769 resolved_type->set_target_type
2770 (resolve_dynamic_type_internal (type->target_type (), addr_stack,
2771 frame, top_level));
2772 }
2773 else
2774 {
2775 /* Before trying to resolve TYPE, make sure it is not a stub. */
2776 type = real_type;
2777
2778 switch (type->code ())
2779 {
2780 case TYPE_CODE_REF:
2781 {
2782 struct property_addr_info pinfo;
2783
2784 pinfo.type = check_typedef (type->target_type ());
2785 pinfo.valaddr = {};
2786 if (addr_stack->valaddr.data () != NULL)
2787 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2788 type);
2789 else
2790 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2791 pinfo.next = addr_stack;
2792
2793 resolved_type = copy_type (type);
2794 resolved_type->set_target_type
2795 (resolve_dynamic_type_internal (type->target_type (),
2796 &pinfo, frame, top_level));
2797 break;
2798 }
2799
2800 case TYPE_CODE_STRING:
2801 /* Strings are very much like an array of characters, and can be
2802 treated as one here. */
2803 case TYPE_CODE_ARRAY:
2804 resolved_type = resolve_dynamic_array_or_string (type, addr_stack,
2805 frame);
2806 break;
2807
2808 case TYPE_CODE_RANGE:
2809 /* Pass 0 for the rank value here, which indicates this is a
2810 range for the first rank of an array. The assumption is that
2811 this rank value is not actually required for the resolution of
2812 the dynamic range, otherwise, we'd be resolving this range
2813 within the context of a dynamic array. */
2814 resolved_type = resolve_dynamic_range (type, addr_stack, frame, 0);
2815 break;
2816
2817 case TYPE_CODE_UNION:
2818 resolved_type = resolve_dynamic_union (type, addr_stack, frame);
2819 break;
2820
2821 case TYPE_CODE_STRUCT:
2822 resolved_type = resolve_dynamic_struct (type, addr_stack, frame);
2823 break;
2824 }
2825 }
2826
2827 if (resolved_type == nullptr)
2828 return type;
2829
2830 if (type_length.has_value ())
2831 {
2832 resolved_type->set_length (*type_length);
2833 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
2834 }
2835
2836 /* Resolve data_location attribute. */
2837 prop = TYPE_DATA_LOCATION (resolved_type);
2838 if (prop != NULL
2839 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2840 {
2841 /* Start of Fortran hack. See comment in f-lang.h for what is going
2842 on here.*/
2843 if (current_language->la_language == language_fortran
2844 && resolved_type->code () == TYPE_CODE_ARRAY)
2845 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2846 value);
2847 /* End of Fortran hack. */
2848 prop->set_const_val (value);
2849 }
2850
2851 return resolved_type;
2852 }
2853
2854 /* See gdbtypes.h */
2855
2856 struct type *
2857 resolve_dynamic_type (struct type *type,
2858 gdb::array_view<const gdb_byte> valaddr,
2859 CORE_ADDR addr,
2860 const frame_info_ptr *in_frame)
2861 {
2862 struct property_addr_info pinfo
2863 = {check_typedef (type), valaddr, addr, NULL};
2864
2865 frame_info_ptr frame;
2866 if (in_frame != nullptr)
2867 frame = *in_frame;
2868
2869 return resolve_dynamic_type_internal (type, &pinfo, frame, 1);
2870 }
2871
2872 /* See gdbtypes.h */
2873
2874 dynamic_prop *
2875 type::dyn_prop (dynamic_prop_node_kind prop_kind) const
2876 {
2877 dynamic_prop_list *node = this->main_type->dyn_prop_list;
2878
2879 while (node != NULL)
2880 {
2881 if (node->prop_kind == prop_kind)
2882 return &node->prop;
2883 node = node->next;
2884 }
2885 return NULL;
2886 }
2887
2888 /* See gdbtypes.h */
2889
2890 void
2891 type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
2892 {
2893 struct dynamic_prop_list *temp;
2894
2895 gdb_assert (this->is_objfile_owned ());
2896
2897 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
2898 struct dynamic_prop_list);
2899 temp->prop_kind = prop_kind;
2900 temp->prop = prop;
2901 temp->next = this->main_type->dyn_prop_list;
2902
2903 this->main_type->dyn_prop_list = temp;
2904 }
2905
2906 /* See gdbtypes.h. */
2907
2908 void
2909 type::remove_dyn_prop (dynamic_prop_node_kind kind)
2910 {
2911 struct dynamic_prop_list *prev_node, *curr_node;
2912
2913 curr_node = this->main_type->dyn_prop_list;
2914 prev_node = NULL;
2915
2916 while (NULL != curr_node)
2917 {
2918 if (curr_node->prop_kind == kind)
2919 {
2920 /* Update the linked list but don't free anything.
2921 The property was allocated on objstack and it is not known
2922 if we are on top of it. Nevertheless, everything is released
2923 when the complete objstack is freed. */
2924 if (NULL == prev_node)
2925 this->main_type->dyn_prop_list = curr_node->next;
2926 else
2927 prev_node->next = curr_node->next;
2928
2929 return;
2930 }
2931
2932 prev_node = curr_node;
2933 curr_node = curr_node->next;
2934 }
2935 }
2936
2937 /* Find the real type of TYPE. This function returns the real type,
2938 after removing all layers of typedefs, and completing opaque or stub
2939 types. Completion changes the TYPE argument, but stripping of
2940 typedefs does not.
2941
2942 Instance flags (e.g. const/volatile) are preserved as typedefs are
2943 stripped. If necessary a new qualified form of the underlying type
2944 is created.
2945
2946 NOTE: This will return a typedef if type::target_type for the typedef has
2947 not been computed and we're either in the middle of reading symbols, or
2948 there was no name for the typedef in the debug info.
2949
2950 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2951 QUITs in the symbol reading code can also throw.
2952 Thus this function can throw an exception.
2953
2954 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2955 the target type.
2956
2957 If this is a stubbed struct (i.e. declared as struct foo *), see if
2958 we can find a full definition in some other file. If so, copy this
2959 definition, so we can use it in future. There used to be a comment
2960 (but not any code) that if we don't find a full definition, we'd
2961 set a flag so we don't spend time in the future checking the same
2962 type. That would be a mistake, though--we might load in more
2963 symbols which contain a full definition for the type. */
2964
2965 struct type *
2966 check_typedef (struct type *type)
2967 {
2968 struct type *orig_type = type;
2969
2970 gdb_assert (type);
2971
2972 /* While we're removing typedefs, we don't want to lose qualifiers.
2973 E.g., const/volatile. */
2974 type_instance_flags instance_flags = type->instance_flags ();
2975
2976 while (type->code () == TYPE_CODE_TYPEDEF)
2977 {
2978 if (!type->target_type ())
2979 {
2980 const char *name;
2981 struct symbol *sym;
2982
2983 /* It is dangerous to call lookup_symbol if we are currently
2984 reading a symtab. Infinite recursion is one danger. */
2985 if (currently_reading_symtab)
2986 return make_qualified_type (type, instance_flags, NULL);
2987
2988 name = type->name ();
2989 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
2990 VAR_DOMAIN as appropriate? */
2991 if (name == NULL)
2992 {
2993 stub_noname_complaint ();
2994 return make_qualified_type (type, instance_flags, NULL);
2995 }
2996 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
2997 if (sym)
2998 type->set_target_type (sym->type ());
2999 else /* TYPE_CODE_UNDEF */
3000 type->set_target_type (type_allocator (type->arch ()).new_type ());
3001 }
3002 type = type->target_type ();
3003
3004 /* Preserve the instance flags as we traverse down the typedef chain.
3005
3006 Handling address spaces/classes is nasty, what do we do if there's a
3007 conflict?
3008 E.g., what if an outer typedef marks the type as class_1 and an inner
3009 typedef marks the type as class_2?
3010 This is the wrong place to do such error checking. We leave it to
3011 the code that created the typedef in the first place to flag the
3012 error. We just pick the outer address space (akin to letting the
3013 outer cast in a chain of casting win), instead of assuming
3014 "it can't happen". */
3015 {
3016 const type_instance_flags ALL_SPACES
3017 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3018 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3019 const type_instance_flags ALL_CLASSES
3020 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3021
3022 type_instance_flags new_instance_flags = type->instance_flags ();
3023
3024 /* Treat code vs data spaces and address classes separately. */
3025 if ((instance_flags & ALL_SPACES) != 0)
3026 new_instance_flags &= ~ALL_SPACES;
3027 if ((instance_flags & ALL_CLASSES) != 0)
3028 new_instance_flags &= ~ALL_CLASSES;
3029
3030 instance_flags |= new_instance_flags;
3031 }
3032 }
3033
3034 /* If this is a struct/class/union with no fields, then check
3035 whether a full definition exists somewhere else. This is for
3036 systems where a type definition with no fields is issued for such
3037 types, instead of identifying them as stub types in the first
3038 place. */
3039
3040 if (TYPE_IS_OPAQUE (type)
3041 && opaque_type_resolution
3042 && !currently_reading_symtab)
3043 {
3044 const char *name = type->name ();
3045 struct type *newtype;
3046
3047 if (name == NULL)
3048 {
3049 stub_noname_complaint ();
3050 return make_qualified_type (type, instance_flags, NULL);
3051 }
3052 newtype = lookup_transparent_type (name);
3053
3054 if (newtype)
3055 {
3056 /* If the resolved type and the stub are in the same
3057 objfile, then replace the stub type with the real deal.
3058 But if they're in separate objfiles, leave the stub
3059 alone; we'll just look up the transparent type every time
3060 we call check_typedef. We can't create pointers between
3061 types allocated to different objfiles, since they may
3062 have different lifetimes. Trying to copy NEWTYPE over to
3063 TYPE's objfile is pointless, too, since you'll have to
3064 move over any other types NEWTYPE refers to, which could
3065 be an unbounded amount of stuff. */
3066 if (newtype->objfile_owner () == type->objfile_owner ())
3067 type = make_qualified_type (newtype, type->instance_flags (), type);
3068 else
3069 type = newtype;
3070 }
3071 }
3072 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3073 types. */
3074 else if (type->is_stub () && !currently_reading_symtab)
3075 {
3076 const char *name = type->name ();
3077 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3078 as appropriate? */
3079 struct symbol *sym;
3080
3081 if (name == NULL)
3082 {
3083 stub_noname_complaint ();
3084 return make_qualified_type (type, instance_flags, NULL);
3085 }
3086 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
3087 if (sym)
3088 {
3089 /* Same as above for opaque types, we can replace the stub
3090 with the complete type only if they are in the same
3091 objfile. */
3092 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3093 type = make_qualified_type (sym->type (),
3094 type->instance_flags (), type);
3095 else
3096 type = sym->type ();
3097 }
3098 }
3099
3100 if (type->target_is_stub ())
3101 {
3102 struct type *target_type = check_typedef (type->target_type ());
3103
3104 if (target_type->is_stub () || target_type->target_is_stub ())
3105 {
3106 /* Nothing we can do. */
3107 }
3108 else if (type->code () == TYPE_CODE_RANGE)
3109 {
3110 type->set_length (target_type->length ());
3111 type->set_target_is_stub (false);
3112 }
3113 else if (type->code () == TYPE_CODE_ARRAY
3114 && update_static_array_size (type))
3115 type->set_target_is_stub (false);
3116 }
3117
3118 type = make_qualified_type (type, instance_flags, NULL);
3119
3120 /* Cache TYPE_LENGTH for future use. */
3121 orig_type->set_length (type->length ());
3122
3123 return type;
3124 }
3125
3126 /* Parse a type expression in the string [P..P+LENGTH). If an error
3127 occurs, silently return a void type. */
3128
3129 static struct type *
3130 safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
3131 {
3132 struct type *type = NULL; /* Initialize to keep gcc happy. */
3133
3134 /* Suppress error messages. */
3135 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3136 &null_stream);
3137
3138 /* Call parse_and_eval_type() without fear of longjmp()s. */
3139 try
3140 {
3141 type = parse_and_eval_type (p, length);
3142 }
3143 catch (const gdb_exception_error &except)
3144 {
3145 type = builtin_type (gdbarch)->builtin_void;
3146 }
3147
3148 return type;
3149 }
3150
3151 /* Ugly hack to convert method stubs into method types.
3152
3153 He ain't kiddin'. This demangles the name of the method into a
3154 string including argument types, parses out each argument type,
3155 generates a string casting a zero to that type, evaluates the
3156 string, and stuffs the resulting type into an argtype vector!!!
3157 Then it knows the type of the whole function (including argument
3158 types for overloading), which info used to be in the stab's but was
3159 removed to hack back the space required for them. */
3160
3161 static void
3162 check_stub_method (struct type *type, int method_id, int signature_id)
3163 {
3164 struct gdbarch *gdbarch = type->arch ();
3165 struct fn_field *f;
3166 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3167 gdb::unique_xmalloc_ptr<char> demangled_name
3168 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3169 char *argtypetext, *p;
3170 int depth = 0, argcount = 1;
3171 struct field *argtypes;
3172 struct type *mtype;
3173
3174 /* Make sure we got back a function string that we can use. */
3175 if (demangled_name)
3176 p = strchr (demangled_name.get (), '(');
3177 else
3178 p = NULL;
3179
3180 if (demangled_name == NULL || p == NULL)
3181 error (_("Internal: Cannot demangle mangled name `%s'."),
3182 mangled_name);
3183
3184 /* Now, read in the parameters that define this type. */
3185 p += 1;
3186 argtypetext = p;
3187 while (*p)
3188 {
3189 if (*p == '(' || *p == '<')
3190 {
3191 depth += 1;
3192 }
3193 else if (*p == ')' || *p == '>')
3194 {
3195 depth -= 1;
3196 }
3197 else if (*p == ',' && depth == 0)
3198 {
3199 argcount += 1;
3200 }
3201
3202 p += 1;
3203 }
3204
3205 /* If we read one argument and it was ``void'', don't count it. */
3206 if (startswith (argtypetext, "(void)"))
3207 argcount -= 1;
3208
3209 /* We need one extra slot, for the THIS pointer. */
3210
3211 argtypes = (struct field *)
3212 TYPE_ZALLOC (type, (argcount + 1) * sizeof (struct field));
3213 p = argtypetext;
3214
3215 /* Add THIS pointer for non-static methods. */
3216 f = TYPE_FN_FIELDLIST1 (type, method_id);
3217 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3218 argcount = 0;
3219 else
3220 {
3221 argtypes[0].set_type (lookup_pointer_type (type));
3222 argcount = 1;
3223 }
3224
3225 if (*p != ')') /* () means no args, skip while. */
3226 {
3227 depth = 0;
3228 while (*p)
3229 {
3230 if (depth <= 0 && (*p == ',' || *p == ')'))
3231 {
3232 /* Avoid parsing of ellipsis, they will be handled below.
3233 Also avoid ``void'' as above. */
3234 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3235 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3236 {
3237 argtypes[argcount].set_type
3238 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3239 argcount += 1;
3240 }
3241 argtypetext = p + 1;
3242 }
3243
3244 if (*p == '(' || *p == '<')
3245 {
3246 depth += 1;
3247 }
3248 else if (*p == ')' || *p == '>')
3249 {
3250 depth -= 1;
3251 }
3252
3253 p += 1;
3254 }
3255 }
3256
3257 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3258
3259 /* Now update the old "stub" type into a real type. */
3260 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3261 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3262 We want a method (TYPE_CODE_METHOD). */
3263 smash_to_method_type (mtype, type, mtype->target_type (),
3264 argtypes, argcount, p[-2] == '.');
3265 mtype->set_is_stub (false);
3266 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3267 }
3268
3269 /* This is the external interface to check_stub_method, above. This
3270 function unstubs all of the signatures for TYPE's METHOD_ID method
3271 name. After calling this function TYPE_FN_FIELD_STUB will be
3272 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3273 correct.
3274
3275 This function unfortunately can not die until stabs do. */
3276
3277 void
3278 check_stub_method_group (struct type *type, int method_id)
3279 {
3280 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3281 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3282
3283 for (int j = 0; j < len; j++)
3284 {
3285 if (TYPE_FN_FIELD_STUB (f, j))
3286 check_stub_method (type, method_id, j);
3287 }
3288 }
3289
3290 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3291 const struct cplus_struct_type cplus_struct_default = { };
3292
3293 void
3294 allocate_cplus_struct_type (struct type *type)
3295 {
3296 if (HAVE_CPLUS_STRUCT (type))
3297 /* Structure was already allocated. Nothing more to do. */
3298 return;
3299
3300 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3301 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3302 TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
3303 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3304 set_type_vptr_fieldno (type, -1);
3305 }
3306
3307 const struct gnat_aux_type gnat_aux_default =
3308 { NULL };
3309
3310 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3311 and allocate the associated gnat-specific data. The gnat-specific
3312 data is also initialized to gnat_aux_default. */
3313
3314 void
3315 allocate_gnat_aux_type (struct type *type)
3316 {
3317 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3318 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3319 TYPE_ZALLOC (type, sizeof (struct gnat_aux_type));
3320 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3321 }
3322
3323 /* Helper function to verify floating-point format and size.
3324 BIT is the type size in bits; if BIT equals -1, the size is
3325 determined by the floatformat. Returns size to be used. */
3326
3327 static int
3328 verify_floatformat (int bit, const struct floatformat *floatformat)
3329 {
3330 gdb_assert (floatformat != NULL);
3331
3332 if (bit == -1)
3333 bit = floatformat->totalsize;
3334
3335 gdb_assert (bit >= 0);
3336 gdb_assert (bit >= floatformat->totalsize);
3337
3338 return bit;
3339 }
3340
3341 /* Return the floating-point format for a floating-point variable of
3342 type TYPE. */
3343
3344 const struct floatformat *
3345 floatformat_from_type (const struct type *type)
3346 {
3347 gdb_assert (type->code () == TYPE_CODE_FLT);
3348 gdb_assert (TYPE_FLOATFORMAT (type));
3349 return TYPE_FLOATFORMAT (type);
3350 }
3351
3352 /* See gdbtypes.h. */
3353
3354 struct type *
3355 init_integer_type (type_allocator &alloc,
3356 int bit, int unsigned_p, const char *name)
3357 {
3358 struct type *t;
3359
3360 t = alloc.new_type (TYPE_CODE_INT, bit, name);
3361 if (unsigned_p)
3362 t->set_is_unsigned (true);
3363
3364 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3365 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3366 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3367
3368 return t;
3369 }
3370
3371 /* See gdbtypes.h. */
3372
3373 struct type *
3374 init_character_type (type_allocator &alloc,
3375 int bit, int unsigned_p, const char *name)
3376 {
3377 struct type *t;
3378
3379 t = alloc.new_type (TYPE_CODE_CHAR, bit, name);
3380 if (unsigned_p)
3381 t->set_is_unsigned (true);
3382
3383 return t;
3384 }
3385
3386 /* See gdbtypes.h. */
3387
3388 struct type *
3389 init_boolean_type (type_allocator &alloc,
3390 int bit, int unsigned_p, const char *name)
3391 {
3392 struct type *t;
3393
3394 t = alloc.new_type (TYPE_CODE_BOOL, bit, name);
3395 if (unsigned_p)
3396 t->set_is_unsigned (true);
3397
3398 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3399 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3400 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3401
3402 return t;
3403 }
3404
3405 /* See gdbtypes.h. */
3406
3407 struct type *
3408 init_float_type (type_allocator &alloc,
3409 int bit, const char *name,
3410 const struct floatformat **floatformats,
3411 enum bfd_endian byte_order)
3412 {
3413 if (byte_order == BFD_ENDIAN_UNKNOWN)
3414 {
3415 struct gdbarch *gdbarch = alloc.arch ();
3416 byte_order = gdbarch_byte_order (gdbarch);
3417 }
3418 const struct floatformat *fmt = floatformats[byte_order];
3419 struct type *t;
3420
3421 bit = verify_floatformat (bit, fmt);
3422 t = alloc.new_type (TYPE_CODE_FLT, bit, name);
3423 TYPE_FLOATFORMAT (t) = fmt;
3424
3425 return t;
3426 }
3427
3428 /* See gdbtypes.h. */
3429
3430 struct type *
3431 init_decfloat_type (type_allocator &alloc, int bit, const char *name)
3432 {
3433 return alloc.new_type (TYPE_CODE_DECFLOAT, bit, name);
3434 }
3435
3436 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3437
3438 bool
3439 can_create_complex_type (struct type *target_type)
3440 {
3441 return (target_type->code () == TYPE_CODE_INT
3442 || target_type->code () == TYPE_CODE_FLT);
3443 }
3444
3445 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3446 name. TARGET_TYPE is the component type. */
3447
3448 struct type *
3449 init_complex_type (const char *name, struct type *target_type)
3450 {
3451 struct type *t;
3452
3453 gdb_assert (can_create_complex_type (target_type));
3454
3455 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3456 {
3457 if (name == nullptr && target_type->name () != nullptr)
3458 {
3459 /* No zero-initialization required, initialized by strcpy/strcat
3460 below. */
3461 char *new_name
3462 = (char *) TYPE_ALLOC (target_type,
3463 strlen (target_type->name ())
3464 + strlen ("_Complex ") + 1);
3465 strcpy (new_name, "_Complex ");
3466 strcat (new_name, target_type->name ());
3467 name = new_name;
3468 }
3469
3470 t = type_allocator (target_type).new_type ();
3471 set_type_code (t, TYPE_CODE_COMPLEX);
3472 t->set_length (2 * target_type->length ());
3473 t->set_name (name);
3474
3475 t->set_target_type (target_type);
3476 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3477 }
3478
3479 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3480 }
3481
3482 /* See gdbtypes.h. */
3483
3484 struct type *
3485 init_pointer_type (type_allocator &alloc,
3486 int bit, const char *name, struct type *target_type)
3487 {
3488 struct type *t;
3489
3490 t = alloc.new_type (TYPE_CODE_PTR, bit, name);
3491 t->set_target_type (target_type);
3492 t->set_is_unsigned (true);
3493 return t;
3494 }
3495
3496 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3497 BIT is the pointer type size in bits.
3498 UNSIGNED_P should be nonzero if the type is unsigned.
3499 NAME is the type name. */
3500
3501 struct type *
3502 init_fixed_point_type (type_allocator &alloc,
3503 int bit, int unsigned_p, const char *name)
3504 {
3505 struct type *t;
3506
3507 t = alloc.new_type (TYPE_CODE_FIXED_POINT, bit, name);
3508 if (unsigned_p)
3509 t->set_is_unsigned (true);
3510
3511 return t;
3512 }
3513
3514 /* See gdbtypes.h. */
3515
3516 unsigned
3517 type_raw_align (struct type *type)
3518 {
3519 if (type->align_log2 != 0)
3520 return 1 << (type->align_log2 - 1);
3521 return 0;
3522 }
3523
3524 /* See gdbtypes.h. */
3525
3526 unsigned
3527 type_align (struct type *type)
3528 {
3529 /* Check alignment provided in the debug information. */
3530 unsigned raw_align = type_raw_align (type);
3531 if (raw_align != 0)
3532 return raw_align;
3533
3534 /* Allow the architecture to provide an alignment. */
3535 ULONGEST align = gdbarch_type_align (type->arch (), type);
3536 if (align != 0)
3537 return align;
3538
3539 switch (type->code ())
3540 {
3541 case TYPE_CODE_PTR:
3542 case TYPE_CODE_FUNC:
3543 case TYPE_CODE_FLAGS:
3544 case TYPE_CODE_INT:
3545 case TYPE_CODE_RANGE:
3546 case TYPE_CODE_FLT:
3547 case TYPE_CODE_ENUM:
3548 case TYPE_CODE_REF:
3549 case TYPE_CODE_RVALUE_REF:
3550 case TYPE_CODE_CHAR:
3551 case TYPE_CODE_BOOL:
3552 case TYPE_CODE_DECFLOAT:
3553 case TYPE_CODE_METHODPTR:
3554 case TYPE_CODE_MEMBERPTR:
3555 align = type_length_units (check_typedef (type));
3556 break;
3557
3558 case TYPE_CODE_ARRAY:
3559 case TYPE_CODE_COMPLEX:
3560 case TYPE_CODE_TYPEDEF:
3561 align = type_align (type->target_type ());
3562 break;
3563
3564 case TYPE_CODE_STRUCT:
3565 case TYPE_CODE_UNION:
3566 {
3567 int number_of_non_static_fields = 0;
3568 for (unsigned i = 0; i < type->num_fields (); ++i)
3569 {
3570 if (!type->field (i).is_static ())
3571 {
3572 number_of_non_static_fields++;
3573 ULONGEST f_align = type_align (type->field (i).type ());
3574 if (f_align == 0)
3575 {
3576 /* Don't pretend we know something we don't. */
3577 align = 0;
3578 break;
3579 }
3580 if (f_align > align)
3581 align = f_align;
3582 }
3583 }
3584 /* A struct with no fields, or with only static fields has an
3585 alignment of 1. */
3586 if (number_of_non_static_fields == 0)
3587 align = 1;
3588 }
3589 break;
3590
3591 case TYPE_CODE_SET:
3592 case TYPE_CODE_STRING:
3593 /* Not sure what to do here, and these can't appear in C or C++
3594 anyway. */
3595 break;
3596
3597 case TYPE_CODE_VOID:
3598 align = 1;
3599 break;
3600
3601 case TYPE_CODE_ERROR:
3602 case TYPE_CODE_METHOD:
3603 default:
3604 break;
3605 }
3606
3607 if ((align & (align - 1)) != 0)
3608 {
3609 /* Not a power of 2, so pass. */
3610 align = 0;
3611 }
3612
3613 return align;
3614 }
3615
3616 /* See gdbtypes.h. */
3617
3618 bool
3619 set_type_align (struct type *type, ULONGEST align)
3620 {
3621 /* Must be a power of 2. Zero is ok. */
3622 gdb_assert ((align & (align - 1)) == 0);
3623
3624 unsigned result = 0;
3625 while (align != 0)
3626 {
3627 ++result;
3628 align >>= 1;
3629 }
3630
3631 if (result >= (1 << TYPE_ALIGN_BITS))
3632 return false;
3633
3634 type->align_log2 = result;
3635 return true;
3636 }
3637
3638 \f
3639 /* Queries on types. */
3640
3641 int
3642 can_dereference (struct type *t)
3643 {
3644 /* FIXME: Should we return true for references as well as
3645 pointers? */
3646 t = check_typedef (t);
3647 return
3648 (t != NULL
3649 && t->code () == TYPE_CODE_PTR
3650 && t->target_type ()->code () != TYPE_CODE_VOID);
3651 }
3652
3653 int
3654 is_integral_type (struct type *t)
3655 {
3656 t = check_typedef (t);
3657 return
3658 ((t != NULL)
3659 && !is_fixed_point_type (t)
3660 && ((t->code () == TYPE_CODE_INT)
3661 || (t->code () == TYPE_CODE_ENUM)
3662 || (t->code () == TYPE_CODE_FLAGS)
3663 || (t->code () == TYPE_CODE_CHAR)
3664 || (t->code () == TYPE_CODE_RANGE)
3665 || (t->code () == TYPE_CODE_BOOL)));
3666 }
3667
3668 int
3669 is_floating_type (struct type *t)
3670 {
3671 t = check_typedef (t);
3672 return
3673 ((t != NULL)
3674 && ((t->code () == TYPE_CODE_FLT)
3675 || (t->code () == TYPE_CODE_DECFLOAT)));
3676 }
3677
3678 /* Return true if TYPE is scalar. */
3679
3680 int
3681 is_scalar_type (struct type *type)
3682 {
3683 type = check_typedef (type);
3684
3685 if (is_fixed_point_type (type))
3686 return 0; /* Implemented as a scalar, but more like a floating point. */
3687
3688 switch (type->code ())
3689 {
3690 case TYPE_CODE_ARRAY:
3691 case TYPE_CODE_STRUCT:
3692 case TYPE_CODE_UNION:
3693 case TYPE_CODE_SET:
3694 case TYPE_CODE_STRING:
3695 return 0;
3696 default:
3697 return 1;
3698 }
3699 }
3700
3701 /* Return true if T is scalar, or a composite type which in practice has
3702 the memory layout of a scalar type. E.g., an array or struct with only
3703 one scalar element inside it, or a union with only scalar elements. */
3704
3705 int
3706 is_scalar_type_recursive (struct type *t)
3707 {
3708 t = check_typedef (t);
3709
3710 if (is_scalar_type (t))
3711 return 1;
3712 /* Are we dealing with an array or string of known dimensions? */
3713 else if ((t->code () == TYPE_CODE_ARRAY
3714 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3715 && t->index_type ()->code () == TYPE_CODE_RANGE)
3716 {
3717 LONGEST low_bound, high_bound;
3718 struct type *elt_type = check_typedef (t->target_type ());
3719
3720 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3721 return (high_bound == low_bound
3722 && is_scalar_type_recursive (elt_type));
3723 else
3724 return 0;
3725 }
3726 /* Are we dealing with a struct with one element? */
3727 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3728 return is_scalar_type_recursive (t->field (0).type ());
3729 else if (t->code () == TYPE_CODE_UNION)
3730 {
3731 int i, n = t->num_fields ();
3732
3733 /* If all elements of the union are scalar, then the union is scalar. */
3734 for (i = 0; i < n; i++)
3735 if (!is_scalar_type_recursive (t->field (i).type ()))
3736 return 0;
3737
3738 return 1;
3739 }
3740
3741 return 0;
3742 }
3743
3744 /* Return true is T is a class or a union. False otherwise. */
3745
3746 int
3747 class_or_union_p (const struct type *t)
3748 {
3749 return (t->code () == TYPE_CODE_STRUCT
3750 || t->code () == TYPE_CODE_UNION);
3751 }
3752
3753 /* A helper function which returns true if types A and B represent the
3754 "same" class type. This is true if the types have the same main
3755 type, or the same name. */
3756
3757 int
3758 class_types_same_p (const struct type *a, const struct type *b)
3759 {
3760 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3761 || (a->name () && b->name ()
3762 && !strcmp (a->name (), b->name ())));
3763 }
3764
3765 /* If BASE is an ancestor of DCLASS return the distance between them.
3766 otherwise return -1;
3767 eg:
3768
3769 class A {};
3770 class B: public A {};
3771 class C: public B {};
3772 class D: C {};
3773
3774 distance_to_ancestor (A, A, 0) = 0
3775 distance_to_ancestor (A, B, 0) = 1
3776 distance_to_ancestor (A, C, 0) = 2
3777 distance_to_ancestor (A, D, 0) = 3
3778
3779 If PUBLIC is 1 then only public ancestors are considered,
3780 and the function returns the distance only if BASE is a public ancestor
3781 of DCLASS.
3782 Eg:
3783
3784 distance_to_ancestor (A, D, 1) = -1. */
3785
3786 static int
3787 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3788 {
3789 int i;
3790 int d;
3791
3792 base = check_typedef (base);
3793 dclass = check_typedef (dclass);
3794
3795 if (class_types_same_p (base, dclass))
3796 return 0;
3797
3798 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3799 {
3800 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3801 continue;
3802
3803 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3804 if (d >= 0)
3805 return 1 + d;
3806 }
3807
3808 return -1;
3809 }
3810
3811 /* Check whether BASE is an ancestor or base class or DCLASS
3812 Return 1 if so, and 0 if not.
3813 Note: If BASE and DCLASS are of the same type, this function
3814 will return 1. So for some class A, is_ancestor (A, A) will
3815 return 1. */
3816
3817 int
3818 is_ancestor (struct type *base, struct type *dclass)
3819 {
3820 return distance_to_ancestor (base, dclass, 0) >= 0;
3821 }
3822
3823 /* Like is_ancestor, but only returns true when BASE is a public
3824 ancestor of DCLASS. */
3825
3826 int
3827 is_public_ancestor (struct type *base, struct type *dclass)
3828 {
3829 return distance_to_ancestor (base, dclass, 1) >= 0;
3830 }
3831
3832 /* A helper function for is_unique_ancestor. */
3833
3834 static int
3835 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3836 int *offset,
3837 const gdb_byte *valaddr, int embedded_offset,
3838 CORE_ADDR address, struct value *val)
3839 {
3840 int i, count = 0;
3841
3842 base = check_typedef (base);
3843 dclass = check_typedef (dclass);
3844
3845 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3846 {
3847 struct type *iter;
3848 int this_offset;
3849
3850 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3851
3852 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3853 address, val);
3854
3855 if (class_types_same_p (base, iter))
3856 {
3857 /* If this is the first subclass, set *OFFSET and set count
3858 to 1. Otherwise, if this is at the same offset as
3859 previous instances, do nothing. Otherwise, increment
3860 count. */
3861 if (*offset == -1)
3862 {
3863 *offset = this_offset;
3864 count = 1;
3865 }
3866 else if (this_offset == *offset)
3867 {
3868 /* Nothing. */
3869 }
3870 else
3871 ++count;
3872 }
3873 else
3874 count += is_unique_ancestor_worker (base, iter, offset,
3875 valaddr,
3876 embedded_offset + this_offset,
3877 address, val);
3878 }
3879
3880 return count;
3881 }
3882
3883 /* Like is_ancestor, but only returns true if BASE is a unique base
3884 class of the type of VAL. */
3885
3886 int
3887 is_unique_ancestor (struct type *base, struct value *val)
3888 {
3889 int offset = -1;
3890
3891 return is_unique_ancestor_worker (base, val->type (), &offset,
3892 val->contents_for_printing ().data (),
3893 val->embedded_offset (),
3894 val->address (), val) == 1;
3895 }
3896
3897 /* See gdbtypes.h. */
3898
3899 enum bfd_endian
3900 type_byte_order (const struct type *type)
3901 {
3902 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
3903 if (type->endianity_is_not_default ())
3904 {
3905 if (byteorder == BFD_ENDIAN_BIG)
3906 return BFD_ENDIAN_LITTLE;
3907 else
3908 {
3909 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
3910 return BFD_ENDIAN_BIG;
3911 }
3912 }
3913
3914 return byteorder;
3915 }
3916
3917 /* See gdbtypes.h. */
3918
3919 bool
3920 is_nocall_function (const struct type *type)
3921 {
3922 if (type->code () != TYPE_CODE_FUNC && type->code () != TYPE_CODE_METHOD)
3923 return false;
3924
3925 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
3926 }
3927
3928 \f
3929 /* Overload resolution. */
3930
3931 /* Return the sum of the rank of A with the rank of B. */
3932
3933 struct rank
3934 sum_ranks (struct rank a, struct rank b)
3935 {
3936 struct rank c;
3937 c.rank = a.rank + b.rank;
3938 c.subrank = a.subrank + b.subrank;
3939 return c;
3940 }
3941
3942 /* Compare rank A and B and return:
3943 0 if a = b
3944 1 if a is better than b
3945 -1 if b is better than a. */
3946
3947 int
3948 compare_ranks (struct rank a, struct rank b)
3949 {
3950 if (a.rank == b.rank)
3951 {
3952 if (a.subrank == b.subrank)
3953 return 0;
3954 if (a.subrank < b.subrank)
3955 return 1;
3956 if (a.subrank > b.subrank)
3957 return -1;
3958 }
3959
3960 if (a.rank < b.rank)
3961 return 1;
3962
3963 /* a.rank > b.rank */
3964 return -1;
3965 }
3966
3967 /* Functions for overload resolution begin here. */
3968
3969 /* Compare two badness vectors A and B and return the result.
3970 0 => A and B are identical
3971 1 => A and B are incomparable
3972 2 => A is better than B
3973 3 => A is worse than B */
3974
3975 int
3976 compare_badness (const badness_vector &a, const badness_vector &b)
3977 {
3978 int i;
3979 int tmp;
3980 /* Any positives in comparison? */
3981 bool found_pos = false;
3982 /* Any negatives in comparison? */
3983 bool found_neg = false;
3984 /* Did A have any INVALID_CONVERSION entries. */
3985 bool a_invalid = false;
3986 /* Did B have any INVALID_CONVERSION entries. */
3987 bool b_invalid = false;
3988
3989 /* differing sizes => incomparable */
3990 if (a.size () != b.size ())
3991 return 1;
3992
3993 /* Subtract b from a */
3994 for (i = 0; i < a.size (); i++)
3995 {
3996 tmp = compare_ranks (b[i], a[i]);
3997 if (tmp > 0)
3998 found_pos = true;
3999 else if (tmp < 0)
4000 found_neg = true;
4001 if (a[i].rank >= INVALID_CONVERSION)
4002 a_invalid = true;
4003 if (b[i].rank >= INVALID_CONVERSION)
4004 b_invalid = true;
4005 }
4006
4007 /* B will only be considered better than or incomparable to A if
4008 they both have invalid entries, or if neither does. That is, if
4009 A has only valid entries, and B has an invalid entry, then A will
4010 be considered better than B, even if B happens to be better for
4011 some parameter. */
4012 if (a_invalid != b_invalid)
4013 {
4014 if (a_invalid)
4015 return 3; /* A > B */
4016 return 2; /* A < B */
4017 }
4018 else if (found_pos)
4019 {
4020 if (found_neg)
4021 return 1; /* incomparable */
4022 else
4023 return 3; /* A > B */
4024 }
4025 else
4026 /* no positives */
4027 {
4028 if (found_neg)
4029 return 2; /* A < B */
4030 else
4031 return 0; /* A == B */
4032 }
4033 }
4034
4035 /* Rank a function by comparing its parameter types (PARMS), to the
4036 types of an argument list (ARGS). Return the badness vector. This
4037 has ARGS.size() + 1 entries. */
4038
4039 badness_vector
4040 rank_function (gdb::array_view<type *> parms,
4041 gdb::array_view<value *> args)
4042 {
4043 /* add 1 for the length-match rank. */
4044 badness_vector bv;
4045 bv.reserve (1 + args.size ());
4046
4047 /* First compare the lengths of the supplied lists.
4048 If there is a mismatch, set it to a high value. */
4049
4050 /* pai/1997-06-03 FIXME: when we have debug info about default
4051 arguments and ellipsis parameter lists, we should consider those
4052 and rank the length-match more finely. */
4053
4054 bv.push_back ((args.size () != parms.size ())
4055 ? LENGTH_MISMATCH_BADNESS
4056 : EXACT_MATCH_BADNESS);
4057
4058 /* Now rank all the parameters of the candidate function. */
4059 size_t min_len = std::min (parms.size (), args.size ());
4060
4061 for (size_t i = 0; i < min_len; i++)
4062 bv.push_back (rank_one_type (parms[i], args[i]->type (),
4063 args[i]));
4064
4065 /* If more arguments than parameters, add dummy entries. */
4066 for (size_t i = min_len; i < args.size (); i++)
4067 bv.push_back (TOO_FEW_PARAMS_BADNESS);
4068
4069 return bv;
4070 }
4071
4072 /* Compare the names of two integer types, assuming that any sign
4073 qualifiers have been checked already. We do it this way because
4074 there may be an "int" in the name of one of the types. */
4075
4076 static int
4077 integer_types_same_name_p (const char *first, const char *second)
4078 {
4079 int first_p, second_p;
4080
4081 /* If both are shorts, return 1; if neither is a short, keep
4082 checking. */
4083 first_p = (strstr (first, "short") != NULL);
4084 second_p = (strstr (second, "short") != NULL);
4085 if (first_p && second_p)
4086 return 1;
4087 if (first_p || second_p)
4088 return 0;
4089
4090 /* Likewise for long. */
4091 first_p = (strstr (first, "long") != NULL);
4092 second_p = (strstr (second, "long") != NULL);
4093 if (first_p && second_p)
4094 return 1;
4095 if (first_p || second_p)
4096 return 0;
4097
4098 /* Likewise for char. */
4099 first_p = (strstr (first, "char") != NULL);
4100 second_p = (strstr (second, "char") != NULL);
4101 if (first_p && second_p)
4102 return 1;
4103 if (first_p || second_p)
4104 return 0;
4105
4106 /* They must both be ints. */
4107 return 1;
4108 }
4109
4110 /* Compares type A to type B. Returns true if they represent the same
4111 type, false otherwise. */
4112
4113 bool
4114 types_equal (struct type *a, struct type *b)
4115 {
4116 /* Identical type pointers. */
4117 /* However, this still doesn't catch all cases of same type for b
4118 and a. The reason is that builtin types are different from
4119 the same ones constructed from the object. */
4120 if (a == b)
4121 return true;
4122
4123 /* Resolve typedefs */
4124 if (a->code () == TYPE_CODE_TYPEDEF)
4125 a = check_typedef (a);
4126 if (b->code () == TYPE_CODE_TYPEDEF)
4127 b = check_typedef (b);
4128
4129 /* Check if identical after resolving typedefs. */
4130 if (a == b)
4131 return true;
4132
4133 /* If after resolving typedefs a and b are not of the same type
4134 code then they are not equal. */
4135 if (a->code () != b->code ())
4136 return false;
4137
4138 /* If a and b are both pointers types or both reference types then
4139 they are equal of the same type iff the objects they refer to are
4140 of the same type. */
4141 if (a->code () == TYPE_CODE_PTR
4142 || a->code () == TYPE_CODE_REF)
4143 return types_equal (a->target_type (),
4144 b->target_type ());
4145
4146 /* Well, damnit, if the names are exactly the same, I'll say they
4147 are exactly the same. This happens when we generate method
4148 stubs. The types won't point to the same address, but they
4149 really are the same. */
4150
4151 if (a->name () && b->name ()
4152 && strcmp (a->name (), b->name ()) == 0)
4153 return true;
4154
4155 /* Two function types are equal if their argument and return types
4156 are equal. */
4157 if (a->code () == TYPE_CODE_FUNC)
4158 {
4159 int i;
4160
4161 if (a->num_fields () != b->num_fields ())
4162 return false;
4163
4164 if (!types_equal (a->target_type (), b->target_type ()))
4165 return false;
4166
4167 for (i = 0; i < a->num_fields (); ++i)
4168 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4169 return false;
4170
4171 return true;
4172 }
4173
4174 return false;
4175 }
4176 \f
4177 /* Deep comparison of types. */
4178
4179 /* An entry in the type-equality bcache. */
4180
4181 struct type_equality_entry
4182 {
4183 type_equality_entry (struct type *t1, struct type *t2)
4184 : type1 (t1),
4185 type2 (t2)
4186 {
4187 }
4188
4189 struct type *type1, *type2;
4190 };
4191
4192 /* A helper function to compare two strings. Returns true if they are
4193 the same, false otherwise. Handles NULLs properly. */
4194
4195 static bool
4196 compare_maybe_null_strings (const char *s, const char *t)
4197 {
4198 if (s == NULL || t == NULL)
4199 return s == t;
4200 return strcmp (s, t) == 0;
4201 }
4202
4203 /* A helper function for check_types_worklist that checks two types for
4204 "deep" equality. Returns true if the types are considered the
4205 same, false otherwise. */
4206
4207 static bool
4208 check_types_equal (struct type *type1, struct type *type2,
4209 std::vector<type_equality_entry> *worklist)
4210 {
4211 type1 = check_typedef (type1);
4212 type2 = check_typedef (type2);
4213
4214 if (type1 == type2)
4215 return true;
4216
4217 if (type1->code () != type2->code ()
4218 || type1->length () != type2->length ()
4219 || type1->is_unsigned () != type2->is_unsigned ()
4220 || type1->has_no_signedness () != type2->has_no_signedness ()
4221 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4222 || type1->has_varargs () != type2->has_varargs ()
4223 || type1->is_vector () != type2->is_vector ()
4224 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4225 || type1->instance_flags () != type2->instance_flags ()
4226 || type1->num_fields () != type2->num_fields ())
4227 return false;
4228
4229 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4230 return false;
4231 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4232 return false;
4233
4234 if (type1->code () == TYPE_CODE_RANGE)
4235 {
4236 if (*type1->bounds () != *type2->bounds ())
4237 return false;
4238 }
4239 else
4240 {
4241 int i;
4242
4243 for (i = 0; i < type1->num_fields (); ++i)
4244 {
4245 const struct field *field1 = &type1->field (i);
4246 const struct field *field2 = &type2->field (i);
4247
4248 if (field1->is_artificial () != field2->is_artificial ()
4249 || field1->bitsize () != field2->bitsize ()
4250 || field1->loc_kind () != field2->loc_kind ())
4251 return false;
4252 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4253 return false;
4254 switch (field1->loc_kind ())
4255 {
4256 case FIELD_LOC_KIND_BITPOS:
4257 if (field1->loc_bitpos () != field2->loc_bitpos ())
4258 return false;
4259 break;
4260 case FIELD_LOC_KIND_ENUMVAL:
4261 if (field1->loc_enumval () != field2->loc_enumval ())
4262 return false;
4263 /* Don't compare types of enum fields, because they don't
4264 have a type. */
4265 continue;
4266 case FIELD_LOC_KIND_PHYSADDR:
4267 if (field1->loc_physaddr () != field2->loc_physaddr ())
4268 return false;
4269 break;
4270 case FIELD_LOC_KIND_PHYSNAME:
4271 if (!compare_maybe_null_strings (field1->loc_physname (),
4272 field2->loc_physname ()))
4273 return false;
4274 break;
4275 case FIELD_LOC_KIND_DWARF_BLOCK:
4276 {
4277 struct dwarf2_locexpr_baton *block1, *block2;
4278
4279 block1 = field1->loc_dwarf_block ();
4280 block2 = field2->loc_dwarf_block ();
4281 if (block1->per_cu != block2->per_cu
4282 || block1->size != block2->size
4283 || memcmp (block1->data, block2->data, block1->size) != 0)
4284 return false;
4285 }
4286 break;
4287 default:
4288 internal_error (_("Unsupported field kind "
4289 "%d by check_types_equal"),
4290 field1->loc_kind ());
4291 }
4292
4293 worklist->emplace_back (field1->type (), field2->type ());
4294 }
4295 }
4296
4297 if (type1->target_type () != NULL)
4298 {
4299 if (type2->target_type () == NULL)
4300 return false;
4301
4302 worklist->emplace_back (type1->target_type (),
4303 type2->target_type ());
4304 }
4305 else if (type2->target_type () != NULL)
4306 return false;
4307
4308 return true;
4309 }
4310
4311 /* Check types on a worklist for equality. Returns false if any pair
4312 is not equal, true if they are all considered equal. */
4313
4314 static bool
4315 check_types_worklist (std::vector<type_equality_entry> *worklist,
4316 gdb::bcache *cache)
4317 {
4318 while (!worklist->empty ())
4319 {
4320 bool added;
4321
4322 struct type_equality_entry entry = std::move (worklist->back ());
4323 worklist->pop_back ();
4324
4325 /* If the type pair has already been visited, we know it is
4326 ok. */
4327 cache->insert (&entry, sizeof (entry), &added);
4328 if (!added)
4329 continue;
4330
4331 if (!check_types_equal (entry.type1, entry.type2, worklist))
4332 return false;
4333 }
4334
4335 return true;
4336 }
4337
4338 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4339 "deep comparison". Otherwise return false. */
4340
4341 bool
4342 types_deeply_equal (struct type *type1, struct type *type2)
4343 {
4344 std::vector<type_equality_entry> worklist;
4345
4346 gdb_assert (type1 != NULL && type2 != NULL);
4347
4348 /* Early exit for the simple case. */
4349 if (type1 == type2)
4350 return true;
4351
4352 gdb::bcache cache;
4353 worklist.emplace_back (type1, type2);
4354 return check_types_worklist (&worklist, &cache);
4355 }
4356
4357 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4358 Otherwise return one. */
4359
4360 int
4361 type_not_allocated (const struct type *type)
4362 {
4363 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4364
4365 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4366 }
4367
4368 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4369 Otherwise return one. */
4370
4371 int
4372 type_not_associated (const struct type *type)
4373 {
4374 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4375
4376 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4377 }
4378
4379 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4380
4381 static struct rank
4382 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4383 {
4384 struct rank rank = {0,0};
4385
4386 switch (arg->code ())
4387 {
4388 case TYPE_CODE_PTR:
4389
4390 /* Allowed pointer conversions are:
4391 (a) pointer to void-pointer conversion. */
4392 if (parm->target_type ()->code () == TYPE_CODE_VOID)
4393 return VOID_PTR_CONVERSION_BADNESS;
4394
4395 /* (b) pointer to ancestor-pointer conversion. */
4396 rank.subrank = distance_to_ancestor (parm->target_type (),
4397 arg->target_type (),
4398 0);
4399 if (rank.subrank >= 0)
4400 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4401
4402 return INCOMPATIBLE_TYPE_BADNESS;
4403 case TYPE_CODE_ARRAY:
4404 {
4405 struct type *t1 = parm->target_type ();
4406 struct type *t2 = arg->target_type ();
4407
4408 if (types_equal (t1, t2))
4409 {
4410 /* Make sure they are CV equal. */
4411 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4412 rank.subrank |= CV_CONVERSION_CONST;
4413 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4414 rank.subrank |= CV_CONVERSION_VOLATILE;
4415 if (rank.subrank != 0)
4416 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4417 return EXACT_MATCH_BADNESS;
4418 }
4419 return INCOMPATIBLE_TYPE_BADNESS;
4420 }
4421 case TYPE_CODE_FUNC:
4422 return rank_one_type (parm->target_type (), arg, NULL);
4423 case TYPE_CODE_INT:
4424 if (value != NULL && value->type ()->code () == TYPE_CODE_INT)
4425 {
4426 if (value_as_long (value) == 0)
4427 {
4428 /* Null pointer conversion: allow it to be cast to a pointer.
4429 [4.10.1 of C++ standard draft n3290] */
4430 return NULL_POINTER_CONVERSION_BADNESS;
4431 }
4432 else
4433 {
4434 /* If type checking is disabled, allow the conversion. */
4435 if (!strict_type_checking)
4436 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4437 }
4438 }
4439 /* fall through */
4440 case TYPE_CODE_ENUM:
4441 case TYPE_CODE_FLAGS:
4442 case TYPE_CODE_CHAR:
4443 case TYPE_CODE_RANGE:
4444 case TYPE_CODE_BOOL:
4445 default:
4446 return INCOMPATIBLE_TYPE_BADNESS;
4447 }
4448 }
4449
4450 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4451
4452 static struct rank
4453 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4454 {
4455 switch (arg->code ())
4456 {
4457 case TYPE_CODE_PTR:
4458 case TYPE_CODE_ARRAY:
4459 return rank_one_type (parm->target_type (),
4460 arg->target_type (), NULL);
4461 default:
4462 return INCOMPATIBLE_TYPE_BADNESS;
4463 }
4464 }
4465
4466 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4467
4468 static struct rank
4469 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4470 {
4471 switch (arg->code ())
4472 {
4473 case TYPE_CODE_PTR: /* funcptr -> func */
4474 return rank_one_type (parm, arg->target_type (), NULL);
4475 default:
4476 return INCOMPATIBLE_TYPE_BADNESS;
4477 }
4478 }
4479
4480 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4481
4482 static struct rank
4483 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4484 {
4485 switch (arg->code ())
4486 {
4487 case TYPE_CODE_INT:
4488 if (arg->length () == parm->length ())
4489 {
4490 /* Deal with signed, unsigned, and plain chars and
4491 signed and unsigned ints. */
4492 if (parm->has_no_signedness ())
4493 {
4494 /* This case only for character types. */
4495 if (arg->has_no_signedness ())
4496 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4497 else /* signed/unsigned char -> plain char */
4498 return INTEGER_CONVERSION_BADNESS;
4499 }
4500 else if (parm->is_unsigned ())
4501 {
4502 if (arg->is_unsigned ())
4503 {
4504 /* unsigned int -> unsigned int, or
4505 unsigned long -> unsigned long */
4506 if (integer_types_same_name_p (parm->name (),
4507 arg->name ()))
4508 return EXACT_MATCH_BADNESS;
4509 else if (integer_types_same_name_p (arg->name (),
4510 "int")
4511 && integer_types_same_name_p (parm->name (),
4512 "long"))
4513 /* unsigned int -> unsigned long */
4514 return INTEGER_PROMOTION_BADNESS;
4515 else
4516 /* unsigned long -> unsigned int */
4517 return INTEGER_CONVERSION_BADNESS;
4518 }
4519 else
4520 {
4521 if (integer_types_same_name_p (arg->name (),
4522 "long")
4523 && integer_types_same_name_p (parm->name (),
4524 "int"))
4525 /* signed long -> unsigned int */
4526 return INTEGER_CONVERSION_BADNESS;
4527 else
4528 /* signed int/long -> unsigned int/long */
4529 return INTEGER_CONVERSION_BADNESS;
4530 }
4531 }
4532 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4533 {
4534 if (integer_types_same_name_p (parm->name (),
4535 arg->name ()))
4536 return EXACT_MATCH_BADNESS;
4537 else if (integer_types_same_name_p (arg->name (),
4538 "int")
4539 && integer_types_same_name_p (parm->name (),
4540 "long"))
4541 return INTEGER_PROMOTION_BADNESS;
4542 else
4543 return INTEGER_CONVERSION_BADNESS;
4544 }
4545 else
4546 return INTEGER_CONVERSION_BADNESS;
4547 }
4548 else if (arg->length () < parm->length ())
4549 return INTEGER_PROMOTION_BADNESS;
4550 else
4551 return INTEGER_CONVERSION_BADNESS;
4552 case TYPE_CODE_ENUM:
4553 case TYPE_CODE_FLAGS:
4554 case TYPE_CODE_CHAR:
4555 case TYPE_CODE_RANGE:
4556 case TYPE_CODE_BOOL:
4557 if (arg->is_declared_class ())
4558 return INCOMPATIBLE_TYPE_BADNESS;
4559 return INTEGER_PROMOTION_BADNESS;
4560 case TYPE_CODE_FLT:
4561 return INT_FLOAT_CONVERSION_BADNESS;
4562 case TYPE_CODE_PTR:
4563 return NS_POINTER_CONVERSION_BADNESS;
4564 default:
4565 return INCOMPATIBLE_TYPE_BADNESS;
4566 }
4567 }
4568
4569 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4570
4571 static struct rank
4572 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4573 {
4574 switch (arg->code ())
4575 {
4576 case TYPE_CODE_INT:
4577 case TYPE_CODE_CHAR:
4578 case TYPE_CODE_RANGE:
4579 case TYPE_CODE_BOOL:
4580 case TYPE_CODE_ENUM:
4581 if (parm->is_declared_class () || arg->is_declared_class ())
4582 return INCOMPATIBLE_TYPE_BADNESS;
4583 return INTEGER_CONVERSION_BADNESS;
4584 case TYPE_CODE_FLT:
4585 return INT_FLOAT_CONVERSION_BADNESS;
4586 default:
4587 return INCOMPATIBLE_TYPE_BADNESS;
4588 }
4589 }
4590
4591 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4592
4593 static struct rank
4594 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4595 {
4596 switch (arg->code ())
4597 {
4598 case TYPE_CODE_RANGE:
4599 case TYPE_CODE_BOOL:
4600 case TYPE_CODE_ENUM:
4601 if (arg->is_declared_class ())
4602 return INCOMPATIBLE_TYPE_BADNESS;
4603 return INTEGER_CONVERSION_BADNESS;
4604 case TYPE_CODE_FLT:
4605 return INT_FLOAT_CONVERSION_BADNESS;
4606 case TYPE_CODE_INT:
4607 if (arg->length () > parm->length ())
4608 return INTEGER_CONVERSION_BADNESS;
4609 else if (arg->length () < parm->length ())
4610 return INTEGER_PROMOTION_BADNESS;
4611 /* fall through */
4612 case TYPE_CODE_CHAR:
4613 /* Deal with signed, unsigned, and plain chars for C++ and
4614 with int cases falling through from previous case. */
4615 if (parm->has_no_signedness ())
4616 {
4617 if (arg->has_no_signedness ())
4618 return EXACT_MATCH_BADNESS;
4619 else
4620 return INTEGER_CONVERSION_BADNESS;
4621 }
4622 else if (parm->is_unsigned ())
4623 {
4624 if (arg->is_unsigned ())
4625 return EXACT_MATCH_BADNESS;
4626 else
4627 return INTEGER_PROMOTION_BADNESS;
4628 }
4629 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4630 return EXACT_MATCH_BADNESS;
4631 else
4632 return INTEGER_CONVERSION_BADNESS;
4633 default:
4634 return INCOMPATIBLE_TYPE_BADNESS;
4635 }
4636 }
4637
4638 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4639
4640 static struct rank
4641 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4642 {
4643 switch (arg->code ())
4644 {
4645 case TYPE_CODE_INT:
4646 case TYPE_CODE_CHAR:
4647 case TYPE_CODE_RANGE:
4648 case TYPE_CODE_BOOL:
4649 case TYPE_CODE_ENUM:
4650 return INTEGER_CONVERSION_BADNESS;
4651 case TYPE_CODE_FLT:
4652 return INT_FLOAT_CONVERSION_BADNESS;
4653 default:
4654 return INCOMPATIBLE_TYPE_BADNESS;
4655 }
4656 }
4657
4658 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4659
4660 static struct rank
4661 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4662 {
4663 switch (arg->code ())
4664 {
4665 /* n3290 draft, section 4.12.1 (conv.bool):
4666
4667 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4668 pointer to member type can be converted to a prvalue of type
4669 bool. A zero value, null pointer value, or null member pointer
4670 value is converted to false; any other value is converted to
4671 true. A prvalue of type std::nullptr_t can be converted to a
4672 prvalue of type bool; the resulting value is false." */
4673 case TYPE_CODE_INT:
4674 case TYPE_CODE_CHAR:
4675 case TYPE_CODE_ENUM:
4676 case TYPE_CODE_FLT:
4677 case TYPE_CODE_MEMBERPTR:
4678 case TYPE_CODE_PTR:
4679 return BOOL_CONVERSION_BADNESS;
4680 case TYPE_CODE_RANGE:
4681 return INCOMPATIBLE_TYPE_BADNESS;
4682 case TYPE_CODE_BOOL:
4683 return EXACT_MATCH_BADNESS;
4684 default:
4685 return INCOMPATIBLE_TYPE_BADNESS;
4686 }
4687 }
4688
4689 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4690
4691 static struct rank
4692 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4693 {
4694 switch (arg->code ())
4695 {
4696 case TYPE_CODE_FLT:
4697 if (arg->length () < parm->length ())
4698 return FLOAT_PROMOTION_BADNESS;
4699 else if (arg->length () == parm->length ())
4700 return EXACT_MATCH_BADNESS;
4701 else
4702 return FLOAT_CONVERSION_BADNESS;
4703 case TYPE_CODE_INT:
4704 case TYPE_CODE_BOOL:
4705 case TYPE_CODE_ENUM:
4706 case TYPE_CODE_RANGE:
4707 case TYPE_CODE_CHAR:
4708 return INT_FLOAT_CONVERSION_BADNESS;
4709 default:
4710 return INCOMPATIBLE_TYPE_BADNESS;
4711 }
4712 }
4713
4714 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4715
4716 static struct rank
4717 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4718 {
4719 switch (arg->code ())
4720 { /* Strictly not needed for C++, but... */
4721 case TYPE_CODE_FLT:
4722 return FLOAT_PROMOTION_BADNESS;
4723 case TYPE_CODE_COMPLEX:
4724 return EXACT_MATCH_BADNESS;
4725 default:
4726 return INCOMPATIBLE_TYPE_BADNESS;
4727 }
4728 }
4729
4730 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4731
4732 static struct rank
4733 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4734 {
4735 struct rank rank = {0, 0};
4736
4737 switch (arg->code ())
4738 {
4739 case TYPE_CODE_STRUCT:
4740 /* Check for derivation */
4741 rank.subrank = distance_to_ancestor (parm, arg, 0);
4742 if (rank.subrank >= 0)
4743 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4744 /* fall through */
4745 default:
4746 return INCOMPATIBLE_TYPE_BADNESS;
4747 }
4748 }
4749
4750 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4751
4752 static struct rank
4753 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4754 {
4755 switch (arg->code ())
4756 {
4757 /* Not in C++ */
4758 case TYPE_CODE_SET:
4759 return rank_one_type (parm->field (0).type (),
4760 arg->field (0).type (), NULL);
4761 default:
4762 return INCOMPATIBLE_TYPE_BADNESS;
4763 }
4764 }
4765
4766 /* Compare one type (PARM) for compatibility with another (ARG).
4767 * PARM is intended to be the parameter type of a function; and
4768 * ARG is the supplied argument's type. This function tests if
4769 * the latter can be converted to the former.
4770 * VALUE is the argument's value or NULL if none (or called recursively)
4771 *
4772 * Return 0 if they are identical types;
4773 * Otherwise, return an integer which corresponds to how compatible
4774 * PARM is to ARG. The higher the return value, the worse the match.
4775 * Generally the "bad" conversions are all uniformly assigned
4776 * INVALID_CONVERSION. */
4777
4778 struct rank
4779 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4780 {
4781 struct rank rank = {0,0};
4782
4783 /* Resolve typedefs */
4784 if (parm->code () == TYPE_CODE_TYPEDEF)
4785 parm = check_typedef (parm);
4786 if (arg->code () == TYPE_CODE_TYPEDEF)
4787 arg = check_typedef (arg);
4788
4789 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4790 {
4791 if (value->lval () == not_lval)
4792 {
4793 /* Rvalues should preferably bind to rvalue references or const
4794 lvalue references. */
4795 if (parm->code () == TYPE_CODE_RVALUE_REF)
4796 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4797 else if (TYPE_CONST (parm->target_type ()))
4798 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4799 else
4800 return INCOMPATIBLE_TYPE_BADNESS;
4801 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4802 }
4803 else
4804 {
4805 /* It's illegal to pass an lvalue as an rvalue. */
4806 if (parm->code () == TYPE_CODE_RVALUE_REF)
4807 return INCOMPATIBLE_TYPE_BADNESS;
4808 }
4809 }
4810
4811 if (types_equal (parm, arg))
4812 {
4813 struct type *t1 = parm;
4814 struct type *t2 = arg;
4815
4816 /* For pointers and references, compare target type. */
4817 if (parm->is_pointer_or_reference ())
4818 {
4819 t1 = parm->target_type ();
4820 t2 = arg->target_type ();
4821 }
4822
4823 /* Make sure they are CV equal, too. */
4824 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4825 rank.subrank |= CV_CONVERSION_CONST;
4826 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4827 rank.subrank |= CV_CONVERSION_VOLATILE;
4828 if (rank.subrank != 0)
4829 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4830 return EXACT_MATCH_BADNESS;
4831 }
4832
4833 /* See through references, since we can almost make non-references
4834 references. */
4835
4836 if (TYPE_IS_REFERENCE (arg))
4837 return (sum_ranks (rank_one_type (parm, arg->target_type (), NULL),
4838 REFERENCE_SEE_THROUGH_BADNESS));
4839 if (TYPE_IS_REFERENCE (parm))
4840 return (sum_ranks (rank_one_type (parm->target_type (), arg, NULL),
4841 REFERENCE_SEE_THROUGH_BADNESS));
4842 if (overload_debug)
4843 {
4844 /* Debugging only. */
4845 gdb_printf (gdb_stderr,
4846 "------ Arg is %s [%d], parm is %s [%d]\n",
4847 arg->name (), arg->code (),
4848 parm->name (), parm->code ());
4849 }
4850
4851 /* x -> y means arg of type x being supplied for parameter of type y. */
4852
4853 switch (parm->code ())
4854 {
4855 case TYPE_CODE_PTR:
4856 return rank_one_type_parm_ptr (parm, arg, value);
4857 case TYPE_CODE_ARRAY:
4858 return rank_one_type_parm_array (parm, arg, value);
4859 case TYPE_CODE_FUNC:
4860 return rank_one_type_parm_func (parm, arg, value);
4861 case TYPE_CODE_INT:
4862 return rank_one_type_parm_int (parm, arg, value);
4863 case TYPE_CODE_ENUM:
4864 return rank_one_type_parm_enum (parm, arg, value);
4865 case TYPE_CODE_CHAR:
4866 return rank_one_type_parm_char (parm, arg, value);
4867 case TYPE_CODE_RANGE:
4868 return rank_one_type_parm_range (parm, arg, value);
4869 case TYPE_CODE_BOOL:
4870 return rank_one_type_parm_bool (parm, arg, value);
4871 case TYPE_CODE_FLT:
4872 return rank_one_type_parm_float (parm, arg, value);
4873 case TYPE_CODE_COMPLEX:
4874 return rank_one_type_parm_complex (parm, arg, value);
4875 case TYPE_CODE_STRUCT:
4876 return rank_one_type_parm_struct (parm, arg, value);
4877 case TYPE_CODE_SET:
4878 return rank_one_type_parm_set (parm, arg, value);
4879 default:
4880 return INCOMPATIBLE_TYPE_BADNESS;
4881 } /* switch (arg->code ()) */
4882 }
4883
4884 /* End of functions for overload resolution. */
4885 \f
4886 /* Routines to pretty-print types. */
4887
4888 static void
4889 print_bit_vector (B_TYPE *bits, int nbits)
4890 {
4891 int bitno;
4892
4893 for (bitno = 0; bitno < nbits; bitno++)
4894 {
4895 if ((bitno % 8) == 0)
4896 {
4897 gdb_puts (" ");
4898 }
4899 if (B_TST (bits, bitno))
4900 gdb_printf (("1"));
4901 else
4902 gdb_printf (("0"));
4903 }
4904 }
4905
4906 /* Note the first arg should be the "this" pointer, we may not want to
4907 include it since we may get into a infinitely recursive
4908 situation. */
4909
4910 static void
4911 print_args (struct field *args, int nargs, int spaces)
4912 {
4913 if (args != NULL)
4914 {
4915 int i;
4916
4917 for (i = 0; i < nargs; i++)
4918 {
4919 gdb_printf
4920 ("%*s[%d] name '%s'\n", spaces, "", i,
4921 args[i].name () != NULL ? args[i].name () : "<NULL>");
4922 recursive_dump_type (args[i].type (), spaces + 2);
4923 }
4924 }
4925 }
4926
4927 static void
4928 dump_fn_fieldlists (struct type *type, int spaces)
4929 {
4930 int method_idx;
4931 int overload_idx;
4932 struct fn_field *f;
4933
4934 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
4935 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
4936 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
4937 {
4938 f = TYPE_FN_FIELDLIST1 (type, method_idx);
4939 gdb_printf
4940 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
4941 method_idx,
4942 TYPE_FN_FIELDLIST_NAME (type, method_idx),
4943 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
4944 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
4945 for (overload_idx = 0;
4946 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
4947 overload_idx++)
4948 {
4949 gdb_printf
4950 ("%*s[%d] physname '%s' (%s)\n",
4951 spaces + 4, "", overload_idx,
4952 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
4953 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
4954 overload_idx)));
4955 gdb_printf
4956 ("%*stype %s\n", spaces + 8, "",
4957 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
4958
4959 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
4960 spaces + 8 + 2);
4961
4962 gdb_printf
4963 ("%*sargs %s\n", spaces + 8, "",
4964 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
4965 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
4966 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
4967 spaces + 8 + 2);
4968 gdb_printf
4969 ("%*sfcontext %s\n", spaces + 8, "",
4970 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
4971 overload_idx)));
4972
4973 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
4974 TYPE_FN_FIELD_CONST (f, overload_idx));
4975 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
4976 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
4977 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
4978 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
4979 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
4980 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
4981 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
4982 TYPE_FN_FIELD_STUB (f, overload_idx));
4983 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
4984 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
4985 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
4986 TYPE_FN_FIELD_DELETED (f, overload_idx));
4987 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
4988 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
4989 }
4990 }
4991 }
4992
4993 static void
4994 print_cplus_stuff (struct type *type, int spaces)
4995 {
4996 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
4997 TYPE_VPTR_FIELDNO (type));
4998 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
4999 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
5000 if (TYPE_VPTR_BASETYPE (type) != NULL)
5001 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
5002
5003 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
5004 TYPE_N_BASECLASSES (type));
5005 gdb_printf ("%*snfn_fields %d\n", spaces, "",
5006 TYPE_NFN_FIELDS (type));
5007 if (TYPE_N_BASECLASSES (type) > 0)
5008 {
5009 gdb_printf
5010 ("%*svirtual_field_bits (%d bits at *%s)",
5011 spaces, "", TYPE_N_BASECLASSES (type),
5012 host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type)));
5013
5014 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
5015 TYPE_N_BASECLASSES (type));
5016 gdb_puts ("\n");
5017 }
5018 if (type->num_fields () > 0)
5019 {
5020 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
5021 {
5022 gdb_printf
5023 ("%*sprivate_field_bits (%d bits at *%s)",
5024 spaces, "", type->num_fields (),
5025 host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type)));
5026 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
5027 type->num_fields ());
5028 gdb_puts ("\n");
5029 }
5030 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
5031 {
5032 gdb_printf
5033 ("%*sprotected_field_bits (%d bits at *%s",
5034 spaces, "", type->num_fields (),
5035 host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type)));
5036 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
5037 type->num_fields ());
5038 gdb_puts ("\n");
5039 }
5040 }
5041 if (TYPE_NFN_FIELDS (type) > 0)
5042 {
5043 dump_fn_fieldlists (type, spaces);
5044 }
5045
5046 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5047 TYPE_CPLUS_CALLING_CONVENTION (type));
5048 }
5049
5050 /* Print the contents of the TYPE's type_specific union, assuming that
5051 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5052
5053 static void
5054 print_gnat_stuff (struct type *type, int spaces)
5055 {
5056 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5057
5058 if (descriptive_type == NULL)
5059 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5060 else
5061 {
5062 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5063 recursive_dump_type (descriptive_type, spaces + 4);
5064 }
5065 }
5066
5067 /* Print the contents of the TYPE's type_specific union, assuming that
5068 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5069
5070 static void
5071 print_fixed_point_type_info (struct type *type, int spaces)
5072 {
5073 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5074 type->fixed_point_scaling_factor ().str ().c_str ());
5075 }
5076
5077 static struct obstack dont_print_type_obstack;
5078
5079 /* Print the dynamic_prop PROP. */
5080
5081 static void
5082 dump_dynamic_prop (dynamic_prop const& prop)
5083 {
5084 switch (prop.kind ())
5085 {
5086 case PROP_CONST:
5087 gdb_printf ("%s", plongest (prop.const_val ()));
5088 break;
5089 case PROP_UNDEFINED:
5090 gdb_printf ("(undefined)");
5091 break;
5092 case PROP_LOCEXPR:
5093 case PROP_LOCLIST:
5094 gdb_printf ("(dynamic)");
5095 break;
5096 default:
5097 gdb_assert_not_reached ("unhandled prop kind");
5098 break;
5099 }
5100 }
5101
5102 void
5103 recursive_dump_type (struct type *type, int spaces)
5104 {
5105 int idx;
5106
5107 if (spaces == 0)
5108 obstack_begin (&dont_print_type_obstack, 0);
5109
5110 if (type->num_fields () > 0
5111 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5112 {
5113 struct type **first_dont_print
5114 = (struct type **) obstack_base (&dont_print_type_obstack);
5115
5116 int i = (struct type **)
5117 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5118
5119 while (--i >= 0)
5120 {
5121 if (type == first_dont_print[i])
5122 {
5123 gdb_printf ("%*stype node %s", spaces, "",
5124 host_address_to_string (type));
5125 gdb_printf (_(" <same as already seen type>\n"));
5126 return;
5127 }
5128 }
5129
5130 obstack_ptr_grow (&dont_print_type_obstack, type);
5131 }
5132
5133 gdb_printf ("%*stype node %s\n", spaces, "",
5134 host_address_to_string (type));
5135 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5136 type->name () ? type->name () : "<NULL>",
5137 host_address_to_string (type->name ()));
5138 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5139 switch (type->code ())
5140 {
5141 case TYPE_CODE_UNDEF:
5142 gdb_printf ("(TYPE_CODE_UNDEF)");
5143 break;
5144 case TYPE_CODE_PTR:
5145 gdb_printf ("(TYPE_CODE_PTR)");
5146 break;
5147 case TYPE_CODE_ARRAY:
5148 gdb_printf ("(TYPE_CODE_ARRAY)");
5149 break;
5150 case TYPE_CODE_STRUCT:
5151 gdb_printf ("(TYPE_CODE_STRUCT)");
5152 break;
5153 case TYPE_CODE_UNION:
5154 gdb_printf ("(TYPE_CODE_UNION)");
5155 break;
5156 case TYPE_CODE_ENUM:
5157 gdb_printf ("(TYPE_CODE_ENUM)");
5158 break;
5159 case TYPE_CODE_FLAGS:
5160 gdb_printf ("(TYPE_CODE_FLAGS)");
5161 break;
5162 case TYPE_CODE_FUNC:
5163 gdb_printf ("(TYPE_CODE_FUNC)");
5164 break;
5165 case TYPE_CODE_INT:
5166 gdb_printf ("(TYPE_CODE_INT)");
5167 break;
5168 case TYPE_CODE_FLT:
5169 gdb_printf ("(TYPE_CODE_FLT)");
5170 break;
5171 case TYPE_CODE_VOID:
5172 gdb_printf ("(TYPE_CODE_VOID)");
5173 break;
5174 case TYPE_CODE_SET:
5175 gdb_printf ("(TYPE_CODE_SET)");
5176 break;
5177 case TYPE_CODE_RANGE:
5178 gdb_printf ("(TYPE_CODE_RANGE)");
5179 break;
5180 case TYPE_CODE_STRING:
5181 gdb_printf ("(TYPE_CODE_STRING)");
5182 break;
5183 case TYPE_CODE_ERROR:
5184 gdb_printf ("(TYPE_CODE_ERROR)");
5185 break;
5186 case TYPE_CODE_MEMBERPTR:
5187 gdb_printf ("(TYPE_CODE_MEMBERPTR)");
5188 break;
5189 case TYPE_CODE_METHODPTR:
5190 gdb_printf ("(TYPE_CODE_METHODPTR)");
5191 break;
5192 case TYPE_CODE_METHOD:
5193 gdb_printf ("(TYPE_CODE_METHOD)");
5194 break;
5195 case TYPE_CODE_REF:
5196 gdb_printf ("(TYPE_CODE_REF)");
5197 break;
5198 case TYPE_CODE_CHAR:
5199 gdb_printf ("(TYPE_CODE_CHAR)");
5200 break;
5201 case TYPE_CODE_BOOL:
5202 gdb_printf ("(TYPE_CODE_BOOL)");
5203 break;
5204 case TYPE_CODE_COMPLEX:
5205 gdb_printf ("(TYPE_CODE_COMPLEX)");
5206 break;
5207 case TYPE_CODE_TYPEDEF:
5208 gdb_printf ("(TYPE_CODE_TYPEDEF)");
5209 break;
5210 case TYPE_CODE_NAMESPACE:
5211 gdb_printf ("(TYPE_CODE_NAMESPACE)");
5212 break;
5213 case TYPE_CODE_FIXED_POINT:
5214 gdb_printf ("(TYPE_CODE_FIXED_POINT)");
5215 break;
5216 default:
5217 gdb_printf ("(UNKNOWN TYPE CODE)");
5218 break;
5219 }
5220 gdb_puts ("\n");
5221 gdb_printf ("%*slength %s\n", spaces, "",
5222 pulongest (type->length ()));
5223 if (type->is_objfile_owned ())
5224 gdb_printf ("%*sobjfile %s\n", spaces, "",
5225 host_address_to_string (type->objfile_owner ()));
5226 else
5227 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5228 host_address_to_string (type->arch_owner ()));
5229 gdb_printf ("%*starget_type %s\n", spaces, "",
5230 host_address_to_string (type->target_type ()));
5231 if (type->target_type () != NULL)
5232 {
5233 recursive_dump_type (type->target_type (), spaces + 2);
5234 }
5235 gdb_printf ("%*spointer_type %s\n", spaces, "",
5236 host_address_to_string (TYPE_POINTER_TYPE (type)));
5237 gdb_printf ("%*sreference_type %s\n", spaces, "",
5238 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5239 gdb_printf ("%*stype_chain %s\n", spaces, "",
5240 host_address_to_string (TYPE_CHAIN (type)));
5241 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5242 (unsigned) type->instance_flags ());
5243 if (TYPE_CONST (type))
5244 {
5245 gdb_puts (" TYPE_CONST");
5246 }
5247 if (TYPE_VOLATILE (type))
5248 {
5249 gdb_puts (" TYPE_VOLATILE");
5250 }
5251 if (TYPE_CODE_SPACE (type))
5252 {
5253 gdb_puts (" TYPE_CODE_SPACE");
5254 }
5255 if (TYPE_DATA_SPACE (type))
5256 {
5257 gdb_puts (" TYPE_DATA_SPACE");
5258 }
5259 if (TYPE_ADDRESS_CLASS_1 (type))
5260 {
5261 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5262 }
5263 if (TYPE_ADDRESS_CLASS_2 (type))
5264 {
5265 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5266 }
5267 if (TYPE_RESTRICT (type))
5268 {
5269 gdb_puts (" TYPE_RESTRICT");
5270 }
5271 if (TYPE_ATOMIC (type))
5272 {
5273 gdb_puts (" TYPE_ATOMIC");
5274 }
5275 gdb_puts ("\n");
5276
5277 gdb_printf ("%*sflags", spaces, "");
5278 if (type->is_unsigned ())
5279 {
5280 gdb_puts (" TYPE_UNSIGNED");
5281 }
5282 if (type->has_no_signedness ())
5283 {
5284 gdb_puts (" TYPE_NOSIGN");
5285 }
5286 if (type->endianity_is_not_default ())
5287 {
5288 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5289 }
5290 if (type->is_stub ())
5291 {
5292 gdb_puts (" TYPE_STUB");
5293 }
5294 if (type->target_is_stub ())
5295 {
5296 gdb_puts (" TYPE_TARGET_STUB");
5297 }
5298 if (type->is_prototyped ())
5299 {
5300 gdb_puts (" TYPE_PROTOTYPED");
5301 }
5302 if (type->has_varargs ())
5303 {
5304 gdb_puts (" TYPE_VARARGS");
5305 }
5306 /* This is used for things like AltiVec registers on ppc. Gcc emits
5307 an attribute for the array type, which tells whether or not we
5308 have a vector, instead of a regular array. */
5309 if (type->is_vector ())
5310 {
5311 gdb_puts (" TYPE_VECTOR");
5312 }
5313 if (type->is_fixed_instance ())
5314 {
5315 gdb_puts (" TYPE_FIXED_INSTANCE");
5316 }
5317 if (type->stub_is_supported ())
5318 {
5319 gdb_puts (" TYPE_STUB_SUPPORTED");
5320 }
5321 if (TYPE_NOTTEXT (type))
5322 {
5323 gdb_puts (" TYPE_NOTTEXT");
5324 }
5325 gdb_puts ("\n");
5326 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5327 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5328 || TYPE_ALLOCATED_PROP (type) != nullptr)
5329 {
5330 gdb_printf ("%*s", spaces, "");
5331 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5332 {
5333 gdb_printf ("associated ");
5334 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5335 }
5336 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5337 {
5338 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5339 gdb_printf (" ");
5340 gdb_printf ("allocated ");
5341 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5342 }
5343 gdb_printf ("\n");
5344 }
5345 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5346 for (idx = 0; idx < type->num_fields (); idx++)
5347 {
5348 if (type->code () == TYPE_CODE_ENUM)
5349 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5350 idx, plongest (type->field (idx).loc_enumval ()));
5351 else
5352 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5353 idx, plongest (type->field (idx).loc_bitpos ()),
5354 type->field (idx).bitsize ());
5355 gdb_printf ("%s name '%s' (%s)\n",
5356 host_address_to_string (type->field (idx).type ()),
5357 type->field (idx).name () != NULL
5358 ? type->field (idx).name ()
5359 : "<NULL>",
5360 host_address_to_string (type->field (idx).name ()));
5361 if (type->field (idx).type () != NULL)
5362 {
5363 recursive_dump_type (type->field (idx).type (), spaces + 4);
5364 }
5365 }
5366 if (type->code () == TYPE_CODE_RANGE)
5367 {
5368 gdb_printf ("%*slow ", spaces, "");
5369 dump_dynamic_prop (type->bounds ()->low);
5370 gdb_printf (" high ");
5371 dump_dynamic_prop (type->bounds ()->high);
5372 gdb_printf ("\n");
5373 }
5374
5375 switch (TYPE_SPECIFIC_FIELD (type))
5376 {
5377 case TYPE_SPECIFIC_CPLUS_STUFF:
5378 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5379 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5380 print_cplus_stuff (type, spaces);
5381 break;
5382
5383 case TYPE_SPECIFIC_GNAT_STUFF:
5384 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5385 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5386 print_gnat_stuff (type, spaces);
5387 break;
5388
5389 case TYPE_SPECIFIC_FLOATFORMAT:
5390 gdb_printf ("%*sfloatformat ", spaces, "");
5391 if (TYPE_FLOATFORMAT (type) == NULL
5392 || TYPE_FLOATFORMAT (type)->name == NULL)
5393 gdb_puts ("(null)");
5394 else
5395 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5396 gdb_puts ("\n");
5397 break;
5398
5399 case TYPE_SPECIFIC_FUNC:
5400 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5401 TYPE_CALLING_CONVENTION (type));
5402 /* tail_call_list is not printed. */
5403 break;
5404
5405 case TYPE_SPECIFIC_SELF_TYPE:
5406 gdb_printf ("%*sself_type %s\n", spaces, "",
5407 host_address_to_string (TYPE_SELF_TYPE (type)));
5408 break;
5409
5410 case TYPE_SPECIFIC_FIXED_POINT:
5411 gdb_printf ("%*sfixed_point_info ", spaces, "");
5412 print_fixed_point_type_info (type, spaces);
5413 gdb_puts ("\n");
5414 break;
5415
5416 case TYPE_SPECIFIC_INT:
5417 if (type->bit_size_differs_p ())
5418 {
5419 unsigned bit_size = type->bit_size ();
5420 unsigned bit_off = type->bit_offset ();
5421 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5422 bit_size, bit_off);
5423 }
5424 break;
5425 }
5426
5427 if (spaces == 0)
5428 obstack_free (&dont_print_type_obstack, NULL);
5429 }
5430 \f
5431 /* Trivial helpers for the libiberty hash table, for mapping one
5432 type to another. */
5433
5434 struct type_pair
5435 {
5436 type_pair (struct type *old_, struct type *newobj_)
5437 : old (old_), newobj (newobj_)
5438 {}
5439
5440 struct type * const old, * const newobj;
5441 };
5442
5443 static hashval_t
5444 type_pair_hash (const void *item)
5445 {
5446 const struct type_pair *pair = (const struct type_pair *) item;
5447
5448 return htab_hash_pointer (pair->old);
5449 }
5450
5451 static int
5452 type_pair_eq (const void *item_lhs, const void *item_rhs)
5453 {
5454 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5455 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5456
5457 return lhs->old == rhs->old;
5458 }
5459
5460 /* Allocate the hash table used by copy_type_recursive to walk
5461 types without duplicates. */
5462
5463 htab_up
5464 create_copied_types_hash ()
5465 {
5466 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5467 htab_delete_entry<type_pair>,
5468 xcalloc, xfree));
5469 }
5470
5471 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5472
5473 static struct dynamic_prop_list *
5474 copy_dynamic_prop_list (struct obstack *storage,
5475 struct dynamic_prop_list *list)
5476 {
5477 struct dynamic_prop_list *copy = list;
5478 struct dynamic_prop_list **node_ptr = &copy;
5479
5480 while (*node_ptr != NULL)
5481 {
5482 struct dynamic_prop_list *node_copy;
5483
5484 node_copy = ((struct dynamic_prop_list *)
5485 obstack_copy (storage, *node_ptr,
5486 sizeof (struct dynamic_prop_list)));
5487 node_copy->prop = (*node_ptr)->prop;
5488 *node_ptr = node_copy;
5489
5490 node_ptr = &node_copy->next;
5491 }
5492
5493 return copy;
5494 }
5495
5496 /* Recursively copy (deep copy) TYPE, if it is associated with
5497 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5498 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5499 it is not associated with OBJFILE. */
5500
5501 struct type *
5502 copy_type_recursive (struct type *type, htab_t copied_types)
5503 {
5504 void **slot;
5505 struct type *new_type;
5506
5507 if (!type->is_objfile_owned ())
5508 return type;
5509
5510 struct type_pair pair (type, nullptr);
5511
5512 slot = htab_find_slot (copied_types, &pair, INSERT);
5513 if (*slot != NULL)
5514 return ((struct type_pair *) *slot)->newobj;
5515
5516 new_type = type_allocator (type->arch ()).new_type ();
5517
5518 /* We must add the new type to the hash table immediately, in case
5519 we encounter this type again during a recursive call below. */
5520 struct type_pair *stored = new type_pair (type, new_type);
5521
5522 *slot = stored;
5523
5524 /* Copy the common fields of types. For the main type, we simply
5525 copy the entire thing and then update specific fields as needed. */
5526 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5527
5528 new_type->set_owner (type->arch ());
5529
5530 if (type->name ())
5531 new_type->set_name (xstrdup (type->name ()));
5532
5533 new_type->set_instance_flags (type->instance_flags ());
5534 new_type->set_length (type->length ());
5535
5536 /* Copy the fields. */
5537 if (type->num_fields ())
5538 {
5539 int i, nfields;
5540
5541 nfields = type->num_fields ();
5542 new_type->alloc_fields (type->num_fields ());
5543
5544 for (i = 0; i < nfields; i++)
5545 {
5546 new_type->field (i).set_is_artificial
5547 (type->field (i).is_artificial ());
5548 new_type->field (i).set_bitsize (type->field (i).bitsize ());
5549 if (type->field (i).type ())
5550 new_type->field (i).set_type
5551 (copy_type_recursive (type->field (i).type (), copied_types));
5552 if (type->field (i).name ())
5553 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5554
5555 switch (type->field (i).loc_kind ())
5556 {
5557 case FIELD_LOC_KIND_BITPOS:
5558 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5559 break;
5560 case FIELD_LOC_KIND_ENUMVAL:
5561 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5562 break;
5563 case FIELD_LOC_KIND_PHYSADDR:
5564 new_type->field (i).set_loc_physaddr
5565 (type->field (i).loc_physaddr ());
5566 break;
5567 case FIELD_LOC_KIND_PHYSNAME:
5568 new_type->field (i).set_loc_physname
5569 (xstrdup (type->field (i).loc_physname ()));
5570 break;
5571 case FIELD_LOC_KIND_DWARF_BLOCK:
5572 new_type->field (i).set_loc_dwarf_block
5573 (type->field (i).loc_dwarf_block ());
5574 break;
5575 default:
5576 internal_error (_("Unexpected type field location kind: %d"),
5577 type->field (i).loc_kind ());
5578 }
5579 }
5580 }
5581
5582 /* For range types, copy the bounds information. */
5583 if (type->code () == TYPE_CODE_RANGE)
5584 {
5585 range_bounds *bounds
5586 = ((struct range_bounds *) TYPE_ALLOC
5587 (new_type, sizeof (struct range_bounds)));
5588
5589 *bounds = *type->bounds ();
5590 new_type->set_bounds (bounds);
5591 }
5592
5593 if (type->main_type->dyn_prop_list != NULL)
5594 new_type->main_type->dyn_prop_list
5595 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5596 type->main_type->dyn_prop_list);
5597
5598
5599 /* Copy pointers to other types. */
5600 if (type->target_type ())
5601 new_type->set_target_type
5602 (copy_type_recursive (type->target_type (), copied_types));
5603
5604 /* Maybe copy the type_specific bits.
5605
5606 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5607 base classes and methods. There's no fundamental reason why we
5608 can't, but at the moment it is not needed. */
5609
5610 switch (TYPE_SPECIFIC_FIELD (type))
5611 {
5612 case TYPE_SPECIFIC_NONE:
5613 break;
5614 case TYPE_SPECIFIC_FUNC:
5615 INIT_FUNC_SPECIFIC (new_type);
5616 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5617 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5618 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5619 break;
5620 case TYPE_SPECIFIC_FLOATFORMAT:
5621 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5622 break;
5623 case TYPE_SPECIFIC_CPLUS_STUFF:
5624 INIT_CPLUS_SPECIFIC (new_type);
5625 break;
5626 case TYPE_SPECIFIC_GNAT_STUFF:
5627 INIT_GNAT_SPECIFIC (new_type);
5628 break;
5629 case TYPE_SPECIFIC_SELF_TYPE:
5630 set_type_self_type (new_type,
5631 copy_type_recursive (TYPE_SELF_TYPE (type),
5632 copied_types));
5633 break;
5634 case TYPE_SPECIFIC_FIXED_POINT:
5635 INIT_FIXED_POINT_SPECIFIC (new_type);
5636 new_type->fixed_point_info ().scaling_factor
5637 = type->fixed_point_info ().scaling_factor;
5638 break;
5639 case TYPE_SPECIFIC_INT:
5640 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5641 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5642 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5643 break;
5644
5645 default:
5646 gdb_assert_not_reached ("bad type_specific_kind");
5647 }
5648
5649 return new_type;
5650 }
5651
5652 /* Make a copy of the given TYPE, except that the pointer & reference
5653 types are not preserved. */
5654
5655 struct type *
5656 copy_type (const struct type *type)
5657 {
5658 struct type *new_type = type_allocator (type).new_type ();
5659 new_type->set_instance_flags (type->instance_flags ());
5660 new_type->set_length (type->length ());
5661 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5662 sizeof (struct main_type));
5663 if (type->main_type->dyn_prop_list != NULL)
5664 {
5665 struct obstack *storage = (type->is_objfile_owned ()
5666 ? &type->objfile_owner ()->objfile_obstack
5667 : gdbarch_obstack (type->arch_owner ()));
5668 new_type->main_type->dyn_prop_list
5669 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5670 }
5671
5672 return new_type;
5673 }
5674 \f
5675 /* Helper functions to initialize architecture-specific types. */
5676
5677 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5678 NAME is the type name. BIT is the size of the flag word in bits. */
5679
5680 struct type *
5681 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5682 {
5683 struct type *type;
5684
5685 type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
5686 type->set_is_unsigned (true);
5687 /* Pre-allocate enough space assuming every field is one bit. */
5688 type->alloc_fields (bit);
5689 type->set_num_fields (0);
5690
5691 return type;
5692 }
5693
5694 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5695 position BITPOS is called NAME. Pass NAME as "" for fields that
5696 should not be printed. */
5697
5698 void
5699 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5700 struct type *field_type, const char *name)
5701 {
5702 int type_bitsize = type->length () * TARGET_CHAR_BIT;
5703 int field_nr = type->num_fields ();
5704
5705 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5706 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5707 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5708 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5709 gdb_assert (name != NULL);
5710
5711 type->set_num_fields (type->num_fields () + 1);
5712 type->field (field_nr).set_name (xstrdup (name));
5713 type->field (field_nr).set_type (field_type);
5714 type->field (field_nr).set_loc_bitpos (start_bitpos);
5715 type->field (field_nr).set_bitsize (nr_bits);
5716 }
5717
5718 /* Special version of append_flags_type_field to add a flag field.
5719 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5720 position BITPOS is called NAME. */
5721
5722 void
5723 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5724 {
5725 append_flags_type_field (type, bitpos, 1,
5726 builtin_type (type->arch ())->builtin_bool,
5727 name);
5728 }
5729
5730 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5731 specified by CODE) associated with GDBARCH. NAME is the type name. */
5732
5733 struct type *
5734 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5735 enum type_code code)
5736 {
5737 struct type *t;
5738
5739 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5740 t = type_allocator (gdbarch).new_type (code, 0, NULL);
5741 t->set_name (name);
5742 INIT_CPLUS_SPECIFIC (t);
5743 return t;
5744 }
5745
5746 /* Add new field with name NAME and type FIELD to composite type T.
5747 Do not set the field's position or adjust the type's length;
5748 the caller should do so. Return the new field. */
5749
5750 struct field *
5751 append_composite_type_field_raw (struct type *t, const char *name,
5752 struct type *field)
5753 {
5754 struct field *f;
5755
5756 t->set_num_fields (t->num_fields () + 1);
5757 t->set_fields (XRESIZEVEC (struct field, t->fields (),
5758 t->num_fields ()));
5759 f = &t->field (t->num_fields () - 1);
5760 memset (f, 0, sizeof f[0]);
5761 f[0].set_type (field);
5762 f[0].set_name (name);
5763 return f;
5764 }
5765
5766 /* Add new field with name NAME and type FIELD to composite type T.
5767 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5768
5769 void
5770 append_composite_type_field_aligned (struct type *t, const char *name,
5771 struct type *field, int alignment)
5772 {
5773 struct field *f = append_composite_type_field_raw (t, name, field);
5774
5775 if (t->code () == TYPE_CODE_UNION)
5776 {
5777 if (t->length () < field->length ())
5778 t->set_length (field->length ());
5779 }
5780 else if (t->code () == TYPE_CODE_STRUCT)
5781 {
5782 t->set_length (t->length () + field->length ());
5783 if (t->num_fields () > 1)
5784 {
5785 f->set_loc_bitpos
5786 (f[-1].loc_bitpos ()
5787 + (f[-1].type ()->length () * TARGET_CHAR_BIT));
5788
5789 if (alignment)
5790 {
5791 int left;
5792
5793 alignment *= TARGET_CHAR_BIT;
5794 left = f[0].loc_bitpos () % alignment;
5795
5796 if (left)
5797 {
5798 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
5799 t->set_length
5800 (t->length () + (alignment - left) / TARGET_CHAR_BIT);
5801 }
5802 }
5803 }
5804 }
5805 }
5806
5807 /* Add new field with name NAME and type FIELD to composite type T. */
5808
5809 void
5810 append_composite_type_field (struct type *t, const char *name,
5811 struct type *field)
5812 {
5813 append_composite_type_field_aligned (t, name, field, 0);
5814 }
5815
5816 \f
5817
5818 /* We manage the lifetimes of fixed_point_type_info objects by
5819 attaching them to the objfile. Currently, these objects are
5820 modified during construction, and GMP does not provide a way to
5821 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5822 them. If we did do this, they could be moved to the per-BFD and
5823 shared across objfiles. */
5824 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
5825 fixed_point_type_storage;
5826
5827 /* Key used for managing the storage of fixed-point type info. */
5828 static const struct registry<objfile>::key<fixed_point_type_storage>
5829 fixed_point_objfile_key;
5830
5831 /* See gdbtypes.h. */
5832
5833 void
5834 allocate_fixed_point_type_info (struct type *type)
5835 {
5836 auto up = gdb::make_unique<fixed_point_type_info> ();
5837 fixed_point_type_info *info;
5838
5839 if (type->is_objfile_owned ())
5840 {
5841 fixed_point_type_storage *storage
5842 = fixed_point_objfile_key.get (type->objfile_owner ());
5843 if (storage == nullptr)
5844 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
5845 info = up.get ();
5846 storage->push_back (std::move (up));
5847 }
5848 else
5849 {
5850 /* We just leak the memory, because that's what we do generally
5851 for non-objfile-attached types. */
5852 info = up.release ();
5853 }
5854
5855 type->set_fixed_point_info (info);
5856 }
5857
5858 /* See gdbtypes.h. */
5859
5860 bool
5861 is_fixed_point_type (struct type *type)
5862 {
5863 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5864 type = check_typedef (type)->target_type ();
5865 type = check_typedef (type);
5866
5867 return type->code () == TYPE_CODE_FIXED_POINT;
5868 }
5869
5870 /* See gdbtypes.h. */
5871
5872 struct type *
5873 type::fixed_point_type_base_type ()
5874 {
5875 struct type *type = this;
5876
5877 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5878 type = check_typedef (type)->target_type ();
5879 type = check_typedef (type);
5880
5881 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
5882 return type;
5883 }
5884
5885 /* See gdbtypes.h. */
5886
5887 const gdb_mpq &
5888 type::fixed_point_scaling_factor ()
5889 {
5890 struct type *type = this->fixed_point_type_base_type ();
5891
5892 return type->fixed_point_info ().scaling_factor;
5893 }
5894
5895 /* See gdbtypes.h. */
5896
5897 void
5898 type::alloc_fields (unsigned int nfields, bool init)
5899 {
5900 this->set_num_fields (nfields);
5901
5902 if (nfields == 0)
5903 {
5904 this->main_type->flds_bnds.fields = nullptr;
5905 return;
5906 }
5907
5908 size_t size = nfields * sizeof (*this->fields ());
5909 struct field *fields
5910 = (struct field *) (init
5911 ? TYPE_ZALLOC (this, size)
5912 : TYPE_ALLOC (this, size));
5913
5914 this->main_type->flds_bnds.fields = fields;
5915 }
5916
5917 /* See gdbtypes.h. */
5918
5919 void
5920 type::copy_fields (struct type *src)
5921 {
5922 unsigned int nfields = src->num_fields ();
5923 alloc_fields (nfields, false);
5924 if (nfields == 0)
5925 return;
5926
5927 size_t size = nfields * sizeof (*this->fields ());
5928 memcpy (this->fields (), src->fields (), size);
5929 }
5930
5931 /* See gdbtypes.h. */
5932
5933 void
5934 type::copy_fields (std::vector<struct field> &src)
5935 {
5936 unsigned int nfields = src.size ();
5937 alloc_fields (nfields, false);
5938 if (nfields == 0)
5939 return;
5940
5941 size_t size = nfields * sizeof (*this->fields ());
5942 memcpy (this->fields (), src.data (), size);
5943 }
5944
5945 /* See gdbtypes.h. */
5946
5947 bool
5948 type::is_string_like ()
5949 {
5950 const language_defn *defn = language_def (this->language ());
5951 return defn->is_string_type_p (this);
5952 }
5953
5954 /* See gdbtypes.h. */
5955
5956 bool
5957 type::is_array_like ()
5958 {
5959 if (code () == TYPE_CODE_ARRAY)
5960 return true;
5961 const language_defn *defn = language_def (this->language ());
5962 return defn->is_array_like (this);
5963 }
5964
5965 \f
5966
5967 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
5968
5969 static struct builtin_type *
5970 create_gdbtypes_data (struct gdbarch *gdbarch)
5971 {
5972 struct builtin_type *builtin_type = new struct builtin_type;
5973
5974 type_allocator alloc (gdbarch);
5975
5976 /* Basic types. */
5977 builtin_type->builtin_void
5978 = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
5979 builtin_type->builtin_char
5980 = init_integer_type (alloc, TARGET_CHAR_BIT,
5981 !gdbarch_char_signed (gdbarch), "char");
5982 builtin_type->builtin_char->set_has_no_signedness (true);
5983 builtin_type->builtin_signed_char
5984 = init_integer_type (alloc, TARGET_CHAR_BIT,
5985 0, "signed char");
5986 builtin_type->builtin_unsigned_char
5987 = init_integer_type (alloc, TARGET_CHAR_BIT,
5988 1, "unsigned char");
5989 builtin_type->builtin_short
5990 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5991 0, "short");
5992 builtin_type->builtin_unsigned_short
5993 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5994 1, "unsigned short");
5995 builtin_type->builtin_int
5996 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5997 0, "int");
5998 builtin_type->builtin_unsigned_int
5999 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
6000 1, "unsigned int");
6001 builtin_type->builtin_long
6002 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
6003 0, "long");
6004 builtin_type->builtin_unsigned_long
6005 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
6006 1, "unsigned long");
6007 builtin_type->builtin_long_long
6008 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
6009 0, "long long");
6010 builtin_type->builtin_unsigned_long_long
6011 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
6012 1, "unsigned long long");
6013 builtin_type->builtin_half
6014 = init_float_type (alloc, gdbarch_half_bit (gdbarch),
6015 "half", gdbarch_half_format (gdbarch));
6016 builtin_type->builtin_float
6017 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
6018 "float", gdbarch_float_format (gdbarch));
6019 builtin_type->builtin_bfloat16
6020 = init_float_type (alloc, gdbarch_bfloat16_bit (gdbarch),
6021 "bfloat16", gdbarch_bfloat16_format (gdbarch));
6022 builtin_type->builtin_double
6023 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
6024 "double", gdbarch_double_format (gdbarch));
6025 builtin_type->builtin_long_double
6026 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
6027 "long double", gdbarch_long_double_format (gdbarch));
6028 builtin_type->builtin_complex
6029 = init_complex_type ("complex", builtin_type->builtin_float);
6030 builtin_type->builtin_double_complex
6031 = init_complex_type ("double complex", builtin_type->builtin_double);
6032 builtin_type->builtin_string
6033 = alloc.new_type (TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
6034 builtin_type->builtin_bool
6035 = init_boolean_type (alloc, TARGET_CHAR_BIT, 1, "bool");
6036
6037 /* The following three are about decimal floating point types, which
6038 are 32-bits, 64-bits and 128-bits respectively. */
6039 builtin_type->builtin_decfloat
6040 = init_decfloat_type (alloc, 32, "_Decimal32");
6041 builtin_type->builtin_decdouble
6042 = init_decfloat_type (alloc, 64, "_Decimal64");
6043 builtin_type->builtin_declong
6044 = init_decfloat_type (alloc, 128, "_Decimal128");
6045
6046 /* "True" character types. */
6047 builtin_type->builtin_true_char
6048 = init_character_type (alloc, TARGET_CHAR_BIT, 0, "true character");
6049 builtin_type->builtin_true_unsigned_char
6050 = init_character_type (alloc, TARGET_CHAR_BIT, 1, "true character");
6051
6052 /* Fixed-size integer types. */
6053 builtin_type->builtin_int0
6054 = init_integer_type (alloc, 0, 0, "int0_t");
6055 builtin_type->builtin_int8
6056 = init_integer_type (alloc, 8, 0, "int8_t");
6057 builtin_type->builtin_uint8
6058 = init_integer_type (alloc, 8, 1, "uint8_t");
6059 builtin_type->builtin_int16
6060 = init_integer_type (alloc, 16, 0, "int16_t");
6061 builtin_type->builtin_uint16
6062 = init_integer_type (alloc, 16, 1, "uint16_t");
6063 builtin_type->builtin_int24
6064 = init_integer_type (alloc, 24, 0, "int24_t");
6065 builtin_type->builtin_uint24
6066 = init_integer_type (alloc, 24, 1, "uint24_t");
6067 builtin_type->builtin_int32
6068 = init_integer_type (alloc, 32, 0, "int32_t");
6069 builtin_type->builtin_uint32
6070 = init_integer_type (alloc, 32, 1, "uint32_t");
6071 builtin_type->builtin_int64
6072 = init_integer_type (alloc, 64, 0, "int64_t");
6073 builtin_type->builtin_uint64
6074 = init_integer_type (alloc, 64, 1, "uint64_t");
6075 builtin_type->builtin_int128
6076 = init_integer_type (alloc, 128, 0, "int128_t");
6077 builtin_type->builtin_uint128
6078 = init_integer_type (alloc, 128, 1, "uint128_t");
6079
6080 builtin_type->builtin_int8->set_instance_flags
6081 (builtin_type->builtin_int8->instance_flags ()
6082 | TYPE_INSTANCE_FLAG_NOTTEXT);
6083
6084 builtin_type->builtin_uint8->set_instance_flags
6085 (builtin_type->builtin_uint8->instance_flags ()
6086 | TYPE_INSTANCE_FLAG_NOTTEXT);
6087
6088 /* Wide character types. */
6089 builtin_type->builtin_char16
6090 = init_integer_type (alloc, 16, 1, "char16_t");
6091 builtin_type->builtin_char32
6092 = init_integer_type (alloc, 32, 1, "char32_t");
6093 builtin_type->builtin_wchar
6094 = init_integer_type (alloc, gdbarch_wchar_bit (gdbarch),
6095 !gdbarch_wchar_signed (gdbarch), "wchar_t");
6096
6097 /* Default data/code pointer types. */
6098 builtin_type->builtin_data_ptr
6099 = lookup_pointer_type (builtin_type->builtin_void);
6100 builtin_type->builtin_func_ptr
6101 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6102 builtin_type->builtin_func_func
6103 = lookup_function_type (builtin_type->builtin_func_ptr);
6104
6105 /* This type represents a GDB internal function. */
6106 builtin_type->internal_fn
6107 = alloc.new_type (TYPE_CODE_INTERNAL_FUNCTION, 0,
6108 "<internal function>");
6109
6110 /* This type represents an xmethod. */
6111 builtin_type->xmethod
6112 = alloc.new_type (TYPE_CODE_XMETHOD, 0, "<xmethod>");
6113
6114 /* This type represents a type that was unrecognized in symbol read-in. */
6115 builtin_type->builtin_error
6116 = alloc.new_type (TYPE_CODE_ERROR, 0, "<unknown type>");
6117
6118 /* The following set of types is used for symbols with no
6119 debug information. */
6120 builtin_type->nodebug_text_symbol
6121 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6122 "<text variable, no debug info>");
6123
6124 builtin_type->nodebug_text_gnu_ifunc_symbol
6125 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6126 "<text gnu-indirect-function variable, no debug info>");
6127 builtin_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6128
6129 builtin_type->nodebug_got_plt_symbol
6130 = init_pointer_type (alloc, gdbarch_addr_bit (gdbarch),
6131 "<text from jump slot in .got.plt, no debug info>",
6132 builtin_type->nodebug_text_symbol);
6133 builtin_type->nodebug_data_symbol
6134 = alloc.new_type (TYPE_CODE_ERROR, 0, "<data variable, no debug info>");
6135 builtin_type->nodebug_unknown_symbol
6136 = alloc.new_type (TYPE_CODE_ERROR, 0,
6137 "<variable (not text or data), no debug info>");
6138 builtin_type->nodebug_tls_symbol
6139 = alloc.new_type (TYPE_CODE_ERROR, 0,
6140 "<thread local variable, no debug info>");
6141
6142 /* NOTE: on some targets, addresses and pointers are not necessarily
6143 the same.
6144
6145 The upshot is:
6146 - gdb's `struct type' always describes the target's
6147 representation.
6148 - gdb's `struct value' objects should always hold values in
6149 target form.
6150 - gdb's CORE_ADDR values are addresses in the unified virtual
6151 address space that the assembler and linker work with. Thus,
6152 since target_read_memory takes a CORE_ADDR as an argument, it
6153 can access any memory on the target, even if the processor has
6154 separate code and data address spaces.
6155
6156 In this context, builtin_type->builtin_core_addr is a bit odd:
6157 it's a target type for a value the target will never see. It's
6158 only used to hold the values of (typeless) linker symbols, which
6159 are indeed in the unified virtual address space. */
6160
6161 builtin_type->builtin_core_addr
6162 = init_integer_type (alloc, gdbarch_addr_bit (gdbarch), 1,
6163 "__CORE_ADDR");
6164 return builtin_type;
6165 }
6166
6167 const struct builtin_type *
6168 builtin_type (struct gdbarch *gdbarch)
6169 {
6170 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6171 if (result == nullptr)
6172 {
6173 result = create_gdbtypes_data (gdbarch);
6174 gdbtypes_data.set (gdbarch, result);
6175 }
6176 return result;
6177 }
6178
6179 const struct builtin_type *
6180 builtin_type (struct objfile *objfile)
6181 {
6182 return builtin_type (objfile->arch ());
6183 }
6184
6185 /* See gdbtypes.h. */
6186
6187 CORE_ADDR
6188 call_site::pc () const
6189 {
6190 return per_objfile->relocate (m_unrelocated_pc);
6191 }
6192
6193 void _initialize_gdbtypes ();
6194 void
6195 _initialize_gdbtypes ()
6196 {
6197 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6198 _("Set debugging of C++ overloading."),
6199 _("Show debugging of C++ overloading."),
6200 _("When enabled, ranking of the "
6201 "functions is displayed."),
6202 NULL,
6203 show_overload_debug,
6204 &setdebuglist, &showdebuglist);
6205
6206 /* Add user knob for controlling resolution of opaque types. */
6207 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6208 &opaque_type_resolution,
6209 _("Set resolution of opaque struct/class/union"
6210 " types (if set before loading symbols)."),
6211 _("Show resolution of opaque struct/class/union"
6212 " types (if set before loading symbols)."),
6213 NULL, NULL,
6214 show_opaque_type_resolution,
6215 &setlist, &showlist);
6216
6217 /* Add an option to permit non-strict type checking. */
6218 add_setshow_boolean_cmd ("type", class_support,
6219 &strict_type_checking,
6220 _("Set strict type checking."),
6221 _("Show strict type checking."),
6222 NULL, NULL,
6223 show_strict_type_checking,
6224 &setchecklist, &showchecklist);
6225 }