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