daily update
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
CommitLineData
c906108c 1/* Support routines for manipulating internal types for GDB.
4f2aea11 2
ecd75fc8 3 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4f2aea11 4
c906108c
SS
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
c906108c
SS
23#include "bfd.h"
24#include "symtab.h"
25#include "symfile.h"
26#include "objfiles.h"
27#include "gdbtypes.h"
28#include "expression.h"
29#include "language.h"
30#include "target.h"
31#include "value.h"
32#include "demangle.h"
33#include "complaints.h"
34#include "gdbcmd.h"
015a42b4 35#include "cp-abi.h"
ae5a43e0 36#include "hashtab.h"
8e7b59a5 37#include "exceptions.h"
8de20a37 38#include "cp-support.h"
ca092b61
DE
39#include "bcache.h"
40#include "dwarf2loc.h"
80180f79 41#include "gdbcore.h"
ac3aafc7 42
6403aeea
SW
43/* Initialize BADNESS constants. */
44
a9d5ef47 45const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
6403aeea 46
a9d5ef47
SW
47const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
48const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
6403aeea 49
a9d5ef47 50const struct rank EXACT_MATCH_BADNESS = {0,0};
6403aeea 51
a9d5ef47
SW
52const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
53const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
54const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
55const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
56const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
57const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
58const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
5b4f6e25 59const struct rank BOOL_CONVERSION_BADNESS = {3,0};
a9d5ef47
SW
60const struct rank BASE_CONVERSION_BADNESS = {2,0};
61const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
da096638 62const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
a9d5ef47 63const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
a451cb65 64const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
6403aeea 65
8da61cc4 66/* Floatformat pairs. */
f9e9243a
UW
67const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
68 &floatformat_ieee_half_big,
69 &floatformat_ieee_half_little
70};
8da61cc4
DJ
71const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
72 &floatformat_ieee_single_big,
73 &floatformat_ieee_single_little
74};
75const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
76 &floatformat_ieee_double_big,
77 &floatformat_ieee_double_little
78};
79const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
80 &floatformat_ieee_double_big,
81 &floatformat_ieee_double_littlebyte_bigword
82};
83const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
84 &floatformat_i387_ext,
85 &floatformat_i387_ext
86};
87const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
88 &floatformat_m68881_ext,
89 &floatformat_m68881_ext
90};
91const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
92 &floatformat_arm_ext_big,
93 &floatformat_arm_ext_littlebyte_bigword
94};
95const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
96 &floatformat_ia64_spill_big,
97 &floatformat_ia64_spill_little
98};
99const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
100 &floatformat_ia64_quad_big,
101 &floatformat_ia64_quad_little
102};
103const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
104 &floatformat_vax_f,
105 &floatformat_vax_f
106};
107const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
108 &floatformat_vax_d,
109 &floatformat_vax_d
110};
b14d30e1 111const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
f5aee5ee
AM
112 &floatformat_ibm_long_double_big,
113 &floatformat_ibm_long_double_little
b14d30e1 114};
8da61cc4 115
2873700e
KS
116/* Should opaque types be resolved? */
117
118static int opaque_type_resolution = 1;
119
120/* A flag to enable printing of debugging information of C++
121 overloading. */
122
123unsigned int overload_debug = 0;
124
a451cb65
KS
125/* A flag to enable strict type checking. */
126
127static int strict_type_checking = 1;
128
2873700e 129/* A function to show whether opaque types are resolved. */
5212577a 130
920d2a44
AC
131static void
132show_opaque_type_resolution (struct ui_file *file, int from_tty,
7ba81444
MS
133 struct cmd_list_element *c,
134 const char *value)
920d2a44 135{
3e43a32a
MS
136 fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
137 "(if set before loading symbols) is %s.\n"),
920d2a44
AC
138 value);
139}
140
2873700e 141/* A function to show whether C++ overload debugging is enabled. */
5212577a 142
920d2a44
AC
143static void
144show_overload_debug (struct ui_file *file, int from_tty,
145 struct cmd_list_element *c, const char *value)
146{
7ba81444
MS
147 fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"),
148 value);
920d2a44 149}
c906108c 150
a451cb65
KS
151/* A function to show the status of strict type checking. */
152
153static void
154show_strict_type_checking (struct ui_file *file, int from_tty,
155 struct cmd_list_element *c, const char *value)
156{
157 fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
158}
159
5212577a 160\f
e9bb382b
UW
161/* Allocate a new OBJFILE-associated type structure and fill it
162 with some defaults. Space for the type structure is allocated
163 on the objfile's objfile_obstack. */
c906108c
SS
164
165struct type *
fba45db2 166alloc_type (struct objfile *objfile)
c906108c 167{
52f0bd74 168 struct type *type;
c906108c 169
e9bb382b
UW
170 gdb_assert (objfile != NULL);
171
7ba81444 172 /* Alloc the structure and start off with all fields zeroed. */
e9bb382b
UW
173 type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
174 TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
175 struct main_type);
176 OBJSTAT (objfile, n_types++);
c906108c 177
e9bb382b
UW
178 TYPE_OBJFILE_OWNED (type) = 1;
179 TYPE_OWNER (type).objfile = objfile;
c906108c 180
7ba81444 181 /* Initialize the fields that might not be zero. */
c906108c
SS
182
183 TYPE_CODE (type) = TYPE_CODE_UNDEF;
c906108c 184 TYPE_VPTR_FIELDNO (type) = -1;
2fdde8f8 185 TYPE_CHAIN (type) = type; /* Chain back to itself. */
c906108c 186
c16abbde 187 return type;
c906108c
SS
188}
189
e9bb382b
UW
190/* Allocate a new GDBARCH-associated type structure and fill it
191 with some defaults. Space for the type structure is allocated
192 on the heap. */
193
194struct type *
195alloc_type_arch (struct gdbarch *gdbarch)
196{
197 struct type *type;
198
199 gdb_assert (gdbarch != NULL);
200
201 /* Alloc the structure and start off with all fields zeroed. */
202
41bf6aca
TT
203 type = XCNEW (struct type);
204 TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
e9bb382b
UW
205
206 TYPE_OBJFILE_OWNED (type) = 0;
207 TYPE_OWNER (type).gdbarch = gdbarch;
208
209 /* Initialize the fields that might not be zero. */
210
211 TYPE_CODE (type) = TYPE_CODE_UNDEF;
212 TYPE_VPTR_FIELDNO (type) = -1;
213 TYPE_CHAIN (type) = type; /* Chain back to itself. */
214
215 return type;
216}
217
218/* If TYPE is objfile-associated, allocate a new type structure
219 associated with the same objfile. If TYPE is gdbarch-associated,
220 allocate a new type structure associated with the same gdbarch. */
221
222struct type *
223alloc_type_copy (const struct type *type)
224{
225 if (TYPE_OBJFILE_OWNED (type))
226 return alloc_type (TYPE_OWNER (type).objfile);
227 else
228 return alloc_type_arch (TYPE_OWNER (type).gdbarch);
229}
230
231/* If TYPE is gdbarch-associated, return that architecture.
232 If TYPE is objfile-associated, return that objfile's architecture. */
233
234struct gdbarch *
235get_type_arch (const struct type *type)
236{
237 if (TYPE_OBJFILE_OWNED (type))
238 return get_objfile_arch (TYPE_OWNER (type).objfile);
239 else
240 return TYPE_OWNER (type).gdbarch;
241}
242
99ad9427
YQ
243/* See gdbtypes.h. */
244
245struct type *
246get_target_type (struct type *type)
247{
248 if (type != NULL)
249 {
250 type = TYPE_TARGET_TYPE (type);
251 if (type != NULL)
252 type = check_typedef (type);
253 }
254
255 return type;
256}
257
2fdde8f8
DJ
258/* Alloc a new type instance structure, fill it with some defaults,
259 and point it at OLDTYPE. Allocate the new type instance from the
260 same place as OLDTYPE. */
261
262static struct type *
263alloc_type_instance (struct type *oldtype)
264{
265 struct type *type;
266
267 /* Allocate the structure. */
268
e9bb382b 269 if (! TYPE_OBJFILE_OWNED (oldtype))
41bf6aca 270 type = XCNEW (struct type);
2fdde8f8 271 else
1deafd4e
PA
272 type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
273 struct type);
274
2fdde8f8
DJ
275 TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
276
277 TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
278
c16abbde 279 return type;
2fdde8f8
DJ
280}
281
282/* Clear all remnants of the previous type at TYPE, in preparation for
e9bb382b 283 replacing it with something else. Preserve owner information. */
5212577a 284
2fdde8f8
DJ
285static void
286smash_type (struct type *type)
287{
e9bb382b
UW
288 int objfile_owned = TYPE_OBJFILE_OWNED (type);
289 union type_owner owner = TYPE_OWNER (type);
290
2fdde8f8
DJ
291 memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
292
e9bb382b
UW
293 /* Restore owner information. */
294 TYPE_OBJFILE_OWNED (type) = objfile_owned;
295 TYPE_OWNER (type) = owner;
296
2fdde8f8
DJ
297 /* For now, delete the rings. */
298 TYPE_CHAIN (type) = type;
299
300 /* For now, leave the pointer/reference types alone. */
301}
302
c906108c
SS
303/* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
304 to a pointer to memory where the pointer type should be stored.
305 If *TYPEPTR is zero, update it to point to the pointer type we return.
306 We allocate new memory if needed. */
307
308struct type *
fba45db2 309make_pointer_type (struct type *type, struct type **typeptr)
c906108c 310{
52f0bd74 311 struct type *ntype; /* New type */
053cb41b 312 struct type *chain;
c906108c
SS
313
314 ntype = TYPE_POINTER_TYPE (type);
315
c5aa993b 316 if (ntype)
c906108c 317 {
c5aa993b 318 if (typeptr == 0)
7ba81444
MS
319 return ntype; /* Don't care about alloc,
320 and have new type. */
c906108c 321 else if (*typeptr == 0)
c5aa993b 322 {
7ba81444 323 *typeptr = ntype; /* Tracking alloc, and have new type. */
c906108c 324 return ntype;
c5aa993b 325 }
c906108c
SS
326 }
327
328 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
329 {
e9bb382b 330 ntype = alloc_type_copy (type);
c906108c
SS
331 if (typeptr)
332 *typeptr = ntype;
333 }
7ba81444 334 else /* We have storage, but need to reset it. */
c906108c
SS
335 {
336 ntype = *typeptr;
053cb41b 337 chain = TYPE_CHAIN (ntype);
2fdde8f8 338 smash_type (ntype);
053cb41b 339 TYPE_CHAIN (ntype) = chain;
c906108c
SS
340 }
341
342 TYPE_TARGET_TYPE (ntype) = type;
343 TYPE_POINTER_TYPE (type) = ntype;
344
5212577a 345 /* FIXME! Assumes the machine has only one representation for pointers! */
c906108c 346
50810684
UW
347 TYPE_LENGTH (ntype)
348 = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
c906108c
SS
349 TYPE_CODE (ntype) = TYPE_CODE_PTR;
350
67b2adb2 351 /* Mark pointers as unsigned. The target converts between pointers
76e71323 352 and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
7ba81444 353 gdbarch_address_to_pointer. */
876cecd0 354 TYPE_UNSIGNED (ntype) = 1;
c5aa993b 355
053cb41b
JB
356 /* Update the length of all the other variants of this type. */
357 chain = TYPE_CHAIN (ntype);
358 while (chain != ntype)
359 {
360 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
361 chain = TYPE_CHAIN (chain);
362 }
363
c906108c
SS
364 return ntype;
365}
366
367/* Given a type TYPE, return a type of pointers to that type.
368 May need to construct such a type if this is the first use. */
369
370struct type *
fba45db2 371lookup_pointer_type (struct type *type)
c906108c 372{
c5aa993b 373 return make_pointer_type (type, (struct type **) 0);
c906108c
SS
374}
375
7ba81444
MS
376/* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
377 points to a pointer to memory where the reference type should be
378 stored. If *TYPEPTR is zero, update it to point to the reference
379 type we return. We allocate new memory if needed. */
c906108c
SS
380
381struct type *
fba45db2 382make_reference_type (struct type *type, struct type **typeptr)
c906108c 383{
52f0bd74 384 struct type *ntype; /* New type */
1e98b326 385 struct type *chain;
c906108c
SS
386
387 ntype = TYPE_REFERENCE_TYPE (type);
388
c5aa993b 389 if (ntype)
c906108c 390 {
c5aa993b 391 if (typeptr == 0)
7ba81444
MS
392 return ntype; /* Don't care about alloc,
393 and have new type. */
c906108c 394 else if (*typeptr == 0)
c5aa993b 395 {
7ba81444 396 *typeptr = ntype; /* Tracking alloc, and have new type. */
c906108c 397 return ntype;
c5aa993b 398 }
c906108c
SS
399 }
400
401 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
402 {
e9bb382b 403 ntype = alloc_type_copy (type);
c906108c
SS
404 if (typeptr)
405 *typeptr = ntype;
406 }
7ba81444 407 else /* We have storage, but need to reset it. */
c906108c
SS
408 {
409 ntype = *typeptr;
1e98b326 410 chain = TYPE_CHAIN (ntype);
2fdde8f8 411 smash_type (ntype);
1e98b326 412 TYPE_CHAIN (ntype) = chain;
c906108c
SS
413 }
414
415 TYPE_TARGET_TYPE (ntype) = type;
416 TYPE_REFERENCE_TYPE (type) = ntype;
417
7ba81444
MS
418 /* FIXME! Assume the machine has only one representation for
419 references, and that it matches the (only) representation for
420 pointers! */
c906108c 421
50810684
UW
422 TYPE_LENGTH (ntype) =
423 gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
c906108c 424 TYPE_CODE (ntype) = TYPE_CODE_REF;
c5aa993b 425
c906108c
SS
426 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
427 TYPE_REFERENCE_TYPE (type) = ntype;
428
1e98b326
JB
429 /* Update the length of all the other variants of this type. */
430 chain = TYPE_CHAIN (ntype);
431 while (chain != ntype)
432 {
433 TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
434 chain = TYPE_CHAIN (chain);
435 }
436
c906108c
SS
437 return ntype;
438}
439
7ba81444
MS
440/* Same as above, but caller doesn't care about memory allocation
441 details. */
c906108c
SS
442
443struct type *
fba45db2 444lookup_reference_type (struct type *type)
c906108c 445{
c5aa993b 446 return make_reference_type (type, (struct type **) 0);
c906108c
SS
447}
448
7ba81444
MS
449/* Lookup a function type that returns type TYPE. TYPEPTR, if
450 nonzero, points to a pointer to memory where the function type
451 should be stored. If *TYPEPTR is zero, update it to point to the
0c8b41f1 452 function type we return. We allocate new memory if needed. */
c906108c
SS
453
454struct type *
0c8b41f1 455make_function_type (struct type *type, struct type **typeptr)
c906108c 456{
52f0bd74 457 struct type *ntype; /* New type */
c906108c
SS
458
459 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
460 {
e9bb382b 461 ntype = alloc_type_copy (type);
c906108c
SS
462 if (typeptr)
463 *typeptr = ntype;
464 }
7ba81444 465 else /* We have storage, but need to reset it. */
c906108c
SS
466 {
467 ntype = *typeptr;
2fdde8f8 468 smash_type (ntype);
c906108c
SS
469 }
470
471 TYPE_TARGET_TYPE (ntype) = type;
472
473 TYPE_LENGTH (ntype) = 1;
474 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
c5aa993b 475
b6cdc2c1
JK
476 INIT_FUNC_SPECIFIC (ntype);
477
c906108c
SS
478 return ntype;
479}
480
c906108c
SS
481/* Given a type TYPE, return a type of functions that return that type.
482 May need to construct such a type if this is the first use. */
483
484struct type *
fba45db2 485lookup_function_type (struct type *type)
c906108c 486{
0c8b41f1 487 return make_function_type (type, (struct type **) 0);
c906108c
SS
488}
489
71918a86 490/* Given a type TYPE and argument types, return the appropriate
a6fb9c08
TT
491 function type. If the final type in PARAM_TYPES is NULL, make a
492 varargs function. */
71918a86
TT
493
494struct type *
495lookup_function_type_with_arguments (struct type *type,
496 int nparams,
497 struct type **param_types)
498{
499 struct type *fn = make_function_type (type, (struct type **) 0);
500 int i;
501
e314d629 502 if (nparams > 0)
a6fb9c08 503 {
e314d629
TT
504 if (param_types[nparams - 1] == NULL)
505 {
506 --nparams;
507 TYPE_VARARGS (fn) = 1;
508 }
509 else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
510 == TYPE_CODE_VOID)
511 {
512 --nparams;
513 /* Caller should have ensured this. */
514 gdb_assert (nparams == 0);
515 TYPE_PROTOTYPED (fn) = 1;
516 }
a6fb9c08
TT
517 }
518
71918a86
TT
519 TYPE_NFIELDS (fn) = nparams;
520 TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
521 for (i = 0; i < nparams; ++i)
522 TYPE_FIELD_TYPE (fn, i) = param_types[i];
523
524 return fn;
525}
526
47663de5
MS
527/* Identify address space identifier by name --
528 return the integer flag defined in gdbtypes.h. */
5212577a
DE
529
530int
50810684 531address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
47663de5 532{
8b2dbe47 533 int type_flags;
d8734c88 534
7ba81444 535 /* Check for known address space delimiters. */
47663de5 536 if (!strcmp (space_identifier, "code"))
876cecd0 537 return TYPE_INSTANCE_FLAG_CODE_SPACE;
47663de5 538 else if (!strcmp (space_identifier, "data"))
876cecd0 539 return TYPE_INSTANCE_FLAG_DATA_SPACE;
5f11f355
AC
540 else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
541 && gdbarch_address_class_name_to_type_flags (gdbarch,
542 space_identifier,
543 &type_flags))
8b2dbe47 544 return type_flags;
47663de5 545 else
8a3fe4f8 546 error (_("Unknown address space specifier: \"%s\""), space_identifier);
47663de5
MS
547}
548
549/* Identify address space identifier by integer flag as defined in
7ba81444 550 gdbtypes.h -- return the string version of the adress space name. */
47663de5 551
321432c0 552const char *
50810684 553address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
47663de5 554{
876cecd0 555 if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
47663de5 556 return "code";
876cecd0 557 else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
47663de5 558 return "data";
876cecd0 559 else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5f11f355
AC
560 && gdbarch_address_class_type_flags_to_name_p (gdbarch))
561 return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
47663de5
MS
562 else
563 return NULL;
564}
565
2fdde8f8 566/* Create a new type with instance flags NEW_FLAGS, based on TYPE.
ad766c0a
JB
567
568 If STORAGE is non-NULL, create the new type instance there.
569 STORAGE must be in the same obstack as TYPE. */
47663de5 570
b9362cc7 571static struct type *
2fdde8f8
DJ
572make_qualified_type (struct type *type, int new_flags,
573 struct type *storage)
47663de5
MS
574{
575 struct type *ntype;
576
577 ntype = type;
5f61c20e
JK
578 do
579 {
580 if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
581 return ntype;
582 ntype = TYPE_CHAIN (ntype);
583 }
584 while (ntype != type);
47663de5 585
2fdde8f8
DJ
586 /* Create a new type instance. */
587 if (storage == NULL)
588 ntype = alloc_type_instance (type);
589 else
590 {
7ba81444
MS
591 /* If STORAGE was provided, it had better be in the same objfile
592 as TYPE. Otherwise, we can't link it into TYPE's cv chain:
593 if one objfile is freed and the other kept, we'd have
594 dangling pointers. */
ad766c0a
JB
595 gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
596
2fdde8f8
DJ
597 ntype = storage;
598 TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
599 TYPE_CHAIN (ntype) = ntype;
600 }
47663de5
MS
601
602 /* Pointers or references to the original type are not relevant to
2fdde8f8 603 the new type. */
47663de5
MS
604 TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
605 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
47663de5 606
2fdde8f8
DJ
607 /* Chain the new qualified type to the old type. */
608 TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
609 TYPE_CHAIN (type) = ntype;
610
611 /* Now set the instance flags and return the new type. */
612 TYPE_INSTANCE_FLAGS (ntype) = new_flags;
47663de5 613
ab5d3da6
KB
614 /* Set length of new type to that of the original type. */
615 TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
616
47663de5
MS
617 return ntype;
618}
619
2fdde8f8
DJ
620/* Make an address-space-delimited variant of a type -- a type that
621 is identical to the one supplied except that it has an address
622 space attribute attached to it (such as "code" or "data").
623
7ba81444
MS
624 The space attributes "code" and "data" are for Harvard
625 architectures. The address space attributes are for architectures
626 which have alternately sized pointers or pointers with alternate
627 representations. */
2fdde8f8
DJ
628
629struct type *
630make_type_with_address_space (struct type *type, int space_flag)
631{
2fdde8f8 632 int new_flags = ((TYPE_INSTANCE_FLAGS (type)
876cecd0
TT
633 & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
634 | TYPE_INSTANCE_FLAG_DATA_SPACE
635 | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
2fdde8f8
DJ
636 | space_flag);
637
638 return make_qualified_type (type, new_flags, NULL);
639}
c906108c
SS
640
641/* Make a "c-v" variant of a type -- a type that is identical to the
642 one supplied except that it may have const or volatile attributes
643 CNST is a flag for setting the const attribute
644 VOLTL is a flag for setting the volatile attribute
645 TYPE is the base type whose variant we are creating.
c906108c 646
ad766c0a
JB
647 If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
648 storage to hold the new qualified type; *TYPEPTR and TYPE must be
649 in the same objfile. Otherwise, allocate fresh memory for the new
650 type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
651 new type we construct. */
5212577a 652
c906108c 653struct type *
7ba81444
MS
654make_cv_type (int cnst, int voltl,
655 struct type *type,
656 struct type **typeptr)
c906108c 657{
52f0bd74 658 struct type *ntype; /* New type */
c906108c 659
2fdde8f8 660 int new_flags = (TYPE_INSTANCE_FLAGS (type)
308d96ed
MS
661 & ~(TYPE_INSTANCE_FLAG_CONST
662 | TYPE_INSTANCE_FLAG_VOLATILE));
c906108c 663
c906108c 664 if (cnst)
876cecd0 665 new_flags |= TYPE_INSTANCE_FLAG_CONST;
c906108c
SS
666
667 if (voltl)
876cecd0 668 new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
a02fd225 669
2fdde8f8 670 if (typeptr && *typeptr != NULL)
a02fd225 671 {
ad766c0a
JB
672 /* TYPE and *TYPEPTR must be in the same objfile. We can't have
673 a C-V variant chain that threads across objfiles: if one
674 objfile gets freed, then the other has a broken C-V chain.
675
676 This code used to try to copy over the main type from TYPE to
677 *TYPEPTR if they were in different objfiles, but that's
678 wrong, too: TYPE may have a field list or member function
679 lists, which refer to types of their own, etc. etc. The
680 whole shebang would need to be copied over recursively; you
681 can't have inter-objfile pointers. The only thing to do is
682 to leave stub types as stub types, and look them up afresh by
683 name each time you encounter them. */
684 gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
2fdde8f8
DJ
685 }
686
7ba81444
MS
687 ntype = make_qualified_type (type, new_flags,
688 typeptr ? *typeptr : NULL);
c906108c 689
2fdde8f8
DJ
690 if (typeptr != NULL)
691 *typeptr = ntype;
a02fd225 692
2fdde8f8 693 return ntype;
a02fd225 694}
c906108c 695
06d66ee9
TT
696/* Make a 'restrict'-qualified version of TYPE. */
697
698struct type *
699make_restrict_type (struct type *type)
700{
701 return make_qualified_type (type,
702 (TYPE_INSTANCE_FLAGS (type)
703 | TYPE_INSTANCE_FLAG_RESTRICT),
704 NULL);
705}
706
2fdde8f8
DJ
707/* Replace the contents of ntype with the type *type. This changes the
708 contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
709 the changes are propogated to all types in the TYPE_CHAIN.
dd6bda65 710
cda6c68a
JB
711 In order to build recursive types, it's inevitable that we'll need
712 to update types in place --- but this sort of indiscriminate
713 smashing is ugly, and needs to be replaced with something more
2fdde8f8
DJ
714 controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
715 clear if more steps are needed. */
5212577a 716
dd6bda65
DJ
717void
718replace_type (struct type *ntype, struct type *type)
719{
ab5d3da6 720 struct type *chain;
dd6bda65 721
ad766c0a
JB
722 /* These two types had better be in the same objfile. Otherwise,
723 the assignment of one type's main type structure to the other
724 will produce a type with references to objects (names; field
725 lists; etc.) allocated on an objfile other than its own. */
726 gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
727
2fdde8f8 728 *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
dd6bda65 729
7ba81444
MS
730 /* The type length is not a part of the main type. Update it for
731 each type on the variant chain. */
ab5d3da6 732 chain = ntype;
5f61c20e
JK
733 do
734 {
735 /* Assert that this element of the chain has no address-class bits
736 set in its flags. Such type variants might have type lengths
737 which are supposed to be different from the non-address-class
738 variants. This assertion shouldn't ever be triggered because
739 symbol readers which do construct address-class variants don't
740 call replace_type(). */
741 gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
742
743 TYPE_LENGTH (chain) = TYPE_LENGTH (type);
744 chain = TYPE_CHAIN (chain);
745 }
746 while (ntype != chain);
ab5d3da6 747
2fdde8f8
DJ
748 /* Assert that the two types have equivalent instance qualifiers.
749 This should be true for at least all of our debug readers. */
750 gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
dd6bda65
DJ
751}
752
c906108c
SS
753/* Implement direct support for MEMBER_TYPE in GNU C++.
754 May need to construct such a type if this is the first use.
755 The TYPE is the type of the member. The DOMAIN is the type
756 of the aggregate that the member belongs to. */
757
758struct type *
0d5de010 759lookup_memberptr_type (struct type *type, struct type *domain)
c906108c 760{
52f0bd74 761 struct type *mtype;
c906108c 762
e9bb382b 763 mtype = alloc_type_copy (type);
0d5de010 764 smash_to_memberptr_type (mtype, domain, type);
c16abbde 765 return mtype;
c906108c
SS
766}
767
0d5de010
DJ
768/* Return a pointer-to-method type, for a method of type TO_TYPE. */
769
770struct type *
771lookup_methodptr_type (struct type *to_type)
772{
773 struct type *mtype;
774
e9bb382b 775 mtype = alloc_type_copy (to_type);
0b92b5bb 776 smash_to_methodptr_type (mtype, to_type);
0d5de010
DJ
777 return mtype;
778}
779
7ba81444
MS
780/* Allocate a stub method whose return type is TYPE. This apparently
781 happens for speed of symbol reading, since parsing out the
782 arguments to the method is cpu-intensive, the way we are doing it.
783 So, we will fill in arguments later. This always returns a fresh
784 type. */
c906108c
SS
785
786struct type *
fba45db2 787allocate_stub_method (struct type *type)
c906108c
SS
788{
789 struct type *mtype;
790
e9bb382b
UW
791 mtype = alloc_type_copy (type);
792 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
793 TYPE_LENGTH (mtype) = 1;
794 TYPE_STUB (mtype) = 1;
c906108c
SS
795 TYPE_TARGET_TYPE (mtype) = type;
796 /* _DOMAIN_TYPE (mtype) = unknown yet */
c16abbde 797 return mtype;
c906108c
SS
798}
799
729efb13
SA
800/* Create a range type with a dynamic range from LOW_BOUND to
801 HIGH_BOUND, inclusive. See create_range_type for further details. */
c906108c
SS
802
803struct type *
729efb13
SA
804create_range_type (struct type *result_type, struct type *index_type,
805 const struct dynamic_prop *low_bound,
806 const struct dynamic_prop *high_bound)
c906108c
SS
807{
808 if (result_type == NULL)
e9bb382b 809 result_type = alloc_type_copy (index_type);
c906108c
SS
810 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
811 TYPE_TARGET_TYPE (result_type) = index_type;
74a9bb82 812 if (TYPE_STUB (index_type))
876cecd0 813 TYPE_TARGET_STUB (result_type) = 1;
c906108c
SS
814 else
815 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
729efb13 816
43bbcdc2
PH
817 TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
818 TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
729efb13
SA
819 TYPE_RANGE_DATA (result_type)->low = *low_bound;
820 TYPE_RANGE_DATA (result_type)->high = *high_bound;
c906108c 821
729efb13 822 if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
876cecd0 823 TYPE_UNSIGNED (result_type) = 1;
c906108c 824
262452ec 825 return result_type;
c906108c
SS
826}
827
729efb13
SA
828/* Create a range type using either a blank type supplied in
829 RESULT_TYPE, or creating a new type, inheriting the objfile from
830 INDEX_TYPE.
831
832 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
833 to HIGH_BOUND, inclusive.
834
835 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
836 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
837
838struct type *
839create_static_range_type (struct type *result_type, struct type *index_type,
840 LONGEST low_bound, LONGEST high_bound)
841{
842 struct dynamic_prop low, high;
843
844 low.kind = PROP_CONST;
845 low.data.const_val = low_bound;
846
847 high.kind = PROP_CONST;
848 high.data.const_val = high_bound;
849
850 result_type = create_range_type (result_type, index_type, &low, &high);
851
852 return result_type;
853}
854
80180f79
SA
855/* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
856 are static, otherwise returns 0. */
857
858static int
859has_static_range (const struct range_bounds *bounds)
860{
861 return (bounds->low.kind == PROP_CONST
862 && bounds->high.kind == PROP_CONST);
863}
864
865
7ba81444
MS
866/* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
867 TYPE. Return 1 if type is a range type, 0 if it is discrete (and
868 bounds will fit in LONGEST), or -1 otherwise. */
c906108c
SS
869
870int
fba45db2 871get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
c906108c
SS
872{
873 CHECK_TYPEDEF (type);
874 switch (TYPE_CODE (type))
875 {
876 case TYPE_CODE_RANGE:
877 *lowp = TYPE_LOW_BOUND (type);
878 *highp = TYPE_HIGH_BOUND (type);
879 return 1;
880 case TYPE_CODE_ENUM:
881 if (TYPE_NFIELDS (type) > 0)
882 {
883 /* The enums may not be sorted by value, so search all
0963b4bd 884 entries. */
c906108c
SS
885 int i;
886
14e75d8e 887 *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
c906108c
SS
888 for (i = 0; i < TYPE_NFIELDS (type); i++)
889 {
14e75d8e
JK
890 if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
891 *lowp = TYPE_FIELD_ENUMVAL (type, i);
892 if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
893 *highp = TYPE_FIELD_ENUMVAL (type, i);
c906108c
SS
894 }
895
7ba81444 896 /* Set unsigned indicator if warranted. */
c5aa993b 897 if (*lowp >= 0)
c906108c 898 {
876cecd0 899 TYPE_UNSIGNED (type) = 1;
c906108c
SS
900 }
901 }
902 else
903 {
904 *lowp = 0;
905 *highp = -1;
906 }
907 return 0;
908 case TYPE_CODE_BOOL:
909 *lowp = 0;
910 *highp = 1;
911 return 0;
912 case TYPE_CODE_INT:
c5aa993b 913 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
c906108c
SS
914 return -1;
915 if (!TYPE_UNSIGNED (type))
916 {
c5aa993b 917 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
c906108c
SS
918 *highp = -*lowp - 1;
919 return 0;
920 }
7ba81444 921 /* ... fall through for unsigned ints ... */
c906108c
SS
922 case TYPE_CODE_CHAR:
923 *lowp = 0;
924 /* This round-about calculation is to avoid shifting by
7b83ea04 925 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
7ba81444 926 if TYPE_LENGTH (type) == sizeof (LONGEST). */
c906108c
SS
927 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
928 *highp = (*highp - 1) | *highp;
929 return 0;
930 default:
931 return -1;
932 }
933}
934
dbc98a8b
KW
935/* Assuming TYPE is a simple, non-empty array type, compute its upper
936 and lower bound. Save the low bound into LOW_BOUND if not NULL.
937 Save the high bound into HIGH_BOUND if not NULL.
938
0963b4bd 939 Return 1 if the operation was successful. Return zero otherwise,
dbc98a8b
KW
940 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
941
942 We now simply use get_discrete_bounds call to get the values
943 of the low and high bounds.
944 get_discrete_bounds can return three values:
945 1, meaning that index is a range,
946 0, meaning that index is a discrete type,
947 or -1 for failure. */
948
949int
950get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
951{
952 struct type *index = TYPE_INDEX_TYPE (type);
953 LONGEST low = 0;
954 LONGEST high = 0;
955 int res;
956
957 if (index == NULL)
958 return 0;
959
960 res = get_discrete_bounds (index, &low, &high);
961 if (res == -1)
962 return 0;
963
964 /* Check if the array bounds are undefined. */
965 if (res == 1
966 && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
967 || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
968 return 0;
969
970 if (low_bound)
971 *low_bound = low;
972
973 if (high_bound)
974 *high_bound = high;
975
976 return 1;
977}
978
7ba81444
MS
979/* Create an array type using either a blank type supplied in
980 RESULT_TYPE, or creating a new type, inheriting the objfile from
981 RANGE_TYPE.
c906108c
SS
982
983 Elements will be of type ELEMENT_TYPE, the indices will be of type
984 RANGE_TYPE.
985
dc53a7ad
JB
986 If BIT_STRIDE is not zero, build a packed array type whose element
987 size is BIT_STRIDE. Otherwise, ignore this parameter.
988
7ba81444
MS
989 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
990 sure it is TYPE_CODE_UNDEF before we bash it into an array
991 type? */
c906108c
SS
992
993struct type *
dc53a7ad
JB
994create_array_type_with_stride (struct type *result_type,
995 struct type *element_type,
996 struct type *range_type,
997 unsigned int bit_stride)
c906108c 998{
c906108c 999 if (result_type == NULL)
e9bb382b
UW
1000 result_type = alloc_type_copy (range_type);
1001
c906108c
SS
1002 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
1003 TYPE_TARGET_TYPE (result_type) = element_type;
80180f79
SA
1004 if (has_static_range (TYPE_RANGE_DATA (range_type)))
1005 {
1006 LONGEST low_bound, high_bound;
1007
1008 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
1009 low_bound = high_bound = 0;
1010 CHECK_TYPEDEF (element_type);
1011 /* Be careful when setting the array length. Ada arrays can be
1012 empty arrays with the high_bound being smaller than the low_bound.
1013 In such cases, the array length should be zero. */
1014 if (high_bound < low_bound)
1015 TYPE_LENGTH (result_type) = 0;
1016 else if (bit_stride > 0)
1017 TYPE_LENGTH (result_type) =
1018 (bit_stride * (high_bound - low_bound + 1) + 7) / 8;
1019 else
1020 TYPE_LENGTH (result_type) =
1021 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
1022 }
ab0d6e0d 1023 else
80180f79
SA
1024 {
1025 /* This type is dynamic and its length needs to be computed
1026 on demand. In the meantime, avoid leaving the TYPE_LENGTH
1027 undefined by setting it to zero. Although we are not expected
1028 to trust TYPE_LENGTH in this case, setting the size to zero
1029 allows us to avoid allocating objects of random sizes in case
1030 we accidently do. */
1031 TYPE_LENGTH (result_type) = 0;
1032 }
1033
c906108c
SS
1034 TYPE_NFIELDS (result_type) = 1;
1035 TYPE_FIELDS (result_type) =
1deafd4e 1036 (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
262452ec 1037 TYPE_INDEX_TYPE (result_type) = range_type;
c906108c 1038 TYPE_VPTR_FIELDNO (result_type) = -1;
dc53a7ad
JB
1039 if (bit_stride > 0)
1040 TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
c906108c 1041
0963b4bd 1042 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays. */
c906108c 1043 if (TYPE_LENGTH (result_type) == 0)
876cecd0 1044 TYPE_TARGET_STUB (result_type) = 1;
c906108c 1045
c16abbde 1046 return result_type;
c906108c
SS
1047}
1048
dc53a7ad
JB
1049/* Same as create_array_type_with_stride but with no bit_stride
1050 (BIT_STRIDE = 0), thus building an unpacked array. */
1051
1052struct type *
1053create_array_type (struct type *result_type,
1054 struct type *element_type,
1055 struct type *range_type)
1056{
1057 return create_array_type_with_stride (result_type, element_type,
1058 range_type, 0);
1059}
1060
e3506a9f
UW
1061struct type *
1062lookup_array_range_type (struct type *element_type,
63375b74 1063 LONGEST low_bound, LONGEST high_bound)
e3506a9f 1064{
50810684 1065 struct gdbarch *gdbarch = get_type_arch (element_type);
e3506a9f
UW
1066 struct type *index_type = builtin_type (gdbarch)->builtin_int;
1067 struct type *range_type
0c9c3474 1068 = create_static_range_type (NULL, index_type, low_bound, high_bound);
d8734c88 1069
e3506a9f
UW
1070 return create_array_type (NULL, element_type, range_type);
1071}
1072
7ba81444
MS
1073/* Create a string type using either a blank type supplied in
1074 RESULT_TYPE, or creating a new type. String types are similar
1075 enough to array of char types that we can use create_array_type to
1076 build the basic type and then bash it into a string type.
c906108c
SS
1077
1078 For fixed length strings, the range type contains 0 as the lower
1079 bound and the length of the string minus one as the upper bound.
1080
7ba81444
MS
1081 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1082 sure it is TYPE_CODE_UNDEF before we bash it into a string
1083 type? */
c906108c
SS
1084
1085struct type *
3b7538c0
UW
1086create_string_type (struct type *result_type,
1087 struct type *string_char_type,
7ba81444 1088 struct type *range_type)
c906108c
SS
1089{
1090 result_type = create_array_type (result_type,
f290d38e 1091 string_char_type,
c906108c
SS
1092 range_type);
1093 TYPE_CODE (result_type) = TYPE_CODE_STRING;
c16abbde 1094 return result_type;
c906108c
SS
1095}
1096
e3506a9f
UW
1097struct type *
1098lookup_string_range_type (struct type *string_char_type,
63375b74 1099 LONGEST low_bound, LONGEST high_bound)
e3506a9f
UW
1100{
1101 struct type *result_type;
d8734c88 1102
e3506a9f
UW
1103 result_type = lookup_array_range_type (string_char_type,
1104 low_bound, high_bound);
1105 TYPE_CODE (result_type) = TYPE_CODE_STRING;
1106 return result_type;
1107}
1108
c906108c 1109struct type *
fba45db2 1110create_set_type (struct type *result_type, struct type *domain_type)
c906108c 1111{
c906108c 1112 if (result_type == NULL)
e9bb382b
UW
1113 result_type = alloc_type_copy (domain_type);
1114
c906108c
SS
1115 TYPE_CODE (result_type) = TYPE_CODE_SET;
1116 TYPE_NFIELDS (result_type) = 1;
1deafd4e 1117 TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
c906108c 1118
74a9bb82 1119 if (!TYPE_STUB (domain_type))
c906108c 1120 {
f9780d5b 1121 LONGEST low_bound, high_bound, bit_length;
d8734c88 1122
c906108c
SS
1123 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
1124 low_bound = high_bound = 0;
1125 bit_length = high_bound - low_bound + 1;
1126 TYPE_LENGTH (result_type)
1127 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
f9780d5b 1128 if (low_bound >= 0)
876cecd0 1129 TYPE_UNSIGNED (result_type) = 1;
c906108c
SS
1130 }
1131 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
1132
c16abbde 1133 return result_type;
c906108c
SS
1134}
1135
ea37ba09
DJ
1136/* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
1137 and any array types nested inside it. */
1138
1139void
1140make_vector_type (struct type *array_type)
1141{
1142 struct type *inner_array, *elt_type;
1143 int flags;
1144
1145 /* Find the innermost array type, in case the array is
1146 multi-dimensional. */
1147 inner_array = array_type;
1148 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
1149 inner_array = TYPE_TARGET_TYPE (inner_array);
1150
1151 elt_type = TYPE_TARGET_TYPE (inner_array);
1152 if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
1153 {
2844d6b5 1154 flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
ea37ba09
DJ
1155 elt_type = make_qualified_type (elt_type, flags, NULL);
1156 TYPE_TARGET_TYPE (inner_array) = elt_type;
1157 }
1158
876cecd0 1159 TYPE_VECTOR (array_type) = 1;
ea37ba09
DJ
1160}
1161
794ac428 1162struct type *
ac3aafc7
EZ
1163init_vector_type (struct type *elt_type, int n)
1164{
1165 struct type *array_type;
d8734c88 1166
e3506a9f 1167 array_type = lookup_array_range_type (elt_type, 0, n - 1);
ea37ba09 1168 make_vector_type (array_type);
ac3aafc7
EZ
1169 return array_type;
1170}
1171
0d5de010
DJ
1172/* Smash TYPE to be a type of pointers to members of DOMAIN with type
1173 TO_TYPE. A member pointer is a wierd thing -- it amounts to a
1174 typed offset into a struct, e.g. "an int at offset 8". A MEMBER
1175 TYPE doesn't include the offset (that's the value of the MEMBER
1176 itself), but does include the structure type into which it points
1177 (for some reason).
c906108c 1178
7ba81444
MS
1179 When "smashing" the type, we preserve the objfile that the old type
1180 pointed to, since we aren't changing where the type is actually
c906108c
SS
1181 allocated. */
1182
1183void
0d5de010
DJ
1184smash_to_memberptr_type (struct type *type, struct type *domain,
1185 struct type *to_type)
c906108c 1186{
2fdde8f8 1187 smash_type (type);
c906108c
SS
1188 TYPE_TARGET_TYPE (type) = to_type;
1189 TYPE_DOMAIN_TYPE (type) = domain;
0d5de010
DJ
1190 /* Assume that a data member pointer is the same size as a normal
1191 pointer. */
50810684
UW
1192 TYPE_LENGTH (type)
1193 = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
0d5de010 1194 TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
c906108c
SS
1195}
1196
0b92b5bb
TT
1197/* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1198
1199 When "smashing" the type, we preserve the objfile that the old type
1200 pointed to, since we aren't changing where the type is actually
1201 allocated. */
1202
1203void
1204smash_to_methodptr_type (struct type *type, struct type *to_type)
1205{
1206 smash_type (type);
1207 TYPE_TARGET_TYPE (type) = to_type;
1208 TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1209 TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1210 TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1211}
1212
c906108c
SS
1213/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
1214 METHOD just means `function that gets an extra "this" argument'.
1215
7ba81444
MS
1216 When "smashing" the type, we preserve the objfile that the old type
1217 pointed to, since we aren't changing where the type is actually
c906108c
SS
1218 allocated. */
1219
1220void
fba45db2 1221smash_to_method_type (struct type *type, struct type *domain,
ad2f7632
DJ
1222 struct type *to_type, struct field *args,
1223 int nargs, int varargs)
c906108c 1224{
2fdde8f8 1225 smash_type (type);
c906108c
SS
1226 TYPE_TARGET_TYPE (type) = to_type;
1227 TYPE_DOMAIN_TYPE (type) = domain;
ad2f7632
DJ
1228 TYPE_FIELDS (type) = args;
1229 TYPE_NFIELDS (type) = nargs;
1230 if (varargs)
876cecd0 1231 TYPE_VARARGS (type) = 1;
c906108c
SS
1232 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
1233 TYPE_CODE (type) = TYPE_CODE_METHOD;
1234}
1235
1236/* Return a typename for a struct/union/enum type without "struct ",
1237 "union ", or "enum ". If the type has a NULL name, return NULL. */
1238
0d5cff50 1239const char *
aa1ee363 1240type_name_no_tag (const struct type *type)
c906108c
SS
1241{
1242 if (TYPE_TAG_NAME (type) != NULL)
1243 return TYPE_TAG_NAME (type);
1244
7ba81444
MS
1245 /* Is there code which expects this to return the name if there is
1246 no tag name? My guess is that this is mainly used for C++ in
1247 cases where the two will always be the same. */
c906108c
SS
1248 return TYPE_NAME (type);
1249}
1250
d8228535
JK
1251/* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1252 Since GCC PR debug/47510 DWARF provides associated information to detect the
1253 anonymous class linkage name from its typedef.
1254
1255 Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1256 apply it itself. */
1257
1258const char *
1259type_name_no_tag_or_error (struct type *type)
1260{
1261 struct type *saved_type = type;
1262 const char *name;
1263 struct objfile *objfile;
1264
1265 CHECK_TYPEDEF (type);
1266
1267 name = type_name_no_tag (type);
1268 if (name != NULL)
1269 return name;
1270
1271 name = type_name_no_tag (saved_type);
1272 objfile = TYPE_OBJFILE (saved_type);
1273 error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
4262abfb
JK
1274 name ? name : "<anonymous>",
1275 objfile ? objfile_name (objfile) : "<arch>");
d8228535
JK
1276}
1277
7ba81444
MS
1278/* Lookup a typedef or primitive type named NAME, visible in lexical
1279 block BLOCK. If NOERR is nonzero, return zero if NAME is not
1280 suitably defined. */
c906108c
SS
1281
1282struct type *
e6c014f2 1283lookup_typename (const struct language_defn *language,
ddd49eee 1284 struct gdbarch *gdbarch, const char *name,
34eaf542 1285 const struct block *block, int noerr)
c906108c 1286{
52f0bd74 1287 struct symbol *sym;
659c9f3a 1288 struct type *type;
c906108c 1289
774b6a14 1290 sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
c51fe631
DE
1291 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1292 return SYMBOL_TYPE (sym);
1293
659c9f3a
DE
1294 type = language_lookup_primitive_type_by_name (language, gdbarch, name);
1295 if (type)
1296 return type;
c51fe631
DE
1297
1298 if (noerr)
1299 return NULL;
1300 error (_("No type named %s."), name);
c906108c
SS
1301}
1302
1303struct type *
e6c014f2 1304lookup_unsigned_typename (const struct language_defn *language,
0d5cff50 1305 struct gdbarch *gdbarch, const char *name)
c906108c
SS
1306{
1307 char *uns = alloca (strlen (name) + 10);
1308
1309 strcpy (uns, "unsigned ");
1310 strcpy (uns + 9, name);
e6c014f2 1311 return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
c906108c
SS
1312}
1313
1314struct type *
e6c014f2 1315lookup_signed_typename (const struct language_defn *language,
0d5cff50 1316 struct gdbarch *gdbarch, const char *name)
c906108c
SS
1317{
1318 struct type *t;
1319 char *uns = alloca (strlen (name) + 8);
1320
1321 strcpy (uns, "signed ");
1322 strcpy (uns + 7, name);
e6c014f2 1323 t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
7ba81444 1324 /* If we don't find "signed FOO" just try again with plain "FOO". */
c906108c
SS
1325 if (t != NULL)
1326 return t;
e6c014f2 1327 return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
c906108c
SS
1328}
1329
1330/* Lookup a structure type named "struct NAME",
1331 visible in lexical block BLOCK. */
1332
1333struct type *
270140bd 1334lookup_struct (const char *name, const struct block *block)
c906108c 1335{
52f0bd74 1336 struct symbol *sym;
c906108c 1337
2570f2b7 1338 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1339
1340 if (sym == NULL)
1341 {
8a3fe4f8 1342 error (_("No struct type named %s."), name);
c906108c
SS
1343 }
1344 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1345 {
7ba81444
MS
1346 error (_("This context has class, union or enum %s, not a struct."),
1347 name);
c906108c
SS
1348 }
1349 return (SYMBOL_TYPE (sym));
1350}
1351
1352/* Lookup a union type named "union NAME",
1353 visible in lexical block BLOCK. */
1354
1355struct type *
270140bd 1356lookup_union (const char *name, const struct block *block)
c906108c 1357{
52f0bd74 1358 struct symbol *sym;
c5aa993b 1359 struct type *t;
c906108c 1360
2570f2b7 1361 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1362
1363 if (sym == NULL)
8a3fe4f8 1364 error (_("No union type named %s."), name);
c906108c 1365
c5aa993b 1366 t = SYMBOL_TYPE (sym);
c906108c
SS
1367
1368 if (TYPE_CODE (t) == TYPE_CODE_UNION)
c16abbde 1369 return t;
c906108c 1370
7ba81444
MS
1371 /* If we get here, it's not a union. */
1372 error (_("This context has class, struct or enum %s, not a union."),
1373 name);
c906108c
SS
1374}
1375
c906108c
SS
1376/* Lookup an enum type named "enum NAME",
1377 visible in lexical block BLOCK. */
1378
1379struct type *
270140bd 1380lookup_enum (const char *name, const struct block *block)
c906108c 1381{
52f0bd74 1382 struct symbol *sym;
c906108c 1383
2570f2b7 1384 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
c906108c
SS
1385 if (sym == NULL)
1386 {
8a3fe4f8 1387 error (_("No enum type named %s."), name);
c906108c
SS
1388 }
1389 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1390 {
7ba81444
MS
1391 error (_("This context has class, struct or union %s, not an enum."),
1392 name);
c906108c
SS
1393 }
1394 return (SYMBOL_TYPE (sym));
1395}
1396
1397/* Lookup a template type named "template NAME<TYPE>",
1398 visible in lexical block BLOCK. */
1399
1400struct type *
7ba81444 1401lookup_template_type (char *name, struct type *type,
270140bd 1402 const struct block *block)
c906108c
SS
1403{
1404 struct symbol *sym;
7ba81444
MS
1405 char *nam = (char *)
1406 alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
d8734c88 1407
c906108c
SS
1408 strcpy (nam, name);
1409 strcat (nam, "<");
0004e5a2 1410 strcat (nam, TYPE_NAME (type));
0963b4bd 1411 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
c906108c 1412
2570f2b7 1413 sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
c906108c
SS
1414
1415 if (sym == NULL)
1416 {
8a3fe4f8 1417 error (_("No template type named %s."), name);
c906108c
SS
1418 }
1419 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1420 {
7ba81444
MS
1421 error (_("This context has class, union or enum %s, not a struct."),
1422 name);
c906108c
SS
1423 }
1424 return (SYMBOL_TYPE (sym));
1425}
1426
7ba81444
MS
1427/* Given a type TYPE, lookup the type of the component of type named
1428 NAME.
c906108c 1429
7ba81444
MS
1430 TYPE can be either a struct or union, or a pointer or reference to
1431 a struct or union. If it is a pointer or reference, its target
1432 type is automatically used. Thus '.' and '->' are interchangable,
1433 as specified for the definitions of the expression element types
1434 STRUCTOP_STRUCT and STRUCTOP_PTR.
c906108c
SS
1435
1436 If NOERR is nonzero, return zero if NAME is not suitably defined.
1437 If NAME is the name of a baseclass type, return that type. */
1438
1439struct type *
d7561cbb 1440lookup_struct_elt_type (struct type *type, const char *name, int noerr)
c906108c
SS
1441{
1442 int i;
c92817ce 1443 char *typename;
c906108c
SS
1444
1445 for (;;)
1446 {
1447 CHECK_TYPEDEF (type);
1448 if (TYPE_CODE (type) != TYPE_CODE_PTR
1449 && TYPE_CODE (type) != TYPE_CODE_REF)
1450 break;
1451 type = TYPE_TARGET_TYPE (type);
1452 }
1453
687d6395
MS
1454 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1455 && TYPE_CODE (type) != TYPE_CODE_UNION)
c906108c 1456 {
c92817ce
TT
1457 typename = type_to_string (type);
1458 make_cleanup (xfree, typename);
1459 error (_("Type %s is not a structure or union type."), typename);
c906108c
SS
1460 }
1461
1462#if 0
7ba81444
MS
1463 /* FIXME: This change put in by Michael seems incorrect for the case
1464 where the structure tag name is the same as the member name.
0963b4bd 1465 I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
7ba81444 1466 foo; } bell;" Disabled by fnf. */
c906108c
SS
1467 {
1468 char *typename;
1469
1470 typename = type_name_no_tag (type);
762f08a3 1471 if (typename != NULL && strcmp (typename, name) == 0)
c906108c
SS
1472 return type;
1473 }
1474#endif
1475
1476 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1477 {
0d5cff50 1478 const char *t_field_name = TYPE_FIELD_NAME (type, i);
c906108c 1479
db577aea 1480 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
1481 {
1482 return TYPE_FIELD_TYPE (type, i);
1483 }
f5a010c0
PM
1484 else if (!t_field_name || *t_field_name == '\0')
1485 {
d8734c88
MS
1486 struct type *subtype
1487 = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1488
f5a010c0
PM
1489 if (subtype != NULL)
1490 return subtype;
1491 }
c906108c
SS
1492 }
1493
1494 /* OK, it's not in this class. Recursively check the baseclasses. */
1495 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1496 {
1497 struct type *t;
1498
9733fc94 1499 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
c906108c
SS
1500 if (t != NULL)
1501 {
1502 return t;
1503 }
1504 }
1505
1506 if (noerr)
1507 {
1508 return NULL;
1509 }
c5aa993b 1510
c92817ce
TT
1511 typename = type_to_string (type);
1512 make_cleanup (xfree, typename);
1513 error (_("Type %s has no component named %s."), typename, name);
c906108c
SS
1514}
1515
ed3ef339
DE
1516/* Store in *MAX the largest number representable by unsigned integer type
1517 TYPE. */
1518
1519void
1520get_unsigned_type_max (struct type *type, ULONGEST *max)
1521{
1522 unsigned int n;
1523
1524 CHECK_TYPEDEF (type);
1525 gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
1526 gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
1527
1528 /* Written this way to avoid overflow. */
1529 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1530 *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1531}
1532
1533/* Store in *MIN, *MAX the smallest and largest numbers representable by
1534 signed integer type TYPE. */
1535
1536void
1537get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1538{
1539 unsigned int n;
1540
1541 CHECK_TYPEDEF (type);
1542 gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
1543 gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
1544
1545 n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1546 *min = -((ULONGEST) 1 << (n - 1));
1547 *max = ((ULONGEST) 1 << (n - 1)) - 1;
1548}
1549
81fe8080
DE
1550/* Lookup the vptr basetype/fieldno values for TYPE.
1551 If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1552 vptr_fieldno. Also, if found and basetype is from the same objfile,
1553 cache the results.
1554 If not found, return -1 and ignore BASETYPEP.
1555 Callers should be aware that in some cases (for example,
c906108c 1556 the type or one of its baseclasses is a stub type and we are
d48cc9dd
DJ
1557 debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1558 this function will not be able to find the
7ba81444 1559 virtual function table pointer, and vptr_fieldno will remain -1 and
81fe8080 1560 vptr_basetype will remain NULL or incomplete. */
c906108c 1561
81fe8080
DE
1562int
1563get_vptr_fieldno (struct type *type, struct type **basetypep)
c906108c
SS
1564{
1565 CHECK_TYPEDEF (type);
1566
1567 if (TYPE_VPTR_FIELDNO (type) < 0)
1568 {
1569 int i;
1570
7ba81444
MS
1571 /* We must start at zero in case the first (and only) baseclass
1572 is virtual (and hence we cannot share the table pointer). */
c906108c
SS
1573 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1574 {
81fe8080
DE
1575 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1576 int fieldno;
1577 struct type *basetype;
1578
1579 fieldno = get_vptr_fieldno (baseclass, &basetype);
1580 if (fieldno >= 0)
c906108c 1581 {
81fe8080 1582 /* If the type comes from a different objfile we can't cache
0963b4bd 1583 it, it may have a different lifetime. PR 2384 */
5ef73790 1584 if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
81fe8080
DE
1585 {
1586 TYPE_VPTR_FIELDNO (type) = fieldno;
1587 TYPE_VPTR_BASETYPE (type) = basetype;
1588 }
1589 if (basetypep)
1590 *basetypep = basetype;
1591 return fieldno;
c906108c
SS
1592 }
1593 }
81fe8080
DE
1594
1595 /* Not found. */
1596 return -1;
1597 }
1598 else
1599 {
1600 if (basetypep)
1601 *basetypep = TYPE_VPTR_BASETYPE (type);
1602 return TYPE_VPTR_FIELDNO (type);
c906108c
SS
1603 }
1604}
1605
44e1a9eb
DJ
1606static void
1607stub_noname_complaint (void)
1608{
e2e0b3e5 1609 complaint (&symfile_complaints, _("stub type has NULL name"));
44e1a9eb
DJ
1610}
1611
d98b7a16 1612/* Worker for is_dynamic_type. */
80180f79 1613
d98b7a16
TT
1614static int
1615is_dynamic_type_internal (struct type *type, int top_level)
80180f79
SA
1616{
1617 type = check_typedef (type);
1618
d98b7a16
TT
1619 /* We only want to recognize references at the outermost level. */
1620 if (top_level && TYPE_CODE (type) == TYPE_CODE_REF)
80180f79
SA
1621 type = check_typedef (TYPE_TARGET_TYPE (type));
1622
3cdcd0ce
JB
1623 /* Types that have a dynamic TYPE_DATA_LOCATION are considered
1624 dynamic, even if the type itself is statically defined.
1625 From a user's point of view, this may appear counter-intuitive;
1626 but it makes sense in this context, because the point is to determine
1627 whether any part of the type needs to be resolved before it can
1628 be exploited. */
1629 if (TYPE_DATA_LOCATION (type) != NULL
1630 && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
1631 || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
1632 return 1;
1633
80180f79
SA
1634 switch (TYPE_CODE (type))
1635 {
6f8a3220
JB
1636 case TYPE_CODE_RANGE:
1637 return !has_static_range (TYPE_RANGE_DATA (type));
6f8a3220 1638
80180f79
SA
1639 case TYPE_CODE_ARRAY:
1640 {
80180f79 1641 gdb_assert (TYPE_NFIELDS (type) == 1);
6f8a3220
JB
1642
1643 /* The array is dynamic if either the bounds are dynamic,
1644 or the elements it contains have a dynamic contents. */
d98b7a16 1645 if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
80180f79 1646 return 1;
d98b7a16 1647 return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
80180f79 1648 }
012370f6
TT
1649
1650 case TYPE_CODE_STRUCT:
1651 case TYPE_CODE_UNION:
1652 {
1653 int i;
1654
1655 for (i = 0; i < TYPE_NFIELDS (type); ++i)
1656 if (!field_is_static (&TYPE_FIELD (type, i))
d98b7a16 1657 && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
012370f6
TT
1658 return 1;
1659 }
1660 break;
80180f79 1661 }
92e2a17f
TT
1662
1663 return 0;
80180f79
SA
1664}
1665
d98b7a16
TT
1666/* See gdbtypes.h. */
1667
1668int
1669is_dynamic_type (struct type *type)
1670{
1671 return is_dynamic_type_internal (type, 1);
1672}
1673
1674static struct type *resolve_dynamic_type_internal (struct type *type,
1675 CORE_ADDR addr,
1676 int top_level);
1677
08412b07
JB
1678/* Given a dynamic range type (dyn_range_type) and address,
1679 return a static version of that type. */
d190df30 1680
80180f79 1681static struct type *
08412b07 1682resolve_dynamic_range (struct type *dyn_range_type, CORE_ADDR addr)
80180f79
SA
1683{
1684 CORE_ADDR value;
6f8a3220 1685 struct type *static_range_type;
80180f79
SA
1686 const struct dynamic_prop *prop;
1687 const struct dwarf2_locexpr_baton *baton;
1688 struct dynamic_prop low_bound, high_bound;
1689
6f8a3220 1690 gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);
80180f79 1691
6f8a3220 1692 prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
08412b07 1693 if (dwarf2_evaluate_property (prop, addr, &value))
80180f79
SA
1694 {
1695 low_bound.kind = PROP_CONST;
1696 low_bound.data.const_val = value;
1697 }
1698 else
1699 {
1700 low_bound.kind = PROP_UNDEFINED;
1701 low_bound.data.const_val = 0;
1702 }
1703
6f8a3220 1704 prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
08412b07 1705 if (dwarf2_evaluate_property (prop, addr, &value))
80180f79
SA
1706 {
1707 high_bound.kind = PROP_CONST;
1708 high_bound.data.const_val = value;
c451ebe5 1709
6f8a3220 1710 if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count)
c451ebe5
SA
1711 high_bound.data.const_val
1712 = low_bound.data.const_val + high_bound.data.const_val - 1;
80180f79
SA
1713 }
1714 else
1715 {
1716 high_bound.kind = PROP_UNDEFINED;
1717 high_bound.data.const_val = 0;
1718 }
1719
6f8a3220
JB
1720 static_range_type = create_range_type (copy_type (dyn_range_type),
1721 TYPE_TARGET_TYPE (dyn_range_type),
1722 &low_bound, &high_bound);
1723 TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1;
1724 return static_range_type;
1725}
1726
1727/* Resolves dynamic bound values of an array type TYPE to static ones.
1728 ADDRESS might be needed to resolve the subrange bounds, it is the location
1729 of the associated array. */
1730
1731static struct type *
08412b07 1732resolve_dynamic_array (struct type *type, CORE_ADDR addr)
6f8a3220
JB
1733{
1734 CORE_ADDR value;
1735 struct type *elt_type;
1736 struct type *range_type;
1737 struct type *ary_dim;
1738
1739 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
1740
1741 elt_type = type;
1742 range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
08412b07 1743 range_type = resolve_dynamic_range (range_type, addr);
6f8a3220 1744
80180f79
SA
1745 ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));
1746
1747 if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
08412b07 1748 elt_type = resolve_dynamic_array (TYPE_TARGET_TYPE (type), addr);
80180f79
SA
1749 else
1750 elt_type = TYPE_TARGET_TYPE (type);
1751
80180f79
SA
1752 return create_array_type (copy_type (type),
1753 elt_type,
1754 range_type);
1755}
1756
012370f6
TT
1757/* Resolve dynamic bounds of members of the union TYPE to static
1758 bounds. */
1759
1760static struct type *
1761resolve_dynamic_union (struct type *type, CORE_ADDR addr)
1762{
1763 struct type *resolved_type;
1764 int i;
1765 unsigned int max_len = 0;
1766
1767 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
1768
1769 resolved_type = copy_type (type);
1770 TYPE_FIELDS (resolved_type)
1771 = TYPE_ALLOC (resolved_type,
1772 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1773 memcpy (TYPE_FIELDS (resolved_type),
1774 TYPE_FIELDS (type),
1775 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1776 for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
1777 {
1778 struct type *t;
1779
1780 if (field_is_static (&TYPE_FIELD (type, i)))
1781 continue;
1782
d98b7a16
TT
1783 t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
1784 addr, 0);
012370f6
TT
1785 TYPE_FIELD_TYPE (resolved_type, i) = t;
1786 if (TYPE_LENGTH (t) > max_len)
1787 max_len = TYPE_LENGTH (t);
1788 }
1789
1790 TYPE_LENGTH (resolved_type) = max_len;
1791 return resolved_type;
1792}
1793
1794/* Resolve dynamic bounds of members of the struct TYPE to static
1795 bounds. */
1796
1797static struct type *
1798resolve_dynamic_struct (struct type *type, CORE_ADDR addr)
1799{
1800 struct type *resolved_type;
1801 int i;
6908c509 1802 unsigned resolved_type_bit_length = 0;
012370f6
TT
1803
1804 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
1805 gdb_assert (TYPE_NFIELDS (type) > 0);
1806
1807 resolved_type = copy_type (type);
1808 TYPE_FIELDS (resolved_type)
1809 = TYPE_ALLOC (resolved_type,
1810 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1811 memcpy (TYPE_FIELDS (resolved_type),
1812 TYPE_FIELDS (type),
1813 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
1814 for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
1815 {
6908c509 1816 unsigned new_bit_length;
012370f6
TT
1817
1818 if (field_is_static (&TYPE_FIELD (type, i)))
1819 continue;
1820
6908c509
JB
1821 TYPE_FIELD_TYPE (resolved_type, i)
1822 = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
d98b7a16 1823 addr, 0);
012370f6 1824
6908c509
JB
1825 /* As we know this field is not a static field, the field's
1826 field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
1827 this is the case, but only trigger a simple error rather
1828 than an internal error if that fails. While failing
1829 that verification indicates a bug in our code, the error
1830 is not severe enough to suggest to the user he stops
1831 his debugging session because of it. */
1832 if (TYPE_FIELD_LOC_KIND (resolved_type, i) != FIELD_LOC_KIND_BITPOS)
1833 error (_("Cannot determine struct field location"
1834 " (invalid location kind)"));
1835 new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
1836 if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
1837 new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
1838 else
1839 new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
1840 * TARGET_CHAR_BIT);
1841
1842 /* Normally, we would use the position and size of the last field
1843 to determine the size of the enclosing structure. But GCC seems
1844 to be encoding the position of some fields incorrectly when
1845 the struct contains a dynamic field that is not placed last.
1846 So we compute the struct size based on the field that has
1847 the highest position + size - probably the best we can do. */
1848 if (new_bit_length > resolved_type_bit_length)
1849 resolved_type_bit_length = new_bit_length;
012370f6
TT
1850 }
1851
012370f6 1852 TYPE_LENGTH (resolved_type)
6908c509
JB
1853 = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1854
012370f6
TT
1855 return resolved_type;
1856}
1857
d98b7a16 1858/* Worker for resolved_dynamic_type. */
80180f79 1859
d98b7a16
TT
1860static struct type *
1861resolve_dynamic_type_internal (struct type *type, CORE_ADDR addr,
1862 int top_level)
80180f79
SA
1863{
1864 struct type *real_type = check_typedef (type);
6f8a3220 1865 struct type *resolved_type = type;
3cdcd0ce
JB
1866 const struct dynamic_prop *prop;
1867 CORE_ADDR value;
80180f79 1868
d98b7a16 1869 if (!is_dynamic_type_internal (real_type, top_level))
80180f79
SA
1870 return type;
1871
6f8a3220
JB
1872 switch (TYPE_CODE (type))
1873 {
1874 case TYPE_CODE_TYPEDEF:
1875 resolved_type = copy_type (type);
1876 TYPE_TARGET_TYPE (resolved_type)
d98b7a16
TT
1877 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr,
1878 top_level);
6f8a3220
JB
1879 break;
1880
1881 case TYPE_CODE_REF:
1882 {
1883 CORE_ADDR target_addr = read_memory_typed_address (addr, type);
1884
1885 resolved_type = copy_type (type);
1886 TYPE_TARGET_TYPE (resolved_type)
d98b7a16
TT
1887 = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
1888 target_addr, top_level);
6f8a3220
JB
1889 break;
1890 }
1891
1892 case TYPE_CODE_ARRAY:
08412b07 1893 resolved_type = resolve_dynamic_array (type, addr);
6f8a3220
JB
1894 break;
1895
1896 case TYPE_CODE_RANGE:
08412b07 1897 resolved_type = resolve_dynamic_range (type, addr);
6f8a3220 1898 break;
012370f6
TT
1899
1900 case TYPE_CODE_UNION:
1901 resolved_type = resolve_dynamic_union (type, addr);
1902 break;
1903
1904 case TYPE_CODE_STRUCT:
1905 resolved_type = resolve_dynamic_struct (type, addr);
1906 break;
6f8a3220 1907 }
80180f79 1908
3cdcd0ce
JB
1909 /* Resolve data_location attribute. */
1910 prop = TYPE_DATA_LOCATION (resolved_type);
1911 if (dwarf2_evaluate_property (prop, addr, &value))
1912 {
1913 TYPE_DATA_LOCATION_ADDR (resolved_type) = value;
1914 TYPE_DATA_LOCATION_KIND (resolved_type) = PROP_CONST;
1915 }
1916 else
1917 TYPE_DATA_LOCATION (resolved_type) = NULL;
1918
80180f79
SA
1919 return resolved_type;
1920}
1921
d98b7a16
TT
1922/* See gdbtypes.h */
1923
1924struct type *
1925resolve_dynamic_type (struct type *type, CORE_ADDR addr)
1926{
1927 return resolve_dynamic_type_internal (type, addr, 1);
1928}
1929
92163a10
JK
1930/* Find the real type of TYPE. This function returns the real type,
1931 after removing all layers of typedefs, and completing opaque or stub
1932 types. Completion changes the TYPE argument, but stripping of
1933 typedefs does not.
1934
1935 Instance flags (e.g. const/volatile) are preserved as typedefs are
1936 stripped. If necessary a new qualified form of the underlying type
1937 is created.
1938
1939 NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1940 not been computed and we're either in the middle of reading symbols, or
1941 there was no name for the typedef in the debug info.
1942
9bc118a5
DE
1943 NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1944 QUITs in the symbol reading code can also throw.
1945 Thus this function can throw an exception.
1946
92163a10
JK
1947 If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1948 the target type.
c906108c
SS
1949
1950 If this is a stubbed struct (i.e. declared as struct foo *), see if
0963b4bd 1951 we can find a full definition in some other file. If so, copy this
7ba81444
MS
1952 definition, so we can use it in future. There used to be a comment
1953 (but not any code) that if we don't find a full definition, we'd
1954 set a flag so we don't spend time in the future checking the same
1955 type. That would be a mistake, though--we might load in more
92163a10 1956 symbols which contain a full definition for the type. */
c906108c
SS
1957
1958struct type *
a02fd225 1959check_typedef (struct type *type)
c906108c
SS
1960{
1961 struct type *orig_type = type;
92163a10
JK
1962 /* While we're removing typedefs, we don't want to lose qualifiers.
1963 E.g., const/volatile. */
1964 int instance_flags = TYPE_INSTANCE_FLAGS (type);
a02fd225 1965
423c0af8
MS
1966 gdb_assert (type);
1967
c906108c
SS
1968 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1969 {
1970 if (!TYPE_TARGET_TYPE (type))
1971 {
0d5cff50 1972 const char *name;
c906108c
SS
1973 struct symbol *sym;
1974
1975 /* It is dangerous to call lookup_symbol if we are currently
7ba81444 1976 reading a symtab. Infinite recursion is one danger. */
c906108c 1977 if (currently_reading_symtab)
92163a10 1978 return make_qualified_type (type, instance_flags, NULL);
c906108c
SS
1979
1980 name = type_name_no_tag (type);
7ba81444
MS
1981 /* FIXME: shouldn't we separately check the TYPE_NAME and
1982 the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1983 VAR_DOMAIN as appropriate? (this code was written before
1984 TYPE_NAME and TYPE_TAG_NAME were separate). */
c906108c
SS
1985 if (name == NULL)
1986 {
23136709 1987 stub_noname_complaint ();
92163a10 1988 return make_qualified_type (type, instance_flags, NULL);
c906108c 1989 }
2570f2b7 1990 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
c906108c
SS
1991 if (sym)
1992 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
7ba81444 1993 else /* TYPE_CODE_UNDEF */
e9bb382b 1994 TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
c906108c
SS
1995 }
1996 type = TYPE_TARGET_TYPE (type);
c906108c 1997
92163a10
JK
1998 /* Preserve the instance flags as we traverse down the typedef chain.
1999
2000 Handling address spaces/classes is nasty, what do we do if there's a
2001 conflict?
2002 E.g., what if an outer typedef marks the type as class_1 and an inner
2003 typedef marks the type as class_2?
2004 This is the wrong place to do such error checking. We leave it to
2005 the code that created the typedef in the first place to flag the
2006 error. We just pick the outer address space (akin to letting the
2007 outer cast in a chain of casting win), instead of assuming
2008 "it can't happen". */
2009 {
2010 const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
2011 | TYPE_INSTANCE_FLAG_DATA_SPACE);
2012 const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
2013 int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
2014
2015 /* Treat code vs data spaces and address classes separately. */
2016 if ((instance_flags & ALL_SPACES) != 0)
2017 new_instance_flags &= ~ALL_SPACES;
2018 if ((instance_flags & ALL_CLASSES) != 0)
2019 new_instance_flags &= ~ALL_CLASSES;
2020
2021 instance_flags |= new_instance_flags;
2022 }
2023 }
a02fd225 2024
7ba81444
MS
2025 /* If this is a struct/class/union with no fields, then check
2026 whether a full definition exists somewhere else. This is for
2027 systems where a type definition with no fields is issued for such
2028 types, instead of identifying them as stub types in the first
2029 place. */
c5aa993b 2030
7ba81444
MS
2031 if (TYPE_IS_OPAQUE (type)
2032 && opaque_type_resolution
2033 && !currently_reading_symtab)
c906108c 2034 {
0d5cff50 2035 const char *name = type_name_no_tag (type);
c5aa993b 2036 struct type *newtype;
d8734c88 2037
c906108c
SS
2038 if (name == NULL)
2039 {
23136709 2040 stub_noname_complaint ();
92163a10 2041 return make_qualified_type (type, instance_flags, NULL);
c906108c
SS
2042 }
2043 newtype = lookup_transparent_type (name);
ad766c0a 2044
c906108c 2045 if (newtype)
ad766c0a 2046 {
7ba81444
MS
2047 /* If the resolved type and the stub are in the same
2048 objfile, then replace the stub type with the real deal.
2049 But if they're in separate objfiles, leave the stub
2050 alone; we'll just look up the transparent type every time
2051 we call check_typedef. We can't create pointers between
2052 types allocated to different objfiles, since they may
2053 have different lifetimes. Trying to copy NEWTYPE over to
2054 TYPE's objfile is pointless, too, since you'll have to
2055 move over any other types NEWTYPE refers to, which could
2056 be an unbounded amount of stuff. */
ad766c0a 2057 if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
92163a10
JK
2058 type = make_qualified_type (newtype,
2059 TYPE_INSTANCE_FLAGS (type),
2060 type);
ad766c0a
JB
2061 else
2062 type = newtype;
2063 }
c906108c 2064 }
7ba81444
MS
2065 /* Otherwise, rely on the stub flag being set for opaque/stubbed
2066 types. */
74a9bb82 2067 else if (TYPE_STUB (type) && !currently_reading_symtab)
c906108c 2068 {
0d5cff50 2069 const char *name = type_name_no_tag (type);
c906108c 2070 /* FIXME: shouldn't we separately check the TYPE_NAME and the
176620f1 2071 TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
7b83ea04
AC
2072 as appropriate? (this code was written before TYPE_NAME and
2073 TYPE_TAG_NAME were separate). */
c906108c 2074 struct symbol *sym;
d8734c88 2075
c906108c
SS
2076 if (name == NULL)
2077 {
23136709 2078 stub_noname_complaint ();
92163a10 2079 return make_qualified_type (type, instance_flags, NULL);
c906108c 2080 }
2570f2b7 2081 sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
c906108c 2082 if (sym)
c26f2453
JB
2083 {
2084 /* Same as above for opaque types, we can replace the stub
92163a10 2085 with the complete type only if they are in the same
c26f2453
JB
2086 objfile. */
2087 if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
92163a10
JK
2088 type = make_qualified_type (SYMBOL_TYPE (sym),
2089 TYPE_INSTANCE_FLAGS (type),
2090 type);
c26f2453
JB
2091 else
2092 type = SYMBOL_TYPE (sym);
2093 }
c906108c
SS
2094 }
2095
74a9bb82 2096 if (TYPE_TARGET_STUB (type))
c906108c
SS
2097 {
2098 struct type *range_type;
2099 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
2100
74a9bb82 2101 if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
c5aa993b 2102 {
73e2eb35 2103 /* Nothing we can do. */
c5aa993b 2104 }
c906108c
SS
2105 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
2106 {
2107 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
876cecd0 2108 TYPE_TARGET_STUB (type) = 0;
c906108c
SS
2109 }
2110 }
92163a10
JK
2111
2112 type = make_qualified_type (type, instance_flags, NULL);
2113
7ba81444 2114 /* Cache TYPE_LENGTH for future use. */
c906108c 2115 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
92163a10 2116
c906108c
SS
2117 return type;
2118}
2119
7ba81444 2120/* Parse a type expression in the string [P..P+LENGTH). If an error
48319d1f 2121 occurs, silently return a void type. */
c91ecb25 2122
b9362cc7 2123static struct type *
48319d1f 2124safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
c91ecb25
ND
2125{
2126 struct ui_file *saved_gdb_stderr;
34365054 2127 struct type *type = NULL; /* Initialize to keep gcc happy. */
8e7b59a5 2128 volatile struct gdb_exception except;
c91ecb25 2129
7ba81444 2130 /* Suppress error messages. */
c91ecb25
ND
2131 saved_gdb_stderr = gdb_stderr;
2132 gdb_stderr = ui_file_new ();
2133
7ba81444 2134 /* Call parse_and_eval_type() without fear of longjmp()s. */
8e7b59a5
KS
2135 TRY_CATCH (except, RETURN_MASK_ERROR)
2136 {
2137 type = parse_and_eval_type (p, length);
2138 }
2139
2140 if (except.reason < 0)
48319d1f 2141 type = builtin_type (gdbarch)->builtin_void;
c91ecb25 2142
7ba81444 2143 /* Stop suppressing error messages. */
c91ecb25
ND
2144 ui_file_delete (gdb_stderr);
2145 gdb_stderr = saved_gdb_stderr;
2146
2147 return type;
2148}
2149
c906108c
SS
2150/* Ugly hack to convert method stubs into method types.
2151
7ba81444
MS
2152 He ain't kiddin'. This demangles the name of the method into a
2153 string including argument types, parses out each argument type,
2154 generates a string casting a zero to that type, evaluates the
2155 string, and stuffs the resulting type into an argtype vector!!!
2156 Then it knows the type of the whole function (including argument
2157 types for overloading), which info used to be in the stab's but was
2158 removed to hack back the space required for them. */
c906108c 2159
de17c821 2160static void
fba45db2 2161check_stub_method (struct type *type, int method_id, int signature_id)
c906108c 2162{
50810684 2163 struct gdbarch *gdbarch = get_type_arch (type);
c906108c
SS
2164 struct fn_field *f;
2165 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
8de20a37
TT
2166 char *demangled_name = gdb_demangle (mangled_name,
2167 DMGL_PARAMS | DMGL_ANSI);
c906108c
SS
2168 char *argtypetext, *p;
2169 int depth = 0, argcount = 1;
ad2f7632 2170 struct field *argtypes;
c906108c
SS
2171 struct type *mtype;
2172
2173 /* Make sure we got back a function string that we can use. */
2174 if (demangled_name)
2175 p = strchr (demangled_name, '(');
502dcf4e
AC
2176 else
2177 p = NULL;
c906108c
SS
2178
2179 if (demangled_name == NULL || p == NULL)
7ba81444
MS
2180 error (_("Internal: Cannot demangle mangled name `%s'."),
2181 mangled_name);
c906108c
SS
2182
2183 /* Now, read in the parameters that define this type. */
2184 p += 1;
2185 argtypetext = p;
2186 while (*p)
2187 {
070ad9f0 2188 if (*p == '(' || *p == '<')
c906108c
SS
2189 {
2190 depth += 1;
2191 }
070ad9f0 2192 else if (*p == ')' || *p == '>')
c906108c
SS
2193 {
2194 depth -= 1;
2195 }
2196 else if (*p == ',' && depth == 0)
2197 {
2198 argcount += 1;
2199 }
2200
2201 p += 1;
2202 }
2203
ad2f7632
DJ
2204 /* If we read one argument and it was ``void'', don't count it. */
2205 if (strncmp (argtypetext, "(void)", 6) == 0)
2206 argcount -= 1;
c906108c 2207
ad2f7632
DJ
2208 /* We need one extra slot, for the THIS pointer. */
2209
2210 argtypes = (struct field *)
2211 TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
c906108c 2212 p = argtypetext;
4a1970e4
DJ
2213
2214 /* Add THIS pointer for non-static methods. */
2215 f = TYPE_FN_FIELDLIST1 (type, method_id);
2216 if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
2217 argcount = 0;
2218 else
2219 {
ad2f7632 2220 argtypes[0].type = lookup_pointer_type (type);
4a1970e4
DJ
2221 argcount = 1;
2222 }
c906108c 2223
0963b4bd 2224 if (*p != ')') /* () means no args, skip while. */
c906108c
SS
2225 {
2226 depth = 0;
2227 while (*p)
2228 {
2229 if (depth <= 0 && (*p == ',' || *p == ')'))
2230 {
ad2f7632
DJ
2231 /* Avoid parsing of ellipsis, they will be handled below.
2232 Also avoid ``void'' as above. */
2233 if (strncmp (argtypetext, "...", p - argtypetext) != 0
2234 && strncmp (argtypetext, "void", p - argtypetext) != 0)
c906108c 2235 {
ad2f7632 2236 argtypes[argcount].type =
48319d1f 2237 safe_parse_type (gdbarch, argtypetext, p - argtypetext);
c906108c
SS
2238 argcount += 1;
2239 }
2240 argtypetext = p + 1;
2241 }
2242
070ad9f0 2243 if (*p == '(' || *p == '<')
c906108c
SS
2244 {
2245 depth += 1;
2246 }
070ad9f0 2247 else if (*p == ')' || *p == '>')
c906108c
SS
2248 {
2249 depth -= 1;
2250 }
2251
2252 p += 1;
2253 }
2254 }
2255
c906108c
SS
2256 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
2257
2258 /* Now update the old "stub" type into a real type. */
2259 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
2260 TYPE_DOMAIN_TYPE (mtype) = type;
ad2f7632
DJ
2261 TYPE_FIELDS (mtype) = argtypes;
2262 TYPE_NFIELDS (mtype) = argcount;
876cecd0 2263 TYPE_STUB (mtype) = 0;
c906108c 2264 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
ad2f7632 2265 if (p[-2] == '.')
876cecd0 2266 TYPE_VARARGS (mtype) = 1;
ad2f7632
DJ
2267
2268 xfree (demangled_name);
c906108c
SS
2269}
2270
7ba81444
MS
2271/* This is the external interface to check_stub_method, above. This
2272 function unstubs all of the signatures for TYPE's METHOD_ID method
2273 name. After calling this function TYPE_FN_FIELD_STUB will be
2274 cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
2275 correct.
de17c821
DJ
2276
2277 This function unfortunately can not die until stabs do. */
2278
2279void
2280check_stub_method_group (struct type *type, int method_id)
2281{
2282 int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
2283 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
f710f4fc 2284 int j, found_stub = 0;
de17c821
DJ
2285
2286 for (j = 0; j < len; j++)
2287 if (TYPE_FN_FIELD_STUB (f, j))
2288 {
2289 found_stub = 1;
2290 check_stub_method (type, method_id, j);
2291 }
2292
7ba81444
MS
2293 /* GNU v3 methods with incorrect names were corrected when we read
2294 in type information, because it was cheaper to do it then. The
2295 only GNU v2 methods with incorrect method names are operators and
2296 destructors; destructors were also corrected when we read in type
2297 information.
de17c821
DJ
2298
2299 Therefore the only thing we need to handle here are v2 operator
2300 names. */
2301 if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
2302 {
2303 int ret;
2304 char dem_opname[256];
2305
7ba81444
MS
2306 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
2307 method_id),
de17c821
DJ
2308 dem_opname, DMGL_ANSI);
2309 if (!ret)
7ba81444
MS
2310 ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type,
2311 method_id),
de17c821
DJ
2312 dem_opname, 0);
2313 if (ret)
2314 TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
2315 }
2316}
2317
9655fd1a
JK
2318/* Ensure it is in .rodata (if available) by workarounding GCC PR 44690. */
2319const struct cplus_struct_type cplus_struct_default = { };
c906108c
SS
2320
2321void
fba45db2 2322allocate_cplus_struct_type (struct type *type)
c906108c 2323{
b4ba55a1
JB
2324 if (HAVE_CPLUS_STRUCT (type))
2325 /* Structure was already allocated. Nothing more to do. */
2326 return;
2327
2328 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
2329 TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2330 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
2331 *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
c906108c
SS
2332}
2333
b4ba55a1
JB
2334const struct gnat_aux_type gnat_aux_default =
2335 { NULL };
2336
2337/* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
2338 and allocate the associated gnat-specific data. The gnat-specific
2339 data is also initialized to gnat_aux_default. */
5212577a 2340
b4ba55a1
JB
2341void
2342allocate_gnat_aux_type (struct type *type)
2343{
2344 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
2345 TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
2346 TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
2347 *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
2348}
2349
c906108c
SS
2350/* Helper function to initialize the standard scalar types.
2351
86f62fd7
TT
2352 If NAME is non-NULL, then it is used to initialize the type name.
2353 Note that NAME is not copied; it is required to have a lifetime at
2354 least as long as OBJFILE. */
c906108c
SS
2355
2356struct type *
7ba81444 2357init_type (enum type_code code, int length, int flags,
748e18ae 2358 const char *name, struct objfile *objfile)
c906108c 2359{
52f0bd74 2360 struct type *type;
c906108c
SS
2361
2362 type = alloc_type (objfile);
2363 TYPE_CODE (type) = code;
2364 TYPE_LENGTH (type) = length;
876cecd0
TT
2365
2366 gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
2367 if (flags & TYPE_FLAG_UNSIGNED)
2368 TYPE_UNSIGNED (type) = 1;
2369 if (flags & TYPE_FLAG_NOSIGN)
2370 TYPE_NOSIGN (type) = 1;
2371 if (flags & TYPE_FLAG_STUB)
2372 TYPE_STUB (type) = 1;
2373 if (flags & TYPE_FLAG_TARGET_STUB)
2374 TYPE_TARGET_STUB (type) = 1;
2375 if (flags & TYPE_FLAG_STATIC)
2376 TYPE_STATIC (type) = 1;
2377 if (flags & TYPE_FLAG_PROTOTYPED)
2378 TYPE_PROTOTYPED (type) = 1;
2379 if (flags & TYPE_FLAG_INCOMPLETE)
2380 TYPE_INCOMPLETE (type) = 1;
2381 if (flags & TYPE_FLAG_VARARGS)
2382 TYPE_VARARGS (type) = 1;
2383 if (flags & TYPE_FLAG_VECTOR)
2384 TYPE_VECTOR (type) = 1;
2385 if (flags & TYPE_FLAG_STUB_SUPPORTED)
2386 TYPE_STUB_SUPPORTED (type) = 1;
876cecd0
TT
2387 if (flags & TYPE_FLAG_FIXED_INSTANCE)
2388 TYPE_FIXED_INSTANCE (type) = 1;
0875794a
JK
2389 if (flags & TYPE_FLAG_GNU_IFUNC)
2390 TYPE_GNU_IFUNC (type) = 1;
876cecd0 2391
86f62fd7 2392 TYPE_NAME (type) = name;
c906108c
SS
2393
2394 /* C++ fancies. */
2395
973ccf8b 2396 if (name && strcmp (name, "char") == 0)
876cecd0 2397 TYPE_NOSIGN (type) = 1;
973ccf8b 2398
b4ba55a1 2399 switch (code)
c906108c 2400 {
b4ba55a1
JB
2401 case TYPE_CODE_STRUCT:
2402 case TYPE_CODE_UNION:
2403 case TYPE_CODE_NAMESPACE:
2404 INIT_CPLUS_SPECIFIC (type);
2405 break;
2406 case TYPE_CODE_FLT:
2407 TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2408 break;
2409 case TYPE_CODE_FUNC:
b6cdc2c1 2410 INIT_FUNC_SPECIFIC (type);
b4ba55a1 2411 break;
c906108c 2412 }
c16abbde 2413 return type;
c906108c 2414}
5212577a
DE
2415\f
2416/* Queries on types. */
c906108c 2417
c906108c 2418int
fba45db2 2419can_dereference (struct type *t)
c906108c 2420{
7ba81444
MS
2421 /* FIXME: Should we return true for references as well as
2422 pointers? */
c906108c
SS
2423 CHECK_TYPEDEF (t);
2424 return
2425 (t != NULL
2426 && TYPE_CODE (t) == TYPE_CODE_PTR
2427 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2428}
2429
adf40b2e 2430int
fba45db2 2431is_integral_type (struct type *t)
adf40b2e
JM
2432{
2433 CHECK_TYPEDEF (t);
2434 return
2435 ((t != NULL)
d4f3574e
SS
2436 && ((TYPE_CODE (t) == TYPE_CODE_INT)
2437 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
4f2aea11 2438 || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
d4f3574e
SS
2439 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2440 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2441 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
adf40b2e
JM
2442}
2443
e09342b5
TJB
2444/* Return true if TYPE is scalar. */
2445
2446static int
2447is_scalar_type (struct type *type)
2448{
2449 CHECK_TYPEDEF (type);
2450
2451 switch (TYPE_CODE (type))
2452 {
2453 case TYPE_CODE_ARRAY:
2454 case TYPE_CODE_STRUCT:
2455 case TYPE_CODE_UNION:
2456 case TYPE_CODE_SET:
2457 case TYPE_CODE_STRING:
e09342b5
TJB
2458 return 0;
2459 default:
2460 return 1;
2461 }
2462}
2463
2464/* Return true if T is scalar, or a composite type which in practice has
90e4670f
TJB
2465 the memory layout of a scalar type. E.g., an array or struct with only
2466 one scalar element inside it, or a union with only scalar elements. */
e09342b5
TJB
2467
2468int
2469is_scalar_type_recursive (struct type *t)
2470{
2471 CHECK_TYPEDEF (t);
2472
2473 if (is_scalar_type (t))
2474 return 1;
2475 /* Are we dealing with an array or string of known dimensions? */
2476 else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2477 || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2478 && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2479 {
2480 LONGEST low_bound, high_bound;
2481 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2482
2483 get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2484
2485 return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2486 }
2487 /* Are we dealing with a struct with one element? */
2488 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2489 return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2490 else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2491 {
2492 int i, n = TYPE_NFIELDS (t);
2493
2494 /* If all elements of the union are scalar, then the union is scalar. */
2495 for (i = 0; i < n; i++)
2496 if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2497 return 0;
2498
2499 return 1;
2500 }
2501
2502 return 0;
2503}
2504
4e8f195d
TT
2505/* A helper function which returns true if types A and B represent the
2506 "same" class type. This is true if the types have the same main
2507 type, or the same name. */
2508
2509int
2510class_types_same_p (const struct type *a, const struct type *b)
2511{
2512 return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2513 || (TYPE_NAME (a) && TYPE_NAME (b)
2514 && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2515}
2516
a9d5ef47
SW
2517/* If BASE is an ancestor of DCLASS return the distance between them.
2518 otherwise return -1;
2519 eg:
2520
2521 class A {};
2522 class B: public A {};
2523 class C: public B {};
2524 class D: C {};
2525
2526 distance_to_ancestor (A, A, 0) = 0
2527 distance_to_ancestor (A, B, 0) = 1
2528 distance_to_ancestor (A, C, 0) = 2
2529 distance_to_ancestor (A, D, 0) = 3
2530
2531 If PUBLIC is 1 then only public ancestors are considered,
2532 and the function returns the distance only if BASE is a public ancestor
2533 of DCLASS.
2534 Eg:
2535
0963b4bd 2536 distance_to_ancestor (A, D, 1) = -1. */
c906108c 2537
0526b37a 2538static int
a9d5ef47 2539distance_to_ancestor (struct type *base, struct type *dclass, int public)
c906108c
SS
2540{
2541 int i;
a9d5ef47 2542 int d;
c5aa993b 2543
c906108c
SS
2544 CHECK_TYPEDEF (base);
2545 CHECK_TYPEDEF (dclass);
2546
4e8f195d 2547 if (class_types_same_p (base, dclass))
a9d5ef47 2548 return 0;
c906108c
SS
2549
2550 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
4e8f195d 2551 {
0526b37a
SW
2552 if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2553 continue;
2554
a9d5ef47
SW
2555 d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2556 if (d >= 0)
2557 return 1 + d;
4e8f195d 2558 }
c906108c 2559
a9d5ef47 2560 return -1;
c906108c 2561}
4e8f195d 2562
0526b37a
SW
2563/* Check whether BASE is an ancestor or base class or DCLASS
2564 Return 1 if so, and 0 if not.
2565 Note: If BASE and DCLASS are of the same type, this function
2566 will return 1. So for some class A, is_ancestor (A, A) will
2567 return 1. */
2568
2569int
2570is_ancestor (struct type *base, struct type *dclass)
2571{
a9d5ef47 2572 return distance_to_ancestor (base, dclass, 0) >= 0;
0526b37a
SW
2573}
2574
4e8f195d
TT
2575/* Like is_ancestor, but only returns true when BASE is a public
2576 ancestor of DCLASS. */
2577
2578int
2579is_public_ancestor (struct type *base, struct type *dclass)
2580{
a9d5ef47 2581 return distance_to_ancestor (base, dclass, 1) >= 0;
4e8f195d
TT
2582}
2583
2584/* A helper function for is_unique_ancestor. */
2585
2586static int
2587is_unique_ancestor_worker (struct type *base, struct type *dclass,
2588 int *offset,
8af8e3bc
PA
2589 const gdb_byte *valaddr, int embedded_offset,
2590 CORE_ADDR address, struct value *val)
4e8f195d
TT
2591{
2592 int i, count = 0;
2593
2594 CHECK_TYPEDEF (base);
2595 CHECK_TYPEDEF (dclass);
2596
2597 for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2598 {
8af8e3bc
PA
2599 struct type *iter;
2600 int this_offset;
4e8f195d 2601
8af8e3bc
PA
2602 iter = check_typedef (TYPE_BASECLASS (dclass, i));
2603
2604 this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2605 address, val);
4e8f195d
TT
2606
2607 if (class_types_same_p (base, iter))
2608 {
2609 /* If this is the first subclass, set *OFFSET and set count
2610 to 1. Otherwise, if this is at the same offset as
2611 previous instances, do nothing. Otherwise, increment
2612 count. */
2613 if (*offset == -1)
2614 {
2615 *offset = this_offset;
2616 count = 1;
2617 }
2618 else if (this_offset == *offset)
2619 {
2620 /* Nothing. */
2621 }
2622 else
2623 ++count;
2624 }
2625 else
2626 count += is_unique_ancestor_worker (base, iter, offset,
8af8e3bc
PA
2627 valaddr,
2628 embedded_offset + this_offset,
2629 address, val);
4e8f195d
TT
2630 }
2631
2632 return count;
2633}
2634
2635/* Like is_ancestor, but only returns true if BASE is a unique base
2636 class of the type of VAL. */
2637
2638int
2639is_unique_ancestor (struct type *base, struct value *val)
2640{
2641 int offset = -1;
2642
2643 return is_unique_ancestor_worker (base, value_type (val), &offset,
8af8e3bc
PA
2644 value_contents_for_printing (val),
2645 value_embedded_offset (val),
2646 value_address (val), val) == 1;
4e8f195d
TT
2647}
2648
c906108c 2649\f
5212577a 2650/* Overload resolution. */
c906108c 2651
6403aeea
SW
2652/* Return the sum of the rank of A with the rank of B. */
2653
2654struct rank
2655sum_ranks (struct rank a, struct rank b)
2656{
2657 struct rank c;
2658 c.rank = a.rank + b.rank;
a9d5ef47 2659 c.subrank = a.subrank + b.subrank;
6403aeea
SW
2660 return c;
2661}
2662
2663/* Compare rank A and B and return:
2664 0 if a = b
2665 1 if a is better than b
2666 -1 if b is better than a. */
2667
2668int
2669compare_ranks (struct rank a, struct rank b)
2670{
2671 if (a.rank == b.rank)
a9d5ef47
SW
2672 {
2673 if (a.subrank == b.subrank)
2674 return 0;
2675 if (a.subrank < b.subrank)
2676 return 1;
2677 if (a.subrank > b.subrank)
2678 return -1;
2679 }
6403aeea
SW
2680
2681 if (a.rank < b.rank)
2682 return 1;
2683
0963b4bd 2684 /* a.rank > b.rank */
6403aeea
SW
2685 return -1;
2686}
c5aa993b 2687
0963b4bd 2688/* Functions for overload resolution begin here. */
c906108c
SS
2689
2690/* Compare two badness vectors A and B and return the result.
7ba81444
MS
2691 0 => A and B are identical
2692 1 => A and B are incomparable
2693 2 => A is better than B
2694 3 => A is worse than B */
c906108c
SS
2695
2696int
fba45db2 2697compare_badness (struct badness_vector *a, struct badness_vector *b)
c906108c
SS
2698{
2699 int i;
2700 int tmp;
c5aa993b
JM
2701 short found_pos = 0; /* any positives in c? */
2702 short found_neg = 0; /* any negatives in c? */
2703
2704 /* differing lengths => incomparable */
c906108c
SS
2705 if (a->length != b->length)
2706 return 1;
2707
c5aa993b
JM
2708 /* Subtract b from a */
2709 for (i = 0; i < a->length; i++)
c906108c 2710 {
6403aeea 2711 tmp = compare_ranks (b->rank[i], a->rank[i]);
c906108c 2712 if (tmp > 0)
c5aa993b 2713 found_pos = 1;
c906108c 2714 else if (tmp < 0)
c5aa993b 2715 found_neg = 1;
c906108c
SS
2716 }
2717
2718 if (found_pos)
2719 {
2720 if (found_neg)
c5aa993b 2721 return 1; /* incomparable */
c906108c 2722 else
c5aa993b 2723 return 3; /* A > B */
c906108c 2724 }
c5aa993b
JM
2725 else
2726 /* no positives */
c906108c
SS
2727 {
2728 if (found_neg)
c5aa993b 2729 return 2; /* A < B */
c906108c 2730 else
c5aa993b 2731 return 0; /* A == B */
c906108c
SS
2732 }
2733}
2734
7ba81444
MS
2735/* Rank a function by comparing its parameter types (PARMS, length
2736 NPARMS), to the types of an argument list (ARGS, length NARGS).
2737 Return a pointer to a badness vector. This has NARGS + 1
2738 entries. */
c906108c
SS
2739
2740struct badness_vector *
7ba81444 2741rank_function (struct type **parms, int nparms,
da096638 2742 struct value **args, int nargs)
c906108c
SS
2743{
2744 int i;
c5aa993b 2745 struct badness_vector *bv;
c906108c
SS
2746 int min_len = nparms < nargs ? nparms : nargs;
2747
2748 bv = xmalloc (sizeof (struct badness_vector));
0963b4bd 2749 bv->length = nargs + 1; /* add 1 for the length-match rank. */
c4e54771 2750 bv->rank = XNEWVEC (struct rank, nargs + 1);
c906108c
SS
2751
2752 /* First compare the lengths of the supplied lists.
7ba81444 2753 If there is a mismatch, set it to a high value. */
c5aa993b 2754
c906108c 2755 /* pai/1997-06-03 FIXME: when we have debug info about default
7ba81444
MS
2756 arguments and ellipsis parameter lists, we should consider those
2757 and rank the length-match more finely. */
c906108c 2758
6403aeea
SW
2759 LENGTH_MATCH (bv) = (nargs != nparms)
2760 ? LENGTH_MISMATCH_BADNESS
2761 : EXACT_MATCH_BADNESS;
c906108c 2762
0963b4bd 2763 /* Now rank all the parameters of the candidate function. */
74cc24b0 2764 for (i = 1; i <= min_len; i++)
da096638
KS
2765 bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2766 args[i - 1]);
c906108c 2767
0963b4bd 2768 /* If more arguments than parameters, add dummy entries. */
c5aa993b 2769 for (i = min_len + 1; i <= nargs; i++)
c906108c
SS
2770 bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2771
2772 return bv;
2773}
2774
973ccf8b
DJ
2775/* Compare the names of two integer types, assuming that any sign
2776 qualifiers have been checked already. We do it this way because
2777 there may be an "int" in the name of one of the types. */
2778
2779static int
2780integer_types_same_name_p (const char *first, const char *second)
2781{
2782 int first_p, second_p;
2783
7ba81444
MS
2784 /* If both are shorts, return 1; if neither is a short, keep
2785 checking. */
973ccf8b
DJ
2786 first_p = (strstr (first, "short") != NULL);
2787 second_p = (strstr (second, "short") != NULL);
2788 if (first_p && second_p)
2789 return 1;
2790 if (first_p || second_p)
2791 return 0;
2792
2793 /* Likewise for long. */
2794 first_p = (strstr (first, "long") != NULL);
2795 second_p = (strstr (second, "long") != NULL);
2796 if (first_p && second_p)
2797 return 1;
2798 if (first_p || second_p)
2799 return 0;
2800
2801 /* Likewise for char. */
2802 first_p = (strstr (first, "char") != NULL);
2803 second_p = (strstr (second, "char") != NULL);
2804 if (first_p && second_p)
2805 return 1;
2806 if (first_p || second_p)
2807 return 0;
2808
2809 /* They must both be ints. */
2810 return 1;
2811}
2812
7062b0a0
SW
2813/* Compares type A to type B returns 1 if the represent the same type
2814 0 otherwise. */
2815
bd69fc68 2816int
7062b0a0
SW
2817types_equal (struct type *a, struct type *b)
2818{
2819 /* Identical type pointers. */
2820 /* However, this still doesn't catch all cases of same type for b
2821 and a. The reason is that builtin types are different from
2822 the same ones constructed from the object. */
2823 if (a == b)
2824 return 1;
2825
2826 /* Resolve typedefs */
2827 if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2828 a = check_typedef (a);
2829 if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2830 b = check_typedef (b);
2831
2832 /* If after resolving typedefs a and b are not of the same type
2833 code then they are not equal. */
2834 if (TYPE_CODE (a) != TYPE_CODE (b))
2835 return 0;
2836
2837 /* If a and b are both pointers types or both reference types then
2838 they are equal of the same type iff the objects they refer to are
2839 of the same type. */
2840 if (TYPE_CODE (a) == TYPE_CODE_PTR
2841 || TYPE_CODE (a) == TYPE_CODE_REF)
2842 return types_equal (TYPE_TARGET_TYPE (a),
2843 TYPE_TARGET_TYPE (b));
2844
0963b4bd 2845 /* Well, damnit, if the names are exactly the same, I'll say they
7062b0a0
SW
2846 are exactly the same. This happens when we generate method
2847 stubs. The types won't point to the same address, but they
0963b4bd 2848 really are the same. */
7062b0a0
SW
2849
2850 if (TYPE_NAME (a) && TYPE_NAME (b)
2851 && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2852 return 1;
2853
2854 /* Check if identical after resolving typedefs. */
2855 if (a == b)
2856 return 1;
2857
9ce98649
TT
2858 /* Two function types are equal if their argument and return types
2859 are equal. */
2860 if (TYPE_CODE (a) == TYPE_CODE_FUNC)
2861 {
2862 int i;
2863
2864 if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
2865 return 0;
2866
2867 if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
2868 return 0;
2869
2870 for (i = 0; i < TYPE_NFIELDS (a); ++i)
2871 if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
2872 return 0;
2873
2874 return 1;
2875 }
2876
7062b0a0
SW
2877 return 0;
2878}
ca092b61
DE
2879\f
2880/* Deep comparison of types. */
2881
2882/* An entry in the type-equality bcache. */
2883
2884typedef struct type_equality_entry
2885{
2886 struct type *type1, *type2;
2887} type_equality_entry_d;
2888
2889DEF_VEC_O (type_equality_entry_d);
2890
2891/* A helper function to compare two strings. Returns 1 if they are
2892 the same, 0 otherwise. Handles NULLs properly. */
2893
2894static int
2895compare_maybe_null_strings (const char *s, const char *t)
2896{
2897 if (s == NULL && t != NULL)
2898 return 0;
2899 else if (s != NULL && t == NULL)
2900 return 0;
2901 else if (s == NULL && t== NULL)
2902 return 1;
2903 return strcmp (s, t) == 0;
2904}
2905
2906/* A helper function for check_types_worklist that checks two types for
2907 "deep" equality. Returns non-zero if the types are considered the
2908 same, zero otherwise. */
2909
2910static int
2911check_types_equal (struct type *type1, struct type *type2,
2912 VEC (type_equality_entry_d) **worklist)
2913{
2914 CHECK_TYPEDEF (type1);
2915 CHECK_TYPEDEF (type2);
2916
2917 if (type1 == type2)
2918 return 1;
2919
2920 if (TYPE_CODE (type1) != TYPE_CODE (type2)
2921 || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
2922 || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
2923 || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
2924 || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
2925 || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
2926 || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
2927 || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
2928 || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
2929 return 0;
2930
2931 if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
2932 TYPE_TAG_NAME (type2)))
2933 return 0;
2934 if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
2935 return 0;
2936
2937 if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
2938 {
2939 if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
2940 sizeof (*TYPE_RANGE_DATA (type1))) != 0)
2941 return 0;
2942 }
2943 else
2944 {
2945 int i;
2946
2947 for (i = 0; i < TYPE_NFIELDS (type1); ++i)
2948 {
2949 const struct field *field1 = &TYPE_FIELD (type1, i);
2950 const struct field *field2 = &TYPE_FIELD (type2, i);
2951 struct type_equality_entry entry;
2952
2953 if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
2954 || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
2955 || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
2956 return 0;
2957 if (!compare_maybe_null_strings (FIELD_NAME (*field1),
2958 FIELD_NAME (*field2)))
2959 return 0;
2960 switch (FIELD_LOC_KIND (*field1))
2961 {
2962 case FIELD_LOC_KIND_BITPOS:
2963 if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
2964 return 0;
2965 break;
2966 case FIELD_LOC_KIND_ENUMVAL:
2967 if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
2968 return 0;
2969 break;
2970 case FIELD_LOC_KIND_PHYSADDR:
2971 if (FIELD_STATIC_PHYSADDR (*field1)
2972 != FIELD_STATIC_PHYSADDR (*field2))
2973 return 0;
2974 break;
2975 case FIELD_LOC_KIND_PHYSNAME:
2976 if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
2977 FIELD_STATIC_PHYSNAME (*field2)))
2978 return 0;
2979 break;
2980 case FIELD_LOC_KIND_DWARF_BLOCK:
2981 {
2982 struct dwarf2_locexpr_baton *block1, *block2;
2983
2984 block1 = FIELD_DWARF_BLOCK (*field1);
2985 block2 = FIELD_DWARF_BLOCK (*field2);
2986 if (block1->per_cu != block2->per_cu
2987 || block1->size != block2->size
2988 || memcmp (block1->data, block2->data, block1->size) != 0)
2989 return 0;
2990 }
2991 break;
2992 default:
2993 internal_error (__FILE__, __LINE__, _("Unsupported field kind "
2994 "%d by check_types_equal"),
2995 FIELD_LOC_KIND (*field1));
2996 }
2997
2998 entry.type1 = FIELD_TYPE (*field1);
2999 entry.type2 = FIELD_TYPE (*field2);
3000 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3001 }
3002 }
3003
3004 if (TYPE_TARGET_TYPE (type1) != NULL)
3005 {
3006 struct type_equality_entry entry;
3007
3008 if (TYPE_TARGET_TYPE (type2) == NULL)
3009 return 0;
3010
3011 entry.type1 = TYPE_TARGET_TYPE (type1);
3012 entry.type2 = TYPE_TARGET_TYPE (type2);
3013 VEC_safe_push (type_equality_entry_d, *worklist, &entry);
3014 }
3015 else if (TYPE_TARGET_TYPE (type2) != NULL)
3016 return 0;
3017
3018 return 1;
3019}
3020
3021/* Check types on a worklist for equality. Returns zero if any pair
3022 is not equal, non-zero if they are all considered equal. */
3023
3024static int
3025check_types_worklist (VEC (type_equality_entry_d) **worklist,
3026 struct bcache *cache)
3027{
3028 while (!VEC_empty (type_equality_entry_d, *worklist))
3029 {
3030 struct type_equality_entry entry;
3031 int added;
3032
3033 entry = *VEC_last (type_equality_entry_d, *worklist);
3034 VEC_pop (type_equality_entry_d, *worklist);
3035
3036 /* If the type pair has already been visited, we know it is
3037 ok. */
3038 bcache_full (&entry, sizeof (entry), cache, &added);
3039 if (!added)
3040 continue;
3041
3042 if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
3043 return 0;
3044 }
7062b0a0 3045
ca092b61
DE
3046 return 1;
3047}
3048
3049/* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
3050 "deep comparison". Otherwise return zero. */
3051
3052int
3053types_deeply_equal (struct type *type1, struct type *type2)
3054{
3055 volatile struct gdb_exception except;
3056 int result = 0;
3057 struct bcache *cache;
3058 VEC (type_equality_entry_d) *worklist = NULL;
3059 struct type_equality_entry entry;
3060
3061 gdb_assert (type1 != NULL && type2 != NULL);
3062
3063 /* Early exit for the simple case. */
3064 if (type1 == type2)
3065 return 1;
3066
3067 cache = bcache_xmalloc (NULL, NULL);
3068
3069 entry.type1 = type1;
3070 entry.type2 = type2;
3071 VEC_safe_push (type_equality_entry_d, worklist, &entry);
3072
3073 TRY_CATCH (except, RETURN_MASK_ALL)
3074 {
3075 result = check_types_worklist (&worklist, cache);
3076 }
3077 /* check_types_worklist calls several nested helper functions,
3078 some of which can raise a GDB Exception, so we just check
3079 and rethrow here. If there is a GDB exception, a comparison
3080 is not capable (or trusted), so exit. */
3081 bcache_xfree (cache);
3082 VEC_free (type_equality_entry_d, worklist);
3083 /* Rethrow if there was a problem. */
3084 if (except.reason < 0)
3085 throw_exception (except);
3086
3087 return result;
3088}
3089\f
c906108c
SS
3090/* Compare one type (PARM) for compatibility with another (ARG).
3091 * PARM is intended to be the parameter type of a function; and
3092 * ARG is the supplied argument's type. This function tests if
3093 * the latter can be converted to the former.
da096638 3094 * VALUE is the argument's value or NULL if none (or called recursively)
c906108c
SS
3095 *
3096 * Return 0 if they are identical types;
3097 * Otherwise, return an integer which corresponds to how compatible
7ba81444
MS
3098 * PARM is to ARG. The higher the return value, the worse the match.
3099 * Generally the "bad" conversions are all uniformly assigned a 100. */
c906108c 3100
6403aeea 3101struct rank
da096638 3102rank_one_type (struct type *parm, struct type *arg, struct value *value)
c906108c 3103{
a9d5ef47 3104 struct rank rank = {0,0};
7062b0a0
SW
3105
3106 if (types_equal (parm, arg))
6403aeea 3107 return EXACT_MATCH_BADNESS;
c906108c
SS
3108
3109 /* Resolve typedefs */
3110 if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
3111 parm = check_typedef (parm);
3112 if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
3113 arg = check_typedef (arg);
3114
db577aea 3115 /* See through references, since we can almost make non-references
7ba81444 3116 references. */
db577aea 3117 if (TYPE_CODE (arg) == TYPE_CODE_REF)
da096638 3118 return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
6403aeea 3119 REFERENCE_CONVERSION_BADNESS));
db577aea 3120 if (TYPE_CODE (parm) == TYPE_CODE_REF)
da096638 3121 return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
6403aeea 3122 REFERENCE_CONVERSION_BADNESS));
5d161b24 3123 if (overload_debug)
7ba81444
MS
3124 /* Debugging only. */
3125 fprintf_filtered (gdb_stderr,
3126 "------ Arg is %s [%d], parm is %s [%d]\n",
3127 TYPE_NAME (arg), TYPE_CODE (arg),
3128 TYPE_NAME (parm), TYPE_CODE (parm));
c906108c 3129
0963b4bd 3130 /* x -> y means arg of type x being supplied for parameter of type y. */
c906108c
SS
3131
3132 switch (TYPE_CODE (parm))
3133 {
c5aa993b
JM
3134 case TYPE_CODE_PTR:
3135 switch (TYPE_CODE (arg))
3136 {
3137 case TYPE_CODE_PTR:
7062b0a0
SW
3138
3139 /* Allowed pointer conversions are:
3140 (a) pointer to void-pointer conversion. */
3141 if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
c5aa993b 3142 return VOID_PTR_CONVERSION_BADNESS;
7062b0a0
SW
3143
3144 /* (b) pointer to ancestor-pointer conversion. */
a9d5ef47
SW
3145 rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
3146 TYPE_TARGET_TYPE (arg),
3147 0);
3148 if (rank.subrank >= 0)
3149 return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
7062b0a0
SW
3150
3151 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b 3152 case TYPE_CODE_ARRAY:
7062b0a0
SW
3153 if (types_equal (TYPE_TARGET_TYPE (parm),
3154 TYPE_TARGET_TYPE (arg)))
6403aeea 3155 return EXACT_MATCH_BADNESS;
7062b0a0 3156 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b 3157 case TYPE_CODE_FUNC:
da096638 3158 return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
c5aa993b 3159 case TYPE_CODE_INT:
a451cb65 3160 if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
da096638 3161 {
a451cb65
KS
3162 if (value_as_long (value) == 0)
3163 {
3164 /* Null pointer conversion: allow it to be cast to a pointer.
3165 [4.10.1 of C++ standard draft n3290] */
3166 return NULL_POINTER_CONVERSION_BADNESS;
3167 }
3168 else
3169 {
3170 /* If type checking is disabled, allow the conversion. */
3171 if (!strict_type_checking)
3172 return NS_INTEGER_POINTER_CONVERSION_BADNESS;
3173 }
da096638
KS
3174 }
3175 /* fall through */
c5aa993b 3176 case TYPE_CODE_ENUM:
4f2aea11 3177 case TYPE_CODE_FLAGS:
c5aa993b
JM
3178 case TYPE_CODE_CHAR:
3179 case TYPE_CODE_RANGE:
3180 case TYPE_CODE_BOOL:
c5aa993b
JM
3181 default:
3182 return INCOMPATIBLE_TYPE_BADNESS;
3183 }
3184 case TYPE_CODE_ARRAY:
3185 switch (TYPE_CODE (arg))
3186 {
3187 case TYPE_CODE_PTR:
3188 case TYPE_CODE_ARRAY:
7ba81444 3189 return rank_one_type (TYPE_TARGET_TYPE (parm),
da096638 3190 TYPE_TARGET_TYPE (arg), NULL);
c5aa993b
JM
3191 default:
3192 return INCOMPATIBLE_TYPE_BADNESS;
3193 }
3194 case TYPE_CODE_FUNC:
3195 switch (TYPE_CODE (arg))
3196 {
3197 case TYPE_CODE_PTR: /* funcptr -> func */
da096638 3198 return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
c5aa993b
JM
3199 default:
3200 return INCOMPATIBLE_TYPE_BADNESS;
3201 }
3202 case TYPE_CODE_INT:
3203 switch (TYPE_CODE (arg))
3204 {
3205 case TYPE_CODE_INT:
3206 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3207 {
3208 /* Deal with signed, unsigned, and plain chars and
7ba81444 3209 signed and unsigned ints. */
c5aa993b
JM
3210 if (TYPE_NOSIGN (parm))
3211 {
0963b4bd 3212 /* This case only for character types. */
7ba81444 3213 if (TYPE_NOSIGN (arg))
6403aeea 3214 return EXACT_MATCH_BADNESS; /* plain char -> plain char */
7ba81444
MS
3215 else /* signed/unsigned char -> plain char */
3216 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3217 }
3218 else if (TYPE_UNSIGNED (parm))
3219 {
3220 if (TYPE_UNSIGNED (arg))
3221 {
7ba81444
MS
3222 /* unsigned int -> unsigned int, or
3223 unsigned long -> unsigned long */
3224 if (integer_types_same_name_p (TYPE_NAME (parm),
3225 TYPE_NAME (arg)))
6403aeea 3226 return EXACT_MATCH_BADNESS;
7ba81444
MS
3227 else if (integer_types_same_name_p (TYPE_NAME (arg),
3228 "int")
3229 && integer_types_same_name_p (TYPE_NAME (parm),
3230 "long"))
3e43a32a
MS
3231 /* unsigned int -> unsigned long */
3232 return INTEGER_PROMOTION_BADNESS;
c5aa993b 3233 else
3e43a32a
MS
3234 /* unsigned long -> unsigned int */
3235 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3236 }
3237 else
3238 {
7ba81444
MS
3239 if (integer_types_same_name_p (TYPE_NAME (arg),
3240 "long")
3241 && integer_types_same_name_p (TYPE_NAME (parm),
3242 "int"))
3e43a32a
MS
3243 /* signed long -> unsigned int */
3244 return INTEGER_CONVERSION_BADNESS;
c5aa993b 3245 else
3e43a32a
MS
3246 /* signed int/long -> unsigned int/long */
3247 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3248 }
3249 }
3250 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3251 {
7ba81444
MS
3252 if (integer_types_same_name_p (TYPE_NAME (parm),
3253 TYPE_NAME (arg)))
6403aeea 3254 return EXACT_MATCH_BADNESS;
7ba81444
MS
3255 else if (integer_types_same_name_p (TYPE_NAME (arg),
3256 "int")
3257 && integer_types_same_name_p (TYPE_NAME (parm),
3258 "long"))
c5aa993b
JM
3259 return INTEGER_PROMOTION_BADNESS;
3260 else
1c5cb38e 3261 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3262 }
3263 else
1c5cb38e 3264 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3265 }
3266 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3267 return INTEGER_PROMOTION_BADNESS;
3268 else
1c5cb38e 3269 return INTEGER_CONVERSION_BADNESS;
c5aa993b 3270 case TYPE_CODE_ENUM:
4f2aea11 3271 case TYPE_CODE_FLAGS:
c5aa993b
JM
3272 case TYPE_CODE_CHAR:
3273 case TYPE_CODE_RANGE:
3274 case TYPE_CODE_BOOL:
3d567982
TT
3275 if (TYPE_DECLARED_CLASS (arg))
3276 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b
JM
3277 return INTEGER_PROMOTION_BADNESS;
3278 case TYPE_CODE_FLT:
3279 return INT_FLOAT_CONVERSION_BADNESS;
3280 case TYPE_CODE_PTR:
3281 return NS_POINTER_CONVERSION_BADNESS;
3282 default:
3283 return INCOMPATIBLE_TYPE_BADNESS;
3284 }
3285 break;
3286 case TYPE_CODE_ENUM:
3287 switch (TYPE_CODE (arg))
3288 {
3289 case TYPE_CODE_INT:
3290 case TYPE_CODE_CHAR:
3291 case TYPE_CODE_RANGE:
3292 case TYPE_CODE_BOOL:
3293 case TYPE_CODE_ENUM:
3d567982
TT
3294 if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg))
3295 return INCOMPATIBLE_TYPE_BADNESS;
1c5cb38e 3296 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3297 case TYPE_CODE_FLT:
3298 return INT_FLOAT_CONVERSION_BADNESS;
3299 default:
3300 return INCOMPATIBLE_TYPE_BADNESS;
3301 }
3302 break;
3303 case TYPE_CODE_CHAR:
3304 switch (TYPE_CODE (arg))
3305 {
3306 case TYPE_CODE_RANGE:
3307 case TYPE_CODE_BOOL:
3308 case TYPE_CODE_ENUM:
3d567982
TT
3309 if (TYPE_DECLARED_CLASS (arg))
3310 return INCOMPATIBLE_TYPE_BADNESS;
1c5cb38e 3311 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3312 case TYPE_CODE_FLT:
3313 return INT_FLOAT_CONVERSION_BADNESS;
3314 case TYPE_CODE_INT:
3315 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
1c5cb38e 3316 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3317 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3318 return INTEGER_PROMOTION_BADNESS;
3319 /* >>> !! else fall through !! <<< */
3320 case TYPE_CODE_CHAR:
7ba81444
MS
3321 /* Deal with signed, unsigned, and plain chars for C++ and
3322 with int cases falling through from previous case. */
c5aa993b
JM
3323 if (TYPE_NOSIGN (parm))
3324 {
3325 if (TYPE_NOSIGN (arg))
6403aeea 3326 return EXACT_MATCH_BADNESS;
c5aa993b 3327 else
1c5cb38e 3328 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3329 }
3330 else if (TYPE_UNSIGNED (parm))
3331 {
3332 if (TYPE_UNSIGNED (arg))
6403aeea 3333 return EXACT_MATCH_BADNESS;
c5aa993b
JM
3334 else
3335 return INTEGER_PROMOTION_BADNESS;
3336 }
3337 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
6403aeea 3338 return EXACT_MATCH_BADNESS;
c5aa993b 3339 else
1c5cb38e 3340 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3341 default:
3342 return INCOMPATIBLE_TYPE_BADNESS;
3343 }
3344 break;
3345 case TYPE_CODE_RANGE:
3346 switch (TYPE_CODE (arg))
3347 {
3348 case TYPE_CODE_INT:
3349 case TYPE_CODE_CHAR:
3350 case TYPE_CODE_RANGE:
3351 case TYPE_CODE_BOOL:
3352 case TYPE_CODE_ENUM:
1c5cb38e 3353 return INTEGER_CONVERSION_BADNESS;
c5aa993b
JM
3354 case TYPE_CODE_FLT:
3355 return INT_FLOAT_CONVERSION_BADNESS;
3356 default:
3357 return INCOMPATIBLE_TYPE_BADNESS;
3358 }
3359 break;
3360 case TYPE_CODE_BOOL:
3361 switch (TYPE_CODE (arg))
3362 {
5b4f6e25
KS
3363 /* n3290 draft, section 4.12.1 (conv.bool):
3364
3365 "A prvalue of arithmetic, unscoped enumeration, pointer, or
3366 pointer to member type can be converted to a prvalue of type
3367 bool. A zero value, null pointer value, or null member pointer
3368 value is converted to false; any other value is converted to
3369 true. A prvalue of type std::nullptr_t can be converted to a
3370 prvalue of type bool; the resulting value is false." */
c5aa993b
JM
3371 case TYPE_CODE_INT:
3372 case TYPE_CODE_CHAR:
c5aa993b
JM
3373 case TYPE_CODE_ENUM:
3374 case TYPE_CODE_FLT:
5b4f6e25 3375 case TYPE_CODE_MEMBERPTR:
c5aa993b 3376 case TYPE_CODE_PTR:
5b4f6e25
KS
3377 return BOOL_CONVERSION_BADNESS;
3378 case TYPE_CODE_RANGE:
3379 return INCOMPATIBLE_TYPE_BADNESS;
c5aa993b 3380 case TYPE_CODE_BOOL:
6403aeea 3381 return EXACT_MATCH_BADNESS;
c5aa993b
JM
3382 default:
3383 return INCOMPATIBLE_TYPE_BADNESS;
3384 }
3385 break;
3386 case TYPE_CODE_FLT:
3387 switch (TYPE_CODE (arg))
3388 {
3389 case TYPE_CODE_FLT:
3390 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3391 return FLOAT_PROMOTION_BADNESS;
3392 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
6403aeea 3393 return EXACT_MATCH_BADNESS;
c5aa993b
JM
3394 else
3395 return FLOAT_CONVERSION_BADNESS;
3396 case TYPE_CODE_INT:
3397 case TYPE_CODE_BOOL:
3398 case TYPE_CODE_ENUM:
3399 case TYPE_CODE_RANGE:
3400 case TYPE_CODE_CHAR:
3401 return INT_FLOAT_CONVERSION_BADNESS;
3402 default:
3403 return INCOMPATIBLE_TYPE_BADNESS;
3404 }
3405 break;
3406 case TYPE_CODE_COMPLEX:
3407 switch (TYPE_CODE (arg))
7ba81444 3408 { /* Strictly not needed for C++, but... */
c5aa993b
JM
3409 case TYPE_CODE_FLT:
3410 return FLOAT_PROMOTION_BADNESS;
3411 case TYPE_CODE_COMPLEX:
6403aeea 3412 return EXACT_MATCH_BADNESS;
c5aa993b
JM
3413 default:
3414 return INCOMPATIBLE_TYPE_BADNESS;
3415 }
3416 break;
3417 case TYPE_CODE_STRUCT:
0963b4bd 3418 /* currently same as TYPE_CODE_CLASS. */
c5aa993b
JM
3419 switch (TYPE_CODE (arg))
3420 {
3421 case TYPE_CODE_STRUCT:
3422 /* Check for derivation */
a9d5ef47
SW
3423 rank.subrank = distance_to_ancestor (parm, arg, 0);
3424 if (rank.subrank >= 0)
3425 return sum_ranks (BASE_CONVERSION_BADNESS, rank);
c5aa993b
JM
3426 /* else fall through */
3427 default:
3428 return INCOMPATIBLE_TYPE_BADNESS;
3429 }
3430 break;
3431 case TYPE_CODE_UNION:
3432 switch (TYPE_CODE (arg))
3433 {
3434 case TYPE_CODE_UNION:
3435 default:
3436 return INCOMPATIBLE_TYPE_BADNESS;
3437 }
3438 break;
0d5de010 3439 case TYPE_CODE_MEMBERPTR:
c5aa993b
JM
3440 switch (TYPE_CODE (arg))
3441 {
3442 default:
3443 return INCOMPATIBLE_TYPE_BADNESS;
3444 }
3445 break;
3446 case TYPE_CODE_METHOD:
3447 switch (TYPE_CODE (arg))
3448 {
3449
3450 default:
3451 return INCOMPATIBLE_TYPE_BADNESS;
3452 }
3453 break;
3454 case TYPE_CODE_REF:
3455 switch (TYPE_CODE (arg))
3456 {
3457
3458 default:
3459 return INCOMPATIBLE_TYPE_BADNESS;
3460 }
3461
3462 break;
3463 case TYPE_CODE_SET:
3464 switch (TYPE_CODE (arg))
3465 {
3466 /* Not in C++ */
3467 case TYPE_CODE_SET:
7ba81444 3468 return rank_one_type (TYPE_FIELD_TYPE (parm, 0),
da096638 3469 TYPE_FIELD_TYPE (arg, 0), NULL);
c5aa993b
JM
3470 default:
3471 return INCOMPATIBLE_TYPE_BADNESS;
3472 }
3473 break;
3474 case TYPE_CODE_VOID:
3475 default:
3476 return INCOMPATIBLE_TYPE_BADNESS;
3477 } /* switch (TYPE_CODE (arg)) */
c906108c
SS
3478}
3479
0963b4bd 3480/* End of functions for overload resolution. */
5212577a
DE
3481\f
3482/* Routines to pretty-print types. */
c906108c 3483
c906108c 3484static void
fba45db2 3485print_bit_vector (B_TYPE *bits, int nbits)
c906108c
SS
3486{
3487 int bitno;
3488
3489 for (bitno = 0; bitno < nbits; bitno++)
3490 {
3491 if ((bitno % 8) == 0)
3492 {
3493 puts_filtered (" ");
3494 }
3495 if (B_TST (bits, bitno))
a3f17187 3496 printf_filtered (("1"));
c906108c 3497 else
a3f17187 3498 printf_filtered (("0"));
c906108c
SS
3499 }
3500}
3501
ad2f7632 3502/* Note the first arg should be the "this" pointer, we may not want to
7ba81444
MS
3503 include it since we may get into a infinitely recursive
3504 situation. */
c906108c
SS
3505
3506static void
ad2f7632 3507print_arg_types (struct field *args, int nargs, int spaces)
c906108c
SS
3508{
3509 if (args != NULL)
3510 {
ad2f7632
DJ
3511 int i;
3512
3513 for (i = 0; i < nargs; i++)
3514 recursive_dump_type (args[i].type, spaces + 2);
c906108c
SS
3515 }
3516}
3517
d6a843b5
JK
3518int
3519field_is_static (struct field *f)
3520{
3521 /* "static" fields are the fields whose location is not relative
3522 to the address of the enclosing struct. It would be nice to
3523 have a dedicated flag that would be set for static fields when
3524 the type is being created. But in practice, checking the field
254e6b9e 3525 loc_kind should give us an accurate answer. */
d6a843b5
JK
3526 return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3527 || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3528}
3529
c906108c 3530static void
fba45db2 3531dump_fn_fieldlists (struct type *type, int spaces)
c906108c
SS
3532{
3533 int method_idx;
3534 int overload_idx;
3535 struct fn_field *f;
3536
3537 printfi_filtered (spaces, "fn_fieldlists ");
d4f3574e 3538 gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
c906108c
SS
3539 printf_filtered ("\n");
3540 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3541 {
3542 f = TYPE_FN_FIELDLIST1 (type, method_idx);
3543 printfi_filtered (spaces + 2, "[%d] name '%s' (",
3544 method_idx,
3545 TYPE_FN_FIELDLIST_NAME (type, method_idx));
d4f3574e
SS
3546 gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3547 gdb_stdout);
a3f17187 3548 printf_filtered (_(") length %d\n"),
c906108c
SS
3549 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3550 for (overload_idx = 0;
3551 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3552 overload_idx++)
3553 {
3554 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3555 overload_idx,
3556 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
d4f3574e
SS
3557 gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3558 gdb_stdout);
c906108c
SS
3559 printf_filtered (")\n");
3560 printfi_filtered (spaces + 8, "type ");
7ba81444
MS
3561 gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx),
3562 gdb_stdout);
c906108c
SS
3563 printf_filtered ("\n");
3564
3565 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3566 spaces + 8 + 2);
3567
3568 printfi_filtered (spaces + 8, "args ");
7ba81444
MS
3569 gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx),
3570 gdb_stdout);
c906108c
SS
3571 printf_filtered ("\n");
3572
ad2f7632 3573 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
7ba81444
MS
3574 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f,
3575 overload_idx)),
ad2f7632 3576 spaces);
c906108c 3577 printfi_filtered (spaces + 8, "fcontext ");
d4f3574e
SS
3578 gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3579 gdb_stdout);
c906108c
SS
3580 printf_filtered ("\n");
3581
3582 printfi_filtered (spaces + 8, "is_const %d\n",
3583 TYPE_FN_FIELD_CONST (f, overload_idx));
3584 printfi_filtered (spaces + 8, "is_volatile %d\n",
3585 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3586 printfi_filtered (spaces + 8, "is_private %d\n",
3587 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3588 printfi_filtered (spaces + 8, "is_protected %d\n",
3589 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3590 printfi_filtered (spaces + 8, "is_stub %d\n",
3591 TYPE_FN_FIELD_STUB (f, overload_idx));
3592 printfi_filtered (spaces + 8, "voffset %u\n",
3593 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3594 }
3595 }
3596}
3597
3598static void
fba45db2 3599print_cplus_stuff (struct type *type, int spaces)
c906108c
SS
3600{
3601 printfi_filtered (spaces, "n_baseclasses %d\n",
3602 TYPE_N_BASECLASSES (type));
3603 printfi_filtered (spaces, "nfn_fields %d\n",
3604 TYPE_NFN_FIELDS (type));
c906108c
SS
3605 if (TYPE_N_BASECLASSES (type) > 0)
3606 {
3607 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3608 TYPE_N_BASECLASSES (type));
7ba81444
MS
3609 gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type),
3610 gdb_stdout);
c906108c
SS
3611 printf_filtered (")");
3612
3613 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3614 TYPE_N_BASECLASSES (type));
3615 puts_filtered ("\n");
3616 }
3617 if (TYPE_NFIELDS (type) > 0)
3618 {
3619 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3620 {
7ba81444
MS
3621 printfi_filtered (spaces,
3622 "private_field_bits (%d bits at *",
c906108c 3623 TYPE_NFIELDS (type));
7ba81444
MS
3624 gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type),
3625 gdb_stdout);
c906108c
SS
3626 printf_filtered (")");
3627 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3628 TYPE_NFIELDS (type));
3629 puts_filtered ("\n");
3630 }
3631 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3632 {
7ba81444
MS
3633 printfi_filtered (spaces,
3634 "protected_field_bits (%d bits at *",
c906108c 3635 TYPE_NFIELDS (type));
7ba81444
MS
3636 gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type),
3637 gdb_stdout);
c906108c
SS
3638 printf_filtered (")");
3639 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3640 TYPE_NFIELDS (type));
3641 puts_filtered ("\n");
3642 }
3643 }
3644 if (TYPE_NFN_FIELDS (type) > 0)
3645 {
3646 dump_fn_fieldlists (type, spaces);
3647 }
3648}
3649
b4ba55a1
JB
3650/* Print the contents of the TYPE's type_specific union, assuming that
3651 its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
3652
3653static void
3654print_gnat_stuff (struct type *type, int spaces)
3655{
3656 struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3657
3658 recursive_dump_type (descriptive_type, spaces + 2);
3659}
3660
c906108c
SS
3661static struct obstack dont_print_type_obstack;
3662
3663void
fba45db2 3664recursive_dump_type (struct type *type, int spaces)
c906108c
SS
3665{
3666 int idx;
3667
3668 if (spaces == 0)
3669 obstack_begin (&dont_print_type_obstack, 0);
3670
3671 if (TYPE_NFIELDS (type) > 0
b4ba55a1 3672 || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
c906108c
SS
3673 {
3674 struct type **first_dont_print
7ba81444 3675 = (struct type **) obstack_base (&dont_print_type_obstack);
c906108c 3676
7ba81444
MS
3677 int i = (struct type **)
3678 obstack_next_free (&dont_print_type_obstack) - first_dont_print;
c906108c
SS
3679
3680 while (--i >= 0)
3681 {
3682 if (type == first_dont_print[i])
3683 {
3684 printfi_filtered (spaces, "type node ");
d4f3574e 3685 gdb_print_host_address (type, gdb_stdout);
a3f17187 3686 printf_filtered (_(" <same as already seen type>\n"));
c906108c
SS
3687 return;
3688 }
3689 }
3690
3691 obstack_ptr_grow (&dont_print_type_obstack, type);
3692 }
3693
3694 printfi_filtered (spaces, "type node ");
d4f3574e 3695 gdb_print_host_address (type, gdb_stdout);
c906108c
SS
3696 printf_filtered ("\n");
3697 printfi_filtered (spaces, "name '%s' (",
3698 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
d4f3574e 3699 gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
c906108c 3700 printf_filtered (")\n");
e9e79dd9
FF
3701 printfi_filtered (spaces, "tagname '%s' (",
3702 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
3703 gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
3704 printf_filtered (")\n");
c906108c
SS
3705 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
3706 switch (TYPE_CODE (type))
3707 {
c5aa993b
JM
3708 case TYPE_CODE_UNDEF:
3709 printf_filtered ("(TYPE_CODE_UNDEF)");
3710 break;
3711 case TYPE_CODE_PTR:
3712 printf_filtered ("(TYPE_CODE_PTR)");
3713 break;
3714 case TYPE_CODE_ARRAY:
3715 printf_filtered ("(TYPE_CODE_ARRAY)");
3716 break;
3717 case TYPE_CODE_STRUCT:
3718 printf_filtered ("(TYPE_CODE_STRUCT)");
3719 break;
3720 case TYPE_CODE_UNION:
3721 printf_filtered ("(TYPE_CODE_UNION)");
3722 break;
3723 case TYPE_CODE_ENUM:
3724 printf_filtered ("(TYPE_CODE_ENUM)");
3725 break;
4f2aea11
MK
3726 case TYPE_CODE_FLAGS:
3727 printf_filtered ("(TYPE_CODE_FLAGS)");
3728 break;
c5aa993b
JM
3729 case TYPE_CODE_FUNC:
3730 printf_filtered ("(TYPE_CODE_FUNC)");
3731 break;
3732 case TYPE_CODE_INT:
3733 printf_filtered ("(TYPE_CODE_INT)");
3734 break;
3735 case TYPE_CODE_FLT:
3736 printf_filtered ("(TYPE_CODE_FLT)");
3737 break;
3738 case TYPE_CODE_VOID:
3739 printf_filtered ("(TYPE_CODE_VOID)");
3740 break;
3741 case TYPE_CODE_SET:
3742 printf_filtered ("(TYPE_CODE_SET)");
3743 break;
3744 case TYPE_CODE_RANGE:
3745 printf_filtered ("(TYPE_CODE_RANGE)");
3746 break;
3747 case TYPE_CODE_STRING:
3748 printf_filtered ("(TYPE_CODE_STRING)");
3749 break;
3750 case TYPE_CODE_ERROR:
3751 printf_filtered ("(TYPE_CODE_ERROR)");
3752 break;
0d5de010
DJ
3753 case TYPE_CODE_MEMBERPTR:
3754 printf_filtered ("(TYPE_CODE_MEMBERPTR)");
3755 break;
3756 case TYPE_CODE_METHODPTR:
3757 printf_filtered ("(TYPE_CODE_METHODPTR)");
c5aa993b
JM
3758 break;
3759 case TYPE_CODE_METHOD:
3760 printf_filtered ("(TYPE_CODE_METHOD)");
3761 break;
3762 case TYPE_CODE_REF:
3763 printf_filtered ("(TYPE_CODE_REF)");
3764 break;
3765 case TYPE_CODE_CHAR:
3766 printf_filtered ("(TYPE_CODE_CHAR)");
3767 break;
3768 case TYPE_CODE_BOOL:
3769 printf_filtered ("(TYPE_CODE_BOOL)");
3770 break;
e9e79dd9
FF
3771 case TYPE_CODE_COMPLEX:
3772 printf_filtered ("(TYPE_CODE_COMPLEX)");
3773 break;
c5aa993b
JM
3774 case TYPE_CODE_TYPEDEF:
3775 printf_filtered ("(TYPE_CODE_TYPEDEF)");
3776 break;
5c4e30ca
DC
3777 case TYPE_CODE_NAMESPACE:
3778 printf_filtered ("(TYPE_CODE_NAMESPACE)");
3779 break;
c5aa993b
JM
3780 default:
3781 printf_filtered ("(UNKNOWN TYPE CODE)");
3782 break;
c906108c
SS
3783 }
3784 puts_filtered ("\n");
3785 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
e9bb382b
UW
3786 if (TYPE_OBJFILE_OWNED (type))
3787 {
3788 printfi_filtered (spaces, "objfile ");
3789 gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
3790 }
3791 else
3792 {
3793 printfi_filtered (spaces, "gdbarch ");
3794 gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
3795 }
c906108c
SS
3796 printf_filtered ("\n");
3797 printfi_filtered (spaces, "target_type ");
d4f3574e 3798 gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
c906108c
SS
3799 printf_filtered ("\n");
3800 if (TYPE_TARGET_TYPE (type) != NULL)
3801 {
3802 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3803 }
3804 printfi_filtered (spaces, "pointer_type ");
d4f3574e 3805 gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
c906108c
SS
3806 printf_filtered ("\n");
3807 printfi_filtered (spaces, "reference_type ");
d4f3574e 3808 gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
c906108c 3809 printf_filtered ("\n");
2fdde8f8
DJ
3810 printfi_filtered (spaces, "type_chain ");
3811 gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
e9e79dd9 3812 printf_filtered ("\n");
7ba81444
MS
3813 printfi_filtered (spaces, "instance_flags 0x%x",
3814 TYPE_INSTANCE_FLAGS (type));
2fdde8f8
DJ
3815 if (TYPE_CONST (type))
3816 {
3817 puts_filtered (" TYPE_FLAG_CONST");
3818 }
3819 if (TYPE_VOLATILE (type))
3820 {
3821 puts_filtered (" TYPE_FLAG_VOLATILE");
3822 }
3823 if (TYPE_CODE_SPACE (type))
3824 {
3825 puts_filtered (" TYPE_FLAG_CODE_SPACE");
3826 }
3827 if (TYPE_DATA_SPACE (type))
3828 {
3829 puts_filtered (" TYPE_FLAG_DATA_SPACE");
3830 }
8b2dbe47
KB
3831 if (TYPE_ADDRESS_CLASS_1 (type))
3832 {
3833 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3834 }
3835 if (TYPE_ADDRESS_CLASS_2 (type))
3836 {
3837 puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3838 }
06d66ee9
TT
3839 if (TYPE_RESTRICT (type))
3840 {
3841 puts_filtered (" TYPE_FLAG_RESTRICT");
3842 }
2fdde8f8 3843 puts_filtered ("\n");
876cecd0
TT
3844
3845 printfi_filtered (spaces, "flags");
762a036f 3846 if (TYPE_UNSIGNED (type))
c906108c
SS
3847 {
3848 puts_filtered (" TYPE_FLAG_UNSIGNED");
3849 }
762a036f
FF
3850 if (TYPE_NOSIGN (type))
3851 {
3852 puts_filtered (" TYPE_FLAG_NOSIGN");
3853 }
3854 if (TYPE_STUB (type))
c906108c
SS
3855 {
3856 puts_filtered (" TYPE_FLAG_STUB");
3857 }
762a036f
FF
3858 if (TYPE_TARGET_STUB (type))
3859 {
3860 puts_filtered (" TYPE_FLAG_TARGET_STUB");
3861 }
3862 if (TYPE_STATIC (type))
3863 {
3864 puts_filtered (" TYPE_FLAG_STATIC");
3865 }
762a036f
FF
3866 if (TYPE_PROTOTYPED (type))
3867 {
3868 puts_filtered (" TYPE_FLAG_PROTOTYPED");
3869 }
3870 if (TYPE_INCOMPLETE (type))
3871 {
3872 puts_filtered (" TYPE_FLAG_INCOMPLETE");
3873 }
762a036f
FF
3874 if (TYPE_VARARGS (type))
3875 {
3876 puts_filtered (" TYPE_FLAG_VARARGS");
3877 }
f5f8a009
EZ
3878 /* This is used for things like AltiVec registers on ppc. Gcc emits
3879 an attribute for the array type, which tells whether or not we
3880 have a vector, instead of a regular array. */
3881 if (TYPE_VECTOR (type))
3882 {
3883 puts_filtered (" TYPE_FLAG_VECTOR");
3884 }
876cecd0
TT
3885 if (TYPE_FIXED_INSTANCE (type))
3886 {
3887 puts_filtered (" TYPE_FIXED_INSTANCE");
3888 }
3889 if (TYPE_STUB_SUPPORTED (type))
3890 {
3891 puts_filtered (" TYPE_STUB_SUPPORTED");
3892 }
3893 if (TYPE_NOTTEXT (type))
3894 {
3895 puts_filtered (" TYPE_NOTTEXT");
3896 }
c906108c
SS
3897 puts_filtered ("\n");
3898 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
d4f3574e 3899 gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
c906108c
SS
3900 puts_filtered ("\n");
3901 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3902 {
14e75d8e
JK
3903 if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3904 printfi_filtered (spaces + 2,
3905 "[%d] enumval %s type ",
3906 idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3907 else
3908 printfi_filtered (spaces + 2,
3909 "[%d] bitpos %d bitsize %d type ",
3910 idx, TYPE_FIELD_BITPOS (type, idx),
3911 TYPE_FIELD_BITSIZE (type, idx));
d4f3574e 3912 gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
c906108c
SS
3913 printf_filtered (" name '%s' (",
3914 TYPE_FIELD_NAME (type, idx) != NULL
3915 ? TYPE_FIELD_NAME (type, idx)
3916 : "<NULL>");
d4f3574e 3917 gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
c906108c
SS
3918 printf_filtered (")\n");
3919 if (TYPE_FIELD_TYPE (type, idx) != NULL)
3920 {
3921 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3922 }
3923 }
43bbcdc2
PH
3924 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3925 {
3926 printfi_filtered (spaces, "low %s%s high %s%s\n",
3927 plongest (TYPE_LOW_BOUND (type)),
3928 TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3929 plongest (TYPE_HIGH_BOUND (type)),
3e43a32a
MS
3930 TYPE_HIGH_BOUND_UNDEFINED (type)
3931 ? " (undefined)" : "");
43bbcdc2 3932 }
c906108c 3933 printfi_filtered (spaces, "vptr_basetype ");
d4f3574e 3934 gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
c906108c
SS
3935 puts_filtered ("\n");
3936 if (TYPE_VPTR_BASETYPE (type) != NULL)
3937 {
3938 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3939 }
7ba81444
MS
3940 printfi_filtered (spaces, "vptr_fieldno %d\n",
3941 TYPE_VPTR_FIELDNO (type));
c906108c 3942
b4ba55a1
JB
3943 switch (TYPE_SPECIFIC_FIELD (type))
3944 {
3945 case TYPE_SPECIFIC_CPLUS_STUFF:
3946 printfi_filtered (spaces, "cplus_stuff ");
3947 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type),
3948 gdb_stdout);
3949 puts_filtered ("\n");
3950 print_cplus_stuff (type, spaces);
3951 break;
8da61cc4 3952
b4ba55a1
JB
3953 case TYPE_SPECIFIC_GNAT_STUFF:
3954 printfi_filtered (spaces, "gnat_stuff ");
3955 gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3956 puts_filtered ("\n");
3957 print_gnat_stuff (type, spaces);
3958 break;
701c159d 3959
b4ba55a1
JB
3960 case TYPE_SPECIFIC_FLOATFORMAT:
3961 printfi_filtered (spaces, "floatformat ");
3962 if (TYPE_FLOATFORMAT (type) == NULL)
3963 puts_filtered ("(null)");
3964 else
3965 {
3966 puts_filtered ("{ ");
3967 if (TYPE_FLOATFORMAT (type)[0] == NULL
3968 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3969 puts_filtered ("(null)");
3970 else
3971 puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3972
3973 puts_filtered (", ");
3974 if (TYPE_FLOATFORMAT (type)[1] == NULL
3975 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3976 puts_filtered ("(null)");
3977 else
3978 puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3979
3980 puts_filtered (" }");
3981 }
3982 puts_filtered ("\n");
3983 break;
c906108c 3984
b6cdc2c1 3985 case TYPE_SPECIFIC_FUNC:
b4ba55a1
JB
3986 printfi_filtered (spaces, "calling_convention %d\n",
3987 TYPE_CALLING_CONVENTION (type));
b6cdc2c1 3988 /* tail_call_list is not printed. */
b4ba55a1 3989 break;
c906108c 3990 }
b4ba55a1 3991
c906108c
SS
3992 if (spaces == 0)
3993 obstack_free (&dont_print_type_obstack, NULL);
3994}
5212577a 3995\f
ae5a43e0
DJ
3996/* Trivial helpers for the libiberty hash table, for mapping one
3997 type to another. */
3998
3999struct type_pair
4000{
4001 struct type *old, *new;
4002};
4003
4004static hashval_t
4005type_pair_hash (const void *item)
4006{
4007 const struct type_pair *pair = item;
d8734c88 4008
ae5a43e0
DJ
4009 return htab_hash_pointer (pair->old);
4010}
4011
4012static int
4013type_pair_eq (const void *item_lhs, const void *item_rhs)
4014{
4015 const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
d8734c88 4016
ae5a43e0
DJ
4017 return lhs->old == rhs->old;
4018}
4019
4020/* Allocate the hash table used by copy_type_recursive to walk
4021 types without duplicates. We use OBJFILE's obstack, because
4022 OBJFILE is about to be deleted. */
4023
4024htab_t
4025create_copied_types_hash (struct objfile *objfile)
4026{
4027 return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
4028 NULL, &objfile->objfile_obstack,
4029 hashtab_obstack_allocate,
4030 dummy_obstack_deallocate);
4031}
4032
7ba81444
MS
4033/* Recursively copy (deep copy) TYPE, if it is associated with
4034 OBJFILE. Return a new type allocated using malloc, a saved type if
4035 we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
4036 not associated with OBJFILE. */
ae5a43e0
DJ
4037
4038struct type *
7ba81444
MS
4039copy_type_recursive (struct objfile *objfile,
4040 struct type *type,
ae5a43e0
DJ
4041 htab_t copied_types)
4042{
4043 struct type_pair *stored, pair;
4044 void **slot;
4045 struct type *new_type;
4046
e9bb382b 4047 if (! TYPE_OBJFILE_OWNED (type))
ae5a43e0
DJ
4048 return type;
4049
7ba81444
MS
4050 /* This type shouldn't be pointing to any types in other objfiles;
4051 if it did, the type might disappear unexpectedly. */
ae5a43e0
DJ
4052 gdb_assert (TYPE_OBJFILE (type) == objfile);
4053
4054 pair.old = type;
4055 slot = htab_find_slot (copied_types, &pair, INSERT);
4056 if (*slot != NULL)
4057 return ((struct type_pair *) *slot)->new;
4058
e9bb382b 4059 new_type = alloc_type_arch (get_type_arch (type));
ae5a43e0
DJ
4060
4061 /* We must add the new type to the hash table immediately, in case
4062 we encounter this type again during a recursive call below. */
3e43a32a
MS
4063 stored
4064 = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
ae5a43e0
DJ
4065 stored->old = type;
4066 stored->new = new_type;
4067 *slot = stored;
4068
876cecd0
TT
4069 /* Copy the common fields of types. For the main type, we simply
4070 copy the entire thing and then update specific fields as needed. */
4071 *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
e9bb382b
UW
4072 TYPE_OBJFILE_OWNED (new_type) = 0;
4073 TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
876cecd0 4074
ae5a43e0
DJ
4075 if (TYPE_NAME (type))
4076 TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
4077 if (TYPE_TAG_NAME (type))
4078 TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
ae5a43e0
DJ
4079
4080 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4081 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4082
4083 /* Copy the fields. */
ae5a43e0
DJ
4084 if (TYPE_NFIELDS (type))
4085 {
4086 int i, nfields;
4087
4088 nfields = TYPE_NFIELDS (type);
fc270c35 4089 TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
ae5a43e0
DJ
4090 for (i = 0; i < nfields; i++)
4091 {
7ba81444
MS
4092 TYPE_FIELD_ARTIFICIAL (new_type, i) =
4093 TYPE_FIELD_ARTIFICIAL (type, i);
ae5a43e0
DJ
4094 TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
4095 if (TYPE_FIELD_TYPE (type, i))
4096 TYPE_FIELD_TYPE (new_type, i)
4097 = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
4098 copied_types);
4099 if (TYPE_FIELD_NAME (type, i))
7ba81444
MS
4100 TYPE_FIELD_NAME (new_type, i) =
4101 xstrdup (TYPE_FIELD_NAME (type, i));
d6a843b5 4102 switch (TYPE_FIELD_LOC_KIND (type, i))
ae5a43e0 4103 {
d6a843b5
JK
4104 case FIELD_LOC_KIND_BITPOS:
4105 SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
4106 TYPE_FIELD_BITPOS (type, i));
4107 break;
14e75d8e
JK
4108 case FIELD_LOC_KIND_ENUMVAL:
4109 SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
4110 TYPE_FIELD_ENUMVAL (type, i));
4111 break;
d6a843b5
JK
4112 case FIELD_LOC_KIND_PHYSADDR:
4113 SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
4114 TYPE_FIELD_STATIC_PHYSADDR (type, i));
4115 break;
4116 case FIELD_LOC_KIND_PHYSNAME:
4117 SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
4118 xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
4119 i)));
4120 break;
4121 default:
4122 internal_error (__FILE__, __LINE__,
4123 _("Unexpected type field location kind: %d"),
4124 TYPE_FIELD_LOC_KIND (type, i));
ae5a43e0
DJ
4125 }
4126 }
4127 }
4128
0963b4bd 4129 /* For range types, copy the bounds information. */
43bbcdc2
PH
4130 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
4131 {
4132 TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
4133 *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
4134 }
4135
3cdcd0ce
JB
4136 /* Copy the data location information. */
4137 if (TYPE_DATA_LOCATION (type) != NULL)
4138 {
4139 TYPE_DATA_LOCATION (new_type)
4140 = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
4141 memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
4142 sizeof (struct dynamic_prop));
4143 }
4144
ae5a43e0
DJ
4145 /* Copy pointers to other types. */
4146 if (TYPE_TARGET_TYPE (type))
7ba81444
MS
4147 TYPE_TARGET_TYPE (new_type) =
4148 copy_type_recursive (objfile,
4149 TYPE_TARGET_TYPE (type),
4150 copied_types);
ae5a43e0 4151 if (TYPE_VPTR_BASETYPE (type))
7ba81444
MS
4152 TYPE_VPTR_BASETYPE (new_type) =
4153 copy_type_recursive (objfile,
4154 TYPE_VPTR_BASETYPE (type),
4155 copied_types);
ae5a43e0
DJ
4156 /* Maybe copy the type_specific bits.
4157
4158 NOTE drow/2005-12-09: We do not copy the C++-specific bits like
4159 base classes and methods. There's no fundamental reason why we
4160 can't, but at the moment it is not needed. */
4161
4162 if (TYPE_CODE (type) == TYPE_CODE_FLT)
d5d6fca5 4163 TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
ae5a43e0
DJ
4164 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4165 || TYPE_CODE (type) == TYPE_CODE_UNION
ae5a43e0
DJ
4166 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
4167 INIT_CPLUS_SPECIFIC (new_type);
4168
4169 return new_type;
4170}
4171
4af88198
JB
4172/* Make a copy of the given TYPE, except that the pointer & reference
4173 types are not preserved.
4174
4175 This function assumes that the given type has an associated objfile.
4176 This objfile is used to allocate the new type. */
4177
4178struct type *
4179copy_type (const struct type *type)
4180{
4181 struct type *new_type;
4182
e9bb382b 4183 gdb_assert (TYPE_OBJFILE_OWNED (type));
4af88198 4184
e9bb382b 4185 new_type = alloc_type_copy (type);
4af88198
JB
4186 TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
4187 TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
4188 memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
4189 sizeof (struct main_type));
3cdcd0ce
JB
4190 if (TYPE_DATA_LOCATION (type) != NULL)
4191 {
4192 TYPE_DATA_LOCATION (new_type)
4193 = TYPE_ALLOC (new_type, sizeof (struct dynamic_prop));
4194 memcpy (TYPE_DATA_LOCATION (new_type), TYPE_DATA_LOCATION (type),
4195 sizeof (struct dynamic_prop));
4196 }
4af88198
JB
4197
4198 return new_type;
4199}
5212577a 4200\f
e9bb382b
UW
4201/* Helper functions to initialize architecture-specific types. */
4202
4203/* Allocate a type structure associated with GDBARCH and set its
4204 CODE, LENGTH, and NAME fields. */
5212577a 4205
e9bb382b
UW
4206struct type *
4207arch_type (struct gdbarch *gdbarch,
4208 enum type_code code, int length, char *name)
4209{
4210 struct type *type;
4211
4212 type = alloc_type_arch (gdbarch);
4213 TYPE_CODE (type) = code;
4214 TYPE_LENGTH (type) = length;
4215
4216 if (name)
4217 TYPE_NAME (type) = xstrdup (name);
4218
4219 return type;
4220}
4221
4222/* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
4223 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4224 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5212577a 4225
e9bb382b
UW
4226struct type *
4227arch_integer_type (struct gdbarch *gdbarch,
4228 int bit, int unsigned_p, char *name)
4229{
4230 struct type *t;
4231
4232 t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
4233 if (unsigned_p)
4234 TYPE_UNSIGNED (t) = 1;
4235 if (name && strcmp (name, "char") == 0)
4236 TYPE_NOSIGN (t) = 1;
4237
4238 return t;
4239}
4240
4241/* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
4242 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4243 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5212577a 4244
e9bb382b
UW
4245struct type *
4246arch_character_type (struct gdbarch *gdbarch,
4247 int bit, int unsigned_p, char *name)
4248{
4249 struct type *t;
4250
4251 t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
4252 if (unsigned_p)
4253 TYPE_UNSIGNED (t) = 1;
4254
4255 return t;
4256}
4257
4258/* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
4259 BIT is the type size in bits. If UNSIGNED_P is non-zero, set
4260 the type's TYPE_UNSIGNED flag. NAME is the type name. */
5212577a 4261
e9bb382b
UW
4262struct type *
4263arch_boolean_type (struct gdbarch *gdbarch,
4264 int bit, int unsigned_p, char *name)
4265{
4266 struct type *t;
4267
4268 t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
4269 if (unsigned_p)
4270 TYPE_UNSIGNED (t) = 1;
4271
4272 return t;
4273}
4274
4275/* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
4276 BIT is the type size in bits; if BIT equals -1, the size is
4277 determined by the floatformat. NAME is the type name. Set the
4278 TYPE_FLOATFORMAT from FLOATFORMATS. */
5212577a 4279
27067745 4280struct type *
e9bb382b
UW
4281arch_float_type (struct gdbarch *gdbarch,
4282 int bit, char *name, const struct floatformat **floatformats)
8da61cc4
DJ
4283{
4284 struct type *t;
4285
4286 if (bit == -1)
4287 {
4288 gdb_assert (floatformats != NULL);
4289 gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
4290 bit = floatformats[0]->totalsize;
4291 }
4292 gdb_assert (bit >= 0);
4293
e9bb382b 4294 t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
8da61cc4
DJ
4295 TYPE_FLOATFORMAT (t) = floatformats;
4296 return t;
4297}
4298
e9bb382b
UW
4299/* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
4300 NAME is the type name. TARGET_TYPE is the component float type. */
5212577a 4301
27067745 4302struct type *
e9bb382b
UW
4303arch_complex_type (struct gdbarch *gdbarch,
4304 char *name, struct type *target_type)
27067745
UW
4305{
4306 struct type *t;
d8734c88 4307
e9bb382b
UW
4308 t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
4309 2 * TYPE_LENGTH (target_type), name);
27067745
UW
4310 TYPE_TARGET_TYPE (t) = target_type;
4311 return t;
4312}
4313
e9bb382b 4314/* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
eb90ce83 4315 NAME is the type name. LENGTH is the size of the flag word in bytes. */
5212577a 4316
e9bb382b
UW
4317struct type *
4318arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
4319{
4320 int nfields = length * TARGET_CHAR_BIT;
4321 struct type *type;
4322
4323 type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
4324 TYPE_UNSIGNED (type) = 1;
4325 TYPE_NFIELDS (type) = nfields;
4326 TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
4327
4328 return type;
4329}
4330
4331/* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
4332 position BITPOS is called NAME. */
5212577a 4333
e9bb382b
UW
4334void
4335append_flags_type_flag (struct type *type, int bitpos, char *name)
4336{
4337 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
4338 gdb_assert (bitpos < TYPE_NFIELDS (type));
4339 gdb_assert (bitpos >= 0);
4340
4341 if (name)
4342 {
4343 TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
945b3a32 4344 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
e9bb382b
UW
4345 }
4346 else
4347 {
4348 /* Don't show this field to the user. */
945b3a32 4349 SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
e9bb382b
UW
4350 }
4351}
4352
4353/* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
4354 specified by CODE) associated with GDBARCH. NAME is the type name. */
5212577a 4355
e9bb382b
UW
4356struct type *
4357arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
4358{
4359 struct type *t;
d8734c88 4360
e9bb382b
UW
4361 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
4362 t = arch_type (gdbarch, code, 0, NULL);
4363 TYPE_TAG_NAME (t) = name;
4364 INIT_CPLUS_SPECIFIC (t);
4365 return t;
4366}
4367
4368/* Add new field with name NAME and type FIELD to composite type T.
f5dff777
DJ
4369 Do not set the field's position or adjust the type's length;
4370 the caller should do so. Return the new field. */
5212577a 4371
f5dff777
DJ
4372struct field *
4373append_composite_type_field_raw (struct type *t, char *name,
4374 struct type *field)
e9bb382b
UW
4375{
4376 struct field *f;
d8734c88 4377
e9bb382b
UW
4378 TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
4379 TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
4380 sizeof (struct field) * TYPE_NFIELDS (t));
4381 f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
4382 memset (f, 0, sizeof f[0]);
4383 FIELD_TYPE (f[0]) = field;
4384 FIELD_NAME (f[0]) = name;
f5dff777
DJ
4385 return f;
4386}
4387
4388/* Add new field with name NAME and type FIELD to composite type T.
4389 ALIGNMENT (if non-zero) specifies the minimum field alignment. */
5212577a 4390
f5dff777
DJ
4391void
4392append_composite_type_field_aligned (struct type *t, char *name,
4393 struct type *field, int alignment)
4394{
4395 struct field *f = append_composite_type_field_raw (t, name, field);
d8734c88 4396
e9bb382b
UW
4397 if (TYPE_CODE (t) == TYPE_CODE_UNION)
4398 {
4399 if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
4400 TYPE_LENGTH (t) = TYPE_LENGTH (field);
4401 }
4402 else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4403 {
4404 TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4405 if (TYPE_NFIELDS (t) > 1)
4406 {
f41f5e61
PA
4407 SET_FIELD_BITPOS (f[0],
4408 (FIELD_BITPOS (f[-1])
4409 + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4410 * TARGET_CHAR_BIT)));
e9bb382b
UW
4411
4412 if (alignment)
4413 {
86c3c1fc
AB
4414 int left;
4415
4416 alignment *= TARGET_CHAR_BIT;
4417 left = FIELD_BITPOS (f[0]) % alignment;
d8734c88 4418
e9bb382b
UW
4419 if (left)
4420 {
f41f5e61 4421 SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
86c3c1fc 4422 TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
e9bb382b
UW
4423 }
4424 }
4425 }
4426 }
4427}
4428
4429/* Add new field with name NAME and type FIELD to composite type T. */
5212577a 4430
e9bb382b
UW
4431void
4432append_composite_type_field (struct type *t, char *name,
4433 struct type *field)
4434{
4435 append_composite_type_field_aligned (t, name, field, 0);
4436}
4437
000177f0
AC
4438static struct gdbarch_data *gdbtypes_data;
4439
4440const struct builtin_type *
4441builtin_type (struct gdbarch *gdbarch)
4442{
4443 return gdbarch_data (gdbarch, gdbtypes_data);
4444}
4445
4446static void *
4447gdbtypes_post_init (struct gdbarch *gdbarch)
4448{
4449 struct builtin_type *builtin_type
4450 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4451
46bf5051 4452 /* Basic types. */
e9bb382b
UW
4453 builtin_type->builtin_void
4454 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4455 builtin_type->builtin_char
4456 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4457 !gdbarch_char_signed (gdbarch), "char");
4458 builtin_type->builtin_signed_char
4459 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4460 0, "signed char");
4461 builtin_type->builtin_unsigned_char
4462 = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4463 1, "unsigned char");
4464 builtin_type->builtin_short
4465 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4466 0, "short");
4467 builtin_type->builtin_unsigned_short
4468 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4469 1, "unsigned short");
4470 builtin_type->builtin_int
4471 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4472 0, "int");
4473 builtin_type->builtin_unsigned_int
4474 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4475 1, "unsigned int");
4476 builtin_type->builtin_long
4477 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4478 0, "long");
4479 builtin_type->builtin_unsigned_long
4480 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4481 1, "unsigned long");
4482 builtin_type->builtin_long_long
4483 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4484 0, "long long");
4485 builtin_type->builtin_unsigned_long_long
4486 = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4487 1, "unsigned long long");
70bd8e24 4488 builtin_type->builtin_float
e9bb382b 4489 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
27067745 4490 "float", gdbarch_float_format (gdbarch));
70bd8e24 4491 builtin_type->builtin_double
e9bb382b 4492 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
27067745 4493 "double", gdbarch_double_format (gdbarch));
70bd8e24 4494 builtin_type->builtin_long_double
e9bb382b 4495 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
27067745 4496 "long double", gdbarch_long_double_format (gdbarch));
70bd8e24 4497 builtin_type->builtin_complex
e9bb382b
UW
4498 = arch_complex_type (gdbarch, "complex",
4499 builtin_type->builtin_float);
70bd8e24 4500 builtin_type->builtin_double_complex
e9bb382b
UW
4501 = arch_complex_type (gdbarch, "double complex",
4502 builtin_type->builtin_double);
4503 builtin_type->builtin_string
4504 = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4505 builtin_type->builtin_bool
4506 = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
000177f0 4507
7678ef8f
TJB
4508 /* The following three are about decimal floating point types, which
4509 are 32-bits, 64-bits and 128-bits respectively. */
4510 builtin_type->builtin_decfloat
e9bb382b 4511 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
7678ef8f 4512 builtin_type->builtin_decdouble
e9bb382b 4513 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
7678ef8f 4514 builtin_type->builtin_declong
e9bb382b 4515 = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
7678ef8f 4516
69feb676 4517 /* "True" character types. */
e9bb382b
UW
4518 builtin_type->builtin_true_char
4519 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4520 builtin_type->builtin_true_unsigned_char
4521 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
69feb676 4522
df4df182 4523 /* Fixed-size integer types. */
e9bb382b
UW
4524 builtin_type->builtin_int0
4525 = arch_integer_type (gdbarch, 0, 0, "int0_t");
4526 builtin_type->builtin_int8
4527 = arch_integer_type (gdbarch, 8, 0, "int8_t");
4528 builtin_type->builtin_uint8
4529 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4530 builtin_type->builtin_int16
4531 = arch_integer_type (gdbarch, 16, 0, "int16_t");
4532 builtin_type->builtin_uint16
4533 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4534 builtin_type->builtin_int32
4535 = arch_integer_type (gdbarch, 32, 0, "int32_t");
4536 builtin_type->builtin_uint32
4537 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4538 builtin_type->builtin_int64
4539 = arch_integer_type (gdbarch, 64, 0, "int64_t");
4540 builtin_type->builtin_uint64
4541 = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4542 builtin_type->builtin_int128
4543 = arch_integer_type (gdbarch, 128, 0, "int128_t");
4544 builtin_type->builtin_uint128
4545 = arch_integer_type (gdbarch, 128, 1, "uint128_t");
2844d6b5
KW
4546 TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4547 TYPE_INSTANCE_FLAG_NOTTEXT;
4548 TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4549 TYPE_INSTANCE_FLAG_NOTTEXT;
df4df182 4550
9a22f0d0
PM
4551 /* Wide character types. */
4552 builtin_type->builtin_char16
4553 = arch_integer_type (gdbarch, 16, 0, "char16_t");
4554 builtin_type->builtin_char32
4555 = arch_integer_type (gdbarch, 32, 0, "char32_t");
4556
4557
46bf5051 4558 /* Default data/code pointer types. */
e9bb382b
UW
4559 builtin_type->builtin_data_ptr
4560 = lookup_pointer_type (builtin_type->builtin_void);
4561 builtin_type->builtin_func_ptr
4562 = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
0875794a
JK
4563 builtin_type->builtin_func_func
4564 = lookup_function_type (builtin_type->builtin_func_ptr);
46bf5051 4565
78267919 4566 /* This type represents a GDB internal function. */
e9bb382b
UW
4567 builtin_type->internal_fn
4568 = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4569 "<internal function>");
78267919 4570
e81e7f5e
SC
4571 /* This type represents an xmethod. */
4572 builtin_type->xmethod
4573 = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
4574
46bf5051
UW
4575 return builtin_type;
4576}
4577
46bf5051
UW
4578/* This set of objfile-based types is intended to be used by symbol
4579 readers as basic types. */
4580
4581static const struct objfile_data *objfile_type_data;
4582
4583const struct objfile_type *
4584objfile_type (struct objfile *objfile)
4585{
4586 struct gdbarch *gdbarch;
4587 struct objfile_type *objfile_type
4588 = objfile_data (objfile, objfile_type_data);
4589
4590 if (objfile_type)
4591 return objfile_type;
4592
4593 objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4594 1, struct objfile_type);
4595
4596 /* Use the objfile architecture to determine basic type properties. */
4597 gdbarch = get_objfile_arch (objfile);
4598
4599 /* Basic types. */
4600 objfile_type->builtin_void
4601 = init_type (TYPE_CODE_VOID, 1,
4602 0,
4603 "void", objfile);
4604
4605 objfile_type->builtin_char
4606 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4607 (TYPE_FLAG_NOSIGN
4608 | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4609 "char", objfile);
4610 objfile_type->builtin_signed_char
4611 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4612 0,
4613 "signed char", objfile);
4614 objfile_type->builtin_unsigned_char
4615 = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4616 TYPE_FLAG_UNSIGNED,
4617 "unsigned char", objfile);
4618 objfile_type->builtin_short
4619 = init_type (TYPE_CODE_INT,
4620 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4621 0, "short", objfile);
4622 objfile_type->builtin_unsigned_short
4623 = init_type (TYPE_CODE_INT,
4624 gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4625 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4626 objfile_type->builtin_int
4627 = init_type (TYPE_CODE_INT,
4628 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4629 0, "int", objfile);
4630 objfile_type->builtin_unsigned_int
4631 = init_type (TYPE_CODE_INT,
4632 gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4633 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4634 objfile_type->builtin_long
4635 = init_type (TYPE_CODE_INT,
4636 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4637 0, "long", objfile);
4638 objfile_type->builtin_unsigned_long
4639 = init_type (TYPE_CODE_INT,
4640 gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4641 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4642 objfile_type->builtin_long_long
4643 = init_type (TYPE_CODE_INT,
4644 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4645 0, "long long", objfile);
4646 objfile_type->builtin_unsigned_long_long
4647 = init_type (TYPE_CODE_INT,
4648 gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4649 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4650
4651 objfile_type->builtin_float
4652 = init_type (TYPE_CODE_FLT,
4653 gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4654 0, "float", objfile);
4655 TYPE_FLOATFORMAT (objfile_type->builtin_float)
4656 = gdbarch_float_format (gdbarch);
4657 objfile_type->builtin_double
4658 = init_type (TYPE_CODE_FLT,
4659 gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4660 0, "double", objfile);
4661 TYPE_FLOATFORMAT (objfile_type->builtin_double)
4662 = gdbarch_double_format (gdbarch);
4663 objfile_type->builtin_long_double
4664 = init_type (TYPE_CODE_FLT,
4665 gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
4666 0, "long double", objfile);
4667 TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
4668 = gdbarch_long_double_format (gdbarch);
4669
4670 /* This type represents a type that was unrecognized in symbol read-in. */
4671 objfile_type->builtin_error
4672 = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
4673
4674 /* The following set of types is used for symbols with no
4675 debug information. */
4676 objfile_type->nodebug_text_symbol
4677 = init_type (TYPE_CODE_FUNC, 1, 0,
4678 "<text variable, no debug info>", objfile);
4679 TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
4680 = objfile_type->builtin_int;
0875794a
JK
4681 objfile_type->nodebug_text_gnu_ifunc_symbol
4682 = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4683 "<text gnu-indirect-function variable, no debug info>",
4684 objfile);
4685 TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4686 = objfile_type->nodebug_text_symbol;
4687 objfile_type->nodebug_got_plt_symbol
4688 = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4689 "<text from jump slot in .got.plt, no debug info>",
4690 objfile);
4691 TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4692 = objfile_type->nodebug_text_symbol;
46bf5051
UW
4693 objfile_type->nodebug_data_symbol
4694 = init_type (TYPE_CODE_INT,
4695 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4696 "<data variable, no debug info>", objfile);
4697 objfile_type->nodebug_unknown_symbol
4698 = init_type (TYPE_CODE_INT, 1, 0,
4699 "<variable (not text or data), no debug info>", objfile);
4700 objfile_type->nodebug_tls_symbol
4701 = init_type (TYPE_CODE_INT,
4702 gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4703 "<thread local variable, no debug info>", objfile);
000177f0
AC
4704
4705 /* NOTE: on some targets, addresses and pointers are not necessarily
0a7cfe2c 4706 the same.
000177f0
AC
4707
4708 The upshot is:
4709 - gdb's `struct type' always describes the target's
4710 representation.
4711 - gdb's `struct value' objects should always hold values in
4712 target form.
4713 - gdb's CORE_ADDR values are addresses in the unified virtual
4714 address space that the assembler and linker work with. Thus,
4715 since target_read_memory takes a CORE_ADDR as an argument, it
4716 can access any memory on the target, even if the processor has
4717 separate code and data address spaces.
4718
46bf5051
UW
4719 In this context, objfile_type->builtin_core_addr is a bit odd:
4720 it's a target type for a value the target will never see. It's
4721 only used to hold the values of (typeless) linker symbols, which
4722 are indeed in the unified virtual address space. */
000177f0 4723
46bf5051
UW
4724 objfile_type->builtin_core_addr
4725 = init_type (TYPE_CODE_INT,
4726 gdbarch_addr_bit (gdbarch) / 8,
4727 TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
64c50499 4728
46bf5051
UW
4729 set_objfile_data (objfile, objfile_type_data, objfile_type);
4730 return objfile_type;
000177f0
AC
4731}
4732
5212577a 4733extern initialize_file_ftype _initialize_gdbtypes;
46bf5051 4734
c906108c 4735void
fba45db2 4736_initialize_gdbtypes (void)
c906108c 4737{
5674de60 4738 gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
46bf5051 4739 objfile_type_data = register_objfile_data ();
5674de60 4740
ccce17b0
YQ
4741 add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4742 _("Set debugging of C++ overloading."),
4743 _("Show debugging of C++ overloading."),
4744 _("When enabled, ranking of the "
4745 "functions is displayed."),
4746 NULL,
4747 show_overload_debug,
4748 &setdebuglist, &showdebuglist);
5674de60 4749
7ba81444 4750 /* Add user knob for controlling resolution of opaque types. */
5674de60 4751 add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
3e43a32a
MS
4752 &opaque_type_resolution,
4753 _("Set resolution of opaque struct/class/union"
4754 " types (if set before loading symbols)."),
4755 _("Show resolution of opaque struct/class/union"
4756 " types (if set before loading symbols)."),
4757 NULL, NULL,
5674de60
UW
4758 show_opaque_type_resolution,
4759 &setlist, &showlist);
a451cb65
KS
4760
4761 /* Add an option to permit non-strict type checking. */
4762 add_setshow_boolean_cmd ("type", class_support,
4763 &strict_type_checking,
4764 _("Set strict type checking."),
4765 _("Show strict type checking."),
4766 NULL, NULL,
4767 show_strict_type_checking,
4768 &setchecklist, &showchecklist);
c906108c 4769}
This page took 1.423293 seconds and 4 git commands to generate.