* debug.h (debug_get_type_size): Declare.
[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     case DEBUG_KIND_TAGGED:
2162       return debug_get_real_type (handle, type->u.knamed->type);
2163     }
2164   /*NOTREACHED*/
2165 }
2166
2167 /* Get the kind of a type.  */
2168
2169 enum debug_type_kind
2170 debug_get_type_kind (handle, type)
2171      PTR handle;
2172      debug_type type;
2173 {
2174   if (type == NULL)
2175     return DEBUG_KIND_ILLEGAL;
2176   type = debug_get_real_type (handle, type);
2177   return type->kind;
2178 }
2179
2180 /* Get the name of a type.  */
2181
2182 const char *
2183 debug_get_type_name (handle, type)
2184      PTR handle;
2185      debug_type type;
2186 {
2187   if (type->kind == DEBUG_KIND_INDIRECT)
2188     {
2189       if (*type->u.kindirect->slot != NULL)
2190         return debug_get_type_name (handle, *type->u.kindirect->slot);
2191       return type->u.kindirect->tag;
2192     }
2193   if (type->kind == DEBUG_KIND_NAMED
2194       || type->kind == DEBUG_KIND_TAGGED)
2195     return type->u.knamed->name->name;
2196   return NULL;
2197 }
2198
2199 /* Get the size of a type.  */
2200
2201 bfd_vma
2202 debug_get_type_size (handle, type)
2203      PTR handle;
2204      debug_type type;
2205 {
2206   if (type == NULL)
2207     return 0;
2208
2209   /* We don't call debug_get_real_type, because somebody might have
2210      called debug_record_type_size on a named or indirect type.  */
2211
2212   if (type->size != 0)
2213     return type->size;
2214
2215   switch (type->kind)
2216     {
2217     default:
2218       return 0;
2219     case DEBUG_KIND_INDIRECT:
2220       if (*type->u.kindirect->slot != NULL)
2221         return debug_get_type_size (handle, *type->u.kindirect->slot);
2222       return 0;
2223     case DEBUG_KIND_NAMED:
2224     case DEBUG_KIND_TAGGED:
2225       return debug_get_type_size (handle, type->u.knamed->type);
2226     }
2227   /*NOTREACHED*/
2228 }
2229
2230 /* Get the return type of a function or method type.  */
2231
2232 debug_type
2233 debug_get_return_type (handle, type)
2234      PTR handle;
2235      debug_type type;
2236 {
2237   if (type == NULL)
2238     return DEBUG_TYPE_NULL;
2239   type = debug_get_real_type (handle, type);
2240   switch (type->kind)
2241     {
2242     default:
2243       return DEBUG_TYPE_NULL;
2244     case DEBUG_KIND_FUNCTION:
2245       return type->u.kfunction->return_type;
2246     case DEBUG_KIND_METHOD:
2247       return type->u.kmethod->return_type;
2248     }
2249   /*NOTREACHED*/      
2250 }
2251
2252 /* Get the parameter types of a function or method type (except that
2253    we don't currently store the parameter types of a function).  */
2254
2255 const debug_type *
2256 debug_get_parameter_types (handle, type, pvarargs)
2257      PTR handle;
2258      debug_type type;
2259      boolean *pvarargs;
2260 {
2261   if (type == NULL)
2262     return NULL;
2263   type = debug_get_real_type (handle, type);
2264   switch (type->kind)
2265     {
2266     default:
2267       return NULL;
2268     case DEBUG_KIND_METHOD:
2269       *pvarargs = type->u.kmethod->varargs;
2270       return type->u.kmethod->arg_types;
2271     }
2272   /*NOTREACHED*/
2273 }
2274
2275 /* Get the target type of a type.  */
2276
2277 debug_type
2278 debug_get_target_type (handle, type)
2279      PTR handle;
2280      debug_type type;
2281 {
2282   if (type == NULL)
2283     return NULL;
2284   type = debug_get_real_type (handle, type);
2285   switch (type->kind)
2286     {
2287     default:
2288       return NULL;
2289     case DEBUG_KIND_POINTER:
2290       return type->u.kpointer;
2291     case DEBUG_KIND_REFERENCE:
2292       return type->u.kreference;
2293     case DEBUG_KIND_CONST:
2294       return type->u.kconst;
2295     case DEBUG_KIND_VOLATILE:
2296       return type->u.kvolatile;
2297     }
2298   /*NOTREACHED*/
2299 }
2300
2301 /* Get the NULL terminated array of fields for a struct, union, or
2302    class.  */
2303
2304 const debug_field *
2305 debug_get_fields (handle, type)
2306      PTR handle;
2307      debug_type type;
2308 {
2309   if (type == NULL)
2310     return NULL;
2311   type = debug_get_real_type (handle, type);
2312   switch (type->kind)
2313     {
2314     default:
2315       return NULL;
2316     case DEBUG_KIND_STRUCT:
2317     case DEBUG_KIND_UNION:
2318     case DEBUG_KIND_CLASS:
2319     case DEBUG_KIND_UNION_CLASS:
2320       return type->u.kclass->fields;
2321     }
2322   /*NOTREACHED*/
2323 }
2324
2325 /* Get the type of a field.  */
2326
2327 /*ARGSUSED*/
2328 debug_type
2329 debug_get_field_type (handle, field)
2330      PTR handle;
2331      debug_field field;
2332 {
2333   if (field == NULL)
2334     return NULL;
2335   return field->type;
2336 }
2337
2338 /* Get the name of a field.  */
2339
2340 /*ARGSUSED*/
2341 const char *
2342 debug_get_field_name (handle, field)
2343      PTR handle;
2344      debug_field field;
2345 {
2346   if (field == NULL)
2347     return NULL;
2348   return field->name;
2349 }
2350
2351 /* Get the bit position of a field.  */
2352
2353 /*ARGSUSED*/
2354 bfd_vma
2355 debug_get_field_bitpos (handle, field)
2356      PTR handle;
2357      debug_field field;
2358 {
2359   if (field == NULL || field->static_member)
2360     return (bfd_vma) -1;
2361   return field->u.f.bitpos;
2362 }
2363
2364 /* Get the bit size of a field.  */
2365
2366 /*ARGSUSED*/
2367 bfd_vma
2368 debug_get_field_bitsize (handle, field)
2369      PTR handle;
2370      debug_field field;
2371 {
2372   if (field == NULL || field->static_member)
2373     return (bfd_vma) -1;
2374   return field->u.f.bitsize;
2375 }
2376
2377 /* Get the visibility of a field.  */
2378
2379 /*ARGSUSED*/
2380 enum debug_visibility
2381 debug_get_field_visibility (handle, field)
2382      PTR handle;
2383      debug_field field;
2384 {
2385   if (field == NULL)
2386     return DEBUG_VISIBILITY_IGNORE;
2387   return field->visibility;
2388 }
2389
2390 /* Get the physical name of a field.  */
2391
2392 const char *
2393 debug_get_field_physname (handle, field)
2394      PTR handle;
2395      debug_field field;
2396 {
2397   if (field == NULL || ! field->static_member)
2398     return NULL;
2399   return field->u.s.physname;
2400 }
2401 \f
2402 /* Write out the debugging information.  This is given a handle to
2403    debugging information, and a set of function pointers to call.  */
2404
2405 boolean
2406 debug_write (handle, fns, fhandle)
2407      PTR handle;
2408      const struct debug_write_fns *fns;
2409      PTR fhandle;
2410 {
2411   struct debug_handle *info = (struct debug_handle *) handle;
2412   struct debug_unit *u;
2413
2414   /* We use a mark to tell whether we have already written out a
2415      particular name.  We use an integer, so that we don't have to
2416      clear the mark fields if we happen to write out the same
2417      information more than once.  */
2418   ++info->mark;
2419
2420   /* The base_id field holds an ID value which will never be used, so
2421      that we can tell whether we have assigned an ID during this call
2422      to debug_write.  */
2423   info->base_id = info->class_id;
2424
2425   for (u = info->units; u != NULL; u = u->next)
2426     {
2427       struct debug_file *f;
2428       boolean first_file;
2429       struct debug_lineno *l;
2430
2431       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2432         return false;
2433
2434       first_file = true;
2435       for (f = u->files; f != NULL; f = f->next)
2436         {
2437           struct debug_name *n;
2438
2439           if (first_file)
2440             first_file = false;
2441           else
2442             {
2443               if (! (*fns->start_source) (fhandle, f->filename))
2444                 return false;
2445             }
2446
2447           if (f->globals != NULL)
2448             {
2449               for (n = f->globals->list; n != NULL; n = n->next)
2450                 {
2451                   if (! debug_write_name (info, fns, fhandle, n))
2452                     return false;
2453                 }
2454             }
2455         }
2456
2457       for (l = u->linenos; l != NULL; l = l->next)
2458         {
2459           unsigned int i;
2460
2461           for (i = 0; i < DEBUG_LINENO_COUNT; i++)
2462             {
2463               if (l->linenos[i] == (unsigned long) -1)
2464                 break;
2465               if (! (*fns->lineno) (fhandle, l->file->filename, l->linenos[i],
2466                                     l->addrs[i]))
2467                 return false;
2468             }
2469         }
2470     }
2471
2472   return true;
2473 }
2474
2475 /* Write out an element in a namespace.  */
2476
2477 static boolean
2478 debug_write_name (info, fns, fhandle, n)
2479      struct debug_handle *info;
2480      const struct debug_write_fns *fns;
2481      PTR fhandle;
2482      struct debug_name *n;
2483 {
2484   /* The class_mark field is used to prevent recursively outputting a
2485      struct or class.  */
2486   ++info->class_mark;
2487
2488   switch (n->kind)
2489     {
2490     case DEBUG_OBJECT_TYPE:
2491       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2492           || ! (*fns->typdef) (fhandle, n->name))
2493         return false;
2494       return true;
2495     case DEBUG_OBJECT_TAG:
2496       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2497         return false;
2498       return (*fns->tag) (fhandle, n->name);
2499     case DEBUG_OBJECT_VARIABLE:
2500       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2501                               (struct debug_name *) NULL))
2502         return false;
2503       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2504                                n->u.variable->val);
2505     case DEBUG_OBJECT_FUNCTION:
2506       return debug_write_function (info, fns, fhandle, n->name,
2507                                    n->linkage, n->u.function);
2508     case DEBUG_OBJECT_INT_CONSTANT:
2509       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2510     case DEBUG_OBJECT_FLOAT_CONSTANT:
2511       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2512     case DEBUG_OBJECT_TYPED_CONSTANT:
2513       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2514                               (struct debug_name *) NULL))
2515         return false;
2516       return (*fns->typed_constant) (fhandle, n->name,
2517                                      n->u.typed_constant->val);
2518     default:
2519       abort ();
2520       return false;
2521     }
2522   /*NOTREACHED*/
2523 }
2524
2525 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
2526    DEBUG_KIND_TAGGED, then the name argument is the name for which we
2527    are about to call typedef or tag.  If the type is anything else,
2528    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2529    points to this one.  */
2530
2531 static boolean
2532 debug_write_type (info, fns, fhandle, type, name)
2533      struct debug_handle *info;
2534      const struct debug_write_fns *fns;
2535      PTR fhandle;
2536      struct debug_type *type;
2537      struct debug_name *name;
2538 {
2539   unsigned int i;
2540   int is;
2541   const char *tag;
2542
2543   /* If we have a name for this type, just output it.  We only output
2544      typedef names after they have been defined.  We output type tags
2545      whenever we are not actually defining them.  */
2546   if ((type->kind == DEBUG_KIND_NAMED
2547        || type->kind == DEBUG_KIND_TAGGED)
2548       && (type->u.knamed->name->mark == info->mark
2549           || (type->kind == DEBUG_KIND_TAGGED
2550               && type->u.knamed->name != name)))
2551     {
2552       if (type->kind == DEBUG_KIND_NAMED)
2553         return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2554       else
2555         {
2556           struct debug_type *real;
2557
2558           real = debug_get_real_type ((PTR) info, type);
2559           return (*fns->tag_type) (fhandle, type->u.knamed->name->name, 0,
2560                                    real->kind);
2561         }
2562     }
2563
2564   /* Mark the name after we have already looked for a known name, so
2565      that we don't just define a type in terms of itself.  We need to
2566      mark the name here so that a struct containing a pointer to
2567      itself will work.  */
2568   if (name != NULL)
2569     name->mark = info->mark;
2570
2571   tag = NULL;
2572   if (name != NULL
2573       && type->kind != DEBUG_KIND_NAMED
2574       && type->kind != DEBUG_KIND_TAGGED)
2575     {
2576       assert (name->kind == DEBUG_OBJECT_TAG);
2577       tag = name->name;
2578     }
2579
2580   switch (type->kind)
2581     {
2582     case DEBUG_KIND_ILLEGAL:
2583       debug_error ("debug_write_type: illegal type encountered");
2584       return false;
2585     case DEBUG_KIND_INDIRECT:
2586       if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2587         return (*fns->empty_type) (fhandle);
2588       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2589                                name);
2590     case DEBUG_KIND_VOID:
2591       return (*fns->void_type) (fhandle);
2592     case DEBUG_KIND_INT:
2593       return (*fns->int_type) (fhandle, type->size, type->u.kint);
2594     case DEBUG_KIND_FLOAT:
2595       return (*fns->float_type) (fhandle, type->size);
2596     case DEBUG_KIND_COMPLEX:
2597       return (*fns->complex_type) (fhandle, type->size);
2598     case DEBUG_KIND_BOOL:
2599       return (*fns->bool_type) (fhandle, type->size);
2600     case DEBUG_KIND_STRUCT:
2601     case DEBUG_KIND_UNION:
2602       if (type->u.kclass != NULL)
2603         {
2604           if (info->class_mark == type->u.kclass->mark
2605               || type->u.kclass->id > info->base_id)
2606             {
2607               /* We are currently outputting this struct, or we have
2608                  already output it.  I don't know if this can happen,
2609                  but it can happen for a class.  */
2610               assert (type->u.kclass->id > info->base_id);
2611               return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2612                                        type->kind);
2613             }
2614           type->u.kclass->mark = info->class_mark;
2615           ++info->class_id;
2616           type->u.kclass->id = info->class_id;
2617         }
2618
2619       if (! (*fns->start_struct_type) (fhandle, tag,
2620                                        (type->u.kclass != NULL
2621                                         ? type->u.kclass->id
2622                                         : 0),
2623                                        type->kind == DEBUG_KIND_STRUCT,
2624                                        type->size))
2625         return false;
2626       if (type->u.kclass != NULL
2627           && type->u.kclass->fields != NULL)
2628         {
2629           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2630             {
2631               struct debug_field *f;
2632
2633               f = type->u.kclass->fields[i];
2634               if (! debug_write_type (info, fns, fhandle, f->type,
2635                                       (struct debug_name *) NULL)
2636                   || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2637                                              f->u.f.bitsize, f->visibility))
2638                 return false;
2639             }
2640         }
2641       return (*fns->end_struct_type) (fhandle);
2642     case DEBUG_KIND_CLASS:
2643     case DEBUG_KIND_UNION_CLASS:
2644       return debug_write_class_type (info, fns, fhandle, type, tag);
2645     case DEBUG_KIND_ENUM:
2646       if (type->u.kenum == NULL)
2647         return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2648                                   (bfd_signed_vma *) NULL);
2649       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2650                                 type->u.kenum->values);
2651     case DEBUG_KIND_POINTER:
2652       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2653                               (struct debug_name *) NULL))
2654         return false;
2655       return (*fns->pointer_type) (fhandle);
2656     case DEBUG_KIND_FUNCTION:
2657       if (type->u.kfunction->arg_types == NULL)
2658         is = -1;
2659       else
2660         {
2661           for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2662             if (! debug_write_type (info, fns, fhandle,
2663                                     type->u.kfunction->arg_types[is],
2664                                     (struct debug_name *) NULL))
2665               return false;
2666         }
2667       if (! debug_write_type (info, fns, fhandle,
2668                               type->u.kfunction->return_type,
2669                               (struct debug_name *) NULL))
2670         return false;
2671       return (*fns->function_type) (fhandle, is,
2672                                     type->u.kfunction->varargs);
2673     case DEBUG_KIND_REFERENCE:
2674       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2675                               (struct debug_name *) NULL))
2676         return false;
2677       return (*fns->reference_type) (fhandle);
2678     case DEBUG_KIND_RANGE:
2679       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2680                               (struct debug_name *) NULL))
2681         return false;
2682       return (*fns->range_type) (fhandle, type->u.krange->lower,
2683                                  type->u.krange->upper);
2684     case DEBUG_KIND_ARRAY:
2685       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2686                               (struct debug_name *) NULL)
2687           || ! debug_write_type (info, fns, fhandle,
2688                                  type->u.karray->range_type,
2689                                  (struct debug_name *) NULL))
2690         return false;
2691       return (*fns->array_type) (fhandle, type->u.karray->lower,
2692                                  type->u.karray->upper,
2693                                  type->u.karray->stringp);
2694     case DEBUG_KIND_SET:
2695       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2696                               (struct debug_name *) NULL))
2697         return false;
2698       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2699     case DEBUG_KIND_OFFSET:
2700       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2701                               (struct debug_name *) NULL)
2702           || ! debug_write_type (info, fns, fhandle,
2703                                  type->u.koffset->target_type,
2704                                  (struct debug_name *) NULL))
2705         return false;
2706       return (*fns->offset_type) (fhandle);
2707     case DEBUG_KIND_METHOD:
2708       if (! debug_write_type (info, fns, fhandle,
2709                               type->u.kmethod->return_type,
2710                               (struct debug_name *) NULL))
2711         return false;
2712       if (type->u.kmethod->arg_types == NULL)
2713         is = -1;
2714       else
2715         {
2716           for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2717             if (! debug_write_type (info, fns, fhandle,
2718                                     type->u.kmethod->arg_types[is],
2719                                     (struct debug_name *) NULL))
2720               return false;
2721         }
2722       if (type->u.kmethod->domain_type != NULL)
2723         {
2724           if (! debug_write_type (info, fns, fhandle,
2725                                   type->u.kmethod->domain_type,
2726                                   (struct debug_name *) NULL))
2727             return false;
2728         }
2729       return (*fns->method_type) (fhandle,
2730                                   type->u.kmethod->domain_type != NULL,
2731                                   is,
2732                                   type->u.kmethod->varargs);
2733     case DEBUG_KIND_CONST:
2734       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2735                               (struct debug_name *) NULL))
2736         return false;
2737       return (*fns->const_type) (fhandle);
2738     case DEBUG_KIND_VOLATILE:
2739       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2740                               (struct debug_name *) NULL))
2741         return false;
2742       return (*fns->volatile_type) (fhandle);
2743     case DEBUG_KIND_NAMED:
2744       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2745                                (struct debug_name *) NULL);
2746     case DEBUG_KIND_TAGGED:
2747       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2748                                type->u.knamed->name);
2749     default:
2750       abort ();
2751       return false;
2752     }
2753 }
2754
2755 /* Write out a class type.  */
2756
2757 static boolean
2758 debug_write_class_type (info, fns, fhandle, type, tag)
2759      struct debug_handle *info;
2760      const struct debug_write_fns *fns;
2761      PTR fhandle;
2762      struct debug_type *type;
2763      const char *tag;
2764 {
2765   unsigned int i;
2766   unsigned int id;
2767   struct debug_type *vptrbase;
2768
2769   if (type->u.kclass == NULL)
2770     {
2771       id = 0;
2772       vptrbase = NULL;
2773     }
2774   else
2775     {
2776       if (info->class_mark == type->u.kclass->mark
2777           || type->u.kclass->id > info->base_id)
2778         {
2779           /* We are currently outputting this class, or we have
2780              already output it.  This can happen when there are
2781              methods for an anonymous class.  */
2782           assert (type->u.kclass->id > info->base_id);
2783           return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2784                                    type->kind);
2785         }
2786       type->u.kclass->mark = info->class_mark;
2787       ++info->class_id;
2788       id = info->class_id;
2789       type->u.kclass->id = id;
2790
2791       vptrbase = type->u.kclass->vptrbase;
2792       if (vptrbase != NULL && vptrbase != type)
2793         {
2794           if (! debug_write_type (info, fns, fhandle, vptrbase,
2795                                   (struct debug_name *) NULL))
2796             return false;
2797         }
2798     }
2799
2800   if (! (*fns->start_class_type) (fhandle, tag, id,
2801                                   type->kind == DEBUG_KIND_CLASS,
2802                                   type->size,
2803                                   vptrbase != NULL,
2804                                   vptrbase == type))
2805     return false;
2806
2807   if (type->u.kclass != NULL)
2808     {
2809       if (type->u.kclass->fields != NULL)
2810         {
2811           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2812             {
2813               struct debug_field *f;
2814
2815               f = type->u.kclass->fields[i];
2816               if (! debug_write_type (info, fns, fhandle, f->type,
2817                                       (struct debug_name *) NULL))
2818                 return false;
2819               if (f->static_member)
2820                 {
2821                   if (! (*fns->class_static_member) (fhandle, f->name,
2822                                                      f->u.s.physname,
2823                                                      f->visibility))
2824                     return false;
2825                 }
2826               else
2827                 {
2828                   if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2829                                               f->u.f.bitsize, f->visibility))
2830                     return false;
2831                 }
2832             }
2833         }
2834
2835       if (type->u.kclass->baseclasses != NULL)
2836         {
2837           for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2838             {
2839               struct debug_baseclass *b;
2840
2841               b = type->u.kclass->baseclasses[i];
2842               if (! debug_write_type (info, fns, fhandle, b->type,
2843                                       (struct debug_name *) NULL))
2844                 return false;
2845               if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2846                                              b->visibility))
2847                 return false;
2848             }
2849         }
2850
2851       if (type->u.kclass->methods != NULL)
2852         {
2853           for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2854             {
2855               struct debug_method *m;
2856               unsigned int j;
2857
2858               m = type->u.kclass->methods[i];
2859               if (! (*fns->class_start_method) (fhandle, m->name))
2860                 return false;
2861               for (j = 0; m->variants[j] != NULL; j++)
2862                 {
2863                   struct debug_method_variant *v;
2864
2865                   v = m->variants[j];
2866                   if (v->context != NULL)
2867                     {
2868                       if (! debug_write_type (info, fns, fhandle, v->context,
2869                                               (struct debug_name *) NULL))
2870                         return false;
2871                     }
2872                   if (! debug_write_type (info, fns, fhandle, v->type,
2873                                           (struct debug_name *) NULL))
2874                     return false;
2875                   if (v->voffset != VOFFSET_STATIC_METHOD)
2876                     {
2877                       if (! (*fns->class_method_variant) (fhandle, v->physname,
2878                                                           v->visibility,
2879                                                           v->constp,
2880                                                           v->volatilep,
2881                                                           v->voffset,
2882                                                           v->context != NULL))
2883                         return false;
2884                     }
2885                   else
2886                     {
2887                       if (! (*fns->class_static_method_variant) (fhandle,
2888                                                                  v->physname,
2889                                                                  v->visibility,
2890                                                                  v->constp,
2891                                                                  v->volatilep))
2892                         return false;
2893                     }
2894                 }
2895               if (! (*fns->class_end_method) (fhandle))
2896                 return false;
2897             }
2898         }
2899     }
2900
2901   return (*fns->end_class_type) (fhandle);
2902 }
2903
2904 /* Write out information for a function.  */
2905
2906 static boolean
2907 debug_write_function (info, fns, fhandle, name, linkage, function)
2908      struct debug_handle *info;
2909      const struct debug_write_fns *fns;
2910      PTR fhandle;
2911      const char *name;
2912      enum debug_object_linkage linkage;
2913      struct debug_function *function;
2914 {
2915   struct debug_parameter *p;
2916   struct debug_block *b;
2917
2918   if (! debug_write_type (info, fns, fhandle, function->return_type,
2919                           (struct debug_name *) NULL))
2920     return false;
2921
2922   if (! (*fns->start_function) (fhandle, name,
2923                                 linkage == DEBUG_LINKAGE_GLOBAL))
2924     return false;
2925
2926   for (p = function->parameters; p != NULL; p = p->next)
2927     {
2928       if (! debug_write_type (info, fns, fhandle, p->type,
2929                               (struct debug_name *) NULL)
2930           || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2931         return false;
2932     }
2933
2934   for (b = function->blocks; b != NULL; b = b->next)
2935     {
2936       if (! debug_write_block (info, fns, fhandle, b))
2937         return false;
2938     }
2939
2940   return (*fns->end_function) (fhandle);
2941 }
2942
2943 /* Write out information for a block.  */
2944
2945 static boolean
2946 debug_write_block (info, fns, fhandle, block)
2947      struct debug_handle *info;
2948      const struct debug_write_fns *fns;
2949      PTR fhandle;
2950      struct debug_block *block;
2951 {
2952   struct debug_name *n;
2953   struct debug_block *b;
2954
2955   if (! (*fns->start_block) (fhandle, block->start))
2956     return false;
2957
2958   if (block->locals != NULL)
2959     {
2960       for (n = block->locals->list; n != NULL; n = n->next)
2961         {
2962           if (! debug_write_name (info, fns, fhandle, n))
2963             return false;
2964         }
2965     }
2966
2967   for (b = block->children; b != NULL; b = b->next)
2968     {
2969       if (! debug_write_block (info, fns, fhandle, b))
2970         return false;
2971     }
2972
2973   return (*fns->end_block) (fhandle, block->end);
2974 }