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