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