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