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