comment change
[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.  */
721
722 boolean
723 debug_record_function (handle, name, return_type, global, addr)
724      PTR handle;
725      const char *name;
726      debug_type return_type;
727      boolean global;
728      bfd_vma addr;
729 {
730   struct debug_handle *info = (struct debug_handle *) handle;
731   struct debug_function *f;
732   struct debug_block *b;
733   struct debug_name *n;
734
735   if (name == NULL)
736     name = "";
737   if (return_type == NULL)
738     return false;
739
740   if (info->current_unit == NULL)
741     {
742       debug_error ("debug_record_function: no debug_set_filename call");
743       return false;
744     }
745
746   f = (struct debug_function *) xmalloc (sizeof *f);
747   memset (f, 0, sizeof *f);
748
749   f->return_type = return_type;
750
751   b = (struct debug_block *) xmalloc (sizeof *b);
752   memset (b, 0, sizeof *b);
753
754   b->start = addr;
755   b->end = (bfd_vma) -1;
756
757   f->blocks = b;
758
759   info->current_function = f;
760   info->current_block = b;
761
762   /* FIXME: If we could handle nested functions, this would be the
763      place: we would want to use a different namespace.  */
764   n = debug_add_to_namespace (info,
765                               &info->current_file->globals,
766                               name,
767                               DEBUG_OBJECT_FUNCTION,
768                               (global
769                                ? DEBUG_LINKAGE_GLOBAL
770                                : DEBUG_LINKAGE_STATIC));
771   if (n == NULL)
772     return false;
773
774   n->u.function = f;
775
776   return true;
777 }
778
779 /* Record a parameter for the current function.  */
780
781 boolean
782 debug_record_parameter (handle, name, type, kind, val)
783      PTR handle;
784      const char *name;
785      debug_type type;
786      enum debug_parm_kind kind;
787      bfd_vma val;
788 {
789   struct debug_handle *info = (struct debug_handle *) handle;
790   struct debug_parameter *p, **pp;
791
792   if (name == NULL || type == NULL)
793     return false;
794
795   if (info->current_unit == NULL
796       || info->current_function == NULL)
797     {
798       debug_error ("debug_record_parameter: no current function");
799       return false;
800     }
801
802   p = (struct debug_parameter *) xmalloc (sizeof *p);
803   memset (p, 0, sizeof *p);
804
805   p->name = name;
806   p->type = type;
807   p->kind = kind;
808   p->val = val;
809
810   for (pp = &info->current_function->parameters;
811        *pp != NULL;
812        pp = &(*pp)->next)
813     ;
814   *pp = p;
815
816   return true;
817 }
818
819 /* End a function.  FIXME: This should handle function nesting.  */
820
821 boolean
822 debug_end_function (handle, addr)
823      PTR handle;
824      bfd_vma addr;
825 {
826   struct debug_handle *info = (struct debug_handle *) handle;
827
828   if (info->current_unit == NULL
829       || info->current_block == NULL
830       || info->current_function == NULL)
831     {
832       debug_error ("debug_end_function: no current function");
833       return false;
834     }
835
836   if (info->current_block->parent != NULL)
837     {
838       debug_error ("debug_end_function: some blocks were not closed");
839       return false;
840     }
841
842   info->current_block->end = addr;
843
844   info->current_function = NULL;
845   info->current_block = NULL;
846
847   return true;
848 }
849
850 /* Start a block in a function.  All local information will be
851    recorded in this block, until the matching call to debug_end_block.
852    debug_start_block and debug_end_block may be nested.  The bfd_vma
853    argument is the address at which this block starts.  */
854
855 boolean
856 debug_start_block (handle, addr)
857      PTR handle;
858      bfd_vma addr;
859 {
860   struct debug_handle *info = (struct debug_handle *) handle;
861   struct debug_block *b, **pb;
862
863   /* We must always have a current block: debug_record_function sets
864      one up.  */
865   if (info->current_unit == NULL
866       || info->current_block == NULL)
867     {
868       debug_error ("debug_start_block: no current block");
869       return false;
870     }
871
872   b = (struct debug_block *) xmalloc (sizeof *b);
873   memset (b, 0, sizeof *b);
874
875   b->parent = info->current_block;
876   b->start = addr;
877   b->end = (bfd_vma) -1;
878
879   /* This new block is a child of the current block.  */
880   for (pb = &info->current_block->children;
881        *pb != NULL;
882        pb = &(*pb)->next)
883     ;
884   *pb = b;
885
886   info->current_block = b;
887
888   return true;
889 }
890
891 /* Finish a block in a function.  This matches the call to
892    debug_start_block.  The argument is the address at which this block
893    ends.  */
894
895 boolean
896 debug_end_block (handle, addr)
897      PTR handle;
898      bfd_vma addr;
899 {
900   struct debug_handle *info = (struct debug_handle *) handle;
901   struct debug_block *parent;
902
903   if (info->current_unit == NULL
904       || info->current_block == NULL)
905     {
906       debug_error ("debug_end_block: no current block");
907       return false;
908     }
909
910   parent = info->current_block->parent;
911   if (parent == NULL)
912     {
913       debug_error ("debug_end_block: attempt to close top level block");
914       return false;
915     }
916
917   info->current_block->end = addr;
918
919   info->current_block = parent;
920
921   return true;
922 }
923
924 /* Associate a line number in the current source file and function
925    with a given address.  */
926
927 boolean
928 debug_record_line (handle, lineno, addr)
929      PTR handle;
930      unsigned long lineno;
931      bfd_vma addr;
932 {
933   struct debug_handle *info = (struct debug_handle *) handle;
934   struct debug_lineno *l;
935   unsigned int i;
936
937   if (info->current_unit == NULL)
938     {
939       debug_error ("debug_record_line: no current unit");
940       return false;
941     }
942
943   l = info->current_lineno;
944   if (l != NULL && l->file == info->current_file)
945     {
946       for (i = 0; i < DEBUG_LINENO_COUNT; i++)
947         {
948           if (l->linenos[i] == (unsigned long) -1)
949             {
950               l->linenos[i] = lineno;
951               l->addrs[i] = addr;
952               return true;
953             }
954         }
955     }
956
957   /* If we get here, then either 1) there is no current_lineno
958      structure, which means this is the first line number in this
959      compilation unit, 2) the current_lineno structure is for a
960      different file, or 3) the current_lineno structure is full.
961      Regardless, we want to allocate a new debug_lineno structure, put
962      it in the right place, and make it the new current_lineno
963      structure.  */
964
965   l = (struct debug_lineno *) xmalloc (sizeof *l);
966   memset (l, 0, sizeof *l);
967
968   l->file = info->current_file;
969   l->linenos[0] = lineno;
970   l->addrs[0] = addr;
971   for (i = 1; i < DEBUG_LINENO_COUNT; i++)
972     l->linenos[i] = (unsigned long) -1;
973
974   if (info->current_lineno != NULL)
975     info->current_lineno->next = l;
976   else
977     info->current_unit->linenos = l;
978
979   info->current_lineno = l;
980
981   return true;
982 }
983
984 /* Start a named common block.  This is a block of variables that may
985    move in memory.  */
986
987 boolean
988 debug_start_common_block (handle, name)
989      PTR handle;
990      const char *name;
991 {
992   /* FIXME */
993   debug_error ("debug_start_common_block: not implemented");
994   return false;
995 }
996
997 /* End a named common block.  */
998
999 boolean
1000 debug_end_common_block (handle, name)
1001      PTR handle;
1002      const char *name;
1003 {
1004   /* FIXME */
1005   debug_error ("debug_end_common_block: not implemented");
1006   return false;
1007 }
1008
1009 /* Record a named integer constant.  */
1010
1011 boolean
1012 debug_record_int_const (handle, name, val)
1013      PTR handle;
1014      const char *name;
1015      bfd_vma val;
1016 {
1017   struct debug_handle *info = (struct debug_handle *) handle;
1018   struct debug_name *n;
1019
1020   if (name == NULL)
1021     return false;
1022
1023   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1024                                       DEBUG_LINKAGE_NONE);
1025   if (n == NULL)
1026     return false;
1027
1028   n->u.int_constant = val;
1029
1030   return true;
1031 }
1032
1033 /* Record a named floating point constant.  */
1034
1035 boolean
1036 debug_record_float_const (handle, name, val)
1037      PTR handle;
1038      const char *name;
1039      double val;
1040 {
1041   struct debug_handle *info = (struct debug_handle *) handle;
1042   struct debug_name *n;
1043
1044   if (name == NULL)
1045     return false;
1046
1047   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1048                                       DEBUG_LINKAGE_NONE);
1049   if (n == NULL)
1050     return false;
1051
1052   n->u.float_constant = val;
1053
1054   return true;
1055 }
1056
1057 /* Record a typed constant with an integral value.  */
1058
1059 boolean
1060 debug_record_typed_const (handle, name, type, val)
1061      PTR handle;
1062      const char *name;
1063      debug_type type;
1064      bfd_vma val;
1065 {
1066   struct debug_handle *info = (struct debug_handle *) handle;
1067   struct debug_name *n;
1068   struct debug_typed_constant *tc;
1069
1070   if (name == NULL || type == NULL)
1071     return false;
1072
1073   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1074                                       DEBUG_LINKAGE_NONE);
1075   if (n == NULL)
1076     return false;
1077
1078   tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
1079   memset (tc, 0, sizeof *tc);
1080
1081   tc->type = type;
1082   tc->val = val;
1083
1084   n->u.typed_constant = tc;
1085
1086   return true;
1087 }
1088
1089 /* Record a label.  */
1090
1091 boolean
1092 debug_record_label (handle, name, type, addr)
1093      PTR handle;
1094      const char *name;
1095      debug_type type;
1096      bfd_vma addr;
1097 {
1098   /* FIXME.  */
1099   debug_error ("debug_record_label not implemented");
1100   return false;
1101 }
1102
1103 /* Record a variable.  */
1104
1105 boolean
1106 debug_record_variable (handle, name, type, kind, val)
1107      PTR handle;
1108      const char *name;
1109      debug_type type;
1110      enum debug_var_kind kind;
1111      bfd_vma val;
1112 {
1113   struct debug_handle *info = (struct debug_handle *) handle;
1114   struct debug_namespace **nsp;
1115   enum debug_object_linkage linkage;
1116   struct debug_name *n;
1117   struct debug_variable *v;
1118
1119   if (name == NULL || type == NULL)
1120     return false;
1121
1122   if (info->current_unit == NULL
1123       || info->current_file == NULL)
1124     {
1125       debug_error ("debug_record_variable: no current file");
1126       return false;
1127     }
1128
1129   if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1130     {
1131       nsp = &info->current_file->globals;
1132       if (kind == DEBUG_GLOBAL)
1133         linkage = DEBUG_LINKAGE_GLOBAL;
1134       else
1135         linkage = DEBUG_LINKAGE_STATIC;
1136     }
1137   else
1138     {
1139       if (info->current_block == NULL)
1140         {
1141           debug_error ("debug_record_variable: no current block");
1142           return false;
1143         }
1144       nsp = &info->current_block->locals;
1145       linkage = DEBUG_LINKAGE_AUTOMATIC;
1146     }
1147
1148   n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1149   if (n == NULL)
1150     return false;
1151
1152   v = (struct debug_variable *) xmalloc (sizeof *v);
1153   memset (v, 0, sizeof *v);
1154
1155   v->kind = kind;
1156   v->type = type;
1157   v->val = val;
1158
1159   n->u.variable = v;
1160
1161   return true;  
1162 }
1163
1164 /* Make a type with a given kind and size.  */
1165
1166 /*ARGSUSED*/
1167 static struct debug_type *
1168 debug_make_type (info, kind, size)
1169      struct debug_handle *info;
1170      enum debug_type_kind kind;
1171      unsigned int size;
1172 {
1173   struct debug_type *t;
1174
1175   t = (struct debug_type *) xmalloc (sizeof *t);
1176   memset (t, 0, sizeof *t);
1177
1178   t->kind = kind;
1179   t->size = size;
1180
1181   return t;
1182 }
1183
1184 /* Make an indirect type which may be used as a placeholder for a type
1185    which is referenced before it is defined.  */
1186
1187 debug_type
1188 debug_make_indirect_type (handle, slot, tag)
1189      PTR handle;
1190      debug_type *slot;
1191      const char *tag;
1192 {
1193   struct debug_handle *info = (struct debug_handle *) handle;
1194   struct debug_type *t;
1195   struct debug_indirect_type *i;
1196
1197   t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1198   if (t == NULL)
1199     return DEBUG_TYPE_NULL;
1200
1201   i = (struct debug_indirect_type *) xmalloc (sizeof *i);
1202   memset (i, 0, sizeof *i);
1203
1204   i->slot = slot;
1205   i->tag = tag;
1206
1207   t->u.kindirect = i;
1208
1209   return t;
1210 }
1211
1212 /* Make a void type.  There is only one of these.  */
1213
1214 debug_type
1215 debug_make_void_type (handle)
1216      PTR handle;
1217 {
1218   struct debug_handle *info = (struct debug_handle *) handle;
1219
1220   return debug_make_type (info, DEBUG_KIND_VOID, 0);
1221 }
1222
1223 /* Make an integer type of a given size.  The boolean argument is true
1224    if the integer is unsigned.  */
1225
1226 debug_type
1227 debug_make_int_type (handle, size, unsignedp)
1228      PTR handle;
1229      unsigned int size;
1230      boolean unsignedp;
1231 {
1232   struct debug_handle *info = (struct debug_handle *) handle;
1233   struct debug_type *t;
1234
1235   t = debug_make_type (info, DEBUG_KIND_INT, size);
1236   if (t == NULL)
1237     return DEBUG_TYPE_NULL;
1238
1239   t->u.kint = unsignedp;
1240
1241   return t;
1242 }
1243
1244 /* Make a floating point type of a given size.  FIXME: On some
1245    platforms, like an Alpha, you probably need to be able to specify
1246    the format.  */
1247
1248 debug_type
1249 debug_make_float_type (handle, size)
1250      PTR handle;
1251      unsigned int size;
1252 {
1253   struct debug_handle *info = (struct debug_handle *) handle;
1254
1255   return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1256 }
1257
1258 /* Make a boolean type of a given size.  */
1259
1260 debug_type
1261 debug_make_bool_type (handle, size)
1262      PTR handle;
1263      unsigned int size;
1264 {
1265   struct debug_handle *info = (struct debug_handle *) handle;
1266
1267   return debug_make_type (info, DEBUG_KIND_BOOL, size);
1268 }
1269
1270 /* Make a complex type of a given size.  */
1271
1272 debug_type
1273 debug_make_complex_type (handle, size)
1274      PTR handle;
1275      unsigned int size;
1276 {
1277   struct debug_handle *info = (struct debug_handle *) handle;
1278
1279   return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1280 }
1281
1282 /* Make a structure type.  The second argument is true for a struct,
1283    false for a union.  The third argument is the size of the struct.
1284    The fourth argument is a NULL terminated array of fields.  */
1285
1286 debug_type
1287 debug_make_struct_type (handle, structp, size, fields)
1288      PTR handle;
1289      boolean structp;
1290      bfd_vma size;
1291      debug_field *fields;
1292 {
1293   struct debug_handle *info = (struct debug_handle *) handle;
1294   struct debug_type *t;
1295   struct debug_class_type *c;
1296
1297   t = debug_make_type (info,
1298                        structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1299                        size);
1300   if (t == NULL)
1301     return DEBUG_TYPE_NULL;
1302
1303   c = (struct debug_class_type *) xmalloc (sizeof *c);
1304   memset (c, 0, sizeof *c);
1305
1306   c->fields = fields;
1307
1308   t->u.kclass = c;
1309
1310   return t;
1311 }
1312
1313 /* Make an object type.  The first three arguments after the handle
1314    are the same as for debug_make_struct_type.  The next arguments are
1315    a NULL terminated array of base classes, a NULL terminated array of
1316    methods, the type of the object holding the virtual function table
1317    if it is not this object, and a boolean which is true if this
1318    object has its own virtual function table.  */
1319
1320 debug_type
1321 debug_make_object_type (handle, structp, size, fields, baseclasses,
1322                         methods, vptrbase, ownvptr)
1323      PTR handle;
1324      boolean structp;
1325      bfd_vma size;
1326      debug_field *fields;
1327      debug_baseclass *baseclasses;
1328      debug_method *methods;
1329      debug_type vptrbase;
1330      boolean ownvptr;
1331 {
1332   struct debug_handle *info = (struct debug_handle *) handle;
1333   struct debug_type *t;
1334   struct debug_class_type *c;
1335
1336   t = debug_make_type (info,
1337                        structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1338                        size);
1339   if (t == NULL)
1340     return DEBUG_TYPE_NULL;
1341
1342   c = (struct debug_class_type *) xmalloc (sizeof *c);
1343   memset (c, 0, sizeof *c);
1344
1345   c->fields = fields;
1346   c->baseclasses = baseclasses;
1347   c->methods = methods;
1348   if (ownvptr)
1349     c->vptrbase = t;
1350   else
1351     c->vptrbase = vptrbase;
1352
1353   t->u.kclass = c;
1354
1355   return t;
1356 }
1357
1358 /* Make an enumeration type.  The arguments are a null terminated
1359    array of strings, and an array of corresponding values.  */
1360
1361 debug_type
1362 debug_make_enum_type (handle, names, values)
1363      PTR handle;
1364      const char **names;
1365      bfd_signed_vma *values;
1366 {
1367   struct debug_handle *info = (struct debug_handle *) handle;
1368   struct debug_type *t;
1369   struct debug_enum_type *e;
1370
1371   t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1372   if (t == NULL)
1373     return DEBUG_TYPE_NULL;
1374
1375   e = (struct debug_enum_type *) xmalloc (sizeof *e);
1376   memset (e, 0, sizeof *e);
1377
1378   e->names = names;
1379   e->values = values;
1380
1381   t->u.kenum = e;
1382
1383   return t;
1384 }
1385
1386 /* Make a pointer to a given type.  */
1387
1388 debug_type
1389 debug_make_pointer_type (handle, type)
1390      PTR handle;
1391      debug_type type;
1392 {
1393   struct debug_handle *info = (struct debug_handle *) handle;
1394   struct debug_type *t;
1395
1396   if (type == NULL)
1397     return DEBUG_TYPE_NULL;
1398
1399   if (type->pointer != DEBUG_TYPE_NULL)
1400     return type->pointer;
1401
1402   t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1403   if (t == NULL)
1404     return DEBUG_TYPE_NULL;
1405
1406   t->u.kpointer = type;
1407
1408   type->pointer = t;
1409
1410   return t;
1411 }
1412
1413 /* Make a function returning a given type.  FIXME: We should be able
1414    to record the parameter types.  */
1415
1416 debug_type
1417 debug_make_function_type (handle, type, arg_types, varargs)
1418      PTR handle;
1419      debug_type type;
1420      debug_type *arg_types;
1421      boolean varargs;
1422 {
1423   struct debug_handle *info = (struct debug_handle *) handle;
1424   struct debug_type *t;
1425   struct debug_function_type *f;
1426
1427   if (type == NULL)
1428     return DEBUG_TYPE_NULL;
1429
1430   t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1431   if (t == NULL)
1432     return DEBUG_TYPE_NULL;
1433
1434   f = (struct debug_function_type *) xmalloc (sizeof *f);
1435   memset (f, 0, sizeof *f);
1436
1437   f->return_type = type;
1438   f->arg_types = arg_types;
1439   f->varargs = varargs;
1440
1441   t->u.kfunction = f;
1442
1443   return t;
1444 }
1445
1446 /* Make a reference to a given type.  */
1447
1448 debug_type
1449 debug_make_reference_type (handle, type)
1450      PTR handle;
1451      debug_type type;
1452 {
1453   struct debug_handle *info = (struct debug_handle *) handle;
1454   struct debug_type *t;
1455
1456   if (type == NULL)
1457     return DEBUG_TYPE_NULL;
1458
1459   t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1460   if (t == NULL)
1461     return DEBUG_TYPE_NULL;
1462
1463   t->u.kreference = type;
1464
1465   return t;
1466 }
1467
1468 /* Make a range of a given type from a lower to an upper bound.  */
1469
1470 debug_type
1471 debug_make_range_type (handle, type, lower, upper)
1472      PTR handle;
1473      debug_type type;
1474      bfd_signed_vma lower;
1475      bfd_signed_vma upper;
1476 {
1477   struct debug_handle *info = (struct debug_handle *) handle;
1478   struct debug_type *t;
1479   struct debug_range_type *r;
1480
1481   if (type == NULL)
1482     return DEBUG_TYPE_NULL;
1483
1484   t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1485   if (t == NULL)
1486     return DEBUG_TYPE_NULL;
1487
1488   r = (struct debug_range_type *) xmalloc (sizeof *r);
1489   memset (r, 0, sizeof *r);
1490
1491   r->type = type;
1492   r->lower = lower;
1493   r->upper = upper;
1494
1495   t->u.krange = r;
1496
1497   return t;
1498 }
1499
1500 /* Make an array type.  The second argument is the type of an element
1501    of the array.  The third argument is the type of a range of the
1502    array.  The fourth and fifth argument are the lower and upper
1503    bounds, respectively.  The sixth argument is true if this array is
1504    actually a string, as in C.  */
1505
1506 debug_type
1507 debug_make_array_type (handle, element_type, range_type, lower, upper,
1508                        stringp)
1509      PTR handle;
1510      debug_type element_type;
1511      debug_type range_type;
1512      bfd_signed_vma lower;
1513      bfd_signed_vma upper;
1514      boolean stringp;
1515 {
1516   struct debug_handle *info = (struct debug_handle *) handle;
1517   struct debug_type *t;
1518   struct debug_array_type *a;
1519
1520   if (element_type == NULL || range_type == NULL)
1521     return DEBUG_TYPE_NULL;
1522
1523   t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1524   if (t == NULL)
1525     return DEBUG_TYPE_NULL;
1526
1527   a = (struct debug_array_type *) xmalloc (sizeof *a);
1528   memset (a, 0, sizeof *a);
1529
1530   a->element_type = element_type;
1531   a->range_type = range_type;
1532   a->lower = lower;
1533   a->upper = upper;
1534   a->stringp = stringp;
1535
1536   t->u.karray = a;
1537
1538   return t;
1539 }
1540
1541 /* Make a set of a given type.  For example, a Pascal set type.  The
1542    boolean argument is true if this set is actually a bitstring, as in
1543    CHILL.  */
1544
1545 debug_type
1546 debug_make_set_type (handle, type, bitstringp)
1547      PTR handle;
1548      debug_type type;
1549      boolean bitstringp;
1550 {
1551   struct debug_handle *info = (struct debug_handle *) handle;
1552   struct debug_type *t;
1553   struct debug_set_type *s;
1554
1555   if (type == NULL)
1556     return DEBUG_TYPE_NULL;
1557
1558   t = debug_make_type (info, DEBUG_KIND_SET, 0);
1559   if (t == NULL)
1560     return DEBUG_TYPE_NULL;
1561
1562   s = (struct debug_set_type *) xmalloc (sizeof *s);
1563   memset (s, 0, sizeof *s);
1564
1565   s->type = type;
1566   s->bitstringp = bitstringp;
1567
1568   t->u.kset = s;
1569
1570   return t;
1571 }
1572
1573 /* Make a type for a pointer which is relative to an object.  The
1574    second argument is the type of the object to which the pointer is
1575    relative.  The third argument is the type that the pointer points
1576    to.  */
1577
1578 debug_type
1579 debug_make_offset_type (handle, base_type, target_type)
1580      PTR handle;
1581      debug_type base_type;
1582      debug_type target_type;
1583 {
1584   struct debug_handle *info = (struct debug_handle *) handle;
1585   struct debug_type *t;
1586   struct debug_offset_type *o;
1587
1588   if (base_type == NULL || target_type == NULL)
1589     return DEBUG_TYPE_NULL;
1590
1591   t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1592   if (t == NULL)
1593     return DEBUG_TYPE_NULL;
1594
1595   o = (struct debug_offset_type *) xmalloc (sizeof *o);
1596   memset (o, 0, sizeof *o);
1597
1598   o->base_type = base_type;
1599   o->target_type = target_type;
1600
1601   t->u.koffset = o;
1602
1603   return t;
1604 }
1605
1606 /* Make a type for a method function.  The second argument is the
1607    return type, the third argument is the domain, and the fourth
1608    argument is a NULL terminated array of argument types.  */
1609
1610 debug_type
1611 debug_make_method_type (handle, return_type, domain_type, arg_types, varargs)
1612      PTR handle;
1613      debug_type return_type;
1614      debug_type domain_type;
1615      debug_type *arg_types;
1616      boolean varargs;
1617 {
1618   struct debug_handle *info = (struct debug_handle *) handle;
1619   struct debug_type *t;
1620   struct debug_method_type *m;
1621
1622   if (return_type == NULL)
1623     return DEBUG_TYPE_NULL;
1624
1625   t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1626   if (t == NULL)
1627     return DEBUG_TYPE_NULL;
1628
1629   m = (struct debug_method_type *) xmalloc (sizeof *m);
1630   memset (m, 0, sizeof *m);
1631
1632   m->return_type = return_type;
1633   m->domain_type = domain_type;
1634   m->arg_types = arg_types;
1635   m->varargs = varargs;
1636
1637   t->u.kmethod = m;
1638
1639   return t;
1640 }
1641
1642 /* Make a const qualified version of a given type.  */
1643
1644 debug_type
1645 debug_make_const_type (handle, type)
1646      PTR handle;
1647      debug_type type;
1648 {
1649   struct debug_handle *info = (struct debug_handle *) handle;
1650   struct debug_type *t;
1651
1652   if (type == NULL)
1653     return DEBUG_TYPE_NULL;
1654
1655   t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1656   if (t == NULL)
1657     return DEBUG_TYPE_NULL;
1658
1659   t->u.kconst = type;
1660
1661   return t;
1662 }
1663
1664 /* Make a volatile qualified version of a given type.  */
1665
1666 debug_type
1667 debug_make_volatile_type (handle, type)
1668      PTR handle;
1669      debug_type type;
1670 {
1671   struct debug_handle *info = (struct debug_handle *) handle;
1672   struct debug_type *t;
1673
1674   if (type == NULL)
1675     return DEBUG_TYPE_NULL;
1676
1677   t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1678   if (t == NULL)
1679     return DEBUG_TYPE_NULL;
1680
1681   t->u.kvolatile = type;
1682
1683   return t;
1684 }
1685
1686 /* Make an undefined tagged type.  For example, a struct which has
1687    been mentioned, but not defined.  */
1688
1689 debug_type
1690 debug_make_undefined_tagged_type (handle, name, kind)
1691      PTR handle;
1692      const char *name;
1693      enum debug_type_kind kind;
1694 {
1695   struct debug_handle *info = (struct debug_handle *) handle;
1696   struct debug_type *t;
1697
1698   if (name == NULL)
1699     return DEBUG_TYPE_NULL;
1700
1701   switch (kind)
1702     {
1703     case DEBUG_KIND_STRUCT:
1704     case DEBUG_KIND_UNION:
1705     case DEBUG_KIND_CLASS:
1706     case DEBUG_KIND_UNION_CLASS:
1707     case DEBUG_KIND_ENUM:
1708       break;
1709
1710     default:
1711       debug_error ("debug_make_undefined_type: unsupported kind");
1712       return DEBUG_TYPE_NULL;
1713     }
1714
1715   t = debug_make_type (info, kind, 0);
1716   if (t == NULL)
1717     return DEBUG_TYPE_NULL;
1718
1719   return debug_tag_type (handle, name, t);
1720 }
1721
1722 /* Make a base class for an object.  The second argument is the base
1723    class type.  The third argument is the bit position of this base
1724    class in the object (always 0 unless doing multiple inheritance).
1725    The fourth argument is whether this is a virtual class.  The fifth
1726    argument is the visibility of the base class.  */
1727
1728 /*ARGSUSED*/
1729 debug_baseclass
1730 debug_make_baseclass (handle, type, bitpos, virtual, visibility)
1731      PTR handle;
1732      debug_type type;
1733      bfd_vma bitpos;
1734      boolean virtual;
1735      enum debug_visibility visibility;
1736 {     
1737   struct debug_baseclass *b;
1738
1739   b = (struct debug_baseclass *) xmalloc (sizeof *b);
1740   memset (b, 0, sizeof *b);
1741
1742   b->type = type;
1743   b->bitpos = bitpos;
1744   b->virtual = virtual;
1745   b->visibility = visibility;
1746
1747   return b;
1748 }
1749
1750 /* Make a field for a struct.  The second argument is the name.  The
1751    third argument is the type of the field.  The fourth argument is
1752    the bit position of the field.  The fifth argument is the size of
1753    the field (it may be zero).  The sixth argument is the visibility
1754    of the field.  */
1755
1756 /*ARGSUSED*/
1757 debug_field
1758 debug_make_field (handle, name, type, bitpos, bitsize, visibility)
1759      PTR handle;
1760      const char *name;
1761      debug_type type;
1762      bfd_vma bitpos;
1763      bfd_vma bitsize;
1764      enum debug_visibility visibility;
1765 {
1766   struct debug_field *f;
1767
1768   f = (struct debug_field *) xmalloc (sizeof *f);
1769   memset (f, 0, sizeof *f);
1770
1771   f->name = name;
1772   f->type = type;
1773   f->static_member = false;
1774   f->u.f.bitpos = bitpos;
1775   f->u.f.bitsize = bitsize;
1776   f->visibility = visibility;
1777
1778   return f;
1779 }
1780
1781 /* Make a static member of an object.  The second argument is the
1782    name.  The third argument is the type of the member.  The fourth
1783    argument is the physical name of the member (i.e., the name as a
1784    global variable).  The fifth argument is the visibility of the
1785    member.  */
1786
1787 /*ARGSUSED*/
1788 debug_field
1789 debug_make_static_member (handle, name, type, physname, visibility)
1790      PTR handle;
1791      const char *name;
1792      debug_type type;
1793      const char *physname;
1794      enum debug_visibility visibility;
1795 {
1796   struct debug_field *f;
1797
1798   f = (struct debug_field *) xmalloc (sizeof *f);
1799   memset (f, 0, sizeof *f);
1800
1801   f->name = name;
1802   f->type = type;
1803   f->static_member = true;
1804   f->u.s.physname = physname;
1805   f->visibility = visibility;
1806
1807   return f;
1808 }
1809
1810 /* Make a method.  The second argument is the name, and the third
1811    argument is a NULL terminated array of method variants.  */
1812
1813 /*ARGSUSED*/
1814 debug_method
1815 debug_make_method (handle, name, variants)
1816      PTR handle;
1817      const char *name;
1818      debug_method_variant *variants;
1819 {
1820   struct debug_method *m;
1821
1822   m = (struct debug_method *) xmalloc (sizeof *m);
1823   memset (m, 0, sizeof *m);
1824
1825   m->name = name;
1826   m->variants = variants;
1827
1828   return m;
1829 }
1830
1831 /* Make a method argument.  The second argument is the real name of
1832    the function.  The third argument is the type of the function.  The
1833    fourth argument is the visibility.  The fifth argument is whether
1834    this is a const function.  The sixth argument is whether this is a
1835    volatile function.  The seventh argument is the offset in the
1836    virtual function table, if any.  The eighth argument is the virtual
1837    function context.  FIXME: Are the const and volatile arguments
1838    necessary?  Could we just use debug_make_const_type?  */
1839
1840 /*ARGSUSED*/
1841 debug_method_variant
1842 debug_make_method_variant (handle, physname, type, visibility, constp,
1843                            volatilep, voffset, context)
1844      PTR handle;
1845      const char *physname;
1846      debug_type type;
1847      enum debug_visibility visibility;
1848      boolean constp;
1849      boolean volatilep;
1850      bfd_vma voffset;
1851      debug_type context;
1852 {
1853   struct debug_method_variant *m;
1854
1855   m = (struct debug_method_variant *) xmalloc (sizeof *m);
1856   memset (m, 0, sizeof *m);
1857
1858   m->physname = physname;
1859   m->type = type;
1860   m->visibility = visibility;
1861   m->constp = constp;
1862   m->volatilep = volatilep;
1863   m->voffset = voffset;
1864   m->context = context;
1865
1866   return m;
1867 }
1868
1869 /* Make a static method argument.  The arguments are the same as for
1870    debug_make_method_variant, except that the last two are omitted
1871    since a static method can not also be virtual.  */
1872
1873 debug_method_variant
1874 debug_make_static_method_variant (handle, physname, type, visibility,
1875                                   constp, volatilep)
1876      PTR handle;
1877      const char *physname;
1878      debug_type type;
1879      enum debug_visibility visibility;
1880      boolean constp;
1881      boolean volatilep;
1882 {
1883   struct debug_method_variant *m;
1884
1885   m = (struct debug_method_variant *) xmalloc (sizeof *m);
1886   memset (m, 0, sizeof *m);
1887
1888   m->physname = physname;
1889   m->type = type;
1890   m->visibility = visibility;
1891   m->constp = constp;
1892   m->volatilep = volatilep;
1893   m->voffset = VOFFSET_STATIC_METHOD;
1894
1895   return m;
1896 }
1897
1898 /* Name a type.  */
1899
1900 debug_type
1901 debug_name_type (handle, name, type)
1902      PTR handle;
1903      const char *name;
1904      debug_type type;
1905 {
1906   struct debug_handle *info = (struct debug_handle *) handle;
1907   struct debug_type *t;
1908   struct debug_named_type *n;
1909   struct debug_name *nm;
1910
1911   if (name == NULL || type == NULL)
1912     return DEBUG_TYPE_NULL;
1913
1914   if (info->current_unit == NULL
1915       || info->current_file == NULL)
1916     {
1917       debug_error ("debug_record_variable: no current file");
1918       return false;
1919     }
1920
1921   t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1922   if (t == NULL)
1923     return DEBUG_TYPE_NULL;
1924
1925   n = (struct debug_named_type *) xmalloc (sizeof *n);
1926   memset (n, 0, sizeof *n);
1927
1928   n->type = type;
1929
1930   t->u.knamed = n;
1931
1932   /* We always add the name to the global namespace.  This is probably
1933      wrong in some cases, but it seems to be right for stabs.  FIXME.  */
1934
1935   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1936                                DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1937   if (nm == NULL)
1938     return false;
1939
1940   nm->u.type = t;
1941
1942   n->name = nm;
1943
1944   return t;
1945 }
1946
1947 /* Tag a type.  */
1948
1949 debug_type
1950 debug_tag_type (handle, name, type)
1951      PTR handle;
1952      const char *name;
1953      debug_type type;
1954 {
1955   struct debug_handle *info = (struct debug_handle *) handle;
1956   struct debug_type *t;
1957   struct debug_named_type *n;
1958   struct debug_name *nm;
1959
1960   if (name == NULL || type == NULL)
1961     return DEBUG_TYPE_NULL;
1962
1963   if (info->current_file == NULL)
1964     {
1965       debug_error ("debug_tag_type: no current file");
1966       return DEBUG_TYPE_NULL;
1967     }
1968
1969   if (type->kind == DEBUG_KIND_TAGGED)
1970     {
1971       if (strcmp (type->u.knamed->name->name, name) == 0)
1972         return type;
1973       debug_error ("debug_tag_type: extra tag attempted");
1974       return DEBUG_TYPE_NULL;
1975     }
1976
1977   t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
1978   if (t == NULL)
1979     return DEBUG_TYPE_NULL;
1980
1981   n = (struct debug_named_type *) xmalloc (sizeof *n);
1982   memset (n, 0, sizeof *n);
1983
1984   n->type = type;
1985
1986   t->u.knamed = n;
1987
1988   /* We keep a global namespace of tags for each compilation unit.  I
1989      don't know if that is the right thing to do.  */
1990
1991   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1992                                DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
1993   if (nm == NULL)
1994     return false;
1995
1996   nm->u.tag = t;
1997
1998   n->name = nm;
1999
2000   return t;
2001 }
2002
2003 /* Record the size of a given type.  */
2004
2005 /*ARGSUSED*/
2006 boolean
2007 debug_record_type_size (handle, type, size)
2008      PTR handle;
2009      debug_type type;
2010      unsigned int size;
2011 {
2012   if (type->size != 0 && type->size != size)
2013     fprintf (stderr, "Warning: changing type size from %d to %d\n",
2014              type->size, size);
2015
2016   type->size = size;
2017
2018   return true;
2019 }
2020
2021 /* Find a named type.  */
2022
2023 debug_type
2024 debug_find_named_type (handle, name)
2025      PTR handle;
2026      const char *name;
2027 {
2028   struct debug_handle *info = (struct debug_handle *) handle;
2029   struct debug_block *b;
2030   struct debug_file *f;
2031
2032   /* We only search the current compilation unit.  I don't know if
2033      this is right or not.  */
2034
2035   if (info->current_unit == NULL)
2036     {
2037       debug_error ("debug_find_named_type: no current compilation unit");
2038       return DEBUG_TYPE_NULL;
2039     }
2040
2041   for (b = info->current_block; b != NULL; b = b->parent)
2042     {
2043       if (b->locals != NULL)
2044         {
2045           struct debug_name *n;
2046
2047           for (n = b->locals->list; n != NULL; n = n->next)
2048             {
2049               if (n->kind == DEBUG_OBJECT_TYPE
2050                   && n->name[0] == name[0]
2051                   && strcmp (n->name, name) == 0)
2052                 return n->u.type;
2053             }
2054         }
2055     }
2056
2057   for (f = info->current_unit->files; f != NULL; f = f->next)
2058     {
2059       if (f->globals != NULL)
2060         {
2061           struct debug_name *n;
2062
2063           for (n = f->globals->list; n != NULL; n = n->next)
2064             {
2065               if (n->kind == DEBUG_OBJECT_TYPE
2066                   && n->name[0] == name[0]
2067                   && strcmp (n->name, name) == 0)
2068                 return n->u.type;
2069             }
2070         }
2071     }
2072
2073   return DEBUG_TYPE_NULL;         
2074 }
2075
2076 /* Find a tagged type.  */
2077
2078 debug_type
2079 debug_find_tagged_type (handle, name, kind)
2080      PTR handle;
2081      const char *name;
2082      enum debug_type_kind kind;
2083 {
2084   struct debug_handle *info = (struct debug_handle *) handle;
2085   struct debug_unit *u;
2086
2087   /* We search the globals of all the compilation units.  I don't know
2088      if this is correct or not.  It would be easy to change.  */
2089
2090   for (u = info->units; u != NULL; u = u->next)
2091     {
2092       struct debug_file *f;
2093
2094       for (f = u->files; f != NULL; f = f->next)
2095         {
2096           struct debug_name *n;
2097
2098           if (f->globals != NULL)
2099             {
2100               for (n = f->globals->list; n != NULL; n = n->next)
2101                 {
2102                   if (n->kind == DEBUG_OBJECT_TAG
2103                       && (kind == DEBUG_KIND_ILLEGAL
2104                           || n->u.tag->kind == kind)
2105                       && n->name[0] == name[0]
2106                       && strcmp (n->name, name) == 0)
2107                     return n->u.tag;
2108                 }
2109             }
2110         }
2111     }
2112
2113   return DEBUG_TYPE_NULL;
2114 }
2115
2116 /* Get a base type.  */
2117
2118 static struct debug_type *
2119 debug_get_real_type (handle, type)
2120      PTR handle;
2121      debug_type type;
2122 {
2123   switch (type->kind)
2124     {
2125     default:
2126       return type;
2127     case DEBUG_KIND_INDIRECT:
2128       if (*type->u.kindirect->slot != NULL)
2129         return debug_get_real_type (handle, *type->u.kindirect->slot);
2130       return type;
2131     case DEBUG_KIND_NAMED:
2132     case DEBUG_KIND_TAGGED:
2133       return debug_get_real_type (handle, type->u.knamed->type);
2134     }
2135   /*NOTREACHED*/
2136 }
2137
2138 /* Get the kind of a type.  */
2139
2140 enum debug_type_kind
2141 debug_get_type_kind (handle, type)
2142      PTR handle;
2143      debug_type type;
2144 {
2145   if (type == NULL)
2146     return DEBUG_KIND_ILLEGAL;
2147   type = debug_get_real_type (handle, type);
2148   return type->kind;
2149 }
2150
2151 /* Get the name of a type.  */
2152
2153 const char *
2154 debug_get_type_name (handle, type)
2155      PTR handle;
2156      debug_type type;
2157 {
2158   if (type->kind == DEBUG_KIND_INDIRECT)
2159     {
2160       if (*type->u.kindirect->slot != NULL)
2161         return debug_get_type_name (handle, *type->u.kindirect->slot);
2162       return type->u.kindirect->tag;
2163     }
2164   if (type->kind == DEBUG_KIND_NAMED
2165       || type->kind == DEBUG_KIND_TAGGED)
2166     return type->u.knamed->name->name;
2167   return NULL;
2168 }
2169
2170 /* Get the size of a type.  */
2171
2172 bfd_vma
2173 debug_get_type_size (handle, type)
2174      PTR handle;
2175      debug_type type;
2176 {
2177   if (type == NULL)
2178     return 0;
2179
2180   /* We don't call debug_get_real_type, because somebody might have
2181      called debug_record_type_size on a named or indirect type.  */
2182
2183   if (type->size != 0)
2184     return type->size;
2185
2186   switch (type->kind)
2187     {
2188     default:
2189       return 0;
2190     case DEBUG_KIND_INDIRECT:
2191       if (*type->u.kindirect->slot != NULL)
2192         return debug_get_type_size (handle, *type->u.kindirect->slot);
2193       return 0;
2194     case DEBUG_KIND_NAMED:
2195     case DEBUG_KIND_TAGGED:
2196       return debug_get_type_size (handle, type->u.knamed->type);
2197     }
2198   /*NOTREACHED*/
2199 }
2200
2201 /* Get the return type of a function or method type.  */
2202
2203 debug_type
2204 debug_get_return_type (handle, type)
2205      PTR handle;
2206      debug_type type;
2207 {
2208   if (type == NULL)
2209     return DEBUG_TYPE_NULL;
2210   type = debug_get_real_type (handle, type);
2211   switch (type->kind)
2212     {
2213     default:
2214       return DEBUG_TYPE_NULL;
2215     case DEBUG_KIND_FUNCTION:
2216       return type->u.kfunction->return_type;
2217     case DEBUG_KIND_METHOD:
2218       return type->u.kmethod->return_type;
2219     }
2220   /*NOTREACHED*/      
2221 }
2222
2223 /* Get the parameter types of a function or method type (except that
2224    we don't currently store the parameter types of a function).  */
2225
2226 const debug_type *
2227 debug_get_parameter_types (handle, type, pvarargs)
2228      PTR handle;
2229      debug_type type;
2230      boolean *pvarargs;
2231 {
2232   if (type == NULL)
2233     return NULL;
2234   type = debug_get_real_type (handle, type);
2235   switch (type->kind)
2236     {
2237     default:
2238       return NULL;
2239     case DEBUG_KIND_FUNCTION:
2240       *pvarargs = type->u.kfunction->varargs;
2241       return type->u.kfunction->arg_types;
2242     case DEBUG_KIND_METHOD:
2243       *pvarargs = type->u.kmethod->varargs;
2244       return type->u.kmethod->arg_types;
2245     }
2246   /*NOTREACHED*/
2247 }
2248
2249 /* Get the target type of a type.  */
2250
2251 debug_type
2252 debug_get_target_type (handle, type)
2253      PTR handle;
2254      debug_type type;
2255 {
2256   if (type == NULL)
2257     return NULL;
2258   type = debug_get_real_type (handle, type);
2259   switch (type->kind)
2260     {
2261     default:
2262       return NULL;
2263     case DEBUG_KIND_POINTER:
2264       return type->u.kpointer;
2265     case DEBUG_KIND_REFERENCE:
2266       return type->u.kreference;
2267     case DEBUG_KIND_CONST:
2268       return type->u.kconst;
2269     case DEBUG_KIND_VOLATILE:
2270       return type->u.kvolatile;
2271     }
2272   /*NOTREACHED*/
2273 }
2274
2275 /* Get the NULL terminated array of fields for a struct, union, or
2276    class.  */
2277
2278 const debug_field *
2279 debug_get_fields (handle, type)
2280      PTR handle;
2281      debug_type type;
2282 {
2283   if (type == NULL)
2284     return NULL;
2285   type = debug_get_real_type (handle, type);
2286   switch (type->kind)
2287     {
2288     default:
2289       return NULL;
2290     case DEBUG_KIND_STRUCT:
2291     case DEBUG_KIND_UNION:
2292     case DEBUG_KIND_CLASS:
2293     case DEBUG_KIND_UNION_CLASS:
2294       return type->u.kclass->fields;
2295     }
2296   /*NOTREACHED*/
2297 }
2298
2299 /* Get the type of a field.  */
2300
2301 /*ARGSUSED*/
2302 debug_type
2303 debug_get_field_type (handle, field)
2304      PTR handle;
2305      debug_field field;
2306 {
2307   if (field == NULL)
2308     return NULL;
2309   return field->type;
2310 }
2311
2312 /* Get the name of a field.  */
2313
2314 /*ARGSUSED*/
2315 const char *
2316 debug_get_field_name (handle, field)
2317      PTR handle;
2318      debug_field field;
2319 {
2320   if (field == NULL)
2321     return NULL;
2322   return field->name;
2323 }
2324
2325 /* Get the bit position of a field.  */
2326
2327 /*ARGSUSED*/
2328 bfd_vma
2329 debug_get_field_bitpos (handle, field)
2330      PTR handle;
2331      debug_field field;
2332 {
2333   if (field == NULL || field->static_member)
2334     return (bfd_vma) -1;
2335   return field->u.f.bitpos;
2336 }
2337
2338 /* Get the bit size of a field.  */
2339
2340 /*ARGSUSED*/
2341 bfd_vma
2342 debug_get_field_bitsize (handle, field)
2343      PTR handle;
2344      debug_field field;
2345 {
2346   if (field == NULL || field->static_member)
2347     return (bfd_vma) -1;
2348   return field->u.f.bitsize;
2349 }
2350
2351 /* Get the visibility of a field.  */
2352
2353 /*ARGSUSED*/
2354 enum debug_visibility
2355 debug_get_field_visibility (handle, field)
2356      PTR handle;
2357      debug_field field;
2358 {
2359   if (field == NULL)
2360     return DEBUG_VISIBILITY_IGNORE;
2361   return field->visibility;
2362 }
2363
2364 /* Get the physical name of a field.  */
2365
2366 const char *
2367 debug_get_field_physname (handle, field)
2368      PTR handle;
2369      debug_field field;
2370 {
2371   if (field == NULL || ! field->static_member)
2372     return NULL;
2373   return field->u.s.physname;
2374 }
2375 \f
2376 /* Write out the debugging information.  This is given a handle to
2377    debugging information, and a set of function pointers to call.  */
2378
2379 boolean
2380 debug_write (handle, fns, fhandle)
2381      PTR handle;
2382      const struct debug_write_fns *fns;
2383      PTR fhandle;
2384 {
2385   struct debug_handle *info = (struct debug_handle *) handle;
2386   struct debug_unit *u;
2387
2388   /* We use a mark to tell whether we have already written out a
2389      particular name.  We use an integer, so that we don't have to
2390      clear the mark fields if we happen to write out the same
2391      information more than once.  */
2392   ++info->mark;
2393
2394   /* The base_id field holds an ID value which will never be used, so
2395      that we can tell whether we have assigned an ID during this call
2396      to debug_write.  */
2397   info->base_id = info->class_id;
2398
2399   for (u = info->units; u != NULL; u = u->next)
2400     {
2401       struct debug_file *f;
2402       boolean first_file;
2403       struct debug_lineno *l;
2404
2405       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2406         return false;
2407
2408       first_file = true;
2409       for (f = u->files; f != NULL; f = f->next)
2410         {
2411           struct debug_name *n;
2412
2413           if (first_file)
2414             first_file = false;
2415           else
2416             {
2417               if (! (*fns->start_source) (fhandle, f->filename))
2418                 return false;
2419             }
2420
2421           if (f->globals != NULL)
2422             {
2423               for (n = f->globals->list; n != NULL; n = n->next)
2424                 {
2425                   if (! debug_write_name (info, fns, fhandle, n))
2426                     return false;
2427                 }
2428             }
2429         }
2430
2431       for (l = u->linenos; l != NULL; l = l->next)
2432         {
2433           unsigned int i;
2434
2435           for (i = 0; i < DEBUG_LINENO_COUNT; i++)
2436             {
2437               if (l->linenos[i] == (unsigned long) -1)
2438                 break;
2439               if (! (*fns->lineno) (fhandle, l->file->filename, l->linenos[i],
2440                                     l->addrs[i]))
2441                 return false;
2442             }
2443         }
2444     }
2445
2446   return true;
2447 }
2448
2449 /* Write out an element in a namespace.  */
2450
2451 static boolean
2452 debug_write_name (info, fns, fhandle, n)
2453      struct debug_handle *info;
2454      const struct debug_write_fns *fns;
2455      PTR fhandle;
2456      struct debug_name *n;
2457 {
2458   /* The class_mark field is used to prevent recursively outputting a
2459      struct or class.  */
2460   ++info->class_mark;
2461
2462   switch (n->kind)
2463     {
2464     case DEBUG_OBJECT_TYPE:
2465       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2466           || ! (*fns->typdef) (fhandle, n->name))
2467         return false;
2468       return true;
2469     case DEBUG_OBJECT_TAG:
2470       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2471         return false;
2472       return (*fns->tag) (fhandle, n->name);
2473     case DEBUG_OBJECT_VARIABLE:
2474       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2475                               (struct debug_name *) NULL))
2476         return false;
2477       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2478                                n->u.variable->val);
2479     case DEBUG_OBJECT_FUNCTION:
2480       return debug_write_function (info, fns, fhandle, n->name,
2481                                    n->linkage, n->u.function);
2482     case DEBUG_OBJECT_INT_CONSTANT:
2483       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2484     case DEBUG_OBJECT_FLOAT_CONSTANT:
2485       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2486     case DEBUG_OBJECT_TYPED_CONSTANT:
2487       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2488                               (struct debug_name *) NULL))
2489         return false;
2490       return (*fns->typed_constant) (fhandle, n->name,
2491                                      n->u.typed_constant->val);
2492     default:
2493       abort ();
2494       return false;
2495     }
2496   /*NOTREACHED*/
2497 }
2498
2499 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
2500    DEBUG_KIND_TAGGED, then the name argument is the name for which we
2501    are about to call typedef or tag.  If the type is anything else,
2502    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2503    points to this one.  */
2504
2505 static boolean
2506 debug_write_type (info, fns, fhandle, type, name)
2507      struct debug_handle *info;
2508      const struct debug_write_fns *fns;
2509      PTR fhandle;
2510      struct debug_type *type;
2511      struct debug_name *name;
2512 {
2513   unsigned int i;
2514   int is;
2515   const char *tag;
2516
2517   /* If we have a name for this type, just output it.  We only output
2518      typedef names after they have been defined.  We output type tags
2519      whenever we are not actually defining them.  */
2520   if ((type->kind == DEBUG_KIND_NAMED
2521        || type->kind == DEBUG_KIND_TAGGED)
2522       && (type->u.knamed->name->mark == info->mark
2523           || (type->kind == DEBUG_KIND_TAGGED
2524               && type->u.knamed->name != name)))
2525     {
2526       if (type->kind == DEBUG_KIND_NAMED)
2527         return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2528       else
2529         {
2530           struct debug_type *real;
2531
2532           real = debug_get_real_type ((PTR) info, type);
2533           return (*fns->tag_type) (fhandle, type->u.knamed->name->name, 0,
2534                                    real->kind);
2535         }
2536     }
2537
2538   /* Mark the name after we have already looked for a known name, so
2539      that we don't just define a type in terms of itself.  We need to
2540      mark the name here so that a struct containing a pointer to
2541      itself will work.  */
2542   if (name != NULL)
2543     name->mark = info->mark;
2544
2545   tag = NULL;
2546   if (name != NULL
2547       && type->kind != DEBUG_KIND_NAMED
2548       && type->kind != DEBUG_KIND_TAGGED)
2549     {
2550       assert (name->kind == DEBUG_OBJECT_TAG);
2551       tag = name->name;
2552     }
2553
2554   switch (type->kind)
2555     {
2556     case DEBUG_KIND_ILLEGAL:
2557       debug_error ("debug_write_type: illegal type encountered");
2558       return false;
2559     case DEBUG_KIND_INDIRECT:
2560       if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2561         return (*fns->empty_type) (fhandle);
2562       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2563                                name);
2564     case DEBUG_KIND_VOID:
2565       return (*fns->void_type) (fhandle);
2566     case DEBUG_KIND_INT:
2567       return (*fns->int_type) (fhandle, type->size, type->u.kint);
2568     case DEBUG_KIND_FLOAT:
2569       return (*fns->float_type) (fhandle, type->size);
2570     case DEBUG_KIND_COMPLEX:
2571       return (*fns->complex_type) (fhandle, type->size);
2572     case DEBUG_KIND_BOOL:
2573       return (*fns->bool_type) (fhandle, type->size);
2574     case DEBUG_KIND_STRUCT:
2575     case DEBUG_KIND_UNION:
2576       if (type->u.kclass != NULL)
2577         {
2578           if (info->class_mark == type->u.kclass->mark
2579               || type->u.kclass->id > info->base_id)
2580             {
2581               /* We are currently outputting this struct, or we have
2582                  already output it.  I don't know if this can happen,
2583                  but it can happen for a class.  */
2584               assert (type->u.kclass->id > info->base_id);
2585               return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2586                                        type->kind);
2587             }
2588           type->u.kclass->mark = info->class_mark;
2589           ++info->class_id;
2590           type->u.kclass->id = info->class_id;
2591         }
2592
2593       if (! (*fns->start_struct_type) (fhandle, tag,
2594                                        (type->u.kclass != NULL
2595                                         ? type->u.kclass->id
2596                                         : 0),
2597                                        type->kind == DEBUG_KIND_STRUCT,
2598                                        type->size))
2599         return false;
2600       if (type->u.kclass != NULL
2601           && type->u.kclass->fields != NULL)
2602         {
2603           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2604             {
2605               struct debug_field *f;
2606
2607               f = type->u.kclass->fields[i];
2608               if (! debug_write_type (info, fns, fhandle, f->type,
2609                                       (struct debug_name *) NULL)
2610                   || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2611                                              f->u.f.bitsize, f->visibility))
2612                 return false;
2613             }
2614         }
2615       return (*fns->end_struct_type) (fhandle);
2616     case DEBUG_KIND_CLASS:
2617     case DEBUG_KIND_UNION_CLASS:
2618       return debug_write_class_type (info, fns, fhandle, type, tag);
2619     case DEBUG_KIND_ENUM:
2620       if (type->u.kenum == NULL)
2621         return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2622                                   (bfd_signed_vma *) NULL);
2623       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2624                                 type->u.kenum->values);
2625     case DEBUG_KIND_POINTER:
2626       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2627                               (struct debug_name *) NULL))
2628         return false;
2629       return (*fns->pointer_type) (fhandle);
2630     case DEBUG_KIND_FUNCTION:
2631       if (type->u.kfunction->arg_types == NULL)
2632         is = -1;
2633       else
2634         {
2635           for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2636             if (! debug_write_type (info, fns, fhandle,
2637                                     type->u.kfunction->arg_types[is],
2638                                     (struct debug_name *) NULL))
2639               return false;
2640         }
2641       if (! debug_write_type (info, fns, fhandle,
2642                               type->u.kfunction->return_type,
2643                               (struct debug_name *) NULL))
2644         return false;
2645       return (*fns->function_type) (fhandle, is,
2646                                     type->u.kfunction->varargs);
2647     case DEBUG_KIND_REFERENCE:
2648       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2649                               (struct debug_name *) NULL))
2650         return false;
2651       return (*fns->reference_type) (fhandle);
2652     case DEBUG_KIND_RANGE:
2653       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2654                               (struct debug_name *) NULL))
2655         return false;
2656       return (*fns->range_type) (fhandle, type->u.krange->lower,
2657                                  type->u.krange->upper);
2658     case DEBUG_KIND_ARRAY:
2659       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2660                               (struct debug_name *) NULL)
2661           || ! debug_write_type (info, fns, fhandle,
2662                                  type->u.karray->range_type,
2663                                  (struct debug_name *) NULL))
2664         return false;
2665       return (*fns->array_type) (fhandle, type->u.karray->lower,
2666                                  type->u.karray->upper,
2667                                  type->u.karray->stringp);
2668     case DEBUG_KIND_SET:
2669       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2670                               (struct debug_name *) NULL))
2671         return false;
2672       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2673     case DEBUG_KIND_OFFSET:
2674       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2675                               (struct debug_name *) NULL)
2676           || ! debug_write_type (info, fns, fhandle,
2677                                  type->u.koffset->target_type,
2678                                  (struct debug_name *) NULL))
2679         return false;
2680       return (*fns->offset_type) (fhandle);
2681     case DEBUG_KIND_METHOD:
2682       if (! debug_write_type (info, fns, fhandle,
2683                               type->u.kmethod->return_type,
2684                               (struct debug_name *) NULL))
2685         return false;
2686       if (type->u.kmethod->arg_types == NULL)
2687         is = -1;
2688       else
2689         {
2690           for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2691             if (! debug_write_type (info, fns, fhandle,
2692                                     type->u.kmethod->arg_types[is],
2693                                     (struct debug_name *) NULL))
2694               return false;
2695         }
2696       if (type->u.kmethod->domain_type != NULL)
2697         {
2698           if (! debug_write_type (info, fns, fhandle,
2699                                   type->u.kmethod->domain_type,
2700                                   (struct debug_name *) NULL))
2701             return false;
2702         }
2703       return (*fns->method_type) (fhandle,
2704                                   type->u.kmethod->domain_type != NULL,
2705                                   is,
2706                                   type->u.kmethod->varargs);
2707     case DEBUG_KIND_CONST:
2708       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2709                               (struct debug_name *) NULL))
2710         return false;
2711       return (*fns->const_type) (fhandle);
2712     case DEBUG_KIND_VOLATILE:
2713       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2714                               (struct debug_name *) NULL))
2715         return false;
2716       return (*fns->volatile_type) (fhandle);
2717     case DEBUG_KIND_NAMED:
2718       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2719                                (struct debug_name *) NULL);
2720     case DEBUG_KIND_TAGGED:
2721       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2722                                type->u.knamed->name);
2723     default:
2724       abort ();
2725       return false;
2726     }
2727 }
2728
2729 /* Write out a class type.  */
2730
2731 static boolean
2732 debug_write_class_type (info, fns, fhandle, type, tag)
2733      struct debug_handle *info;
2734      const struct debug_write_fns *fns;
2735      PTR fhandle;
2736      struct debug_type *type;
2737      const char *tag;
2738 {
2739   unsigned int i;
2740   unsigned int id;
2741   struct debug_type *vptrbase;
2742
2743   if (type->u.kclass == NULL)
2744     {
2745       id = 0;
2746       vptrbase = NULL;
2747     }
2748   else
2749     {
2750       if (info->class_mark == type->u.kclass->mark
2751           || type->u.kclass->id > info->base_id)
2752         {
2753           /* We are currently outputting this class, or we have
2754              already output it.  This can happen when there are
2755              methods for an anonymous class.  */
2756           assert (type->u.kclass->id > info->base_id);
2757           return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2758                                    type->kind);
2759         }
2760       type->u.kclass->mark = info->class_mark;
2761       ++info->class_id;
2762       id = info->class_id;
2763       type->u.kclass->id = id;
2764
2765       vptrbase = type->u.kclass->vptrbase;
2766       if (vptrbase != NULL && vptrbase != type)
2767         {
2768           if (! debug_write_type (info, fns, fhandle, vptrbase,
2769                                   (struct debug_name *) NULL))
2770             return false;
2771         }
2772     }
2773
2774   if (! (*fns->start_class_type) (fhandle, tag, id,
2775                                   type->kind == DEBUG_KIND_CLASS,
2776                                   type->size,
2777                                   vptrbase != NULL,
2778                                   vptrbase == type))
2779     return false;
2780
2781   if (type->u.kclass != NULL)
2782     {
2783       if (type->u.kclass->fields != NULL)
2784         {
2785           for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2786             {
2787               struct debug_field *f;
2788
2789               f = type->u.kclass->fields[i];
2790               if (! debug_write_type (info, fns, fhandle, f->type,
2791                                       (struct debug_name *) NULL))
2792                 return false;
2793               if (f->static_member)
2794                 {
2795                   if (! (*fns->class_static_member) (fhandle, f->name,
2796                                                      f->u.s.physname,
2797                                                      f->visibility))
2798                     return false;
2799                 }
2800               else
2801                 {
2802                   if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2803                                               f->u.f.bitsize, f->visibility))
2804                     return false;
2805                 }
2806             }
2807         }
2808
2809       if (type->u.kclass->baseclasses != NULL)
2810         {
2811           for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2812             {
2813               struct debug_baseclass *b;
2814
2815               b = type->u.kclass->baseclasses[i];
2816               if (! debug_write_type (info, fns, fhandle, b->type,
2817                                       (struct debug_name *) NULL))
2818                 return false;
2819               if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2820                                              b->visibility))
2821                 return false;
2822             }
2823         }
2824
2825       if (type->u.kclass->methods != NULL)
2826         {
2827           for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2828             {
2829               struct debug_method *m;
2830               unsigned int j;
2831
2832               m = type->u.kclass->methods[i];
2833               if (! (*fns->class_start_method) (fhandle, m->name))
2834                 return false;
2835               for (j = 0; m->variants[j] != NULL; j++)
2836                 {
2837                   struct debug_method_variant *v;
2838
2839                   v = m->variants[j];
2840                   if (v->context != NULL)
2841                     {
2842                       if (! debug_write_type (info, fns, fhandle, v->context,
2843                                               (struct debug_name *) NULL))
2844                         return false;
2845                     }
2846                   if (! debug_write_type (info, fns, fhandle, v->type,
2847                                           (struct debug_name *) NULL))
2848                     return false;
2849                   if (v->voffset != VOFFSET_STATIC_METHOD)
2850                     {
2851                       if (! (*fns->class_method_variant) (fhandle, v->physname,
2852                                                           v->visibility,
2853                                                           v->constp,
2854                                                           v->volatilep,
2855                                                           v->voffset,
2856                                                           v->context != NULL))
2857                         return false;
2858                     }
2859                   else
2860                     {
2861                       if (! (*fns->class_static_method_variant) (fhandle,
2862                                                                  v->physname,
2863                                                                  v->visibility,
2864                                                                  v->constp,
2865                                                                  v->volatilep))
2866                         return false;
2867                     }
2868                 }
2869               if (! (*fns->class_end_method) (fhandle))
2870                 return false;
2871             }
2872         }
2873     }
2874
2875   return (*fns->end_class_type) (fhandle);
2876 }
2877
2878 /* Write out information for a function.  */
2879
2880 static boolean
2881 debug_write_function (info, fns, fhandle, name, linkage, function)
2882      struct debug_handle *info;
2883      const struct debug_write_fns *fns;
2884      PTR fhandle;
2885      const char *name;
2886      enum debug_object_linkage linkage;
2887      struct debug_function *function;
2888 {
2889   struct debug_parameter *p;
2890   struct debug_block *b;
2891
2892   if (! debug_write_type (info, fns, fhandle, function->return_type,
2893                           (struct debug_name *) NULL))
2894     return false;
2895
2896   if (! (*fns->start_function) (fhandle, name,
2897                                 linkage == DEBUG_LINKAGE_GLOBAL))
2898     return false;
2899
2900   for (p = function->parameters; p != NULL; p = p->next)
2901     {
2902       if (! debug_write_type (info, fns, fhandle, p->type,
2903                               (struct debug_name *) NULL)
2904           || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2905         return false;
2906     }
2907
2908   for (b = function->blocks; b != NULL; b = b->next)
2909     {
2910       if (! debug_write_block (info, fns, fhandle, b))
2911         return false;
2912     }
2913
2914   return (*fns->end_function) (fhandle);
2915 }
2916
2917 /* Write out information for a block.  */
2918
2919 static boolean
2920 debug_write_block (info, fns, fhandle, block)
2921      struct debug_handle *info;
2922      const struct debug_write_fns *fns;
2923      PTR fhandle;
2924      struct debug_block *block;
2925 {
2926   struct debug_name *n;
2927   struct debug_block *b;
2928
2929   if (! (*fns->start_block) (fhandle, block->start))
2930     return false;
2931
2932   if (block->locals != NULL)
2933     {
2934       for (n = block->locals->list; n != NULL; n = n->next)
2935         {
2936           if (! debug_write_name (info, fns, fhandle, n))
2937             return false;
2938         }
2939     }
2940
2941   for (b = block->children; b != NULL; b = b->next)
2942     {
2943       if (! debug_write_block (info, fns, fhandle, b))
2944         return false;
2945     }
2946
2947   return (*fns->end_block) (fhandle, block->end);
2948 }