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