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