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