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