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