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