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