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