* COP2 testing changes.
[binutils-gdb.git] / binutils / debug.c
blobc6ce8a7c01495f5862690b725533985e86b6ab86
1 /* debug.c -- Handle generic debugging information.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
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.
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.
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. */
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. */
28 #include <stdio.h>
29 #include <assert.h>
31 #include "bfd.h"
32 #include "bucomm.h"
33 #include "libiberty.h"
34 #include "debug.h"
36 /* Global information we keep for debugging. A pointer to this
37 structure is the debugging handle passed to all the routines. */
39 struct debug_handle
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 /* A list of classes which have assigned ID's during debug_write.
60 This is linked through the next_id field of debug_class_type. */
61 struct debug_class_id *id_list;
62 /* A list used to avoid recursion during debug_type_samep. */
63 struct debug_type_compare_list *compare_list;
66 /* Information we keep for a single compilation unit. */
68 struct debug_unit
70 /* The next compilation unit. */
71 struct debug_unit *next;
72 /* A list of files included in this compilation unit. The first
73 file is always the main one, and that is where the main file name
74 is stored. */
75 struct debug_file *files;
76 /* Line number information for this compilation unit. This is not
77 stored by function, because assembler code may have line number
78 information without function information. */
79 struct debug_lineno *linenos;
82 /* Information kept for a single source file. */
84 struct debug_file
86 /* The next source file in this compilation unit. */
87 struct debug_file *next;
88 /* The name of the source file. */
89 const char *filename;
90 /* Global functions, variables, types, etc. */
91 struct debug_namespace *globals;
94 /* A type. */
96 struct debug_type
98 /* Kind of type. */
99 enum debug_type_kind kind;
100 /* Size of type (0 if not known). */
101 unsigned int size;
102 /* Type which is a pointer to this type. */
103 debug_type pointer;
104 /* Tagged union with additional information about the type. */
105 union
107 /* DEBUG_KIND_INDIRECT. */
108 struct debug_indirect_type *kindirect;
109 /* DEBUG_KIND_INT. */
110 /* Whether the integer is unsigned. */
111 boolean kint;
112 /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
113 DEBUG_KIND_UNION_CLASS. */
114 struct debug_class_type *kclass;
115 /* DEBUG_KIND_ENUM. */
116 struct debug_enum_type *kenum;
117 /* DEBUG_KIND_POINTER. */
118 struct debug_type *kpointer;
119 /* DEBUG_KIND_FUNCTION. */
120 struct debug_function_type *kfunction;
121 /* DEBUG_KIND_REFERENCE. */
122 struct debug_type *kreference;
123 /* DEBUG_KIND_RANGE. */
124 struct debug_range_type *krange;
125 /* DEBUG_KIND_ARRAY. */
126 struct debug_array_type *karray;
127 /* DEBUG_KIND_SET. */
128 struct debug_set_type *kset;
129 /* DEBUG_KIND_OFFSET. */
130 struct debug_offset_type *koffset;
131 /* DEBUG_KIND_METHOD. */
132 struct debug_method_type *kmethod;
133 /* DEBUG_KIND_CONST. */
134 struct debug_type *kconst;
135 /* DEBUG_KIND_VOLATILE. */
136 struct debug_type *kvolatile;
137 /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
138 struct debug_named_type *knamed;
139 } u;
142 /* Information kept for an indirect type. */
144 struct debug_indirect_type
146 /* Slot where the final type will appear. */
147 debug_type *slot;
148 /* Tag. */
149 const char *tag;
152 /* Information kept for a struct, union, or class. */
154 struct debug_class_type
156 /* NULL terminated array of fields. */
157 debug_field *fields;
158 /* A mark field which indicates whether the struct has already been
159 printed. */
160 unsigned int mark;
161 /* This is used to uniquely identify unnamed structs when printing. */
162 unsigned int id;
163 /* The remaining fields are only used for DEBUG_KIND_CLASS and
164 DEBUG_KIND_UNION_CLASS. */
165 /* NULL terminated array of base classes. */
166 debug_baseclass *baseclasses;
167 /* NULL terminated array of methods. */
168 debug_method *methods;
169 /* The type of the class providing the virtual function table for
170 this class. This may point to the type itself. */
171 debug_type vptrbase;
174 /* Information kept for an enum. */
176 struct debug_enum_type
178 /* NULL terminated array of names. */
179 const char **names;
180 /* Array of corresponding values. */
181 bfd_signed_vma *values;
184 /* Information kept for a function. FIXME: We should be able to
185 record the parameter types. */
187 struct debug_function_type
189 /* Return type. */
190 debug_type return_type;
191 /* NULL terminated array of argument types. */
192 debug_type *arg_types;
193 /* Whether the function takes a variable number of arguments. */
194 boolean varargs;
197 /* Information kept for a range. */
199 struct debug_range_type
201 /* Range base type. */
202 debug_type type;
203 /* Lower bound. */
204 bfd_signed_vma lower;
205 /* Upper bound. */
206 bfd_signed_vma upper;
209 /* Information kept for an array. */
211 struct debug_array_type
213 /* Element type. */
214 debug_type element_type;
215 /* Range type. */
216 debug_type range_type;
217 /* Lower bound. */
218 bfd_signed_vma lower;
219 /* Upper bound. */
220 bfd_signed_vma upper;
221 /* Whether this array is really a string. */
222 boolean stringp;
225 /* Information kept for a set. */
227 struct debug_set_type
229 /* Base type. */
230 debug_type type;
231 /* Whether this set is really a bitstring. */
232 boolean bitstringp;
235 /* Information kept for an offset type (a based pointer). */
237 struct debug_offset_type
239 /* The type the pointer is an offset from. */
240 debug_type base_type;
241 /* The type the pointer points to. */
242 debug_type target_type;
245 /* Information kept for a method type. */
247 struct debug_method_type
249 /* The return type. */
250 debug_type return_type;
251 /* The object type which this method is for. */
252 debug_type domain_type;
253 /* A NULL terminated array of argument types. */
254 debug_type *arg_types;
255 /* Whether the method takes a variable number of arguments. */
256 boolean varargs;
259 /* Information kept for a named type. */
261 struct debug_named_type
263 /* Name. */
264 struct debug_name *name;
265 /* Real type. */
266 debug_type type;
269 /* A field in a struct or union. */
271 struct debug_field
273 /* Name of the field. */
274 const char *name;
275 /* Type of the field. */
276 struct debug_type *type;
277 /* Visibility of the field. */
278 enum debug_visibility visibility;
279 /* Whether this is a static member. */
280 boolean static_member;
281 union
283 /* If static_member is false. */
284 struct
286 /* Bit position of the field in the struct. */
287 unsigned int bitpos;
288 /* Size of the field in bits. */
289 unsigned int bitsize;
290 } f;
291 /* If static_member is true. */
292 struct
294 const char *physname;
295 } s;
296 } u;
299 /* A base class for an object. */
301 struct debug_baseclass
303 /* Type of the base class. */
304 struct debug_type *type;
305 /* Bit position of the base class in the object. */
306 unsigned int bitpos;
307 /* Whether the base class is virtual. */
308 boolean virtual;
309 /* Visibility of the base class. */
310 enum debug_visibility visibility;
313 /* A method of an object. */
315 struct debug_method
317 /* The name of the method. */
318 const char *name;
319 /* A NULL terminated array of different types of variants. */
320 struct debug_method_variant **variants;
323 /* The variants of a method function of an object. These indicate
324 which method to run. */
326 struct debug_method_variant
328 /* The physical name of the function. */
329 const char *physname;
330 /* The type of the function. */
331 struct debug_type *type;
332 /* The visibility of the function. */
333 enum debug_visibility visibility;
334 /* Whether the function is const. */
335 boolean constp;
336 /* Whether the function is volatile. */
337 boolean volatilep;
338 /* The offset to the function in the virtual function table. */
339 bfd_vma voffset;
340 /* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
341 #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
342 /* Context of a virtual method function. */
343 struct debug_type *context;
346 /* A variable. This is the information we keep for a variable object.
347 This has no name; a name is associated with a variable in a
348 debug_name structure. */
350 struct debug_variable
352 /* Kind of variable. */
353 enum debug_var_kind kind;
354 /* Type. */
355 debug_type type;
356 /* Value. The interpretation of the value depends upon kind. */
357 bfd_vma val;
360 /* A function. This has no name; a name is associated with a function
361 in a debug_name structure. */
363 struct debug_function
365 /* Return type. */
366 debug_type return_type;
367 /* Parameter information. */
368 struct debug_parameter *parameters;
369 /* Block information. The first structure on the list is the main
370 block of the function, and describes function local variables. */
371 struct debug_block *blocks;
374 /* A function parameter. */
376 struct debug_parameter
378 /* Next parameter. */
379 struct debug_parameter *next;
380 /* Name. */
381 const char *name;
382 /* Type. */
383 debug_type type;
384 /* Kind. */
385 enum debug_parm_kind kind;
386 /* Value (meaning depends upon kind). */
387 bfd_vma val;
390 /* A typed constant. */
392 struct debug_typed_constant
394 /* Type. */
395 debug_type type;
396 /* Value. FIXME: We may eventually need to support non-integral
397 values. */
398 bfd_vma val;
401 /* Information about a block within a function. */
403 struct debug_block
405 /* Next block with the same parent. */
406 struct debug_block *next;
407 /* Parent block. */
408 struct debug_block *parent;
409 /* List of child blocks. */
410 struct debug_block *children;
411 /* Start address of the block. */
412 bfd_vma start;
413 /* End address of the block. */
414 bfd_vma end;
415 /* Local variables. */
416 struct debug_namespace *locals;
419 /* Line number information we keep for a compilation unit. FIXME:
420 This structure is easy to create, but can be very space
421 inefficient. */
423 struct debug_lineno
425 /* More line number information for this block. */
426 struct debug_lineno *next;
427 /* Source file. */
428 struct debug_file *file;
429 /* Line numbers, terminated by a -1 or the end of the array. */
430 #define DEBUG_LINENO_COUNT 10
431 unsigned long linenos[DEBUG_LINENO_COUNT];
432 /* Addresses for the line numbers. */
433 bfd_vma addrs[DEBUG_LINENO_COUNT];
436 /* A namespace. This is a mapping from names to objects. FIXME: This
437 should be implemented as a hash table. */
439 struct debug_namespace
441 /* List of items in this namespace. */
442 struct debug_name *list;
443 /* Pointer to where the next item in this namespace should go. */
444 struct debug_name **tail;
447 /* Kinds of objects that appear in a namespace. */
449 enum debug_object_kind
451 /* A type. */
452 DEBUG_OBJECT_TYPE,
453 /* A tagged type (really a different sort of namespace). */
454 DEBUG_OBJECT_TAG,
455 /* A variable. */
456 DEBUG_OBJECT_VARIABLE,
457 /* A function. */
458 DEBUG_OBJECT_FUNCTION,
459 /* An integer constant. */
460 DEBUG_OBJECT_INT_CONSTANT,
461 /* A floating point constant. */
462 DEBUG_OBJECT_FLOAT_CONSTANT,
463 /* A typed constant. */
464 DEBUG_OBJECT_TYPED_CONSTANT
467 /* Linkage of an object that appears in a namespace. */
469 enum debug_object_linkage
471 /* Local variable. */
472 DEBUG_LINKAGE_AUTOMATIC,
473 /* Static--either file static or function static, depending upon the
474 namespace is. */
475 DEBUG_LINKAGE_STATIC,
476 /* Global. */
477 DEBUG_LINKAGE_GLOBAL,
478 /* No linkage. */
479 DEBUG_LINKAGE_NONE
482 /* A name in a namespace. */
484 struct debug_name
486 /* Next name in this namespace. */
487 struct debug_name *next;
488 /* Name. */
489 const char *name;
490 /* Mark. This is used by debug_write. */
491 unsigned int mark;
492 /* Kind of object. */
493 enum debug_object_kind kind;
494 /* Linkage of object. */
495 enum debug_object_linkage linkage;
496 /* Tagged union with additional information about the object. */
497 union
499 /* DEBUG_OBJECT_TYPE. */
500 struct debug_type *type;
501 /* DEBUG_OBJECT_TAG. */
502 struct debug_type *tag;
503 /* DEBUG_OBJECT_VARIABLE. */
504 struct debug_variable *variable;
505 /* DEBUG_OBJECT_FUNCTION. */
506 struct debug_function *function;
507 /* DEBUG_OBJECT_INT_CONSTANT. */
508 bfd_vma int_constant;
509 /* DEBUG_OBJECT_FLOAT_CONSTANT. */
510 double float_constant;
511 /* DEBUG_OBJECT_TYPED_CONSTANT. */
512 struct debug_typed_constant *typed_constant;
513 } u;
516 /* During debug_write, a linked list of these structures is used to
517 keep track of ID numbers that have been assigned to classes. */
519 struct debug_class_id
521 /* Next ID number. */
522 struct debug_class_id *next;
523 /* The type with the ID. */
524 struct debug_type *type;
525 /* The tag; NULL if no tag. */
526 const char *tag;
529 /* During debug_type_samep, a linked list of these structures is kept
530 on the stack to avoid infinite recursion. */
532 struct debug_type_compare_list
534 /* Next type on list. */
535 struct debug_type_compare_list *next;
536 /* The types we are comparing. */
537 struct debug_type *t1;
538 struct debug_type *t2;
541 /* Local functions. */
543 static void debug_error PARAMS ((const char *));
544 static struct debug_name *debug_add_to_namespace
545 PARAMS ((struct debug_handle *, struct debug_namespace **, const char *,
546 enum debug_object_kind, enum debug_object_linkage));
547 static struct debug_name *debug_add_to_current_namespace
548 PARAMS ((struct debug_handle *, const char *, enum debug_object_kind,
549 enum debug_object_linkage));
550 static struct debug_type *debug_make_type
551 PARAMS ((struct debug_handle *, enum debug_type_kind, unsigned int));
552 static struct debug_type *debug_get_real_type PARAMS ((PTR, debug_type));
553 static boolean debug_write_name
554 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
555 struct debug_name *));
556 static boolean debug_write_type
557 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
558 struct debug_type *, struct debug_name *));
559 static boolean debug_write_class_type
560 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
561 struct debug_type *, const char *));
562 static boolean debug_write_function
563 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
564 const char *, enum debug_object_linkage, struct debug_function *));
565 static boolean debug_write_block
566 PARAMS ((struct debug_handle *, const struct debug_write_fns *, PTR,
567 struct debug_block *));
568 static boolean debug_set_class_id
569 PARAMS ((struct debug_handle *, const char *, struct debug_type *));
570 static boolean debug_type_samep
571 PARAMS ((struct debug_handle *, struct debug_type *, struct debug_type *));
572 static boolean debug_class_type_samep
573 PARAMS ((struct debug_handle *, struct debug_type *, struct debug_type *));
575 /* Issue an error message. */
577 static void
578 debug_error (message)
579 const char *message;
581 fprintf (stderr, "%s\n", message);
584 /* Add an object to a namespace. */
586 static struct debug_name *
587 debug_add_to_namespace (info, nsp, name, kind, linkage)
588 struct debug_handle *info;
589 struct debug_namespace **nsp;
590 const char *name;
591 enum debug_object_kind kind;
592 enum debug_object_linkage linkage;
594 struct debug_name *n;
595 struct debug_namespace *ns;
597 n = (struct debug_name *) xmalloc (sizeof *n);
598 memset (n, 0, sizeof *n);
600 n->name = name;
601 n->kind = kind;
602 n->linkage = linkage;
604 ns = *nsp;
605 if (ns == NULL)
607 ns = (struct debug_namespace *) xmalloc (sizeof *ns);
608 memset (ns, 0, sizeof *ns);
610 ns->tail = &ns->list;
612 *nsp = ns;
615 *ns->tail = n;
616 ns->tail = &n->next;
618 return n;
621 /* Add an object to the current namespace. */
623 static struct debug_name *
624 debug_add_to_current_namespace (info, name, kind, linkage)
625 struct debug_handle *info;
626 const char *name;
627 enum debug_object_kind kind;
628 enum debug_object_linkage linkage;
630 struct debug_namespace **nsp;
632 if (info->current_unit == NULL
633 || info->current_file == NULL)
635 debug_error ("debug_add_to_current_namespace: no current file");
636 return NULL;
639 if (info->current_block != NULL)
640 nsp = &info->current_block->locals;
641 else
642 nsp = &info->current_file->globals;
644 return debug_add_to_namespace (info, nsp, name, kind, linkage);
647 /* Return a handle for debugging information. */
650 debug_init ()
652 struct debug_handle *ret;
654 ret = (struct debug_handle *) xmalloc (sizeof *ret);
655 memset (ret, 0, sizeof *ret);
656 return (PTR) ret;
659 /* Set the source filename. This implicitly starts a new compilation
660 unit. */
662 boolean
663 debug_set_filename (handle, name)
664 PTR handle;
665 const char *name;
667 struct debug_handle *info = (struct debug_handle *) handle;
668 struct debug_file *nfile;
669 struct debug_unit *nunit;
671 if (name == NULL)
672 name = "";
674 nfile = (struct debug_file *) xmalloc (sizeof *nfile);
675 memset (nfile, 0, sizeof *nfile);
677 nfile->filename = name;
679 nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
680 memset (nunit, 0, sizeof *nunit);
682 nunit->files = nfile;
683 info->current_file = nfile;
685 if (info->current_unit != NULL)
686 info->current_unit->next = nunit;
687 else
689 assert (info->units == NULL);
690 info->units = nunit;
693 info->current_unit = nunit;
695 info->current_function = NULL;
696 info->current_block = NULL;
697 info->current_lineno = NULL;
699 return true;
702 /* Change source files to the given file name. This is used for
703 include files in a single compilation unit. */
705 boolean
706 debug_start_source (handle, name)
707 PTR handle;
708 const char *name;
710 struct debug_handle *info = (struct debug_handle *) handle;
711 struct debug_file *f, **pf;
713 if (name == NULL)
714 name = "";
716 if (info->current_unit == NULL)
718 debug_error ("debug_start_source: no debug_set_filename call");
719 return false;
722 for (f = info->current_unit->files; f != NULL; f = f->next)
724 if (f->filename[0] == name[0]
725 && f->filename[1] == name[1]
726 && strcmp (f->filename, name) == 0)
728 info->current_file = f;
729 return true;
733 f = (struct debug_file *) xmalloc (sizeof *f);
734 memset (f, 0, sizeof *f);
736 f->filename = name;
738 for (pf = &info->current_file->next;
739 *pf != NULL;
740 pf = &(*pf)->next)
742 *pf = f;
744 info->current_file = f;
746 return true;
749 /* Record a function definition. This implicitly starts a function
750 block. The debug_type argument is the type of the return value.
751 The boolean indicates whether the function is globally visible.
752 The bfd_vma is the address of the start of the function. Currently
753 the parameter types are specified by calls to
754 debug_record_parameter. FIXME: There is no way to specify nested
755 functions. */
757 boolean
758 debug_record_function (handle, name, return_type, global, addr)
759 PTR handle;
760 const char *name;
761 debug_type return_type;
762 boolean global;
763 bfd_vma addr;
765 struct debug_handle *info = (struct debug_handle *) handle;
766 struct debug_function *f;
767 struct debug_block *b;
768 struct debug_name *n;
770 if (name == NULL)
771 name = "";
772 if (return_type == NULL)
773 return false;
775 if (info->current_unit == NULL)
777 debug_error ("debug_record_function: no debug_set_filename call");
778 return false;
781 f = (struct debug_function *) xmalloc (sizeof *f);
782 memset (f, 0, sizeof *f);
784 f->return_type = return_type;
786 b = (struct debug_block *) xmalloc (sizeof *b);
787 memset (b, 0, sizeof *b);
789 b->start = addr;
790 b->end = (bfd_vma) -1;
792 f->blocks = b;
794 info->current_function = f;
795 info->current_block = b;
797 /* FIXME: If we could handle nested functions, this would be the
798 place: we would want to use a different namespace. */
799 n = debug_add_to_namespace (info,
800 &info->current_file->globals,
801 name,
802 DEBUG_OBJECT_FUNCTION,
803 (global
804 ? DEBUG_LINKAGE_GLOBAL
805 : DEBUG_LINKAGE_STATIC));
806 if (n == NULL)
807 return false;
809 n->u.function = f;
811 return true;
814 /* Record a parameter for the current function. */
816 boolean
817 debug_record_parameter (handle, name, type, kind, val)
818 PTR handle;
819 const char *name;
820 debug_type type;
821 enum debug_parm_kind kind;
822 bfd_vma val;
824 struct debug_handle *info = (struct debug_handle *) handle;
825 struct debug_parameter *p, **pp;
827 if (name == NULL || type == NULL)
828 return false;
830 if (info->current_unit == NULL
831 || info->current_function == NULL)
833 debug_error ("debug_record_parameter: no current function");
834 return false;
837 p = (struct debug_parameter *) xmalloc (sizeof *p);
838 memset (p, 0, sizeof *p);
840 p->name = name;
841 p->type = type;
842 p->kind = kind;
843 p->val = val;
845 for (pp = &info->current_function->parameters;
846 *pp != NULL;
847 pp = &(*pp)->next)
849 *pp = p;
851 return true;
854 /* End a function. FIXME: This should handle function nesting. */
856 boolean
857 debug_end_function (handle, addr)
858 PTR handle;
859 bfd_vma addr;
861 struct debug_handle *info = (struct debug_handle *) handle;
863 if (info->current_unit == NULL
864 || info->current_block == NULL
865 || info->current_function == NULL)
867 debug_error ("debug_end_function: no current function");
868 return false;
871 if (info->current_block->parent != NULL)
873 debug_error ("debug_end_function: some blocks were not closed");
874 return false;
877 info->current_block->end = addr;
879 info->current_function = NULL;
880 info->current_block = NULL;
882 return true;
885 /* Start a block in a function. All local information will be
886 recorded in this block, until the matching call to debug_end_block.
887 debug_start_block and debug_end_block may be nested. The bfd_vma
888 argument is the address at which this block starts. */
890 boolean
891 debug_start_block (handle, addr)
892 PTR handle;
893 bfd_vma addr;
895 struct debug_handle *info = (struct debug_handle *) handle;
896 struct debug_block *b, **pb;
898 /* We must always have a current block: debug_record_function sets
899 one up. */
900 if (info->current_unit == NULL
901 || info->current_block == NULL)
903 debug_error ("debug_start_block: no current block");
904 return false;
907 b = (struct debug_block *) xmalloc (sizeof *b);
908 memset (b, 0, sizeof *b);
910 b->parent = info->current_block;
911 b->start = addr;
912 b->end = (bfd_vma) -1;
914 /* This new block is a child of the current block. */
915 for (pb = &info->current_block->children;
916 *pb != NULL;
917 pb = &(*pb)->next)
919 *pb = b;
921 info->current_block = b;
923 return true;
926 /* Finish a block in a function. This matches the call to
927 debug_start_block. The argument is the address at which this block
928 ends. */
930 boolean
931 debug_end_block (handle, addr)
932 PTR handle;
933 bfd_vma addr;
935 struct debug_handle *info = (struct debug_handle *) handle;
936 struct debug_block *parent;
938 if (info->current_unit == NULL
939 || info->current_block == NULL)
941 debug_error ("debug_end_block: no current block");
942 return false;
945 parent = info->current_block->parent;
946 if (parent == NULL)
948 debug_error ("debug_end_block: attempt to close top level block");
949 return false;
952 info->current_block->end = addr;
954 info->current_block = parent;
956 return true;
959 /* Associate a line number in the current source file and function
960 with a given address. */
962 boolean
963 debug_record_line (handle, lineno, addr)
964 PTR handle;
965 unsigned long lineno;
966 bfd_vma addr;
968 struct debug_handle *info = (struct debug_handle *) handle;
969 struct debug_lineno *l;
970 unsigned int i;
972 if (info->current_unit == NULL)
974 debug_error ("debug_record_line: no current unit");
975 return false;
978 l = info->current_lineno;
979 if (l != NULL && l->file == info->current_file)
981 for (i = 0; i < DEBUG_LINENO_COUNT; i++)
983 if (l->linenos[i] == (unsigned long) -1)
985 l->linenos[i] = lineno;
986 l->addrs[i] = addr;
987 return true;
992 /* If we get here, then either 1) there is no current_lineno
993 structure, which means this is the first line number in this
994 compilation unit, 2) the current_lineno structure is for a
995 different file, or 3) the current_lineno structure is full.
996 Regardless, we want to allocate a new debug_lineno structure, put
997 it in the right place, and make it the new current_lineno
998 structure. */
1000 l = (struct debug_lineno *) xmalloc (sizeof *l);
1001 memset (l, 0, sizeof *l);
1003 l->file = info->current_file;
1004 l->linenos[0] = lineno;
1005 l->addrs[0] = addr;
1006 for (i = 1; i < DEBUG_LINENO_COUNT; i++)
1007 l->linenos[i] = (unsigned long) -1;
1009 if (info->current_lineno != NULL)
1010 info->current_lineno->next = l;
1011 else
1012 info->current_unit->linenos = l;
1014 info->current_lineno = l;
1016 return true;
1019 /* Start a named common block. This is a block of variables that may
1020 move in memory. */
1022 boolean
1023 debug_start_common_block (handle, name)
1024 PTR handle;
1025 const char *name;
1027 /* FIXME */
1028 debug_error ("debug_start_common_block: not implemented");
1029 return false;
1032 /* End a named common block. */
1034 boolean
1035 debug_end_common_block (handle, name)
1036 PTR handle;
1037 const char *name;
1039 /* FIXME */
1040 debug_error ("debug_end_common_block: not implemented");
1041 return false;
1044 /* Record a named integer constant. */
1046 boolean
1047 debug_record_int_const (handle, name, val)
1048 PTR handle;
1049 const char *name;
1050 bfd_vma val;
1052 struct debug_handle *info = (struct debug_handle *) handle;
1053 struct debug_name *n;
1055 if (name == NULL)
1056 return false;
1058 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1059 DEBUG_LINKAGE_NONE);
1060 if (n == NULL)
1061 return false;
1063 n->u.int_constant = val;
1065 return true;
1068 /* Record a named floating point constant. */
1070 boolean
1071 debug_record_float_const (handle, name, val)
1072 PTR handle;
1073 const char *name;
1074 double val;
1076 struct debug_handle *info = (struct debug_handle *) handle;
1077 struct debug_name *n;
1079 if (name == NULL)
1080 return false;
1082 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1083 DEBUG_LINKAGE_NONE);
1084 if (n == NULL)
1085 return false;
1087 n->u.float_constant = val;
1089 return true;
1092 /* Record a typed constant with an integral value. */
1094 boolean
1095 debug_record_typed_const (handle, name, type, val)
1096 PTR handle;
1097 const char *name;
1098 debug_type type;
1099 bfd_vma val;
1101 struct debug_handle *info = (struct debug_handle *) handle;
1102 struct debug_name *n;
1103 struct debug_typed_constant *tc;
1105 if (name == NULL || type == NULL)
1106 return false;
1108 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1109 DEBUG_LINKAGE_NONE);
1110 if (n == NULL)
1111 return false;
1113 tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
1114 memset (tc, 0, sizeof *tc);
1116 tc->type = type;
1117 tc->val = val;
1119 n->u.typed_constant = tc;
1121 return true;
1124 /* Record a label. */
1126 boolean
1127 debug_record_label (handle, name, type, addr)
1128 PTR handle;
1129 const char *name;
1130 debug_type type;
1131 bfd_vma addr;
1133 /* FIXME. */
1134 debug_error ("debug_record_label not implemented");
1135 return false;
1138 /* Record a variable. */
1140 boolean
1141 debug_record_variable (handle, name, type, kind, val)
1142 PTR handle;
1143 const char *name;
1144 debug_type type;
1145 enum debug_var_kind kind;
1146 bfd_vma val;
1148 struct debug_handle *info = (struct debug_handle *) handle;
1149 struct debug_namespace **nsp;
1150 enum debug_object_linkage linkage;
1151 struct debug_name *n;
1152 struct debug_variable *v;
1154 if (name == NULL || type == NULL)
1155 return false;
1157 if (info->current_unit == NULL
1158 || info->current_file == NULL)
1160 debug_error ("debug_record_variable: no current file");
1161 return false;
1164 if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1166 nsp = &info->current_file->globals;
1167 if (kind == DEBUG_GLOBAL)
1168 linkage = DEBUG_LINKAGE_GLOBAL;
1169 else
1170 linkage = DEBUG_LINKAGE_STATIC;
1172 else
1174 if (info->current_block == NULL)
1176 debug_error ("debug_record_variable: no current block");
1177 return false;
1179 nsp = &info->current_block->locals;
1180 linkage = DEBUG_LINKAGE_AUTOMATIC;
1183 n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1184 if (n == NULL)
1185 return false;
1187 v = (struct debug_variable *) xmalloc (sizeof *v);
1188 memset (v, 0, sizeof *v);
1190 v->kind = kind;
1191 v->type = type;
1192 v->val = val;
1194 n->u.variable = v;
1196 return true;
1199 /* Make a type with a given kind and size. */
1201 /*ARGSUSED*/
1202 static struct debug_type *
1203 debug_make_type (info, kind, size)
1204 struct debug_handle *info;
1205 enum debug_type_kind kind;
1206 unsigned int size;
1208 struct debug_type *t;
1210 t = (struct debug_type *) xmalloc (sizeof *t);
1211 memset (t, 0, sizeof *t);
1213 t->kind = kind;
1214 t->size = size;
1216 return t;
1219 /* Make an indirect type which may be used as a placeholder for a type
1220 which is referenced before it is defined. */
1222 debug_type
1223 debug_make_indirect_type (handle, slot, tag)
1224 PTR handle;
1225 debug_type *slot;
1226 const char *tag;
1228 struct debug_handle *info = (struct debug_handle *) handle;
1229 struct debug_type *t;
1230 struct debug_indirect_type *i;
1232 t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1233 if (t == NULL)
1234 return DEBUG_TYPE_NULL;
1236 i = (struct debug_indirect_type *) xmalloc (sizeof *i);
1237 memset (i, 0, sizeof *i);
1239 i->slot = slot;
1240 i->tag = tag;
1242 t->u.kindirect = i;
1244 return t;
1247 /* Make a void type. There is only one of these. */
1249 debug_type
1250 debug_make_void_type (handle)
1251 PTR handle;
1253 struct debug_handle *info = (struct debug_handle *) handle;
1255 return debug_make_type (info, DEBUG_KIND_VOID, 0);
1258 /* Make an integer type of a given size. The boolean argument is true
1259 if the integer is unsigned. */
1261 debug_type
1262 debug_make_int_type (handle, size, unsignedp)
1263 PTR handle;
1264 unsigned int size;
1265 boolean unsignedp;
1267 struct debug_handle *info = (struct debug_handle *) handle;
1268 struct debug_type *t;
1270 t = debug_make_type (info, DEBUG_KIND_INT, size);
1271 if (t == NULL)
1272 return DEBUG_TYPE_NULL;
1274 t->u.kint = unsignedp;
1276 return t;
1279 /* Make a floating point type of a given size. FIXME: On some
1280 platforms, like an Alpha, you probably need to be able to specify
1281 the format. */
1283 debug_type
1284 debug_make_float_type (handle, size)
1285 PTR handle;
1286 unsigned int size;
1288 struct debug_handle *info = (struct debug_handle *) handle;
1290 return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1293 /* Make a boolean type of a given size. */
1295 debug_type
1296 debug_make_bool_type (handle, size)
1297 PTR handle;
1298 unsigned int size;
1300 struct debug_handle *info = (struct debug_handle *) handle;
1302 return debug_make_type (info, DEBUG_KIND_BOOL, size);
1305 /* Make a complex type of a given size. */
1307 debug_type
1308 debug_make_complex_type (handle, size)
1309 PTR handle;
1310 unsigned int size;
1312 struct debug_handle *info = (struct debug_handle *) handle;
1314 return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1317 /* Make a structure type. The second argument is true for a struct,
1318 false for a union. The third argument is the size of the struct.
1319 The fourth argument is a NULL terminated array of fields. */
1321 debug_type
1322 debug_make_struct_type (handle, structp, size, fields)
1323 PTR handle;
1324 boolean structp;
1325 bfd_vma size;
1326 debug_field *fields;
1328 struct debug_handle *info = (struct debug_handle *) handle;
1329 struct debug_type *t;
1330 struct debug_class_type *c;
1332 t = debug_make_type (info,
1333 structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1334 size);
1335 if (t == NULL)
1336 return DEBUG_TYPE_NULL;
1338 c = (struct debug_class_type *) xmalloc (sizeof *c);
1339 memset (c, 0, sizeof *c);
1341 c->fields = fields;
1343 t->u.kclass = c;
1345 return t;
1348 /* Make an object type. The first three arguments after the handle
1349 are the same as for debug_make_struct_type. The next arguments are
1350 a NULL terminated array of base classes, a NULL terminated array of
1351 methods, the type of the object holding the virtual function table
1352 if it is not this object, and a boolean which is true if this
1353 object has its own virtual function table. */
1355 debug_type
1356 debug_make_object_type (handle, structp, size, fields, baseclasses,
1357 methods, vptrbase, ownvptr)
1358 PTR handle;
1359 boolean structp;
1360 bfd_vma size;
1361 debug_field *fields;
1362 debug_baseclass *baseclasses;
1363 debug_method *methods;
1364 debug_type vptrbase;
1365 boolean ownvptr;
1367 struct debug_handle *info = (struct debug_handle *) handle;
1368 struct debug_type *t;
1369 struct debug_class_type *c;
1371 t = debug_make_type (info,
1372 structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1373 size);
1374 if (t == NULL)
1375 return DEBUG_TYPE_NULL;
1377 c = (struct debug_class_type *) xmalloc (sizeof *c);
1378 memset (c, 0, sizeof *c);
1380 c->fields = fields;
1381 c->baseclasses = baseclasses;
1382 c->methods = methods;
1383 if (ownvptr)
1384 c->vptrbase = t;
1385 else
1386 c->vptrbase = vptrbase;
1388 t->u.kclass = c;
1390 return t;
1393 /* Make an enumeration type. The arguments are a null terminated
1394 array of strings, and an array of corresponding values. */
1396 debug_type
1397 debug_make_enum_type (handle, names, values)
1398 PTR handle;
1399 const char **names;
1400 bfd_signed_vma *values;
1402 struct debug_handle *info = (struct debug_handle *) handle;
1403 struct debug_type *t;
1404 struct debug_enum_type *e;
1406 t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1407 if (t == NULL)
1408 return DEBUG_TYPE_NULL;
1410 e = (struct debug_enum_type *) xmalloc (sizeof *e);
1411 memset (e, 0, sizeof *e);
1413 e->names = names;
1414 e->values = values;
1416 t->u.kenum = e;
1418 return t;
1421 /* Make a pointer to a given type. */
1423 debug_type
1424 debug_make_pointer_type (handle, type)
1425 PTR handle;
1426 debug_type type;
1428 struct debug_handle *info = (struct debug_handle *) handle;
1429 struct debug_type *t;
1431 if (type == NULL)
1432 return DEBUG_TYPE_NULL;
1434 if (type->pointer != DEBUG_TYPE_NULL)
1435 return type->pointer;
1437 t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1438 if (t == NULL)
1439 return DEBUG_TYPE_NULL;
1441 t->u.kpointer = type;
1443 type->pointer = t;
1445 return t;
1448 /* Make a function returning a given type. FIXME: We should be able
1449 to record the parameter types. */
1451 debug_type
1452 debug_make_function_type (handle, type, arg_types, varargs)
1453 PTR handle;
1454 debug_type type;
1455 debug_type *arg_types;
1456 boolean varargs;
1458 struct debug_handle *info = (struct debug_handle *) handle;
1459 struct debug_type *t;
1460 struct debug_function_type *f;
1462 if (type == NULL)
1463 return DEBUG_TYPE_NULL;
1465 t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1466 if (t == NULL)
1467 return DEBUG_TYPE_NULL;
1469 f = (struct debug_function_type *) xmalloc (sizeof *f);
1470 memset (f, 0, sizeof *f);
1472 f->return_type = type;
1473 f->arg_types = arg_types;
1474 f->varargs = varargs;
1476 t->u.kfunction = f;
1478 return t;
1481 /* Make a reference to a given type. */
1483 debug_type
1484 debug_make_reference_type (handle, type)
1485 PTR handle;
1486 debug_type type;
1488 struct debug_handle *info = (struct debug_handle *) handle;
1489 struct debug_type *t;
1491 if (type == NULL)
1492 return DEBUG_TYPE_NULL;
1494 t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1495 if (t == NULL)
1496 return DEBUG_TYPE_NULL;
1498 t->u.kreference = type;
1500 return t;
1503 /* Make a range of a given type from a lower to an upper bound. */
1505 debug_type
1506 debug_make_range_type (handle, type, lower, upper)
1507 PTR handle;
1508 debug_type type;
1509 bfd_signed_vma lower;
1510 bfd_signed_vma upper;
1512 struct debug_handle *info = (struct debug_handle *) handle;
1513 struct debug_type *t;
1514 struct debug_range_type *r;
1516 if (type == NULL)
1517 return DEBUG_TYPE_NULL;
1519 t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1520 if (t == NULL)
1521 return DEBUG_TYPE_NULL;
1523 r = (struct debug_range_type *) xmalloc (sizeof *r);
1524 memset (r, 0, sizeof *r);
1526 r->type = type;
1527 r->lower = lower;
1528 r->upper = upper;
1530 t->u.krange = r;
1532 return t;
1535 /* Make an array type. The second argument is the type of an element
1536 of the array. The third argument is the type of a range of the
1537 array. The fourth and fifth argument are the lower and upper
1538 bounds, respectively. The sixth argument is true if this array is
1539 actually a string, as in C. */
1541 debug_type
1542 debug_make_array_type (handle, element_type, range_type, lower, upper,
1543 stringp)
1544 PTR handle;
1545 debug_type element_type;
1546 debug_type range_type;
1547 bfd_signed_vma lower;
1548 bfd_signed_vma upper;
1549 boolean stringp;
1551 struct debug_handle *info = (struct debug_handle *) handle;
1552 struct debug_type *t;
1553 struct debug_array_type *a;
1555 if (element_type == NULL || range_type == NULL)
1556 return DEBUG_TYPE_NULL;
1558 t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1559 if (t == NULL)
1560 return DEBUG_TYPE_NULL;
1562 a = (struct debug_array_type *) xmalloc (sizeof *a);
1563 memset (a, 0, sizeof *a);
1565 a->element_type = element_type;
1566 a->range_type = range_type;
1567 a->lower = lower;
1568 a->upper = upper;
1569 a->stringp = stringp;
1571 t->u.karray = a;
1573 return t;
1576 /* Make a set of a given type. For example, a Pascal set type. The
1577 boolean argument is true if this set is actually a bitstring, as in
1578 CHILL. */
1580 debug_type
1581 debug_make_set_type (handle, type, bitstringp)
1582 PTR handle;
1583 debug_type type;
1584 boolean bitstringp;
1586 struct debug_handle *info = (struct debug_handle *) handle;
1587 struct debug_type *t;
1588 struct debug_set_type *s;
1590 if (type == NULL)
1591 return DEBUG_TYPE_NULL;
1593 t = debug_make_type (info, DEBUG_KIND_SET, 0);
1594 if (t == NULL)
1595 return DEBUG_TYPE_NULL;
1597 s = (struct debug_set_type *) xmalloc (sizeof *s);
1598 memset (s, 0, sizeof *s);
1600 s->type = type;
1601 s->bitstringp = bitstringp;
1603 t->u.kset = s;
1605 return t;
1608 /* Make a type for a pointer which is relative to an object. The
1609 second argument is the type of the object to which the pointer is
1610 relative. The third argument is the type that the pointer points
1611 to. */
1613 debug_type
1614 debug_make_offset_type (handle, base_type, target_type)
1615 PTR handle;
1616 debug_type base_type;
1617 debug_type target_type;
1619 struct debug_handle *info = (struct debug_handle *) handle;
1620 struct debug_type *t;
1621 struct debug_offset_type *o;
1623 if (base_type == NULL || target_type == NULL)
1624 return DEBUG_TYPE_NULL;
1626 t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1627 if (t == NULL)
1628 return DEBUG_TYPE_NULL;
1630 o = (struct debug_offset_type *) xmalloc (sizeof *o);
1631 memset (o, 0, sizeof *o);
1633 o->base_type = base_type;
1634 o->target_type = target_type;
1636 t->u.koffset = o;
1638 return t;
1641 /* Make a type for a method function. The second argument is the
1642 return type, the third argument is the domain, and the fourth
1643 argument is a NULL terminated array of argument types. */
1645 debug_type
1646 debug_make_method_type (handle, return_type, domain_type, arg_types, varargs)
1647 PTR handle;
1648 debug_type return_type;
1649 debug_type domain_type;
1650 debug_type *arg_types;
1651 boolean varargs;
1653 struct debug_handle *info = (struct debug_handle *) handle;
1654 struct debug_type *t;
1655 struct debug_method_type *m;
1657 if (return_type == NULL)
1658 return DEBUG_TYPE_NULL;
1660 t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1661 if (t == NULL)
1662 return DEBUG_TYPE_NULL;
1664 m = (struct debug_method_type *) xmalloc (sizeof *m);
1665 memset (m, 0, sizeof *m);
1667 m->return_type = return_type;
1668 m->domain_type = domain_type;
1669 m->arg_types = arg_types;
1670 m->varargs = varargs;
1672 t->u.kmethod = m;
1674 return t;
1677 /* Make a const qualified version of a given type. */
1679 debug_type
1680 debug_make_const_type (handle, type)
1681 PTR handle;
1682 debug_type type;
1684 struct debug_handle *info = (struct debug_handle *) handle;
1685 struct debug_type *t;
1687 if (type == NULL)
1688 return DEBUG_TYPE_NULL;
1690 t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1691 if (t == NULL)
1692 return DEBUG_TYPE_NULL;
1694 t->u.kconst = type;
1696 return t;
1699 /* Make a volatile qualified version of a given type. */
1701 debug_type
1702 debug_make_volatile_type (handle, type)
1703 PTR handle;
1704 debug_type type;
1706 struct debug_handle *info = (struct debug_handle *) handle;
1707 struct debug_type *t;
1709 if (type == NULL)
1710 return DEBUG_TYPE_NULL;
1712 t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1713 if (t == NULL)
1714 return DEBUG_TYPE_NULL;
1716 t->u.kvolatile = type;
1718 return t;
1721 /* Make an undefined tagged type. For example, a struct which has
1722 been mentioned, but not defined. */
1724 debug_type
1725 debug_make_undefined_tagged_type (handle, name, kind)
1726 PTR handle;
1727 const char *name;
1728 enum debug_type_kind kind;
1730 struct debug_handle *info = (struct debug_handle *) handle;
1731 struct debug_type *t;
1733 if (name == NULL)
1734 return DEBUG_TYPE_NULL;
1736 switch (kind)
1738 case DEBUG_KIND_STRUCT:
1739 case DEBUG_KIND_UNION:
1740 case DEBUG_KIND_CLASS:
1741 case DEBUG_KIND_UNION_CLASS:
1742 case DEBUG_KIND_ENUM:
1743 break;
1745 default:
1746 debug_error ("debug_make_undefined_type: unsupported kind");
1747 return DEBUG_TYPE_NULL;
1750 t = debug_make_type (info, kind, 0);
1751 if (t == NULL)
1752 return DEBUG_TYPE_NULL;
1754 return debug_tag_type (handle, name, t);
1757 /* Make a base class for an object. The second argument is the base
1758 class type. The third argument is the bit position of this base
1759 class in the object (always 0 unless doing multiple inheritance).
1760 The fourth argument is whether this is a virtual class. The fifth
1761 argument is the visibility of the base class. */
1763 /*ARGSUSED*/
1764 debug_baseclass
1765 debug_make_baseclass (handle, type, bitpos, virtual, visibility)
1766 PTR handle;
1767 debug_type type;
1768 bfd_vma bitpos;
1769 boolean virtual;
1770 enum debug_visibility visibility;
1772 struct debug_baseclass *b;
1774 b = (struct debug_baseclass *) xmalloc (sizeof *b);
1775 memset (b, 0, sizeof *b);
1777 b->type = type;
1778 b->bitpos = bitpos;
1779 b->virtual = virtual;
1780 b->visibility = visibility;
1782 return b;
1785 /* Make a field for a struct. The second argument is the name. The
1786 third argument is the type of the field. The fourth argument is
1787 the bit position of the field. The fifth argument is the size of
1788 the field (it may be zero). The sixth argument is the visibility
1789 of the field. */
1791 /*ARGSUSED*/
1792 debug_field
1793 debug_make_field (handle, name, type, bitpos, bitsize, visibility)
1794 PTR handle;
1795 const char *name;
1796 debug_type type;
1797 bfd_vma bitpos;
1798 bfd_vma bitsize;
1799 enum debug_visibility visibility;
1801 struct debug_field *f;
1803 f = (struct debug_field *) xmalloc (sizeof *f);
1804 memset (f, 0, sizeof *f);
1806 f->name = name;
1807 f->type = type;
1808 f->static_member = false;
1809 f->u.f.bitpos = bitpos;
1810 f->u.f.bitsize = bitsize;
1811 f->visibility = visibility;
1813 return f;
1816 /* Make a static member of an object. The second argument is the
1817 name. The third argument is the type of the member. The fourth
1818 argument is the physical name of the member (i.e., the name as a
1819 global variable). The fifth argument is the visibility of the
1820 member. */
1822 /*ARGSUSED*/
1823 debug_field
1824 debug_make_static_member (handle, name, type, physname, visibility)
1825 PTR handle;
1826 const char *name;
1827 debug_type type;
1828 const char *physname;
1829 enum debug_visibility visibility;
1831 struct debug_field *f;
1833 f = (struct debug_field *) xmalloc (sizeof *f);
1834 memset (f, 0, sizeof *f);
1836 f->name = name;
1837 f->type = type;
1838 f->static_member = true;
1839 f->u.s.physname = physname;
1840 f->visibility = visibility;
1842 return f;
1845 /* Make a method. The second argument is the name, and the third
1846 argument is a NULL terminated array of method variants. */
1848 /*ARGSUSED*/
1849 debug_method
1850 debug_make_method (handle, name, variants)
1851 PTR handle;
1852 const char *name;
1853 debug_method_variant *variants;
1855 struct debug_method *m;
1857 m = (struct debug_method *) xmalloc (sizeof *m);
1858 memset (m, 0, sizeof *m);
1860 m->name = name;
1861 m->variants = variants;
1863 return m;
1866 /* Make a method argument. The second argument is the real name of
1867 the function. The third argument is the type of the function. The
1868 fourth argument is the visibility. The fifth argument is whether
1869 this is a const function. The sixth argument is whether this is a
1870 volatile function. The seventh argument is the offset in the
1871 virtual function table, if any. The eighth argument is the virtual
1872 function context. FIXME: Are the const and volatile arguments
1873 necessary? Could we just use debug_make_const_type? */
1875 /*ARGSUSED*/
1876 debug_method_variant
1877 debug_make_method_variant (handle, physname, type, visibility, constp,
1878 volatilep, voffset, context)
1879 PTR handle;
1880 const char *physname;
1881 debug_type type;
1882 enum debug_visibility visibility;
1883 boolean constp;
1884 boolean volatilep;
1885 bfd_vma voffset;
1886 debug_type context;
1888 struct debug_method_variant *m;
1890 m = (struct debug_method_variant *) xmalloc (sizeof *m);
1891 memset (m, 0, sizeof *m);
1893 m->physname = physname;
1894 m->type = type;
1895 m->visibility = visibility;
1896 m->constp = constp;
1897 m->volatilep = volatilep;
1898 m->voffset = voffset;
1899 m->context = context;
1901 return m;
1904 /* Make a static method argument. The arguments are the same as for
1905 debug_make_method_variant, except that the last two are omitted
1906 since a static method can not also be virtual. */
1908 debug_method_variant
1909 debug_make_static_method_variant (handle, physname, type, visibility,
1910 constp, volatilep)
1911 PTR handle;
1912 const char *physname;
1913 debug_type type;
1914 enum debug_visibility visibility;
1915 boolean constp;
1916 boolean volatilep;
1918 struct debug_method_variant *m;
1920 m = (struct debug_method_variant *) xmalloc (sizeof *m);
1921 memset (m, 0, sizeof *m);
1923 m->physname = physname;
1924 m->type = type;
1925 m->visibility = visibility;
1926 m->constp = constp;
1927 m->volatilep = volatilep;
1928 m->voffset = VOFFSET_STATIC_METHOD;
1930 return m;
1933 /* Name a type. */
1935 debug_type
1936 debug_name_type (handle, name, type)
1937 PTR handle;
1938 const char *name;
1939 debug_type type;
1941 struct debug_handle *info = (struct debug_handle *) handle;
1942 struct debug_type *t;
1943 struct debug_named_type *n;
1944 struct debug_name *nm;
1946 if (name == NULL || type == NULL)
1947 return DEBUG_TYPE_NULL;
1949 if (info->current_unit == NULL
1950 || info->current_file == NULL)
1952 debug_error ("debug_record_variable: no current file");
1953 return false;
1956 t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1957 if (t == NULL)
1958 return DEBUG_TYPE_NULL;
1960 n = (struct debug_named_type *) xmalloc (sizeof *n);
1961 memset (n, 0, sizeof *n);
1963 n->type = type;
1965 t->u.knamed = n;
1967 /* We always add the name to the global namespace. This is probably
1968 wrong in some cases, but it seems to be right for stabs. FIXME. */
1970 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1971 DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1972 if (nm == NULL)
1973 return false;
1975 nm->u.type = t;
1977 n->name = nm;
1979 return t;
1982 /* Tag a type. */
1984 debug_type
1985 debug_tag_type (handle, name, type)
1986 PTR handle;
1987 const char *name;
1988 debug_type type;
1990 struct debug_handle *info = (struct debug_handle *) handle;
1991 struct debug_type *t;
1992 struct debug_named_type *n;
1993 struct debug_name *nm;
1995 if (name == NULL || type == NULL)
1996 return DEBUG_TYPE_NULL;
1998 if (info->current_file == NULL)
2000 debug_error ("debug_tag_type: no current file");
2001 return DEBUG_TYPE_NULL;
2004 if (type->kind == DEBUG_KIND_TAGGED)
2006 if (strcmp (type->u.knamed->name->name, name) == 0)
2007 return type;
2008 debug_error ("debug_tag_type: extra tag attempted");
2009 return DEBUG_TYPE_NULL;
2012 t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
2013 if (t == NULL)
2014 return DEBUG_TYPE_NULL;
2016 n = (struct debug_named_type *) xmalloc (sizeof *n);
2017 memset (n, 0, sizeof *n);
2019 n->type = type;
2021 t->u.knamed = n;
2023 /* We keep a global namespace of tags for each compilation unit. I
2024 don't know if that is the right thing to do. */
2026 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
2027 DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
2028 if (nm == NULL)
2029 return false;
2031 nm->u.tag = t;
2033 n->name = nm;
2035 return t;
2038 /* Record the size of a given type. */
2040 /*ARGSUSED*/
2041 boolean
2042 debug_record_type_size (handle, type, size)
2043 PTR handle;
2044 debug_type type;
2045 unsigned int size;
2047 if (type->size != 0 && type->size != size)
2048 fprintf (stderr, "Warning: changing type size from %d to %d\n",
2049 type->size, size);
2051 type->size = size;
2053 return true;
2056 /* Find a named type. */
2058 debug_type
2059 debug_find_named_type (handle, name)
2060 PTR handle;
2061 const char *name;
2063 struct debug_handle *info = (struct debug_handle *) handle;
2064 struct debug_block *b;
2065 struct debug_file *f;
2067 /* We only search the current compilation unit. I don't know if
2068 this is right or not. */
2070 if (info->current_unit == NULL)
2072 debug_error ("debug_find_named_type: no current compilation unit");
2073 return DEBUG_TYPE_NULL;
2076 for (b = info->current_block; b != NULL; b = b->parent)
2078 if (b->locals != NULL)
2080 struct debug_name *n;
2082 for (n = b->locals->list; n != NULL; n = n->next)
2084 if (n->kind == DEBUG_OBJECT_TYPE
2085 && n->name[0] == name[0]
2086 && strcmp (n->name, name) == 0)
2087 return n->u.type;
2092 for (f = info->current_unit->files; f != NULL; f = f->next)
2094 if (f->globals != NULL)
2096 struct debug_name *n;
2098 for (n = f->globals->list; n != NULL; n = n->next)
2100 if (n->kind == DEBUG_OBJECT_TYPE
2101 && n->name[0] == name[0]
2102 && strcmp (n->name, name) == 0)
2103 return n->u.type;
2108 return DEBUG_TYPE_NULL;
2111 /* Find a tagged type. */
2113 debug_type
2114 debug_find_tagged_type (handle, name, kind)
2115 PTR handle;
2116 const char *name;
2117 enum debug_type_kind kind;
2119 struct debug_handle *info = (struct debug_handle *) handle;
2120 struct debug_unit *u;
2122 /* We search the globals of all the compilation units. I don't know
2123 if this is correct or not. It would be easy to change. */
2125 for (u = info->units; u != NULL; u = u->next)
2127 struct debug_file *f;
2129 for (f = u->files; f != NULL; f = f->next)
2131 struct debug_name *n;
2133 if (f->globals != NULL)
2135 for (n = f->globals->list; n != NULL; n = n->next)
2137 if (n->kind == DEBUG_OBJECT_TAG
2138 && (kind == DEBUG_KIND_ILLEGAL
2139 || n->u.tag->kind == kind)
2140 && n->name[0] == name[0]
2141 && strcmp (n->name, name) == 0)
2142 return n->u.tag;
2148 return DEBUG_TYPE_NULL;
2151 /* Get a base type. */
2153 static struct debug_type *
2154 debug_get_real_type (handle, type)
2155 PTR handle;
2156 debug_type type;
2158 switch (type->kind)
2160 default:
2161 return type;
2162 case DEBUG_KIND_INDIRECT:
2163 if (*type->u.kindirect->slot != NULL)
2164 return debug_get_real_type (handle, *type->u.kindirect->slot);
2165 return type;
2166 case DEBUG_KIND_NAMED:
2167 case DEBUG_KIND_TAGGED:
2168 return debug_get_real_type (handle, type->u.knamed->type);
2170 /*NOTREACHED*/
2173 /* Get the kind of a type. */
2175 enum debug_type_kind
2176 debug_get_type_kind (handle, type)
2177 PTR handle;
2178 debug_type type;
2180 if (type == NULL)
2181 return DEBUG_KIND_ILLEGAL;
2182 type = debug_get_real_type (handle, type);
2183 return type->kind;
2186 /* Get the name of a type. */
2188 const char *
2189 debug_get_type_name (handle, type)
2190 PTR handle;
2191 debug_type type;
2193 if (type->kind == DEBUG_KIND_INDIRECT)
2195 if (*type->u.kindirect->slot != NULL)
2196 return debug_get_type_name (handle, *type->u.kindirect->slot);
2197 return type->u.kindirect->tag;
2199 if (type->kind == DEBUG_KIND_NAMED
2200 || type->kind == DEBUG_KIND_TAGGED)
2201 return type->u.knamed->name->name;
2202 return NULL;
2205 /* Get the size of a type. */
2207 bfd_vma
2208 debug_get_type_size (handle, type)
2209 PTR handle;
2210 debug_type type;
2212 if (type == NULL)
2213 return 0;
2215 /* We don't call debug_get_real_type, because somebody might have
2216 called debug_record_type_size on a named or indirect type. */
2218 if (type->size != 0)
2219 return type->size;
2221 switch (type->kind)
2223 default:
2224 return 0;
2225 case DEBUG_KIND_INDIRECT:
2226 if (*type->u.kindirect->slot != NULL)
2227 return debug_get_type_size (handle, *type->u.kindirect->slot);
2228 return 0;
2229 case DEBUG_KIND_NAMED:
2230 case DEBUG_KIND_TAGGED:
2231 return debug_get_type_size (handle, type->u.knamed->type);
2233 /*NOTREACHED*/
2236 /* Get the return type of a function or method type. */
2238 debug_type
2239 debug_get_return_type (handle, type)
2240 PTR handle;
2241 debug_type type;
2243 if (type == NULL)
2244 return DEBUG_TYPE_NULL;
2245 type = debug_get_real_type (handle, type);
2246 switch (type->kind)
2248 default:
2249 return DEBUG_TYPE_NULL;
2250 case DEBUG_KIND_FUNCTION:
2251 return type->u.kfunction->return_type;
2252 case DEBUG_KIND_METHOD:
2253 return type->u.kmethod->return_type;
2255 /*NOTREACHED*/
2258 /* Get the parameter types of a function or method type (except that
2259 we don't currently store the parameter types of a function). */
2261 const debug_type *
2262 debug_get_parameter_types (handle, type, pvarargs)
2263 PTR handle;
2264 debug_type type;
2265 boolean *pvarargs;
2267 if (type == NULL)
2268 return NULL;
2269 type = debug_get_real_type (handle, type);
2270 switch (type->kind)
2272 default:
2273 return NULL;
2274 case DEBUG_KIND_FUNCTION:
2275 *pvarargs = type->u.kfunction->varargs;
2276 return type->u.kfunction->arg_types;
2277 case DEBUG_KIND_METHOD:
2278 *pvarargs = type->u.kmethod->varargs;
2279 return type->u.kmethod->arg_types;
2281 /*NOTREACHED*/
2284 /* Get the target type of a type. */
2286 debug_type
2287 debug_get_target_type (handle, type)
2288 PTR handle;
2289 debug_type type;
2291 if (type == NULL)
2292 return NULL;
2293 type = debug_get_real_type (handle, type);
2294 switch (type->kind)
2296 default:
2297 return NULL;
2298 case DEBUG_KIND_POINTER:
2299 return type->u.kpointer;
2300 case DEBUG_KIND_REFERENCE:
2301 return type->u.kreference;
2302 case DEBUG_KIND_CONST:
2303 return type->u.kconst;
2304 case DEBUG_KIND_VOLATILE:
2305 return type->u.kvolatile;
2307 /*NOTREACHED*/
2310 /* Get the NULL terminated array of fields for a struct, union, or
2311 class. */
2313 const debug_field *
2314 debug_get_fields (handle, type)
2315 PTR handle;
2316 debug_type type;
2318 if (type == NULL)
2319 return NULL;
2320 type = debug_get_real_type (handle, type);
2321 switch (type->kind)
2323 default:
2324 return NULL;
2325 case DEBUG_KIND_STRUCT:
2326 case DEBUG_KIND_UNION:
2327 case DEBUG_KIND_CLASS:
2328 case DEBUG_KIND_UNION_CLASS:
2329 return type->u.kclass->fields;
2331 /*NOTREACHED*/
2334 /* Get the type of a field. */
2336 /*ARGSUSED*/
2337 debug_type
2338 debug_get_field_type (handle, field)
2339 PTR handle;
2340 debug_field field;
2342 if (field == NULL)
2343 return NULL;
2344 return field->type;
2347 /* Get the name of a field. */
2349 /*ARGSUSED*/
2350 const char *
2351 debug_get_field_name (handle, field)
2352 PTR handle;
2353 debug_field field;
2355 if (field == NULL)
2356 return NULL;
2357 return field->name;
2360 /* Get the bit position of a field. */
2362 /*ARGSUSED*/
2363 bfd_vma
2364 debug_get_field_bitpos (handle, field)
2365 PTR handle;
2366 debug_field field;
2368 if (field == NULL || field->static_member)
2369 return (bfd_vma) -1;
2370 return field->u.f.bitpos;
2373 /* Get the bit size of a field. */
2375 /*ARGSUSED*/
2376 bfd_vma
2377 debug_get_field_bitsize (handle, field)
2378 PTR handle;
2379 debug_field field;
2381 if (field == NULL || field->static_member)
2382 return (bfd_vma) -1;
2383 return field->u.f.bitsize;
2386 /* Get the visibility of a field. */
2388 /*ARGSUSED*/
2389 enum debug_visibility
2390 debug_get_field_visibility (handle, field)
2391 PTR handle;
2392 debug_field field;
2394 if (field == NULL)
2395 return DEBUG_VISIBILITY_IGNORE;
2396 return field->visibility;
2399 /* Get the physical name of a field. */
2401 const char *
2402 debug_get_field_physname (handle, field)
2403 PTR handle;
2404 debug_field field;
2406 if (field == NULL || ! field->static_member)
2407 return NULL;
2408 return field->u.s.physname;
2411 /* Write out the debugging information. This is given a handle to
2412 debugging information, and a set of function pointers to call. */
2414 boolean
2415 debug_write (handle, fns, fhandle)
2416 PTR handle;
2417 const struct debug_write_fns *fns;
2418 PTR fhandle;
2420 struct debug_handle *info = (struct debug_handle *) handle;
2421 struct debug_unit *u;
2423 /* We use a mark to tell whether we have already written out a
2424 particular name. We use an integer, so that we don't have to
2425 clear the mark fields if we happen to write out the same
2426 information more than once. */
2427 ++info->mark;
2429 /* The base_id field holds an ID value which will never be used, so
2430 that we can tell whether we have assigned an ID during this call
2431 to debug_write. */
2432 info->base_id = info->class_id;
2434 /* We keep a linked list of classes for which was have assigned ID's
2435 during this call to debug_write. */
2436 info->id_list = NULL;
2438 for (u = info->units; u != NULL; u = u->next)
2440 struct debug_file *f;
2441 boolean first_file;
2442 struct debug_lineno *l;
2444 if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2445 return false;
2447 first_file = true;
2448 for (f = u->files; f != NULL; f = f->next)
2450 struct debug_name *n;
2452 if (first_file)
2453 first_file = false;
2454 else
2456 if (! (*fns->start_source) (fhandle, f->filename))
2457 return false;
2460 if (f->globals != NULL)
2462 for (n = f->globals->list; n != NULL; n = n->next)
2464 if (! debug_write_name (info, fns, fhandle, n))
2465 return false;
2470 for (l = u->linenos; l != NULL; l = l->next)
2472 unsigned int i;
2474 for (i = 0; i < DEBUG_LINENO_COUNT; i++)
2476 if (l->linenos[i] == (unsigned long) -1)
2477 break;
2478 if (! (*fns->lineno) (fhandle, l->file->filename, l->linenos[i],
2479 l->addrs[i]))
2480 return false;
2485 return true;
2488 /* Write out an element in a namespace. */
2490 static boolean
2491 debug_write_name (info, fns, fhandle, n)
2492 struct debug_handle *info;
2493 const struct debug_write_fns *fns;
2494 PTR fhandle;
2495 struct debug_name *n;
2497 switch (n->kind)
2499 case DEBUG_OBJECT_TYPE:
2500 if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2501 || ! (*fns->typdef) (fhandle, n->name))
2502 return false;
2503 return true;
2504 case DEBUG_OBJECT_TAG:
2505 if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2506 return false;
2507 return (*fns->tag) (fhandle, n->name);
2508 case DEBUG_OBJECT_VARIABLE:
2509 if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2510 (struct debug_name *) NULL))
2511 return false;
2512 return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2513 n->u.variable->val);
2514 case DEBUG_OBJECT_FUNCTION:
2515 return debug_write_function (info, fns, fhandle, n->name,
2516 n->linkage, n->u.function);
2517 case DEBUG_OBJECT_INT_CONSTANT:
2518 return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2519 case DEBUG_OBJECT_FLOAT_CONSTANT:
2520 return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2521 case DEBUG_OBJECT_TYPED_CONSTANT:
2522 if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2523 (struct debug_name *) NULL))
2524 return false;
2525 return (*fns->typed_constant) (fhandle, n->name,
2526 n->u.typed_constant->val);
2527 default:
2528 abort ();
2529 return false;
2531 /*NOTREACHED*/
2534 /* Write out a type. If the type is DEBUG_KIND_NAMED or
2535 DEBUG_KIND_TAGGED, then the name argument is the name for which we
2536 are about to call typedef or tag. If the type is anything else,
2537 then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2538 points to this one. */
2540 static boolean
2541 debug_write_type (info, fns, fhandle, type, name)
2542 struct debug_handle *info;
2543 const struct debug_write_fns *fns;
2544 PTR fhandle;
2545 struct debug_type *type;
2546 struct debug_name *name;
2548 unsigned int i;
2549 int is;
2550 const char *tag;
2552 /* If we have a name for this type, just output it. We only output
2553 typedef names after they have been defined. We output type tags
2554 whenever we are not actually defining them. */
2555 if ((type->kind == DEBUG_KIND_NAMED
2556 || type->kind == DEBUG_KIND_TAGGED)
2557 && (type->u.knamed->name->mark == info->mark
2558 || (type->kind == DEBUG_KIND_TAGGED
2559 && type->u.knamed->name != name)))
2561 if (type->kind == DEBUG_KIND_NAMED)
2562 return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2563 else
2565 struct debug_type *real;
2566 unsigned int id;
2568 real = debug_get_real_type ((PTR) info, type);
2569 id = 0;
2570 if ((real->kind == DEBUG_KIND_STRUCT
2571 || real->kind == DEBUG_KIND_UNION
2572 || real->kind == DEBUG_KIND_CLASS
2573 || real->kind == DEBUG_KIND_UNION_CLASS)
2574 && real->u.kclass != NULL)
2576 if (real->u.kclass->id <= info->base_id)
2578 if (! debug_set_class_id (info,
2579 type->u.knamed->name->name,
2580 real))
2581 return false;
2583 id = real->u.kclass->id;
2586 return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
2587 real->kind);
2591 /* Mark the name after we have already looked for a known name, so
2592 that we don't just define a type in terms of itself. We need to
2593 mark the name here so that a struct containing a pointer to
2594 itself will work. */
2595 if (name != NULL)
2596 name->mark = info->mark;
2598 tag = NULL;
2599 if (name != NULL
2600 && type->kind != DEBUG_KIND_NAMED
2601 && type->kind != DEBUG_KIND_TAGGED)
2603 assert (name->kind == DEBUG_OBJECT_TAG);
2604 tag = name->name;
2607 switch (type->kind)
2609 case DEBUG_KIND_ILLEGAL:
2610 debug_error ("debug_write_type: illegal type encountered");
2611 return false;
2612 case DEBUG_KIND_INDIRECT:
2613 if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
2614 return (*fns->empty_type) (fhandle);
2615 return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2616 name);
2617 case DEBUG_KIND_VOID:
2618 return (*fns->void_type) (fhandle);
2619 case DEBUG_KIND_INT:
2620 return (*fns->int_type) (fhandle, type->size, type->u.kint);
2621 case DEBUG_KIND_FLOAT:
2622 return (*fns->float_type) (fhandle, type->size);
2623 case DEBUG_KIND_COMPLEX:
2624 return (*fns->complex_type) (fhandle, type->size);
2625 case DEBUG_KIND_BOOL:
2626 return (*fns->bool_type) (fhandle, type->size);
2627 case DEBUG_KIND_STRUCT:
2628 case DEBUG_KIND_UNION:
2629 if (type->u.kclass != NULL)
2631 if (type->u.kclass->id <= info->base_id)
2633 if (! debug_set_class_id (info, tag, type))
2634 return false;
2637 if (info->mark == type->u.kclass->mark)
2639 /* We are currently outputting this struct, or we have
2640 already output it. I don't know if this can happen,
2641 but it can happen for a class. */
2642 assert (type->u.kclass->id > info->base_id);
2643 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2644 type->kind);
2646 type->u.kclass->mark = info->mark;
2649 if (! (*fns->start_struct_type) (fhandle, tag,
2650 (type->u.kclass != NULL
2651 ? type->u.kclass->id
2652 : 0),
2653 type->kind == DEBUG_KIND_STRUCT,
2654 type->size))
2655 return false;
2656 if (type->u.kclass != NULL
2657 && type->u.kclass->fields != NULL)
2659 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2661 struct debug_field *f;
2663 f = type->u.kclass->fields[i];
2664 if (! debug_write_type (info, fns, fhandle, f->type,
2665 (struct debug_name *) NULL)
2666 || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2667 f->u.f.bitsize, f->visibility))
2668 return false;
2671 return (*fns->end_struct_type) (fhandle);
2672 case DEBUG_KIND_CLASS:
2673 case DEBUG_KIND_UNION_CLASS:
2674 return debug_write_class_type (info, fns, fhandle, type, tag);
2675 case DEBUG_KIND_ENUM:
2676 if (type->u.kenum == NULL)
2677 return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2678 (bfd_signed_vma *) NULL);
2679 return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2680 type->u.kenum->values);
2681 case DEBUG_KIND_POINTER:
2682 if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2683 (struct debug_name *) NULL))
2684 return false;
2685 return (*fns->pointer_type) (fhandle);
2686 case DEBUG_KIND_FUNCTION:
2687 if (type->u.kfunction->arg_types == NULL)
2688 is = -1;
2689 else
2691 for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2692 if (! debug_write_type (info, fns, fhandle,
2693 type->u.kfunction->arg_types[is],
2694 (struct debug_name *) NULL))
2695 return false;
2697 if (! debug_write_type (info, fns, fhandle,
2698 type->u.kfunction->return_type,
2699 (struct debug_name *) NULL))
2700 return false;
2701 return (*fns->function_type) (fhandle, is,
2702 type->u.kfunction->varargs);
2703 case DEBUG_KIND_REFERENCE:
2704 if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2705 (struct debug_name *) NULL))
2706 return false;
2707 return (*fns->reference_type) (fhandle);
2708 case DEBUG_KIND_RANGE:
2709 if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2710 (struct debug_name *) NULL))
2711 return false;
2712 return (*fns->range_type) (fhandle, type->u.krange->lower,
2713 type->u.krange->upper);
2714 case DEBUG_KIND_ARRAY:
2715 if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2716 (struct debug_name *) NULL)
2717 || ! debug_write_type (info, fns, fhandle,
2718 type->u.karray->range_type,
2719 (struct debug_name *) NULL))
2720 return false;
2721 return (*fns->array_type) (fhandle, type->u.karray->lower,
2722 type->u.karray->upper,
2723 type->u.karray->stringp);
2724 case DEBUG_KIND_SET:
2725 if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2726 (struct debug_name *) NULL))
2727 return false;
2728 return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2729 case DEBUG_KIND_OFFSET:
2730 if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2731 (struct debug_name *) NULL)
2732 || ! debug_write_type (info, fns, fhandle,
2733 type->u.koffset->target_type,
2734 (struct debug_name *) NULL))
2735 return false;
2736 return (*fns->offset_type) (fhandle);
2737 case DEBUG_KIND_METHOD:
2738 if (! debug_write_type (info, fns, fhandle,
2739 type->u.kmethod->return_type,
2740 (struct debug_name *) NULL))
2741 return false;
2742 if (type->u.kmethod->arg_types == NULL)
2743 is = -1;
2744 else
2746 for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2747 if (! debug_write_type (info, fns, fhandle,
2748 type->u.kmethod->arg_types[is],
2749 (struct debug_name *) NULL))
2750 return false;
2752 if (type->u.kmethod->domain_type != NULL)
2754 if (! debug_write_type (info, fns, fhandle,
2755 type->u.kmethod->domain_type,
2756 (struct debug_name *) NULL))
2757 return false;
2759 return (*fns->method_type) (fhandle,
2760 type->u.kmethod->domain_type != NULL,
2762 type->u.kmethod->varargs);
2763 case DEBUG_KIND_CONST:
2764 if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2765 (struct debug_name *) NULL))
2766 return false;
2767 return (*fns->const_type) (fhandle);
2768 case DEBUG_KIND_VOLATILE:
2769 if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2770 (struct debug_name *) NULL))
2771 return false;
2772 return (*fns->volatile_type) (fhandle);
2773 case DEBUG_KIND_NAMED:
2774 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2775 (struct debug_name *) NULL);
2776 case DEBUG_KIND_TAGGED:
2777 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2778 type->u.knamed->name);
2779 default:
2780 abort ();
2781 return false;
2785 /* Write out a class type. */
2787 static boolean
2788 debug_write_class_type (info, fns, fhandle, type, tag)
2789 struct debug_handle *info;
2790 const struct debug_write_fns *fns;
2791 PTR fhandle;
2792 struct debug_type *type;
2793 const char *tag;
2795 unsigned int i;
2796 unsigned int id;
2797 struct debug_type *vptrbase;
2799 if (type->u.kclass == NULL)
2801 id = 0;
2802 vptrbase = NULL;
2804 else
2806 if (type->u.kclass->id <= info->base_id)
2808 if (! debug_set_class_id (info, tag, type))
2809 return false;
2812 if (info->mark == type->u.kclass->mark)
2814 /* We are currently outputting this class, or we have
2815 already output it. This can happen when there are
2816 methods for an anonymous class. */
2817 assert (type->u.kclass->id > info->base_id);
2818 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2819 type->kind);
2821 type->u.kclass->mark = info->mark;
2822 id = type->u.kclass->id;
2824 vptrbase = type->u.kclass->vptrbase;
2825 if (vptrbase != NULL && vptrbase != type)
2827 if (! debug_write_type (info, fns, fhandle, vptrbase,
2828 (struct debug_name *) NULL))
2829 return false;
2833 if (! (*fns->start_class_type) (fhandle, tag, id,
2834 type->kind == DEBUG_KIND_CLASS,
2835 type->size,
2836 vptrbase != NULL,
2837 vptrbase == type))
2838 return false;
2840 if (type->u.kclass != NULL)
2842 if (type->u.kclass->fields != NULL)
2844 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2846 struct debug_field *f;
2848 f = type->u.kclass->fields[i];
2849 if (! debug_write_type (info, fns, fhandle, f->type,
2850 (struct debug_name *) NULL))
2851 return false;
2852 if (f->static_member)
2854 if (! (*fns->class_static_member) (fhandle, f->name,
2855 f->u.s.physname,
2856 f->visibility))
2857 return false;
2859 else
2861 if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2862 f->u.f.bitsize, f->visibility))
2863 return false;
2868 if (type->u.kclass->baseclasses != NULL)
2870 for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2872 struct debug_baseclass *b;
2874 b = type->u.kclass->baseclasses[i];
2875 if (! debug_write_type (info, fns, fhandle, b->type,
2876 (struct debug_name *) NULL))
2877 return false;
2878 if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->virtual,
2879 b->visibility))
2880 return false;
2884 if (type->u.kclass->methods != NULL)
2886 for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2888 struct debug_method *m;
2889 unsigned int j;
2891 m = type->u.kclass->methods[i];
2892 if (! (*fns->class_start_method) (fhandle, m->name))
2893 return false;
2894 for (j = 0; m->variants[j] != NULL; j++)
2896 struct debug_method_variant *v;
2898 v = m->variants[j];
2899 if (v->context != NULL)
2901 if (! debug_write_type (info, fns, fhandle, v->context,
2902 (struct debug_name *) NULL))
2903 return false;
2905 if (! debug_write_type (info, fns, fhandle, v->type,
2906 (struct debug_name *) NULL))
2907 return false;
2908 if (v->voffset != VOFFSET_STATIC_METHOD)
2910 if (! (*fns->class_method_variant) (fhandle, v->physname,
2911 v->visibility,
2912 v->constp,
2913 v->volatilep,
2914 v->voffset,
2915 v->context != NULL))
2916 return false;
2918 else
2920 if (! (*fns->class_static_method_variant) (fhandle,
2921 v->physname,
2922 v->visibility,
2923 v->constp,
2924 v->volatilep))
2925 return false;
2928 if (! (*fns->class_end_method) (fhandle))
2929 return false;
2934 return (*fns->end_class_type) (fhandle);
2937 /* Write out information for a function. */
2939 static boolean
2940 debug_write_function (info, fns, fhandle, name, linkage, function)
2941 struct debug_handle *info;
2942 const struct debug_write_fns *fns;
2943 PTR fhandle;
2944 const char *name;
2945 enum debug_object_linkage linkage;
2946 struct debug_function *function;
2948 struct debug_parameter *p;
2949 struct debug_block *b;
2951 if (! debug_write_type (info, fns, fhandle, function->return_type,
2952 (struct debug_name *) NULL))
2953 return false;
2955 if (! (*fns->start_function) (fhandle, name,
2956 linkage == DEBUG_LINKAGE_GLOBAL))
2957 return false;
2959 for (p = function->parameters; p != NULL; p = p->next)
2961 if (! debug_write_type (info, fns, fhandle, p->type,
2962 (struct debug_name *) NULL)
2963 || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2964 return false;
2967 for (b = function->blocks; b != NULL; b = b->next)
2969 if (! debug_write_block (info, fns, fhandle, b))
2970 return false;
2973 return (*fns->end_function) (fhandle);
2976 /* Write out information for a block. */
2978 static boolean
2979 debug_write_block (info, fns, fhandle, block)
2980 struct debug_handle *info;
2981 const struct debug_write_fns *fns;
2982 PTR fhandle;
2983 struct debug_block *block;
2985 struct debug_name *n;
2986 struct debug_block *b;
2988 /* I can't see any point to writing out a block with no local
2989 variables, so we don't bother, except for the top level block. */
2990 if (block->locals != NULL || block->parent == NULL)
2992 if (! (*fns->start_block) (fhandle, block->start))
2993 return false;
2996 if (block->locals != NULL)
2998 for (n = block->locals->list; n != NULL; n = n->next)
3000 if (! debug_write_name (info, fns, fhandle, n))
3001 return false;
3005 for (b = block->children; b != NULL; b = b->next)
3007 if (! debug_write_block (info, fns, fhandle, b))
3008 return false;
3011 if (block->locals != NULL || block->parent == NULL)
3013 if (! (*fns->end_block) (fhandle, block->end))
3014 return false;
3017 return true;
3020 /* Get the ID number for a class. If during the same call to
3021 debug_write we find a struct with the same definition with the same
3022 name, we use the same ID. This type of things happens because the
3023 same struct will be defined by multiple compilation units. */
3025 static boolean
3026 debug_set_class_id (info, tag, type)
3027 struct debug_handle *info;
3028 const char *tag;
3029 struct debug_type *type;
3031 struct debug_class_type *c;
3032 struct debug_class_id *l;
3034 assert (type->kind == DEBUG_KIND_STRUCT
3035 || type->kind == DEBUG_KIND_UNION
3036 || type->kind == DEBUG_KIND_CLASS
3037 || type->kind == DEBUG_KIND_UNION_CLASS);
3039 c = type->u.kclass;
3041 if (c->id > info->base_id)
3042 return true;
3044 for (l = info->id_list; l != NULL; l = l->next)
3046 if (l->type->kind != type->kind)
3047 continue;
3049 if (tag == NULL)
3051 if (l->tag != NULL)
3052 continue;
3054 else
3056 if (l->tag == NULL
3057 || l->tag[0] != tag[0]
3058 || strcmp (l->tag, tag) != 0)
3059 continue;
3062 if (debug_type_samep (info, l->type, type))
3064 c->id = l->type->u.kclass->id;
3065 return true;
3069 /* There are no identical types. Use a new ID, and add it to the
3070 list. */
3071 ++info->class_id;
3072 c->id = info->class_id;
3074 l = (struct debug_class_id *) xmalloc (sizeof *l);
3075 memset (l, 0, sizeof *l);
3077 l->type = type;
3078 l->tag = tag;
3080 l->next = info->id_list;
3081 info->id_list = l;
3083 return true;
3086 /* See if two types are the same. At this point, we don't care about
3087 tags and the like. */
3089 static boolean
3090 debug_type_samep (info, t1, t2)
3091 struct debug_handle *info;
3092 struct debug_type *t1;
3093 struct debug_type *t2;
3095 struct debug_type_compare_list *l;
3096 struct debug_type_compare_list top;
3097 boolean ret;
3099 while (t1->kind == DEBUG_KIND_INDIRECT)
3101 t1 = *t1->u.kindirect->slot;
3102 if (t1 == NULL)
3103 return false;
3105 while (t2->kind == DEBUG_KIND_INDIRECT)
3107 t2 = *t2->u.kindirect->slot;
3108 if (t2 == NULL)
3109 return false;
3112 if (t1 == t2)
3113 return true;
3115 /* As a special case, permit a typedef to match a tag, since C++
3116 debugging output will sometimes add a typedef where C debugging
3117 output will not. */
3118 if (t1->kind == DEBUG_KIND_NAMED
3119 && t2->kind == DEBUG_KIND_TAGGED)
3120 return debug_type_samep (info, t1->u.knamed->type, t2);
3121 else if (t1->kind == DEBUG_KIND_TAGGED
3122 && t2->kind == DEBUG_KIND_NAMED)
3123 return debug_type_samep (info, t1, t2->u.knamed->type);
3125 if (t1->kind != t2->kind
3126 || t1->size != t2->size)
3127 return false;
3129 /* Get rid of the trivial cases first. */
3130 switch (t1->kind)
3132 default:
3133 break;
3134 case DEBUG_KIND_VOID:
3135 case DEBUG_KIND_FLOAT:
3136 case DEBUG_KIND_COMPLEX:
3137 case DEBUG_KIND_BOOL:
3138 return true;
3139 case DEBUG_KIND_INT:
3140 return t1->u.kint == t2->u.kint;
3143 /* We have to avoid an infinite recursion. We do this by keeping a
3144 list of types which we are comparing. We just keep the list on
3145 the stack. If we encounter a pair of types we are currently
3146 comparing, we just assume that they are equal. */
3147 for (l = info->compare_list; l != NULL; l = l->next)
3149 if (l->t1 == t1 && l->t2 == t2)
3150 return true;
3153 top.t1 = t1;
3154 top.t2 = t2;
3155 top.next = info->compare_list;
3156 info->compare_list = &top;
3158 switch (t1->kind)
3160 default:
3161 abort ();
3162 ret = false;
3163 break;
3165 case DEBUG_KIND_STRUCT:
3166 case DEBUG_KIND_UNION:
3167 case DEBUG_KIND_CLASS:
3168 case DEBUG_KIND_UNION_CLASS:
3169 if (t1->u.kclass == NULL)
3170 ret = t2->u.kclass == NULL;
3171 else if (t2->u.kclass == NULL)
3172 ret = false;
3173 else if (t1->u.kclass->id > info->base_id
3174 && t1->u.kclass->id == t2->u.kclass->id)
3175 ret = true;
3176 else
3177 ret = debug_class_type_samep (info, t1, t2);
3178 break;
3180 case DEBUG_KIND_ENUM:
3181 if (t1->u.kenum == NULL)
3182 ret = t2->u.kenum == NULL;
3183 else if (t2->u.kenum == NULL)
3184 ret = false;
3185 else
3187 const char **pn1, **pn2;
3188 bfd_signed_vma *pv1, *pv2;
3190 pn1 = t1->u.kenum->names;
3191 pn2 = t2->u.kenum->names;
3192 pv1 = t1->u.kenum->values;
3193 pv2 = t2->u.kenum->values;
3194 while (*pn1 != NULL && *pn2 != NULL)
3196 if (**pn1 != **pn2
3197 || *pv1 != *pv2
3198 || strcmp (*pn1, *pn2) != 0)
3199 break;
3200 ++pn1;
3201 ++pn2;
3202 ++pv1;
3203 ++pv2;
3205 ret = *pn1 == NULL && *pn2 == NULL;
3207 break;
3209 case DEBUG_KIND_POINTER:
3210 ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
3211 break;
3213 case DEBUG_KIND_FUNCTION:
3214 if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
3215 || ! debug_type_samep (info, t1->u.kfunction->return_type,
3216 t2->u.kfunction->return_type)
3217 || ((t1->u.kfunction->arg_types == NULL)
3218 != (t2->u.kfunction->arg_types == NULL)))
3219 ret = false;
3220 else if (t1->u.kfunction->arg_types == NULL)
3221 ret = true;
3222 else
3224 struct debug_type **a1, **a2;
3226 a1 = t1->u.kfunction->arg_types;
3227 a2 = t2->u.kfunction->arg_types;
3228 while (*a1 != NULL && *a2 != NULL)
3229 if (! debug_type_samep (info, *a1, *a2))
3230 break;
3231 ret = *a1 == NULL && *a2 == NULL;
3233 break;
3235 case DEBUG_KIND_REFERENCE:
3236 ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
3237 break;
3239 case DEBUG_KIND_RANGE:
3240 ret = (t1->u.krange->lower == t2->u.krange->lower
3241 && t1->u.krange->upper == t2->u.krange->upper
3242 && debug_type_samep (info, t1->u.krange->type,
3243 t2->u.krange->type));
3245 case DEBUG_KIND_ARRAY:
3246 ret = (t1->u.karray->lower == t2->u.karray->lower
3247 && t1->u.karray->upper == t2->u.karray->upper
3248 && t1->u.karray->stringp == t2->u.karray->stringp
3249 && debug_type_samep (info, t1->u.karray->element_type,
3250 t2->u.karray->element_type));
3251 break;
3253 case DEBUG_KIND_SET:
3254 ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
3255 && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
3256 break;
3258 case DEBUG_KIND_OFFSET:
3259 ret = (debug_type_samep (info, t1->u.koffset->base_type,
3260 t2->u.koffset->base_type)
3261 && debug_type_samep (info, t1->u.koffset->target_type,
3262 t2->u.koffset->target_type));
3263 break;
3265 case DEBUG_KIND_METHOD:
3266 if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
3267 || ! debug_type_samep (info, t1->u.kmethod->return_type,
3268 t2->u.kmethod->return_type)
3269 || ! debug_type_samep (info, t1->u.kmethod->domain_type,
3270 t2->u.kmethod->domain_type)
3271 || ((t1->u.kmethod->arg_types == NULL)
3272 != (t2->u.kmethod->arg_types == NULL)))
3273 ret = false;
3274 else if (t1->u.kmethod->arg_types == NULL)
3275 ret = true;
3276 else
3278 struct debug_type **a1, **a2;
3280 a1 = t1->u.kmethod->arg_types;
3281 a2 = t2->u.kmethod->arg_types;
3282 while (*a1 != NULL && *a2 != NULL)
3283 if (! debug_type_samep (info, *a1, *a2))
3284 break;
3285 ret = *a1 == NULL && *a2 == NULL;
3287 break;
3289 case DEBUG_KIND_CONST:
3290 ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
3291 break;
3293 case DEBUG_KIND_VOLATILE:
3294 ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
3295 break;
3297 case DEBUG_KIND_NAMED:
3298 case DEBUG_KIND_TAGGED:
3299 ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
3300 && debug_type_samep (info, t1->u.knamed->type,
3301 t2->u.knamed->type));
3302 break;
3305 info->compare_list = top.next;
3307 return ret;
3310 /* See if two classes are the same. This is a subroutine of
3311 debug_type_samep. */
3313 static boolean
3314 debug_class_type_samep (info, t1, t2)
3315 struct debug_handle *info;
3316 struct debug_type *t1;
3317 struct debug_type *t2;
3319 struct debug_class_type *c1, *c2;
3321 c1 = t1->u.kclass;
3322 c2 = t2->u.kclass;
3324 if ((c1->fields == NULL) != (c2->fields == NULL)
3325 || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
3326 || (c1->methods == NULL) != (c2->methods == NULL)
3327 || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
3328 return false;
3330 if (c1->fields != NULL)
3332 struct debug_field **pf1, **pf2;
3334 for (pf1 = c1->fields, pf2 = c2->fields;
3335 *pf1 != NULL && *pf2 != NULL;
3336 pf1++, pf2++)
3338 struct debug_field *f1, *f2;
3340 f1 = *pf1;
3341 f2 = *pf2;
3342 if (f1->name[0] != f2->name[0]
3343 || f1->visibility != f2->visibility
3344 || f1->static_member != f2->static_member)
3345 return false;
3346 if (f1->static_member)
3348 if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
3349 return false;
3351 else
3353 if (f1->u.f.bitpos != f2->u.f.bitpos
3354 || f1->u.f.bitsize != f2->u.f.bitsize)
3355 return false;
3357 /* We do the checks which require function calls last. We
3358 don't require that the types of fields have the same
3359 names, since that sometimes fails in the presence of
3360 typedefs and we really don't care. */
3361 if (strcmp (f1->name, f2->name) != 0
3362 || ! debug_type_samep (info,
3363 debug_get_real_type ((PTR) info,
3364 f1->type),
3365 debug_get_real_type ((PTR) info,
3366 f2->type)))
3367 return false;
3369 if (*pf1 != NULL || *pf2 != NULL)
3370 return false;
3373 if (c1->vptrbase != NULL)
3375 if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
3376 return false;
3379 if (c1->baseclasses != NULL)
3381 struct debug_baseclass **pb1, **pb2;
3383 for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
3384 *pb1 != NULL && *pb2 != NULL;
3385 ++pb1, ++pb2)
3387 struct debug_baseclass *b1, *b2;
3389 b1 = *pb1;
3390 b2 = *pb2;
3391 if (b1->bitpos != b2->bitpos
3392 || b1->virtual != b2->virtual
3393 || b1->visibility != b2->visibility
3394 || ! debug_type_samep (info, b1->type, b2->type))
3395 return false;
3397 if (*pb1 != NULL || *pb2 != NULL)
3398 return false;
3401 if (c1->methods != NULL)
3403 struct debug_method **pm1, **pm2;
3405 for (pm1 = c1->methods, pm2 = c2->methods;
3406 *pm1 != NULL && *pm2 != NULL;
3407 ++pm1, ++pm2)
3409 struct debug_method *m1, *m2;
3411 m1 = *pm1;
3412 m2 = *pm2;
3413 if (m1->name[0] != m2->name[0]
3414 || strcmp (m1->name, m2->name) != 0
3415 || (m1->variants == NULL) != (m2->variants == NULL))
3416 return false;
3417 if (m1->variants == NULL)
3419 struct debug_method_variant **pv1, **pv2;
3421 for (pv1 = m1->variants, pv2 = m2->variants;
3422 *pv1 != NULL && *pv2 != NULL;
3423 ++pv1, ++pv2)
3425 struct debug_method_variant *v1, *v2;
3427 v1 = *pv1;
3428 v2 = *pv2;
3429 if (v1->physname[0] != v2->physname[0]
3430 || v1->visibility != v2->visibility
3431 || v1->constp != v2->constp
3432 || v1->volatilep != v2->volatilep
3433 || v1->voffset != v2->voffset
3434 || (v1->context == NULL) != (v2->context == NULL)
3435 || strcmp (v1->physname, v2->physname) != 0
3436 || ! debug_type_samep (info, v1->type, v2->type))
3437 return false;
3438 if (v1->context != NULL)
3440 if (! debug_type_samep (info, v1->context,
3441 v2->context))
3442 return false;
3445 if (*pv1 != NULL || *pv2 != NULL)
3446 return false;
3449 if (*pm1 != NULL || *pm2 != NULL)
3450 return false;
3453 return true;