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