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