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