OSDN Git Service

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