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