1/* Support routines for manipulating internal types for GDB.
2   Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004 Free Software Foundation, Inc.
4   Contributed by Cygnus Support, using pieces from other GDB modules.
5
6   This file is part of GDB.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.  */
22
23#include "defs.h"
24#include "gdb_string.h"
25#include "bfd.h"
26#include "symtab.h"
27#include "symfile.h"
28#include "objfiles.h"
29#include "gdbtypes.h"
30#include "expression.h"
31#include "language.h"
32#include "target.h"
33#include "value.h"
34#include "demangle.h"
35#include "complaints.h"
36#include "gdbcmd.h"
37#include "wrapper.h"
38#include "cp-abi.h"
39#include "gdb_assert.h"
40
41/* These variables point to the objects
42   representing the predefined C data types.  */
43
44struct type *builtin_type_void;
45struct type *builtin_type_char;
46struct type *builtin_type_true_char;
47struct type *builtin_type_short;
48struct type *builtin_type_int;
49struct type *builtin_type_long;
50struct type *builtin_type_long_long;
51struct type *builtin_type_signed_char;
52struct type *builtin_type_unsigned_char;
53struct type *builtin_type_unsigned_short;
54struct type *builtin_type_unsigned_int;
55struct type *builtin_type_unsigned_long;
56struct type *builtin_type_unsigned_long_long;
57struct type *builtin_type_float;
58struct type *builtin_type_double;
59struct type *builtin_type_long_double;
60struct type *builtin_type_complex;
61struct type *builtin_type_double_complex;
62struct type *builtin_type_string;
63struct type *builtin_type_int0;
64struct type *builtin_type_int8;
65struct type *builtin_type_uint8;
66struct type *builtin_type_int16;
67struct type *builtin_type_uint16;
68struct type *builtin_type_int32;
69struct type *builtin_type_uint32;
70struct type *builtin_type_int64;
71struct type *builtin_type_uint64;
72struct type *builtin_type_int128;
73struct type *builtin_type_uint128;
74struct type *builtin_type_bool;
75
76/* 128 bit long vector types */
77struct type *builtin_type_v2_double;
78struct type *builtin_type_v4_float;
79struct type *builtin_type_v2_int64;
80struct type *builtin_type_v4_int32;
81struct type *builtin_type_v8_int16;
82struct type *builtin_type_v16_int8;
83/* 64 bit long vector types */
84struct type *builtin_type_v2_float;
85struct type *builtin_type_v2_int32;
86struct type *builtin_type_v4_int16;
87struct type *builtin_type_v8_int8;
88
89struct type *builtin_type_v4sf;
90struct type *builtin_type_v4si;
91struct type *builtin_type_v16qi;
92struct type *builtin_type_v8qi;
93struct type *builtin_type_v8hi;
94struct type *builtin_type_v4hi;
95struct type *builtin_type_v2si;
96struct type *builtin_type_vec64;
97struct type *builtin_type_vec64i;
98struct type *builtin_type_vec128;
99struct type *builtin_type_vec128i;
100struct type *builtin_type_ieee_single_big;
101struct type *builtin_type_ieee_single_little;
102struct type *builtin_type_ieee_double_big;
103struct type *builtin_type_ieee_double_little;
104struct type *builtin_type_ieee_double_littlebyte_bigword;
105struct type *builtin_type_i387_ext;
106struct type *builtin_type_m68881_ext;
107struct type *builtin_type_i960_ext;
108struct type *builtin_type_m88110_ext;
109struct type *builtin_type_m88110_harris_ext;
110struct type *builtin_type_arm_ext_big;
111struct type *builtin_type_arm_ext_littlebyte_bigword;
112struct type *builtin_type_ia64_spill_big;
113struct type *builtin_type_ia64_spill_little;
114struct type *builtin_type_ia64_quad_big;
115struct type *builtin_type_ia64_quad_little;
116struct type *builtin_type_void_data_ptr;
117struct type *builtin_type_void_func_ptr;
118struct type *builtin_type_CORE_ADDR;
119struct type *builtin_type_bfd_vma;
120
121int opaque_type_resolution = 1;
122int overload_debug = 0;
123
124struct extra
125  {
126    char str[128];
127    int len;
128  };				/* maximum extension is 128! FIXME */
129
130static void print_bit_vector (B_TYPE *, int);
131static void print_arg_types (struct field *, int, int);
132static void dump_fn_fieldlists (struct type *, int);
133static void print_cplus_stuff (struct type *, int);
134static void virtual_base_list_aux (struct type *dclass);
135
136
137/* Alloc a new type structure and fill it with some defaults.  If
138   OBJFILE is non-NULL, then allocate the space for the type structure
139   in that objfile's objfile_obstack.  Otherwise allocate the new type structure
140   by xmalloc () (for permanent types).  */
141
142struct type *
143alloc_type (struct objfile *objfile)
144{
145  struct type *type;
146
147  /* Alloc the structure and start off with all fields zeroed. */
148
149  if (objfile == NULL)
150    {
151      type = xmalloc (sizeof (struct type));
152      memset (type, 0, sizeof (struct type));
153      TYPE_MAIN_TYPE (type) = xmalloc (sizeof (struct main_type));
154    }
155  else
156    {
157      type = obstack_alloc (&objfile->objfile_obstack,
158			    sizeof (struct type));
159      memset (type, 0, sizeof (struct type));
160      TYPE_MAIN_TYPE (type) = obstack_alloc (&objfile->objfile_obstack,
161					     sizeof (struct main_type));
162      OBJSTAT (objfile, n_types++);
163    }
164  memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
165
166  /* Initialize the fields that might not be zero. */
167
168  TYPE_CODE (type) = TYPE_CODE_UNDEF;
169  TYPE_OBJFILE (type) = objfile;
170  TYPE_VPTR_FIELDNO (type) = -1;
171  TYPE_CHAIN (type) = type;	/* Chain back to itself.  */
172
173  return (type);
174}
175
176/* Alloc a new type instance structure, fill it with some defaults,
177   and point it at OLDTYPE.  Allocate the new type instance from the
178   same place as OLDTYPE.  */
179
180static struct type *
181alloc_type_instance (struct type *oldtype)
182{
183  struct type *type;
184
185  /* Allocate the structure.  */
186
187  if (TYPE_OBJFILE (oldtype) == NULL)
188    {
189      type = xmalloc (sizeof (struct type));
190      memset (type, 0, sizeof (struct type));
191    }
192  else
193    {
194      type = obstack_alloc (&TYPE_OBJFILE (oldtype)->objfile_obstack,
195			    sizeof (struct type));
196      memset (type, 0, sizeof (struct type));
197    }
198  TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
199
200  TYPE_CHAIN (type) = type;	/* Chain back to itself for now.  */
201
202  return (type);
203}
204
205/* Clear all remnants of the previous type at TYPE, in preparation for
206   replacing it with something else.  */
207static void
208smash_type (struct type *type)
209{
210  memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
211
212  /* For now, delete the rings.  */
213  TYPE_CHAIN (type) = type;
214
215  /* For now, leave the pointer/reference types alone.  */
216}
217
218/* Lookup a pointer to a type TYPE.  TYPEPTR, if nonzero, points
219   to a pointer to memory where the pointer type should be stored.
220   If *TYPEPTR is zero, update it to point to the pointer type we return.
221   We allocate new memory if needed.  */
222
223struct type *
224make_pointer_type (struct type *type, struct type **typeptr)
225{
226  struct type *ntype;	/* New type */
227  struct objfile *objfile;
228
229  ntype = TYPE_POINTER_TYPE (type);
230
231  if (ntype)
232    {
233      if (typeptr == 0)
234	return ntype;		/* Don't care about alloc, and have new type.  */
235      else if (*typeptr == 0)
236	{
237	  *typeptr = ntype;	/* Tracking alloc, and we have new type.  */
238	  return ntype;
239	}
240    }
241
242  if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
243    {
244      ntype = alloc_type (TYPE_OBJFILE (type));
245      if (typeptr)
246	*typeptr = ntype;
247    }
248  else
249    /* We have storage, but need to reset it.  */
250    {
251      ntype = *typeptr;
252      objfile = TYPE_OBJFILE (ntype);
253      smash_type (ntype);
254      TYPE_OBJFILE (ntype) = objfile;
255    }
256
257  TYPE_TARGET_TYPE (ntype) = type;
258  TYPE_POINTER_TYPE (type) = ntype;
259
260  /* FIXME!  Assume the machine has only one representation for pointers!  */
261
262  TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
263  TYPE_CODE (ntype) = TYPE_CODE_PTR;
264
265  /* Mark pointers as unsigned.  The target converts between pointers
266     and addresses (CORE_ADDRs) using POINTER_TO_ADDRESS() and
267     ADDRESS_TO_POINTER(). */
268  TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
269
270  if (!TYPE_POINTER_TYPE (type))	/* Remember it, if don't have one.  */
271    TYPE_POINTER_TYPE (type) = ntype;
272
273  return ntype;
274}
275
276/* Given a type TYPE, return a type of pointers to that type.
277   May need to construct such a type if this is the first use.  */
278
279struct type *
280lookup_pointer_type (struct type *type)
281{
282  return make_pointer_type (type, (struct type **) 0);
283}
284
285/* Lookup a C++ `reference' to a type TYPE.  TYPEPTR, if nonzero, points
286   to a pointer to memory where the reference type should be stored.
287   If *TYPEPTR is zero, update it to point to the reference type we return.
288   We allocate new memory if needed.  */
289
290struct type *
291make_reference_type (struct type *type, struct type **typeptr)
292{
293  struct type *ntype;	/* New type */
294  struct objfile *objfile;
295
296  ntype = TYPE_REFERENCE_TYPE (type);
297
298  if (ntype)
299    {
300      if (typeptr == 0)
301	return ntype;		/* Don't care about alloc, and have new type.  */
302      else if (*typeptr == 0)
303	{
304	  *typeptr = ntype;	/* Tracking alloc, and we have new type.  */
305	  return ntype;
306	}
307    }
308
309  if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
310    {
311      ntype = alloc_type (TYPE_OBJFILE (type));
312      if (typeptr)
313	*typeptr = ntype;
314    }
315  else
316    /* We have storage, but need to reset it.  */
317    {
318      ntype = *typeptr;
319      objfile = TYPE_OBJFILE (ntype);
320      smash_type (ntype);
321      TYPE_OBJFILE (ntype) = objfile;
322    }
323
324  TYPE_TARGET_TYPE (ntype) = type;
325  TYPE_REFERENCE_TYPE (type) = ntype;
326
327  /* FIXME!  Assume the machine has only one representation for references,
328     and that it matches the (only) representation for pointers!  */
329
330  TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
331  TYPE_CODE (ntype) = TYPE_CODE_REF;
332
333  if (!TYPE_REFERENCE_TYPE (type))	/* Remember it, if don't have one.  */
334    TYPE_REFERENCE_TYPE (type) = ntype;
335
336  return ntype;
337}
338
339/* Same as above, but caller doesn't care about memory allocation details.  */
340
341struct type *
342lookup_reference_type (struct type *type)
343{
344  return make_reference_type (type, (struct type **) 0);
345}
346
347/* Lookup a function type that returns type TYPE.  TYPEPTR, if nonzero, points
348   to a pointer to memory where the function type should be stored.
349   If *TYPEPTR is zero, update it to point to the function type we return.
350   We allocate new memory if needed.  */
351
352struct type *
353make_function_type (struct type *type, struct type **typeptr)
354{
355  struct type *ntype;	/* New type */
356  struct objfile *objfile;
357
358  if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
359    {
360      ntype = alloc_type (TYPE_OBJFILE (type));
361      if (typeptr)
362	*typeptr = ntype;
363    }
364  else
365    /* We have storage, but need to reset it.  */
366    {
367      ntype = *typeptr;
368      objfile = TYPE_OBJFILE (ntype);
369      smash_type (ntype);
370      TYPE_OBJFILE (ntype) = objfile;
371    }
372
373  TYPE_TARGET_TYPE (ntype) = type;
374
375  TYPE_LENGTH (ntype) = 1;
376  TYPE_CODE (ntype) = TYPE_CODE_FUNC;
377
378  return ntype;
379}
380
381
382/* Given a type TYPE, return a type of functions that return that type.
383   May need to construct such a type if this is the first use.  */
384
385struct type *
386lookup_function_type (struct type *type)
387{
388  return make_function_type (type, (struct type **) 0);
389}
390
391/* Identify address space identifier by name --
392   return the integer flag defined in gdbtypes.h.  */
393extern int
394address_space_name_to_int (char *space_identifier)
395{
396  struct gdbarch *gdbarch = current_gdbarch;
397  int type_flags;
398  /* Check for known address space delimiters. */
399  if (!strcmp (space_identifier, "code"))
400    return TYPE_FLAG_CODE_SPACE;
401  else if (!strcmp (space_identifier, "data"))
402    return TYPE_FLAG_DATA_SPACE;
403  else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
404           && gdbarch_address_class_name_to_type_flags (gdbarch,
405							space_identifier,
406							&type_flags))
407    return type_flags;
408  else
409    error ("Unknown address space specifier: \"%s\"", space_identifier);
410}
411
412/* Identify address space identifier by integer flag as defined in
413   gdbtypes.h -- return the string version of the adress space name. */
414
415const char *
416address_space_int_to_name (int space_flag)
417{
418  struct gdbarch *gdbarch = current_gdbarch;
419  if (space_flag & TYPE_FLAG_CODE_SPACE)
420    return "code";
421  else if (space_flag & TYPE_FLAG_DATA_SPACE)
422    return "data";
423  else if ((space_flag & TYPE_FLAG_ADDRESS_CLASS_ALL)
424           && gdbarch_address_class_type_flags_to_name_p (gdbarch))
425    return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
426  else
427    return NULL;
428}
429
430/* Create a new type with instance flags NEW_FLAGS, based on TYPE.
431   If STORAGE is non-NULL, create the new type instance there.  */
432
433static struct type *
434make_qualified_type (struct type *type, int new_flags,
435		     struct type *storage)
436{
437  struct type *ntype;
438
439  ntype = type;
440  do {
441    if (TYPE_INSTANCE_FLAGS (ntype) == new_flags)
442      return ntype;
443    ntype = TYPE_CHAIN (ntype);
444  } while (ntype != type);
445
446  /* Create a new type instance.  */
447  if (storage == NULL)
448    ntype = alloc_type_instance (type);
449  else
450    {
451      ntype = storage;
452      TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
453      TYPE_CHAIN (ntype) = ntype;
454    }
455
456  /* Pointers or references to the original type are not relevant to
457     the new type.  */
458  TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
459  TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
460
461  /* Chain the new qualified type to the old type.  */
462  TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
463  TYPE_CHAIN (type) = ntype;
464
465  /* Now set the instance flags and return the new type.  */
466  TYPE_INSTANCE_FLAGS (ntype) = new_flags;
467
468  /* Set length of new type to that of the original type.  */
469  TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
470
471  return ntype;
472}
473
474/* Make an address-space-delimited variant of a type -- a type that
475   is identical to the one supplied except that it has an address
476   space attribute attached to it (such as "code" or "data").
477
478   The space attributes "code" and "data" are for Harvard architectures.
479   The address space attributes are for architectures which have
480   alternately sized pointers or pointers with alternate representations.  */
481
482struct type *
483make_type_with_address_space (struct type *type, int space_flag)
484{
485  struct type *ntype;
486  int new_flags = ((TYPE_INSTANCE_FLAGS (type)
487		    & ~(TYPE_FLAG_CODE_SPACE | TYPE_FLAG_DATA_SPACE
488		        | TYPE_FLAG_ADDRESS_CLASS_ALL))
489		   | space_flag);
490
491  return make_qualified_type (type, new_flags, NULL);
492}
493
494/* Make a "c-v" variant of a type -- a type that is identical to the
495   one supplied except that it may have const or volatile attributes
496   CNST is a flag for setting the const attribute
497   VOLTL is a flag for setting the volatile attribute
498   TYPE is the base type whose variant we are creating.
499   TYPEPTR, if nonzero, points
500   to a pointer to memory where the reference type should be stored.
501   If *TYPEPTR is zero, update it to point to the reference type we return.
502   We allocate new memory if needed.  */
503
504struct type *
505make_cvr_type (int cnst, int voltl, int restrct, struct type *type,
506               struct type **typeptr)
507{
508  struct type *ntype;	/* New type */
509  struct type *tmp_type = type;	/* tmp type */
510  struct objfile *objfile;
511
512  int new_flags = (TYPE_INSTANCE_FLAGS (type)
513		   & ~(TYPE_FLAG_CONST | TYPE_FLAG_VOLATILE));
514
515  if (cnst)
516    new_flags |= TYPE_FLAG_CONST;
517
518  if (voltl)
519    new_flags |= TYPE_FLAG_VOLATILE;
520
521  if (restrct)
522    new_flags |= TYPE_FLAG_RESTRICT;
523
524  if (typeptr && *typeptr != NULL)
525    {
526      /* Objfile is per-core-type.  This const-qualified type had best
527	 belong to the same objfile as the type it is qualifying, unless
528	 we are overwriting a stub type, in which case the safest thing
529	 to do is to copy the core type into the new objfile.  */
530
531      gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type)
532		  || TYPE_STUB (*typeptr));
533      if (TYPE_OBJFILE (*typeptr) != TYPE_OBJFILE (type))
534	{
535	  TYPE_MAIN_TYPE (*typeptr)
536	    = TYPE_ALLOC (*typeptr, sizeof (struct main_type));
537	  *TYPE_MAIN_TYPE (*typeptr)
538	    = *TYPE_MAIN_TYPE (type);
539	}
540    }
541
542  ntype = make_qualified_type (type, new_flags, typeptr ? *typeptr : NULL);
543
544  if (typeptr != NULL)
545    *typeptr = ntype;
546
547  return ntype;
548}
549
550/* Replace the contents of ntype with the type *type.  This changes the
551   contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
552   the changes are propogated to all types in the TYPE_CHAIN.
553
554   In order to build recursive types, it's inevitable that we'll need
555   to update types in place --- but this sort of indiscriminate
556   smashing is ugly, and needs to be replaced with something more
557   controlled.  TYPE_MAIN_TYPE is a step in this direction; it's not
558   clear if more steps are needed.  */
559void
560replace_type (struct type *ntype, struct type *type)
561{
562  struct type *chain;
563
564  *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
565
566  /* The type length is not a part of the main type.  Update it for each
567     type on the variant chain.  */
568  chain = ntype;
569  do {
570    /* Assert that this element of the chain has no address-class bits
571       set in its flags.  Such type variants might have type lengths
572       which are supposed to be different from the non-address-class
573       variants.  This assertion shouldn't ever be triggered because
574       symbol readers which do construct address-class variants don't
575       call replace_type().  */
576    gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
577
578    TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
579    chain = TYPE_CHAIN (chain);
580  } while (ntype != chain);
581
582  /* Assert that the two types have equivalent instance qualifiers.
583     This should be true for at least all of our debug readers.  */
584  gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type));
585}
586
587/* Implement direct support for MEMBER_TYPE in GNU C++.
588   May need to construct such a type if this is the first use.
589   The TYPE is the type of the member.  The DOMAIN is the type
590   of the aggregate that the member belongs to.  */
591
592struct type *
593lookup_member_type (struct type *type, struct type *domain)
594{
595  struct type *mtype;
596
597  mtype = alloc_type (TYPE_OBJFILE (type));
598  smash_to_member_type (mtype, domain, type);
599  return (mtype);
600}
601
602/* Allocate a stub method whose return type is TYPE.
603   This apparently happens for speed of symbol reading, since parsing
604   out the arguments to the method is cpu-intensive, the way we are doing
605   it.  So, we will fill in arguments later.
606   This always returns a fresh type.   */
607
608struct type *
609allocate_stub_method (struct type *type)
610{
611  struct type *mtype;
612
613  mtype = init_type (TYPE_CODE_METHOD, 1, TYPE_FLAG_STUB, NULL,
614		     TYPE_OBJFILE (type));
615  TYPE_TARGET_TYPE (mtype) = type;
616  /*  _DOMAIN_TYPE (mtype) = unknown yet */
617  return (mtype);
618}
619
620/* Create a range type using either a blank type supplied in RESULT_TYPE,
621   or creating a new type, inheriting the objfile from INDEX_TYPE.
622
623   Indices will be of type INDEX_TYPE, and will range from LOW_BOUND to
624   HIGH_BOUND, inclusive.
625
626   FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
627   sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
628
629struct type *
630create_range_type (struct type *result_type, struct type *index_type,
631		   int low_bound, int high_bound)
632{
633  if (result_type == NULL)
634    {
635      result_type = alloc_type (TYPE_OBJFILE (index_type));
636    }
637  TYPE_CODE (result_type) = TYPE_CODE_RANGE;
638  TYPE_TARGET_TYPE (result_type) = index_type;
639  if (TYPE_STUB (index_type))
640    TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
641  else
642    TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
643  TYPE_NFIELDS (result_type) = 2;
644  TYPE_FIELDS (result_type) = (struct field *)
645    TYPE_ALLOC (result_type, 2 * sizeof (struct field));
646  memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
647  TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
648  TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
649  TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int;	/* FIXME */
650  TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int;	/* FIXME */
651
652  if (low_bound >= 0)
653    TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
654
655  return (result_type);
656}
657
658/* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type TYPE.
659   Return 1 of type is a range type, 0 if it is discrete (and bounds
660   will fit in LONGEST), or -1 otherwise. */
661
662int
663get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
664{
665  CHECK_TYPEDEF (type);
666  switch (TYPE_CODE (type))
667    {
668    case TYPE_CODE_RANGE:
669      *lowp = TYPE_LOW_BOUND (type);
670      *highp = TYPE_HIGH_BOUND (type);
671      return 1;
672    case TYPE_CODE_ENUM:
673      if (TYPE_NFIELDS (type) > 0)
674	{
675	  /* The enums may not be sorted by value, so search all
676	     entries */
677	  int i;
678
679	  *lowp = *highp = TYPE_FIELD_BITPOS (type, 0);
680	  for (i = 0; i < TYPE_NFIELDS (type); i++)
681	    {
682	      if (TYPE_FIELD_BITPOS (type, i) < *lowp)
683		*lowp = TYPE_FIELD_BITPOS (type, i);
684	      if (TYPE_FIELD_BITPOS (type, i) > *highp)
685		*highp = TYPE_FIELD_BITPOS (type, i);
686	    }
687
688	  /* Set unsigned indicator if warranted. */
689	  if (*lowp >= 0)
690	    {
691	      TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
692	    }
693	}
694      else
695	{
696	  *lowp = 0;
697	  *highp = -1;
698	}
699      return 0;
700    case TYPE_CODE_BOOL:
701      *lowp = 0;
702      *highp = 1;
703      return 0;
704    case TYPE_CODE_INT:
705      if (TYPE_LENGTH (type) > sizeof (LONGEST))	/* Too big */
706	return -1;
707      if (!TYPE_UNSIGNED (type))
708	{
709	  *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
710	  *highp = -*lowp - 1;
711	  return 0;
712	}
713      /* ... fall through for unsigned ints ... */
714    case TYPE_CODE_CHAR:
715      *lowp = 0;
716      /* This round-about calculation is to avoid shifting by
717         TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
718         if TYPE_LENGTH (type) == sizeof (LONGEST). */
719      *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
720      *highp = (*highp - 1) | *highp;
721      return 0;
722    default:
723      return -1;
724    }
725}
726
727/* Create an array type using either a blank type supplied in RESULT_TYPE,
728   or creating a new type, inheriting the objfile from RANGE_TYPE.
729
730   Elements will be of type ELEMENT_TYPE, the indices will be of type
731   RANGE_TYPE.
732
733   FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
734   sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
735
736struct type *
737create_array_type (struct type *result_type, struct type *element_type,
738		   struct type *range_type)
739{
740  LONGEST low_bound, high_bound;
741
742  if (result_type == NULL)
743    {
744      result_type = alloc_type (TYPE_OBJFILE (range_type));
745    }
746  TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
747  TYPE_TARGET_TYPE (result_type) = element_type;
748  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
749    low_bound = high_bound = 0;
750  CHECK_TYPEDEF (element_type);
751  TYPE_LENGTH (result_type) =
752    TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
753  TYPE_NFIELDS (result_type) = 1;
754  TYPE_FIELDS (result_type) =
755    (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
756  memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
757  TYPE_FIELD_TYPE (result_type, 0) = range_type;
758  TYPE_VPTR_FIELDNO (result_type) = -1;
759
760  /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */
761  if (TYPE_LENGTH (result_type) == 0)
762    TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
763
764  return (result_type);
765}
766
767/* Create a string type using either a blank type supplied in RESULT_TYPE,
768   or creating a new type.  String types are similar enough to array of
769   char types that we can use create_array_type to build the basic type
770   and then bash it into a string type.
771
772   For fixed length strings, the range type contains 0 as the lower
773   bound and the length of the string minus one as the upper bound.
774
775   FIXME:  Maybe we should check the TYPE_CODE of RESULT_TYPE to make
776   sure it is TYPE_CODE_UNDEF before we bash it into a string type? */
777
778struct type *
779create_string_type (struct type *result_type, struct type *range_type)
780{
781  result_type = create_array_type (result_type,
782				   *current_language->string_char_type,
783				   range_type);
784  TYPE_CODE (result_type) = TYPE_CODE_STRING;
785  return (result_type);
786}
787
788struct type *
789create_set_type (struct type *result_type, struct type *domain_type)
790{
791  LONGEST low_bound, high_bound, bit_length;
792  if (result_type == NULL)
793    {
794      result_type = alloc_type (TYPE_OBJFILE (domain_type));
795    }
796  TYPE_CODE (result_type) = TYPE_CODE_SET;
797  TYPE_NFIELDS (result_type) = 1;
798  TYPE_FIELDS (result_type) = (struct field *)
799    TYPE_ALLOC (result_type, 1 * sizeof (struct field));
800  memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
801
802  if (!TYPE_STUB (domain_type))
803    {
804      if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
805	low_bound = high_bound = 0;
806      bit_length = high_bound - low_bound + 1;
807      TYPE_LENGTH (result_type)
808	= (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
809    }
810  TYPE_FIELD_TYPE (result_type, 0) = domain_type;
811
812  if (low_bound >= 0)
813    TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
814
815  return (result_type);
816}
817
818/* Construct and return a type of the form:
819	struct NAME { ELT_TYPE ELT_NAME[N]; }
820   We use these types for SIMD registers.  For example, the type of
821   the SSE registers on the late x86-family processors is:
822	struct __builtin_v4sf { float f[4]; }
823   built by the function call:
824	init_simd_type ("__builtin_v4sf", builtin_type_float, "f", 4)
825   The type returned is a permanent type, allocated using malloc; it
826   doesn't live in any objfile's obstack.  */
827static struct type *
828init_simd_type (char *name,
829		struct type *elt_type,
830		char *elt_name,
831		int n)
832{
833  struct type *simd_type;
834  struct type *array_type;
835
836  simd_type = init_composite_type (name, TYPE_CODE_STRUCT);
837  array_type = create_array_type (0, elt_type,
838				  create_range_type (0, builtin_type_int,
839						     0, n-1));
840  append_composite_type_field (simd_type, elt_name, array_type);
841  return simd_type;
842}
843
844static struct type *
845init_vector_type (struct type *elt_type, int n)
846{
847  struct type *array_type;
848
849  array_type = create_array_type (0, elt_type,
850				  create_range_type (0, builtin_type_int,
851						     0, n-1));
852  TYPE_FLAGS (array_type) |= TYPE_FLAG_VECTOR;
853  return array_type;
854}
855
856static struct type *
857build_builtin_type_vec64 (void)
858{
859  /* Construct a type for the 64 bit registers.  The type we're
860     building is this: */
861#if 0
862  union __gdb_builtin_type_vec64
863  {
864    int64_t uint64;
865    float v2_float[2];
866    int32_t v2_int32[2];
867    int16_t v4_int16[4];
868    int8_t v8_int8[8];
869  };
870#endif
871
872  struct type *t;
873
874  t = init_composite_type ("__gdb_builtin_type_vec64", TYPE_CODE_UNION);
875  append_composite_type_field (t, "uint64", builtin_type_int64);
876  append_composite_type_field (t, "v2_float", builtin_type_v2_float);
877  append_composite_type_field (t, "v2_int32", builtin_type_v2_int32);
878  append_composite_type_field (t, "v4_int16", builtin_type_v4_int16);
879  append_composite_type_field (t, "v8_int8", builtin_type_v8_int8);
880
881  TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR;
882  TYPE_NAME (t) = "builtin_type_vec64";
883  return t;
884}
885
886static struct type *
887build_builtin_type_vec64i (void)
888{
889  /* Construct a type for the 64 bit registers.  The type we're
890     building is this: */
891#if 0
892  union __gdb_builtin_type_vec64i
893  {
894    int64_t uint64;
895    int32_t v2_int32[2];
896    int16_t v4_int16[4];
897    int8_t v8_int8[8];
898  };
899#endif
900
901  struct type *t;
902
903  t = init_composite_type ("__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
904  append_composite_type_field (t, "uint64", builtin_type_int64);
905  append_composite_type_field (t, "v2_int32", builtin_type_v2_int32);
906  append_composite_type_field (t, "v4_int16", builtin_type_v4_int16);
907  append_composite_type_field (t, "v8_int8", builtin_type_v8_int8);
908
909  TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR;
910  TYPE_NAME (t) = "builtin_type_vec64i";
911  return t;
912}
913
914static struct type *
915build_builtin_type_vec128 (void)
916{
917  /* Construct a type for the 128 bit registers.  The type we're
918     building is this: */
919#if 0
920 union __gdb_builtin_type_vec128
921  {
922    int128_t uint128;
923    float v4_float[4];
924    int32_t v4_int32[4];
925    int16_t v8_int16[8];
926    int8_t v16_int8[16];
927  };
928#endif
929
930  struct type *t;
931
932  t = init_composite_type ("__gdb_builtin_type_vec128", TYPE_CODE_UNION);
933  append_composite_type_field (t, "uint128", builtin_type_int128);
934  append_composite_type_field (t, "v4_float", builtin_type_v4_float);
935  append_composite_type_field (t, "v4_int32", builtin_type_v4_int32);
936  append_composite_type_field (t, "v8_int16", builtin_type_v8_int16);
937  append_composite_type_field (t, "v16_int8", builtin_type_v16_int8);
938
939  TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR;
940  TYPE_NAME (t) = "builtin_type_vec128";
941  return t;
942}
943
944static struct type *
945build_builtin_type_vec128i (void)
946{
947  /* 128-bit Intel SIMD registers */
948  struct type *t;
949
950  t = init_composite_type ("__gdb_builtin_type_vec128i", TYPE_CODE_UNION);
951  append_composite_type_field (t, "v4_float", builtin_type_v4_float);
952  append_composite_type_field (t, "v2_double", builtin_type_v2_double);
953  append_composite_type_field (t, "v16_int8", builtin_type_v16_int8);
954  append_composite_type_field (t, "v8_int16", builtin_type_v8_int16);
955  append_composite_type_field (t, "v4_int32", builtin_type_v4_int32);
956  append_composite_type_field (t, "v2_int64", builtin_type_v2_int64);
957  append_composite_type_field (t, "uint128", builtin_type_int128);
958
959  TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR;
960  TYPE_NAME (t) = "builtin_type_vec128i";
961  return t;
962}
963
964/* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
965   A MEMBER is a wierd thing -- it amounts to a typed offset into
966   a struct, e.g. "an int at offset 8".  A MEMBER TYPE doesn't
967   include the offset (that's the value of the MEMBER itself), but does
968   include the structure type into which it points (for some reason).
969
970   When "smashing" the type, we preserve the objfile that the
971   old type pointed to, since we aren't changing where the type is actually
972   allocated.  */
973
974void
975smash_to_member_type (struct type *type, struct type *domain,
976		      struct type *to_type)
977{
978  struct objfile *objfile;
979
980  objfile = TYPE_OBJFILE (type);
981
982  smash_type (type);
983  TYPE_OBJFILE (type) = objfile;
984  TYPE_TARGET_TYPE (type) = to_type;
985  TYPE_DOMAIN_TYPE (type) = domain;
986  TYPE_LENGTH (type) = 1;	/* In practice, this is never needed.  */
987  TYPE_CODE (type) = TYPE_CODE_MEMBER;
988}
989
990/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
991   METHOD just means `function that gets an extra "this" argument'.
992
993   When "smashing" the type, we preserve the objfile that the
994   old type pointed to, since we aren't changing where the type is actually
995   allocated.  */
996
997void
998smash_to_method_type (struct type *type, struct type *domain,
999		      struct type *to_type, struct field *args,
1000		      int nargs, int varargs)
1001{
1002  struct objfile *objfile;
1003
1004  objfile = TYPE_OBJFILE (type);
1005
1006  smash_type (type);
1007  TYPE_OBJFILE (type) = objfile;
1008  TYPE_TARGET_TYPE (type) = to_type;
1009  TYPE_DOMAIN_TYPE (type) = domain;
1010  TYPE_FIELDS (type) = args;
1011  TYPE_NFIELDS (type) = nargs;
1012  if (varargs)
1013    TYPE_FLAGS (type) |= TYPE_FLAG_VARARGS;
1014  TYPE_LENGTH (type) = 1;	/* In practice, this is never needed.  */
1015  TYPE_CODE (type) = TYPE_CODE_METHOD;
1016}
1017
1018/* Return a typename for a struct/union/enum type without "struct ",
1019   "union ", or "enum ".  If the type has a NULL name, return NULL.  */
1020
1021char *
1022type_name_no_tag (const struct type *type)
1023{
1024  if (TYPE_TAG_NAME (type) != NULL)
1025    return TYPE_TAG_NAME (type);
1026
1027  /* Is there code which expects this to return the name if there is no
1028     tag name?  My guess is that this is mainly used for C++ in cases where
1029     the two will always be the same.  */
1030  return TYPE_NAME (type);
1031}
1032
1033/* Lookup a primitive type named NAME.
1034   Return zero if NAME is not a primitive type. */
1035
1036struct type *
1037lookup_primitive_typename (char *name)
1038{
1039  struct type **const *p;
1040
1041  for (p = current_language->la_builtin_type_vector; *p != NULL; p++)
1042    {
1043      if (strcmp (TYPE_NAME (**p), name) == 0)
1044	{
1045	  return (**p);
1046	}
1047    }
1048  return (NULL);
1049}
1050
1051/* Lookup a typedef or primitive type named NAME,
1052   visible in lexical block BLOCK.
1053   If NOERR is nonzero, return zero if NAME is not suitably defined.  */
1054
1055struct type *
1056lookup_typename (char *name, struct block *block, int noerr)
1057{
1058  struct symbol *sym;
1059  struct type *tmp;
1060
1061  sym = lookup_symbol (name, block, VAR_DOMAIN, 0, (struct symtab **) NULL);
1062  if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1063    {
1064      tmp = lookup_primitive_typename (name);
1065      if (tmp)
1066	{
1067	  return (tmp);
1068	}
1069      else if (!tmp && noerr)
1070	{
1071	  return (NULL);
1072	}
1073      else
1074	{
1075	  error ("No type named %s.", name);
1076	}
1077    }
1078  return (SYMBOL_TYPE (sym));
1079}
1080
1081struct type *
1082lookup_unsigned_typename (char *name)
1083{
1084  char *uns = alloca (strlen (name) + 10);
1085
1086  strcpy (uns, "unsigned ");
1087  strcpy (uns + 9, name);
1088  return (lookup_typename (uns, (struct block *) NULL, 0));
1089}
1090
1091struct type *
1092lookup_signed_typename (char *name)
1093{
1094  struct type *t;
1095  char *uns = alloca (strlen (name) + 8);
1096
1097  strcpy (uns, "signed ");
1098  strcpy (uns + 7, name);
1099  t = lookup_typename (uns, (struct block *) NULL, 1);
1100  /* If we don't find "signed FOO" just try again with plain "FOO". */
1101  if (t != NULL)
1102    return t;
1103  return lookup_typename (name, (struct block *) NULL, 0);
1104}
1105
1106/* Lookup a structure type named "struct NAME",
1107   visible in lexical block BLOCK.  */
1108
1109struct type *
1110lookup_struct (char *name, struct block *block)
1111{
1112  struct symbol *sym;
1113
1114  sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1115		       (struct symtab **) NULL);
1116
1117  if (sym == NULL)
1118    {
1119      error ("No struct type named %s.", name);
1120    }
1121  if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1122    {
1123      error ("This context has class, union or enum %s, not a struct.", name);
1124    }
1125  return (SYMBOL_TYPE (sym));
1126}
1127
1128/* Lookup a union type named "union NAME",
1129   visible in lexical block BLOCK.  */
1130
1131struct type *
1132lookup_union (char *name, struct block *block)
1133{
1134  struct symbol *sym;
1135  struct type *t;
1136
1137  sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1138		       (struct symtab **) NULL);
1139
1140  if (sym == NULL)
1141    error ("No union type named %s.", name);
1142
1143  t = SYMBOL_TYPE (sym);
1144
1145  if (TYPE_CODE (t) == TYPE_CODE_UNION)
1146    return (t);
1147
1148  /* C++ unions may come out with TYPE_CODE_CLASS, but we look at
1149   * a further "declared_type" field to discover it is really a union.
1150   */
1151  if (HAVE_CPLUS_STRUCT (t))
1152    if (TYPE_DECLARED_TYPE (t) == DECLARED_TYPE_UNION)
1153      return (t);
1154
1155  /* If we get here, it's not a union */
1156  error ("This context has class, struct or enum %s, not a union.", name);
1157}
1158
1159
1160/* Lookup an enum type named "enum NAME",
1161   visible in lexical block BLOCK.  */
1162
1163struct type *
1164lookup_enum (char *name, struct block *block)
1165{
1166  struct symbol *sym;
1167
1168  sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
1169		       (struct symtab **) NULL);
1170  if (sym == NULL)
1171    {
1172      error ("No enum type named %s.", name);
1173    }
1174  if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
1175    {
1176      error ("This context has class, struct or union %s, not an enum.", name);
1177    }
1178  return (SYMBOL_TYPE (sym));
1179}
1180
1181/* Lookup a template type named "template NAME<TYPE>",
1182   visible in lexical block BLOCK.  */
1183
1184struct type *
1185lookup_template_type (char *name, struct type *type, struct block *block)
1186{
1187  struct symbol *sym;
1188  char *nam = (char *) alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4);
1189  strcpy (nam, name);
1190  strcat (nam, "<");
1191  strcat (nam, TYPE_NAME (type));
1192  strcat (nam, " >");		/* FIXME, extra space still introduced in gcc? */
1193
1194  sym = lookup_symbol (nam, block, VAR_DOMAIN, 0, (struct symtab **) NULL);
1195
1196  if (sym == NULL)
1197    {
1198      error ("No template type named %s.", name);
1199    }
1200  if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
1201    {
1202      error ("This context has class, union or enum %s, not a struct.", name);
1203    }
1204  return (SYMBOL_TYPE (sym));
1205}
1206
1207/* Given a type TYPE, lookup the type of the component of type named NAME.
1208
1209   TYPE can be either a struct or union, or a pointer or reference to a struct or
1210   union.  If it is a pointer or reference, its target type is automatically used.
1211   Thus '.' and '->' are interchangable, as specified for the definitions of the
1212   expression element types STRUCTOP_STRUCT and STRUCTOP_PTR.
1213
1214   If NOERR is nonzero, return zero if NAME is not suitably defined.
1215   If NAME is the name of a baseclass type, return that type.  */
1216
1217struct type *
1218lookup_struct_elt_type (struct type *type, char *name, int noerr)
1219{
1220  int i;
1221
1222  for (;;)
1223    {
1224      CHECK_TYPEDEF (type);
1225      if (TYPE_CODE (type) != TYPE_CODE_PTR
1226	  && TYPE_CODE (type) != TYPE_CODE_REF)
1227	break;
1228      type = TYPE_TARGET_TYPE (type);
1229    }
1230
1231  if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
1232      TYPE_CODE (type) != TYPE_CODE_UNION)
1233    {
1234      target_terminal_ours ();
1235      gdb_flush (gdb_stdout);
1236      fprintf_unfiltered (gdb_stderr, "Type ");
1237      type_print (type, "", gdb_stderr, -1);
1238      error (" is not a structure or union type.");
1239    }
1240
1241#if 0
1242  /* FIXME:  This change put in by Michael seems incorrect for the case where
1243     the structure tag name is the same as the member name.  I.E. when doing
1244     "ptype bell->bar" for "struct foo { int bar; int foo; } bell;"
1245     Disabled by fnf. */
1246  {
1247    char *typename;
1248
1249    typename = type_name_no_tag (type);
1250    if (typename != NULL && strcmp (typename, name) == 0)
1251      return type;
1252  }
1253#endif
1254
1255  for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1256    {
1257      char *t_field_name = TYPE_FIELD_NAME (type, i);
1258
1259      if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1260	{
1261	  return TYPE_FIELD_TYPE (type, i);
1262	}
1263    }
1264
1265  /* OK, it's not in this class.  Recursively check the baseclasses.  */
1266  for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1267    {
1268      struct type *t;
1269
1270      t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, noerr);
1271      if (t != NULL)
1272	{
1273	  return t;
1274	}
1275    }
1276
1277  if (noerr)
1278    {
1279      return NULL;
1280    }
1281
1282  target_terminal_ours ();
1283  gdb_flush (gdb_stdout);
1284  fprintf_unfiltered (gdb_stderr, "Type ");
1285  type_print (type, "", gdb_stderr, -1);
1286  fprintf_unfiltered (gdb_stderr, " has no component named ");
1287  fputs_filtered (name, gdb_stderr);
1288  error (".");
1289  return (struct type *) -1;	/* For lint */
1290}
1291
1292/* If possible, make the vptr_fieldno and vptr_basetype fields of TYPE
1293   valid.  Callers should be aware that in some cases (for example,
1294   the type or one of its baseclasses is a stub type and we are
1295   debugging a .o file), this function will not be able to find the virtual
1296   function table pointer, and vptr_fieldno will remain -1 and vptr_basetype
1297   will remain NULL.  */
1298
1299void
1300fill_in_vptr_fieldno (struct type *type)
1301{
1302  CHECK_TYPEDEF (type);
1303
1304  if (TYPE_VPTR_FIELDNO (type) < 0)
1305    {
1306      int i;
1307
1308      /* We must start at zero in case the first (and only) baseclass is
1309         virtual (and hence we cannot share the table pointer).  */
1310      for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1311	{
1312	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1313	  fill_in_vptr_fieldno (baseclass);
1314	  if (TYPE_VPTR_FIELDNO (baseclass) >= 0)
1315	    {
1316	      TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (baseclass);
1317	      TYPE_VPTR_BASETYPE (type) = TYPE_VPTR_BASETYPE (baseclass);
1318	      break;
1319	    }
1320	}
1321    }
1322}
1323
1324/* Find the method and field indices for the destructor in class type T.
1325   Return 1 if the destructor was found, otherwise, return 0.  */
1326
1327int
1328get_destructor_fn_field (struct type *t, int *method_indexp, int *field_indexp)
1329{
1330  int i;
1331
1332  for (i = 0; i < TYPE_NFN_FIELDS (t); i++)
1333    {
1334      int j;
1335      struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1336
1337      for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (t, i); j++)
1338	{
1339	  if (is_destructor_name (TYPE_FN_FIELD_PHYSNAME (f, j)) != 0)
1340	    {
1341	      *method_indexp = i;
1342	      *field_indexp = j;
1343	      return 1;
1344	    }
1345	}
1346    }
1347  return 0;
1348}
1349
1350static void
1351stub_noname_complaint (void)
1352{
1353  complaint (&symfile_complaints, "stub type has NULL name");
1354}
1355
1356/* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
1357
1358   If this is a stubbed struct (i.e. declared as struct foo *), see if
1359   we can find a full definition in some other file. If so, copy this
1360   definition, so we can use it in future.  There used to be a comment (but
1361   not any code) that if we don't find a full definition, we'd set a flag
1362   so we don't spend time in the future checking the same type.  That would
1363   be a mistake, though--we might load in more symbols which contain a
1364   full definition for the type.
1365
1366   This used to be coded as a macro, but I don't think it is called
1367   often enough to merit such treatment.  */
1368
1369/* Find the real type of TYPE.  This function returns the real type, after
1370   removing all layers of typedefs and completing opaque or stub types.
1371   Completion changes the TYPE argument, but stripping of typedefs does
1372   not.  */
1373
1374struct type *
1375check_typedef (struct type *type)
1376{
1377  struct type *orig_type = type;
1378  int is_const, is_volatile, is_restrict;
1379
1380  while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1381    {
1382      if (!TYPE_TARGET_TYPE (type))
1383	{
1384	  char *name;
1385	  struct symbol *sym;
1386
1387	  /* It is dangerous to call lookup_symbol if we are currently
1388	     reading a symtab.  Infinite recursion is one danger. */
1389	  if (currently_reading_symtab)
1390	    return type;
1391
1392	  name = type_name_no_tag (type);
1393	  /* FIXME: shouldn't we separately check the TYPE_NAME and the
1394	     TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1395	     as appropriate?  (this code was written before TYPE_NAME and
1396	     TYPE_TAG_NAME were separate).  */
1397	  if (name == NULL)
1398	    {
1399	      stub_noname_complaint ();
1400	      return type;
1401	    }
1402	  sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0,
1403			       (struct symtab **) NULL);
1404	  if (sym)
1405	    TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
1406	  else
1407	    TYPE_TARGET_TYPE (type) = alloc_type (NULL);	/* TYPE_CODE_UNDEF */
1408	}
1409      type = TYPE_TARGET_TYPE (type);
1410    }
1411
1412  is_const = TYPE_CONST (type);
1413  is_volatile = TYPE_VOLATILE (type);
1414  is_restrict = TYPE_RESTRICT (type);
1415
1416  /* If this is a struct/class/union with no fields, then check whether a
1417     full definition exists somewhere else.  This is for systems where a
1418     type definition with no fields is issued for such types, instead of
1419     identifying them as stub types in the first place */
1420
1421  if (TYPE_IS_OPAQUE (type) && opaque_type_resolution && !currently_reading_symtab)
1422    {
1423      char *name = type_name_no_tag (type);
1424      struct type *newtype;
1425      if (name == NULL)
1426	{
1427	  stub_noname_complaint ();
1428	  return type;
1429	}
1430      newtype = lookup_transparent_type (name);
1431      if (newtype)
1432	make_cvr_type (is_const, is_volatile, is_restrict, newtype, &type);
1433    }
1434  /* Otherwise, rely on the stub flag being set for opaque/stubbed types */
1435  else if (TYPE_STUB (type) && !currently_reading_symtab)
1436    {
1437      char *name = type_name_no_tag (type);
1438      /* FIXME: shouldn't we separately check the TYPE_NAME and the
1439         TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN
1440         as appropriate?  (this code was written before TYPE_NAME and
1441         TYPE_TAG_NAME were separate).  */
1442      struct symbol *sym;
1443      if (name == NULL)
1444	{
1445	  stub_noname_complaint ();
1446	  return type;
1447	}
1448      sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0, (struct symtab **) NULL);
1449      if (sym)
1450	make_cvr_type (is_const, is_volatile, is_restrict, SYMBOL_TYPE (sym),
1451                       &type);
1452    }
1453
1454  if (TYPE_TARGET_STUB (type))
1455    {
1456      struct type *range_type;
1457      struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1458
1459      if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
1460	{
1461	}
1462      else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
1463	       && TYPE_NFIELDS (type) == 1
1464	       && (TYPE_CODE (range_type = TYPE_FIELD_TYPE (type, 0))
1465		   == TYPE_CODE_RANGE))
1466	{
1467	  /* Now recompute the length of the array type, based on its
1468	     number of elements and the target type's length.  */
1469	  TYPE_LENGTH (type) =
1470	    ((TYPE_FIELD_BITPOS (range_type, 1)
1471	      - TYPE_FIELD_BITPOS (range_type, 0)
1472	      + 1)
1473	     * TYPE_LENGTH (target_type));
1474	  TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1475	}
1476      else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
1477	{
1478	  TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
1479	  TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
1480	}
1481    }
1482  /* Cache TYPE_LENGTH for future use. */
1483  TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
1484  return type;
1485}
1486
1487/* Parse a type expression in the string [P..P+LENGTH).  If an error occurs,
1488   silently return builtin_type_void. */
1489
1490static struct type *
1491safe_parse_type (char *p, int length)
1492{
1493  struct ui_file *saved_gdb_stderr;
1494  struct type *type;
1495
1496  /* Suppress error messages. */
1497  saved_gdb_stderr = gdb_stderr;
1498  gdb_stderr = ui_file_new ();
1499
1500  /* Call parse_and_eval_type() without fear of longjmp()s. */
1501  if (!gdb_parse_and_eval_type (p, length, &type))
1502    type = builtin_type_void;
1503
1504  /* Stop suppressing error messages. */
1505  ui_file_delete (gdb_stderr);
1506  gdb_stderr = saved_gdb_stderr;
1507
1508  return type;
1509}
1510
1511/* Ugly hack to convert method stubs into method types.
1512
1513   He ain't kiddin'.  This demangles the name of the method into a string
1514   including argument types, parses out each argument type, generates
1515   a string casting a zero to that type, evaluates the string, and stuffs
1516   the resulting type into an argtype vector!!!  Then it knows the type
1517   of the whole function (including argument types for overloading),
1518   which info used to be in the stab's but was removed to hack back
1519   the space required for them.  */
1520
1521static void
1522check_stub_method (struct type *type, int method_id, int signature_id)
1523{
1524  struct fn_field *f;
1525  char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
1526  char *demangled_name = cplus_demangle (mangled_name,
1527					 DMGL_PARAMS | DMGL_ANSI);
1528  char *argtypetext, *p;
1529  int depth = 0, argcount = 1;
1530  struct field *argtypes;
1531  struct type *mtype;
1532
1533  /* Make sure we got back a function string that we can use.  */
1534  if (demangled_name)
1535    p = strchr (demangled_name, '(');
1536  else
1537    p = NULL;
1538
1539  if (demangled_name == NULL || p == NULL)
1540    error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
1541
1542  /* Now, read in the parameters that define this type.  */
1543  p += 1;
1544  argtypetext = p;
1545  while (*p)
1546    {
1547      if (*p == '(' || *p == '<')
1548	{
1549	  depth += 1;
1550	}
1551      else if (*p == ')' || *p == '>')
1552	{
1553	  depth -= 1;
1554	}
1555      else if (*p == ',' && depth == 0)
1556	{
1557	  argcount += 1;
1558	}
1559
1560      p += 1;
1561    }
1562
1563  /* If we read one argument and it was ``void'', don't count it.  */
1564  if (strncmp (argtypetext, "(void)", 6) == 0)
1565    argcount -= 1;
1566
1567  /* We need one extra slot, for the THIS pointer.  */
1568
1569  argtypes = (struct field *)
1570    TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
1571  p = argtypetext;
1572
1573  /* Add THIS pointer for non-static methods.  */
1574  f = TYPE_FN_FIELDLIST1 (type, method_id);
1575  if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
1576    argcount = 0;
1577  else
1578    {
1579      argtypes[0].type = lookup_pointer_type (type);
1580      argcount = 1;
1581    }
1582
1583  if (*p != ')')		/* () means no args, skip while */
1584    {
1585      depth = 0;
1586      while (*p)
1587	{
1588	  if (depth <= 0 && (*p == ',' || *p == ')'))
1589	    {
1590	      /* Avoid parsing of ellipsis, they will be handled below.
1591	         Also avoid ``void'' as above.  */
1592	      if (strncmp (argtypetext, "...", p - argtypetext) != 0
1593		  && strncmp (argtypetext, "void", p - argtypetext) != 0)
1594		{
1595		  argtypes[argcount].type =
1596		    safe_parse_type (argtypetext, p - argtypetext);
1597		  argcount += 1;
1598		}
1599	      argtypetext = p + 1;
1600	    }
1601
1602	  if (*p == '(' || *p == '<')
1603	    {
1604	      depth += 1;
1605	    }
1606	  else if (*p == ')' || *p == '>')
1607	    {
1608	      depth -= 1;
1609	    }
1610
1611	  p += 1;
1612	}
1613    }
1614
1615  TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
1616
1617  /* Now update the old "stub" type into a real type.  */
1618  mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
1619  TYPE_DOMAIN_TYPE (mtype) = type;
1620  TYPE_FIELDS (mtype) = argtypes;
1621  TYPE_NFIELDS (mtype) = argcount;
1622  TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
1623  TYPE_FN_FIELD_STUB (f, signature_id) = 0;
1624  if (p[-2] == '.')
1625    TYPE_FLAGS (mtype) |= TYPE_FLAG_VARARGS;
1626
1627  xfree (demangled_name);
1628}
1629
1630/* This is the external interface to check_stub_method, above.  This function
1631   unstubs all of the signatures for TYPE's METHOD_ID method name.  After
1632   calling this function TYPE_FN_FIELD_STUB will be cleared for each signature
1633   and TYPE_FN_FIELDLIST_NAME will be correct.
1634
1635   This function unfortunately can not die until stabs do.  */
1636
1637void
1638check_stub_method_group (struct type *type, int method_id)
1639{
1640  int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
1641  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
1642  int j, found_stub = 0;
1643
1644  for (j = 0; j < len; j++)
1645    if (TYPE_FN_FIELD_STUB (f, j))
1646      {
1647	found_stub = 1;
1648	check_stub_method (type, method_id, j);
1649      }
1650
1651  /* GNU v3 methods with incorrect names were corrected when we read in
1652     type information, because it was cheaper to do it then.  The only GNU v2
1653     methods with incorrect method names are operators and destructors;
1654     destructors were also corrected when we read in type information.
1655
1656     Therefore the only thing we need to handle here are v2 operator
1657     names.  */
1658  if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0)
1659    {
1660      int ret;
1661      char dem_opname[256];
1662
1663      ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, method_id),
1664				   dem_opname, DMGL_ANSI);
1665      if (!ret)
1666	ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, method_id),
1667				     dem_opname, 0);
1668      if (ret)
1669	TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname);
1670    }
1671}
1672
1673const struct cplus_struct_type cplus_struct_default;
1674
1675void
1676allocate_cplus_struct_type (struct type *type)
1677{
1678  if (!HAVE_CPLUS_STRUCT (type))
1679    {
1680      TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1681	TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1682      *(TYPE_CPLUS_SPECIFIC (type)) = cplus_struct_default;
1683    }
1684}
1685
1686/* Helper function to initialize the standard scalar types.
1687
1688   If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
1689   of the string pointed to by name in the objfile_obstack for that objfile,
1690   and initialize the type name to that copy.  There are places (mipsread.c
1691   in particular, where init_type is called with a NULL value for NAME). */
1692
1693struct type *
1694init_type (enum type_code code, int length, int flags, char *name,
1695	   struct objfile *objfile)
1696{
1697  struct type *type;
1698
1699  type = alloc_type (objfile);
1700  TYPE_CODE (type) = code;
1701  TYPE_LENGTH (type) = length;
1702  TYPE_FLAGS (type) |= flags;
1703  if ((name != NULL) && (objfile != NULL))
1704    {
1705      TYPE_NAME (type) =
1706	obsavestring (name, strlen (name), &objfile->objfile_obstack);
1707    }
1708  else
1709    {
1710      TYPE_NAME (type) = name;
1711    }
1712
1713  /* C++ fancies.  */
1714
1715  if (name && strcmp (name, "char") == 0)
1716    TYPE_FLAGS (type) |= TYPE_FLAG_NOSIGN;
1717
1718  if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
1719      || code == TYPE_CODE_NAMESPACE)
1720    {
1721      INIT_CPLUS_SPECIFIC (type);
1722    }
1723  return (type);
1724}
1725
1726/* Helper function.  Create an empty composite type.  */
1727
1728struct type *
1729init_composite_type (char *name, enum type_code code)
1730{
1731  struct type *t;
1732  gdb_assert (code == TYPE_CODE_STRUCT
1733	      || code == TYPE_CODE_UNION);
1734  t = init_type (code, 0, 0, NULL, NULL);
1735  TYPE_TAG_NAME (t) = name;
1736  return t;
1737}
1738
1739/* Helper function.  Append a field to a composite type.  */
1740
1741void
1742append_composite_type_field (struct type *t, char *name, struct type *field)
1743{
1744  struct field *f;
1745  TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
1746  TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t),
1747			      sizeof (struct field) * TYPE_NFIELDS (t));
1748  f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
1749  memset (f, 0, sizeof f[0]);
1750  FIELD_TYPE (f[0]) = field;
1751  FIELD_NAME (f[0]) = name;
1752  if (TYPE_CODE (t) == TYPE_CODE_UNION)
1753    {
1754      if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
1755	TYPE_LENGTH (t) = TYPE_LENGTH (field);
1756    }
1757  else if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
1758    {
1759      TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
1760      if (TYPE_NFIELDS (t) > 1)
1761	{
1762	  FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1])
1763				 + TYPE_LENGTH (field) * TARGET_CHAR_BIT);
1764	}
1765    }
1766}
1767
1768/* Look up a fundamental type for the specified objfile.
1769   May need to construct such a type if this is the first use.
1770
1771   Some object file formats (ELF, COFF, etc) do not define fundamental
1772   types such as "int" or "double".  Others (stabs for example), do
1773   define fundamental types.
1774
1775   For the formats which don't provide fundamental types, gdb can create
1776   such types, using defaults reasonable for the current language and
1777   the current target machine.
1778
1779   NOTE:  This routine is obsolescent.  Each debugging format reader
1780   should manage it's own fundamental types, either creating them from
1781   suitable defaults or reading them from the debugging information,
1782   whichever is appropriate.  The DWARF reader has already been
1783   fixed to do this.  Once the other readers are fixed, this routine
1784   will go away.  Also note that fundamental types should be managed
1785   on a compilation unit basis in a multi-language environment, not
1786   on a linkage unit basis as is done here. */
1787
1788
1789struct type *
1790lookup_fundamental_type (struct objfile *objfile, int typeid)
1791{
1792  struct type **typep;
1793  int nbytes;
1794
1795  if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
1796    {
1797      error ("internal error - invalid fundamental type id %d", typeid);
1798    }
1799
1800  /* If this is the first time we need a fundamental type for this objfile
1801     then we need to initialize the vector of type pointers. */
1802
1803  if (objfile->fundamental_types == NULL)
1804    {
1805      nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
1806      objfile->fundamental_types = (struct type **)
1807	obstack_alloc (&objfile->objfile_obstack, nbytes);
1808      memset ((char *) objfile->fundamental_types, 0, nbytes);
1809      OBJSTAT (objfile, n_types += FT_NUM_MEMBERS);
1810    }
1811
1812  /* Look for this particular type in the fundamental type vector.  If one is
1813     not found, create and install one appropriate for the current language. */
1814
1815  typep = objfile->fundamental_types + typeid;
1816  if (*typep == NULL)
1817    {
1818      *typep = create_fundamental_type (objfile, typeid);
1819    }
1820
1821  return (*typep);
1822}
1823
1824int
1825can_dereference (struct type *t)
1826{
1827  /* FIXME: Should we return true for references as well as pointers?  */
1828  CHECK_TYPEDEF (t);
1829  return
1830    (t != NULL
1831     && TYPE_CODE (t) == TYPE_CODE_PTR
1832     && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1833}
1834
1835int
1836is_integral_type (struct type *t)
1837{
1838  CHECK_TYPEDEF (t);
1839  return
1840    ((t != NULL)
1841     && ((TYPE_CODE (t) == TYPE_CODE_INT)
1842	 || (TYPE_CODE (t) == TYPE_CODE_ENUM)
1843	 || (TYPE_CODE (t) == TYPE_CODE_CHAR)
1844	 || (TYPE_CODE (t) == TYPE_CODE_RANGE)
1845	 || (TYPE_CODE (t) == TYPE_CODE_BOOL)));
1846}
1847
1848/* Check whether BASE is an ancestor or base class or DCLASS
1849   Return 1 if so, and 0 if not.
1850   Note: callers may want to check for identity of the types before
1851   calling this function -- identical types are considered to satisfy
1852   the ancestor relationship even if they're identical */
1853
1854int
1855is_ancestor (struct type *base, struct type *dclass)
1856{
1857  int i;
1858
1859  CHECK_TYPEDEF (base);
1860  CHECK_TYPEDEF (dclass);
1861
1862  if (base == dclass)
1863    return 1;
1864  if (TYPE_NAME (base) && TYPE_NAME (dclass) &&
1865      !strcmp (TYPE_NAME (base), TYPE_NAME (dclass)))
1866    return 1;
1867
1868  for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1869    if (is_ancestor (base, TYPE_BASECLASS (dclass, i)))
1870      return 1;
1871
1872  return 0;
1873}
1874
1875
1876
1877/* See whether DCLASS has a virtual table.  This routine is aimed at
1878   the HP/Taligent ANSI C++ runtime model, and may not work with other
1879   runtime models.  Return 1 => Yes, 0 => No.  */
1880
1881int
1882has_vtable (struct type *dclass)
1883{
1884  /* In the HP ANSI C++ runtime model, a class has a vtable only if it
1885     has virtual functions or virtual bases.  */
1886
1887  int i;
1888
1889  if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1890    return 0;
1891
1892  /* First check for the presence of virtual bases */
1893  if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1894    for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1895      if (B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i))
1896	return 1;
1897
1898  /* Next check for virtual functions */
1899  if (TYPE_FN_FIELDLISTS (dclass))
1900    for (i = 0; i < TYPE_NFN_FIELDS (dclass); i++)
1901      if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, i), 0))
1902	return 1;
1903
1904  /* Recurse on non-virtual bases to see if any of them needs a vtable */
1905  if (TYPE_FIELD_VIRTUAL_BITS (dclass))
1906    for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1907      if ((!B_TST (TYPE_FIELD_VIRTUAL_BITS (dclass), i)) &&
1908	  (has_vtable (TYPE_FIELD_TYPE (dclass, i))))
1909	return 1;
1910
1911  /* Well, maybe we don't need a virtual table */
1912  return 0;
1913}
1914
1915/* Return a pointer to the "primary base class" of DCLASS.
1916
1917   A NULL return indicates that DCLASS has no primary base, or that it
1918   couldn't be found (insufficient information).
1919
1920   This routine is aimed at the HP/Taligent ANSI C++ runtime model,
1921   and may not work with other runtime models.  */
1922
1923struct type *
1924primary_base_class (struct type *dclass)
1925{
1926  /* In HP ANSI C++'s runtime model, a "primary base class" of a class
1927     is the first directly inherited, non-virtual base class that
1928     requires a virtual table */
1929
1930  int i;
1931
1932  if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1933    return NULL;
1934
1935  for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1936    if (!TYPE_FIELD_VIRTUAL (dclass, i) &&
1937	has_vtable (TYPE_FIELD_TYPE (dclass, i)))
1938      return TYPE_FIELD_TYPE (dclass, i);
1939
1940  return NULL;
1941}
1942
1943/* Global manipulated by virtual_base_list[_aux]() */
1944
1945static struct vbase *current_vbase_list = NULL;
1946
1947/* Return a pointer to a null-terminated list of struct vbase
1948   items. The vbasetype pointer of each item in the list points to the
1949   type information for a virtual base of the argument DCLASS.
1950
1951   Helper function for virtual_base_list().
1952   Note: the list goes backward, right-to-left. virtual_base_list()
1953   copies the items out in reverse order.  */
1954
1955static void
1956virtual_base_list_aux (struct type *dclass)
1957{
1958  struct vbase *tmp_vbase;
1959  int i;
1960
1961  if (TYPE_CODE (dclass) != TYPE_CODE_CLASS)
1962    return;
1963
1964  for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
1965    {
1966      /* Recurse on this ancestor, first */
1967      virtual_base_list_aux (TYPE_FIELD_TYPE (dclass, i));
1968
1969      /* If this current base is itself virtual, add it to the list */
1970      if (BASETYPE_VIA_VIRTUAL (dclass, i))
1971	{
1972	  struct type *basetype = TYPE_FIELD_TYPE (dclass, i);
1973
1974	  /* Check if base already recorded */
1975	  tmp_vbase = current_vbase_list;
1976	  while (tmp_vbase)
1977	    {
1978	      if (tmp_vbase->vbasetype == basetype)
1979		break;		/* found it */
1980	      tmp_vbase = tmp_vbase->next;
1981	    }
1982
1983	  if (!tmp_vbase)	/* normal exit from loop */
1984	    {
1985	      /* Allocate new item for this virtual base */
1986	      tmp_vbase = (struct vbase *) xmalloc (sizeof (struct vbase));
1987
1988	      /* Stick it on at the end of the list */
1989	      tmp_vbase->vbasetype = basetype;
1990	      tmp_vbase->next = current_vbase_list;
1991	      current_vbase_list = tmp_vbase;
1992	    }
1993	}			/* if virtual */
1994    }				/* for loop over bases */
1995}
1996
1997
1998/* Compute the list of virtual bases in the right order.  Virtual
1999   bases are laid out in the object's memory area in order of their
2000   occurrence in a depth-first, left-to-right search through the
2001   ancestors.
2002
2003   Argument DCLASS is the type whose virtual bases are required.
2004   Return value is the address of a null-terminated array of pointers
2005   to struct type items.
2006
2007   This routine is aimed at the HP/Taligent ANSI C++ runtime model,
2008   and may not work with other runtime models.
2009
2010   This routine merely hands off the argument to virtual_base_list_aux()
2011   and then copies the result into an array to save space.  */
2012
2013struct type **
2014virtual_base_list (struct type *dclass)
2015{
2016  struct vbase *tmp_vbase;
2017  struct vbase *tmp_vbase_2;
2018  int i;
2019  int count;
2020  struct type **vbase_array;
2021
2022  current_vbase_list = NULL;
2023  virtual_base_list_aux (dclass);
2024
2025  for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
2026    /* no body */ ;
2027
2028  count = i;
2029
2030  vbase_array = (struct type **) xmalloc ((count + 1) * sizeof (struct type *));
2031
2032  for (i = count - 1, tmp_vbase = current_vbase_list; i >= 0; i--, tmp_vbase = tmp_vbase->next)
2033    vbase_array[i] = tmp_vbase->vbasetype;
2034
2035  /* Get rid of constructed chain */
2036  tmp_vbase_2 = tmp_vbase = current_vbase_list;
2037  while (tmp_vbase)
2038    {
2039      tmp_vbase = tmp_vbase->next;
2040      xfree (tmp_vbase_2);
2041      tmp_vbase_2 = tmp_vbase;
2042    }
2043
2044  vbase_array[count] = NULL;
2045  return vbase_array;
2046}
2047
2048/* Return the length of the virtual base list of the type DCLASS.  */
2049
2050int
2051virtual_base_list_length (struct type *dclass)
2052{
2053  int i;
2054  struct vbase *tmp_vbase;
2055
2056  current_vbase_list = NULL;
2057  virtual_base_list_aux (dclass);
2058
2059  for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; i++, tmp_vbase = tmp_vbase->next)
2060    /* no body */ ;
2061  return i;
2062}
2063
2064/* Return the number of elements of the virtual base list of the type
2065   DCLASS, ignoring those appearing in the primary base (and its
2066   primary base, recursively).  */
2067
2068int
2069virtual_base_list_length_skip_primaries (struct type *dclass)
2070{
2071  int i;
2072  struct vbase *tmp_vbase;
2073  struct type *primary;
2074
2075  primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
2076
2077  if (!primary)
2078    return virtual_base_list_length (dclass);
2079
2080  current_vbase_list = NULL;
2081  virtual_base_list_aux (dclass);
2082
2083  for (i = 0, tmp_vbase = current_vbase_list; tmp_vbase != NULL; tmp_vbase = tmp_vbase->next)
2084    {
2085      if (virtual_base_index (tmp_vbase->vbasetype, primary) >= 0)
2086	continue;
2087      i++;
2088    }
2089  return i;
2090}
2091
2092
2093/* Return the index (position) of type BASE, which is a virtual base
2094   class of DCLASS, in the latter's virtual base list.  A return of -1
2095   indicates "not found" or a problem.  */
2096
2097int
2098virtual_base_index (struct type *base, struct type *dclass)
2099{
2100  struct type *vbase;
2101  int i;
2102
2103  if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
2104      (TYPE_CODE (base) != TYPE_CODE_CLASS))
2105    return -1;
2106
2107  i = 0;
2108  vbase = virtual_base_list (dclass)[0];
2109  while (vbase)
2110    {
2111      if (vbase == base)
2112	break;
2113      vbase = virtual_base_list (dclass)[++i];
2114    }
2115
2116  return vbase ? i : -1;
2117}
2118
2119
2120
2121/* Return the index (position) of type BASE, which is a virtual base
2122   class of DCLASS, in the latter's virtual base list. Skip over all
2123   bases that may appear in the virtual base list of the primary base
2124   class of DCLASS (recursively).  A return of -1 indicates "not
2125   found" or a problem.  */
2126
2127int
2128virtual_base_index_skip_primaries (struct type *base, struct type *dclass)
2129{
2130  struct type *vbase;
2131  int i, j;
2132  struct type *primary;
2133
2134  if ((TYPE_CODE (dclass) != TYPE_CODE_CLASS) ||
2135      (TYPE_CODE (base) != TYPE_CODE_CLASS))
2136    return -1;
2137
2138  primary = TYPE_RUNTIME_PTR (dclass) ? TYPE_PRIMARY_BASE (dclass) : NULL;
2139
2140  j = -1;
2141  i = 0;
2142  vbase = virtual_base_list (dclass)[0];
2143  while (vbase)
2144    {
2145      if (!primary || (virtual_base_index_skip_primaries (vbase, primary) < 0))
2146	j++;
2147      if (vbase == base)
2148	break;
2149      vbase = virtual_base_list (dclass)[++i];
2150    }
2151
2152  return vbase ? j : -1;
2153}
2154
2155/* Return position of a derived class DCLASS in the list of
2156 * primary bases starting with the remotest ancestor.
2157 * Position returned is 0-based. */
2158
2159int
2160class_index_in_primary_list (struct type *dclass)
2161{
2162  struct type *pbc;		/* primary base class */
2163
2164  /* Simply recurse on primary base */
2165  pbc = TYPE_PRIMARY_BASE (dclass);
2166  if (pbc)
2167    return 1 + class_index_in_primary_list (pbc);
2168  else
2169    return 0;
2170}
2171
2172/* Return a count of the number of virtual functions a type has.
2173 * This includes all the virtual functions it inherits from its
2174 * base classes too.
2175 */
2176
2177/* pai: FIXME This doesn't do the right thing: count redefined virtual
2178 * functions only once (latest redefinition)
2179 */
2180
2181int
2182count_virtual_fns (struct type *dclass)
2183{
2184  int fn, oi;			/* function and overloaded instance indices */
2185  int vfuncs;			/* count to return */
2186
2187  /* recurse on bases that can share virtual table */
2188  struct type *pbc = primary_base_class (dclass);
2189  if (pbc)
2190    vfuncs = count_virtual_fns (pbc);
2191  else
2192    vfuncs = 0;
2193
2194  for (fn = 0; fn < TYPE_NFN_FIELDS (dclass); fn++)
2195    for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (dclass, fn); oi++)
2196      if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (dclass, fn), oi))
2197	vfuncs++;
2198
2199  return vfuncs;
2200}
2201
2202
2203
2204/* Functions for overload resolution begin here */
2205
2206/* Compare two badness vectors A and B and return the result.
2207 * 0 => A and B are identical
2208 * 1 => A and B are incomparable
2209 * 2 => A is better than B
2210 * 3 => A is worse than B */
2211
2212int
2213compare_badness (struct badness_vector *a, struct badness_vector *b)
2214{
2215  int i;
2216  int tmp;
2217  short found_pos = 0;		/* any positives in c? */
2218  short found_neg = 0;		/* any negatives in c? */
2219
2220  /* differing lengths => incomparable */
2221  if (a->length != b->length)
2222    return 1;
2223
2224  /* Subtract b from a */
2225  for (i = 0; i < a->length; i++)
2226    {
2227      tmp = a->rank[i] - b->rank[i];
2228      if (tmp > 0)
2229	found_pos = 1;
2230      else if (tmp < 0)
2231	found_neg = 1;
2232    }
2233
2234  if (found_pos)
2235    {
2236      if (found_neg)
2237	return 1;		/* incomparable */
2238      else
2239	return 3;		/* A > B */
2240    }
2241  else
2242    /* no positives */
2243    {
2244      if (found_neg)
2245	return 2;		/* A < B */
2246      else
2247	return 0;		/* A == B */
2248    }
2249}
2250
2251/* Rank a function by comparing its parameter types (PARMS, length NPARMS),
2252 * to the types of an argument list (ARGS, length NARGS).
2253 * Return a pointer to a badness vector. This has NARGS + 1 entries. */
2254
2255struct badness_vector *
2256rank_function (struct type **parms, int nparms, struct type **args, int nargs)
2257{
2258  int i;
2259  struct badness_vector *bv;
2260  int min_len = nparms < nargs ? nparms : nargs;
2261
2262  bv = xmalloc (sizeof (struct badness_vector));
2263  bv->length = nargs + 1;	/* add 1 for the length-match rank */
2264  bv->rank = xmalloc ((nargs + 1) * sizeof (int));
2265
2266  /* First compare the lengths of the supplied lists.
2267   * If there is a mismatch, set it to a high value. */
2268
2269  /* pai/1997-06-03 FIXME: when we have debug info about default
2270   * arguments and ellipsis parameter lists, we should consider those
2271   * and rank the length-match more finely. */
2272
2273  LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0;
2274
2275  /* Now rank all the parameters of the candidate function */
2276  for (i = 1; i <= min_len; i++)
2277    bv->rank[i] = rank_one_type (parms[i-1], args[i-1]);
2278
2279  /* If more arguments than parameters, add dummy entries */
2280  for (i = min_len + 1; i <= nargs; i++)
2281    bv->rank[i] = TOO_FEW_PARAMS_BADNESS;
2282
2283  return bv;
2284}
2285
2286/* Compare the names of two integer types, assuming that any sign
2287   qualifiers have been checked already.  We do it this way because
2288   there may be an "int" in the name of one of the types.  */
2289
2290static int
2291integer_types_same_name_p (const char *first, const char *second)
2292{
2293  int first_p, second_p;
2294
2295  /* If both are shorts, return 1; if neither is a short, keep checking.  */
2296  first_p = (strstr (first, "short") != NULL);
2297  second_p = (strstr (second, "short") != NULL);
2298  if (first_p && second_p)
2299    return 1;
2300  if (first_p || second_p)
2301    return 0;
2302
2303  /* Likewise for long.  */
2304  first_p = (strstr (first, "long") != NULL);
2305  second_p = (strstr (second, "long") != NULL);
2306  if (first_p && second_p)
2307    return 1;
2308  if (first_p || second_p)
2309    return 0;
2310
2311  /* Likewise for char.  */
2312  first_p = (strstr (first, "char") != NULL);
2313  second_p = (strstr (second, "char") != NULL);
2314  if (first_p && second_p)
2315    return 1;
2316  if (first_p || second_p)
2317    return 0;
2318
2319  /* They must both be ints.  */
2320  return 1;
2321}
2322
2323/* Compare one type (PARM) for compatibility with another (ARG).
2324 * PARM is intended to be the parameter type of a function; and
2325 * ARG is the supplied argument's type.  This function tests if
2326 * the latter can be converted to the former.
2327 *
2328 * Return 0 if they are identical types;
2329 * Otherwise, return an integer which corresponds to how compatible
2330 * PARM is to ARG. The higher the return value, the worse the match.
2331 * Generally the "bad" conversions are all uniformly assigned a 100 */
2332
2333int
2334rank_one_type (struct type *parm, struct type *arg)
2335{
2336  /* Identical type pointers */
2337  /* However, this still doesn't catch all cases of same type for arg
2338   * and param. The reason is that builtin types are different from
2339   * the same ones constructed from the object. */
2340  if (parm == arg)
2341    return 0;
2342
2343  /* Resolve typedefs */
2344  if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF)
2345    parm = check_typedef (parm);
2346  if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF)
2347    arg = check_typedef (arg);
2348
2349  /*
2350     Well, damnit, if the names are exactly the same,
2351     i'll say they are exactly the same. This happens when we generate
2352     method stubs. The types won't point to the same address, but they
2353     really are the same.
2354  */
2355
2356  if (TYPE_NAME (parm) && TYPE_NAME (arg) &&
2357      !strcmp (TYPE_NAME (parm), TYPE_NAME (arg)))
2358      return 0;
2359
2360  /* Check if identical after resolving typedefs */
2361  if (parm == arg)
2362    return 0;
2363
2364  /* See through references, since we can almost make non-references
2365     references. */
2366  if (TYPE_CODE (arg) == TYPE_CODE_REF)
2367    return (rank_one_type (parm, TYPE_TARGET_TYPE (arg))
2368	    + REFERENCE_CONVERSION_BADNESS);
2369  if (TYPE_CODE (parm) == TYPE_CODE_REF)
2370    return (rank_one_type (TYPE_TARGET_TYPE (parm), arg)
2371	    + REFERENCE_CONVERSION_BADNESS);
2372  if (overload_debug)
2373  /* Debugging only. */
2374    fprintf_filtered (gdb_stderr,"------ Arg is %s [%d], parm is %s [%d]\n",
2375        TYPE_NAME (arg), TYPE_CODE (arg), TYPE_NAME (parm), TYPE_CODE (parm));
2376
2377  /* x -> y means arg of type x being supplied for parameter of type y */
2378
2379  switch (TYPE_CODE (parm))
2380    {
2381    case TYPE_CODE_PTR:
2382      switch (TYPE_CODE (arg))
2383	{
2384	case TYPE_CODE_PTR:
2385	  if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID)
2386	    return VOID_PTR_CONVERSION_BADNESS;
2387	  else
2388	    return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2389	case TYPE_CODE_ARRAY:
2390	  return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2391	case TYPE_CODE_FUNC:
2392	  return rank_one_type (TYPE_TARGET_TYPE (parm), arg);
2393	case TYPE_CODE_INT:
2394	case TYPE_CODE_ENUM:
2395	case TYPE_CODE_CHAR:
2396	case TYPE_CODE_RANGE:
2397	case TYPE_CODE_BOOL:
2398	  return POINTER_CONVERSION_BADNESS;
2399	default:
2400	  return INCOMPATIBLE_TYPE_BADNESS;
2401	}
2402    case TYPE_CODE_ARRAY:
2403      switch (TYPE_CODE (arg))
2404	{
2405	case TYPE_CODE_PTR:
2406	case TYPE_CODE_ARRAY:
2407	  return rank_one_type (TYPE_TARGET_TYPE (parm), TYPE_TARGET_TYPE (arg));
2408	default:
2409	  return INCOMPATIBLE_TYPE_BADNESS;
2410	}
2411    case TYPE_CODE_FUNC:
2412      switch (TYPE_CODE (arg))
2413	{
2414	case TYPE_CODE_PTR:	/* funcptr -> func */
2415	  return rank_one_type (parm, TYPE_TARGET_TYPE (arg));
2416	default:
2417	  return INCOMPATIBLE_TYPE_BADNESS;
2418	}
2419    case TYPE_CODE_INT:
2420      switch (TYPE_CODE (arg))
2421	{
2422	case TYPE_CODE_INT:
2423	  if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2424	    {
2425	      /* Deal with signed, unsigned, and plain chars and
2426	         signed and unsigned ints */
2427	      if (TYPE_NOSIGN (parm))
2428		{
2429		  /* This case only for character types */
2430		  if (TYPE_NOSIGN (arg))	/* plain char -> plain char */
2431		    return 0;
2432		  else
2433		    return INTEGER_CONVERSION_BADNESS;	/* signed/unsigned char -> plain char */
2434		}
2435	      else if (TYPE_UNSIGNED (parm))
2436		{
2437		  if (TYPE_UNSIGNED (arg))
2438		    {
2439		      /* unsigned int -> unsigned int, or unsigned long -> unsigned long */
2440		      if (integer_types_same_name_p (TYPE_NAME (parm), TYPE_NAME (arg)))
2441			return 0;
2442		      else if (integer_types_same_name_p (TYPE_NAME (arg), "int")
2443			       && integer_types_same_name_p (TYPE_NAME (parm), "long"))
2444			return INTEGER_PROMOTION_BADNESS;	/* unsigned int -> unsigned long */
2445		      else
2446			return INTEGER_CONVERSION_BADNESS;	/* unsigned long -> unsigned int */
2447		    }
2448		  else
2449		    {
2450		      if (integer_types_same_name_p (TYPE_NAME (arg), "long")
2451			  && integer_types_same_name_p (TYPE_NAME (parm), "int"))
2452			return INTEGER_CONVERSION_BADNESS;	/* signed long -> unsigned int */
2453		      else
2454			return INTEGER_CONVERSION_BADNESS;	/* signed int/long -> unsigned int/long */
2455		    }
2456		}
2457	      else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2458		{
2459		  if (integer_types_same_name_p (TYPE_NAME (parm), TYPE_NAME (arg)))
2460		    return 0;
2461		  else if (integer_types_same_name_p (TYPE_NAME (arg), "int")
2462			   && integer_types_same_name_p (TYPE_NAME (parm), "long"))
2463		    return INTEGER_PROMOTION_BADNESS;
2464		  else
2465		    return INTEGER_CONVERSION_BADNESS;
2466		}
2467	      else
2468		return INTEGER_CONVERSION_BADNESS;
2469	    }
2470	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2471	    return INTEGER_PROMOTION_BADNESS;
2472	  else
2473	    return INTEGER_CONVERSION_BADNESS;
2474	case TYPE_CODE_ENUM:
2475	case TYPE_CODE_CHAR:
2476	case TYPE_CODE_RANGE:
2477	case TYPE_CODE_BOOL:
2478	  return INTEGER_PROMOTION_BADNESS;
2479	case TYPE_CODE_FLT:
2480	  return INT_FLOAT_CONVERSION_BADNESS;
2481	case TYPE_CODE_PTR:
2482	  return NS_POINTER_CONVERSION_BADNESS;
2483	default:
2484	  return INCOMPATIBLE_TYPE_BADNESS;
2485	}
2486      break;
2487    case TYPE_CODE_ENUM:
2488      switch (TYPE_CODE (arg))
2489	{
2490	case TYPE_CODE_INT:
2491	case TYPE_CODE_CHAR:
2492	case TYPE_CODE_RANGE:
2493	case TYPE_CODE_BOOL:
2494	case TYPE_CODE_ENUM:
2495	  return INTEGER_CONVERSION_BADNESS;
2496	case TYPE_CODE_FLT:
2497	  return INT_FLOAT_CONVERSION_BADNESS;
2498	default:
2499	  return INCOMPATIBLE_TYPE_BADNESS;
2500	}
2501      break;
2502    case TYPE_CODE_CHAR:
2503      switch (TYPE_CODE (arg))
2504	{
2505	case TYPE_CODE_RANGE:
2506	case TYPE_CODE_BOOL:
2507	case TYPE_CODE_ENUM:
2508	  return INTEGER_CONVERSION_BADNESS;
2509	case TYPE_CODE_FLT:
2510	  return INT_FLOAT_CONVERSION_BADNESS;
2511	case TYPE_CODE_INT:
2512	  if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
2513	    return INTEGER_CONVERSION_BADNESS;
2514	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2515	    return INTEGER_PROMOTION_BADNESS;
2516	  /* >>> !! else fall through !! <<< */
2517	case TYPE_CODE_CHAR:
2518	  /* Deal with signed, unsigned, and plain chars for C++
2519	     and with int cases falling through from previous case */
2520	  if (TYPE_NOSIGN (parm))
2521	    {
2522	      if (TYPE_NOSIGN (arg))
2523		return 0;
2524	      else
2525		return INTEGER_CONVERSION_BADNESS;
2526	    }
2527	  else if (TYPE_UNSIGNED (parm))
2528	    {
2529	      if (TYPE_UNSIGNED (arg))
2530		return 0;
2531	      else
2532		return INTEGER_PROMOTION_BADNESS;
2533	    }
2534	  else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
2535	    return 0;
2536	  else
2537	    return INTEGER_CONVERSION_BADNESS;
2538	default:
2539	  return INCOMPATIBLE_TYPE_BADNESS;
2540	}
2541      break;
2542    case TYPE_CODE_RANGE:
2543      switch (TYPE_CODE (arg))
2544	{
2545	case TYPE_CODE_INT:
2546	case TYPE_CODE_CHAR:
2547	case TYPE_CODE_RANGE:
2548	case TYPE_CODE_BOOL:
2549	case TYPE_CODE_ENUM:
2550	  return INTEGER_CONVERSION_BADNESS;
2551	case TYPE_CODE_FLT:
2552	  return INT_FLOAT_CONVERSION_BADNESS;
2553	default:
2554	  return INCOMPATIBLE_TYPE_BADNESS;
2555	}
2556      break;
2557    case TYPE_CODE_BOOL:
2558      switch (TYPE_CODE (arg))
2559	{
2560	case TYPE_CODE_INT:
2561	case TYPE_CODE_CHAR:
2562	case TYPE_CODE_RANGE:
2563	case TYPE_CODE_ENUM:
2564	case TYPE_CODE_FLT:
2565	case TYPE_CODE_PTR:
2566	  return BOOLEAN_CONVERSION_BADNESS;
2567	case TYPE_CODE_BOOL:
2568	  return 0;
2569	default:
2570	  return INCOMPATIBLE_TYPE_BADNESS;
2571	}
2572      break;
2573    case TYPE_CODE_FLT:
2574      switch (TYPE_CODE (arg))
2575	{
2576	case TYPE_CODE_FLT:
2577	  if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
2578	    return FLOAT_PROMOTION_BADNESS;
2579	  else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
2580	    return 0;
2581	  else
2582	    return FLOAT_CONVERSION_BADNESS;
2583	case TYPE_CODE_INT:
2584	case TYPE_CODE_BOOL:
2585	case TYPE_CODE_ENUM:
2586	case TYPE_CODE_RANGE:
2587	case TYPE_CODE_CHAR:
2588	  return INT_FLOAT_CONVERSION_BADNESS;
2589	default:
2590	  return INCOMPATIBLE_TYPE_BADNESS;
2591	}
2592      break;
2593    case TYPE_CODE_COMPLEX:
2594      switch (TYPE_CODE (arg))
2595	{			/* Strictly not needed for C++, but... */
2596	case TYPE_CODE_FLT:
2597	  return FLOAT_PROMOTION_BADNESS;
2598	case TYPE_CODE_COMPLEX:
2599	  return 0;
2600	default:
2601	  return INCOMPATIBLE_TYPE_BADNESS;
2602	}
2603      break;
2604    case TYPE_CODE_STRUCT:
2605      /* currently same as TYPE_CODE_CLASS */
2606      switch (TYPE_CODE (arg))
2607	{
2608	case TYPE_CODE_STRUCT:
2609	  /* Check for derivation */
2610	  if (is_ancestor (parm, arg))
2611	    return BASE_CONVERSION_BADNESS;
2612	  /* else fall through */
2613	default:
2614	  return INCOMPATIBLE_TYPE_BADNESS;
2615	}
2616      break;
2617    case TYPE_CODE_UNION:
2618      switch (TYPE_CODE (arg))
2619	{
2620	case TYPE_CODE_UNION:
2621	default:
2622	  return INCOMPATIBLE_TYPE_BADNESS;
2623	}
2624      break;
2625    case TYPE_CODE_MEMBER:
2626      switch (TYPE_CODE (arg))
2627	{
2628	default:
2629	  return INCOMPATIBLE_TYPE_BADNESS;
2630	}
2631      break;
2632    case TYPE_CODE_METHOD:
2633      switch (TYPE_CODE (arg))
2634	{
2635
2636	default:
2637	  return INCOMPATIBLE_TYPE_BADNESS;
2638	}
2639      break;
2640    case TYPE_CODE_REF:
2641      switch (TYPE_CODE (arg))
2642	{
2643
2644	default:
2645	  return INCOMPATIBLE_TYPE_BADNESS;
2646	}
2647
2648      break;
2649    case TYPE_CODE_SET:
2650      switch (TYPE_CODE (arg))
2651	{
2652	  /* Not in C++ */
2653	case TYPE_CODE_SET:
2654	  return rank_one_type (TYPE_FIELD_TYPE (parm, 0), TYPE_FIELD_TYPE (arg, 0));
2655	default:
2656	  return INCOMPATIBLE_TYPE_BADNESS;
2657	}
2658      break;
2659    case TYPE_CODE_VOID:
2660    default:
2661      return INCOMPATIBLE_TYPE_BADNESS;
2662    }				/* switch (TYPE_CODE (arg)) */
2663}
2664
2665
2666/* End of functions for overload resolution */
2667
2668static void
2669print_bit_vector (B_TYPE *bits, int nbits)
2670{
2671  int bitno;
2672
2673  for (bitno = 0; bitno < nbits; bitno++)
2674    {
2675      if ((bitno % 8) == 0)
2676	{
2677	  puts_filtered (" ");
2678	}
2679      if (B_TST (bits, bitno))
2680	{
2681	  printf_filtered ("1");
2682	}
2683      else
2684	{
2685	  printf_filtered ("0");
2686	}
2687    }
2688}
2689
2690/* Note the first arg should be the "this" pointer, we may not want to
2691   include it since we may get into a infinitely recursive situation.  */
2692
2693static void
2694print_arg_types (struct field *args, int nargs, int spaces)
2695{
2696  if (args != NULL)
2697    {
2698      int i;
2699
2700      for (i = 0; i < nargs; i++)
2701	recursive_dump_type (args[i].type, spaces + 2);
2702    }
2703}
2704
2705static void
2706dump_fn_fieldlists (struct type *type, int spaces)
2707{
2708  int method_idx;
2709  int overload_idx;
2710  struct fn_field *f;
2711
2712  printfi_filtered (spaces, "fn_fieldlists ");
2713  gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
2714  printf_filtered ("\n");
2715  for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
2716    {
2717      f = TYPE_FN_FIELDLIST1 (type, method_idx);
2718      printfi_filtered (spaces + 2, "[%d] name '%s' (",
2719			method_idx,
2720			TYPE_FN_FIELDLIST_NAME (type, method_idx));
2721      gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
2722			      gdb_stdout);
2723      printf_filtered (") length %d\n",
2724		       TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
2725      for (overload_idx = 0;
2726	   overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
2727	   overload_idx++)
2728	{
2729	  printfi_filtered (spaces + 4, "[%d] physname '%s' (",
2730			    overload_idx,
2731			    TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
2732	  gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
2733				  gdb_stdout);
2734	  printf_filtered (")\n");
2735	  printfi_filtered (spaces + 8, "type ");
2736	  gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), gdb_stdout);
2737	  printf_filtered ("\n");
2738
2739	  recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
2740			       spaces + 8 + 2);
2741
2742	  printfi_filtered (spaces + 8, "args ");
2743	  gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), gdb_stdout);
2744	  printf_filtered ("\n");
2745
2746	  print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx),
2747			   TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)),
2748			   spaces);
2749	  printfi_filtered (spaces + 8, "fcontext ");
2750	  gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
2751				  gdb_stdout);
2752	  printf_filtered ("\n");
2753
2754	  printfi_filtered (spaces + 8, "is_const %d\n",
2755			    TYPE_FN_FIELD_CONST (f, overload_idx));
2756	  printfi_filtered (spaces + 8, "is_volatile %d\n",
2757			    TYPE_FN_FIELD_VOLATILE (f, overload_idx));
2758	  printfi_filtered (spaces + 8, "is_private %d\n",
2759			    TYPE_FN_FIELD_PRIVATE (f, overload_idx));
2760	  printfi_filtered (spaces + 8, "is_protected %d\n",
2761			    TYPE_FN_FIELD_PROTECTED (f, overload_idx));
2762	  printfi_filtered (spaces + 8, "is_stub %d\n",
2763			    TYPE_FN_FIELD_STUB (f, overload_idx));
2764	  printfi_filtered (spaces + 8, "voffset %u\n",
2765			    TYPE_FN_FIELD_VOFFSET (f, overload_idx));
2766	}
2767    }
2768}
2769
2770static void
2771print_cplus_stuff (struct type *type, int spaces)
2772{
2773  printfi_filtered (spaces, "n_baseclasses %d\n",
2774		    TYPE_N_BASECLASSES (type));
2775  printfi_filtered (spaces, "nfn_fields %d\n",
2776		    TYPE_NFN_FIELDS (type));
2777  printfi_filtered (spaces, "nfn_fields_total %d\n",
2778		    TYPE_NFN_FIELDS_TOTAL (type));
2779  if (TYPE_N_BASECLASSES (type) > 0)
2780    {
2781      printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
2782			TYPE_N_BASECLASSES (type));
2783      gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), gdb_stdout);
2784      printf_filtered (")");
2785
2786      print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
2787			TYPE_N_BASECLASSES (type));
2788      puts_filtered ("\n");
2789    }
2790  if (TYPE_NFIELDS (type) > 0)
2791    {
2792      if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
2793	{
2794	  printfi_filtered (spaces, "private_field_bits (%d bits at *",
2795			    TYPE_NFIELDS (type));
2796	  gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), gdb_stdout);
2797	  printf_filtered (")");
2798	  print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
2799			    TYPE_NFIELDS (type));
2800	  puts_filtered ("\n");
2801	}
2802      if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
2803	{
2804	  printfi_filtered (spaces, "protected_field_bits (%d bits at *",
2805			    TYPE_NFIELDS (type));
2806	  gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), gdb_stdout);
2807	  printf_filtered (")");
2808	  print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
2809			    TYPE_NFIELDS (type));
2810	  puts_filtered ("\n");
2811	}
2812    }
2813  if (TYPE_NFN_FIELDS (type) > 0)
2814    {
2815      dump_fn_fieldlists (type, spaces);
2816    }
2817}
2818
2819static void
2820print_bound_type (int bt)
2821{
2822  switch (bt)
2823    {
2824    case BOUND_CANNOT_BE_DETERMINED:
2825      printf_filtered ("(BOUND_CANNOT_BE_DETERMINED)");
2826      break;
2827    case BOUND_BY_REF_ON_STACK:
2828      printf_filtered ("(BOUND_BY_REF_ON_STACK)");
2829      break;
2830    case BOUND_BY_VALUE_ON_STACK:
2831      printf_filtered ("(BOUND_BY_VALUE_ON_STACK)");
2832      break;
2833    case BOUND_BY_REF_IN_REG:
2834      printf_filtered ("(BOUND_BY_REF_IN_REG)");
2835      break;
2836    case BOUND_BY_VALUE_IN_REG:
2837      printf_filtered ("(BOUND_BY_VALUE_IN_REG)");
2838      break;
2839    case BOUND_SIMPLE:
2840      printf_filtered ("(BOUND_SIMPLE)");
2841      break;
2842    default:
2843      printf_filtered ("(unknown bound type)");
2844      break;
2845    }
2846}
2847
2848static struct obstack dont_print_type_obstack;
2849
2850void
2851recursive_dump_type (struct type *type, int spaces)
2852{
2853  int idx;
2854
2855  if (spaces == 0)
2856    obstack_begin (&dont_print_type_obstack, 0);
2857
2858  if (TYPE_NFIELDS (type) > 0
2859      || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
2860    {
2861      struct type **first_dont_print
2862      = (struct type **) obstack_base (&dont_print_type_obstack);
2863
2864      int i = (struct type **) obstack_next_free (&dont_print_type_obstack)
2865      - first_dont_print;
2866
2867      while (--i >= 0)
2868	{
2869	  if (type == first_dont_print[i])
2870	    {
2871	      printfi_filtered (spaces, "type node ");
2872	      gdb_print_host_address (type, gdb_stdout);
2873	      printf_filtered (" <same as already seen type>\n");
2874	      return;
2875	    }
2876	}
2877
2878      obstack_ptr_grow (&dont_print_type_obstack, type);
2879    }
2880
2881  printfi_filtered (spaces, "type node ");
2882  gdb_print_host_address (type, gdb_stdout);
2883  printf_filtered ("\n");
2884  printfi_filtered (spaces, "name '%s' (",
2885		    TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
2886  gdb_print_host_address (TYPE_NAME (type), gdb_stdout);
2887  printf_filtered (")\n");
2888  printfi_filtered (spaces, "tagname '%s' (",
2889		    TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>");
2890  gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout);
2891  printf_filtered (")\n");
2892  printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
2893  switch (TYPE_CODE (type))
2894    {
2895    case TYPE_CODE_UNDEF:
2896      printf_filtered ("(TYPE_CODE_UNDEF)");
2897      break;
2898    case TYPE_CODE_PTR:
2899      printf_filtered ("(TYPE_CODE_PTR)");
2900      break;
2901    case TYPE_CODE_ARRAY:
2902      printf_filtered ("(TYPE_CODE_ARRAY)");
2903      break;
2904    case TYPE_CODE_STRUCT:
2905      printf_filtered ("(TYPE_CODE_STRUCT)");
2906      break;
2907    case TYPE_CODE_UNION:
2908      printf_filtered ("(TYPE_CODE_UNION)");
2909      break;
2910    case TYPE_CODE_ENUM:
2911      printf_filtered ("(TYPE_CODE_ENUM)");
2912      break;
2913    case TYPE_CODE_FUNC:
2914      printf_filtered ("(TYPE_CODE_FUNC)");
2915      break;
2916    case TYPE_CODE_INT:
2917      printf_filtered ("(TYPE_CODE_INT)");
2918      break;
2919    case TYPE_CODE_FLT:
2920      printf_filtered ("(TYPE_CODE_FLT)");
2921      break;
2922    case TYPE_CODE_VOID:
2923      printf_filtered ("(TYPE_CODE_VOID)");
2924      break;
2925    case TYPE_CODE_SET:
2926      printf_filtered ("(TYPE_CODE_SET)");
2927      break;
2928    case TYPE_CODE_RANGE:
2929      printf_filtered ("(TYPE_CODE_RANGE)");
2930      break;
2931    case TYPE_CODE_STRING:
2932      printf_filtered ("(TYPE_CODE_STRING)");
2933      break;
2934    case TYPE_CODE_BITSTRING:
2935      printf_filtered ("(TYPE_CODE_BITSTRING)");
2936      break;
2937    case TYPE_CODE_ERROR:
2938      printf_filtered ("(TYPE_CODE_ERROR)");
2939      break;
2940    case TYPE_CODE_MEMBER:
2941      printf_filtered ("(TYPE_CODE_MEMBER)");
2942      break;
2943    case TYPE_CODE_METHOD:
2944      printf_filtered ("(TYPE_CODE_METHOD)");
2945      break;
2946    case TYPE_CODE_REF:
2947      printf_filtered ("(TYPE_CODE_REF)");
2948      break;
2949    case TYPE_CODE_CHAR:
2950      printf_filtered ("(TYPE_CODE_CHAR)");
2951      break;
2952    case TYPE_CODE_BOOL:
2953      printf_filtered ("(TYPE_CODE_BOOL)");
2954      break;
2955    case TYPE_CODE_COMPLEX:
2956      printf_filtered ("(TYPE_CODE_COMPLEX)");
2957      break;
2958    case TYPE_CODE_TYPEDEF:
2959      printf_filtered ("(TYPE_CODE_TYPEDEF)");
2960      break;
2961    case TYPE_CODE_TEMPLATE:
2962      printf_filtered ("(TYPE_CODE_TEMPLATE)");
2963      break;
2964    case TYPE_CODE_TEMPLATE_ARG:
2965      printf_filtered ("(TYPE_CODE_TEMPLATE_ARG)");
2966      break;
2967    case TYPE_CODE_NAMESPACE:
2968      printf_filtered ("(TYPE_CODE_NAMESPACE)");
2969      break;
2970    default:
2971      printf_filtered ("(UNKNOWN TYPE CODE)");
2972      break;
2973    }
2974  puts_filtered ("\n");
2975  printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
2976  printfi_filtered (spaces, "upper_bound_type 0x%x ",
2977		    TYPE_ARRAY_UPPER_BOUND_TYPE (type));
2978  print_bound_type (TYPE_ARRAY_UPPER_BOUND_TYPE (type));
2979  puts_filtered ("\n");
2980  printfi_filtered (spaces, "lower_bound_type 0x%x ",
2981		    TYPE_ARRAY_LOWER_BOUND_TYPE (type));
2982  print_bound_type (TYPE_ARRAY_LOWER_BOUND_TYPE (type));
2983  puts_filtered ("\n");
2984  printfi_filtered (spaces, "objfile ");
2985  gdb_print_host_address (TYPE_OBJFILE (type), gdb_stdout);
2986  printf_filtered ("\n");
2987  printfi_filtered (spaces, "target_type ");
2988  gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout);
2989  printf_filtered ("\n");
2990  if (TYPE_TARGET_TYPE (type) != NULL)
2991    {
2992      recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
2993    }
2994  printfi_filtered (spaces, "pointer_type ");
2995  gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout);
2996  printf_filtered ("\n");
2997  printfi_filtered (spaces, "reference_type ");
2998  gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
2999  printf_filtered ("\n");
3000  printfi_filtered (spaces, "type_chain ");
3001  gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout);
3002  printf_filtered ("\n");
3003  printfi_filtered (spaces, "instance_flags 0x%x", TYPE_INSTANCE_FLAGS (type));
3004  if (TYPE_CONST (type))
3005    {
3006      puts_filtered (" TYPE_FLAG_CONST");
3007    }
3008  if (TYPE_VOLATILE (type))
3009    {
3010      puts_filtered (" TYPE_FLAG_VOLATILE");
3011    }
3012  if (TYPE_CODE_SPACE (type))
3013    {
3014      puts_filtered (" TYPE_FLAG_CODE_SPACE");
3015    }
3016  if (TYPE_DATA_SPACE (type))
3017    {
3018      puts_filtered (" TYPE_FLAG_DATA_SPACE");
3019    }
3020  if (TYPE_ADDRESS_CLASS_1 (type))
3021    {
3022      puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1");
3023    }
3024  if (TYPE_ADDRESS_CLASS_2 (type))
3025    {
3026      puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2");
3027    }
3028  puts_filtered ("\n");
3029  printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
3030  if (TYPE_UNSIGNED (type))
3031    {
3032      puts_filtered (" TYPE_FLAG_UNSIGNED");
3033    }
3034  if (TYPE_NOSIGN (type))
3035    {
3036      puts_filtered (" TYPE_FLAG_NOSIGN");
3037    }
3038  if (TYPE_STUB (type))
3039    {
3040      puts_filtered (" TYPE_FLAG_STUB");
3041    }
3042  if (TYPE_TARGET_STUB (type))
3043    {
3044      puts_filtered (" TYPE_FLAG_TARGET_STUB");
3045    }
3046  if (TYPE_STATIC (type))
3047    {
3048      puts_filtered (" TYPE_FLAG_STATIC");
3049    }
3050  if (TYPE_PROTOTYPED (type))
3051    {
3052      puts_filtered (" TYPE_FLAG_PROTOTYPED");
3053    }
3054  if (TYPE_INCOMPLETE (type))
3055    {
3056      puts_filtered (" TYPE_FLAG_INCOMPLETE");
3057    }
3058  if (TYPE_VARARGS (type))
3059    {
3060      puts_filtered (" TYPE_FLAG_VARARGS");
3061    }
3062  /* This is used for things like AltiVec registers on ppc.  Gcc emits
3063     an attribute for the array type, which tells whether or not we
3064     have a vector, instead of a regular array.  */
3065  if (TYPE_VECTOR (type))
3066    {
3067      puts_filtered (" TYPE_FLAG_VECTOR");
3068    }
3069  puts_filtered ("\n");
3070  printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
3071  gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout);
3072  puts_filtered ("\n");
3073  for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
3074    {
3075      printfi_filtered (spaces + 2,
3076			"[%d] bitpos %d bitsize %d type ",
3077			idx, TYPE_FIELD_BITPOS (type, idx),
3078			TYPE_FIELD_BITSIZE (type, idx));
3079      gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
3080      printf_filtered (" name '%s' (",
3081		       TYPE_FIELD_NAME (type, idx) != NULL
3082		       ? TYPE_FIELD_NAME (type, idx)
3083		       : "<NULL>");
3084      gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
3085      printf_filtered (")\n");
3086      if (TYPE_FIELD_TYPE (type, idx) != NULL)
3087	{
3088	  recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
3089	}
3090    }
3091  printfi_filtered (spaces, "vptr_basetype ");
3092  gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
3093  puts_filtered ("\n");
3094  if (TYPE_VPTR_BASETYPE (type) != NULL)
3095    {
3096      recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
3097    }
3098  printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
3099  switch (TYPE_CODE (type))
3100    {
3101    case TYPE_CODE_STRUCT:
3102      printfi_filtered (spaces, "cplus_stuff ");
3103      gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
3104      puts_filtered ("\n");
3105      print_cplus_stuff (type, spaces);
3106      break;
3107
3108    case TYPE_CODE_FLT:
3109      printfi_filtered (spaces, "floatformat ");
3110      if (TYPE_FLOATFORMAT (type) == NULL
3111	  || TYPE_FLOATFORMAT (type)->name == NULL)
3112	puts_filtered ("(null)");
3113      else
3114	puts_filtered (TYPE_FLOATFORMAT (type)->name);
3115      puts_filtered ("\n");
3116      break;
3117
3118    default:
3119      /* We have to pick one of the union types to be able print and test
3120         the value.  Pick cplus_struct_type, even though we know it isn't
3121         any particular one. */
3122      printfi_filtered (spaces, "type_specific ");
3123      gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
3124      if (TYPE_CPLUS_SPECIFIC (type) != NULL)
3125	{
3126	  printf_filtered (" (unknown data form)");
3127	}
3128      printf_filtered ("\n");
3129      break;
3130
3131    }
3132  if (spaces == 0)
3133    obstack_free (&dont_print_type_obstack, NULL);
3134}
3135
3136static void build_gdbtypes (void);
3137static void
3138build_gdbtypes (void)
3139{
3140  builtin_type_void =
3141    init_type (TYPE_CODE_VOID, 1,
3142	       0,
3143	       "void", (struct objfile *) NULL);
3144  builtin_type_char =
3145    init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3146	       (TYPE_FLAG_NOSIGN
3147                | (TARGET_CHAR_SIGNED ? 0 : TYPE_FLAG_UNSIGNED)),
3148	       "char", (struct objfile *) NULL);
3149  builtin_type_true_char =
3150    init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3151	       0,
3152	       "true character", (struct objfile *) NULL);
3153  builtin_type_signed_char =
3154    init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3155	       0,
3156	       "signed char", (struct objfile *) NULL);
3157  builtin_type_unsigned_char =
3158    init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3159	       TYPE_FLAG_UNSIGNED,
3160	       "unsigned char", (struct objfile *) NULL);
3161  builtin_type_short =
3162    init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
3163	       0,
3164	       "short", (struct objfile *) NULL);
3165  builtin_type_unsigned_short =
3166    init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
3167	       TYPE_FLAG_UNSIGNED,
3168	       "unsigned short", (struct objfile *) NULL);
3169  builtin_type_int =
3170    init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
3171	       0,
3172	       "int", (struct objfile *) NULL);
3173  builtin_type_unsigned_int =
3174    init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
3175	       TYPE_FLAG_UNSIGNED,
3176	       "unsigned int", (struct objfile *) NULL);
3177  builtin_type_long =
3178    init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
3179	       0,
3180	       "long", (struct objfile *) NULL);
3181  builtin_type_unsigned_long =
3182    init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
3183	       TYPE_FLAG_UNSIGNED,
3184	       "unsigned long", (struct objfile *) NULL);
3185  builtin_type_long_long =
3186    init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
3187	       0,
3188	       "long long", (struct objfile *) NULL);
3189  builtin_type_unsigned_long_long =
3190    init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
3191	       TYPE_FLAG_UNSIGNED,
3192	       "unsigned long long", (struct objfile *) NULL);
3193  builtin_type_float =
3194    init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
3195	       0,
3196	       "float", (struct objfile *) NULL);
3197/* vinschen@redhat.com 2002-02-08:
3198   The below lines are disabled since they are doing the wrong
3199   thing for non-multiarch targets.  They are setting the correct
3200   type of floats for the target but while on multiarch targets
3201   this is done everytime the architecture changes, it's done on
3202   non-multiarch targets only on startup, leaving the wrong values
3203   in even if the architecture changes (eg. from big-endian to
3204   little-endian).  */
3205#if 0
3206  TYPE_FLOATFORMAT (builtin_type_float) = TARGET_FLOAT_FORMAT;
3207#endif
3208  builtin_type_double =
3209    init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
3210	       0,
3211	       "double", (struct objfile *) NULL);
3212#if 0
3213  TYPE_FLOATFORMAT (builtin_type_double) = TARGET_DOUBLE_FORMAT;
3214#endif
3215  builtin_type_long_double =
3216    init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
3217	       0,
3218	       "long double", (struct objfile *) NULL);
3219#if 0
3220  TYPE_FLOATFORMAT (builtin_type_long_double) = TARGET_LONG_DOUBLE_FORMAT;
3221#endif
3222  builtin_type_complex =
3223    init_type (TYPE_CODE_COMPLEX, 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
3224	       0,
3225	       "complex", (struct objfile *) NULL);
3226  TYPE_TARGET_TYPE (builtin_type_complex) = builtin_type_float;
3227  builtin_type_double_complex =
3228    init_type (TYPE_CODE_COMPLEX, 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
3229	       0,
3230	       "double complex", (struct objfile *) NULL);
3231  TYPE_TARGET_TYPE (builtin_type_double_complex) = builtin_type_double;
3232  builtin_type_string =
3233    init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3234	       0,
3235	       "string", (struct objfile *) NULL);
3236  builtin_type_int0 =
3237    init_type (TYPE_CODE_INT, 0 / 8,
3238	       0,
3239	       "int0_t", (struct objfile *) NULL);
3240  builtin_type_int8 =
3241    init_type (TYPE_CODE_INT, 8 / 8,
3242	       0,
3243	       "int8_t", (struct objfile *) NULL);
3244  builtin_type_uint8 =
3245    init_type (TYPE_CODE_INT, 8 / 8,
3246	       TYPE_FLAG_UNSIGNED,
3247	       "uint8_t", (struct objfile *) NULL);
3248  builtin_type_int16 =
3249    init_type (TYPE_CODE_INT, 16 / 8,
3250	       0,
3251	       "int16_t", (struct objfile *) NULL);
3252  builtin_type_uint16 =
3253    init_type (TYPE_CODE_INT, 16 / 8,
3254	       TYPE_FLAG_UNSIGNED,
3255	       "uint16_t", (struct objfile *) NULL);
3256  builtin_type_int32 =
3257    init_type (TYPE_CODE_INT, 32 / 8,
3258	       0,
3259	       "int32_t", (struct objfile *) NULL);
3260  builtin_type_uint32 =
3261    init_type (TYPE_CODE_INT, 32 / 8,
3262	       TYPE_FLAG_UNSIGNED,
3263	       "uint32_t", (struct objfile *) NULL);
3264  builtin_type_int64 =
3265    init_type (TYPE_CODE_INT, 64 / 8,
3266	       0,
3267	       "int64_t", (struct objfile *) NULL);
3268  builtin_type_uint64 =
3269    init_type (TYPE_CODE_INT, 64 / 8,
3270	       TYPE_FLAG_UNSIGNED,
3271	       "uint64_t", (struct objfile *) NULL);
3272  builtin_type_int128 =
3273    init_type (TYPE_CODE_INT, 128 / 8,
3274	       0,
3275	       "int128_t", (struct objfile *) NULL);
3276  builtin_type_uint128 =
3277    init_type (TYPE_CODE_INT, 128 / 8,
3278	       TYPE_FLAG_UNSIGNED,
3279	       "uint128_t", (struct objfile *) NULL);
3280  builtin_type_bool =
3281    init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
3282	       0,
3283	       "bool", (struct objfile *) NULL);
3284
3285  /* Add user knob for controlling resolution of opaque types */
3286  add_show_from_set
3287    (add_set_cmd ("opaque-type-resolution", class_support, var_boolean, (char *) &opaque_type_resolution,
3288		  "Set resolution of opaque struct/class/union types (if set before loading symbols).",
3289		  &setlist),
3290     &showlist);
3291  opaque_type_resolution = 1;
3292
3293  /* Build SIMD types.  */
3294  builtin_type_v4sf
3295    = init_simd_type ("__builtin_v4sf", builtin_type_float, "f", 4);
3296  builtin_type_v4si
3297    = init_simd_type ("__builtin_v4si", builtin_type_int32, "f", 4);
3298  builtin_type_v16qi
3299    = init_simd_type ("__builtin_v16qi", builtin_type_int8, "f", 16);
3300  builtin_type_v8qi
3301    = init_simd_type ("__builtin_v8qi", builtin_type_int8, "f", 8);
3302  builtin_type_v8hi
3303    = init_simd_type ("__builtin_v8hi", builtin_type_int16, "f", 8);
3304  builtin_type_v4hi
3305    = init_simd_type ("__builtin_v4hi", builtin_type_int16, "f", 4);
3306  builtin_type_v2si
3307    = init_simd_type ("__builtin_v2si", builtin_type_int32, "f", 2);
3308
3309  /* 128 bit vectors.  */
3310  builtin_type_v2_double = init_vector_type (builtin_type_double, 2);
3311  builtin_type_v4_float = init_vector_type (builtin_type_float, 4);
3312  builtin_type_v2_int64 = init_vector_type (builtin_type_int64, 2);
3313  builtin_type_v4_int32 = init_vector_type (builtin_type_int32, 4);
3314  builtin_type_v8_int16 = init_vector_type (builtin_type_int16, 8);
3315  builtin_type_v16_int8 = init_vector_type (builtin_type_int8, 16);
3316  /* 64 bit vectors.  */
3317  builtin_type_v2_float = init_vector_type (builtin_type_float, 2);
3318  builtin_type_v2_int32 = init_vector_type (builtin_type_int32, 2);
3319  builtin_type_v4_int16 = init_vector_type (builtin_type_int16, 4);
3320  builtin_type_v8_int8 = init_vector_type (builtin_type_int8, 8);
3321
3322  /* Vector types.  */
3323  builtin_type_vec64 = build_builtin_type_vec64 ();
3324  builtin_type_vec64i = build_builtin_type_vec64i ();
3325  builtin_type_vec128 = build_builtin_type_vec128 ();
3326  builtin_type_vec128i = build_builtin_type_vec128i ();
3327
3328  /* Pointer/Address types. */
3329
3330  /* NOTE: on some targets, addresses and pointers are not necessarily
3331     the same --- for example, on the D10V, pointers are 16 bits long,
3332     but addresses are 32 bits long.  See doc/gdbint.texinfo,
3333     ``Pointers Are Not Always Addresses''.
3334
3335     The upshot is:
3336     - gdb's `struct type' always describes the target's
3337       representation.
3338     - gdb's `struct value' objects should always hold values in
3339       target form.
3340     - gdb's CORE_ADDR values are addresses in the unified virtual
3341       address space that the assembler and linker work with.  Thus,
3342       since target_read_memory takes a CORE_ADDR as an argument, it
3343       can access any memory on the target, even if the processor has
3344       separate code and data address spaces.
3345
3346     So, for example:
3347     - If v is a value holding a D10V code pointer, its contents are
3348       in target form: a big-endian address left-shifted two bits.
3349     - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as
3350       sizeof (void *) == 2 on the target.
3351
3352     In this context, builtin_type_CORE_ADDR is a bit odd: it's a
3353     target type for a value the target will never see.  It's only
3354     used to hold the values of (typeless) linker symbols, which are
3355     indeed in the unified virtual address space.  */
3356  builtin_type_void_data_ptr = make_pointer_type (builtin_type_void, NULL);
3357  builtin_type_void_func_ptr
3358    = lookup_pointer_type (lookup_function_type (builtin_type_void));
3359  builtin_type_CORE_ADDR =
3360    init_type (TYPE_CODE_INT, TARGET_ADDR_BIT / 8,
3361	       TYPE_FLAG_UNSIGNED,
3362	       "__CORE_ADDR", (struct objfile *) NULL);
3363  builtin_type_bfd_vma =
3364    init_type (TYPE_CODE_INT, TARGET_BFD_VMA_BIT / 8,
3365	       TYPE_FLAG_UNSIGNED,
3366	       "__bfd_vma", (struct objfile *) NULL);
3367}
3368
3369extern void _initialize_gdbtypes (void);
3370void
3371_initialize_gdbtypes (void)
3372{
3373  struct cmd_list_element *c;
3374  build_gdbtypes ();
3375
3376  /* FIXME - For the moment, handle types by swapping them in and out.
3377     Should be using the per-architecture data-pointer and a large
3378     struct. */
3379  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void);
3380  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_char);
3381  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_short);
3382  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int);
3383  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long);
3384  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long_long);
3385  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_signed_char);
3386  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_char);
3387  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_short);
3388  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_int);
3389  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_long);
3390  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_unsigned_long_long);
3391  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_float);
3392  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_double);
3393  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_long_double);
3394  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_complex);
3395  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_double_complex);
3396  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_string);
3397  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int8);
3398  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_uint8);
3399  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int16);
3400  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_uint16);
3401  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int32);
3402  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_uint32);
3403  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int64);
3404  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_uint64);
3405  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_int128);
3406  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_uint128);
3407  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4sf);
3408  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4si);
3409  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v16qi);
3410  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v8qi);
3411  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v8hi);
3412  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4hi);
3413  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v2si);
3414  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v2_double);
3415  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4_float);
3416  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v2_int64);
3417  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4_int32);
3418  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v8_int16);
3419  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v16_int8);
3420  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v2_float);
3421  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v2_int32);
3422  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v8_int8);
3423  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_v4_int16);
3424  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_vec128);
3425  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_vec128i);
3426  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void_data_ptr);
3427  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_void_func_ptr);
3428  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_CORE_ADDR);
3429  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_bfd_vma);
3430  deprecated_register_gdbarch_swap (NULL, 0, build_gdbtypes);
3431
3432  /* Note: These types do not need to be swapped - they are target
3433     neutral.  */
3434  builtin_type_ieee_single_big =
3435    init_type (TYPE_CODE_FLT, floatformat_ieee_single_big.totalsize / 8,
3436	       0, "builtin_type_ieee_single_big", NULL);
3437  TYPE_FLOATFORMAT (builtin_type_ieee_single_big) = &floatformat_ieee_single_big;
3438  builtin_type_ieee_single_little =
3439    init_type (TYPE_CODE_FLT, floatformat_ieee_single_little.totalsize / 8,
3440	       0, "builtin_type_ieee_single_little", NULL);
3441  TYPE_FLOATFORMAT (builtin_type_ieee_single_little) = &floatformat_ieee_single_little;
3442  builtin_type_ieee_double_big =
3443    init_type (TYPE_CODE_FLT, floatformat_ieee_double_big.totalsize / 8,
3444	       0, "builtin_type_ieee_double_big", NULL);
3445  TYPE_FLOATFORMAT (builtin_type_ieee_double_big) = &floatformat_ieee_double_big;
3446  builtin_type_ieee_double_little =
3447    init_type (TYPE_CODE_FLT, floatformat_ieee_double_little.totalsize / 8,
3448	       0, "builtin_type_ieee_double_little", NULL);
3449  TYPE_FLOATFORMAT (builtin_type_ieee_double_little) = &floatformat_ieee_double_little;
3450  builtin_type_ieee_double_littlebyte_bigword =
3451    init_type (TYPE_CODE_FLT, floatformat_ieee_double_littlebyte_bigword.totalsize / 8,
3452	       0, "builtin_type_ieee_double_littlebyte_bigword", NULL);
3453  TYPE_FLOATFORMAT (builtin_type_ieee_double_littlebyte_bigword) = &floatformat_ieee_double_littlebyte_bigword;
3454  builtin_type_i387_ext =
3455    init_type (TYPE_CODE_FLT, floatformat_i387_ext.totalsize / 8,
3456	       0, "builtin_type_i387_ext", NULL);
3457  TYPE_FLOATFORMAT (builtin_type_i387_ext) = &floatformat_i387_ext;
3458  builtin_type_m68881_ext =
3459    init_type (TYPE_CODE_FLT, floatformat_m68881_ext.totalsize / 8,
3460	       0, "builtin_type_m68881_ext", NULL);
3461  TYPE_FLOATFORMAT (builtin_type_m68881_ext) = &floatformat_m68881_ext;
3462  builtin_type_i960_ext =
3463    init_type (TYPE_CODE_FLT, floatformat_i960_ext.totalsize / 8,
3464	       0, "builtin_type_i960_ext", NULL);
3465  TYPE_FLOATFORMAT (builtin_type_i960_ext) = &floatformat_i960_ext;
3466  builtin_type_m88110_ext =
3467    init_type (TYPE_CODE_FLT, floatformat_m88110_ext.totalsize / 8,
3468	       0, "builtin_type_m88110_ext", NULL);
3469  TYPE_FLOATFORMAT (builtin_type_m88110_ext) = &floatformat_m88110_ext;
3470  builtin_type_m88110_harris_ext =
3471    init_type (TYPE_CODE_FLT, floatformat_m88110_harris_ext.totalsize / 8,
3472	       0, "builtin_type_m88110_harris_ext", NULL);
3473  TYPE_FLOATFORMAT (builtin_type_m88110_harris_ext) = &floatformat_m88110_harris_ext;
3474  builtin_type_arm_ext_big =
3475    init_type (TYPE_CODE_FLT, floatformat_arm_ext_big.totalsize / 8,
3476	       0, "builtin_type_arm_ext_big", NULL);
3477  TYPE_FLOATFORMAT (builtin_type_arm_ext_big) = &floatformat_arm_ext_big;
3478  builtin_type_arm_ext_littlebyte_bigword =
3479    init_type (TYPE_CODE_FLT, floatformat_arm_ext_littlebyte_bigword.totalsize / 8,
3480	       0, "builtin_type_arm_ext_littlebyte_bigword", NULL);
3481  TYPE_FLOATFORMAT (builtin_type_arm_ext_littlebyte_bigword) = &floatformat_arm_ext_littlebyte_bigword;
3482  builtin_type_ia64_spill_big =
3483    init_type (TYPE_CODE_FLT, floatformat_ia64_spill_big.totalsize / 8,
3484	       0, "builtin_type_ia64_spill_big", NULL);
3485  TYPE_FLOATFORMAT (builtin_type_ia64_spill_big) = &floatformat_ia64_spill_big;
3486  builtin_type_ia64_spill_little =
3487    init_type (TYPE_CODE_FLT, floatformat_ia64_spill_little.totalsize / 8,
3488	       0, "builtin_type_ia64_spill_little", NULL);
3489  TYPE_FLOATFORMAT (builtin_type_ia64_spill_little) = &floatformat_ia64_spill_little;
3490  builtin_type_ia64_quad_big =
3491    init_type (TYPE_CODE_FLT, floatformat_ia64_quad_big.totalsize / 8,
3492	       0, "builtin_type_ia64_quad_big", NULL);
3493  TYPE_FLOATFORMAT (builtin_type_ia64_quad_big) = &floatformat_ia64_quad_big;
3494  builtin_type_ia64_quad_little =
3495    init_type (TYPE_CODE_FLT, floatformat_ia64_quad_little.totalsize / 8,
3496	       0, "builtin_type_ia64_quad_little", NULL);
3497  TYPE_FLOATFORMAT (builtin_type_ia64_quad_little) = &floatformat_ia64_quad_little;
3498
3499  add_show_from_set (
3500		     add_set_cmd ("overload", no_class, var_zinteger, (char *) &overload_debug,
3501				  "Set debugging of C++ overloading.\n\
3502			  When enabled, ranking of the functions\n\
3503			  is displayed.", &setdebuglist),
3504		     &showdebuglist);
3505}
3506