vla: evaluate operand of sizeof if its type is a vla
[platform/upstream/binutils.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2
3    Copyright (C) 1992-2014 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Support, using pieces from other GDB modules.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include <string.h>
24 #include "bfd.h"
25 #include "symtab.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "language.h"
31 #include "target.h"
32 #include "value.h"
33 #include "demangle.h"
34 #include "complaints.h"
35 #include "gdbcmd.h"
36 #include "cp-abi.h"
37 #include "gdb_assert.h"
38 #include "hashtab.h"
39 #include "exceptions.h"
40 #include "cp-support.h"
41 #include "bcache.h"
42 #include "dwarf2loc.h"
43
44 /* Initialize BADNESS constants.  */
45
46 const struct rank LENGTH_MISMATCH_BADNESS = {100,0};
47
48 const struct rank TOO_FEW_PARAMS_BADNESS = {100,0};
49 const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0};
50
51 const struct rank EXACT_MATCH_BADNESS = {0,0};
52
53 const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
54 const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
55 const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
56 const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
57 const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
58 const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
59 const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
60 const struct rank BOOL_CONVERSION_BADNESS = {3,0};
61 const struct rank BASE_CONVERSION_BADNESS = {2,0};
62 const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
63 const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
64 const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
65 const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
66
67 /* Floatformat pairs.  */
68 const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
69   &floatformat_ieee_half_big,
70   &floatformat_ieee_half_little
71 };
72 const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
73   &floatformat_ieee_single_big,
74   &floatformat_ieee_single_little
75 };
76 const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
77   &floatformat_ieee_double_big,
78   &floatformat_ieee_double_little
79 };
80 const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
81   &floatformat_ieee_double_big,
82   &floatformat_ieee_double_littlebyte_bigword
83 };
84 const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
85   &floatformat_i387_ext,
86   &floatformat_i387_ext
87 };
88 const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
89   &floatformat_m68881_ext,
90   &floatformat_m68881_ext
91 };
92 const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
93   &floatformat_arm_ext_big,
94   &floatformat_arm_ext_littlebyte_bigword
95 };
96 const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
97   &floatformat_ia64_spill_big,
98   &floatformat_ia64_spill_little
99 };
100 const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = {
101   &floatformat_ia64_quad_big,
102   &floatformat_ia64_quad_little
103 };
104 const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
105   &floatformat_vax_f,
106   &floatformat_vax_f
107 };
108 const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
109   &floatformat_vax_d,
110   &floatformat_vax_d
111 };
112 const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
113   &floatformat_ibm_long_double_big,
114   &floatformat_ibm_long_double_little
115 };
116
117 /* Should opaque types be resolved?  */
118
119 static int opaque_type_resolution = 1;
120
121 /* A flag to enable printing of debugging information of C++
122    overloading.  */
123
124 unsigned int overload_debug = 0;
125
126 /* A flag to enable strict type checking.  */
127
128 static int strict_type_checking = 1;
129
130 /* A function to show whether opaque types are resolved.  */
131
132 static void
133 show_opaque_type_resolution (struct ui_file *file, int from_tty,
134                              struct cmd_list_element *c, 
135                              const char *value)
136 {
137   fprintf_filtered (file, _("Resolution of opaque struct/class/union types "
138                             "(if set before loading symbols) is %s.\n"),
139                     value);
140 }
141
142 /* A function to show whether C++ overload debugging is enabled.  */
143
144 static void
145 show_overload_debug (struct ui_file *file, int from_tty,
146                      struct cmd_list_element *c, const char *value)
147 {
148   fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), 
149                     value);
150 }
151
152 /* A function to show the status of strict type checking.  */
153
154 static void
155 show_strict_type_checking (struct ui_file *file, int from_tty,
156                            struct cmd_list_element *c, const char *value)
157 {
158   fprintf_filtered (file, _("Strict type checking is %s.\n"), value);
159 }
160
161 \f
162 /* Allocate a new OBJFILE-associated type structure and fill it
163    with some defaults.  Space for the type structure is allocated
164    on the objfile's objfile_obstack.  */
165
166 struct type *
167 alloc_type (struct objfile *objfile)
168 {
169   struct type *type;
170
171   gdb_assert (objfile != NULL);
172
173   /* Alloc the structure and start off with all fields zeroed.  */
174   type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
175   TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
176                                           struct main_type);
177   OBJSTAT (objfile, n_types++);
178
179   TYPE_OBJFILE_OWNED (type) = 1;
180   TYPE_OWNER (type).objfile = objfile;
181
182   /* Initialize the fields that might not be zero.  */
183
184   TYPE_CODE (type) = TYPE_CODE_UNDEF;
185   TYPE_VPTR_FIELDNO (type) = -1;
186   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
187
188   return type;
189 }
190
191 /* Allocate a new GDBARCH-associated type structure and fill it
192    with some defaults.  Space for the type structure is allocated
193    on the heap.  */
194
195 struct type *
196 alloc_type_arch (struct gdbarch *gdbarch)
197 {
198   struct type *type;
199
200   gdb_assert (gdbarch != NULL);
201
202   /* Alloc the structure and start off with all fields zeroed.  */
203
204   type = XCNEW (struct type);
205   TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
206
207   TYPE_OBJFILE_OWNED (type) = 0;
208   TYPE_OWNER (type).gdbarch = gdbarch;
209
210   /* Initialize the fields that might not be zero.  */
211
212   TYPE_CODE (type) = TYPE_CODE_UNDEF;
213   TYPE_VPTR_FIELDNO (type) = -1;
214   TYPE_CHAIN (type) = type;     /* Chain back to itself.  */
215
216   return type;
217 }
218
219 /* If TYPE is objfile-associated, allocate a new type structure
220    associated with the same objfile.  If TYPE is gdbarch-associated,
221    allocate a new type structure associated with the same gdbarch.  */
222
223 struct type *
224 alloc_type_copy (const struct type *type)
225 {
226   if (TYPE_OBJFILE_OWNED (type))
227     return alloc_type (TYPE_OWNER (type).objfile);
228   else
229     return alloc_type_arch (TYPE_OWNER (type).gdbarch);
230 }
231
232 /* If TYPE is gdbarch-associated, return that architecture.
233    If TYPE is objfile-associated, return that objfile's architecture.  */
234
235 struct gdbarch *
236 get_type_arch (const struct type *type)
237 {
238   if (TYPE_OBJFILE_OWNED (type))
239     return get_objfile_arch (TYPE_OWNER (type).objfile);
240   else
241     return TYPE_OWNER (type).gdbarch;
242 }
243
244 /* See gdbtypes.h.  */
245
246 struct type *
247 get_target_type (struct type *type)
248 {
249   if (type != NULL)
250     {
251       type = TYPE_TARGET_TYPE (type);
252       if (type != NULL)
253         type = check_typedef (type);
254     }
255
256   return type;
257 }
258
259 /* Alloc a new type instance structure, fill it with some defaults,
260    and point it at OLDTYPE.  Allocate the new type instance from the
261    same place as OLDTYPE.  */
262
263 static struct type *
264 alloc_type_instance (struct type *oldtype)
265 {
266   struct type *type;
267
268   /* Allocate the structure.  */
269
270   if (! TYPE_OBJFILE_OWNED (oldtype))
271     type = XCNEW (struct type);
272   else
273     type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack,
274                            struct type);
275
276   TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
277
278   TYPE_CHAIN (type) = type;     /* Chain back to itself for now.  */
279
280   return type;
281 }
282
283 /* Clear all remnants of the previous type at TYPE, in preparation for
284    replacing it with something else.  Preserve owner information.  */
285
286 static void
287 smash_type (struct type *type)
288 {
289   int objfile_owned = TYPE_OBJFILE_OWNED (type);
290   union type_owner owner = TYPE_OWNER (type);
291
292   memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
293
294   /* Restore owner information.  */
295   TYPE_OBJFILE_OWNED (type) = objfile_owned;
296   TYPE_OWNER (type) = owner;
297
298   /* For now, delete the rings.  */
299   TYPE_CHAIN (type) = type;
300
301   /* For now, leave the pointer/reference types alone.  */
302 }
303
304 /* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
305    to a pointer to memory where the pointer type should be stored.
306    If *TYPEPTR is zero, update it to point to the pointer type we return.
307    We allocate new memory if needed.  */
308
309 struct type *
310 make_pointer_type (struct type *type, struct type **typeptr)
311 {
312   struct type *ntype;   /* New type */
313   struct type *chain;
314
315   ntype = TYPE_POINTER_TYPE (type);
316
317   if (ntype)
318     {
319       if (typeptr == 0)
320         return ntype;           /* Don't care about alloc, 
321                                    and have new type.  */
322       else if (*typeptr == 0)
323         {
324           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
325           return ntype;
326         }
327     }
328
329   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
330     {
331       ntype = alloc_type_copy (type);
332       if (typeptr)
333         *typeptr = ntype;
334     }
335   else                  /* We have storage, but need to reset it.  */
336     {
337       ntype = *typeptr;
338       chain = TYPE_CHAIN (ntype);
339       smash_type (ntype);
340       TYPE_CHAIN (ntype) = chain;
341     }
342
343   TYPE_TARGET_TYPE (ntype) = type;
344   TYPE_POINTER_TYPE (type) = ntype;
345
346   /* FIXME!  Assumes the machine has only one representation for pointers!  */
347
348   TYPE_LENGTH (ntype)
349     = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
350   TYPE_CODE (ntype) = TYPE_CODE_PTR;
351
352   /* Mark pointers as unsigned.  The target converts between pointers
353      and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
354      gdbarch_address_to_pointer.  */
355   TYPE_UNSIGNED (ntype) = 1;
356
357   /* Update the length of all the other variants of this type.  */
358   chain = TYPE_CHAIN (ntype);
359   while (chain != ntype)
360     {
361       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
362       chain = TYPE_CHAIN (chain);
363     }
364
365   return ntype;
366 }
367
368 /* Given a type TYPE, return a type of pointers to that type.
369    May need to construct such a type if this is the first use.  */
370
371 struct type *
372 lookup_pointer_type (struct type *type)
373 {
374   return make_pointer_type (type, (struct type **) 0);
375 }
376
377 /* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero,
378    points to a pointer to memory where the reference type should be
379    stored.  If *TYPEPTR is zero, update it to point to the reference
380    type we return.  We allocate new memory if needed.  */
381
382 struct type *
383 make_reference_type (struct type *type, struct type **typeptr)
384 {
385   struct type *ntype;   /* New type */
386   struct type *chain;
387
388   ntype = TYPE_REFERENCE_TYPE (type);
389
390   if (ntype)
391     {
392       if (typeptr == 0)
393         return ntype;           /* Don't care about alloc, 
394                                    and have new type.  */
395       else if (*typeptr == 0)
396         {
397           *typeptr = ntype;     /* Tracking alloc, and have new type.  */
398           return ntype;
399         }
400     }
401
402   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
403     {
404       ntype = alloc_type_copy (type);
405       if (typeptr)
406         *typeptr = ntype;
407     }
408   else                  /* We have storage, but need to reset it.  */
409     {
410       ntype = *typeptr;
411       chain = TYPE_CHAIN (ntype);
412       smash_type (ntype);
413       TYPE_CHAIN (ntype) = chain;
414     }
415
416   TYPE_TARGET_TYPE (ntype) = type;
417   TYPE_REFERENCE_TYPE (type) = ntype;
418
419   /* FIXME!  Assume the machine has only one representation for
420      references, and that it matches the (only) representation for
421      pointers!  */
422
423   TYPE_LENGTH (ntype) =
424     gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT;
425   TYPE_CODE (ntype) = TYPE_CODE_REF;
426
427   if (!TYPE_REFERENCE_TYPE (type))      /* Remember it, if don't have one.  */
428     TYPE_REFERENCE_TYPE (type) = ntype;
429
430   /* Update the length of all the other variants of this type.  */
431   chain = TYPE_CHAIN (ntype);
432   while (chain != ntype)
433     {
434       TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
435       chain = TYPE_CHAIN (chain);
436     }
437
438   return ntype;
439 }
440
441 /* Same as above, but caller doesn't care about memory allocation
442    details.  */
443
444 struct type *
445 lookup_reference_type (struct type *type)
446 {
447   return make_reference_type (type, (struct type **) 0);
448 }
449
450 /* Lookup a function type that returns type TYPE.  TYPEPTR, if
451    nonzero, points to a pointer to memory where the function type
452    should be stored.  If *TYPEPTR is zero, update it to point to the
453    function type we return.  We allocate new memory if needed.  */
454
455 struct type *
456 make_function_type (struct type *type, struct type **typeptr)
457 {
458   struct type *ntype;   /* New type */
459
460   if (typeptr == 0 || *typeptr == 0)    /* We'll need to allocate one.  */
461     {
462       ntype = alloc_type_copy (type);
463       if (typeptr)
464         *typeptr = ntype;
465     }
466   else                  /* We have storage, but need to reset it.  */
467     {
468       ntype = *typeptr;
469       smash_type (ntype);
470     }
471
472   TYPE_TARGET_TYPE (ntype) = type;
473
474   TYPE_LENGTH (ntype) = 1;
475   TYPE_CODE (ntype) = TYPE_CODE_FUNC;
476
477   INIT_FUNC_SPECIFIC (ntype);
478
479   return ntype;
480 }
481
482 /* Given a type TYPE, return a type of functions that return that type.
483    May need to construct such a type if this is the first use.  */
484
485 struct type *
486 lookup_function_type (struct type *type)
487 {
488   return make_function_type (type, (struct type **) 0);
489 }
490
491 /* Given a type TYPE and argument types, return the appropriate
492    function type.  If the final type in PARAM_TYPES is NULL, make a
493    varargs function.  */
494
495 struct type *
496 lookup_function_type_with_arguments (struct type *type,
497                                      int nparams,
498                                      struct type **param_types)
499 {
500   struct type *fn = make_function_type (type, (struct type **) 0);
501   int i;
502
503   if (nparams > 0)
504     {
505       if (param_types[nparams - 1] == NULL)
506         {
507           --nparams;
508           TYPE_VARARGS (fn) = 1;
509         }
510       else if (TYPE_CODE (check_typedef (param_types[nparams - 1]))
511                == TYPE_CODE_VOID)
512         {
513           --nparams;
514           /* Caller should have ensured this.  */
515           gdb_assert (nparams == 0);
516           TYPE_PROTOTYPED (fn) = 1;
517         }
518     }
519
520   TYPE_NFIELDS (fn) = nparams;
521   TYPE_FIELDS (fn) = TYPE_ZALLOC (fn, nparams * sizeof (struct field));
522   for (i = 0; i < nparams; ++i)
523     TYPE_FIELD_TYPE (fn, i) = param_types[i];
524
525   return fn;
526 }
527
528 /* Identify address space identifier by name --
529    return the integer flag defined in gdbtypes.h.  */
530
531 int
532 address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier)
533 {
534   int type_flags;
535
536   /* Check for known address space delimiters.  */
537   if (!strcmp (space_identifier, "code"))
538     return TYPE_INSTANCE_FLAG_CODE_SPACE;
539   else if (!strcmp (space_identifier, "data"))
540     return TYPE_INSTANCE_FLAG_DATA_SPACE;
541   else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
542            && gdbarch_address_class_name_to_type_flags (gdbarch,
543                                                         space_identifier,
544                                                         &type_flags))
545     return type_flags;
546   else
547     error (_("Unknown address space specifier: \"%s\""), space_identifier);
548 }
549
550 /* Identify address space identifier by integer flag as defined in 
551    gdbtypes.h -- return the string version of the adress space name.  */
552
553 const char *
554 address_space_int_to_name (struct gdbarch *gdbarch, int space_flag)
555 {
556   if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
557     return "code";
558   else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
559     return "data";
560   else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
561            && gdbarch_address_class_type_flags_to_name_p (gdbarch))
562     return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
563   else
564     return NULL;
565 }
566
567 /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
568
569    If STORAGE is non-NULL, create the new type instance there.
570    STORAGE must be in the same obstack as TYPE.  */
571
572 static struct type *
573 make_qualified_type (struct type *type, int new_flags,
574                      struct type *storage)
575 {
576   struct type *ntype;
577
578   ntype = type;
579   do
580     {
581       if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
582         return ntype;
583       ntype = TYPE_CHAIN (ntype);
584     }
585   while (ntype != type);
586
587   /* Create a new type instance.  */
588   if (storage == NULL)
589     ntype = alloc_type_instance (type);
590   else
591     {
592       /* If STORAGE was provided, it had better be in the same objfile
593          as TYPE.  Otherwise, we can't link it into TYPE's cv chain:
594          if one objfile is freed and the other kept, we'd have
595          dangling pointers.  */
596       gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage));
597
598       ntype = storage;
599       TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
600       TYPE_CHAIN (ntype) = ntype;
601     }
602
603   /* Pointers or references to the original type are not relevant to
604      the new type.  */
605   TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
606   TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
607
608   /* Chain the new qualified type to the old type.  */
609   TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
610   TYPE_CHAIN (type) = ntype;
611
612   /* Now set the instance flags and return the new type.  */
613   TYPE_INSTANCE_FLAGS (ntype) = new_flags;
614
615   /* Set length of new type to that of the original type.  */
616   TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
617
618   return ntype;
619 }
620
621 /* Make an address-space-delimited variant of a type -- a type that
622    is identical to the one supplied except that it has an address
623    space attribute attached to it (such as "code" or "data").
624
625    The space attributes "code" and "data" are for Harvard
626    architectures.  The address space attributes are for architectures
627    which have alternately sized pointers or pointers with alternate
628    representations.  */
629
630 struct type *
631 make_type_with_address_space (struct type *type, int space_flag)
632 {
633   int new_flags = ((TYPE_INSTANCE_FLAGS (type)
634                     & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
635                         | TYPE_INSTANCE_FLAG_DATA_SPACE
636                         | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
637                    | space_flag);
638
639   return make_qualified_type (type, new_flags, NULL);
640 }
641
642 /* Make a "c-v" variant of a type -- a type that is identical to the
643    one supplied except that it may have const or volatile attributes
644    CNST is a flag for setting the const attribute
645    VOLTL is a flag for setting the volatile attribute
646    TYPE is the base type whose variant we are creating.
647
648    If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
649    storage to hold the new qualified type; *TYPEPTR and TYPE must be
650    in the same objfile.  Otherwise, allocate fresh memory for the new
651    type whereever TYPE lives.  If TYPEPTR is non-zero, set it to the
652    new type we construct.  */
653
654 struct type *
655 make_cv_type (int cnst, int voltl, 
656               struct type *type, 
657               struct type **typeptr)
658 {
659   struct type *ntype;   /* New type */
660
661   int new_flags = (TYPE_INSTANCE_FLAGS (type)
662                    & ~(TYPE_INSTANCE_FLAG_CONST 
663                        | TYPE_INSTANCE_FLAG_VOLATILE));
664
665   if (cnst)
666     new_flags |= TYPE_INSTANCE_FLAG_CONST;
667
668   if (voltl)
669     new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
670
671   if (typeptr && *typeptr != NULL)
672     {
673       /* TYPE and *TYPEPTR must be in the same objfile.  We can't have
674          a C-V variant chain that threads across objfiles: if one
675          objfile gets freed, then the other has a broken C-V chain.
676
677          This code used to try to copy over the main type from TYPE to
678          *TYPEPTR if they were in different objfiles, but that's
679          wrong, too: TYPE may have a field list or member function
680          lists, which refer to types of their own, etc. etc.  The
681          whole shebang would need to be copied over recursively; you
682          can't have inter-objfile pointers.  The only thing to do is
683          to leave stub types as stub types, and look them up afresh by
684          name each time you encounter them.  */
685       gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type));
686     }
687   
688   ntype = make_qualified_type (type, new_flags, 
689                                typeptr ? *typeptr : NULL);
690
691   if (typeptr != NULL)
692     *typeptr = ntype;
693
694   return ntype;
695 }
696
697 /* Make a 'restrict'-qualified version of TYPE.  */
698
699 struct type *
700 make_restrict_type (struct type *type)
701 {
702   return make_qualified_type (type,
703                               (TYPE_INSTANCE_FLAGS (type)
704                                | TYPE_INSTANCE_FLAG_RESTRICT),
705                               NULL);
706 }
707
708 /* Replace the contents of ntype with the type *type.  This changes the
709    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
710    the changes are propogated to all types in the TYPE_CHAIN.
711
712    In order to build recursive types, it's inevitable that we'll need
713    to update types in place --- but this sort of indiscriminate
714    smashing is ugly, and needs to be replaced with something more
715    controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
716    clear if more steps are needed.  */
717
718 void
719 replace_type (struct type *ntype, struct type *type)
720 {
721   struct type *chain;
722
723   /* These two types had better be in the same objfile.  Otherwise,
724      the assignment of one type's main type structure to the other
725      will produce a type with references to objects (names; field
726      lists; etc.) allocated on an objfile other than its own.  */
727   gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype));
728
729   *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
730
731   /* The type length is not a part of the main type.  Update it for
732      each type on the variant chain.  */
733   chain = ntype;
734   do
735     {
736       /* Assert that this element of the chain has no address-class bits
737          set in its flags.  Such type variants might have type lengths
738          which are supposed to be different from the non-address-class
739          variants.  This assertion shouldn't ever be triggered because
740          symbol readers which do construct address-class variants don't
741          call replace_type().  */
742       gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
743
744       TYPE_LENGTH (chain) = TYPE_LENGTH (type);
745       chain = TYPE_CHAIN (chain);
746     }
747   while (ntype != chain);
748
749   /* Assert that the two types have equivalent instance qualifiers.
750      This should be true for at least all of our debug readers.  */
751   gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
752 }
753
754 /* Implement direct support for MEMBER_TYPE in GNU C++.
755    May need to construct such a type if this is the first use.
756    The TYPE is the type of the member.  The DOMAIN is the type
757    of the aggregate that the member belongs to.  */
758
759 struct type *
760 lookup_memberptr_type (struct type *type, struct type *domain)
761 {
762   struct type *mtype;
763
764   mtype = alloc_type_copy (type);
765   smash_to_memberptr_type (mtype, domain, type);
766   return mtype;
767 }
768
769 /* Return a pointer-to-method type, for a method of type TO_TYPE.  */
770
771 struct type *
772 lookup_methodptr_type (struct type *to_type)
773 {
774   struct type *mtype;
775
776   mtype = alloc_type_copy (to_type);
777   smash_to_methodptr_type (mtype, to_type);
778   return mtype;
779 }
780
781 /* Allocate a stub method whose return type is TYPE.  This apparently
782    happens for speed of symbol reading, since parsing out the
783    arguments to the method is cpu-intensive, the way we are doing it.
784    So, we will fill in arguments later.  This always returns a fresh
785    type.  */
786
787 struct type *
788 allocate_stub_method (struct type *type)
789 {
790   struct type *mtype;
791
792   mtype = alloc_type_copy (type);
793   TYPE_CODE (mtype) = TYPE_CODE_METHOD;
794   TYPE_LENGTH (mtype) = 1;
795   TYPE_STUB (mtype) = 1;
796   TYPE_TARGET_TYPE (mtype) = type;
797   /*  _DOMAIN_TYPE (mtype) = unknown yet */
798   return mtype;
799 }
800
801 /* Create a range type with a dynamic range from LOW_BOUND to
802    HIGH_BOUND, inclusive.  See create_range_type for further details. */
803
804 struct type *
805 create_range_type (struct type *result_type, struct type *index_type,
806                    const struct dynamic_prop *low_bound,
807                    const struct dynamic_prop *high_bound)
808 {
809   if (result_type == NULL)
810     result_type = alloc_type_copy (index_type);
811   TYPE_CODE (result_type) = TYPE_CODE_RANGE;
812   TYPE_TARGET_TYPE (result_type) = index_type;
813   if (TYPE_STUB (index_type))
814     TYPE_TARGET_STUB (result_type) = 1;
815   else
816     TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
817
818   TYPE_RANGE_DATA (result_type) = (struct range_bounds *)
819     TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
820   TYPE_RANGE_DATA (result_type)->low = *low_bound;
821   TYPE_RANGE_DATA (result_type)->high = *high_bound;
822
823   if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
824     TYPE_UNSIGNED (result_type) = 1;
825
826   return result_type;
827 }
828
829 /* Create a range type using either a blank type supplied in
830    RESULT_TYPE, or creating a new type, inheriting the objfile from
831    INDEX_TYPE.
832
833    Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
834    to HIGH_BOUND, inclusive.
835
836    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
837    sure it is TYPE_CODE_UNDEF before we bash it into a range type?  */
838
839 struct type *
840 create_static_range_type (struct type *result_type, struct type *index_type,
841                           LONGEST low_bound, LONGEST high_bound)
842 {
843   struct dynamic_prop low, high;
844
845   low.kind = PROP_CONST;
846   low.data.const_val = low_bound;
847
848   high.kind = PROP_CONST;
849   high.data.const_val = high_bound;
850
851   result_type = create_range_type (result_type, index_type, &low, &high);
852
853   return result_type;
854 }
855
856 /* Predicate tests whether BOUNDS are static.  Returns 1 if all bounds values
857    are static, otherwise returns 0.  */
858
859 static int
860 has_static_range (const struct range_bounds *bounds)
861 {
862   return (bounds->low.kind == PROP_CONST
863           && bounds->high.kind == PROP_CONST);
864 }
865
866
867 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
868    TYPE.  Return 1 if type is a range type, 0 if it is discrete (and
869    bounds will fit in LONGEST), or -1 otherwise.  */
870
871 int
872 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
873 {
874   CHECK_TYPEDEF (type);
875   switch (TYPE_CODE (type))
876     {
877     case TYPE_CODE_RANGE:
878       *lowp = TYPE_LOW_BOUND (type);
879       *highp = TYPE_HIGH_BOUND (type);
880       return 1;
881     case TYPE_CODE_ENUM:
882       if (TYPE_NFIELDS (type) > 0)
883         {
884           /* The enums may not be sorted by value, so search all
885              entries.  */
886           int i;
887
888           *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0);
889           for (i = 0; i < TYPE_NFIELDS (type); i++)
890             {
891               if (TYPE_FIELD_ENUMVAL (type, i) < *lowp)
892                 *lowp = TYPE_FIELD_ENUMVAL (type, i);
893               if (TYPE_FIELD_ENUMVAL (type, i) > *highp)
894                 *highp = TYPE_FIELD_ENUMVAL (type, i);
895             }
896
897           /* Set unsigned indicator if warranted.  */
898           if (*lowp >= 0)
899             {
900               TYPE_UNSIGNED (type) = 1;
901             }
902         }
903       else
904         {
905           *lowp = 0;
906           *highp = -1;
907         }
908       return 0;
909     case TYPE_CODE_BOOL:
910       *lowp = 0;
911       *highp = 1;
912       return 0;
913     case TYPE_CODE_INT:
914       if (TYPE_LENGTH (type) > sizeof (LONGEST))        /* Too big */
915         return -1;
916       if (!TYPE_UNSIGNED (type))
917         {
918           *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
919           *highp = -*lowp - 1;
920           return 0;
921         }
922       /* ... fall through for unsigned ints ...  */
923     case TYPE_CODE_CHAR:
924       *lowp = 0;
925       /* This round-about calculation is to avoid shifting by
926          TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
927          if TYPE_LENGTH (type) == sizeof (LONGEST).  */
928       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
929       *highp = (*highp - 1) | *highp;
930       return 0;
931     default:
932       return -1;
933     }
934 }
935
936 /* Assuming TYPE is a simple, non-empty array type, compute its upper
937    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
938    Save the high bound into HIGH_BOUND if not NULL.
939
940    Return 1 if the operation was successful.  Return zero otherwise,
941    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
942
943    We now simply use get_discrete_bounds call to get the values
944    of the low and high bounds.
945    get_discrete_bounds can return three values:
946    1, meaning that index is a range,
947    0, meaning that index is a discrete type,
948    or -1 for failure.  */
949
950 int
951 get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
952 {
953   struct type *index = TYPE_INDEX_TYPE (type);
954   LONGEST low = 0;
955   LONGEST high = 0;
956   int res;
957
958   if (index == NULL)
959     return 0;
960
961   res = get_discrete_bounds (index, &low, &high);
962   if (res == -1)
963     return 0;
964
965   /* Check if the array bounds are undefined.  */
966   if (res == 1
967       && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
968           || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))))
969     return 0;
970
971   if (low_bound)
972     *low_bound = low;
973
974   if (high_bound)
975     *high_bound = high;
976
977   return 1;
978 }
979
980 /* Create an array type using either a blank type supplied in
981    RESULT_TYPE, or creating a new type, inheriting the objfile from
982    RANGE_TYPE.
983
984    Elements will be of type ELEMENT_TYPE, the indices will be of type
985    RANGE_TYPE.
986
987    If BIT_STRIDE is not zero, build a packed array type whose element
988    size is BIT_STRIDE.  Otherwise, ignore this parameter.
989
990    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
991    sure it is TYPE_CODE_UNDEF before we bash it into an array
992    type?  */
993
994 struct type *
995 create_array_type_with_stride (struct type *result_type,
996                                struct type *element_type,
997                                struct type *range_type,
998                                unsigned int bit_stride)
999 {
1000   if (result_type == NULL)
1001     result_type = alloc_type_copy (range_type);
1002
1003   TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
1004   TYPE_TARGET_TYPE (result_type) = element_type;
1005   if (has_static_range (TYPE_RANGE_DATA (range_type)))
1006     {
1007       LONGEST low_bound, high_bound;
1008
1009       if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
1010         low_bound = high_bound = 0;
1011       CHECK_TYPEDEF (element_type);
1012       /* Be careful when setting the array length.  Ada arrays can be
1013          empty arrays with the high_bound being smaller than the low_bound.
1014          In such cases, the array length should be zero.  */
1015       if (high_bound < low_bound)
1016         TYPE_LENGTH (result_type) = 0;
1017       else if (bit_stride > 0)
1018         TYPE_LENGTH (result_type) =
1019           (bit_stride * (high_bound - low_bound + 1) + 7) / 8;
1020       else
1021         TYPE_LENGTH (result_type) =
1022           TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
1023     }
1024   else
1025     {
1026       /* This type is dynamic and its length needs to be computed
1027          on demand.  In the meantime, avoid leaving the TYPE_LENGTH
1028          undefined by setting it to zero.  Although we are not expected
1029          to trust TYPE_LENGTH in this case, setting the size to zero
1030          allows us to avoid allocating objects of random sizes in case
1031          we accidently do.  */
1032       TYPE_LENGTH (result_type) = 0;
1033     }
1034
1035   TYPE_NFIELDS (result_type) = 1;
1036   TYPE_FIELDS (result_type) =
1037     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
1038   TYPE_INDEX_TYPE (result_type) = range_type;
1039   TYPE_VPTR_FIELDNO (result_type) = -1;
1040   if (bit_stride > 0)
1041     TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
1042
1043   /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
1044   if (TYPE_LENGTH (result_type) == 0)
1045     TYPE_TARGET_STUB (result_type) = 1;
1046
1047   return result_type;
1048 }
1049
1050 /* Same as create_array_type_with_stride but with no bit_stride
1051    (BIT_STRIDE = 0), thus building an unpacked array.  */
1052
1053 struct type *
1054 create_array_type (struct type *result_type,
1055                    struct type *element_type,
1056                    struct type *range_type)
1057 {
1058   return create_array_type_with_stride (result_type, element_type,
1059                                         range_type, 0);
1060 }
1061
1062 struct type *
1063 lookup_array_range_type (struct type *element_type,
1064                          LONGEST low_bound, LONGEST high_bound)
1065 {
1066   struct gdbarch *gdbarch = get_type_arch (element_type);
1067   struct type *index_type = builtin_type (gdbarch)->builtin_int;
1068   struct type *range_type
1069     = create_static_range_type (NULL, index_type, low_bound, high_bound);
1070
1071   return create_array_type (NULL, element_type, range_type);
1072 }
1073
1074 /* Create a string type using either a blank type supplied in
1075    RESULT_TYPE, or creating a new type.  String types are similar
1076    enough to array of char types that we can use create_array_type to
1077    build the basic type and then bash it into a string type.
1078
1079    For fixed length strings, the range type contains 0 as the lower
1080    bound and the length of the string minus one as the upper bound.
1081
1082    FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
1083    sure it is TYPE_CODE_UNDEF before we bash it into a string
1084    type?  */
1085
1086 struct type *
1087 create_string_type (struct type *result_type,
1088                     struct type *string_char_type,
1089                     struct type *range_type)
1090 {
1091   result_type = create_array_type (result_type,
1092                                    string_char_type,
1093                                    range_type);
1094   TYPE_CODE (result_type) = TYPE_CODE_STRING;
1095   return result_type;
1096 }
1097
1098 struct type *
1099 lookup_string_range_type (struct type *string_char_type,
1100                           LONGEST low_bound, LONGEST high_bound)
1101 {
1102   struct type *result_type;
1103
1104   result_type = lookup_array_range_type (string_char_type,
1105                                          low_bound, high_bound);
1106   TYPE_CODE (result_type) = TYPE_CODE_STRING;
1107   return result_type;
1108 }
1109
1110 struct type *
1111 create_set_type (struct type *result_type, struct type *domain_type)
1112 {
1113   if (result_type == NULL)
1114     result_type = alloc_type_copy (domain_type);
1115
1116   TYPE_CODE (result_type) = TYPE_CODE_SET;
1117   TYPE_NFIELDS (result_type) = 1;
1118   TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field));
1119
1120   if (!TYPE_STUB (domain_type))
1121     {
1122       LONGEST low_bound, high_bound, bit_length;
1123
1124       if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
1125         low_bound = high_bound = 0;
1126       bit_length = high_bound - low_bound + 1;
1127       TYPE_LENGTH (result_type)
1128         = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
1129       if (low_bound >= 0)
1130         TYPE_UNSIGNED (result_type) = 1;
1131     }
1132   TYPE_FIELD_TYPE (result_type, 0) = domain_type;
1133
1134   return result_type;
1135 }
1136
1137 /* Convert ARRAY_TYPE to a vector type.  This may modify ARRAY_TYPE
1138    and any array types nested inside it.  */
1139
1140 void
1141 make_vector_type (struct type *array_type)
1142 {
1143   struct type *inner_array, *elt_type;
1144   int flags;
1145
1146   /* Find the innermost array type, in case the array is
1147      multi-dimensional.  */
1148   inner_array = array_type;
1149   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
1150     inner_array = TYPE_TARGET_TYPE (inner_array);
1151
1152   elt_type = TYPE_TARGET_TYPE (inner_array);
1153   if (TYPE_CODE (elt_type) == TYPE_CODE_INT)
1154     {
1155       flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT;
1156       elt_type = make_qualified_type (elt_type, flags, NULL);
1157       TYPE_TARGET_TYPE (inner_array) = elt_type;
1158     }
1159
1160   TYPE_VECTOR (array_type) = 1;
1161 }
1162
1163 struct type *
1164 init_vector_type (struct type *elt_type, int n)
1165 {
1166   struct type *array_type;
1167
1168   array_type = lookup_array_range_type (elt_type, 0, n - 1);
1169   make_vector_type (array_type);
1170   return array_type;
1171 }
1172
1173 /* Smash TYPE to be a type of pointers to members of DOMAIN with type
1174    TO_TYPE.  A member pointer is a wierd thing -- it amounts to a
1175    typed offset into a struct, e.g. "an int at offset 8".  A MEMBER
1176    TYPE doesn't include the offset (that's the value of the MEMBER
1177    itself), but does include the structure type into which it points
1178    (for some reason).
1179
1180    When "smashing" the type, we preserve the objfile that the old type
1181    pointed to, since we aren't changing where the type is actually
1182    allocated.  */
1183
1184 void
1185 smash_to_memberptr_type (struct type *type, struct type *domain,
1186                          struct type *to_type)
1187 {
1188   smash_type (type);
1189   TYPE_TARGET_TYPE (type) = to_type;
1190   TYPE_DOMAIN_TYPE (type) = domain;
1191   /* Assume that a data member pointer is the same size as a normal
1192      pointer.  */
1193   TYPE_LENGTH (type)
1194     = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT;
1195   TYPE_CODE (type) = TYPE_CODE_MEMBERPTR;
1196 }
1197
1198 /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
1199
1200    When "smashing" the type, we preserve the objfile that the old type
1201    pointed to, since we aren't changing where the type is actually
1202    allocated.  */
1203
1204 void
1205 smash_to_methodptr_type (struct type *type, struct type *to_type)
1206 {
1207   smash_type (type);
1208   TYPE_TARGET_TYPE (type) = to_type;
1209   TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type);
1210   TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
1211   TYPE_CODE (type) = TYPE_CODE_METHODPTR;
1212 }
1213
1214 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
1215    METHOD just means `function that gets an extra "this" argument'.
1216
1217    When "smashing" the type, we preserve the objfile that the old type
1218    pointed to, since we aren't changing where the type is actually
1219    allocated.  */
1220
1221 void
1222 smash_to_method_type (struct type *type, struct type *domain,
1223                       struct type *to_type, struct field *args,
1224                       int nargs, int varargs)
1225 {
1226   smash_type (type);
1227   TYPE_TARGET_TYPE (type) = to_type;
1228   TYPE_DOMAIN_TYPE (type) = domain;
1229   TYPE_FIELDS (type) = args;
1230   TYPE_NFIELDS (type) = nargs;
1231   if (varargs)
1232     TYPE_VARARGS (type) = 1;
1233   TYPE_LENGTH (type) = 1;       /* In practice, this is never needed.  */
1234   TYPE_CODE (type) = TYPE_CODE_METHOD;
1235 }
1236
1237 /* Return a typename for a struct/union/enum type without "struct ",
1238    "union ", or "enum ".  If the type has a NULL name, return NULL.  */
1239
1240 const char *
1241 type_name_no_tag (const struct type *type)
1242 {
1243   if (TYPE_TAG_NAME (type) != NULL)
1244     return TYPE_TAG_NAME (type);
1245
1246   /* Is there code which expects this to return the name if there is
1247      no tag name?  My guess is that this is mainly used for C++ in
1248      cases where the two will always be the same.  */
1249   return TYPE_NAME (type);
1250 }
1251
1252 /* A wrapper of type_name_no_tag which calls error if the type is anonymous.
1253    Since GCC PR debug/47510 DWARF provides associated information to detect the
1254    anonymous class linkage name from its typedef.
1255
1256    Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
1257    apply it itself.  */
1258
1259 const char *
1260 type_name_no_tag_or_error (struct type *type)
1261 {
1262   struct type *saved_type = type;
1263   const char *name;
1264   struct objfile *objfile;
1265
1266   CHECK_TYPEDEF (type);
1267
1268   name = type_name_no_tag (type);
1269   if (name != NULL)
1270     return name;
1271
1272   name = type_name_no_tag (saved_type);
1273   objfile = TYPE_OBJFILE (saved_type);
1274   error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
1275          name ? name : "<anonymous>",
1276          objfile ? objfile_name (objfile) : "<arch>");
1277 }
1278
1279 /* Lookup a typedef or primitive type named NAME, visible in lexical
1280    block BLOCK.  If NOERR is nonzero, return zero if NAME is not
1281    suitably defined.  */
1282
1283 struct type *
1284 lookup_typename (const struct language_defn *language,
1285                  struct gdbarch *gdbarch, const char *name,
1286                  const struct block *block, int noerr)
1287 {
1288   struct symbol *sym;
1289   struct type *type;
1290
1291   sym = lookup_symbol (name, block, VAR_DOMAIN, 0);
1292   if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1293     return SYMBOL_TYPE (sym);
1294
1295   type = language_lookup_primitive_type_by_name (language, gdbarch, name);
1296   if (type)
1297     return type;
1298
1299   if (noerr)
1300     return NULL;
1301   error (_("No type named %s."), name);
1302 }
1303
1304 struct type *
1305 lookup_unsigned_typename (const struct language_defn *language,
1306                           struct gdbarch *gdbarch, const char *name)
1307 {
1308   char *uns = alloca (strlen (name) + 10);
1309
1310   strcpy (uns, "unsigned ");
1311   strcpy (uns + 9, name);
1312   return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0);
1313 }
1314
1315 struct type *
1316 lookup_signed_typename (const struct language_defn *language,
1317                         struct gdbarch *gdbarch, const char *name)
1318 {
1319   struct type *t;
1320   char *uns = alloca (strlen (name) + 8);
1321
1322   strcpy (uns, "signed ");
1323   strcpy (uns + 7, name);
1324   t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1);
1325   /* If we don't find "signed FOO" just try again with plain "FOO".  */
1326   if (t != NULL)
1327     return t;
1328   return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0);
1329 }
1330
1331 /* Lookup a structure type named "struct NAME",
1332    visible in lexical block BLOCK.  */
1333
1334 struct type *
1335 lookup_struct (const char *name, const struct block *block)
1336 {
1337   struct symbol *sym;
1338
1339   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1340
1341   if (sym == NULL)
1342     {
1343       error (_("No struct type named %s."), name);
1344     }
1345   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1346     {
1347       error (_("This context has class, union or enum %s, not a struct."),
1348              name);
1349     }
1350   return (SYMBOL_TYPE (sym));
1351 }
1352
1353 /* Lookup a union type named "union NAME",
1354    visible in lexical block BLOCK.  */
1355
1356 struct type *
1357 lookup_union (const char *name, const struct block *block)
1358 {
1359   struct symbol *sym;
1360   struct type *t;
1361
1362   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1363
1364   if (sym == NULL)
1365     error (_("No union type named %s."), name);
1366
1367   t = SYMBOL_TYPE (sym);
1368
1369   if (TYPE_CODE (t) == TYPE_CODE_UNION)
1370     return t;
1371
1372   /* If we get here, it's not a union.  */
1373   error (_("This context has class, struct or enum %s, not a union."), 
1374          name);
1375 }
1376
1377 /* Lookup an enum type named "enum NAME",
1378    visible in lexical block BLOCK.  */
1379
1380 struct type *
1381 lookup_enum (const char *name, const struct block *block)
1382 {
1383   struct symbol *sym;
1384
1385   sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0);
1386   if (sym == NULL)
1387     {
1388       error (_("No enum type named %s."), name);
1389     }
1390   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1391     {
1392       error (_("This context has class, struct or union %s, not an enum."), 
1393              name);
1394     }
1395   return (SYMBOL_TYPE (sym));
1396 }
1397
1398 /* Lookup a template type named "template NAME<TYPE>",
1399    visible in lexical block BLOCK.  */
1400
1401 struct type *
1402 lookup_template_type (char *name, struct type *type, 
1403                       const struct block *block)
1404 {
1405   struct symbol *sym;
1406   char *nam = (char *) 
1407     alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1408
1409   strcpy (nam, name);
1410   strcat (nam, "<");
1411   strcat (nam, TYPE_NAME (type));
1412   strcat (nam, " >");   /* FIXME, extra space still introduced in gcc?  */
1413
1414   sym = lookup_symbol (nam, block, VAR_DOMAIN, 0);
1415
1416   if (sym == NULL)
1417     {
1418       error (_("No template type named %s."), name);
1419     }
1420   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1421     {
1422       error (_("This context has class, union or enum %s, not a struct."),
1423              name);
1424     }
1425   return (SYMBOL_TYPE (sym));
1426 }
1427
1428 /* Given a type TYPE, lookup the type of the component of type named
1429    NAME.
1430
1431    TYPE can be either a struct or union, or a pointer or reference to
1432    a struct or union.  If it is a pointer or reference, its target
1433    type is automatically used.  Thus '.' and '->' are interchangable,
1434    as specified for the definitions of the expression element types
1435    STRUCTOP_STRUCT and STRUCTOP_PTR.
1436
1437    If NOERR is nonzero, return zero if NAME is not suitably defined.
1438    If NAME is the name of a baseclass type, return that type.  */
1439
1440 struct type *
1441 lookup_struct_elt_type (struct type *type, const char *name, int noerr)
1442 {
1443   int i;
1444   char *typename;
1445
1446   for (;;)
1447     {
1448       CHECK_TYPEDEF (type);
1449       if (TYPE_CODE (type) != TYPE_CODE_PTR
1450           && TYPE_CODE (type) != TYPE_CODE_REF)
1451         break;
1452       type = TYPE_TARGET_TYPE (type);
1453     }
1454
1455   if (TYPE_CODE (type) != TYPE_CODE_STRUCT 
1456       && TYPE_CODE (type) != TYPE_CODE_UNION)
1457     {
1458       typename = type_to_string (type);
1459       make_cleanup (xfree, typename);
1460       error (_("Type %s is not a structure or union type."), typename);
1461     }
1462
1463 #if 0
1464   /* FIXME: This change put in by Michael seems incorrect for the case
1465      where the structure tag name is the same as the member name.
1466      I.e. when doing "ptype bell->bar" for "struct foo { int bar; int
1467      foo; } bell;" Disabled by fnf.  */
1468   {
1469     char *typename;
1470
1471     typename = type_name_no_tag (type);
1472     if (typename != NULL && strcmp (typename, name) == 0)
1473       return type;
1474   }
1475 #endif
1476
1477   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1478     {
1479       const char *t_field_name = TYPE_FIELD_NAME (type, i);
1480
1481       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1482         {
1483           return TYPE_FIELD_TYPE (type, i);
1484         }
1485      else if (!t_field_name || *t_field_name == '\0')
1486         {
1487           struct type *subtype 
1488             = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1);
1489
1490           if (subtype != NULL)
1491             return subtype;
1492         }
1493     }
1494
1495   /* OK, it's not in this class.  Recursively check the baseclasses.  */
1496   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1497     {
1498       struct type *t;
1499
1500       t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1);
1501       if (t != NULL)
1502         {
1503           return t;
1504         }
1505     }
1506
1507   if (noerr)
1508     {
1509       return NULL;
1510     }
1511
1512   typename = type_to_string (type);
1513   make_cleanup (xfree, typename);
1514   error (_("Type %s has no component named %s."), typename, name);
1515 }
1516
1517 /* Store in *MAX the largest number representable by unsigned integer type
1518    TYPE.  */
1519
1520 void
1521 get_unsigned_type_max (struct type *type, ULONGEST *max)
1522 {
1523   unsigned int n;
1524
1525   CHECK_TYPEDEF (type);
1526   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
1527   gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
1528
1529   /* Written this way to avoid overflow.  */
1530   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1531   *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
1532 }
1533
1534 /* Store in *MIN, *MAX the smallest and largest numbers representable by
1535    signed integer type TYPE.  */
1536
1537 void
1538 get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
1539 {
1540   unsigned int n;
1541
1542   CHECK_TYPEDEF (type);
1543   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
1544   gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
1545
1546   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
1547   *min = -((ULONGEST) 1 << (n - 1));
1548   *max = ((ULONGEST) 1 << (n - 1)) - 1;
1549 }
1550
1551 /* Lookup the vptr basetype/fieldno values for TYPE.
1552    If found store vptr_basetype in *BASETYPEP if non-NULL, and return
1553    vptr_fieldno.  Also, if found and basetype is from the same objfile,
1554    cache the results.
1555    If not found, return -1 and ignore BASETYPEP.
1556    Callers should be aware that in some cases (for example,
1557    the type or one of its baseclasses is a stub type and we are
1558    debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
1559    this function will not be able to find the
1560    virtual function table pointer, and vptr_fieldno will remain -1 and
1561    vptr_basetype will remain NULL or incomplete.  */
1562
1563 int
1564 get_vptr_fieldno (struct type *type, struct type **basetypep)
1565 {
1566   CHECK_TYPEDEF (type);
1567
1568   if (TYPE_VPTR_FIELDNO (type) < 0)
1569     {
1570       int i;
1571
1572       /* We must start at zero in case the first (and only) baseclass
1573          is virtual (and hence we cannot share the table pointer).  */
1574       for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1575         {
1576           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1577           int fieldno;
1578           struct type *basetype;
1579
1580           fieldno = get_vptr_fieldno (baseclass, &basetype);
1581           if (fieldno >= 0)
1582             {
1583               /* If the type comes from a different objfile we can't cache
1584                  it, it may have a different lifetime.  PR 2384 */
1585               if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype))
1586                 {
1587                   TYPE_VPTR_FIELDNO (type) = fieldno;
1588                   TYPE_VPTR_BASETYPE (type) = basetype;
1589                 }
1590               if (basetypep)
1591                 *basetypep = basetype;
1592               return fieldno;
1593             }
1594         }
1595
1596       /* Not found.  */
1597       return -1;
1598     }
1599   else
1600     {
1601       if (basetypep)
1602         *basetypep = TYPE_VPTR_BASETYPE (type);
1603       return TYPE_VPTR_FIELDNO (type);
1604     }
1605 }
1606
1607 static void
1608 stub_noname_complaint (void)
1609 {
1610   complaint (&symfile_complaints, _("stub type has NULL name"));
1611 }
1612
1613 /* See gdbtypes.h.  */
1614
1615 int
1616 is_dynamic_type (struct type *type)
1617 {
1618   type = check_typedef (type);
1619
1620   if (TYPE_CODE (type) == TYPE_CODE_REF)
1621     type = check_typedef (TYPE_TARGET_TYPE (type));
1622
1623   switch (TYPE_CODE (type))
1624     {
1625     case TYPE_CODE_ARRAY:
1626       {
1627         const struct type *range_type;
1628
1629         gdb_assert (TYPE_NFIELDS (type) == 1);
1630         range_type = TYPE_INDEX_TYPE (type);
1631         if (!has_static_range (TYPE_RANGE_DATA (range_type)))
1632           return 1;
1633         else
1634           return is_dynamic_type (TYPE_TARGET_TYPE (type));
1635         break;
1636       }
1637     default:
1638       return 0;
1639       break;
1640     }
1641 }
1642
1643 /* Resolves dynamic bound values of an array type TYPE to static ones.
1644    ADDRESS might be needed to resolve the subrange bounds, it is the location
1645    of the associated array.  */
1646
1647 static struct type *
1648 resolve_dynamic_bounds (struct type *type, CORE_ADDR addr)
1649 {
1650   CORE_ADDR value;
1651   struct type *elt_type;
1652   struct type *range_type;
1653   struct type *ary_dim;
1654   const struct dynamic_prop *prop;
1655   const struct dwarf2_locexpr_baton *baton;
1656   struct dynamic_prop low_bound, high_bound;
1657
1658   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1659     {
1660       struct type *copy = copy_type (type);
1661
1662       TYPE_TARGET_TYPE (copy)
1663         = resolve_dynamic_bounds (TYPE_TARGET_TYPE (type), addr);
1664
1665       return copy;
1666     }
1667
1668   gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
1669
1670   elt_type = type;
1671   range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
1672
1673   prop = &TYPE_RANGE_DATA (range_type)->low;
1674   if (dwarf2_evaluate_property (prop, addr, &value))
1675     {
1676       low_bound.kind = PROP_CONST;
1677       low_bound.data.const_val = value;
1678     }
1679   else
1680     {
1681       low_bound.kind = PROP_UNDEFINED;
1682       low_bound.data.const_val = 0;
1683     }
1684
1685   prop = &TYPE_RANGE_DATA (range_type)->high;
1686   if (dwarf2_evaluate_property (prop, addr, &value))
1687     {
1688       high_bound.kind = PROP_CONST;
1689       high_bound.data.const_val = value;
1690
1691       if (TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count)
1692         high_bound.data.const_val
1693           = low_bound.data.const_val + high_bound.data.const_val - 1;
1694     }
1695   else
1696     {
1697       high_bound.kind = PROP_UNDEFINED;
1698       high_bound.data.const_val = 0;
1699     }
1700
1701   ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));
1702
1703   if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
1704     elt_type = resolve_dynamic_bounds (TYPE_TARGET_TYPE (type), addr);
1705   else
1706     elt_type = TYPE_TARGET_TYPE (type);
1707
1708   range_type = create_range_type (NULL,
1709                                   TYPE_TARGET_TYPE (range_type),
1710                                   &low_bound, &high_bound);
1711   TYPE_RANGE_DATA (range_type)->flag_bound_evaluated = 1;
1712   return create_array_type (copy_type (type),
1713                             elt_type,
1714                             range_type);
1715 }
1716
1717 /* See gdbtypes.h  */
1718
1719 struct type *
1720 resolve_dynamic_type (struct type *type, CORE_ADDR addr)
1721 {
1722   struct type *real_type = check_typedef (type);
1723   struct type *resolved_type;
1724
1725   if (!is_dynamic_type (real_type))
1726     return type;
1727
1728   resolved_type = resolve_dynamic_bounds (type, addr);
1729
1730   return resolved_type;
1731 }
1732
1733 /* Find the real type of TYPE.  This function returns the real type,
1734    after removing all layers of typedefs, and completing opaque or stub
1735    types.  Completion changes the TYPE argument, but stripping of
1736    typedefs does not.
1737
1738    Instance flags (e.g. const/volatile) are preserved as typedefs are
1739    stripped.  If necessary a new qualified form of the underlying type
1740    is created.
1741
1742    NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
1743    not been computed and we're either in the middle of reading symbols, or
1744    there was no name for the typedef in the debug info.
1745
1746    NOTE: Lookup of opaque types can throw errors for invalid symbol files.
1747    QUITs in the symbol reading code can also throw.
1748    Thus this function can throw an exception.
1749
1750    If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
1751    the target type.
1752
1753    If this is a stubbed struct (i.e. declared as struct foo *), see if
1754    we can find a full definition in some other file.  If so, copy this
1755    definition, so we can use it in future.  There used to be a comment
1756    (but not any code) that if we don't find a full definition, we'd
1757    set a flag so we don't spend time in the future checking the same
1758    type.  That would be a mistake, though--we might load in more
1759    symbols which contain a full definition for the type.  */
1760
1761 struct type *
1762 check_typedef (struct type *type)
1763 {
1764   struct type *orig_type = type;
1765   /* While we're removing typedefs, we don't want to lose qualifiers.
1766      E.g., const/volatile.  */
1767   int instance_flags = TYPE_INSTANCE_FLAGS (type);
1768
1769   gdb_assert (type);
1770
1771   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1772     {
1773       if (!TYPE_TARGET_TYPE (type))
1774         {
1775           const char *name;
1776           struct symbol *sym;
1777
1778           /* It is dangerous to call lookup_symbol if we are currently
1779              reading a symtab.  Infinite recursion is one danger.  */
1780           if (currently_reading_symtab)
1781             return make_qualified_type (type, instance_flags, NULL);
1782
1783           name = type_name_no_tag (type);
1784           /* FIXME: shouldn't we separately check the TYPE_NAME and
1785              the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or
1786              VAR_DOMAIN as appropriate?  (this code was written before
1787              TYPE_NAME and TYPE_TAG_NAME were separate).  */
1788           if (name == NULL)
1789             {
1790               stub_noname_complaint ();
1791               return make_qualified_type (type, instance_flags, NULL);
1792             }
1793           sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1794           if (sym)
1795             TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1796           else                                  /* TYPE_CODE_UNDEF */
1797             TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type));
1798         }
1799       type = TYPE_TARGET_TYPE (type);
1800
1801       /* Preserve the instance flags as we traverse down the typedef chain.
1802
1803          Handling address spaces/classes is nasty, what do we do if there's a
1804          conflict?
1805          E.g., what if an outer typedef marks the type as class_1 and an inner
1806          typedef marks the type as class_2?
1807          This is the wrong place to do such error checking.  We leave it to
1808          the code that created the typedef in the first place to flag the
1809          error.  We just pick the outer address space (akin to letting the
1810          outer cast in a chain of casting win), instead of assuming
1811          "it can't happen".  */
1812       {
1813         const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE
1814                                 | TYPE_INSTANCE_FLAG_DATA_SPACE);
1815         const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
1816         int new_instance_flags = TYPE_INSTANCE_FLAGS (type);
1817
1818         /* Treat code vs data spaces and address classes separately.  */
1819         if ((instance_flags & ALL_SPACES) != 0)
1820           new_instance_flags &= ~ALL_SPACES;
1821         if ((instance_flags & ALL_CLASSES) != 0)
1822           new_instance_flags &= ~ALL_CLASSES;
1823
1824         instance_flags |= new_instance_flags;
1825       }
1826     }
1827
1828   /* If this is a struct/class/union with no fields, then check
1829      whether a full definition exists somewhere else.  This is for
1830      systems where a type definition with no fields is issued for such
1831      types, instead of identifying them as stub types in the first
1832      place.  */
1833
1834   if (TYPE_IS_OPAQUE (type) 
1835       && opaque_type_resolution 
1836       && !currently_reading_symtab)
1837     {
1838       const char *name = type_name_no_tag (type);
1839       struct type *newtype;
1840
1841       if (name == NULL)
1842         {
1843           stub_noname_complaint ();
1844           return make_qualified_type (type, instance_flags, NULL);
1845         }
1846       newtype = lookup_transparent_type (name);
1847
1848       if (newtype)
1849         {
1850           /* If the resolved type and the stub are in the same
1851              objfile, then replace the stub type with the real deal.
1852              But if they're in separate objfiles, leave the stub
1853              alone; we'll just look up the transparent type every time
1854              we call check_typedef.  We can't create pointers between
1855              types allocated to different objfiles, since they may
1856              have different lifetimes.  Trying to copy NEWTYPE over to
1857              TYPE's objfile is pointless, too, since you'll have to
1858              move over any other types NEWTYPE refers to, which could
1859              be an unbounded amount of stuff.  */
1860           if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type))
1861             type = make_qualified_type (newtype,
1862                                         TYPE_INSTANCE_FLAGS (type),
1863                                         type);
1864           else
1865             type = newtype;
1866         }
1867     }
1868   /* Otherwise, rely on the stub flag being set for opaque/stubbed
1869      types.  */
1870   else if (TYPE_STUB (type) && !currently_reading_symtab)
1871     {
1872       const char *name = type_name_no_tag (type);
1873       /* FIXME: shouldn't we separately check the TYPE_NAME and the
1874          TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1875          as appropriate?  (this code was written before TYPE_NAME and
1876          TYPE_TAG_NAME were separate).  */
1877       struct symbol *sym;
1878
1879       if (name == NULL)
1880         {
1881           stub_noname_complaint ();
1882           return make_qualified_type (type, instance_flags, NULL);
1883         }
1884       sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0);
1885       if (sym)
1886         {
1887           /* Same as above for opaque types, we can replace the stub
1888              with the complete type only if they are in the same
1889              objfile.  */
1890           if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type))
1891             type = make_qualified_type (SYMBOL_TYPE (sym),
1892                                         TYPE_INSTANCE_FLAGS (type),
1893                                         type);
1894           else
1895             type = SYMBOL_TYPE (sym);
1896         }
1897     }
1898
1899   if (TYPE_TARGET_STUB (type))
1900     {
1901       struct type *range_type;
1902       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1903
1904       if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1905         {
1906           /* Nothing we can do.  */
1907         }
1908       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1909         {
1910           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1911           TYPE_TARGET_STUB (type) = 0;
1912         }
1913     }
1914
1915   type = make_qualified_type (type, instance_flags, NULL);
1916
1917   /* Cache TYPE_LENGTH for future use.  */
1918   TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1919
1920   return type;
1921 }
1922
1923 /* Parse a type expression in the string [P..P+LENGTH).  If an error
1924    occurs, silently return a void type.  */
1925
1926 static struct type *
1927 safe_parse_type (struct gdbarch *gdbarch, char *p, int length)
1928 {
1929   struct ui_file *saved_gdb_stderr;
1930   struct type *type = NULL; /* Initialize to keep gcc happy.  */
1931   volatile struct gdb_exception except;
1932
1933   /* Suppress error messages.  */
1934   saved_gdb_stderr = gdb_stderr;
1935   gdb_stderr = ui_file_new ();
1936
1937   /* Call parse_and_eval_type() without fear of longjmp()s.  */
1938   TRY_CATCH (except, RETURN_MASK_ERROR)
1939     {
1940       type = parse_and_eval_type (p, length);
1941     }
1942
1943   if (except.reason < 0)
1944     type = builtin_type (gdbarch)->builtin_void;
1945
1946   /* Stop suppressing error messages.  */
1947   ui_file_delete (gdb_stderr);
1948   gdb_stderr = saved_gdb_stderr;
1949
1950   return type;
1951 }
1952
1953 /* Ugly hack to convert method stubs into method types.
1954
1955    He ain't kiddin'.  This demangles the name of the method into a
1956    string including argument types, parses out each argument type,
1957    generates a string casting a zero to that type, evaluates the
1958    string, and stuffs the resulting type into an argtype vector!!!
1959    Then it knows the type of the whole function (including argument
1960    types for overloading), which info used to be in the stab's but was
1961    removed to hack back the space required for them.  */
1962
1963 static void
1964 check_stub_method (struct type *type, int method_id, int signature_id)
1965 {
1966   struct gdbarch *gdbarch = get_type_arch (type);
1967   struct fn_field *f;
1968   char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1969   char *demangled_name = gdb_demangle (mangled_name,
1970                                        DMGL_PARAMS | DMGL_ANSI);
1971   char *argtypetext, *p;
1972   int depth = 0, argcount = 1;
1973   struct field *argtypes;
1974   struct type *mtype;
1975
1976   /* Make sure we got back a function string that we can use.  */
1977   if (demangled_name)
1978     p = strchr (demangled_name, '(');
1979   else
1980     p = NULL;
1981
1982   if (demangled_name == NULL || p == NULL)
1983     error (_("Internal: Cannot demangle mangled name `%s'."), 
1984            mangled_name);
1985
1986   /* Now, read in the parameters that define this type.  */
1987   p += 1;
1988   argtypetext = p;
1989   while (*p)
1990     {
1991       if (*p == '(' || *p == '<')
1992         {
1993           depth += 1;
1994         }
1995       else if (*p == ')' || *p == '>')
1996         {
1997           depth -= 1;
1998         }
1999       else if (*p == ',' && depth == 0)
2000         {
2001           argcount += 1;
2002         }
2003
2004       p += 1;
2005     }
2006
2007   /* If we read one argument and it was ``void'', don't count it.  */
2008   if (strncmp (argtypetext, "(void)", 6) == 0)
2009     argcount -= 1;
2010
2011   /* We need one extra slot, for the THIS pointer.  */
2012
2013   argtypes = (struct field *)
2014     TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
2015   p = argtypetext;
2016
2017   /* Add THIS pointer for non-static methods.  */
2018   f = TYPE_FN_FIELDLIST1 (type, method_id);
2019   if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
2020     argcount = 0;
2021   else
2022     {
2023       argtypes[0].type = lookup_pointer_type (type);
2024       argcount = 1;
2025     }
2026
2027   if (*p != ')')                /* () means no args, skip while.  */
2028     {
2029       depth = 0;
2030       while (*p)
2031         {
2032           if (depth <= 0 && (*p == ',' || *p == ')'))
2033             {
2034               /* Avoid parsing of ellipsis, they will be handled below.
2035                  Also avoid ``void'' as above.  */
2036               if (strncmp (argtypetext, "...", p - argtypetext) != 0
2037                   && strncmp (argtypetext, "void", p - argtypetext) != 0)
2038                 {
2039                   argtypes[argcount].type =
2040                     safe_parse_type (gdbarch, argtypetext, p - argtypetext);
2041                   argcount += 1;
2042                 }
2043               argtypetext = p + 1;
2044             }
2045
2046           if (*p == '(' || *p == '<')
2047             {
2048               depth += 1;
2049             }
2050           else if (*p == ')' || *p == '>')
2051             {
2052               depth -= 1;
2053             }
2054
2055           p += 1;
2056         }
2057     }
2058
2059   TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
2060
2061   /* Now update the old "stub" type into a real type.  */
2062   mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
2063   TYPE_DOMAIN_TYPE (mtype) = type;
2064   TYPE_FIELDS (mtype) = argtypes;
2065   TYPE_NFIELDS (mtype) = argcount;
2066   TYPE_STUB (mtype) = 0;
2067   TYPE_FN_FIELD_STUB (f, signature_id) = 0;
2068   if (p[-2] == '.')
2069     TYPE_VARARGS (mtype) = 1;
2070
2071   xfree (demangled_name);
2072 }
2073
2074 /* This is the external interface to check_stub_method, above.  This
2075    function unstubs all of the signatures for TYPE's METHOD_ID method
2076    name.  After calling this function TYPE_FN_FIELD_STUB will be
2077    cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
2078    correct.
2079
2080    This function unfortunately can not die until stabs do.  */
2081
2082 void
2083 check_stub_method_group (struct type *type, int method_id)
2084 {
2085   int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
2086   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
2087   int j, found_stub = 0;
2088
2089   for (j = 0; j < len; j++)
2090     if (TYPE_FN_FIELD_STUB (f, j))
2091       {
2092         found_stub = 1;
2093         check_stub_method (type, method_id, j);
2094       }
2095
2096   /* GNU v3 methods with incorrect names were corrected when we read
2097      in type information, because it was cheaper to do it then.  The
2098      only GNU v2 methods with incorrect method names are operators and
2099      destructors; destructors were also corrected when we read in type
2100      information.
2101
2102      Therefore the only thing we need to handle here are v2 operator
2103      names.  */
2104   if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
2105     {
2106       int ret;
2107       char dem_opname[256];
2108
2109       ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2110                                                            method_id),
2111                                    dem_opname, DMGL_ANSI);
2112       if (!ret)
2113         ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, 
2114                                                              method_id),
2115                                      dem_opname, 0);
2116       if (ret)
2117         TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
2118     }
2119 }
2120
2121 /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690.  */
2122 const struct cplus_struct_type cplus_struct_default = { };
2123
2124 void
2125 allocate_cplus_struct_type (struct type *type)
2126 {
2127   if (HAVE_CPLUS_STRUCT (type))
2128     /* Structure was already allocated.  Nothing more to do.  */
2129     return;
2130
2131   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
2132   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2133     TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
2134   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
2135 }
2136
2137 const struct gnat_aux_type gnat_aux_default =
2138   { NULL };
2139
2140 /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
2141    and allocate the associated gnat-specific data.  The gnat-specific
2142    data is also initialized to gnat_aux_default.  */
2143
2144 void
2145 allocate_gnat_aux_type (struct type *type)
2146 {
2147   TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
2148   TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
2149     TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
2150   *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
2151 }
2152
2153 /* Helper function to initialize the standard scalar types.
2154
2155    If NAME is non-NULL, then it is used to initialize the type name.
2156    Note that NAME is not copied; it is required to have a lifetime at
2157    least as long as OBJFILE.  */
2158
2159 struct type *
2160 init_type (enum type_code code, int length, int flags,
2161            const char *name, struct objfile *objfile)
2162 {
2163   struct type *type;
2164
2165   type = alloc_type (objfile);
2166   TYPE_CODE (type) = code;
2167   TYPE_LENGTH (type) = length;
2168
2169   gdb_assert (!(flags & (TYPE_FLAG_MIN - 1)));
2170   if (flags & TYPE_FLAG_UNSIGNED)
2171     TYPE_UNSIGNED (type) = 1;
2172   if (flags & TYPE_FLAG_NOSIGN)
2173     TYPE_NOSIGN (type) = 1;
2174   if (flags & TYPE_FLAG_STUB)
2175     TYPE_STUB (type) = 1;
2176   if (flags & TYPE_FLAG_TARGET_STUB)
2177     TYPE_TARGET_STUB (type) = 1;
2178   if (flags & TYPE_FLAG_STATIC)
2179     TYPE_STATIC (type) = 1;
2180   if (flags & TYPE_FLAG_PROTOTYPED)
2181     TYPE_PROTOTYPED (type) = 1;
2182   if (flags & TYPE_FLAG_INCOMPLETE)
2183     TYPE_INCOMPLETE (type) = 1;
2184   if (flags & TYPE_FLAG_VARARGS)
2185     TYPE_VARARGS (type) = 1;
2186   if (flags & TYPE_FLAG_VECTOR)
2187     TYPE_VECTOR (type) = 1;
2188   if (flags & TYPE_FLAG_STUB_SUPPORTED)
2189     TYPE_STUB_SUPPORTED (type) = 1;
2190   if (flags & TYPE_FLAG_FIXED_INSTANCE)
2191     TYPE_FIXED_INSTANCE (type) = 1;
2192   if (flags & TYPE_FLAG_GNU_IFUNC)
2193     TYPE_GNU_IFUNC (type) = 1;
2194
2195   TYPE_NAME (type) = name;
2196
2197   /* C++ fancies.  */
2198
2199   if (name && strcmp (name, "char") == 0)
2200     TYPE_NOSIGN (type) = 1;
2201
2202   switch (code)
2203     {
2204       case TYPE_CODE_STRUCT:
2205       case TYPE_CODE_UNION:
2206       case TYPE_CODE_NAMESPACE:
2207         INIT_CPLUS_SPECIFIC (type);
2208         break;
2209       case TYPE_CODE_FLT:
2210         TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
2211         break;
2212       case TYPE_CODE_FUNC:
2213         INIT_FUNC_SPECIFIC (type);
2214         break;
2215     }
2216   return type;
2217 }
2218 \f
2219 /* Queries on types.  */
2220
2221 int
2222 can_dereference (struct type *t)
2223 {
2224   /* FIXME: Should we return true for references as well as
2225      pointers?  */
2226   CHECK_TYPEDEF (t);
2227   return
2228     (t != NULL
2229      && TYPE_CODE (t) == TYPE_CODE_PTR
2230      && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
2231 }
2232
2233 int
2234 is_integral_type (struct type *t)
2235 {
2236   CHECK_TYPEDEF (t);
2237   return
2238     ((t != NULL)
2239      && ((TYPE_CODE (t) == TYPE_CODE_INT)
2240          || (TYPE_CODE (t) == TYPE_CODE_ENUM)
2241          || (TYPE_CODE (t) == TYPE_CODE_FLAGS)
2242          || (TYPE_CODE (t) == TYPE_CODE_CHAR)
2243          || (TYPE_CODE (t) == TYPE_CODE_RANGE)
2244          || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
2245 }
2246
2247 /* Return true if TYPE is scalar.  */
2248
2249 static int
2250 is_scalar_type (struct type *type)
2251 {
2252   CHECK_TYPEDEF (type);
2253
2254   switch (TYPE_CODE (type))
2255     {
2256     case TYPE_CODE_ARRAY:
2257     case TYPE_CODE_STRUCT:
2258     case TYPE_CODE_UNION:
2259     case TYPE_CODE_SET:
2260     case TYPE_CODE_STRING:
2261       return 0;
2262     default:
2263       return 1;
2264     }
2265 }
2266
2267 /* Return true if T is scalar, or a composite type which in practice has
2268    the memory layout of a scalar type.  E.g., an array or struct with only
2269    one scalar element inside it, or a union with only scalar elements.  */
2270
2271 int
2272 is_scalar_type_recursive (struct type *t)
2273 {
2274   CHECK_TYPEDEF (t);
2275
2276   if (is_scalar_type (t))
2277     return 1;
2278   /* Are we dealing with an array or string of known dimensions?  */
2279   else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY
2280             || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1
2281            && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE)
2282     {
2283       LONGEST low_bound, high_bound;
2284       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
2285
2286       get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound);
2287
2288       return high_bound == low_bound && is_scalar_type_recursive (elt_type);
2289     }
2290   /* Are we dealing with a struct with one element?  */
2291   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1)
2292     return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0));
2293   else if (TYPE_CODE (t) == TYPE_CODE_UNION)
2294     {
2295       int i, n = TYPE_NFIELDS (t);
2296
2297       /* If all elements of the union are scalar, then the union is scalar.  */
2298       for (i = 0; i < n; i++)
2299         if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i)))
2300           return 0;
2301
2302       return 1;
2303     }
2304
2305   return 0;
2306 }
2307
2308 /* A helper function which returns true if types A and B represent the
2309    "same" class type.  This is true if the types have the same main
2310    type, or the same name.  */
2311
2312 int
2313 class_types_same_p (const struct type *a, const struct type *b)
2314 {
2315   return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
2316           || (TYPE_NAME (a) && TYPE_NAME (b)
2317               && !strcmp (TYPE_NAME (a), TYPE_NAME (b))));
2318 }
2319
2320 /* If BASE is an ancestor of DCLASS return the distance between them.
2321    otherwise return -1;
2322    eg:
2323
2324    class A {};
2325    class B: public A {};
2326    class C: public B {};
2327    class D: C {};
2328
2329    distance_to_ancestor (A, A, 0) = 0
2330    distance_to_ancestor (A, B, 0) = 1
2331    distance_to_ancestor (A, C, 0) = 2
2332    distance_to_ancestor (A, D, 0) = 3
2333
2334    If PUBLIC is 1 then only public ancestors are considered,
2335    and the function returns the distance only if BASE is a public ancestor
2336    of DCLASS.
2337    Eg:
2338
2339    distance_to_ancestor (A, D, 1) = -1.  */
2340
2341 static int
2342 distance_to_ancestor (struct type *base, struct type *dclass, int public)
2343 {
2344   int i;
2345   int d;
2346
2347   CHECK_TYPEDEF (base);
2348   CHECK_TYPEDEF (dclass);
2349
2350   if (class_types_same_p (base, dclass))
2351     return 0;
2352
2353   for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
2354     {
2355       if (public && ! BASETYPE_VIA_PUBLIC (dclass, i))
2356         continue;
2357
2358       d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), public);
2359       if (d >= 0)
2360         return 1 + d;
2361     }
2362
2363   return -1;
2364 }
2365
2366 /* Check whether BASE is an ancestor or base class or DCLASS
2367    Return 1 if so, and 0 if not.
2368    Note: If BASE and DCLASS are of the same type, this function
2369    will return 1. So for some class A, is_ancestor (A, A) will
2370    return 1.  */
2371
2372 int
2373 is_ancestor (struct type *base, struct type *dclass)
2374 {
2375   return distance_to_ancestor (base, dclass, 0) >= 0;
2376 }
2377
2378 /* Like is_ancestor, but only returns true when BASE is a public
2379    ancestor of DCLASS.  */
2380
2381 int
2382 is_public_ancestor (struct type *base, struct type *dclass)
2383 {
2384   return distance_to_ancestor (base, dclass, 1) >= 0;
2385 }
2386
2387 /* A helper function for is_unique_ancestor.  */
2388
2389 static int
2390 is_unique_ancestor_worker (struct type *base, struct type *dclass,
2391                            int *offset,
2392                            const gdb_byte *valaddr, int embedded_offset,
2393                            CORE_ADDR address, struct value *val)
2394 {
2395   int i, count = 0;
2396
2397   CHECK_TYPEDEF (base);
2398   CHECK_TYPEDEF (dclass);
2399
2400   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
2401     {
2402       struct type *iter;
2403       int this_offset;
2404
2405       iter = check_typedef (TYPE_BASECLASS (dclass, i));
2406
2407       this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
2408                                       address, val);
2409
2410       if (class_types_same_p (base, iter))
2411         {
2412           /* If this is the first subclass, set *OFFSET and set count
2413              to 1.  Otherwise, if this is at the same offset as
2414              previous instances, do nothing.  Otherwise, increment
2415              count.  */
2416           if (*offset == -1)
2417             {
2418               *offset = this_offset;
2419               count = 1;
2420             }
2421           else if (this_offset == *offset)
2422             {
2423               /* Nothing.  */
2424             }
2425           else
2426             ++count;
2427         }
2428       else
2429         count += is_unique_ancestor_worker (base, iter, offset,
2430                                             valaddr,
2431                                             embedded_offset + this_offset,
2432                                             address, val);
2433     }
2434
2435   return count;
2436 }
2437
2438 /* Like is_ancestor, but only returns true if BASE is a unique base
2439    class of the type of VAL.  */
2440
2441 int
2442 is_unique_ancestor (struct type *base, struct value *val)
2443 {
2444   int offset = -1;
2445
2446   return is_unique_ancestor_worker (base, value_type (val), &offset,
2447                                     value_contents_for_printing (val),
2448                                     value_embedded_offset (val),
2449                                     value_address (val), val) == 1;
2450 }
2451
2452 \f
2453 /* Overload resolution.  */
2454
2455 /* Return the sum of the rank of A with the rank of B.  */
2456
2457 struct rank
2458 sum_ranks (struct rank a, struct rank b)
2459 {
2460   struct rank c;
2461   c.rank = a.rank + b.rank;
2462   c.subrank = a.subrank + b.subrank;
2463   return c;
2464 }
2465
2466 /* Compare rank A and B and return:
2467    0 if a = b
2468    1 if a is better than b
2469   -1 if b is better than a.  */
2470
2471 int
2472 compare_ranks (struct rank a, struct rank b)
2473 {
2474   if (a.rank == b.rank)
2475     {
2476       if (a.subrank == b.subrank)
2477         return 0;
2478       if (a.subrank < b.subrank)
2479         return 1;
2480       if (a.subrank > b.subrank)
2481         return -1;
2482     }
2483
2484   if (a.rank < b.rank)
2485     return 1;
2486
2487   /* a.rank > b.rank */
2488   return -1;
2489 }
2490
2491 /* Functions for overload resolution begin here.  */
2492
2493 /* Compare two badness vectors A and B and return the result.
2494    0 => A and B are identical
2495    1 => A and B are incomparable
2496    2 => A is better than B
2497    3 => A is worse than B  */
2498
2499 int
2500 compare_badness (struct badness_vector *a, struct badness_vector *b)
2501 {
2502   int i;
2503   int tmp;
2504   short found_pos = 0;          /* any positives in c? */
2505   short found_neg = 0;          /* any negatives in c? */
2506
2507   /* differing lengths => incomparable */
2508   if (a->length != b->length)
2509     return 1;
2510
2511   /* Subtract b from a */
2512   for (i = 0; i < a->length; i++)
2513     {
2514       tmp = compare_ranks (b->rank[i], a->rank[i]);
2515       if (tmp > 0)
2516         found_pos = 1;
2517       else if (tmp < 0)
2518         found_neg = 1;
2519     }
2520
2521   if (found_pos)
2522     {
2523       if (found_neg)
2524         return 1;               /* incomparable */
2525       else
2526         return 3;               /* A > B */
2527     }
2528   else
2529     /* no positives */
2530     {
2531       if (found_neg)
2532         return 2;               /* A < B */
2533       else
2534         return 0;               /* A == B */
2535     }
2536 }
2537
2538 /* Rank a function by comparing its parameter types (PARMS, length
2539    NPARMS), to the types of an argument list (ARGS, length NARGS).
2540    Return a pointer to a badness vector.  This has NARGS + 1
2541    entries.  */
2542
2543 struct badness_vector *
2544 rank_function (struct type **parms, int nparms, 
2545                struct value **args, int nargs)
2546 {
2547   int i;
2548   struct badness_vector *bv;
2549   int min_len = nparms < nargs ? nparms : nargs;
2550
2551   bv = xmalloc (sizeof (struct badness_vector));
2552   bv->length = nargs + 1;       /* add 1 for the length-match rank.  */
2553   bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2554
2555   /* First compare the lengths of the supplied lists.
2556      If there is a mismatch, set it to a high value.  */
2557
2558   /* pai/1997-06-03 FIXME: when we have debug info about default
2559      arguments and ellipsis parameter lists, we should consider those
2560      and rank the length-match more finely.  */
2561
2562   LENGTH_MATCH (bv) = (nargs != nparms)
2563                       ? LENGTH_MISMATCH_BADNESS
2564                       : EXACT_MATCH_BADNESS;
2565
2566   /* Now rank all the parameters of the candidate function.  */
2567   for (i = 1; i <= min_len; i++)
2568     bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]),
2569                                  args[i - 1]);
2570
2571   /* If more arguments than parameters, add dummy entries.  */
2572   for (i = min_len + 1; i <= nargs; i++)
2573     bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2574
2575   return bv;
2576 }
2577
2578 /* Compare the names of two integer types, assuming that any sign
2579    qualifiers have been checked already.  We do it this way because
2580    there may be an "int" in the name of one of the types.  */
2581
2582 static int
2583 integer_types_same_name_p (const char *first, const char *second)
2584 {
2585   int first_p, second_p;
2586
2587   /* If both are shorts, return 1; if neither is a short, keep
2588      checking.  */
2589   first_p = (strstr (first, "short") != NULL);
2590   second_p = (strstr (second, "short") != NULL);
2591   if (first_p && second_p)
2592     return 1;
2593   if (first_p || second_p)
2594     return 0;
2595
2596   /* Likewise for long.  */
2597   first_p = (strstr (first, "long") != NULL);
2598   second_p = (strstr (second, "long") != NULL);
2599   if (first_p && second_p)
2600     return 1;
2601   if (first_p || second_p)
2602     return 0;
2603
2604   /* Likewise for char.  */
2605   first_p = (strstr (first, "char") != NULL);
2606   second_p = (strstr (second, "char") != NULL);
2607   if (first_p && second_p)
2608     return 1;
2609   if (first_p || second_p)
2610     return 0;
2611
2612   /* They must both be ints.  */
2613   return 1;
2614 }
2615
2616 /* Compares type A to type B returns 1 if the represent the same type
2617    0 otherwise.  */
2618
2619 int
2620 types_equal (struct type *a, struct type *b)
2621 {
2622   /* Identical type pointers.  */
2623   /* However, this still doesn't catch all cases of same type for b
2624      and a.  The reason is that builtin types are different from
2625      the same ones constructed from the object.  */
2626   if (a == b)
2627     return 1;
2628
2629   /* Resolve typedefs */
2630   if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF)
2631     a = check_typedef (a);
2632   if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF)
2633     b = check_typedef (b);
2634
2635   /* If after resolving typedefs a and b are not of the same type
2636      code then they are not equal.  */
2637   if (TYPE_CODE (a) != TYPE_CODE (b))
2638     return 0;
2639
2640   /* If a and b are both pointers types or both reference types then
2641      they are equal of the same type iff the objects they refer to are
2642      of the same type.  */
2643   if (TYPE_CODE (a) == TYPE_CODE_PTR
2644       || TYPE_CODE (a) == TYPE_CODE_REF)
2645     return types_equal (TYPE_TARGET_TYPE (a),
2646                         TYPE_TARGET_TYPE (b));
2647
2648   /* Well, damnit, if the names are exactly the same, I'll say they
2649      are exactly the same.  This happens when we generate method
2650      stubs.  The types won't point to the same address, but they
2651      really are the same.  */
2652
2653   if (TYPE_NAME (a) && TYPE_NAME (b)
2654       && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0)
2655     return 1;
2656
2657   /* Check if identical after resolving typedefs.  */
2658   if (a == b)
2659     return 1;
2660
2661   /* Two function types are equal if their argument and return types
2662      are equal.  */
2663   if (TYPE_CODE (a) == TYPE_CODE_FUNC)
2664     {
2665       int i;
2666
2667       if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b))
2668         return 0;
2669       
2670       if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
2671         return 0;
2672
2673       for (i = 0; i < TYPE_NFIELDS (a); ++i)
2674         if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i)))
2675           return 0;
2676
2677       return 1;
2678     }
2679
2680   return 0;
2681 }
2682 \f
2683 /* Deep comparison of types.  */
2684
2685 /* An entry in the type-equality bcache.  */
2686
2687 typedef struct type_equality_entry
2688 {
2689   struct type *type1, *type2;
2690 } type_equality_entry_d;
2691
2692 DEF_VEC_O (type_equality_entry_d);
2693
2694 /* A helper function to compare two strings.  Returns 1 if they are
2695    the same, 0 otherwise.  Handles NULLs properly.  */
2696
2697 static int
2698 compare_maybe_null_strings (const char *s, const char *t)
2699 {
2700   if (s == NULL && t != NULL)
2701     return 0;
2702   else if (s != NULL && t == NULL)
2703     return 0;
2704   else if (s == NULL && t== NULL)
2705     return 1;
2706   return strcmp (s, t) == 0;
2707 }
2708
2709 /* A helper function for check_types_worklist that checks two types for
2710    "deep" equality.  Returns non-zero if the types are considered the
2711    same, zero otherwise.  */
2712
2713 static int
2714 check_types_equal (struct type *type1, struct type *type2,
2715                    VEC (type_equality_entry_d) **worklist)
2716 {
2717   CHECK_TYPEDEF (type1);
2718   CHECK_TYPEDEF (type2);
2719
2720   if (type1 == type2)
2721     return 1;
2722
2723   if (TYPE_CODE (type1) != TYPE_CODE (type2)
2724       || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
2725       || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)
2726       || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2)
2727       || TYPE_VARARGS (type1) != TYPE_VARARGS (type2)
2728       || TYPE_VECTOR (type1) != TYPE_VECTOR (type2)
2729       || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
2730       || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2)
2731       || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2))
2732     return 0;
2733
2734   if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1),
2735                                    TYPE_TAG_NAME (type2)))
2736     return 0;
2737   if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2)))
2738     return 0;
2739
2740   if (TYPE_CODE (type1) == TYPE_CODE_RANGE)
2741     {
2742       if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2),
2743                   sizeof (*TYPE_RANGE_DATA (type1))) != 0)
2744         return 0;
2745     }
2746   else
2747     {
2748       int i;
2749
2750       for (i = 0; i < TYPE_NFIELDS (type1); ++i)
2751         {
2752           const struct field *field1 = &TYPE_FIELD (type1, i);
2753           const struct field *field2 = &TYPE_FIELD (type2, i);
2754           struct type_equality_entry entry;
2755
2756           if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
2757               || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
2758               || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2))
2759             return 0;
2760           if (!compare_maybe_null_strings (FIELD_NAME (*field1),
2761                                            FIELD_NAME (*field2)))
2762             return 0;
2763           switch (FIELD_LOC_KIND (*field1))
2764             {
2765             case FIELD_LOC_KIND_BITPOS:
2766               if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2))
2767                 return 0;
2768               break;
2769             case FIELD_LOC_KIND_ENUMVAL:
2770               if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
2771                 return 0;
2772               break;
2773             case FIELD_LOC_KIND_PHYSADDR:
2774               if (FIELD_STATIC_PHYSADDR (*field1)
2775                   != FIELD_STATIC_PHYSADDR (*field2))
2776                 return 0;
2777               break;
2778             case FIELD_LOC_KIND_PHYSNAME:
2779               if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1),
2780                                                FIELD_STATIC_PHYSNAME (*field2)))
2781                 return 0;
2782               break;
2783             case FIELD_LOC_KIND_DWARF_BLOCK:
2784               {
2785                 struct dwarf2_locexpr_baton *block1, *block2;
2786
2787                 block1 = FIELD_DWARF_BLOCK (*field1);
2788                 block2 = FIELD_DWARF_BLOCK (*field2);
2789                 if (block1->per_cu != block2->per_cu
2790                     || block1->size != block2->size
2791                     || memcmp (block1->data, block2->data, block1->size) != 0)
2792                   return 0;
2793               }
2794               break;
2795             default:
2796               internal_error (__FILE__, __LINE__, _("Unsupported field kind "
2797                                                     "%d by check_types_equal"),
2798                               FIELD_LOC_KIND (*field1));
2799             }
2800
2801           entry.type1 = FIELD_TYPE (*field1);
2802           entry.type2 = FIELD_TYPE (*field2);
2803           VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2804         }
2805     }
2806
2807   if (TYPE_TARGET_TYPE (type1) != NULL)
2808     {
2809       struct type_equality_entry entry;
2810
2811       if (TYPE_TARGET_TYPE (type2) == NULL)
2812         return 0;
2813
2814       entry.type1 = TYPE_TARGET_TYPE (type1);
2815       entry.type2 = TYPE_TARGET_TYPE (type2);
2816       VEC_safe_push (type_equality_entry_d, *worklist, &entry);
2817     }
2818   else if (TYPE_TARGET_TYPE (type2) != NULL)
2819     return 0;
2820
2821   return 1;
2822 }
2823
2824 /* Check types on a worklist for equality.  Returns zero if any pair
2825    is not equal, non-zero if they are all considered equal.  */
2826
2827 static int
2828 check_types_worklist (VEC (type_equality_entry_d) **worklist,
2829                       struct bcache *cache)
2830 {
2831   while (!VEC_empty (type_equality_entry_d, *worklist))
2832     {
2833       struct type_equality_entry entry;
2834       int added;
2835
2836       entry = *VEC_last (type_equality_entry_d, *worklist);
2837       VEC_pop (type_equality_entry_d, *worklist);
2838
2839       /* If the type pair has already been visited, we know it is
2840          ok.  */
2841       bcache_full (&entry, sizeof (entry), cache, &added);
2842       if (!added)
2843         continue;
2844
2845       if (check_types_equal (entry.type1, entry.type2, worklist) == 0)
2846         return 0;
2847     }
2848
2849   return 1;
2850 }
2851
2852 /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a
2853    "deep comparison".  Otherwise return zero.  */
2854
2855 int
2856 types_deeply_equal (struct type *type1, struct type *type2)
2857 {
2858   volatile struct gdb_exception except;
2859   int result = 0;
2860   struct bcache *cache;
2861   VEC (type_equality_entry_d) *worklist = NULL;
2862   struct type_equality_entry entry;
2863
2864   gdb_assert (type1 != NULL && type2 != NULL);
2865
2866   /* Early exit for the simple case.  */
2867   if (type1 == type2)
2868     return 1;
2869
2870   cache = bcache_xmalloc (NULL, NULL);
2871
2872   entry.type1 = type1;
2873   entry.type2 = type2;
2874   VEC_safe_push (type_equality_entry_d, worklist, &entry);
2875
2876   TRY_CATCH (except, RETURN_MASK_ALL)
2877     {
2878       result = check_types_worklist (&worklist, cache);
2879     }
2880   /* check_types_worklist calls several nested helper functions,
2881      some of which can raise a GDB Exception, so we just check
2882      and rethrow here.  If there is a GDB exception, a comparison
2883      is not capable (or trusted), so exit.  */
2884   bcache_xfree (cache);
2885   VEC_free (type_equality_entry_d, worklist);
2886   /* Rethrow if there was a problem.  */
2887   if (except.reason < 0)
2888     throw_exception (except);
2889
2890   return result;
2891 }
2892 \f
2893 /* Compare one type (PARM) for compatibility with another (ARG).
2894  * PARM is intended to be the parameter type of a function; and
2895  * ARG is the supplied argument's type.  This function tests if
2896  * the latter can be converted to the former.
2897  * VALUE is the argument's value or NULL if none (or called recursively)
2898  *
2899  * Return 0 if they are identical types;
2900  * Otherwise, return an integer which corresponds to how compatible
2901  * PARM is to ARG.  The higher the return value, the worse the match.
2902  * Generally the "bad" conversions are all uniformly assigned a 100.  */
2903
2904 struct rank
2905 rank_one_type (struct type *parm, struct type *arg, struct value *value)
2906 {
2907   struct rank rank = {0,0};
2908
2909   if (types_equal (parm, arg))
2910     return EXACT_MATCH_BADNESS;
2911
2912   /* Resolve typedefs */
2913   if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2914     parm = check_typedef (parm);
2915   if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2916     arg = check_typedef (arg);
2917
2918   /* See through references, since we can almost make non-references
2919      references.  */
2920   if (TYPE_CODE (arg) == TYPE_CODE_REF)
2921     return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
2922                        REFERENCE_CONVERSION_BADNESS));
2923   if (TYPE_CODE (parm) == TYPE_CODE_REF)
2924     return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
2925                        REFERENCE_CONVERSION_BADNESS));
2926   if (overload_debug)
2927   /* Debugging only.  */
2928     fprintf_filtered (gdb_stderr, 
2929                       "------ Arg is %s [%d], parm is %s [%d]\n",
2930                       TYPE_NAME (arg), TYPE_CODE (arg), 
2931                       TYPE_NAME (parm), TYPE_CODE (parm));
2932
2933   /* x -> y means arg of type x being supplied for parameter of type y.  */
2934
2935   switch (TYPE_CODE (parm))
2936     {
2937     case TYPE_CODE_PTR:
2938       switch (TYPE_CODE (arg))
2939         {
2940         case TYPE_CODE_PTR:
2941
2942           /* Allowed pointer conversions are:
2943              (a) pointer to void-pointer conversion.  */
2944           if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2945             return VOID_PTR_CONVERSION_BADNESS;
2946
2947           /* (b) pointer to ancestor-pointer conversion.  */
2948           rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
2949                                                TYPE_TARGET_TYPE (arg),
2950                                                0);
2951           if (rank.subrank >= 0)
2952             return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
2953
2954           return INCOMPATIBLE_TYPE_BADNESS;
2955         case TYPE_CODE_ARRAY:
2956           if (types_equal (TYPE_TARGET_TYPE (parm),
2957                            TYPE_TARGET_TYPE (arg)))
2958             return EXACT_MATCH_BADNESS;
2959           return INCOMPATIBLE_TYPE_BADNESS;
2960         case TYPE_CODE_FUNC:
2961           return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
2962         case TYPE_CODE_INT:
2963           if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT)
2964             {
2965               if (value_as_long (value) == 0)
2966                 {
2967                   /* Null pointer conversion: allow it to be cast to a pointer.
2968                      [4.10.1 of C++ standard draft n3290]  */
2969                   return NULL_POINTER_CONVERSION_BADNESS;
2970                 }
2971               else
2972                 {
2973                   /* If type checking is disabled, allow the conversion.  */
2974                   if (!strict_type_checking)
2975                     return NS_INTEGER_POINTER_CONVERSION_BADNESS;
2976                 }
2977             }
2978           /* fall through  */
2979         case TYPE_CODE_ENUM:
2980         case TYPE_CODE_FLAGS:
2981         case TYPE_CODE_CHAR:
2982         case TYPE_CODE_RANGE:
2983         case TYPE_CODE_BOOL:
2984         default:
2985           return INCOMPATIBLE_TYPE_BADNESS;
2986         }
2987     case TYPE_CODE_ARRAY:
2988       switch (TYPE_CODE (arg))
2989         {
2990         case TYPE_CODE_PTR:
2991         case TYPE_CODE_ARRAY:
2992           return rank_one_type (TYPE_TARGET_TYPE (parm), 
2993                                 TYPE_TARGET_TYPE (arg), NULL);
2994         default:
2995           return INCOMPATIBLE_TYPE_BADNESS;
2996         }
2997     case TYPE_CODE_FUNC:
2998       switch (TYPE_CODE (arg))
2999         {
3000         case TYPE_CODE_PTR:     /* funcptr -> func */
3001           return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
3002         default:
3003           return INCOMPATIBLE_TYPE_BADNESS;
3004         }
3005     case TYPE_CODE_INT:
3006       switch (TYPE_CODE (arg))
3007         {
3008         case TYPE_CODE_INT:
3009           if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3010             {
3011               /* Deal with signed, unsigned, and plain chars and
3012                  signed and unsigned ints.  */
3013               if (TYPE_NOSIGN (parm))
3014                 {
3015                   /* This case only for character types.  */
3016                   if (TYPE_NOSIGN (arg))
3017                     return EXACT_MATCH_BADNESS; /* plain char -> plain char */
3018                   else          /* signed/unsigned char -> plain char */
3019                     return INTEGER_CONVERSION_BADNESS;
3020                 }
3021               else if (TYPE_UNSIGNED (parm))
3022                 {
3023                   if (TYPE_UNSIGNED (arg))
3024                     {
3025                       /* unsigned int -> unsigned int, or 
3026                          unsigned long -> unsigned long */
3027                       if (integer_types_same_name_p (TYPE_NAME (parm), 
3028                                                      TYPE_NAME (arg)))
3029                         return EXACT_MATCH_BADNESS;
3030                       else if (integer_types_same_name_p (TYPE_NAME (arg), 
3031                                                           "int")
3032                                && integer_types_same_name_p (TYPE_NAME (parm),
3033                                                              "long"))
3034                         /* unsigned int -> unsigned long */
3035                         return INTEGER_PROMOTION_BADNESS;
3036                       else
3037                         /* unsigned long -> unsigned int */
3038                         return INTEGER_CONVERSION_BADNESS;
3039                     }
3040                   else
3041                     {
3042                       if (integer_types_same_name_p (TYPE_NAME (arg), 
3043                                                      "long")
3044                           && integer_types_same_name_p (TYPE_NAME (parm), 
3045                                                         "int"))
3046                         /* signed long -> unsigned int */
3047                         return INTEGER_CONVERSION_BADNESS;
3048                       else
3049                         /* signed int/long -> unsigned int/long */
3050                         return INTEGER_CONVERSION_BADNESS;
3051                     }
3052                 }
3053               else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3054                 {
3055                   if (integer_types_same_name_p (TYPE_NAME (parm), 
3056                                                  TYPE_NAME (arg)))
3057                     return EXACT_MATCH_BADNESS;
3058                   else if (integer_types_same_name_p (TYPE_NAME (arg), 
3059                                                       "int")
3060                            && integer_types_same_name_p (TYPE_NAME (parm), 
3061                                                          "long"))
3062                     return INTEGER_PROMOTION_BADNESS;
3063                   else
3064                     return INTEGER_CONVERSION_BADNESS;
3065                 }
3066               else
3067                 return INTEGER_CONVERSION_BADNESS;
3068             }
3069           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3070             return INTEGER_PROMOTION_BADNESS;
3071           else
3072             return INTEGER_CONVERSION_BADNESS;
3073         case TYPE_CODE_ENUM:
3074         case TYPE_CODE_FLAGS:
3075         case TYPE_CODE_CHAR:
3076         case TYPE_CODE_RANGE:
3077         case TYPE_CODE_BOOL:
3078           return INTEGER_PROMOTION_BADNESS;
3079         case TYPE_CODE_FLT:
3080           return INT_FLOAT_CONVERSION_BADNESS;
3081         case TYPE_CODE_PTR:
3082           return NS_POINTER_CONVERSION_BADNESS;
3083         default:
3084           return INCOMPATIBLE_TYPE_BADNESS;
3085         }
3086       break;
3087     case TYPE_CODE_ENUM:
3088       switch (TYPE_CODE (arg))
3089         {
3090         case TYPE_CODE_INT:
3091         case TYPE_CODE_CHAR:
3092         case TYPE_CODE_RANGE:
3093         case TYPE_CODE_BOOL:
3094         case TYPE_CODE_ENUM:
3095           return INTEGER_CONVERSION_BADNESS;
3096         case TYPE_CODE_FLT:
3097           return INT_FLOAT_CONVERSION_BADNESS;
3098         default:
3099           return INCOMPATIBLE_TYPE_BADNESS;
3100         }
3101       break;
3102     case TYPE_CODE_CHAR:
3103       switch (TYPE_CODE (arg))
3104         {
3105         case TYPE_CODE_RANGE:
3106         case TYPE_CODE_BOOL:
3107         case TYPE_CODE_ENUM:
3108           return INTEGER_CONVERSION_BADNESS;
3109         case TYPE_CODE_FLT:
3110           return INT_FLOAT_CONVERSION_BADNESS;
3111         case TYPE_CODE_INT:
3112           if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
3113             return INTEGER_CONVERSION_BADNESS;
3114           else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3115             return INTEGER_PROMOTION_BADNESS;
3116           /* >>> !! else fall through !! <<< */
3117         case TYPE_CODE_CHAR:
3118           /* Deal with signed, unsigned, and plain chars for C++ and
3119              with int cases falling through from previous case.  */
3120           if (TYPE_NOSIGN (parm))
3121             {
3122               if (TYPE_NOSIGN (arg))
3123                 return EXACT_MATCH_BADNESS;
3124               else
3125                 return INTEGER_CONVERSION_BADNESS;
3126             }
3127           else if (TYPE_UNSIGNED (parm))
3128             {
3129               if (TYPE_UNSIGNED (arg))
3130                 return EXACT_MATCH_BADNESS;
3131               else
3132                 return INTEGER_PROMOTION_BADNESS;
3133             }
3134           else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
3135             return EXACT_MATCH_BADNESS;
3136           else
3137             return INTEGER_CONVERSION_BADNESS;
3138         default:
3139           return INCOMPATIBLE_TYPE_BADNESS;
3140         }
3141       break;
3142     case TYPE_CODE_RANGE:
3143       switch (TYPE_CODE (arg))
3144         {
3145         case TYPE_CODE_INT:
3146         case TYPE_CODE_CHAR:
3147         case TYPE_CODE_RANGE:
3148         case TYPE_CODE_BOOL:
3149         case TYPE_CODE_ENUM:
3150           return INTEGER_CONVERSION_BADNESS;
3151         case TYPE_CODE_FLT:
3152           return INT_FLOAT_CONVERSION_BADNESS;
3153         default:
3154           return INCOMPATIBLE_TYPE_BADNESS;
3155         }
3156       break;
3157     case TYPE_CODE_BOOL:
3158       switch (TYPE_CODE (arg))
3159         {
3160           /* n3290 draft, section 4.12.1 (conv.bool):
3161
3162              "A prvalue of arithmetic, unscoped enumeration, pointer, or
3163              pointer to member type can be converted to a prvalue of type
3164              bool.  A zero value, null pointer value, or null member pointer
3165              value is converted to false; any other value is converted to
3166              true.  A prvalue of type std::nullptr_t can be converted to a
3167              prvalue of type bool; the resulting value is false."  */
3168         case TYPE_CODE_INT:
3169         case TYPE_CODE_CHAR:
3170         case TYPE_CODE_ENUM:
3171         case TYPE_CODE_FLT:
3172         case TYPE_CODE_MEMBERPTR:
3173         case TYPE_CODE_PTR:
3174           return BOOL_CONVERSION_BADNESS;
3175         case TYPE_CODE_RANGE:
3176           return INCOMPATIBLE_TYPE_BADNESS;
3177         case TYPE_CODE_BOOL:
3178           return EXACT_MATCH_BADNESS;
3179         default:
3180           return INCOMPATIBLE_TYPE_BADNESS;
3181         }
3182       break;
3183     case TYPE_CODE_FLT:
3184       switch (TYPE_CODE (arg))
3185         {
3186         case TYPE_CODE_FLT:
3187           if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
3188             return FLOAT_PROMOTION_BADNESS;
3189           else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
3190             return EXACT_MATCH_BADNESS;
3191           else
3192             return FLOAT_CONVERSION_BADNESS;
3193         case TYPE_CODE_INT:
3194         case TYPE_CODE_BOOL:
3195         case TYPE_CODE_ENUM:
3196         case TYPE_CODE_RANGE:
3197         case TYPE_CODE_CHAR:
3198           return INT_FLOAT_CONVERSION_BADNESS;
3199         default:
3200           return INCOMPATIBLE_TYPE_BADNESS;
3201         }
3202       break;
3203     case TYPE_CODE_COMPLEX:
3204       switch (TYPE_CODE (arg))
3205         {               /* Strictly not needed for C++, but...  */
3206         case TYPE_CODE_FLT:
3207           return FLOAT_PROMOTION_BADNESS;
3208         case TYPE_CODE_COMPLEX:
3209           return EXACT_MATCH_BADNESS;
3210         default:
3211           return INCOMPATIBLE_TYPE_BADNESS;
3212         }
3213       break;
3214     case TYPE_CODE_STRUCT:
3215       /* currently same as TYPE_CODE_CLASS.  */
3216       switch (TYPE_CODE (arg))
3217         {
3218         case TYPE_CODE_STRUCT:
3219           /* Check for derivation */
3220           rank.subrank = distance_to_ancestor (parm, arg, 0);
3221           if (rank.subrank >= 0)
3222             return sum_ranks (BASE_CONVERSION_BADNESS, rank);
3223           /* else fall through */
3224         default:
3225           return INCOMPATIBLE_TYPE_BADNESS;
3226         }
3227       break;
3228     case TYPE_CODE_UNION:
3229       switch (TYPE_CODE (arg))
3230         {
3231         case TYPE_CODE_UNION:
3232         default:
3233           return INCOMPATIBLE_TYPE_BADNESS;
3234         }
3235       break;
3236     case TYPE_CODE_MEMBERPTR:
3237       switch (TYPE_CODE (arg))
3238         {
3239         default:
3240           return INCOMPATIBLE_TYPE_BADNESS;
3241         }
3242       break;
3243     case TYPE_CODE_METHOD:
3244       switch (TYPE_CODE (arg))
3245         {
3246
3247         default:
3248           return INCOMPATIBLE_TYPE_BADNESS;
3249         }
3250       break;
3251     case TYPE_CODE_REF:
3252       switch (TYPE_CODE (arg))
3253         {
3254
3255         default:
3256           return INCOMPATIBLE_TYPE_BADNESS;
3257         }
3258
3259       break;
3260     case TYPE_CODE_SET:
3261       switch (TYPE_CODE (arg))
3262         {
3263           /* Not in C++ */
3264         case TYPE_CODE_SET:
3265           return rank_one_type (TYPE_FIELD_TYPE (parm, 0), 
3266                                 TYPE_FIELD_TYPE (arg, 0), NULL);
3267         default:
3268           return INCOMPATIBLE_TYPE_BADNESS;
3269         }
3270       break;
3271     case TYPE_CODE_VOID:
3272     default:
3273       return INCOMPATIBLE_TYPE_BADNESS;
3274     }                           /* switch (TYPE_CODE (arg)) */
3275 }
3276
3277 /* End of functions for overload resolution.  */
3278 \f
3279 /* Routines to pretty-print types.  */
3280
3281 static void
3282 print_bit_vector (B_TYPE *bits, int nbits)
3283 {
3284   int bitno;
3285
3286   for (bitno = 0; bitno < nbits; bitno++)
3287     {
3288       if ((bitno % 8) == 0)
3289         {
3290           puts_filtered (" ");
3291         }
3292       if (B_TST (bits, bitno))
3293         printf_filtered (("1"));
3294       else
3295         printf_filtered (("0"));
3296     }
3297 }
3298
3299 /* Note the first arg should be the "this" pointer, we may not want to
3300    include it since we may get into a infinitely recursive
3301    situation.  */
3302
3303 static void
3304 print_arg_types (struct field *args, int nargs, int spaces)
3305 {
3306   if (args != NULL)
3307     {
3308       int i;
3309
3310       for (i = 0; i < nargs; i++)
3311         recursive_dump_type (args[i].type, spaces + 2);
3312     }
3313 }
3314
3315 int
3316 field_is_static (struct field *f)
3317 {
3318   /* "static" fields are the fields whose location is not relative
3319      to the address of the enclosing struct.  It would be nice to
3320      have a dedicated flag that would be set for static fields when
3321      the type is being created.  But in practice, checking the field
3322      loc_kind should give us an accurate answer.  */
3323   return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME
3324           || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR);
3325 }
3326
3327 static void
3328 dump_fn_fieldlists (struct type *type, int spaces)
3329 {
3330   int method_idx;
3331   int overload_idx;
3332   struct fn_field *f;
3333
3334   printfi_filtered (spaces, "fn_fieldlists ");
3335   gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
3336   printf_filtered ("\n");
3337   for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
3338     {
3339       f = TYPE_FN_FIELDLIST1 (type, method_idx);
3340       printfi_filtered (spaces + 2, "[%d] name '%s' (",
3341                         method_idx,
3342                         TYPE_FN_FIELDLIST_NAME (type, method_idx));
3343       gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
3344                               gdb_stdout);
3345       printf_filtered (_(") length %d\n"),
3346                        TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
3347       for (overload_idx = 0;
3348            overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
3349            overload_idx++)
3350         {
3351           printfi_filtered (spaces + 4, "[%d] physname '%s' (",
3352                             overload_idx,
3353                             TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
3354           gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
3355                                   gdb_stdout);
3356           printf_filtered (")\n");
3357           printfi_filtered (spaces + 8, "type ");
3358           gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), 
3359                                   gdb_stdout);
3360           printf_filtered ("\n");
3361
3362           recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
3363                                spaces + 8 + 2);
3364
3365           printfi_filtered (spaces + 8, "args ");
3366           gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), 
3367                                   gdb_stdout);
3368           printf_filtered ("\n");
3369
3370           print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
3371                            TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, 
3372                                                              overload_idx)),
3373                            spaces);
3374           printfi_filtered (spaces + 8, "fcontext ");
3375           gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
3376                                   gdb_stdout);
3377           printf_filtered ("\n");
3378
3379           printfi_filtered (spaces + 8, "is_const %d\n",
3380                             TYPE_FN_FIELD_CONST (f, overload_idx));
3381           printfi_filtered (spaces + 8, "is_volatile %d\n",
3382                             TYPE_FN_FIELD_VOLATILE (f, overload_idx));
3383           printfi_filtered (spaces + 8, "is_private %d\n",
3384                             TYPE_FN_FIELD_PRIVATE (f, overload_idx));
3385           printfi_filtered (spaces + 8, "is_protected %d\n",
3386                             TYPE_FN_FIELD_PROTECTED (f, overload_idx));
3387           printfi_filtered (spaces + 8, "is_stub %d\n",
3388                             TYPE_FN_FIELD_STUB (f, overload_idx));
3389           printfi_filtered (spaces + 8, "voffset %u\n",
3390                             TYPE_FN_FIELD_VOFFSET (f, overload_idx));
3391         }
3392     }
3393 }
3394
3395 static void
3396 print_cplus_stuff (struct type *type, int spaces)
3397 {
3398   printfi_filtered (spaces, "n_baseclasses %d\n",
3399                     TYPE_N_BASECLASSES (type));
3400   printfi_filtered (spaces, "nfn_fields %d\n",
3401                     TYPE_NFN_FIELDS (type));
3402   if (TYPE_N_BASECLASSES (type) > 0)
3403     {
3404       printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
3405                         TYPE_N_BASECLASSES (type));
3406       gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), 
3407                               gdb_stdout);
3408       printf_filtered (")");
3409
3410       print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
3411                         TYPE_N_BASECLASSES (type));
3412       puts_filtered ("\n");
3413     }
3414   if (TYPE_NFIELDS (type) > 0)
3415     {
3416       if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
3417         {
3418           printfi_filtered (spaces, 
3419                             "private_field_bits (%d bits at *",
3420                             TYPE_NFIELDS (type));
3421           gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), 
3422                                   gdb_stdout);
3423           printf_filtered (")");
3424           print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
3425                             TYPE_NFIELDS (type));
3426           puts_filtered ("\n");
3427         }
3428       if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
3429         {
3430           printfi_filtered (spaces, 
3431                             "protected_field_bits (%d bits at *",
3432                             TYPE_NFIELDS (type));
3433           gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), 
3434                                   gdb_stdout);
3435           printf_filtered (")");
3436           print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
3437                             TYPE_NFIELDS (type));
3438           puts_filtered ("\n");
3439         }
3440     }
3441   if (TYPE_NFN_FIELDS (type) > 0)
3442     {
3443       dump_fn_fieldlists (type, spaces);
3444     }
3445 }
3446
3447 /* Print the contents of the TYPE's type_specific union, assuming that
3448    its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF.  */
3449
3450 static void
3451 print_gnat_stuff (struct type *type, int spaces)
3452 {
3453   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
3454
3455   recursive_dump_type (descriptive_type, spaces + 2);
3456 }
3457
3458 static struct obstack dont_print_type_obstack;
3459
3460 void
3461 recursive_dump_type (struct type *type, int spaces)
3462 {
3463   int idx;
3464
3465   if (spaces == 0)
3466     obstack_begin (&dont_print_type_obstack, 0);
3467
3468   if (TYPE_NFIELDS (type) > 0
3469       || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
3470     {
3471       struct type **first_dont_print
3472         = (struct type **) obstack_base (&dont_print_type_obstack);
3473
3474       int i = (struct type **) 
3475         obstack_next_free (&dont_print_type_obstack) - first_dont_print;
3476
3477       while (--i >= 0)
3478         {
3479           if (type == first_dont_print[i])
3480             {
3481               printfi_filtered (spaces, "type node ");
3482               gdb_print_host_address (type, gdb_stdout);
3483               printf_filtered (_(" <same as already seen type>\n"));
3484               return;
3485             }
3486         }
3487
3488       obstack_ptr_grow (&dont_print_type_obstack, type);
3489     }
3490
3491   printfi_filtered (spaces, "type node ");
3492   gdb_print_host_address (type, gdb_stdout);
3493   printf_filtered ("\n");
3494   printfi_filtered (spaces, "name '%s' (",
3495                     TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
3496   gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
3497   printf_filtered (")\n");
3498   printfi_filtered (spaces, "tagname '%s' (",
3499                     TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
3500   gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
3501   printf_filtered (")\n");
3502   printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
3503   switch (TYPE_CODE (type))
3504     {
3505     case TYPE_CODE_UNDEF:
3506       printf_filtered ("(TYPE_CODE_UNDEF)");
3507       break;
3508     case TYPE_CODE_PTR:
3509       printf_filtered ("(TYPE_CODE_PTR)");
3510       break;
3511     case TYPE_CODE_ARRAY:
3512       printf_filtered ("(TYPE_CODE_ARRAY)");
3513       break;
3514     case TYPE_CODE_STRUCT:
3515       printf_filtered ("(TYPE_CODE_STRUCT)");
3516       break;
3517     case TYPE_CODE_UNION:
3518       printf_filtered ("(TYPE_CODE_UNION)");
3519       break;
3520     case TYPE_CODE_ENUM:
3521       printf_filtered ("(TYPE_CODE_ENUM)");
3522       break;
3523     case TYPE_CODE_FLAGS:
3524       printf_filtered ("(TYPE_CODE_FLAGS)");
3525       break;
3526     case TYPE_CODE_FUNC:
3527       printf_filtered ("(TYPE_CODE_FUNC)");
3528       break;
3529     case TYPE_CODE_INT:
3530       printf_filtered ("(TYPE_CODE_INT)");
3531       break;
3532     case TYPE_CODE_FLT:
3533       printf_filtered ("(TYPE_CODE_FLT)");
3534       break;
3535     case TYPE_CODE_VOID:
3536       printf_filtered ("(TYPE_CODE_VOID)");
3537       break;
3538     case TYPE_CODE_SET:
3539       printf_filtered ("(TYPE_CODE_SET)");
3540       break;
3541     case TYPE_CODE_RANGE:
3542       printf_filtered ("(TYPE_CODE_RANGE)");
3543       break;
3544     case TYPE_CODE_STRING:
3545       printf_filtered ("(TYPE_CODE_STRING)");
3546       break;
3547     case TYPE_CODE_ERROR:
3548       printf_filtered ("(TYPE_CODE_ERROR)");
3549       break;
3550     case TYPE_CODE_MEMBERPTR:
3551       printf_filtered ("(TYPE_CODE_MEMBERPTR)");
3552       break;
3553     case TYPE_CODE_METHODPTR:
3554       printf_filtered ("(TYPE_CODE_METHODPTR)");
3555       break;
3556     case TYPE_CODE_METHOD:
3557       printf_filtered ("(TYPE_CODE_METHOD)");
3558       break;
3559     case TYPE_CODE_REF:
3560       printf_filtered ("(TYPE_CODE_REF)");
3561       break;
3562     case TYPE_CODE_CHAR:
3563       printf_filtered ("(TYPE_CODE_CHAR)");
3564       break;
3565     case TYPE_CODE_BOOL:
3566       printf_filtered ("(TYPE_CODE_BOOL)");
3567       break;
3568     case TYPE_CODE_COMPLEX:
3569       printf_filtered ("(TYPE_CODE_COMPLEX)");
3570       break;
3571     case TYPE_CODE_TYPEDEF:
3572       printf_filtered ("(TYPE_CODE_TYPEDEF)");
3573       break;
3574     case TYPE_CODE_NAMESPACE:
3575       printf_filtered ("(TYPE_CODE_NAMESPACE)");
3576       break;
3577     default:
3578       printf_filtered ("(UNKNOWN TYPE CODE)");
3579       break;
3580     }
3581   puts_filtered ("\n");
3582   printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
3583   if (TYPE_OBJFILE_OWNED (type))
3584     {
3585       printfi_filtered (spaces, "objfile ");
3586       gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout);
3587     }
3588   else
3589     {
3590       printfi_filtered (spaces, "gdbarch ");
3591       gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout);
3592     }
3593   printf_filtered ("\n");
3594   printfi_filtered (spaces, "target_type ");
3595   gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
3596   printf_filtered ("\n");
3597   if (TYPE_TARGET_TYPE (type) != NULL)
3598     {
3599       recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
3600     }
3601   printfi_filtered (spaces, "pointer_type ");
3602   gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
3603   printf_filtered ("\n");
3604   printfi_filtered (spaces, "reference_type ");
3605   gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
3606   printf_filtered ("\n");
3607   printfi_filtered (spaces, "type_chain ");
3608   gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3609   printf_filtered ("\n");
3610   printfi_filtered (spaces, "instance_flags 0x%x", 
3611                     TYPE_INSTANCE_FLAGS (type));
3612   if (TYPE_CONST (type))
3613     {
3614       puts_filtered (" TYPE_FLAG_CONST");
3615     }
3616   if (TYPE_VOLATILE (type))
3617     {
3618       puts_filtered (" TYPE_FLAG_VOLATILE");
3619     }
3620   if (TYPE_CODE_SPACE (type))
3621     {
3622       puts_filtered (" TYPE_FLAG_CODE_SPACE");
3623     }
3624   if (TYPE_DATA_SPACE (type))
3625     {
3626       puts_filtered (" TYPE_FLAG_DATA_SPACE");
3627     }
3628   if (TYPE_ADDRESS_CLASS_1 (type))
3629     {
3630       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3631     }
3632   if (TYPE_ADDRESS_CLASS_2 (type))
3633     {
3634       puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3635     }
3636   if (TYPE_RESTRICT (type))
3637     {
3638       puts_filtered (" TYPE_FLAG_RESTRICT");
3639     }
3640   puts_filtered ("\n");
3641
3642   printfi_filtered (spaces, "flags");
3643   if (TYPE_UNSIGNED (type))
3644     {
3645       puts_filtered (" TYPE_FLAG_UNSIGNED");
3646     }
3647   if (TYPE_NOSIGN (type))
3648     {
3649       puts_filtered (" TYPE_FLAG_NOSIGN");
3650     }
3651   if (TYPE_STUB (type))
3652     {
3653       puts_filtered (" TYPE_FLAG_STUB");
3654     }
3655   if (TYPE_TARGET_STUB (type))
3656     {
3657       puts_filtered (" TYPE_FLAG_TARGET_STUB");
3658     }
3659   if (TYPE_STATIC (type))
3660     {
3661       puts_filtered (" TYPE_FLAG_STATIC");
3662     }
3663   if (TYPE_PROTOTYPED (type))
3664     {
3665       puts_filtered (" TYPE_FLAG_PROTOTYPED");
3666     }
3667   if (TYPE_INCOMPLETE (type))
3668     {
3669       puts_filtered (" TYPE_FLAG_INCOMPLETE");
3670     }
3671   if (TYPE_VARARGS (type))
3672     {
3673       puts_filtered (" TYPE_FLAG_VARARGS");
3674     }
3675   /* This is used for things like AltiVec registers on ppc.  Gcc emits
3676      an attribute for the array type, which tells whether or not we
3677      have a vector, instead of a regular array.  */
3678   if (TYPE_VECTOR (type))
3679     {
3680       puts_filtered (" TYPE_FLAG_VECTOR");
3681     }
3682   if (TYPE_FIXED_INSTANCE (type))
3683     {
3684       puts_filtered (" TYPE_FIXED_INSTANCE");
3685     }
3686   if (TYPE_STUB_SUPPORTED (type))
3687     {
3688       puts_filtered (" TYPE_STUB_SUPPORTED");
3689     }
3690   if (TYPE_NOTTEXT (type))
3691     {
3692       puts_filtered (" TYPE_NOTTEXT");
3693     }
3694   puts_filtered ("\n");
3695   printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3696   gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3697   puts_filtered ("\n");
3698   for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3699     {
3700       if (TYPE_CODE (type) == TYPE_CODE_ENUM)
3701         printfi_filtered (spaces + 2,
3702                           "[%d] enumval %s type ",
3703                           idx, plongest (TYPE_FIELD_ENUMVAL (type, idx)));
3704       else
3705         printfi_filtered (spaces + 2,
3706                           "[%d] bitpos %d bitsize %d type ",
3707                           idx, TYPE_FIELD_BITPOS (type, idx),
3708                           TYPE_FIELD_BITSIZE (type, idx));
3709       gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3710       printf_filtered (" name '%s' (",
3711                        TYPE_FIELD_NAME (type, idx) != NULL
3712                        ? TYPE_FIELD_NAME (type, idx)
3713                        : "<NULL>");
3714       gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3715       printf_filtered (")\n");
3716       if (TYPE_FIELD_TYPE (type, idx) != NULL)
3717         {
3718           recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3719         }
3720     }
3721   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3722     {
3723       printfi_filtered (spaces, "low %s%s  high %s%s\n",
3724                         plongest (TYPE_LOW_BOUND (type)), 
3725                         TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "",
3726                         plongest (TYPE_HIGH_BOUND (type)),
3727                         TYPE_HIGH_BOUND_UNDEFINED (type) 
3728                         ? " (undefined)" : "");
3729     }
3730   printfi_filtered (spaces, "vptr_basetype ");
3731   gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3732   puts_filtered ("\n");
3733   if (TYPE_VPTR_BASETYPE (type) != NULL)
3734     {
3735       recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3736     }
3737   printfi_filtered (spaces, "vptr_fieldno %d\n", 
3738                     TYPE_VPTR_FIELDNO (type));
3739
3740   switch (TYPE_SPECIFIC_FIELD (type))
3741     {
3742       case TYPE_SPECIFIC_CPLUS_STUFF:
3743         printfi_filtered (spaces, "cplus_stuff ");
3744         gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), 
3745                                 gdb_stdout);
3746         puts_filtered ("\n");
3747         print_cplus_stuff (type, spaces);
3748         break;
3749
3750       case TYPE_SPECIFIC_GNAT_STUFF:
3751         printfi_filtered (spaces, "gnat_stuff ");
3752         gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout);
3753         puts_filtered ("\n");
3754         print_gnat_stuff (type, spaces);
3755         break;
3756
3757       case TYPE_SPECIFIC_FLOATFORMAT:
3758         printfi_filtered (spaces, "floatformat ");
3759         if (TYPE_FLOATFORMAT (type) == NULL)
3760           puts_filtered ("(null)");
3761         else
3762           {
3763             puts_filtered ("{ ");
3764             if (TYPE_FLOATFORMAT (type)[0] == NULL
3765                 || TYPE_FLOATFORMAT (type)[0]->name == NULL)
3766               puts_filtered ("(null)");
3767             else
3768               puts_filtered (TYPE_FLOATFORMAT (type)[0]->name);
3769
3770             puts_filtered (", ");
3771             if (TYPE_FLOATFORMAT (type)[1] == NULL
3772                 || TYPE_FLOATFORMAT (type)[1]->name == NULL)
3773               puts_filtered ("(null)");
3774             else
3775               puts_filtered (TYPE_FLOATFORMAT (type)[1]->name);
3776
3777             puts_filtered (" }");
3778           }
3779         puts_filtered ("\n");
3780         break;
3781
3782       case TYPE_SPECIFIC_FUNC:
3783         printfi_filtered (spaces, "calling_convention %d\n",
3784                           TYPE_CALLING_CONVENTION (type));
3785         /* tail_call_list is not printed.  */
3786         break;
3787     }
3788
3789   if (spaces == 0)
3790     obstack_free (&dont_print_type_obstack, NULL);
3791 }
3792 \f
3793 /* Trivial helpers for the libiberty hash table, for mapping one
3794    type to another.  */
3795
3796 struct type_pair
3797 {
3798   struct type *old, *new;
3799 };
3800
3801 static hashval_t
3802 type_pair_hash (const void *item)
3803 {
3804   const struct type_pair *pair = item;
3805
3806   return htab_hash_pointer (pair->old);
3807 }
3808
3809 static int
3810 type_pair_eq (const void *item_lhs, const void *item_rhs)
3811 {
3812   const struct type_pair *lhs = item_lhs, *rhs = item_rhs;
3813
3814   return lhs->old == rhs->old;
3815 }
3816
3817 /* Allocate the hash table used by copy_type_recursive to walk
3818    types without duplicates.  We use OBJFILE's obstack, because
3819    OBJFILE is about to be deleted.  */
3820
3821 htab_t
3822 create_copied_types_hash (struct objfile *objfile)
3823 {
3824   return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
3825                                NULL, &objfile->objfile_obstack,
3826                                hashtab_obstack_allocate,
3827                                dummy_obstack_deallocate);
3828 }
3829
3830 /* Recursively copy (deep copy) TYPE, if it is associated with
3831    OBJFILE.  Return a new type allocated using malloc, a saved type if
3832    we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
3833    not associated with OBJFILE.  */
3834
3835 struct type *
3836 copy_type_recursive (struct objfile *objfile, 
3837                      struct type *type,
3838                      htab_t copied_types)
3839 {
3840   struct type_pair *stored, pair;
3841   void **slot;
3842   struct type *new_type;
3843
3844   if (! TYPE_OBJFILE_OWNED (type))
3845     return type;
3846
3847   /* This type shouldn't be pointing to any types in other objfiles;
3848      if it did, the type might disappear unexpectedly.  */
3849   gdb_assert (TYPE_OBJFILE (type) == objfile);
3850
3851   pair.old = type;
3852   slot = htab_find_slot (copied_types, &pair, INSERT);
3853   if (*slot != NULL)
3854     return ((struct type_pair *) *slot)->new;
3855
3856   new_type = alloc_type_arch (get_type_arch (type));
3857
3858   /* We must add the new type to the hash table immediately, in case
3859      we encounter this type again during a recursive call below.  */
3860   stored
3861     = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
3862   stored->old = type;
3863   stored->new = new_type;
3864   *slot = stored;
3865
3866   /* Copy the common fields of types.  For the main type, we simply
3867      copy the entire thing and then update specific fields as needed.  */
3868   *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
3869   TYPE_OBJFILE_OWNED (new_type) = 0;
3870   TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
3871
3872   if (TYPE_NAME (type))
3873     TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
3874   if (TYPE_TAG_NAME (type))
3875     TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type));
3876
3877   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3878   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3879
3880   /* Copy the fields.  */
3881   if (TYPE_NFIELDS (type))
3882     {
3883       int i, nfields;
3884
3885       nfields = TYPE_NFIELDS (type);
3886       TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields);
3887       for (i = 0; i < nfields; i++)
3888         {
3889           TYPE_FIELD_ARTIFICIAL (new_type, i) = 
3890             TYPE_FIELD_ARTIFICIAL (type, i);
3891           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
3892           if (TYPE_FIELD_TYPE (type, i))
3893             TYPE_FIELD_TYPE (new_type, i)
3894               = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
3895                                      copied_types);
3896           if (TYPE_FIELD_NAME (type, i))
3897             TYPE_FIELD_NAME (new_type, i) = 
3898               xstrdup (TYPE_FIELD_NAME (type, i));
3899           switch (TYPE_FIELD_LOC_KIND (type, i))
3900             {
3901             case FIELD_LOC_KIND_BITPOS:
3902               SET_FIELD_BITPOS (TYPE_FIELD (new_type, i),
3903                                 TYPE_FIELD_BITPOS (type, i));
3904               break;
3905             case FIELD_LOC_KIND_ENUMVAL:
3906               SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i),
3907                                  TYPE_FIELD_ENUMVAL (type, i));
3908               break;
3909             case FIELD_LOC_KIND_PHYSADDR:
3910               SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i),
3911                                   TYPE_FIELD_STATIC_PHYSADDR (type, i));
3912               break;
3913             case FIELD_LOC_KIND_PHYSNAME:
3914               SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i),
3915                                   xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type,
3916                                                                        i)));
3917               break;
3918             default:
3919               internal_error (__FILE__, __LINE__,
3920                               _("Unexpected type field location kind: %d"),
3921                               TYPE_FIELD_LOC_KIND (type, i));
3922             }
3923         }
3924     }
3925
3926   /* For range types, copy the bounds information.  */
3927   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
3928     {
3929       TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
3930       *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
3931     }
3932
3933   /* Copy pointers to other types.  */
3934   if (TYPE_TARGET_TYPE (type))
3935     TYPE_TARGET_TYPE (new_type) = 
3936       copy_type_recursive (objfile, 
3937                            TYPE_TARGET_TYPE (type),
3938                            copied_types);
3939   if (TYPE_VPTR_BASETYPE (type))
3940     TYPE_VPTR_BASETYPE (new_type) = 
3941       copy_type_recursive (objfile,
3942                            TYPE_VPTR_BASETYPE (type),
3943                            copied_types);
3944   /* Maybe copy the type_specific bits.
3945
3946      NOTE drow/2005-12-09: We do not copy the C++-specific bits like
3947      base classes and methods.  There's no fundamental reason why we
3948      can't, but at the moment it is not needed.  */
3949
3950   if (TYPE_CODE (type) == TYPE_CODE_FLT)
3951     TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
3952   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3953            || TYPE_CODE (type) == TYPE_CODE_UNION
3954            || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
3955     INIT_CPLUS_SPECIFIC (new_type);
3956
3957   return new_type;
3958 }
3959
3960 /* Make a copy of the given TYPE, except that the pointer & reference
3961    types are not preserved.
3962    
3963    This function assumes that the given type has an associated objfile.
3964    This objfile is used to allocate the new type.  */
3965
3966 struct type *
3967 copy_type (const struct type *type)
3968 {
3969   struct type *new_type;
3970
3971   gdb_assert (TYPE_OBJFILE_OWNED (type));
3972
3973   new_type = alloc_type_copy (type);
3974   TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
3975   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
3976   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
3977           sizeof (struct main_type));
3978
3979   return new_type;
3980 }
3981 \f
3982 /* Helper functions to initialize architecture-specific types.  */
3983
3984 /* Allocate a type structure associated with GDBARCH and set its
3985    CODE, LENGTH, and NAME fields.  */
3986
3987 struct type *
3988 arch_type (struct gdbarch *gdbarch,
3989            enum type_code code, int length, char *name)
3990 {
3991   struct type *type;
3992
3993   type = alloc_type_arch (gdbarch);
3994   TYPE_CODE (type) = code;
3995   TYPE_LENGTH (type) = length;
3996
3997   if (name)
3998     TYPE_NAME (type) = xstrdup (name);
3999
4000   return type;
4001 }
4002
4003 /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
4004    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4005    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4006
4007 struct type *
4008 arch_integer_type (struct gdbarch *gdbarch,
4009                    int bit, int unsigned_p, char *name)
4010 {
4011   struct type *t;
4012
4013   t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name);
4014   if (unsigned_p)
4015     TYPE_UNSIGNED (t) = 1;
4016   if (name && strcmp (name, "char") == 0)
4017     TYPE_NOSIGN (t) = 1;
4018
4019   return t;
4020 }
4021
4022 /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
4023    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4024    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4025
4026 struct type *
4027 arch_character_type (struct gdbarch *gdbarch,
4028                      int bit, int unsigned_p, char *name)
4029 {
4030   struct type *t;
4031
4032   t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name);
4033   if (unsigned_p)
4034     TYPE_UNSIGNED (t) = 1;
4035
4036   return t;
4037 }
4038
4039 /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
4040    BIT is the type size in bits.  If UNSIGNED_P is non-zero, set
4041    the type's TYPE_UNSIGNED flag.  NAME is the type name.  */
4042
4043 struct type *
4044 arch_boolean_type (struct gdbarch *gdbarch,
4045                    int bit, int unsigned_p, char *name)
4046 {
4047   struct type *t;
4048
4049   t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name);
4050   if (unsigned_p)
4051     TYPE_UNSIGNED (t) = 1;
4052
4053   return t;
4054 }
4055
4056 /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
4057    BIT is the type size in bits; if BIT equals -1, the size is
4058    determined by the floatformat.  NAME is the type name.  Set the
4059    TYPE_FLOATFORMAT from FLOATFORMATS.  */
4060
4061 struct type *
4062 arch_float_type (struct gdbarch *gdbarch,
4063                  int bit, char *name, const struct floatformat **floatformats)
4064 {
4065   struct type *t;
4066
4067   if (bit == -1)
4068     {
4069       gdb_assert (floatformats != NULL);
4070       gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL);
4071       bit = floatformats[0]->totalsize;
4072     }
4073   gdb_assert (bit >= 0);
4074
4075   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
4076   TYPE_FLOATFORMAT (t) = floatformats;
4077   return t;
4078 }
4079
4080 /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH.
4081    NAME is the type name.  TARGET_TYPE is the component float type.  */
4082
4083 struct type *
4084 arch_complex_type (struct gdbarch *gdbarch,
4085                    char *name, struct type *target_type)
4086 {
4087   struct type *t;
4088
4089   t = arch_type (gdbarch, TYPE_CODE_COMPLEX,
4090                  2 * TYPE_LENGTH (target_type), name);
4091   TYPE_TARGET_TYPE (t) = target_type;
4092   return t;
4093 }
4094
4095 /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
4096    NAME is the type name.  LENGTH is the size of the flag word in bytes.  */
4097
4098 struct type *
4099 arch_flags_type (struct gdbarch *gdbarch, char *name, int length)
4100 {
4101   int nfields = length * TARGET_CHAR_BIT;
4102   struct type *type;
4103
4104   type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name);
4105   TYPE_UNSIGNED (type) = 1;
4106   TYPE_NFIELDS (type) = nfields;
4107   TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field));
4108
4109   return type;
4110 }
4111
4112 /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
4113    position BITPOS is called NAME.  */
4114
4115 void
4116 append_flags_type_flag (struct type *type, int bitpos, char *name)
4117 {
4118   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS);
4119   gdb_assert (bitpos < TYPE_NFIELDS (type));
4120   gdb_assert (bitpos >= 0);
4121
4122   if (name)
4123     {
4124       TYPE_FIELD_NAME (type, bitpos) = xstrdup (name);
4125       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), bitpos);
4126     }
4127   else
4128     {
4129       /* Don't show this field to the user.  */
4130       SET_FIELD_BITPOS (TYPE_FIELD (type, bitpos), -1);
4131     }
4132 }
4133
4134 /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
4135    specified by CODE) associated with GDBARCH.  NAME is the type name.  */
4136
4137 struct type *
4138 arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code)
4139 {
4140   struct type *t;
4141
4142   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
4143   t = arch_type (gdbarch, code, 0, NULL);
4144   TYPE_TAG_NAME (t) = name;
4145   INIT_CPLUS_SPECIFIC (t);
4146   return t;
4147 }
4148
4149 /* Add new field with name NAME and type FIELD to composite type T.
4150    Do not set the field's position or adjust the type's length;
4151    the caller should do so.  Return the new field.  */
4152
4153 struct field *
4154 append_composite_type_field_raw (struct type *t, char *name,
4155                                  struct type *field)
4156 {
4157   struct field *f;
4158
4159   TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
4160   TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
4161                               sizeof (struct field) * TYPE_NFIELDS (t));
4162   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
4163   memset (f, 0, sizeof f[0]);
4164   FIELD_TYPE (f[0]) = field;
4165   FIELD_NAME (f[0]) = name;
4166   return f;
4167 }
4168
4169 /* Add new field with name NAME and type FIELD to composite type T.
4170    ALIGNMENT (if non-zero) specifies the minimum field alignment.  */
4171
4172 void
4173 append_composite_type_field_aligned (struct type *t, char *name,
4174                                      struct type *field, int alignment)
4175 {
4176   struct field *f = append_composite_type_field_raw (t, name, field);
4177
4178   if (TYPE_CODE (t) == TYPE_CODE_UNION)
4179     {
4180       if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
4181         TYPE_LENGTH (t) = TYPE_LENGTH (field);
4182     }
4183   else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
4184     {
4185       TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
4186       if (TYPE_NFIELDS (t) > 1)
4187         {
4188           SET_FIELD_BITPOS (f[0],
4189                             (FIELD_BITPOS (f[-1])
4190                              + (TYPE_LENGTH (FIELD_TYPE (f[-1]))
4191                                 * TARGET_CHAR_BIT)));
4192
4193           if (alignment)
4194             {
4195               int left;
4196
4197               alignment *= TARGET_CHAR_BIT;
4198               left = FIELD_BITPOS (f[0]) % alignment;
4199
4200               if (left)
4201                 {
4202                   SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left));
4203                   TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
4204                 }
4205             }
4206         }
4207     }
4208 }
4209
4210 /* Add new field with name NAME and type FIELD to composite type T.  */
4211
4212 void
4213 append_composite_type_field (struct type *t, char *name,
4214                              struct type *field)
4215 {
4216   append_composite_type_field_aligned (t, name, field, 0);
4217 }
4218
4219 static struct gdbarch_data *gdbtypes_data;
4220
4221 const struct builtin_type *
4222 builtin_type (struct gdbarch *gdbarch)
4223 {
4224   return gdbarch_data (gdbarch, gdbtypes_data);
4225 }
4226
4227 static void *
4228 gdbtypes_post_init (struct gdbarch *gdbarch)
4229 {
4230   struct builtin_type *builtin_type
4231     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
4232
4233   /* Basic types.  */
4234   builtin_type->builtin_void
4235     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
4236   builtin_type->builtin_char
4237     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4238                          !gdbarch_char_signed (gdbarch), "char");
4239   builtin_type->builtin_signed_char
4240     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4241                          0, "signed char");
4242   builtin_type->builtin_unsigned_char
4243     = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
4244                          1, "unsigned char");
4245   builtin_type->builtin_short
4246     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4247                          0, "short");
4248   builtin_type->builtin_unsigned_short
4249     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
4250                          1, "unsigned short");
4251   builtin_type->builtin_int
4252     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4253                          0, "int");
4254   builtin_type->builtin_unsigned_int
4255     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
4256                          1, "unsigned int");
4257   builtin_type->builtin_long
4258     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4259                          0, "long");
4260   builtin_type->builtin_unsigned_long
4261     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
4262                          1, "unsigned long");
4263   builtin_type->builtin_long_long
4264     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4265                          0, "long long");
4266   builtin_type->builtin_unsigned_long_long
4267     = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
4268                          1, "unsigned long long");
4269   builtin_type->builtin_float
4270     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
4271                        "float", gdbarch_float_format (gdbarch));
4272   builtin_type->builtin_double
4273     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
4274                        "double", gdbarch_double_format (gdbarch));
4275   builtin_type->builtin_long_double
4276     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
4277                        "long double", gdbarch_long_double_format (gdbarch));
4278   builtin_type->builtin_complex
4279     = arch_complex_type (gdbarch, "complex",
4280                          builtin_type->builtin_float);
4281   builtin_type->builtin_double_complex
4282     = arch_complex_type (gdbarch, "double complex",
4283                          builtin_type->builtin_double);
4284   builtin_type->builtin_string
4285     = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string");
4286   builtin_type->builtin_bool
4287     = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool");
4288
4289   /* The following three are about decimal floating point types, which
4290      are 32-bits, 64-bits and 128-bits respectively.  */
4291   builtin_type->builtin_decfloat
4292     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32");
4293   builtin_type->builtin_decdouble
4294     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64");
4295   builtin_type->builtin_declong
4296     = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128");
4297
4298   /* "True" character types.  */
4299   builtin_type->builtin_true_char
4300     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
4301   builtin_type->builtin_true_unsigned_char
4302     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
4303
4304   /* Fixed-size integer types.  */
4305   builtin_type->builtin_int0
4306     = arch_integer_type (gdbarch, 0, 0, "int0_t");
4307   builtin_type->builtin_int8
4308     = arch_integer_type (gdbarch, 8, 0, "int8_t");
4309   builtin_type->builtin_uint8
4310     = arch_integer_type (gdbarch, 8, 1, "uint8_t");
4311   builtin_type->builtin_int16
4312     = arch_integer_type (gdbarch, 16, 0, "int16_t");
4313   builtin_type->builtin_uint16
4314     = arch_integer_type (gdbarch, 16, 1, "uint16_t");
4315   builtin_type->builtin_int32
4316     = arch_integer_type (gdbarch, 32, 0, "int32_t");
4317   builtin_type->builtin_uint32
4318     = arch_integer_type (gdbarch, 32, 1, "uint32_t");
4319   builtin_type->builtin_int64
4320     = arch_integer_type (gdbarch, 64, 0, "int64_t");
4321   builtin_type->builtin_uint64
4322     = arch_integer_type (gdbarch, 64, 1, "uint64_t");
4323   builtin_type->builtin_int128
4324     = arch_integer_type (gdbarch, 128, 0, "int128_t");
4325   builtin_type->builtin_uint128
4326     = arch_integer_type (gdbarch, 128, 1, "uint128_t");
4327   TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |=
4328     TYPE_INSTANCE_FLAG_NOTTEXT;
4329   TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |=
4330     TYPE_INSTANCE_FLAG_NOTTEXT;
4331
4332   /* Wide character types.  */
4333   builtin_type->builtin_char16
4334     = arch_integer_type (gdbarch, 16, 0, "char16_t");
4335   builtin_type->builtin_char32
4336     = arch_integer_type (gdbarch, 32, 0, "char32_t");
4337         
4338
4339   /* Default data/code pointer types.  */
4340   builtin_type->builtin_data_ptr
4341     = lookup_pointer_type (builtin_type->builtin_void);
4342   builtin_type->builtin_func_ptr
4343     = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
4344   builtin_type->builtin_func_func
4345     = lookup_function_type (builtin_type->builtin_func_ptr);
4346
4347   /* This type represents a GDB internal function.  */
4348   builtin_type->internal_fn
4349     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
4350                  "<internal function>");
4351
4352   return builtin_type;
4353 }
4354
4355 /* This set of objfile-based types is intended to be used by symbol
4356    readers as basic types.  */
4357
4358 static const struct objfile_data *objfile_type_data;
4359
4360 const struct objfile_type *
4361 objfile_type (struct objfile *objfile)
4362 {
4363   struct gdbarch *gdbarch;
4364   struct objfile_type *objfile_type
4365     = objfile_data (objfile, objfile_type_data);
4366
4367   if (objfile_type)
4368     return objfile_type;
4369
4370   objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
4371                                  1, struct objfile_type);
4372
4373   /* Use the objfile architecture to determine basic type properties.  */
4374   gdbarch = get_objfile_arch (objfile);
4375
4376   /* Basic types.  */
4377   objfile_type->builtin_void
4378     = init_type (TYPE_CODE_VOID, 1,
4379                  0,
4380                  "void", objfile);
4381
4382   objfile_type->builtin_char
4383     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4384                  (TYPE_FLAG_NOSIGN
4385                   | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)),
4386                  "char", objfile);
4387   objfile_type->builtin_signed_char
4388     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4389                  0,
4390                  "signed char", objfile);
4391   objfile_type->builtin_unsigned_char
4392     = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
4393                  TYPE_FLAG_UNSIGNED,
4394                  "unsigned char", objfile);
4395   objfile_type->builtin_short
4396     = init_type (TYPE_CODE_INT,
4397                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4398                  0, "short", objfile);
4399   objfile_type->builtin_unsigned_short
4400     = init_type (TYPE_CODE_INT,
4401                  gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT,
4402                  TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
4403   objfile_type->builtin_int
4404     = init_type (TYPE_CODE_INT,
4405                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4406                  0, "int", objfile);
4407   objfile_type->builtin_unsigned_int
4408     = init_type (TYPE_CODE_INT,
4409                  gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT,
4410                  TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
4411   objfile_type->builtin_long
4412     = init_type (TYPE_CODE_INT,
4413                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4414                  0, "long", objfile);
4415   objfile_type->builtin_unsigned_long
4416     = init_type (TYPE_CODE_INT,
4417                  gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT,
4418                  TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
4419   objfile_type->builtin_long_long
4420     = init_type (TYPE_CODE_INT,
4421                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4422                  0, "long long", objfile);
4423   objfile_type->builtin_unsigned_long_long
4424     = init_type (TYPE_CODE_INT,
4425                  gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT,
4426                  TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
4427
4428   objfile_type->builtin_float
4429     = init_type (TYPE_CODE_FLT,
4430                  gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT,
4431                  0, "float", objfile);
4432   TYPE_FLOATFORMAT (objfile_type->builtin_float)
4433     = gdbarch_float_format (gdbarch);
4434   objfile_type->builtin_double
4435     = init_type (TYPE_CODE_FLT,
4436                  gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT,
4437                  0, "double", objfile);
4438   TYPE_FLOATFORMAT (objfile_type->builtin_double)
4439     = gdbarch_double_format (gdbarch);
4440   objfile_type->builtin_long_double
4441     = init_type (TYPE_CODE_FLT,
4442                  gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT,
4443                  0, "long double", objfile);
4444   TYPE_FLOATFORMAT (objfile_type->builtin_long_double)
4445     = gdbarch_long_double_format (gdbarch);
4446
4447   /* This type represents a type that was unrecognized in symbol read-in.  */
4448   objfile_type->builtin_error
4449     = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile);
4450
4451   /* The following set of types is used for symbols with no
4452      debug information.  */
4453   objfile_type->nodebug_text_symbol
4454     = init_type (TYPE_CODE_FUNC, 1, 0,
4455                  "<text variable, no debug info>", objfile);
4456   TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol)
4457     = objfile_type->builtin_int;
4458   objfile_type->nodebug_text_gnu_ifunc_symbol
4459     = init_type (TYPE_CODE_FUNC, 1, TYPE_FLAG_GNU_IFUNC,
4460                  "<text gnu-indirect-function variable, no debug info>",
4461                  objfile);
4462   TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol)
4463     = objfile_type->nodebug_text_symbol;
4464   objfile_type->nodebug_got_plt_symbol
4465     = init_type (TYPE_CODE_PTR, gdbarch_addr_bit (gdbarch) / 8, 0,
4466                  "<text from jump slot in .got.plt, no debug info>",
4467                  objfile);
4468   TYPE_TARGET_TYPE (objfile_type->nodebug_got_plt_symbol)
4469     = objfile_type->nodebug_text_symbol;
4470   objfile_type->nodebug_data_symbol
4471     = init_type (TYPE_CODE_INT,
4472                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4473                  "<data variable, no debug info>", objfile);
4474   objfile_type->nodebug_unknown_symbol
4475     = init_type (TYPE_CODE_INT, 1, 0,
4476                  "<variable (not text or data), no debug info>", objfile);
4477   objfile_type->nodebug_tls_symbol
4478     = init_type (TYPE_CODE_INT,
4479                  gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0,
4480                  "<thread local variable, no debug info>", objfile);
4481
4482   /* NOTE: on some targets, addresses and pointers are not necessarily
4483      the same.
4484
4485      The upshot is:
4486      - gdb's `struct type' always describes the target's
4487        representation.
4488      - gdb's `struct value' objects should always hold values in
4489        target form.
4490      - gdb's CORE_ADDR values are addresses in the unified virtual
4491        address space that the assembler and linker work with.  Thus,
4492        since target_read_memory takes a CORE_ADDR as an argument, it
4493        can access any memory on the target, even if the processor has
4494        separate code and data address spaces.
4495
4496      In this context, objfile_type->builtin_core_addr is a bit odd:
4497      it's a target type for a value the target will never see.  It's
4498      only used to hold the values of (typeless) linker symbols, which
4499      are indeed in the unified virtual address space.  */
4500
4501   objfile_type->builtin_core_addr
4502     = init_type (TYPE_CODE_INT,
4503                  gdbarch_addr_bit (gdbarch) / 8,
4504                  TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile);
4505
4506   set_objfile_data (objfile, objfile_type_data, objfile_type);
4507   return objfile_type;
4508 }
4509
4510 extern initialize_file_ftype _initialize_gdbtypes;
4511
4512 void
4513 _initialize_gdbtypes (void)
4514 {
4515   gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
4516   objfile_type_data = register_objfile_data ();
4517
4518   add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
4519                              _("Set debugging of C++ overloading."),
4520                              _("Show debugging of C++ overloading."),
4521                              _("When enabled, ranking of the "
4522                                "functions is displayed."),
4523                              NULL,
4524                              show_overload_debug,
4525                              &setdebuglist, &showdebuglist);
4526
4527   /* Add user knob for controlling resolution of opaque types.  */
4528   add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
4529                            &opaque_type_resolution,
4530                            _("Set resolution of opaque struct/class/union"
4531                              " types (if set before loading symbols)."),
4532                            _("Show resolution of opaque struct/class/union"
4533                              " types (if set before loading symbols)."),
4534                            NULL, NULL,
4535                            show_opaque_type_resolution,
4536                            &setlist, &showlist);
4537
4538   /* Add an option to permit non-strict type checking.  */
4539   add_setshow_boolean_cmd ("type", class_support,
4540                            &strict_type_checking,
4541                            _("Set strict type checking."),
4542                            _("Show strict type checking."),
4543                            NULL, NULL,
4544                            show_strict_type_checking,
4545                            &setchecklist, &showchecklist);
4546 }