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