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