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