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