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