debug.c revision 104834
1241279Smarcel/* debug.c -- Handle generic debugging information.
2241279Smarcel   Copyright 1995, 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
3241279Smarcel   Written by Ian Lance Taylor <ian@cygnus.com>.
4241279Smarcel
5241279Smarcel   This file is part of GNU Binutils.
6241279Smarcel
7241279Smarcel   This program is free software; you can redistribute it and/or modify
8253883Ssjg   it under the terms of the GNU General Public License as published by
9241279Smarcel   the Free Software Foundation; either version 2 of the License, or
10253883Ssjg   (at your option) any later version.
11241279Smarcel
12241279Smarcel   This program is distributed in the hope that it will be useful,
13241279Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14241279Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15241279Smarcel   GNU General Public License for more details.
16241279Smarcel
17241279Smarcel   You should have received a copy of the GNU General Public License
18241279Smarcel   along with this program; if not, write to the Free Software
19241279Smarcel   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20241279Smarcel   02111-1307, USA.  */
21241279Smarcel
22241279Smarcel/* This file implements a generic debugging format.  We may eventually
23241279Smarcel   have readers which convert different formats into this generic
24241279Smarcel   format, and writers which write it out.  The initial impetus for
25241279Smarcel   this was writing a convertor from stabs to HP IEEE-695 debugging
26241279Smarcel   format.  */
27241279Smarcel
28241279Smarcel#include <stdio.h>
29241279Smarcel#include <assert.h>
30241279Smarcel
31241279Smarcel#include "bfd.h"
32241279Smarcel#include "bucomm.h"
33241279Smarcel#include "libiberty.h"
34241279Smarcel#include "debug.h"
35241279Smarcel
36241279Smarcel/* Global information we keep for debugging.  A pointer to this
37241279Smarcel   structure is the debugging handle passed to all the routines.  */
38249033Ssjg
39241279Smarcelstruct debug_handle
40241279Smarcel{
41241279Smarcel  /* A linked list of compilation units.  */
42241279Smarcel  struct debug_unit *units;
43241279Smarcel  /* The current compilation unit.  */
44241279Smarcel  struct debug_unit *current_unit;
45241279Smarcel  /* The current source file.  */
46241279Smarcel  struct debug_file *current_file;
47241279Smarcel  /* The current function.  */
48241279Smarcel  struct debug_function *current_function;
49241279Smarcel  /* The current block.  */
50241279Smarcel  struct debug_block *current_block;
51243115Ssjg  /* The current line number information for the current unit.  */
52241279Smarcel  struct debug_lineno *current_lineno;
53241279Smarcel  /* Mark.  This is used by debug_write.  */
54241279Smarcel  unsigned int mark;
55241279Smarcel  /* A struct/class ID used by debug_write.  */
56241279Smarcel  unsigned int class_id;
57241279Smarcel  /* The base for class_id for this call to debug_write.  */
58241279Smarcel  unsigned int base_id;
59241279Smarcel  /* The current line number in debug_write.  */
60241279Smarcel  struct debug_lineno *current_write_lineno;
61241279Smarcel  unsigned int current_write_lineno_index;
62241279Smarcel  /* A list of classes which have assigned ID's during debug_write.
63241279Smarcel     This is linked through the next_id field of debug_class_type.  */
64243115Ssjg  struct debug_class_id *id_list;
65241279Smarcel  /* A list used to avoid recursion during debug_type_samep.  */
66241279Smarcel  struct debug_type_compare_list *compare_list;
67241279Smarcel};
68241279Smarcel
69241279Smarcel/* Information we keep for a single compilation unit.  */
70241279Smarcel
71241279Smarcelstruct debug_unit
72241279Smarcel{
73241279Smarcel  /* The next compilation unit.  */
74241279Smarcel  struct debug_unit *next;
75241279Smarcel  /* A list of files included in this compilation unit.  The first
76241279Smarcel     file is always the main one, and that is where the main file name
77241279Smarcel     is stored.  */
78241279Smarcel  struct debug_file *files;
79241279Smarcel  /* Line number information for this compilation unit.  This is not
80241279Smarcel     stored by function, because assembler code may have line number
81241279Smarcel     information without function information.  */
82241279Smarcel  struct debug_lineno *linenos;
83246325Ssjg};
84241279Smarcel
85241279Smarcel/* Information kept for a single source file.  */
86241279Smarcel
87241279Smarcelstruct debug_file
88246325Ssjg{
89241279Smarcel  /* The next source file in this compilation unit.  */
90241279Smarcel  struct debug_file *next;
91241279Smarcel  /* The name of the source file.  */
92241279Smarcel  const char *filename;
93241279Smarcel  /* Global functions, variables, types, etc.  */
94241279Smarcel  struct debug_namespace *globals;
95241279Smarcel};
96241279Smarcel
97241279Smarcel/* A type.  */
98241279Smarcel
99253883Ssjgstruct debug_type
100241279Smarcel{
101241279Smarcel  /* Kind of type.  */
102241279Smarcel  enum debug_type_kind kind;
103241279Smarcel  /* Size of type (0 if not known).  */
104241279Smarcel  unsigned int size;
105241279Smarcel  /* Type which is a pointer to this type.  */
106241279Smarcel  debug_type pointer;
107241279Smarcel  /* Tagged union with additional information about the type.  */
108241279Smarcel  union
109    {
110      /* DEBUG_KIND_INDIRECT.  */
111      struct debug_indirect_type *kindirect;
112      /* DEBUG_KIND_INT.  */
113      /* Whether the integer is unsigned.  */
114      boolean kint;
115      /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
116         DEBUG_KIND_UNION_CLASS.  */
117      struct debug_class_type *kclass;
118      /* DEBUG_KIND_ENUM.  */
119      struct debug_enum_type *kenum;
120      /* DEBUG_KIND_POINTER.  */
121      struct debug_type *kpointer;
122      /* DEBUG_KIND_FUNCTION.  */
123      struct debug_function_type *kfunction;
124      /* DEBUG_KIND_REFERENCE.  */
125      struct debug_type *kreference;
126      /* DEBUG_KIND_RANGE.  */
127      struct debug_range_type *krange;
128      /* DEBUG_KIND_ARRAY.  */
129      struct debug_array_type *karray;
130      /* DEBUG_KIND_SET.  */
131      struct debug_set_type *kset;
132      /* DEBUG_KIND_OFFSET.  */
133      struct debug_offset_type *koffset;
134      /* DEBUG_KIND_METHOD.  */
135      struct debug_method_type *kmethod;
136      /* DEBUG_KIND_CONST.  */
137      struct debug_type *kconst;
138      /* DEBUG_KIND_VOLATILE.  */
139      struct debug_type *kvolatile;
140      /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED.  */
141      struct debug_named_type *knamed;
142    } u;
143};
144
145/* Information kept for an indirect type.  */
146
147struct debug_indirect_type
148{
149  /* Slot where the final type will appear.  */
150  debug_type *slot;
151  /* Tag.  */
152  const char *tag;
153};
154
155/* Information kept for a struct, union, or class.  */
156
157struct debug_class_type
158{
159  /* NULL terminated array of fields.  */
160  debug_field *fields;
161  /* A mark field which indicates whether the struct has already been
162     printed.  */
163  unsigned int mark;
164  /* This is used to uniquely identify unnamed structs when printing.  */
165  unsigned int id;
166  /* The remaining fields are only used for DEBUG_KIND_CLASS and
167     DEBUG_KIND_UNION_CLASS.  */
168  /* NULL terminated array of base classes.  */
169  debug_baseclass *baseclasses;
170  /* NULL terminated array of methods.  */
171  debug_method *methods;
172  /* The type of the class providing the virtual function table for
173     this class.  This may point to the type itself.  */
174  debug_type vptrbase;
175};
176
177/* Information kept for an enum.  */
178
179struct debug_enum_type
180{
181  /* NULL terminated array of names.  */
182  const char **names;
183  /* Array of corresponding values.  */
184  bfd_signed_vma *values;
185};
186
187/* Information kept for a function.  FIXME: We should be able to
188   record the parameter types.  */
189
190struct debug_function_type
191{
192  /* Return type.  */
193  debug_type return_type;
194  /* NULL terminated array of argument types.  */
195  debug_type *arg_types;
196  /* Whether the function takes a variable number of arguments.  */
197  boolean varargs;
198};
199
200/* Information kept for a range.  */
201
202struct debug_range_type
203{
204  /* Range base type.  */
205  debug_type type;
206  /* Lower bound.  */
207  bfd_signed_vma lower;
208  /* Upper bound.  */
209  bfd_signed_vma upper;
210};
211
212/* Information kept for an array.  */
213
214struct debug_array_type
215{
216  /* Element type.  */
217  debug_type element_type;
218  /* Range type.  */
219  debug_type range_type;
220  /* Lower bound.  */
221  bfd_signed_vma lower;
222  /* Upper bound.  */
223  bfd_signed_vma upper;
224  /* Whether this array is really a string.  */
225  boolean stringp;
226};
227
228/* Information kept for a set.  */
229
230struct debug_set_type
231{
232  /* Base type.  */
233  debug_type type;
234  /* Whether this set is really a bitstring.  */
235  boolean bitstringp;
236};
237
238/* Information kept for an offset type (a based pointer).  */
239
240struct debug_offset_type
241{
242  /* The type the pointer is an offset from.  */
243  debug_type base_type;
244  /* The type the pointer points to.  */
245  debug_type target_type;
246};
247
248/* Information kept for a method type.  */
249
250struct debug_method_type
251{
252  /* The return type.  */
253  debug_type return_type;
254  /* The object type which this method is for.  */
255  debug_type domain_type;
256  /* A NULL terminated array of argument types.  */
257  debug_type *arg_types;
258  /* Whether the method takes a variable number of arguments.  */
259  boolean varargs;
260};
261
262/* Information kept for a named type.  */
263
264struct debug_named_type
265{
266  /* Name.  */
267  struct debug_name *name;
268  /* Real type.  */
269  debug_type type;
270};
271
272/* A field in a struct or union.  */
273
274struct debug_field
275{
276  /* Name of the field.  */
277  const char *name;
278  /* Type of the field.  */
279  struct debug_type *type;
280  /* Visibility of the field.  */
281  enum debug_visibility visibility;
282  /* Whether this is a static member.  */
283  boolean static_member;
284  union
285    {
286      /* If static_member is false.  */
287      struct
288	{
289	  /* Bit position of the field in the struct.  */
290	  unsigned int bitpos;
291	  /* Size of the field in bits.  */
292	  unsigned int bitsize;
293	} f;
294      /* If static_member is true.  */
295      struct
296	{
297	  const char *physname;
298	} s;
299    } u;
300};
301
302/* A base class for an object.  */
303
304struct debug_baseclass
305{
306  /* Type of the base class.  */
307  struct debug_type *type;
308  /* Bit position of the base class in the object.  */
309  unsigned int bitpos;
310  /* Whether the base class is virtual.  */
311  boolean virtual;
312  /* Visibility of the base class.  */
313  enum debug_visibility visibility;
314};
315
316/* A method of an object.  */
317
318struct debug_method
319{
320  /* The name of the method.  */
321  const char *name;
322  /* A NULL terminated array of different types of variants.  */
323  struct debug_method_variant **variants;
324};
325
326/* The variants of a method function of an object.  These indicate
327   which method to run.  */
328
329struct debug_method_variant
330{
331  /* The physical name of the function.  */
332  const char *physname;
333  /* The type of the function.  */
334  struct debug_type *type;
335  /* The visibility of the function.  */
336  enum debug_visibility visibility;
337  /* Whether the function is const.  */
338  boolean constp;
339  /* Whether the function is volatile.  */
340  boolean volatilep;
341  /* The offset to the function in the virtual function table.  */
342  bfd_vma voffset;
343  /* If voffset is VOFFSET_STATIC_METHOD, this is a static method.  */
344#define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
345  /* Context of a virtual method function.  */
346  struct debug_type *context;
347};
348
349/* A variable.  This is the information we keep for a variable object.
350   This has no name; a name is associated with a variable in a
351   debug_name structure.  */
352
353struct debug_variable
354{
355  /* Kind of variable.  */
356  enum debug_var_kind kind;
357  /* Type.  */
358  debug_type type;
359  /* Value.  The interpretation of the value depends upon kind.  */
360  bfd_vma val;
361};
362
363/* A function.  This has no name; a name is associated with a function
364   in a debug_name structure.  */
365
366struct debug_function
367{
368  /* Return type.  */
369  debug_type return_type;
370  /* Parameter information.  */
371  struct debug_parameter *parameters;
372  /* Block information.  The first structure on the list is the main
373     block of the function, and describes function local variables.  */
374  struct debug_block *blocks;
375};
376
377/* A function parameter.  */
378
379struct debug_parameter
380{
381  /* Next parameter.  */
382  struct debug_parameter *next;
383  /* Name.  */
384  const char *name;
385  /* Type.  */
386  debug_type type;
387  /* Kind.  */
388  enum debug_parm_kind kind;
389  /* Value (meaning depends upon kind).  */
390  bfd_vma val;
391};
392
393/* A typed constant.  */
394
395struct debug_typed_constant
396{
397  /* Type.  */
398  debug_type type;
399  /* Value.  FIXME: We may eventually need to support non-integral
400     values.  */
401  bfd_vma val;
402};
403
404/* Information about a block within a function.  */
405
406struct debug_block
407{
408  /* Next block with the same parent.  */
409  struct debug_block *next;
410  /* Parent block.  */
411  struct debug_block *parent;
412  /* List of child blocks.  */
413  struct debug_block *children;
414  /* Start address of the block.  */
415  bfd_vma start;
416  /* End address of the block.  */
417  bfd_vma end;
418  /* Local variables.  */
419  struct debug_namespace *locals;
420};
421
422/* Line number information we keep for a compilation unit.  FIXME:
423   This structure is easy to create, but can be very space
424   inefficient.  */
425
426struct debug_lineno
427{
428  /* More line number information for this block.  */
429  struct debug_lineno *next;
430  /* Source file.  */
431  struct debug_file *file;
432  /* Line numbers, terminated by a -1 or the end of the array.  */
433#define DEBUG_LINENO_COUNT 10
434  unsigned long linenos[DEBUG_LINENO_COUNT];
435  /* Addresses for the line numbers.  */
436  bfd_vma addrs[DEBUG_LINENO_COUNT];
437};
438
439/* A namespace.  This is a mapping from names to objects.  FIXME: This
440   should be implemented as a hash table.  */
441
442struct debug_namespace
443{
444  /* List of items in this namespace.  */
445  struct debug_name *list;
446  /* Pointer to where the next item in this namespace should go.  */
447  struct debug_name **tail;
448};
449
450/* Kinds of objects that appear in a namespace.  */
451
452enum debug_object_kind
453{
454  /* A type.  */
455  DEBUG_OBJECT_TYPE,
456  /* A tagged type (really a different sort of namespace).  */
457  DEBUG_OBJECT_TAG,
458  /* A variable.  */
459  DEBUG_OBJECT_VARIABLE,
460  /* A function.  */
461  DEBUG_OBJECT_FUNCTION,
462  /* An integer constant.  */
463  DEBUG_OBJECT_INT_CONSTANT,
464  /* A floating point constant.  */
465  DEBUG_OBJECT_FLOAT_CONSTANT,
466  /* A typed constant.  */
467  DEBUG_OBJECT_TYPED_CONSTANT
468};
469
470/* Linkage of an object that appears in a namespace.  */
471
472enum debug_object_linkage
473{
474  /* Local variable.  */
475  DEBUG_LINKAGE_AUTOMATIC,
476  /* Static--either file static or function static, depending upon the
477     namespace is.  */
478  DEBUG_LINKAGE_STATIC,
479  /* Global.  */
480  DEBUG_LINKAGE_GLOBAL,
481  /* No linkage.  */
482  DEBUG_LINKAGE_NONE
483};
484
485/* A name in a namespace.  */
486
487struct debug_name
488{
489  /* Next name in this namespace.  */
490  struct debug_name *next;
491  /* Name.  */
492  const char *name;
493  /* Mark.  This is used by debug_write.  */
494  unsigned int mark;
495  /* Kind of object.  */
496  enum debug_object_kind kind;
497  /* Linkage of object.  */
498  enum debug_object_linkage linkage;
499  /* Tagged union with additional information about the object.  */
500  union
501    {
502      /* DEBUG_OBJECT_TYPE.  */
503      struct debug_type *type;
504      /* DEBUG_OBJECT_TAG.  */
505      struct debug_type *tag;
506      /* DEBUG_OBJECT_VARIABLE.  */
507      struct debug_variable *variable;
508      /* DEBUG_OBJECT_FUNCTION.  */
509      struct debug_function *function;
510      /* DEBUG_OBJECT_INT_CONSTANT.  */
511      bfd_vma int_constant;
512      /* DEBUG_OBJECT_FLOAT_CONSTANT.  */
513      double float_constant;
514      /* DEBUG_OBJECT_TYPED_CONSTANT.  */
515      struct debug_typed_constant *typed_constant;
516    } u;
517};
518
519/* During debug_write, a linked list of these structures is used to
520   keep track of ID numbers that have been assigned to classes.  */
521
522struct debug_class_id
523{
524  /* Next ID number.  */
525  struct debug_class_id *next;
526  /* The type with the ID.  */
527  struct debug_type *type;
528  /* The tag; NULL if no tag.  */
529  const char *tag;
530};
531
532/* During debug_type_samep, a linked list of these structures is kept
533   on the stack to avoid infinite recursion.  */
534
535struct debug_type_compare_list
536{
537  /* Next type on list.  */
538  struct debug_type_compare_list *next;
539  /* The types we are comparing.  */
540  struct debug_type *t1;
541  struct debug_type *t2;
542};
543
544/* During debug_get_real_type, a linked list of these structures is
545   kept on the stack to avoid infinite recursion.  */
546
547struct debug_type_real_list
548{
549  /* Next type on list.  */
550  struct debug_type_real_list *next;
551  /* The type we are checking.  */
552  struct debug_type *t;
553};
554
555/* Local functions.  */
556
557static void debug_error PARAMS ((const char *));
558static struct debug_name *debug_add_to_namespace
559  PARAMS ((struct debug_handle *, struct debug_namespace **, const char *,
560	   enum debug_object_kind, enum debug_object_linkage));
561static struct debug_name *debug_add_to_current_namespace
562  PARAMS ((struct debug_handle *, const char *, enum debug_object_kind,
563	   enum debug_object_linkage));
564static struct debug_type *debug_make_type
565  PARAMS ((struct debug_handle *, enum debug_type_kind, unsigned int));
566static struct debug_type *debug_get_real_type
567  PARAMS ((PTR, debug_type, struct debug_type_real_list *));
568static boolean debug_write_name
569  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
570	   struct debug_name *));
571static boolean debug_write_type
572  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
573	   struct debug_type *, struct debug_name *));
574static boolean debug_write_class_type
575  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
576	   struct debug_type *, const char *));
577static boolean debug_write_function
578  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
579	   const char *, enum debug_object_linkage, struct debug_function *));
580static boolean debug_write_block
581  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
582	   struct debug_block *));
583static boolean debug_write_linenos
584  PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
585	   bfd_vma));
586static boolean debug_set_class_id
587  PARAMS ((struct debug_handle *, const char *, struct debug_type *));
588static boolean debug_type_samep
589  PARAMS ((struct debug_handle *, struct debug_type *, struct debug_type *));
590static boolean debug_class_type_samep
591  PARAMS ((struct debug_handle *, struct debug_type *, struct debug_type *));
592
593/* Issue an error message.  */
594
595static void
596debug_error (message)
597     const char *message;
598{
599  fprintf (stderr, "%s\n", message);
600}
601
602/* Add an object to a namespace.  */
603
604static struct debug_name *
605debug_add_to_namespace (info, nsp, name, kind, linkage)
606     struct debug_handle *info ATTRIBUTE_UNUSED;
607     struct debug_namespace **nsp;
608     const char *name;
609     enum debug_object_kind kind;
610     enum debug_object_linkage linkage;
611{
612  struct debug_name *n;
613  struct debug_namespace *ns;
614
615  n = (struct debug_name *) xmalloc (sizeof *n);
616  memset (n, 0, sizeof *n);
617
618  n->name = name;
619  n->kind = kind;
620  n->linkage = linkage;
621
622  ns = *nsp;
623  if (ns == NULL)
624    {
625      ns = (struct debug_namespace *) xmalloc (sizeof *ns);
626      memset (ns, 0, sizeof *ns);
627
628      ns->tail = &ns->list;
629
630      *nsp = ns;
631    }
632
633  *ns->tail = n;
634  ns->tail = &n->next;
635
636  return n;
637}
638
639/* Add an object to the current namespace.  */
640
641static struct debug_name *
642debug_add_to_current_namespace (info, name, kind, linkage)
643     struct debug_handle *info;
644     const char *name;
645     enum debug_object_kind kind;
646     enum debug_object_linkage linkage;
647{
648  struct debug_namespace **nsp;
649
650  if (info->current_unit == NULL
651      || info->current_file == NULL)
652    {
653      debug_error (_("debug_add_to_current_namespace: no current file"));
654      return NULL;
655    }
656
657  if (info->current_block != NULL)
658    nsp = &info->current_block->locals;
659  else
660    nsp = &info->current_file->globals;
661
662  return debug_add_to_namespace (info, nsp, name, kind, linkage);
663}
664
665/* Return a handle for debugging information.  */
666
667PTR
668debug_init ()
669{
670  struct debug_handle *ret;
671
672  ret = (struct debug_handle *) xmalloc (sizeof *ret);
673  memset (ret, 0, sizeof *ret);
674  return (PTR) ret;
675}
676
677/* Set the source filename.  This implicitly starts a new compilation
678   unit.  */
679
680boolean
681debug_set_filename (handle, name)
682     PTR handle;
683     const char *name;
684{
685  struct debug_handle *info = (struct debug_handle *) handle;
686  struct debug_file *nfile;
687  struct debug_unit *nunit;
688
689  if (name == NULL)
690    name = "";
691
692  nfile = (struct debug_file *) xmalloc (sizeof *nfile);
693  memset (nfile, 0, sizeof *nfile);
694
695  nfile->filename = name;
696
697  nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
698  memset (nunit, 0, sizeof *nunit);
699
700  nunit->files = nfile;
701  info->current_file = nfile;
702
703  if (info->current_unit != NULL)
704    info->current_unit->next = nunit;
705  else
706    {
707      assert (info->units == NULL);
708      info->units = nunit;
709    }
710
711  info->current_unit = nunit;
712
713  info->current_function = NULL;
714  info->current_block = NULL;
715  info->current_lineno = NULL;
716
717  return true;
718}
719
720/* Change source files to the given file name.  This is used for
721   include files in a single compilation unit.  */
722
723boolean
724debug_start_source (handle, name)
725     PTR handle;
726     const char *name;
727{
728  struct debug_handle *info = (struct debug_handle *) handle;
729  struct debug_file *f, **pf;
730
731  if (name == NULL)
732    name = "";
733
734  if (info->current_unit == NULL)
735    {
736      debug_error (_("debug_start_source: no debug_set_filename call"));
737      return false;
738    }
739
740  for (f = info->current_unit->files; f != NULL; f = f->next)
741    {
742      if (f->filename[0] == name[0]
743	  && f->filename[1] == name[1]
744	  && strcmp (f->filename, name) == 0)
745	{
746	  info->current_file = f;
747	  return true;
748	}
749    }
750
751  f = (struct debug_file *) xmalloc (sizeof *f);
752  memset (f, 0, sizeof *f);
753
754  f->filename = name;
755
756  for (pf = &info->current_file->next;
757       *pf != NULL;
758       pf = &(*pf)->next)
759    ;
760  *pf = f;
761
762  info->current_file = f;
763
764  return true;
765}
766
767/* Record a function definition.  This implicitly starts a function
768   block.  The debug_type argument is the type of the return value.
769   The boolean indicates whether the function is globally visible.
770   The bfd_vma is the address of the start of the function.  Currently
771   the parameter types are specified by calls to
772   debug_record_parameter.  FIXME: There is no way to specify nested
773   functions.  */
774
775boolean
776debug_record_function (handle, name, return_type, global, addr)
777     PTR handle;
778     const char *name;
779     debug_type return_type;
780     boolean global;
781     bfd_vma addr;
782{
783  struct debug_handle *info = (struct debug_handle *) handle;
784  struct debug_function *f;
785  struct debug_block *b;
786  struct debug_name *n;
787
788  if (name == NULL)
789    name = "";
790  if (return_type == NULL)
791    return false;
792
793  if (info->current_unit == NULL)
794    {
795      debug_error (_("debug_record_function: no debug_set_filename call"));
796      return false;
797    }
798
799  f = (struct debug_function *) xmalloc (sizeof *f);
800  memset (f, 0, sizeof *f);
801
802  f->return_type = return_type;
803
804  b = (struct debug_block *) xmalloc (sizeof *b);
805  memset (b, 0, sizeof *b);
806
807  b->start = addr;
808  b->end = (bfd_vma) -1;
809
810  f->blocks = b;
811
812  info->current_function = f;
813  info->current_block = b;
814
815  /* FIXME: If we could handle nested functions, this would be the
816     place: we would want to use a different namespace.  */
817  n = debug_add_to_namespace (info,
818			      &info->current_file->globals,
819			      name,
820			      DEBUG_OBJECT_FUNCTION,
821			      (global
822			       ? DEBUG_LINKAGE_GLOBAL
823			       : DEBUG_LINKAGE_STATIC));
824  if (n == NULL)
825    return false;
826
827  n->u.function = f;
828
829  return true;
830}
831
832/* Record a parameter for the current function.  */
833
834boolean
835debug_record_parameter (handle, name, type, kind, val)
836     PTR handle;
837     const char *name;
838     debug_type type;
839     enum debug_parm_kind kind;
840     bfd_vma val;
841{
842  struct debug_handle *info = (struct debug_handle *) handle;
843  struct debug_parameter *p, **pp;
844
845  if (name == NULL || type == NULL)
846    return false;
847
848  if (info->current_unit == NULL
849      || info->current_function == NULL)
850    {
851      debug_error (_("debug_record_parameter: no current function"));
852      return false;
853    }
854
855  p = (struct debug_parameter *) xmalloc (sizeof *p);
856  memset (p, 0, sizeof *p);
857
858  p->name = name;
859  p->type = type;
860  p->kind = kind;
861  p->val = val;
862
863  for (pp = &info->current_function->parameters;
864       *pp != NULL;
865       pp = &(*pp)->next)
866    ;
867  *pp = p;
868
869  return true;
870}
871
872/* End a function.  FIXME: This should handle function nesting.  */
873
874boolean
875debug_end_function (handle, addr)
876     PTR handle;
877     bfd_vma addr;
878{
879  struct debug_handle *info = (struct debug_handle *) handle;
880
881  if (info->current_unit == NULL
882      || info->current_block == NULL
883      || info->current_function == NULL)
884    {
885      debug_error (_("debug_end_function: no current function"));
886      return false;
887    }
888
889  if (info->current_block->parent != NULL)
890    {
891      debug_error (_("debug_end_function: some blocks were not closed"));
892      return false;
893    }
894
895  info->current_block->end = addr;
896
897  info->current_function = NULL;
898  info->current_block = NULL;
899
900  return true;
901}
902
903/* Start a block in a function.  All local information will be
904   recorded in this block, until the matching call to debug_end_block.
905   debug_start_block and debug_end_block may be nested.  The bfd_vma
906   argument is the address at which this block starts.  */
907
908boolean
909debug_start_block (handle, addr)
910     PTR handle;
911     bfd_vma addr;
912{
913  struct debug_handle *info = (struct debug_handle *) handle;
914  struct debug_block *b, **pb;
915
916  /* We must always have a current block: debug_record_function sets
917     one up.  */
918  if (info->current_unit == NULL
919      || info->current_block == NULL)
920    {
921      debug_error (_("debug_start_block: no current block"));
922      return false;
923    }
924
925  b = (struct debug_block *) xmalloc (sizeof *b);
926  memset (b, 0, sizeof *b);
927
928  b->parent = info->current_block;
929  b->start = addr;
930  b->end = (bfd_vma) -1;
931
932  /* This new block is a child of the current block.  */
933  for (pb = &info->current_block->children;
934       *pb != NULL;
935       pb = &(*pb)->next)
936    ;
937  *pb = b;
938
939  info->current_block = b;
940
941  return true;
942}
943
944/* Finish a block in a function.  This matches the call to
945   debug_start_block.  The argument is the address at which this block
946   ends.  */
947
948boolean
949debug_end_block (handle, addr)
950     PTR handle;
951     bfd_vma addr;
952{
953  struct debug_handle *info = (struct debug_handle *) handle;
954  struct debug_block *parent;
955
956  if (info->current_unit == NULL
957      || info->current_block == NULL)
958    {
959      debug_error (_("debug_end_block: no current block"));
960      return false;
961    }
962
963  parent = info->current_block->parent;
964  if (parent == NULL)
965    {
966      debug_error (_("debug_end_block: attempt to close top level block"));
967      return false;
968    }
969
970  info->current_block->end = addr;
971
972  info->current_block = parent;
973
974  return true;
975}
976
977/* Associate a line number in the current source file and function
978   with a given address.  */
979
980boolean
981debug_record_line (handle, lineno, addr)
982     PTR handle;
983     unsigned long lineno;
984     bfd_vma addr;
985{
986  struct debug_handle *info = (struct debug_handle *) handle;
987  struct debug_lineno *l;
988  unsigned int i;
989
990  if (info->current_unit == NULL)
991    {
992      debug_error (_("debug_record_line: no current unit"));
993      return false;
994    }
995
996  l = info->current_lineno;
997  if (l != NULL && l->file == info->current_file)
998    {
999      for (i = 0; i < DEBUG_LINENO_COUNT; i++)
1000	{
1001	  if (l->linenos[i] == (unsigned long) -1)
1002	    {
1003	      l->linenos[i] = lineno;
1004	      l->addrs[i] = addr;
1005	      return true;
1006	    }
1007	}
1008    }
1009
1010  /* If we get here, then either 1) there is no current_lineno
1011     structure, which means this is the first line number in this
1012     compilation unit, 2) the current_lineno structure is for a
1013     different file, or 3) the current_lineno structure is full.
1014     Regardless, we want to allocate a new debug_lineno structure, put
1015     it in the right place, and make it the new current_lineno
1016     structure.  */
1017
1018  l = (struct debug_lineno *) xmalloc (sizeof *l);
1019  memset (l, 0, sizeof *l);
1020
1021  l->file = info->current_file;
1022  l->linenos[0] = lineno;
1023  l->addrs[0] = addr;
1024  for (i = 1; i < DEBUG_LINENO_COUNT; i++)
1025    l->linenos[i] = (unsigned long) -1;
1026
1027  if (info->current_lineno != NULL)
1028    info->current_lineno->next = l;
1029  else
1030    info->current_unit->linenos = l;
1031
1032  info->current_lineno = l;
1033
1034  return true;
1035}
1036
1037/* Start a named common block.  This is a block of variables that may
1038   move in memory.  */
1039
1040boolean
1041debug_start_common_block (handle, name)
1042     PTR handle ATTRIBUTE_UNUSED;
1043     const char *name ATTRIBUTE_UNUSED;
1044{
1045  /* FIXME */
1046  debug_error (_("debug_start_common_block: not implemented"));
1047  return false;
1048}
1049
1050/* End a named common block.  */
1051
1052boolean
1053debug_end_common_block (handle, name)
1054     PTR handle ATTRIBUTE_UNUSED;
1055     const char *name ATTRIBUTE_UNUSED;
1056{
1057  /* FIXME */
1058  debug_error (_("debug_end_common_block: not implemented"));
1059  return false;
1060}
1061
1062/* Record a named integer constant.  */
1063
1064boolean
1065debug_record_int_const (handle, name, val)
1066     PTR handle;
1067     const char *name;
1068     bfd_vma val;
1069{
1070  struct debug_handle *info = (struct debug_handle *) handle;
1071  struct debug_name *n;
1072
1073  if (name == NULL)
1074    return false;
1075
1076  n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1077				      DEBUG_LINKAGE_NONE);
1078  if (n == NULL)
1079    return false;
1080
1081  n->u.int_constant = val;
1082
1083  return true;
1084}
1085
1086/* Record a named floating point constant.  */
1087
1088boolean
1089debug_record_float_const (handle, name, val)
1090     PTR handle;
1091     const char *name;
1092     double val;
1093{
1094  struct debug_handle *info = (struct debug_handle *) handle;
1095  struct debug_name *n;
1096
1097  if (name == NULL)
1098    return false;
1099
1100  n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1101				      DEBUG_LINKAGE_NONE);
1102  if (n == NULL)
1103    return false;
1104
1105  n->u.float_constant = val;
1106
1107  return true;
1108}
1109
1110/* Record a typed constant with an integral value.  */
1111
1112boolean
1113debug_record_typed_const (handle, name, type, val)
1114     PTR handle;
1115     const char *name;
1116     debug_type type;
1117     bfd_vma val;
1118{
1119  struct debug_handle *info = (struct debug_handle *) handle;
1120  struct debug_name *n;
1121  struct debug_typed_constant *tc;
1122
1123  if (name == NULL || type == NULL)
1124    return false;
1125
1126  n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1127				      DEBUG_LINKAGE_NONE);
1128  if (n == NULL)
1129    return false;
1130
1131  tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
1132  memset (tc, 0, sizeof *tc);
1133
1134  tc->type = type;
1135  tc->val = val;
1136
1137  n->u.typed_constant = tc;
1138
1139  return true;
1140}
1141
1142/* Record a label.  */
1143
1144boolean
1145debug_record_label (handle, name, type, addr)
1146     PTR handle ATTRIBUTE_UNUSED;
1147     const char *name ATTRIBUTE_UNUSED;
1148     debug_type type ATTRIBUTE_UNUSED;
1149     bfd_vma addr ATTRIBUTE_UNUSED;
1150{
1151  /* FIXME.  */
1152  debug_error (_("debug_record_label: not implemented"));
1153  return false;
1154}
1155
1156/* Record a variable.  */
1157
1158boolean
1159debug_record_variable (handle, name, type, kind, val)
1160     PTR handle;
1161     const char *name;
1162     debug_type type;
1163     enum debug_var_kind kind;
1164     bfd_vma val;
1165{
1166  struct debug_handle *info = (struct debug_handle *) handle;
1167  struct debug_namespace **nsp;
1168  enum debug_object_linkage linkage;
1169  struct debug_name *n;
1170  struct debug_variable *v;
1171
1172  if (name == NULL || type == NULL)
1173    return false;
1174
1175  if (info->current_unit == NULL
1176      || info->current_file == NULL)
1177    {
1178      debug_error (_("debug_record_variable: no current file"));
1179      return false;
1180    }
1181
1182  if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1183    {
1184      nsp = &info->current_file->globals;
1185      if (kind == DEBUG_GLOBAL)
1186	linkage = DEBUG_LINKAGE_GLOBAL;
1187      else
1188	linkage = DEBUG_LINKAGE_STATIC;
1189    }
1190  else
1191    {
1192      if (info->current_block == NULL)
1193	{
1194	  debug_error (_("debug_record_variable: no current block"));
1195	  return false;
1196	}
1197      nsp = &info->current_block->locals;
1198      linkage = DEBUG_LINKAGE_AUTOMATIC;
1199    }
1200
1201  n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1202  if (n == NULL)
1203    return false;
1204
1205  v = (struct debug_variable *) xmalloc (sizeof *v);
1206  memset (v, 0, sizeof *v);
1207
1208  v->kind = kind;
1209  v->type = type;
1210  v->val = val;
1211
1212  n->u.variable = v;
1213
1214  return true;
1215}
1216
1217/* Make a type with a given kind and size.  */
1218
1219static struct debug_type *
1220debug_make_type (info, kind, size)
1221     struct debug_handle *info ATTRIBUTE_UNUSED;
1222     enum debug_type_kind kind;
1223     unsigned int size;
1224{
1225  struct debug_type *t;
1226
1227  t = (struct debug_type *) xmalloc (sizeof *t);
1228  memset (t, 0, sizeof *t);
1229
1230  t->kind = kind;
1231  t->size = size;
1232
1233  return t;
1234}
1235
1236/* Make an indirect type which may be used as a placeholder for a type
1237   which is referenced before it is defined.  */
1238
1239debug_type
1240debug_make_indirect_type (handle, slot, tag)
1241     PTR handle;
1242     debug_type *slot;
1243     const char *tag;
1244{
1245  struct debug_handle *info = (struct debug_handle *) handle;
1246  struct debug_type *t;
1247  struct debug_indirect_type *i;
1248
1249  t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1250  if (t == NULL)
1251    return DEBUG_TYPE_NULL;
1252
1253  i = (struct debug_indirect_type *) xmalloc (sizeof *i);
1254  memset (i, 0, sizeof *i);
1255
1256  i->slot = slot;
1257  i->tag = tag;
1258
1259  t->u.kindirect = i;
1260
1261  return t;
1262}
1263
1264/* Make a void type.  There is only one of these.  */
1265
1266debug_type
1267debug_make_void_type (handle)
1268     PTR handle;
1269{
1270  struct debug_handle *info = (struct debug_handle *) handle;
1271
1272  return debug_make_type (info, DEBUG_KIND_VOID, 0);
1273}
1274
1275/* Make an integer type of a given size.  The boolean argument is true
1276   if the integer is unsigned.  */
1277
1278debug_type
1279debug_make_int_type (handle, size, unsignedp)
1280     PTR handle;
1281     unsigned int size;
1282     boolean unsignedp;
1283{
1284  struct debug_handle *info = (struct debug_handle *) handle;
1285  struct debug_type *t;
1286
1287  t = debug_make_type (info, DEBUG_KIND_INT, size);
1288  if (t == NULL)
1289    return DEBUG_TYPE_NULL;
1290
1291  t->u.kint = unsignedp;
1292
1293  return t;
1294}
1295
1296/* Make a floating point type of a given size.  FIXME: On some
1297   platforms, like an Alpha, you probably need to be able to specify
1298   the format.  */
1299
1300debug_type
1301debug_make_float_type (handle, size)
1302     PTR handle;
1303     unsigned int size;
1304{
1305  struct debug_handle *info = (struct debug_handle *) handle;
1306
1307  return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1308}
1309
1310/* Make a boolean type of a given size.  */
1311
1312debug_type
1313debug_make_bool_type (handle, size)
1314     PTR handle;
1315     unsigned int size;
1316{
1317  struct debug_handle *info = (struct debug_handle *) handle;
1318
1319  return debug_make_type (info, DEBUG_KIND_BOOL, size);
1320}
1321
1322/* Make a complex type of a given size.  */
1323
1324debug_type
1325debug_make_complex_type (handle, size)
1326     PTR handle;
1327     unsigned int size;
1328{
1329  struct debug_handle *info = (struct debug_handle *) handle;
1330
1331  return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1332}
1333
1334/* Make a structure type.  The second argument is true for a struct,
1335   false for a union.  The third argument is the size of the struct.
1336   The fourth argument is a NULL terminated array of fields.  */
1337
1338debug_type
1339debug_make_struct_type (handle, structp, size, fields)
1340     PTR handle;
1341     boolean structp;
1342     bfd_vma size;
1343     debug_field *fields;
1344{
1345  struct debug_handle *info = (struct debug_handle *) handle;
1346  struct debug_type *t;
1347  struct debug_class_type *c;
1348
1349  t = debug_make_type (info,
1350		       structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1351		       size);
1352  if (t == NULL)
1353    return DEBUG_TYPE_NULL;
1354
1355  c = (struct debug_class_type *) xmalloc (sizeof *c);
1356  memset (c, 0, sizeof *c);
1357
1358  c->fields = fields;
1359
1360  t->u.kclass = c;
1361
1362  return t;
1363}
1364
1365/* Make an object type.  The first three arguments after the handle
1366   are the same as for debug_make_struct_type.  The next arguments are
1367   a NULL terminated array of base classes, a NULL terminated array of
1368   methods, the type of the object holding the virtual function table
1369   if it is not this object, and a boolean which is true if this
1370   object has its own virtual function table.  */
1371
1372debug_type
1373debug_make_object_type (handle, structp, size, fields, baseclasses,
1374			methods, vptrbase, ownvptr)
1375     PTR handle;
1376     boolean structp;
1377     bfd_vma size;
1378     debug_field *fields;
1379     debug_baseclass *baseclasses;
1380     debug_method *methods;
1381     debug_type vptrbase;
1382     boolean ownvptr;
1383{
1384  struct debug_handle *info = (struct debug_handle *) handle;
1385  struct debug_type *t;
1386  struct debug_class_type *c;
1387
1388  t = debug_make_type (info,
1389		       structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1390		       size);
1391  if (t == NULL)
1392    return DEBUG_TYPE_NULL;
1393
1394  c = (struct debug_class_type *) xmalloc (sizeof *c);
1395  memset (c, 0, sizeof *c);
1396
1397  c->fields = fields;
1398  c->baseclasses = baseclasses;
1399  c->methods = methods;
1400  if (ownvptr)
1401    c->vptrbase = t;
1402  else
1403    c->vptrbase = vptrbase;
1404
1405  t->u.kclass = c;
1406
1407  return t;
1408}
1409
1410/* Make an enumeration type.  The arguments are a null terminated
1411   array of strings, and an array of corresponding values.  */
1412
1413debug_type
1414debug_make_enum_type (handle, names, values)
1415     PTR handle;
1416     const char **names;
1417     bfd_signed_vma *values;
1418{
1419  struct debug_handle *info = (struct debug_handle *) handle;
1420  struct debug_type *t;
1421  struct debug_enum_type *e;
1422
1423  t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1424  if (t == NULL)
1425    return DEBUG_TYPE_NULL;
1426
1427  e = (struct debug_enum_type *) xmalloc (sizeof *e);
1428  memset (e, 0, sizeof *e);
1429
1430  e->names = names;
1431  e->values = values;
1432
1433  t->u.kenum = e;
1434
1435  return t;
1436}
1437
1438/* Make a pointer to a given type.  */
1439
1440debug_type
1441debug_make_pointer_type (handle, type)
1442     PTR handle;
1443     debug_type type;
1444{
1445  struct debug_handle *info = (struct debug_handle *) handle;
1446  struct debug_type *t;
1447
1448  if (type == NULL)
1449    return DEBUG_TYPE_NULL;
1450
1451  if (type->pointer != DEBUG_TYPE_NULL)
1452    return type->pointer;
1453
1454  t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1455  if (t == NULL)
1456    return DEBUG_TYPE_NULL;
1457
1458  t->u.kpointer = type;
1459
1460  type->pointer = t;
1461
1462  return t;
1463}
1464
1465/* Make a function returning a given type.  FIXME: We should be able
1466   to record the parameter types.  */
1467
1468debug_type
1469debug_make_function_type (handle, type, arg_types, varargs)
1470     PTR handle;
1471     debug_type type;
1472     debug_type *arg_types;
1473     boolean varargs;
1474{
1475  struct debug_handle *info = (struct debug_handle *) handle;
1476  struct debug_type *t;
1477  struct debug_function_type *f;
1478
1479  if (type == NULL)
1480    return DEBUG_TYPE_NULL;
1481
1482  t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1483  if (t == NULL)
1484    return DEBUG_TYPE_NULL;
1485
1486  f = (struct debug_function_type *) xmalloc (sizeof *f);
1487  memset (f, 0, sizeof *f);
1488
1489  f->return_type = type;
1490  f->arg_types = arg_types;
1491  f->varargs = varargs;
1492
1493  t->u.kfunction = f;
1494
1495  return t;
1496}
1497
1498/* Make a reference to a given type.  */
1499
1500debug_type
1501debug_make_reference_type (handle, type)
1502     PTR handle;
1503     debug_type type;
1504{
1505  struct debug_handle *info = (struct debug_handle *) handle;
1506  struct debug_type *t;
1507
1508  if (type == NULL)
1509    return DEBUG_TYPE_NULL;
1510
1511  t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1512  if (t == NULL)
1513    return DEBUG_TYPE_NULL;
1514
1515  t->u.kreference = type;
1516
1517  return t;
1518}
1519
1520/* Make a range of a given type from a lower to an upper bound.  */
1521
1522debug_type
1523debug_make_range_type (handle, type, lower, upper)
1524     PTR handle;
1525     debug_type type;
1526     bfd_signed_vma lower;
1527     bfd_signed_vma upper;
1528{
1529  struct debug_handle *info = (struct debug_handle *) handle;
1530  struct debug_type *t;
1531  struct debug_range_type *r;
1532
1533  if (type == NULL)
1534    return DEBUG_TYPE_NULL;
1535
1536  t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1537  if (t == NULL)
1538    return DEBUG_TYPE_NULL;
1539
1540  r = (struct debug_range_type *) xmalloc (sizeof *r);
1541  memset (r, 0, sizeof *r);
1542
1543  r->type = type;
1544  r->lower = lower;
1545  r->upper = upper;
1546
1547  t->u.krange = r;
1548
1549  return t;
1550}
1551
1552/* Make an array type.  The second argument is the type of an element
1553   of the array.  The third argument is the type of a range of the
1554   array.  The fourth and fifth argument are the lower and upper
1555   bounds, respectively.  The sixth argument is true if this array is
1556   actually a string, as in C.  */
1557
1558debug_type
1559debug_make_array_type (handle, element_type, range_type, lower, upper,
1560		       stringp)
1561     PTR handle;
1562     debug_type element_type;
1563     debug_type range_type;
1564     bfd_signed_vma lower;
1565     bfd_signed_vma upper;
1566     boolean stringp;
1567{
1568  struct debug_handle *info = (struct debug_handle *) handle;
1569  struct debug_type *t;
1570  struct debug_array_type *a;
1571
1572  if (element_type == NULL || range_type == NULL)
1573    return DEBUG_TYPE_NULL;
1574
1575  t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1576  if (t == NULL)
1577    return DEBUG_TYPE_NULL;
1578
1579  a = (struct debug_array_type *) xmalloc (sizeof *a);
1580  memset (a, 0, sizeof *a);
1581
1582  a->element_type = element_type;
1583  a->range_type = range_type;
1584  a->lower = lower;
1585  a->upper = upper;
1586  a->stringp = stringp;
1587
1588  t->u.karray = a;
1589
1590  return t;
1591}
1592
1593/* Make a set of a given type.  For example, a Pascal set type.  The
1594   boolean argument is true if this set is actually a bitstring, as in
1595   CHILL.  */
1596
1597debug_type
1598debug_make_set_type (handle, type, bitstringp)
1599     PTR handle;
1600     debug_type type;
1601     boolean bitstringp;
1602{
1603  struct debug_handle *info = (struct debug_handle *) handle;
1604  struct debug_type *t;
1605  struct debug_set_type *s;
1606
1607  if (type == NULL)
1608    return DEBUG_TYPE_NULL;
1609
1610  t = debug_make_type (info, DEBUG_KIND_SET, 0);
1611  if (t == NULL)
1612    return DEBUG_TYPE_NULL;
1613
1614  s = (struct debug_set_type *) xmalloc (sizeof *s);
1615  memset (s, 0, sizeof *s);
1616
1617  s->type = type;
1618  s->bitstringp = bitstringp;
1619
1620  t->u.kset = s;
1621
1622  return t;
1623}
1624
1625/* Make a type for a pointer which is relative to an object.  The
1626   second argument is the type of the object to which the pointer is
1627   relative.  The third argument is the type that the pointer points
1628   to.  */
1629
1630debug_type
1631debug_make_offset_type (handle, base_type, target_type)
1632     PTR handle;
1633     debug_type base_type;
1634     debug_type target_type;
1635{
1636  struct debug_handle *info = (struct debug_handle *) handle;
1637  struct debug_type *t;
1638  struct debug_offset_type *o;
1639
1640  if (base_type == NULL || target_type == NULL)
1641    return DEBUG_TYPE_NULL;
1642
1643  t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1644  if (t == NULL)
1645    return DEBUG_TYPE_NULL;
1646
1647  o = (struct debug_offset_type *) xmalloc (sizeof *o);
1648  memset (o, 0, sizeof *o);
1649
1650  o->base_type = base_type;
1651  o->target_type = target_type;
1652
1653  t->u.koffset = o;
1654
1655  return t;
1656}
1657
1658/* Make a type for a method function.  The second argument is the
1659   return type, the third argument is the domain, and the fourth
1660   argument is a NULL terminated array of argument types.  */
1661
1662debug_type
1663debug_make_method_type (handle, return_type, domain_type, arg_types, varargs)
1664     PTR handle;
1665     debug_type return_type;
1666     debug_type domain_type;
1667     debug_type *arg_types;
1668     boolean varargs;
1669{
1670  struct debug_handle *info = (struct debug_handle *) handle;
1671  struct debug_type *t;
1672  struct debug_method_type *m;
1673
1674  if (return_type == NULL)
1675    return DEBUG_TYPE_NULL;
1676
1677  t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1678  if (t == NULL)
1679    return DEBUG_TYPE_NULL;
1680
1681  m = (struct debug_method_type *) xmalloc (sizeof *m);
1682  memset (m, 0, sizeof *m);
1683
1684  m->return_type = return_type;
1685  m->domain_type = domain_type;
1686  m->arg_types = arg_types;
1687  m->varargs = varargs;
1688
1689  t->u.kmethod = m;
1690
1691  return t;
1692}
1693
1694/* Make a const qualified version of a given type.  */
1695
1696debug_type
1697debug_make_const_type (handle, type)
1698     PTR handle;
1699     debug_type type;
1700{
1701  struct debug_handle *info = (struct debug_handle *) handle;
1702  struct debug_type *t;
1703
1704  if (type == NULL)
1705    return DEBUG_TYPE_NULL;
1706
1707  t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1708  if (t == NULL)
1709    return DEBUG_TYPE_NULL;
1710
1711  t->u.kconst = type;
1712
1713  return t;
1714}
1715
1716/* Make a volatile qualified version of a given type.  */
1717
1718debug_type
1719debug_make_volatile_type (handle, type)
1720     PTR handle;
1721     debug_type type;
1722{
1723  struct debug_handle *info = (struct debug_handle *) handle;
1724  struct debug_type *t;
1725
1726  if (type == NULL)
1727    return DEBUG_TYPE_NULL;
1728
1729  t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1730  if (t == NULL)
1731    return DEBUG_TYPE_NULL;
1732
1733  t->u.kvolatile = type;
1734
1735  return t;
1736}
1737
1738/* Make an undefined tagged type.  For example, a struct which has
1739   been mentioned, but not defined.  */
1740
1741debug_type
1742debug_make_undefined_tagged_type (handle, name, kind)
1743     PTR handle;
1744     const char *name;
1745     enum debug_type_kind kind;
1746{
1747  struct debug_handle *info = (struct debug_handle *) handle;
1748  struct debug_type *t;
1749
1750  if (name == NULL)
1751    return DEBUG_TYPE_NULL;
1752
1753  switch (kind)
1754    {
1755    case DEBUG_KIND_STRUCT:
1756    case DEBUG_KIND_UNION:
1757    case DEBUG_KIND_CLASS:
1758    case DEBUG_KIND_UNION_CLASS:
1759    case DEBUG_KIND_ENUM:
1760      break;
1761
1762    default:
1763      debug_error (_("debug_make_undefined_type: unsupported kind"));
1764      return DEBUG_TYPE_NULL;
1765    }
1766
1767  t = debug_make_type (info, kind, 0);
1768  if (t == NULL)
1769    return DEBUG_TYPE_NULL;
1770
1771  return debug_tag_type (handle, name, t);
1772}
1773
1774/* Make a base class for an object.  The second argument is the base
1775   class type.  The third argument is the bit position of this base
1776   class in the object (always 0 unless doing multiple inheritance).
1777   The fourth argument is whether this is a virtual class.  The fifth
1778   argument is the visibility of the base class.  */
1779
1780debug_baseclass
1781debug_make_baseclass (handle, type, bitpos, virtual, visibility)
1782     PTR handle ATTRIBUTE_UNUSED;
1783     debug_type type;
1784     bfd_vma bitpos;
1785     boolean virtual;
1786     enum debug_visibility visibility;
1787{
1788  struct debug_baseclass *b;
1789
1790  b = (struct debug_baseclass *) xmalloc (sizeof *b);
1791  memset (b, 0, sizeof *b);
1792
1793  b->type = type;
1794  b->bitpos = bitpos;
1795  b->virtual = virtual;
1796  b->visibility = visibility;
1797
1798  return b;
1799}
1800
1801/* Make a field for a struct.  The second argument is the name.  The
1802   third argument is the type of the field.  The fourth argument is
1803   the bit position of the field.  The fifth argument is the size of
1804   the field (it may be zero).  The sixth argument is the visibility
1805   of the field.  */
1806
1807debug_field
1808debug_make_field (handle, name, type, bitpos, bitsize, visibility)
1809     PTR handle ATTRIBUTE_UNUSED;
1810     const char *name;
1811     debug_type type;
1812     bfd_vma bitpos;
1813     bfd_vma bitsize;
1814     enum debug_visibility visibility;
1815{
1816  struct debug_field *f;
1817
1818  f = (struct debug_field *) xmalloc (sizeof *f);
1819  memset (f, 0, sizeof *f);
1820
1821  f->name = name;
1822  f->type = type;
1823  f->static_member = false;
1824  f->u.f.bitpos = bitpos;
1825  f->u.f.bitsize = bitsize;
1826  f->visibility = visibility;
1827
1828  return f;
1829}
1830
1831/* Make a static member of an object.  The second argument is the
1832   name.  The third argument is the type of the member.  The fourth
1833   argument is the physical name of the member (i.e., the name as a
1834   global variable).  The fifth argument is the visibility of the
1835   member.  */
1836
1837debug_field
1838debug_make_static_member (handle, name, type, physname, visibility)
1839     PTR handle ATTRIBUTE_UNUSED;
1840     const char *name;
1841     debug_type type;
1842     const char *physname;
1843     enum debug_visibility visibility;
1844{
1845  struct debug_field *f;
1846
1847  f = (struct debug_field *) xmalloc (sizeof *f);
1848  memset (f, 0, sizeof *f);
1849
1850  f->name = name;
1851  f->type = type;
1852  f->static_member = true;
1853  f->u.s.physname = physname;
1854  f->visibility = visibility;
1855
1856  return f;
1857}
1858
1859/* Make a method.  The second argument is the name, and the third
1860   argument is a NULL terminated array of method variants.  */
1861
1862debug_method
1863debug_make_method (handle, name, variants)
1864     PTR handle ATTRIBUTE_UNUSED;
1865     const char *name;
1866     debug_method_variant *variants;
1867{
1868  struct debug_method *m;
1869
1870  m = (struct debug_method *) xmalloc (sizeof *m);
1871  memset (m, 0, sizeof *m);
1872
1873  m->name = name;
1874  m->variants = variants;
1875
1876  return m;
1877}
1878
1879/* Make a method argument.  The second argument is the real name of
1880   the function.  The third argument is the type of the function.  The
1881   fourth argument is the visibility.  The fifth argument is whether
1882   this is a const function.  The sixth argument is whether this is a
1883   volatile function.  The seventh argument is the offset in the
1884   virtual function table, if any.  The eighth argument is the virtual
1885   function context.  FIXME: Are the const and volatile arguments
1886   necessary?  Could we just use debug_make_const_type?  */
1887
1888debug_method_variant
1889debug_make_method_variant (handle, physname, type, visibility, constp,
1890			   volatilep, voffset, context)
1891     PTR handle ATTRIBUTE_UNUSED;
1892     const char *physname;
1893     debug_type type;
1894     enum debug_visibility visibility;
1895     boolean constp;
1896     boolean volatilep;
1897     bfd_vma voffset;
1898     debug_type context;
1899{
1900  struct debug_method_variant *m;
1901
1902  m = (struct debug_method_variant *) xmalloc (sizeof *m);
1903  memset (m, 0, sizeof *m);
1904
1905  m->physname = physname;
1906  m->type = type;
1907  m->visibility = visibility;
1908  m->constp = constp;
1909  m->volatilep = volatilep;
1910  m->voffset = voffset;
1911  m->context = context;
1912
1913  return m;
1914}
1915
1916/* Make a static method argument.  The arguments are the same as for
1917   debug_make_method_variant, except that the last two are omitted
1918   since a static method can not also be virtual.  */
1919
1920debug_method_variant
1921debug_make_static_method_variant (handle, physname, type, visibility,
1922				  constp, volatilep)
1923     PTR handle ATTRIBUTE_UNUSED;
1924     const char *physname;
1925     debug_type type;
1926     enum debug_visibility visibility;
1927     boolean constp;
1928     boolean volatilep;
1929{
1930  struct debug_method_variant *m;
1931
1932  m = (struct debug_method_variant *) xmalloc (sizeof *m);
1933  memset (m, 0, sizeof *m);
1934
1935  m->physname = physname;
1936  m->type = type;
1937  m->visibility = visibility;
1938  m->constp = constp;
1939  m->volatilep = volatilep;
1940  m->voffset = VOFFSET_STATIC_METHOD;
1941
1942  return m;
1943}
1944
1945/* Name a type.  */
1946
1947debug_type
1948debug_name_type (handle, name, type)
1949     PTR handle;
1950     const char *name;
1951     debug_type type;
1952{
1953  struct debug_handle *info = (struct debug_handle *) handle;
1954  struct debug_type *t;
1955  struct debug_named_type *n;
1956  struct debug_name *nm;
1957
1958  if (name == NULL || type == NULL)
1959    return DEBUG_TYPE_NULL;
1960
1961  if (info->current_unit == NULL
1962      || info->current_file == NULL)
1963    {
1964      debug_error (_("debug_name_type: no current file"));
1965      return DEBUG_TYPE_NULL;
1966    }
1967
1968  t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1969  if (t == NULL)
1970    return DEBUG_TYPE_NULL;
1971
1972  n = (struct debug_named_type *) xmalloc (sizeof *n);
1973  memset (n, 0, sizeof *n);
1974
1975  n->type = type;
1976
1977  t->u.knamed = n;
1978
1979  /* We always add the name to the global namespace.  This is probably
1980     wrong in some cases, but it seems to be right for stabs.  FIXME.  */
1981
1982  nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1983			       DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1984  if (nm == NULL)
1985    return DEBUG_TYPE_NULL;
1986
1987  nm->u.type = t;
1988
1989  n->name = nm;
1990
1991  return t;
1992}
1993
1994/* Tag a type.  */
1995
1996debug_type
1997debug_tag_type (handle, name, type)
1998     PTR handle;
1999     const char *name;
2000     debug_type type;
2001{
2002  struct debug_handle *info = (struct debug_handle *) handle;
2003  struct debug_type *t;
2004  struct debug_named_type *n;
2005  struct debug_name *nm;
2006
2007  if (name == NULL || type == NULL)
2008    return DEBUG_TYPE_NULL;
2009
2010  if (info->current_file == NULL)
2011    {
2012      debug_error (_("debug_tag_type: no current file"));
2013      return DEBUG_TYPE_NULL;
2014    }
2015
2016  if (type->kind == DEBUG_KIND_TAGGED)
2017    {
2018      if (strcmp (type->u.knamed->name->name, name) == 0)
2019	return type;
2020      debug_error (_("debug_tag_type: extra tag attempted"));
2021      return DEBUG_TYPE_NULL;
2022    }
2023
2024  t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
2025  if (t == NULL)
2026    return DEBUG_TYPE_NULL;
2027
2028  n = (struct debug_named_type *) xmalloc (sizeof *n);
2029  memset (n, 0, sizeof *n);
2030
2031  n->type = type;
2032
2033  t->u.knamed = n;
2034
2035  /* We keep a global namespace of tags for each compilation unit.  I
2036     don't know if that is the right thing to do.  */
2037
2038  nm = debug_add_to_namespace (info, &info->current_file->globals, name,
2039			       DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
2040  if (nm == NULL)
2041    return DEBUG_TYPE_NULL;
2042
2043  nm->u.tag = t;
2044
2045  n->name = nm;
2046
2047  return t;
2048}
2049
2050/* Record the size of a given type.  */
2051
2052boolean
2053debug_record_type_size (handle, type, size)
2054     PTR handle ATTRIBUTE_UNUSED;
2055     debug_type type;
2056     unsigned int size;
2057{
2058  if (type->size != 0 && type->size != size)
2059    fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
2060	     type->size, size);
2061
2062  type->size = size;
2063
2064  return true;
2065}
2066
2067/* Find a named type.  */
2068
2069debug_type
2070debug_find_named_type (handle, name)
2071     PTR handle;
2072     const char *name;
2073{
2074  struct debug_handle *info = (struct debug_handle *) handle;
2075  struct debug_block *b;
2076  struct debug_file *f;
2077
2078  /* We only search the current compilation unit.  I don't know if
2079     this is right or not.  */
2080
2081  if (info->current_unit == NULL)
2082    {
2083      debug_error (_("debug_find_named_type: no current compilation unit"));
2084      return DEBUG_TYPE_NULL;
2085    }
2086
2087  for (b = info->current_block; b != NULL; b = b->parent)
2088    {
2089      if (b->locals != NULL)
2090	{
2091	  struct debug_name *n;
2092
2093	  for (n = b->locals->list; n != NULL; n = n->next)
2094	    {
2095	      if (n->kind == DEBUG_OBJECT_TYPE
2096		  && n->name[0] == name[0]
2097		  && strcmp (n->name, name) == 0)
2098		return n->u.type;
2099	    }
2100	}
2101    }
2102
2103  for (f = info->current_unit->files; f != NULL; f = f->next)
2104    {
2105      if (f->globals != NULL)
2106	{
2107	  struct debug_name *n;
2108
2109	  for (n = f->globals->list; n != NULL; n = n->next)
2110	    {
2111	      if (n->kind == DEBUG_OBJECT_TYPE
2112		  && n->name[0] == name[0]
2113		  && strcmp (n->name, name) == 0)
2114		return n->u.type;
2115	    }
2116	}
2117    }
2118
2119  return DEBUG_TYPE_NULL;
2120}
2121
2122/* Find a tagged type.  */
2123
2124debug_type
2125debug_find_tagged_type (handle, name, kind)
2126     PTR handle;
2127     const char *name;
2128     enum debug_type_kind kind;
2129{
2130  struct debug_handle *info = (struct debug_handle *) handle;
2131  struct debug_unit *u;
2132
2133  /* We search the globals of all the compilation units.  I don't know
2134     if this is correct or not.  It would be easy to change.  */
2135
2136  for (u = info->units; u != NULL; u = u->next)
2137    {
2138      struct debug_file *f;
2139
2140      for (f = u->files; f != NULL; f = f->next)
2141	{
2142	  struct debug_name *n;
2143
2144	  if (f->globals != NULL)
2145	    {
2146	      for (n = f->globals->list; n != NULL; n = n->next)
2147		{
2148		  if (n->kind == DEBUG_OBJECT_TAG
2149		      && (kind == DEBUG_KIND_ILLEGAL
2150			  || n->u.tag->kind == kind)
2151		      && n->name[0] == name[0]
2152		      && strcmp (n->name, name) == 0)
2153		    return n->u.tag;
2154		}
2155	    }
2156	}
2157    }
2158
2159  return DEBUG_TYPE_NULL;
2160}
2161
2162/* Get a base type.  We build a linked list on the stack to avoid
2163   crashing if the type is defined circularly.  */
2164
2165static struct debug_type *
2166debug_get_real_type (handle, type, list)
2167     PTR handle;
2168     debug_type type;
2169     struct debug_type_real_list *list;
2170{
2171  struct debug_type_real_list *l;
2172  struct debug_type_real_list rl;
2173
2174  switch (type->kind)
2175    {
2176    default:
2177      return type;
2178
2179    case DEBUG_KIND_INDIRECT:
2180    case DEBUG_KIND_NAMED:
2181    case DEBUG_KIND_TAGGED:
2182      break;
2183    }
2184
2185  for (l = list; l != NULL; l = l->next)
2186    {
2187      if (l->t == type)
2188	{
2189	  fprintf (stderr,
2190		   _("debug_get_real_type: circular debug information for %s\n"),
2191		   debug_get_type_name (handle, type));
2192	  return NULL;
2193	}
2194    }
2195
2196  rl.next = list;
2197  rl.t = type;
2198
2199  switch (type->kind)
2200    {
2201      /* The default case is just here to avoid warnings.  */
2202    default:
2203    case DEBUG_KIND_INDIRECT:
2204      if (*type->u.kindirect->slot != NULL)
2205	return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
2206      return type;
2207    case DEBUG_KIND_NAMED:
2208    case DEBUG_KIND_TAGGED:
2209      return debug_get_real_type (handle, type->u.knamed->type, &rl);
2210    }
2211  /*NOTREACHED*/
2212}
2213
2214/* Get the kind of a type.  */
2215
2216enum debug_type_kind
2217debug_get_type_kind (handle, type)
2218     PTR handle;
2219     debug_type type;
2220{
2221  if (type == NULL)
2222    return DEBUG_KIND_ILLEGAL;
2223  type = debug_get_real_type (handle, type, NULL);
2224  if (type == NULL)
2225    return DEBUG_KIND_ILLEGAL;
2226  return type->kind;
2227}
2228
2229/* Get the name of a type.  */
2230
2231const char *
2232debug_get_type_name (handle, type)
2233     PTR handle;
2234     debug_type type;
2235{
2236  if (type->kind == DEBUG_KIND_INDIRECT)
2237    {
2238      if (*type->u.kindirect->slot != NULL)
2239	return debug_get_type_name (handle, *type->u.kindirect->slot);
2240      return type->u.kindirect->tag;
2241    }
2242  if (type->kind == DEBUG_KIND_NAMED
2243      || type->kind == DEBUG_KIND_TAGGED)
2244    return type->u.knamed->name->name;
2245  return NULL;
2246}
2247
2248/* Get the size of a type.  */
2249
2250bfd_vma
2251debug_get_type_size (handle, type)
2252     PTR handle;
2253     debug_type type;
2254{
2255  if (type == NULL)
2256    return 0;
2257
2258  /* We don't call debug_get_real_type, because somebody might have
2259     called debug_record_type_size on a named or indirect type.  */
2260
2261  if (type->size != 0)
2262    return type->size;
2263
2264  switch (type->kind)
2265    {
2266    default:
2267      return 0;
2268    case DEBUG_KIND_INDIRECT:
2269      if (*type->u.kindirect->slot != NULL)
2270	return debug_get_type_size (handle, *type->u.kindirect->slot);
2271      return 0;
2272    case DEBUG_KIND_NAMED:
2273    case DEBUG_KIND_TAGGED:
2274      return debug_get_type_size (handle, type->u.knamed->type);
2275    }
2276  /*NOTREACHED*/
2277}
2278
2279/* Get the return type of a function or method type.  */
2280
2281debug_type
2282debug_get_return_type (handle, type)
2283     PTR handle;
2284     debug_type type;
2285{
2286  if (type == NULL)
2287    return DEBUG_TYPE_NULL;
2288  type = debug_get_real_type (handle, type, NULL);
2289  if (type == NULL)
2290    return DEBUG_TYPE_NULL;
2291  switch (type->kind)
2292    {
2293    default:
2294      return DEBUG_TYPE_NULL;
2295    case DEBUG_KIND_FUNCTION:
2296      return type->u.kfunction->return_type;
2297    case DEBUG_KIND_METHOD:
2298      return type->u.kmethod->return_type;
2299    }
2300  /*NOTREACHED*/
2301}
2302
2303/* Get the parameter types of a function or method type (except that
2304   we don't currently store the parameter types of a function).  */
2305
2306const debug_type *
2307debug_get_parameter_types (handle, type, pvarargs)
2308     PTR handle;
2309     debug_type type;
2310     boolean *pvarargs;
2311{
2312  if (type == NULL)
2313    return NULL;
2314  type = debug_get_real_type (handle, type, NULL);
2315  if (type == NULL)
2316    return NULL;
2317  switch (type->kind)
2318    {
2319    default:
2320      return NULL;
2321    case DEBUG_KIND_FUNCTION:
2322      *pvarargs = type->u.kfunction->varargs;
2323      return type->u.kfunction->arg_types;
2324    case DEBUG_KIND_METHOD:
2325      *pvarargs = type->u.kmethod->varargs;
2326      return type->u.kmethod->arg_types;
2327    }
2328  /*NOTREACHED*/
2329}
2330
2331/* Get the target type of a type.  */
2332
2333debug_type
2334debug_get_target_type (handle, type)
2335     PTR handle;
2336     debug_type type;
2337{
2338  if (type == NULL)
2339    return NULL;
2340  type = debug_get_real_type (handle, type, NULL);
2341  if (type == NULL)
2342    return NULL;
2343  switch (type->kind)
2344    {
2345    default:
2346      return NULL;
2347    case DEBUG_KIND_POINTER:
2348      return type->u.kpointer;
2349    case DEBUG_KIND_REFERENCE:
2350      return type->u.kreference;
2351    case DEBUG_KIND_CONST:
2352      return type->u.kconst;
2353    case DEBUG_KIND_VOLATILE:
2354      return type->u.kvolatile;
2355    }
2356  /*NOTREACHED*/
2357}
2358
2359/* Get the NULL terminated array of fields for a struct, union, or
2360   class.  */
2361
2362const debug_field *
2363debug_get_fields (handle, type)
2364     PTR handle;
2365     debug_type type;
2366{
2367  if (type == NULL)
2368    return NULL;
2369  type = debug_get_real_type (handle, type, NULL);
2370  if (type == NULL)
2371    return NULL;
2372  switch (type->kind)
2373    {
2374    default:
2375      return NULL;
2376    case DEBUG_KIND_STRUCT:
2377    case DEBUG_KIND_UNION:
2378    case DEBUG_KIND_CLASS:
2379    case DEBUG_KIND_UNION_CLASS:
2380      return type->u.kclass->fields;
2381    }
2382  /*NOTREACHED*/
2383}
2384
2385/* Get the type of a field.  */
2386
2387debug_type
2388debug_get_field_type (handle, field)
2389     PTR handle ATTRIBUTE_UNUSED;
2390     debug_field field;
2391{
2392  if (field == NULL)
2393    return NULL;
2394  return field->type;
2395}
2396
2397/* Get the name of a field.  */
2398
2399const char *
2400debug_get_field_name (handle, field)
2401     PTR handle ATTRIBUTE_UNUSED;
2402     debug_field field;
2403{
2404  if (field == NULL)
2405    return NULL;
2406  return field->name;
2407}
2408
2409/* Get the bit position of a field.  */
2410
2411bfd_vma
2412debug_get_field_bitpos (handle, field)
2413     PTR handle ATTRIBUTE_UNUSED;
2414     debug_field field;
2415{
2416  if (field == NULL || field->static_member)
2417    return (bfd_vma) -1;
2418  return field->u.f.bitpos;
2419}
2420
2421/* Get the bit size of a field.  */
2422
2423bfd_vma
2424debug_get_field_bitsize (handle, field)
2425     PTR handle ATTRIBUTE_UNUSED;
2426     debug_field field;
2427{
2428  if (field == NULL || field->static_member)
2429    return (bfd_vma) -1;
2430  return field->u.f.bitsize;
2431}
2432
2433/* Get the visibility of a field.  */
2434
2435enum debug_visibility
2436debug_get_field_visibility (handle, field)
2437     PTR handle ATTRIBUTE_UNUSED;
2438     debug_field field;
2439{
2440  if (field == NULL)
2441    return DEBUG_VISIBILITY_IGNORE;
2442  return field->visibility;
2443}
2444
2445/* Get the physical name of a field.  */
2446
2447const char *
2448debug_get_field_physname (handle, field)
2449     PTR handle ATTRIBUTE_UNUSED;
2450     debug_field field;
2451{
2452  if (field == NULL || ! field->static_member)
2453    return NULL;
2454  return field->u.s.physname;
2455}
2456
2457/* Write out the debugging information.  This is given a handle to
2458   debugging information, and a set of function pointers to call.  */
2459
2460boolean
2461debug_write (handle, fns, fhandle)
2462     PTR handle;
2463     const struct debug_write_fns *fns;
2464     PTR fhandle;
2465{
2466  struct debug_handle *info = (struct debug_handle *) handle;
2467  struct debug_unit *u;
2468
2469  /* We use a mark to tell whether we have already written out a
2470     particular name.  We use an integer, so that we don't have to
2471     clear the mark fields if we happen to write out the same
2472     information more than once.  */
2473  ++info->mark;
2474
2475  /* The base_id field holds an ID value which will never be used, so
2476     that we can tell whether we have assigned an ID during this call
2477     to debug_write.  */
2478  info->base_id = info->class_id;
2479
2480  /* We keep a linked list of classes for which was have assigned ID's
2481     during this call to debug_write.  */
2482  info->id_list = NULL;
2483
2484  for (u = info->units; u != NULL; u = u->next)
2485    {
2486      struct debug_file *f;
2487      boolean first_file;
2488
2489      info->current_write_lineno = u->linenos;
2490      info->current_write_lineno_index = 0;
2491
2492      if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2493	return false;
2494
2495      first_file = true;
2496      for (f = u->files; f != NULL; f = f->next)
2497	{
2498	  struct debug_name *n;
2499
2500	  if (first_file)
2501	    first_file = false;
2502	  else
2503	    {
2504	      if (! (*fns->start_source) (fhandle, f->filename))
2505		return false;
2506	    }
2507
2508	  if (f->globals != NULL)
2509	    {
2510	      for (n = f->globals->list; n != NULL; n = n->next)
2511		{
2512		  if (! debug_write_name (info, fns, fhandle, n))
2513		    return false;
2514		}
2515	    }
2516	}
2517
2518      /* Output any line number information which hasn't already been
2519         handled.  */
2520      if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
2521	return false;
2522    }
2523
2524  return true;
2525}
2526
2527/* Write out an element in a namespace.  */
2528
2529static boolean
2530debug_write_name (info, fns, fhandle, n)
2531     struct debug_handle *info;
2532     const struct debug_write_fns *fns;
2533     PTR fhandle;
2534     struct debug_name *n;
2535{
2536  switch (n->kind)
2537    {
2538    case DEBUG_OBJECT_TYPE:
2539      if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2540	  || ! (*fns->typdef) (fhandle, n->name))
2541	return false;
2542      return true;
2543    case DEBUG_OBJECT_TAG:
2544      if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2545	return false;
2546      return (*fns->tag) (fhandle, n->name);
2547    case DEBUG_OBJECT_VARIABLE:
2548      if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2549			      (struct debug_name *) NULL))
2550	return false;
2551      return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2552			       n->u.variable->val);
2553    case DEBUG_OBJECT_FUNCTION:
2554      return debug_write_function (info, fns, fhandle, n->name,
2555				   n->linkage, n->u.function);
2556    case DEBUG_OBJECT_INT_CONSTANT:
2557      return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2558    case DEBUG_OBJECT_FLOAT_CONSTANT:
2559      return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2560    case DEBUG_OBJECT_TYPED_CONSTANT:
2561      if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2562			      (struct debug_name *) NULL))
2563	return false;
2564      return (*fns->typed_constant) (fhandle, n->name,
2565				     n->u.typed_constant->val);
2566    default:
2567      abort ();
2568      return false;
2569    }
2570  /*NOTREACHED*/
2571}
2572
2573/* Write out a type.  If the type is DEBUG_KIND_NAMED or
2574   DEBUG_KIND_TAGGED, then the name argument is the name for which we
2575   are about to call typedef or tag.  If the type is anything else,
2576   then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2577   points to this one.  */
2578
2579static boolean
2580debug_write_type (info, fns, fhandle, type, name)
2581     struct debug_handle *info;
2582     const struct debug_write_fns *fns;
2583     PTR fhandle;
2584     struct debug_type *type;
2585     struct debug_name *name;
2586{
2587  unsigned int i;
2588  int is;
2589  const char *tag = NULL;
2590
2591  /* If we have a name for this type, just output it.  We only output
2592     typedef names after they have been defined.  We output type tags
2593     whenever we are not actually defining them.  */
2594  if ((type->kind == DEBUG_KIND_NAMED
2595       || type->kind == DEBUG_KIND_TAGGED)
2596      && (type->u.knamed->name->mark == info->mark
2597	  || (type->kind == DEBUG_KIND_TAGGED
2598	      && type->u.knamed->name != name)))
2599    {
2600      if (type->kind == DEBUG_KIND_NAMED)
2601	return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2602      else
2603	{
2604	  struct debug_type *real;
2605	  unsigned int id;
2606
2607	  real = debug_get_real_type ((PTR) info, type, NULL);
2608	  if (real == NULL)
2609	    return (*fns->empty_type) (fhandle);
2610	  id = 0;
2611	  if ((real->kind == DEBUG_KIND_STRUCT
2612	       || real->kind == DEBUG_KIND_UNION
2613	       || real->kind == DEBUG_KIND_CLASS
2614	       || real->kind == DEBUG_KIND_UNION_CLASS)
2615	      && real->u.kclass != NULL)
2616	    {
2617	      if (real->u.kclass->id <= info->base_id)
2618		{
2619		  if (! debug_set_class_id (info,
2620					    type->u.knamed->name->name,
2621					    real))
2622		    return false;
2623		}
2624	      id = real->u.kclass->id;
2625	    }
2626
2627	  return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
2628				   real->kind);
2629	}
2630    }
2631
2632  /* Mark the name after we have already looked for a known name, so
2633     that we don't just define a type in terms of itself.  We need to
2634     mark the name here so that a struct containing a pointer to
2635     itself will work.  */
2636  if (name != NULL)
2637    name->mark = info->mark;
2638
2639  if (name != NULL
2640      && type->kind != DEBUG_KIND_NAMED
2641      && type->kind != DEBUG_KIND_TAGGED)
2642    {
2643      assert (name->kind == DEBUG_OBJECT_TAG);
2644      tag = name->name;
2645    }
2646
2647  switch (type->kind)
2648    {
2649    case DEBUG_KIND_ILLEGAL:
2650      debug_error (_("debug_write_type: illegal type encountered"));
2651      return false;
2652    case DEBUG_KIND_INDIRECT:
2653      if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2654	return (*fns->empty_type) (fhandle);
2655      return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2656			       name);
2657    case DEBUG_KIND_VOID:
2658      return (*fns->void_type) (fhandle);
2659    case DEBUG_KIND_INT:
2660      return (*fns->int_type) (fhandle, type->size, type->u.kint);
2661    case DEBUG_KIND_FLOAT:
2662      return (*fns->float_type) (fhandle, type->size);
2663    case DEBUG_KIND_COMPLEX:
2664      return (*fns->complex_type) (fhandle, type->size);
2665    case DEBUG_KIND_BOOL:
2666      return (*fns->bool_type) (fhandle, type->size);
2667    case DEBUG_KIND_STRUCT:
2668    case DEBUG_KIND_UNION:
2669      if (type->u.kclass != NULL)
2670	{
2671	  if (type->u.kclass->id <= info->base_id)
2672	    {
2673	      if (! debug_set_class_id (info, tag, type))
2674		return false;
2675	    }
2676
2677	  if (info->mark == type->u.kclass->mark)
2678	    {
2679	      /* We are currently outputting this struct, or we have
2680		 already output it.  I don't know if this can happen,
2681		 but it can happen for a class.  */
2682	      assert (type->u.kclass->id > info->base_id);
2683	      return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2684				       type->kind);
2685	    }
2686	  type->u.kclass->mark = info->mark;
2687	}
2688
2689      if (! (*fns->start_struct_type) (fhandle, tag,
2690				       (type->u.kclass != NULL
2691					? type->u.kclass->id
2692					: 0),
2693				       type->kind == DEBUG_KIND_STRUCT,
2694				       type->size))
2695	return false;
2696      if (type->u.kclass != NULL
2697	  && type->u.kclass->fields != NULL)
2698	{
2699	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2700	    {
2701	      struct debug_field *f;
2702
2703	      f = type->u.kclass->fields[i];
2704	      if (! debug_write_type (info, fns, fhandle, f->type,
2705				      (struct debug_name *) NULL)
2706		  || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2707					     f->u.f.bitsize, f->visibility))
2708		return false;
2709	    }
2710	}
2711      return (*fns->end_struct_type) (fhandle);
2712    case DEBUG_KIND_CLASS:
2713    case DEBUG_KIND_UNION_CLASS:
2714      return debug_write_class_type (info, fns, fhandle, type, tag);
2715    case DEBUG_KIND_ENUM:
2716      if (type->u.kenum == NULL)
2717	return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2718				  (bfd_signed_vma *) NULL);
2719      return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2720				type->u.kenum->values);
2721    case DEBUG_KIND_POINTER:
2722      if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2723			      (struct debug_name *) NULL))
2724	return false;
2725      return (*fns->pointer_type) (fhandle);
2726    case DEBUG_KIND_FUNCTION:
2727      if (! debug_write_type (info, fns, fhandle,
2728			      type->u.kfunction->return_type,
2729			      (struct debug_name *) NULL))
2730	return false;
2731      if (type->u.kfunction->arg_types == NULL)
2732	is = -1;
2733      else
2734	{
2735	  for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2736	    if (! debug_write_type (info, fns, fhandle,
2737				    type->u.kfunction->arg_types[is],
2738				    (struct debug_name *) NULL))
2739	      return false;
2740	}
2741      return (*fns->function_type) (fhandle, is,
2742				    type->u.kfunction->varargs);
2743    case DEBUG_KIND_REFERENCE:
2744      if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2745			      (struct debug_name *) NULL))
2746	return false;
2747      return (*fns->reference_type) (fhandle);
2748    case DEBUG_KIND_RANGE:
2749      if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2750			      (struct debug_name *) NULL))
2751	return false;
2752      return (*fns->range_type) (fhandle, type->u.krange->lower,
2753				 type->u.krange->upper);
2754    case DEBUG_KIND_ARRAY:
2755      if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2756			      (struct debug_name *) NULL)
2757	  || ! debug_write_type (info, fns, fhandle,
2758				 type->u.karray->range_type,
2759				 (struct debug_name *) NULL))
2760	return false;
2761      return (*fns->array_type) (fhandle, type->u.karray->lower,
2762				 type->u.karray->upper,
2763				 type->u.karray->stringp);
2764    case DEBUG_KIND_SET:
2765      if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2766			      (struct debug_name *) NULL))
2767	return false;
2768      return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2769    case DEBUG_KIND_OFFSET:
2770      if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2771			      (struct debug_name *) NULL)
2772	  || ! debug_write_type (info, fns, fhandle,
2773				 type->u.koffset->target_type,
2774				 (struct debug_name *) NULL))
2775	return false;
2776      return (*fns->offset_type) (fhandle);
2777    case DEBUG_KIND_METHOD:
2778      if (! debug_write_type (info, fns, fhandle,
2779			      type->u.kmethod->return_type,
2780			      (struct debug_name *) NULL))
2781	return false;
2782      if (type->u.kmethod->arg_types == NULL)
2783	is = -1;
2784      else
2785	{
2786	  for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2787	    if (! debug_write_type (info, fns, fhandle,
2788				    type->u.kmethod->arg_types[is],
2789				    (struct debug_name *) NULL))
2790	      return false;
2791	}
2792      if (type->u.kmethod->domain_type != NULL)
2793	{
2794	  if (! debug_write_type (info, fns, fhandle,
2795				  type->u.kmethod->domain_type,
2796				  (struct debug_name *) NULL))
2797	    return false;
2798	}
2799      return (*fns->method_type) (fhandle,
2800				  type->u.kmethod->domain_type != NULL,
2801				  is,
2802				  type->u.kmethod->varargs);
2803    case DEBUG_KIND_CONST:
2804      if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2805			      (struct debug_name *) NULL))
2806	return false;
2807      return (*fns->const_type) (fhandle);
2808    case DEBUG_KIND_VOLATILE:
2809      if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2810			      (struct debug_name *) NULL))
2811	return false;
2812      return (*fns->volatile_type) (fhandle);
2813    case DEBUG_KIND_NAMED:
2814      return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2815			       (struct debug_name *) NULL);
2816    case DEBUG_KIND_TAGGED:
2817      return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2818			       type->u.knamed->name);
2819    default:
2820      abort ();
2821      return false;
2822    }
2823}
2824
2825/* Write out a class type.  */
2826
2827static boolean
2828debug_write_class_type (info, fns, fhandle, type, tag)
2829     struct debug_handle *info;
2830     const struct debug_write_fns *fns;
2831     PTR fhandle;
2832     struct debug_type *type;
2833     const char *tag;
2834{
2835  unsigned int i;
2836  unsigned int id;
2837  struct debug_type *vptrbase;
2838
2839  if (type->u.kclass == NULL)
2840    {
2841      id = 0;
2842      vptrbase = NULL;
2843    }
2844  else
2845    {
2846      if (type->u.kclass->id <= info->base_id)
2847	{
2848	  if (! debug_set_class_id (info, tag, type))
2849	    return false;
2850	}
2851
2852      if (info->mark == type->u.kclass->mark)
2853	{
2854	  /* We are currently outputting this class, or we have
2855	     already output it.  This can happen when there are
2856	     methods for an anonymous class.  */
2857	  assert (type->u.kclass->id > info->base_id);
2858	  return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2859				   type->kind);
2860	}
2861      type->u.kclass->mark = info->mark;
2862      id = type->u.kclass->id;
2863
2864      vptrbase = type->u.kclass->vptrbase;
2865      if (vptrbase != NULL && vptrbase != type)
2866	{
2867	  if (! debug_write_type (info, fns, fhandle, vptrbase,
2868				  (struct debug_name *) NULL))
2869	    return false;
2870	}
2871    }
2872
2873  if (! (*fns->start_class_type) (fhandle, tag, id,
2874				  type->kind == DEBUG_KIND_CLASS,
2875				  type->size,
2876				  vptrbase != NULL,
2877				  vptrbase == type))
2878    return false;
2879
2880  if (type->u.kclass != NULL)
2881    {
2882      if (type->u.kclass->fields != NULL)
2883	{
2884	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2885	    {
2886	      struct debug_field *f;
2887
2888	      f = type->u.kclass->fields[i];
2889	      if (! debug_write_type (info, fns, fhandle, f->type,
2890				      (struct debug_name *) NULL))
2891		return false;
2892	      if (f->static_member)
2893		{
2894		  if (! (*fns->class_static_member) (fhandle, f->name,
2895						     f->u.s.physname,
2896						     f->visibility))
2897		    return false;
2898		}
2899	      else
2900		{
2901		  if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2902					      f->u.f.bitsize, f->visibility))
2903		    return false;
2904		}
2905	    }
2906	}
2907
2908      if (type->u.kclass->baseclasses != NULL)
2909	{
2910	  for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2911	    {
2912	      struct debug_baseclass *b;
2913
2914	      b = type->u.kclass->baseclasses[i];
2915	      if (! debug_write_type (info, fns, fhandle, b->type,
2916				      (struct debug_name *) NULL))
2917		return false;
2918	      if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2919					     b->visibility))
2920		return false;
2921	    }
2922	}
2923
2924      if (type->u.kclass->methods != NULL)
2925	{
2926	  for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2927	    {
2928	      struct debug_method *m;
2929	      unsigned int j;
2930
2931	      m = type->u.kclass->methods[i];
2932	      if (! (*fns->class_start_method) (fhandle, m->name))
2933		return false;
2934	      for (j = 0; m->variants[j] != NULL; j++)
2935		{
2936		  struct debug_method_variant *v;
2937
2938		  v = m->variants[j];
2939		  if (v->context != NULL)
2940		    {
2941		      if (! debug_write_type (info, fns, fhandle, v->context,
2942					      (struct debug_name *) NULL))
2943			return false;
2944		    }
2945		  if (! debug_write_type (info, fns, fhandle, v->type,
2946					  (struct debug_name *) NULL))
2947		    return false;
2948		  if (v->voffset != VOFFSET_STATIC_METHOD)
2949		    {
2950		      if (! (*fns->class_method_variant) (fhandle, v->physname,
2951							  v->visibility,
2952							  v->constp,
2953							  v->volatilep,
2954							  v->voffset,
2955							  v->context != NULL))
2956			return false;
2957		    }
2958		  else
2959		    {
2960		      if (! (*fns->class_static_method_variant) (fhandle,
2961								 v->physname,
2962								 v->visibility,
2963								 v->constp,
2964								 v->volatilep))
2965			return false;
2966		    }
2967		}
2968	      if (! (*fns->class_end_method) (fhandle))
2969		return false;
2970	    }
2971	}
2972    }
2973
2974  return (*fns->end_class_type) (fhandle);
2975}
2976
2977/* Write out information for a function.  */
2978
2979static boolean
2980debug_write_function (info, fns, fhandle, name, linkage, function)
2981     struct debug_handle *info;
2982     const struct debug_write_fns *fns;
2983     PTR fhandle;
2984     const char *name;
2985     enum debug_object_linkage linkage;
2986     struct debug_function *function;
2987{
2988  struct debug_parameter *p;
2989  struct debug_block *b;
2990
2991  if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
2992    return false;
2993
2994  if (! debug_write_type (info, fns, fhandle, function->return_type,
2995			  (struct debug_name *) NULL))
2996    return false;
2997
2998  if (! (*fns->start_function) (fhandle, name,
2999				linkage == DEBUG_LINKAGE_GLOBAL))
3000    return false;
3001
3002  for (p = function->parameters; p != NULL; p = p->next)
3003    {
3004      if (! debug_write_type (info, fns, fhandle, p->type,
3005			      (struct debug_name *) NULL)
3006	  || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
3007	return false;
3008    }
3009
3010  for (b = function->blocks; b != NULL; b = b->next)
3011    {
3012      if (! debug_write_block (info, fns, fhandle, b))
3013	return false;
3014    }
3015
3016  return (*fns->end_function) (fhandle);
3017}
3018
3019/* Write out information for a block.  */
3020
3021static boolean
3022debug_write_block (info, fns, fhandle, block)
3023     struct debug_handle *info;
3024     const struct debug_write_fns *fns;
3025     PTR fhandle;
3026     struct debug_block *block;
3027{
3028  struct debug_name *n;
3029  struct debug_block *b;
3030
3031  if (! debug_write_linenos (info, fns, fhandle, block->start))
3032    return false;
3033
3034  /* I can't see any point to writing out a block with no local
3035     variables, so we don't bother, except for the top level block.  */
3036  if (block->locals != NULL || block->parent == NULL)
3037    {
3038      if (! (*fns->start_block) (fhandle, block->start))
3039	return false;
3040    }
3041
3042  if (block->locals != NULL)
3043    {
3044      for (n = block->locals->list; n != NULL; n = n->next)
3045	{
3046	  if (! debug_write_name (info, fns, fhandle, n))
3047	    return false;
3048	}
3049    }
3050
3051  for (b = block->children; b != NULL; b = b->next)
3052    {
3053      if (! debug_write_block (info, fns, fhandle, b))
3054	return false;
3055    }
3056
3057  if (! debug_write_linenos (info, fns, fhandle, block->end))
3058    return false;
3059
3060  if (block->locals != NULL || block->parent == NULL)
3061    {
3062      if (! (*fns->end_block) (fhandle, block->end))
3063	return false;
3064    }
3065
3066  return true;
3067}
3068
3069/* Write out line number information up to ADDRESS.  */
3070
3071static boolean
3072debug_write_linenos (info, fns, fhandle, address)
3073     struct debug_handle *info;
3074     const struct debug_write_fns *fns;
3075     PTR fhandle;
3076     bfd_vma address;
3077{
3078  while (info->current_write_lineno != NULL)
3079    {
3080      struct debug_lineno *l;
3081
3082      l = info->current_write_lineno;
3083
3084      while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
3085	{
3086	  if (l->linenos[info->current_write_lineno_index]
3087	      == (unsigned long) -1)
3088	    break;
3089
3090	  if (l->addrs[info->current_write_lineno_index] >= address)
3091	    return true;
3092
3093	  if (! (*fns->lineno) (fhandle, l->file->filename,
3094				l->linenos[info->current_write_lineno_index],
3095				l->addrs[info->current_write_lineno_index]))
3096	    return false;
3097
3098	  ++info->current_write_lineno_index;
3099	}
3100
3101      info->current_write_lineno = l->next;
3102      info->current_write_lineno_index = 0;
3103    }
3104
3105  return true;
3106}
3107
3108/* Get the ID number for a class.  If during the same call to
3109   debug_write we find a struct with the same definition with the same
3110   name, we use the same ID.  This type of things happens because the
3111   same struct will be defined by multiple compilation units.  */
3112
3113static boolean
3114debug_set_class_id (info, tag, type)
3115     struct debug_handle *info;
3116     const char *tag;
3117     struct debug_type *type;
3118{
3119  struct debug_class_type *c;
3120  struct debug_class_id *l;
3121
3122  assert (type->kind == DEBUG_KIND_STRUCT
3123	  || type->kind == DEBUG_KIND_UNION
3124	  || type->kind == DEBUG_KIND_CLASS
3125	  || type->kind == DEBUG_KIND_UNION_CLASS);
3126
3127  c = type->u.kclass;
3128
3129  if (c->id > info->base_id)
3130    return true;
3131
3132  for (l = info->id_list; l != NULL; l = l->next)
3133    {
3134      if (l->type->kind != type->kind)
3135	continue;
3136
3137      if (tag == NULL)
3138	{
3139	  if (l->tag != NULL)
3140	    continue;
3141	}
3142      else
3143	{
3144	  if (l->tag == NULL
3145	      || l->tag[0] != tag[0]
3146	      || strcmp (l->tag, tag) != 0)
3147	    continue;
3148	}
3149
3150      if (debug_type_samep (info, l->type, type))
3151	{
3152	  c->id = l->type->u.kclass->id;
3153	  return true;
3154	}
3155    }
3156
3157  /* There are no identical types.  Use a new ID, and add it to the
3158     list.  */
3159  ++info->class_id;
3160  c->id = info->class_id;
3161
3162  l = (struct debug_class_id *) xmalloc (sizeof *l);
3163  memset (l, 0, sizeof *l);
3164
3165  l->type = type;
3166  l->tag = tag;
3167
3168  l->next = info->id_list;
3169  info->id_list = l;
3170
3171  return true;
3172}
3173
3174/* See if two types are the same.  At this point, we don't care about
3175   tags and the like.  */
3176
3177static boolean
3178debug_type_samep (info, t1, t2)
3179     struct debug_handle *info;
3180     struct debug_type *t1;
3181     struct debug_type *t2;
3182{
3183  struct debug_type_compare_list *l;
3184  struct debug_type_compare_list top;
3185  boolean ret;
3186
3187  if (t1 == NULL)
3188    return t2 == NULL;
3189  if (t2 == NULL)
3190    return false;
3191
3192  while (t1->kind == DEBUG_KIND_INDIRECT)
3193    {
3194      t1 = *t1->u.kindirect->slot;
3195      if (t1 == NULL)
3196	return false;
3197    }
3198  while (t2->kind == DEBUG_KIND_INDIRECT)
3199    {
3200      t2 = *t2->u.kindirect->slot;
3201      if (t2 == NULL)
3202	return false;
3203    }
3204
3205  if (t1 == t2)
3206    return true;
3207
3208  /* As a special case, permit a typedef to match a tag, since C++
3209     debugging output will sometimes add a typedef where C debugging
3210     output will not.  */
3211  if (t1->kind == DEBUG_KIND_NAMED
3212      && t2->kind == DEBUG_KIND_TAGGED)
3213    return debug_type_samep (info, t1->u.knamed->type, t2);
3214  else if (t1->kind == DEBUG_KIND_TAGGED
3215	   && t2->kind == DEBUG_KIND_NAMED)
3216    return debug_type_samep (info, t1, t2->u.knamed->type);
3217
3218  if (t1->kind != t2->kind
3219      || t1->size != t2->size)
3220    return false;
3221
3222  /* Get rid of the trivial cases first.  */
3223  switch (t1->kind)
3224    {
3225    default:
3226      break;
3227    case DEBUG_KIND_VOID:
3228    case DEBUG_KIND_FLOAT:
3229    case DEBUG_KIND_COMPLEX:
3230    case DEBUG_KIND_BOOL:
3231      return true;
3232    case DEBUG_KIND_INT:
3233      return t1->u.kint == t2->u.kint;
3234    }
3235
3236  /* We have to avoid an infinite recursion.  We do this by keeping a
3237     list of types which we are comparing.  We just keep the list on
3238     the stack.  If we encounter a pair of types we are currently
3239     comparing, we just assume that they are equal.  */
3240  for (l = info->compare_list; l != NULL; l = l->next)
3241    {
3242      if (l->t1 == t1 && l->t2 == t2)
3243	return true;
3244    }
3245
3246  top.t1 = t1;
3247  top.t2 = t2;
3248  top.next = info->compare_list;
3249  info->compare_list = &top;
3250
3251  switch (t1->kind)
3252    {
3253    default:
3254      abort ();
3255      ret = false;
3256      break;
3257
3258    case DEBUG_KIND_STRUCT:
3259    case DEBUG_KIND_UNION:
3260    case DEBUG_KIND_CLASS:
3261    case DEBUG_KIND_UNION_CLASS:
3262      if (t1->u.kclass == NULL)
3263	ret = t2->u.kclass == NULL;
3264      else if (t2->u.kclass == NULL)
3265	ret = false;
3266      else if (t1->u.kclass->id > info->base_id
3267	       && t1->u.kclass->id == t2->u.kclass->id)
3268	ret = true;
3269      else
3270	ret = debug_class_type_samep (info, t1, t2);
3271      break;
3272
3273    case DEBUG_KIND_ENUM:
3274      if (t1->u.kenum == NULL)
3275	ret = t2->u.kenum == NULL;
3276      else if (t2->u.kenum == NULL)
3277	ret = false;
3278      else
3279	{
3280	  const char **pn1, **pn2;
3281	  bfd_signed_vma *pv1, *pv2;
3282
3283	  pn1 = t1->u.kenum->names;
3284	  pn2 = t2->u.kenum->names;
3285	  pv1 = t1->u.kenum->values;
3286	  pv2 = t2->u.kenum->values;
3287	  while (*pn1 != NULL && *pn2 != NULL)
3288	    {
3289	      if (**pn1 != **pn2
3290		  || *pv1 != *pv2
3291		  || strcmp (*pn1, *pn2) != 0)
3292		break;
3293	      ++pn1;
3294	      ++pn2;
3295	      ++pv1;
3296	      ++pv2;
3297	    }
3298	  ret = *pn1 == NULL && *pn2 == NULL;
3299	}
3300      break;
3301
3302    case DEBUG_KIND_POINTER:
3303      ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
3304      break;
3305
3306    case DEBUG_KIND_FUNCTION:
3307      if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
3308	  || ! debug_type_samep (info, t1->u.kfunction->return_type,
3309				 t2->u.kfunction->return_type)
3310	  || ((t1->u.kfunction->arg_types == NULL)
3311	      != (t2->u.kfunction->arg_types == NULL)))
3312	ret = false;
3313      else if (t1->u.kfunction->arg_types == NULL)
3314	ret = true;
3315      else
3316	{
3317	  struct debug_type **a1, **a2;
3318
3319	  a1 = t1->u.kfunction->arg_types;
3320	  a2 = t2->u.kfunction->arg_types;
3321	  while (*a1 != NULL && *a2 != NULL)
3322	    {
3323	      if (! debug_type_samep (info, *a1, *a2))
3324		break;
3325	      ++a1;
3326	      ++a2;
3327	    }
3328	  ret = *a1 == NULL && *a2 == NULL;
3329	}
3330      break;
3331
3332    case DEBUG_KIND_REFERENCE:
3333      ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
3334      break;
3335
3336    case DEBUG_KIND_RANGE:
3337      ret = (t1->u.krange->lower == t2->u.krange->lower
3338	     && t1->u.krange->upper == t2->u.krange->upper
3339	     && debug_type_samep (info, t1->u.krange->type,
3340				  t2->u.krange->type));
3341
3342    case DEBUG_KIND_ARRAY:
3343      ret = (t1->u.karray->lower == t2->u.karray->lower
3344	     && t1->u.karray->upper == t2->u.karray->upper
3345	     && t1->u.karray->stringp == t2->u.karray->stringp
3346	     && debug_type_samep (info, t1->u.karray->element_type,
3347				  t2->u.karray->element_type));
3348      break;
3349
3350    case DEBUG_KIND_SET:
3351      ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
3352	     && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
3353      break;
3354
3355    case DEBUG_KIND_OFFSET:
3356      ret = (debug_type_samep (info, t1->u.koffset->base_type,
3357			       t2->u.koffset->base_type)
3358	     && debug_type_samep (info, t1->u.koffset->target_type,
3359				  t2->u.koffset->target_type));
3360      break;
3361
3362    case DEBUG_KIND_METHOD:
3363      if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
3364	  || ! debug_type_samep (info, t1->u.kmethod->return_type,
3365				 t2->u.kmethod->return_type)
3366	  || ! debug_type_samep (info, t1->u.kmethod->domain_type,
3367				 t2->u.kmethod->domain_type)
3368	  || ((t1->u.kmethod->arg_types == NULL)
3369	      != (t2->u.kmethod->arg_types == NULL)))
3370	ret = false;
3371      else if (t1->u.kmethod->arg_types == NULL)
3372	ret = true;
3373      else
3374	{
3375	  struct debug_type **a1, **a2;
3376
3377	  a1 = t1->u.kmethod->arg_types;
3378	  a2 = t2->u.kmethod->arg_types;
3379	  while (*a1 != NULL && *a2 != NULL)
3380	    {
3381	      if (! debug_type_samep (info, *a1, *a2))
3382		break;
3383	      ++a1;
3384	      ++a2;
3385	    }
3386	  ret = *a1 == NULL && *a2 == NULL;
3387	}
3388      break;
3389
3390    case DEBUG_KIND_CONST:
3391      ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
3392      break;
3393
3394    case DEBUG_KIND_VOLATILE:
3395      ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
3396      break;
3397
3398    case DEBUG_KIND_NAMED:
3399    case DEBUG_KIND_TAGGED:
3400      ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
3401	     && debug_type_samep (info, t1->u.knamed->type,
3402				  t2->u.knamed->type));
3403      break;
3404    }
3405
3406  info->compare_list = top.next;
3407
3408  return ret;
3409}
3410
3411/* See if two classes are the same.  This is a subroutine of
3412   debug_type_samep.  */
3413
3414static boolean
3415debug_class_type_samep (info, t1, t2)
3416     struct debug_handle *info;
3417     struct debug_type *t1;
3418     struct debug_type *t2;
3419{
3420  struct debug_class_type *c1, *c2;
3421
3422  c1 = t1->u.kclass;
3423  c2 = t2->u.kclass;
3424
3425  if ((c1->fields == NULL) != (c2->fields == NULL)
3426      || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
3427      || (c1->methods == NULL) != (c2->methods == NULL)
3428      || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
3429    return false;
3430
3431  if (c1->fields != NULL)
3432    {
3433      struct debug_field **pf1, **pf2;
3434
3435      for (pf1 = c1->fields, pf2 = c2->fields;
3436	   *pf1 != NULL && *pf2 != NULL;
3437	   pf1++, pf2++)
3438	{
3439	  struct debug_field *f1, *f2;
3440
3441	  f1 = *pf1;
3442	  f2 = *pf2;
3443	  if (f1->name[0] != f2->name[0]
3444	      || f1->visibility != f2->visibility
3445	      || f1->static_member != f2->static_member)
3446	    return false;
3447	  if (f1->static_member)
3448	    {
3449	      if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
3450		return false;
3451	    }
3452	  else
3453	    {
3454	      if (f1->u.f.bitpos != f2->u.f.bitpos
3455		  || f1->u.f.bitsize != f2->u.f.bitsize)
3456		return false;
3457	    }
3458	  /* We do the checks which require function calls last.  We
3459             don't require that the types of fields have the same
3460             names, since that sometimes fails in the presence of
3461             typedefs and we really don't care.  */
3462	  if (strcmp (f1->name, f2->name) != 0
3463	      || ! debug_type_samep (info,
3464				     debug_get_real_type ((PTR) info,
3465							  f1->type, NULL),
3466				     debug_get_real_type ((PTR) info,
3467							  f2->type, NULL)))
3468	    return false;
3469	}
3470      if (*pf1 != NULL || *pf2 != NULL)
3471	return false;
3472    }
3473
3474  if (c1->vptrbase != NULL)
3475    {
3476      if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
3477	return false;
3478    }
3479
3480  if (c1->baseclasses != NULL)
3481    {
3482      struct debug_baseclass **pb1, **pb2;
3483
3484      for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
3485	   *pb1 != NULL && *pb2 != NULL;
3486	   ++pb1, ++pb2)
3487	{
3488	  struct debug_baseclass *b1, *b2;
3489
3490	  b1 = *pb1;
3491	  b2 = *pb2;
3492	  if (b1->bitpos != b2->bitpos
3493	      || b1->virtual != b2->virtual
3494	      || b1->visibility != b2->visibility
3495	      || ! debug_type_samep (info, b1->type, b2->type))
3496	    return false;
3497	}
3498      if (*pb1 != NULL || *pb2 != NULL)
3499	return false;
3500    }
3501
3502  if (c1->methods != NULL)
3503    {
3504      struct debug_method **pm1, **pm2;
3505
3506      for (pm1 = c1->methods, pm2 = c2->methods;
3507	   *pm1 != NULL && *pm2 != NULL;
3508	   ++pm1, ++pm2)
3509	{
3510	  struct debug_method *m1, *m2;
3511
3512	  m1 = *pm1;
3513	  m2 = *pm2;
3514	  if (m1->name[0] != m2->name[0]
3515	      || strcmp (m1->name, m2->name) != 0
3516	      || (m1->variants == NULL) != (m2->variants == NULL))
3517	    return false;
3518	  if (m1->variants == NULL)
3519	    {
3520	      struct debug_method_variant **pv1, **pv2;
3521
3522	      for (pv1 = m1->variants, pv2 = m2->variants;
3523		   *pv1 != NULL && *pv2 != NULL;
3524		   ++pv1, ++pv2)
3525		{
3526		  struct debug_method_variant *v1, *v2;
3527
3528		  v1 = *pv1;
3529		  v2 = *pv2;
3530		  if (v1->physname[0] != v2->physname[0]
3531		      || v1->visibility != v2->visibility
3532		      || v1->constp != v2->constp
3533		      || v1->volatilep != v2->volatilep
3534		      || v1->voffset != v2->voffset
3535		      || (v1->context == NULL) != (v2->context == NULL)
3536		      || strcmp (v1->physname, v2->physname) != 0
3537		      || ! debug_type_samep (info, v1->type, v2->type))
3538		    return false;
3539		  if (v1->context != NULL)
3540		    {
3541		      if (! debug_type_samep (info, v1->context,
3542					      v2->context))
3543			return false;
3544		    }
3545		}
3546	      if (*pv1 != NULL || *pv2 != NULL)
3547		return false;
3548	    }
3549	}
3550      if (*pm1 != NULL || *pm2 != NULL)
3551	return false;
3552    }
3553
3554  return true;
3555}
3556