gdb:
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
CommitLineData
c906108c 1/* Support routines for manipulating internal types for GDB.
4f2aea11 2
9b254dd1 3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
4c38e0a4
JB
4 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
4f2aea11 6
c906108c
SS
7 Contributed by Cygnus Support, using pieces from other GDB modules.
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
c5aa993b 14 (at your option) any later version.
c906108c 15
c5aa993b
JM
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
c906108c 20
c5aa993b 21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
23
24#include "defs.h"
25#include "gdb_string.h"
26#include "bfd.h"
27#include "symtab.h"
28#include "symfile.h"
29#include "objfiles.h"
30#include "gdbtypes.h"
31#include "expression.h"
32#include "language.h"
33#include "target.h"
34#include "value.h"
35#include "demangle.h"
36#include "complaints.h"
37#include "gdbcmd.h"
c91ecb25 38#include "wrapper.h"
015a42b4 39#include "cp-abi.h"
a02fd225 40#include "gdb_assert.h"
ae5a43e0 41#include "hashtab.h"
c906108c 42
ac3aafc7 43
8da61cc4 44/* Floatformat pairs. */
f9e9243a
UW
45const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
46 &floatformat_ieee_half_big,
47 &floatformat_ieee_half_little
48};
8da61cc4
DJ
49const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
50 &floatformat_ieee_single_big,
51 &floatformat_ieee_single_little
52};
53const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
54 &floatformat_ieee_double_big,
55 &floatformat_ieee_double_little
56};
57const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
58 &floatformat_ieee_double_big,
59 &floatformat_ieee_double_littlebyte_bigword
60};
61const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
62 &floatformat_i387_ext,
63 &floatformat_i387_ext
64};
65const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
66 &floatformat_m68881_ext,
67 &floatformat_m68881_ext
68};
69const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
70 &floatformat_arm_ext_big,
71 &floatformat_arm_ext_littlebyte_bigword
72};
73const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
74 &floatformat_ia64_spill_big,
75 &floatformat_ia64_spill_little
76};
77const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
78 &floatformat_ia64_quad_big,
79 &floatformat_ia64_quad_little
80};
81const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
82 &floatformat_vax_f,
83 &floatformat_vax_f
84};
85const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
86 &floatformat_vax_d,
87 &floatformat_vax_d
88};
b14d30e1
JM
89const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
90 &floatformat_ibm_long_double,
91 &floatformat_ibm_long_double
92};
8da61cc4 93
8da61cc4 94
c906108c 95int opaque_type_resolution = 1;
920d2a44
AC
96static void
97show_opaque_type_resolution (struct ui_file *file, int from_tty,
7ba81444
MS
98 struct cmd_list_element *c,
99 const char *value)
920d2a44
AC
100{
101 fprintf_filtered (file, _("\
102Resolution of opaque struct/class/union types (if set before loading symbols) is %s.\n"),
103 value);
104}
105
5d161b24 106int overload_debug = 0;
920d2a44
AC
107static void
108show_overload_debug (struct ui_file *file, int from_tty,
109 struct cmd_list_element *c, const char *value)
110{
7ba81444
MS
111 fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
112 value);
920d2a44 113}
c906108c 114
c5aa993b
JM
115struct extra
116 {
117 char str[128];
118 int len;
7ba81444 119 }; /* Maximum extension is 128! FIXME */
c906108c 120
a14ed312 121static void print_bit_vector (B_TYPE *, int);
ad2f7632 122static void print_arg_types (struct field *, int, int);
a14ed312
KB
123static void dump_fn_fieldlists (struct type *, int);
124static void print_cplus_stuff (struct type *, int);
7a292a7a 125
c906108c 126
e9bb382b
UW
127/* Allocate a new OBJFILE-associated type structure and fill it
128 with some defaults. Space for the type structure is allocated
129 on the objfile's objfile_obstack. */
c906108c
SS
130
131struct type *
fba45db2 132alloc_type (struct objfile *objfile)
c906108c 133{
52f0bd74 134 struct type *type;
c906108c 135
e9bb382b
UW
136 gdb_assert (objfile != NULL);
137
7ba81444 138 /* Alloc the structure and start off with all fields zeroed. */
e9bb382b
UW
139 type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
140 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
141 struct main_type);
142 OBJSTAT (objfile, n_types++);
c906108c 143
e9bb382b
UW
144 TYPE_OBJFILE_OWNED (type) = 1;
145 TYPE_OWNER (type).objfile = objfile;
c906108c 146
7ba81444 147 /* Initialize the fields that might not be zero. */
c906108c
SS
148
149 TYPE_CODE (type) = TYPE_CODE_UNDEF;
c906108c 150 TYPE_VPTR_FIELDNO (type) = -1;
2fdde8f8 151 TYPE_CHAIN (type) = type; /* Chain back to itself. */
c906108c 152
c16abbde 153 return type;
c906108c
SS
154}
155
e9bb382b
UW
156/* Allocate a new GDBARCH-associated type structure and fill it
157 with some defaults. Space for the type structure is allocated
158 on the heap. */
159
160struct type *
161alloc_type_arch (struct gdbarch *gdbarch)
162{
163 struct type *type;
164
165 gdb_assert (gdbarch != NULL);
166
167 /* Alloc the structure and start off with all fields zeroed. */
168
169 type = XZALLOC (struct type);
170 TYPE_MAIN_TYPE (type) = XZALLOC (struct main_type);
171
172 TYPE_OBJFILE_OWNED (type) = 0;
173 TYPE_OWNER (type).gdbarch = gdbarch;
174
175 /* Initialize the fields that might not be zero. */
176
177 TYPE_CODE (type) = TYPE_CODE_UNDEF;
178 TYPE_VPTR_FIELDNO (type) = -1;
179 TYPE_CHAIN (type) = type; /* Chain back to itself. */
180
181 return type;
182}
183
184/* If TYPE is objfile-associated, allocate a new type structure
185 associated with the same objfile. If TYPE is gdbarch-associated,
186 allocate a new type structure associated with the same gdbarch. */
187
188struct type *
189alloc_type_copy (const struct type *type)
190{
191 if (TYPE_OBJFILE_OWNED (type))
192 return alloc_type (TYPE_OWNER (type).objfile);
193 else
194 return alloc_type_arch (TYPE_OWNER (type).gdbarch);
195}
196
197/* If TYPE is gdbarch-associated, return that architecture.
198 If TYPE is objfile-associated, return that objfile's architecture. */
199
200struct gdbarch *
201get_type_arch (const struct type *type)
202{
203 if (TYPE_OBJFILE_OWNED (type))
204 return get_objfile_arch (TYPE_OWNER (type).objfile);
205 else
206 return TYPE_OWNER (type).gdbarch;
207}
208
209
2fdde8f8
DJ
210/* Alloc a new type instance structure, fill it with some defaults,
211 and point it at OLDTYPE. Allocate the new type instance from the
212 same place as OLDTYPE. */
213
214static struct type *
215alloc_type_instance (struct type *oldtype)
216{
217 struct type *type;
218
219 /* Allocate the structure. */
220
e9bb382b 221 if (! TYPE_OBJFILE_OWNED (oldtype))
1deafd4e 222 type = XZALLOC (struct type);
2fdde8f8 223 else
1deafd4e
PA
224 type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
225 struct type);
226
2fdde8f8
DJ
227 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
228
229 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
230
c16abbde 231 return type;
2fdde8f8
DJ
232}
233
234/* Clear all remnants of the previous type at TYPE, in preparation for
e9bb382b 235 replacing it with something else. Preserve owner information. */
2fdde8f8
DJ
236static void
237smash_type (struct type *type)
238{
e9bb382b
UW
239 int objfile_owned = TYPE_OBJFILE_OWNED (type);
240 union type_owner owner = TYPE_OWNER (type);
241
2fdde8f8
DJ
242 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
243
e9bb382b
UW
244 /* Restore owner information. */
245 TYPE_OBJFILE_OWNED (type) = objfile_owned;
246 TYPE_OWNER (type) = owner;
247
2fdde8f8
DJ
248 /* For now, delete the rings. */
249 TYPE_CHAIN (type) = type;
250
251 /* For now, leave the pointer/reference types alone. */
252}
253
c906108c
SS
254/* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
255 to a pointer to memory where the pointer type should be stored.
256 If *TYPEPTR is zero, update it to point to the pointer type we return.
257 We allocate new memory if needed. */
258
259struct type *
fba45db2 260make_pointer_type (struct type *type, struct type **typeptr)
c906108c 261{
52f0bd74 262 struct type *ntype; /* New type */
053cb41b 263 struct type *chain;
c906108c
SS
264
265 ntype = TYPE_POINTER_TYPE (type);
266
c5aa993b 267 if (ntype)
c906108c 268 {
c5aa993b 269 if (typeptr == 0)
7ba81444
MS
270 return ntype; /* Don't care about alloc,
271 and have new type. */
c906108c 272 else if (*typeptr == 0)
c5aa993b 273 {
7ba81444 274 *typeptr = ntype; /* Tracking alloc, and have new type. */
c906108c 275 return ntype;
c5aa993b 276 }
c906108c
SS
277 }
278
279 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
280 {
e9bb382b 281 ntype = alloc_type_copy (type);
c906108c
SS
282 if (typeptr)
283 *typeptr = ntype;
284 }
7ba81444 285 else /* We have storage, but need to reset it. */
c906108c
SS
286 {
287 ntype = *typeptr;
053cb41b 288 chain = TYPE_CHAIN (ntype);
2fdde8f8 289 smash_type (ntype);
053cb41b 290 TYPE_CHAIN (ntype) = chain;
c906108c
SS
291 }
292
293 TYPE_TARGET_TYPE (ntype) = type;
294 TYPE_POINTER_TYPE (type) = ntype;
295
7ba81444
MS
296 /* FIXME! Assume the machine has only one representation for
297 pointers! */
c906108c 298
50810684
UW
299 TYPE_LENGTH (ntype)
300 = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
c906108c
SS
301 TYPE_CODE (ntype) = TYPE_CODE_PTR;
302
67b2adb2 303 /* Mark pointers as unsigned. The target converts between pointers
76e71323 304 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
7ba81444 305 gdbarch_address_to_pointer. */
876cecd0 306 TYPE_UNSIGNED (ntype) = 1;
c5aa993b 307
c906108c
SS
308 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
309 TYPE_POINTER_TYPE (type) = ntype;
310
053cb41b
JB
311 /* Update the length of all the other variants of this type. */
312 chain = TYPE_CHAIN (ntype);
313 while (chain != ntype)
314 {
315 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
316 chain = TYPE_CHAIN (chain);
317 }
318
c906108c
SS
319 return ntype;
320}
321
322/* Given a type TYPE, return a type of pointers to that type.
323 May need to construct such a type if this is the first use. */
324
325struct type *
fba45db2 326lookup_pointer_type (struct type *type)
c906108c 327{
c5aa993b 328 return make_pointer_type (type, (struct type **) 0);
c906108c
SS
329}
330
7ba81444
MS
331/* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
332 points to a pointer to memory where the reference type should be
333 stored. If *TYPEPTR is zero, update it to point to the reference
334 type we return. We allocate new memory if needed. */
c906108c
SS
335
336struct type *
fba45db2 337make_reference_type (struct type *type, struct type **typeptr)
c906108c 338{
52f0bd74 339 struct type *ntype; /* New type */
1e98b326 340 struct type *chain;
c906108c
SS
341
342 ntype = TYPE_REFERENCE_TYPE (type);
343
c5aa993b 344 if (ntype)
c906108c 345 {
c5aa993b 346 if (typeptr == 0)
7ba81444
MS
347 return ntype; /* Don't care about alloc,
348 and have new type. */
c906108c 349 else if (*typeptr == 0)
c5aa993b 350 {
7ba81444 351 *typeptr = ntype; /* Tracking alloc, and have new type. */
c906108c 352 return ntype;
c5aa993b 353 }
c906108c
SS
354 }
355
356 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
357 {
e9bb382b 358 ntype = alloc_type_copy (type);
c906108c
SS
359 if (typeptr)
360 *typeptr = ntype;
361 }
7ba81444 362 else /* We have storage, but need to reset it. */
c906108c
SS
363 {
364 ntype = *typeptr;
1e98b326 365 chain = TYPE_CHAIN (ntype);
2fdde8f8 366 smash_type (ntype);
1e98b326 367 TYPE_CHAIN (ntype) = chain;
c906108c
SS
368 }
369
370 TYPE_TARGET_TYPE (ntype) = type;
371 TYPE_REFERENCE_TYPE (type) = ntype;
372
7ba81444
MS
373 /* FIXME! Assume the machine has only one representation for
374 references, and that it matches the (only) representation for
375 pointers! */
c906108c 376
50810684
UW
377 TYPE_LENGTH (ntype) =
378 gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
c906108c 379 TYPE_CODE (ntype) = TYPE_CODE_REF;
c5aa993b 380
c906108c
SS
381 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
382 TYPE_REFERENCE_TYPE (type) = ntype;
383
1e98b326
JB
384 /* Update the length of all the other variants of this type. */
385 chain = TYPE_CHAIN (ntype);
386 while (chain != ntype)
387 {
388 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
389 chain = TYPE_CHAIN (chain);
390 }
391
c906108c
SS
392 return ntype;
393}
394
7ba81444
MS
395/* Same as above, but caller doesn't care about memory allocation
396 details. */
c906108c
SS
397
398struct type *
fba45db2 399lookup_reference_type (struct type *type)
c906108c 400{
c5aa993b 401 return make_reference_type (type, (struct type **) 0);
c906108c
SS
402}
403
7ba81444
MS
404/* Lookup a function type that returns type TYPE. TYPEPTR, if
405 nonzero, points to a pointer to memory where the function type
406 should be stored. If *TYPEPTR is zero, update it to point to the
0c8b41f1 407 function type we return. We allocate new memory if needed. */
c906108c
SS
408
409struct type *
0c8b41f1 410make_function_type (struct type *type, struct type **typeptr)
c906108c 411{
52f0bd74 412 struct type *ntype; /* New type */
c906108c
SS
413
414 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
415 {
e9bb382b 416 ntype = alloc_type_copy (type);
c906108c
SS
417 if (typeptr)
418 *typeptr = ntype;
419 }
7ba81444 420 else /* We have storage, but need to reset it. */
c906108c
SS
421 {
422 ntype = *typeptr;
2fdde8f8 423 smash_type (ntype);
c906108c
SS
424 }
425
426 TYPE_TARGET_TYPE (ntype) = type;
427
428 TYPE_LENGTH (ntype) = 1;
429 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
c5aa993b 430
c906108c
SS
431 return ntype;
432}
433
434
435/* Given a type TYPE, return a type of functions that return that type.
436 May need to construct such a type if this is the first use. */
437
438struct type *
fba45db2 439lookup_function_type (struct type *type)
c906108c 440{
0c8b41f1 441 return make_function_type (type, (struct type **) 0);
c906108c
SS
442}
443
47663de5
MS
444/* Identify address space identifier by name --
445 return the integer flag defined in gdbtypes.h. */
446extern int
50810684 447address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
47663de5 448{
8b2dbe47 449 int type_flags;
d8734c88 450
7ba81444 451 /* Check for known address space delimiters. */
47663de5 452 if (!strcmp (space_identifier, "code"))
876cecd0 453 return TYPE_INSTANCE_FLAG_CODE_SPACE;
47663de5 454 else if (!strcmp (space_identifier, "data"))
876cecd0 455 return TYPE_INSTANCE_FLAG_DATA_SPACE;
5f11f355
AC
456 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
457 && gdbarch_address_class_name_to_type_flags (gdbarch,
458 space_identifier,
459 &type_flags))
8b2dbe47 460 return type_flags;
47663de5 461 else
8a3fe4f8 462 error (_("Unknown address space specifier: \"%s\""), space_identifier);
47663de5
MS
463}
464
465/* Identify address space identifier by integer flag as defined in
7ba81444 466 gdbtypes.h -- return the string version of the adress space name. */
47663de5 467
321432c0 468const char *
50810684 469address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
47663de5 470{
876cecd0 471 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
47663de5 472 return "code";
876cecd0 473 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
47663de5 474 return "data";
876cecd0 475 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5f11f355
AC
476 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
477 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
47663de5
MS
478 else
479 return NULL;
480}
481
2fdde8f8 482/* Create a new type with instance flags NEW_FLAGS, based on TYPE.
ad766c0a
JB
483
484 If STORAGE is non-NULL, create the new type instance there.
485 STORAGE must be in the same obstack as TYPE. */
47663de5 486
b9362cc7 487static struct type *
2fdde8f8
DJ
488make_qualified_type (struct type *type, int new_flags,
489 struct type *storage)
47663de5
MS
490{
491 struct type *ntype;
492
493 ntype = type;
5f61c20e
JK
494 do
495 {
496 if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
497 return ntype;
498 ntype = TYPE_CHAIN (ntype);
499 }
500 while (ntype != type);
47663de5 501
2fdde8f8
DJ
502 /* Create a new type instance. */
503 if (storage == NULL)
504 ntype = alloc_type_instance (type);
505 else
506 {
7ba81444
MS
507 /* If STORAGE was provided, it had better be in the same objfile
508 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
509 if one objfile is freed and the other kept, we'd have
510 dangling pointers. */
ad766c0a
JB
511 gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
512
2fdde8f8
DJ
513 ntype = storage;
514 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
515 TYPE_CHAIN (ntype) = ntype;
516 }
47663de5
MS
517
518 /* Pointers or references to the original type are not relevant to
2fdde8f8 519 the new type. */
47663de5
MS
520 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
521 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
47663de5 522
2fdde8f8
DJ
523 /* Chain the new qualified type to the old type. */
524 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
525 TYPE_CHAIN (type) = ntype;
526
527 /* Now set the instance flags and return the new type. */
528 TYPE_INSTANCE_FLAGS (ntype) = new_flags;
47663de5 529
ab5d3da6
KB
530 /* Set length of new type to that of the original type. */
531 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
532
47663de5
MS
533 return ntype;
534}
535
2fdde8f8
DJ
536/* Make an address-space-delimited variant of a type -- a type that
537 is identical to the one supplied except that it has an address
538 space attribute attached to it (such as "code" or "data").
539
7ba81444
MS
540 The space attributes "code" and "data" are for Harvard
541 architectures. The address space attributes are for architectures
542 which have alternately sized pointers or pointers with alternate
543 representations. */
2fdde8f8
DJ
544
545struct type *
546make_type_with_address_space (struct type *type, int space_flag)
547{
2fdde8f8 548 int new_flags = ((TYPE_INSTANCE_FLAGS (type)
876cecd0
TT
549 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
550 | TYPE_INSTANCE_FLAG_DATA_SPACE
551 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
2fdde8f8
DJ
552 | space_flag);
553
554 return make_qualified_type (type, new_flags, NULL);
555}
c906108c
SS
556
557/* Make a "c-v" variant of a type -- a type that is identical to the
558 one supplied except that it may have const or volatile attributes
559 CNST is a flag for setting the const attribute
560 VOLTL is a flag for setting the volatile attribute
561 TYPE is the base type whose variant we are creating.
c906108c 562
ad766c0a
JB
563 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
564 storage to hold the new qualified type; *TYPEPTR and TYPE must be
565 in the same objfile. Otherwise, allocate fresh memory for the new
566 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
567 new type we construct. */
c906108c 568struct type *
7ba81444
MS
569make_cv_type (int cnst, int voltl,
570 struct type *type,
571 struct type **typeptr)
c906108c 572{
52f0bd74 573 struct type *ntype; /* New type */
c906108c 574
2fdde8f8 575 int new_flags = (TYPE_INSTANCE_FLAGS (type)
308d96ed
MS
576 & ~(TYPE_INSTANCE_FLAG_CONST
577 | TYPE_INSTANCE_FLAG_VOLATILE));
c906108c 578
c906108c 579 if (cnst)
876cecd0 580 new_flags |= TYPE_INSTANCE_FLAG_CONST;
c906108c
SS
581
582 if (voltl)
876cecd0 583 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
a02fd225 584
2fdde8f8 585 if (typeptr && *typeptr != NULL)
a02fd225 586 {
ad766c0a
JB
587 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
588 a C-V variant chain that threads across objfiles: if one
589 objfile gets freed, then the other has a broken C-V chain.
590
591 This code used to try to copy over the main type from TYPE to
592 *TYPEPTR if they were in different objfiles, but that's
593 wrong, too: TYPE may have a field list or member function
594 lists, which refer to types of their own, etc. etc. The
595 whole shebang would need to be copied over recursively; you
596 can't have inter-objfile pointers. The only thing to do is
597 to leave stub types as stub types, and look them up afresh by
598 name each time you encounter them. */
599 gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
2fdde8f8
DJ
600 }
601
7ba81444
MS
602 ntype = make_qualified_type (type, new_flags,
603 typeptr ? *typeptr : NULL);
c906108c 604
2fdde8f8
DJ
605 if (typeptr != NULL)
606 *typeptr = ntype;
a02fd225 607
2fdde8f8 608 return ntype;
a02fd225 609}
c906108c 610
2fdde8f8
DJ
611/* Replace the contents of ntype with the type *type. This changes the
612 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
613 the changes are propogated to all types in the TYPE_CHAIN.
dd6bda65 614
cda6c68a
JB
615 In order to build recursive types, it's inevitable that we'll need
616 to update types in place --- but this sort of indiscriminate
617 smashing is ugly, and needs to be replaced with something more
2fdde8f8
DJ
618 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
619 clear if more steps are needed. */
dd6bda65
DJ
620void
621replace_type (struct type *ntype, struct type *type)
622{
ab5d3da6 623 struct type *chain;
dd6bda65 624
ad766c0a
JB
625 /* These two types had better be in the same objfile. Otherwise,
626 the assignment of one type's main type structure to the other
627 will produce a type with references to objects (names; field
628 lists; etc.) allocated on an objfile other than its own. */
629 gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
630
2fdde8f8 631 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
dd6bda65 632
7ba81444
MS
633 /* The type length is not a part of the main type. Update it for
634 each type on the variant chain. */
ab5d3da6 635 chain = ntype;
5f61c20e
JK
636 do
637 {
638 /* Assert that this element of the chain has no address-class bits
639 set in its flags. Such type variants might have type lengths
640 which are supposed to be different from the non-address-class
641 variants. This assertion shouldn't ever be triggered because
642 symbol readers which do construct address-class variants don't
643 call replace_type(). */
644 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
645
646 TYPE_LENGTH (chain) = TYPE_LENGTH (type);
647 chain = TYPE_CHAIN (chain);
648 }
649 while (ntype != chain);
ab5d3da6 650
2fdde8f8
DJ
651 /* Assert that the two types have equivalent instance qualifiers.
652 This should be true for at least all of our debug readers. */
653 gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
dd6bda65
DJ
654}
655
c906108c
SS
656/* Implement direct support for MEMBER_TYPE in GNU C++.
657 May need to construct such a type if this is the first use.
658 The TYPE is the type of the member. The DOMAIN is the type
659 of the aggregate that the member belongs to. */
660
661struct type *
0d5de010 662lookup_memberptr_type (struct type *type, struct type *domain)
c906108c 663{
52f0bd74 664 struct type *mtype;
c906108c 665
e9bb382b 666 mtype = alloc_type_copy (type);
0d5de010 667 smash_to_memberptr_type (mtype, domain, type);
c16abbde 668 return mtype;
c906108c
SS
669}
670
0d5de010
DJ
671/* Return a pointer-to-method type, for a method of type TO_TYPE. */
672
673struct type *
674lookup_methodptr_type (struct type *to_type)
675{
676 struct type *mtype;
677
e9bb382b 678 mtype = alloc_type_copy (to_type);
0b92b5bb 679 smash_to_methodptr_type (mtype, to_type);
0d5de010
DJ
680 return mtype;
681}
682
7ba81444
MS
683/* Allocate a stub method whose return type is TYPE. This apparently
684 happens for speed of symbol reading, since parsing out the
685 arguments to the method is cpu-intensive, the way we are doing it.
686 So, we will fill in arguments later. This always returns a fresh
687 type. */
c906108c
SS
688
689struct type *
fba45db2 690allocate_stub_method (struct type *type)
c906108c
SS
691{
692 struct type *mtype;
693
e9bb382b
UW
694 mtype = alloc_type_copy (type);
695 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
696 TYPE_LENGTH (mtype) = 1;
697 TYPE_STUB (mtype) = 1;
c906108c
SS
698 TYPE_TARGET_TYPE (mtype) = type;
699 /* _DOMAIN_TYPE (mtype) = unknown yet */
c16abbde 700 return mtype;
c906108c
SS
701}
702
7ba81444
MS
703/* Create a range type using either a blank type supplied in
704 RESULT_TYPE, or creating a new type, inheriting the objfile from
705 INDEX_TYPE.
c906108c 706
7ba81444
MS
707 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
708 to HIGH_BOUND, inclusive.
c906108c 709
7ba81444
MS
710 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
711 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
c906108c
SS
712
713struct type *
fba45db2 714create_range_type (struct type *result_type, struct type *index_type,
43bbcdc2 715 LONGEST low_bound, LONGEST high_bound)
c906108c
SS
716{
717 if (result_type == NULL)
e9bb382b 718 result_type = alloc_type_copy (index_type);
c906108c
SS
719 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
720 TYPE_TARGET_TYPE (result_type) = index_type;
74a9bb82 721 if (TYPE_STUB (index_type))
876cecd0 722 TYPE_TARGET_STUB (result_type) = 1;
c906108c
SS
723 else
724 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
43bbcdc2
PH
725 TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
726 TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
262452ec
JK
727 TYPE_LOW_BOUND (result_type) = low_bound;
728 TYPE_HIGH_BOUND (result_type) = high_bound;
c906108c 729
c5aa993b 730 if (low_bound >= 0)
876cecd0 731 TYPE_UNSIGNED (result_type) = 1;
c906108c 732
262452ec 733 return result_type;
c906108c
SS
734}
735
7ba81444
MS
736/* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
737 TYPE. Return 1 if type is a range type, 0 if it is discrete (and
738 bounds will fit in LONGEST), or -1 otherwise. */
c906108c
SS
739
740int
fba45db2 741get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
c906108c
SS
742{
743 CHECK_TYPEDEF (type);
744 switch (TYPE_CODE (type))
745 {
746 case TYPE_CODE_RANGE:
747 *lowp = TYPE_LOW_BOUND (type);
748 *highp = TYPE_HIGH_BOUND (type);
749 return 1;
750 case TYPE_CODE_ENUM:
751 if (TYPE_NFIELDS (type) > 0)
752 {
753 /* The enums may not be sorted by value, so search all
754 entries */
755 int i;
756
757 *lowp = *highp = TYPE_FIELD_BITPOS (type, 0);
758 for (i = 0; i < TYPE_NFIELDS (type); i++)
759 {
760 if (TYPE_FIELD_BITPOS (type, i) < *lowp)
761 *lowp = TYPE_FIELD_BITPOS (type, i);
762 if (TYPE_FIELD_BITPOS (type, i) > *highp)
763 *highp = TYPE_FIELD_BITPOS (type, i);
764 }
765
7ba81444 766 /* Set unsigned indicator if warranted. */
c5aa993b 767 if (*lowp >= 0)
c906108c 768 {
876cecd0 769 TYPE_UNSIGNED (type) = 1;
c906108c
SS
770 }
771 }
772 else
773 {
774 *lowp = 0;
775 *highp = -1;
776 }
777 return 0;
778 case TYPE_CODE_BOOL:
779 *lowp = 0;
780 *highp = 1;
781 return 0;
782 case TYPE_CODE_INT:
c5aa993b 783 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
c906108c
SS
784 return -1;
785 if (!TYPE_UNSIGNED (type))
786 {
c5aa993b 787 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
c906108c
SS
788 *highp = -*lowp - 1;
789 return 0;
790 }
7ba81444 791 /* ... fall through for unsigned ints ... */
c906108c
SS
792 case TYPE_CODE_CHAR:
793 *lowp = 0;
794 /* This round-about calculation is to avoid shifting by
7b83ea04 795 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
7ba81444 796 if TYPE_LENGTH (type) == sizeof (LONGEST). */
c906108c
SS
797 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
798 *highp = (*highp - 1) | *highp;
799 return 0;
800 default:
801 return -1;
802 }
803}
804
dbc98a8b
KW
805/* Assuming TYPE is a simple, non-empty array type, compute its upper
806 and lower bound. Save the low bound into LOW_BOUND if not NULL.
807 Save the high bound into HIGH_BOUND if not NULL.
808
809 Return 1 if the operation was successful. Return zero otherwise,
810 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
811
812 We now simply use get_discrete_bounds call to get the values
813 of the low and high bounds.
814 get_discrete_bounds can return three values:
815 1, meaning that index is a range,
816 0, meaning that index is a discrete type,
817 or -1 for failure. */
818
819int
820get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
821{
822 struct type *index = TYPE_INDEX_TYPE (type);
823 LONGEST low = 0;
824 LONGEST high = 0;
825 int res;
826
827 if (index == NULL)
828 return 0;
829
830 res = get_discrete_bounds (index, &low, &high);
831 if (res == -1)
832 return 0;
833
834 /* Check if the array bounds are undefined. */
835 if (res == 1
836 && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
837 || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
838 return 0;
839
840 if (low_bound)
841 *low_bound = low;
842
843 if (high_bound)
844 *high_bound = high;
845
846 return 1;
847}
848
7ba81444
MS
849/* Create an array type using either a blank type supplied in
850 RESULT_TYPE, or creating a new type, inheriting the objfile from
851 RANGE_TYPE.
c906108c
SS
852
853 Elements will be of type ELEMENT_TYPE, the indices will be of type
854 RANGE_TYPE.
855
7ba81444
MS
856 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
857 sure it is TYPE_CODE_UNDEF before we bash it into an array
858 type? */
c906108c
SS
859
860struct type *
7ba81444
MS
861create_array_type (struct type *result_type,
862 struct type *element_type,
fba45db2 863 struct type *range_type)
c906108c
SS
864{
865 LONGEST low_bound, high_bound;
866
867 if (result_type == NULL)
e9bb382b
UW
868 result_type = alloc_type_copy (range_type);
869
c906108c
SS
870 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
871 TYPE_TARGET_TYPE (result_type) = element_type;
872 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
873 low_bound = high_bound = 0;
874 CHECK_TYPEDEF (element_type);
ab0d6e0d
JB
875 /* Be careful when setting the array length. Ada arrays can be
876 empty arrays with the high_bound being smaller than the low_bound.
877 In such cases, the array length should be zero. */
878 if (high_bound < low_bound)
879 TYPE_LENGTH (result_type) = 0;
880 else
881 TYPE_LENGTH (result_type) =
882 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
c906108c
SS
883 TYPE_NFIELDS (result_type) = 1;
884 TYPE_FIELDS (result_type) =
1deafd4e 885 (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
262452ec 886 TYPE_INDEX_TYPE (result_type) = range_type;
c906108c
SS
887 TYPE_VPTR_FIELDNO (result_type) = -1;
888
889 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */
890 if (TYPE_LENGTH (result_type) == 0)
876cecd0 891 TYPE_TARGET_STUB (result_type) = 1;
c906108c 892
c16abbde 893 return result_type;
c906108c
SS
894}
895
e3506a9f
UW
896struct type *
897lookup_array_range_type (struct type *element_type,
898 int low_bound, int high_bound)
899{
50810684 900 struct gdbarch *gdbarch = get_type_arch (element_type);
e3506a9f
UW
901 struct type *index_type = builtin_type (gdbarch)->builtin_int;
902 struct type *range_type
903 = create_range_type (NULL, index_type, low_bound, high_bound);
d8734c88 904
e3506a9f
UW
905 return create_array_type (NULL, element_type, range_type);
906}
907
7ba81444
MS
908/* Create a string type using either a blank type supplied in
909 RESULT_TYPE, or creating a new type. String types are similar
910 enough to array of char types that we can use create_array_type to
911 build the basic type and then bash it into a string type.
c906108c
SS
912
913 For fixed length strings, the range type contains 0 as the lower
914 bound and the length of the string minus one as the upper bound.
915
7ba81444
MS
916 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
917 sure it is TYPE_CODE_UNDEF before we bash it into a string
918 type? */
c906108c
SS
919
920struct type *
3b7538c0
UW
921create_string_type (struct type *result_type,
922 struct type *string_char_type,
7ba81444 923 struct type *range_type)
c906108c
SS
924{
925 result_type = create_array_type (result_type,
f290d38e 926 string_char_type,
c906108c
SS
927 range_type);
928 TYPE_CODE (result_type) = TYPE_CODE_STRING;
c16abbde 929 return result_type;
c906108c
SS
930}
931
e3506a9f
UW
932struct type *
933lookup_string_range_type (struct type *string_char_type,
934 int low_bound, int high_bound)
935{
936 struct type *result_type;
d8734c88 937
e3506a9f
UW
938 result_type = lookup_array_range_type (string_char_type,
939 low_bound, high_bound);
940 TYPE_CODE (result_type) = TYPE_CODE_STRING;
941 return result_type;
942}
943
c906108c 944struct type *
fba45db2 945create_set_type (struct type *result_type, struct type *domain_type)
c906108c 946{
c906108c 947 if (result_type == NULL)
e9bb382b
UW
948 result_type = alloc_type_copy (domain_type);
949
c906108c
SS
950 TYPE_CODE (result_type) = TYPE_CODE_SET;
951 TYPE_NFIELDS (result_type) = 1;
1deafd4e 952 TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
c906108c 953
74a9bb82 954 if (!TYPE_STUB (domain_type))
c906108c 955 {
f9780d5b 956 LONGEST low_bound, high_bound, bit_length;
d8734c88 957
c906108c
SS
958 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
959 low_bound = high_bound = 0;
960 bit_length = high_bound - low_bound + 1;
961 TYPE_LENGTH (result_type)
962 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
f9780d5b 963 if (low_bound >= 0)
876cecd0 964 TYPE_UNSIGNED (result_type) = 1;
c906108c
SS
965 }
966 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
967
c16abbde 968 return result_type;
c906108c
SS
969}
970
ea37ba09
DJ
971/* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
972 and any array types nested inside it. */
973
974void
975make_vector_type (struct type *array_type)
976{
977 struct type *inner_array, *elt_type;
978 int flags;
979
980 /* Find the innermost array type, in case the array is
981 multi-dimensional. */
982 inner_array = array_type;
983 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
984 inner_array = TYPE_TARGET_TYPE (inner_array);
985
986 elt_type = TYPE_TARGET_TYPE (inner_array);
987 if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
988 {
2844d6b5 989 flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
ea37ba09
DJ
990 elt_type = make_qualified_type (elt_type, flags, NULL);
991 TYPE_TARGET_TYPE (inner_array) = elt_type;
992 }
993
876cecd0 994 TYPE_VECTOR (array_type) = 1;
ea37ba09
DJ
995}
996
794ac428 997struct type *
ac3aafc7
EZ
998init_vector_type (struct type *elt_type, int n)
999{
1000 struct type *array_type;
d8734c88 1001
e3506a9f 1002 array_type = lookup_array_range_type (elt_type, 0, n - 1);
ea37ba09 1003 make_vector_type (array_type);
ac3aafc7
EZ
1004 return array_type;
1005}
1006
0d5de010
DJ
1007/* Smash TYPE to be a type of pointers to members of DOMAIN with type
1008 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1009 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1010 TYPE doesn't include the offset (that's the value of the MEMBER
1011 itself), but does include the structure type into which it points
1012 (for some reason).
c906108c 1013
7ba81444
MS
1014 When "smashing" the type, we preserve the objfile that the old type
1015 pointed to, since we aren't changing where the type is actually
c906108c
SS
1016 allocated. */
1017
1018void
0d5de010
DJ
1019smash_to_memberptr_type (struct type *type, struct type *domain,
1020 struct type *to_type)
c906108c 1021{
2fdde8f8 1022 smash_type (type);
c906108c
SS
1023 TYPE_TARGET_TYPE (type) = to_type;
1024 TYPE_DOMAIN_TYPE (type) = domain;
0d5de010
DJ
1025 /* Assume that a data member pointer is the same size as a normal
1026 pointer. */
50810684
UW
1027 TYPE_LENGTH (type)
1028 = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
0d5de010 1029 TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
c906108c
SS
1030}
1031
0b92b5bb
TT
1032/* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1033
1034 When "smashing" the type, we preserve the objfile that the old type
1035 pointed to, since we aren't changing where the type is actually
1036 allocated. */
1037
1038void
1039smash_to_methodptr_type (struct type *type, struct type *to_type)
1040{
1041 smash_type (type);
1042 TYPE_TARGET_TYPE (type) = to_type;
1043 TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1044 TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1045 TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1046}
1047
c906108c
SS
1048/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
1049 METHOD just means `function that gets an extra "this" argument'.
1050
7ba81444
MS
1051 When "smashing" the type, we preserve the objfile that the old type
1052 pointed to, since we aren't changing where the type is actually
c906108c
SS
1053 allocated. */
1054
1055void
fba45db2 1056smash_to_method_type (struct type *type, struct type *domain,
ad2f7632
DJ
1057 struct type *to_type, struct field *args,
1058 int nargs, int varargs)
c906108c 1059{
2fdde8f8 1060 smash_type (type);
c906108c
SS
1061 TYPE_TARGET_TYPE (type) = to_type;
1062 TYPE_DOMAIN_TYPE (type) = domain;
ad2f7632
DJ
1063 TYPE_FIELDS (type) = args;
1064 TYPE_NFIELDS (type) = nargs;
1065 if (varargs)
876cecd0 1066 TYPE_VARARGS (type) = 1;
c906108c
SS
1067 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
1068 TYPE_CODE (type) = TYPE_CODE_METHOD;
1069}
1070
1071/* Return a typename for a struct/union/enum type without "struct ",
1072 "union ", or "enum ". If the type has a NULL name, return NULL. */
1073
1074char *
aa1ee363 1075type_name_no_tag (const struct type *type)
c906108c
SS
1076{
1077 if (TYPE_TAG_NAME (type) != NULL)
1078 return TYPE_TAG_NAME (type);
1079
7ba81444
MS
1080 /* Is there code which expects this to return the name if there is
1081 no tag name? My guess is that this is mainly used for C++ in
1082 cases where the two will always be the same. */
c906108c
SS
1083 return TYPE_NAME (type);
1084}
1085
7ba81444
MS
1086/* Lookup a typedef or primitive type named NAME, visible in lexical
1087 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1088 suitably defined. */
c906108c
SS
1089
1090struct type *
e6c014f2
UW
1091lookup_typename (const struct language_defn *language,
1092 struct gdbarch *gdbarch, char *name,
34eaf542 1093 const struct block *block, int noerr)
c906108c 1094{
52f0bd74
AC
1095 struct symbol *sym;
1096 struct type *tmp;
c906108c 1097
774b6a14 1098 sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
c906108c
SS
1099 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1100 {
e6c014f2 1101 tmp = language_lookup_primitive_type_by_name (language, gdbarch, name);
c906108c
SS
1102 if (tmp)
1103 {
c16abbde 1104 return tmp;
c906108c
SS
1105 }
1106 else if (!tmp && noerr)
1107 {
c16abbde 1108 return NULL;
c906108c
SS
1109 }
1110 else
1111 {
8a3fe4f8 1112 error (_("No type named %s."), name);
c906108c
SS
1113 }
1114 }
1115 return (SYMBOL_TYPE (sym));
1116}
1117
1118struct type *
e6c014f2
UW
1119lookup_unsigned_typename (const struct language_defn *language,
1120 struct gdbarch *gdbarch, char *name)
c906108c
SS
1121{
1122 char *uns = alloca (strlen (name) + 10);
1123
1124 strcpy (uns, "unsigned ");
1125 strcpy (uns + 9, name);
e6c014f2 1126 return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
c906108c
SS
1127}
1128
1129struct type *
e6c014f2
UW
1130lookup_signed_typename (const struct language_defn *language,
1131 struct gdbarch *gdbarch, char *name)
c906108c
SS
1132{
1133 struct type *t;
1134 char *uns = alloca (strlen (name) + 8);
1135
1136 strcpy (uns, "signed ");
1137 strcpy (uns + 7, name);
e6c014f2 1138 t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
7ba81444 1139 /* If we don't find "signed FOO" just try again with plain "FOO". */
c906108c
SS
1140 if (t != NULL)
1141 return t;
e6c014f2 1142 return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
c906108c
SS
1143}
1144
1145/* Lookup a structure type named "struct NAME",
1146 visible in lexical block BLOCK. */
1147
1148struct type *
fba45db2 1149lookup_struct (char *name, struct block *block)
c906108c 1150{
52f0bd74 1151 struct symbol *sym;
c906108c 1152
2570f2b7 1153 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1154
1155 if (sym == NULL)
1156 {
8a3fe4f8 1157 error (_("No struct type named %s."), name);
c906108c
SS
1158 }
1159 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1160 {
7ba81444
MS
1161 error (_("This context has class, union or enum %s, not a struct."),
1162 name);
c906108c
SS
1163 }
1164 return (SYMBOL_TYPE (sym));
1165}
1166
1167/* Lookup a union type named "union NAME",
1168 visible in lexical block BLOCK. */
1169
1170struct type *
fba45db2 1171lookup_union (char *name, struct block *block)
c906108c 1172{
52f0bd74 1173 struct symbol *sym;
c5aa993b 1174 struct type *t;
c906108c 1175
2570f2b7 1176 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1177
1178 if (sym == NULL)
8a3fe4f8 1179 error (_("No union type named %s."), name);
c906108c 1180
c5aa993b 1181 t = SYMBOL_TYPE (sym);
c906108c
SS
1182
1183 if (TYPE_CODE (t) == TYPE_CODE_UNION)
c16abbde 1184 return t;
c906108c 1185
7ba81444
MS
1186 /* If we get here, it's not a union. */
1187 error (_("This context has class, struct or enum %s, not a union."),
1188 name);
c906108c
SS
1189}
1190
1191
1192/* Lookup an enum type named "enum NAME",
1193 visible in lexical block BLOCK. */
1194
1195struct type *
fba45db2 1196lookup_enum (char *name, struct block *block)
c906108c 1197{
52f0bd74 1198 struct symbol *sym;
c906108c 1199
2570f2b7 1200 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1201 if (sym == NULL)
1202 {
8a3fe4f8 1203 error (_("No enum type named %s."), name);
c906108c
SS
1204 }
1205 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1206 {
7ba81444
MS
1207 error (_("This context has class, struct or union %s, not an enum."),
1208 name);
c906108c
SS
1209 }
1210 return (SYMBOL_TYPE (sym));
1211}
1212
1213/* Lookup a template type named "template NAME<TYPE>",
1214 visible in lexical block BLOCK. */
1215
1216struct type *
7ba81444
MS
1217lookup_template_type (char *name, struct type *type,
1218 struct block *block)
c906108c
SS
1219{
1220 struct symbol *sym;
7ba81444
MS
1221 char *nam = (char *)
1222 alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
d8734c88 1223
c906108c
SS
1224 strcpy (nam, name);
1225 strcat (nam, "<");
0004e5a2 1226 strcat (nam, TYPE_NAME (type));
7ba81444 1227 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
c906108c 1228
2570f2b7 1229 sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
c906108c
SS
1230
1231 if (sym == NULL)
1232 {
8a3fe4f8 1233 error (_("No template type named %s."), name);
c906108c
SS
1234 }
1235 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1236 {
7ba81444
MS
1237 error (_("This context has class, union or enum %s, not a struct."),
1238 name);
c906108c
SS
1239 }
1240 return (SYMBOL_TYPE (sym));
1241}
1242
7ba81444
MS
1243/* Given a type TYPE, lookup the type of the component of type named
1244 NAME.
c906108c 1245
7ba81444
MS
1246 TYPE can be either a struct or union, or a pointer or reference to
1247 a struct or union. If it is a pointer or reference, its target
1248 type is automatically used. Thus '.' and '->' are interchangable,
1249 as specified for the definitions of the expression element types
1250 STRUCTOP_STRUCT and STRUCTOP_PTR.
c906108c
SS
1251
1252 If NOERR is nonzero, return zero if NAME is not suitably defined.
1253 If NAME is the name of a baseclass type, return that type. */
1254
1255struct type *
fba45db2 1256lookup_struct_elt_type (struct type *type, char *name, int noerr)
c906108c
SS
1257{
1258 int i;
c92817ce 1259 char *typename;
c906108c
SS
1260
1261 for (;;)
1262 {
1263 CHECK_TYPEDEF (type);
1264 if (TYPE_CODE (type) != TYPE_CODE_PTR
1265 && TYPE_CODE (type) != TYPE_CODE_REF)
1266 break;
1267 type = TYPE_TARGET_TYPE (type);
1268 }
1269
687d6395
MS
1270 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1271 && TYPE_CODE (type) != TYPE_CODE_UNION)
c906108c 1272 {
c92817ce
TT
1273 typename = type_to_string (type);
1274 make_cleanup (xfree, typename);
1275 error (_("Type %s is not a structure or union type."), typename);
c906108c
SS
1276 }
1277
1278#if 0
7ba81444
MS
1279 /* FIXME: This change put in by Michael seems incorrect for the case
1280 where the structure tag name is the same as the member name.
1281 I.E. when doing "ptype bell->bar" for "struct foo { int bar; int
1282 foo; } bell;" Disabled by fnf. */
c906108c
SS
1283 {
1284 char *typename;
1285
1286 typename = type_name_no_tag (type);
762f08a3 1287 if (typename != NULL && strcmp (typename, name) == 0)
c906108c
SS
1288 return type;
1289 }
1290#endif
1291
1292 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1293 {
1294 char *t_field_name = TYPE_FIELD_NAME (type, i);
1295
db577aea 1296 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
1297 {
1298 return TYPE_FIELD_TYPE (type, i);
1299 }
f5a010c0
PM
1300 else if (!t_field_name || *t_field_name == '\0')
1301 {
d8734c88
MS
1302 struct type *subtype
1303 = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1304
f5a010c0
PM
1305 if (subtype != NULL)
1306 return subtype;
1307 }
c906108c
SS
1308 }
1309
1310 /* OK, it's not in this class. Recursively check the baseclasses. */
1311 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1312 {
1313 struct type *t;
1314
9733fc94 1315 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
c906108c
SS
1316 if (t != NULL)
1317 {
1318 return t;
1319 }
1320 }
1321
1322 if (noerr)
1323 {
1324 return NULL;
1325 }
c5aa993b 1326
c92817ce
TT
1327 typename = type_to_string (type);
1328 make_cleanup (xfree, typename);
1329 error (_("Type %s has no component named %s."), typename, name);
c906108c
SS
1330}
1331
81fe8080
DE
1332/* Lookup the vptr basetype/fieldno values for TYPE.
1333 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1334 vptr_fieldno. Also, if found and basetype is from the same objfile,
1335 cache the results.
1336 If not found, return -1 and ignore BASETYPEP.
1337 Callers should be aware that in some cases (for example,
c906108c 1338 the type or one of its baseclasses is a stub type and we are
d48cc9dd
DJ
1339 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1340 this function will not be able to find the
7ba81444 1341 virtual function table pointer, and vptr_fieldno will remain -1 and
81fe8080 1342 vptr_basetype will remain NULL or incomplete. */
c906108c 1343
81fe8080
DE
1344int
1345get_vptr_fieldno (struct type *type, struct type **basetypep)
c906108c
SS
1346{
1347 CHECK_TYPEDEF (type);
1348
1349 if (TYPE_VPTR_FIELDNO (type) < 0)
1350 {
1351 int i;
1352
7ba81444
MS
1353 /* We must start at zero in case the first (and only) baseclass
1354 is virtual (and hence we cannot share the table pointer). */
c906108c
SS
1355 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1356 {
81fe8080
DE
1357 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1358 int fieldno;
1359 struct type *basetype;
1360
1361 fieldno = get_vptr_fieldno (baseclass, &basetype);
1362 if (fieldno >= 0)
c906108c 1363 {
81fe8080
DE
1364 /* If the type comes from a different objfile we can't cache
1365 it, it may have a different lifetime. PR 2384 */
5ef73790 1366 if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
81fe8080
DE
1367 {
1368 TYPE_VPTR_FIELDNO (type) = fieldno;
1369 TYPE_VPTR_BASETYPE (type) = basetype;
1370 }
1371 if (basetypep)
1372 *basetypep = basetype;
1373 return fieldno;
c906108c
SS
1374 }
1375 }
81fe8080
DE
1376
1377 /* Not found. */
1378 return -1;
1379 }
1380 else
1381 {
1382 if (basetypep)
1383 *basetypep = TYPE_VPTR_BASETYPE (type);
1384 return TYPE_VPTR_FIELDNO (type);
c906108c
SS
1385 }
1386}
1387
44e1a9eb
DJ
1388static void
1389stub_noname_complaint (void)
1390{
e2e0b3e5 1391 complaint (&symfile_complaints, _("stub type has NULL name"));
44e1a9eb
DJ
1392}
1393
92163a10
JK
1394/* Find the real type of TYPE. This function returns the real type,
1395 after removing all layers of typedefs, and completing opaque or stub
1396 types. Completion changes the TYPE argument, but stripping of
1397 typedefs does not.
1398
1399 Instance flags (e.g. const/volatile) are preserved as typedefs are
1400 stripped. If necessary a new qualified form of the underlying type
1401 is created.
1402
1403 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1404 not been computed and we're either in the middle of reading symbols, or
1405 there was no name for the typedef in the debug info.
1406
1407 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1408 the target type.
c906108c
SS
1409
1410 If this is a stubbed struct (i.e. declared as struct foo *), see if
1411 we can find a full definition in some other file. If so, copy this
7ba81444
MS
1412 definition, so we can use it in future. There used to be a comment
1413 (but not any code) that if we don't find a full definition, we'd
1414 set a flag so we don't spend time in the future checking the same
1415 type. That would be a mistake, though--we might load in more
92163a10 1416 symbols which contain a full definition for the type. */
c906108c
SS
1417
1418struct type *
a02fd225 1419check_typedef (struct type *type)
c906108c
SS
1420{
1421 struct type *orig_type = type;
92163a10
JK
1422 /* While we're removing typedefs, we don't want to lose qualifiers.
1423 E.g., const/volatile. */
1424 int instance_flags = TYPE_INSTANCE_FLAGS (type);
a02fd225 1425
423c0af8
MS
1426 gdb_assert (type);
1427
c906108c
SS
1428 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1429 {
1430 if (!TYPE_TARGET_TYPE (type))
1431 {
c5aa993b 1432 char *name;
c906108c
SS
1433 struct symbol *sym;
1434
1435 /* It is dangerous to call lookup_symbol if we are currently
7ba81444 1436 reading a symtab. Infinite recursion is one danger. */
c906108c 1437 if (currently_reading_symtab)
92163a10 1438 return make_qualified_type (type, instance_flags, NULL);
c906108c
SS
1439
1440 name = type_name_no_tag (type);
7ba81444
MS
1441 /* FIXME: shouldn't we separately check the TYPE_NAME and
1442 the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1443 VAR_DOMAIN as appropriate? (this code was written before
1444 TYPE_NAME and TYPE_TAG_NAME were separate). */
c906108c
SS
1445 if (name == NULL)
1446 {
23136709 1447 stub_noname_complaint ();
92163a10 1448 return make_qualified_type (type, instance_flags, NULL);
c906108c 1449 }
2570f2b7 1450 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
c906108c
SS
1451 if (sym)
1452 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
7ba81444 1453 else /* TYPE_CODE_UNDEF */
e9bb382b 1454 TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
c906108c
SS
1455 }
1456 type = TYPE_TARGET_TYPE (type);
c906108c 1457
92163a10
JK
1458 /* Preserve the instance flags as we traverse down the typedef chain.
1459
1460 Handling address spaces/classes is nasty, what do we do if there's a
1461 conflict?
1462 E.g., what if an outer typedef marks the type as class_1 and an inner
1463 typedef marks the type as class_2?
1464 This is the wrong place to do such error checking. We leave it to
1465 the code that created the typedef in the first place to flag the
1466 error. We just pick the outer address space (akin to letting the
1467 outer cast in a chain of casting win), instead of assuming
1468 "it can't happen". */
1469 {
1470 const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
1471 | TYPE_INSTANCE_FLAG_DATA_SPACE);
1472 const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
1473 int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
1474
1475 /* Treat code vs data spaces and address classes separately. */
1476 if ((instance_flags & ALL_SPACES) != 0)
1477 new_instance_flags &= ~ALL_SPACES;
1478 if ((instance_flags & ALL_CLASSES) != 0)
1479 new_instance_flags &= ~ALL_CLASSES;
1480
1481 instance_flags |= new_instance_flags;
1482 }
1483 }
a02fd225 1484
7ba81444
MS
1485 /* If this is a struct/class/union with no fields, then check
1486 whether a full definition exists somewhere else. This is for
1487 systems where a type definition with no fields is issued for such
1488 types, instead of identifying them as stub types in the first
1489 place. */
c5aa993b 1490
7ba81444
MS
1491 if (TYPE_IS_OPAQUE (type)
1492 && opaque_type_resolution
1493 && !currently_reading_symtab)
c906108c 1494 {
c5aa993b
JM
1495 char *name = type_name_no_tag (type);
1496 struct type *newtype;
d8734c88 1497
c906108c
SS
1498 if (name == NULL)
1499 {
23136709 1500 stub_noname_complaint ();
92163a10 1501 return make_qualified_type (type, instance_flags, NULL);
c906108c
SS
1502 }
1503 newtype = lookup_transparent_type (name);
ad766c0a 1504
c906108c 1505 if (newtype)
ad766c0a 1506 {
7ba81444
MS
1507 /* If the resolved type and the stub are in the same
1508 objfile, then replace the stub type with the real deal.
1509 But if they're in separate objfiles, leave the stub
1510 alone; we'll just look up the transparent type every time
1511 we call check_typedef. We can't create pointers between
1512 types allocated to different objfiles, since they may
1513 have different lifetimes. Trying to copy NEWTYPE over to
1514 TYPE's objfile is pointless, too, since you'll have to
1515 move over any other types NEWTYPE refers to, which could
1516 be an unbounded amount of stuff. */
ad766c0a 1517 if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
92163a10
JK
1518 type = make_qualified_type (newtype,
1519 TYPE_INSTANCE_FLAGS (type),
1520 type);
ad766c0a
JB
1521 else
1522 type = newtype;
1523 }
c906108c 1524 }
7ba81444
MS
1525 /* Otherwise, rely on the stub flag being set for opaque/stubbed
1526 types. */
74a9bb82 1527 else if (TYPE_STUB (type) && !currently_reading_symtab)
c906108c 1528 {
c5aa993b 1529 char *name = type_name_no_tag (type);
c906108c 1530 /* FIXME: shouldn't we separately check the TYPE_NAME and the
176620f1 1531 TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
7b83ea04
AC
1532 as appropriate? (this code was written before TYPE_NAME and
1533 TYPE_TAG_NAME were separate). */
c906108c 1534 struct symbol *sym;
d8734c88 1535
c906108c
SS
1536 if (name == NULL)
1537 {
23136709 1538 stub_noname_complaint ();
92163a10 1539 return make_qualified_type (type, instance_flags, NULL);
c906108c 1540 }
2570f2b7 1541 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
c906108c 1542 if (sym)
c26f2453
JB
1543 {
1544 /* Same as above for opaque types, we can replace the stub
92163a10 1545 with the complete type only if they are in the same
c26f2453
JB
1546 objfile. */
1547 if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
92163a10
JK
1548 type = make_qualified_type (SYMBOL_TYPE (sym),
1549 TYPE_INSTANCE_FLAGS (type),
1550 type);
c26f2453
JB
1551 else
1552 type = SYMBOL_TYPE (sym);
1553 }
c906108c
SS
1554 }
1555
74a9bb82 1556 if (TYPE_TARGET_STUB (type))
c906108c
SS
1557 {
1558 struct type *range_type;
1559 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1560
74a9bb82 1561 if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
c5aa993b 1562 {
7ba81444 1563 /* Empty. */
c5aa993b 1564 }
c906108c
SS
1565 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1566 && TYPE_NFIELDS (type) == 1
262452ec 1567 && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
c906108c
SS
1568 == TYPE_CODE_RANGE))
1569 {
1570 /* Now recompute the length of the array type, based on its
ab0d6e0d
JB
1571 number of elements and the target type's length.
1572 Watch out for Ada null Ada arrays where the high bound
43bbcdc2
PH
1573 is smaller than the low bound. */
1574 const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
1575 const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
1576 ULONGEST len;
1577
ab0d6e0d 1578 if (high_bound < low_bound)
43bbcdc2 1579 len = 0;
d8734c88
MS
1580 else
1581 {
1582 /* For now, we conservatively take the array length to be 0
1583 if its length exceeds UINT_MAX. The code below assumes
1584 that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
1585 which is technically not guaranteed by C, but is usually true
1586 (because it would be true if x were unsigned with its
1587 high-order bit on). It uses the fact that
1588 high_bound-low_bound is always representable in
1589 ULONGEST and that if high_bound-low_bound+1 overflows,
1590 it overflows to 0. We must change these tests if we
1591 decide to increase the representation of TYPE_LENGTH
1592 from unsigned int to ULONGEST. */
1593 ULONGEST ulow = low_bound, uhigh = high_bound;
1594 ULONGEST tlen = TYPE_LENGTH (target_type);
1595
1596 len = tlen * (uhigh - ulow + 1);
1597 if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh
1598 || len > UINT_MAX)
1599 len = 0;
1600 }
43bbcdc2 1601 TYPE_LENGTH (type) = len;
876cecd0 1602 TYPE_TARGET_STUB (type) = 0;
c906108c
SS
1603 }
1604 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1605 {
1606 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
876cecd0 1607 TYPE_TARGET_STUB (type) = 0;
c906108c
SS
1608 }
1609 }
92163a10
JK
1610
1611 type = make_qualified_type (type, instance_flags, NULL);
1612
7ba81444 1613 /* Cache TYPE_LENGTH for future use. */
c906108c 1614 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
92163a10 1615
c906108c
SS
1616 return type;
1617}
1618
7ba81444 1619/* Parse a type expression in the string [P..P+LENGTH). If an error
48319d1f 1620 occurs, silently return a void type. */
c91ecb25 1621
b9362cc7 1622static struct type *
48319d1f 1623safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
c91ecb25
ND
1624{
1625 struct ui_file *saved_gdb_stderr;
1626 struct type *type;
1627
7ba81444 1628 /* Suppress error messages. */
c91ecb25
ND
1629 saved_gdb_stderr = gdb_stderr;
1630 gdb_stderr = ui_file_new ();
1631
7ba81444 1632 /* Call parse_and_eval_type() without fear of longjmp()s. */
c91ecb25 1633 if (!gdb_parse_and_eval_type (p, length, &type))
48319d1f 1634 type = builtin_type (gdbarch)->builtin_void;
c91ecb25 1635
7ba81444 1636 /* Stop suppressing error messages. */
c91ecb25
ND
1637 ui_file_delete (gdb_stderr);
1638 gdb_stderr = saved_gdb_stderr;
1639
1640 return type;
1641}
1642
c906108c
SS
1643/* Ugly hack to convert method stubs into method types.
1644
7ba81444
MS
1645 He ain't kiddin'. This demangles the name of the method into a
1646 string including argument types, parses out each argument type,
1647 generates a string casting a zero to that type, evaluates the
1648 string, and stuffs the resulting type into an argtype vector!!!
1649 Then it knows the type of the whole function (including argument
1650 types for overloading), which info used to be in the stab's but was
1651 removed to hack back the space required for them. */
c906108c 1652
de17c821 1653static void
fba45db2 1654check_stub_method (struct type *type, int method_id, int signature_id)
c906108c 1655{
50810684 1656 struct gdbarch *gdbarch = get_type_arch (type);
c906108c
SS
1657 struct fn_field *f;
1658 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1659 char *demangled_name = cplus_demangle (mangled_name,
1660 DMGL_PARAMS | DMGL_ANSI);
1661 char *argtypetext, *p;
1662 int depth = 0, argcount = 1;
ad2f7632 1663 struct field *argtypes;
c906108c
SS
1664 struct type *mtype;
1665
1666 /* Make sure we got back a function string that we can use. */
1667 if (demangled_name)
1668 p = strchr (demangled_name, '(');
502dcf4e
AC
1669 else
1670 p = NULL;
c906108c
SS
1671
1672 if (demangled_name == NULL || p == NULL)
7ba81444
MS
1673 error (_("Internal: Cannot demangle mangled name `%s'."),
1674 mangled_name);
c906108c
SS
1675
1676 /* Now, read in the parameters that define this type. */
1677 p += 1;
1678 argtypetext = p;
1679 while (*p)
1680 {
070ad9f0 1681 if (*p == '(' || *p == '<')
c906108c
SS
1682 {
1683 depth += 1;
1684 }
070ad9f0 1685 else if (*p == ')' || *p == '>')
c906108c
SS
1686 {
1687 depth -= 1;
1688 }
1689 else if (*p == ',' && depth == 0)
1690 {
1691 argcount += 1;
1692 }
1693
1694 p += 1;
1695 }
1696
ad2f7632
DJ
1697 /* If we read one argument and it was ``void'', don't count it. */
1698 if (strncmp (argtypetext, "(void)", 6) == 0)
1699 argcount -= 1;
c906108c 1700
ad2f7632
DJ
1701 /* We need one extra slot, for the THIS pointer. */
1702
1703 argtypes = (struct field *)
1704 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
c906108c 1705 p = argtypetext;
4a1970e4
DJ
1706
1707 /* Add THIS pointer for non-static methods. */
1708 f = TYPE_FN_FIELDLIST1 (type, method_id);
1709 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1710 argcount = 0;
1711 else
1712 {
ad2f7632 1713 argtypes[0].type = lookup_pointer_type (type);
4a1970e4
DJ
1714 argcount = 1;
1715 }
c906108c 1716
c5aa993b 1717 if (*p != ')') /* () means no args, skip while */
c906108c
SS
1718 {
1719 depth = 0;
1720 while (*p)
1721 {
1722 if (depth <= 0 && (*p == ',' || *p == ')'))
1723 {
ad2f7632
DJ
1724 /* Avoid parsing of ellipsis, they will be handled below.
1725 Also avoid ``void'' as above. */
1726 if (strncmp (argtypetext, "...", p - argtypetext) != 0
1727 && strncmp (argtypetext, "void", p - argtypetext) != 0)
c906108c 1728 {
ad2f7632 1729 argtypes[argcount].type =
48319d1f 1730 safe_parse_type (gdbarch, argtypetext, p - argtypetext);
c906108c
SS
1731 argcount += 1;
1732 }
1733 argtypetext = p + 1;
1734 }
1735
070ad9f0 1736 if (*p == '(' || *p == '<')
c906108c
SS
1737 {
1738 depth += 1;
1739 }
070ad9f0 1740 else if (*p == ')' || *p == '>')
c906108c
SS
1741 {
1742 depth -= 1;
1743 }
1744
1745 p += 1;
1746 }
1747 }
1748
c906108c
SS
1749 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1750
1751 /* Now update the old "stub" type into a real type. */
1752 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1753 TYPE_DOMAIN_TYPE (mtype) = type;
ad2f7632
DJ
1754 TYPE_FIELDS (mtype) = argtypes;
1755 TYPE_NFIELDS (mtype) = argcount;
876cecd0 1756 TYPE_STUB (mtype) = 0;
c906108c 1757 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
ad2f7632 1758 if (p[-2] == '.')
876cecd0 1759 TYPE_VARARGS (mtype) = 1;
ad2f7632
DJ
1760
1761 xfree (demangled_name);
c906108c
SS
1762}
1763
7ba81444
MS
1764/* This is the external interface to check_stub_method, above. This
1765 function unstubs all of the signatures for TYPE's METHOD_ID method
1766 name. After calling this function TYPE_FN_FIELD_STUB will be
1767 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
1768 correct.
de17c821
DJ
1769
1770 This function unfortunately can not die until stabs do. */
1771
1772void
1773check_stub_method_group (struct type *type, int method_id)
1774{
1775 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1776 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
f710f4fc 1777 int j, found_stub = 0;
de17c821
DJ
1778
1779 for (j = 0; j < len; j++)
1780 if (TYPE_FN_FIELD_STUB (f, j))
1781 {
1782 found_stub = 1;
1783 check_stub_method (type, method_id, j);
1784 }
1785
7ba81444
MS
1786 /* GNU v3 methods with incorrect names were corrected when we read
1787 in type information, because it was cheaper to do it then. The
1788 only GNU v2 methods with incorrect method names are operators and
1789 destructors; destructors were also corrected when we read in type
1790 information.
de17c821
DJ
1791
1792 Therefore the only thing we need to handle here are v2 operator
1793 names. */
1794 if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1795 {
1796 int ret;
1797 char dem_opname[256];
1798
7ba81444
MS
1799 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
1800 method_id),
de17c821
DJ
1801 dem_opname, DMGL_ANSI);
1802 if (!ret)
7ba81444
MS
1803 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
1804 method_id),
de17c821
DJ
1805 dem_opname, 0);
1806 if (ret)
1807 TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
1808 }
1809}
1810
9655fd1a
JK
1811/* Ensure it is in .rodata (if available) by workarounding GCC PR 44690. */
1812const struct cplus_struct_type cplus_struct_default = { };
c906108c
SS
1813
1814void
fba45db2 1815allocate_cplus_struct_type (struct type *type)
c906108c 1816{
b4ba55a1
JB
1817 if (HAVE_CPLUS_STRUCT (type))
1818 /* Structure was already allocated. Nothing more to do. */
1819 return;
1820
1821 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
1822 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1823 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1824 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
c906108c
SS
1825}
1826
b4ba55a1
JB
1827const struct gnat_aux_type gnat_aux_default =
1828 { NULL };
1829
1830/* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
1831 and allocate the associated gnat-specific data. The gnat-specific
1832 data is also initialized to gnat_aux_default. */
1833void
1834allocate_gnat_aux_type (struct type *type)
1835{
1836 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
1837 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
1838 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
1839 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
1840}
1841
1842
c906108c
SS
1843/* Helper function to initialize the standard scalar types.
1844
e9bb382b
UW
1845 If NAME is non-NULL, then we make a copy of the string pointed
1846 to by name in the objfile_obstack for that objfile, and initialize
1847 the type name to that copy. There are places (mipsread.c in particular),
1848 where init_type is called with a NULL value for NAME). */
c906108c
SS
1849
1850struct type *
7ba81444
MS
1851init_type (enum type_code code, int length, int flags,
1852 char *name, struct objfile *objfile)
c906108c 1853{
52f0bd74 1854 struct type *type;
c906108c
SS
1855
1856 type = alloc_type (objfile);
1857 TYPE_CODE (type) = code;
1858 TYPE_LENGTH (type) = length;
876cecd0
TT
1859
1860 gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
1861 if (flags & TYPE_FLAG_UNSIGNED)
1862 TYPE_UNSIGNED (type) = 1;
1863 if (flags & TYPE_FLAG_NOSIGN)
1864 TYPE_NOSIGN (type) = 1;
1865 if (flags & TYPE_FLAG_STUB)
1866 TYPE_STUB (type) = 1;
1867 if (flags & TYPE_FLAG_TARGET_STUB)
1868 TYPE_TARGET_STUB (type) = 1;
1869 if (flags & TYPE_FLAG_STATIC)
1870 TYPE_STATIC (type) = 1;
1871 if (flags & TYPE_FLAG_PROTOTYPED)
1872 TYPE_PROTOTYPED (type) = 1;
1873 if (flags & TYPE_FLAG_INCOMPLETE)
1874 TYPE_INCOMPLETE (type) = 1;
1875 if (flags & TYPE_FLAG_VARARGS)
1876 TYPE_VARARGS (type) = 1;
1877 if (flags & TYPE_FLAG_VECTOR)
1878 TYPE_VECTOR (type) = 1;
1879 if (flags & TYPE_FLAG_STUB_SUPPORTED)
1880 TYPE_STUB_SUPPORTED (type) = 1;
876cecd0
TT
1881 if (flags & TYPE_FLAG_FIXED_INSTANCE)
1882 TYPE_FIXED_INSTANCE (type) = 1;
1883
e9bb382b
UW
1884 if (name)
1885 TYPE_NAME (type) = obsavestring (name, strlen (name),
1886 &objfile->objfile_obstack);
c906108c
SS
1887
1888 /* C++ fancies. */
1889
973ccf8b 1890 if (name && strcmp (name, "char") == 0)
876cecd0 1891 TYPE_NOSIGN (type) = 1;
973ccf8b 1892
b4ba55a1 1893 switch (code)
c906108c 1894 {
b4ba55a1
JB
1895 case TYPE_CODE_STRUCT:
1896 case TYPE_CODE_UNION:
1897 case TYPE_CODE_NAMESPACE:
1898 INIT_CPLUS_SPECIFIC (type);
1899 break;
1900 case TYPE_CODE_FLT:
1901 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
1902 break;
1903 case TYPE_CODE_FUNC:
1904 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CALLING_CONVENTION;
1905 break;
c906108c 1906 }
c16abbde 1907 return type;
c906108c
SS
1908}
1909
c906108c 1910int
fba45db2 1911can_dereference (struct type *t)
c906108c 1912{
7ba81444
MS
1913 /* FIXME: Should we return true for references as well as
1914 pointers? */
c906108c
SS
1915 CHECK_TYPEDEF (t);
1916 return
1917 (t != NULL
1918 && TYPE_CODE (t) == TYPE_CODE_PTR
1919 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1920}
1921
adf40b2e 1922int
fba45db2 1923is_integral_type (struct type *t)
adf40b2e
JM
1924{
1925 CHECK_TYPEDEF (t);
1926 return
1927 ((t != NULL)
d4f3574e
SS
1928 && ((TYPE_CODE (t) == TYPE_CODE_INT)
1929 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
4f2aea11 1930 || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
d4f3574e
SS
1931 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
1932 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
1933 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
adf40b2e
JM
1934}
1935
4e8f195d
TT
1936/* A helper function which returns true if types A and B represent the
1937 "same" class type. This is true if the types have the same main
1938 type, or the same name. */
1939
1940int
1941class_types_same_p (const struct type *a, const struct type *b)
1942{
1943 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
1944 || (TYPE_NAME (a) && TYPE_NAME (b)
1945 && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
1946}
1947
0526b37a
SW
1948/* Check whether BASE is an ancestor or base class of DCLASS
1949 Return 1 if so, and 0 if not. If PUBLIC is 1 then only public
1950 ancestors are considered, and the function returns 1 only if
1951 BASE is a public ancestor of DCLASS. */
c906108c 1952
0526b37a
SW
1953static int
1954do_is_ancestor (struct type *base, struct type *dclass, int public)
c906108c
SS
1955{
1956 int i;
c5aa993b 1957
c906108c
SS
1958 CHECK_TYPEDEF (base);
1959 CHECK_TYPEDEF (dclass);
1960
4e8f195d 1961 if (class_types_same_p (base, dclass))
6b1ba9a0 1962 return 1;
c906108c
SS
1963
1964 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
4e8f195d 1965 {
0526b37a
SW
1966 if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
1967 continue;
1968
1969 if (do_is_ancestor (base, TYPE_BASECLASS (dclass, i), public))
4e8f195d
TT
1970 return 1;
1971 }
c906108c
SS
1972
1973 return 0;
1974}
4e8f195d 1975
0526b37a
SW
1976/* Check whether BASE is an ancestor or base class or DCLASS
1977 Return 1 if so, and 0 if not.
1978 Note: If BASE and DCLASS are of the same type, this function
1979 will return 1. So for some class A, is_ancestor (A, A) will
1980 return 1. */
1981
1982int
1983is_ancestor (struct type *base, struct type *dclass)
1984{
1985 return do_is_ancestor (base, dclass, 0);
1986}
1987
4e8f195d
TT
1988/* Like is_ancestor, but only returns true when BASE is a public
1989 ancestor of DCLASS. */
1990
1991int
1992is_public_ancestor (struct type *base, struct type *dclass)
1993{
0526b37a 1994 return do_is_ancestor (base, dclass, 1);
4e8f195d
TT
1995}
1996
1997/* A helper function for is_unique_ancestor. */
1998
1999static int
2000is_unique_ancestor_worker (struct type *base, struct type *dclass,
2001 int *offset,
2002 const bfd_byte *contents, CORE_ADDR address)
2003{
2004 int i, count = 0;
2005
2006 CHECK_TYPEDEF (base);
2007 CHECK_TYPEDEF (dclass);
2008
2009 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2010 {
2011 struct type *iter = check_typedef (TYPE_BASECLASS (dclass, i));
2012 int this_offset = baseclass_offset (dclass, i, contents, address);
2013
2014 if (this_offset == -1)
2015 error (_("virtual baseclass botch"));
2016
2017 if (class_types_same_p (base, iter))
2018 {
2019 /* If this is the first subclass, set *OFFSET and set count
2020 to 1. Otherwise, if this is at the same offset as
2021 previous instances, do nothing. Otherwise, increment
2022 count. */
2023 if (*offset == -1)
2024 {
2025 *offset = this_offset;
2026 count = 1;
2027 }
2028 else if (this_offset == *offset)
2029 {
2030 /* Nothing. */
2031 }
2032 else
2033 ++count;
2034 }
2035 else
2036 count += is_unique_ancestor_worker (base, iter, offset,
2037 contents + this_offset,
2038 address + this_offset);
2039 }
2040
2041 return count;
2042}
2043
2044/* Like is_ancestor, but only returns true if BASE is a unique base
2045 class of the type of VAL. */
2046
2047int
2048is_unique_ancestor (struct type *base, struct value *val)
2049{
2050 int offset = -1;
2051
2052 return is_unique_ancestor_worker (base, value_type (val), &offset,
2053 value_contents (val),
2054 value_address (val)) == 1;
2055}
2056
c906108c
SS
2057\f
2058
c5aa993b 2059
c906108c
SS
2060/* Functions for overload resolution begin here */
2061
2062/* Compare two badness vectors A and B and return the result.
7ba81444
MS
2063 0 => A and B are identical
2064 1 => A and B are incomparable
2065 2 => A is better than B
2066 3 => A is worse than B */
c906108c
SS
2067
2068int
fba45db2 2069compare_badness (struct badness_vector *a, struct badness_vector *b)
c906108c
SS
2070{
2071 int i;
2072 int tmp;
c5aa993b
JM
2073 short found_pos = 0; /* any positives in c? */
2074 short found_neg = 0; /* any negatives in c? */
2075
2076 /* differing lengths => incomparable */
c906108c
SS
2077 if (a->length != b->length)
2078 return 1;
2079
c5aa993b
JM
2080 /* Subtract b from a */
2081 for (i = 0; i < a->length; i++)
c906108c
SS
2082 {
2083 tmp = a->rank[i] - b->rank[i];
2084 if (tmp > 0)
c5aa993b 2085 found_pos = 1;
c906108c 2086 else if (tmp < 0)
c5aa993b 2087 found_neg = 1;
c906108c
SS
2088 }
2089
2090 if (found_pos)
2091 {
2092 if (found_neg)
c5aa993b 2093 return 1; /* incomparable */
c906108c 2094 else
c5aa993b 2095 return 3; /* A > B */
c906108c 2096 }
c5aa993b
JM
2097 else
2098 /* no positives */
c906108c
SS
2099 {
2100 if (found_neg)
c5aa993b 2101 return 2; /* A < B */
c906108c 2102 else
c5aa993b 2103 return 0; /* A == B */
c906108c
SS
2104 }
2105}
2106
7ba81444
MS
2107/* Rank a function by comparing its parameter types (PARMS, length
2108 NPARMS), to the types of an argument list (ARGS, length NARGS).
2109 Return a pointer to a badness vector. This has NARGS + 1
2110 entries. */
c906108c
SS
2111
2112struct badness_vector *
7ba81444
MS
2113rank_function (struct type **parms, int nparms,
2114 struct type **args, int nargs)
c906108c
SS
2115{
2116 int i;
c5aa993b 2117 struct badness_vector *bv;
c906108c
SS
2118 int min_len = nparms < nargs ? nparms : nargs;
2119
2120 bv = xmalloc (sizeof (struct badness_vector));
c5aa993b 2121 bv->length = nargs + 1; /* add 1 for the length-match rank */
c906108c
SS
2122 bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2123
2124 /* First compare the lengths of the supplied lists.
7ba81444 2125 If there is a mismatch, set it to a high value. */
c5aa993b 2126
c906108c 2127 /* pai/1997-06-03 FIXME: when we have debug info about default
7ba81444
MS
2128 arguments and ellipsis parameter lists, we should consider those
2129 and rank the length-match more finely. */
c906108c
SS
2130
2131 LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
2132
2133 /* Now rank all the parameters of the candidate function */
74cc24b0
DB
2134 for (i = 1; i <= min_len; i++)
2135 bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
c906108c 2136
c5aa993b
JM
2137 /* If more arguments than parameters, add dummy entries */
2138 for (i = min_len + 1; i <= nargs; i++)
c906108c
SS
2139 bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2140
2141 return bv;
2142}
2143
973ccf8b
DJ
2144/* Compare the names of two integer types, assuming that any sign
2145 qualifiers have been checked already. We do it this way because
2146 there may be an "int" in the name of one of the types. */
2147
2148static int
2149integer_types_same_name_p (const char *first, const char *second)
2150{
2151 int first_p, second_p;
2152
7ba81444
MS
2153 /* If both are shorts, return 1; if neither is a short, keep
2154 checking. */
973ccf8b
DJ
2155 first_p = (strstr (first, "short") != NULL);
2156 second_p = (strstr (second, "short") != NULL);
2157 if (first_p && second_p)
2158 return 1;
2159 if (first_p || second_p)
2160 return 0;
2161
2162 /* Likewise for long. */
2163 first_p = (strstr (first, "long") != NULL);
2164 second_p = (strstr (second, "long") != NULL);
2165 if (first_p && second_p)
2166 return 1;
2167 if (first_p || second_p)
2168 return 0;
2169
2170 /* Likewise for char. */
2171 first_p = (strstr (first, "char") != NULL);
2172 second_p = (strstr (second, "char") != NULL);
2173 if (first_p && second_p)
2174 return 1;
2175 if (first_p || second_p)
2176 return 0;
2177
2178 /* They must both be ints. */
2179 return 1;
2180}
2181
7062b0a0
SW
2182/* Compares type A to type B returns 1 if the represent the same type
2183 0 otherwise. */
2184
2185static int
2186types_equal (struct type *a, struct type *b)
2187{
2188 /* Identical type pointers. */
2189 /* However, this still doesn't catch all cases of same type for b
2190 and a. The reason is that builtin types are different from
2191 the same ones constructed from the object. */
2192 if (a == b)
2193 return 1;
2194
2195 /* Resolve typedefs */
2196 if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2197 a = check_typedef (a);
2198 if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2199 b = check_typedef (b);
2200
2201 /* If after resolving typedefs a and b are not of the same type
2202 code then they are not equal. */
2203 if (TYPE_CODE (a) != TYPE_CODE (b))
2204 return 0;
2205
2206 /* If a and b are both pointers types or both reference types then
2207 they are equal of the same type iff the objects they refer to are
2208 of the same type. */
2209 if (TYPE_CODE (a) == TYPE_CODE_PTR
2210 || TYPE_CODE (a) == TYPE_CODE_REF)
2211 return types_equal (TYPE_TARGET_TYPE (a),
2212 TYPE_TARGET_TYPE (b));
2213
2214 /*
2215 Well, damnit, if the names are exactly the same, I'll say they
2216 are exactly the same. This happens when we generate method
2217 stubs. The types won't point to the same address, but they
2218 really are the same.
2219 */
2220
2221 if (TYPE_NAME (a) && TYPE_NAME (b)
2222 && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2223 return 1;
2224
2225 /* Check if identical after resolving typedefs. */
2226 if (a == b)
2227 return 1;
2228
2229 return 0;
2230}
2231
c906108c
SS
2232/* Compare one type (PARM) for compatibility with another (ARG).
2233 * PARM is intended to be the parameter type of a function; and
2234 * ARG is the supplied argument's type. This function tests if
2235 * the latter can be converted to the former.
2236 *
2237 * Return 0 if they are identical types;
2238 * Otherwise, return an integer which corresponds to how compatible
7ba81444
MS
2239 * PARM is to ARG. The higher the return value, the worse the match.
2240 * Generally the "bad" conversions are all uniformly assigned a 100. */
c906108c
SS
2241
2242int
fba45db2 2243rank_one_type (struct type *parm, struct type *arg)
c906108c 2244{
7062b0a0
SW
2245
2246 if (types_equal (parm, arg))
c906108c
SS
2247 return 0;
2248
2249 /* Resolve typedefs */
2250 if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2251 parm = check_typedef (parm);
2252 if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2253 arg = check_typedef (arg);
2254
db577aea 2255 /* See through references, since we can almost make non-references
7ba81444 2256 references. */
db577aea 2257 if (TYPE_CODE (arg) == TYPE_CODE_REF)
6b1ba9a0 2258 return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
db577aea
AC
2259 + REFERENCE_CONVERSION_BADNESS);
2260 if (TYPE_CODE (parm) == TYPE_CODE_REF)
6b1ba9a0 2261 return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
db577aea 2262 + REFERENCE_CONVERSION_BADNESS);
5d161b24 2263 if (overload_debug)
7ba81444
MS
2264 /* Debugging only. */
2265 fprintf_filtered (gdb_stderr,
2266 "------ Arg is %s [%d], parm is %s [%d]\n",
2267 TYPE_NAME (arg), TYPE_CODE (arg),
2268 TYPE_NAME (parm), TYPE_CODE (parm));
c906108c
SS
2269
2270 /* x -> y means arg of type x being supplied for parameter of type y */
2271
2272 switch (TYPE_CODE (parm))
2273 {
c5aa993b
JM
2274 case TYPE_CODE_PTR:
2275 switch (TYPE_CODE (arg))
2276 {
2277 case TYPE_CODE_PTR:
7062b0a0
SW
2278
2279 /* Allowed pointer conversions are:
2280 (a) pointer to void-pointer conversion. */
2281 if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
c5aa993b 2282 return VOID_PTR_CONVERSION_BADNESS;
7062b0a0
SW
2283
2284 /* (b) pointer to ancestor-pointer conversion. */
2285 if (is_ancestor (TYPE_TARGET_TYPE (parm),
2286 TYPE_TARGET_TYPE (arg)))
2287 return BASE_PTR_CONVERSION_BADNESS;
2288
2289 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b 2290 case TYPE_CODE_ARRAY:
7062b0a0
SW
2291 if (types_equal (TYPE_TARGET_TYPE (parm),
2292 TYPE_TARGET_TYPE (arg)))
2293 return 0;
2294 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b
JM
2295 case TYPE_CODE_FUNC:
2296 return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2297 case TYPE_CODE_INT:
2298 case TYPE_CODE_ENUM:
4f2aea11 2299 case TYPE_CODE_FLAGS:
c5aa993b
JM
2300 case TYPE_CODE_CHAR:
2301 case TYPE_CODE_RANGE:
2302 case TYPE_CODE_BOOL:
c5aa993b
JM
2303 default:
2304 return INCOMPATIBLE_TYPE_BADNESS;
2305 }
2306 case TYPE_CODE_ARRAY:
2307 switch (TYPE_CODE (arg))
2308 {
2309 case TYPE_CODE_PTR:
2310 case TYPE_CODE_ARRAY:
7ba81444
MS
2311 return rank_one_type (TYPE_TARGET_TYPE (parm),
2312 TYPE_TARGET_TYPE (arg));
c5aa993b
JM
2313 default:
2314 return INCOMPATIBLE_TYPE_BADNESS;
2315 }
2316 case TYPE_CODE_FUNC:
2317 switch (TYPE_CODE (arg))
2318 {
2319 case TYPE_CODE_PTR: /* funcptr -> func */
2320 return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2321 default:
2322 return INCOMPATIBLE_TYPE_BADNESS;
2323 }
2324 case TYPE_CODE_INT:
2325 switch (TYPE_CODE (arg))
2326 {
2327 case TYPE_CODE_INT:
2328 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2329 {
2330 /* Deal with signed, unsigned, and plain chars and
7ba81444 2331 signed and unsigned ints. */
c5aa993b
JM
2332 if (TYPE_NOSIGN (parm))
2333 {
2334 /* This case only for character types */
7ba81444
MS
2335 if (TYPE_NOSIGN (arg))
2336 return 0; /* plain char -> plain char */
2337 else /* signed/unsigned char -> plain char */
2338 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2339 }
2340 else if (TYPE_UNSIGNED (parm))
2341 {
2342 if (TYPE_UNSIGNED (arg))
2343 {
7ba81444
MS
2344 /* unsigned int -> unsigned int, or
2345 unsigned long -> unsigned long */
2346 if (integer_types_same_name_p (TYPE_NAME (parm),
2347 TYPE_NAME (arg)))
973ccf8b 2348 return 0;
7ba81444
MS
2349 else if (integer_types_same_name_p (TYPE_NAME (arg),
2350 "int")
2351 && integer_types_same_name_p (TYPE_NAME (parm),
2352 "long"))
c5aa993b
JM
2353 return INTEGER_PROMOTION_BADNESS; /* unsigned int -> unsigned long */
2354 else
1c5cb38e 2355 return INTEGER_CONVERSION_BADNESS; /* unsigned long -> unsigned int */
c5aa993b
JM
2356 }
2357 else
2358 {
7ba81444
MS
2359 if (integer_types_same_name_p (TYPE_NAME (arg),
2360 "long")
2361 && integer_types_same_name_p (TYPE_NAME (parm),
2362 "int"))
1c5cb38e 2363 return INTEGER_CONVERSION_BADNESS; /* signed long -> unsigned int */
c5aa993b
JM
2364 else
2365 return INTEGER_CONVERSION_BADNESS; /* signed int/long -> unsigned int/long */
2366 }
2367 }
2368 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2369 {
7ba81444
MS
2370 if (integer_types_same_name_p (TYPE_NAME (parm),
2371 TYPE_NAME (arg)))
c5aa993b 2372 return 0;
7ba81444
MS
2373 else if (integer_types_same_name_p (TYPE_NAME (arg),
2374 "int")
2375 && integer_types_same_name_p (TYPE_NAME (parm),
2376 "long"))
c5aa993b
JM
2377 return INTEGER_PROMOTION_BADNESS;
2378 else
1c5cb38e 2379 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2380 }
2381 else
1c5cb38e 2382 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2383 }
2384 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2385 return INTEGER_PROMOTION_BADNESS;
2386 else
1c5cb38e 2387 return INTEGER_CONVERSION_BADNESS;
c5aa993b 2388 case TYPE_CODE_ENUM:
4f2aea11 2389 case TYPE_CODE_FLAGS:
c5aa993b
JM
2390 case TYPE_CODE_CHAR:
2391 case TYPE_CODE_RANGE:
2392 case TYPE_CODE_BOOL:
2393 return INTEGER_PROMOTION_BADNESS;
2394 case TYPE_CODE_FLT:
2395 return INT_FLOAT_CONVERSION_BADNESS;
2396 case TYPE_CODE_PTR:
2397 return NS_POINTER_CONVERSION_BADNESS;
2398 default:
2399 return INCOMPATIBLE_TYPE_BADNESS;
2400 }
2401 break;
2402 case TYPE_CODE_ENUM:
2403 switch (TYPE_CODE (arg))
2404 {
2405 case TYPE_CODE_INT:
2406 case TYPE_CODE_CHAR:
2407 case TYPE_CODE_RANGE:
2408 case TYPE_CODE_BOOL:
2409 case TYPE_CODE_ENUM:
1c5cb38e 2410 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2411 case TYPE_CODE_FLT:
2412 return INT_FLOAT_CONVERSION_BADNESS;
2413 default:
2414 return INCOMPATIBLE_TYPE_BADNESS;
2415 }
2416 break;
2417 case TYPE_CODE_CHAR:
2418 switch (TYPE_CODE (arg))
2419 {
2420 case TYPE_CODE_RANGE:
2421 case TYPE_CODE_BOOL:
2422 case TYPE_CODE_ENUM:
1c5cb38e 2423 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2424 case TYPE_CODE_FLT:
2425 return INT_FLOAT_CONVERSION_BADNESS;
2426 case TYPE_CODE_INT:
2427 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
1c5cb38e 2428 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2429 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2430 return INTEGER_PROMOTION_BADNESS;
2431 /* >>> !! else fall through !! <<< */
2432 case TYPE_CODE_CHAR:
7ba81444
MS
2433 /* Deal with signed, unsigned, and plain chars for C++ and
2434 with int cases falling through from previous case. */
c5aa993b
JM
2435 if (TYPE_NOSIGN (parm))
2436 {
2437 if (TYPE_NOSIGN (arg))
2438 return 0;
2439 else
1c5cb38e 2440 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2441 }
2442 else if (TYPE_UNSIGNED (parm))
2443 {
2444 if (TYPE_UNSIGNED (arg))
2445 return 0;
2446 else
2447 return INTEGER_PROMOTION_BADNESS;
2448 }
2449 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2450 return 0;
2451 else
1c5cb38e 2452 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2453 default:
2454 return INCOMPATIBLE_TYPE_BADNESS;
2455 }
2456 break;
2457 case TYPE_CODE_RANGE:
2458 switch (TYPE_CODE (arg))
2459 {
2460 case TYPE_CODE_INT:
2461 case TYPE_CODE_CHAR:
2462 case TYPE_CODE_RANGE:
2463 case TYPE_CODE_BOOL:
2464 case TYPE_CODE_ENUM:
1c5cb38e 2465 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
2466 case TYPE_CODE_FLT:
2467 return INT_FLOAT_CONVERSION_BADNESS;
2468 default:
2469 return INCOMPATIBLE_TYPE_BADNESS;
2470 }
2471 break;
2472 case TYPE_CODE_BOOL:
2473 switch (TYPE_CODE (arg))
2474 {
2475 case TYPE_CODE_INT:
2476 case TYPE_CODE_CHAR:
2477 case TYPE_CODE_RANGE:
2478 case TYPE_CODE_ENUM:
2479 case TYPE_CODE_FLT:
026ffab7 2480 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b 2481 case TYPE_CODE_PTR:
026ffab7 2482 return BOOL_PTR_CONVERSION_BADNESS;
c5aa993b
JM
2483 case TYPE_CODE_BOOL:
2484 return 0;
2485 default:
2486 return INCOMPATIBLE_TYPE_BADNESS;
2487 }
2488 break;
2489 case TYPE_CODE_FLT:
2490 switch (TYPE_CODE (arg))
2491 {
2492 case TYPE_CODE_FLT:
2493 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2494 return FLOAT_PROMOTION_BADNESS;
2495 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2496 return 0;
2497 else
2498 return FLOAT_CONVERSION_BADNESS;
2499 case TYPE_CODE_INT:
2500 case TYPE_CODE_BOOL:
2501 case TYPE_CODE_ENUM:
2502 case TYPE_CODE_RANGE:
2503 case TYPE_CODE_CHAR:
2504 return INT_FLOAT_CONVERSION_BADNESS;
2505 default:
2506 return INCOMPATIBLE_TYPE_BADNESS;
2507 }
2508 break;
2509 case TYPE_CODE_COMPLEX:
2510 switch (TYPE_CODE (arg))
7ba81444 2511 { /* Strictly not needed for C++, but... */
c5aa993b
JM
2512 case TYPE_CODE_FLT:
2513 return FLOAT_PROMOTION_BADNESS;
2514 case TYPE_CODE_COMPLEX:
2515 return 0;
2516 default:
2517 return INCOMPATIBLE_TYPE_BADNESS;
2518 }
2519 break;
2520 case TYPE_CODE_STRUCT:
c906108c 2521 /* currently same as TYPE_CODE_CLASS */
c5aa993b
JM
2522 switch (TYPE_CODE (arg))
2523 {
2524 case TYPE_CODE_STRUCT:
2525 /* Check for derivation */
2526 if (is_ancestor (parm, arg))
2527 return BASE_CONVERSION_BADNESS;
2528 /* else fall through */
2529 default:
2530 return INCOMPATIBLE_TYPE_BADNESS;
2531 }
2532 break;
2533 case TYPE_CODE_UNION:
2534 switch (TYPE_CODE (arg))
2535 {
2536 case TYPE_CODE_UNION:
2537 default:
2538 return INCOMPATIBLE_TYPE_BADNESS;
2539 }
2540 break;
0d5de010 2541 case TYPE_CODE_MEMBERPTR:
c5aa993b
JM
2542 switch (TYPE_CODE (arg))
2543 {
2544 default:
2545 return INCOMPATIBLE_TYPE_BADNESS;
2546 }
2547 break;
2548 case TYPE_CODE_METHOD:
2549 switch (TYPE_CODE (arg))
2550 {
2551
2552 default:
2553 return INCOMPATIBLE_TYPE_BADNESS;
2554 }
2555 break;
2556 case TYPE_CODE_REF:
2557 switch (TYPE_CODE (arg))
2558 {
2559
2560 default:
2561 return INCOMPATIBLE_TYPE_BADNESS;
2562 }
2563
2564 break;
2565 case TYPE_CODE_SET:
2566 switch (TYPE_CODE (arg))
2567 {
2568 /* Not in C++ */
2569 case TYPE_CODE_SET:
7ba81444
MS
2570 return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
2571 TYPE_FIELD_TYPE (arg, 0));
c5aa993b
JM
2572 default:
2573 return INCOMPATIBLE_TYPE_BADNESS;
2574 }
2575 break;
2576 case TYPE_CODE_VOID:
2577 default:
2578 return INCOMPATIBLE_TYPE_BADNESS;
2579 } /* switch (TYPE_CODE (arg)) */
c906108c
SS
2580}
2581
c5aa993b
JM
2582
2583/* End of functions for overload resolution */
c906108c 2584
c906108c 2585static void
fba45db2 2586print_bit_vector (B_TYPE *bits, int nbits)
c906108c
SS
2587{
2588 int bitno;
2589
2590 for (bitno = 0; bitno < nbits; bitno++)
2591 {
2592 if ((bitno % 8) == 0)
2593 {
2594 puts_filtered (" ");
2595 }
2596 if (B_TST (bits, bitno))
a3f17187 2597 printf_filtered (("1"));
c906108c 2598 else
a3f17187 2599 printf_filtered (("0"));
c906108c
SS
2600 }
2601}
2602
ad2f7632 2603/* Note the first arg should be the "this" pointer, we may not want to
7ba81444
MS
2604 include it since we may get into a infinitely recursive
2605 situation. */
c906108c
SS
2606
2607static void
ad2f7632 2608print_arg_types (struct field *args, int nargs, int spaces)
c906108c
SS
2609{
2610 if (args != NULL)
2611 {
ad2f7632
DJ
2612 int i;
2613
2614 for (i = 0; i < nargs; i++)
2615 recursive_dump_type (args[i].type, spaces + 2);
c906108c
SS
2616 }
2617}
2618
d6a843b5
JK
2619int
2620field_is_static (struct field *f)
2621{
2622 /* "static" fields are the fields whose location is not relative
2623 to the address of the enclosing struct. It would be nice to
2624 have a dedicated flag that would be set for static fields when
2625 the type is being created. But in practice, checking the field
254e6b9e 2626 loc_kind should give us an accurate answer. */
d6a843b5
JK
2627 return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
2628 || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
2629}
2630
c906108c 2631static void
fba45db2 2632dump_fn_fieldlists (struct type *type, int spaces)
c906108c
SS
2633{
2634 int method_idx;
2635 int overload_idx;
2636 struct fn_field *f;
2637
2638 printfi_filtered (spaces, "fn_fieldlists ");
d4f3574e 2639 gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
c906108c
SS
2640 printf_filtered ("\n");
2641 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2642 {
2643 f = TYPE_FN_FIELDLIST1 (type, method_idx);
2644 printfi_filtered (spaces + 2, "[%d] name '%s' (",
2645 method_idx,
2646 TYPE_FN_FIELDLIST_NAME (type, method_idx));
d4f3574e
SS
2647 gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2648 gdb_stdout);
a3f17187 2649 printf_filtered (_(") length %d\n"),
c906108c
SS
2650 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2651 for (overload_idx = 0;
2652 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2653 overload_idx++)
2654 {
2655 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2656 overload_idx,
2657 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
d4f3574e
SS
2658 gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2659 gdb_stdout);
c906108c
SS
2660 printf_filtered (")\n");
2661 printfi_filtered (spaces + 8, "type ");
7ba81444
MS
2662 gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
2663 gdb_stdout);
c906108c
SS
2664 printf_filtered ("\n");
2665
2666 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2667 spaces + 8 + 2);
2668
2669 printfi_filtered (spaces + 8, "args ");
7ba81444
MS
2670 gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
2671 gdb_stdout);
c906108c
SS
2672 printf_filtered ("\n");
2673
ad2f7632 2674 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
7ba81444
MS
2675 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f,
2676 overload_idx)),
ad2f7632 2677 spaces);
c906108c 2678 printfi_filtered (spaces + 8, "fcontext ");
d4f3574e
SS
2679 gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2680 gdb_stdout);
c906108c
SS
2681 printf_filtered ("\n");
2682
2683 printfi_filtered (spaces + 8, "is_const %d\n",
2684 TYPE_FN_FIELD_CONST (f, overload_idx));
2685 printfi_filtered (spaces + 8, "is_volatile %d\n",
2686 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2687 printfi_filtered (spaces + 8, "is_private %d\n",
2688 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2689 printfi_filtered (spaces + 8, "is_protected %d\n",
2690 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2691 printfi_filtered (spaces + 8, "is_stub %d\n",
2692 TYPE_FN_FIELD_STUB (f, overload_idx));
2693 printfi_filtered (spaces + 8, "voffset %u\n",
2694 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2695 }
2696 }
2697}
2698
2699static void
fba45db2 2700print_cplus_stuff (struct type *type, int spaces)
c906108c
SS
2701{
2702 printfi_filtered (spaces, "n_baseclasses %d\n",
2703 TYPE_N_BASECLASSES (type));
2704 printfi_filtered (spaces, "nfn_fields %d\n",
2705 TYPE_NFN_FIELDS (type));
2706 printfi_filtered (spaces, "nfn_fields_total %d\n",
2707 TYPE_NFN_FIELDS_TOTAL (type));
2708 if (TYPE_N_BASECLASSES (type) > 0)
2709 {
2710 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2711 TYPE_N_BASECLASSES (type));
7ba81444
MS
2712 gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
2713 gdb_stdout);
c906108c
SS
2714 printf_filtered (")");
2715
2716 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2717 TYPE_N_BASECLASSES (type));
2718 puts_filtered ("\n");
2719 }
2720 if (TYPE_NFIELDS (type) > 0)
2721 {
2722 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2723 {
7ba81444
MS
2724 printfi_filtered (spaces,
2725 "private_field_bits (%d bits at *",
c906108c 2726 TYPE_NFIELDS (type));
7ba81444
MS
2727 gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
2728 gdb_stdout);
c906108c
SS
2729 printf_filtered (")");
2730 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2731 TYPE_NFIELDS (type));
2732 puts_filtered ("\n");
2733 }
2734 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2735 {
7ba81444
MS
2736 printfi_filtered (spaces,
2737 "protected_field_bits (%d bits at *",
c906108c 2738 TYPE_NFIELDS (type));
7ba81444
MS
2739 gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
2740 gdb_stdout);
c906108c
SS
2741 printf_filtered (")");
2742 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2743 TYPE_NFIELDS (type));
2744 puts_filtered ("\n");
2745 }
2746 }
2747 if (TYPE_NFN_FIELDS (type) > 0)
2748 {
2749 dump_fn_fieldlists (type, spaces);
2750 }
2751}
2752
b4ba55a1
JB
2753/* Print the contents of the TYPE's type_specific union, assuming that
2754 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
2755
2756static void
2757print_gnat_stuff (struct type *type, int spaces)
2758{
2759 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
2760
2761 recursive_dump_type (descriptive_type, spaces + 2);
2762}
2763
c906108c
SS
2764static struct obstack dont_print_type_obstack;
2765
2766void
fba45db2 2767recursive_dump_type (struct type *type, int spaces)
c906108c
SS
2768{
2769 int idx;
2770
2771 if (spaces == 0)
2772 obstack_begin (&dont_print_type_obstack, 0);
2773
2774 if (TYPE_NFIELDS (type) > 0
b4ba55a1 2775 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
c906108c
SS
2776 {
2777 struct type **first_dont_print
7ba81444 2778 = (struct type **) obstack_base (&dont_print_type_obstack);
c906108c 2779
7ba81444
MS
2780 int i = (struct type **)
2781 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
c906108c
SS
2782
2783 while (--i >= 0)
2784 {
2785 if (type == first_dont_print[i])
2786 {
2787 printfi_filtered (spaces, "type node ");
d4f3574e 2788 gdb_print_host_address (type, gdb_stdout);
a3f17187 2789 printf_filtered (_(" <same as already seen type>\n"));
c906108c
SS
2790 return;
2791 }
2792 }
2793
2794 obstack_ptr_grow (&dont_print_type_obstack, type);
2795 }
2796
2797 printfi_filtered (spaces, "type node ");
d4f3574e 2798 gdb_print_host_address (type, gdb_stdout);
c906108c
SS
2799 printf_filtered ("\n");
2800 printfi_filtered (spaces, "name '%s' (",
2801 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
d4f3574e 2802 gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
c906108c 2803 printf_filtered (")\n");
e9e79dd9
FF
2804 printfi_filtered (spaces, "tagname '%s' (",
2805 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
2806 gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2807 printf_filtered (")\n");
c906108c
SS
2808 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2809 switch (TYPE_CODE (type))
2810 {
c5aa993b
JM
2811 case TYPE_CODE_UNDEF:
2812 printf_filtered ("(TYPE_CODE_UNDEF)");
2813 break;
2814 case TYPE_CODE_PTR:
2815 printf_filtered ("(TYPE_CODE_PTR)");
2816 break;
2817 case TYPE_CODE_ARRAY:
2818 printf_filtered ("(TYPE_CODE_ARRAY)");
2819 break;
2820 case TYPE_CODE_STRUCT:
2821 printf_filtered ("(TYPE_CODE_STRUCT)");
2822 break;
2823 case TYPE_CODE_UNION:
2824 printf_filtered ("(TYPE_CODE_UNION)");
2825 break;
2826 case TYPE_CODE_ENUM:
2827 printf_filtered ("(TYPE_CODE_ENUM)");
2828 break;
4f2aea11
MK
2829 case TYPE_CODE_FLAGS:
2830 printf_filtered ("(TYPE_CODE_FLAGS)");
2831 break;
c5aa993b
JM
2832 case TYPE_CODE_FUNC:
2833 printf_filtered ("(TYPE_CODE_FUNC)");
2834 break;
2835 case TYPE_CODE_INT:
2836 printf_filtered ("(TYPE_CODE_INT)");
2837 break;
2838 case TYPE_CODE_FLT:
2839 printf_filtered ("(TYPE_CODE_FLT)");
2840 break;
2841 case TYPE_CODE_VOID:
2842 printf_filtered ("(TYPE_CODE_VOID)");
2843 break;
2844 case TYPE_CODE_SET:
2845 printf_filtered ("(TYPE_CODE_SET)");
2846 break;
2847 case TYPE_CODE_RANGE:
2848 printf_filtered ("(TYPE_CODE_RANGE)");
2849 break;
2850 case TYPE_CODE_STRING:
2851 printf_filtered ("(TYPE_CODE_STRING)");
2852 break;
e9e79dd9
FF
2853 case TYPE_CODE_BITSTRING:
2854 printf_filtered ("(TYPE_CODE_BITSTRING)");
2855 break;
c5aa993b
JM
2856 case TYPE_CODE_ERROR:
2857 printf_filtered ("(TYPE_CODE_ERROR)");
2858 break;
0d5de010
DJ
2859 case TYPE_CODE_MEMBERPTR:
2860 printf_filtered ("(TYPE_CODE_MEMBERPTR)");
2861 break;
2862 case TYPE_CODE_METHODPTR:
2863 printf_filtered ("(TYPE_CODE_METHODPTR)");
c5aa993b
JM
2864 break;
2865 case TYPE_CODE_METHOD:
2866 printf_filtered ("(TYPE_CODE_METHOD)");
2867 break;
2868 case TYPE_CODE_REF:
2869 printf_filtered ("(TYPE_CODE_REF)");
2870 break;
2871 case TYPE_CODE_CHAR:
2872 printf_filtered ("(TYPE_CODE_CHAR)");
2873 break;
2874 case TYPE_CODE_BOOL:
2875 printf_filtered ("(TYPE_CODE_BOOL)");
2876 break;
e9e79dd9
FF
2877 case TYPE_CODE_COMPLEX:
2878 printf_filtered ("(TYPE_CODE_COMPLEX)");
2879 break;
c5aa993b
JM
2880 case TYPE_CODE_TYPEDEF:
2881 printf_filtered ("(TYPE_CODE_TYPEDEF)");
2882 break;
5c4e30ca
DC
2883 case TYPE_CODE_NAMESPACE:
2884 printf_filtered ("(TYPE_CODE_NAMESPACE)");
2885 break;
c5aa993b
JM
2886 default:
2887 printf_filtered ("(UNKNOWN TYPE CODE)");
2888 break;
c906108c
SS
2889 }
2890 puts_filtered ("\n");
2891 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
e9bb382b
UW
2892 if (TYPE_OBJFILE_OWNED (type))
2893 {
2894 printfi_filtered (spaces, "objfile ");
2895 gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
2896 }
2897 else
2898 {
2899 printfi_filtered (spaces, "gdbarch ");
2900 gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
2901 }
c906108c
SS
2902 printf_filtered ("\n");
2903 printfi_filtered (spaces, "target_type ");
d4f3574e 2904 gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
c906108c
SS
2905 printf_filtered ("\n");
2906 if (TYPE_TARGET_TYPE (type) != NULL)
2907 {
2908 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
2909 }
2910 printfi_filtered (spaces, "pointer_type ");
d4f3574e 2911 gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
c906108c
SS
2912 printf_filtered ("\n");
2913 printfi_filtered (spaces, "reference_type ");
d4f3574e 2914 gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
c906108c 2915 printf_filtered ("\n");
2fdde8f8
DJ
2916 printfi_filtered (spaces, "type_chain ");
2917 gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
e9e79dd9 2918 printf_filtered ("\n");
7ba81444
MS
2919 printfi_filtered (spaces, "instance_flags 0x%x",
2920 TYPE_INSTANCE_FLAGS (type));
2fdde8f8
DJ
2921 if (TYPE_CONST (type))
2922 {
2923 puts_filtered (" TYPE_FLAG_CONST");
2924 }
2925 if (TYPE_VOLATILE (type))
2926 {
2927 puts_filtered (" TYPE_FLAG_VOLATILE");
2928 }
2929 if (TYPE_CODE_SPACE (type))
2930 {
2931 puts_filtered (" TYPE_FLAG_CODE_SPACE");
2932 }
2933 if (TYPE_DATA_SPACE (type))
2934 {
2935 puts_filtered (" TYPE_FLAG_DATA_SPACE");
2936 }
8b2dbe47
KB
2937 if (TYPE_ADDRESS_CLASS_1 (type))
2938 {
2939 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
2940 }
2941 if (TYPE_ADDRESS_CLASS_2 (type))
2942 {
2943 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
2944 }
2fdde8f8 2945 puts_filtered ("\n");
876cecd0
TT
2946
2947 printfi_filtered (spaces, "flags");
762a036f 2948 if (TYPE_UNSIGNED (type))
c906108c
SS
2949 {
2950 puts_filtered (" TYPE_FLAG_UNSIGNED");
2951 }
762a036f
FF
2952 if (TYPE_NOSIGN (type))
2953 {
2954 puts_filtered (" TYPE_FLAG_NOSIGN");
2955 }
2956 if (TYPE_STUB (type))
c906108c
SS
2957 {
2958 puts_filtered (" TYPE_FLAG_STUB");
2959 }
762a036f
FF
2960 if (TYPE_TARGET_STUB (type))
2961 {
2962 puts_filtered (" TYPE_FLAG_TARGET_STUB");
2963 }
2964 if (TYPE_STATIC (type))
2965 {
2966 puts_filtered (" TYPE_FLAG_STATIC");
2967 }
762a036f
FF
2968 if (TYPE_PROTOTYPED (type))
2969 {
2970 puts_filtered (" TYPE_FLAG_PROTOTYPED");
2971 }
2972 if (TYPE_INCOMPLETE (type))
2973 {
2974 puts_filtered (" TYPE_FLAG_INCOMPLETE");
2975 }
762a036f
FF
2976 if (TYPE_VARARGS (type))
2977 {
2978 puts_filtered (" TYPE_FLAG_VARARGS");
2979 }
f5f8a009
EZ
2980 /* This is used for things like AltiVec registers on ppc. Gcc emits
2981 an attribute for the array type, which tells whether or not we
2982 have a vector, instead of a regular array. */
2983 if (TYPE_VECTOR (type))
2984 {
2985 puts_filtered (" TYPE_FLAG_VECTOR");
2986 }
876cecd0
TT
2987 if (TYPE_FIXED_INSTANCE (type))
2988 {
2989 puts_filtered (" TYPE_FIXED_INSTANCE");
2990 }
2991 if (TYPE_STUB_SUPPORTED (type))
2992 {
2993 puts_filtered (" TYPE_STUB_SUPPORTED");
2994 }
2995 if (TYPE_NOTTEXT (type))
2996 {
2997 puts_filtered (" TYPE_NOTTEXT");
2998 }
c906108c
SS
2999 puts_filtered ("\n");
3000 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
d4f3574e 3001 gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
c906108c
SS
3002 puts_filtered ("\n");
3003 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3004 {
3005 printfi_filtered (spaces + 2,
3006 "[%d] bitpos %d bitsize %d type ",
3007 idx, TYPE_FIELD_BITPOS (type, idx),
3008 TYPE_FIELD_BITSIZE (type, idx));
d4f3574e 3009 gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
c906108c
SS
3010 printf_filtered (" name '%s' (",
3011 TYPE_FIELD_NAME (type, idx) != NULL
3012 ? TYPE_FIELD_NAME (type, idx)
3013 : "<NULL>");
d4f3574e 3014 gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
c906108c
SS
3015 printf_filtered (")\n");
3016 if (TYPE_FIELD_TYPE (type, idx) != NULL)
3017 {
3018 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3019 }
3020 }
43bbcdc2
PH
3021 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3022 {
3023 printfi_filtered (spaces, "low %s%s high %s%s\n",
3024 plongest (TYPE_LOW_BOUND (type)),
3025 TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3026 plongest (TYPE_HIGH_BOUND (type)),
3027 TYPE_HIGH_BOUND_UNDEFINED (type) ? " (undefined)" : "");
3028 }
c906108c 3029 printfi_filtered (spaces, "vptr_basetype ");
d4f3574e 3030 gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
c906108c
SS
3031 puts_filtered ("\n");
3032 if (TYPE_VPTR_BASETYPE (type) != NULL)
3033 {
3034 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3035 }
7ba81444
MS
3036 printfi_filtered (spaces, "vptr_fieldno %d\n",
3037 TYPE_VPTR_FIELDNO (type));
c906108c 3038
b4ba55a1
JB
3039 switch (TYPE_SPECIFIC_FIELD (type))
3040 {
3041 case TYPE_SPECIFIC_CPLUS_STUFF:
3042 printfi_filtered (spaces, "cplus_stuff ");
3043 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
3044 gdb_stdout);
3045 puts_filtered ("\n");
3046 print_cplus_stuff (type, spaces);
3047 break;
8da61cc4 3048
b4ba55a1
JB
3049 case TYPE_SPECIFIC_GNAT_STUFF:
3050 printfi_filtered (spaces, "gnat_stuff ");
3051 gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3052 puts_filtered ("\n");
3053 print_gnat_stuff (type, spaces);
3054 break;
701c159d 3055
b4ba55a1
JB
3056 case TYPE_SPECIFIC_FLOATFORMAT:
3057 printfi_filtered (spaces, "floatformat ");
3058 if (TYPE_FLOATFORMAT (type) == NULL)
3059 puts_filtered ("(null)");
3060 else
3061 {
3062 puts_filtered ("{ ");
3063 if (TYPE_FLOATFORMAT (type)[0] == NULL
3064 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3065 puts_filtered ("(null)");
3066 else
3067 puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3068
3069 puts_filtered (", ");
3070 if (TYPE_FLOATFORMAT (type)[1] == NULL
3071 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3072 puts_filtered ("(null)");
3073 else
3074 puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3075
3076 puts_filtered (" }");
3077 }
3078 puts_filtered ("\n");
3079 break;
c906108c 3080
b4ba55a1
JB
3081 case TYPE_SPECIFIC_CALLING_CONVENTION:
3082 printfi_filtered (spaces, "calling_convention %d\n",
3083 TYPE_CALLING_CONVENTION (type));
3084 break;
c906108c 3085 }
b4ba55a1 3086
c906108c
SS
3087 if (spaces == 0)
3088 obstack_free (&dont_print_type_obstack, NULL);
3089}
3090
ae5a43e0
DJ
3091/* Trivial helpers for the libiberty hash table, for mapping one
3092 type to another. */
3093
3094struct type_pair
3095{
3096 struct type *old, *new;
3097};
3098
3099static hashval_t
3100type_pair_hash (const void *item)
3101{
3102 const struct type_pair *pair = item;
d8734c88 3103
ae5a43e0
DJ
3104 return htab_hash_pointer (pair->old);
3105}
3106
3107static int
3108type_pair_eq (const void *item_lhs, const void *item_rhs)
3109{
3110 const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
d8734c88 3111
ae5a43e0
DJ
3112 return lhs->old == rhs->old;
3113}
3114
3115/* Allocate the hash table used by copy_type_recursive to walk
3116 types without duplicates. We use OBJFILE's obstack, because
3117 OBJFILE is about to be deleted. */
3118
3119htab_t
3120create_copied_types_hash (struct objfile *objfile)
3121{
3122 return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3123 NULL, &objfile->objfile_obstack,
3124 hashtab_obstack_allocate,
3125 dummy_obstack_deallocate);
3126}
3127
7ba81444
MS
3128/* Recursively copy (deep copy) TYPE, if it is associated with
3129 OBJFILE. Return a new type allocated using malloc, a saved type if
3130 we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
3131 not associated with OBJFILE. */
ae5a43e0
DJ
3132
3133struct type *
7ba81444
MS
3134copy_type_recursive (struct objfile *objfile,
3135 struct type *type,
ae5a43e0
DJ
3136 htab_t copied_types)
3137{
3138 struct type_pair *stored, pair;
3139 void **slot;
3140 struct type *new_type;
3141
e9bb382b 3142 if (! TYPE_OBJFILE_OWNED (type))
ae5a43e0
DJ
3143 return type;
3144
7ba81444
MS
3145 /* This type shouldn't be pointing to any types in other objfiles;
3146 if it did, the type might disappear unexpectedly. */
ae5a43e0
DJ
3147 gdb_assert (TYPE_OBJFILE (type) == objfile);
3148
3149 pair.old = type;
3150 slot = htab_find_slot (copied_types, &pair, INSERT);
3151 if (*slot != NULL)
3152 return ((struct type_pair *) *slot)->new;
3153
e9bb382b 3154 new_type = alloc_type_arch (get_type_arch (type));
ae5a43e0
DJ
3155
3156 /* We must add the new type to the hash table immediately, in case
3157 we encounter this type again during a recursive call below. */
d87ecdfb 3158 stored = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
ae5a43e0
DJ
3159 stored->old = type;
3160 stored->new = new_type;
3161 *slot = stored;
3162
876cecd0
TT
3163 /* Copy the common fields of types. For the main type, we simply
3164 copy the entire thing and then update specific fields as needed. */
3165 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
e9bb382b
UW
3166 TYPE_OBJFILE_OWNED (new_type) = 0;
3167 TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
876cecd0 3168
ae5a43e0
DJ
3169 if (TYPE_NAME (type))
3170 TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3171 if (TYPE_TAG_NAME (type))
3172 TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
ae5a43e0
DJ
3173
3174 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3175 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3176
3177 /* Copy the fields. */
ae5a43e0
DJ
3178 if (TYPE_NFIELDS (type))
3179 {
3180 int i, nfields;
3181
3182 nfields = TYPE_NFIELDS (type);
1deafd4e 3183 TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field);
ae5a43e0
DJ
3184 for (i = 0; i < nfields; i++)
3185 {
7ba81444
MS
3186 TYPE_FIELD_ARTIFICIAL (new_type, i) =
3187 TYPE_FIELD_ARTIFICIAL (type, i);
ae5a43e0
DJ
3188 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3189 if (TYPE_FIELD_TYPE (type, i))
3190 TYPE_FIELD_TYPE (new_type, i)
3191 = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3192 copied_types);
3193 if (TYPE_FIELD_NAME (type, i))
7ba81444
MS
3194 TYPE_FIELD_NAME (new_type, i) =
3195 xstrdup (TYPE_FIELD_NAME (type, i));
d6a843b5 3196 switch (TYPE_FIELD_LOC_KIND (type, i))
ae5a43e0 3197 {
d6a843b5
JK
3198 case FIELD_LOC_KIND_BITPOS:
3199 SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
3200 TYPE_FIELD_BITPOS (type, i));
3201 break;
3202 case FIELD_LOC_KIND_PHYSADDR:
3203 SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3204 TYPE_FIELD_STATIC_PHYSADDR (type, i));
3205 break;
3206 case FIELD_LOC_KIND_PHYSNAME:
3207 SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3208 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
3209 i)));
3210 break;
3211 default:
3212 internal_error (__FILE__, __LINE__,
3213 _("Unexpected type field location kind: %d"),
3214 TYPE_FIELD_LOC_KIND (type, i));
ae5a43e0
DJ
3215 }
3216 }
3217 }
3218
43bbcdc2
PH
3219 /* For range types, copy the bounds information. */
3220 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3221 {
3222 TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
3223 *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
3224 }
3225
ae5a43e0
DJ
3226 /* Copy pointers to other types. */
3227 if (TYPE_TARGET_TYPE (type))
7ba81444
MS
3228 TYPE_TARGET_TYPE (new_type) =
3229 copy_type_recursive (objfile,
3230 TYPE_TARGET_TYPE (type),
3231 copied_types);
ae5a43e0 3232 if (TYPE_VPTR_BASETYPE (type))
7ba81444
MS
3233 TYPE_VPTR_BASETYPE (new_type) =
3234 copy_type_recursive (objfile,
3235 TYPE_VPTR_BASETYPE (type),
3236 copied_types);
ae5a43e0
DJ
3237 /* Maybe copy the type_specific bits.
3238
3239 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3240 base classes and methods. There's no fundamental reason why we
3241 can't, but at the moment it is not needed. */
3242
3243 if (TYPE_CODE (type) == TYPE_CODE_FLT)
d5d6fca5 3244 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
ae5a43e0
DJ
3245 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3246 || TYPE_CODE (type) == TYPE_CODE_UNION
ae5a43e0
DJ
3247 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3248 INIT_CPLUS_SPECIFIC (new_type);
3249
3250 return new_type;
3251}
3252
4af88198
JB
3253/* Make a copy of the given TYPE, except that the pointer & reference
3254 types are not preserved.
3255
3256 This function assumes that the given type has an associated objfile.
3257 This objfile is used to allocate the new type. */
3258
3259struct type *
3260copy_type (const struct type *type)
3261{
3262 struct type *new_type;
3263
e9bb382b 3264 gdb_assert (TYPE_OBJFILE_OWNED (type));
4af88198 3265
e9bb382b 3266 new_type = alloc_type_copy (type);
4af88198
JB
3267 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3268 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3269 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
3270 sizeof (struct main_type));
3271
3272 return new_type;
3273}
3274
e9bb382b
UW
3275
3276/* Helper functions to initialize architecture-specific types. */
3277
3278/* Allocate a type structure associated with GDBARCH and set its
3279 CODE, LENGTH, and NAME fields. */
3280struct type *
3281arch_type (struct gdbarch *gdbarch,
3282 enum type_code code, int length, char *name)
3283{
3284 struct type *type;
3285
3286 type = alloc_type_arch (gdbarch);
3287 TYPE_CODE (type) = code;
3288 TYPE_LENGTH (type) = length;
3289
3290 if (name)
3291 TYPE_NAME (type) = xstrdup (name);
3292
3293 return type;
3294}
3295
3296/* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
3297 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3298 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3299struct type *
3300arch_integer_type (struct gdbarch *gdbarch,
3301 int bit, int unsigned_p, char *name)
3302{
3303 struct type *t;
3304
3305 t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
3306 if (unsigned_p)
3307 TYPE_UNSIGNED (t) = 1;
3308 if (name && strcmp (name, "char") == 0)
3309 TYPE_NOSIGN (t) = 1;
3310
3311 return t;
3312}
3313
3314/* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
3315 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3316 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3317struct type *
3318arch_character_type (struct gdbarch *gdbarch,
3319 int bit, int unsigned_p, char *name)
3320{
3321 struct type *t;
3322
3323 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
3324 if (unsigned_p)
3325 TYPE_UNSIGNED (t) = 1;
3326
3327 return t;
3328}
3329
3330/* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
3331 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
3332 the type's TYPE_UNSIGNED flag. NAME is the type name. */
3333struct type *
3334arch_boolean_type (struct gdbarch *gdbarch,
3335 int bit, int unsigned_p, char *name)
3336{
3337 struct type *t;
3338
3339 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
3340 if (unsigned_p)
3341 TYPE_UNSIGNED (t) = 1;
3342
3343 return t;
3344}
3345
3346/* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
3347 BIT is the type size in bits; if BIT equals -1, the size is
3348 determined by the floatformat. NAME is the type name. Set the
3349 TYPE_FLOATFORMAT from FLOATFORMATS. */
27067745 3350struct type *
e9bb382b
UW
3351arch_float_type (struct gdbarch *gdbarch,
3352 int bit, char *name, const struct floatformat **floatformats)
8da61cc4
DJ
3353{
3354 struct type *t;
3355
3356 if (bit == -1)
3357 {
3358 gdb_assert (floatformats != NULL);
3359 gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
3360 bit = floatformats[0]->totalsize;
3361 }
3362 gdb_assert (bit >= 0);
3363
e9bb382b 3364 t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
8da61cc4
DJ
3365 TYPE_FLOATFORMAT (t) = floatformats;
3366 return t;
3367}
3368
e9bb382b
UW
3369/* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
3370 NAME is the type name. TARGET_TYPE is the component float type. */
27067745 3371struct type *
e9bb382b
UW
3372arch_complex_type (struct gdbarch *gdbarch,
3373 char *name, struct type *target_type)
27067745
UW
3374{
3375 struct type *t;
d8734c88 3376
e9bb382b
UW
3377 t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
3378 2 * TYPE_LENGTH (target_type), name);
27067745
UW
3379 TYPE_TARGET_TYPE (t) = target_type;
3380 return t;
3381}
3382
e9bb382b 3383/* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
eb90ce83 3384 NAME is the type name. LENGTH is the size of the flag word in bytes. */
e9bb382b
UW
3385struct type *
3386arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
3387{
3388 int nfields = length * TARGET_CHAR_BIT;
3389 struct type *type;
3390
3391 type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
3392 TYPE_UNSIGNED (type) = 1;
3393 TYPE_NFIELDS (type) = nfields;
3394 TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
3395
3396 return type;
3397}
3398
3399/* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
3400 position BITPOS is called NAME. */
3401void
3402append_flags_type_flag (struct type *type, int bitpos, char *name)
3403{
3404 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
3405 gdb_assert (bitpos < TYPE_NFIELDS (type));
3406 gdb_assert (bitpos >= 0);
3407
3408 if (name)
3409 {
3410 TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
3411 TYPE_FIELD_BITPOS (type, bitpos) = bitpos;
3412 }
3413 else
3414 {
3415 /* Don't show this field to the user. */
3416 TYPE_FIELD_BITPOS (type, bitpos) = -1;
3417 }
3418}
3419
3420/* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
3421 specified by CODE) associated with GDBARCH. NAME is the type name. */
3422struct type *
3423arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
3424{
3425 struct type *t;
d8734c88 3426
e9bb382b
UW
3427 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
3428 t = arch_type (gdbarch, code, 0, NULL);
3429 TYPE_TAG_NAME (t) = name;
3430 INIT_CPLUS_SPECIFIC (t);
3431 return t;
3432}
3433
3434/* Add new field with name NAME and type FIELD to composite type T.
f5dff777
DJ
3435 Do not set the field's position or adjust the type's length;
3436 the caller should do so. Return the new field. */
3437struct field *
3438append_composite_type_field_raw (struct type *t, char *name,
3439 struct type *field)
e9bb382b
UW
3440{
3441 struct field *f;
d8734c88 3442
e9bb382b
UW
3443 TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
3444 TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
3445 sizeof (struct field) * TYPE_NFIELDS (t));
3446 f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
3447 memset (f, 0, sizeof f[0]);
3448 FIELD_TYPE (f[0]) = field;
3449 FIELD_NAME (f[0]) = name;
f5dff777
DJ
3450 return f;
3451}
3452
3453/* Add new field with name NAME and type FIELD to composite type T.
3454 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
3455void
3456append_composite_type_field_aligned (struct type *t, char *name,
3457 struct type *field, int alignment)
3458{
3459 struct field *f = append_composite_type_field_raw (t, name, field);
d8734c88 3460
e9bb382b
UW
3461 if (TYPE_CODE (t) == TYPE_CODE_UNION)
3462 {
3463 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
3464 TYPE_LENGTH (t) = TYPE_LENGTH (field);
3465 }
3466 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
3467 {
3468 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
3469 if (TYPE_NFIELDS (t) > 1)
3470 {
3471 FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1])
3472 + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
3473 * TARGET_CHAR_BIT));
3474
3475 if (alignment)
3476 {
3477 int left = FIELD_BITPOS (f[0]) % (alignment * TARGET_CHAR_BIT);
d8734c88 3478
e9bb382b
UW
3479 if (left)
3480 {
3481 FIELD_BITPOS (f[0]) += left;
3482 TYPE_LENGTH (t) += left / TARGET_CHAR_BIT;
3483 }
3484 }
3485 }
3486 }
3487}
3488
3489/* Add new field with name NAME and type FIELD to composite type T. */
3490void
3491append_composite_type_field (struct type *t, char *name,
3492 struct type *field)
3493{
3494 append_composite_type_field_aligned (t, name, field, 0);
3495}
3496
3497
000177f0
AC
3498static struct gdbarch_data *gdbtypes_data;
3499
3500const struct builtin_type *
3501builtin_type (struct gdbarch *gdbarch)
3502{
3503 return gdbarch_data (gdbarch, gdbtypes_data);
3504}
3505
3506static void *
3507gdbtypes_post_init (struct gdbarch *gdbarch)
3508{
3509 struct builtin_type *builtin_type
3510 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
3511
46bf5051 3512 /* Basic types. */
e9bb382b
UW
3513 builtin_type->builtin_void
3514 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
3515 builtin_type->builtin_char
3516 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3517 !gdbarch_char_signed (gdbarch), "char");
3518 builtin_type->builtin_signed_char
3519 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3520 0, "signed char");
3521 builtin_type->builtin_unsigned_char
3522 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
3523 1, "unsigned char");
3524 builtin_type->builtin_short
3525 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
3526 0, "short");
3527 builtin_type->builtin_unsigned_short
3528 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
3529 1, "unsigned short");
3530 builtin_type->builtin_int
3531 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
3532 0, "int");
3533 builtin_type->builtin_unsigned_int
3534 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
3535 1, "unsigned int");
3536 builtin_type->builtin_long
3537 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
3538 0, "long");
3539 builtin_type->builtin_unsigned_long
3540 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
3541 1, "unsigned long");
3542 builtin_type->builtin_long_long
3543 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
3544 0, "long long");
3545 builtin_type->builtin_unsigned_long_long
3546 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
3547 1, "unsigned long long");
70bd8e24 3548 builtin_type->builtin_float
e9bb382b 3549 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
27067745 3550 "float", gdbarch_float_format (gdbarch));
70bd8e24 3551 builtin_type->builtin_double
e9bb382b 3552 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
27067745 3553 "double", gdbarch_double_format (gdbarch));
70bd8e24 3554 builtin_type->builtin_long_double
e9bb382b 3555 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
27067745 3556 "long double", gdbarch_long_double_format (gdbarch));
70bd8e24 3557 builtin_type->builtin_complex
e9bb382b
UW
3558 = arch_complex_type (gdbarch, "complex",
3559 builtin_type->builtin_float);
70bd8e24 3560 builtin_type->builtin_double_complex
e9bb382b
UW
3561 = arch_complex_type (gdbarch, "double complex",
3562 builtin_type->builtin_double);
3563 builtin_type->builtin_string
3564 = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
3565 builtin_type->builtin_bool
3566 = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
000177f0 3567
7678ef8f
TJB
3568 /* The following three are about decimal floating point types, which
3569 are 32-bits, 64-bits and 128-bits respectively. */
3570 builtin_type->builtin_decfloat
e9bb382b 3571 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
7678ef8f 3572 builtin_type->builtin_decdouble
e9bb382b 3573 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
7678ef8f 3574 builtin_type->builtin_declong
e9bb382b 3575 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
7678ef8f 3576
69feb676 3577 /* "True" character types. */
e9bb382b
UW
3578 builtin_type->builtin_true_char
3579 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
3580 builtin_type->builtin_true_unsigned_char
3581 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
69feb676 3582
df4df182 3583 /* Fixed-size integer types. */
e9bb382b
UW
3584 builtin_type->builtin_int0
3585 = arch_integer_type (gdbarch, 0, 0, "int0_t");
3586 builtin_type->builtin_int8
3587 = arch_integer_type (gdbarch, 8, 0, "int8_t");
3588 builtin_type->builtin_uint8
3589 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
3590 builtin_type->builtin_int16
3591 = arch_integer_type (gdbarch, 16, 0, "int16_t");
3592 builtin_type->builtin_uint16
3593 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
3594 builtin_type->builtin_int32
3595 = arch_integer_type (gdbarch, 32, 0, "int32_t");
3596 builtin_type->builtin_uint32
3597 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
3598 builtin_type->builtin_int64
3599 = arch_integer_type (gdbarch, 64, 0, "int64_t");
3600 builtin_type->builtin_uint64
3601 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
3602 builtin_type->builtin_int128
3603 = arch_integer_type (gdbarch, 128, 0, "int128_t");
3604 builtin_type->builtin_uint128
3605 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
2844d6b5
KW
3606 TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
3607 TYPE_INSTANCE_FLAG_NOTTEXT;
3608 TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
3609 TYPE_INSTANCE_FLAG_NOTTEXT;
df4df182 3610
9a22f0d0
PM
3611 /* Wide character types. */
3612 builtin_type->builtin_char16
3613 = arch_integer_type (gdbarch, 16, 0, "char16_t");
3614 builtin_type->builtin_char32
3615 = arch_integer_type (gdbarch, 32, 0, "char32_t");
3616
3617
46bf5051 3618 /* Default data/code pointer types. */
e9bb382b
UW
3619 builtin_type->builtin_data_ptr
3620 = lookup_pointer_type (builtin_type->builtin_void);
3621 builtin_type->builtin_func_ptr
3622 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
46bf5051 3623
78267919 3624 /* This type represents a GDB internal function. */
e9bb382b
UW
3625 builtin_type->internal_fn
3626 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
3627 "<internal function>");
78267919 3628
46bf5051
UW
3629 return builtin_type;
3630}
3631
3632
3633/* This set of objfile-based types is intended to be used by symbol
3634 readers as basic types. */
3635
3636static const struct objfile_data *objfile_type_data;
3637
3638const struct objfile_type *
3639objfile_type (struct objfile *objfile)
3640{
3641 struct gdbarch *gdbarch;
3642 struct objfile_type *objfile_type
3643 = objfile_data (objfile, objfile_type_data);
3644
3645 if (objfile_type)
3646 return objfile_type;
3647
3648 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
3649 1, struct objfile_type);
3650
3651 /* Use the objfile architecture to determine basic type properties. */
3652 gdbarch = get_objfile_arch (objfile);
3653
3654 /* Basic types. */
3655 objfile_type->builtin_void
3656 = init_type (TYPE_CODE_VOID, 1,
3657 0,
3658 "void", objfile);
3659
3660 objfile_type->builtin_char
3661 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3662 (TYPE_FLAG_NOSIGN
3663 | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
3664 "char", objfile);
3665 objfile_type->builtin_signed_char
3666 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3667 0,
3668 "signed char", objfile);
3669 objfile_type->builtin_unsigned_char
3670 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3671 TYPE_FLAG_UNSIGNED,
3672 "unsigned char", objfile);
3673 objfile_type->builtin_short
3674 = init_type (TYPE_CODE_INT,
3675 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
3676 0, "short", objfile);
3677 objfile_type->builtin_unsigned_short
3678 = init_type (TYPE_CODE_INT,
3679 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
3680 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
3681 objfile_type->builtin_int
3682 = init_type (TYPE_CODE_INT,
3683 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
3684 0, "int", objfile);
3685 objfile_type->builtin_unsigned_int
3686 = init_type (TYPE_CODE_INT,
3687 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
3688 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
3689 objfile_type->builtin_long
3690 = init_type (TYPE_CODE_INT,
3691 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
3692 0, "long", objfile);
3693 objfile_type->builtin_unsigned_long
3694 = init_type (TYPE_CODE_INT,
3695 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
3696 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
3697 objfile_type->builtin_long_long
3698 = init_type (TYPE_CODE_INT,
3699 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
3700 0, "long long", objfile);
3701 objfile_type->builtin_unsigned_long_long
3702 = init_type (TYPE_CODE_INT,
3703 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
3704 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
3705
3706 objfile_type->builtin_float
3707 = init_type (TYPE_CODE_FLT,
3708 gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
3709 0, "float", objfile);
3710 TYPE_FLOATFORMAT (objfile_type->builtin_float)
3711 = gdbarch_float_format (gdbarch);
3712 objfile_type->builtin_double
3713 = init_type (TYPE_CODE_FLT,
3714 gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
3715 0, "double", objfile);
3716 TYPE_FLOATFORMAT (objfile_type->builtin_double)
3717 = gdbarch_double_format (gdbarch);
3718 objfile_type->builtin_long_double
3719 = init_type (TYPE_CODE_FLT,
3720 gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
3721 0, "long double", objfile);
3722 TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
3723 = gdbarch_long_double_format (gdbarch);
3724
3725 /* This type represents a type that was unrecognized in symbol read-in. */
3726 objfile_type->builtin_error
3727 = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
3728
3729 /* The following set of types is used for symbols with no
3730 debug information. */
3731 objfile_type->nodebug_text_symbol
3732 = init_type (TYPE_CODE_FUNC, 1, 0,
3733 "<text variable, no debug info>", objfile);
3734 TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
3735 = objfile_type->builtin_int;
3736 objfile_type->nodebug_data_symbol
3737 = init_type (TYPE_CODE_INT,
3738 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3739 "<data variable, no debug info>", objfile);
3740 objfile_type->nodebug_unknown_symbol
3741 = init_type (TYPE_CODE_INT, 1, 0,
3742 "<variable (not text or data), no debug info>", objfile);
3743 objfile_type->nodebug_tls_symbol
3744 = init_type (TYPE_CODE_INT,
3745 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
3746 "<thread local variable, no debug info>", objfile);
000177f0
AC
3747
3748 /* NOTE: on some targets, addresses and pointers are not necessarily
3749 the same --- for example, on the D10V, pointers are 16 bits long,
3750 but addresses are 32 bits long. See doc/gdbint.texinfo,
3751 ``Pointers Are Not Always Addresses''.
3752
3753 The upshot is:
3754 - gdb's `struct type' always describes the target's
3755 representation.
3756 - gdb's `struct value' objects should always hold values in
3757 target form.
3758 - gdb's CORE_ADDR values are addresses in the unified virtual
3759 address space that the assembler and linker work with. Thus,
3760 since target_read_memory takes a CORE_ADDR as an argument, it
3761 can access any memory on the target, even if the processor has
3762 separate code and data address spaces.
3763
3764 So, for example:
3765 - If v is a value holding a D10V code pointer, its contents are
3766 in target form: a big-endian address left-shifted two bits.
3767 - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3768 sizeof (void *) == 2 on the target.
3769
46bf5051
UW
3770 In this context, objfile_type->builtin_core_addr is a bit odd:
3771 it's a target type for a value the target will never see. It's
3772 only used to hold the values of (typeless) linker symbols, which
3773 are indeed in the unified virtual address space. */
000177f0 3774
46bf5051
UW
3775 objfile_type->builtin_core_addr
3776 = init_type (TYPE_CODE_INT,
3777 gdbarch_addr_bit (gdbarch) / 8,
3778 TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
64c50499 3779
46bf5051
UW
3780 set_objfile_data (objfile, objfile_type_data, objfile_type);
3781 return objfile_type;
000177f0
AC
3782}
3783
46bf5051 3784
a14ed312 3785extern void _initialize_gdbtypes (void);
c906108c 3786void
fba45db2 3787_initialize_gdbtypes (void)
c906108c 3788{
5674de60 3789 gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
46bf5051 3790 objfile_type_data = register_objfile_data ();
5674de60 3791
85c07804
AC
3792 add_setshow_zinteger_cmd ("overload", no_class, &overload_debug, _("\
3793Set debugging of C++ overloading."), _("\
3794Show debugging of C++ overloading."), _("\
3795When enabled, ranking of the functions is displayed."),
3796 NULL,
920d2a44 3797 show_overload_debug,
85c07804 3798 &setdebuglist, &showdebuglist);
5674de60 3799
7ba81444 3800 /* Add user knob for controlling resolution of opaque types. */
5674de60
UW
3801 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3802 &opaque_type_resolution, _("\
3803Set resolution of opaque struct/class/union types (if set before loading symbols)."), _("\
3804Show resolution of opaque struct/class/union types (if set before loading symbols)."), NULL,
3805 NULL,
3806 show_opaque_type_resolution,
3807 &setlist, &showlist);
c906108c 3808}
This page took 1.469605 seconds and 4 git commands to generate.