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