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