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