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