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