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