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