OSDN Git Service

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