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