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