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