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