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