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