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