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