* gas/mips/mips.exp: Fix typo.
[binutils.git] / binutils / prdbg.c
blobb29eec6b569c0caa470a98a308a79fc4e3b685c3
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright 1995, 1996, 2002 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 prints out the generic debugging information, by
23 supplying a set of routines to debug_write. */
25 #include <stdio.h>
26 #include <assert.h>
28 #include "bfd.h"
29 #include "bucomm.h"
30 #include "libiberty.h"
31 #include "debug.h"
32 #include "budbg.h"
34 /* This is the structure we use as a handle for these routines. */
36 struct pr_handle
38 /* File to print information to. */
39 FILE *f;
40 /* Current indentation level. */
41 unsigned int indent;
42 /* Type stack. */
43 struct pr_stack *stack;
44 /* Parameter number we are about to output. */
45 int parameter;
48 /* The type stack. */
50 struct pr_stack
52 /* Next element on the stack. */
53 struct pr_stack *next;
54 /* This element. */
55 char *type;
56 /* Current visibility of fields if this is a class. */
57 enum debug_visibility visibility;
58 /* Name of the current method we are handling. */
59 const char *method;
62 static void indent
63 PARAMS ((struct pr_handle *));
64 static bfd_boolean push_type
65 PARAMS ((struct pr_handle *, const char *));
66 static bfd_boolean prepend_type
67 PARAMS ((struct pr_handle *, const char *));
68 static bfd_boolean append_type
69 PARAMS ((struct pr_handle *, const char *));
70 static bfd_boolean substitute_type
71 PARAMS ((struct pr_handle *, const char *));
72 static bfd_boolean indent_type
73 PARAMS ((struct pr_handle *));
74 static char *pop_type
75 PARAMS ((struct pr_handle *));
76 static void print_vma
77 PARAMS ((bfd_vma, char *, bfd_boolean, bfd_boolean));
78 static bfd_boolean pr_fix_visibility
79 PARAMS ((struct pr_handle *, enum debug_visibility));
80 static bfd_boolean pr_start_compilation_unit
81 PARAMS ((PTR, const char *));
82 static bfd_boolean pr_start_source
83 PARAMS ((PTR, const char *));
84 static bfd_boolean pr_empty_type
85 PARAMS ((PTR));
86 static bfd_boolean pr_void_type
87 PARAMS ((PTR));
88 static bfd_boolean pr_int_type
89 PARAMS ((PTR, unsigned int, bfd_boolean));
90 static bfd_boolean pr_float_type
91 PARAMS ((PTR, unsigned int));
92 static bfd_boolean pr_complex_type
93 PARAMS ((PTR, unsigned int));
94 static bfd_boolean pr_bool_type
95 PARAMS ((PTR, unsigned int));
96 static bfd_boolean pr_enum_type
97 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
98 static bfd_boolean pr_pointer_type
99 PARAMS ((PTR));
100 static bfd_boolean pr_function_type
101 PARAMS ((PTR, int, bfd_boolean));
102 static bfd_boolean pr_reference_type
103 PARAMS ((PTR));
104 static bfd_boolean pr_range_type
105 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
106 static bfd_boolean pr_array_type
107 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, bfd_boolean));
108 static bfd_boolean pr_set_type
109 PARAMS ((PTR, bfd_boolean));
110 static bfd_boolean pr_offset_type
111 PARAMS ((PTR));
112 static bfd_boolean pr_method_type
113 PARAMS ((PTR, bfd_boolean, int, bfd_boolean));
114 static bfd_boolean pr_const_type
115 PARAMS ((PTR));
116 static bfd_boolean pr_volatile_type
117 PARAMS ((PTR));
118 static bfd_boolean pr_start_struct_type
119 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int));
120 static bfd_boolean pr_struct_field
121 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
122 static bfd_boolean pr_end_struct_type
123 PARAMS ((PTR));
124 static bfd_boolean pr_start_class_type
125 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int,
126 bfd_boolean, bfd_boolean));
127 static bfd_boolean pr_class_static_member
128 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
129 static bfd_boolean pr_class_baseclass
130 PARAMS ((PTR, bfd_vma, bfd_boolean, enum debug_visibility));
131 static bfd_boolean pr_class_start_method
132 PARAMS ((PTR, const char *));
133 static bfd_boolean pr_class_method_variant
134 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
135 bfd_vma, bfd_boolean));
136 static bfd_boolean pr_class_static_method_variant
137 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean,
138 bfd_boolean));
139 static bfd_boolean pr_class_end_method
140 PARAMS ((PTR));
141 static bfd_boolean pr_end_class_type
142 PARAMS ((PTR));
143 static bfd_boolean pr_typedef_type
144 PARAMS ((PTR, const char *));
145 static bfd_boolean pr_tag_type
146 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
147 static bfd_boolean pr_typdef
148 PARAMS ((PTR, const char *));
149 static bfd_boolean pr_tag
150 PARAMS ((PTR, const char *));
151 static bfd_boolean pr_int_constant
152 PARAMS ((PTR, const char *, bfd_vma));
153 static bfd_boolean pr_float_constant
154 PARAMS ((PTR, const char *, double));
155 static bfd_boolean pr_typed_constant
156 PARAMS ((PTR, const char *, bfd_vma));
157 static bfd_boolean pr_variable
158 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
159 static bfd_boolean pr_start_function
160 PARAMS ((PTR, const char *, bfd_boolean));
161 static bfd_boolean pr_function_parameter
162 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
163 static bfd_boolean pr_start_block
164 PARAMS ((PTR, bfd_vma));
165 static bfd_boolean pr_end_block
166 PARAMS ((PTR, bfd_vma));
167 static bfd_boolean pr_end_function
168 PARAMS ((PTR));
169 static bfd_boolean pr_lineno
170 PARAMS ((PTR, const char *, unsigned long, bfd_vma));
172 static const struct debug_write_fns pr_fns =
174 pr_start_compilation_unit,
175 pr_start_source,
176 pr_empty_type,
177 pr_void_type,
178 pr_int_type,
179 pr_float_type,
180 pr_complex_type,
181 pr_bool_type,
182 pr_enum_type,
183 pr_pointer_type,
184 pr_function_type,
185 pr_reference_type,
186 pr_range_type,
187 pr_array_type,
188 pr_set_type,
189 pr_offset_type,
190 pr_method_type,
191 pr_const_type,
192 pr_volatile_type,
193 pr_start_struct_type,
194 pr_struct_field,
195 pr_end_struct_type,
196 pr_start_class_type,
197 pr_class_static_member,
198 pr_class_baseclass,
199 pr_class_start_method,
200 pr_class_method_variant,
201 pr_class_static_method_variant,
202 pr_class_end_method,
203 pr_end_class_type,
204 pr_typedef_type,
205 pr_tag_type,
206 pr_typdef,
207 pr_tag,
208 pr_int_constant,
209 pr_float_constant,
210 pr_typed_constant,
211 pr_variable,
212 pr_start_function,
213 pr_function_parameter,
214 pr_start_block,
215 pr_end_block,
216 pr_end_function,
217 pr_lineno
220 /* Print out the generic debugging information recorded in dhandle. */
222 bfd_boolean
223 print_debugging_info (f, dhandle)
224 FILE *f;
225 PTR dhandle;
227 struct pr_handle info;
229 info.f = f;
230 info.indent = 0;
231 info.stack = NULL;
232 info.parameter = 0;
234 return debug_write (dhandle, &pr_fns, (PTR) &info);
237 /* Indent to the current indentation level. */
239 static void
240 indent (info)
241 struct pr_handle *info;
243 unsigned int i;
245 for (i = 0; i < info->indent; i++)
246 putc (' ', info->f);
249 /* Push a type on the type stack. */
251 static bfd_boolean
252 push_type (info, type)
253 struct pr_handle *info;
254 const char *type;
256 struct pr_stack *n;
258 if (type == NULL)
259 return FALSE;
261 n = (struct pr_stack *) xmalloc (sizeof *n);
262 memset (n, 0, sizeof *n);
264 n->type = xstrdup (type);
265 n->visibility = DEBUG_VISIBILITY_IGNORE;
266 n->method = NULL;
267 n->next = info->stack;
268 info->stack = n;
270 return TRUE;
273 /* Prepend a string onto the type on the top of the type stack. */
275 static bfd_boolean
276 prepend_type (info, s)
277 struct pr_handle *info;
278 const char *s;
280 char *n;
282 assert (info->stack != NULL);
284 n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
285 sprintf (n, "%s%s", s, info->stack->type);
286 free (info->stack->type);
287 info->stack->type = n;
289 return TRUE;
292 /* Append a string to the type on the top of the type stack. */
294 static bfd_boolean
295 append_type (info, s)
296 struct pr_handle *info;
297 const char *s;
299 unsigned int len;
301 if (s == NULL)
302 return FALSE;
304 assert (info->stack != NULL);
306 len = strlen (info->stack->type);
307 info->stack->type = (char *) xrealloc (info->stack->type,
308 len + strlen (s) + 1);
309 strcpy (info->stack->type + len, s);
311 return TRUE;
314 /* We use an underscore to indicate where the name should go in a type
315 string. This function substitutes a string for the underscore. If
316 there is no underscore, the name follows the type. */
318 static bfd_boolean
319 substitute_type (info, s)
320 struct pr_handle *info;
321 const char *s;
323 char *u;
325 assert (info->stack != NULL);
327 u = strchr (info->stack->type, '|');
328 if (u != NULL)
330 char *n;
332 n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
334 memcpy (n, info->stack->type, u - info->stack->type);
335 strcpy (n + (u - info->stack->type), s);
336 strcat (n, u + 1);
338 free (info->stack->type);
339 info->stack->type = n;
341 return TRUE;
344 if (strchr (s, '|') != NULL
345 && (strchr (info->stack->type, '{') != NULL
346 || strchr (info->stack->type, '(') != NULL))
348 if (! prepend_type (info, "(")
349 || ! append_type (info, ")"))
350 return FALSE;
353 if (*s == '\0')
354 return TRUE;
356 return (append_type (info, " ")
357 && append_type (info, s));
360 /* Indent the type at the top of the stack by appending spaces. */
362 static bfd_boolean
363 indent_type (info)
364 struct pr_handle *info;
366 unsigned int i;
368 for (i = 0; i < info->indent; i++)
370 if (! append_type (info, " "))
371 return FALSE;
374 return TRUE;
377 /* Pop a type from the type stack. */
379 static char *
380 pop_type (info)
381 struct pr_handle *info;
383 struct pr_stack *o;
384 char *ret;
386 assert (info->stack != NULL);
388 o = info->stack;
389 info->stack = o->next;
390 ret = o->type;
391 free (o);
393 return ret;
396 /* Print a VMA value into a string. */
398 static void
399 print_vma (vma, buf, unsignedp, hexp)
400 bfd_vma vma;
401 char *buf;
402 bfd_boolean unsignedp;
403 bfd_boolean hexp;
405 if (sizeof (vma) <= sizeof (unsigned long))
407 if (hexp)
408 sprintf (buf, "0x%lx", (unsigned long) vma);
409 else if (unsignedp)
410 sprintf (buf, "%lu", (unsigned long) vma);
411 else
412 sprintf (buf, "%ld", (long) vma);
414 else
416 buf[0] = '0';
417 buf[1] = 'x';
418 sprintf_vma (buf + 2, vma);
422 /* Start a new compilation unit. */
424 static bfd_boolean
425 pr_start_compilation_unit (p, filename)
426 PTR p;
427 const char *filename;
429 struct pr_handle *info = (struct pr_handle *) p;
431 assert (info->indent == 0);
433 fprintf (info->f, "%s:\n", filename);
435 return TRUE;
438 /* Start a source file within a compilation unit. */
440 static bfd_boolean
441 pr_start_source (p, filename)
442 PTR p;
443 const char *filename;
445 struct pr_handle *info = (struct pr_handle *) p;
447 assert (info->indent == 0);
449 fprintf (info->f, " %s:\n", filename);
451 return TRUE;
454 /* Push an empty type onto the type stack. */
456 static bfd_boolean
457 pr_empty_type (p)
458 PTR p;
460 struct pr_handle *info = (struct pr_handle *) p;
462 return push_type (info, "<undefined>");
465 /* Push a void type onto the type stack. */
467 static bfd_boolean
468 pr_void_type (p)
469 PTR p;
471 struct pr_handle *info = (struct pr_handle *) p;
473 return push_type (info, "void");
476 /* Push an integer type onto the type stack. */
478 static bfd_boolean
479 pr_int_type (p, size, unsignedp)
480 PTR p;
481 unsigned int size;
482 bfd_boolean unsignedp;
484 struct pr_handle *info = (struct pr_handle *) p;
485 char ab[10];
487 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
488 return push_type (info, ab);
491 /* Push a floating type onto the type stack. */
493 static bfd_boolean
494 pr_float_type (p, size)
495 PTR p;
496 unsigned int size;
498 struct pr_handle *info = (struct pr_handle *) p;
499 char ab[10];
501 if (size == 4)
502 return push_type (info, "float");
503 else if (size == 8)
504 return push_type (info, "double");
506 sprintf (ab, "float%d", size * 8);
507 return push_type (info, ab);
510 /* Push a complex type onto the type stack. */
512 static bfd_boolean
513 pr_complex_type (p, size)
514 PTR p;
515 unsigned int size;
517 struct pr_handle *info = (struct pr_handle *) p;
519 if (! pr_float_type (p, size))
520 return FALSE;
522 return prepend_type (info, "complex ");
525 /* Push a bfd_boolean type onto the type stack. */
527 static bfd_boolean
528 pr_bool_type (p, size)
529 PTR p;
530 unsigned int size;
532 struct pr_handle *info = (struct pr_handle *) p;
533 char ab[10];
535 sprintf (ab, "bool%d", size * 8);
537 return push_type (info, ab);
540 /* Push an enum type onto the type stack. */
542 static bfd_boolean
543 pr_enum_type (p, tag, names, values)
544 PTR p;
545 const char *tag;
546 const char **names;
547 bfd_signed_vma *values;
549 struct pr_handle *info = (struct pr_handle *) p;
550 unsigned int i;
551 bfd_signed_vma val;
553 if (! push_type (info, "enum "))
554 return FALSE;
555 if (tag != NULL)
557 if (! append_type (info, tag)
558 || ! append_type (info, " "))
559 return FALSE;
561 if (! append_type (info, "{ "))
562 return FALSE;
564 if (names == NULL)
566 if (! append_type (info, "/* undefined */"))
567 return FALSE;
569 else
571 val = 0;
572 for (i = 0; names[i] != NULL; i++)
574 if (i > 0)
576 if (! append_type (info, ", "))
577 return FALSE;
580 if (! append_type (info, names[i]))
581 return FALSE;
583 if (values[i] != val)
585 char ab[20];
587 print_vma (values[i], ab, FALSE, FALSE);
588 if (! append_type (info, " = ")
589 || ! append_type (info, ab))
590 return FALSE;
591 val = values[i];
594 ++val;
598 return append_type (info, " }");
601 /* Turn the top type on the stack into a pointer. */
603 static bfd_boolean
604 pr_pointer_type (p)
605 PTR p;
607 struct pr_handle *info = (struct pr_handle *) p;
608 char *s;
610 assert (info->stack != NULL);
612 s = strchr (info->stack->type, '|');
613 if (s != NULL && s[1] == '[')
614 return substitute_type (info, "(*|)");
615 return substitute_type (info, "*|");
618 /* Turn the top type on the stack into a function returning that type. */
620 static bfd_boolean
621 pr_function_type (p, argcount, varargs)
622 PTR p;
623 int argcount;
624 bfd_boolean varargs;
626 struct pr_handle *info = (struct pr_handle *) p;
627 char **arg_types;
628 unsigned int len;
629 char *s;
631 assert (info->stack != NULL);
633 len = 10;
635 if (argcount <= 0)
637 arg_types = NULL;
638 len += 15;
640 else
642 int i;
644 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
645 for (i = argcount - 1; i >= 0; i--)
647 if (! substitute_type (info, ""))
648 return FALSE;
649 arg_types[i] = pop_type (info);
650 if (arg_types[i] == NULL)
651 return FALSE;
652 len += strlen (arg_types[i]) + 2;
654 if (varargs)
655 len += 5;
658 /* Now the return type is on the top of the stack. */
660 s = (char *) xmalloc (len);
661 strcpy (s, "(|) (");
663 if (argcount < 0)
664 strcat (s, "/* unknown */");
665 else
667 int i;
669 for (i = 0; i < argcount; i++)
671 if (i > 0)
672 strcat (s, ", ");
673 strcat (s, arg_types[i]);
675 if (varargs)
677 if (i > 0)
678 strcat (s, ", ");
679 strcat (s, "...");
681 if (argcount > 0)
682 free (arg_types);
685 strcat (s, ")");
687 if (! substitute_type (info, s))
688 return FALSE;
690 free (s);
692 return TRUE;
695 /* Turn the top type on the stack into a reference to that type. */
697 static bfd_boolean
698 pr_reference_type (p)
699 PTR p;
701 struct pr_handle *info = (struct pr_handle *) p;
703 assert (info->stack != NULL);
705 return substitute_type (info, "&|");
708 /* Make a range type. */
710 static bfd_boolean
711 pr_range_type (p, lower, upper)
712 PTR p;
713 bfd_signed_vma lower;
714 bfd_signed_vma upper;
716 struct pr_handle *info = (struct pr_handle *) p;
717 char abl[20], abu[20];
719 assert (info->stack != NULL);
721 if (! substitute_type (info, ""))
722 return FALSE;
724 print_vma (lower, abl, FALSE, FALSE);
725 print_vma (upper, abu, FALSE, FALSE);
727 return (prepend_type (info, "range (")
728 && append_type (info, "):")
729 && append_type (info, abl)
730 && append_type (info, ":")
731 && append_type (info, abu));
734 /* Make an array type. */
736 static bfd_boolean
737 pr_array_type (p, lower, upper, stringp)
738 PTR p;
739 bfd_signed_vma lower;
740 bfd_signed_vma upper;
741 bfd_boolean stringp;
743 struct pr_handle *info = (struct pr_handle *) p;
744 char *range_type;
745 char abl[20], abu[20], ab[50];
747 range_type = pop_type (info);
748 if (range_type == NULL)
749 return FALSE;
751 if (lower == 0)
753 if (upper == -1)
754 sprintf (ab, "|[]");
755 else
757 print_vma (upper + 1, abu, FALSE, FALSE);
758 sprintf (ab, "|[%s]", abu);
761 else
763 print_vma (lower, abl, FALSE, FALSE);
764 print_vma (upper, abu, FALSE, FALSE);
765 sprintf (ab, "|[%s:%s]", abl, abu);
768 if (! substitute_type (info, ab))
769 return FALSE;
771 if (strcmp (range_type, "int") != 0)
773 if (! append_type (info, ":")
774 || ! append_type (info, range_type))
775 return FALSE;
778 if (stringp)
780 if (! append_type (info, " /* string */"))
781 return FALSE;
784 return TRUE;
787 /* Make a set type. */
789 static bfd_boolean
790 pr_set_type (p, bitstringp)
791 PTR p;
792 bfd_boolean bitstringp;
794 struct pr_handle *info = (struct pr_handle *) p;
796 if (! substitute_type (info, ""))
797 return FALSE;
799 if (! prepend_type (info, "set { ")
800 || ! append_type (info, " }"))
801 return FALSE;
803 if (bitstringp)
805 if (! append_type (info, "/* bitstring */"))
806 return FALSE;
809 return TRUE;
812 /* Make an offset type. */
814 static bfd_boolean
815 pr_offset_type (p)
816 PTR p;
818 struct pr_handle *info = (struct pr_handle *) p;
819 char *t;
821 if (! substitute_type (info, ""))
822 return FALSE;
824 t = pop_type (info);
825 if (t == NULL)
826 return FALSE;
828 return (substitute_type (info, "")
829 && prepend_type (info, " ")
830 && prepend_type (info, t)
831 && append_type (info, "::|"));
834 /* Make a method type. */
836 static bfd_boolean
837 pr_method_type (p, domain, argcount, varargs)
838 PTR p;
839 bfd_boolean domain;
840 int argcount;
841 bfd_boolean varargs;
843 struct pr_handle *info = (struct pr_handle *) p;
844 unsigned int len;
845 char *domain_type;
846 char **arg_types;
847 char *s;
849 len = 10;
851 if (! domain)
852 domain_type = NULL;
853 else
855 if (! substitute_type (info, ""))
856 return FALSE;
857 domain_type = pop_type (info);
858 if (domain_type == NULL)
859 return FALSE;
860 if (strncmp (domain_type, "class ", sizeof "class " - 1) == 0
861 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
862 domain_type += sizeof "class " - 1;
863 else if (strncmp (domain_type, "union class ",
864 sizeof "union class ") == 0
865 && (strchr (domain_type + sizeof "union class " - 1, ' ')
866 == NULL))
867 domain_type += sizeof "union class " - 1;
868 len += strlen (domain_type);
871 if (argcount <= 0)
873 arg_types = NULL;
874 len += 15;
876 else
878 int i;
880 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
881 for (i = argcount - 1; i >= 0; i--)
883 if (! substitute_type (info, ""))
884 return FALSE;
885 arg_types[i] = pop_type (info);
886 if (arg_types[i] == NULL)
887 return FALSE;
888 len += strlen (arg_types[i]) + 2;
890 if (varargs)
891 len += 5;
894 /* Now the return type is on the top of the stack. */
896 s = (char *) xmalloc (len);
897 if (! domain)
898 *s = '\0';
899 else
900 strcpy (s, domain_type);
901 strcat (s, "::| (");
903 if (argcount < 0)
904 strcat (s, "/* unknown */");
905 else
907 int i;
909 for (i = 0; i < argcount; i++)
911 if (i > 0)
912 strcat (s, ", ");
913 strcat (s, arg_types[i]);
915 if (varargs)
917 if (i > 0)
918 strcat (s, ", ");
919 strcat (s, "...");
921 if (argcount > 0)
922 free (arg_types);
925 strcat (s, ")");
927 if (! substitute_type (info, s))
928 return FALSE;
930 free (s);
932 return TRUE;
935 /* Make a const qualified type. */
937 static bfd_boolean
938 pr_const_type (p)
939 PTR p;
941 struct pr_handle *info = (struct pr_handle *) p;
943 return substitute_type (info, "const |");
946 /* Make a volatile qualified type. */
948 static bfd_boolean
949 pr_volatile_type (p)
950 PTR p;
952 struct pr_handle *info = (struct pr_handle *) p;
954 return substitute_type (info, "volatile |");
957 /* Start accumulating a struct type. */
959 static bfd_boolean
960 pr_start_struct_type (p, tag, id, structp, size)
961 PTR p;
962 const char *tag;
963 unsigned int id;
964 bfd_boolean structp;
965 unsigned int size;
967 struct pr_handle *info = (struct pr_handle *) p;
969 info->indent += 2;
971 if (! push_type (info, structp ? "struct " : "union "))
972 return FALSE;
973 if (tag != NULL)
975 if (! append_type (info, tag))
976 return FALSE;
978 else
980 char idbuf[20];
982 sprintf (idbuf, "%%anon%u", id);
983 if (! append_type (info, idbuf))
984 return FALSE;
987 if (! append_type (info, " {"))
988 return FALSE;
989 if (size != 0 || tag != NULL)
991 char ab[30];
993 if (! append_type (info, " /*"))
994 return FALSE;
996 if (size != 0)
998 sprintf (ab, " size %u", size);
999 if (! append_type (info, ab))
1000 return FALSE;
1002 if (tag != NULL)
1004 sprintf (ab, " id %u", id);
1005 if (! append_type (info, ab))
1006 return FALSE;
1008 if (! append_type (info, " */"))
1009 return FALSE;
1011 if (! append_type (info, "\n"))
1012 return FALSE;
1014 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1016 return indent_type (info);
1019 /* Output the visibility of a field in a struct. */
1021 static bfd_boolean
1022 pr_fix_visibility (info, visibility)
1023 struct pr_handle *info;
1024 enum debug_visibility visibility;
1026 const char *s = NULL;
1027 char *t;
1028 unsigned int len;
1030 assert (info->stack != NULL);
1032 if (info->stack->visibility == visibility)
1033 return TRUE;
1035 switch (visibility)
1037 case DEBUG_VISIBILITY_PUBLIC:
1038 s = "public";
1039 break;
1040 case DEBUG_VISIBILITY_PRIVATE:
1041 s = "private";
1042 break;
1043 case DEBUG_VISIBILITY_PROTECTED:
1044 s = "protected";
1045 break;
1046 case DEBUG_VISIBILITY_IGNORE:
1047 s = "/* ignore */";
1048 break;
1049 default:
1050 abort ();
1051 return FALSE;
1054 /* Trim off a trailing space in the struct string, to make the
1055 output look a bit better, then stick on the visibility string. */
1057 t = info->stack->type;
1058 len = strlen (t);
1059 assert (t[len - 1] == ' ');
1060 t[len - 1] = '\0';
1062 if (! append_type (info, s)
1063 || ! append_type (info, ":\n")
1064 || ! indent_type (info))
1065 return FALSE;
1067 info->stack->visibility = visibility;
1069 return TRUE;
1072 /* Add a field to a struct type. */
1074 static bfd_boolean
1075 pr_struct_field (p, name, bitpos, bitsize, visibility)
1076 PTR p;
1077 const char *name;
1078 bfd_vma bitpos;
1079 bfd_vma bitsize;
1080 enum debug_visibility visibility;
1082 struct pr_handle *info = (struct pr_handle *) p;
1083 char ab[20];
1084 char *t;
1086 if (! substitute_type (info, name))
1087 return FALSE;
1089 if (! append_type (info, "; /* "))
1090 return FALSE;
1092 if (bitsize != 0)
1094 print_vma (bitsize, ab, TRUE, FALSE);
1095 if (! append_type (info, "bitsize ")
1096 || ! append_type (info, ab)
1097 || ! append_type (info, ", "))
1098 return FALSE;
1101 print_vma (bitpos, ab, TRUE, FALSE);
1102 if (! append_type (info, "bitpos ")
1103 || ! append_type (info, ab)
1104 || ! append_type (info, " */\n")
1105 || ! indent_type (info))
1106 return FALSE;
1108 t = pop_type (info);
1109 if (t == NULL)
1110 return FALSE;
1112 if (! pr_fix_visibility (info, visibility))
1113 return FALSE;
1115 return append_type (info, t);
1118 /* Finish a struct type. */
1120 static bfd_boolean
1121 pr_end_struct_type (p)
1122 PTR p;
1124 struct pr_handle *info = (struct pr_handle *) p;
1125 char *s;
1127 assert (info->stack != NULL);
1128 assert (info->indent >= 2);
1130 info->indent -= 2;
1132 /* Change the trailing indentation to have a close brace. */
1133 s = info->stack->type + strlen (info->stack->type) - 2;
1134 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1136 *s++ = '}';
1137 *s = '\0';
1139 return TRUE;
1142 /* Start a class type. */
1144 static bfd_boolean
1145 pr_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1146 PTR p;
1147 const char *tag;
1148 unsigned int id;
1149 bfd_boolean structp;
1150 unsigned int size;
1151 bfd_boolean vptr;
1152 bfd_boolean ownvptr;
1154 struct pr_handle *info = (struct pr_handle *) p;
1155 char *tv = NULL;
1157 info->indent += 2;
1159 if (vptr && ! ownvptr)
1161 tv = pop_type (info);
1162 if (tv == NULL)
1163 return FALSE;
1166 if (! push_type (info, structp ? "class " : "union class "))
1167 return FALSE;
1168 if (tag != NULL)
1170 if (! append_type (info, tag))
1171 return FALSE;
1173 else
1175 char idbuf[20];
1177 sprintf (idbuf, "%%anon%u", id);
1178 if (! append_type (info, idbuf))
1179 return FALSE;
1182 if (! append_type (info, " {"))
1183 return FALSE;
1184 if (size != 0 || vptr || ownvptr || tag != NULL)
1186 if (! append_type (info, " /*"))
1187 return FALSE;
1189 if (size != 0)
1191 char ab[20];
1193 sprintf (ab, "%u", size);
1194 if (! append_type (info, " size ")
1195 || ! append_type (info, ab))
1196 return FALSE;
1199 if (vptr)
1201 if (! append_type (info, " vtable "))
1202 return FALSE;
1203 if (ownvptr)
1205 if (! append_type (info, "self "))
1206 return FALSE;
1208 else
1210 if (! append_type (info, tv)
1211 || ! append_type (info, " "))
1212 return FALSE;
1216 if (tag != NULL)
1218 char ab[30];
1220 sprintf (ab, " id %u", id);
1221 if (! append_type (info, ab))
1222 return FALSE;
1225 if (! append_type (info, " */"))
1226 return FALSE;
1229 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1231 return (append_type (info, "\n")
1232 && indent_type (info));
1235 /* Add a static member to a class. */
1237 static bfd_boolean
1238 pr_class_static_member (p, name, physname, visibility)
1239 PTR p;
1240 const char *name;
1241 const char *physname;
1242 enum debug_visibility visibility;
1244 struct pr_handle *info = (struct pr_handle *) p;
1245 char *t;
1247 if (! substitute_type (info, name))
1248 return FALSE;
1250 if (! prepend_type (info, "static ")
1251 || ! append_type (info, "; /* ")
1252 || ! append_type (info, physname)
1253 || ! append_type (info, " */\n")
1254 || ! indent_type (info))
1255 return FALSE;
1257 t = pop_type (info);
1258 if (t == NULL)
1259 return FALSE;
1261 if (! pr_fix_visibility (info, visibility))
1262 return FALSE;
1264 return append_type (info, t);
1267 /* Add a base class to a class. */
1269 static bfd_boolean
1270 pr_class_baseclass (p, bitpos, virtual, visibility)
1271 PTR p;
1272 bfd_vma bitpos;
1273 bfd_boolean virtual;
1274 enum debug_visibility visibility;
1276 struct pr_handle *info = (struct pr_handle *) p;
1277 char *t;
1278 const char *prefix;
1279 char ab[20];
1280 char *s, *l, *n;
1282 assert (info->stack != NULL && info->stack->next != NULL);
1284 if (! substitute_type (info, ""))
1285 return FALSE;
1287 t = pop_type (info);
1288 if (t == NULL)
1289 return FALSE;
1291 if (strncmp (t, "class ", sizeof "class " - 1) == 0)
1292 t += sizeof "class " - 1;
1294 /* Push it back on to take advantage of the prepend_type and
1295 append_type routines. */
1296 if (! push_type (info, t))
1297 return FALSE;
1299 if (virtual)
1301 if (! prepend_type (info, "virtual "))
1302 return FALSE;
1305 switch (visibility)
1307 case DEBUG_VISIBILITY_PUBLIC:
1308 prefix = "public ";
1309 break;
1310 case DEBUG_VISIBILITY_PROTECTED:
1311 prefix = "protected ";
1312 break;
1313 case DEBUG_VISIBILITY_PRIVATE:
1314 prefix = "private ";
1315 break;
1316 default:
1317 prefix = "/* unknown visibility */ ";
1318 break;
1321 if (! prepend_type (info, prefix))
1322 return FALSE;
1324 if (bitpos != 0)
1326 print_vma (bitpos, ab, TRUE, FALSE);
1327 if (! append_type (info, " /* bitpos ")
1328 || ! append_type (info, ab)
1329 || ! append_type (info, " */"))
1330 return FALSE;
1333 /* Now the top of the stack is something like "public A / * bitpos
1334 10 * /". The next element on the stack is something like "class
1335 xx { / * size 8 * /\n...". We want to substitute the top of the
1336 stack in before the {. */
1337 s = strchr (info->stack->next->type, '{');
1338 assert (s != NULL);
1339 --s;
1341 /* If there is already a ':', then we already have a baseclass, and
1342 we must append this one after a comma. */
1343 for (l = info->stack->next->type; l != s; l++)
1344 if (*l == ':')
1345 break;
1346 if (! prepend_type (info, l == s ? " : " : ", "))
1347 return FALSE;
1349 t = pop_type (info);
1350 if (t == NULL)
1351 return FALSE;
1353 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1354 memcpy (n, info->stack->type, s - info->stack->type);
1355 strcpy (n + (s - info->stack->type), t);
1356 strcat (n, s);
1358 free (info->stack->type);
1359 info->stack->type = n;
1361 free (t);
1363 return TRUE;
1366 /* Start adding a method to a class. */
1368 static bfd_boolean
1369 pr_class_start_method (p, name)
1370 PTR p;
1371 const char *name;
1373 struct pr_handle *info = (struct pr_handle *) p;
1375 assert (info->stack != NULL);
1376 info->stack->method = name;
1377 return TRUE;
1380 /* Add a variant to a method. */
1382 static bfd_boolean
1383 pr_class_method_variant (p, physname, visibility, constp, volatilep, voffset,
1384 context)
1385 PTR p;
1386 const char *physname;
1387 enum debug_visibility visibility;
1388 bfd_boolean constp;
1389 bfd_boolean volatilep;
1390 bfd_vma voffset;
1391 bfd_boolean context;
1393 struct pr_handle *info = (struct pr_handle *) p;
1394 char *method_type;
1395 char *context_type;
1397 assert (info->stack != NULL);
1398 assert (info->stack->next != NULL);
1400 /* Put the const and volatile qualifiers on the type. */
1401 if (volatilep)
1403 if (! append_type (info, " volatile"))
1404 return FALSE;
1406 if (constp)
1408 if (! append_type (info, " const"))
1409 return FALSE;
1412 /* Stick the name of the method into its type. */
1413 if (! substitute_type (info,
1414 (context
1415 ? info->stack->next->next->method
1416 : info->stack->next->method)))
1417 return FALSE;
1419 /* Get the type. */
1420 method_type = pop_type (info);
1421 if (method_type == NULL)
1422 return FALSE;
1424 /* Pull off the context type if there is one. */
1425 if (! context)
1426 context_type = NULL;
1427 else
1429 context_type = pop_type (info);
1430 if (context_type == NULL)
1431 return FALSE;
1434 /* Now the top of the stack is the class. */
1436 if (! pr_fix_visibility (info, visibility))
1437 return FALSE;
1439 if (! append_type (info, method_type)
1440 || ! append_type (info, " /* ")
1441 || ! append_type (info, physname)
1442 || ! append_type (info, " "))
1443 return FALSE;
1444 if (context || voffset != 0)
1446 char ab[20];
1448 if (context)
1450 if (! append_type (info, "context ")
1451 || ! append_type (info, context_type)
1452 || ! append_type (info, " "))
1453 return FALSE;
1455 print_vma (voffset, ab, TRUE, FALSE);
1456 if (! append_type (info, "voffset ")
1457 || ! append_type (info, ab))
1458 return FALSE;
1461 return (append_type (info, " */;\n")
1462 && indent_type (info));
1465 /* Add a static variant to a method. */
1467 static bfd_boolean
1468 pr_class_static_method_variant (p, physname, visibility, constp, volatilep)
1469 PTR p;
1470 const char *physname;
1471 enum debug_visibility visibility;
1472 bfd_boolean constp;
1473 bfd_boolean volatilep;
1475 struct pr_handle *info = (struct pr_handle *) p;
1476 char *method_type;
1478 assert (info->stack != NULL);
1479 assert (info->stack->next != NULL);
1480 assert (info->stack->next->method != NULL);
1482 /* Put the const and volatile qualifiers on the type. */
1483 if (volatilep)
1485 if (! append_type (info, " volatile"))
1486 return FALSE;
1488 if (constp)
1490 if (! append_type (info, " const"))
1491 return FALSE;
1494 /* Mark it as static. */
1495 if (! prepend_type (info, "static "))
1496 return FALSE;
1498 /* Stick the name of the method into its type. */
1499 if (! substitute_type (info, info->stack->next->method))
1500 return FALSE;
1502 /* Get the type. */
1503 method_type = pop_type (info);
1504 if (method_type == NULL)
1505 return FALSE;
1507 /* Now the top of the stack is the class. */
1509 if (! pr_fix_visibility (info, visibility))
1510 return FALSE;
1512 return (append_type (info, method_type)
1513 && append_type (info, " /* ")
1514 && append_type (info, physname)
1515 && append_type (info, " */;\n")
1516 && indent_type (info));
1519 /* Finish up a method. */
1521 static bfd_boolean
1522 pr_class_end_method (p)
1523 PTR p;
1525 struct pr_handle *info = (struct pr_handle *) p;
1527 info->stack->method = NULL;
1528 return TRUE;
1531 /* Finish up a class. */
1533 static bfd_boolean
1534 pr_end_class_type (p)
1535 PTR p;
1537 return pr_end_struct_type (p);
1540 /* Push a type on the stack using a typedef name. */
1542 static bfd_boolean
1543 pr_typedef_type (p, name)
1544 PTR p;
1545 const char *name;
1547 struct pr_handle *info = (struct pr_handle *) p;
1549 return push_type (info, name);
1552 /* Push a type on the stack using a tag name. */
1554 static bfd_boolean
1555 pr_tag_type (p, name, id, kind)
1556 PTR p;
1557 const char *name;
1558 unsigned int id;
1559 enum debug_type_kind kind;
1561 struct pr_handle *info = (struct pr_handle *) p;
1562 const char *t, *tag;
1563 char idbuf[20];
1565 switch (kind)
1567 case DEBUG_KIND_STRUCT:
1568 t = "struct ";
1569 break;
1570 case DEBUG_KIND_UNION:
1571 t = "union ";
1572 break;
1573 case DEBUG_KIND_ENUM:
1574 t = "enum ";
1575 break;
1576 case DEBUG_KIND_CLASS:
1577 t = "class ";
1578 break;
1579 case DEBUG_KIND_UNION_CLASS:
1580 t = "union class ";
1581 break;
1582 default:
1583 abort ();
1584 return FALSE;
1587 if (! push_type (info, t))
1588 return FALSE;
1589 if (name != NULL)
1590 tag = name;
1591 else
1593 sprintf (idbuf, "%%anon%u", id);
1594 tag = idbuf;
1597 if (! append_type (info, tag))
1598 return FALSE;
1599 if (name != NULL && kind != DEBUG_KIND_ENUM)
1601 sprintf (idbuf, " /* id %u */", id);
1602 if (! append_type (info, idbuf))
1603 return FALSE;
1606 return TRUE;
1609 /* Output a typedef. */
1611 static bfd_boolean
1612 pr_typdef (p, name)
1613 PTR p;
1614 const char *name;
1616 struct pr_handle *info = (struct pr_handle *) p;
1617 char *s;
1619 if (! substitute_type (info, name))
1620 return FALSE;
1622 s = pop_type (info);
1623 if (s == NULL)
1624 return FALSE;
1626 indent (info);
1627 fprintf (info->f, "typedef %s;\n", s);
1629 free (s);
1631 return TRUE;
1634 /* Output a tag. The tag should already be in the string on the
1635 stack, so all we have to do here is print it out. */
1637 static bfd_boolean
1638 pr_tag (p, name)
1639 PTR p;
1640 const char *name ATTRIBUTE_UNUSED;
1642 struct pr_handle *info = (struct pr_handle *) p;
1643 char *t;
1645 t = pop_type (info);
1646 if (t == NULL)
1647 return FALSE;
1649 indent (info);
1650 fprintf (info->f, "%s;\n", t);
1652 free (t);
1654 return TRUE;
1657 /* Output an integer constant. */
1659 static bfd_boolean
1660 pr_int_constant (p, name, val)
1661 PTR p;
1662 const char *name;
1663 bfd_vma val;
1665 struct pr_handle *info = (struct pr_handle *) p;
1666 char ab[20];
1668 indent (info);
1669 print_vma (val, ab, FALSE, FALSE);
1670 fprintf (info->f, "const int %s = %s;\n", name, ab);
1671 return TRUE;
1674 /* Output a floating point constant. */
1676 static bfd_boolean
1677 pr_float_constant (p, name, val)
1678 PTR p;
1679 const char *name;
1680 double val;
1682 struct pr_handle *info = (struct pr_handle *) p;
1684 indent (info);
1685 fprintf (info->f, "const double %s = %g;\n", name, val);
1686 return TRUE;
1689 /* Output a typed constant. */
1691 static bfd_boolean
1692 pr_typed_constant (p, name, val)
1693 PTR p;
1694 const char *name;
1695 bfd_vma val;
1697 struct pr_handle *info = (struct pr_handle *) p;
1698 char *t;
1699 char ab[20];
1701 t = pop_type (info);
1702 if (t == NULL)
1703 return FALSE;
1705 indent (info);
1706 print_vma (val, ab, FALSE, FALSE);
1707 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1709 free (t);
1711 return TRUE;
1714 /* Output a variable. */
1716 static bfd_boolean
1717 pr_variable (p, name, kind, val)
1718 PTR p;
1719 const char *name;
1720 enum debug_var_kind kind;
1721 bfd_vma val;
1723 struct pr_handle *info = (struct pr_handle *) p;
1724 char *t;
1725 char ab[20];
1727 if (! substitute_type (info, name))
1728 return FALSE;
1730 t = pop_type (info);
1731 if (t == NULL)
1732 return FALSE;
1734 indent (info);
1735 switch (kind)
1737 case DEBUG_STATIC:
1738 case DEBUG_LOCAL_STATIC:
1739 fprintf (info->f, "static ");
1740 break;
1741 case DEBUG_REGISTER:
1742 fprintf (info->f, "register ");
1743 break;
1744 default:
1745 break;
1747 print_vma (val, ab, TRUE, TRUE);
1748 fprintf (info->f, "%s /* %s */;\n", t, ab);
1750 free (t);
1752 return TRUE;
1755 /* Start outputting a function. */
1757 static bfd_boolean
1758 pr_start_function (p, name, global)
1759 PTR p;
1760 const char *name;
1761 bfd_boolean global;
1763 struct pr_handle *info = (struct pr_handle *) p;
1764 char *t;
1766 if (! substitute_type (info, name))
1767 return FALSE;
1769 t = pop_type (info);
1770 if (t == NULL)
1771 return FALSE;
1773 indent (info);
1774 if (! global)
1775 fprintf (info->f, "static ");
1776 fprintf (info->f, "%s (", t);
1778 info->parameter = 1;
1780 return TRUE;
1783 /* Output a function parameter. */
1785 static bfd_boolean
1786 pr_function_parameter (p, name, kind, val)
1787 PTR p;
1788 const char *name;
1789 enum debug_parm_kind kind;
1790 bfd_vma val;
1792 struct pr_handle *info = (struct pr_handle *) p;
1793 char *t;
1794 char ab[20];
1796 if (kind == DEBUG_PARM_REFERENCE
1797 || kind == DEBUG_PARM_REF_REG)
1799 if (! pr_reference_type (p))
1800 return FALSE;
1803 if (! substitute_type (info, name))
1804 return FALSE;
1806 t = pop_type (info);
1807 if (t == NULL)
1808 return FALSE;
1810 if (info->parameter != 1)
1811 fprintf (info->f, ", ");
1813 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1814 fprintf (info->f, "register ");
1816 print_vma (val, ab, TRUE, TRUE);
1817 fprintf (info->f, "%s /* %s */", t, ab);
1819 free (t);
1821 ++info->parameter;
1823 return TRUE;
1826 /* Start writing out a block. */
1828 static bfd_boolean
1829 pr_start_block (p, addr)
1830 PTR p;
1831 bfd_vma addr;
1833 struct pr_handle *info = (struct pr_handle *) p;
1834 char ab[20];
1836 if (info->parameter > 0)
1838 fprintf (info->f, ")\n");
1839 info->parameter = 0;
1842 indent (info);
1843 print_vma (addr, ab, TRUE, TRUE);
1844 fprintf (info->f, "{ /* %s */\n", ab);
1846 info->indent += 2;
1848 return TRUE;
1851 /* Write out line number information. */
1853 static bfd_boolean
1854 pr_lineno (p, filename, lineno, addr)
1855 PTR p;
1856 const char *filename;
1857 unsigned long lineno;
1858 bfd_vma addr;
1860 struct pr_handle *info = (struct pr_handle *) p;
1861 char ab[20];
1863 indent (info);
1864 print_vma (addr, ab, TRUE, TRUE);
1865 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1867 return TRUE;
1870 /* Finish writing out a block. */
1872 static bfd_boolean
1873 pr_end_block (p, addr)
1874 PTR p;
1875 bfd_vma addr;
1877 struct pr_handle *info = (struct pr_handle *) p;
1878 char ab[20];
1880 info->indent -= 2;
1882 indent (info);
1883 print_vma (addr, ab, TRUE, TRUE);
1884 fprintf (info->f, "} /* %s */\n", ab);
1886 return TRUE;
1889 /* Finish writing out a function. */
1891 static bfd_boolean
1892 pr_end_function (p)
1893 PTR p ATTRIBUTE_UNUSED;
1895 return TRUE;