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