* debug.c (debug_append_filename): Remove.
[external/binutils.git] / binutils / debug.c
1 /* debug.c -- Handle generic debugging information.
2    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* This file implements a generic debugging format.  We may eventually
23    have readers which convert different formats into this generic
24    format, and writers which write it out.  The initial impetus for
25    this was writing a convertor from stabs to HP IEEE-695 debugging
26    format.  */
27
28 #include <stdio.h>
29 #include <assert.h>
30
31 #include "bfd.h"
32 #include "bucomm.h"
33 #include "libiberty.h"
34 #include "debug.h"
35
36 /* Global information we keep for debugging.  A pointer to this
37    structure is the debugging handle passed to all the routines.  */
38
39 struct debug_handle
40 {
41   /* A linked list of compilation units.  */
42   struct debug_unit *units;
43   /* The current compilation unit.  */
44   struct debug_unit *current_unit;
45   /* The current source file.  */
46   struct debug_file *current_file;
47   /* The current function.  */
48   struct debug_function *current_function;
49   /* The current block.  */
50   struct debug_block *current_block;
51   /* The current line number information for the current unit.  */
52   struct debug_lineno *current_lineno;
53   /* Mark.  This is used by debug_write.  */
54   unsigned int mark;
55   /* Another mark used by debug_write.  */
56   unsigned int class_mark;
57   /* A struct/class ID used by debug_write.  */
58   unsigned int class_id;
59   /* The base for class_id for this call to debug_write.  */
60   unsigned int base_id;
61 };
62
63 /* Information we keep for a single compilation unit.  */
64
65 struct debug_unit
66 {
67   /* The next compilation unit.  */
68   struct debug_unit *next;
69   /* A list of files included in this compilation unit.  The first
70      file is always the main one, and that is where the main file name
71      is stored.  */
72   struct debug_file *files;
73   /* Line number information for this compilation unit.  This is not
74      stored by function, because assembler code may have line number
75      information without function information.  */
76   struct debug_lineno *linenos;
77 };
78
79 /* Information kept for a single source file.  */
80
81 struct debug_file
82 {
83   /* The next source file in this compilation unit.  */
84   struct debug_file *next;
85   /* The name of the source file.  */
86   const char *filename;
87   /* Global functions, variables, types, etc.  */
88   struct debug_namespace *globals;
89 };
90
91 /* A type.  */
92
93 struct debug_type
94 {
95   /* Kind of type.  */
96   enum debug_type_kind kind;
97   /* Size of type (0 if not known).  */
98   unsigned int size;
99   /* Type which is a pointer to this type.  */
100   debug_type pointer;
101   /* Tagged union with additional information about the type.  */
102   union
103     {
104       /* DEBUG_KIND_INDIRECT.  */
105       struct debug_indirect_type *kindirect;
106       /* DEBUG_KIND_INT.  */
107       /* Whether the integer is unsigned.  */
108       boolean kint;
109       /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
110          DEBUG_KIND_UNION_CLASS.  */
111       struct debug_class_type *kclass;
112       /* DEBUG_KIND_ENUM.  */
113       struct debug_enum_type *kenum;
114       /* DEBUG_KIND_POINTER.  */
115       struct debug_type *kpointer;
116       /* DEBUG_KIND_FUNCTION.  */
117       struct debug_function_type *kfunction;
118       /* DEBUG_KIND_REFERENCE.  */
119       struct debug_type *kreference;
120       /* DEBUG_KIND_RANGE.  */
121       struct debug_range_type *krange;
122       /* DEBUG_KIND_ARRAY.  */
123       struct debug_array_type *karray;
124       /* DEBUG_KIND_SET.  */
125       struct debug_set_type *kset;
126       /* DEBUG_KIND_OFFSET.  */
127       struct debug_offset_type *koffset;
128       /* DEBUG_KIND_METHOD.  */
129       struct debug_method_type *kmethod;
130       /* DEBUG_KIND_CONST.  */
131       struct debug_type *kconst;
132       /* DEBUG_KIND_VOLATILE.  */
133       struct debug_type *kvolatile;
134       /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED.  */
135       struct debug_named_type *knamed;
136     } u;
137 };
138
139 /* Information kept for an indirect type.  */
140
141 struct debug_indirect_type
142 {
143   /* Slot where the final type will appear.  */
144   debug_type *slot;
145   /* Tag.  */
146   const char *tag;
147 };
148
149 /* Information kept for a struct, union, or class.  */
150
151 struct debug_class_type
152 {
153   /* NULL terminated array of fields.  */
154   debug_field *fields;
155   /* A mark field used to avoid recursively printing out structs.  */
156   unsigned int mark;
157   /* This is used to uniquely identify unnamed structs when printing.  */
158   unsigned int id;
159   /* The remaining fields are only used for DEBUG_KIND_CLASS and
160      DEBUG_KIND_UNION_CLASS.  */
161   /* NULL terminated array of base classes.  */
162   debug_baseclass *baseclasses;
163   /* NULL terminated array of methods.  */
164   debug_method *methods;
165   /* The type of the class providing the virtual function table for
166      this class.  This may point to the type itself.  */
167   debug_type vptrbase;
168 };
169
170 /* Information kept for an enum.  */
171
172 struct debug_enum_type
173 {
174   /* NULL terminated array of names.  */
175   const char **names;
176   /* Array of corresponding values.  */
177   bfd_signed_vma *values;
178 };
179
180 /* Information kept for a function.  FIXME: We should be able to
181    record the parameter types.  */
182
183 struct debug_function_type
184 {
185   /* Return type.  */
186   debug_type return_type;
187   /* NULL terminated array of argument types.  */
188   debug_type *arg_types;
189   /* Whether the function takes a variable number of arguments.  */
190   boolean varargs;
191 };
192
193 /* Information kept for a range.  */
194
195 struct debug_range_type
196 {
197   /* Range base type.  */
198   debug_type type;
199   /* Lower bound.  */
200   bfd_signed_vma lower;
201   /* Upper bound.  */
202   bfd_signed_vma upper;
203 };
204
205 /* Information kept for an array.  */
206
207 struct debug_array_type
208 {
209   /* Element type.  */
210   debug_type element_type;
211   /* Range type.  */
212   debug_type range_type;
213   /* Lower bound.  */
214   bfd_signed_vma lower;
215   /* Upper bound.  */
216   bfd_signed_vma upper;
217   /* Whether this array is really a string.  */
218   boolean stringp;
219 };
220
221 /* Information kept for a set.  */
222
223 struct debug_set_type
224 {
225   /* Base type.  */
226   debug_type type;
227   /* Whether this set is really a bitstring.  */
228   boolean bitstringp;
229 };
230
231 /* Information kept for an offset type (a based pointer).  */
232
233 struct debug_offset_type
234 {
235   /* The type the pointer is an offset from.  */
236   debug_type base_type;
237   /* The type the pointer points to.  */
238   debug_type target_type;
239 };
240
241 /* Information kept for a method type.  */
242
243 struct debug_method_type
244 {
245   /* The return type.  */
246   debug_type return_type;
247   /* The object type which this method is for.  */
248   debug_type domain_type;
249   /* A NULL terminated array of argument types.  */
250   debug_type *arg_types;
251   /* Whether the method takes a variable number of arguments.  */
252   boolean varargs;
253 };
254
255 /* Information kept for a named type.  */
256
257 struct debug_named_type
258 {
259   /* Name.  */
260   struct debug_name *name;
261   /* Real type.  */
262   debug_type type;
263 };
264
265 /* A field in a struct or union.  */
266
267 struct debug_field
268 {
269   /* Name of the field.  */
270   const char *name;
271   /* Type of the field.  */
272   struct debug_type *type;
273   /* Visibility of the field.  */
274   enum debug_visibility visibility;
275   /* Whether this is a static member.  */
276   boolean static_member;
277   union
278     {
279       /* If static_member is false.  */
280       struct
281         {
282           /* Bit position of the field in the struct.  */
283           unsigned int bitpos;
284           /* Size of the field in bits.  */
285           unsigned int bitsize;
286         } f;
287       /* If static_member is true.  */
288       struct
289         {
290           const char *physname;
291         } s;
292     } u;
293 };
294
295 /* A base class for an object.  */
296
297 struct debug_baseclass
298 {
299   /* Type of the base class.  */
300   struct debug_type *type;
301   /* Bit position of the base class in the object.  */
302   unsigned int bitpos;
303   /* Whether the base class is virtual.  */
304   boolean virtual;
305   /* Visibility of the base class.  */
306   enum debug_visibility visibility;
307 };
308
309 /* A method of an object.  */
310
311 struct debug_method
312 {
313   /* The name of the method.  */
314   const char *name;
315   /* A NULL terminated array of different types of variants.  */
316   struct debug_method_variant **variants;
317 };
318
319 /* The variants of a method function of an object.  These indicate
320    which method to run.  */
321
322 struct debug_method_variant
323 {
324   /* The physical name of the function.  */
325   const char *physname;
326   /* The type of the function.  */
327   struct debug_type *type;
328   /* The visibility of the function.  */
329   enum debug_visibility visibility;
330   /* Whether the function is const.  */
331   boolean constp;
332   /* Whether the function is volatile.  */
333   boolean volatilep;
334   /* The offset to the function in the virtual function table.  */
335   bfd_vma voffset;
336   /* If voffset is VOFFSET_STATIC_METHOD, this is a static method.  */
337 #define VOFFSET_STATIC_METHOD (1)
338   /* Context of a virtual method function.  */
339   struct debug_type *context;
340 };
341
342 /* A variable.  This is the information we keep for a variable object.
343    This has no name; a name is associated with a variable in a
344    debug_name structure.  */
345
346 struct debug_variable
347 {
348   /* Kind of variable.  */
349   enum debug_var_kind kind;
350   /* Type.  */
351   debug_type type;
352   /* Value.  The interpretation of the value depends upon kind.  */
353   bfd_vma val;
354 };
355
356 /* A function.  This has no name; a name is associated with a function
357    in a debug_name structure.  */
358
359 struct debug_function
360 {
361   /* Return type.  */
362   debug_type return_type;
363   /* Parameter information.  */
364   struct debug_parameter *parameters;
365   /* Block information.  The first structure on the list is the main
366      block of the function, and describes function local variables.  */
367   struct debug_block *blocks;
368 };
369
370 /* A function parameter.  */
371
372 struct debug_parameter
373 {
374   /* Next parameter.  */
375   struct debug_parameter *next;
376   /* Name.  */
377   const char *name;
378   /* Type.  */
379   debug_type type;
380   /* Kind.  */
381   enum debug_parm_kind kind;
382   /* Value (meaning depends upon kind).  */
383   bfd_vma val;
384 };
385
386 /* A typed constant.  */
387
388 struct debug_typed_constant
389 {
390   /* Type.  */
391   debug_type type;
392   /* Value.  FIXME: We may eventually need to support non-integral
393      values.  */
394   bfd_vma val;
395 };
396
397 /* Information about a block within a function.  */
398
399 struct debug_block
400 {
401   /* Next block with the same parent.  */
402   struct debug_block *next;
403   /* Parent block.  */
404   struct debug_block *parent;
405   /* List of child blocks.  */
406   struct debug_block *children;
407   /* Start address of the block.  */
408   bfd_vma start;
409   /* End address of the block.  */
410   bfd_vma end;
411   /* Local variables.  */
412   struct debug_namespace *locals;
413 };
414
415 /* Line number information we keep for a compilation unit.  FIXME:
416    This structure is easy to create, but can be very space
417    inefficient.  */
418
419 struct debug_lineno
420 {
421   /* More line number information for this block.  */
422   struct debug_lineno *next;
423   /* Source file.  */
424   struct debug_file *file;
425   /* Line numbers, terminated by a -1 or the end of the array.  */
426 #define DEBUG_LINENO_COUNT 10
427   unsigned long linenos[DEBUG_LINENO_COUNT];
428   /* Addresses for the line numbers.  */
429   bfd_vma addrs[DEBUG_LINENO_COUNT];
430 };
431
432 /* A namespace.  This is a mapping from names to objects.  FIXME: This
433    should be implemented as a hash table.  */
434
435 struct debug_namespace
436 {
437   /* List of items in this namespace.  */
438   struct debug_name *list;
439   /* Pointer to where the next item in this namespace should go.  */
440   struct debug_name **tail;
441 };
442
443 /* Kinds of objects that appear in a namespace.  */
444
445 enum debug_object_kind
446 {
447   /* A type.  */
448   DEBUG_OBJECT_TYPE,
449   /* A tagged type (really a different sort of namespace).  */
450   DEBUG_OBJECT_TAG,
451   /* A variable.  */
452   DEBUG_OBJECT_VARIABLE,
453   /* A function.  */
454   DEBUG_OBJECT_FUNCTION,
455   /* An integer constant.  */
456   DEBUG_OBJECT_INT_CONSTANT,
457   /* A floating point constant.  */
458   DEBUG_OBJECT_FLOAT_CONSTANT,
459   /* A typed constant.  */
460   DEBUG_OBJECT_TYPED_CONSTANT
461 };
462
463 /* Linkage of an object that appears in a namespace.  */
464
465 enum debug_object_linkage
466 {
467   /* Local variable.  */
468   DEBUG_LINKAGE_AUTOMATIC,
469   /* Static--either file static or function static, depending upon the
470      namespace is.  */
471   DEBUG_LINKAGE_STATIC,
472   /* Global.  */
473   DEBUG_LINKAGE_GLOBAL,
474   /* No linkage.  */
475   DEBUG_LINKAGE_NONE
476 };
477
478 /* A name in a namespace.  */
479
480 struct debug_name
481 {
482   /* Next name in this namespace.  */
483   struct debug_name *next;
484   /* Name.  */
485   const char *name;
486   /* Mark.  This is used by debug_write.  */
487   unsigned int mark;
488   /* Kind of object.  */
489   enum debug_object_kind kind;
490   /* Linkage of object.  */
491   enum debug_object_linkage linkage;
492   /* Tagged union with additional information about the object.  */
493   union
494     {
495       /* DEBUG_OBJECT_TYPE.  */
496       struct debug_type *type;
497       /* DEBUG_OBJECT_TAG.  */
498       struct debug_type *tag;
499       /* DEBUG_OBJECT_VARIABLE.  */
500       struct debug_variable *variable;
501       /* DEBUG_OBJECT_FUNCTION.  */
502       struct debug_function *function;
503       /* DEBUG_OBJECT_INT_CONSTANT.  */
504       bfd_vma int_constant;
505       /* DEBUG_OBJECT_FLOAT_CONSTANT.  */
506       double float_constant;
507       /* DEBUG_OBJECT_TYPED_CONSTANT.  */
508       struct debug_typed_constant *typed_constant;
509     } u;
510 };
511
512 /* Local functions.  */
513
514 static void debug_error PARAMS ((const char *));
515 static struct debug_name *debug_add_to_namespace
516   PARAMS ((struct debug_handle *, struct debug_namespace **, const char *,
517            enum debug_object_kind, enum debug_object_linkage));
518 static struct debug_name *debug_add_to_current_namespace
519   PARAMS ((struct debug_handle *, const char *, enum debug_object_kind,
520            enum debug_object_linkage));
521 static struct debug_type *debug_make_type
522   PARAMS ((struct debug_handle *, enum debug_type_kind, unsigned int));
523 static struct debug_type *debug_get_real_type PARAMS ((PTR, debug_type));
524 static boolean debug_write_name
525   PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
526            struct debug_name *));
527 static boolean debug_write_type
528   PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
529            struct debug_type *, struct debug_name *));
530 static boolean debug_write_class_type
531   PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
532            struct debug_type *, const char *));
533 static boolean debug_write_function
534   PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
535            const char *, enum debug_object_linkage, struct debug_function *));
536 static boolean debug_write_block
537   PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
538            struct debug_block *));
539 \f
540 /* Issue an error message.  */
541
542 static void
543 debug_error (message)
544      const char *message;
545 {
546   fprintf (stderr, "%s\n", message);
547 }
548
549 /* Add an object to a namespace.  */
550
551 static struct debug_name *
552 debug_add_to_namespace (info, nsp, name, kind, linkage)
553      struct debug_handle *info;
554      struct debug_namespace **nsp;
555      const char *name;
556      enum debug_object_kind kind;
557      enum debug_object_linkage linkage;
558 {
559   struct debug_name *n;
560   struct debug_namespace *ns;
561
562   n = (struct debug_name *) xmalloc (sizeof *n);
563   memset (n, 0, sizeof *n);
564
565   n->name = name;
566   n->kind = kind;
567   n->linkage = linkage;
568
569   ns = *nsp;
570   if (ns == NULL)
571     {
572       ns = (struct debug_namespace *) xmalloc (sizeof *ns);
573       memset (ns, 0, sizeof *ns);
574
575       ns->tail = &ns->list;
576
577       *nsp = ns;
578     }
579
580   *ns->tail = n;
581   ns->tail = &n->next;
582
583   return n;
584 }
585
586 /* Add an object to the current namespace.  */
587
588 static struct debug_name *
589 debug_add_to_current_namespace (info, name, kind, linkage)
590      struct debug_handle *info;
591      const char *name;
592      enum debug_object_kind kind;
593      enum debug_object_linkage linkage;
594 {
595   struct debug_namespace **nsp;
596
597   if (info->current_unit == NULL
598       || info->current_file == NULL)
599     {
600       debug_error ("debug_add_to_current_namespace: no current file");
601       return NULL;
602     }
603
604   if (info->current_block != NULL)
605     nsp = &info->current_block->locals;
606   else
607     nsp = &info->current_file->globals;
608
609   return debug_add_to_namespace (info, nsp, name, kind, linkage);
610 }
611 \f
612 /* Return a handle for debugging information.  */
613
614 PTR
615 debug_init ()
616 {
617   struct debug_handle *ret;
618
619   ret = (struct debug_handle *) xmalloc (sizeof *ret);
620   memset (ret, 0, sizeof *ret);
621   return (PTR) ret;
622 }
623
624 /* Set the source filename.  This implicitly starts a new compilation
625    unit.  */
626
627 boolean
628 debug_set_filename (handle, name)
629      PTR handle;
630      const char *name;
631 {
632   struct debug_handle *info = (struct debug_handle *) handle;
633   struct debug_file *nfile;
634   struct debug_unit *nunit;
635
636   if (name == NULL)
637     name = "";
638
639   nfile = (struct debug_file *) xmalloc (sizeof *nfile);
640   memset (nfile, 0, sizeof *nfile);
641
642   nfile->filename = name;
643
644   nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
645   memset (nunit, 0, sizeof *nunit);
646
647   nunit->files = nfile;
648   info->current_file = nfile;
649
650   if (info->current_unit != NULL)
651     info->current_unit->next = nunit;
652   else
653     {
654       assert (info->units == NULL);
655       info->units = nunit;
656     }
657
658   info->current_unit = nunit;
659
660   info->current_function = NULL;
661   info->current_block = NULL;
662   info->current_lineno = NULL;
663
664   return true;
665 }
666
667 /* 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_METHOD:
2241       *pvarargs = type->u.kmethod->varargs;
2242       return type->u.kmethod->arg_types;
2243     }
2244   /*NOTREACHED*/
2245 }
2246
2247 /* Get the target type of a type.  */
2248
2249 debug_type
2250 debug_get_target_type (handle, type)
2251      PTR handle;
2252      debug_type type;
2253 {
2254   if (type == NULL)
2255     return NULL;
2256   type = debug_get_real_type (handle, type);
2257   switch (type->kind)
2258     {
2259     default:
2260       return NULL;
2261     case DEBUG_KIND_POINTER:
2262       return type->u.kpointer;
2263     case DEBUG_KIND_REFERENCE:
2264       return type->u.kreference;
2265     case DEBUG_KIND_CONST:
2266       return type->u.kconst;
2267     case DEBUG_KIND_VOLATILE:
2268       return type->u.kvolatile;
2269     }
2270   /*NOTREACHED*/
2271 }
2272
2273 /* Get the NULL terminated array of fields for a struct, union, or
2274    class.  */
2275
2276 const debug_field *
2277 debug_get_fields (handle, type)
2278      PTR handle;
2279      debug_type type;
2280 {
2281   if (type == NULL)
2282     return NULL;
2283   type = debug_get_real_type (handle, type);
2284   switch (type->kind)
2285     {
2286     default:
2287       return NULL;
2288     case DEBUG_KIND_STRUCT:
2289     case DEBUG_KIND_UNION:
2290     case DEBUG_KIND_CLASS:
2291     case DEBUG_KIND_UNION_CLASS:
2292       return type->u.kclass->fields;
2293     }
2294   /*NOTREACHED*/
2295 }
2296
2297 /* Get the type of a field.  */
2298
2299 /*ARGSUSED*/
2300 debug_type
2301 debug_get_field_type (handle, field)
2302      PTR handle;
2303      debug_field field;
2304 {
2305   if (field == NULL)
2306     return NULL;
2307   return field->type;
2308 }
2309
2310 /* Get the name of a field.  */
2311
2312 /*ARGSUSED*/
2313 const char *
2314 debug_get_field_name (handle, field)
2315      PTR handle;
2316      debug_field field;
2317 {
2318   if (field == NULL)
2319     return NULL;
2320   return field->name;
2321 }
2322
2323 /* Get the bit position of a field.  */
2324
2325 /*ARGSUSED*/
2326 bfd_vma
2327 debug_get_field_bitpos (handle, field)
2328      PTR handle;
2329      debug_field field;
2330 {
2331   if (field == NULL || field->static_member)
2332     return (bfd_vma) -1;
2333   return field->u.f.bitpos;
2334 }
2335
2336 /* Get the bit size of a field.  */
2337
2338 /*ARGSUSED*/
2339 bfd_vma
2340 debug_get_field_bitsize (handle, field)
2341      PTR handle;
2342      debug_field field;
2343 {
2344   if (field == NULL || field->static_member)
2345     return (bfd_vma) -1;
2346   return field->u.f.bitsize;
2347 }
2348
2349 /* Get the visibility of a field.  */
2350
2351 /*ARGSUSED*/
2352 enum debug_visibility
2353 debug_get_field_visibility (handle, field)
2354      PTR handle;
2355      debug_field field;
2356 {
2357   if (field == NULL)
2358     return DEBUG_VISIBILITY_IGNORE;
2359   return field->visibility;
2360 }
2361
2362 /* Get the physical name of a field.  */
2363
2364 const char *
2365 debug_get_field_physname (handle, field)
2366      PTR handle;
2367      debug_field field;
2368 {
2369   if (field == NULL || ! field->static_member)
2370     return NULL;
2371   return field->u.s.physname;
2372 }
2373 \f
2374 /* Write out the debugging information.  This is given a handle to
2375    debugging information, and a set of function pointers to call.  */
2376
2377 boolean
2378 debug_write (handle, fns, fhandle)
2379      PTR handle;
2380      const struct debug_write_fns *fns;
2381      PTR fhandle;
2382 {
2383   struct debug_handle *info = (struct debug_handle *) handle;
2384   struct debug_unit *u;
2385
2386   /* We use a mark to tell whether we have already written out a
2387      particular name.  We use an integer, so that we don't have to
2388      clear the mark fields if we happen to write out the same
2389      information more than once.  */
2390   ++info->mark;
2391
2392   /* The base_id field holds an ID value which will never be used, so
2393      that we can tell whether we have assigned an ID during this call
2394      to debug_write.  */
2395   info->base_id = info->class_id;
2396
2397   for (u = info->units; u != NULL; u = u->next)
2398     {
2399       struct debug_file *f;
2400       boolean first_file;
2401       struct debug_lineno *l;
2402
2403       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2404         return false;
2405
2406       first_file = true;
2407       for (f = u->files; f != NULL; f = f->next)
2408         {
2409           struct debug_name *n;
2410
2411           if (first_file)
2412             first_file = false;
2413           else
2414             {
2415               if (! (*fns->start_source) (fhandle, f->filename))
2416                 return false;
2417             }
2418
2419           if (f->globals != NULL)
2420             {
2421               for (n = f->globals->list; n != NULL; n = n->next)
2422                 {
2423                   if (! debug_write_name (info, fns, fhandle, n))
2424                     return false;
2425                 }
2426             }
2427         }
2428
2429       for (l = u->linenos; l != NULL; l = l->next)
2430         {
2431           unsigned int i;
2432
2433           for (i = 0; i < DEBUG_LINENO_COUNT; i++)
2434             {
2435               if (l->linenos[i] == (unsigned long) -1)
2436                 break;
2437               if (! (*fns->lineno) (fhandle, l->file->filename, l->linenos[i],
2438                                     l->addrs[i]))
2439                 return false;
2440             }
2441         }
2442     }
2443
2444   return true;
2445 }
2446
2447 /* Write out an element in a namespace.  */
2448
2449 static boolean
2450 debug_write_name (info, fns, fhandle, n)
2451      struct debug_handle *info;
2452      const struct debug_write_fns *fns;
2453      PTR fhandle;
2454      struct debug_name *n;
2455 {
2456   /* The class_mark field is used to prevent recursively outputting a
2457      struct or class.  */
2458   ++info->class_mark;
2459
2460   switch (n->kind)
2461     {
2462     case DEBUG_OBJECT_TYPE:
2463       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2464           || ! (*fns->typdef) (fhandle, n->name))
2465         return false;
2466       return true;
2467     case DEBUG_OBJECT_TAG:
2468       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2469         return false;
2470       return (*fns->tag) (fhandle, n->name);
2471     case DEBUG_OBJECT_VARIABLE:
2472       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2473                               (struct debug_name *) NULL))
2474         return false;
2475       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2476                                n->u.variable->val);
2477     case DEBUG_OBJECT_FUNCTION:
2478       return debug_write_function (info, fns, fhandle, n->name,
2479                                    n->linkage, n->u.function);
2480     case DEBUG_OBJECT_INT_CONSTANT:
2481       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2482     case DEBUG_OBJECT_FLOAT_CONSTANT:
2483       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2484     case DEBUG_OBJECT_TYPED_CONSTANT:
2485       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2486                               (struct debug_name *) NULL))
2487         return false;
2488       return (*fns->typed_constant) (fhandle, n->name,
2489                                      n->u.typed_constant->val);
2490     default:
2491       abort ();
2492       return false;
2493     }
2494   /*NOTREACHED*/
2495 }
2496
2497 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
2498    DEBUG_KIND_TAGGED, then the name argument is the name for which we
2499    are about to call typedef or tag.  If the type is anything else,
2500    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2501    points to this one.  */
2502
2503 static boolean
2504 debug_write_type (info, fns, fhandle, type, name)
2505      struct debug_handle *info;
2506      const struct debug_write_fns *fns;
2507      PTR fhandle;
2508      struct debug_type *type;
2509      struct debug_name *name;
2510 {
2511   unsigned int i;
2512   int is;
2513   const char *tag;
2514
2515   /* If we have a name for this type, just output it.  We only output
2516      typedef names after they have been defined.  We output type tags
2517      whenever we are not actually defining them.  */
2518   if ((type->kind == DEBUG_KIND_NAMED
2519        || type->kind == DEBUG_KIND_TAGGED)
2520       && (type->u.knamed->name->mark == info->mark
2521           || (type->kind == DEBUG_KIND_TAGGED
2522               && type->u.knamed->name != name)))
2523     {
2524       if (type->kind == DEBUG_KIND_NAMED)
2525         return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2526       else
2527         {
2528           struct debug_type *real;
2529
2530           real = debug_get_real_type ((PTR) info, type);
2531           return (*fns->tag_type) (fhandle, type->u.knamed->name->name, 0,
2532                                    real->kind);
2533         }
2534     }
2535
2536   /* Mark the name after we have already looked for a known name, so
2537      that we don't just define a type in terms of itself.  We need to
2538      mark the name here so that a struct containing a pointer to
2539      itself will work.  */
2540   if (name != NULL)
2541     name->mark = info->mark;
2542
2543   tag = NULL;
2544   if (name != NULL
2545       && type->kind != DEBUG_KIND_NAMED
2546       && type->kind != DEBUG_KIND_TAGGED)
2547     {
2548       assert (name->kind == DEBUG_OBJECT_TAG);
2549       tag = name->name;
2550     }
2551
2552   switch (type->kind)
2553     {
2554     case DEBUG_KIND_ILLEGAL:
2555       debug_error ("debug_write_type: illegal type encountered");
2556       return false;
2557     case DEBUG_KIND_INDIRECT:
2558       if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2559         return (*fns->empty_type) (fhandle);
2560       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2561                                name);
2562     case DEBUG_KIND_VOID:
2563       return (*fns->void_type) (fhandle);
2564     case DEBUG_KIND_INT:
2565       return (*fns->int_type) (fhandle, type->size, type->u.kint);
2566     case DEBUG_KIND_FLOAT:
2567       return (*fns->float_type) (fhandle, type->size);
2568     case DEBUG_KIND_COMPLEX:
2569       return (*fns->complex_type) (fhandle, type->size);
2570     case DEBUG_KIND_BOOL:
2571       return (*fns->bool_type) (fhandle, type->size);
2572     case DEBUG_KIND_STRUCT:
2573     case DEBUG_KIND_UNION:
2574       if (type->u.kclass != NULL)
2575         {
2576           if (info->class_mark == type->u.kclass->mark
2577               || type->u.kclass->id > info->base_id)
2578             {
2579               /* We are currently outputting this struct, or we have
2580                  already output it.  I don't know if this can happen,
2581                  but it can happen for a class.  */
2582               assert (type->u.kclass->id > info->base_id);
2583               return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2584                                        type->kind);
2585             }
2586           type->u.kclass->mark = info->class_mark;
2587           ++info->class_id;
2588           type->u.kclass->id = info->class_id;
2589         }
2590
2591       if (! (*fns->start_struct_type) (fhandle, tag,
2592                                        (type->u.kclass != NULL
2593                                         ? type->u.kclass->id
2594                                         : 0),
2595                                        type->kind == DEBUG_KIND_STRUCT,
2596                                        type->size))
2597         return false;
2598       if (type->u.kclass != NULL
2599           && type->u.kclass->fields != NULL)
2600         {
2601           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2602             {
2603               struct debug_field *f;
2604
2605               f = type->u.kclass->fields[i];
2606               if (! debug_write_type (info, fns, fhandle, f->type,
2607                                       (struct debug_name *) NULL)
2608                   || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2609                                              f->u.f.bitsize, f->visibility))
2610                 return false;
2611             }
2612         }
2613       return (*fns->end_struct_type) (fhandle);
2614     case DEBUG_KIND_CLASS:
2615     case DEBUG_KIND_UNION_CLASS:
2616       return debug_write_class_type (info, fns, fhandle, type, tag);
2617     case DEBUG_KIND_ENUM:
2618       if (type->u.kenum == NULL)
2619         return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2620                                   (bfd_signed_vma *) NULL);
2621       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2622                                 type->u.kenum->values);
2623     case DEBUG_KIND_POINTER:
2624       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2625                               (struct debug_name *) NULL))
2626         return false;
2627       return (*fns->pointer_type) (fhandle);
2628     case DEBUG_KIND_FUNCTION:
2629       if (type->u.kfunction->arg_types == NULL)
2630         is = -1;
2631       else
2632         {
2633           for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2634             if (! debug_write_type (info, fns, fhandle,
2635                                     type->u.kfunction->arg_types[is],
2636                                     (struct debug_name *) NULL))
2637               return false;
2638         }
2639       if (! debug_write_type (info, fns, fhandle,
2640                               type->u.kfunction->return_type,
2641                               (struct debug_name *) NULL))
2642         return false;
2643       return (*fns->function_type) (fhandle, is,
2644                                     type->u.kfunction->varargs);
2645     case DEBUG_KIND_REFERENCE:
2646       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2647                               (struct debug_name *) NULL))
2648         return false;
2649       return (*fns->reference_type) (fhandle);
2650     case DEBUG_KIND_RANGE:
2651       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2652                               (struct debug_name *) NULL))
2653         return false;
2654       return (*fns->range_type) (fhandle, type->u.krange->lower,
2655                                  type->u.krange->upper);
2656     case DEBUG_KIND_ARRAY:
2657       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2658                               (struct debug_name *) NULL)
2659           || ! debug_write_type (info, fns, fhandle,
2660                                  type->u.karray->range_type,
2661                                  (struct debug_name *) NULL))
2662         return false;
2663       return (*fns->array_type) (fhandle, type->u.karray->lower,
2664                                  type->u.karray->upper,
2665                                  type->u.karray->stringp);
2666     case DEBUG_KIND_SET:
2667       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2668                               (struct debug_name *) NULL))
2669         return false;
2670       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2671     case DEBUG_KIND_OFFSET:
2672       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2673                               (struct debug_name *) NULL)
2674           || ! debug_write_type (info, fns, fhandle,
2675                                  type->u.koffset->target_type,
2676                                  (struct debug_name *) NULL))
2677         return false;
2678       return (*fns->offset_type) (fhandle);
2679     case DEBUG_KIND_METHOD:
2680       if (! debug_write_type (info, fns, fhandle,
2681                               type->u.kmethod->return_type,
2682                               (struct debug_name *) NULL))
2683         return false;
2684       if (type->u.kmethod->arg_types == NULL)
2685         is = -1;
2686       else
2687         {
2688           for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2689             if (! debug_write_type (info, fns, fhandle,
2690                                     type->u.kmethod->arg_types[is],
2691                                     (struct debug_name *) NULL))
2692               return false;
2693         }
2694       if (type->u.kmethod->domain_type != NULL)
2695         {
2696           if (! debug_write_type (info, fns, fhandle,
2697                                   type->u.kmethod->domain_type,
2698                                   (struct debug_name *) NULL))
2699             return false;
2700         }
2701       return (*fns->method_type) (fhandle,
2702                                   type->u.kmethod->domain_type != NULL,
2703                                   is,
2704                                   type->u.kmethod->varargs);
2705     case DEBUG_KIND_CONST:
2706       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2707                               (struct debug_name *) NULL))
2708         return false;
2709       return (*fns->const_type) (fhandle);
2710     case DEBUG_KIND_VOLATILE:
2711       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2712                               (struct debug_name *) NULL))
2713         return false;
2714       return (*fns->volatile_type) (fhandle);
2715     case DEBUG_KIND_NAMED:
2716       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2717                                (struct debug_name *) NULL);
2718     case DEBUG_KIND_TAGGED:
2719       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2720                                type->u.knamed->name);
2721     default:
2722       abort ();
2723       return false;
2724     }
2725 }
2726
2727 /* Write out a class type.  */
2728
2729 static boolean
2730 debug_write_class_type (info, fns, fhandle, type, tag)
2731      struct debug_handle *info;
2732      const struct debug_write_fns *fns;
2733      PTR fhandle;
2734      struct debug_type *type;
2735      const char *tag;
2736 {
2737   unsigned int i;
2738   unsigned int id;
2739   struct debug_type *vptrbase;
2740
2741   if (type->u.kclass == NULL)
2742     {
2743       id = 0;
2744       vptrbase = NULL;
2745     }
2746   else
2747     {
2748       if (info->class_mark == type->u.kclass->mark
2749           || type->u.kclass->id > info->base_id)
2750         {
2751           /* We are currently outputting this class, or we have
2752              already output it.  This can happen when there are
2753              methods for an anonymous class.  */
2754           assert (type->u.kclass->id > info->base_id);
2755           return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2756                                    type->kind);
2757         }
2758       type->u.kclass->mark = info->class_mark;
2759       ++info->class_id;
2760       id = info->class_id;
2761       type->u.kclass->id = id;
2762
2763       vptrbase = type->u.kclass->vptrbase;
2764       if (vptrbase != NULL && vptrbase != type)
2765         {
2766           if (! debug_write_type (info, fns, fhandle, vptrbase,
2767                                   (struct debug_name *) NULL))
2768             return false;
2769         }
2770     }
2771
2772   if (! (*fns->start_class_type) (fhandle, tag, id,
2773                                   type->kind == DEBUG_KIND_CLASS,
2774                                   type->size,
2775                                   vptrbase != NULL,
2776                                   vptrbase == type))
2777     return false;
2778
2779   if (type->u.kclass != NULL)
2780     {
2781       if (type->u.kclass->fields != NULL)
2782         {
2783           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2784             {
2785               struct debug_field *f;
2786
2787               f = type->u.kclass->fields[i];
2788               if (! debug_write_type (info, fns, fhandle, f->type,
2789                                       (struct debug_name *) NULL))
2790                 return false;
2791               if (f->static_member)
2792                 {
2793                   if (! (*fns->class_static_member) (fhandle, f->name,
2794                                                      f->u.s.physname,
2795                                                      f->visibility))
2796                     return false;
2797                 }
2798               else
2799                 {
2800                   if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2801                                               f->u.f.bitsize, f->visibility))
2802                     return false;
2803                 }
2804             }
2805         }
2806
2807       if (type->u.kclass->baseclasses != NULL)
2808         {
2809           for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2810             {
2811               struct debug_baseclass *b;
2812
2813               b = type->u.kclass->baseclasses[i];
2814               if (! debug_write_type (info, fns, fhandle, b->type,
2815                                       (struct debug_name *) NULL))
2816                 return false;
2817               if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2818                                              b->visibility))
2819                 return false;
2820             }
2821         }
2822
2823       if (type->u.kclass->methods != NULL)
2824         {
2825           for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2826             {
2827               struct debug_method *m;
2828               unsigned int j;
2829
2830               m = type->u.kclass->methods[i];
2831               if (! (*fns->class_start_method) (fhandle, m->name))
2832                 return false;
2833               for (j = 0; m->variants[j] != NULL; j++)
2834                 {
2835                   struct debug_method_variant *v;
2836
2837                   v = m->variants[j];
2838                   if (v->context != NULL)
2839                     {
2840                       if (! debug_write_type (info, fns, fhandle, v->context,
2841                                               (struct debug_name *) NULL))
2842                         return false;
2843                     }
2844                   if (! debug_write_type (info, fns, fhandle, v->type,
2845                                           (struct debug_name *) NULL))
2846                     return false;
2847                   if (v->voffset != VOFFSET_STATIC_METHOD)
2848                     {
2849                       if (! (*fns->class_method_variant) (fhandle, v->physname,
2850                                                           v->visibility,
2851                                                           v->constp,
2852                                                           v->volatilep,
2853                                                           v->voffset,
2854                                                           v->context != NULL))
2855                         return false;
2856                     }
2857                   else
2858                     {
2859                       if (! (*fns->class_static_method_variant) (fhandle,
2860                                                                  v->physname,
2861                                                                  v->visibility,
2862                                                                  v->constp,
2863                                                                  v->volatilep))
2864                         return false;
2865                     }
2866                 }
2867               if (! (*fns->class_end_method) (fhandle))
2868                 return false;
2869             }
2870         }
2871     }
2872
2873   return (*fns->end_class_type) (fhandle);
2874 }
2875
2876 /* Write out information for a function.  */
2877
2878 static boolean
2879 debug_write_function (info, fns, fhandle, name, linkage, function)
2880      struct debug_handle *info;
2881      const struct debug_write_fns *fns;
2882      PTR fhandle;
2883      const char *name;
2884      enum debug_object_linkage linkage;
2885      struct debug_function *function;
2886 {
2887   struct debug_parameter *p;
2888   struct debug_block *b;
2889
2890   if (! debug_write_type (info, fns, fhandle, function->return_type,
2891                           (struct debug_name *) NULL))
2892     return false;
2893
2894   if (! (*fns->start_function) (fhandle, name,
2895                                 linkage == DEBUG_LINKAGE_GLOBAL))
2896     return false;
2897
2898   for (p = function->parameters; p != NULL; p = p->next)
2899     {
2900       if (! debug_write_type (info, fns, fhandle, p->type,
2901                               (struct debug_name *) NULL)
2902           || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2903         return false;
2904     }
2905
2906   for (b = function->blocks; b != NULL; b = b->next)
2907     {
2908       if (! debug_write_block (info, fns, fhandle, b))
2909         return false;
2910     }
2911
2912   return (*fns->end_function) (fhandle);
2913 }
2914
2915 /* Write out information for a block.  */
2916
2917 static boolean
2918 debug_write_block (info, fns, fhandle, block)
2919      struct debug_handle *info;
2920      const struct debug_write_fns *fns;
2921      PTR fhandle;
2922      struct debug_block *block;
2923 {
2924   struct debug_name *n;
2925   struct debug_block *b;
2926
2927   if (! (*fns->start_block) (fhandle, block->start))
2928     return false;
2929
2930   if (block->locals != NULL)
2931     {
2932       for (n = block->locals->list; n != NULL; n = n->next)
2933         {
2934           if (! debug_write_name (info, fns, fhandle, n))
2935             return false;
2936         }
2937     }
2938
2939   for (b = block->children; b != NULL; b = b->next)
2940     {
2941       if (! debug_write_block (info, fns, fhandle, b))
2942         return false;
2943     }
2944
2945   return (*fns->end_block) (fhandle, block->end);
2946 }