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