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