]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbtypes.c
gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[thirdparty/binutils-gdb.git] / gdb / gdbtypes.c
CommitLineData
c906108c 1/* Support routines for manipulating internal types for GDB.
4f2aea11 2
1d506c26 3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4f2aea11 4
c906108c
SS
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 21
c906108c
SS
22#include "bfd.h"
23#include "symtab.h"
24#include "symfile.h"
25#include "objfiles.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "language.h"
29#include "target.h"
30#include "value.h"
31#include "demangle.h"
32#include "complaints.h"
5b9707eb 33#include "cli/cli-cmds.h"
015a42b4 34#include "cp-abi.h"
ae5a43e0 35#include "hashtab.h"
8de20a37 36#include "cp-support.h"
ca092b61 37#include "bcache.h"
82ca8957 38#include "dwarf2/loc.h"
b84aaada 39#include "dwarf2/read.h"
80180f79 40#include "gdbcore.h"
1841ee5d 41#include "floatformat.h"
a5c641b5 42#include "f-lang.h"
ef83a141 43#include <algorithm>
09584414 44#include "gmp-utils.h"
84914f59
TT
45#include "rust-lang.h"
46#include "ada-lang.h"
ac3aafc7 47
ac03c8d8
TT
48/* The value of an invalid conversion badness. */
49#define INVALID_CONVERSION 100
50
5f59e7e0 51static struct dynamic_prop_list *
52copy_dynamic_prop_list (struct obstack *, struct dynamic_prop_list *);
53
6403aeea
SW
54/* Initialize BADNESS constants. */
55
ac03c8d8 56const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
6403aeea 57
ac03c8d8
TT
58const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
59const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
6403aeea 60
a9d5ef47 61const struct rank EXACT_MATCH_BADNESS = {0,0};
6403aeea 62
a9d5ef47
SW
63const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
64const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
65const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
e15c3eb4 66const struct rank CV_CONVERSION_BADNESS = {1, 0};
a9d5ef47
SW
67const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
68const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
69const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
70const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
5b4f6e25 71const struct rank BOOL_CONVERSION_BADNESS = {3,0};
a9d5ef47
SW
72const struct rank BASE_CONVERSION_BADNESS = {2,0};
73const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
06acc08f 74const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
da096638 75const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
a9d5ef47 76const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
a451cb65 77const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
1d2f86b6 78const struct rank VARARG_BADNESS = {4, 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
9edce54e 1657 sym = lookup_symbol_in_language (name, block, SEARCH_TYPE_DOMAIN,
d12307c1 1658 language->la_language, NULL).symbol;
9edce54e 1659 if (sym != nullptr)
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
ccf41c24 1701 sym = lookup_symbol (name, block, SEARCH_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
ccf41c24 1724 sym = lookup_symbol (name, block, SEARCH_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
ccf41c24 1747 sym = lookup_symbol (name, block, SEARCH_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
ccf41c24
TT
1774 symbol *sym = lookup_symbol (nam.c_str (), block,
1775 SEARCH_STRUCT_DOMAIN, 0).symbol;
c906108c
SS
1776
1777 if (sym == NULL)
1778 {
8a3fe4f8 1779 error (_("No template type named %s."), name);
c906108c 1780 }
5f9c5a63 1781 if (sym->type ()->code () != TYPE_CODE_STRUCT)
c906108c 1782 {
7ba81444
MS
1783 error (_("This context has class, union or enum %s, not a struct."),
1784 name);
c906108c 1785 }
5f9c5a63 1786 return (sym->type ());
c906108c
SS
1787}
1788
ef0bd204 1789/* See gdbtypes.h. */
c906108c 1790
ef0bd204
JB
1791struct_elt
1792lookup_struct_elt (struct type *type, const char *name, int noerr)
c906108c
SS
1793{
1794 int i;
1795
1796 for (;;)
1797 {
f168693b 1798 type = check_typedef (type);
78134374
SM
1799 if (type->code () != TYPE_CODE_PTR
1800 && type->code () != TYPE_CODE_REF)
c906108c 1801 break;
27710edb 1802 type = type->target_type ();
c906108c
SS
1803 }
1804
78134374
SM
1805 if (type->code () != TYPE_CODE_STRUCT
1806 && type->code () != TYPE_CODE_UNION)
c906108c 1807 {
2f408ecb
PA
1808 std::string type_name = type_to_string (type);
1809 error (_("Type %s is not a structure or union type."),
1810 type_name.c_str ());
c906108c
SS
1811 }
1812
1f704f76 1813 for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
c906108c 1814 {
33d16dd9 1815 const char *t_field_name = type->field (i).name ();
c906108c 1816
db577aea 1817 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c 1818 {
b610c045 1819 return {&type->field (i), type->field (i).loc_bitpos ()};
c906108c 1820 }
c11f01db 1821 else if (!t_field_name || *t_field_name == '\0')
f5a010c0 1822 {
ef0bd204 1823 struct_elt elt
940da03e 1824 = lookup_struct_elt (type->field (i).type (), name, 1);
ef0bd204
JB
1825 if (elt.field != NULL)
1826 {
b610c045 1827 elt.offset += type->field (i).loc_bitpos ();
ef0bd204
JB
1828 return elt;
1829 }
f5a010c0 1830 }
c906108c
SS
1831 }
1832
1833 /* OK, it's not in this class. Recursively check the baseclasses. */
1834 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1835 {
ef0bd204
JB
1836 struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
1837 if (elt.field != NULL)
1838 return elt;
c906108c
SS
1839 }
1840
1841 if (noerr)
ef0bd204 1842 return {nullptr, 0};
c5aa993b 1843
2f408ecb
PA
1844 std::string type_name = type_to_string (type);
1845 error (_("Type %s has no component named %s."), type_name.c_str (), name);
c906108c
SS
1846}
1847
ef0bd204
JB
1848/* See gdbtypes.h. */
1849
1850struct type *
1851lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1852{
1853 struct_elt elt = lookup_struct_elt (type, name, noerr);
1854 if (elt.field != NULL)
b6cdac4b 1855 return elt.field->type ();
ef0bd204
JB
1856 else
1857 return NULL;
1858}
1859
c3c1e645 1860/* Return the largest number representable by unsigned integer type TYPE. */
ed3ef339 1861
c3c1e645
GB
1862ULONGEST
1863get_unsigned_type_max (struct type *type)
ed3ef339
DE
1864{
1865 unsigned int n;
1866
f168693b 1867 type = check_typedef (type);
c6d940a9 1868 gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
df86565b 1869 gdb_assert (type->length () <= sizeof (ULONGEST));
ed3ef339
DE
1870
1871 /* Written this way to avoid overflow. */
df86565b 1872 n = type->length () * TARGET_CHAR_BIT;
c3c1e645 1873 return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
ed3ef339
DE
1874}
1875
1876/* Store in *MIN, *MAX the smallest and largest numbers representable by
1877 signed integer type TYPE. */
1878
1879void
1880get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1881{
1882 unsigned int n;
1883
f168693b 1884 type = check_typedef (type);
c6d940a9 1885 gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
df86565b 1886 gdb_assert (type->length () <= sizeof (LONGEST));
ed3ef339 1887
df86565b 1888 n = type->length () * TARGET_CHAR_BIT;
ed3ef339
DE
1889 *min = -((ULONGEST) 1 << (n - 1));
1890 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1891}
1892
b5b591a8
GB
1893/* Return the largest value representable by pointer type TYPE. */
1894
1895CORE_ADDR
1896get_pointer_type_max (struct type *type)
1897{
1898 unsigned int n;
1899
1900 type = check_typedef (type);
1901 gdb_assert (type->code () == TYPE_CODE_PTR);
df86565b 1902 gdb_assert (type->length () <= sizeof (CORE_ADDR));
b5b591a8 1903
df86565b 1904 n = type->length () * TARGET_CHAR_BIT;
b5b591a8
GB
1905 return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
1906}
1907
ae6ae975
DE
1908/* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
1909 cplus_stuff.vptr_fieldno.
1910
1911 cplus_stuff is initialized to cplus_struct_default which does not
1912 set vptr_fieldno to -1 for portability reasons (IWBN to use C99
1913 designated initializers). We cope with that here. */
1914
1915int
1916internal_type_vptr_fieldno (struct type *type)
1917{
f168693b 1918 type = check_typedef (type);
78134374
SM
1919 gdb_assert (type->code () == TYPE_CODE_STRUCT
1920 || type->code () == TYPE_CODE_UNION);
ae6ae975
DE
1921 if (!HAVE_CPLUS_STRUCT (type))
1922 return -1;
1923 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
1924}
1925
1926/* Set the value of cplus_stuff.vptr_fieldno. */
1927
1928void
1929set_type_vptr_fieldno (struct type *type, int fieldno)
1930{
f168693b 1931 type = check_typedef (type);
78134374
SM
1932 gdb_assert (type->code () == TYPE_CODE_STRUCT
1933 || type->code () == TYPE_CODE_UNION);
ae6ae975
DE
1934 if (!HAVE_CPLUS_STRUCT (type))
1935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1936 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
1937}
1938
1939/* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
1940 cplus_stuff.vptr_basetype. */
1941
1942struct type *
1943internal_type_vptr_basetype (struct type *type)
1944{
f168693b 1945 type = check_typedef (type);
78134374
SM
1946 gdb_assert (type->code () == TYPE_CODE_STRUCT
1947 || type->code () == TYPE_CODE_UNION);
ae6ae975
DE
1948 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
1949 return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
1950}
1951
1952/* Set the value of cplus_stuff.vptr_basetype. */
1953
1954void
1955set_type_vptr_basetype (struct type *type, struct type *basetype)
1956{
f168693b 1957 type = check_typedef (type);
78134374
SM
1958 gdb_assert (type->code () == TYPE_CODE_STRUCT
1959 || type->code () == TYPE_CODE_UNION);
ae6ae975
DE
1960 if (!HAVE_CPLUS_STRUCT (type))
1961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1962 TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
1963}
1964
81fe8080
DE
1965/* Lookup the vptr basetype/fieldno values for TYPE.
1966 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1967 vptr_fieldno. Also, if found and basetype is from the same objfile,
1968 cache the results.
1969 If not found, return -1 and ignore BASETYPEP.
1970 Callers should be aware that in some cases (for example,
c906108c 1971 the type or one of its baseclasses is a stub type and we are
d48cc9dd
DJ
1972 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1973 this function will not be able to find the
7ba81444 1974 virtual function table pointer, and vptr_fieldno will remain -1 and
81fe8080 1975 vptr_basetype will remain NULL or incomplete. */
c906108c 1976
81fe8080
DE
1977int
1978get_vptr_fieldno (struct type *type, struct type **basetypep)
c906108c 1979{
f168693b 1980 type = check_typedef (type);
c906108c
SS
1981
1982 if (TYPE_VPTR_FIELDNO (type) < 0)
1983 {
1984 int i;
1985
7ba81444 1986 /* We must start at zero in case the first (and only) baseclass
dda83cd7 1987 is virtual (and hence we cannot share the table pointer). */
c906108c
SS
1988 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1989 {
81fe8080
DE
1990 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1991 int fieldno;
1992 struct type *basetype;
1993
1994 fieldno = get_vptr_fieldno (baseclass, &basetype);
1995 if (fieldno >= 0)
c906108c 1996 {
81fe8080 1997 /* If the type comes from a different objfile we can't cache
0963b4bd 1998 it, it may have a different lifetime. PR 2384 */
6ac37371 1999 if (type->objfile_owner () == basetype->objfile_owner ())
81fe8080 2000 {
ae6ae975
DE
2001 set_type_vptr_fieldno (type, fieldno);
2002 set_type_vptr_basetype (type, basetype);
81fe8080
DE
2003 }
2004 if (basetypep)
2005 *basetypep = basetype;
2006 return fieldno;
c906108c
SS
2007 }
2008 }
81fe8080
DE
2009
2010 /* Not found. */
2011 return -1;
2012 }
2013 else
2014 {
2015 if (basetypep)
2016 *basetypep = TYPE_VPTR_BASETYPE (type);
2017 return TYPE_VPTR_FIELDNO (type);
c906108c
SS
2018 }
2019}
2020
44e1a9eb
DJ
2021static void
2022stub_noname_complaint (void)
2023{
b98664d3 2024 complaint (_("stub type has NULL name"));
44e1a9eb
DJ
2025}
2026
a405673c
JB
2027/* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
2028 attached to it, and that property has a non-constant value. */
2029
2030static int
2031array_type_has_dynamic_stride (struct type *type)
2032{
24e99c6c 2033 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
a405673c 2034
9c0fb734 2035 return prop != nullptr && prop->is_constant ();
a405673c
JB
2036}
2037
d98b7a16 2038/* Worker for is_dynamic_type. */
80180f79 2039
86542ab5
TT
2040static bool
2041is_dynamic_type_internal (struct type *type, bool top_level)
80180f79
SA
2042{
2043 type = check_typedef (type);
2044
f18fc7e5
BH
2045 /* We only want to recognize references and pointers at the outermost
2046 level. */
2047 if (top_level && type->is_pointer_or_reference ())
27710edb 2048 type = check_typedef (type->target_type ());
e771e4be 2049
3cdcd0ce
JB
2050 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
2051 dynamic, even if the type itself is statically defined.
2052 From a user's point of view, this may appear counter-intuitive;
2053 but it makes sense in this context, because the point is to determine
2054 whether any part of the type needs to be resolved before it can
2055 be exploited. */
2056 if (TYPE_DATA_LOCATION (type) != NULL
2057 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
2058 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
86542ab5 2059 return true;
3cdcd0ce 2060
3f2f83dd 2061 if (TYPE_ASSOCIATED_PROP (type))
86542ab5 2062 return true;
3f2f83dd
KB
2063
2064 if (TYPE_ALLOCATED_PROP (type))
86542ab5 2065 return true;
3f2f83dd 2066
24e99c6c 2067 struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
8c2e4e06 2068 if (prop != nullptr && prop->kind () != PROP_TYPE)
86542ab5 2069 return true;
ef83a141 2070
f8e89861 2071 if (TYPE_HAS_DYNAMIC_LENGTH (type))
86542ab5 2072 return true;
f8e89861 2073
78134374 2074 switch (type->code ())
80180f79 2075 {
6f8a3220 2076 case TYPE_CODE_RANGE:
ddb87a81
JB
2077 {
2078 /* A range type is obviously dynamic if it has at least one
2079 dynamic bound. But also consider the range type to be
2080 dynamic when its subtype is dynamic, even if the bounds
2081 of the range type are static. It allows us to assume that
2082 the subtype of a static range type is also static. */
599088e3 2083 return (!has_static_range (type->bounds ())
86542ab5 2084 || is_dynamic_type_internal (type->target_type (), false));
ddb87a81 2085 }
6f8a3220 2086
216a7e6b
AB
2087 case TYPE_CODE_STRING:
2088 /* Strings are very much like an array of characters, and can be
2089 treated as one here. */
80180f79
SA
2090 case TYPE_CODE_ARRAY:
2091 {
1f704f76 2092 gdb_assert (type->num_fields () == 1);
6f8a3220 2093
a405673c 2094 /* The array is dynamic if either the bounds are dynamic... */
86542ab5
TT
2095 if (is_dynamic_type_internal (type->index_type (), false))
2096 return true;
a405673c 2097 /* ... or the elements it contains have a dynamic contents... */
86542ab5
TT
2098 if (is_dynamic_type_internal (type->target_type (), false))
2099 return true;
a405673c
JB
2100 /* ... or if it has a dynamic stride... */
2101 if (array_type_has_dynamic_stride (type))
86542ab5
TT
2102 return true;
2103 return false;
80180f79 2104 }
012370f6
TT
2105
2106 case TYPE_CODE_STRUCT:
2107 case TYPE_CODE_UNION:
2108 {
2109 int i;
2110
7d79de9a
TT
2111 bool is_cplus = HAVE_CPLUS_STRUCT (type);
2112
1f704f76 2113 for (i = 0; i < type->num_fields (); ++i)
7d79de9a
TT
2114 {
2115 /* Static fields can be ignored here. */
c819a338 2116 if (type->field (i).is_static ())
7d79de9a
TT
2117 continue;
2118 /* If the field has dynamic type, then so does TYPE. */
86542ab5
TT
2119 if (is_dynamic_type_internal (type->field (i).type (), false))
2120 return true;
7d79de9a
TT
2121 /* If the field is at a fixed offset, then it is not
2122 dynamic. */
2ad53ea1 2123 if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
7d79de9a
TT
2124 continue;
2125 /* Do not consider C++ virtual base types to be dynamic
2126 due to the field's offset being dynamic; these are
2127 handled via other means. */
2128 if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
2129 continue;
86542ab5 2130 return true;
7d79de9a 2131 }
012370f6
TT
2132 }
2133 break;
80180f79 2134 }
92e2a17f 2135
86542ab5 2136 return false;
80180f79
SA
2137}
2138
d98b7a16
TT
2139/* See gdbtypes.h. */
2140
86542ab5 2141bool
d98b7a16
TT
2142is_dynamic_type (struct type *type)
2143{
86542ab5 2144 return is_dynamic_type_internal (type, true);
d98b7a16
TT
2145}
2146
df25ebbd 2147static struct type *resolve_dynamic_type_internal
aeabe83d 2148 (struct type *type, struct property_addr_info *addr_stack,
86542ab5 2149 const frame_info_ptr &frame, bool top_level);
d98b7a16 2150
df25ebbd
JB
2151/* Given a dynamic range type (dyn_range_type) and a stack of
2152 struct property_addr_info elements, return a static version
b7874836
AB
2153 of that type.
2154
2155 When RESOLVE_P is true then the returned static range is created by
2156 actually evaluating any dynamic properties within the range type, while
2157 when RESOLVE_P is false the returned static range has all of the bounds
2158 and stride information set to undefined. The RESOLVE_P set to false
2159 case will be used when evaluating a dynamic array that is not
2160 allocated, or not associated, i.e. the bounds information might not be
3fb842ce
AB
2161 initialized yet.
2162
2163 RANK is the array rank for which we are resolving this range, and is a
2164 zero based count. The rank should never be negative.
2165*/
d190df30 2166
80180f79 2167static struct type *
df25ebbd 2168resolve_dynamic_range (struct type *dyn_range_type,
b7874836 2169 struct property_addr_info *addr_stack,
aeabe83d 2170 const frame_info_ptr &frame,
df7a7bdd 2171 int rank, bool resolve_p = true)
80180f79
SA
2172{
2173 CORE_ADDR value;
ddb87a81 2174 struct type *static_range_type, *static_target_type;
5bbd8269 2175 struct dynamic_prop low_bound, high_bound, stride;
80180f79 2176
78134374 2177 gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
3fb842ce 2178 gdb_assert (rank >= 0);
80180f79 2179
599088e3 2180 const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
a8b16509
TT
2181 if (resolve_p)
2182 {
2183 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2184 { (CORE_ADDR) rank }))
2185 low_bound.set_const_val (value);
2186 else if (prop->kind () == PROP_UNDEFINED)
2187 low_bound.set_undefined ();
2188 else
2189 low_bound.set_optimized_out ();
2190 }
80180f79 2191 else
8c2e4e06 2192 low_bound.set_undefined ();
80180f79 2193
599088e3 2194 prop = &dyn_range_type->bounds ()->high;
a8b16509 2195 if (resolve_p)
80180f79 2196 {
a8b16509
TT
2197 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value,
2198 { (CORE_ADDR) rank }))
2199 {
2200 high_bound.set_const_val (value);
c451ebe5 2201
a8b16509
TT
2202 if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
2203 high_bound.set_const_val
2204 (low_bound.const_val () + high_bound.const_val () - 1);
2205 }
2206 else if (prop->kind () == PROP_UNDEFINED)
2207 high_bound.set_undefined ();
2208 else
2209 high_bound.set_optimized_out ();
80180f79
SA
2210 }
2211 else
8c2e4e06 2212 high_bound.set_undefined ();
80180f79 2213
599088e3
SM
2214 bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
2215 prop = &dyn_range_type->bounds ()->stride;
aeabe83d 2216 if (resolve_p && dwarf2_evaluate_property (prop, frame, addr_stack, &value,
df7a7bdd 2217 { (CORE_ADDR) rank }))
5bbd8269 2218 {
8c2e4e06 2219 stride.set_const_val (value);
5bbd8269
AB
2220
2221 /* If we have a bit stride that is not an exact number of bytes then
2222 I really don't think this is going to work with current GDB, the
2223 array indexing code in GDB seems to be pretty heavily tied to byte
2224 offsets right now. Assuming 8 bits in a byte. */
8ee511af 2225 struct gdbarch *gdbarch = dyn_range_type->arch ();
5bbd8269
AB
2226 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
2227 if (!byte_stride_p && (value % (unit_size * 8)) != 0)
2228 error (_("bit strides that are not a multiple of the byte size "
2229 "are currently not supported"));
2230 }
2231 else
2232 {
8c2e4e06 2233 stride.set_undefined ();
5bbd8269
AB
2234 byte_stride_p = true;
2235 }
2236
ddb87a81 2237 static_target_type
27710edb 2238 = resolve_dynamic_type_internal (dyn_range_type->target_type (),
86542ab5 2239 addr_stack, frame, false);
599088e3 2240 LONGEST bias = dyn_range_type->bounds ()->bias;
e727c536 2241 type_allocator alloc (dyn_range_type);
5bbd8269 2242 static_range_type = create_range_type_with_stride
e727c536 2243 (alloc, static_target_type,
5bbd8269 2244 &low_bound, &high_bound, bias, &stride, byte_stride_p);
4bfcb286 2245 static_range_type->set_name (dyn_range_type->name ());
599088e3 2246 static_range_type->bounds ()->flag_bound_evaluated = 1;
6f8a3220
JB
2247 return static_range_type;
2248}
2249
df7a7bdd 2250/* Helper function for resolve_dynamic_array_or_string. This function
2251 resolves the properties for a single array at RANK within a nested array
3fb842ce 2252 of arrays structure. The RANK value is greater than or equal to 0, and
df7a7bdd 2253 starts at it's maximum value and goes down by 1 for each recursive call
2254 to this function. So, for a 3-dimensional array, the first call to this
3fb842ce
AB
2255 function has RANK == 2, then we call ourselves recursively with RANK ==
2256 1, than again with RANK == 0, and at that point we should return.
df7a7bdd 2257
2258 TYPE is updated as the dynamic properties are resolved, and so, should
2259 be a copy of the dynamic type, rather than the original dynamic type
2260 itself.
2261
2262 ADDR_STACK is a stack of struct property_addr_info to be used if needed
2263 during the dynamic resolution.
b7874836
AB
2264
2265 When RESOLVE_P is true then the dynamic properties of TYPE are
2266 evaluated, otherwise the dynamic properties of TYPE are not evaluated,
2267 instead we assume the array is not allocated/associated yet. */
6f8a3220
JB
2268
2269static struct type *
df7a7bdd 2270resolve_dynamic_array_or_string_1 (struct type *type,
2271 struct property_addr_info *addr_stack,
aeabe83d 2272 const frame_info_ptr &frame,
df7a7bdd 2273 int rank, bool resolve_p)
6f8a3220
JB
2274{
2275 CORE_ADDR value;
2276 struct type *elt_type;
2277 struct type *range_type;
2278 struct type *ary_dim;
3f2f83dd 2279 struct dynamic_prop *prop;
a405673c 2280 unsigned int bit_stride = 0;
6f8a3220 2281
216a7e6b
AB
2282 /* For dynamic type resolution strings can be treated like arrays of
2283 characters. */
78134374
SM
2284 gdb_assert (type->code () == TYPE_CODE_ARRAY
2285 || type->code () == TYPE_CODE_STRING);
6f8a3220 2286
3fb842ce
AB
2287 /* As the rank is a zero based count we expect this to never be
2288 negative. */
2289 gdb_assert (rank >= 0);
3f2f83dd 2290
b7874836
AB
2291 /* Resolve the allocated and associated properties before doing anything
2292 else. If an array is not allocated or not associated then (at least
2293 for Fortran) there is no guarantee that the data to define the upper
2294 bound, lower bound, or stride will be correct. If RESOLVE_P is
2295 already false at this point then this is not the first dimension of
2296 the array and a more outer dimension has already marked this array as
2297 not allocated/associated, as such we just ignore this property. This
2298 is fine as GDB only checks the allocated/associated on the outer most
2299 dimension of the array. */
3f2f83dd 2300 prop = TYPE_ALLOCATED_PROP (type);
b7874836 2301 if (prop != NULL && resolve_p
aeabe83d 2302 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
b7874836
AB
2303 {
2304 prop->set_const_val (value);
2305 if (value == 0)
2306 resolve_p = false;
2307 }
8c2e4e06 2308
3f2f83dd 2309 prop = TYPE_ASSOCIATED_PROP (type);
b7874836 2310 if (prop != NULL && resolve_p
aeabe83d 2311 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
b7874836
AB
2312 {
2313 prop->set_const_val (value);
2314 if (value == 0)
2315 resolve_p = false;
2316 }
3f2f83dd 2317
b7874836 2318 range_type = check_typedef (type->index_type ());
df7a7bdd 2319 range_type
aeabe83d 2320 = resolve_dynamic_range (range_type, addr_stack, frame, rank, resolve_p);
80180f79 2321
27710edb 2322 ary_dim = check_typedef (type->target_type ());
78134374 2323 if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
df7a7bdd 2324 {
2325 ary_dim = copy_type (ary_dim);
2326 elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
aeabe83d
TT
2327 frame, rank - 1,
2328 resolve_p);
df7a7bdd 2329 }
80180f79 2330 else
27710edb 2331 elt_type = type->target_type ();
80180f79 2332
24e99c6c 2333 prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
b7874836 2334 if (prop != NULL && resolve_p)
a405673c 2335 {
aeabe83d 2336 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
a405673c 2337 {
7aa91313 2338 type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
a405673c
JB
2339 bit_stride = (unsigned int) (value * 8);
2340 }
2341 else
2342 {
2343 /* Could be a bug in our code, but it could also happen
2344 if the DWARF info is not correct. Issue a warning,
2345 and assume no byte/bit stride (leave bit_stride = 0). */
2346 warning (_("cannot determine array stride for type %s"),
7d93a1e0 2347 type->name () ? type->name () : "<no name>");
a405673c
JB
2348 }
2349 }
2350 else
3757d2d4 2351 bit_stride = type->field (0).bitsize ();
a405673c 2352
9e76b17a
TT
2353 type_allocator alloc (type, type_allocator::SMASH);
2354 return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
dda83cd7 2355 bit_stride);
80180f79
SA
2356}
2357
df7a7bdd 2358/* Resolve an array or string type with dynamic properties, return a new
2359 type with the dynamic properties resolved to actual values. The
2360 ADDR_STACK represents the location of the object being resolved. */
2361
2362static struct type *
2363resolve_dynamic_array_or_string (struct type *type,
aeabe83d
TT
2364 struct property_addr_info *addr_stack,
2365 const frame_info_ptr &frame)
df7a7bdd 2366{
2367 CORE_ADDR value;
2368 int rank = 0;
2369
2370 /* For dynamic type resolution strings can be treated like arrays of
2371 characters. */
2372 gdb_assert (type->code () == TYPE_CODE_ARRAY
2373 || type->code () == TYPE_CODE_STRING);
2374
2375 type = copy_type (type);
2376
2377 /* Resolve the rank property to get rank value. */
2378 struct dynamic_prop *prop = TYPE_RANK_PROP (type);
aeabe83d 2379 if (dwarf2_evaluate_property (prop, frame, addr_stack, &value))
df7a7bdd 2380 {
2381 prop->set_const_val (value);
2382 rank = value;
2383
2384 if (rank == 0)
2385 {
5f59e7e0 2386 /* Rank is zero, if a variable is passed as an argument to a
2387 function. In this case the resolved type should not be an
2388 array, but should instead be that of an array element. */
2389 struct type *dynamic_array_type = type;
27710edb 2390 type = copy_type (dynamic_array_type->target_type ());
5f59e7e0 2391 struct dynamic_prop_list *prop_list
2392 = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
2393 if (prop_list != nullptr)
2394 {
2395 struct obstack *obstack
2396 = &type->objfile_owner ()->objfile_obstack;
2397 TYPE_MAIN_TYPE (type)->dyn_prop_list
2398 = copy_dynamic_prop_list (obstack, prop_list);
2399 }
2400 return type;
df7a7bdd 2401 }
2402 else if (type->code () == TYPE_CODE_STRING && rank != 1)
2403 {
2404 /* What would this even mean? A string with a dynamic rank
2405 greater than 1. */
2406 error (_("unable to handle string with dynamic rank greater than 1"));
2407 }
2408 else if (rank > 1)
2409 {
2410 /* Arrays with dynamic rank are initially just an array type
2411 with a target type that is the array element.
2412
2413 However, now we know the rank of the array we need to build
2414 the array of arrays structure that GDB expects, that is we
2415 need an array type that has a target which is an array type,
2416 and so on, until eventually, we have the element type at the
2417 end of the chain. Create all the additional array types here
2418 by copying the top level array type. */
27710edb 2419 struct type *element_type = type->target_type ();
df7a7bdd 2420 struct type *rank_type = type;
2421 for (int i = 1; i < rank; i++)
2422 {
8a50fdce 2423 rank_type->set_target_type (copy_type (rank_type));
27710edb 2424 rank_type = rank_type->target_type ();
df7a7bdd 2425 }
8a50fdce 2426 rank_type->set_target_type (element_type);
df7a7bdd 2427 }
2428 }
2429 else
2430 {
2431 rank = 1;
2432
27710edb 2433 for (struct type *tmp_type = check_typedef (type->target_type ());
df7a7bdd 2434 tmp_type->code () == TYPE_CODE_ARRAY;
27710edb 2435 tmp_type = check_typedef (tmp_type->target_type ()))
df7a7bdd 2436 ++rank;
2437 }
2438
3fb842ce
AB
2439 /* The rank that we calculated above is actually a count of the number of
2440 ranks. However, when we resolve the type of each individual array
2441 rank we should actually use a rank "offset", e.g. an array with a rank
2442 count of 1 (calculated above) will use the rank offset 0 in order to
2443 resolve the details of the first array dimension. As a result, we
2444 reduce the rank by 1 here. */
2445 --rank;
2446
aeabe83d
TT
2447 return resolve_dynamic_array_or_string_1 (type, addr_stack, frame, rank,
2448 true);
df7a7bdd 2449}
2450
012370f6 2451/* Resolve dynamic bounds of members of the union TYPE to static
df25ebbd
JB
2452 bounds. ADDR_STACK is a stack of struct property_addr_info
2453 to be used if needed during the dynamic resolution. */
012370f6
TT
2454
2455static struct type *
df25ebbd 2456resolve_dynamic_union (struct type *type,
aeabe83d
TT
2457 struct property_addr_info *addr_stack,
2458 const frame_info_ptr &frame)
012370f6
TT
2459{
2460 struct type *resolved_type;
2461 int i;
2462 unsigned int max_len = 0;
2463
78134374 2464 gdb_assert (type->code () == TYPE_CODE_UNION);
012370f6
TT
2465
2466 resolved_type = copy_type (type);
2774f2da 2467 resolved_type->copy_fields (type);
1f704f76 2468 for (i = 0; i < resolved_type->num_fields (); ++i)
012370f6
TT
2469 {
2470 struct type *t;
2471
c819a338 2472 if (type->field (i).is_static ())
012370f6
TT
2473 continue;
2474
940da03e 2475 t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
86542ab5 2476 addr_stack, frame, false);
5d14b6e5 2477 resolved_type->field (i).set_type (t);
2f33032a
KS
2478
2479 struct type *real_type = check_typedef (t);
df86565b
SM
2480 if (real_type->length () > max_len)
2481 max_len = real_type->length ();
012370f6
TT
2482 }
2483
b6cdbc9a 2484 resolved_type->set_length (max_len);
012370f6
TT
2485 return resolved_type;
2486}
2487
ef83a141
TT
2488/* See gdbtypes.h. */
2489
2490bool
2491variant::matches (ULONGEST value, bool is_unsigned) const
2492{
2493 for (const discriminant_range &range : discriminants)
2494 if (range.contains (value, is_unsigned))
2495 return true;
2496 return false;
2497}
2498
2499static void
2500compute_variant_fields_inner (struct type *type,
2501 struct property_addr_info *addr_stack,
2502 const variant_part &part,
2503 std::vector<bool> &flags);
2504
2505/* A helper function to determine which variant fields will be active.
2506 This handles both the variant's direct fields, and any variant
2507 parts embedded in this variant. TYPE is the type we're examining.
2508 ADDR_STACK holds information about the concrete object. VARIANT is
2509 the current variant to be handled. FLAGS is where the results are
2510 stored -- this function sets the Nth element in FLAGS if the
2511 corresponding field is enabled. ENABLED is whether this variant is
2512 enabled or not. */
2513
2514static void
2515compute_variant_fields_recurse (struct type *type,
2516 struct property_addr_info *addr_stack,
2517 const variant &variant,
2518 std::vector<bool> &flags,
2519 bool enabled)
2520{
2521 for (int field = variant.first_field; field < variant.last_field; ++field)
2522 flags[field] = enabled;
2523
2524 for (const variant_part &new_part : variant.parts)
2525 {
2526 if (enabled)
2527 compute_variant_fields_inner (type, addr_stack, new_part, flags);
2528 else
2529 {
2530 for (const auto &sub_variant : new_part.variants)
2531 compute_variant_fields_recurse (type, addr_stack, sub_variant,
2532 flags, enabled);
2533 }
2534 }
2535}
2536
2537/* A helper function to determine which variant fields will be active.
2538 This evaluates the discriminant, decides which variant (if any) is
2539 active, and then updates FLAGS to reflect which fields should be
2540 available. TYPE is the type we're examining. ADDR_STACK holds
2541 information about the concrete object. VARIANT is the current
2542 variant to be handled. FLAGS is where the results are stored --
2543 this function sets the Nth element in FLAGS if the corresponding
2544 field is enabled. */
2545
2546static void
2547compute_variant_fields_inner (struct type *type,
2548 struct property_addr_info *addr_stack,
2549 const variant_part &part,
2550 std::vector<bool> &flags)
2551{
2552 /* Evaluate the discriminant. */
6b09f134 2553 std::optional<ULONGEST> discr_value;
ef83a141
TT
2554 if (part.discriminant_index != -1)
2555 {
2556 int idx = part.discriminant_index;
2557
2ad53ea1 2558 if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
ef83a141
TT
2559 error (_("Cannot determine struct field location"
2560 " (invalid location kind)"));
2561
b249d2c2
TT
2562 if (addr_stack->valaddr.data () != NULL)
2563 discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
2564 idx);
ef83a141
TT
2565 else
2566 {
2567 CORE_ADDR addr = (addr_stack->addr
b610c045 2568 + (type->field (idx).loc_bitpos ()
ef83a141
TT
2569 / TARGET_CHAR_BIT));
2570
3757d2d4 2571 LONGEST bitsize = type->field (idx).bitsize ();
ef83a141
TT
2572 LONGEST size = bitsize / 8;
2573 if (size == 0)
df86565b 2574 size = type->field (idx).type ()->length ();
ef83a141
TT
2575
2576 gdb_byte bits[sizeof (ULONGEST)];
2577 read_memory (addr, bits, size);
2578
b610c045 2579 LONGEST bitpos = (type->field (idx).loc_bitpos ()
ef83a141
TT
2580 % TARGET_CHAR_BIT);
2581
940da03e 2582 discr_value = unpack_bits_as_long (type->field (idx).type (),
ef83a141
TT
2583 bits, bitpos, bitsize);
2584 }
2585 }
2586
2587 /* Go through each variant and see which applies. */
2588 const variant *default_variant = nullptr;
2589 const variant *applied_variant = nullptr;
2590 for (const auto &variant : part.variants)
2591 {
2592 if (variant.is_default ())
2593 default_variant = &variant;
2594 else if (discr_value.has_value ()
2595 && variant.matches (*discr_value, part.is_unsigned))
2596 {
2597 applied_variant = &variant;
2598 break;
2599 }
2600 }
2601 if (applied_variant == nullptr)
2602 applied_variant = default_variant;
2603
2604 for (const auto &variant : part.variants)
2605 compute_variant_fields_recurse (type, addr_stack, variant,
2606 flags, applied_variant == &variant);
2607}
2608
2609/* Determine which variant fields are available in TYPE. The enabled
2610 fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
2611 about the concrete object. PARTS describes the top-level variant
2612 parts for this type. */
2613
2614static void
2615compute_variant_fields (struct type *type,
2616 struct type *resolved_type,
2617 struct property_addr_info *addr_stack,
2618 const gdb::array_view<variant_part> &parts)
2619{
2620 /* Assume all fields are included by default. */
1f704f76 2621 std::vector<bool> flags (resolved_type->num_fields (), true);
ef83a141
TT
2622
2623 /* Now disable fields based on the variants that control them. */
2624 for (const auto &part : parts)
2625 compute_variant_fields_inner (type, addr_stack, part, flags);
2626
2774f2da
TV
2627 unsigned int nfields = std::count (flags.begin (), flags.end (), true);
2628 /* No need to zero-initialize the newly allocated fields, they'll be
2629 initialized by the copy in the loop below. */
2630 resolved_type->alloc_fields (nfields, false);
3cabb6b0 2631
ef83a141 2632 int out = 0;
1f704f76 2633 for (int i = 0; i < type->num_fields (); ++i)
ef83a141
TT
2634 {
2635 if (!flags[i])
2636 continue;
2637
ceacbf6e 2638 resolved_type->field (out) = type->field (i);
ef83a141
TT
2639 ++out;
2640 }
2641}
2642
012370f6 2643/* Resolve dynamic bounds of members of the struct TYPE to static
df25ebbd
JB
2644 bounds. ADDR_STACK is a stack of struct property_addr_info to
2645 be used if needed during the dynamic resolution. */
012370f6
TT
2646
2647static struct type *
df25ebbd 2648resolve_dynamic_struct (struct type *type,
aeabe83d
TT
2649 struct property_addr_info *addr_stack,
2650 const frame_info_ptr &frame)
012370f6
TT
2651{
2652 struct type *resolved_type;
2653 int i;
6908c509 2654 unsigned resolved_type_bit_length = 0;
012370f6 2655
78134374 2656 gdb_assert (type->code () == TYPE_CODE_STRUCT);
012370f6
TT
2657
2658 resolved_type = copy_type (type);
ef83a141 2659
24e99c6c 2660 dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
8c2e4e06 2661 if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
ef83a141
TT
2662 {
2663 compute_variant_fields (type, resolved_type, addr_stack,
8c2e4e06 2664 *variant_prop->variant_parts ());
ef83a141
TT
2665 /* We want to leave the property attached, so that the Rust code
2666 can tell whether the type was originally an enum. */
8c2e4e06 2667 variant_prop->set_original_type (type);
ef83a141
TT
2668 }
2669 else
2670 {
2774f2da 2671 resolved_type->copy_fields (type);
ef83a141
TT
2672 }
2673
1f704f76 2674 for (i = 0; i < resolved_type->num_fields (); ++i)
012370f6 2675 {
6908c509 2676 unsigned new_bit_length;
df25ebbd 2677 struct property_addr_info pinfo;
012370f6 2678
c819a338 2679 if (resolved_type->field (i).is_static ())
012370f6
TT
2680 continue;
2681
2ad53ea1 2682 if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
7d79de9a
TT
2683 {
2684 struct dwarf2_property_baton baton;
2685 baton.property_type
940da03e 2686 = lookup_pointer_type (resolved_type->field (i).type ());
51e36a3a 2687 baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
7d79de9a
TT
2688
2689 struct dynamic_prop prop;
8c2e4e06 2690 prop.set_locexpr (&baton);
7d79de9a
TT
2691
2692 CORE_ADDR addr;
aeabe83d 2693 if (dwarf2_evaluate_property (&prop, frame, addr_stack, &addr,
1fb43cf7 2694 {addr_stack->addr}))
cd3f655c
SM
2695 resolved_type->field (i).set_loc_bitpos
2696 (TARGET_CHAR_BIT * (addr - addr_stack->addr));
7d79de9a
TT
2697 }
2698
6908c509
JB
2699 /* As we know this field is not a static field, the field's
2700 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
2701 this is the case, but only trigger a simple error rather
2702 than an internal error if that fails. While failing
2703 that verification indicates a bug in our code, the error
2704 is not severe enough to suggest to the user he stops
2705 his debugging session because of it. */
2ad53ea1 2706 if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
6908c509
JB
2707 error (_("Cannot determine struct field location"
2708 " (invalid location kind)"));
df25ebbd 2709
940da03e 2710 pinfo.type = check_typedef (resolved_type->field (i).type ());
b610c045 2711 size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
c3345124 2712 pinfo.valaddr = addr_stack->valaddr;
05fb05a9
TT
2713 if (!pinfo.valaddr.empty ())
2714 pinfo.valaddr = pinfo.valaddr.slice (offset);
2715 pinfo.addr = addr_stack->addr + offset;
df25ebbd
JB
2716 pinfo.next = addr_stack;
2717
5d14b6e5 2718 resolved_type->field (i).set_type
940da03e 2719 (resolve_dynamic_type_internal (resolved_type->field (i).type (),
86542ab5 2720 &pinfo, frame, false));
2ad53ea1 2721 gdb_assert (resolved_type->field (i).loc_kind ()
df25ebbd
JB
2722 == FIELD_LOC_KIND_BITPOS);
2723
b610c045 2724 new_bit_length = resolved_type->field (i).loc_bitpos ();
3757d2d4
SM
2725 if (resolved_type->field (i).bitsize () != 0)
2726 new_bit_length += resolved_type->field (i).bitsize ();
6908c509 2727 else
2f33032a
KS
2728 {
2729 struct type *real_type
2730 = check_typedef (resolved_type->field (i).type ());
2731
df86565b 2732 new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
2f33032a 2733 }
6908c509
JB
2734
2735 /* Normally, we would use the position and size of the last field
2736 to determine the size of the enclosing structure. But GCC seems
2737 to be encoding the position of some fields incorrectly when
2738 the struct contains a dynamic field that is not placed last.
2739 So we compute the struct size based on the field that has
2740 the highest position + size - probably the best we can do. */
2741 if (new_bit_length > resolved_type_bit_length)
2742 resolved_type_bit_length = new_bit_length;
012370f6
TT
2743 }
2744
9920b434
BH
2745 /* The length of a type won't change for fortran, but it does for C and Ada.
2746 For fortran the size of dynamic fields might change over time but not the
2747 type length of the structure. If we adapt it, we run into problems
2748 when calculating the element offset for arrays of structs. */
2749 if (current_language->la_language != language_fortran)
b6cdbc9a
SM
2750 resolved_type->set_length ((resolved_type_bit_length + TARGET_CHAR_BIT - 1)
2751 / TARGET_CHAR_BIT);
6908c509 2752
9e195661
PMR
2753 /* The Ada language uses this field as a cache for static fixed types: reset
2754 it as RESOLVED_TYPE must have its own static fixed type. */
8a50fdce 2755 resolved_type->set_target_type (nullptr);
9e195661 2756
012370f6
TT
2757 return resolved_type;
2758}
2759
d98b7a16 2760/* Worker for resolved_dynamic_type. */
80180f79 2761
d98b7a16 2762static struct type *
df25ebbd 2763resolve_dynamic_type_internal (struct type *type,
ee715b5a 2764 struct property_addr_info *addr_stack,
aeabe83d 2765 const frame_info_ptr &frame,
86542ab5 2766 bool top_level)
80180f79
SA
2767{
2768 struct type *real_type = check_typedef (type);
f8e89861 2769 struct type *resolved_type = nullptr;
d9823cbb 2770 struct dynamic_prop *prop;
3cdcd0ce 2771 CORE_ADDR value;
80180f79 2772
ee715b5a 2773 if (!is_dynamic_type_internal (real_type, top_level))
80180f79
SA
2774 return type;
2775
6b09f134 2776 std::optional<CORE_ADDR> type_length;
f8e89861
TT
2777 prop = TYPE_DYNAMIC_LENGTH (type);
2778 if (prop != NULL
aeabe83d 2779 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
f8e89861
TT
2780 type_length = value;
2781
78134374 2782 if (type->code () == TYPE_CODE_TYPEDEF)
6f8a3220 2783 {
cac9b138 2784 resolved_type = copy_type (type);
8a50fdce 2785 resolved_type->set_target_type
27710edb 2786 (resolve_dynamic_type_internal (type->target_type (), addr_stack,
aeabe83d 2787 frame, top_level));
5537b577 2788 }
8a50fdce 2789 else
5537b577
JK
2790 {
2791 /* Before trying to resolve TYPE, make sure it is not a stub. */
2792 type = real_type;
012370f6 2793
78134374 2794 switch (type->code ())
5537b577 2795 {
e771e4be 2796 case TYPE_CODE_REF:
f18fc7e5
BH
2797 case TYPE_CODE_PTR:
2798 case TYPE_CODE_RVALUE_REF:
e771e4be
PMR
2799 {
2800 struct property_addr_info pinfo;
2801
27710edb 2802 pinfo.type = check_typedef (type->target_type ());
b249d2c2
TT
2803 pinfo.valaddr = {};
2804 if (addr_stack->valaddr.data () != NULL)
2805 pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
2806 type);
c3345124
JB
2807 else
2808 pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
e771e4be
PMR
2809 pinfo.next = addr_stack;
2810
3a3f1548
TT
2811 /* Special case a NULL pointer here -- we don't want to
2812 dereference it. */
2813 if (pinfo.addr != 0)
2814 {
2815 resolved_type = copy_type (type);
2816 resolved_type->set_target_type
2817 (resolve_dynamic_type_internal (type->target_type (),
2818 &pinfo, frame, true));
2819 }
e771e4be
PMR
2820 break;
2821 }
2822
216a7e6b
AB
2823 case TYPE_CODE_STRING:
2824 /* Strings are very much like an array of characters, and can be
2825 treated as one here. */
5537b577 2826 case TYPE_CODE_ARRAY:
aeabe83d
TT
2827 resolved_type = resolve_dynamic_array_or_string (type, addr_stack,
2828 frame);
5537b577
JK
2829 break;
2830
2831 case TYPE_CODE_RANGE:
3fb842ce
AB
2832 /* Pass 0 for the rank value here, which indicates this is a
2833 range for the first rank of an array. The assumption is that
2834 this rank value is not actually required for the resolution of
2835 the dynamic range, otherwise, we'd be resolving this range
2836 within the context of a dynamic array. */
aeabe83d 2837 resolved_type = resolve_dynamic_range (type, addr_stack, frame, 0);
5537b577
JK
2838 break;
2839
2840 case TYPE_CODE_UNION:
aeabe83d 2841 resolved_type = resolve_dynamic_union (type, addr_stack, frame);
5537b577
JK
2842 break;
2843
2844 case TYPE_CODE_STRUCT:
aeabe83d 2845 resolved_type = resolve_dynamic_struct (type, addr_stack, frame);
5537b577
JK
2846 break;
2847 }
6f8a3220 2848 }
80180f79 2849
f8e89861
TT
2850 if (resolved_type == nullptr)
2851 return type;
2852
2853 if (type_length.has_value ())
2854 {
b6cdbc9a 2855 resolved_type->set_length (*type_length);
7aa91313 2856 resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
f8e89861
TT
2857 }
2858
3cdcd0ce
JB
2859 /* Resolve data_location attribute. */
2860 prop = TYPE_DATA_LOCATION (resolved_type);
63e43d3a 2861 if (prop != NULL
aeabe83d 2862 && dwarf2_evaluate_property (prop, frame, addr_stack, &value))
a5c641b5
AB
2863 {
2864 /* Start of Fortran hack. See comment in f-lang.h for what is going
2865 on here.*/
2866 if (current_language->la_language == language_fortran
2867 && resolved_type->code () == TYPE_CODE_ARRAY)
2868 value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
2869 value);
2870 /* End of Fortran hack. */
2871 prop->set_const_val (value);
2872 }
3cdcd0ce 2873
80180f79
SA
2874 return resolved_type;
2875}
2876
d98b7a16
TT
2877/* See gdbtypes.h */
2878
2879struct type *
b249d2c2
TT
2880resolve_dynamic_type (struct type *type,
2881 gdb::array_view<const gdb_byte> valaddr,
aeabe83d
TT
2882 CORE_ADDR addr,
2883 const frame_info_ptr *in_frame)
d98b7a16 2884{
c3345124
JB
2885 struct property_addr_info pinfo
2886 = {check_typedef (type), valaddr, addr, NULL};
df25ebbd 2887
aeabe83d
TT
2888 frame_info_ptr frame;
2889 if (in_frame != nullptr)
2890 frame = *in_frame;
2891
86542ab5 2892 return resolve_dynamic_type_internal (type, &pinfo, frame, true);
d98b7a16
TT
2893}
2894
d9823cbb
KB
2895/* See gdbtypes.h */
2896
24e99c6c
SM
2897dynamic_prop *
2898type::dyn_prop (dynamic_prop_node_kind prop_kind) const
d9823cbb 2899{
98d48915 2900 dynamic_prop_list *node = this->main_type->dyn_prop_list;
d9823cbb
KB
2901
2902 while (node != NULL)
2903 {
2904 if (node->prop_kind == prop_kind)
dda83cd7 2905 return &node->prop;
d9823cbb
KB
2906 node = node->next;
2907 }
2908 return NULL;
2909}
2910
2911/* See gdbtypes.h */
2912
2913void
5c54719c 2914type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
d9823cbb
KB
2915{
2916 struct dynamic_prop_list *temp;
2917
30625020 2918 gdb_assert (this->is_objfile_owned ());
d9823cbb 2919
6ac37371 2920 temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
50a82047 2921 struct dynamic_prop_list);
d9823cbb 2922 temp->prop_kind = prop_kind;
283a9958 2923 temp->prop = prop;
98d48915 2924 temp->next = this->main_type->dyn_prop_list;
d9823cbb 2925
98d48915 2926 this->main_type->dyn_prop_list = temp;
d9823cbb
KB
2927}
2928
7aa91313 2929/* See gdbtypes.h. */
9920b434
BH
2930
2931void
7aa91313 2932type::remove_dyn_prop (dynamic_prop_node_kind kind)
9920b434
BH
2933{
2934 struct dynamic_prop_list *prev_node, *curr_node;
2935
98d48915 2936 curr_node = this->main_type->dyn_prop_list;
9920b434
BH
2937 prev_node = NULL;
2938
2939 while (NULL != curr_node)
2940 {
7aa91313 2941 if (curr_node->prop_kind == kind)
9920b434
BH
2942 {
2943 /* Update the linked list but don't free anything.
49ba125f 2944 The property was allocated on obstack and it is not known
9920b434 2945 if we are on top of it. Nevertheless, everything is released
49ba125f 2946 when the complete obstack is freed. */
9920b434 2947 if (NULL == prev_node)
98d48915 2948 this->main_type->dyn_prop_list = curr_node->next;
9920b434
BH
2949 else
2950 prev_node->next = curr_node->next;
2951
2952 return;
2953 }
2954
2955 prev_node = curr_node;
2956 curr_node = curr_node->next;
2957 }
2958}
d9823cbb 2959
92163a10
JK
2960/* Find the real type of TYPE. This function returns the real type,
2961 after removing all layers of typedefs, and completing opaque or stub
2962 types. Completion changes the TYPE argument, but stripping of
2963 typedefs does not.
2964
2965 Instance flags (e.g. const/volatile) are preserved as typedefs are
2966 stripped. If necessary a new qualified form of the underlying type
2967 is created.
2968
27710edb 2969 NOTE: This will return a typedef if type::target_type for the typedef has
92163a10
JK
2970 not been computed and we're either in the middle of reading symbols, or
2971 there was no name for the typedef in the debug info.
2972
9bc118a5
DE
2973 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
2974 QUITs in the symbol reading code can also throw.
2975 Thus this function can throw an exception.
2976
92163a10
JK
2977 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
2978 the target type.
c906108c
SS
2979
2980 If this is a stubbed struct (i.e. declared as struct foo *), see if
0963b4bd 2981 we can find a full definition in some other file. If so, copy this
7ba81444
MS
2982 definition, so we can use it in future. There used to be a comment
2983 (but not any code) that if we don't find a full definition, we'd
2984 set a flag so we don't spend time in the future checking the same
2985 type. That would be a mistake, though--we might load in more
92163a10 2986 symbols which contain a full definition for the type. */
c906108c
SS
2987
2988struct type *
a02fd225 2989check_typedef (struct type *type)
c906108c
SS
2990{
2991 struct type *orig_type = type;
a02fd225 2992
423c0af8
MS
2993 gdb_assert (type);
2994
314ad88d
PA
2995 /* While we're removing typedefs, we don't want to lose qualifiers.
2996 E.g., const/volatile. */
2997 type_instance_flags instance_flags = type->instance_flags ();
2998
78134374 2999 while (type->code () == TYPE_CODE_TYPEDEF)
c906108c 3000 {
27710edb 3001 if (!type->target_type ())
c906108c 3002 {
0d5cff50 3003 const char *name;
c906108c
SS
3004 struct symbol *sym;
3005
3006 /* It is dangerous to call lookup_symbol if we are currently
7ba81444 3007 reading a symtab. Infinite recursion is one danger. */
c906108c 3008 if (currently_reading_symtab)
92163a10 3009 return make_qualified_type (type, instance_flags, NULL);
c906108c 3010
7d93a1e0 3011 name = type->name ();
c906108c
SS
3012 if (name == NULL)
3013 {
23136709 3014 stub_noname_complaint ();
92163a10 3015 return make_qualified_type (type, instance_flags, NULL);
c906108c 3016 }
974b36c2
TT
3017 domain_search_flag flag
3018 = ((type->language () == language_c
3019 || type->language () == language_objc
3020 || type->language () == language_opencl
3021 || type->language () == language_minimal)
3022 ? SEARCH_STRUCT_DOMAIN
3023 : SEARCH_TYPE_DOMAIN);
3024 sym = lookup_symbol (name, nullptr, flag, nullptr).symbol;
c906108c 3025 if (sym)
8a50fdce 3026 type->set_target_type (sym->type ());
7ba81444 3027 else /* TYPE_CODE_UNDEF */
c9eb9f18 3028 type->set_target_type (type_allocator (type->arch ()).new_type ());
c906108c 3029 }
27710edb 3030 type = type->target_type ();
c906108c 3031
92163a10
JK
3032 /* Preserve the instance flags as we traverse down the typedef chain.
3033
3034 Handling address spaces/classes is nasty, what do we do if there's a
3035 conflict?
3036 E.g., what if an outer typedef marks the type as class_1 and an inner
3037 typedef marks the type as class_2?
3038 This is the wrong place to do such error checking. We leave it to
3039 the code that created the typedef in the first place to flag the
3040 error. We just pick the outer address space (akin to letting the
3041 outer cast in a chain of casting win), instead of assuming
3042 "it can't happen". */
3043 {
314ad88d
PA
3044 const type_instance_flags ALL_SPACES
3045 = (TYPE_INSTANCE_FLAG_CODE_SPACE
3046 | TYPE_INSTANCE_FLAG_DATA_SPACE);
3047 const type_instance_flags ALL_CLASSES
3048 = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
3049
3050 type_instance_flags new_instance_flags = type->instance_flags ();
92163a10
JK
3051
3052 /* Treat code vs data spaces and address classes separately. */
3053 if ((instance_flags & ALL_SPACES) != 0)
3054 new_instance_flags &= ~ALL_SPACES;
3055 if ((instance_flags & ALL_CLASSES) != 0)
3056 new_instance_flags &= ~ALL_CLASSES;
3057
3058 instance_flags |= new_instance_flags;
3059 }
3060 }
a02fd225 3061
7ba81444
MS
3062 /* If this is a struct/class/union with no fields, then check
3063 whether a full definition exists somewhere else. This is for
3064 systems where a type definition with no fields is issued for such
3065 types, instead of identifying them as stub types in the first
3066 place. */
c5aa993b 3067
7ba81444
MS
3068 if (TYPE_IS_OPAQUE (type)
3069 && opaque_type_resolution
3070 && !currently_reading_symtab)
c906108c 3071 {
7d93a1e0 3072 const char *name = type->name ();
c5aa993b 3073 struct type *newtype;
d8734c88 3074
c906108c
SS
3075 if (name == NULL)
3076 {
23136709 3077 stub_noname_complaint ();
92163a10 3078 return make_qualified_type (type, instance_flags, NULL);
c906108c
SS
3079 }
3080 newtype = lookup_transparent_type (name);
ad766c0a 3081
c906108c 3082 if (newtype)
ad766c0a 3083 {
7ba81444
MS
3084 /* If the resolved type and the stub are in the same
3085 objfile, then replace the stub type with the real deal.
3086 But if they're in separate objfiles, leave the stub
3087 alone; we'll just look up the transparent type every time
3088 we call check_typedef. We can't create pointers between
3089 types allocated to different objfiles, since they may
3090 have different lifetimes. Trying to copy NEWTYPE over to
3091 TYPE's objfile is pointless, too, since you'll have to
3092 move over any other types NEWTYPE refers to, which could
3093 be an unbounded amount of stuff. */
6ac37371 3094 if (newtype->objfile_owner () == type->objfile_owner ())
10242f36 3095 type = make_qualified_type (newtype, type->instance_flags (), type);
ad766c0a
JB
3096 else
3097 type = newtype;
3098 }
c906108c 3099 }
7ba81444
MS
3100 /* Otherwise, rely on the stub flag being set for opaque/stubbed
3101 types. */
e46d3488 3102 else if (type->is_stub () && !currently_reading_symtab)
c906108c 3103 {
7d93a1e0 3104 const char *name = type->name ();
c906108c 3105 struct symbol *sym;
d8734c88 3106
c906108c
SS
3107 if (name == NULL)
3108 {
23136709 3109 stub_noname_complaint ();
92163a10 3110 return make_qualified_type (type, instance_flags, NULL);
c906108c 3111 }
974b36c2
TT
3112 domain_search_flag flag
3113 = ((type->language () == language_c
3114 || type->language () == language_objc
3115 || type->language () == language_opencl
3116 || type->language () == language_minimal)
3117 ? SEARCH_STRUCT_DOMAIN
3118 : SEARCH_TYPE_DOMAIN);
3119 sym = lookup_symbol (name, nullptr, flag, nullptr).symbol;
c906108c 3120 if (sym)
dda83cd7
SM
3121 {
3122 /* Same as above for opaque types, we can replace the stub
3123 with the complete type only if they are in the same
3124 objfile. */
5f9c5a63
SM
3125 if (sym->type ()->objfile_owner () == type->objfile_owner ())
3126 type = make_qualified_type (sym->type (),
10242f36 3127 type->instance_flags (), type);
c26f2453 3128 else
5f9c5a63 3129 type = sym->type ();
dda83cd7 3130 }
c906108c
SS
3131 }
3132
d2183968 3133 if (type->target_is_stub ())
c906108c 3134 {
27710edb 3135 struct type *target_type = check_typedef (type->target_type ());
c906108c 3136
d2183968 3137 if (target_type->is_stub () || target_type->target_is_stub ())
c5aa993b 3138 {
73e2eb35 3139 /* Nothing we can do. */
c5aa993b 3140 }
78134374 3141 else if (type->code () == TYPE_CODE_RANGE)
c906108c 3142 {
df86565b 3143 type->set_length (target_type->length ());
8f53807e 3144 type->set_target_is_stub (false);
c906108c 3145 }
78134374 3146 else if (type->code () == TYPE_CODE_ARRAY
8dbb1375 3147 && update_static_array_size (type))
8f53807e 3148 type->set_target_is_stub (false);
c906108c 3149 }
92163a10
JK
3150
3151 type = make_qualified_type (type, instance_flags, NULL);
3152
7ba81444 3153 /* Cache TYPE_LENGTH for future use. */
df86565b 3154 orig_type->set_length (type->length ());
92163a10 3155
c906108c
SS
3156 return type;
3157}
3158
7ba81444 3159/* Parse a type expression in the string [P..P+LENGTH). If an error
48319d1f 3160 occurs, silently return a void type. */
c91ecb25 3161
b9362cc7 3162static struct type *
f5756acc 3163safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
c91ecb25 3164{
34365054 3165 struct type *type = NULL; /* Initialize to keep gcc happy. */
c91ecb25 3166
7ba81444 3167 /* Suppress error messages. */
c3d4b6a6
TT
3168 scoped_restore saved_gdb_stderr = make_scoped_restore (&gdb_stderr,
3169 &null_stream);
c91ecb25 3170
7ba81444 3171 /* Call parse_and_eval_type() without fear of longjmp()s. */
a70b8144 3172 try
8e7b59a5
KS
3173 {
3174 type = parse_and_eval_type (p, length);
3175 }
230d2906 3176 catch (const gdb_exception_error &except)
492d29ea
PA
3177 {
3178 type = builtin_type (gdbarch)->builtin_void;
3179 }
c91ecb25 3180
c91ecb25
ND
3181 return type;
3182}
3183
c906108c
SS
3184/* Ugly hack to convert method stubs into method types.
3185
7ba81444
MS
3186 He ain't kiddin'. This demangles the name of the method into a
3187 string including argument types, parses out each argument type,
3188 generates a string casting a zero to that type, evaluates the
3189 string, and stuffs the resulting type into an argtype vector!!!
3190 Then it knows the type of the whole function (including argument
3191 types for overloading), which info used to be in the stab's but was
3192 removed to hack back the space required for them. */
c906108c 3193
de17c821 3194static void
fba45db2 3195check_stub_method (struct type *type, int method_id, int signature_id)
c906108c 3196{
8ee511af 3197 struct gdbarch *gdbarch = type->arch ();
c906108c
SS
3198 struct fn_field *f;
3199 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
3456e70c
TT
3200 gdb::unique_xmalloc_ptr<char> demangled_name
3201 = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
c906108c
SS
3202 char *argtypetext, *p;
3203 int depth = 0, argcount = 1;
ad2f7632 3204 struct field *argtypes;
c906108c
SS
3205 struct type *mtype;
3206
3207 /* Make sure we got back a function string that we can use. */
3208 if (demangled_name)
3456e70c 3209 p = strchr (demangled_name.get (), '(');
502dcf4e
AC
3210 else
3211 p = NULL;
c906108c
SS
3212
3213 if (demangled_name == NULL || p == NULL)
7ba81444
MS
3214 error (_("Internal: Cannot demangle mangled name `%s'."),
3215 mangled_name);
c906108c
SS
3216
3217 /* Now, read in the parameters that define this type. */
3218 p += 1;
3219 argtypetext = p;
3220 while (*p)
3221 {
070ad9f0 3222 if (*p == '(' || *p == '<')
c906108c
SS
3223 {
3224 depth += 1;
3225 }
070ad9f0 3226 else if (*p == ')' || *p == '>')
c906108c
SS
3227 {
3228 depth -= 1;
3229 }
3230 else if (*p == ',' && depth == 0)
3231 {
3232 argcount += 1;
3233 }
3234
3235 p += 1;
3236 }
3237
ad2f7632 3238 /* If we read one argument and it was ``void'', don't count it. */
61012eef 3239 if (startswith (argtypetext, "(void)"))
ad2f7632 3240 argcount -= 1;
c906108c 3241
ad2f7632
DJ
3242 /* We need one extra slot, for the THIS pointer. */
3243
3244 argtypes = (struct field *)
959db212 3245 TYPE_ZALLOC (type, (argcount + 1) * sizeof (struct field));
c906108c 3246 p = argtypetext;
4a1970e4
DJ
3247
3248 /* Add THIS pointer for non-static methods. */
3249 f = TYPE_FN_FIELDLIST1 (type, method_id);
3250 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
3251 argcount = 0;
3252 else
3253 {
5d14b6e5 3254 argtypes[0].set_type (lookup_pointer_type (type));
4a1970e4
DJ
3255 argcount = 1;
3256 }
c906108c 3257
0963b4bd 3258 if (*p != ')') /* () means no args, skip while. */
c906108c
SS
3259 {
3260 depth = 0;
3261 while (*p)
3262 {
3263 if (depth <= 0 && (*p == ',' || *p == ')'))
3264 {
ad2f7632 3265 /* Avoid parsing of ellipsis, they will be handled below.
dda83cd7 3266 Also avoid ``void'' as above. */
ad2f7632
DJ
3267 if (strncmp (argtypetext, "...", p - argtypetext) != 0
3268 && strncmp (argtypetext, "void", p - argtypetext) != 0)
c906108c 3269 {
5d14b6e5
SM
3270 argtypes[argcount].set_type
3271 (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
c906108c
SS
3272 argcount += 1;
3273 }
3274 argtypetext = p + 1;
3275 }
3276
070ad9f0 3277 if (*p == '(' || *p == '<')
c906108c
SS
3278 {
3279 depth += 1;
3280 }
070ad9f0 3281 else if (*p == ')' || *p == '>')
c906108c
SS
3282 {
3283 depth -= 1;
3284 }
3285
3286 p += 1;
3287 }
3288 }
3289
c906108c
SS
3290 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
3291
3292 /* Now update the old "stub" type into a real type. */
3293 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
09e2d7c7
DE
3294 /* MTYPE may currently be a function (TYPE_CODE_FUNC).
3295 We want a method (TYPE_CODE_METHOD). */
27710edb 3296 smash_to_method_type (mtype, type, mtype->target_type (),
09e2d7c7 3297 argtypes, argcount, p[-2] == '.');
b4b73759 3298 mtype->set_is_stub (false);
c906108c
SS
3299 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
3300}
3301
7ba81444
MS
3302/* This is the external interface to check_stub_method, above. This
3303 function unstubs all of the signatures for TYPE's METHOD_ID method
3304 name. After calling this function TYPE_FN_FIELD_STUB will be
3305 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
3306 correct.
de17c821
DJ
3307
3308 This function unfortunately can not die until stabs do. */
3309
3310void
3311check_stub_method_group (struct type *type, int method_id)
3312{
3313 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
3314 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
de17c821 3315
041be526
SM
3316 for (int j = 0; j < len; j++)
3317 {
3318 if (TYPE_FN_FIELD_STUB (f, j))
de17c821 3319 check_stub_method (type, method_id, j);
de17c821
DJ
3320 }
3321}
3322
405feb71 3323/* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
9655fd1a 3324const struct cplus_struct_type cplus_struct_default = { };
c906108c
SS
3325
3326void
fba45db2 3327allocate_cplus_struct_type (struct type *type)
c906108c 3328{
b4ba55a1
JB
3329 if (HAVE_CPLUS_STRUCT (type))
3330 /* Structure was already allocated. Nothing more to do. */
3331 return;
3332
3333 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
3334 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
959db212 3335 TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
b4ba55a1 3336 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
ae6ae975 3337 set_type_vptr_fieldno (type, -1);
c906108c
SS
3338}
3339
b4ba55a1
JB
3340const struct gnat_aux_type gnat_aux_default =
3341 { NULL };
3342
3343/* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
3344 and allocate the associated gnat-specific data. The gnat-specific
3345 data is also initialized to gnat_aux_default. */
5212577a 3346
b4ba55a1
JB
3347void
3348allocate_gnat_aux_type (struct type *type)
3349{
3350 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
3351 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
959db212 3352 TYPE_ZALLOC (type, sizeof (struct gnat_aux_type));
b4ba55a1
JB
3353 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
3354}
3355
19f392bc
UW
3356/* Helper function to verify floating-point format and size.
3357 BIT is the type size in bits; if BIT equals -1, the size is
3358 determined by the floatformat. Returns size to be used. */
3359
3360static int
0db7851f 3361verify_floatformat (int bit, const struct floatformat *floatformat)
19f392bc 3362{
0db7851f 3363 gdb_assert (floatformat != NULL);
9b790ce7 3364
19f392bc 3365 if (bit == -1)
0db7851f 3366 bit = floatformat->totalsize;
19f392bc 3367
0db7851f
UW
3368 gdb_assert (bit >= 0);
3369 gdb_assert (bit >= floatformat->totalsize);
19f392bc
UW
3370
3371 return bit;
3372}
3373
0db7851f
UW
3374/* Return the floating-point format for a floating-point variable of
3375 type TYPE. */
3376
3377const struct floatformat *
3378floatformat_from_type (const struct type *type)
3379{
78134374 3380 gdb_assert (type->code () == TYPE_CODE_FLT);
0db7851f
UW
3381 gdb_assert (TYPE_FLOATFORMAT (type));
3382 return TYPE_FLOATFORMAT (type);
3383}
3384
2d39ccd3 3385/* See gdbtypes.h. */
19f392bc
UW
3386
3387struct type *
2d39ccd3 3388init_integer_type (type_allocator &alloc,
19f392bc
UW
3389 int bit, int unsigned_p, const char *name)
3390{
3391 struct type *t;
3392
2d39ccd3 3393 t = alloc.new_type (TYPE_CODE_INT, bit, name);
19f392bc 3394 if (unsigned_p)
653223d3 3395 t->set_is_unsigned (true);
19f392bc 3396
20a5fcbd
TT
3397 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3398 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3399 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3400
19f392bc
UW
3401 return t;
3402}
3403
f50b437c 3404/* See gdbtypes.h. */
19f392bc
UW
3405
3406struct type *
f50b437c 3407init_character_type (type_allocator &alloc,
19f392bc
UW
3408 int bit, int unsigned_p, const char *name)
3409{
3410 struct type *t;
3411
f50b437c 3412 t = alloc.new_type (TYPE_CODE_CHAR, bit, name);
19f392bc 3413 if (unsigned_p)
653223d3 3414 t->set_is_unsigned (true);
19f392bc
UW
3415
3416 return t;
3417}
3418
46c04ea3 3419/* See gdbtypes.h. */
19f392bc
UW
3420
3421struct type *
46c04ea3 3422init_boolean_type (type_allocator &alloc,
19f392bc
UW
3423 int bit, int unsigned_p, const char *name)
3424{
3425 struct type *t;
3426
46c04ea3 3427 t = alloc.new_type (TYPE_CODE_BOOL, bit, name);
19f392bc 3428 if (unsigned_p)
653223d3 3429 t->set_is_unsigned (true);
19f392bc 3430
20a5fcbd
TT
3431 TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
3432 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
3433 TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
3434
19f392bc
UW
3435 return t;
3436}
3437
77c5f496 3438/* See gdbtypes.h. */
19f392bc
UW
3439
3440struct type *
77c5f496 3441init_float_type (type_allocator &alloc,
19f392bc 3442 int bit, const char *name,
103a685e
TT
3443 const struct floatformat **floatformats,
3444 enum bfd_endian byte_order)
19f392bc 3445{
103a685e
TT
3446 if (byte_order == BFD_ENDIAN_UNKNOWN)
3447 {
77c5f496 3448 struct gdbarch *gdbarch = alloc.arch ();
103a685e
TT
3449 byte_order = gdbarch_byte_order (gdbarch);
3450 }
3451 const struct floatformat *fmt = floatformats[byte_order];
19f392bc
UW
3452 struct type *t;
3453
0db7851f 3454 bit = verify_floatformat (bit, fmt);
77c5f496 3455 t = alloc.new_type (TYPE_CODE_FLT, bit, name);
0db7851f 3456 TYPE_FLOATFORMAT (t) = fmt;
19f392bc
UW
3457
3458 return t;
3459}
3460
0776344a 3461/* See gdbtypes.h. */
19f392bc
UW
3462
3463struct type *
0776344a 3464init_decfloat_type (type_allocator &alloc, int bit, const char *name)
19f392bc 3465{
0776344a 3466 return alloc.new_type (TYPE_CODE_DECFLOAT, bit, name);
19f392bc
UW
3467}
3468
ae710496
TV
3469/* Return true if init_complex_type can be called with TARGET_TYPE. */
3470
3471bool
3472can_create_complex_type (struct type *target_type)
3473{
3474 return (target_type->code () == TYPE_CODE_INT
3475 || target_type->code () == TYPE_CODE_FLT);
3476}
3477
5b930b45
TT
3478/* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
3479 name. TARGET_TYPE is the component type. */
19f392bc
UW
3480
3481struct type *
5b930b45 3482init_complex_type (const char *name, struct type *target_type)
19f392bc
UW
3483{
3484 struct type *t;
3485
ae710496 3486 gdb_assert (can_create_complex_type (target_type));
5b930b45
TT
3487
3488 if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
3489 {
6b9d0dfd 3490 if (name == nullptr && target_type->name () != nullptr)
5b930b45 3491 {
959db212
TV
3492 /* No zero-initialization required, initialized by strcpy/strcat
3493 below. */
5b930b45
TT
3494 char *new_name
3495 = (char *) TYPE_ALLOC (target_type,
7d93a1e0 3496 strlen (target_type->name ())
5b930b45
TT
3497 + strlen ("_Complex ") + 1);
3498 strcpy (new_name, "_Complex ");
7d93a1e0 3499 strcat (new_name, target_type->name ());
5b930b45
TT
3500 name = new_name;
3501 }
3502
9fa83a7a 3503 t = type_allocator (target_type).new_type ();
5b930b45 3504 set_type_code (t, TYPE_CODE_COMPLEX);
df86565b 3505 t->set_length (2 * target_type->length ());
d0e39ea2 3506 t->set_name (name);
5b930b45 3507
8a50fdce 3508 t->set_target_type (target_type);
5b930b45
TT
3509 TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
3510 }
3511
3512 return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
19f392bc
UW
3513}
3514
9c794d2d 3515/* See gdbtypes.h. */
19f392bc
UW
3516
3517struct type *
9c794d2d 3518init_pointer_type (type_allocator &alloc,
19f392bc
UW
3519 int bit, const char *name, struct type *target_type)
3520{
3521 struct type *t;
3522
9c794d2d 3523 t = alloc.new_type (TYPE_CODE_PTR, bit, name);
8a50fdce 3524 t->set_target_type (target_type);
653223d3 3525 t->set_is_unsigned (true);
19f392bc
UW
3526 return t;
3527}
3528
09584414
JB
3529/* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
3530 BIT is the pointer type size in bits.
3531 UNSIGNED_P should be nonzero if the type is unsigned.
3532 NAME is the type name. */
3533
3534struct type *
d5592272 3535init_fixed_point_type (type_allocator &alloc,
09584414
JB
3536 int bit, int unsigned_p, const char *name)
3537{
3538 struct type *t;
3539
d5592272 3540 t = alloc.new_type (TYPE_CODE_FIXED_POINT, bit, name);
09584414
JB
3541 if (unsigned_p)
3542 t->set_is_unsigned (true);
3543
3544 return t;
3545}
3546
2b4424c3
TT
3547/* See gdbtypes.h. */
3548
3549unsigned
3550type_raw_align (struct type *type)
3551{
3552 if (type->align_log2 != 0)
3553 return 1 << (type->align_log2 - 1);
3554 return 0;
3555}
3556
3557/* See gdbtypes.h. */
3558
3559unsigned
3560type_align (struct type *type)
3561{
5561fc30 3562 /* Check alignment provided in the debug information. */
2b4424c3
TT
3563 unsigned raw_align = type_raw_align (type);
3564 if (raw_align != 0)
3565 return raw_align;
3566
5561fc30 3567 /* Allow the architecture to provide an alignment. */
8ee511af 3568 ULONGEST align = gdbarch_type_align (type->arch (), type);
5561fc30
AB
3569 if (align != 0)
3570 return align;
3571
78134374 3572 switch (type->code ())
2b4424c3
TT
3573 {
3574 case TYPE_CODE_PTR:
3575 case TYPE_CODE_FUNC:
3576 case TYPE_CODE_FLAGS:
3577 case TYPE_CODE_INT:
75ba10dc 3578 case TYPE_CODE_RANGE:
2b4424c3
TT
3579 case TYPE_CODE_FLT:
3580 case TYPE_CODE_ENUM:
3581 case TYPE_CODE_REF:
3582 case TYPE_CODE_RVALUE_REF:
3583 case TYPE_CODE_CHAR:
3584 case TYPE_CODE_BOOL:
3585 case TYPE_CODE_DECFLOAT:
70cd633e
AB
3586 case TYPE_CODE_METHODPTR:
3587 case TYPE_CODE_MEMBERPTR:
5561fc30 3588 align = type_length_units (check_typedef (type));
2b4424c3
TT
3589 break;
3590
3591 case TYPE_CODE_ARRAY:
3592 case TYPE_CODE_COMPLEX:
3593 case TYPE_CODE_TYPEDEF:
27710edb 3594 align = type_align (type->target_type ());
2b4424c3
TT
3595 break;
3596
3597 case TYPE_CODE_STRUCT:
3598 case TYPE_CODE_UNION:
3599 {
41077b66 3600 int number_of_non_static_fields = 0;
1f704f76 3601 for (unsigned i = 0; i < type->num_fields (); ++i)
2b4424c3 3602 {
c819a338 3603 if (!type->field (i).is_static ())
2b4424c3 3604 {
41077b66 3605 number_of_non_static_fields++;
940da03e 3606 ULONGEST f_align = type_align (type->field (i).type ());
bf9a735e
AB
3607 if (f_align == 0)
3608 {
3609 /* Don't pretend we know something we don't. */
3610 align = 0;
3611 break;
3612 }
3613 if (f_align > align)
3614 align = f_align;
2b4424c3 3615 }
2b4424c3 3616 }
41077b66
AB
3617 /* A struct with no fields, or with only static fields has an
3618 alignment of 1. */
3619 if (number_of_non_static_fields == 0)
3620 align = 1;
2b4424c3
TT
3621 }
3622 break;
3623
3624 case TYPE_CODE_SET:
2b4424c3
TT
3625 case TYPE_CODE_STRING:
3626 /* Not sure what to do here, and these can't appear in C or C++
3627 anyway. */
3628 break;
3629
2b4424c3
TT
3630 case TYPE_CODE_VOID:
3631 align = 1;
3632 break;
3633
3634 case TYPE_CODE_ERROR:
3635 case TYPE_CODE_METHOD:
3636 default:
3637 break;
3638 }
3639
3640 if ((align & (align - 1)) != 0)
3641 {
3642 /* Not a power of 2, so pass. */
3643 align = 0;
3644 }
3645
3646 return align;
3647}
3648
3649/* See gdbtypes.h. */
3650
3651bool
3652set_type_align (struct type *type, ULONGEST align)
3653{
3654 /* Must be a power of 2. Zero is ok. */
3655 gdb_assert ((align & (align - 1)) == 0);
3656
3657 unsigned result = 0;
3658 while (align != 0)
3659 {
3660 ++result;
3661 align >>= 1;
3662 }
3663
3664 if (result >= (1 << TYPE_ALIGN_BITS))
3665 return false;
3666
3667 type->align_log2 = result;
3668 return true;
3669}
3670
5212577a
DE
3671\f
3672/* Queries on types. */
c906108c 3673
c906108c 3674int
fba45db2 3675can_dereference (struct type *t)
c906108c 3676{
7ba81444
MS
3677 /* FIXME: Should we return true for references as well as
3678 pointers? */
f168693b 3679 t = check_typedef (t);
c906108c
SS
3680 return
3681 (t != NULL
78134374 3682 && t->code () == TYPE_CODE_PTR
27710edb 3683 && t->target_type ()->code () != TYPE_CODE_VOID);
c906108c
SS
3684}
3685
adf40b2e 3686int
fba45db2 3687is_integral_type (struct type *t)
adf40b2e 3688{
f168693b 3689 t = check_typedef (t);
adf40b2e
JM
3690 return
3691 ((t != NULL)
09584414 3692 && !is_fixed_point_type (t)
78134374
SM
3693 && ((t->code () == TYPE_CODE_INT)
3694 || (t->code () == TYPE_CODE_ENUM)
3695 || (t->code () == TYPE_CODE_FLAGS)
3696 || (t->code () == TYPE_CODE_CHAR)
3697 || (t->code () == TYPE_CODE_RANGE)
3698 || (t->code () == TYPE_CODE_BOOL)));
adf40b2e
JM
3699}
3700
70100014
UW
3701int
3702is_floating_type (struct type *t)
3703{
3704 t = check_typedef (t);
3705 return
3706 ((t != NULL)
78134374
SM
3707 && ((t->code () == TYPE_CODE_FLT)
3708 || (t->code () == TYPE_CODE_DECFLOAT)));
70100014
UW
3709}
3710
e09342b5
TJB
3711/* Return true if TYPE is scalar. */
3712
220475ed 3713int
e09342b5
TJB
3714is_scalar_type (struct type *type)
3715{
f168693b 3716 type = check_typedef (type);
e09342b5 3717
09584414
JB
3718 if (is_fixed_point_type (type))
3719 return 0; /* Implemented as a scalar, but more like a floating point. */
3720
78134374 3721 switch (type->code ())
e09342b5
TJB
3722 {
3723 case TYPE_CODE_ARRAY:
3724 case TYPE_CODE_STRUCT:
3725 case TYPE_CODE_UNION:
3726 case TYPE_CODE_SET:
3727 case TYPE_CODE_STRING:
e09342b5
TJB
3728 return 0;
3729 default:
3730 return 1;
3731 }
3732}
3733
3734/* Return true if T is scalar, or a composite type which in practice has
90e4670f
TJB
3735 the memory layout of a scalar type. E.g., an array or struct with only
3736 one scalar element inside it, or a union with only scalar elements. */
e09342b5
TJB
3737
3738int
3739is_scalar_type_recursive (struct type *t)
3740{
f168693b 3741 t = check_typedef (t);
e09342b5
TJB
3742
3743 if (is_scalar_type (t))
3744 return 1;
3745 /* Are we dealing with an array or string of known dimensions? */
78134374 3746 else if ((t->code () == TYPE_CODE_ARRAY
1f704f76 3747 || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
3d967001 3748 && t->index_type ()->code () == TYPE_CODE_RANGE)
e09342b5
TJB
3749 {
3750 LONGEST low_bound, high_bound;
27710edb 3751 struct type *elt_type = check_typedef (t->target_type ());
e09342b5 3752
f8676776
LS
3753 if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
3754 return (high_bound == low_bound
3755 && is_scalar_type_recursive (elt_type));
3756 else
3757 return 0;
e09342b5
TJB
3758 }
3759 /* Are we dealing with a struct with one element? */
1f704f76 3760 else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
940da03e 3761 return is_scalar_type_recursive (t->field (0).type ());
78134374 3762 else if (t->code () == TYPE_CODE_UNION)
e09342b5 3763 {
1f704f76 3764 int i, n = t->num_fields ();
e09342b5
TJB
3765
3766 /* If all elements of the union are scalar, then the union is scalar. */
3767 for (i = 0; i < n; i++)
940da03e 3768 if (!is_scalar_type_recursive (t->field (i).type ()))
e09342b5
TJB
3769 return 0;
3770
3771 return 1;
3772 }
3773
3774 return 0;
3775}
3776
6c659fc2
SC
3777/* Return true is T is a class or a union. False otherwise. */
3778
3779int
3780class_or_union_p (const struct type *t)
3781{
78134374 3782 return (t->code () == TYPE_CODE_STRUCT
dda83cd7 3783 || t->code () == TYPE_CODE_UNION);
6c659fc2
SC
3784}
3785
4e8f195d
TT
3786/* A helper function which returns true if types A and B represent the
3787 "same" class type. This is true if the types have the same main
3788 type, or the same name. */
3789
3790int
3791class_types_same_p (const struct type *a, const struct type *b)
3792{
3793 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
7d93a1e0
SM
3794 || (a->name () && b->name ()
3795 && !strcmp (a->name (), b->name ())));
4e8f195d
TT
3796}
3797
a9d5ef47
SW
3798/* If BASE is an ancestor of DCLASS return the distance between them.
3799 otherwise return -1;
3800 eg:
3801
3802 class A {};
3803 class B: public A {};
3804 class C: public B {};
3805 class D: C {};
3806
3807 distance_to_ancestor (A, A, 0) = 0
3808 distance_to_ancestor (A, B, 0) = 1
3809 distance_to_ancestor (A, C, 0) = 2
3810 distance_to_ancestor (A, D, 0) = 3
3811
3812 If PUBLIC is 1 then only public ancestors are considered,
3813 and the function returns the distance only if BASE is a public ancestor
3814 of DCLASS.
3815 Eg:
3816
0963b4bd 3817 distance_to_ancestor (A, D, 1) = -1. */
c906108c 3818
0526b37a 3819static int
fe978cb0 3820distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
c906108c
SS
3821{
3822 int i;
a9d5ef47 3823 int d;
c5aa993b 3824
f168693b
SM
3825 base = check_typedef (base);
3826 dclass = check_typedef (dclass);
c906108c 3827
4e8f195d 3828 if (class_types_same_p (base, dclass))
a9d5ef47 3829 return 0;
c906108c
SS
3830
3831 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
4e8f195d 3832 {
fe978cb0 3833 if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
0526b37a
SW
3834 continue;
3835
fe978cb0 3836 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
a9d5ef47
SW
3837 if (d >= 0)
3838 return 1 + d;
4e8f195d 3839 }
c906108c 3840
a9d5ef47 3841 return -1;
c906108c 3842}
4e8f195d 3843
0526b37a
SW
3844/* Check whether BASE is an ancestor or base class or DCLASS
3845 Return 1 if so, and 0 if not.
3846 Note: If BASE and DCLASS are of the same type, this function
3847 will return 1. So for some class A, is_ancestor (A, A) will
3848 return 1. */
3849
3850int
3851is_ancestor (struct type *base, struct type *dclass)
3852{
a9d5ef47 3853 return distance_to_ancestor (base, dclass, 0) >= 0;
0526b37a
SW
3854}
3855
4e8f195d
TT
3856/* Like is_ancestor, but only returns true when BASE is a public
3857 ancestor of DCLASS. */
3858
3859int
3860is_public_ancestor (struct type *base, struct type *dclass)
3861{
a9d5ef47 3862 return distance_to_ancestor (base, dclass, 1) >= 0;
4e8f195d
TT
3863}
3864
3865/* A helper function for is_unique_ancestor. */
3866
3867static int
3868is_unique_ancestor_worker (struct type *base, struct type *dclass,
3869 int *offset,
8af8e3bc
PA
3870 const gdb_byte *valaddr, int embedded_offset,
3871 CORE_ADDR address, struct value *val)
4e8f195d
TT
3872{
3873 int i, count = 0;
3874
f168693b
SM
3875 base = check_typedef (base);
3876 dclass = check_typedef (dclass);
4e8f195d
TT
3877
3878 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
3879 {
8af8e3bc
PA
3880 struct type *iter;
3881 int this_offset;
4e8f195d 3882
8af8e3bc
PA
3883 iter = check_typedef (TYPE_BASECLASS (dclass, i));
3884
3885 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
3886 address, val);
4e8f195d
TT
3887
3888 if (class_types_same_p (base, iter))
3889 {
3890 /* If this is the first subclass, set *OFFSET and set count
3891 to 1. Otherwise, if this is at the same offset as
3892 previous instances, do nothing. Otherwise, increment
3893 count. */
3894 if (*offset == -1)
3895 {
3896 *offset = this_offset;
3897 count = 1;
3898 }
3899 else if (this_offset == *offset)
3900 {
3901 /* Nothing. */
3902 }
3903 else
3904 ++count;
3905 }
3906 else
3907 count += is_unique_ancestor_worker (base, iter, offset,
8af8e3bc
PA
3908 valaddr,
3909 embedded_offset + this_offset,
3910 address, val);
4e8f195d
TT
3911 }
3912
3913 return count;
3914}
3915
3916/* Like is_ancestor, but only returns true if BASE is a unique base
3917 class of the type of VAL. */
3918
3919int
3920is_unique_ancestor (struct type *base, struct value *val)
3921{
3922 int offset = -1;
3923
d0c97917 3924 return is_unique_ancestor_worker (base, val->type (), &offset,
efaf1ae0 3925 val->contents_for_printing ().data (),
391f8628 3926 val->embedded_offset (),
9feb2d07 3927 val->address (), val) == 1;
4e8f195d
TT
3928}
3929
7ab4a236
TT
3930/* See gdbtypes.h. */
3931
3932enum bfd_endian
3933type_byte_order (const struct type *type)
3934{
8ee511af 3935 bfd_endian byteorder = gdbarch_byte_order (type->arch ());
04f5bab2 3936 if (type->endianity_is_not_default ())
7ab4a236
TT
3937 {
3938 if (byteorder == BFD_ENDIAN_BIG)
dda83cd7 3939 return BFD_ENDIAN_LITTLE;
7ab4a236
TT
3940 else
3941 {
3942 gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
3943 return BFD_ENDIAN_BIG;
3944 }
3945 }
3946
3947 return byteorder;
3948}
3949
0b35f123
LS
3950/* See gdbtypes.h. */
3951
3952bool
3953is_nocall_function (const struct type *type)
3954{
78554598
TT
3955 if (type->code () != TYPE_CODE_FUNC && type->code () != TYPE_CODE_METHOD)
3956 return false;
0b35f123
LS
3957
3958 return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
3959}
3960
c906108c 3961\f
5212577a 3962/* Overload resolution. */
c906108c 3963
6403aeea
SW
3964/* Return the sum of the rank of A with the rank of B. */
3965
3966struct rank
3967sum_ranks (struct rank a, struct rank b)
3968{
3969 struct rank c;
3970 c.rank = a.rank + b.rank;
a9d5ef47 3971 c.subrank = a.subrank + b.subrank;
6403aeea
SW
3972 return c;
3973}
3974
3975/* Compare rank A and B and return:
3976 0 if a = b
3977 1 if a is better than b
3978 -1 if b is better than a. */
3979
3980int
3981compare_ranks (struct rank a, struct rank b)
3982{
3983 if (a.rank == b.rank)
a9d5ef47
SW
3984 {
3985 if (a.subrank == b.subrank)
3986 return 0;
3987 if (a.subrank < b.subrank)
3988 return 1;
3989 if (a.subrank > b.subrank)
3990 return -1;
3991 }
6403aeea
SW
3992
3993 if (a.rank < b.rank)
3994 return 1;
3995
0963b4bd 3996 /* a.rank > b.rank */
6403aeea
SW
3997 return -1;
3998}
c5aa993b 3999
0963b4bd 4000/* Functions for overload resolution begin here. */
c906108c
SS
4001
4002/* Compare two badness vectors A and B and return the result.
7ba81444
MS
4003 0 => A and B are identical
4004 1 => A and B are incomparable
4005 2 => A is better than B
4006 3 => A is worse than B */
c906108c
SS
4007
4008int
82ceee50 4009compare_badness (const badness_vector &a, const badness_vector &b)
c906108c
SS
4010{
4011 int i;
4012 int tmp;
ac03c8d8
TT
4013 /* Any positives in comparison? */
4014 bool found_pos = false;
4015 /* Any negatives in comparison? */
4016 bool found_neg = false;
4017 /* Did A have any INVALID_CONVERSION entries. */
4018 bool a_invalid = false;
4019 /* Did B have any INVALID_CONVERSION entries. */
4020 bool b_invalid = false;
c5aa993b 4021
82ceee50
PA
4022 /* differing sizes => incomparable */
4023 if (a.size () != b.size ())
c906108c
SS
4024 return 1;
4025
c5aa993b 4026 /* Subtract b from a */
82ceee50 4027 for (i = 0; i < a.size (); i++)
c906108c 4028 {
82ceee50 4029 tmp = compare_ranks (b[i], a[i]);
c906108c 4030 if (tmp > 0)
ac03c8d8 4031 found_pos = true;
c906108c 4032 else if (tmp < 0)
ac03c8d8
TT
4033 found_neg = true;
4034 if (a[i].rank >= INVALID_CONVERSION)
4035 a_invalid = true;
4036 if (b[i].rank >= INVALID_CONVERSION)
4037 b_invalid = true;
c906108c
SS
4038 }
4039
ac03c8d8
TT
4040 /* B will only be considered better than or incomparable to A if
4041 they both have invalid entries, or if neither does. That is, if
4042 A has only valid entries, and B has an invalid entry, then A will
4043 be considered better than B, even if B happens to be better for
4044 some parameter. */
4045 if (a_invalid != b_invalid)
4046 {
4047 if (a_invalid)
4048 return 3; /* A > B */
4049 return 2; /* A < B */
4050 }
4051 else if (found_pos)
c906108c
SS
4052 {
4053 if (found_neg)
c5aa993b 4054 return 1; /* incomparable */
c906108c 4055 else
c5aa993b 4056 return 3; /* A > B */
c906108c 4057 }
c5aa993b
JM
4058 else
4059 /* no positives */
c906108c
SS
4060 {
4061 if (found_neg)
c5aa993b 4062 return 2; /* A < B */
c906108c 4063 else
c5aa993b 4064 return 0; /* A == B */
c906108c
SS
4065 }
4066}
4067
6b1747cd 4068/* Rank a function by comparing its parameter types (PARMS), to the
82ceee50
PA
4069 types of an argument list (ARGS). Return the badness vector. This
4070 has ARGS.size() + 1 entries. */
c906108c 4071
82ceee50 4072badness_vector
6b1747cd 4073rank_function (gdb::array_view<type *> parms,
1d2f86b6
HD
4074 gdb::array_view<value *> args,
4075 bool varargs)
c906108c 4076{
82ceee50
PA
4077 /* add 1 for the length-match rank. */
4078 badness_vector bv;
4079 bv.reserve (1 + args.size ());
c906108c
SS
4080
4081 /* First compare the lengths of the supplied lists.
7ba81444 4082 If there is a mismatch, set it to a high value. */
c5aa993b 4083
c906108c 4084 /* pai/1997-06-03 FIXME: when we have debug info about default
7ba81444
MS
4085 arguments and ellipsis parameter lists, we should consider those
4086 and rank the length-match more finely. */
c906108c 4087
1d2f86b6
HD
4088 bv.push_back ((args.size () != parms.size ()
4089 && (! varargs || args.size () < parms.size ()))
82ceee50
PA
4090 ? LENGTH_MISMATCH_BADNESS
4091 : EXACT_MATCH_BADNESS);
c906108c 4092
0963b4bd 4093 /* Now rank all the parameters of the candidate function. */
82ceee50
PA
4094 size_t min_len = std::min (parms.size (), args.size ());
4095
4096 for (size_t i = 0; i < min_len; i++)
d0c97917 4097 bv.push_back (rank_one_type (parms[i], args[i]->type (),
82ceee50 4098 args[i]));
c906108c 4099
0963b4bd 4100 /* If more arguments than parameters, add dummy entries. */
82ceee50 4101 for (size_t i = min_len; i < args.size (); i++)
1d2f86b6 4102 bv.push_back (varargs ? VARARG_BADNESS : TOO_FEW_PARAMS_BADNESS);
c906108c
SS
4103
4104 return bv;
4105}
4106
973ccf8b
DJ
4107/* Compare the names of two integer types, assuming that any sign
4108 qualifiers have been checked already. We do it this way because
4109 there may be an "int" in the name of one of the types. */
4110
4111static int
4112integer_types_same_name_p (const char *first, const char *second)
4113{
4114 int first_p, second_p;
4115
7ba81444
MS
4116 /* If both are shorts, return 1; if neither is a short, keep
4117 checking. */
973ccf8b
DJ
4118 first_p = (strstr (first, "short") != NULL);
4119 second_p = (strstr (second, "short") != NULL);
4120 if (first_p && second_p)
4121 return 1;
4122 if (first_p || second_p)
4123 return 0;
4124
4125 /* Likewise for long. */
4126 first_p = (strstr (first, "long") != NULL);
4127 second_p = (strstr (second, "long") != NULL);
4128 if (first_p && second_p)
4129 return 1;
4130 if (first_p || second_p)
4131 return 0;
4132
4133 /* Likewise for char. */
4134 first_p = (strstr (first, "char") != NULL);
4135 second_p = (strstr (second, "char") != NULL);
4136 if (first_p && second_p)
4137 return 1;
4138 if (first_p || second_p)
4139 return 0;
4140
4141 /* They must both be ints. */
4142 return 1;
4143}
4144
894882e3
TT
4145/* Compares type A to type B. Returns true if they represent the same
4146 type, false otherwise. */
7062b0a0 4147
894882e3 4148bool
7062b0a0
SW
4149types_equal (struct type *a, struct type *b)
4150{
4151 /* Identical type pointers. */
4152 /* However, this still doesn't catch all cases of same type for b
4153 and a. The reason is that builtin types are different from
4154 the same ones constructed from the object. */
4155 if (a == b)
894882e3 4156 return true;
7062b0a0
SW
4157
4158 /* Resolve typedefs */
78134374 4159 if (a->code () == TYPE_CODE_TYPEDEF)
7062b0a0 4160 a = check_typedef (a);
78134374 4161 if (b->code () == TYPE_CODE_TYPEDEF)
7062b0a0
SW
4162 b = check_typedef (b);
4163
5e18990f
AB
4164 /* Check if identical after resolving typedefs. */
4165 if (a == b)
4166 return true;
4167
7062b0a0
SW
4168 /* If after resolving typedefs a and b are not of the same type
4169 code then they are not equal. */
78134374 4170 if (a->code () != b->code ())
894882e3 4171 return false;
7062b0a0
SW
4172
4173 /* If a and b are both pointers types or both reference types then
4174 they are equal of the same type iff the objects they refer to are
4175 of the same type. */
78134374
SM
4176 if (a->code () == TYPE_CODE_PTR
4177 || a->code () == TYPE_CODE_REF)
27710edb
SM
4178 return types_equal (a->target_type (),
4179 b->target_type ());
7062b0a0 4180
0963b4bd 4181 /* Well, damnit, if the names are exactly the same, I'll say they
7062b0a0
SW
4182 are exactly the same. This happens when we generate method
4183 stubs. The types won't point to the same address, but they
0963b4bd 4184 really are the same. */
7062b0a0 4185
7d93a1e0
SM
4186 if (a->name () && b->name ()
4187 && strcmp (a->name (), b->name ()) == 0)
894882e3 4188 return true;
7062b0a0 4189
9ce98649
TT
4190 /* Two function types are equal if their argument and return types
4191 are equal. */
78134374 4192 if (a->code () == TYPE_CODE_FUNC)
9ce98649
TT
4193 {
4194 int i;
4195
1f704f76 4196 if (a->num_fields () != b->num_fields ())
894882e3 4197 return false;
9ce98649 4198
27710edb 4199 if (!types_equal (a->target_type (), b->target_type ()))
894882e3 4200 return false;
9ce98649 4201
1f704f76 4202 for (i = 0; i < a->num_fields (); ++i)
940da03e 4203 if (!types_equal (a->field (i).type (), b->field (i).type ()))
894882e3 4204 return false;
9ce98649 4205
894882e3 4206 return true;
9ce98649
TT
4207 }
4208
105470cd
HD
4209 /* Two array types are the same if they have the same element types
4210 and array bounds. */
4211 if (a->code () == TYPE_CODE_ARRAY)
4212 {
4213 if (!types_equal (a->target_type (), b->target_type ()))
4214 return false;
4215
4216 if (*a->bounds () != *b->bounds ())
4217 return false;
4218
4219 return true;
4220 }
4221
894882e3 4222 return false;
7062b0a0 4223}
ca092b61
DE
4224\f
4225/* Deep comparison of types. */
4226
4227/* An entry in the type-equality bcache. */
4228
894882e3 4229struct type_equality_entry
ca092b61 4230{
894882e3
TT
4231 type_equality_entry (struct type *t1, struct type *t2)
4232 : type1 (t1),
4233 type2 (t2)
4234 {
4235 }
ca092b61 4236
894882e3
TT
4237 struct type *type1, *type2;
4238};
ca092b61 4239
894882e3
TT
4240/* A helper function to compare two strings. Returns true if they are
4241 the same, false otherwise. Handles NULLs properly. */
ca092b61 4242
894882e3 4243static bool
ca092b61
DE
4244compare_maybe_null_strings (const char *s, const char *t)
4245{
894882e3
TT
4246 if (s == NULL || t == NULL)
4247 return s == t;
ca092b61
DE
4248 return strcmp (s, t) == 0;
4249}
4250
4251/* A helper function for check_types_worklist that checks two types for
894882e3
TT
4252 "deep" equality. Returns true if the types are considered the
4253 same, false otherwise. */
ca092b61 4254
894882e3 4255static bool
ca092b61 4256check_types_equal (struct type *type1, struct type *type2,
894882e3 4257 std::vector<type_equality_entry> *worklist)
ca092b61 4258{
f168693b
SM
4259 type1 = check_typedef (type1);
4260 type2 = check_typedef (type2);
ca092b61
DE
4261
4262 if (type1 == type2)
894882e3 4263 return true;
ca092b61 4264
78134374 4265 if (type1->code () != type2->code ()
df86565b 4266 || type1->length () != type2->length ()
c6d940a9 4267 || type1->is_unsigned () != type2->is_unsigned ()
20ce4123 4268 || type1->has_no_signedness () != type2->has_no_signedness ()
04f5bab2 4269 || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
a409645d 4270 || type1->has_varargs () != type2->has_varargs ()
bd63c870 4271 || type1->is_vector () != type2->is_vector ()
ca092b61 4272 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
10242f36 4273 || type1->instance_flags () != type2->instance_flags ()
1f704f76 4274 || type1->num_fields () != type2->num_fields ())
894882e3 4275 return false;
ca092b61 4276
7d93a1e0 4277 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
894882e3 4278 return false;
7d93a1e0 4279 if (!compare_maybe_null_strings (type1->name (), type2->name ()))
894882e3 4280 return false;
ca092b61 4281
78134374 4282 if (type1->code () == TYPE_CODE_RANGE)
ca092b61 4283 {
599088e3 4284 if (*type1->bounds () != *type2->bounds ())
894882e3 4285 return false;
ca092b61
DE
4286 }
4287 else
4288 {
4289 int i;
4290
1f704f76 4291 for (i = 0; i < type1->num_fields (); ++i)
ca092b61 4292 {
ceacbf6e
SM
4293 const struct field *field1 = &type1->field (i);
4294 const struct field *field2 = &type2->field (i);
ca092b61 4295
6c0f7493 4296 if (field1->is_artificial () != field2->is_artificial ()
3be8c919 4297 || field1->bitsize () != field2->bitsize ()
8d939e8e 4298 || field1->loc_kind () != field2->loc_kind ())
894882e3 4299 return false;
33d16dd9 4300 if (!compare_maybe_null_strings (field1->name (), field2->name ()))
894882e3 4301 return false;
8d939e8e 4302 switch (field1->loc_kind ())
ca092b61
DE
4303 {
4304 case FIELD_LOC_KIND_BITPOS:
3a543e21 4305 if (field1->loc_bitpos () != field2->loc_bitpos ())
894882e3 4306 return false;
ca092b61
DE
4307 break;
4308 case FIELD_LOC_KIND_ENUMVAL:
5d2038e3 4309 if (field1->loc_enumval () != field2->loc_enumval ())
894882e3 4310 return false;
fa639f55
HD
4311 /* Don't compare types of enum fields, because they don't
4312 have a type. */
4313 continue;
ca092b61 4314 case FIELD_LOC_KIND_PHYSADDR:
31a1516a 4315 if (field1->loc_physaddr () != field2->loc_physaddr ())
894882e3 4316 return false;
ca092b61
DE
4317 break;
4318 case FIELD_LOC_KIND_PHYSNAME:
16654a59
SM
4319 if (!compare_maybe_null_strings (field1->loc_physname (),
4320 field2->loc_physname ()))
894882e3 4321 return false;
ca092b61
DE
4322 break;
4323 case FIELD_LOC_KIND_DWARF_BLOCK:
4324 {
4325 struct dwarf2_locexpr_baton *block1, *block2;
4326
d8557c3d
SM
4327 block1 = field1->loc_dwarf_block ();
4328 block2 = field2->loc_dwarf_block ();
ca092b61
DE
4329 if (block1->per_cu != block2->per_cu
4330 || block1->size != block2->size
4331 || memcmp (block1->data, block2->data, block1->size) != 0)
894882e3 4332 return false;
ca092b61
DE
4333 }
4334 break;
4335 default:
f34652de 4336 internal_error (_("Unsupported field kind "
ca092b61 4337 "%d by check_types_equal"),
8d939e8e 4338 field1->loc_kind ());
ca092b61
DE
4339 }
4340
b6cdac4b 4341 worklist->emplace_back (field1->type (), field2->type ());
ca092b61
DE
4342 }
4343 }
4344
27710edb 4345 if (type1->target_type () != NULL)
ca092b61 4346 {
27710edb 4347 if (type2->target_type () == NULL)
894882e3 4348 return false;
ca092b61 4349
27710edb
SM
4350 worklist->emplace_back (type1->target_type (),
4351 type2->target_type ());
ca092b61 4352 }
27710edb 4353 else if (type2->target_type () != NULL)
894882e3 4354 return false;
ca092b61 4355
894882e3 4356 return true;
ca092b61
DE
4357}
4358
894882e3
TT
4359/* Check types on a worklist for equality. Returns false if any pair
4360 is not equal, true if they are all considered equal. */
ca092b61 4361
894882e3
TT
4362static bool
4363check_types_worklist (std::vector<type_equality_entry> *worklist,
dfb65191 4364 gdb::bcache *cache)
ca092b61 4365{
894882e3 4366 while (!worklist->empty ())
ca092b61 4367 {
ef5e5b0b 4368 bool added;
ca092b61 4369
894882e3
TT
4370 struct type_equality_entry entry = std::move (worklist->back ());
4371 worklist->pop_back ();
ca092b61
DE
4372
4373 /* If the type pair has already been visited, we know it is
4374 ok. */
25629dfd 4375 cache->insert (&entry, sizeof (entry), &added);
ca092b61
DE
4376 if (!added)
4377 continue;
4378
894882e3
TT
4379 if (!check_types_equal (entry.type1, entry.type2, worklist))
4380 return false;
ca092b61 4381 }
7062b0a0 4382
894882e3 4383 return true;
ca092b61
DE
4384}
4385
894882e3
TT
4386/* Return true if types TYPE1 and TYPE2 are equal, as determined by a
4387 "deep comparison". Otherwise return false. */
ca092b61 4388
894882e3 4389bool
ca092b61
DE
4390types_deeply_equal (struct type *type1, struct type *type2)
4391{
894882e3 4392 std::vector<type_equality_entry> worklist;
ca092b61
DE
4393
4394 gdb_assert (type1 != NULL && type2 != NULL);
4395
4396 /* Early exit for the simple case. */
4397 if (type1 == type2)
894882e3 4398 return true;
ca092b61 4399
89806626 4400 gdb::bcache cache;
894882e3 4401 worklist.emplace_back (type1, type2);
25629dfd 4402 return check_types_worklist (&worklist, &cache);
ca092b61 4403}
3f2f83dd
KB
4404
4405/* Allocated status of type TYPE. Return zero if type TYPE is allocated.
4406 Otherwise return one. */
4407
4408int
4409type_not_allocated (const struct type *type)
4410{
4411 struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
4412
9c0fb734 4413 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
3f2f83dd
KB
4414}
4415
4416/* Associated status of type TYPE. Return zero if type TYPE is associated.
4417 Otherwise return one. */
4418
4419int
4420type_not_associated (const struct type *type)
4421{
4422 struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
4423
9c0fb734 4424 return prop != nullptr && prop->is_constant () && prop->const_val () == 0;
3f2f83dd 4425}
9293fc63
SM
4426
4427/* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
4428
4429static struct rank
4430rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
4431{
4432 struct rank rank = {0,0};
4433
78134374 4434 switch (arg->code ())
9293fc63
SM
4435 {
4436 case TYPE_CODE_PTR:
4437
4438 /* Allowed pointer conversions are:
4439 (a) pointer to void-pointer conversion. */
27710edb 4440 if (parm->target_type ()->code () == TYPE_CODE_VOID)
9293fc63
SM
4441 return VOID_PTR_CONVERSION_BADNESS;
4442
4443 /* (b) pointer to ancestor-pointer conversion. */
27710edb
SM
4444 rank.subrank = distance_to_ancestor (parm->target_type (),
4445 arg->target_type (),
9293fc63
SM
4446 0);
4447 if (rank.subrank >= 0)
4448 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
4449
4450 return INCOMPATIBLE_TYPE_BADNESS;
4451 case TYPE_CODE_ARRAY:
4452 {
27710edb
SM
4453 struct type *t1 = parm->target_type ();
4454 struct type *t2 = arg->target_type ();
9293fc63
SM
4455
4456 if (types_equal (t1, t2))
4457 {
4458 /* Make sure they are CV equal. */
4459 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4460 rank.subrank |= CV_CONVERSION_CONST;
4461 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4462 rank.subrank |= CV_CONVERSION_VOLATILE;
4463 if (rank.subrank != 0)
4464 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4465 return EXACT_MATCH_BADNESS;
4466 }
4467 return INCOMPATIBLE_TYPE_BADNESS;
4468 }
4469 case TYPE_CODE_FUNC:
27710edb 4470 return rank_one_type (parm->target_type (), arg, NULL);
9293fc63 4471 case TYPE_CODE_INT:
d0c97917 4472 if (value != NULL && value->type ()->code () == TYPE_CODE_INT)
9293fc63
SM
4473 {
4474 if (value_as_long (value) == 0)
4475 {
4476 /* Null pointer conversion: allow it to be cast to a pointer.
4477 [4.10.1 of C++ standard draft n3290] */
4478 return NULL_POINTER_CONVERSION_BADNESS;
4479 }
4480 else
4481 {
4482 /* If type checking is disabled, allow the conversion. */
4483 if (!strict_type_checking)
4484 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
4485 }
4486 }
d182e398 4487 [[fallthrough]];
9293fc63
SM
4488 case TYPE_CODE_ENUM:
4489 case TYPE_CODE_FLAGS:
4490 case TYPE_CODE_CHAR:
4491 case TYPE_CODE_RANGE:
4492 case TYPE_CODE_BOOL:
4493 default:
4494 return INCOMPATIBLE_TYPE_BADNESS;
4495 }
4496}
4497
b9f4512f
SM
4498/* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
4499
4500static struct rank
4501rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
4502{
78134374 4503 switch (arg->code ())
b9f4512f
SM
4504 {
4505 case TYPE_CODE_PTR:
4506 case TYPE_CODE_ARRAY:
27710edb
SM
4507 return rank_one_type (parm->target_type (),
4508 arg->target_type (), NULL);
b9f4512f
SM
4509 default:
4510 return INCOMPATIBLE_TYPE_BADNESS;
4511 }
4512}
4513
f1f832d6
SM
4514/* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
4515
4516static struct rank
4517rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
4518{
78134374 4519 switch (arg->code ())
f1f832d6
SM
4520 {
4521 case TYPE_CODE_PTR: /* funcptr -> func */
27710edb 4522 return rank_one_type (parm, arg->target_type (), NULL);
f1f832d6
SM
4523 default:
4524 return INCOMPATIBLE_TYPE_BADNESS;
4525 }
4526}
4527
34910087
SM
4528/* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
4529
4530static struct rank
4531rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
4532{
78134374 4533 switch (arg->code ())
34910087
SM
4534 {
4535 case TYPE_CODE_INT:
df86565b 4536 if (arg->length () == parm->length ())
34910087
SM
4537 {
4538 /* Deal with signed, unsigned, and plain chars and
4539 signed and unsigned ints. */
20ce4123 4540 if (parm->has_no_signedness ())
34910087
SM
4541 {
4542 /* This case only for character types. */
20ce4123 4543 if (arg->has_no_signedness ())
34910087
SM
4544 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
4545 else /* signed/unsigned char -> plain char */
4546 return INTEGER_CONVERSION_BADNESS;
4547 }
c6d940a9 4548 else if (parm->is_unsigned ())
34910087 4549 {
c6d940a9 4550 if (arg->is_unsigned ())
34910087
SM
4551 {
4552 /* unsigned int -> unsigned int, or
4553 unsigned long -> unsigned long */
7d93a1e0
SM
4554 if (integer_types_same_name_p (parm->name (),
4555 arg->name ()))
34910087 4556 return EXACT_MATCH_BADNESS;
7d93a1e0 4557 else if (integer_types_same_name_p (arg->name (),
34910087 4558 "int")
7d93a1e0 4559 && integer_types_same_name_p (parm->name (),
34910087
SM
4560 "long"))
4561 /* unsigned int -> unsigned long */
4562 return INTEGER_PROMOTION_BADNESS;
4563 else
4564 /* unsigned long -> unsigned int */
4565 return INTEGER_CONVERSION_BADNESS;
4566 }
4567 else
4568 {
7d93a1e0 4569 if (integer_types_same_name_p (arg->name (),
34910087 4570 "long")
7d93a1e0 4571 && integer_types_same_name_p (parm->name (),
34910087
SM
4572 "int"))
4573 /* signed long -> unsigned int */
4574 return INTEGER_CONVERSION_BADNESS;
4575 else
4576 /* signed int/long -> unsigned int/long */
4577 return INTEGER_CONVERSION_BADNESS;
4578 }
4579 }
20ce4123 4580 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
34910087 4581 {
7d93a1e0
SM
4582 if (integer_types_same_name_p (parm->name (),
4583 arg->name ()))
34910087 4584 return EXACT_MATCH_BADNESS;
7d93a1e0 4585 else if (integer_types_same_name_p (arg->name (),
34910087 4586 "int")
7d93a1e0 4587 && integer_types_same_name_p (parm->name (),
34910087
SM
4588 "long"))
4589 return INTEGER_PROMOTION_BADNESS;
4590 else
4591 return INTEGER_CONVERSION_BADNESS;
4592 }
4593 else
4594 return INTEGER_CONVERSION_BADNESS;
4595 }
df86565b 4596 else if (arg->length () < parm->length ())
34910087
SM
4597 return INTEGER_PROMOTION_BADNESS;
4598 else
4599 return INTEGER_CONVERSION_BADNESS;
4600 case TYPE_CODE_ENUM:
4601 case TYPE_CODE_FLAGS:
4602 case TYPE_CODE_CHAR:
4603 case TYPE_CODE_RANGE:
4604 case TYPE_CODE_BOOL:
3bc440a2 4605 if (arg->is_declared_class ())
34910087
SM
4606 return INCOMPATIBLE_TYPE_BADNESS;
4607 return INTEGER_PROMOTION_BADNESS;
4608 case TYPE_CODE_FLT:
4609 return INT_FLOAT_CONVERSION_BADNESS;
4610 case TYPE_CODE_PTR:
4611 return NS_POINTER_CONVERSION_BADNESS;
4612 default:
4613 return INCOMPATIBLE_TYPE_BADNESS;
4614 }
4615}
4616
793cd1d2
SM
4617/* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
4618
4619static struct rank
4620rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
4621{
78134374 4622 switch (arg->code ())
793cd1d2
SM
4623 {
4624 case TYPE_CODE_INT:
4625 case TYPE_CODE_CHAR:
4626 case TYPE_CODE_RANGE:
4627 case TYPE_CODE_BOOL:
4628 case TYPE_CODE_ENUM:
3bc440a2 4629 if (parm->is_declared_class () || arg->is_declared_class ())
793cd1d2
SM
4630 return INCOMPATIBLE_TYPE_BADNESS;
4631 return INTEGER_CONVERSION_BADNESS;
4632 case TYPE_CODE_FLT:
4633 return INT_FLOAT_CONVERSION_BADNESS;
4634 default:
4635 return INCOMPATIBLE_TYPE_BADNESS;
4636 }
4637}
4638
41ea4728
SM
4639/* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
4640
4641static struct rank
4642rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
4643{
78134374 4644 switch (arg->code ())
41ea4728
SM
4645 {
4646 case TYPE_CODE_RANGE:
4647 case TYPE_CODE_BOOL:
4648 case TYPE_CODE_ENUM:
3bc440a2 4649 if (arg->is_declared_class ())
41ea4728
SM
4650 return INCOMPATIBLE_TYPE_BADNESS;
4651 return INTEGER_CONVERSION_BADNESS;
4652 case TYPE_CODE_FLT:
4653 return INT_FLOAT_CONVERSION_BADNESS;
4654 case TYPE_CODE_INT:
df86565b 4655 if (arg->length () > parm->length ())
41ea4728 4656 return INTEGER_CONVERSION_BADNESS;
df86565b 4657 else if (arg->length () < parm->length ())
41ea4728 4658 return INTEGER_PROMOTION_BADNESS;
d182e398 4659 [[fallthrough]];
41ea4728
SM
4660 case TYPE_CODE_CHAR:
4661 /* Deal with signed, unsigned, and plain chars for C++ and
4662 with int cases falling through from previous case. */
20ce4123 4663 if (parm->has_no_signedness ())
41ea4728 4664 {
20ce4123 4665 if (arg->has_no_signedness ())
41ea4728
SM
4666 return EXACT_MATCH_BADNESS;
4667 else
4668 return INTEGER_CONVERSION_BADNESS;
4669 }
c6d940a9 4670 else if (parm->is_unsigned ())
41ea4728 4671 {
c6d940a9 4672 if (arg->is_unsigned ())
41ea4728
SM
4673 return EXACT_MATCH_BADNESS;
4674 else
4675 return INTEGER_PROMOTION_BADNESS;
4676 }
20ce4123 4677 else if (!arg->has_no_signedness () && !arg->is_unsigned ())
41ea4728
SM
4678 return EXACT_MATCH_BADNESS;
4679 else
4680 return INTEGER_CONVERSION_BADNESS;
4681 default:
4682 return INCOMPATIBLE_TYPE_BADNESS;
4683 }
4684}
4685
0dd322dc
SM
4686/* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
4687
4688static struct rank
4689rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
4690{
78134374 4691 switch (arg->code ())
0dd322dc
SM
4692 {
4693 case TYPE_CODE_INT:
4694 case TYPE_CODE_CHAR:
4695 case TYPE_CODE_RANGE:
4696 case TYPE_CODE_BOOL:
4697 case TYPE_CODE_ENUM:
4698 return INTEGER_CONVERSION_BADNESS;
4699 case TYPE_CODE_FLT:
4700 return INT_FLOAT_CONVERSION_BADNESS;
4701 default:
4702 return INCOMPATIBLE_TYPE_BADNESS;
4703 }
4704}
4705
2c509035
SM
4706/* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
4707
4708static struct rank
4709rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
4710{
78134374 4711 switch (arg->code ())
2c509035
SM
4712 {
4713 /* n3290 draft, section 4.12.1 (conv.bool):
4714
4715 "A prvalue of arithmetic, unscoped enumeration, pointer, or
4716 pointer to member type can be converted to a prvalue of type
4717 bool. A zero value, null pointer value, or null member pointer
4718 value is converted to false; any other value is converted to
4719 true. A prvalue of type std::nullptr_t can be converted to a
4720 prvalue of type bool; the resulting value is false." */
4721 case TYPE_CODE_INT:
4722 case TYPE_CODE_CHAR:
4723 case TYPE_CODE_ENUM:
4724 case TYPE_CODE_FLT:
4725 case TYPE_CODE_MEMBERPTR:
4726 case TYPE_CODE_PTR:
4727 return BOOL_CONVERSION_BADNESS;
4728 case TYPE_CODE_RANGE:
4729 return INCOMPATIBLE_TYPE_BADNESS;
4730 case TYPE_CODE_BOOL:
4731 return EXACT_MATCH_BADNESS;
4732 default:
4733 return INCOMPATIBLE_TYPE_BADNESS;
4734 }
4735}
4736
7f17b20d
SM
4737/* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
4738
4739static struct rank
4740rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
4741{
78134374 4742 switch (arg->code ())
7f17b20d
SM
4743 {
4744 case TYPE_CODE_FLT:
df86565b 4745 if (arg->length () < parm->length ())
7f17b20d 4746 return FLOAT_PROMOTION_BADNESS;
df86565b 4747 else if (arg->length () == parm->length ())
7f17b20d
SM
4748 return EXACT_MATCH_BADNESS;
4749 else
4750 return FLOAT_CONVERSION_BADNESS;
4751 case TYPE_CODE_INT:
4752 case TYPE_CODE_BOOL:
4753 case TYPE_CODE_ENUM:
4754 case TYPE_CODE_RANGE:
4755 case TYPE_CODE_CHAR:
4756 return INT_FLOAT_CONVERSION_BADNESS;
4757 default:
4758 return INCOMPATIBLE_TYPE_BADNESS;
4759 }
4760}
4761
2598a94b
SM
4762/* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
4763
4764static struct rank
4765rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
4766{
78134374 4767 switch (arg->code ())
2598a94b
SM
4768 { /* Strictly not needed for C++, but... */
4769 case TYPE_CODE_FLT:
4770 return FLOAT_PROMOTION_BADNESS;
4771 case TYPE_CODE_COMPLEX:
4772 return EXACT_MATCH_BADNESS;
4773 default:
4774 return INCOMPATIBLE_TYPE_BADNESS;
4775 }
4776}
4777
595f96a9
SM
4778/* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
4779
4780static struct rank
4781rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
4782{
4783 struct rank rank = {0, 0};
4784
78134374 4785 switch (arg->code ())
595f96a9
SM
4786 {
4787 case TYPE_CODE_STRUCT:
4788 /* Check for derivation */
4789 rank.subrank = distance_to_ancestor (parm, arg, 0);
4790 if (rank.subrank >= 0)
4791 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
d182e398 4792 [[fallthrough]];
595f96a9
SM
4793 default:
4794 return INCOMPATIBLE_TYPE_BADNESS;
4795 }
4796}
4797
f09ce22d
SM
4798/* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
4799
4800static struct rank
4801rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
4802{
78134374 4803 switch (arg->code ())
f09ce22d
SM
4804 {
4805 /* Not in C++ */
4806 case TYPE_CODE_SET:
940da03e
SM
4807 return rank_one_type (parm->field (0).type (),
4808 arg->field (0).type (), NULL);
f09ce22d
SM
4809 default:
4810 return INCOMPATIBLE_TYPE_BADNESS;
4811 }
4812}
4813
c906108c
SS
4814/* Compare one type (PARM) for compatibility with another (ARG).
4815 * PARM is intended to be the parameter type of a function; and
4816 * ARG is the supplied argument's type. This function tests if
4817 * the latter can be converted to the former.
da096638 4818 * VALUE is the argument's value or NULL if none (or called recursively)
c906108c
SS
4819 *
4820 * Return 0 if they are identical types;
4821 * Otherwise, return an integer which corresponds to how compatible
7ba81444 4822 * PARM is to ARG. The higher the return value, the worse the match.
ac03c8d8
TT
4823 * Generally the "bad" conversions are all uniformly assigned
4824 * INVALID_CONVERSION. */
c906108c 4825
6403aeea 4826struct rank
da096638 4827rank_one_type (struct type *parm, struct type *arg, struct value *value)
c906108c 4828{
a9d5ef47 4829 struct rank rank = {0,0};
7062b0a0 4830
c906108c 4831 /* Resolve typedefs */
78134374 4832 if (parm->code () == TYPE_CODE_TYPEDEF)
c906108c 4833 parm = check_typedef (parm);
78134374 4834 if (arg->code () == TYPE_CODE_TYPEDEF)
c906108c
SS
4835 arg = check_typedef (arg);
4836
e15c3eb4 4837 if (TYPE_IS_REFERENCE (parm) && value != NULL)
15c0a2a9 4838 {
736355f2 4839 if (value->lval () == not_lval)
e15c3eb4
KS
4840 {
4841 /* Rvalues should preferably bind to rvalue references or const
4842 lvalue references. */
78134374 4843 if (parm->code () == TYPE_CODE_RVALUE_REF)
e15c3eb4 4844 rank.subrank = REFERENCE_CONVERSION_RVALUE;
27710edb 4845 else if (TYPE_CONST (parm->target_type ()))
e15c3eb4
KS
4846 rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
4847 else
4848 return INCOMPATIBLE_TYPE_BADNESS;
4849 return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
4850 }
4851 else
4852 {
330f1d38 4853 /* It's illegal to pass an lvalue as an rvalue. */
78134374 4854 if (parm->code () == TYPE_CODE_RVALUE_REF)
330f1d38 4855 return INCOMPATIBLE_TYPE_BADNESS;
e15c3eb4 4856 }
15c0a2a9
AV
4857 }
4858
4859 if (types_equal (parm, arg))
15c0a2a9 4860 {
e15c3eb4
KS
4861 struct type *t1 = parm;
4862 struct type *t2 = arg;
15c0a2a9 4863
e15c3eb4 4864 /* For pointers and references, compare target type. */
809f3be1 4865 if (parm->is_pointer_or_reference ())
e15c3eb4 4866 {
27710edb
SM
4867 t1 = parm->target_type ();
4868 t2 = arg->target_type ();
e15c3eb4 4869 }
15c0a2a9 4870
e15c3eb4
KS
4871 /* Make sure they are CV equal, too. */
4872 if (TYPE_CONST (t1) != TYPE_CONST (t2))
4873 rank.subrank |= CV_CONVERSION_CONST;
4874 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
4875 rank.subrank |= CV_CONVERSION_VOLATILE;
4876 if (rank.subrank != 0)
4877 return sum_ranks (CV_CONVERSION_BADNESS, rank);
4878 return EXACT_MATCH_BADNESS;
15c0a2a9
AV
4879 }
4880
db577aea 4881 /* See through references, since we can almost make non-references
7ba81444 4882 references. */
aa006118
AV
4883
4884 if (TYPE_IS_REFERENCE (arg))
27710edb 4885 return (sum_ranks (rank_one_type (parm, arg->target_type (), NULL),
dda83cd7 4886 REFERENCE_SEE_THROUGH_BADNESS));
aa006118 4887 if (TYPE_IS_REFERENCE (parm))
27710edb 4888 return (sum_ranks (rank_one_type (parm->target_type (), arg, NULL),
dda83cd7 4889 REFERENCE_SEE_THROUGH_BADNESS));
5d161b24 4890 if (overload_debug)
01add95b
SM
4891 {
4892 /* Debugging only. */
6cb06a8c
TT
4893 gdb_printf (gdb_stderr,
4894 "------ Arg is %s [%d], parm is %s [%d]\n",
4895 arg->name (), arg->code (),
4896 parm->name (), parm->code ());
01add95b 4897 }
c906108c 4898
0963b4bd 4899 /* x -> y means arg of type x being supplied for parameter of type y. */
c906108c 4900
78134374 4901 switch (parm->code ())
c906108c 4902 {
c5aa993b 4903 case TYPE_CODE_PTR:
9293fc63 4904 return rank_one_type_parm_ptr (parm, arg, value);
c5aa993b 4905 case TYPE_CODE_ARRAY:
b9f4512f 4906 return rank_one_type_parm_array (parm, arg, value);
c5aa993b 4907 case TYPE_CODE_FUNC:
f1f832d6 4908 return rank_one_type_parm_func (parm, arg, value);
c5aa993b 4909 case TYPE_CODE_INT:
34910087 4910 return rank_one_type_parm_int (parm, arg, value);
c5aa993b 4911 case TYPE_CODE_ENUM:
793cd1d2 4912 return rank_one_type_parm_enum (parm, arg, value);
c5aa993b 4913 case TYPE_CODE_CHAR:
41ea4728 4914 return rank_one_type_parm_char (parm, arg, value);
c5aa993b 4915 case TYPE_CODE_RANGE:
0dd322dc 4916 return rank_one_type_parm_range (parm, arg, value);
c5aa993b 4917 case TYPE_CODE_BOOL:
2c509035 4918 return rank_one_type_parm_bool (parm, arg, value);
c5aa993b 4919 case TYPE_CODE_FLT:
7f17b20d 4920 return rank_one_type_parm_float (parm, arg, value);
c5aa993b 4921 case TYPE_CODE_COMPLEX:
2598a94b 4922 return rank_one_type_parm_complex (parm, arg, value);
c5aa993b 4923 case TYPE_CODE_STRUCT:
595f96a9 4924 return rank_one_type_parm_struct (parm, arg, value);
c5aa993b 4925 case TYPE_CODE_SET:
f09ce22d 4926 return rank_one_type_parm_set (parm, arg, value);
c5aa993b
JM
4927 default:
4928 return INCOMPATIBLE_TYPE_BADNESS;
78134374 4929 } /* switch (arg->code ()) */
c906108c
SS
4930}
4931
0963b4bd 4932/* End of functions for overload resolution. */
5212577a 4933\f
c906108c 4934
ad2f7632 4935/* Note the first arg should be the "this" pointer, we may not want to
7ba81444
MS
4936 include it since we may get into a infinitely recursive
4937 situation. */
c906108c
SS
4938
4939static void
4c9e8482 4940print_args (struct field *args, int nargs, int spaces)
c906108c
SS
4941{
4942 if (args != NULL)
4943 {
ad2f7632
DJ
4944 int i;
4945
4946 for (i = 0; i < nargs; i++)
4c9e8482 4947 {
6cb06a8c 4948 gdb_printf
d3fd12df
SM
4949 ("%*s[%d] name '%s'\n", spaces, "", i,
4950 args[i].name () != NULL ? args[i].name () : "<NULL>");
5d14b6e5 4951 recursive_dump_type (args[i].type (), spaces + 2);
4c9e8482 4952 }
c906108c
SS
4953 }
4954}
4955
4956static void
fba45db2 4957dump_fn_fieldlists (struct type *type, int spaces)
c906108c
SS
4958{
4959 int method_idx;
4960 int overload_idx;
4961 struct fn_field *f;
4962
6cb06a8c
TT
4963 gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
4964 host_address_to_string (TYPE_FN_FIELDLISTS (type)));
c906108c
SS
4965 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
4966 {
4967 f = TYPE_FN_FIELDLIST1 (type, method_idx);
6cb06a8c 4968 gdb_printf
64b7cc50
TT
4969 ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
4970 method_idx,
4971 TYPE_FN_FIELDLIST_NAME (type, method_idx),
4972 host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
4973 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
c906108c
SS
4974 for (overload_idx = 0;
4975 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
4976 overload_idx++)
4977 {
6cb06a8c 4978 gdb_printf
64b7cc50
TT
4979 ("%*s[%d] physname '%s' (%s)\n",
4980 spaces + 4, "", overload_idx,
4981 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
4982 host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
4983 overload_idx)));
6cb06a8c 4984 gdb_printf
64b7cc50
TT
4985 ("%*stype %s\n", spaces + 8, "",
4986 host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
c906108c
SS
4987
4988 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
4989 spaces + 8 + 2);
4990
6cb06a8c 4991 gdb_printf
64b7cc50
TT
4992 ("%*sargs %s\n", spaces + 8, "",
4993 host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
4c9e8482 4994 print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
1f704f76 4995 TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
4c9e8482 4996 spaces + 8 + 2);
6cb06a8c 4997 gdb_printf
64b7cc50
TT
4998 ("%*sfcontext %s\n", spaces + 8, "",
4999 host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
5000 overload_idx)));
c906108c 5001
6cb06a8c
TT
5002 gdb_printf ("%*sis_const %d\n", spaces + 8, "",
5003 TYPE_FN_FIELD_CONST (f, overload_idx));
5004 gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
5005 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
5006 gdb_printf ("%*sis_private %d\n", spaces + 8, "",
5007 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
5008 gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
5009 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
5010 gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
5011 TYPE_FN_FIELD_STUB (f, overload_idx));
5012 gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
5013 TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
5014 gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
5015 TYPE_FN_FIELD_DELETED (f, overload_idx));
5016 gdb_printf ("%*svoffset %u\n", spaces + 8, "",
5017 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
c906108c
SS
5018 }
5019 }
5020}
5021
5022static void
fba45db2 5023print_cplus_stuff (struct type *type, int spaces)
c906108c 5024{
6cb06a8c
TT
5025 gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
5026 TYPE_VPTR_FIELDNO (type));
5027 gdb_printf ("%*svptr_basetype %s\n", spaces, "",
5028 host_address_to_string (TYPE_VPTR_BASETYPE (type)));
ae6ae975
DE
5029 if (TYPE_VPTR_BASETYPE (type) != NULL)
5030 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
5031
6cb06a8c
TT
5032 gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
5033 TYPE_N_BASECLASSES (type));
5034 gdb_printf ("%*snfn_fields %d\n", spaces, "",
5035 TYPE_NFN_FIELDS (type));
c906108c
SS
5036 if (TYPE_NFN_FIELDS (type) > 0)
5037 {
5038 dump_fn_fieldlists (type, spaces);
5039 }
e35000a7 5040
6cb06a8c
TT
5041 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5042 TYPE_CPLUS_CALLING_CONVENTION (type));
c906108c
SS
5043}
5044
b4ba55a1
JB
5045/* Print the contents of the TYPE's type_specific union, assuming that
5046 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
5047
5048static void
5049print_gnat_stuff (struct type *type, int spaces)
5050{
5051 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
5052
8cd00c59 5053 if (descriptive_type == NULL)
6cb06a8c 5054 gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
8cd00c59
PMR
5055 else
5056 {
6cb06a8c 5057 gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
8cd00c59
PMR
5058 recursive_dump_type (descriptive_type, spaces + 4);
5059 }
b4ba55a1
JB
5060}
5061
09584414
JB
5062/* Print the contents of the TYPE's type_specific union, assuming that
5063 its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
5064
5065static void
5066print_fixed_point_type_info (struct type *type, int spaces)
5067{
6cb06a8c
TT
5068 gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
5069 type->fixed_point_scaling_factor ().str ().c_str ());
09584414
JB
5070}
5071
c906108c
SS
5072static struct obstack dont_print_type_obstack;
5073
53d5a2a5
TV
5074/* Print the dynamic_prop PROP. */
5075
5076static void
5077dump_dynamic_prop (dynamic_prop const& prop)
5078{
5079 switch (prop.kind ())
5080 {
5081 case PROP_CONST:
6cb06a8c 5082 gdb_printf ("%s", plongest (prop.const_val ()));
53d5a2a5
TV
5083 break;
5084 case PROP_UNDEFINED:
6cb06a8c 5085 gdb_printf ("(undefined)");
53d5a2a5
TV
5086 break;
5087 case PROP_LOCEXPR:
5088 case PROP_LOCLIST:
6cb06a8c 5089 gdb_printf ("(dynamic)");
53d5a2a5
TV
5090 break;
5091 default:
5092 gdb_assert_not_reached ("unhandled prop kind");
5093 break;
5094 }
5095}
5096
e626733c
TT
5097/* Return a string that represents a type code. */
5098static const char *
5099type_code_name (type_code code)
5100{
5101 switch (code)
5102 {
5103#define OP(X) case X: return # X;
5104#include "type-codes.def"
5105#undef OP
5106
5107 case TYPE_CODE_UNDEF:
5108 return "TYPE_CODE_UNDEF";
5109 }
5110
5111 gdb_assert_not_reached ("unhandled type_code");
5112}
5113
c906108c 5114void
fba45db2 5115recursive_dump_type (struct type *type, int spaces)
c906108c
SS
5116{
5117 int idx;
5118
5119 if (spaces == 0)
5120 obstack_begin (&dont_print_type_obstack, 0);
5121
1f704f76 5122 if (type->num_fields () > 0
b4ba55a1 5123 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
c906108c
SS
5124 {
5125 struct type **first_dont_print
7ba81444 5126 = (struct type **) obstack_base (&dont_print_type_obstack);
c906108c 5127
7ba81444
MS
5128 int i = (struct type **)
5129 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
c906108c
SS
5130
5131 while (--i >= 0)
5132 {
5133 if (type == first_dont_print[i])
5134 {
6cb06a8c
TT
5135 gdb_printf ("%*stype node %s", spaces, "",
5136 host_address_to_string (type));
5137 gdb_printf (_(" <same as already seen type>\n"));
c906108c
SS
5138 return;
5139 }
5140 }
5141
5142 obstack_ptr_grow (&dont_print_type_obstack, type);
5143 }
5144
6cb06a8c
TT
5145 gdb_printf ("%*stype node %s\n", spaces, "",
5146 host_address_to_string (type));
5147 gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
5148 type->name () ? type->name () : "<NULL>",
5149 host_address_to_string (type->name ()));
5150 gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
e626733c 5151 gdb_printf ("(%s)", type_code_name (type->code ()));
0426ad51 5152 gdb_puts ("\n");
6cb06a8c 5153 gdb_printf ("%*slength %s\n", spaces, "",
df86565b 5154 pulongest (type->length ()));
30625020 5155 if (type->is_objfile_owned ())
6cb06a8c
TT
5156 gdb_printf ("%*sobjfile %s\n", spaces, "",
5157 host_address_to_string (type->objfile_owner ()));
e9bb382b 5158 else
6cb06a8c
TT
5159 gdb_printf ("%*sgdbarch %s\n", spaces, "",
5160 host_address_to_string (type->arch_owner ()));
5161 gdb_printf ("%*starget_type %s\n", spaces, "",
27710edb
SM
5162 host_address_to_string (type->target_type ()));
5163 if (type->target_type () != NULL)
c906108c 5164 {
27710edb 5165 recursive_dump_type (type->target_type (), spaces + 2);
c906108c 5166 }
6cb06a8c
TT
5167 gdb_printf ("%*spointer_type %s\n", spaces, "",
5168 host_address_to_string (TYPE_POINTER_TYPE (type)));
5169 gdb_printf ("%*sreference_type %s\n", spaces, "",
5170 host_address_to_string (TYPE_REFERENCE_TYPE (type)));
5171 gdb_printf ("%*stype_chain %s\n", spaces, "",
5172 host_address_to_string (TYPE_CHAIN (type)));
5173 gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
5174 (unsigned) type->instance_flags ());
2fdde8f8
DJ
5175 if (TYPE_CONST (type))
5176 {
0426ad51 5177 gdb_puts (" TYPE_CONST");
2fdde8f8
DJ
5178 }
5179 if (TYPE_VOLATILE (type))
5180 {
0426ad51 5181 gdb_puts (" TYPE_VOLATILE");
2fdde8f8
DJ
5182 }
5183 if (TYPE_CODE_SPACE (type))
5184 {
0426ad51 5185 gdb_puts (" TYPE_CODE_SPACE");
2fdde8f8
DJ
5186 }
5187 if (TYPE_DATA_SPACE (type))
5188 {
0426ad51 5189 gdb_puts (" TYPE_DATA_SPACE");
2fdde8f8 5190 }
8b2dbe47
KB
5191 if (TYPE_ADDRESS_CLASS_1 (type))
5192 {
0426ad51 5193 gdb_puts (" TYPE_ADDRESS_CLASS_1");
8b2dbe47
KB
5194 }
5195 if (TYPE_ADDRESS_CLASS_2 (type))
5196 {
0426ad51 5197 gdb_puts (" TYPE_ADDRESS_CLASS_2");
8b2dbe47 5198 }
06d66ee9
TT
5199 if (TYPE_RESTRICT (type))
5200 {
0426ad51 5201 gdb_puts (" TYPE_RESTRICT");
06d66ee9 5202 }
a2c2acaf
MW
5203 if (TYPE_ATOMIC (type))
5204 {
0426ad51 5205 gdb_puts (" TYPE_ATOMIC");
a2c2acaf 5206 }
0426ad51 5207 gdb_puts ("\n");
876cecd0 5208
6cb06a8c 5209 gdb_printf ("%*sflags", spaces, "");
c6d940a9 5210 if (type->is_unsigned ())
c906108c 5211 {
0426ad51 5212 gdb_puts (" TYPE_UNSIGNED");
c906108c 5213 }
20ce4123 5214 if (type->has_no_signedness ())
762a036f 5215 {
0426ad51 5216 gdb_puts (" TYPE_NOSIGN");
762a036f 5217 }
04f5bab2 5218 if (type->endianity_is_not_default ())
34877895 5219 {
0426ad51 5220 gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
34877895 5221 }
e46d3488 5222 if (type->is_stub ())
c906108c 5223 {
0426ad51 5224 gdb_puts (" TYPE_STUB");
c906108c 5225 }
d2183968 5226 if (type->target_is_stub ())
762a036f 5227 {
0426ad51 5228 gdb_puts (" TYPE_TARGET_STUB");
762a036f 5229 }
7f9f399b 5230 if (type->is_prototyped ())
762a036f 5231 {
0426ad51 5232 gdb_puts (" TYPE_PROTOTYPED");
762a036f 5233 }
a409645d 5234 if (type->has_varargs ())
762a036f 5235 {
0426ad51 5236 gdb_puts (" TYPE_VARARGS");
762a036f 5237 }
f5f8a009
EZ
5238 /* This is used for things like AltiVec registers on ppc. Gcc emits
5239 an attribute for the array type, which tells whether or not we
5240 have a vector, instead of a regular array. */
bd63c870 5241 if (type->is_vector ())
f5f8a009 5242 {
0426ad51 5243 gdb_puts (" TYPE_VECTOR");
f5f8a009 5244 }
22c4c60c 5245 if (type->is_fixed_instance ())
876cecd0 5246 {
0426ad51 5247 gdb_puts (" TYPE_FIXED_INSTANCE");
876cecd0 5248 }
3f46044c 5249 if (type->stub_is_supported ())
876cecd0 5250 {
0426ad51 5251 gdb_puts (" TYPE_STUB_SUPPORTED");
876cecd0
TT
5252 }
5253 if (TYPE_NOTTEXT (type))
5254 {
0426ad51 5255 gdb_puts (" TYPE_NOTTEXT");
876cecd0 5256 }
0426ad51 5257 gdb_puts ("\n");
6cb06a8c 5258 gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
5ba3b20e
AB
5259 if (TYPE_ASSOCIATED_PROP (type) != nullptr
5260 || TYPE_ALLOCATED_PROP (type) != nullptr)
5261 {
6cb06a8c 5262 gdb_printf ("%*s", spaces, "");
5ba3b20e
AB
5263 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
5264 {
6cb06a8c 5265 gdb_printf ("associated ");
5ba3b20e
AB
5266 dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
5267 }
5268 if (TYPE_ALLOCATED_PROP (type) != nullptr)
5269 {
5270 if (TYPE_ASSOCIATED_PROP (type) != nullptr)
6cb06a8c
TT
5271 gdb_printf (" ");
5272 gdb_printf ("allocated ");
5ba3b20e
AB
5273 dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
5274 }
6cb06a8c 5275 gdb_printf ("\n");
5ba3b20e 5276 }
6cb06a8c 5277 gdb_printf ("%s\n", host_address_to_string (type->fields ()));
1f704f76 5278 for (idx = 0; idx < type->num_fields (); idx++)
c906108c 5279 {
20aadb93 5280 field &fld = type->field (idx);
78134374 5281 if (type->code () == TYPE_CODE_ENUM)
6cb06a8c 5282 gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
20aadb93 5283 idx, plongest (fld.loc_enumval ()));
14e75d8e 5284 else
6cb06a8c 5285 gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
20aadb93
TT
5286 idx, plongest (fld.loc_bitpos ()),
5287 fld.bitsize ());
5ffb4736 5288 gdb_printf ("%s name '%s' (%s)",
20aadb93
TT
5289 host_address_to_string (fld.type ()),
5290 fld.name () != NULL
5291 ? fld.name ()
6cb06a8c 5292 : "<NULL>",
20aadb93
TT
5293 host_address_to_string (fld.name ()));
5294 if (fld.is_virtual ())
5ffb4736
TT
5295 gdb_printf (" virtual");
5296
20aadb93 5297 if (fld.is_private ())
5ffb4736 5298 gdb_printf (" private");
20aadb93 5299 else if (fld.is_protected ())
5ffb4736 5300 gdb_printf (" protected");
20aadb93 5301 else if (fld.is_ignored ())
5ffb4736
TT
5302 gdb_printf (" ignored");
5303
5304 gdb_printf ("\n");
20aadb93 5305 if (fld.type () != NULL)
c906108c 5306 {
20aadb93 5307 recursive_dump_type (fld.type (), spaces + 4);
c906108c
SS
5308 }
5309 }
78134374 5310 if (type->code () == TYPE_CODE_RANGE)
43bbcdc2 5311 {
6cb06a8c 5312 gdb_printf ("%*slow ", spaces, "");
53d5a2a5 5313 dump_dynamic_prop (type->bounds ()->low);
6cb06a8c 5314 gdb_printf (" high ");
53d5a2a5 5315 dump_dynamic_prop (type->bounds ()->high);
6cb06a8c 5316 gdb_printf ("\n");
43bbcdc2 5317 }
c906108c 5318
b4ba55a1
JB
5319 switch (TYPE_SPECIFIC_FIELD (type))
5320 {
9c808ba1
TT
5321 case TYPE_SPECIFIC_CPLUS_STUFF:
5322 gdb_printf ("%*scplus_stuff %s\n", spaces, "",
5323 host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
5324 print_cplus_stuff (type, spaces);
5325 break;
8da61cc4 5326
9c808ba1
TT
5327 case TYPE_SPECIFIC_GNAT_STUFF:
5328 gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
5329 host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
5330 print_gnat_stuff (type, spaces);
5331 break;
701c159d 5332
9c808ba1
TT
5333 case TYPE_SPECIFIC_FLOATFORMAT:
5334 gdb_printf ("%*sfloatformat ", spaces, "");
5335 if (TYPE_FLOATFORMAT (type) == NULL
5336 || TYPE_FLOATFORMAT (type)->name == NULL)
5337 gdb_puts ("(null)");
5338 else
5339 gdb_puts (TYPE_FLOATFORMAT (type)->name);
5340 gdb_puts ("\n");
5341 break;
c906108c 5342
9c808ba1
TT
5343 case TYPE_SPECIFIC_FUNC:
5344 gdb_printf ("%*scalling_convention %d\n", spaces, "",
5345 TYPE_CALLING_CONVENTION (type));
5346 /* tail_call_list is not printed. */
5347 break;
09e2d7c7 5348
9c808ba1
TT
5349 case TYPE_SPECIFIC_SELF_TYPE:
5350 gdb_printf ("%*sself_type %s\n", spaces, "",
5351 host_address_to_string (TYPE_SELF_TYPE (type)));
5352 break;
20a5fcbd 5353
9c808ba1
TT
5354 case TYPE_SPECIFIC_FIXED_POINT:
5355 gdb_printf ("%*sfixed_point_info ", spaces, "");
5356 print_fixed_point_type_info (type, spaces);
5357 gdb_puts ("\n");
5358 break;
09584414 5359
20a5fcbd
TT
5360 case TYPE_SPECIFIC_INT:
5361 if (type->bit_size_differs_p ())
5362 {
5363 unsigned bit_size = type->bit_size ();
5364 unsigned bit_off = type->bit_offset ();
6cb06a8c
TT
5365 gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
5366 bit_size, bit_off);
20a5fcbd
TT
5367 }
5368 break;
c906108c 5369 }
b4ba55a1 5370
c906108c
SS
5371 if (spaces == 0)
5372 obstack_free (&dont_print_type_obstack, NULL);
5373}
5212577a 5374\f
ae5a43e0
DJ
5375/* Trivial helpers for the libiberty hash table, for mapping one
5376 type to another. */
5377
bde539c2 5378struct type_pair
ae5a43e0 5379{
fd90ace4
YQ
5380 type_pair (struct type *old_, struct type *newobj_)
5381 : old (old_), newobj (newobj_)
5382 {}
5383
5384 struct type * const old, * const newobj;
ae5a43e0
DJ
5385};
5386
5387static hashval_t
5388type_pair_hash (const void *item)
5389{
9a3c8263 5390 const struct type_pair *pair = (const struct type_pair *) item;
d8734c88 5391
ae5a43e0
DJ
5392 return htab_hash_pointer (pair->old);
5393}
5394
5395static int
5396type_pair_eq (const void *item_lhs, const void *item_rhs)
5397{
9a3c8263
SM
5398 const struct type_pair *lhs = (const struct type_pair *) item_lhs;
5399 const struct type_pair *rhs = (const struct type_pair *) item_rhs;
d8734c88 5400
ae5a43e0
DJ
5401 return lhs->old == rhs->old;
5402}
5403
5404/* Allocate the hash table used by copy_type_recursive to walk
bde539c2 5405 types without duplicates. */
ae5a43e0 5406
6108fd18 5407htab_up
bde539c2 5408create_copied_types_hash ()
ae5a43e0 5409{
bde539c2
TT
5410 return htab_up (htab_create_alloc (1, type_pair_hash, type_pair_eq,
5411 htab_delete_entry<type_pair>,
5412 xcalloc, xfree));
ae5a43e0
DJ
5413}
5414
d9823cbb
KB
5415/* Recursively copy (deep copy) a dynamic attribute list of a type. */
5416
5417static struct dynamic_prop_list *
bde539c2 5418copy_dynamic_prop_list (struct obstack *storage,
d9823cbb
KB
5419 struct dynamic_prop_list *list)
5420{
5421 struct dynamic_prop_list *copy = list;
5422 struct dynamic_prop_list **node_ptr = &copy;
5423
5424 while (*node_ptr != NULL)
5425 {
5426 struct dynamic_prop_list *node_copy;
5427
224c3ddb 5428 node_copy = ((struct dynamic_prop_list *)
bde539c2 5429 obstack_copy (storage, *node_ptr,
224c3ddb 5430 sizeof (struct dynamic_prop_list)));
283a9958 5431 node_copy->prop = (*node_ptr)->prop;
d9823cbb
KB
5432 *node_ptr = node_copy;
5433
5434 node_ptr = &node_copy->next;
5435 }
5436
5437 return copy;
5438}
5439
7ba81444 5440/* Recursively copy (deep copy) TYPE, if it is associated with
eed8b28a
PP
5441 OBJFILE. Return a new type owned by the gdbarch associated with the type, a
5442 saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
5443 it is not associated with OBJFILE. */
ae5a43e0
DJ
5444
5445struct type *
bde539c2 5446copy_type_recursive (struct type *type, htab_t copied_types)
ae5a43e0 5447{
ae5a43e0
DJ
5448 void **slot;
5449 struct type *new_type;
5450
30625020 5451 if (!type->is_objfile_owned ())
ae5a43e0
DJ
5452 return type;
5453
fd90ace4
YQ
5454 struct type_pair pair (type, nullptr);
5455
ae5a43e0
DJ
5456 slot = htab_find_slot (copied_types, &pair, INSERT);
5457 if (*slot != NULL)
fe978cb0 5458 return ((struct type_pair *) *slot)->newobj;
ae5a43e0 5459
c9eb9f18 5460 new_type = type_allocator (type->arch ()).new_type ();
ae5a43e0
DJ
5461
5462 /* We must add the new type to the hash table immediately, in case
5463 we encounter this type again during a recursive call below. */
bde539c2 5464 struct type_pair *stored = new type_pair (type, new_type);
fd90ace4 5465
ae5a43e0
DJ
5466 *slot = stored;
5467
876cecd0
TT
5468 /* Copy the common fields of types. For the main type, we simply
5469 copy the entire thing and then update specific fields as needed. */
5470 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
5b7d941b 5471
8ee511af 5472 new_type->set_owner (type->arch ());
876cecd0 5473
7d93a1e0
SM
5474 if (type->name ())
5475 new_type->set_name (xstrdup (type->name ()));
ae5a43e0 5476
314ad88d 5477 new_type->set_instance_flags (type->instance_flags ());
df86565b 5478 new_type->set_length (type->length ());
ae5a43e0
DJ
5479
5480 /* Copy the fields. */
1f704f76 5481 if (type->num_fields ())
ae5a43e0
DJ
5482 {
5483 int i, nfields;
5484
1f704f76 5485 nfields = type->num_fields ();
2774f2da 5486 new_type->alloc_fields (type->num_fields ());
3cabb6b0 5487
ae5a43e0
DJ
5488 for (i = 0; i < nfields; i++)
5489 {
321d8b3f 5490 new_type->field (i).set_is_artificial
454977cd 5491 (type->field (i).is_artificial ());
3757d2d4 5492 new_type->field (i).set_bitsize (type->field (i).bitsize ());
940da03e 5493 if (type->field (i).type ())
5d14b6e5 5494 new_type->field (i).set_type
bde539c2 5495 (copy_type_recursive (type->field (i).type (), copied_types));
33d16dd9
SM
5496 if (type->field (i).name ())
5497 new_type->field (i).set_name (xstrdup (type->field (i).name ()));
2ad53ea1
SM
5498
5499 switch (type->field (i).loc_kind ())
ae5a43e0 5500 {
d6a843b5 5501 case FIELD_LOC_KIND_BITPOS:
b610c045 5502 new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
d6a843b5 5503 break;
14e75d8e 5504 case FIELD_LOC_KIND_ENUMVAL:
970db518 5505 new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
14e75d8e 5506 break;
d6a843b5 5507 case FIELD_LOC_KIND_PHYSADDR:
cd3f655c 5508 new_type->field (i).set_loc_physaddr
e06c3e11 5509 (type->field (i).loc_physaddr ());
d6a843b5
JK
5510 break;
5511 case FIELD_LOC_KIND_PHYSNAME:
cd3f655c 5512 new_type->field (i).set_loc_physname
fcbbbd90 5513 (xstrdup (type->field (i).loc_physname ()));
d6a843b5 5514 break;
287de656
SM
5515 case FIELD_LOC_KIND_DWARF_BLOCK:
5516 new_type->field (i).set_loc_dwarf_block
51e36a3a 5517 (type->field (i).loc_dwarf_block ());
287de656 5518 break;
d6a843b5 5519 default:
f34652de 5520 internal_error (_("Unexpected type field location kind: %d"),
2ad53ea1 5521 type->field (i).loc_kind ());
ae5a43e0
DJ
5522 }
5523 }
5524 }
5525
0963b4bd 5526 /* For range types, copy the bounds information. */
78134374 5527 if (type->code () == TYPE_CODE_RANGE)
43bbcdc2 5528 {
c4dfcb36 5529 range_bounds *bounds
dda83cd7 5530 = ((struct range_bounds *) TYPE_ALLOC
c4dfcb36
SM
5531 (new_type, sizeof (struct range_bounds)));
5532
5533 *bounds = *type->bounds ();
5534 new_type->set_bounds (bounds);
43bbcdc2
PH
5535 }
5536
98d48915
SM
5537 if (type->main_type->dyn_prop_list != NULL)
5538 new_type->main_type->dyn_prop_list
bde539c2 5539 = copy_dynamic_prop_list (gdbarch_obstack (new_type->arch_owner ()),
98d48915 5540 type->main_type->dyn_prop_list);
d9823cbb 5541
3cdcd0ce 5542
ae5a43e0 5543 /* Copy pointers to other types. */
27710edb 5544 if (type->target_type ())
8a50fdce 5545 new_type->set_target_type
27710edb 5546 (copy_type_recursive (type->target_type (), copied_types));
f6b3afbf 5547
ae5a43e0
DJ
5548 /* Maybe copy the type_specific bits.
5549
5550 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
5551 base classes and methods. There's no fundamental reason why we
5552 can't, but at the moment it is not needed. */
5553
f6b3afbf
DE
5554 switch (TYPE_SPECIFIC_FIELD (type))
5555 {
5556 case TYPE_SPECIFIC_NONE:
5557 break;
5558 case TYPE_SPECIFIC_FUNC:
5559 INIT_FUNC_SPECIFIC (new_type);
5560 TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
5561 TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
5562 TYPE_TAIL_CALL_LIST (new_type) = NULL;
5563 break;
5564 case TYPE_SPECIFIC_FLOATFORMAT:
5565 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
5566 break;
5567 case TYPE_SPECIFIC_CPLUS_STUFF:
5568 INIT_CPLUS_SPECIFIC (new_type);
5569 break;
5570 case TYPE_SPECIFIC_GNAT_STUFF:
5571 INIT_GNAT_SPECIFIC (new_type);
5572 break;
09e2d7c7
DE
5573 case TYPE_SPECIFIC_SELF_TYPE:
5574 set_type_self_type (new_type,
bde539c2 5575 copy_type_recursive (TYPE_SELF_TYPE (type),
09e2d7c7
DE
5576 copied_types));
5577 break;
09584414
JB
5578 case TYPE_SPECIFIC_FIXED_POINT:
5579 INIT_FIXED_POINT_SPECIFIC (new_type);
2a12c336
JB
5580 new_type->fixed_point_info ().scaling_factor
5581 = type->fixed_point_info ().scaling_factor;
09584414 5582 break;
20a5fcbd
TT
5583 case TYPE_SPECIFIC_INT:
5584 TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
5585 TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
5586 = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
5587 break;
5588
f6b3afbf
DE
5589 default:
5590 gdb_assert_not_reached ("bad type_specific_kind");
5591 }
ae5a43e0
DJ
5592
5593 return new_type;
5594}
5595
4af88198 5596/* Make a copy of the given TYPE, except that the pointer & reference
8e2da165 5597 types are not preserved. */
4af88198
JB
5598
5599struct type *
5600copy_type (const struct type *type)
5601{
9fa83a7a 5602 struct type *new_type = type_allocator (type).new_type ();
314ad88d 5603 new_type->set_instance_flags (type->instance_flags ());
df86565b 5604 new_type->set_length (type->length ());
4af88198
JB
5605 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
5606 sizeof (struct main_type));
98d48915 5607 if (type->main_type->dyn_prop_list != NULL)
8e2da165
TT
5608 {
5609 struct obstack *storage = (type->is_objfile_owned ()
5610 ? &type->objfile_owner ()->objfile_obstack
5611 : gdbarch_obstack (type->arch_owner ()));
5612 new_type->main_type->dyn_prop_list
5613 = copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
5614 }
4af88198
JB
5615
5616 return new_type;
5617}
5212577a 5618\f
e9bb382b
UW
5619/* Helper functions to initialize architecture-specific types. */
5620
e9bb382b 5621/* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
77b7c781 5622 NAME is the type name. BIT is the size of the flag word in bits. */
5212577a 5623
e9bb382b 5624struct type *
77b7c781 5625arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
e9bb382b 5626{
e9bb382b
UW
5627 struct type *type;
5628
cc495054 5629 type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
653223d3 5630 type->set_is_unsigned (true);
81516450 5631 /* Pre-allocate enough space assuming every field is one bit. */
2774f2da
TV
5632 type->alloc_fields (bit);
5633 type->set_num_fields (0);
e9bb382b
UW
5634
5635 return type;
5636}
5637
5638/* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
81516450
DE
5639 position BITPOS is called NAME. Pass NAME as "" for fields that
5640 should not be printed. */
5641
5642void
5643append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
695bfa52 5644 struct type *field_type, const char *name)
81516450 5645{
df86565b 5646 int type_bitsize = type->length () * TARGET_CHAR_BIT;
1f704f76 5647 int field_nr = type->num_fields ();
81516450 5648
78134374 5649 gdb_assert (type->code () == TYPE_CODE_FLAGS);
1f704f76 5650 gdb_assert (type->num_fields () + 1 <= type_bitsize);
81516450 5651 gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
602885d8 5652 gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
81516450
DE
5653 gdb_assert (name != NULL);
5654
5a8edb75 5655 type->set_num_fields (type->num_fields () + 1);
d3fd12df 5656 type->field (field_nr).set_name (xstrdup (name));
5d14b6e5 5657 type->field (field_nr).set_type (field_type);
cd3f655c 5658 type->field (field_nr).set_loc_bitpos (start_bitpos);
886176b8 5659 type->field (field_nr).set_bitsize (nr_bits);
81516450
DE
5660}
5661
5662/* Special version of append_flags_type_field to add a flag field.
5663 Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
e9bb382b 5664 position BITPOS is called NAME. */
5212577a 5665
e9bb382b 5666void
695bfa52 5667append_flags_type_flag (struct type *type, int bitpos, const char *name)
e9bb382b 5668{
81516450 5669 append_flags_type_field (type, bitpos, 1,
8ee511af 5670 builtin_type (type->arch ())->builtin_bool,
81516450 5671 name);
e9bb382b
UW
5672}
5673
5674/* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
5675 specified by CODE) associated with GDBARCH. NAME is the type name. */
5212577a 5676
e9bb382b 5677struct type *
695bfa52
TT
5678arch_composite_type (struct gdbarch *gdbarch, const char *name,
5679 enum type_code code)
e9bb382b
UW
5680{
5681 struct type *t;
d8734c88 5682
e9bb382b 5683 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
cc495054 5684 t = type_allocator (gdbarch).new_type (code, 0, NULL);
d0e39ea2 5685 t->set_name (name);
e9bb382b
UW
5686 INIT_CPLUS_SPECIFIC (t);
5687 return t;
5688}
5689
5690/* Add new field with name NAME and type FIELD to composite type T.
f5dff777
DJ
5691 Do not set the field's position or adjust the type's length;
5692 the caller should do so. Return the new field. */
5212577a 5693
f5dff777 5694struct field *
695bfa52 5695append_composite_type_field_raw (struct type *t, const char *name,
f5dff777 5696 struct type *field)
e9bb382b
UW
5697{
5698 struct field *f;
d8734c88 5699
1f704f76 5700 t->set_num_fields (t->num_fields () + 1);
80fc5e77 5701 t->set_fields (XRESIZEVEC (struct field, t->fields (),
3cabb6b0 5702 t->num_fields ()));
80fc5e77 5703 f = &t->field (t->num_fields () - 1);
e9bb382b 5704 memset (f, 0, sizeof f[0]);
5d14b6e5 5705 f[0].set_type (field);
d3fd12df 5706 f[0].set_name (name);
f5dff777
DJ
5707 return f;
5708}
5709
5710/* Add new field with name NAME and type FIELD to composite type T.
5711 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5212577a 5712
f5dff777 5713void
695bfa52 5714append_composite_type_field_aligned (struct type *t, const char *name,
f5dff777
DJ
5715 struct type *field, int alignment)
5716{
5717 struct field *f = append_composite_type_field_raw (t, name, field);
d8734c88 5718
78134374 5719 if (t->code () == TYPE_CODE_UNION)
e9bb382b 5720 {
df86565b
SM
5721 if (t->length () < field->length ())
5722 t->set_length (field->length ());
e9bb382b 5723 }
78134374 5724 else if (t->code () == TYPE_CODE_STRUCT)
e9bb382b 5725 {
df86565b 5726 t->set_length (t->length () + field->length ());
1f704f76 5727 if (t->num_fields () > 1)
e9bb382b 5728 {
cd3f655c 5729 f->set_loc_bitpos
df86565b
SM
5730 (f[-1].loc_bitpos ()
5731 + (f[-1].type ()->length () * TARGET_CHAR_BIT));
e9bb382b
UW
5732
5733 if (alignment)
5734 {
86c3c1fc
AB
5735 int left;
5736
5737 alignment *= TARGET_CHAR_BIT;
3a543e21 5738 left = f[0].loc_bitpos () % alignment;
d8734c88 5739
e9bb382b
UW
5740 if (left)
5741 {
3a543e21 5742 f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
b6cdbc9a
SM
5743 t->set_length
5744 (t->length () + (alignment - left) / TARGET_CHAR_BIT);
e9bb382b
UW
5745 }
5746 }
5747 }
5748 }
5749}
5750
5751/* Add new field with name NAME and type FIELD to composite type T. */
5212577a 5752
e9bb382b 5753void
695bfa52 5754append_composite_type_field (struct type *t, const char *name,
e9bb382b
UW
5755 struct type *field)
5756{
5757 append_composite_type_field_aligned (t, name, field, 0);
5758}
5759
09584414
JB
5760\f
5761
5762/* We manage the lifetimes of fixed_point_type_info objects by
5763 attaching them to the objfile. Currently, these objects are
5764 modified during construction, and GMP does not provide a way to
5765 hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
5766 them. If we did do this, they could be moved to the per-BFD and
5767 shared across objfiles. */
5768typedef std::vector<std::unique_ptr<fixed_point_type_info>>
5769 fixed_point_type_storage;
5770
5771/* Key used for managing the storage of fixed-point type info. */
08b8a139 5772static const struct registry<objfile>::key<fixed_point_type_storage>
09584414
JB
5773 fixed_point_objfile_key;
5774
5775/* See gdbtypes.h. */
5776
2a12c336 5777void
09584414
JB
5778allocate_fixed_point_type_info (struct type *type)
5779{
6b62451a 5780 auto up = std::make_unique<fixed_point_type_info> ();
2a12c336 5781 fixed_point_type_info *info;
09584414 5782
30625020 5783 if (type->is_objfile_owned ())
09584414
JB
5784 {
5785 fixed_point_type_storage *storage
6ac37371 5786 = fixed_point_objfile_key.get (type->objfile_owner ());
09584414 5787 if (storage == nullptr)
6ac37371 5788 storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
2a12c336 5789 info = up.get ();
09584414
JB
5790 storage->push_back (std::move (up));
5791 }
5792 else
5793 {
5794 /* We just leak the memory, because that's what we do generally
5795 for non-objfile-attached types. */
2a12c336 5796 info = up.release ();
09584414
JB
5797 }
5798
2a12c336 5799 type->set_fixed_point_info (info);
09584414
JB
5800}
5801
5802/* See gdbtypes.h. */
5803
5804bool
5805is_fixed_point_type (struct type *type)
5806{
5807 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
27710edb 5808 type = check_typedef (type)->target_type ();
09584414
JB
5809 type = check_typedef (type);
5810
5811 return type->code () == TYPE_CODE_FIXED_POINT;
5812}
5813
5814/* See gdbtypes.h. */
5815
5816struct type *
d19937a7 5817type::fixed_point_type_base_type ()
09584414 5818{
d19937a7
JB
5819 struct type *type = this;
5820
09584414 5821 while (check_typedef (type)->code () == TYPE_CODE_RANGE)
27710edb 5822 type = check_typedef (type)->target_type ();
09584414
JB
5823 type = check_typedef (type);
5824
5825 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
5826 return type;
5827}
5828
5829/* See gdbtypes.h. */
5830
5831const gdb_mpq &
e6fcee3a 5832type::fixed_point_scaling_factor ()
09584414 5833{
e6fcee3a 5834 struct type *type = this->fixed_point_type_base_type ();
09584414 5835
2a12c336 5836 return type->fixed_point_info ().scaling_factor;
09584414
JB
5837}
5838
2774f2da
TV
5839/* See gdbtypes.h. */
5840
5841void
5842type::alloc_fields (unsigned int nfields, bool init)
5843{
5844 this->set_num_fields (nfields);
5845
5846 if (nfields == 0)
5847 {
5848 this->main_type->flds_bnds.fields = nullptr;
5849 return;
5850 }
5851
5852 size_t size = nfields * sizeof (*this->fields ());
5853 struct field *fields
5854 = (struct field *) (init
5855 ? TYPE_ZALLOC (this, size)
5856 : TYPE_ALLOC (this, size));
5857
5858 this->main_type->flds_bnds.fields = fields;
5859}
5860
5861/* See gdbtypes.h. */
5862
5863void
5864type::copy_fields (struct type *src)
5865{
5866 unsigned int nfields = src->num_fields ();
5867 alloc_fields (nfields, false);
5868 if (nfields == 0)
5869 return;
5870
5871 size_t size = nfields * sizeof (*this->fields ());
5872 memcpy (this->fields (), src->fields (), size);
5873}
5874
5875/* See gdbtypes.h. */
5876
5877void
5878type::copy_fields (std::vector<struct field> &src)
5879{
5880 unsigned int nfields = src.size ();
5881 alloc_fields (nfields, false);
5882 if (nfields == 0)
5883 return;
5884
5885 size_t size = nfields * sizeof (*this->fields ());
5886 memcpy (this->fields (), src.data (), size);
5887}
84914f59 5888
76fc0f62
TT
5889/* See gdbtypes.h. */
5890
5891bool
5892type::is_string_like ()
5893{
5894 const language_defn *defn = language_def (this->language ());
5895 return defn->is_string_type_p (this);
5896}
5897
5898/* See gdbtypes.h. */
5899
84914f59
TT
5900bool
5901type::is_array_like ()
5902{
5903 if (code () == TYPE_CODE_ARRAY)
5904 return true;
76fc0f62
TT
5905 const language_defn *defn = language_def (this->language ());
5906 return defn->is_array_like (this);
84914f59
TT
5907}
5908
09584414
JB
5909\f
5910
cb275538 5911static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
000177f0 5912
cb275538
TT
5913static struct builtin_type *
5914create_gdbtypes_data (struct gdbarch *gdbarch)
000177f0 5915{
cb275538 5916 struct builtin_type *builtin_type = new struct builtin_type;
000177f0 5917
cc495054
TT
5918 type_allocator alloc (gdbarch);
5919
46bf5051 5920 /* Basic types. */
e9bb382b 5921 builtin_type->builtin_void
cc495054 5922 = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
e9bb382b 5923 builtin_type->builtin_char
2d39ccd3 5924 = init_integer_type (alloc, TARGET_CHAR_BIT,
e9bb382b 5925 !gdbarch_char_signed (gdbarch), "char");
15152a54 5926 builtin_type->builtin_char->set_has_no_signedness (true);
e9bb382b 5927 builtin_type->builtin_signed_char
2d39ccd3 5928 = init_integer_type (alloc, TARGET_CHAR_BIT,
e9bb382b
UW
5929 0, "signed char");
5930 builtin_type->builtin_unsigned_char
2d39ccd3 5931 = init_integer_type (alloc, TARGET_CHAR_BIT,
e9bb382b
UW
5932 1, "unsigned char");
5933 builtin_type->builtin_short
2d39ccd3 5934 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
e9bb382b
UW
5935 0, "short");
5936 builtin_type->builtin_unsigned_short
2d39ccd3 5937 = init_integer_type (alloc, gdbarch_short_bit (gdbarch),
e9bb382b
UW
5938 1, "unsigned short");
5939 builtin_type->builtin_int
2d39ccd3 5940 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
e9bb382b
UW
5941 0, "int");
5942 builtin_type->builtin_unsigned_int
2d39ccd3 5943 = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
e9bb382b
UW
5944 1, "unsigned int");
5945 builtin_type->builtin_long
2d39ccd3 5946 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
e9bb382b
UW
5947 0, "long");
5948 builtin_type->builtin_unsigned_long
2d39ccd3 5949 = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
e9bb382b
UW
5950 1, "unsigned long");
5951 builtin_type->builtin_long_long
2d39ccd3 5952 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
e9bb382b
UW
5953 0, "long long");
5954 builtin_type->builtin_unsigned_long_long
2d39ccd3 5955 = init_integer_type (alloc, gdbarch_long_long_bit (gdbarch),
e9bb382b 5956 1, "unsigned long long");
a6d0f249 5957 builtin_type->builtin_half
77c5f496 5958 = init_float_type (alloc, gdbarch_half_bit (gdbarch),
a6d0f249 5959 "half", gdbarch_half_format (gdbarch));
70bd8e24 5960 builtin_type->builtin_float
77c5f496 5961 = init_float_type (alloc, gdbarch_float_bit (gdbarch),
27067745 5962 "float", gdbarch_float_format (gdbarch));
2a67f09d 5963 builtin_type->builtin_bfloat16
77c5f496 5964 = init_float_type (alloc, gdbarch_bfloat16_bit (gdbarch),
2a67f09d 5965 "bfloat16", gdbarch_bfloat16_format (gdbarch));
70bd8e24 5966 builtin_type->builtin_double
77c5f496 5967 = init_float_type (alloc, gdbarch_double_bit (gdbarch),
27067745 5968 "double", gdbarch_double_format (gdbarch));
70bd8e24 5969 builtin_type->builtin_long_double
77c5f496 5970 = init_float_type (alloc, gdbarch_long_double_bit (gdbarch),
27067745 5971 "long double", gdbarch_long_double_format (gdbarch));
70bd8e24 5972 builtin_type->builtin_complex
5b930b45 5973 = init_complex_type ("complex", builtin_type->builtin_float);
70bd8e24 5974 builtin_type->builtin_double_complex
5b930b45 5975 = init_complex_type ("double complex", builtin_type->builtin_double);
e9bb382b 5976 builtin_type->builtin_string
cc495054 5977 = alloc.new_type (TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
e9bb382b 5978 builtin_type->builtin_bool
46c04ea3 5979 = init_boolean_type (alloc, TARGET_CHAR_BIT, 1, "bool");
000177f0 5980
7678ef8f
TJB
5981 /* The following three are about decimal floating point types, which
5982 are 32-bits, 64-bits and 128-bits respectively. */
5983 builtin_type->builtin_decfloat
0776344a 5984 = init_decfloat_type (alloc, 32, "_Decimal32");
7678ef8f 5985 builtin_type->builtin_decdouble
0776344a 5986 = init_decfloat_type (alloc, 64, "_Decimal64");
7678ef8f 5987 builtin_type->builtin_declong
0776344a 5988 = init_decfloat_type (alloc, 128, "_Decimal128");
7678ef8f 5989
69feb676 5990 /* "True" character types. */
e9bb382b 5991 builtin_type->builtin_true_char
f50b437c 5992 = init_character_type (alloc, TARGET_CHAR_BIT, 0, "true character");
e9bb382b 5993 builtin_type->builtin_true_unsigned_char
f50b437c 5994 = init_character_type (alloc, TARGET_CHAR_BIT, 1, "true character");
69feb676 5995
df4df182 5996 /* Fixed-size integer types. */
e9bb382b 5997 builtin_type->builtin_int0
2d39ccd3 5998 = init_integer_type (alloc, 0, 0, "int0_t");
e9bb382b 5999 builtin_type->builtin_int8
2d39ccd3 6000 = init_integer_type (alloc, 8, 0, "int8_t");
e9bb382b 6001 builtin_type->builtin_uint8
2d39ccd3 6002 = init_integer_type (alloc, 8, 1, "uint8_t");
e9bb382b 6003 builtin_type->builtin_int16
2d39ccd3 6004 = init_integer_type (alloc, 16, 0, "int16_t");
e9bb382b 6005 builtin_type->builtin_uint16
2d39ccd3 6006 = init_integer_type (alloc, 16, 1, "uint16_t");
d1908f2d 6007 builtin_type->builtin_int24
2d39ccd3 6008 = init_integer_type (alloc, 24, 0, "int24_t");
d1908f2d 6009 builtin_type->builtin_uint24
2d39ccd3 6010 = init_integer_type (alloc, 24, 1, "uint24_t");
e9bb382b 6011 builtin_type->builtin_int32
2d39ccd3 6012 = init_integer_type (alloc, 32, 0, "int32_t");
e9bb382b 6013 builtin_type->builtin_uint32
2d39ccd3 6014 = init_integer_type (alloc, 32, 1, "uint32_t");
e9bb382b 6015 builtin_type->builtin_int64
2d39ccd3 6016 = init_integer_type (alloc, 64, 0, "int64_t");
e9bb382b 6017 builtin_type->builtin_uint64
2d39ccd3 6018 = init_integer_type (alloc, 64, 1, "uint64_t");
e9bb382b 6019 builtin_type->builtin_int128
2d39ccd3 6020 = init_integer_type (alloc, 128, 0, "int128_t");
e9bb382b 6021 builtin_type->builtin_uint128
2d39ccd3 6022 = init_integer_type (alloc, 128, 1, "uint128_t");
314ad88d
PA
6023
6024 builtin_type->builtin_int8->set_instance_flags
6025 (builtin_type->builtin_int8->instance_flags ()
6026 | TYPE_INSTANCE_FLAG_NOTTEXT);
6027
6028 builtin_type->builtin_uint8->set_instance_flags
6029 (builtin_type->builtin_uint8->instance_flags ()
6030 | TYPE_INSTANCE_FLAG_NOTTEXT);
df4df182 6031
9a22f0d0
PM
6032 /* Wide character types. */
6033 builtin_type->builtin_char16
2d39ccd3 6034 = init_integer_type (alloc, 16, 1, "char16_t");
9a22f0d0 6035 builtin_type->builtin_char32
2d39ccd3 6036 = init_integer_type (alloc, 32, 1, "char32_t");
53375380 6037 builtin_type->builtin_wchar
2d39ccd3 6038 = init_integer_type (alloc, gdbarch_wchar_bit (gdbarch),
53375380 6039 !gdbarch_wchar_signed (gdbarch), "wchar_t");
9a22f0d0 6040
46bf5051 6041 /* Default data/code pointer types. */
e9bb382b
UW
6042 builtin_type->builtin_data_ptr
6043 = lookup_pointer_type (builtin_type->builtin_void);
6044 builtin_type->builtin_func_ptr
6045 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
0875794a
JK
6046 builtin_type->builtin_func_func
6047 = lookup_function_type (builtin_type->builtin_func_ptr);
46bf5051 6048
78267919 6049 /* This type represents a GDB internal function. */
e9bb382b 6050 builtin_type->internal_fn
cc495054
TT
6051 = alloc.new_type (TYPE_CODE_INTERNAL_FUNCTION, 0,
6052 "<internal function>");
78267919 6053
e81e7f5e
SC
6054 /* This type represents an xmethod. */
6055 builtin_type->xmethod
cc495054 6056 = alloc.new_type (TYPE_CODE_XMETHOD, 0, "<xmethod>");
e81e7f5e 6057
a9a775da
TT
6058 /* This type represents a type that was unrecognized in symbol read-in. */
6059 builtin_type->builtin_error
6060 = alloc.new_type (TYPE_CODE_ERROR, 0, "<unknown type>");
6061
6062 /* The following set of types is used for symbols with no
6063 debug information. */
6064 builtin_type->nodebug_text_symbol
6065 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6066 "<text variable, no debug info>");
6067
6068 builtin_type->nodebug_text_gnu_ifunc_symbol
6069 = alloc.new_type (TYPE_CODE_FUNC, TARGET_CHAR_BIT,
6070 "<text gnu-indirect-function variable, no debug info>");
6071 builtin_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
6072
6073 builtin_type->nodebug_got_plt_symbol
6074 = init_pointer_type (alloc, gdbarch_addr_bit (gdbarch),
6075 "<text from jump slot in .got.plt, no debug info>",
6076 builtin_type->nodebug_text_symbol);
6077 builtin_type->nodebug_data_symbol
6078 = alloc.new_type (TYPE_CODE_ERROR, 0, "<data variable, no debug info>");
6079 builtin_type->nodebug_unknown_symbol
6080 = alloc.new_type (TYPE_CODE_ERROR, 0,
6081 "<variable (not text or data), no debug info>");
6082 builtin_type->nodebug_tls_symbol
6083 = alloc.new_type (TYPE_CODE_ERROR, 0,
6084 "<thread local variable, no debug info>");
6085
6086 /* NOTE: on some targets, addresses and pointers are not necessarily
6087 the same.
6088
6089 The upshot is:
6090 - gdb's `struct type' always describes the target's
6091 representation.
6092 - gdb's `struct value' objects should always hold values in
6093 target form.
6094 - gdb's CORE_ADDR values are addresses in the unified virtual
6095 address space that the assembler and linker work with. Thus,
6096 since target_read_memory takes a CORE_ADDR as an argument, it
6097 can access any memory on the target, even if the processor has
6098 separate code and data address spaces.
6099
6100 In this context, builtin_type->builtin_core_addr is a bit odd:
6101 it's a target type for a value the target will never see. It's
6102 only used to hold the values of (typeless) linker symbols, which
6103 are indeed in the unified virtual address space. */
6104
6105 builtin_type->builtin_core_addr
6106 = init_integer_type (alloc, gdbarch_addr_bit (gdbarch), 1,
6107 "__CORE_ADDR");
46bf5051
UW
6108 return builtin_type;
6109}
6110
cb275538
TT
6111const struct builtin_type *
6112builtin_type (struct gdbarch *gdbarch)
6113{
6114 struct builtin_type *result = gdbtypes_data.get (gdbarch);
6115 if (result == nullptr)
6116 {
6117 result = create_gdbtypes_data (gdbarch);
6118 gdbtypes_data.set (gdbarch, result);
6119 }
6120 return result;
6121}
6122
2d1bc552 6123const struct builtin_type *
a8ed3dde 6124builtin_type (struct objfile *objfile)
46bf5051 6125{
2d1bc552 6126 return builtin_type (objfile->arch ());
000177f0
AC
6127}
6128
b84aaada
SM
6129/* See gdbtypes.h. */
6130
6131CORE_ADDR
6132call_site::pc () const
6133{
1e73d09f 6134 return per_objfile->relocate (m_unrelocated_pc);
b84aaada
SM
6135}
6136
6c265988 6137void _initialize_gdbtypes ();
c906108c 6138void
6c265988 6139_initialize_gdbtypes ()
c906108c 6140{
ccce17b0
YQ
6141 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
6142 _("Set debugging of C++ overloading."),
6143 _("Show debugging of C++ overloading."),
6144 _("When enabled, ranking of the "
6145 "functions is displayed."),
6146 NULL,
6147 show_overload_debug,
6148 &setdebuglist, &showdebuglist);
5674de60 6149
7ba81444 6150 /* Add user knob for controlling resolution of opaque types. */
5674de60 6151 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3e43a32a
MS
6152 &opaque_type_resolution,
6153 _("Set resolution of opaque struct/class/union"
6154 " types (if set before loading symbols)."),
6155 _("Show resolution of opaque struct/class/union"
6156 " types (if set before loading symbols)."),
6157 NULL, NULL,
5674de60
UW
6158 show_opaque_type_resolution,
6159 &setlist, &showlist);
a451cb65
KS
6160
6161 /* Add an option to permit non-strict type checking. */
6162 add_setshow_boolean_cmd ("type", class_support,
6163 &strict_type_checking,
6164 _("Set strict type checking."),
6165 _("Show strict type checking."),
6166 NULL, NULL,
6167 show_strict_type_checking,
6168 &setchecklist, &showchecklist);
c906108c 6169}