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