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