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