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