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