]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbtypes.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "bfd.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "language.h"
30 #include "target.h"
31 #include "value.h"
32 #include "demangle.h"
33 #include "complaints.h"
34 #include "gdbcmd.h"
35 #include "cp-abi.h"
36 #include "hashtab.h"
37 #include "cp-support.h"
38 #include "bcache.h"
39 #include "dwarf2/loc.h"
40 #include "dwarf2/read.h"
41 #include "gdbcore.h"
42 #include "floatformat.h"
43 #include "f-lang.h"
44 #include <algorithm>
45 #include "gmp-utils.h"
46 #include "rust-lang.h"
47 #include "ada-lang.h"
48
49 /* The value of an invalid conversion badness. */
50 #define INVALID_CONVERSION 100
51
52 static struct dynamic_prop_list *
53 copy_dynamic_prop_list (struct obstack *, struct dynamic_prop_list *);
54
55 /* Initialize BADNESS constants. */
56
57 const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
58
59 const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
60 const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
61
62 const struct rank EXACT_MATCH_BADNESS = {0,0};
63
64 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
65 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
66 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
67 const struct rank CV_CONVERSION_BADNESS = {1, 0};
68 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
69 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
70 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
71 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
72 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
73 const struct rank BASE_CONVERSION_BADNESS = {2,0};
74 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
75 const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
76 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
77 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
78 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
79 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 adress 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 whereever 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 propogated 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_ADDR_OFFSET:
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 accidently 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 wierd 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, VAR_DOMAIN,
1659 language->la_language, NULL).symbol;
1660 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
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, 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, 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, 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, VAR_DOMAIN, 0).symbol;
1776
1777 if (sym == NULL)
1778 {
1779 error (_("No template type named %s."), name);
1780 }
1781 if (sym->type ()->code () != TYPE_CODE_STRUCT)
1782 {
1783 error (_("This context has class, union or enum %s, not a struct."),
1784 name);
1785 }
1786 return (sym->type ());
1787 }
1788
1789 /* See gdbtypes.h. */
1790
1791 struct_elt
1792 lookup_struct_elt (struct type *type, const char *name, int noerr)
1793 {
1794 int i;
1795
1796 for (;;)
1797 {
1798 type = check_typedef (type);
1799 if (type->code () != TYPE_CODE_PTR
1800 && type->code () != TYPE_CODE_REF)
1801 break;
1802 type = type->target_type ();
1803 }
1804
1805 if (type->code () != TYPE_CODE_STRUCT
1806 && type->code () != TYPE_CODE_UNION)
1807 {
1808 std::string type_name = type_to_string (type);
1809 error (_("Type %s is not a structure or union type."),
1810 type_name.c_str ());
1811 }
1812
1813 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
1814 {
1815 const char *t_field_name = type->field (i).name ();
1816
1817 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1818 {
1819 return {&type->field (i), type->field (i).loc_bitpos ()};
1820 }
1821 else if (!t_field_name || *t_field_name == '\0')
1822 {
1823 struct_elt elt
1824 = lookup_struct_elt (type->field (i).type (), name, 1);
1825 if (elt.field != NULL)
1826 {
1827 elt.offset += type->field (i).loc_bitpos ();
1828 return elt;
1829 }
1830 }
1831 }
1832
1833 /* OK, it's not in this class. Recursively check the baseclasses. */
1834 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1835 {
1836 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1837 if (elt.field != NULL)
1838 return elt;
1839 }
1840
1841 if (noerr)
1842 return {nullptr, 0};
1843
1844 std::string type_name = type_to_string (type);
1845 error (_("Type %s has no component named %s."), type_name.c_str (), name);
1846 }
1847
1848 /* See gdbtypes.h. */
1849
1850 struct type *
1851 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1852 {
1853 struct_elt elt = lookup_struct_elt (type, name, noerr);
1854 if (elt.field != NULL)
1855 return elt.field->type ();
1856 else
1857 return NULL;
1858 }
1859
1860 /* Return the largest number representable by unsigned integer type TYPE. */
1861
1862 ULONGEST
1863 get_unsigned_type_max (struct type *type)
1864 {
1865 unsigned int n;
1866
1867 type = check_typedef (type);
1868 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
1869 gdb_assert (type->length () <= sizeof (ULONGEST));
1870
1871 /* Written this way to avoid overflow. */
1872 n = type->length () * TARGET_CHAR_BIT;
1873 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1874 }
1875
1876 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1877 signed integer type TYPE. */
1878
1879 void
1880 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1881 {
1882 unsigned int n;
1883
1884 type = check_typedef (type);
1885 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
1886 gdb_assert (type->length () <= sizeof (LONGEST));
1887
1888 n = type->length () * TARGET_CHAR_BIT;
1889 *min = -((ULONGEST) 1 << (n - 1));
1890 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1891 }
1892
1893 /* Return the largest value representable by pointer type TYPE. */
1894
1895 CORE_ADDR
1896 get_pointer_type_max (struct type *type)
1897 {
1898 unsigned int n;
1899
1900 type = check_typedef (type);
1901 gdb_assert (type->code () == TYPE_CODE_PTR);
1902 gdb_assert (type->length () <= sizeof (CORE_ADDR));
1903
1904 n = type->length () * TARGET_CHAR_BIT;
1905 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1906 }
1907
1908 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1909 cplus_stuff.vptr_fieldno.
1910
1911 cplus_stuff is initialized to cplus_struct_default which does not
1912 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1913 designated initializers). We cope with that here. */
1914
1915 int
1916 internal_type_vptr_fieldno (struct type *type)
1917 {
1918 type = check_typedef (type);
1919 gdb_assert (type->code () == TYPE_CODE_STRUCT
1920 || type->code () == TYPE_CODE_UNION);
1921 if (!HAVE_CPLUS_STRUCT (type))
1922 return -1;
1923 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1924 }
1925
1926 /* Set the value of cplus_stuff.vptr_fieldno. */
1927
1928 void
1929 set_type_vptr_fieldno (struct type *type, int fieldno)
1930 {
1931 type = check_typedef (type);
1932 gdb_assert (type->code () == TYPE_CODE_STRUCT
1933 || type->code () == TYPE_CODE_UNION);
1934 if (!HAVE_CPLUS_STRUCT (type))
1935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1936 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1937 }
1938
1939 /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1940 cplus_stuff.vptr_basetype. */
1941
1942 struct type *
1943 internal_type_vptr_basetype (struct type *type)
1944 {
1945 type = check_typedef (type);
1946 gdb_assert (type->code () == TYPE_CODE_STRUCT
1947 || type->code () == TYPE_CODE_UNION);
1948 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1949 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1950 }
1951
1952 /* Set the value of cplus_stuff.vptr_basetype. */
1953
1954 void
1955 set_type_vptr_basetype (struct type *type, struct type *basetype)
1956 {
1957 type = check_typedef (type);
1958 gdb_assert (type->code () == TYPE_CODE_STRUCT
1959 || type->code () == TYPE_CODE_UNION);
1960 if (!HAVE_CPLUS_STRUCT (type))
1961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1962 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1963 }
1964
1965 /* Lookup the vptr basetype/fieldno values for TYPE.
1966 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1967 vptr_fieldno. Also, if found and basetype is from the same objfile,
1968 cache the results.
1969 If not found, return -1 and ignore BASETYPEP.
1970 Callers should be aware that in some cases (for example,
1971 the type or one of its baseclasses is a stub type and we are
1972 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1973 this function will not be able to find the
1974 virtual function table pointer, and vptr_fieldno will remain -1 and
1975 vptr_basetype will remain NULL or incomplete. */
1976
1977 int
1978 get_vptr_fieldno (struct type *type, struct type **basetypep)
1979 {
1980 type = check_typedef (type);
1981
1982 if (TYPE_VPTR_FIELDNO (type) < 0)
1983 {
1984 int i;
1985
1986 /* We must start at zero in case the first (and only) baseclass
1987 is virtual (and hence we cannot share the table pointer). */
1988 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1989 {
1990 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1991 int fieldno;
1992 struct type *basetype;
1993
1994 fieldno = get_vptr_fieldno (baseclass, &basetype);
1995 if (fieldno >= 0)
1996 {
1997 /* If the type comes from a different objfile we can't cache
1998 it, it may have a different lifetime. PR 2384 */
1999 if (type->objfile_owner () == basetype->objfile_owner ())
2000 {
2001 set_type_vptr_fieldno (type, fieldno);
2002 set_type_vptr_basetype (type, basetype);
2003 }
2004 if (basetypep)
2005 *basetypep = basetype;
2006 return fieldno;
2007 }
2008 }
2009
2010 /* Not found. */
2011 return -1;
2012 }
2013 else
2014 {
2015 if (basetypep)
2016 *basetypep = TYPE_VPTR_BASETYPE (type);
2017 return TYPE_VPTR_FIELDNO (type);
2018 }
2019 }
2020
2021 static void
2022 stub_noname_complaint (void)
2023 {
2024 complaint (_("stub type has NULL name"));
2025 }
2026
2027 /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2028 attached to it, and that property has a non-constant value. */
2029
2030 static int
2031 array_type_has_dynamic_stride (struct type *type)
2032 {
2033 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2034
2035 return prop != nullptr && prop->is_constant ();
2036 }
2037
2038 /* Worker for is_dynamic_type. */
2039
2040 static int
2041 is_dynamic_type_internal (struct type *type, int top_level)
2042 {
2043 type = check_typedef (type);
2044
2045 /* We only want to recognize references at the outermost level. */
2046 if (top_level && type->code () == TYPE_CODE_REF)
2047 type = check_typedef (type->target_type ());
2048
2049 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2050 dynamic, even if the type itself is statically defined.
2051 From a user's point of view, this may appear counter-intuitive;
2052 but it makes sense in this context, because the point is to determine
2053 whether any part of the type needs to be resolved before it can
2054 be exploited. */
2055 if (TYPE_DATA_LOCATION (type) != NULL
2056 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2057 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
2058 return 1;
2059
2060 if (TYPE_ASSOCIATED_PROP (type))
2061 return 1;
2062
2063 if (TYPE_ALLOCATED_PROP (type))
2064 return 1;
2065
2066 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2067 if (prop != nullptr && prop->kind () != PROP_TYPE)
2068 return 1;
2069
2070 if (TYPE_HAS_DYNAMIC_LENGTH (type))
2071 return 1;
2072
2073 switch (type->code ())
2074 {
2075 case TYPE_CODE_RANGE:
2076 {
2077 /* A range type is obviously dynamic if it has at least one
2078 dynamic bound. But also consider the range type to be
2079 dynamic when its subtype is dynamic, even if the bounds
2080 of the range type are static. It allows us to assume that
2081 the subtype of a static range type is also static. */
2082 return (!has_static_range (type->bounds ())
2083 || is_dynamic_type_internal (type->target_type (), 0));
2084 }
2085
2086 case TYPE_CODE_STRING:
2087 /* Strings are very much like an array of characters, and can be
2088 treated as one here. */
2089 case TYPE_CODE_ARRAY:
2090 {
2091 gdb_assert (type->num_fields () == 1);
2092
2093 /* The array is dynamic if either the bounds are dynamic... */
2094 if (is_dynamic_type_internal (type->index_type (), 0))
2095 return 1;
2096 /* ... or the elements it contains have a dynamic contents... */
2097 if (is_dynamic_type_internal (type->target_type (), 0))
2098 return 1;
2099 /* ... or if it has a dynamic stride... */
2100 if (array_type_has_dynamic_stride (type))
2101 return 1;
2102 return 0;
2103 }
2104
2105 case TYPE_CODE_STRUCT:
2106 case TYPE_CODE_UNION:
2107 {
2108 int i;
2109
2110 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2111
2112 for (i = 0; i < type->num_fields (); ++i)
2113 {
2114 /* Static fields can be ignored here. */
2115 if (type->field (i).is_static ())
2116 continue;
2117 /* If the field has dynamic type, then so does TYPE. */
2118 if (is_dynamic_type_internal (type->field (i).type (), 0))
2119 return 1;
2120 /* If the field is at a fixed offset, then it is not
2121 dynamic. */
2122 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
2123 continue;
2124 /* Do not consider C++ virtual base types to be dynamic
2125 due to the field's offset being dynamic; these are
2126 handled via other means. */
2127 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2128 continue;
2129 return 1;
2130 }
2131 }
2132 break;
2133 }
2134
2135 return 0;
2136 }
2137
2138 /* See gdbtypes.h. */
2139
2140 int
2141 is_dynamic_type (struct type *type)
2142 {
2143 return is_dynamic_type_internal (type, 1);
2144 }
2145
2146 static struct type *resolve_dynamic_type_internal
2147 (struct type *type, struct property_addr_info *addr_stack,
2148 const frame_info_ptr &frame, int top_level);
2149
2150 /* Given a dynamic range type (dyn_range_type) and a stack of
2151 struct property_addr_info elements, return a static version
2152 of that type.
2153
2154 When RESOLVE_P is true then the returned static range is created by
2155 actually evaluating any dynamic properties within the range type, while
2156 when RESOLVE_P is false the returned static range has all of the bounds
2157 and stride information set to undefined. The RESOLVE_P set to false
2158 case will be used when evaluating a dynamic array that is not
2159 allocated, or not associated, i.e. the bounds information might not be
2160 initialized yet.
2161
2162 RANK is the array rank for which we are resolving this range, and is a
2163 zero based count. The rank should never be negative.
2164 */
2165
2166 static struct type *
2167 resolve_dynamic_range (struct type *dyn_range_type,
2168 struct property_addr_info *addr_stack,
2169 const frame_info_ptr &frame,
2170 int rank, bool resolve_p = true)
2171 {
2172 CORE_ADDR value;
2173 struct type *static_range_type, *static_target_type;
2174 struct dynamic_prop low_bound, high_bound, stride;
2175
2176 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
2177 gdb_assert (rank >= 0);
2178
2179 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
2180 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2181 { (CORE_ADDR) rank }))
2182 low_bound.set_const_val (value);
2183 else
2184 low_bound.set_undefined ();
2185
2186 prop = &dyn_range_type->bounds ()->high;
2187 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2188 { (CORE_ADDR) rank }))
2189 {
2190 high_bound.set_const_val (value);
2191
2192 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2193 high_bound.set_const_val
2194 (low_bound.const_val () + high_bound.const_val () - 1);
2195 }
2196 else
2197 high_bound.set_undefined ();
2198
2199 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2200 prop = &dyn_range_type->bounds ()->stride;
2201 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2202 { (CORE_ADDR) rank }))
2203 {
2204 stride.set_const_val (value);
2205
2206 /* If we have a bit stride that is not an exact number of bytes then
2207 I really don't think this is going to work with current GDB, the
2208 array indexing code in GDB seems to be pretty heavily tied to byte
2209 offsets right now. Assuming 8 bits in a byte. */
2210 struct gdbarch *gdbarch = dyn_range_type->arch ();
2211 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2212 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2213 error (_("bit strides that are not a multiple of the byte size "
2214 "are currently not supported"));
2215 }
2216 else
2217 {
2218 stride.set_undefined ();
2219 byte_stride_p = true;
2220 }
2221
2222 static_target_type
2223 = resolve_dynamic_type_internal (dyn_range_type->target_type (),
2224 addr_stack, frame, 0);
2225 LONGEST bias = dyn_range_type->bounds ()->bias;
2226 type_allocator alloc (dyn_range_type);
2227 static_range_type = create_range_type_with_stride
2228 (alloc, static_target_type,
2229 &low_bound, &high_bound, bias, &stride, byte_stride_p);
2230 static_range_type->set_name (dyn_range_type->name ());
2231 static_range_type->bounds ()->flag_bound_evaluated = 1;
2232 return static_range_type;
2233 }
2234
2235 /* Helper function for resolve_dynamic_array_or_string. This function
2236 resolves the properties for a single array at RANK within a nested array
2237 of arrays structure. The RANK value is greater than or equal to 0, and
2238 starts at it's maximum value and goes down by 1 for each recursive call
2239 to this function. So, for a 3-dimensional array, the first call to this
2240 function has RANK == 2, then we call ourselves recursively with RANK ==
2241 1, than again with RANK == 0, and at that point we should return.
2242
2243 TYPE is updated as the dynamic properties are resolved, and so, should
2244 be a copy of the dynamic type, rather than the original dynamic type
2245 itself.
2246
2247 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2248 during the dynamic resolution.
2249
2250 When RESOLVE_P is true then the dynamic properties of TYPE are
2251 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2252 instead we assume the array is not allocated/associated yet. */
2253
2254 static struct type *
2255 resolve_dynamic_array_or_string_1 (struct type *type,
2256 struct property_addr_info *addr_stack,
2257 const frame_info_ptr &frame,
2258 int rank, bool resolve_p)
2259 {
2260 CORE_ADDR value;
2261 struct type *elt_type;
2262 struct type *range_type;
2263 struct type *ary_dim;
2264 struct dynamic_prop *prop;
2265 unsigned int bit_stride = 0;
2266
2267 /* For dynamic type resolution strings can be treated like arrays of
2268 characters. */
2269 gdb_assert (type->code () == TYPE_CODE_ARRAY
2270 || type->code () == TYPE_CODE_STRING);
2271
2272 /* As the rank is a zero based count we expect this to never be
2273 negative. */
2274 gdb_assert (rank >= 0);
2275
2276 /* Resolve the allocated and associated properties before doing anything
2277 else. If an array is not allocated or not associated then (at least
2278 for Fortran) there is no guarantee that the data to define the upper
2279 bound, lower bound, or stride will be correct. If RESOLVE_P is
2280 already false at this point then this is not the first dimension of
2281 the array and a more outer dimension has already marked this array as
2282 not allocated/associated, as such we just ignore this property. This
2283 is fine as GDB only checks the allocated/associated on the outer most
2284 dimension of the array. */
2285 prop = TYPE_ALLOCATED_PROP (type);
2286 if (prop != NULL && resolve_p
2287 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2288 {
2289 prop->set_const_val (value);
2290 if (value == 0)
2291 resolve_p = false;
2292 }
2293
2294 prop = TYPE_ASSOCIATED_PROP (type);
2295 if (prop != NULL && resolve_p
2296 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2297 {
2298 prop->set_const_val (value);
2299 if (value == 0)
2300 resolve_p = false;
2301 }
2302
2303 range_type = check_typedef (type->index_type ());
2304 range_type
2305 = resolve_dynamic_range (range_type, addr_stack, frame, rank, resolve_p);
2306
2307 ary_dim = check_typedef (type->target_type ());
2308 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
2309 {
2310 ary_dim = copy_type (ary_dim);
2311 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
2312 frame, rank - 1,
2313 resolve_p);
2314 }
2315 else
2316 elt_type = type->target_type ();
2317
2318 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
2319 if (prop != NULL && resolve_p)
2320 {
2321 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2322 {
2323 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
2324 bit_stride = (unsigned int) (value * 8);
2325 }
2326 else
2327 {
2328 /* Could be a bug in our code, but it could also happen
2329 if the DWARF info is not correct. Issue a warning,
2330 and assume no byte/bit stride (leave bit_stride = 0). */
2331 warning (_("cannot determine array stride for type %s"),
2332 type->name () ? type->name () : "<no name>");
2333 }
2334 }
2335 else
2336 bit_stride = type->field (0).bitsize ();
2337
2338 type_allocator alloc (type, type_allocator::SMASH);
2339 return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
2340 bit_stride);
2341 }
2342
2343 /* Resolve an array or string type with dynamic properties, return a new
2344 type with the dynamic properties resolved to actual values. The
2345 ADDR_STACK represents the location of the object being resolved. */
2346
2347 static struct type *
2348 resolve_dynamic_array_or_string (struct type *type,
2349 struct property_addr_info *addr_stack,
2350 const frame_info_ptr &frame)
2351 {
2352 CORE_ADDR value;
2353 int rank = 0;
2354
2355 /* For dynamic type resolution strings can be treated like arrays of
2356 characters. */
2357 gdb_assert (type->code () == TYPE_CODE_ARRAY
2358 || type->code () == TYPE_CODE_STRING);
2359
2360 type = copy_type (type);
2361
2362 /* Resolve the rank property to get rank value. */
2363 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
2364 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2365 {
2366 prop->set_const_val (value);
2367 rank = value;
2368
2369 if (rank == 0)
2370 {
2371 /* Rank is zero, if a variable is passed as an argument to a
2372 function. In this case the resolved type should not be an
2373 array, but should instead be that of an array element. */
2374 struct type *dynamic_array_type = type;
2375 type = copy_type (dynamic_array_type->target_type ());
2376 struct dynamic_prop_list *prop_list
2377 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2378 if (prop_list != nullptr)
2379 {
2380 struct obstack *obstack
2381 = &type->objfile_owner ()->objfile_obstack;
2382 TYPE_MAIN_TYPE (type)->dyn_prop_list
2383 = copy_dynamic_prop_list (obstack, prop_list);
2384 }
2385 return type;
2386 }
2387 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2388 {
2389 /* What would this even mean? A string with a dynamic rank
2390 greater than 1. */
2391 error (_("unable to handle string with dynamic rank greater than 1"));
2392 }
2393 else if (rank > 1)
2394 {
2395 /* Arrays with dynamic rank are initially just an array type
2396 with a target type that is the array element.
2397
2398 However, now we know the rank of the array we need to build
2399 the array of arrays structure that GDB expects, that is we
2400 need an array type that has a target which is an array type,
2401 and so on, until eventually, we have the element type at the
2402 end of the chain. Create all the additional array types here
2403 by copying the top level array type. */
2404 struct type *element_type = type->target_type ();
2405 struct type *rank_type = type;
2406 for (int i = 1; i < rank; i++)
2407 {
2408 rank_type->set_target_type (copy_type (rank_type));
2409 rank_type = rank_type->target_type ();
2410 }
2411 rank_type->set_target_type (element_type);
2412 }
2413 }
2414 else
2415 {
2416 rank = 1;
2417
2418 for (struct type *tmp_type = check_typedef (type->target_type ());
2419 tmp_type->code () == TYPE_CODE_ARRAY;
2420 tmp_type = check_typedef (tmp_type->target_type ()))
2421 ++rank;
2422 }
2423
2424 /* The rank that we calculated above is actually a count of the number of
2425 ranks. However, when we resolve the type of each individual array
2426 rank we should actually use a rank "offset", e.g. an array with a rank
2427 count of 1 (calculated above) will use the rank offset 0 in order to
2428 resolve the details of the first array dimension. As a result, we
2429 reduce the rank by 1 here. */
2430 --rank;
2431
2432 return resolve_dynamic_array_or_string_1 (type, addr_stack, frame, rank,
2433 true);
2434 }
2435
2436 /* Resolve dynamic bounds of members of the union TYPE to static
2437 bounds. ADDR_STACK is a stack of struct property_addr_info
2438 to be used if needed during the dynamic resolution. */
2439
2440 static struct type *
2441 resolve_dynamic_union (struct type *type,
2442 struct property_addr_info *addr_stack,
2443 const frame_info_ptr &frame)
2444 {
2445 struct type *resolved_type;
2446 int i;
2447 unsigned int max_len = 0;
2448
2449 gdb_assert (type->code () == TYPE_CODE_UNION);
2450
2451 resolved_type = copy_type (type);
2452 resolved_type->copy_fields (type);
2453 for (i = 0; i < resolved_type->num_fields (); ++i)
2454 {
2455 struct type *t;
2456
2457 if (type->field (i).is_static ())
2458 continue;
2459
2460 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
2461 addr_stack, frame, 0);
2462 resolved_type->field (i).set_type (t);
2463
2464 struct type *real_type = check_typedef (t);
2465 if (real_type->length () > max_len)
2466 max_len = real_type->length ();
2467 }
2468
2469 resolved_type->set_length (max_len);
2470 return resolved_type;
2471 }
2472
2473 /* See gdbtypes.h. */
2474
2475 bool
2476 variant::matches (ULONGEST value, bool is_unsigned) const
2477 {
2478 for (const discriminant_range &range : discriminants)
2479 if (range.contains (value, is_unsigned))
2480 return true;
2481 return false;
2482 }
2483
2484 static void
2485 compute_variant_fields_inner (struct type *type,
2486 struct property_addr_info *addr_stack,
2487 const variant_part &part,
2488 std::vector<bool> &flags);
2489
2490 /* A helper function to determine which variant fields will be active.
2491 This handles both the variant's direct fields, and any variant
2492 parts embedded in this variant. TYPE is the type we're examining.
2493 ADDR_STACK holds information about the concrete object. VARIANT is
2494 the current variant to be handled. FLAGS is where the results are
2495 stored -- this function sets the Nth element in FLAGS if the
2496 corresponding field is enabled. ENABLED is whether this variant is
2497 enabled or not. */
2498
2499 static void
2500 compute_variant_fields_recurse (struct type *type,
2501 struct property_addr_info *addr_stack,
2502 const variant &variant,
2503 std::vector<bool> &flags,
2504 bool enabled)
2505 {
2506 for (int field = variant.first_field; field < variant.last_field; ++field)
2507 flags[field] = enabled;
2508
2509 for (const variant_part &new_part : variant.parts)
2510 {
2511 if (enabled)
2512 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2513 else
2514 {
2515 for (const auto &sub_variant : new_part.variants)
2516 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2517 flags, enabled);
2518 }
2519 }
2520 }
2521
2522 /* A helper function to determine which variant fields will be active.
2523 This evaluates the discriminant, decides which variant (if any) is
2524 active, and then updates FLAGS to reflect which fields should be
2525 available. TYPE is the type we're examining. ADDR_STACK holds
2526 information about the concrete object. VARIANT is the current
2527 variant to be handled. FLAGS is where the results are stored --
2528 this function sets the Nth element in FLAGS if the corresponding
2529 field is enabled. */
2530
2531 static void
2532 compute_variant_fields_inner (struct type *type,
2533 struct property_addr_info *addr_stack,
2534 const variant_part &part,
2535 std::vector<bool> &flags)
2536 {
2537 /* Evaluate the discriminant. */
2538 std::optional<ULONGEST> discr_value;
2539 if (part.discriminant_index != -1)
2540 {
2541 int idx = part.discriminant_index;
2542
2543 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
2544 error (_("Cannot determine struct field location"
2545 " (invalid location kind)"));
2546
2547 if (addr_stack->valaddr.data () != NULL)
2548 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2549 idx);
2550 else
2551 {
2552 CORE_ADDR addr = (addr_stack->addr
2553 + (type->field (idx).loc_bitpos ()
2554 / TARGET_CHAR_BIT));
2555
2556 LONGEST bitsize = type->field (idx).bitsize ();
2557 LONGEST size = bitsize / 8;
2558 if (size == 0)
2559 size = type->field (idx).type ()->length ();
2560
2561 gdb_byte bits[sizeof (ULONGEST)];
2562 read_memory (addr, bits, size);
2563
2564 LONGEST bitpos = (type->field (idx).loc_bitpos ()
2565 % TARGET_CHAR_BIT);
2566
2567 discr_value = unpack_bits_as_long (type->field (idx).type (),
2568 bits, bitpos, bitsize);
2569 }
2570 }
2571
2572 /* Go through each variant and see which applies. */
2573 const variant *default_variant = nullptr;
2574 const variant *applied_variant = nullptr;
2575 for (const auto &variant : part.variants)
2576 {
2577 if (variant.is_default ())
2578 default_variant = &variant;
2579 else if (discr_value.has_value ()
2580 && variant.matches (*discr_value, part.is_unsigned))
2581 {
2582 applied_variant = &variant;
2583 break;
2584 }
2585 }
2586 if (applied_variant == nullptr)
2587 applied_variant = default_variant;
2588
2589 for (const auto &variant : part.variants)
2590 compute_variant_fields_recurse (type, addr_stack, variant,
2591 flags, applied_variant == &variant);
2592 }
2593
2594 /* Determine which variant fields are available in TYPE. The enabled
2595 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2596 about the concrete object. PARTS describes the top-level variant
2597 parts for this type. */
2598
2599 static void
2600 compute_variant_fields (struct type *type,
2601 struct type *resolved_type,
2602 struct property_addr_info *addr_stack,
2603 const gdb::array_view<variant_part> &parts)
2604 {
2605 /* Assume all fields are included by default. */
2606 std::vector<bool> flags (resolved_type->num_fields (), true);
2607
2608 /* Now disable fields based on the variants that control them. */
2609 for (const auto &part : parts)
2610 compute_variant_fields_inner (type, addr_stack, part, flags);
2611
2612 unsigned int nfields = std::count (flags.begin (), flags.end (), true);
2613 /* No need to zero-initialize the newly allocated fields, they'll be
2614 initialized by the copy in the loop below. */
2615 resolved_type->alloc_fields (nfields, false);
2616
2617 int out = 0;
2618 for (int i = 0; i < type->num_fields (); ++i)
2619 {
2620 if (!flags[i])
2621 continue;
2622
2623 resolved_type->field (out) = type->field (i);
2624 ++out;
2625 }
2626 }
2627
2628 /* Resolve dynamic bounds of members of the struct TYPE to static
2629 bounds. ADDR_STACK is a stack of struct property_addr_info to
2630 be used if needed during the dynamic resolution. */
2631
2632 static struct type *
2633 resolve_dynamic_struct (struct type *type,
2634 struct property_addr_info *addr_stack,
2635 const frame_info_ptr &frame)
2636 {
2637 struct type *resolved_type;
2638 int i;
2639 unsigned resolved_type_bit_length = 0;
2640
2641 gdb_assert (type->code () == TYPE_CODE_STRUCT);
2642
2643 resolved_type = copy_type (type);
2644
2645 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
2646 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
2647 {
2648 compute_variant_fields (type, resolved_type, addr_stack,
2649 *variant_prop->variant_parts ());
2650 /* We want to leave the property attached, so that the Rust code
2651 can tell whether the type was originally an enum. */
2652 variant_prop->set_original_type (type);
2653 }
2654 else
2655 {
2656 resolved_type->copy_fields (type);
2657 }
2658
2659 for (i = 0; i < resolved_type->num_fields (); ++i)
2660 {
2661 unsigned new_bit_length;
2662 struct property_addr_info pinfo;
2663
2664 if (resolved_type->field (i).is_static ())
2665 continue;
2666
2667 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
2668 {
2669 struct dwarf2_property_baton baton;
2670 baton.property_type
2671 = lookup_pointer_type (resolved_type->field (i).type ());
2672 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
2673
2674 struct dynamic_prop prop;
2675 prop.set_locexpr (&baton);
2676
2677 CORE_ADDR addr;
2678 if (dwarf2_evaluate_property (&prop, frame, addr_stack, &addr,
2679 {addr_stack->addr}))
2680 resolved_type->field (i).set_loc_bitpos
2681 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
2682 }
2683
2684 /* As we know this field is not a static field, the field's
2685 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2686 this is the case, but only trigger a simple error rather
2687 than an internal error if that fails. While failing
2688 that verification indicates a bug in our code, the error
2689 is not severe enough to suggest to the user he stops
2690 his debugging session because of it. */
2691 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
2692 error (_("Cannot determine struct field location"
2693 " (invalid location kind)"));
2694
2695 pinfo.type = check_typedef (resolved_type->field (i).type ());
2696 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
2697 pinfo.valaddr = addr_stack->valaddr;
2698 if (!pinfo.valaddr.empty ())
2699 pinfo.valaddr = pinfo.valaddr.slice (offset);
2700 pinfo.addr = addr_stack->addr + offset;
2701 pinfo.next = addr_stack;
2702
2703 resolved_type->field (i).set_type
2704 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
2705 &pinfo, frame, 0));
2706 gdb_assert (resolved_type->field (i).loc_kind ()
2707 == FIELD_LOC_KIND_BITPOS);
2708
2709 new_bit_length = resolved_type->field (i).loc_bitpos ();
2710 if (resolved_type->field (i).bitsize () != 0)
2711 new_bit_length += resolved_type->field (i).bitsize ();
2712 else
2713 {
2714 struct type *real_type
2715 = check_typedef (resolved_type->field (i).type ());
2716
2717 new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
2718 }
2719
2720 /* Normally, we would use the position and size of the last field
2721 to determine the size of the enclosing structure. But GCC seems
2722 to be encoding the position of some fields incorrectly when
2723 the struct contains a dynamic field that is not placed last.
2724 So we compute the struct size based on the field that has
2725 the highest position + size - probably the best we can do. */
2726 if (new_bit_length > resolved_type_bit_length)
2727 resolved_type_bit_length = new_bit_length;
2728 }
2729
2730 /* The length of a type won't change for fortran, but it does for C and Ada.
2731 For fortran the size of dynamic fields might change over time but not the
2732 type length of the structure. If we adapt it, we run into problems
2733 when calculating the element offset for arrays of structs. */
2734 if (current_language->la_language != language_fortran)
2735 resolved_type->set_length ((resolved_type_bit_length + TARGET_CHAR_BIT - 1)
2736 / TARGET_CHAR_BIT);
2737
2738 /* The Ada language uses this field as a cache for static fixed types: reset
2739 it as RESOLVED_TYPE must have its own static fixed type. */
2740 resolved_type->set_target_type (nullptr);
2741
2742 return resolved_type;
2743 }
2744
2745 /* Worker for resolved_dynamic_type. */
2746
2747 static struct type *
2748 resolve_dynamic_type_internal (struct type *type,
2749 struct property_addr_info *addr_stack,
2750 const frame_info_ptr &frame,
2751 int top_level)
2752 {
2753 struct type *real_type = check_typedef (type);
2754 struct type *resolved_type = nullptr;
2755 struct dynamic_prop *prop;
2756 CORE_ADDR value;
2757
2758 if (!is_dynamic_type_internal (real_type, top_level))
2759 return type;
2760
2761 std::optional<CORE_ADDR> type_length;
2762 prop = TYPE_DYNAMIC_LENGTH (type);
2763 if (prop != NULL
2764 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2765 type_length = value;
2766
2767 if (type->code () == TYPE_CODE_TYPEDEF)
2768 {
2769 resolved_type = copy_type (type);
2770 resolved_type->set_target_type
2771 (resolve_dynamic_type_internal (type->target_type (), addr_stack,
2772 frame, top_level));
2773 }
2774 else
2775 {
2776 /* Before trying to resolve TYPE, make sure it is not a stub. */
2777 type = real_type;
2778
2779 switch (type->code ())
2780 {
2781 case TYPE_CODE_REF:
2782 {
2783 struct property_addr_info pinfo;
2784
2785 pinfo.type = check_typedef (type->target_type ());
2786 pinfo.valaddr = {};
2787 if (addr_stack->valaddr.data () != NULL)
2788 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2789 type);
2790 else
2791 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
2792 pinfo.next = addr_stack;
2793
2794 resolved_type = copy_type (type);
2795 resolved_type->set_target_type
2796 (resolve_dynamic_type_internal (type->target_type (),
2797 &pinfo, frame, top_level));
2798 break;
2799 }
2800
2801 case TYPE_CODE_STRING:
2802 /* Strings are very much like an array of characters, and can be
2803 treated as one here. */
2804 case TYPE_CODE_ARRAY:
2805 resolved_type = resolve_dynamic_array_or_string (type, addr_stack,
2806 frame);
2807 break;
2808
2809 case TYPE_CODE_RANGE:
2810 /* Pass 0 for the rank value here, which indicates this is a
2811 range for the first rank of an array. The assumption is that
2812 this rank value is not actually required for the resolution of
2813 the dynamic range, otherwise, we'd be resolving this range
2814 within the context of a dynamic array. */
2815 resolved_type = resolve_dynamic_range (type, addr_stack, frame, 0);
2816 break;
2817
2818 case TYPE_CODE_UNION:
2819 resolved_type = resolve_dynamic_union (type, addr_stack, frame);
2820 break;
2821
2822 case TYPE_CODE_STRUCT:
2823 resolved_type = resolve_dynamic_struct (type, addr_stack, frame);
2824 break;
2825 }
2826 }
2827
2828 if (resolved_type == nullptr)
2829 return type;
2830
2831 if (type_length.has_value ())
2832 {
2833 resolved_type->set_length (*type_length);
2834 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
2835 }
2836
2837 /* Resolve data_location attribute. */
2838 prop = TYPE_DATA_LOCATION (resolved_type);
2839 if (prop != NULL
2840 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
2841 {
2842 /* Start of Fortran hack. See comment in f-lang.h for what is going
2843 on here.*/
2844 if (current_language->la_language == language_fortran
2845 && resolved_type->code () == TYPE_CODE_ARRAY)
2846 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2847 value);
2848 /* End of Fortran hack. */
2849 prop->set_const_val (value);
2850 }
2851
2852 return resolved_type;
2853 }
2854
2855 /* See gdbtypes.h */
2856
2857 struct type *
2858 resolve_dynamic_type (struct type *type,
2859 gdb::array_view<const gdb_byte> valaddr,
2860 CORE_ADDR addr,
2861 const frame_info_ptr *in_frame)
2862 {
2863 struct property_addr_info pinfo
2864 = {check_typedef (type), valaddr, addr, NULL};
2865
2866 frame_info_ptr frame;
2867 if (in_frame != nullptr)
2868 frame = *in_frame;
2869
2870 return resolve_dynamic_type_internal (type, &pinfo, frame, 1);
2871 }
2872
2873 /* See gdbtypes.h */
2874
2875 dynamic_prop *
2876 type::dyn_prop (dynamic_prop_node_kind prop_kind) const
2877 {
2878 dynamic_prop_list *node = this->main_type->dyn_prop_list;
2879
2880 while (node != NULL)
2881 {
2882 if (node->prop_kind == prop_kind)
2883 return &node->prop;
2884 node = node->next;
2885 }
2886 return NULL;
2887 }
2888
2889 /* See gdbtypes.h */
2890
2891 void
2892 type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
2893 {
2894 struct dynamic_prop_list *temp;
2895
2896 gdb_assert (this->is_objfile_owned ());
2897
2898 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
2899 struct dynamic_prop_list);
2900 temp->prop_kind = prop_kind;
2901 temp->prop = prop;
2902 temp->next = this->main_type->dyn_prop_list;
2903
2904 this->main_type->dyn_prop_list = temp;
2905 }
2906
2907 /* See gdbtypes.h. */
2908
2909 void
2910 type::remove_dyn_prop (dynamic_prop_node_kind kind)
2911 {
2912 struct dynamic_prop_list *prev_node, *curr_node;
2913
2914 curr_node = this->main_type->dyn_prop_list;
2915 prev_node = NULL;
2916
2917 while (NULL != curr_node)
2918 {
2919 if (curr_node->prop_kind == kind)
2920 {
2921 /* Update the linked list but don't free anything.
2922 The property was allocated on objstack and it is not known
2923 if we are on top of it. Nevertheless, everything is released
2924 when the complete objstack is freed. */
2925 if (NULL == prev_node)
2926 this->main_type->dyn_prop_list = curr_node->next;
2927 else
2928 prev_node->next = curr_node->next;
2929
2930 return;
2931 }
2932
2933 prev_node = curr_node;
2934 curr_node = curr_node->next;
2935 }
2936 }
2937
2938 /* Find the real type of TYPE. This function returns the real type,
2939 after removing all layers of typedefs, and completing opaque or stub
2940 types. Completion changes the TYPE argument, but stripping of
2941 typedefs does not.
2942
2943 Instance flags (e.g. const/volatile) are preserved as typedefs are
2944 stripped. If necessary a new qualified form of the underlying type
2945 is created.
2946
2947 NOTE: This will return a typedef if type::target_type for the typedef has
2948 not been computed and we're either in the middle of reading symbols, or
2949 there was no name for the typedef in the debug info.
2950
2951 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2952 QUITs in the symbol reading code can also throw.
2953 Thus this function can throw an exception.
2954
2955 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2956 the target type.
2957
2958 If this is a stubbed struct (i.e. declared as struct foo *), see if
2959 we can find a full definition in some other file. If so, copy this
2960 definition, so we can use it in future. There used to be a comment
2961 (but not any code) that if we don't find a full definition, we'd
2962 set a flag so we don't spend time in the future checking the same
2963 type. That would be a mistake, though--we might load in more
2964 symbols which contain a full definition for the type. */
2965
2966 struct type *
2967 check_typedef (struct type *type)
2968 {
2969 struct type *orig_type = type;
2970
2971 gdb_assert (type);
2972
2973 /* While we're removing typedefs, we don't want to lose qualifiers.
2974 E.g., const/volatile. */
2975 type_instance_flags instance_flags = type->instance_flags ();
2976
2977 while (type->code () == TYPE_CODE_TYPEDEF)
2978 {
2979 if (!type->target_type ())
2980 {
2981 const char *name;
2982 struct symbol *sym;
2983
2984 /* It is dangerous to call lookup_symbol if we are currently
2985 reading a symtab. Infinite recursion is one danger. */
2986 if (currently_reading_symtab)
2987 return make_qualified_type (type, instance_flags, NULL);
2988
2989 name = type->name ();
2990 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
2991 VAR_DOMAIN as appropriate? */
2992 if (name == NULL)
2993 {
2994 stub_noname_complaint ();
2995 return make_qualified_type (type, instance_flags, NULL);
2996 }
2997 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
2998 if (sym)
2999 type->set_target_type (sym->type ());
3000 else /* TYPE_CODE_UNDEF */
3001 type->set_target_type (type_allocator (type->arch ()).new_type ());
3002 }
3003 type = type->target_type ();
3004
3005 /* Preserve the instance flags as we traverse down the typedef chain.
3006
3007 Handling address spaces/classes is nasty, what do we do if there's a
3008 conflict?
3009 E.g., what if an outer typedef marks the type as class_1 and an inner
3010 typedef marks the type as class_2?
3011 This is the wrong place to do such error checking. We leave it to
3012 the code that created the typedef in the first place to flag the
3013 error. We just pick the outer address space (akin to letting the
3014 outer cast in a chain of casting win), instead of assuming
3015 "it can't happen". */
3016 {
3017 const type_instance_flags ALL_SPACES
3018 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3019 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3020 const type_instance_flags ALL_CLASSES
3021 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3022
3023 type_instance_flags new_instance_flags = type->instance_flags ();
3024
3025 /* Treat code vs data spaces and address classes separately. */
3026 if ((instance_flags & ALL_SPACES) != 0)
3027 new_instance_flags &= ~ALL_SPACES;
3028 if ((instance_flags & ALL_CLASSES) != 0)
3029 new_instance_flags &= ~ALL_CLASSES;
3030
3031 instance_flags |= new_instance_flags;
3032 }
3033 }
3034
3035 /* If this is a struct/class/union with no fields, then check
3036 whether a full definition exists somewhere else. This is for
3037 systems where a type definition with no fields is issued for such
3038 types, instead of identifying them as stub types in the first
3039 place. */
3040
3041 if (TYPE_IS_OPAQUE (type)
3042 && opaque_type_resolution
3043 && !currently_reading_symtab)
3044 {
3045 const char *name = type->name ();
3046 struct type *newtype;
3047
3048 if (name == NULL)
3049 {
3050 stub_noname_complaint ();
3051 return make_qualified_type (type, instance_flags, NULL);
3052 }
3053 newtype = lookup_transparent_type (name);
3054
3055 if (newtype)
3056 {
3057 /* If the resolved type and the stub are in the same
3058 objfile, then replace the stub type with the real deal.
3059 But if they're in separate objfiles, leave the stub
3060 alone; we'll just look up the transparent type every time
3061 we call check_typedef. We can't create pointers between
3062 types allocated to different objfiles, since they may
3063 have different lifetimes. Trying to copy NEWTYPE over to
3064 TYPE's objfile is pointless, too, since you'll have to
3065 move over any other types NEWTYPE refers to, which could
3066 be an unbounded amount of stuff. */
3067 if (newtype->objfile_owner () == type->objfile_owner ())
3068 type = make_qualified_type (newtype, type->instance_flags (), type);
3069 else
3070 type = newtype;
3071 }
3072 }
3073 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3074 types. */
3075 else if (type->is_stub () && !currently_reading_symtab)
3076 {
3077 const char *name = type->name ();
3078 /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
3079 as appropriate? */
3080 struct symbol *sym;
3081
3082 if (name == NULL)
3083 {
3084 stub_noname_complaint ();
3085 return make_qualified_type (type, instance_flags, NULL);
3086 }
3087 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
3088 if (sym)
3089 {
3090 /* Same as above for opaque types, we can replace the stub
3091 with the complete type only if they are in the same
3092 objfile. */
3093 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3094 type = make_qualified_type (sym->type (),
3095 type->instance_flags (), type);
3096 else
3097 type = sym->type ();
3098 }
3099 }
3100
3101 if (type->target_is_stub ())
3102 {
3103 struct type *target_type = check_typedef (type->target_type ());
3104
3105 if (target_type->is_stub () || target_type->target_is_stub ())
3106 {
3107 /* Nothing we can do. */
3108 }
3109 else if (type->code () == TYPE_CODE_RANGE)
3110 {
3111 type->set_length (target_type->length ());
3112 type->set_target_is_stub (false);
3113 }
3114 else if (type->code () == TYPE_CODE_ARRAY
3115 && update_static_array_size (type))
3116 type->set_target_is_stub (false);
3117 }
3118
3119 type = make_qualified_type (type, instance_flags, NULL);
3120
3121 /* Cache TYPE_LENGTH for future use. */
3122 orig_type->set_length (type->length ());
3123
3124 return type;
3125 }
3126
3127 /* Parse a type expression in the string [P..P+LENGTH). If an error
3128 occurs, silently return a void type. */
3129
3130 static struct type *
3131 safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
3132 {
3133 struct type *type = NULL; /* Initialize to keep gcc happy. */
3134
3135 /* Suppress error messages. */
3136 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3137 &null_stream);
3138
3139 /* Call parse_and_eval_type() without fear of longjmp()s. */
3140 try
3141 {
3142 type = parse_and_eval_type (p, length);
3143 }
3144 catch (const gdb_exception_error &except)
3145 {
3146 type = builtin_type (gdbarch)->builtin_void;
3147 }
3148
3149 return type;
3150 }
3151
3152 /* Ugly hack to convert method stubs into method types.
3153
3154 He ain't kiddin'. This demangles the name of the method into a
3155 string including argument types, parses out each argument type,
3156 generates a string casting a zero to that type, evaluates the
3157 string, and stuffs the resulting type into an argtype vector!!!
3158 Then it knows the type of the whole function (including argument
3159 types for overloading), which info used to be in the stab's but was
3160 removed to hack back the space required for them. */
3161
3162 static void
3163 check_stub_method (struct type *type, int method_id, int signature_id)
3164 {
3165 struct gdbarch *gdbarch = type->arch ();
3166 struct fn_field *f;
3167 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3168 gdb::unique_xmalloc_ptr<char> demangled_name
3169 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
3170 char *argtypetext, *p;
3171 int depth = 0, argcount = 1;
3172 struct field *argtypes;
3173 struct type *mtype;
3174
3175 /* Make sure we got back a function string that we can use. */
3176 if (demangled_name)
3177 p = strchr (demangled_name.get (), '(');
3178 else
3179 p = NULL;
3180
3181 if (demangled_name == NULL || p == NULL)
3182 error (_("Internal: Cannot demangle mangled name `%s'."),
3183 mangled_name);
3184
3185 /* Now, read in the parameters that define this type. */
3186 p += 1;
3187 argtypetext = p;
3188 while (*p)
3189 {
3190 if (*p == '(' || *p == '<')
3191 {
3192 depth += 1;
3193 }
3194 else if (*p == ')' || *p == '>')
3195 {
3196 depth -= 1;
3197 }
3198 else if (*p == ',' && depth == 0)
3199 {
3200 argcount += 1;
3201 }
3202
3203 p += 1;
3204 }
3205
3206 /* If we read one argument and it was ``void'', don't count it. */
3207 if (startswith (argtypetext, "(void)"))
3208 argcount -= 1;
3209
3210 /* We need one extra slot, for the THIS pointer. */
3211
3212 argtypes = (struct field *)
3213 TYPE_ZALLOC (type, (argcount + 1) * sizeof (struct field));
3214 p = argtypetext;
3215
3216 /* Add THIS pointer for non-static methods. */
3217 f = TYPE_FN_FIELDLIST1 (type, method_id);
3218 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3219 argcount = 0;
3220 else
3221 {
3222 argtypes[0].set_type (lookup_pointer_type (type));
3223 argcount = 1;
3224 }
3225
3226 if (*p != ')') /* () means no args, skip while. */
3227 {
3228 depth = 0;
3229 while (*p)
3230 {
3231 if (depth <= 0 && (*p == ',' || *p == ')'))
3232 {
3233 /* Avoid parsing of ellipsis, they will be handled below.
3234 Also avoid ``void'' as above. */
3235 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3236 && strncmp (argtypetext, "void", p - argtypetext) != 0)
3237 {
3238 argtypes[argcount].set_type
3239 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
3240 argcount += 1;
3241 }
3242 argtypetext = p + 1;
3243 }
3244
3245 if (*p == '(' || *p == '<')
3246 {
3247 depth += 1;
3248 }
3249 else if (*p == ')' || *p == '>')
3250 {
3251 depth -= 1;
3252 }
3253
3254 p += 1;
3255 }
3256 }
3257
3258 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3259
3260 /* Now update the old "stub" type into a real type. */
3261 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
3262 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3263 We want a method (TYPE_CODE_METHOD). */
3264 smash_to_method_type (mtype, type, mtype->target_type (),
3265 argtypes, argcount, p[-2] == '.');
3266 mtype->set_is_stub (false);
3267 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3268 }
3269
3270 /* This is the external interface to check_stub_method, above. This
3271 function unstubs all of the signatures for TYPE's METHOD_ID method
3272 name. After calling this function TYPE_FN_FIELD_STUB will be
3273 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3274 correct.
3275
3276 This function unfortunately can not die until stabs do. */
3277
3278 void
3279 check_stub_method_group (struct type *type, int method_id)
3280 {
3281 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3282 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
3283
3284 for (int j = 0; j < len; j++)
3285 {
3286 if (TYPE_FN_FIELD_STUB (f, j))
3287 check_stub_method (type, method_id, j);
3288 }
3289 }
3290
3291 /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
3292 const struct cplus_struct_type cplus_struct_default = { };
3293
3294 void
3295 allocate_cplus_struct_type (struct type *type)
3296 {
3297 if (HAVE_CPLUS_STRUCT (type))
3298 /* Structure was already allocated. Nothing more to do. */
3299 return;
3300
3301 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3302 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
3303 TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
3304 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
3305 set_type_vptr_fieldno (type, -1);
3306 }
3307
3308 const struct gnat_aux_type gnat_aux_default =
3309 { NULL };
3310
3311 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3312 and allocate the associated gnat-specific data. The gnat-specific
3313 data is also initialized to gnat_aux_default. */
3314
3315 void
3316 allocate_gnat_aux_type (struct type *type)
3317 {
3318 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3319 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
3320 TYPE_ZALLOC (type, sizeof (struct gnat_aux_type));
3321 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3322 }
3323
3324 /* Helper function to verify floating-point format and size.
3325 BIT is the type size in bits; if BIT equals -1, the size is
3326 determined by the floatformat. Returns size to be used. */
3327
3328 static int
3329 verify_floatformat (int bit, const struct floatformat *floatformat)
3330 {
3331 gdb_assert (floatformat != NULL);
3332
3333 if (bit == -1)
3334 bit = floatformat->totalsize;
3335
3336 gdb_assert (bit >= 0);
3337 gdb_assert (bit >= floatformat->totalsize);
3338
3339 return bit;
3340 }
3341
3342 /* Return the floating-point format for a floating-point variable of
3343 type TYPE. */
3344
3345 const struct floatformat *
3346 floatformat_from_type (const struct type *type)
3347 {
3348 gdb_assert (type->code () == TYPE_CODE_FLT);
3349 gdb_assert (TYPE_FLOATFORMAT (type));
3350 return TYPE_FLOATFORMAT (type);
3351 }
3352
3353 /* See gdbtypes.h. */
3354
3355 struct type *
3356 init_integer_type (type_allocator &alloc,
3357 int bit, int unsigned_p, const char *name)
3358 {
3359 struct type *t;
3360
3361 t = alloc.new_type (TYPE_CODE_INT, bit, name);
3362 if (unsigned_p)
3363 t->set_is_unsigned (true);
3364
3365 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3366 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3367 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3368
3369 return t;
3370 }
3371
3372 /* See gdbtypes.h. */
3373
3374 struct type *
3375 init_character_type (type_allocator &alloc,
3376 int bit, int unsigned_p, const char *name)
3377 {
3378 struct type *t;
3379
3380 t = alloc.new_type (TYPE_CODE_CHAR, bit, name);
3381 if (unsigned_p)
3382 t->set_is_unsigned (true);
3383
3384 return t;
3385 }
3386
3387 /* See gdbtypes.h. */
3388
3389 struct type *
3390 init_boolean_type (type_allocator &alloc,
3391 int bit, int unsigned_p, const char *name)
3392 {
3393 struct type *t;
3394
3395 t = alloc.new_type (TYPE_CODE_BOOL, bit, name);
3396 if (unsigned_p)
3397 t->set_is_unsigned (true);
3398
3399 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3400 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3401 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3402
3403 return t;
3404 }
3405
3406 /* See gdbtypes.h. */
3407
3408 struct type *
3409 init_float_type (type_allocator &alloc,
3410 int bit, const char *name,
3411 const struct floatformat **floatformats,
3412 enum bfd_endian byte_order)
3413 {
3414 if (byte_order == BFD_ENDIAN_UNKNOWN)
3415 {
3416 struct gdbarch *gdbarch = alloc.arch ();
3417 byte_order = gdbarch_byte_order (gdbarch);
3418 }
3419 const struct floatformat *fmt = floatformats[byte_order];
3420 struct type *t;
3421
3422 bit = verify_floatformat (bit, fmt);
3423 t = alloc.new_type (TYPE_CODE_FLT, bit, name);
3424 TYPE_FLOATFORMAT (t) = fmt;
3425
3426 return t;
3427 }
3428
3429 /* See gdbtypes.h. */
3430
3431 struct type *
3432 init_decfloat_type (type_allocator &alloc, int bit, const char *name)
3433 {
3434 return alloc.new_type (TYPE_CODE_DECFLOAT, bit, name);
3435 }
3436
3437 /* Return true if init_complex_type can be called with TARGET_TYPE. */
3438
3439 bool
3440 can_create_complex_type (struct type *target_type)
3441 {
3442 return (target_type->code () == TYPE_CODE_INT
3443 || target_type->code () == TYPE_CODE_FLT);
3444 }
3445
3446 /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3447 name. TARGET_TYPE is the component type. */
3448
3449 struct type *
3450 init_complex_type (const char *name, struct type *target_type)
3451 {
3452 struct type *t;
3453
3454 gdb_assert (can_create_complex_type (target_type));
3455
3456 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3457 {
3458 if (name == nullptr && target_type->name () != nullptr)
3459 {
3460 /* No zero-initialization required, initialized by strcpy/strcat
3461 below. */
3462 char *new_name
3463 = (char *) TYPE_ALLOC (target_type,
3464 strlen (target_type->name ())
3465 + strlen ("_Complex ") + 1);
3466 strcpy (new_name, "_Complex ");
3467 strcat (new_name, target_type->name ());
3468 name = new_name;
3469 }
3470
3471 t = type_allocator (target_type).new_type ();
3472 set_type_code (t, TYPE_CODE_COMPLEX);
3473 t->set_length (2 * target_type->length ());
3474 t->set_name (name);
3475
3476 t->set_target_type (target_type);
3477 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3478 }
3479
3480 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
3481 }
3482
3483 /* See gdbtypes.h. */
3484
3485 struct type *
3486 init_pointer_type (type_allocator &alloc,
3487 int bit, const char *name, struct type *target_type)
3488 {
3489 struct type *t;
3490
3491 t = alloc.new_type (TYPE_CODE_PTR, bit, name);
3492 t->set_target_type (target_type);
3493 t->set_is_unsigned (true);
3494 return t;
3495 }
3496
3497 /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3498 BIT is the pointer type size in bits.
3499 UNSIGNED_P should be nonzero if the type is unsigned.
3500 NAME is the type name. */
3501
3502 struct type *
3503 init_fixed_point_type (type_allocator &alloc,
3504 int bit, int unsigned_p, const char *name)
3505 {
3506 struct type *t;
3507
3508 t = alloc.new_type (TYPE_CODE_FIXED_POINT, bit, name);
3509 if (unsigned_p)
3510 t->set_is_unsigned (true);
3511
3512 return t;
3513 }
3514
3515 /* See gdbtypes.h. */
3516
3517 unsigned
3518 type_raw_align (struct type *type)
3519 {
3520 if (type->align_log2 != 0)
3521 return 1 << (type->align_log2 - 1);
3522 return 0;
3523 }
3524
3525 /* See gdbtypes.h. */
3526
3527 unsigned
3528 type_align (struct type *type)
3529 {
3530 /* Check alignment provided in the debug information. */
3531 unsigned raw_align = type_raw_align (type);
3532 if (raw_align != 0)
3533 return raw_align;
3534
3535 /* Allow the architecture to provide an alignment. */
3536 ULONGEST align = gdbarch_type_align (type->arch (), type);
3537 if (align != 0)
3538 return align;
3539
3540 switch (type->code ())
3541 {
3542 case TYPE_CODE_PTR:
3543 case TYPE_CODE_FUNC:
3544 case TYPE_CODE_FLAGS:
3545 case TYPE_CODE_INT:
3546 case TYPE_CODE_RANGE:
3547 case TYPE_CODE_FLT:
3548 case TYPE_CODE_ENUM:
3549 case TYPE_CODE_REF:
3550 case TYPE_CODE_RVALUE_REF:
3551 case TYPE_CODE_CHAR:
3552 case TYPE_CODE_BOOL:
3553 case TYPE_CODE_DECFLOAT:
3554 case TYPE_CODE_METHODPTR:
3555 case TYPE_CODE_MEMBERPTR:
3556 align = type_length_units (check_typedef (type));
3557 break;
3558
3559 case TYPE_CODE_ARRAY:
3560 case TYPE_CODE_COMPLEX:
3561 case TYPE_CODE_TYPEDEF:
3562 align = type_align (type->target_type ());
3563 break;
3564
3565 case TYPE_CODE_STRUCT:
3566 case TYPE_CODE_UNION:
3567 {
3568 int number_of_non_static_fields = 0;
3569 for (unsigned i = 0; i < type->num_fields (); ++i)
3570 {
3571 if (!type->field (i).is_static ())
3572 {
3573 number_of_non_static_fields++;
3574 ULONGEST f_align = type_align (type->field (i).type ());
3575 if (f_align == 0)
3576 {
3577 /* Don't pretend we know something we don't. */
3578 align = 0;
3579 break;
3580 }
3581 if (f_align > align)
3582 align = f_align;
3583 }
3584 }
3585 /* A struct with no fields, or with only static fields has an
3586 alignment of 1. */
3587 if (number_of_non_static_fields == 0)
3588 align = 1;
3589 }
3590 break;
3591
3592 case TYPE_CODE_SET:
3593 case TYPE_CODE_STRING:
3594 /* Not sure what to do here, and these can't appear in C or C++
3595 anyway. */
3596 break;
3597
3598 case TYPE_CODE_VOID:
3599 align = 1;
3600 break;
3601
3602 case TYPE_CODE_ERROR:
3603 case TYPE_CODE_METHOD:
3604 default:
3605 break;
3606 }
3607
3608 if ((align & (align - 1)) != 0)
3609 {
3610 /* Not a power of 2, so pass. */
3611 align = 0;
3612 }
3613
3614 return align;
3615 }
3616
3617 /* See gdbtypes.h. */
3618
3619 bool
3620 set_type_align (struct type *type, ULONGEST align)
3621 {
3622 /* Must be a power of 2. Zero is ok. */
3623 gdb_assert ((align & (align - 1)) == 0);
3624
3625 unsigned result = 0;
3626 while (align != 0)
3627 {
3628 ++result;
3629 align >>= 1;
3630 }
3631
3632 if (result >= (1 << TYPE_ALIGN_BITS))
3633 return false;
3634
3635 type->align_log2 = result;
3636 return true;
3637 }
3638
3639 \f
3640 /* Queries on types. */
3641
3642 int
3643 can_dereference (struct type *t)
3644 {
3645 /* FIXME: Should we return true for references as well as
3646 pointers? */
3647 t = check_typedef (t);
3648 return
3649 (t != NULL
3650 && t->code () == TYPE_CODE_PTR
3651 && t->target_type ()->code () != TYPE_CODE_VOID);
3652 }
3653
3654 int
3655 is_integral_type (struct type *t)
3656 {
3657 t = check_typedef (t);
3658 return
3659 ((t != NULL)
3660 && !is_fixed_point_type (t)
3661 && ((t->code () == TYPE_CODE_INT)
3662 || (t->code () == TYPE_CODE_ENUM)
3663 || (t->code () == TYPE_CODE_FLAGS)
3664 || (t->code () == TYPE_CODE_CHAR)
3665 || (t->code () == TYPE_CODE_RANGE)
3666 || (t->code () == TYPE_CODE_BOOL)));
3667 }
3668
3669 int
3670 is_floating_type (struct type *t)
3671 {
3672 t = check_typedef (t);
3673 return
3674 ((t != NULL)
3675 && ((t->code () == TYPE_CODE_FLT)
3676 || (t->code () == TYPE_CODE_DECFLOAT)));
3677 }
3678
3679 /* Return true if TYPE is scalar. */
3680
3681 int
3682 is_scalar_type (struct type *type)
3683 {
3684 type = check_typedef (type);
3685
3686 if (is_fixed_point_type (type))
3687 return 0; /* Implemented as a scalar, but more like a floating point. */
3688
3689 switch (type->code ())
3690 {
3691 case TYPE_CODE_ARRAY:
3692 case TYPE_CODE_STRUCT:
3693 case TYPE_CODE_UNION:
3694 case TYPE_CODE_SET:
3695 case TYPE_CODE_STRING:
3696 return 0;
3697 default:
3698 return 1;
3699 }
3700 }
3701
3702 /* Return true if T is scalar, or a composite type which in practice has
3703 the memory layout of a scalar type. E.g., an array or struct with only
3704 one scalar element inside it, or a union with only scalar elements. */
3705
3706 int
3707 is_scalar_type_recursive (struct type *t)
3708 {
3709 t = check_typedef (t);
3710
3711 if (is_scalar_type (t))
3712 return 1;
3713 /* Are we dealing with an array or string of known dimensions? */
3714 else if ((t->code () == TYPE_CODE_ARRAY
3715 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3716 && t->index_type ()->code () == TYPE_CODE_RANGE)
3717 {
3718 LONGEST low_bound, high_bound;
3719 struct type *elt_type = check_typedef (t->target_type ());
3720
3721 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3722 return (high_bound == low_bound
3723 && is_scalar_type_recursive (elt_type));
3724 else
3725 return 0;
3726 }
3727 /* Are we dealing with a struct with one element? */
3728 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
3729 return is_scalar_type_recursive (t->field (0).type ());
3730 else if (t->code () == TYPE_CODE_UNION)
3731 {
3732 int i, n = t->num_fields ();
3733
3734 /* If all elements of the union are scalar, then the union is scalar. */
3735 for (i = 0; i < n; i++)
3736 if (!is_scalar_type_recursive (t->field (i).type ()))
3737 return 0;
3738
3739 return 1;
3740 }
3741
3742 return 0;
3743 }
3744
3745 /* Return true is T is a class or a union. False otherwise. */
3746
3747 int
3748 class_or_union_p (const struct type *t)
3749 {
3750 return (t->code () == TYPE_CODE_STRUCT
3751 || t->code () == TYPE_CODE_UNION);
3752 }
3753
3754 /* A helper function which returns true if types A and B represent the
3755 "same" class type. This is true if the types have the same main
3756 type, or the same name. */
3757
3758 int
3759 class_types_same_p (const struct type *a, const struct type *b)
3760 {
3761 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
3762 || (a->name () && b->name ()
3763 && !strcmp (a->name (), b->name ())));
3764 }
3765
3766 /* If BASE is an ancestor of DCLASS return the distance between them.
3767 otherwise return -1;
3768 eg:
3769
3770 class A {};
3771 class B: public A {};
3772 class C: public B {};
3773 class D: C {};
3774
3775 distance_to_ancestor (A, A, 0) = 0
3776 distance_to_ancestor (A, B, 0) = 1
3777 distance_to_ancestor (A, C, 0) = 2
3778 distance_to_ancestor (A, D, 0) = 3
3779
3780 If PUBLIC is 1 then only public ancestors are considered,
3781 and the function returns the distance only if BASE is a public ancestor
3782 of DCLASS.
3783 Eg:
3784
3785 distance_to_ancestor (A, D, 1) = -1. */
3786
3787 static int
3788 distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
3789 {
3790 int i;
3791 int d;
3792
3793 base = check_typedef (base);
3794 dclass = check_typedef (dclass);
3795
3796 if (class_types_same_p (base, dclass))
3797 return 0;
3798
3799 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
3800 {
3801 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
3802 continue;
3803
3804 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
3805 if (d >= 0)
3806 return 1 + d;
3807 }
3808
3809 return -1;
3810 }
3811
3812 /* Check whether BASE is an ancestor or base class or DCLASS
3813 Return 1 if so, and 0 if not.
3814 Note: If BASE and DCLASS are of the same type, this function
3815 will return 1. So for some class A, is_ancestor (A, A) will
3816 return 1. */
3817
3818 int
3819 is_ancestor (struct type *base, struct type *dclass)
3820 {
3821 return distance_to_ancestor (base, dclass, 0) >= 0;
3822 }
3823
3824 /* Like is_ancestor, but only returns true when BASE is a public
3825 ancestor of DCLASS. */
3826
3827 int
3828 is_public_ancestor (struct type *base, struct type *dclass)
3829 {
3830 return distance_to_ancestor (base, dclass, 1) >= 0;
3831 }
3832
3833 /* A helper function for is_unique_ancestor. */
3834
3835 static int
3836 is_unique_ancestor_worker (struct type *base, struct type *dclass,
3837 int *offset,
3838 const gdb_byte *valaddr, int embedded_offset,
3839 CORE_ADDR address, struct value *val)
3840 {
3841 int i, count = 0;
3842
3843 base = check_typedef (base);
3844 dclass = check_typedef (dclass);
3845
3846 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3847 {
3848 struct type *iter;
3849 int this_offset;
3850
3851 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3852
3853 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3854 address, val);
3855
3856 if (class_types_same_p (base, iter))
3857 {
3858 /* If this is the first subclass, set *OFFSET and set count
3859 to 1. Otherwise, if this is at the same offset as
3860 previous instances, do nothing. Otherwise, increment
3861 count. */
3862 if (*offset == -1)
3863 {
3864 *offset = this_offset;
3865 count = 1;
3866 }
3867 else if (this_offset == *offset)
3868 {
3869 /* Nothing. */
3870 }
3871 else
3872 ++count;
3873 }
3874 else
3875 count += is_unique_ancestor_worker (base, iter, offset,
3876 valaddr,
3877 embedded_offset + this_offset,
3878 address, val);
3879 }
3880
3881 return count;
3882 }
3883
3884 /* Like is_ancestor, but only returns true if BASE is a unique base
3885 class of the type of VAL. */
3886
3887 int
3888 is_unique_ancestor (struct type *base, struct value *val)
3889 {
3890 int offset = -1;
3891
3892 return is_unique_ancestor_worker (base, val->type (), &offset,
3893 val->contents_for_printing ().data (),
3894 val->embedded_offset (),
3895 val->address (), val) == 1;
3896 }
3897
3898 /* See gdbtypes.h. */
3899
3900 enum bfd_endian
3901 type_byte_order (const struct type *type)
3902 {
3903 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
3904 if (type->endianity_is_not_default ())
3905 {
3906 if (byteorder == BFD_ENDIAN_BIG)
3907 return BFD_ENDIAN_LITTLE;
3908 else
3909 {
3910 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
3911 return BFD_ENDIAN_BIG;
3912 }
3913 }
3914
3915 return byteorder;
3916 }
3917
3918 /* See gdbtypes.h. */
3919
3920 bool
3921 is_nocall_function (const struct type *type)
3922 {
3923 if (type->code () != TYPE_CODE_FUNC && type->code () != TYPE_CODE_METHOD)
3924 return false;
3925
3926 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
3927 }
3928
3929 \f
3930 /* Overload resolution. */
3931
3932 /* Return the sum of the rank of A with the rank of B. */
3933
3934 struct rank
3935 sum_ranks (struct rank a, struct rank b)
3936 {
3937 struct rank c;
3938 c.rank = a.rank + b.rank;
3939 c.subrank = a.subrank + b.subrank;
3940 return c;
3941 }
3942
3943 /* Compare rank A and B and return:
3944 0 if a = b
3945 1 if a is better than b
3946 -1 if b is better than a. */
3947
3948 int
3949 compare_ranks (struct rank a, struct rank b)
3950 {
3951 if (a.rank == b.rank)
3952 {
3953 if (a.subrank == b.subrank)
3954 return 0;
3955 if (a.subrank < b.subrank)
3956 return 1;
3957 if (a.subrank > b.subrank)
3958 return -1;
3959 }
3960
3961 if (a.rank < b.rank)
3962 return 1;
3963
3964 /* a.rank > b.rank */
3965 return -1;
3966 }
3967
3968 /* Functions for overload resolution begin here. */
3969
3970 /* Compare two badness vectors A and B and return the result.
3971 0 => A and B are identical
3972 1 => A and B are incomparable
3973 2 => A is better than B
3974 3 => A is worse than B */
3975
3976 int
3977 compare_badness (const badness_vector &a, const badness_vector &b)
3978 {
3979 int i;
3980 int tmp;
3981 /* Any positives in comparison? */
3982 bool found_pos = false;
3983 /* Any negatives in comparison? */
3984 bool found_neg = false;
3985 /* Did A have any INVALID_CONVERSION entries. */
3986 bool a_invalid = false;
3987 /* Did B have any INVALID_CONVERSION entries. */
3988 bool b_invalid = false;
3989
3990 /* differing sizes => incomparable */
3991 if (a.size () != b.size ())
3992 return 1;
3993
3994 /* Subtract b from a */
3995 for (i = 0; i < a.size (); i++)
3996 {
3997 tmp = compare_ranks (b[i], a[i]);
3998 if (tmp > 0)
3999 found_pos = true;
4000 else if (tmp < 0)
4001 found_neg = true;
4002 if (a[i].rank >= INVALID_CONVERSION)
4003 a_invalid = true;
4004 if (b[i].rank >= INVALID_CONVERSION)
4005 b_invalid = true;
4006 }
4007
4008 /* B will only be considered better than or incomparable to A if
4009 they both have invalid entries, or if neither does. That is, if
4010 A has only valid entries, and B has an invalid entry, then A will
4011 be considered better than B, even if B happens to be better for
4012 some parameter. */
4013 if (a_invalid != b_invalid)
4014 {
4015 if (a_invalid)
4016 return 3; /* A > B */
4017 return 2; /* A < B */
4018 }
4019 else if (found_pos)
4020 {
4021 if (found_neg)
4022 return 1; /* incomparable */
4023 else
4024 return 3; /* A > B */
4025 }
4026 else
4027 /* no positives */
4028 {
4029 if (found_neg)
4030 return 2; /* A < B */
4031 else
4032 return 0; /* A == B */
4033 }
4034 }
4035
4036 /* Rank a function by comparing its parameter types (PARMS), to the
4037 types of an argument list (ARGS). Return the badness vector. This
4038 has ARGS.size() + 1 entries. */
4039
4040 badness_vector
4041 rank_function (gdb::array_view<type *> parms,
4042 gdb::array_view<value *> args,
4043 bool varargs)
4044 {
4045 /* add 1 for the length-match rank. */
4046 badness_vector bv;
4047 bv.reserve (1 + args.size ());
4048
4049 /* First compare the lengths of the supplied lists.
4050 If there is a mismatch, set it to a high value. */
4051
4052 /* pai/1997-06-03 FIXME: when we have debug info about default
4053 arguments and ellipsis parameter lists, we should consider those
4054 and rank the length-match more finely. */
4055
4056 bv.push_back ((args.size () != parms.size ()
4057 && (! varargs || args.size () < parms.size ()))
4058 ? LENGTH_MISMATCH_BADNESS
4059 : EXACT_MATCH_BADNESS);
4060
4061 /* Now rank all the parameters of the candidate function. */
4062 size_t min_len = std::min (parms.size (), args.size ());
4063
4064 for (size_t i = 0; i < min_len; i++)
4065 bv.push_back (rank_one_type (parms[i], args[i]->type (),
4066 args[i]));
4067
4068 /* If more arguments than parameters, add dummy entries. */
4069 for (size_t i = min_len; i < args.size (); i++)
4070 bv.push_back (varargs ? VARARG_BADNESS : TOO_FEW_PARAMS_BADNESS);
4071
4072 return bv;
4073 }
4074
4075 /* Compare the names of two integer types, assuming that any sign
4076 qualifiers have been checked already. We do it this way because
4077 there may be an "int" in the name of one of the types. */
4078
4079 static int
4080 integer_types_same_name_p (const char *first, const char *second)
4081 {
4082 int first_p, second_p;
4083
4084 /* If both are shorts, return 1; if neither is a short, keep
4085 checking. */
4086 first_p = (strstr (first, "short") != NULL);
4087 second_p = (strstr (second, "short") != NULL);
4088 if (first_p && second_p)
4089 return 1;
4090 if (first_p || second_p)
4091 return 0;
4092
4093 /* Likewise for long. */
4094 first_p = (strstr (first, "long") != NULL);
4095 second_p = (strstr (second, "long") != NULL);
4096 if (first_p && second_p)
4097 return 1;
4098 if (first_p || second_p)
4099 return 0;
4100
4101 /* Likewise for char. */
4102 first_p = (strstr (first, "char") != NULL);
4103 second_p = (strstr (second, "char") != NULL);
4104 if (first_p && second_p)
4105 return 1;
4106 if (first_p || second_p)
4107 return 0;
4108
4109 /* They must both be ints. */
4110 return 1;
4111 }
4112
4113 /* Compares type A to type B. Returns true if they represent the same
4114 type, false otherwise. */
4115
4116 bool
4117 types_equal (struct type *a, struct type *b)
4118 {
4119 /* Identical type pointers. */
4120 /* However, this still doesn't catch all cases of same type for b
4121 and a. The reason is that builtin types are different from
4122 the same ones constructed from the object. */
4123 if (a == b)
4124 return true;
4125
4126 /* Resolve typedefs */
4127 if (a->code () == TYPE_CODE_TYPEDEF)
4128 a = check_typedef (a);
4129 if (b->code () == TYPE_CODE_TYPEDEF)
4130 b = check_typedef (b);
4131
4132 /* Check if identical after resolving typedefs. */
4133 if (a == b)
4134 return true;
4135
4136 /* If after resolving typedefs a and b are not of the same type
4137 code then they are not equal. */
4138 if (a->code () != b->code ())
4139 return false;
4140
4141 /* If a and b are both pointers types or both reference types then
4142 they are equal of the same type iff the objects they refer to are
4143 of the same type. */
4144 if (a->code () == TYPE_CODE_PTR
4145 || a->code () == TYPE_CODE_REF)
4146 return types_equal (a->target_type (),
4147 b->target_type ());
4148
4149 /* Well, damnit, if the names are exactly the same, I'll say they
4150 are exactly the same. This happens when we generate method
4151 stubs. The types won't point to the same address, but they
4152 really are the same. */
4153
4154 if (a->name () && b->name ()
4155 && strcmp (a->name (), b->name ()) == 0)
4156 return true;
4157
4158 /* Two function types are equal if their argument and return types
4159 are equal. */
4160 if (a->code () == TYPE_CODE_FUNC)
4161 {
4162 int i;
4163
4164 if (a->num_fields () != b->num_fields ())
4165 return false;
4166
4167 if (!types_equal (a->target_type (), b->target_type ()))
4168 return false;
4169
4170 for (i = 0; i < a->num_fields (); ++i)
4171 if (!types_equal (a->field (i).type (), b->field (i).type ()))
4172 return false;
4173
4174 return true;
4175 }
4176
4177 return false;
4178 }
4179 \f
4180 /* Deep comparison of types. */
4181
4182 /* An entry in the type-equality bcache. */
4183
4184 struct type_equality_entry
4185 {
4186 type_equality_entry (struct type *t1, struct type *t2)
4187 : type1 (t1),
4188 type2 (t2)
4189 {
4190 }
4191
4192 struct type *type1, *type2;
4193 };
4194
4195 /* A helper function to compare two strings. Returns true if they are
4196 the same, false otherwise. Handles NULLs properly. */
4197
4198 static bool
4199 compare_maybe_null_strings (const char *s, const char *t)
4200 {
4201 if (s == NULL || t == NULL)
4202 return s == t;
4203 return strcmp (s, t) == 0;
4204 }
4205
4206 /* A helper function for check_types_worklist that checks two types for
4207 "deep" equality. Returns true if the types are considered the
4208 same, false otherwise. */
4209
4210 static bool
4211 check_types_equal (struct type *type1, struct type *type2,
4212 std::vector<type_equality_entry> *worklist)
4213 {
4214 type1 = check_typedef (type1);
4215 type2 = check_typedef (type2);
4216
4217 if (type1 == type2)
4218 return true;
4219
4220 if (type1->code () != type2->code ()
4221 || type1->length () != type2->length ()
4222 || type1->is_unsigned () != type2->is_unsigned ()
4223 || type1->has_no_signedness () != type2->has_no_signedness ()
4224 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
4225 || type1->has_varargs () != type2->has_varargs ()
4226 || type1->is_vector () != type2->is_vector ()
4227 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
4228 || type1->instance_flags () != type2->instance_flags ()
4229 || type1->num_fields () != type2->num_fields ())
4230 return false;
4231
4232 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4233 return false;
4234 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
4235 return false;
4236
4237 if (type1->code () == TYPE_CODE_RANGE)
4238 {
4239 if (*type1->bounds () != *type2->bounds ())
4240 return false;
4241 }
4242 else
4243 {
4244 int i;
4245
4246 for (i = 0; i < type1->num_fields (); ++i)
4247 {
4248 const struct field *field1 = &type1->field (i);
4249 const struct field *field2 = &type2->field (i);
4250
4251 if (field1->is_artificial () != field2->is_artificial ()
4252 || field1->bitsize () != field2->bitsize ()
4253 || field1->loc_kind () != field2->loc_kind ())
4254 return false;
4255 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
4256 return false;
4257 switch (field1->loc_kind ())
4258 {
4259 case FIELD_LOC_KIND_BITPOS:
4260 if (field1->loc_bitpos () != field2->loc_bitpos ())
4261 return false;
4262 break;
4263 case FIELD_LOC_KIND_ENUMVAL:
4264 if (field1->loc_enumval () != field2->loc_enumval ())
4265 return false;
4266 /* Don't compare types of enum fields, because they don't
4267 have a type. */
4268 continue;
4269 case FIELD_LOC_KIND_PHYSADDR:
4270 if (field1->loc_physaddr () != field2->loc_physaddr ())
4271 return false;
4272 break;
4273 case FIELD_LOC_KIND_PHYSNAME:
4274 if (!compare_maybe_null_strings (field1->loc_physname (),
4275 field2->loc_physname ()))
4276 return false;
4277 break;
4278 case FIELD_LOC_KIND_DWARF_BLOCK:
4279 {
4280 struct dwarf2_locexpr_baton *block1, *block2;
4281
4282 block1 = field1->loc_dwarf_block ();
4283 block2 = field2->loc_dwarf_block ();
4284 if (block1->per_cu != block2->per_cu
4285 || block1->size != block2->size
4286 || memcmp (block1->data, block2->data, block1->size) != 0)
4287 return false;
4288 }
4289 break;
4290 default:
4291 internal_error (_("Unsupported field kind "
4292 "%d by check_types_equal"),
4293 field1->loc_kind ());
4294 }
4295
4296 worklist->emplace_back (field1->type (), field2->type ());
4297 }
4298 }
4299
4300 if (type1->target_type () != NULL)
4301 {
4302 if (type2->target_type () == NULL)
4303 return false;
4304
4305 worklist->emplace_back (type1->target_type (),
4306 type2->target_type ());
4307 }
4308 else if (type2->target_type () != NULL)
4309 return false;
4310
4311 return true;
4312 }
4313
4314 /* Check types on a worklist for equality. Returns false if any pair
4315 is not equal, true if they are all considered equal. */
4316
4317 static bool
4318 check_types_worklist (std::vector<type_equality_entry> *worklist,
4319 gdb::bcache *cache)
4320 {
4321 while (!worklist->empty ())
4322 {
4323 bool added;
4324
4325 struct type_equality_entry entry = std::move (worklist->back ());
4326 worklist->pop_back ();
4327
4328 /* If the type pair has already been visited, we know it is
4329 ok. */
4330 cache->insert (&entry, sizeof (entry), &added);
4331 if (!added)
4332 continue;
4333
4334 if (!check_types_equal (entry.type1, entry.type2, worklist))
4335 return false;
4336 }
4337
4338 return true;
4339 }
4340
4341 /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4342 "deep comparison". Otherwise return false. */
4343
4344 bool
4345 types_deeply_equal (struct type *type1, struct type *type2)
4346 {
4347 std::vector<type_equality_entry> worklist;
4348
4349 gdb_assert (type1 != NULL && type2 != NULL);
4350
4351 /* Early exit for the simple case. */
4352 if (type1 == type2)
4353 return true;
4354
4355 gdb::bcache cache;
4356 worklist.emplace_back (type1, type2);
4357 return check_types_worklist (&worklist, &cache);
4358 }
4359
4360 /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4361 Otherwise return one. */
4362
4363 int
4364 type_not_allocated (const struct type *type)
4365 {
4366 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4367
4368 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4369 }
4370
4371 /* Associated status of type TYPE. Return zero if type TYPE is associated.
4372 Otherwise return one. */
4373
4374 int
4375 type_not_associated (const struct type *type)
4376 {
4377 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4378
4379 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
4380 }
4381
4382 /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4383
4384 static struct rank
4385 rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4386 {
4387 struct rank rank = {0,0};
4388
4389 switch (arg->code ())
4390 {
4391 case TYPE_CODE_PTR:
4392
4393 /* Allowed pointer conversions are:
4394 (a) pointer to void-pointer conversion. */
4395 if (parm->target_type ()->code () == TYPE_CODE_VOID)
4396 return VOID_PTR_CONVERSION_BADNESS;
4397
4398 /* (b) pointer to ancestor-pointer conversion. */
4399 rank.subrank = distance_to_ancestor (parm->target_type (),
4400 arg->target_type (),
4401 0);
4402 if (rank.subrank >= 0)
4403 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4404
4405 return INCOMPATIBLE_TYPE_BADNESS;
4406 case TYPE_CODE_ARRAY:
4407 {
4408 struct type *t1 = parm->target_type ();
4409 struct type *t2 = arg->target_type ();
4410
4411 if (types_equal (t1, t2))
4412 {
4413 /* Make sure they are CV equal. */
4414 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4415 rank.subrank |= CV_CONVERSION_CONST;
4416 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4417 rank.subrank |= CV_CONVERSION_VOLATILE;
4418 if (rank.subrank != 0)
4419 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4420 return EXACT_MATCH_BADNESS;
4421 }
4422 return INCOMPATIBLE_TYPE_BADNESS;
4423 }
4424 case TYPE_CODE_FUNC:
4425 return rank_one_type (parm->target_type (), arg, NULL);
4426 case TYPE_CODE_INT:
4427 if (value != NULL && value->type ()->code () == TYPE_CODE_INT)
4428 {
4429 if (value_as_long (value) == 0)
4430 {
4431 /* Null pointer conversion: allow it to be cast to a pointer.
4432 [4.10.1 of C++ standard draft n3290] */
4433 return NULL_POINTER_CONVERSION_BADNESS;
4434 }
4435 else
4436 {
4437 /* If type checking is disabled, allow the conversion. */
4438 if (!strict_type_checking)
4439 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4440 }
4441 }
4442 [[fallthrough]];
4443 case TYPE_CODE_ENUM:
4444 case TYPE_CODE_FLAGS:
4445 case TYPE_CODE_CHAR:
4446 case TYPE_CODE_RANGE:
4447 case TYPE_CODE_BOOL:
4448 default:
4449 return INCOMPATIBLE_TYPE_BADNESS;
4450 }
4451 }
4452
4453 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4454
4455 static struct rank
4456 rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4457 {
4458 switch (arg->code ())
4459 {
4460 case TYPE_CODE_PTR:
4461 case TYPE_CODE_ARRAY:
4462 return rank_one_type (parm->target_type (),
4463 arg->target_type (), NULL);
4464 default:
4465 return INCOMPATIBLE_TYPE_BADNESS;
4466 }
4467 }
4468
4469 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4470
4471 static struct rank
4472 rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4473 {
4474 switch (arg->code ())
4475 {
4476 case TYPE_CODE_PTR: /* funcptr -> func */
4477 return rank_one_type (parm, arg->target_type (), NULL);
4478 default:
4479 return INCOMPATIBLE_TYPE_BADNESS;
4480 }
4481 }
4482
4483 /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4484
4485 static struct rank
4486 rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4487 {
4488 switch (arg->code ())
4489 {
4490 case TYPE_CODE_INT:
4491 if (arg->length () == parm->length ())
4492 {
4493 /* Deal with signed, unsigned, and plain chars and
4494 signed and unsigned ints. */
4495 if (parm->has_no_signedness ())
4496 {
4497 /* This case only for character types. */
4498 if (arg->has_no_signedness ())
4499 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4500 else /* signed/unsigned char -> plain char */
4501 return INTEGER_CONVERSION_BADNESS;
4502 }
4503 else if (parm->is_unsigned ())
4504 {
4505 if (arg->is_unsigned ())
4506 {
4507 /* unsigned int -> unsigned int, or
4508 unsigned long -> unsigned long */
4509 if (integer_types_same_name_p (parm->name (),
4510 arg->name ()))
4511 return EXACT_MATCH_BADNESS;
4512 else if (integer_types_same_name_p (arg->name (),
4513 "int")
4514 && integer_types_same_name_p (parm->name (),
4515 "long"))
4516 /* unsigned int -> unsigned long */
4517 return INTEGER_PROMOTION_BADNESS;
4518 else
4519 /* unsigned long -> unsigned int */
4520 return INTEGER_CONVERSION_BADNESS;
4521 }
4522 else
4523 {
4524 if (integer_types_same_name_p (arg->name (),
4525 "long")
4526 && integer_types_same_name_p (parm->name (),
4527 "int"))
4528 /* signed long -> unsigned int */
4529 return INTEGER_CONVERSION_BADNESS;
4530 else
4531 /* signed int/long -> unsigned int/long */
4532 return INTEGER_CONVERSION_BADNESS;
4533 }
4534 }
4535 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4536 {
4537 if (integer_types_same_name_p (parm->name (),
4538 arg->name ()))
4539 return EXACT_MATCH_BADNESS;
4540 else if (integer_types_same_name_p (arg->name (),
4541 "int")
4542 && integer_types_same_name_p (parm->name (),
4543 "long"))
4544 return INTEGER_PROMOTION_BADNESS;
4545 else
4546 return INTEGER_CONVERSION_BADNESS;
4547 }
4548 else
4549 return INTEGER_CONVERSION_BADNESS;
4550 }
4551 else if (arg->length () < parm->length ())
4552 return INTEGER_PROMOTION_BADNESS;
4553 else
4554 return INTEGER_CONVERSION_BADNESS;
4555 case TYPE_CODE_ENUM:
4556 case TYPE_CODE_FLAGS:
4557 case TYPE_CODE_CHAR:
4558 case TYPE_CODE_RANGE:
4559 case TYPE_CODE_BOOL:
4560 if (arg->is_declared_class ())
4561 return INCOMPATIBLE_TYPE_BADNESS;
4562 return INTEGER_PROMOTION_BADNESS;
4563 case TYPE_CODE_FLT:
4564 return INT_FLOAT_CONVERSION_BADNESS;
4565 case TYPE_CODE_PTR:
4566 return NS_POINTER_CONVERSION_BADNESS;
4567 default:
4568 return INCOMPATIBLE_TYPE_BADNESS;
4569 }
4570 }
4571
4572 /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4573
4574 static struct rank
4575 rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4576 {
4577 switch (arg->code ())
4578 {
4579 case TYPE_CODE_INT:
4580 case TYPE_CODE_CHAR:
4581 case TYPE_CODE_RANGE:
4582 case TYPE_CODE_BOOL:
4583 case TYPE_CODE_ENUM:
4584 if (parm->is_declared_class () || arg->is_declared_class ())
4585 return INCOMPATIBLE_TYPE_BADNESS;
4586 return INTEGER_CONVERSION_BADNESS;
4587 case TYPE_CODE_FLT:
4588 return INT_FLOAT_CONVERSION_BADNESS;
4589 default:
4590 return INCOMPATIBLE_TYPE_BADNESS;
4591 }
4592 }
4593
4594 /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4595
4596 static struct rank
4597 rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4598 {
4599 switch (arg->code ())
4600 {
4601 case TYPE_CODE_RANGE:
4602 case TYPE_CODE_BOOL:
4603 case TYPE_CODE_ENUM:
4604 if (arg->is_declared_class ())
4605 return INCOMPATIBLE_TYPE_BADNESS;
4606 return INTEGER_CONVERSION_BADNESS;
4607 case TYPE_CODE_FLT:
4608 return INT_FLOAT_CONVERSION_BADNESS;
4609 case TYPE_CODE_INT:
4610 if (arg->length () > parm->length ())
4611 return INTEGER_CONVERSION_BADNESS;
4612 else if (arg->length () < parm->length ())
4613 return INTEGER_PROMOTION_BADNESS;
4614 [[fallthrough]];
4615 case TYPE_CODE_CHAR:
4616 /* Deal with signed, unsigned, and plain chars for C++ and
4617 with int cases falling through from previous case. */
4618 if (parm->has_no_signedness ())
4619 {
4620 if (arg->has_no_signedness ())
4621 return EXACT_MATCH_BADNESS;
4622 else
4623 return INTEGER_CONVERSION_BADNESS;
4624 }
4625 else if (parm->is_unsigned ())
4626 {
4627 if (arg->is_unsigned ())
4628 return EXACT_MATCH_BADNESS;
4629 else
4630 return INTEGER_PROMOTION_BADNESS;
4631 }
4632 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
4633 return EXACT_MATCH_BADNESS;
4634 else
4635 return INTEGER_CONVERSION_BADNESS;
4636 default:
4637 return INCOMPATIBLE_TYPE_BADNESS;
4638 }
4639 }
4640
4641 /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4642
4643 static struct rank
4644 rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4645 {
4646 switch (arg->code ())
4647 {
4648 case TYPE_CODE_INT:
4649 case TYPE_CODE_CHAR:
4650 case TYPE_CODE_RANGE:
4651 case TYPE_CODE_BOOL:
4652 case TYPE_CODE_ENUM:
4653 return INTEGER_CONVERSION_BADNESS;
4654 case TYPE_CODE_FLT:
4655 return INT_FLOAT_CONVERSION_BADNESS;
4656 default:
4657 return INCOMPATIBLE_TYPE_BADNESS;
4658 }
4659 }
4660
4661 /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4662
4663 static struct rank
4664 rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4665 {
4666 switch (arg->code ())
4667 {
4668 /* n3290 draft, section 4.12.1 (conv.bool):
4669
4670 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4671 pointer to member type can be converted to a prvalue of type
4672 bool. A zero value, null pointer value, or null member pointer
4673 value is converted to false; any other value is converted to
4674 true. A prvalue of type std::nullptr_t can be converted to a
4675 prvalue of type bool; the resulting value is false." */
4676 case TYPE_CODE_INT:
4677 case TYPE_CODE_CHAR:
4678 case TYPE_CODE_ENUM:
4679 case TYPE_CODE_FLT:
4680 case TYPE_CODE_MEMBERPTR:
4681 case TYPE_CODE_PTR:
4682 return BOOL_CONVERSION_BADNESS;
4683 case TYPE_CODE_RANGE:
4684 return INCOMPATIBLE_TYPE_BADNESS;
4685 case TYPE_CODE_BOOL:
4686 return EXACT_MATCH_BADNESS;
4687 default:
4688 return INCOMPATIBLE_TYPE_BADNESS;
4689 }
4690 }
4691
4692 /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4693
4694 static struct rank
4695 rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4696 {
4697 switch (arg->code ())
4698 {
4699 case TYPE_CODE_FLT:
4700 if (arg->length () < parm->length ())
4701 return FLOAT_PROMOTION_BADNESS;
4702 else if (arg->length () == parm->length ())
4703 return EXACT_MATCH_BADNESS;
4704 else
4705 return FLOAT_CONVERSION_BADNESS;
4706 case TYPE_CODE_INT:
4707 case TYPE_CODE_BOOL:
4708 case TYPE_CODE_ENUM:
4709 case TYPE_CODE_RANGE:
4710 case TYPE_CODE_CHAR:
4711 return INT_FLOAT_CONVERSION_BADNESS;
4712 default:
4713 return INCOMPATIBLE_TYPE_BADNESS;
4714 }
4715 }
4716
4717 /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4718
4719 static struct rank
4720 rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4721 {
4722 switch (arg->code ())
4723 { /* Strictly not needed for C++, but... */
4724 case TYPE_CODE_FLT:
4725 return FLOAT_PROMOTION_BADNESS;
4726 case TYPE_CODE_COMPLEX:
4727 return EXACT_MATCH_BADNESS;
4728 default:
4729 return INCOMPATIBLE_TYPE_BADNESS;
4730 }
4731 }
4732
4733 /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4734
4735 static struct rank
4736 rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4737 {
4738 struct rank rank = {0, 0};
4739
4740 switch (arg->code ())
4741 {
4742 case TYPE_CODE_STRUCT:
4743 /* Check for derivation */
4744 rank.subrank = distance_to_ancestor (parm, arg, 0);
4745 if (rank.subrank >= 0)
4746 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
4747 [[fallthrough]];
4748 default:
4749 return INCOMPATIBLE_TYPE_BADNESS;
4750 }
4751 }
4752
4753 /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4754
4755 static struct rank
4756 rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4757 {
4758 switch (arg->code ())
4759 {
4760 /* Not in C++ */
4761 case TYPE_CODE_SET:
4762 return rank_one_type (parm->field (0).type (),
4763 arg->field (0).type (), NULL);
4764 default:
4765 return INCOMPATIBLE_TYPE_BADNESS;
4766 }
4767 }
4768
4769 /* Compare one type (PARM) for compatibility with another (ARG).
4770 * PARM is intended to be the parameter type of a function; and
4771 * ARG is the supplied argument's type. This function tests if
4772 * the latter can be converted to the former.
4773 * VALUE is the argument's value or NULL if none (or called recursively)
4774 *
4775 * Return 0 if they are identical types;
4776 * Otherwise, return an integer which corresponds to how compatible
4777 * PARM is to ARG. The higher the return value, the worse the match.
4778 * Generally the "bad" conversions are all uniformly assigned
4779 * INVALID_CONVERSION. */
4780
4781 struct rank
4782 rank_one_type (struct type *parm, struct type *arg, struct value *value)
4783 {
4784 struct rank rank = {0,0};
4785
4786 /* Resolve typedefs */
4787 if (parm->code () == TYPE_CODE_TYPEDEF)
4788 parm = check_typedef (parm);
4789 if (arg->code () == TYPE_CODE_TYPEDEF)
4790 arg = check_typedef (arg);
4791
4792 if (TYPE_IS_REFERENCE (parm) && value != NULL)
4793 {
4794 if (value->lval () == not_lval)
4795 {
4796 /* Rvalues should preferably bind to rvalue references or const
4797 lvalue references. */
4798 if (parm->code () == TYPE_CODE_RVALUE_REF)
4799 rank.subrank = REFERENCE_CONVERSION_RVALUE;
4800 else if (TYPE_CONST (parm->target_type ()))
4801 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4802 else
4803 return INCOMPATIBLE_TYPE_BADNESS;
4804 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4805 }
4806 else
4807 {
4808 /* It's illegal to pass an lvalue as an rvalue. */
4809 if (parm->code () == TYPE_CODE_RVALUE_REF)
4810 return INCOMPATIBLE_TYPE_BADNESS;
4811 }
4812 }
4813
4814 if (types_equal (parm, arg))
4815 {
4816 struct type *t1 = parm;
4817 struct type *t2 = arg;
4818
4819 /* For pointers and references, compare target type. */
4820 if (parm->is_pointer_or_reference ())
4821 {
4822 t1 = parm->target_type ();
4823 t2 = arg->target_type ();
4824 }
4825
4826 /* Make sure they are CV equal, too. */
4827 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4828 rank.subrank |= CV_CONVERSION_CONST;
4829 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4830 rank.subrank |= CV_CONVERSION_VOLATILE;
4831 if (rank.subrank != 0)
4832 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4833 return EXACT_MATCH_BADNESS;
4834 }
4835
4836 /* See through references, since we can almost make non-references
4837 references. */
4838
4839 if (TYPE_IS_REFERENCE (arg))
4840 return (sum_ranks (rank_one_type (parm, arg->target_type (), NULL),
4841 REFERENCE_SEE_THROUGH_BADNESS));
4842 if (TYPE_IS_REFERENCE (parm))
4843 return (sum_ranks (rank_one_type (parm->target_type (), arg, NULL),
4844 REFERENCE_SEE_THROUGH_BADNESS));
4845 if (overload_debug)
4846 {
4847 /* Debugging only. */
4848 gdb_printf (gdb_stderr,
4849 "------ Arg is %s [%d], parm is %s [%d]\n",
4850 arg->name (), arg->code (),
4851 parm->name (), parm->code ());
4852 }
4853
4854 /* x -> y means arg of type x being supplied for parameter of type y. */
4855
4856 switch (parm->code ())
4857 {
4858 case TYPE_CODE_PTR:
4859 return rank_one_type_parm_ptr (parm, arg, value);
4860 case TYPE_CODE_ARRAY:
4861 return rank_one_type_parm_array (parm, arg, value);
4862 case TYPE_CODE_FUNC:
4863 return rank_one_type_parm_func (parm, arg, value);
4864 case TYPE_CODE_INT:
4865 return rank_one_type_parm_int (parm, arg, value);
4866 case TYPE_CODE_ENUM:
4867 return rank_one_type_parm_enum (parm, arg, value);
4868 case TYPE_CODE_CHAR:
4869 return rank_one_type_parm_char (parm, arg, value);
4870 case TYPE_CODE_RANGE:
4871 return rank_one_type_parm_range (parm, arg, value);
4872 case TYPE_CODE_BOOL:
4873 return rank_one_type_parm_bool (parm, arg, value);
4874 case TYPE_CODE_FLT:
4875 return rank_one_type_parm_float (parm, arg, value);
4876 case TYPE_CODE_COMPLEX:
4877 return rank_one_type_parm_complex (parm, arg, value);
4878 case TYPE_CODE_STRUCT:
4879 return rank_one_type_parm_struct (parm, arg, value);
4880 case TYPE_CODE_SET:
4881 return rank_one_type_parm_set (parm, arg, value);
4882 default:
4883 return INCOMPATIBLE_TYPE_BADNESS;
4884 } /* switch (arg->code ()) */
4885 }
4886
4887 /* End of functions for overload resolution. */
4888 \f
4889
4890 /* Note the first arg should be the "this" pointer, we may not want to
4891 include it since we may get into a infinitely recursive
4892 situation. */
4893
4894 static void
4895 print_args (struct field *args, int nargs, int spaces)
4896 {
4897 if (args != NULL)
4898 {
4899 int i;
4900
4901 for (i = 0; i < nargs; i++)
4902 {
4903 gdb_printf
4904 ("%*s[%d] name '%s'\n", spaces, "", i,
4905 args[i].name () != NULL ? args[i].name () : "<NULL>");
4906 recursive_dump_type (args[i].type (), spaces + 2);
4907 }
4908 }
4909 }
4910
4911 static void
4912 dump_fn_fieldlists (struct type *type, int spaces)
4913 {
4914 int method_idx;
4915 int overload_idx;
4916 struct fn_field *f;
4917
4918 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
4919 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
4920 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
4921 {
4922 f = TYPE_FN_FIELDLIST1 (type, method_idx);
4923 gdb_printf
4924 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
4925 method_idx,
4926 TYPE_FN_FIELDLIST_NAME (type, method_idx),
4927 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
4928 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
4929 for (overload_idx = 0;
4930 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
4931 overload_idx++)
4932 {
4933 gdb_printf
4934 ("%*s[%d] physname '%s' (%s)\n",
4935 spaces + 4, "", overload_idx,
4936 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
4937 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
4938 overload_idx)));
4939 gdb_printf
4940 ("%*stype %s\n", spaces + 8, "",
4941 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
4942
4943 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
4944 spaces + 8 + 2);
4945
4946 gdb_printf
4947 ("%*sargs %s\n", spaces + 8, "",
4948 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
4949 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
4950 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
4951 spaces + 8 + 2);
4952 gdb_printf
4953 ("%*sfcontext %s\n", spaces + 8, "",
4954 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
4955 overload_idx)));
4956
4957 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
4958 TYPE_FN_FIELD_CONST (f, overload_idx));
4959 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
4960 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
4961 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
4962 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
4963 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
4964 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
4965 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
4966 TYPE_FN_FIELD_STUB (f, overload_idx));
4967 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
4968 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
4969 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
4970 TYPE_FN_FIELD_DELETED (f, overload_idx));
4971 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
4972 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
4973 }
4974 }
4975 }
4976
4977 static void
4978 print_cplus_stuff (struct type *type, int spaces)
4979 {
4980 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
4981 TYPE_VPTR_FIELDNO (type));
4982 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
4983 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
4984 if (TYPE_VPTR_BASETYPE (type) != NULL)
4985 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
4986
4987 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
4988 TYPE_N_BASECLASSES (type));
4989 gdb_printf ("%*snfn_fields %d\n", spaces, "",
4990 TYPE_NFN_FIELDS (type));
4991 if (TYPE_NFN_FIELDS (type) > 0)
4992 {
4993 dump_fn_fieldlists (type, spaces);
4994 }
4995
4996 gdb_printf ("%*scalling_convention %d\n", spaces, "",
4997 TYPE_CPLUS_CALLING_CONVENTION (type));
4998 }
4999
5000 /* Print the contents of the TYPE's type_specific union, assuming that
5001 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5002
5003 static void
5004 print_gnat_stuff (struct type *type, int spaces)
5005 {
5006 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5007
5008 if (descriptive_type == NULL)
5009 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
5010 else
5011 {
5012 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
5013 recursive_dump_type (descriptive_type, spaces + 4);
5014 }
5015 }
5016
5017 /* Print the contents of the TYPE's type_specific union, assuming that
5018 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5019
5020 static void
5021 print_fixed_point_type_info (struct type *type, int spaces)
5022 {
5023 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5024 type->fixed_point_scaling_factor ().str ().c_str ());
5025 }
5026
5027 static struct obstack dont_print_type_obstack;
5028
5029 /* Print the dynamic_prop PROP. */
5030
5031 static void
5032 dump_dynamic_prop (dynamic_prop const& prop)
5033 {
5034 switch (prop.kind ())
5035 {
5036 case PROP_CONST:
5037 gdb_printf ("%s", plongest (prop.const_val ()));
5038 break;
5039 case PROP_UNDEFINED:
5040 gdb_printf ("(undefined)");
5041 break;
5042 case PROP_LOCEXPR:
5043 case PROP_LOCLIST:
5044 gdb_printf ("(dynamic)");
5045 break;
5046 default:
5047 gdb_assert_not_reached ("unhandled prop kind");
5048 break;
5049 }
5050 }
5051
5052 /* Return a string that represents a type code. */
5053 static const char *
5054 type_code_name (type_code code)
5055 {
5056 switch (code)
5057 {
5058 #define OP(X) case X: return # X;
5059 #include "type-codes.def"
5060 #undef OP
5061
5062 case TYPE_CODE_UNDEF:
5063 return "TYPE_CODE_UNDEF";
5064 }
5065
5066 gdb_assert_not_reached ("unhandled type_code");
5067 }
5068
5069 void
5070 recursive_dump_type (struct type *type, int spaces)
5071 {
5072 int idx;
5073
5074 if (spaces == 0)
5075 obstack_begin (&dont_print_type_obstack, 0);
5076
5077 if (type->num_fields () > 0
5078 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
5079 {
5080 struct type **first_dont_print
5081 = (struct type **) obstack_base (&dont_print_type_obstack);
5082
5083 int i = (struct type **)
5084 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
5085
5086 while (--i >= 0)
5087 {
5088 if (type == first_dont_print[i])
5089 {
5090 gdb_printf ("%*stype node %s", spaces, "",
5091 host_address_to_string (type));
5092 gdb_printf (_(" <same as already seen type>\n"));
5093 return;
5094 }
5095 }
5096
5097 obstack_ptr_grow (&dont_print_type_obstack, type);
5098 }
5099
5100 gdb_printf ("%*stype node %s\n", spaces, "",
5101 host_address_to_string (type));
5102 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5103 type->name () ? type->name () : "<NULL>",
5104 host_address_to_string (type->name ()));
5105 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
5106 gdb_printf ("(%s)", type_code_name (type->code ()));
5107 gdb_puts ("\n");
5108 gdb_printf ("%*slength %s\n", spaces, "",
5109 pulongest (type->length ()));
5110 if (type->is_objfile_owned ())
5111 gdb_printf ("%*sobjfile %s\n", spaces, "",
5112 host_address_to_string (type->objfile_owner ()));
5113 else
5114 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5115 host_address_to_string (type->arch_owner ()));
5116 gdb_printf ("%*starget_type %s\n", spaces, "",
5117 host_address_to_string (type->target_type ()));
5118 if (type->target_type () != NULL)
5119 {
5120 recursive_dump_type (type->target_type (), spaces + 2);
5121 }
5122 gdb_printf ("%*spointer_type %s\n", spaces, "",
5123 host_address_to_string (TYPE_POINTER_TYPE (type)));
5124 gdb_printf ("%*sreference_type %s\n", spaces, "",
5125 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5126 gdb_printf ("%*stype_chain %s\n", spaces, "",
5127 host_address_to_string (TYPE_CHAIN (type)));
5128 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5129 (unsigned) type->instance_flags ());
5130 if (TYPE_CONST (type))
5131 {
5132 gdb_puts (" TYPE_CONST");
5133 }
5134 if (TYPE_VOLATILE (type))
5135 {
5136 gdb_puts (" TYPE_VOLATILE");
5137 }
5138 if (TYPE_CODE_SPACE (type))
5139 {
5140 gdb_puts (" TYPE_CODE_SPACE");
5141 }
5142 if (TYPE_DATA_SPACE (type))
5143 {
5144 gdb_puts (" TYPE_DATA_SPACE");
5145 }
5146 if (TYPE_ADDRESS_CLASS_1 (type))
5147 {
5148 gdb_puts (" TYPE_ADDRESS_CLASS_1");
5149 }
5150 if (TYPE_ADDRESS_CLASS_2 (type))
5151 {
5152 gdb_puts (" TYPE_ADDRESS_CLASS_2");
5153 }
5154 if (TYPE_RESTRICT (type))
5155 {
5156 gdb_puts (" TYPE_RESTRICT");
5157 }
5158 if (TYPE_ATOMIC (type))
5159 {
5160 gdb_puts (" TYPE_ATOMIC");
5161 }
5162 gdb_puts ("\n");
5163
5164 gdb_printf ("%*sflags", spaces, "");
5165 if (type->is_unsigned ())
5166 {
5167 gdb_puts (" TYPE_UNSIGNED");
5168 }
5169 if (type->has_no_signedness ())
5170 {
5171 gdb_puts (" TYPE_NOSIGN");
5172 }
5173 if (type->endianity_is_not_default ())
5174 {
5175 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
5176 }
5177 if (type->is_stub ())
5178 {
5179 gdb_puts (" TYPE_STUB");
5180 }
5181 if (type->target_is_stub ())
5182 {
5183 gdb_puts (" TYPE_TARGET_STUB");
5184 }
5185 if (type->is_prototyped ())
5186 {
5187 gdb_puts (" TYPE_PROTOTYPED");
5188 }
5189 if (type->has_varargs ())
5190 {
5191 gdb_puts (" TYPE_VARARGS");
5192 }
5193 /* This is used for things like AltiVec registers on ppc. Gcc emits
5194 an attribute for the array type, which tells whether or not we
5195 have a vector, instead of a regular array. */
5196 if (type->is_vector ())
5197 {
5198 gdb_puts (" TYPE_VECTOR");
5199 }
5200 if (type->is_fixed_instance ())
5201 {
5202 gdb_puts (" TYPE_FIXED_INSTANCE");
5203 }
5204 if (type->stub_is_supported ())
5205 {
5206 gdb_puts (" TYPE_STUB_SUPPORTED");
5207 }
5208 if (TYPE_NOTTEXT (type))
5209 {
5210 gdb_puts (" TYPE_NOTTEXT");
5211 }
5212 gdb_puts ("\n");
5213 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5214 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5215 || TYPE_ALLOCATED_PROP (type) != nullptr)
5216 {
5217 gdb_printf ("%*s", spaces, "");
5218 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5219 {
5220 gdb_printf ("associated ");
5221 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5222 }
5223 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5224 {
5225 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5226 gdb_printf (" ");
5227 gdb_printf ("allocated ");
5228 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5229 }
5230 gdb_printf ("\n");
5231 }
5232 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
5233 for (idx = 0; idx < type->num_fields (); idx++)
5234 {
5235 field &fld = type->field (idx);
5236 if (type->code () == TYPE_CODE_ENUM)
5237 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
5238 idx, plongest (fld.loc_enumval ()));
5239 else
5240 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
5241 idx, plongest (fld.loc_bitpos ()),
5242 fld.bitsize ());
5243 gdb_printf ("%s name '%s' (%s)",
5244 host_address_to_string (fld.type ()),
5245 fld.name () != NULL
5246 ? fld.name ()
5247 : "<NULL>",
5248 host_address_to_string (fld.name ()));
5249 if (fld.is_virtual ())
5250 gdb_printf (" virtual");
5251
5252 if (fld.is_private ())
5253 gdb_printf (" private");
5254 else if (fld.is_protected ())
5255 gdb_printf (" protected");
5256 else if (fld.is_ignored ())
5257 gdb_printf (" ignored");
5258
5259 gdb_printf ("\n");
5260 if (fld.type () != NULL)
5261 {
5262 recursive_dump_type (fld.type (), spaces + 4);
5263 }
5264 }
5265 if (type->code () == TYPE_CODE_RANGE)
5266 {
5267 gdb_printf ("%*slow ", spaces, "");
5268 dump_dynamic_prop (type->bounds ()->low);
5269 gdb_printf (" high ");
5270 dump_dynamic_prop (type->bounds ()->high);
5271 gdb_printf ("\n");
5272 }
5273
5274 switch (TYPE_SPECIFIC_FIELD (type))
5275 {
5276 case TYPE_SPECIFIC_CPLUS_STUFF:
5277 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5278 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5279 print_cplus_stuff (type, spaces);
5280 break;
5281
5282 case TYPE_SPECIFIC_GNAT_STUFF:
5283 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5284 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5285 print_gnat_stuff (type, spaces);
5286 break;
5287
5288 case TYPE_SPECIFIC_FLOATFORMAT:
5289 gdb_printf ("%*sfloatformat ", spaces, "");
5290 if (TYPE_FLOATFORMAT (type) == NULL
5291 || TYPE_FLOATFORMAT (type)->name == NULL)
5292 gdb_puts ("(null)");
5293 else
5294 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5295 gdb_puts ("\n");
5296 break;
5297
5298 case TYPE_SPECIFIC_FUNC:
5299 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5300 TYPE_CALLING_CONVENTION (type));
5301 /* tail_call_list is not printed. */
5302 break;
5303
5304 case TYPE_SPECIFIC_SELF_TYPE:
5305 gdb_printf ("%*sself_type %s\n", spaces, "",
5306 host_address_to_string (TYPE_SELF_TYPE (type)));
5307 break;
5308
5309 case TYPE_SPECIFIC_FIXED_POINT:
5310 gdb_printf ("%*sfixed_point_info ", spaces, "");
5311 print_fixed_point_type_info (type, spaces);
5312 gdb_puts ("\n");
5313 break;
5314
5315 case TYPE_SPECIFIC_INT:
5316 if (type->bit_size_differs_p ())
5317 {
5318 unsigned bit_size = type->bit_size ();
5319 unsigned bit_off = type->bit_offset ();
5320 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5321 bit_size, bit_off);
5322 }
5323 break;
5324 }
5325
5326 if (spaces == 0)
5327 obstack_free (&dont_print_type_obstack, NULL);
5328 }
5329 \f
5330 /* Trivial helpers for the libiberty hash table, for mapping one
5331 type to another. */
5332
5333 struct type_pair
5334 {
5335 type_pair (struct type *old_, struct type *newobj_)
5336 : old (old_), newobj (newobj_)
5337 {}
5338
5339 struct type * const old, * const newobj;
5340 };
5341
5342 static hashval_t
5343 type_pair_hash (const void *item)
5344 {
5345 const struct type_pair *pair = (const struct type_pair *) item;
5346
5347 return htab_hash_pointer (pair->old);
5348 }
5349
5350 static int
5351 type_pair_eq (const void *item_lhs, const void *item_rhs)
5352 {
5353 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5354 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
5355
5356 return lhs->old == rhs->old;
5357 }
5358
5359 /* Allocate the hash table used by copy_type_recursive to walk
5360 types without duplicates. */
5361
5362 htab_up
5363 create_copied_types_hash ()
5364 {
5365 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5366 htab_delete_entry<type_pair>,
5367 xcalloc, xfree));
5368 }
5369
5370 /* Recursively copy (deep copy) a dynamic attribute list of a type. */
5371
5372 static struct dynamic_prop_list *
5373 copy_dynamic_prop_list (struct obstack *storage,
5374 struct dynamic_prop_list *list)
5375 {
5376 struct dynamic_prop_list *copy = list;
5377 struct dynamic_prop_list **node_ptr = &copy;
5378
5379 while (*node_ptr != NULL)
5380 {
5381 struct dynamic_prop_list *node_copy;
5382
5383 node_copy = ((struct dynamic_prop_list *)
5384 obstack_copy (storage, *node_ptr,
5385 sizeof (struct dynamic_prop_list)));
5386 node_copy->prop = (*node_ptr)->prop;
5387 *node_ptr = node_copy;
5388
5389 node_ptr = &node_copy->next;
5390 }
5391
5392 return copy;
5393 }
5394
5395 /* Recursively copy (deep copy) TYPE, if it is associated with
5396 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5397 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5398 it is not associated with OBJFILE. */
5399
5400 struct type *
5401 copy_type_recursive (struct type *type, htab_t copied_types)
5402 {
5403 void **slot;
5404 struct type *new_type;
5405
5406 if (!type->is_objfile_owned ())
5407 return type;
5408
5409 struct type_pair pair (type, nullptr);
5410
5411 slot = htab_find_slot (copied_types, &pair, INSERT);
5412 if (*slot != NULL)
5413 return ((struct type_pair *) *slot)->newobj;
5414
5415 new_type = type_allocator (type->arch ()).new_type ();
5416
5417 /* We must add the new type to the hash table immediately, in case
5418 we encounter this type again during a recursive call below. */
5419 struct type_pair *stored = new type_pair (type, new_type);
5420
5421 *slot = stored;
5422
5423 /* Copy the common fields of types. For the main type, we simply
5424 copy the entire thing and then update specific fields as needed. */
5425 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5426
5427 new_type->set_owner (type->arch ());
5428
5429 if (type->name ())
5430 new_type->set_name (xstrdup (type->name ()));
5431
5432 new_type->set_instance_flags (type->instance_flags ());
5433 new_type->set_length (type->length ());
5434
5435 /* Copy the fields. */
5436 if (type->num_fields ())
5437 {
5438 int i, nfields;
5439
5440 nfields = type->num_fields ();
5441 new_type->alloc_fields (type->num_fields ());
5442
5443 for (i = 0; i < nfields; i++)
5444 {
5445 new_type->field (i).set_is_artificial
5446 (type->field (i).is_artificial ());
5447 new_type->field (i).set_bitsize (type->field (i).bitsize ());
5448 if (type->field (i).type ())
5449 new_type->field (i).set_type
5450 (copy_type_recursive (type->field (i).type (), copied_types));
5451 if (type->field (i).name ())
5452 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
5453
5454 switch (type->field (i).loc_kind ())
5455 {
5456 case FIELD_LOC_KIND_BITPOS:
5457 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
5458 break;
5459 case FIELD_LOC_KIND_ENUMVAL:
5460 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
5461 break;
5462 case FIELD_LOC_KIND_PHYSADDR:
5463 new_type->field (i).set_loc_physaddr
5464 (type->field (i).loc_physaddr ());
5465 break;
5466 case FIELD_LOC_KIND_PHYSNAME:
5467 new_type->field (i).set_loc_physname
5468 (xstrdup (type->field (i).loc_physname ()));
5469 break;
5470 case FIELD_LOC_KIND_DWARF_BLOCK:
5471 new_type->field (i).set_loc_dwarf_block
5472 (type->field (i).loc_dwarf_block ());
5473 break;
5474 default:
5475 internal_error (_("Unexpected type field location kind: %d"),
5476 type->field (i).loc_kind ());
5477 }
5478 }
5479 }
5480
5481 /* For range types, copy the bounds information. */
5482 if (type->code () == TYPE_CODE_RANGE)
5483 {
5484 range_bounds *bounds
5485 = ((struct range_bounds *) TYPE_ALLOC
5486 (new_type, sizeof (struct range_bounds)));
5487
5488 *bounds = *type->bounds ();
5489 new_type->set_bounds (bounds);
5490 }
5491
5492 if (type->main_type->dyn_prop_list != NULL)
5493 new_type->main_type->dyn_prop_list
5494 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
5495 type->main_type->dyn_prop_list);
5496
5497
5498 /* Copy pointers to other types. */
5499 if (type->target_type ())
5500 new_type->set_target_type
5501 (copy_type_recursive (type->target_type (), copied_types));
5502
5503 /* Maybe copy the type_specific bits.
5504
5505 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5506 base classes and methods. There's no fundamental reason why we
5507 can't, but at the moment it is not needed. */
5508
5509 switch (TYPE_SPECIFIC_FIELD (type))
5510 {
5511 case TYPE_SPECIFIC_NONE:
5512 break;
5513 case TYPE_SPECIFIC_FUNC:
5514 INIT_FUNC_SPECIFIC (new_type);
5515 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5516 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5517 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5518 break;
5519 case TYPE_SPECIFIC_FLOATFORMAT:
5520 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5521 break;
5522 case TYPE_SPECIFIC_CPLUS_STUFF:
5523 INIT_CPLUS_SPECIFIC (new_type);
5524 break;
5525 case TYPE_SPECIFIC_GNAT_STUFF:
5526 INIT_GNAT_SPECIFIC (new_type);
5527 break;
5528 case TYPE_SPECIFIC_SELF_TYPE:
5529 set_type_self_type (new_type,
5530 copy_type_recursive (TYPE_SELF_TYPE (type),
5531 copied_types));
5532 break;
5533 case TYPE_SPECIFIC_FIXED_POINT:
5534 INIT_FIXED_POINT_SPECIFIC (new_type);
5535 new_type->fixed_point_info ().scaling_factor
5536 = type->fixed_point_info ().scaling_factor;
5537 break;
5538 case TYPE_SPECIFIC_INT:
5539 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5540 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5541 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5542 break;
5543
5544 default:
5545 gdb_assert_not_reached ("bad type_specific_kind");
5546 }
5547
5548 return new_type;
5549 }
5550
5551 /* Make a copy of the given TYPE, except that the pointer & reference
5552 types are not preserved. */
5553
5554 struct type *
5555 copy_type (const struct type *type)
5556 {
5557 struct type *new_type = type_allocator (type).new_type ();
5558 new_type->set_instance_flags (type->instance_flags ());
5559 new_type->set_length (type->length ());
5560 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5561 sizeof (struct main_type));
5562 if (type->main_type->dyn_prop_list != NULL)
5563 {
5564 struct obstack *storage = (type->is_objfile_owned ()
5565 ? &type->objfile_owner ()->objfile_obstack
5566 : gdbarch_obstack (type->arch_owner ()));
5567 new_type->main_type->dyn_prop_list
5568 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5569 }
5570
5571 return new_type;
5572 }
5573 \f
5574 /* Helper functions to initialize architecture-specific types. */
5575
5576 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
5577 NAME is the type name. BIT is the size of the flag word in bits. */
5578
5579 struct type *
5580 arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
5581 {
5582 struct type *type;
5583
5584 type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
5585 type->set_is_unsigned (true);
5586 /* Pre-allocate enough space assuming every field is one bit. */
5587 type->alloc_fields (bit);
5588 type->set_num_fields (0);
5589
5590 return type;
5591 }
5592
5593 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5594 position BITPOS is called NAME. Pass NAME as "" for fields that
5595 should not be printed. */
5596
5597 void
5598 append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
5599 struct type *field_type, const char *name)
5600 {
5601 int type_bitsize = type->length () * TARGET_CHAR_BIT;
5602 int field_nr = type->num_fields ();
5603
5604 gdb_assert (type->code () == TYPE_CODE_FLAGS);
5605 gdb_assert (type->num_fields () + 1 <= type_bitsize);
5606 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
5607 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
5608 gdb_assert (name != NULL);
5609
5610 type->set_num_fields (type->num_fields () + 1);
5611 type->field (field_nr).set_name (xstrdup (name));
5612 type->field (field_nr).set_type (field_type);
5613 type->field (field_nr).set_loc_bitpos (start_bitpos);
5614 type->field (field_nr).set_bitsize (nr_bits);
5615 }
5616
5617 /* Special version of append_flags_type_field to add a flag field.
5618 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
5619 position BITPOS is called NAME. */
5620
5621 void
5622 append_flags_type_flag (struct type *type, int bitpos, const char *name)
5623 {
5624 append_flags_type_field (type, bitpos, 1,
5625 builtin_type (type->arch ())->builtin_bool,
5626 name);
5627 }
5628
5629 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5630 specified by CODE) associated with GDBARCH. NAME is the type name. */
5631
5632 struct type *
5633 arch_composite_type (struct gdbarch *gdbarch, const char *name,
5634 enum type_code code)
5635 {
5636 struct type *t;
5637
5638 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
5639 t = type_allocator (gdbarch).new_type (code, 0, NULL);
5640 t->set_name (name);
5641 INIT_CPLUS_SPECIFIC (t);
5642 return t;
5643 }
5644
5645 /* Add new field with name NAME and type FIELD to composite type T.
5646 Do not set the field's position or adjust the type's length;
5647 the caller should do so. Return the new field. */
5648
5649 struct field *
5650 append_composite_type_field_raw (struct type *t, const char *name,
5651 struct type *field)
5652 {
5653 struct field *f;
5654
5655 t->set_num_fields (t->num_fields () + 1);
5656 t->set_fields (XRESIZEVEC (struct field, t->fields (),
5657 t->num_fields ()));
5658 f = &t->field (t->num_fields () - 1);
5659 memset (f, 0, sizeof f[0]);
5660 f[0].set_type (field);
5661 f[0].set_name (name);
5662 return f;
5663 }
5664
5665 /* Add new field with name NAME and type FIELD to composite type T.
5666 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5667
5668 void
5669 append_composite_type_field_aligned (struct type *t, const char *name,
5670 struct type *field, int alignment)
5671 {
5672 struct field *f = append_composite_type_field_raw (t, name, field);
5673
5674 if (t->code () == TYPE_CODE_UNION)
5675 {
5676 if (t->length () < field->length ())
5677 t->set_length (field->length ());
5678 }
5679 else if (t->code () == TYPE_CODE_STRUCT)
5680 {
5681 t->set_length (t->length () + field->length ());
5682 if (t->num_fields () > 1)
5683 {
5684 f->set_loc_bitpos
5685 (f[-1].loc_bitpos ()
5686 + (f[-1].type ()->length () * TARGET_CHAR_BIT));
5687
5688 if (alignment)
5689 {
5690 int left;
5691
5692 alignment *= TARGET_CHAR_BIT;
5693 left = f[0].loc_bitpos () % alignment;
5694
5695 if (left)
5696 {
5697 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
5698 t->set_length
5699 (t->length () + (alignment - left) / TARGET_CHAR_BIT);
5700 }
5701 }
5702 }
5703 }
5704 }
5705
5706 /* Add new field with name NAME and type FIELD to composite type T. */
5707
5708 void
5709 append_composite_type_field (struct type *t, const char *name,
5710 struct type *field)
5711 {
5712 append_composite_type_field_aligned (t, name, field, 0);
5713 }
5714
5715 \f
5716
5717 /* We manage the lifetimes of fixed_point_type_info objects by
5718 attaching them to the objfile. Currently, these objects are
5719 modified during construction, and GMP does not provide a way to
5720 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5721 them. If we did do this, they could be moved to the per-BFD and
5722 shared across objfiles. */
5723 typedef std::vector<std::unique_ptr<fixed_point_type_info>>
5724 fixed_point_type_storage;
5725
5726 /* Key used for managing the storage of fixed-point type info. */
5727 static const struct registry<objfile>::key<fixed_point_type_storage>
5728 fixed_point_objfile_key;
5729
5730 /* See gdbtypes.h. */
5731
5732 void
5733 allocate_fixed_point_type_info (struct type *type)
5734 {
5735 auto up = std::make_unique<fixed_point_type_info> ();
5736 fixed_point_type_info *info;
5737
5738 if (type->is_objfile_owned ())
5739 {
5740 fixed_point_type_storage *storage
5741 = fixed_point_objfile_key.get (type->objfile_owner ());
5742 if (storage == nullptr)
5743 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
5744 info = up.get ();
5745 storage->push_back (std::move (up));
5746 }
5747 else
5748 {
5749 /* We just leak the memory, because that's what we do generally
5750 for non-objfile-attached types. */
5751 info = up.release ();
5752 }
5753
5754 type->set_fixed_point_info (info);
5755 }
5756
5757 /* See gdbtypes.h. */
5758
5759 bool
5760 is_fixed_point_type (struct type *type)
5761 {
5762 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5763 type = check_typedef (type)->target_type ();
5764 type = check_typedef (type);
5765
5766 return type->code () == TYPE_CODE_FIXED_POINT;
5767 }
5768
5769 /* See gdbtypes.h. */
5770
5771 struct type *
5772 type::fixed_point_type_base_type ()
5773 {
5774 struct type *type = this;
5775
5776 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
5777 type = check_typedef (type)->target_type ();
5778 type = check_typedef (type);
5779
5780 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
5781 return type;
5782 }
5783
5784 /* See gdbtypes.h. */
5785
5786 const gdb_mpq &
5787 type::fixed_point_scaling_factor ()
5788 {
5789 struct type *type = this->fixed_point_type_base_type ();
5790
5791 return type->fixed_point_info ().scaling_factor;
5792 }
5793
5794 /* See gdbtypes.h. */
5795
5796 void
5797 type::alloc_fields (unsigned int nfields, bool init)
5798 {
5799 this->set_num_fields (nfields);
5800
5801 if (nfields == 0)
5802 {
5803 this->main_type->flds_bnds.fields = nullptr;
5804 return;
5805 }
5806
5807 size_t size = nfields * sizeof (*this->fields ());
5808 struct field *fields
5809 = (struct field *) (init
5810 ? TYPE_ZALLOC (this, size)
5811 : TYPE_ALLOC (this, size));
5812
5813 this->main_type->flds_bnds.fields = fields;
5814 }
5815
5816 /* See gdbtypes.h. */
5817
5818 void
5819 type::copy_fields (struct type *src)
5820 {
5821 unsigned int nfields = src->num_fields ();
5822 alloc_fields (nfields, false);
5823 if (nfields == 0)
5824 return;
5825
5826 size_t size = nfields * sizeof (*this->fields ());
5827 memcpy (this->fields (), src->fields (), size);
5828 }
5829
5830 /* See gdbtypes.h. */
5831
5832 void
5833 type::copy_fields (std::vector<struct field> &src)
5834 {
5835 unsigned int nfields = src.size ();
5836 alloc_fields (nfields, false);
5837 if (nfields == 0)
5838 return;
5839
5840 size_t size = nfields * sizeof (*this->fields ());
5841 memcpy (this->fields (), src.data (), size);
5842 }
5843
5844 /* See gdbtypes.h. */
5845
5846 bool
5847 type::is_string_like ()
5848 {
5849 const language_defn *defn = language_def (this->language ());
5850 return defn->is_string_type_p (this);
5851 }
5852
5853 /* See gdbtypes.h. */
5854
5855 bool
5856 type::is_array_like ()
5857 {
5858 if (code () == TYPE_CODE_ARRAY)
5859 return true;
5860 const language_defn *defn = language_def (this->language ());
5861 return defn->is_array_like (this);
5862 }
5863
5864 \f
5865
5866 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
5867
5868 static struct builtin_type *
5869 create_gdbtypes_data (struct gdbarch *gdbarch)
5870 {
5871 struct builtin_type *builtin_type = new struct builtin_type;
5872
5873 type_allocator alloc (gdbarch);
5874
5875 /* Basic types. */
5876 builtin_type->builtin_void
5877 = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
5878 builtin_type->builtin_char
5879 = init_integer_type (alloc, TARGET_CHAR_BIT,
5880 !gdbarch_char_signed (gdbarch), "char");
5881 builtin_type->builtin_char->set_has_no_signedness (true);
5882 builtin_type->builtin_signed_char
5883 = init_integer_type (alloc, TARGET_CHAR_BIT,
5884 0, "signed char");
5885 builtin_type->builtin_unsigned_char
5886 = init_integer_type (alloc, TARGET_CHAR_BIT,
5887 1, "unsigned char");
5888 builtin_type->builtin_short
5889 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5890 0, "short");
5891 builtin_type->builtin_unsigned_short
5892 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
5893 1, "unsigned short");
5894 builtin_type->builtin_int
5895 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5896 0, "int");
5897 builtin_type->builtin_unsigned_int
5898 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
5899 1, "unsigned int");
5900 builtin_type->builtin_long
5901 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5902 0, "long");
5903 builtin_type->builtin_unsigned_long
5904 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
5905 1, "unsigned long");
5906 builtin_type->builtin_long_long
5907 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5908 0, "long long");
5909 builtin_type->builtin_unsigned_long_long
5910 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
5911 1, "unsigned long long");
5912 builtin_type->builtin_half
5913 = init_float_type (alloc, gdbarch_half_bit (gdbarch),
5914 "half", gdbarch_half_format (gdbarch));
5915 builtin_type->builtin_float
5916 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
5917 "float", gdbarch_float_format (gdbarch));
5918 builtin_type->builtin_bfloat16
5919 = init_float_type (alloc, gdbarch_bfloat16_bit (gdbarch),
5920 "bfloat16", gdbarch_bfloat16_format (gdbarch));
5921 builtin_type->builtin_double
5922 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
5923 "double", gdbarch_double_format (gdbarch));
5924 builtin_type->builtin_long_double
5925 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
5926 "long double", gdbarch_long_double_format (gdbarch));
5927 builtin_type->builtin_complex
5928 = init_complex_type ("complex", builtin_type->builtin_float);
5929 builtin_type->builtin_double_complex
5930 = init_complex_type ("double complex", builtin_type->builtin_double);
5931 builtin_type->builtin_string
5932 = alloc.new_type (TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
5933 builtin_type->builtin_bool
5934 = init_boolean_type (alloc, TARGET_CHAR_BIT, 1, "bool");
5935
5936 /* The following three are about decimal floating point types, which
5937 are 32-bits, 64-bits and 128-bits respectively. */
5938 builtin_type->builtin_decfloat
5939 = init_decfloat_type (alloc, 32, "_Decimal32");
5940 builtin_type->builtin_decdouble
5941 = init_decfloat_type (alloc, 64, "_Decimal64");
5942 builtin_type->builtin_declong
5943 = init_decfloat_type (alloc, 128, "_Decimal128");
5944
5945 /* "True" character types. */
5946 builtin_type->builtin_true_char
5947 = init_character_type (alloc, TARGET_CHAR_BIT, 0, "true character");
5948 builtin_type->builtin_true_unsigned_char
5949 = init_character_type (alloc, TARGET_CHAR_BIT, 1, "true character");
5950
5951 /* Fixed-size integer types. */
5952 builtin_type->builtin_int0
5953 = init_integer_type (alloc, 0, 0, "int0_t");
5954 builtin_type->builtin_int8
5955 = init_integer_type (alloc, 8, 0, "int8_t");
5956 builtin_type->builtin_uint8
5957 = init_integer_type (alloc, 8, 1, "uint8_t");
5958 builtin_type->builtin_int16
5959 = init_integer_type (alloc, 16, 0, "int16_t");
5960 builtin_type->builtin_uint16
5961 = init_integer_type (alloc, 16, 1, "uint16_t");
5962 builtin_type->builtin_int24
5963 = init_integer_type (alloc, 24, 0, "int24_t");
5964 builtin_type->builtin_uint24
5965 = init_integer_type (alloc, 24, 1, "uint24_t");
5966 builtin_type->builtin_int32
5967 = init_integer_type (alloc, 32, 0, "int32_t");
5968 builtin_type->builtin_uint32
5969 = init_integer_type (alloc, 32, 1, "uint32_t");
5970 builtin_type->builtin_int64
5971 = init_integer_type (alloc, 64, 0, "int64_t");
5972 builtin_type->builtin_uint64
5973 = init_integer_type (alloc, 64, 1, "uint64_t");
5974 builtin_type->builtin_int128
5975 = init_integer_type (alloc, 128, 0, "int128_t");
5976 builtin_type->builtin_uint128
5977 = init_integer_type (alloc, 128, 1, "uint128_t");
5978
5979 builtin_type->builtin_int8->set_instance_flags
5980 (builtin_type->builtin_int8->instance_flags ()
5981 | TYPE_INSTANCE_FLAG_NOTTEXT);
5982
5983 builtin_type->builtin_uint8->set_instance_flags
5984 (builtin_type->builtin_uint8->instance_flags ()
5985 | TYPE_INSTANCE_FLAG_NOTTEXT);
5986
5987 /* Wide character types. */
5988 builtin_type->builtin_char16
5989 = init_integer_type (alloc, 16, 1, "char16_t");
5990 builtin_type->builtin_char32
5991 = init_integer_type (alloc, 32, 1, "char32_t");
5992 builtin_type->builtin_wchar
5993 = init_integer_type (alloc, gdbarch_wchar_bit (gdbarch),
5994 !gdbarch_wchar_signed (gdbarch), "wchar_t");
5995
5996 /* Default data/code pointer types. */
5997 builtin_type->builtin_data_ptr
5998 = lookup_pointer_type (builtin_type->builtin_void);
5999 builtin_type->builtin_func_ptr
6000 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
6001 builtin_type->builtin_func_func
6002 = lookup_function_type (builtin_type->builtin_func_ptr);
6003
6004 /* This type represents a GDB internal function. */
6005 builtin_type->internal_fn
6006 = alloc.new_type (TYPE_CODE_INTERNAL_FUNCTION, 0,
6007 "<internal function>");
6008
6009 /* This type represents an xmethod. */
6010 builtin_type->xmethod
6011 = alloc.new_type (TYPE_CODE_XMETHOD, 0, "<xmethod>");
6012
6013 /* This type represents a type that was unrecognized in symbol read-in. */
6014 builtin_type->builtin_error
6015 = alloc.new_type (TYPE_CODE_ERROR, 0, "<unknown type>");
6016
6017 /* The following set of types is used for symbols with no
6018 debug information. */
6019 builtin_type->nodebug_text_symbol
6020 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6021 "<text variable, no debug info>");
6022
6023 builtin_type->nodebug_text_gnu_ifunc_symbol
6024 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6025 "<text gnu-indirect-function variable, no debug info>");
6026 builtin_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6027
6028 builtin_type->nodebug_got_plt_symbol
6029 = init_pointer_type (alloc, gdbarch_addr_bit (gdbarch),
6030 "<text from jump slot in .got.plt, no debug info>",
6031 builtin_type->nodebug_text_symbol);
6032 builtin_type->nodebug_data_symbol
6033 = alloc.new_type (TYPE_CODE_ERROR, 0, "<data variable, no debug info>");
6034 builtin_type->nodebug_unknown_symbol
6035 = alloc.new_type (TYPE_CODE_ERROR, 0,
6036 "<variable (not text or data), no debug info>");
6037 builtin_type->nodebug_tls_symbol
6038 = alloc.new_type (TYPE_CODE_ERROR, 0,
6039 "<thread local variable, no debug info>");
6040
6041 /* NOTE: on some targets, addresses and pointers are not necessarily
6042 the same.
6043
6044 The upshot is:
6045 - gdb's `struct type' always describes the target's
6046 representation.
6047 - gdb's `struct value' objects should always hold values in
6048 target form.
6049 - gdb's CORE_ADDR values are addresses in the unified virtual
6050 address space that the assembler and linker work with. Thus,
6051 since target_read_memory takes a CORE_ADDR as an argument, it
6052 can access any memory on the target, even if the processor has
6053 separate code and data address spaces.
6054
6055 In this context, builtin_type->builtin_core_addr is a bit odd:
6056 it's a target type for a value the target will never see. It's
6057 only used to hold the values of (typeless) linker symbols, which
6058 are indeed in the unified virtual address space. */
6059
6060 builtin_type->builtin_core_addr
6061 = init_integer_type (alloc, gdbarch_addr_bit (gdbarch), 1,
6062 "__CORE_ADDR");
6063 return builtin_type;
6064 }
6065
6066 const struct builtin_type *
6067 builtin_type (struct gdbarch *gdbarch)
6068 {
6069 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6070 if (result == nullptr)
6071 {
6072 result = create_gdbtypes_data (gdbarch);
6073 gdbtypes_data.set (gdbarch, result);
6074 }
6075 return result;
6076 }
6077
6078 const struct builtin_type *
6079 builtin_type (struct objfile *objfile)
6080 {
6081 return builtin_type (objfile->arch ());
6082 }
6083
6084 /* See gdbtypes.h. */
6085
6086 CORE_ADDR
6087 call_site::pc () const
6088 {
6089 return per_objfile->relocate (m_unrelocated_pc);
6090 }
6091
6092 void _initialize_gdbtypes ();
6093 void
6094 _initialize_gdbtypes ()
6095 {
6096 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6097 _("Set debugging of C++ overloading."),
6098 _("Show debugging of C++ overloading."),
6099 _("When enabled, ranking of the "
6100 "functions is displayed."),
6101 NULL,
6102 show_overload_debug,
6103 &setdebuglist, &showdebuglist);
6104
6105 /* Add user knob for controlling resolution of opaque types. */
6106 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
6107 &opaque_type_resolution,
6108 _("Set resolution of opaque struct/class/union"
6109 " types (if set before loading symbols)."),
6110 _("Show resolution of opaque struct/class/union"
6111 " types (if set before loading symbols)."),
6112 NULL, NULL,
6113 show_opaque_type_resolution,
6114 &setlist, &showlist);
6115
6116 /* Add an option to permit non-strict type checking. */
6117 add_setshow_boolean_cmd ("type", class_support,
6118 &strict_type_checking,
6119 _("Set strict type checking."),
6120 _("Show strict type checking."),
6121 NULL, NULL,
6122 show_strict_type_checking,
6123 &setchecklist, &showchecklist);
6124 }