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