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