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