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