* gdbtypes.h (builtin_type_void_data_ptr): Rename
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support, using pieces from other GDB modules.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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
40 /* These variables point to the objects
41 representing the predefined C data types. */
42
43 struct type *builtin_type_void;
44 struct type *builtin_type_char;
45 struct type *builtin_type_true_char;
46 struct type *builtin_type_short;
47 struct type *builtin_type_int;
48 struct type *builtin_type_long;
49 struct type *builtin_type_long_long;
50 struct type *builtin_type_signed_char;
51 struct type *builtin_type_unsigned_char;
52 struct type *builtin_type_unsigned_short;
53 struct type *builtin_type_unsigned_int;
54 struct type *builtin_type_unsigned_long;
55 struct type *builtin_type_unsigned_long_long;
56 struct type *builtin_type_float;
57 struct type *builtin_type_double;
58 struct type *builtin_type_long_double;
59 struct type *builtin_type_complex;
60 struct type *builtin_type_double_complex;
61 struct type *builtin_type_string;
62 struct type *builtin_type_int8;
63 struct type *builtin_type_uint8;
64 struct type *builtin_type_int16;
65 struct type *builtin_type_uint16;
66 struct type *builtin_type_int32;
67 struct type *builtin_type_uint32;
68 struct type *builtin_type_int64;
69 struct type *builtin_type_uint64;
70 struct type *builtin_type_bool;
71 struct type *builtin_type_v4sf;
72 struct type *builtin_type_v4si;
73 struct type *builtin_type_v8qi;
74 struct type *builtin_type_v4hi;
75 struct type *builtin_type_v2si;
76 struct type *builtin_type_void_data_ptr;
77 struct type *builtin_type_void_func_ptr;
78 struct type *builtin_type_CORE_ADDR;
79 struct type *builtin_type_bfd_vma;
80
81 int opaque_type_resolution = 1;
82 int overload_debug = 0;
83
84 struct extra
85 {
86 char str[128];
87 int len;
88 }; /* maximum extension is 128! FIXME */
89
90 static void add_name (struct extra *, char *);
91 static void add_mangled_type (struct extra *, struct type *);
92 #if 0
93 static void cfront_mangle_name (struct type *, int, int);
94 #endif
95 static void print_bit_vector (B_TYPE *, int);
96 static void print_arg_types (struct type **, int);
97 static void dump_fn_fieldlists (struct type *, int);
98 static void print_cplus_stuff (struct type *, int);
99 static void virtual_base_list_aux (struct type *dclass);
100
101
102 /* Alloc a new type structure and fill it with some defaults. If
103 OBJFILE is non-NULL, then allocate the space for the type structure
104 in that objfile's type_obstack. */
105
106 struct type *
107 alloc_type (struct objfile *objfile)
108 {
109 register struct type *type;
110
111 /* Alloc the structure and start off with all fields zeroed. */
112
113 if (objfile == NULL)
114 {
115 type = (struct type *) xmalloc (sizeof (struct type));
116 }
117 else
118 {
119 type = (struct type *) obstack_alloc (&objfile->type_obstack,
120 sizeof (struct type));
121 OBJSTAT (objfile, n_types++);
122 }
123 memset ((char *) type, 0, sizeof (struct type));
124
125 /* Initialize the fields that might not be zero. */
126
127 TYPE_CODE (type) = TYPE_CODE_UNDEF;
128 TYPE_OBJFILE (type) = objfile;
129 TYPE_VPTR_FIELDNO (type) = -1;
130 TYPE_CV_TYPE (type) = type; /* chain back to itself */
131
132 return (type);
133 }
134
135 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
136 to a pointer to memory where the pointer type should be stored.
137 If *TYPEPTR is zero, update it to point to the pointer type we return.
138 We allocate new memory if needed. */
139
140 struct type *
141 make_pointer_type (struct type *type, struct type **typeptr)
142 {
143 register struct type *ntype; /* New type */
144 struct objfile *objfile;
145
146 ntype = TYPE_POINTER_TYPE (type);
147
148 if (ntype)
149 {
150 if (typeptr == 0)
151 return ntype; /* Don't care about alloc, and have new type. */
152 else if (*typeptr == 0)
153 {
154 *typeptr = ntype; /* Tracking alloc, and we have new type. */
155 return ntype;
156 }
157 }
158
159 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
160 {
161 ntype = alloc_type (TYPE_OBJFILE (type));
162 if (typeptr)
163 *typeptr = ntype;
164 }
165 else
166 /* We have storage, but need to reset it. */
167 {
168 ntype = *typeptr;
169 objfile = TYPE_OBJFILE (ntype);
170 memset ((char *) ntype, 0, sizeof (struct type));
171 TYPE_OBJFILE (ntype) = objfile;
172 }
173
174 TYPE_TARGET_TYPE (ntype) = type;
175 TYPE_POINTER_TYPE (type) = ntype;
176
177 /* FIXME! Assume the machine has only one representation for pointers! */
178
179 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
180 TYPE_CODE (ntype) = TYPE_CODE_PTR;
181
182 /* Mark pointers as unsigned. The target converts between pointers
183 and addresses (CORE_ADDRs) using POINTER_TO_ADDRESS() and
184 ADDRESS_TO_POINTER(). */
185 TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
186
187 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
188 TYPE_POINTER_TYPE (type) = ntype;
189
190 return ntype;
191 }
192
193 /* Given a type TYPE, return a type of pointers to that type.
194 May need to construct such a type if this is the first use. */
195
196 struct type *
197 lookup_pointer_type (struct type *type)
198 {
199 return make_pointer_type (type, (struct type **) 0);
200 }
201
202 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, points
203 to a pointer to memory where the reference type should be stored.
204 If *TYPEPTR is zero, update it to point to the reference type we return.
205 We allocate new memory if needed. */
206
207 struct type *
208 make_reference_type (struct type *type, struct type **typeptr)
209 {
210 register struct type *ntype; /* New type */
211 struct objfile *objfile;
212
213 ntype = TYPE_REFERENCE_TYPE (type);
214
215 if (ntype)
216 {
217 if (typeptr == 0)
218 return ntype; /* Don't care about alloc, and have new type. */
219 else if (*typeptr == 0)
220 {
221 *typeptr = ntype; /* Tracking alloc, and we have new type. */
222 return ntype;
223 }
224 }
225
226 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
227 {
228 ntype = alloc_type (TYPE_OBJFILE (type));
229 if (typeptr)
230 *typeptr = ntype;
231 }
232 else
233 /* We have storage, but need to reset it. */
234 {
235 ntype = *typeptr;
236 objfile = TYPE_OBJFILE (ntype);
237 memset ((char *) ntype, 0, sizeof (struct type));
238 TYPE_OBJFILE (ntype) = objfile;
239 }
240
241 TYPE_TARGET_TYPE (ntype) = type;
242 TYPE_REFERENCE_TYPE (type) = ntype;
243
244 /* FIXME! Assume the machine has only one representation for references,
245 and that it matches the (only) representation for pointers! */
246
247 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
248 TYPE_CODE (ntype) = TYPE_CODE_REF;
249
250 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
251 TYPE_REFERENCE_TYPE (type) = ntype;
252
253 return ntype;
254 }
255
256 /* Same as above, but caller doesn't care about memory allocation details. */
257
258 struct type *
259 lookup_reference_type (struct type *type)
260 {
261 return make_reference_type (type, (struct type **) 0);
262 }
263
264 /* Lookup a function type that returns type TYPE. TYPEPTR, if nonzero, points
265 to a pointer to memory where the function type should be stored.
266 If *TYPEPTR is zero, update it to point to the function type we return.
267 We allocate new memory if needed. */
268
269 struct type *
270 make_function_type (struct type *type, struct type **typeptr)
271 {
272 register struct type *ntype; /* New type */
273 struct objfile *objfile;
274
275 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
276 {
277 ntype = alloc_type (TYPE_OBJFILE (type));
278 if (typeptr)
279 *typeptr = ntype;
280 }
281 else
282 /* We have storage, but need to reset it. */
283 {
284 ntype = *typeptr;
285 objfile = TYPE_OBJFILE (ntype);
286 memset ((char *) ntype, 0, sizeof (struct type));
287 TYPE_OBJFILE (ntype) = objfile;
288 }
289
290 TYPE_TARGET_TYPE (ntype) = type;
291
292 TYPE_LENGTH (ntype) = 1;
293 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
294
295 return ntype;
296 }
297
298
299 /* Given a type TYPE, return a type of functions that return that type.
300 May need to construct such a type if this is the first use. */
301
302 struct type *
303 lookup_function_type (struct type *type)
304 {
305 return make_function_type (type, (struct type **) 0);
306 }
307
308
309 /* Make a "c-v" variant of a type -- a type that is identical to the
310 one supplied except that it may have const or volatile attributes
311 CNST is a flag for setting the const attribute
312 VOLTL is a flag for setting the volatile attribute
313 TYPE is the base type whose variant we are creating.
314 TYPEPTR, if nonzero, points
315 to a pointer to memory where the reference type should be stored.
316 If *TYPEPTR is zero, update it to point to the reference type we return.
317 We allocate new memory if needed. */
318
319 struct type *
320 make_cv_type (int cnst, int voltl, struct type *type, struct type **typeptr)
321 {
322 register struct type *ntype; /* New type */
323 register struct type *tmp_type = type; /* tmp type */
324 struct objfile *objfile;
325
326 ntype = TYPE_CV_TYPE (type);
327
328 while (ntype != type)
329 {
330 if ((TYPE_CONST (ntype) == cnst) &&
331 (TYPE_VOLATILE (ntype) == voltl))
332 {
333 if (typeptr == 0)
334 return ntype;
335 else if (*typeptr == 0)
336 {
337 *typeptr = ntype; /* Tracking alloc, and we have new type. */
338 return ntype;
339 }
340 }
341 tmp_type = ntype;
342 ntype = TYPE_CV_TYPE (ntype);
343 }
344
345 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
346 {
347 ntype = alloc_type (TYPE_OBJFILE (type));
348 if (typeptr)
349 *typeptr = ntype;
350 }
351 else
352 /* We have storage, but need to reset it. */
353 {
354 ntype = *typeptr;
355 objfile = TYPE_OBJFILE (ntype);
356 /* memset ((char *) ntype, 0, sizeof (struct type)); */
357 TYPE_OBJFILE (ntype) = objfile;
358 }
359
360 /* Copy original type */
361 memcpy ((char *) ntype, (char *) type, sizeof (struct type));
362 /* But zero out fields that shouldn't be copied */
363 TYPE_POINTER_TYPE (ntype) = (struct type *) 0; /* Need new pointer kind */
364 TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0; /* Need new referene kind */
365 /* Note: TYPE_TARGET_TYPE can be left as is */
366
367 /* Set flags appropriately */
368 if (cnst)
369 TYPE_FLAGS (ntype) |= TYPE_FLAG_CONST;
370 else
371 TYPE_FLAGS (ntype) &= ~TYPE_FLAG_CONST;
372
373 if (voltl)
374 TYPE_FLAGS (ntype) |= TYPE_FLAG_VOLATILE;
375 else
376 TYPE_FLAGS (ntype) &= ~TYPE_FLAG_VOLATILE;
377
378 /* Fix the chain of cv variants */
379 TYPE_CV_TYPE (ntype) = type;
380 TYPE_CV_TYPE (tmp_type) = ntype;
381
382 return ntype;
383 }
384
385
386
387
388 /* Implement direct support for MEMBER_TYPE in GNU C++.
389 May need to construct such a type if this is the first use.
390 The TYPE is the type of the member. The DOMAIN is the type
391 of the aggregate that the member belongs to. */
392
393 struct type *
394 lookup_member_type (struct type *type, struct type *domain)
395 {
396 register struct type *mtype;
397
398 mtype = alloc_type (TYPE_OBJFILE (type));
399 smash_to_member_type (mtype, domain, type);
400 return (mtype);
401 }
402
403 /* Allocate a stub method whose return type is TYPE.
404 This apparently happens for speed of symbol reading, since parsing
405 out the arguments to the method is cpu-intensive, the way we are doing
406 it. So, we will fill in arguments later.
407 This always returns a fresh type. */
408
409 struct type *
410 allocate_stub_method (struct type *type)
411 {
412 struct type *mtype;
413
414 mtype = alloc_type (TYPE_OBJFILE (type));
415 TYPE_TARGET_TYPE (mtype) = type;
416 /* _DOMAIN_TYPE (mtype) = unknown yet */
417 /* _ARG_TYPES (mtype) = unknown yet */
418 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
419 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
420 TYPE_LENGTH (mtype) = 1;
421 return (mtype);
422 }
423
424 /* Create a range type using either a blank type supplied in RESULT_TYPE,
425 or creating a new type, inheriting the objfile from INDEX_TYPE.
426
427 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND to
428 HIGH_BOUND, inclusive.
429
430 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
431 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
432
433 struct type *
434 create_range_type (struct type *result_type, struct type *index_type,
435 int low_bound, int high_bound)
436 {
437 if (result_type == NULL)
438 {
439 result_type = alloc_type (TYPE_OBJFILE (index_type));
440 }
441 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
442 TYPE_TARGET_TYPE (result_type) = index_type;
443 if (TYPE_FLAGS (index_type) & TYPE_FLAG_STUB)
444 TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
445 else
446 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
447 TYPE_NFIELDS (result_type) = 2;
448 TYPE_FIELDS (result_type) = (struct field *)
449 TYPE_ALLOC (result_type, 2 * sizeof (struct field));
450 memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
451 TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
452 TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
453 TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int; /* FIXME */
454 TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int; /* FIXME */
455
456 if (low_bound >= 0)
457 TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
458
459 return (result_type);
460 }
461
462 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type TYPE.
463 Return 1 of type is a range type, 0 if it is discrete (and bounds
464 will fit in LONGEST), or -1 otherwise. */
465
466 int
467 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
468 {
469 CHECK_TYPEDEF (type);
470 switch (TYPE_CODE (type))
471 {
472 case TYPE_CODE_RANGE:
473 *lowp = TYPE_LOW_BOUND (type);
474 *highp = TYPE_HIGH_BOUND (type);
475 return 1;
476 case TYPE_CODE_ENUM:
477 if (TYPE_NFIELDS (type) > 0)
478 {
479 /* The enums may not be sorted by value, so search all
480 entries */
481 int i;
482
483 *lowp = *highp = TYPE_FIELD_BITPOS (type, 0);
484 for (i = 0; i < TYPE_NFIELDS (type); i++)
485 {
486 if (TYPE_FIELD_BITPOS (type, i) < *lowp)
487 *lowp = TYPE_FIELD_BITPOS (type, i);
488 if (TYPE_FIELD_BITPOS (type, i) > *highp)
489 *highp = TYPE_FIELD_BITPOS (type, i);
490 }
491
492 /* Set unsigned indicator if warranted. */
493 if (*lowp >= 0)
494 {
495 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
496 }
497 }
498 else
499 {
500 *lowp = 0;
501 *highp = -1;
502 }
503 return 0;
504 case TYPE_CODE_BOOL:
505 *lowp = 0;
506 *highp = 1;
507 return 0;
508 case TYPE_CODE_INT:
509 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
510 return -1;
511 if (!TYPE_UNSIGNED (type))
512 {
513 *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
514 *highp = -*lowp - 1;
515 return 0;
516 }
517 /* ... fall through for unsigned ints ... */
518 case TYPE_CODE_CHAR:
519 *lowp = 0;
520 /* This round-about calculation is to avoid shifting by
521 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
522 if TYPE_LENGTH (type) == sizeof (LONGEST). */
523 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
524 *highp = (*highp - 1) | *highp;
525 return 0;
526 default:
527 return -1;
528 }
529 }
530
531 /* Create an array type using either a blank type supplied in RESULT_TYPE,
532 or creating a new type, inheriting the objfile from RANGE_TYPE.
533
534 Elements will be of type ELEMENT_TYPE, the indices will be of type
535 RANGE_TYPE.
536
537 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
538 sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
539
540 struct type *
541 create_array_type (struct type *result_type, struct type *element_type,
542 struct type *range_type)
543 {
544 LONGEST low_bound, high_bound;
545
546 if (result_type == NULL)
547 {
548 result_type = alloc_type (TYPE_OBJFILE (range_type));
549 }
550 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
551 TYPE_TARGET_TYPE (result_type) = element_type;
552 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
553 low_bound = high_bound = 0;
554 CHECK_TYPEDEF (element_type);
555 TYPE_LENGTH (result_type) =
556 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
557 TYPE_NFIELDS (result_type) = 1;
558 TYPE_FIELDS (result_type) =
559 (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
560 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
561 TYPE_FIELD_TYPE (result_type, 0) = range_type;
562 TYPE_VPTR_FIELDNO (result_type) = -1;
563
564 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */
565 if (TYPE_LENGTH (result_type) == 0)
566 TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
567
568 return (result_type);
569 }
570
571 /* Create a string type using either a blank type supplied in RESULT_TYPE,
572 or creating a new type. String types are similar enough to array of
573 char types that we can use create_array_type to build the basic type
574 and then bash it into a string type.
575
576 For fixed length strings, the range type contains 0 as the lower
577 bound and the length of the string minus one as the upper bound.
578
579 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
580 sure it is TYPE_CODE_UNDEF before we bash it into a string type? */
581
582 struct type *
583 create_string_type (struct type *result_type, struct type *range_type)
584 {
585 result_type = create_array_type (result_type,
586 *current_language->string_char_type,
587 range_type);
588 TYPE_CODE (result_type) = TYPE_CODE_STRING;
589 return (result_type);
590 }
591
592 struct type *
593 create_set_type (struct type *result_type, struct type *domain_type)
594 {
595 LONGEST low_bound, high_bound, bit_length;
596 if (result_type == NULL)
597 {
598 result_type = alloc_type (TYPE_OBJFILE (domain_type));
599 }
600 TYPE_CODE (result_type) = TYPE_CODE_SET;
601 TYPE_NFIELDS (result_type) = 1;
602 TYPE_FIELDS (result_type) = (struct field *)
603 TYPE_ALLOC (result_type, 1 * sizeof (struct field));
604 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
605
606 if (!(TYPE_FLAGS (domain_type) & TYPE_FLAG_STUB))
607 {
608 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
609 low_bound = high_bound = 0;
610 bit_length = high_bound - low_bound + 1;
611 TYPE_LENGTH (result_type)
612 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
613 }
614 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
615
616 if (low_bound >= 0)
617 TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
618
619 return (result_type);
620 }
621
622
623 /* Construct and return a type of the form:
624 struct NAME { ELT_TYPE ELT_NAME[N]; }
625 We use these types for SIMD registers. For example, the type of
626 the SSE registers on the late x86-family processors is:
627 struct __builtin_v4sf { float f[4]; }
628 built by the function call:
629 init_simd_type ("__builtin_v4sf", builtin_type_float, "f", 4)
630 The type returned is a permanent type, allocated using malloc; it
631 doesn't live in any objfile's obstack. */
632 static struct type *
633 init_simd_type (char *name,
634 struct type *elt_type,
635 char *elt_name,
636 int n)
637 {
638 struct type *t;
639 struct field *f;
640
641 /* Build the field structure. */
642 f = xmalloc (sizeof (*f));
643 memset (f, 0, sizeof (*f));
644 f->loc.bitpos = 0;
645 f->type = create_array_type (0, elt_type,
646 create_range_type (0, builtin_type_int,
647 0, n-1));
648 f->name = elt_name;
649
650 /* Build a struct type with that field. */
651 t = init_type (TYPE_CODE_STRUCT, n * TYPE_LENGTH (elt_type), 0, 0, 0);
652 t->nfields = 1;
653 t->fields = f;
654 t->tag_name = name;
655
656 return t;
657 }
658
659
660 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
661 A MEMBER is a wierd thing -- it amounts to a typed offset into
662 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
663 include the offset (that's the value of the MEMBER itself), but does
664 include the structure type into which it points (for some reason).
665
666 When "smashing" the type, we preserve the objfile that the
667 old type pointed to, since we aren't changing where the type is actually
668 allocated. */
669
670 void
671 smash_to_member_type (struct type *type, struct type *domain,
672 struct type *to_type)
673 {
674 struct objfile *objfile;
675
676 objfile = TYPE_OBJFILE (type);
677
678 memset ((char *) type, 0, sizeof (struct type));
679 TYPE_OBJFILE (type) = objfile;
680 TYPE_TARGET_TYPE (type) = to_type;
681 TYPE_DOMAIN_TYPE (type) = domain;
682 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
683 TYPE_CODE (type) = TYPE_CODE_MEMBER;
684 }
685
686 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
687 METHOD just means `function that gets an extra "this" argument'.
688
689 When "smashing" the type, we preserve the objfile that the
690 old type pointed to, since we aren't changing where the type is actually
691 allocated. */
692
693 void
694 smash_to_method_type (struct type *type, struct type *domain,
695 struct type *to_type, struct type **args)
696 {
697 struct objfile *objfile;
698
699 objfile = TYPE_OBJFILE (type);
700
701 memset ((char *) type, 0, sizeof (struct type));
702 TYPE_OBJFILE (type) = objfile;
703 TYPE_TARGET_TYPE (type) = to_type;
704 TYPE_DOMAIN_TYPE (type) = domain;
705 TYPE_ARG_TYPES (type) = args;
706 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
707 TYPE_CODE (type) = TYPE_CODE_METHOD;
708 }
709
710 /* Return a typename for a struct/union/enum type without "struct ",
711 "union ", or "enum ". If the type has a NULL name, return NULL. */
712
713 char *
714 type_name_no_tag (register const struct type *type)
715 {
716 if (TYPE_TAG_NAME (type) != NULL)
717 return TYPE_TAG_NAME (type);
718
719 /* Is there code which expects this to return the name if there is no
720 tag name? My guess is that this is mainly used for C++ in cases where
721 the two will always be the same. */
722 return TYPE_NAME (type);
723 }
724
725 /* Lookup a primitive type named NAME.
726 Return zero if NAME is not a primitive type. */
727
728 struct type *
729 lookup_primitive_typename (char *name)
730 {
731 struct type **const *p;
732
733 for (p = current_language->la_builtin_type_vector; *p != NULL; p++)
734 {
735 if (STREQ ((**p)->name, name))
736 {
737 return (**p);
738 }
739 }
740 return (NULL);
741 }
742
743 /* Lookup a typedef or primitive type named NAME,
744 visible in lexical block BLOCK.
745 If NOERR is nonzero, return zero if NAME is not suitably defined. */
746
747 struct type *
748 lookup_typename (char *name, struct block *block, int noerr)
749 {
750 register struct symbol *sym;
751 register struct type *tmp;
752
753 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
754 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
755 {
756 tmp = lookup_primitive_typename (name);
757 if (tmp)
758 {
759 return (tmp);
760 }
761 else if (!tmp && noerr)
762 {
763 return (NULL);
764 }
765 else
766 {
767 error ("No type named %s.", name);
768 }
769 }
770 return (SYMBOL_TYPE (sym));
771 }
772
773 struct type *
774 lookup_unsigned_typename (char *name)
775 {
776 char *uns = alloca (strlen (name) + 10);
777
778 strcpy (uns, "unsigned ");
779 strcpy (uns + 9, name);
780 return (lookup_typename (uns, (struct block *) NULL, 0));
781 }
782
783 struct type *
784 lookup_signed_typename (char *name)
785 {
786 struct type *t;
787 char *uns = alloca (strlen (name) + 8);
788
789 strcpy (uns, "signed ");
790 strcpy (uns + 7, name);
791 t = lookup_typename (uns, (struct block *) NULL, 1);
792 /* If we don't find "signed FOO" just try again with plain "FOO". */
793 if (t != NULL)
794 return t;
795 return lookup_typename (name, (struct block *) NULL, 0);
796 }
797
798 /* Lookup a structure type named "struct NAME",
799 visible in lexical block BLOCK. */
800
801 struct type *
802 lookup_struct (char *name, struct block *block)
803 {
804 register struct symbol *sym;
805
806 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
807 (struct symtab **) NULL);
808
809 if (sym == NULL)
810 {
811 error ("No struct type named %s.", name);
812 }
813 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
814 {
815 error ("This context has class, union or enum %s, not a struct.", name);
816 }
817 return (SYMBOL_TYPE (sym));
818 }
819
820 /* Lookup a union type named "union NAME",
821 visible in lexical block BLOCK. */
822
823 struct type *
824 lookup_union (char *name, struct block *block)
825 {
826 register struct symbol *sym;
827 struct type *t;
828
829 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
830 (struct symtab **) NULL);
831
832 if (sym == NULL)
833 error ("No union type named %s.", name);
834
835 t = SYMBOL_TYPE (sym);
836
837 if (TYPE_CODE (t) == TYPE_CODE_UNION)
838 return (t);
839
840 /* C++ unions may come out with TYPE_CODE_CLASS, but we look at
841 * a further "declared_type" field to discover it is really a union.
842 */
843 if (HAVE_CPLUS_STRUCT (t))
844 if (TYPE_DECLARED_TYPE (t) == DECLARED_TYPE_UNION)
845 return (t);
846
847 /* If we get here, it's not a union */
848 error ("This context has class, struct or enum %s, not a union.", name);
849 }
850
851
852 /* Lookup an enum type named "enum NAME",
853 visible in lexical block BLOCK. */
854
855 struct type *
856 lookup_enum (char *name, struct block *block)
857 {
858 register struct symbol *sym;
859
860 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
861 (struct symtab **) NULL);
862 if (sym == NULL)
863 {
864 error ("No enum type named %s.", name);
865 }
866 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
867 {
868 error ("This context has class, struct or union %s, not an enum.", name);
869 }
870 return (SYMBOL_TYPE (sym));
871 }
872
873 /* Lookup a template type named "template NAME<TYPE>",
874 visible in lexical block BLOCK. */
875
876 struct type *
877 lookup_template_type (char *name, struct type *type, struct block *block)
878 {
879 struct symbol *sym;
880 char *nam = (char *) alloca (strlen (name) + strlen (type->name) + 4);
881 strcpy (nam, name);
882 strcat (nam, "<");
883 strcat (nam, type->name);
884 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
885
886 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
887
888 if (sym == NULL)
889 {
890 error ("No template type named %s.", name);
891 }
892 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
893 {
894 error ("This context has class, union or enum %s, not a struct.", name);
895 }
896 return (SYMBOL_TYPE (sym));
897 }
898
899 /* Given a type TYPE, lookup the type of the component of type named NAME.
900
901 TYPE can be either a struct or union, or a pointer or reference to a struct or
902 union. If it is a pointer or reference, its target type is automatically used.
903 Thus '.' and '->' are interchangable, as specified for the definitions of the
904 expression element types STRUCTOP_STRUCT and STRUCTOP_PTR.
905
906 If NOERR is nonzero, return zero if NAME is not suitably defined.
907 If NAME is the name of a baseclass type, return that type. */
908
909 struct type *
910 lookup_struct_elt_type (struct type *type, char *name, int noerr)
911 {
912 int i;
913
914 for (;;)
915 {
916 CHECK_TYPEDEF (type);
917 if (TYPE_CODE (type) != TYPE_CODE_PTR
918 && TYPE_CODE (type) != TYPE_CODE_REF)
919 break;
920 type = TYPE_TARGET_TYPE (type);
921 }
922
923 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
924 TYPE_CODE (type) != TYPE_CODE_UNION)
925 {
926 target_terminal_ours ();
927 gdb_flush (gdb_stdout);
928 fprintf_unfiltered (gdb_stderr, "Type ");
929 type_print (type, "", gdb_stderr, -1);
930 error (" is not a structure or union type.");
931 }
932
933 #if 0
934 /* FIXME: This change put in by Michael seems incorrect for the case where
935 the structure tag name is the same as the member name. I.E. when doing
936 "ptype bell->bar" for "struct foo { int bar; int foo; } bell;"
937 Disabled by fnf. */
938 {
939 char *typename;
940
941 typename = type_name_no_tag (type);
942 if (typename != NULL && STREQ (typename, name))
943 return type;
944 }
945 #endif
946
947 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
948 {
949 char *t_field_name = TYPE_FIELD_NAME (type, i);
950
951 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
952 {
953 return TYPE_FIELD_TYPE (type, i);
954 }
955 }
956
957 /* OK, it's not in this class. Recursively check the baseclasses. */
958 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
959 {
960 struct type *t;
961
962 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, noerr);
963 if (t != NULL)
964 {
965 return t;
966 }
967 }
968
969 if (noerr)
970 {
971 return NULL;
972 }
973
974 target_terminal_ours ();
975 gdb_flush (gdb_stdout);
976 fprintf_unfiltered (gdb_stderr, "Type ");
977 type_print (type, "", gdb_stderr, -1);
978 fprintf_unfiltered (gdb_stderr, " has no component named ");
979 fputs_filtered (name, gdb_stderr);
980 error (".");
981 return (struct type *) -1; /* For lint */
982 }
983
984 /* If possible, make the vptr_fieldno and vptr_basetype fields of TYPE
985 valid. Callers should be aware that in some cases (for example,
986 the type or one of its baseclasses is a stub type and we are
987 debugging a .o file), this function will not be able to find the virtual
988 function table pointer, and vptr_fieldno will remain -1 and vptr_basetype
989 will remain NULL. */
990
991 void
992 fill_in_vptr_fieldno (struct type *type)
993 {
994 CHECK_TYPEDEF (type);
995
996 if (TYPE_VPTR_FIELDNO (type) < 0)
997 {
998 int i;
999
1000 /* We must start at zero in case the first (and only) baseclass is
1001 virtual (and hence we cannot share the table pointer). */
1002 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1003 {
1004 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
1005 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
1006 {
1007 TYPE_VPTR_FIELDNO (type)
1008 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
1009 TYPE_VPTR_BASETYPE (type)
1010 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
1011 break;
1012 }
1013 }
1014 }
1015 }
1016
1017 /* Find the method and field indices for the destructor in class type T.
1018 Return 1 if the destructor was found, otherwise, return 0. */
1019
1020 int
1021 get_destructor_fn_field (struct type *t, int *method_indexp, int *field_indexp)
1022 {
1023 int i;
1024
1025 for (i = 0; i < TYPE_NFN_FIELDS (t); i++)
1026 {
1027 int j;
1028 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1029
1030 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (t, i); j++)
1031 {
1032 if (is_destructor_name (TYPE_FN_FIELD_PHYSNAME (f, j)) != 0)
1033 {
1034 *method_indexp = i;
1035 *field_indexp = j;
1036 return 1;
1037 }
1038 }
1039 }
1040 return 0;
1041 }
1042
1043 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
1044
1045 If this is a stubbed struct (i.e. declared as struct foo *), see if
1046 we can find a full definition in some other file. If so, copy this
1047 definition, so we can use it in future. There used to be a comment (but
1048 not any code) that if we don't find a full definition, we'd set a flag
1049 so we don't spend time in the future checking the same type. That would
1050 be a mistake, though--we might load in more symbols which contain a
1051 full definition for the type.
1052
1053 This used to be coded as a macro, but I don't think it is called
1054 often enough to merit such treatment. */
1055
1056 struct complaint stub_noname_complaint =
1057 {"stub type has NULL name", 0, 0};
1058
1059 struct type *
1060 check_typedef (register struct type *type)
1061 {
1062 struct type *orig_type = type;
1063 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1064 {
1065 if (!TYPE_TARGET_TYPE (type))
1066 {
1067 char *name;
1068 struct symbol *sym;
1069
1070 /* It is dangerous to call lookup_symbol if we are currently
1071 reading a symtab. Infinite recursion is one danger. */
1072 if (currently_reading_symtab)
1073 return type;
1074
1075 name = type_name_no_tag (type);
1076 /* FIXME: shouldn't we separately check the TYPE_NAME and the
1077 TYPE_TAG_NAME, and look in STRUCT_NAMESPACE and/or VAR_NAMESPACE
1078 as appropriate? (this code was written before TYPE_NAME and
1079 TYPE_TAG_NAME were separate). */
1080 if (name == NULL)
1081 {
1082 complain (&stub_noname_complaint);
1083 return type;
1084 }
1085 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
1086 (struct symtab **) NULL);
1087 if (sym)
1088 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1089 else
1090 TYPE_TARGET_TYPE (type) = alloc_type (NULL); /* TYPE_CODE_UNDEF */
1091 }
1092 type = TYPE_TARGET_TYPE (type);
1093 }
1094
1095 /* If this is a struct/class/union with no fields, then check whether a
1096 full definition exists somewhere else. This is for systems where a
1097 type definition with no fields is issued for such types, instead of
1098 identifying them as stub types in the first place */
1099
1100 if (TYPE_IS_OPAQUE (type) && opaque_type_resolution && !currently_reading_symtab)
1101 {
1102 char *name = type_name_no_tag (type);
1103 struct type *newtype;
1104 if (name == NULL)
1105 {
1106 complain (&stub_noname_complaint);
1107 return type;
1108 }
1109 newtype = lookup_transparent_type (name);
1110 if (newtype)
1111 {
1112 memcpy ((char *) type, (char *) newtype, sizeof (struct type));
1113 }
1114 }
1115 /* Otherwise, rely on the stub flag being set for opaque/stubbed types */
1116 else if ((TYPE_FLAGS (type) & TYPE_FLAG_STUB) && !currently_reading_symtab)
1117 {
1118 char *name = type_name_no_tag (type);
1119 /* FIXME: shouldn't we separately check the TYPE_NAME and the
1120 TYPE_TAG_NAME, and look in STRUCT_NAMESPACE and/or VAR_NAMESPACE
1121 as appropriate? (this code was written before TYPE_NAME and
1122 TYPE_TAG_NAME were separate). */
1123 struct symbol *sym;
1124 if (name == NULL)
1125 {
1126 complain (&stub_noname_complaint);
1127 return type;
1128 }
1129 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0, (struct symtab **) NULL);
1130 if (sym)
1131 {
1132 memcpy ((char *) type, (char *) SYMBOL_TYPE (sym), sizeof (struct type));
1133 }
1134 }
1135
1136 if (TYPE_FLAGS (type) & TYPE_FLAG_TARGET_STUB)
1137 {
1138 struct type *range_type;
1139 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1140
1141 if (TYPE_FLAGS (target_type) & (TYPE_FLAG_STUB | TYPE_FLAG_TARGET_STUB))
1142 {
1143 }
1144 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1145 && TYPE_NFIELDS (type) == 1
1146 && (TYPE_CODE (range_type = TYPE_FIELD_TYPE (type, 0))
1147 == TYPE_CODE_RANGE))
1148 {
1149 /* Now recompute the length of the array type, based on its
1150 number of elements and the target type's length. */
1151 TYPE_LENGTH (type) =
1152 ((TYPE_FIELD_BITPOS (range_type, 1)
1153 - TYPE_FIELD_BITPOS (range_type, 0)
1154 + 1)
1155 * TYPE_LENGTH (target_type));
1156 TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1157 }
1158 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1159 {
1160 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1161 TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1162 }
1163 }
1164 /* Cache TYPE_LENGTH for future use. */
1165 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1166 return type;
1167 }
1168
1169 /* New code added to support parsing of Cfront stabs strings */
1170 #define INIT_EXTRA { pextras->len=0; pextras->str[0]='\0'; }
1171 #define ADD_EXTRA(c) { pextras->str[pextras->len++]=c; }
1172
1173 static void
1174 add_name (struct extra *pextras, char *n)
1175 {
1176 int nlen;
1177
1178 if ((nlen = (n ? strlen (n) : 0)) == 0)
1179 return;
1180 sprintf (pextras->str + pextras->len, "%d%s", nlen, n);
1181 pextras->len = strlen (pextras->str);
1182 }
1183
1184 static void
1185 add_mangled_type (struct extra *pextras, struct type *t)
1186 {
1187 enum type_code tcode;
1188 int tlen, tflags;
1189 char *tname;
1190
1191 tcode = TYPE_CODE (t);
1192 tlen = TYPE_LENGTH (t);
1193 tflags = TYPE_FLAGS (t);
1194 tname = TYPE_NAME (t);
1195 /* args of "..." seem to get mangled as "e" */
1196
1197 switch (tcode)
1198 {
1199 case TYPE_CODE_INT:
1200 if (tflags == 1)
1201 ADD_EXTRA ('U');
1202 switch (tlen)
1203 {
1204 case 1:
1205 ADD_EXTRA ('c');
1206 break;
1207 case 2:
1208 ADD_EXTRA ('s');
1209 break;
1210 case 4:
1211 {
1212 char *pname;
1213 if ((pname = strrchr (tname, 'l'), pname) && !strcmp (pname, "long"))
1214 {
1215 ADD_EXTRA ('l');
1216 }
1217 else
1218 {
1219 ADD_EXTRA ('i');
1220 }
1221 }
1222 break;
1223 default:
1224 {
1225
1226 static struct complaint msg =
1227 {"Bad int type code length x%x\n", 0, 0};
1228
1229 complain (&msg, tlen);
1230
1231 }
1232 }
1233 break;
1234 case TYPE_CODE_FLT:
1235 switch (tlen)
1236 {
1237 case 4:
1238 ADD_EXTRA ('f');
1239 break;
1240 case 8:
1241 ADD_EXTRA ('d');
1242 break;
1243 case 16:
1244 ADD_EXTRA ('r');
1245 break;
1246 default:
1247 {
1248 static struct complaint msg =
1249 {"Bad float type code length x%x\n", 0, 0};
1250 complain (&msg, tlen);
1251 }
1252 }
1253 break;
1254 case TYPE_CODE_REF:
1255 ADD_EXTRA ('R');
1256 /* followed by what it's a ref to */
1257 break;
1258 case TYPE_CODE_PTR:
1259 ADD_EXTRA ('P');
1260 /* followed by what it's a ptr to */
1261 break;
1262 case TYPE_CODE_TYPEDEF:
1263 {
1264 static struct complaint msg =
1265 {"Typedefs in overloaded functions not yet supported\n", 0, 0};
1266 complain (&msg);
1267 }
1268 /* followed by type bytes & name */
1269 break;
1270 case TYPE_CODE_FUNC:
1271 ADD_EXTRA ('F');
1272 /* followed by func's arg '_' & ret types */
1273 break;
1274 case TYPE_CODE_VOID:
1275 ADD_EXTRA ('v');
1276 break;
1277 case TYPE_CODE_METHOD:
1278 ADD_EXTRA ('M');
1279 /* followed by name of class and func's arg '_' & ret types */
1280 add_name (pextras, tname);
1281 ADD_EXTRA ('F'); /* then mangle function */
1282 break;
1283 case TYPE_CODE_STRUCT: /* C struct */
1284 case TYPE_CODE_UNION: /* C union */
1285 case TYPE_CODE_ENUM: /* Enumeration type */
1286 /* followed by name of type */
1287 add_name (pextras, tname);
1288 break;
1289
1290 /* errors possible types/not supported */
1291 case TYPE_CODE_CHAR:
1292 case TYPE_CODE_ARRAY: /* Array type */
1293 case TYPE_CODE_MEMBER: /* Member type */
1294 case TYPE_CODE_BOOL:
1295 case TYPE_CODE_COMPLEX: /* Complex float */
1296 case TYPE_CODE_UNDEF:
1297 case TYPE_CODE_SET: /* Pascal sets */
1298 case TYPE_CODE_RANGE:
1299 case TYPE_CODE_STRING:
1300 case TYPE_CODE_BITSTRING:
1301 case TYPE_CODE_ERROR:
1302 default:
1303 {
1304 static struct complaint msg =
1305 {"Unknown type code x%x\n", 0, 0};
1306 complain (&msg, tcode);
1307 }
1308 }
1309 if (t->target_type)
1310 add_mangled_type (pextras, t->target_type);
1311 }
1312
1313 #if 0
1314 void
1315 cfront_mangle_name (struct type *type, int i, int j)
1316 {
1317 struct fn_field *f;
1318 char *mangled_name = gdb_mangle_name (type, i, j);
1319
1320 f = TYPE_FN_FIELDLIST1 (type, i); /* moved from below */
1321
1322 /* kludge to support cfront methods - gdb expects to find "F" for
1323 ARM_mangled names, so when we mangle, we have to add it here */
1324 if (ARM_DEMANGLING)
1325 {
1326 int k;
1327 char *arm_mangled_name;
1328 struct fn_field *method = &f[j];
1329 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1330 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1331 char *newname = type_name_no_tag (type);
1332
1333 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1334 int nargs = TYPE_NFIELDS (ftype); /* number of args */
1335 struct extra extras, *pextras = &extras;
1336 INIT_EXTRA
1337
1338 if (TYPE_FN_FIELD_STATIC_P (f, j)) /* j for sublist within this list */
1339 ADD_EXTRA ('S')
1340 ADD_EXTRA ('F')
1341 /* add args here! */
1342 if (nargs <= 1) /* no args besides this */
1343 ADD_EXTRA ('v')
1344 else
1345 {
1346 for (k = 1; k < nargs; k++)
1347 {
1348 struct type *t;
1349 t = TYPE_FIELD_TYPE (ftype, k);
1350 add_mangled_type (pextras, t);
1351 }
1352 }
1353 ADD_EXTRA ('\0')
1354 printf ("add_mangled_type: %s\n", extras.str); /* FIXME */
1355 xasprintf (&arm_mangled_name, "%s%s", mangled_name, extras.str);
1356 xfree (mangled_name);
1357 mangled_name = arm_mangled_name;
1358 }
1359 }
1360 #endif /* 0 */
1361
1362 #undef ADD_EXTRA
1363 /* End of new code added to support parsing of Cfront stabs strings */
1364
1365 /* Parse a type expression in the string [P..P+LENGTH). If an error occurs,
1366 silently return builtin_type_void. */
1367
1368 struct type *
1369 safe_parse_type (char *p, int length)
1370 {
1371 struct ui_file *saved_gdb_stderr;
1372 struct type *type;
1373
1374 /* Suppress error messages. */
1375 saved_gdb_stderr = gdb_stderr;
1376 gdb_stderr = ui_file_new ();
1377
1378 /* Call parse_and_eval_type() without fear of longjmp()s. */
1379 if (!gdb_parse_and_eval_type (p, length, &type))
1380 type = builtin_type_void;
1381
1382 /* Stop suppressing error messages. */
1383 ui_file_delete (gdb_stderr);
1384 gdb_stderr = saved_gdb_stderr;
1385
1386 return type;
1387 }
1388
1389 /* Ugly hack to convert method stubs into method types.
1390
1391 He ain't kiddin'. This demangles the name of the method into a string
1392 including argument types, parses out each argument type, generates
1393 a string casting a zero to that type, evaluates the string, and stuffs
1394 the resulting type into an argtype vector!!! Then it knows the type
1395 of the whole function (including argument types for overloading),
1396 which info used to be in the stab's but was removed to hack back
1397 the space required for them. */
1398
1399 void
1400 check_stub_method (struct type *type, int method_id, int signature_id)
1401 {
1402 struct fn_field *f;
1403 char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1404 char *demangled_name = cplus_demangle (mangled_name,
1405 DMGL_PARAMS | DMGL_ANSI);
1406 char *argtypetext, *p;
1407 int depth = 0, argcount = 1;
1408 struct type **argtypes;
1409 struct type *mtype;
1410
1411 /* Make sure we got back a function string that we can use. */
1412 if (demangled_name)
1413 p = strchr (demangled_name, '(');
1414 else
1415 p = NULL;
1416
1417 if (demangled_name == NULL || p == NULL)
1418 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
1419
1420 /* Now, read in the parameters that define this type. */
1421 p += 1;
1422 argtypetext = p;
1423 while (*p)
1424 {
1425 if (*p == '(' || *p == '<')
1426 {
1427 depth += 1;
1428 }
1429 else if (*p == ')' || *p == '>')
1430 {
1431 depth -= 1;
1432 }
1433 else if (*p == ',' && depth == 0)
1434 {
1435 argcount += 1;
1436 }
1437
1438 p += 1;
1439 }
1440
1441 /* We need two more slots: one for the THIS pointer, and one for the
1442 NULL [...] or void [end of arglist]. */
1443
1444 argtypes = (struct type **)
1445 TYPE_ALLOC (type, (argcount + 2) * sizeof (struct type *));
1446 p = argtypetext;
1447 /* FIXME: This is wrong for static member functions. */
1448 argtypes[0] = lookup_pointer_type (type);
1449 argcount = 1;
1450
1451 if (*p != ')') /* () means no args, skip while */
1452 {
1453 depth = 0;
1454 while (*p)
1455 {
1456 if (depth <= 0 && (*p == ',' || *p == ')'))
1457 {
1458 /* Avoid parsing of ellipsis, they will be handled below. */
1459 if (strncmp (argtypetext, "...", p - argtypetext) != 0)
1460 {
1461 argtypes[argcount] =
1462 safe_parse_type (argtypetext, p - argtypetext);
1463 argcount += 1;
1464 }
1465 argtypetext = p + 1;
1466 }
1467
1468 if (*p == '(' || *p == '<')
1469 {
1470 depth += 1;
1471 }
1472 else if (*p == ')' || *p == '>')
1473 {
1474 depth -= 1;
1475 }
1476
1477 p += 1;
1478 }
1479 }
1480
1481 if (p[-2] != '.') /* Not '...' */
1482 {
1483 argtypes[argcount] = builtin_type_void; /* List terminator */
1484 }
1485 else
1486 {
1487 argtypes[argcount] = NULL; /* Ellist terminator */
1488 }
1489
1490 xfree (demangled_name);
1491
1492 f = TYPE_FN_FIELDLIST1 (type, method_id);
1493
1494 TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1495
1496 /* Now update the old "stub" type into a real type. */
1497 mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1498 TYPE_DOMAIN_TYPE (mtype) = type;
1499 TYPE_ARG_TYPES (mtype) = argtypes;
1500 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
1501 TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1502 }
1503
1504 const struct cplus_struct_type cplus_struct_default;
1505
1506 void
1507 allocate_cplus_struct_type (struct type *type)
1508 {
1509 if (!HAVE_CPLUS_STRUCT (type))
1510 {
1511 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1512 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1513 *(TYPE_CPLUS_SPECIFIC (type)) = cplus_struct_default;
1514 }
1515 }
1516
1517 /* Helper function to initialize the standard scalar types.
1518
1519 If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
1520 of the string pointed to by name in the type_obstack for that objfile,
1521 and initialize the type name to that copy. There are places (mipsread.c
1522 in particular, where init_type is called with a NULL value for NAME). */
1523
1524 struct type *
1525 init_type (enum type_code code, int length, int flags, char *name,
1526 struct objfile *objfile)
1527 {
1528 register struct type *type;
1529
1530 type = alloc_type (objfile);
1531 TYPE_CODE (type) = code;
1532 TYPE_LENGTH (type) = length;
1533 TYPE_FLAGS (type) |= flags;
1534 if ((name != NULL) && (objfile != NULL))
1535 {
1536 TYPE_NAME (type) =
1537 obsavestring (name, strlen (name), &objfile->type_obstack);
1538 }
1539 else
1540 {
1541 TYPE_NAME (type) = name;
1542 }
1543
1544 /* C++ fancies. */
1545
1546 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
1547 {
1548 INIT_CPLUS_SPECIFIC (type);
1549 }
1550 return (type);
1551 }
1552
1553 /* Look up a fundamental type for the specified objfile.
1554 May need to construct such a type if this is the first use.
1555
1556 Some object file formats (ELF, COFF, etc) do not define fundamental
1557 types such as "int" or "double". Others (stabs for example), do
1558 define fundamental types.
1559
1560 For the formats which don't provide fundamental types, gdb can create
1561 such types, using defaults reasonable for the current language and
1562 the current target machine.
1563
1564 NOTE: This routine is obsolescent. Each debugging format reader
1565 should manage it's own fundamental types, either creating them from
1566 suitable defaults or reading them from the debugging information,
1567 whichever is appropriate. The DWARF reader has already been
1568 fixed to do this. Once the other readers are fixed, this routine
1569 will go away. Also note that fundamental types should be managed
1570 on a compilation unit basis in a multi-language environment, not
1571 on a linkage unit basis as is done here. */
1572
1573
1574 struct type *
1575 lookup_fundamental_type (struct objfile *objfile, int typeid)
1576 {
1577 register struct type **typep;
1578 register int nbytes;
1579
1580 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
1581 {
1582 error ("internal error - invalid fundamental type id %d", typeid);
1583 }
1584
1585 /* If this is the first time we need a fundamental type for this objfile
1586 then we need to initialize the vector of type pointers. */
1587
1588 if (objfile->fundamental_types == NULL)
1589 {
1590 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
1591 objfile->fundamental_types = (struct type **)
1592 obstack_alloc (&objfile->type_obstack, nbytes);
1593 memset ((char *) objfile->fundamental_types, 0, nbytes);
1594 OBJSTAT (objfile, n_types += FT_NUM_MEMBERS);
1595 }
1596
1597 /* Look for this particular type in the fundamental type vector. If one is
1598 not found, create and install one appropriate for the current language. */
1599
1600 typep = objfile->fundamental_types + typeid;
1601 if (*typep == NULL)
1602 {
1603 *typep = create_fundamental_type (objfile, typeid);
1604 }
1605
1606 return (*typep);
1607 }
1608
1609 int
1610 can_dereference (struct type *t)
1611 {
1612 /* FIXME: Should we return true for references as well as pointers? */
1613 CHECK_TYPEDEF (t);
1614 return
1615 (t != NULL
1616 && TYPE_CODE (t) == TYPE_CODE_PTR
1617 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1618 }
1619
1620 int
1621 is_integral_type (struct type *t)
1622 {
1623 CHECK_TYPEDEF (t);
1624 return
1625 ((t != NULL)
1626 && ((TYPE_CODE (t) == TYPE_CODE_INT)
1627 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
1628 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
1629 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
1630 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
1631 }
1632
1633 /* Chill varying string and arrays are represented as follows:
1634
1635 struct { int __var_length; ELEMENT_TYPE[MAX_SIZE] __var_data};
1636
1637 Return true if TYPE is such a Chill varying type. */
1638
1639 int
1640 chill_varying_type (struct type *type)
1641 {
1642 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1643 || TYPE_NFIELDS (type) != 2
1644 || strcmp (TYPE_FIELD_NAME (type, 0), "__var_length") != 0)
1645 return 0;
1646 return 1;
1647 }
1648
1649 /* Check whether BASE is an ancestor or base class or DCLASS
1650 Return 1 if so, and 0 if not.
1651 Note: callers may want to check for identity of the types before
1652 calling this function -- identical types are considered to satisfy
1653 the ancestor relationship even if they're identical */
1654
1655 int
1656 is_ancestor (struct type *base, struct type *dclass)
1657 {
1658 int i;
1659
1660 CHECK_TYPEDEF (base);
1661 CHECK_TYPEDEF (dclass);
1662
1663 if (base == dclass)
1664 return 1;
1665 if (TYPE_NAME (base) && TYPE_NAME (dclass) &&
1666 !strcmp (TYPE_NAME (base), TYPE_NAME (dclass)))
1667 return 1;
1668
1669 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1670 if (is_ancestor (base, TYPE_BASECLASS (dclass, i)))
1671 return 1;
1672
1673 return 0;
1674 }
1675
1676
1677
1678 /* See whether DCLASS has a virtual table. This routine is aimed at
1679 the HP/Taligent ANSI C++ runtime model, and may not work with other
1680 runtime models. Return 1 => Yes, 0 => No. */
1681
1682 int
1683 has_vtable (struct type *dclass)
1684 {
1685 /* In the HP ANSI C++ runtime model, a class has a vtable only if it
1686 has virtual functions or virtual bases. */
1687
1688 register int i;
1689
1690 if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1691 return 0;
1692
1693 /* First check for the presence of virtual bases */
1694 if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1695 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1696 if (B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i))
1697 return 1;
1698
1699 /* Next check for virtual functions */
1700 if (TYPE_FN_FIELDLISTS (dclass))
1701 for (i = 0; i < TYPE_NFN_FIELDS (dclass); i++)
1702 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, i), 0))
1703 return 1;
1704
1705 /* Recurse on non-virtual bases to see if any of them needs a vtable */
1706 if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1707 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1708 if ((!B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i)) &&
1709 (has_vtable (TYPE_FIELD_TYPE (dclass, i))))
1710 return 1;
1711
1712 /* Well, maybe we don't need a virtual table */
1713 return 0;
1714 }
1715
1716 /* Return a pointer to the "primary base class" of DCLASS.
1717
1718 A NULL return indicates that DCLASS has no primary base, or that it
1719 couldn't be found (insufficient information).
1720
1721 This routine is aimed at the HP/Taligent ANSI C++ runtime model,
1722 and may not work with other runtime models. */
1723
1724 struct type *
1725 primary_base_class (struct type *dclass)
1726 {
1727 /* In HP ANSI C++'s runtime model, a "primary base class" of a class
1728 is the first directly inherited, non-virtual base class that
1729 requires a virtual table */
1730
1731 register int i;
1732
1733 if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1734 return NULL;
1735
1736 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1737 if (!TYPE_FIELD_VIRTUAL (dclass, i) &&
1738 has_vtable (TYPE_FIELD_TYPE (dclass, i)))
1739 return TYPE_FIELD_TYPE (dclass, i);
1740
1741 return NULL;
1742 }
1743
1744 /* Global manipulated by virtual_base_list[_aux]() */
1745
1746 static struct vbase *current_vbase_list = NULL;
1747
1748 /* Return a pointer to a null-terminated list of struct vbase
1749 items. The vbasetype pointer of each item in the list points to the
1750 type information for a virtual base of the argument DCLASS.
1751
1752 Helper function for virtual_base_list().
1753 Note: the list goes backward, right-to-left. virtual_base_list()
1754 copies the items out in reverse order. */
1755
1756 static void
1757 virtual_base_list_aux (struct type *dclass)
1758 {
1759 struct vbase *tmp_vbase;
1760 register int i;
1761
1762 if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1763 return;
1764
1765 for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1766 {
1767 /* Recurse on this ancestor, first */
1768 virtual_base_list_aux (TYPE_FIELD_TYPE (dclass, i));
1769
1770 /* If this current base is itself virtual, add it to the list */
1771 if (BASETYPE_VIA_VIRTUAL (dclass, i))
1772 {
1773 struct type *basetype = TYPE_FIELD_TYPE (dclass, i);
1774
1775 /* Check if base already recorded */
1776 tmp_vbase = current_vbase_list;
1777 while (tmp_vbase)
1778 {
1779 if (tmp_vbase->vbasetype == basetype)
1780 break; /* found it */
1781 tmp_vbase = tmp_vbase->next;
1782 }
1783
1784 if (!tmp_vbase) /* normal exit from loop */
1785 {
1786 /* Allocate new item for this virtual base */
1787 tmp_vbase = (struct vbase *) xmalloc (sizeof (struct vbase));
1788
1789 /* Stick it on at the end of the list */
1790 tmp_vbase->vbasetype = basetype;
1791 tmp_vbase->next = current_vbase_list;
1792 current_vbase_list = tmp_vbase;
1793 }
1794 } /* if virtual */
1795 } /* for loop over bases */
1796 }
1797
1798
1799 /* Compute the list of virtual bases in the right order. Virtual
1800 bases are laid out in the object's memory area in order of their
1801 occurrence in a depth-first, left-to-right search through the
1802 ancestors.
1803
1804 Argument DCLASS is the type whose virtual bases are required.
1805 Return value is the address of a null-terminated array of pointers
1806 to struct type items.
1807
1808 This routine is aimed at the HP/Taligent ANSI C++ runtime model,
1809 and may not work with other runtime models.
1810
1811 This routine merely hands off the argument to virtual_base_list_aux()
1812 and then copies the result into an array to save space. */
1813
1814 struct type **
1815 virtual_base_list (struct type *dclass)
1816 {
1817 register struct vbase *tmp_vbase;
1818 register struct vbase *tmp_vbase_2;
1819 register int i;
1820 int count;
1821 struct type **vbase_array;
1822
1823 current_vbase_list = NULL;
1824 virtual_base_list_aux (dclass);
1825
1826 for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
1827 /* no body */ ;
1828
1829 count = i;
1830
1831 vbase_array = (struct type **) xmalloc ((count + 1) * sizeof (struct type *));
1832
1833 for (i = count - 1, tmp_vbase = current_vbase_list; i >= 0; i--, tmp_vbase = tmp_vbase->next)
1834 vbase_array[i] = tmp_vbase->vbasetype;
1835
1836 /* Get rid of constructed chain */
1837 tmp_vbase_2 = tmp_vbase = current_vbase_list;
1838 while (tmp_vbase)
1839 {
1840 tmp_vbase = tmp_vbase->next;
1841 xfree (tmp_vbase_2);
1842 tmp_vbase_2 = tmp_vbase;
1843 }
1844
1845 vbase_array[count] = NULL;
1846 return vbase_array;
1847 }
1848
1849 /* Return the length of the virtual base list of the type DCLASS. */
1850
1851 int
1852 virtual_base_list_length (struct type *dclass)
1853 {
1854 register int i;
1855 register struct vbase *tmp_vbase;
1856
1857 current_vbase_list = NULL;
1858 virtual_base_list_aux (dclass);
1859
1860 for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
1861 /* no body */ ;
1862 return i;
1863 }
1864
1865 /* Return the number of elements of the virtual base list of the type
1866 DCLASS, ignoring those appearing in the primary base (and its
1867 primary base, recursively). */
1868
1869 int
1870 virtual_base_list_length_skip_primaries (struct type *dclass)
1871 {
1872 register int i;
1873 register struct vbase *tmp_vbase;
1874 struct type *primary;
1875
1876 primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
1877
1878 if (!primary)
1879 return virtual_base_list_length (dclass);
1880
1881 current_vbase_list = NULL;
1882 virtual_base_list_aux (dclass);
1883
1884 for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; tmp_vbase = tmp_vbase->next)
1885 {
1886 if (virtual_base_index (tmp_vbase->vbasetype, primary) >= 0)
1887 continue;
1888 i++;
1889 }
1890 return i;
1891 }
1892
1893
1894 /* Return the index (position) of type BASE, which is a virtual base
1895 class of DCLASS, in the latter's virtual base list. A return of -1
1896 indicates "not found" or a problem. */
1897
1898 int
1899 virtual_base_index (struct type *base, struct type *dclass)
1900 {
1901 register struct type *vbase;
1902 register int i;
1903
1904 if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
1905 (TYPE_CODE (base) != TYPE_CODE_CLASS))
1906 return -1;
1907
1908 i = 0;
1909 vbase = virtual_base_list (dclass)[0];
1910 while (vbase)
1911 {
1912 if (vbase == base)
1913 break;
1914 vbase = virtual_base_list (dclass)[++i];
1915 }
1916
1917 return vbase ? i : -1;
1918 }
1919
1920
1921
1922 /* Return the index (position) of type BASE, which is a virtual base
1923 class of DCLASS, in the latter's virtual base list. Skip over all
1924 bases that may appear in the virtual base list of the primary base
1925 class of DCLASS (recursively). A return of -1 indicates "not
1926 found" or a problem. */
1927
1928 int
1929 virtual_base_index_skip_primaries (struct type *base, struct type *dclass)
1930 {
1931 register struct type *vbase;
1932 register int i, j;
1933 struct type *primary;
1934
1935 if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
1936 (TYPE_CODE (base) != TYPE_CODE_CLASS))
1937 return -1;
1938
1939 primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
1940
1941 j = -1;
1942 i = 0;
1943 vbase = virtual_base_list (dclass)[0];
1944 while (vbase)
1945 {
1946 if (!primary || (virtual_base_index_skip_primaries (vbase, primary) < 0))
1947 j++;
1948 if (vbase == base)
1949 break;
1950 vbase = virtual_base_list (dclass)[++i];
1951 }
1952
1953 return vbase ? j : -1;
1954 }
1955
1956 /* Return position of a derived class DCLASS in the list of
1957 * primary bases starting with the remotest ancestor.
1958 * Position returned is 0-based. */
1959
1960 int
1961 class_index_in_primary_list (struct type *dclass)
1962 {
1963 struct type *pbc; /* primary base class */
1964
1965 /* Simply recurse on primary base */
1966 pbc = TYPE_PRIMARY_BASE (dclass);
1967 if (pbc)
1968 return 1 + class_index_in_primary_list (pbc);
1969 else
1970 return 0;
1971 }
1972
1973 /* Return a count of the number of virtual functions a type has.
1974 * This includes all the virtual functions it inherits from its
1975 * base classes too.
1976 */
1977
1978 /* pai: FIXME This doesn't do the right thing: count redefined virtual
1979 * functions only once (latest redefinition)
1980 */
1981
1982 int
1983 count_virtual_fns (struct type *dclass)
1984 {
1985 int fn, oi; /* function and overloaded instance indices */
1986 int vfuncs; /* count to return */
1987
1988 /* recurse on bases that can share virtual table */
1989 struct type *pbc = primary_base_class (dclass);
1990 if (pbc)
1991 vfuncs = count_virtual_fns (pbc);
1992 else
1993 vfuncs = 0;
1994
1995 for (fn = 0; fn < TYPE_NFN_FIELDS (dclass); fn++)
1996 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (dclass, fn); oi++)
1997 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, fn), oi))
1998 vfuncs++;
1999
2000 return vfuncs;
2001 }
2002 \f
2003
2004
2005 /* Functions for overload resolution begin here */
2006
2007 /* Compare two badness vectors A and B and return the result.
2008 * 0 => A and B are identical
2009 * 1 => A and B are incomparable
2010 * 2 => A is better than B
2011 * 3 => A is worse than B */
2012
2013 int
2014 compare_badness (struct badness_vector *a, struct badness_vector *b)
2015 {
2016 int i;
2017 int tmp;
2018 short found_pos = 0; /* any positives in c? */
2019 short found_neg = 0; /* any negatives in c? */
2020
2021 /* differing lengths => incomparable */
2022 if (a->length != b->length)
2023 return 1;
2024
2025 /* Subtract b from a */
2026 for (i = 0; i < a->length; i++)
2027 {
2028 tmp = a->rank[i] - b->rank[i];
2029 if (tmp > 0)
2030 found_pos = 1;
2031 else if (tmp < 0)
2032 found_neg = 1;
2033 }
2034
2035 if (found_pos)
2036 {
2037 if (found_neg)
2038 return 1; /* incomparable */
2039 else
2040 return 3; /* A > B */
2041 }
2042 else
2043 /* no positives */
2044 {
2045 if (found_neg)
2046 return 2; /* A < B */
2047 else
2048 return 0; /* A == B */
2049 }
2050 }
2051
2052 /* Rank a function by comparing its parameter types (PARMS, length NPARMS),
2053 * to the types of an argument list (ARGS, length NARGS).
2054 * Return a pointer to a badness vector. This has NARGS + 1 entries. */
2055
2056 struct badness_vector *
2057 rank_function (struct type **parms, int nparms, struct type **args, int nargs)
2058 {
2059 int i;
2060 struct badness_vector *bv;
2061 int min_len = nparms < nargs ? nparms : nargs;
2062
2063 bv = xmalloc (sizeof (struct badness_vector));
2064 bv->length = nargs + 1; /* add 1 for the length-match rank */
2065 bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2066
2067 /* First compare the lengths of the supplied lists.
2068 * If there is a mismatch, set it to a high value. */
2069
2070 /* pai/1997-06-03 FIXME: when we have debug info about default
2071 * arguments and ellipsis parameter lists, we should consider those
2072 * and rank the length-match more finely. */
2073
2074 LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
2075
2076 /* Now rank all the parameters of the candidate function */
2077 for (i = 1; i <= min_len; i++)
2078 bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
2079
2080 /* If more arguments than parameters, add dummy entries */
2081 for (i = min_len + 1; i <= nargs; i++)
2082 bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2083
2084 return bv;
2085 }
2086
2087 /* Compare one type (PARM) for compatibility with another (ARG).
2088 * PARM is intended to be the parameter type of a function; and
2089 * ARG is the supplied argument's type. This function tests if
2090 * the latter can be converted to the former.
2091 *
2092 * Return 0 if they are identical types;
2093 * Otherwise, return an integer which corresponds to how compatible
2094 * PARM is to ARG. The higher the return value, the worse the match.
2095 * Generally the "bad" conversions are all uniformly assigned a 100 */
2096
2097 int
2098 rank_one_type (struct type *parm, struct type *arg)
2099 {
2100 /* Identical type pointers */
2101 /* However, this still doesn't catch all cases of same type for arg
2102 * and param. The reason is that builtin types are different from
2103 * the same ones constructed from the object. */
2104 if (parm == arg)
2105 return 0;
2106
2107 /* Resolve typedefs */
2108 if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2109 parm = check_typedef (parm);
2110 if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2111 arg = check_typedef (arg);
2112
2113 /*
2114 Well, damnit, if the names are exactly the same,
2115 i'll say they are exactly the same. This happens when we generate
2116 method stubs. The types won't point to the same address, but they
2117 really are the same.
2118 */
2119
2120 if (TYPE_NAME (parm) && TYPE_NAME (arg) &&
2121 !strcmp (TYPE_NAME (parm), TYPE_NAME (arg)))
2122 return 0;
2123
2124 /* Check if identical after resolving typedefs */
2125 if (parm == arg)
2126 return 0;
2127
2128 /* See through references, since we can almost make non-references
2129 references. */
2130 if (TYPE_CODE (arg) == TYPE_CODE_REF)
2131 return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
2132 + REFERENCE_CONVERSION_BADNESS);
2133 if (TYPE_CODE (parm) == TYPE_CODE_REF)
2134 return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
2135 + REFERENCE_CONVERSION_BADNESS);
2136 if (overload_debug)
2137 /* Debugging only. */
2138 fprintf_filtered (gdb_stderr,"------ Arg is %s [%d], parm is %s [%d]\n",
2139 TYPE_NAME (arg), TYPE_CODE (arg), TYPE_NAME (parm), TYPE_CODE (parm));
2140
2141 /* x -> y means arg of type x being supplied for parameter of type y */
2142
2143 switch (TYPE_CODE (parm))
2144 {
2145 case TYPE_CODE_PTR:
2146 switch (TYPE_CODE (arg))
2147 {
2148 case TYPE_CODE_PTR:
2149 if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2150 return VOID_PTR_CONVERSION_BADNESS;
2151 else
2152 return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2153 case TYPE_CODE_ARRAY:
2154 return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2155 case TYPE_CODE_FUNC:
2156 return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2157 case TYPE_CODE_INT:
2158 case TYPE_CODE_ENUM:
2159 case TYPE_CODE_CHAR:
2160 case TYPE_CODE_RANGE:
2161 case TYPE_CODE_BOOL:
2162 return POINTER_CONVERSION_BADNESS;
2163 default:
2164 return INCOMPATIBLE_TYPE_BADNESS;
2165 }
2166 case TYPE_CODE_ARRAY:
2167 switch (TYPE_CODE (arg))
2168 {
2169 case TYPE_CODE_PTR:
2170 case TYPE_CODE_ARRAY:
2171 return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2172 default:
2173 return INCOMPATIBLE_TYPE_BADNESS;
2174 }
2175 case TYPE_CODE_FUNC:
2176 switch (TYPE_CODE (arg))
2177 {
2178 case TYPE_CODE_PTR: /* funcptr -> func */
2179 return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2180 default:
2181 return INCOMPATIBLE_TYPE_BADNESS;
2182 }
2183 case TYPE_CODE_INT:
2184 switch (TYPE_CODE (arg))
2185 {
2186 case TYPE_CODE_INT:
2187 if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2188 {
2189 /* Deal with signed, unsigned, and plain chars and
2190 signed and unsigned ints */
2191 if (TYPE_NOSIGN (parm))
2192 {
2193 /* This case only for character types */
2194 if (TYPE_NOSIGN (arg)) /* plain char -> plain char */
2195 return 0;
2196 else
2197 return INTEGER_COERCION_BADNESS; /* signed/unsigned char -> plain char */
2198 }
2199 else if (TYPE_UNSIGNED (parm))
2200 {
2201 if (TYPE_UNSIGNED (arg))
2202 {
2203 if (!strcmp_iw (TYPE_NAME (parm), TYPE_NAME (arg)))
2204 return 0; /* unsigned int -> unsigned int, or unsigned long -> unsigned long */
2205 else if (!strcmp_iw (TYPE_NAME (arg), "int") && !strcmp_iw (TYPE_NAME (parm), "long"))
2206 return INTEGER_PROMOTION_BADNESS; /* unsigned int -> unsigned long */
2207 else
2208 return INTEGER_COERCION_BADNESS; /* unsigned long -> unsigned int */
2209 }
2210 else
2211 {
2212 if (!strcmp_iw (TYPE_NAME (arg), "long") && !strcmp_iw (TYPE_NAME (parm), "int"))
2213 return INTEGER_COERCION_BADNESS; /* signed long -> unsigned int */
2214 else
2215 return INTEGER_CONVERSION_BADNESS; /* signed int/long -> unsigned int/long */
2216 }
2217 }
2218 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2219 {
2220 if (!strcmp_iw (TYPE_NAME (parm), TYPE_NAME (arg)))
2221 return 0;
2222 else if (!strcmp_iw (TYPE_NAME (arg), "int") && !strcmp_iw (TYPE_NAME (parm), "long"))
2223 return INTEGER_PROMOTION_BADNESS;
2224 else
2225 return INTEGER_COERCION_BADNESS;
2226 }
2227 else
2228 return INTEGER_COERCION_BADNESS;
2229 }
2230 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2231 return INTEGER_PROMOTION_BADNESS;
2232 else
2233 return INTEGER_COERCION_BADNESS;
2234 case TYPE_CODE_ENUM:
2235 case TYPE_CODE_CHAR:
2236 case TYPE_CODE_RANGE:
2237 case TYPE_CODE_BOOL:
2238 return INTEGER_PROMOTION_BADNESS;
2239 case TYPE_CODE_FLT:
2240 return INT_FLOAT_CONVERSION_BADNESS;
2241 case TYPE_CODE_PTR:
2242 return NS_POINTER_CONVERSION_BADNESS;
2243 default:
2244 return INCOMPATIBLE_TYPE_BADNESS;
2245 }
2246 break;
2247 case TYPE_CODE_ENUM:
2248 switch (TYPE_CODE (arg))
2249 {
2250 case TYPE_CODE_INT:
2251 case TYPE_CODE_CHAR:
2252 case TYPE_CODE_RANGE:
2253 case TYPE_CODE_BOOL:
2254 case TYPE_CODE_ENUM:
2255 return INTEGER_COERCION_BADNESS;
2256 case TYPE_CODE_FLT:
2257 return INT_FLOAT_CONVERSION_BADNESS;
2258 default:
2259 return INCOMPATIBLE_TYPE_BADNESS;
2260 }
2261 break;
2262 case TYPE_CODE_CHAR:
2263 switch (TYPE_CODE (arg))
2264 {
2265 case TYPE_CODE_RANGE:
2266 case TYPE_CODE_BOOL:
2267 case TYPE_CODE_ENUM:
2268 return INTEGER_COERCION_BADNESS;
2269 case TYPE_CODE_FLT:
2270 return INT_FLOAT_CONVERSION_BADNESS;
2271 case TYPE_CODE_INT:
2272 if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2273 return INTEGER_COERCION_BADNESS;
2274 else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2275 return INTEGER_PROMOTION_BADNESS;
2276 /* >>> !! else fall through !! <<< */
2277 case TYPE_CODE_CHAR:
2278 /* Deal with signed, unsigned, and plain chars for C++
2279 and with int cases falling through from previous case */
2280 if (TYPE_NOSIGN (parm))
2281 {
2282 if (TYPE_NOSIGN (arg))
2283 return 0;
2284 else
2285 return INTEGER_COERCION_BADNESS;
2286 }
2287 else if (TYPE_UNSIGNED (parm))
2288 {
2289 if (TYPE_UNSIGNED (arg))
2290 return 0;
2291 else
2292 return INTEGER_PROMOTION_BADNESS;
2293 }
2294 else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2295 return 0;
2296 else
2297 return INTEGER_COERCION_BADNESS;
2298 default:
2299 return INCOMPATIBLE_TYPE_BADNESS;
2300 }
2301 break;
2302 case TYPE_CODE_RANGE:
2303 switch (TYPE_CODE (arg))
2304 {
2305 case TYPE_CODE_INT:
2306 case TYPE_CODE_CHAR:
2307 case TYPE_CODE_RANGE:
2308 case TYPE_CODE_BOOL:
2309 case TYPE_CODE_ENUM:
2310 return INTEGER_COERCION_BADNESS;
2311 case TYPE_CODE_FLT:
2312 return INT_FLOAT_CONVERSION_BADNESS;
2313 default:
2314 return INCOMPATIBLE_TYPE_BADNESS;
2315 }
2316 break;
2317 case TYPE_CODE_BOOL:
2318 switch (TYPE_CODE (arg))
2319 {
2320 case TYPE_CODE_INT:
2321 case TYPE_CODE_CHAR:
2322 case TYPE_CODE_RANGE:
2323 case TYPE_CODE_ENUM:
2324 case TYPE_CODE_FLT:
2325 case TYPE_CODE_PTR:
2326 return BOOLEAN_CONVERSION_BADNESS;
2327 case TYPE_CODE_BOOL:
2328 return 0;
2329 default:
2330 return INCOMPATIBLE_TYPE_BADNESS;
2331 }
2332 break;
2333 case TYPE_CODE_FLT:
2334 switch (TYPE_CODE (arg))
2335 {
2336 case TYPE_CODE_FLT:
2337 if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2338 return FLOAT_PROMOTION_BADNESS;
2339 else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2340 return 0;
2341 else
2342 return FLOAT_CONVERSION_BADNESS;
2343 case TYPE_CODE_INT:
2344 case TYPE_CODE_BOOL:
2345 case TYPE_CODE_ENUM:
2346 case TYPE_CODE_RANGE:
2347 case TYPE_CODE_CHAR:
2348 return INT_FLOAT_CONVERSION_BADNESS;
2349 default:
2350 return INCOMPATIBLE_TYPE_BADNESS;
2351 }
2352 break;
2353 case TYPE_CODE_COMPLEX:
2354 switch (TYPE_CODE (arg))
2355 { /* Strictly not needed for C++, but... */
2356 case TYPE_CODE_FLT:
2357 return FLOAT_PROMOTION_BADNESS;
2358 case TYPE_CODE_COMPLEX:
2359 return 0;
2360 default:
2361 return INCOMPATIBLE_TYPE_BADNESS;
2362 }
2363 break;
2364 case TYPE_CODE_STRUCT:
2365 /* currently same as TYPE_CODE_CLASS */
2366 switch (TYPE_CODE (arg))
2367 {
2368 case TYPE_CODE_STRUCT:
2369 /* Check for derivation */
2370 if (is_ancestor (parm, arg))
2371 return BASE_CONVERSION_BADNESS;
2372 /* else fall through */
2373 default:
2374 return INCOMPATIBLE_TYPE_BADNESS;
2375 }
2376 break;
2377 case TYPE_CODE_UNION:
2378 switch (TYPE_CODE (arg))
2379 {
2380 case TYPE_CODE_UNION:
2381 default:
2382 return INCOMPATIBLE_TYPE_BADNESS;
2383 }
2384 break;
2385 case TYPE_CODE_MEMBER:
2386 switch (TYPE_CODE (arg))
2387 {
2388 default:
2389 return INCOMPATIBLE_TYPE_BADNESS;
2390 }
2391 break;
2392 case TYPE_CODE_METHOD:
2393 switch (TYPE_CODE (arg))
2394 {
2395
2396 default:
2397 return INCOMPATIBLE_TYPE_BADNESS;
2398 }
2399 break;
2400 case TYPE_CODE_REF:
2401 switch (TYPE_CODE (arg))
2402 {
2403
2404 default:
2405 return INCOMPATIBLE_TYPE_BADNESS;
2406 }
2407
2408 break;
2409 case TYPE_CODE_SET:
2410 switch (TYPE_CODE (arg))
2411 {
2412 /* Not in C++ */
2413 case TYPE_CODE_SET:
2414 return rank_one_type (TYPE_FIELD_TYPE (parm, 0), TYPE_FIELD_TYPE (arg, 0));
2415 default:
2416 return INCOMPATIBLE_TYPE_BADNESS;
2417 }
2418 break;
2419 case TYPE_CODE_VOID:
2420 default:
2421 return INCOMPATIBLE_TYPE_BADNESS;
2422 } /* switch (TYPE_CODE (arg)) */
2423 }
2424
2425
2426 /* End of functions for overload resolution */
2427
2428 static void
2429 print_bit_vector (B_TYPE *bits, int nbits)
2430 {
2431 int bitno;
2432
2433 for (bitno = 0; bitno < nbits; bitno++)
2434 {
2435 if ((bitno % 8) == 0)
2436 {
2437 puts_filtered (" ");
2438 }
2439 if (B_TST (bits, bitno))
2440 {
2441 printf_filtered ("1");
2442 }
2443 else
2444 {
2445 printf_filtered ("0");
2446 }
2447 }
2448 }
2449
2450 /* The args list is a strange beast. It is either terminated by a NULL
2451 pointer for varargs functions, or by a pointer to a TYPE_CODE_VOID
2452 type for normal fixed argcount functions. (FIXME someday)
2453 Also note the first arg should be the "this" pointer, we may not want to
2454 include it since we may get into a infinitely recursive situation. */
2455
2456 static void
2457 print_arg_types (struct type **args, int spaces)
2458 {
2459 if (args != NULL)
2460 {
2461 while (*args != NULL)
2462 {
2463 recursive_dump_type (*args, spaces + 2);
2464 if ((*args++)->code == TYPE_CODE_VOID)
2465 {
2466 break;
2467 }
2468 }
2469 }
2470 }
2471
2472 static void
2473 dump_fn_fieldlists (struct type *type, int spaces)
2474 {
2475 int method_idx;
2476 int overload_idx;
2477 struct fn_field *f;
2478
2479 printfi_filtered (spaces, "fn_fieldlists ");
2480 gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
2481 printf_filtered ("\n");
2482 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2483 {
2484 f = TYPE_FN_FIELDLIST1 (type, method_idx);
2485 printfi_filtered (spaces + 2, "[%d] name '%s' (",
2486 method_idx,
2487 TYPE_FN_FIELDLIST_NAME (type, method_idx));
2488 gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2489 gdb_stdout);
2490 printf_filtered (") length %d\n",
2491 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2492 for (overload_idx = 0;
2493 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2494 overload_idx++)
2495 {
2496 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2497 overload_idx,
2498 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
2499 gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2500 gdb_stdout);
2501 printf_filtered (")\n");
2502 printfi_filtered (spaces + 8, "type ");
2503 gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), gdb_stdout);
2504 printf_filtered ("\n");
2505
2506 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2507 spaces + 8 + 2);
2508
2509 printfi_filtered (spaces + 8, "args ");
2510 gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), gdb_stdout);
2511 printf_filtered ("\n");
2512
2513 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), spaces);
2514 printfi_filtered (spaces + 8, "fcontext ");
2515 gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2516 gdb_stdout);
2517 printf_filtered ("\n");
2518
2519 printfi_filtered (spaces + 8, "is_const %d\n",
2520 TYPE_FN_FIELD_CONST (f, overload_idx));
2521 printfi_filtered (spaces + 8, "is_volatile %d\n",
2522 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2523 printfi_filtered (spaces + 8, "is_private %d\n",
2524 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2525 printfi_filtered (spaces + 8, "is_protected %d\n",
2526 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2527 printfi_filtered (spaces + 8, "is_stub %d\n",
2528 TYPE_FN_FIELD_STUB (f, overload_idx));
2529 printfi_filtered (spaces + 8, "voffset %u\n",
2530 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2531 }
2532 }
2533 }
2534
2535 static void
2536 print_cplus_stuff (struct type *type, int spaces)
2537 {
2538 printfi_filtered (spaces, "n_baseclasses %d\n",
2539 TYPE_N_BASECLASSES (type));
2540 printfi_filtered (spaces, "nfn_fields %d\n",
2541 TYPE_NFN_FIELDS (type));
2542 printfi_filtered (spaces, "nfn_fields_total %d\n",
2543 TYPE_NFN_FIELDS_TOTAL (type));
2544 if (TYPE_N_BASECLASSES (type) > 0)
2545 {
2546 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2547 TYPE_N_BASECLASSES (type));
2548 gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), gdb_stdout);
2549 printf_filtered (")");
2550
2551 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2552 TYPE_N_BASECLASSES (type));
2553 puts_filtered ("\n");
2554 }
2555 if (TYPE_NFIELDS (type) > 0)
2556 {
2557 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2558 {
2559 printfi_filtered (spaces, "private_field_bits (%d bits at *",
2560 TYPE_NFIELDS (type));
2561 gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), gdb_stdout);
2562 printf_filtered (")");
2563 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2564 TYPE_NFIELDS (type));
2565 puts_filtered ("\n");
2566 }
2567 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2568 {
2569 printfi_filtered (spaces, "protected_field_bits (%d bits at *",
2570 TYPE_NFIELDS (type));
2571 gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), gdb_stdout);
2572 printf_filtered (")");
2573 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2574 TYPE_NFIELDS (type));
2575 puts_filtered ("\n");
2576 }
2577 }
2578 if (TYPE_NFN_FIELDS (type) > 0)
2579 {
2580 dump_fn_fieldlists (type, spaces);
2581 }
2582 }
2583
2584 static struct obstack dont_print_type_obstack;
2585
2586 void
2587 recursive_dump_type (struct type *type, int spaces)
2588 {
2589 int idx;
2590
2591 if (spaces == 0)
2592 obstack_begin (&dont_print_type_obstack, 0);
2593
2594 if (TYPE_NFIELDS (type) > 0
2595 || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
2596 {
2597 struct type **first_dont_print
2598 = (struct type **) obstack_base (&dont_print_type_obstack);
2599
2600 int i = (struct type **) obstack_next_free (&dont_print_type_obstack)
2601 - first_dont_print;
2602
2603 while (--i >= 0)
2604 {
2605 if (type == first_dont_print[i])
2606 {
2607 printfi_filtered (spaces, "type node ");
2608 gdb_print_host_address (type, gdb_stdout);
2609 printf_filtered (" <same as already seen type>\n");
2610 return;
2611 }
2612 }
2613
2614 obstack_ptr_grow (&dont_print_type_obstack, type);
2615 }
2616
2617 printfi_filtered (spaces, "type node ");
2618 gdb_print_host_address (type, gdb_stdout);
2619 printf_filtered ("\n");
2620 printfi_filtered (spaces, "name '%s' (",
2621 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
2622 gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
2623 printf_filtered (")\n");
2624 if (TYPE_TAG_NAME (type) != NULL)
2625 {
2626 printfi_filtered (spaces, "tagname '%s' (",
2627 TYPE_TAG_NAME (type));
2628 gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2629 printf_filtered (")\n");
2630 }
2631 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2632 switch (TYPE_CODE (type))
2633 {
2634 case TYPE_CODE_UNDEF:
2635 printf_filtered ("(TYPE_CODE_UNDEF)");
2636 break;
2637 case TYPE_CODE_PTR:
2638 printf_filtered ("(TYPE_CODE_PTR)");
2639 break;
2640 case TYPE_CODE_ARRAY:
2641 printf_filtered ("(TYPE_CODE_ARRAY)");
2642 break;
2643 case TYPE_CODE_STRUCT:
2644 printf_filtered ("(TYPE_CODE_STRUCT)");
2645 break;
2646 case TYPE_CODE_UNION:
2647 printf_filtered ("(TYPE_CODE_UNION)");
2648 break;
2649 case TYPE_CODE_ENUM:
2650 printf_filtered ("(TYPE_CODE_ENUM)");
2651 break;
2652 case TYPE_CODE_FUNC:
2653 printf_filtered ("(TYPE_CODE_FUNC)");
2654 break;
2655 case TYPE_CODE_INT:
2656 printf_filtered ("(TYPE_CODE_INT)");
2657 break;
2658 case TYPE_CODE_FLT:
2659 printf_filtered ("(TYPE_CODE_FLT)");
2660 break;
2661 case TYPE_CODE_VOID:
2662 printf_filtered ("(TYPE_CODE_VOID)");
2663 break;
2664 case TYPE_CODE_SET:
2665 printf_filtered ("(TYPE_CODE_SET)");
2666 break;
2667 case TYPE_CODE_RANGE:
2668 printf_filtered ("(TYPE_CODE_RANGE)");
2669 break;
2670 case TYPE_CODE_STRING:
2671 printf_filtered ("(TYPE_CODE_STRING)");
2672 break;
2673 case TYPE_CODE_ERROR:
2674 printf_filtered ("(TYPE_CODE_ERROR)");
2675 break;
2676 case TYPE_CODE_MEMBER:
2677 printf_filtered ("(TYPE_CODE_MEMBER)");
2678 break;
2679 case TYPE_CODE_METHOD:
2680 printf_filtered ("(TYPE_CODE_METHOD)");
2681 break;
2682 case TYPE_CODE_REF:
2683 printf_filtered ("(TYPE_CODE_REF)");
2684 break;
2685 case TYPE_CODE_CHAR:
2686 printf_filtered ("(TYPE_CODE_CHAR)");
2687 break;
2688 case TYPE_CODE_BOOL:
2689 printf_filtered ("(TYPE_CODE_BOOL)");
2690 break;
2691 case TYPE_CODE_TYPEDEF:
2692 printf_filtered ("(TYPE_CODE_TYPEDEF)");
2693 break;
2694 default:
2695 printf_filtered ("(UNKNOWN TYPE CODE)");
2696 break;
2697 }
2698 puts_filtered ("\n");
2699 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
2700 printfi_filtered (spaces, "objfile ");
2701 gdb_print_host_address (TYPE_OBJFILE (type), gdb_stdout);
2702 printf_filtered ("\n");
2703 printfi_filtered (spaces, "target_type ");
2704 gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
2705 printf_filtered ("\n");
2706 if (TYPE_TARGET_TYPE (type) != NULL)
2707 {
2708 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
2709 }
2710 printfi_filtered (spaces, "pointer_type ");
2711 gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
2712 printf_filtered ("\n");
2713 printfi_filtered (spaces, "reference_type ");
2714 gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
2715 printf_filtered ("\n");
2716 printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
2717 if (TYPE_FLAGS (type) & TYPE_FLAG_UNSIGNED)
2718 {
2719 puts_filtered (" TYPE_FLAG_UNSIGNED");
2720 }
2721 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
2722 {
2723 puts_filtered (" TYPE_FLAG_STUB");
2724 }
2725 puts_filtered ("\n");
2726 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
2727 gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
2728 puts_filtered ("\n");
2729 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
2730 {
2731 printfi_filtered (spaces + 2,
2732 "[%d] bitpos %d bitsize %d type ",
2733 idx, TYPE_FIELD_BITPOS (type, idx),
2734 TYPE_FIELD_BITSIZE (type, idx));
2735 gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
2736 printf_filtered (" name '%s' (",
2737 TYPE_FIELD_NAME (type, idx) != NULL
2738 ? TYPE_FIELD_NAME (type, idx)
2739 : "<NULL>");
2740 gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
2741 printf_filtered (")\n");
2742 if (TYPE_FIELD_TYPE (type, idx) != NULL)
2743 {
2744 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
2745 }
2746 }
2747 printfi_filtered (spaces, "vptr_basetype ");
2748 gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
2749 puts_filtered ("\n");
2750 if (TYPE_VPTR_BASETYPE (type) != NULL)
2751 {
2752 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
2753 }
2754 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
2755 switch (TYPE_CODE (type))
2756 {
2757 case TYPE_CODE_METHOD:
2758 case TYPE_CODE_FUNC:
2759 printfi_filtered (spaces, "arg_types ");
2760 gdb_print_host_address (TYPE_ARG_TYPES (type), gdb_stdout);
2761 puts_filtered ("\n");
2762 print_arg_types (TYPE_ARG_TYPES (type), spaces);
2763 break;
2764
2765 case TYPE_CODE_STRUCT:
2766 printfi_filtered (spaces, "cplus_stuff ");
2767 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
2768 puts_filtered ("\n");
2769 print_cplus_stuff (type, spaces);
2770 break;
2771
2772 default:
2773 /* We have to pick one of the union types to be able print and test
2774 the value. Pick cplus_struct_type, even though we know it isn't
2775 any particular one. */
2776 printfi_filtered (spaces, "type_specific ");
2777 gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
2778 if (TYPE_CPLUS_SPECIFIC (type) != NULL)
2779 {
2780 printf_filtered (" (unknown data form)");
2781 }
2782 printf_filtered ("\n");
2783 break;
2784
2785 }
2786 if (spaces == 0)
2787 obstack_free (&dont_print_type_obstack, NULL);
2788 }
2789
2790 static void build_gdbtypes (void);
2791 static void
2792 build_gdbtypes (void)
2793 {
2794 builtin_type_void =
2795 init_type (TYPE_CODE_VOID, 1,
2796 0,
2797 "void", (struct objfile *) NULL);
2798 builtin_type_char =
2799 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2800 0,
2801 "char", (struct objfile *) NULL);
2802 TYPE_FLAGS (builtin_type_char) |= TYPE_FLAG_NOSIGN;
2803 builtin_type_true_char =
2804 init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2805 0,
2806 "true character", (struct objfile *) NULL);
2807 builtin_type_signed_char =
2808 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2809 0,
2810 "signed char", (struct objfile *) NULL);
2811 builtin_type_unsigned_char =
2812 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2813 TYPE_FLAG_UNSIGNED,
2814 "unsigned char", (struct objfile *) NULL);
2815 builtin_type_short =
2816 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
2817 0,
2818 "short", (struct objfile *) NULL);
2819 builtin_type_unsigned_short =
2820 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
2821 TYPE_FLAG_UNSIGNED,
2822 "unsigned short", (struct objfile *) NULL);
2823 builtin_type_int =
2824 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
2825 0,
2826 "int", (struct objfile *) NULL);
2827 builtin_type_unsigned_int =
2828 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
2829 TYPE_FLAG_UNSIGNED,
2830 "unsigned int", (struct objfile *) NULL);
2831 builtin_type_long =
2832 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
2833 0,
2834 "long", (struct objfile *) NULL);
2835 builtin_type_unsigned_long =
2836 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
2837 TYPE_FLAG_UNSIGNED,
2838 "unsigned long", (struct objfile *) NULL);
2839 builtin_type_long_long =
2840 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
2841 0,
2842 "long long", (struct objfile *) NULL);
2843 builtin_type_unsigned_long_long =
2844 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
2845 TYPE_FLAG_UNSIGNED,
2846 "unsigned long long", (struct objfile *) NULL);
2847 builtin_type_float =
2848 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
2849 0,
2850 "float", (struct objfile *) NULL);
2851 builtin_type_double =
2852 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
2853 0,
2854 "double", (struct objfile *) NULL);
2855 builtin_type_long_double =
2856 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
2857 0,
2858 "long double", (struct objfile *) NULL);
2859 builtin_type_complex =
2860 init_type (TYPE_CODE_COMPLEX, 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
2861 0,
2862 "complex", (struct objfile *) NULL);
2863 TYPE_TARGET_TYPE (builtin_type_complex) = builtin_type_float;
2864 builtin_type_double_complex =
2865 init_type (TYPE_CODE_COMPLEX, 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
2866 0,
2867 "double complex", (struct objfile *) NULL);
2868 TYPE_TARGET_TYPE (builtin_type_double_complex) = builtin_type_double;
2869 builtin_type_string =
2870 init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2871 0,
2872 "string", (struct objfile *) NULL);
2873 builtin_type_int8 =
2874 init_type (TYPE_CODE_INT, 8 / 8,
2875 0,
2876 "int8_t", (struct objfile *) NULL);
2877 builtin_type_uint8 =
2878 init_type (TYPE_CODE_INT, 8 / 8,
2879 TYPE_FLAG_UNSIGNED,
2880 "uint8_t", (struct objfile *) NULL);
2881 builtin_type_int16 =
2882 init_type (TYPE_CODE_INT, 16 / 8,
2883 0,
2884 "int16_t", (struct objfile *) NULL);
2885 builtin_type_uint16 =
2886 init_type (TYPE_CODE_INT, 16 / 8,
2887 TYPE_FLAG_UNSIGNED,
2888 "uint16_t", (struct objfile *) NULL);
2889 builtin_type_int32 =
2890 init_type (TYPE_CODE_INT, 32 / 8,
2891 0,
2892 "int32_t", (struct objfile *) NULL);
2893 builtin_type_uint32 =
2894 init_type (TYPE_CODE_INT, 32 / 8,
2895 TYPE_FLAG_UNSIGNED,
2896 "uint32_t", (struct objfile *) NULL);
2897 builtin_type_int64 =
2898 init_type (TYPE_CODE_INT, 64 / 8,
2899 0,
2900 "int64_t", (struct objfile *) NULL);
2901 builtin_type_uint64 =
2902 init_type (TYPE_CODE_INT, 64 / 8,
2903 TYPE_FLAG_UNSIGNED,
2904 "uint64_t", (struct objfile *) NULL);
2905 builtin_type_bool =
2906 init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
2907 0,
2908 "bool", (struct objfile *) NULL);
2909
2910 /* Add user knob for controlling resolution of opaque types */
2911 add_show_from_set
2912 (add_set_cmd ("opaque-type-resolution", class_support, var_boolean, (char *) &opaque_type_resolution,
2913 "Set resolution of opaque struct/class/union types (if set before loading symbols).",
2914 &setlist),
2915 &showlist);
2916 opaque_type_resolution = 1;
2917
2918
2919 /* Build SIMD types. */
2920 builtin_type_v4sf
2921 = init_simd_type ("__builtin_v4sf", builtin_type_float, "f", 4);
2922 builtin_type_v4si
2923 = init_simd_type ("__builtin_v4si", builtin_type_int32, "f", 4);
2924 builtin_type_v8qi
2925 = init_simd_type ("__builtin_v8qi", builtin_type_int8, "f", 8);
2926 builtin_type_v4hi
2927 = init_simd_type ("__builtin_v4hi", builtin_type_int16, "f", 4);
2928 builtin_type_v2si
2929 = init_simd_type ("__builtin_v2si", builtin_type_int32, "f", 2);
2930
2931 /* Pointer/Address types. */
2932
2933 /* NOTE: on some targets, addresses and pointers are not necessarily
2934 the same --- for example, on the D10V, pointers are 16 bits long,
2935 but addresses are 32 bits long. See doc/gdbint.texinfo,
2936 ``Pointers Are Not Always Addresses''.
2937
2938 The upshot is:
2939 - gdb's `struct type' always describes the target's
2940 representation.
2941 - gdb's `struct value' objects should always hold values in
2942 target form.
2943 - gdb's CORE_ADDR values are addresses in the unified virtual
2944 address space that the assembler and linker work with. Thus,
2945 since target_read_memory takes a CORE_ADDR as an argument, it
2946 can access any memory on the target, even if the processor has
2947 separate code and data address spaces.
2948
2949 So, for example:
2950 - If v is a value holding a D10V code pointer, its contents are
2951 in target form: a big-endian address left-shifted two bits.
2952 - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
2953 sizeof (void *) == 2 on the target.
2954
2955 In this context, builtin_type_CORE_ADDR is a bit odd: it's a
2956 target type for a value the target will never see. It's only
2957 used to hold the values of (typeless) linker symbols, which are
2958 indeed in the unified virtual address space. */
2959 builtin_type_void_data_ptr = make_pointer_type (builtin_type_void, NULL);
2960 builtin_type_void_func_ptr
2961 = lookup_pointer_type (lookup_function_type (builtin_type_void));
2962 builtin_type_CORE_ADDR =
2963 init_type (TYPE_CODE_INT, TARGET_ADDR_BIT / 8,
2964 TYPE_FLAG_UNSIGNED,
2965 "__CORE_ADDR", (struct objfile *) NULL);
2966 builtin_type_bfd_vma =
2967 init_type (TYPE_CODE_INT, TARGET_BFD_VMA_BIT / 8,
2968 TYPE_FLAG_UNSIGNED,
2969 "__bfd_vma", (struct objfile *) NULL);
2970 }
2971
2972
2973 extern void _initialize_gdbtypes (void);
2974 void
2975 _initialize_gdbtypes (void)
2976 {
2977 struct cmd_list_element *c;
2978 build_gdbtypes ();
2979
2980 /* FIXME - For the moment, handle types by swapping them in and out.
2981 Should be using the per-architecture data-pointer and a large
2982 struct. */
2983 register_gdbarch_swap (&builtin_type_void, sizeof (struct type *), NULL);
2984 register_gdbarch_swap (&builtin_type_char, sizeof (struct type *), NULL);
2985 register_gdbarch_swap (&builtin_type_short, sizeof (struct type *), NULL);
2986 register_gdbarch_swap (&builtin_type_int, sizeof (struct type *), NULL);
2987 register_gdbarch_swap (&builtin_type_long, sizeof (struct type *), NULL);
2988 register_gdbarch_swap (&builtin_type_long_long, sizeof (struct type *), NULL);
2989 register_gdbarch_swap (&builtin_type_signed_char, sizeof (struct type *), NULL);
2990 register_gdbarch_swap (&builtin_type_unsigned_char, sizeof (struct type *), NULL);
2991 register_gdbarch_swap (&builtin_type_unsigned_short, sizeof (struct type *), NULL);
2992 register_gdbarch_swap (&builtin_type_unsigned_int, sizeof (struct type *), NULL);
2993 register_gdbarch_swap (&builtin_type_unsigned_long, sizeof (struct type *), NULL);
2994 register_gdbarch_swap (&builtin_type_unsigned_long_long, sizeof (struct type *), NULL);
2995 register_gdbarch_swap (&builtin_type_float, sizeof (struct type *), NULL);
2996 register_gdbarch_swap (&builtin_type_double, sizeof (struct type *), NULL);
2997 register_gdbarch_swap (&builtin_type_long_double, sizeof (struct type *), NULL);
2998 register_gdbarch_swap (&builtin_type_complex, sizeof (struct type *), NULL);
2999 register_gdbarch_swap (&builtin_type_double_complex, sizeof (struct type *), NULL);
3000 register_gdbarch_swap (&builtin_type_string, sizeof (struct type *), NULL);
3001 register_gdbarch_swap (&builtin_type_int8, sizeof (struct type *), NULL);
3002 register_gdbarch_swap (&builtin_type_uint8, sizeof (struct type *), NULL);
3003 register_gdbarch_swap (&builtin_type_int16, sizeof (struct type *), NULL);
3004 register_gdbarch_swap (&builtin_type_uint16, sizeof (struct type *), NULL);
3005 register_gdbarch_swap (&builtin_type_int32, sizeof (struct type *), NULL);
3006 register_gdbarch_swap (&builtin_type_uint32, sizeof (struct type *), NULL);
3007 register_gdbarch_swap (&builtin_type_int64, sizeof (struct type *), NULL);
3008 register_gdbarch_swap (&builtin_type_uint64, sizeof (struct type *), NULL);
3009 register_gdbarch_swap (&builtin_type_v4sf, sizeof (struct type *), NULL);
3010 register_gdbarch_swap (&builtin_type_v4si, sizeof (struct type *), NULL);
3011 register_gdbarch_swap (&builtin_type_v8qi, sizeof (struct type *), NULL);
3012 register_gdbarch_swap (&builtin_type_v4hi, sizeof (struct type *), NULL);
3013 register_gdbarch_swap (&builtin_type_v2si, sizeof (struct type *), NULL);
3014 REGISTER_GDBARCH_SWAP (builtin_type_void_data_ptr);
3015 REGISTER_GDBARCH_SWAP (builtin_type_void_func_ptr);
3016 REGISTER_GDBARCH_SWAP (builtin_type_CORE_ADDR);
3017 REGISTER_GDBARCH_SWAP (builtin_type_bfd_vma);
3018 register_gdbarch_swap (NULL, 0, build_gdbtypes);
3019
3020 add_show_from_set (
3021 add_set_cmd ("overload", no_class, var_zinteger, (char *) &overload_debug,
3022 "Set debugging of C++ overloading.\n\
3023 When enabled, ranking of the functions\n\
3024 is displayed.", &setdebuglist),
3025 &showdebuglist);
3026 }
This page took 0.091181 seconds and 5 git commands to generate.