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