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