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