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