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