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