This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / binutils / prdbg.c
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4
5 This file is part of GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This file prints out the generic debugging information, by
23 supplying a set of routines to debug_write. */
24
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include "bfd.h"
29 #include "bucomm.h"
30 #include "libiberty.h"
31 #include "debug.h"
32 #include "budbg.h"
33
34 /* This is the structure we use as a handle for these routines. */
35
36 struct pr_handle
37 {
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;
46 };
47
48 /* The type stack. */
49
50 struct pr_stack
51 {
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;
60 };
61
62 static void indent PARAMS ((struct pr_handle *));
63 static boolean push_type PARAMS ((struct pr_handle *, const char *));
64 static boolean prepend_type PARAMS ((struct pr_handle *, const char *));
65 static boolean append_type PARAMS ((struct pr_handle *, const char *));
66 static boolean substitute_type PARAMS ((struct pr_handle *, const char *));
67 static boolean indent_type PARAMS ((struct pr_handle *));
68 static char *pop_type PARAMS ((struct pr_handle *));
69 static void print_vma PARAMS ((bfd_vma, char *, boolean, boolean));
70 static boolean pr_fix_visibility
71 PARAMS ((struct pr_handle *, enum debug_visibility));
72
73 static boolean pr_start_compilation_unit PARAMS ((PTR, const char *));
74 static boolean pr_start_source PARAMS ((PTR, const char *));
75 static boolean pr_empty_type PARAMS ((PTR));
76 static boolean pr_void_type PARAMS ((PTR));
77 static boolean pr_int_type PARAMS ((PTR, unsigned int, boolean));
78 static boolean pr_float_type PARAMS ((PTR, unsigned int));
79 static boolean pr_complex_type PARAMS ((PTR, unsigned int));
80 static boolean pr_bool_type PARAMS ((PTR, unsigned int));
81 static boolean pr_enum_type
82 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
83 static boolean pr_pointer_type PARAMS ((PTR));
84 static boolean pr_function_type PARAMS ((PTR, int, boolean));
85 static boolean pr_reference_type PARAMS ((PTR));
86 static boolean pr_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
87 static boolean pr_array_type
88 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
89 static boolean pr_set_type PARAMS ((PTR, boolean));
90 static boolean pr_offset_type PARAMS ((PTR));
91 static boolean pr_method_type PARAMS ((PTR, boolean, int, boolean));
92 static boolean pr_const_type PARAMS ((PTR));
93 static boolean pr_volatile_type PARAMS ((PTR));
94 static boolean pr_start_struct_type
95 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int));
96 static boolean pr_struct_field
97 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
98 static boolean pr_end_struct_type PARAMS ((PTR));
99 static boolean pr_start_class_type
100 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean,
101 boolean));
102 static boolean pr_class_static_member
103 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
104 static boolean pr_class_baseclass
105 PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
106 static boolean pr_class_start_method PARAMS ((PTR, const char *));
107 static boolean pr_class_method_variant
108 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
109 bfd_vma, boolean));
110 static boolean pr_class_static_method_variant
111 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
112 static boolean pr_class_end_method PARAMS ((PTR));
113 static boolean pr_end_class_type PARAMS ((PTR));
114 static boolean pr_typedef_type PARAMS ((PTR, const char *));
115 static boolean pr_tag_type
116 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
117 static boolean pr_typdef PARAMS ((PTR, const char *));
118 static boolean pr_tag PARAMS ((PTR, const char *));
119 static boolean pr_int_constant PARAMS ((PTR, const char *, bfd_vma));
120 static boolean pr_float_constant PARAMS ((PTR, const char *, double));
121 static boolean pr_typed_constant PARAMS ((PTR, const char *, bfd_vma));
122 static boolean pr_variable
123 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
124 static boolean pr_start_function PARAMS ((PTR, const char *, boolean));
125 static boolean pr_function_parameter
126 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
127 static boolean pr_start_block PARAMS ((PTR, bfd_vma));
128 static boolean pr_end_block PARAMS ((PTR, bfd_vma));
129 static boolean pr_end_function PARAMS ((PTR));
130 static boolean pr_lineno PARAMS ((PTR, const char *, unsigned long, bfd_vma));
131
132 static const struct debug_write_fns pr_fns =
133 {
134 pr_start_compilation_unit,
135 pr_start_source,
136 pr_empty_type,
137 pr_void_type,
138 pr_int_type,
139 pr_float_type,
140 pr_complex_type,
141 pr_bool_type,
142 pr_enum_type,
143 pr_pointer_type,
144 pr_function_type,
145 pr_reference_type,
146 pr_range_type,
147 pr_array_type,
148 pr_set_type,
149 pr_offset_type,
150 pr_method_type,
151 pr_const_type,
152 pr_volatile_type,
153 pr_start_struct_type,
154 pr_struct_field,
155 pr_end_struct_type,
156 pr_start_class_type,
157 pr_class_static_member,
158 pr_class_baseclass,
159 pr_class_start_method,
160 pr_class_method_variant,
161 pr_class_static_method_variant,
162 pr_class_end_method,
163 pr_end_class_type,
164 pr_typedef_type,
165 pr_tag_type,
166 pr_typdef,
167 pr_tag,
168 pr_int_constant,
169 pr_float_constant,
170 pr_typed_constant,
171 pr_variable,
172 pr_start_function,
173 pr_function_parameter,
174 pr_start_block,
175 pr_end_block,
176 pr_end_function,
177 pr_lineno
178 };
179 \f
180 /* Print out the generic debugging information recorded in dhandle. */
181
182 boolean
183 print_debugging_info (f, dhandle)
184 FILE *f;
185 PTR dhandle;
186 {
187 struct pr_handle info;
188
189 info.f = f;
190 info.indent = 0;
191 info.stack = NULL;
192 info.parameter = 0;
193
194 return debug_write (dhandle, &pr_fns, (PTR) &info);
195 }
196 \f
197 /* Indent to the current indentation level. */
198
199 static void
200 indent (info)
201 struct pr_handle *info;
202 {
203 unsigned int i;
204
205 for (i = 0; i < info->indent; i++)
206 putc (' ', info->f);
207 }
208
209 /* Push a type on the type stack. */
210
211 static boolean
212 push_type (info, type)
213 struct pr_handle *info;
214 const char *type;
215 {
216 struct pr_stack *n;
217
218 if (type == NULL)
219 return false;
220
221 n = (struct pr_stack *) xmalloc (sizeof *n);
222 memset (n, 0, sizeof *n);
223
224 n->type = xstrdup (type);
225 n->visibility = DEBUG_VISIBILITY_IGNORE;
226 n->method = NULL;
227 n->next = info->stack;
228 info->stack = n;
229
230 return true;
231 }
232
233 /* Prepend a string onto the type on the top of the type stack. */
234
235 static boolean
236 prepend_type (info, s)
237 struct pr_handle *info;
238 const char *s;
239 {
240 char *n;
241
242 assert (info->stack != NULL);
243
244 n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
245 sprintf (n, "%s%s", s, info->stack->type);
246 free (info->stack->type);
247 info->stack->type = n;
248
249 return true;
250 }
251
252 /* Append a string to the type on the top of the type stack. */
253
254 static boolean
255 append_type (info, s)
256 struct pr_handle *info;
257 const char *s;
258 {
259 unsigned int len;
260
261 if (s == NULL)
262 return false;
263
264 assert (info->stack != NULL);
265
266 len = strlen (info->stack->type);
267 info->stack->type = (char *) xrealloc (info->stack->type,
268 len + strlen (s) + 1);
269 strcpy (info->stack->type + len, s);
270
271 return true;
272 }
273
274 /* We use an underscore to indicate where the name should go in a type
275 string. This function substitutes a string for the underscore. If
276 there is no underscore, the name follows the type. */
277
278 static boolean
279 substitute_type (info, s)
280 struct pr_handle *info;
281 const char *s;
282 {
283 char *u;
284
285 assert (info->stack != NULL);
286
287 u = strchr (info->stack->type, '|');
288 if (u != NULL)
289 {
290 char *n;
291
292 n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
293
294 memcpy (n, info->stack->type, u - info->stack->type);
295 strcpy (n + (u - info->stack->type), s);
296 strcat (n, u + 1);
297
298 free (info->stack->type);
299 info->stack->type = n;
300
301 return true;
302 }
303
304 if (strchr (s, '|') != NULL
305 && (strchr (info->stack->type, '{') != NULL
306 || strchr (info->stack->type, '(') != NULL))
307 {
308 if (! prepend_type (info, "(")
309 || ! append_type (info, ")"))
310 return false;
311 }
312
313 if (*s == '\0')
314 return true;
315
316 return (append_type (info, " ")
317 && append_type (info, s));
318 }
319
320 /* Indent the type at the top of the stack by appending spaces. */
321
322 static boolean
323 indent_type (info)
324 struct pr_handle *info;
325 {
326 unsigned int i;
327
328 for (i = 0; i < info->indent; i++)
329 {
330 if (! append_type (info, " "))
331 return false;
332 }
333
334 return true;
335 }
336
337 /* Pop a type from the type stack. */
338
339 static char *
340 pop_type (info)
341 struct pr_handle *info;
342 {
343 struct pr_stack *o;
344 char *ret;
345
346 assert (info->stack != NULL);
347
348 o = info->stack;
349 info->stack = o->next;
350 ret = o->type;
351 free (o);
352
353 return ret;
354 }
355
356 /* Print a VMA value into a string. */
357
358 static void
359 print_vma (vma, buf, unsignedp, hexp)
360 bfd_vma vma;
361 char *buf;
362 boolean unsignedp;
363 boolean hexp;
364 {
365 if (sizeof (vma) <= sizeof (unsigned long))
366 {
367 if (hexp)
368 sprintf (buf, "0x%lx", (unsigned long) vma);
369 else if (unsignedp)
370 sprintf (buf, "%lu", (unsigned long) vma);
371 else
372 sprintf (buf, "%ld", (long) vma);
373 }
374 else
375 {
376 buf[0] = '0';
377 buf[1] = 'x';
378 sprintf_vma (buf + 2, vma);
379 }
380 }
381 \f
382 /* Start a new compilation unit. */
383
384 static boolean
385 pr_start_compilation_unit (p, filename)
386 PTR p;
387 const char *filename;
388 {
389 struct pr_handle *info = (struct pr_handle *) p;
390
391 assert (info->indent == 0);
392
393 fprintf (info->f, "%s:\n", filename);
394
395 return true;
396 }
397
398 /* Start a source file within a compilation unit. */
399
400 static boolean
401 pr_start_source (p, filename)
402 PTR p;
403 const char *filename;
404 {
405 struct pr_handle *info = (struct pr_handle *) p;
406
407 assert (info->indent == 0);
408
409 fprintf (info->f, " %s:\n", filename);
410
411 return true;
412 }
413
414 /* Push an empty type onto the type stack. */
415
416 static boolean
417 pr_empty_type (p)
418 PTR p;
419 {
420 struct pr_handle *info = (struct pr_handle *) p;
421
422 return push_type (info, "<undefined>");
423 }
424
425 /* Push a void type onto the type stack. */
426
427 static boolean
428 pr_void_type (p)
429 PTR p;
430 {
431 struct pr_handle *info = (struct pr_handle *) p;
432
433 return push_type (info, "void");
434 }
435
436 /* Push an integer type onto the type stack. */
437
438 static boolean
439 pr_int_type (p, size, unsignedp)
440 PTR p;
441 unsigned int size;
442 boolean unsignedp;
443 {
444 struct pr_handle *info = (struct pr_handle *) p;
445 char ab[10];
446
447 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
448 return push_type (info, ab);
449 }
450
451 /* Push a floating type onto the type stack. */
452
453 static boolean
454 pr_float_type (p, size)
455 PTR p;
456 unsigned int size;
457 {
458 struct pr_handle *info = (struct pr_handle *) p;
459 char ab[10];
460
461 if (size == 4)
462 return push_type (info, "float");
463 else if (size == 8)
464 return push_type (info, "double");
465
466 sprintf (ab, "float%d", size * 8);
467 return push_type (info, ab);
468 }
469
470 /* Push a complex type onto the type stack. */
471
472 static boolean
473 pr_complex_type (p, size)
474 PTR p;
475 unsigned int size;
476 {
477 struct pr_handle *info = (struct pr_handle *) p;
478
479 if (! pr_float_type (p, size))
480 return false;
481
482 return prepend_type (info, "complex ");
483 }
484
485 /* Push a boolean type onto the type stack. */
486
487 static boolean
488 pr_bool_type (p, size)
489 PTR p;
490 unsigned int size;
491 {
492 struct pr_handle *info = (struct pr_handle *) p;
493 char ab[10];
494
495 sprintf (ab, "bool%d", size * 8);
496
497 return push_type (info, ab);
498 }
499
500 /* Push an enum type onto the type stack. */
501
502 static boolean
503 pr_enum_type (p, tag, names, values)
504 PTR p;
505 const char *tag;
506 const char **names;
507 bfd_signed_vma *values;
508 {
509 struct pr_handle *info = (struct pr_handle *) p;
510 unsigned int i;
511 bfd_signed_vma val;
512
513 if (! push_type (info, "enum "))
514 return false;
515 if (tag != NULL)
516 {
517 if (! append_type (info, tag)
518 || ! append_type (info, " "))
519 return false;
520 }
521 if (! append_type (info, "{ "))
522 return false;
523
524 if (names == NULL)
525 {
526 if (! append_type (info, "/* undefined */"))
527 return false;
528 }
529 else
530 {
531 val = 0;
532 for (i = 0; names[i] != NULL; i++)
533 {
534 if (i > 0)
535 {
536 if (! append_type (info, ", "))
537 return false;
538 }
539
540 if (! append_type (info, names[i]))
541 return false;
542
543 if (values[i] != val)
544 {
545 char ab[20];
546
547 print_vma (values[i], ab, false, false);
548 if (! append_type (info, " = ")
549 || ! append_type (info, ab))
550 return false;
551 val = values[i];
552 }
553
554 ++val;
555 }
556 }
557
558 return append_type (info, " }");
559 }
560
561 /* Turn the top type on the stack into a pointer. */
562
563 static boolean
564 pr_pointer_type (p)
565 PTR p;
566 {
567 struct pr_handle *info = (struct pr_handle *) p;
568 char *s;
569
570 assert (info->stack != NULL);
571
572 s = strchr (info->stack->type, '|');
573 if (s != NULL && s[1] == '[')
574 return substitute_type (info, "(*|)");
575 return substitute_type (info, "*|");
576 }
577
578 /* Turn the top type on the stack into a function returning that type. */
579
580 static boolean
581 pr_function_type (p, argcount, varargs)
582 PTR p;
583 int argcount;
584 boolean varargs;
585 {
586 struct pr_handle *info = (struct pr_handle *) p;
587 char **arg_types;
588 unsigned int len;
589 char *s;
590
591 assert (info->stack != NULL);
592
593 len = 10;
594
595 if (argcount <= 0)
596 {
597 arg_types = NULL;
598 len += 15;
599 }
600 else
601 {
602 int i;
603
604 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
605 for (i = argcount - 1; i >= 0; i--)
606 {
607 if (! substitute_type (info, ""))
608 return false;
609 arg_types[i] = pop_type (info);
610 if (arg_types[i] == NULL)
611 return false;
612 len += strlen (arg_types[i]) + 2;
613 }
614 if (varargs)
615 len += 5;
616 }
617
618 /* Now the return type is on the top of the stack. */
619
620 s = (char *) xmalloc (len);
621 strcpy (s, "(|) (");
622
623 if (argcount < 0)
624 strcat (s, "/* unknown */");
625 else
626 {
627 int i;
628
629 for (i = 0; i < argcount; i++)
630 {
631 if (i > 0)
632 strcat (s, ", ");
633 strcat (s, arg_types[i]);
634 }
635 if (varargs)
636 {
637 if (i > 0)
638 strcat (s, ", ");
639 strcat (s, "...");
640 }
641 if (argcount > 0)
642 free (arg_types);
643 }
644
645 strcat (s, ")");
646
647 if (! substitute_type (info, s))
648 return false;
649
650 free (s);
651
652 return true;
653 }
654
655 /* Turn the top type on the stack into a reference to that type. */
656
657 static boolean
658 pr_reference_type (p)
659 PTR p;
660 {
661 struct pr_handle *info = (struct pr_handle *) p;
662
663 assert (info->stack != NULL);
664
665 return substitute_type (info, "&|");
666 }
667
668 /* Make a range type. */
669
670 static boolean
671 pr_range_type (p, lower, upper)
672 PTR p;
673 bfd_signed_vma lower;
674 bfd_signed_vma upper;
675 {
676 struct pr_handle *info = (struct pr_handle *) p;
677 char abl[20], abu[20];
678
679 assert (info->stack != NULL);
680
681 if (! substitute_type (info, ""))
682 return false;
683
684 print_vma (lower, abl, false, false);
685 print_vma (upper, abu, false, false);
686
687 return (prepend_type (info, "range (")
688 && append_type (info, "):")
689 && append_type (info, abl)
690 && append_type (info, ":")
691 && append_type (info, abu));
692 }
693
694 /* Make an array type. */
695
696 /*ARGSUSED*/
697 static boolean
698 pr_array_type (p, lower, upper, stringp)
699 PTR p;
700 bfd_signed_vma lower;
701 bfd_signed_vma upper;
702 boolean stringp;
703 {
704 struct pr_handle *info = (struct pr_handle *) p;
705 char *range_type;
706 char abl[20], abu[20], ab[50];
707
708 range_type = pop_type (info);
709 if (range_type == NULL)
710 return false;
711
712 if (lower == 0)
713 {
714 if (upper == -1)
715 sprintf (ab, "|[]");
716 else
717 {
718 print_vma (upper + 1, abu, false, false);
719 sprintf (ab, "|[%s]", abu);
720 }
721 }
722 else
723 {
724 print_vma (lower, abl, false, false);
725 print_vma (upper, abu, false, false);
726 sprintf (ab, "|[%s:%s]", abl, abu);
727 }
728
729 if (! substitute_type (info, ab))
730 return false;
731
732 if (strcmp (range_type, "int") != 0)
733 {
734 if (! append_type (info, ":")
735 || ! append_type (info, range_type))
736 return false;
737 }
738
739 if (stringp)
740 {
741 if (! append_type (info, " /* string */"))
742 return false;
743 }
744
745 return true;
746 }
747
748 /* Make a set type. */
749
750 /*ARGSUSED*/
751 static boolean
752 pr_set_type (p, bitstringp)
753 PTR p;
754 boolean bitstringp;
755 {
756 struct pr_handle *info = (struct pr_handle *) p;
757
758 if (! substitute_type (info, ""))
759 return false;
760
761 if (! prepend_type (info, "set { ")
762 || ! append_type (info, " }"))
763 return false;
764
765 if (bitstringp)
766 {
767 if (! append_type (info, "/* bitstring */"))
768 return false;
769 }
770
771 return true;
772 }
773
774 /* Make an offset type. */
775
776 static boolean
777 pr_offset_type (p)
778 PTR p;
779 {
780 struct pr_handle *info = (struct pr_handle *) p;
781 char *t;
782
783 if (! substitute_type (info, ""))
784 return false;
785
786 t = pop_type (info);
787 if (t == NULL)
788 return false;
789
790 return (substitute_type (info, "")
791 && prepend_type (info, " ")
792 && prepend_type (info, t)
793 && append_type (info, "::|"));
794 }
795
796 /* Make a method type. */
797
798 static boolean
799 pr_method_type (p, domain, argcount, varargs)
800 PTR p;
801 boolean domain;
802 int argcount;
803 boolean varargs;
804 {
805 struct pr_handle *info = (struct pr_handle *) p;
806 unsigned int len;
807 char *domain_type;
808 char **arg_types;
809 char *s;
810
811 len = 10;
812
813 if (! domain)
814 domain_type = NULL;
815 else
816 {
817 if (! substitute_type (info, ""))
818 return false;
819 domain_type = pop_type (info);
820 if (domain_type == NULL)
821 return false;
822 if (strncmp (domain_type, "class ", sizeof "class " - 1) == 0
823 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
824 domain_type += sizeof "class " - 1;
825 else if (strncmp (domain_type, "union class ",
826 sizeof "union class ") == 0
827 && (strchr (domain_type + sizeof "union class " - 1, ' ')
828 == NULL))
829 domain_type += sizeof "union class " - 1;
830 len += strlen (domain_type);
831 }
832
833 if (argcount <= 0)
834 {
835 arg_types = NULL;
836 len += 15;
837 }
838 else
839 {
840 int i;
841
842 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
843 for (i = argcount - 1; i >= 0; i--)
844 {
845 if (! substitute_type (info, ""))
846 return false;
847 arg_types[i] = pop_type (info);
848 if (arg_types[i] == NULL)
849 return false;
850 len += strlen (arg_types[i]) + 2;
851 }
852 if (varargs)
853 len += 5;
854 }
855
856 /* Now the return type is on the top of the stack. */
857
858 s = (char *) xmalloc (len);
859 if (! domain)
860 *s = '\0';
861 else
862 strcpy (s, domain_type);
863 strcat (s, "::| (");
864
865 if (argcount < 0)
866 strcat (s, "/* unknown */");
867 else
868 {
869 int i;
870
871 for (i = 0; i < argcount; i++)
872 {
873 if (i > 0)
874 strcat (s, ", ");
875 strcat (s, arg_types[i]);
876 }
877 if (varargs)
878 {
879 if (i > 0)
880 strcat (s, ", ");
881 strcat (s, "...");
882 }
883 if (argcount > 0)
884 free (arg_types);
885 }
886
887 strcat (s, ")");
888
889 if (! substitute_type (info, s))
890 return false;
891
892 free (s);
893
894 return true;
895 }
896
897 /* Make a const qualified type. */
898
899 static boolean
900 pr_const_type (p)
901 PTR p;
902 {
903 struct pr_handle *info = (struct pr_handle *) p;
904
905 return substitute_type (info, "const |");
906 }
907
908 /* Make a volatile qualified type. */
909
910 static boolean
911 pr_volatile_type (p)
912 PTR p;
913 {
914 struct pr_handle *info = (struct pr_handle *) p;
915
916 return substitute_type (info, "volatile |");
917 }
918
919 /* Start accumulating a struct type. */
920
921 static boolean
922 pr_start_struct_type (p, tag, id, structp, size)
923 PTR p;
924 const char *tag;
925 unsigned int id;
926 boolean structp;
927 unsigned int size;
928 {
929 struct pr_handle *info = (struct pr_handle *) p;
930
931 info->indent += 2;
932
933 if (! push_type (info, structp ? "struct " : "union "))
934 return false;
935 if (tag != NULL)
936 {
937 if (! append_type (info, tag))
938 return false;
939 }
940 else
941 {
942 char idbuf[20];
943
944 sprintf (idbuf, "%%anon%u", id);
945 if (! append_type (info, idbuf))
946 return false;
947 }
948
949 if (! append_type (info, " {"))
950 return false;
951 if (size != 0 || tag != NULL)
952 {
953 char ab[30];
954
955 if (! append_type (info, " /*"))
956 return false;
957
958 if (size != 0)
959 {
960 sprintf (ab, " size %u", size);
961 if (! append_type (info, ab))
962 return false;
963 }
964 if (tag != NULL)
965 {
966 sprintf (ab, " id %u", id);
967 if (! append_type (info, ab))
968 return false;
969 }
970 if (! append_type (info, " */"))
971 return false;
972 }
973 if (! append_type (info, "\n"))
974 return false;
975
976 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
977
978 return indent_type (info);
979 }
980
981 /* Output the visibility of a field in a struct. */
982
983 static boolean
984 pr_fix_visibility (info, visibility)
985 struct pr_handle *info;
986 enum debug_visibility visibility;
987 {
988 const char *s;
989 char *t;
990 unsigned int len;
991
992 assert (info->stack != NULL);
993
994 if (info->stack->visibility == visibility)
995 return true;
996
997 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
998
999 switch (visibility)
1000 {
1001 case DEBUG_VISIBILITY_PUBLIC:
1002 s = "public";
1003 break;
1004 case DEBUG_VISIBILITY_PRIVATE:
1005 s = "private";
1006 break;
1007 case DEBUG_VISIBILITY_PROTECTED:
1008 s = "protected";
1009 break;
1010 default:
1011 abort ();
1012 return false;
1013 }
1014
1015 /* Trim off a trailing space in the struct string, to make the
1016 output look a bit better, then stick on the visibility string. */
1017
1018 t = info->stack->type;
1019 len = strlen (t);
1020 assert (t[len - 1] == ' ');
1021 t[len - 1] = '\0';
1022
1023 if (! append_type (info, s)
1024 || ! append_type (info, ":\n")
1025 || ! indent_type (info))
1026 return false;
1027
1028 info->stack->visibility = visibility;
1029
1030 return true;
1031 }
1032
1033 /* Add a field to a struct type. */
1034
1035 static boolean
1036 pr_struct_field (p, name, bitpos, bitsize, visibility)
1037 PTR p;
1038 const char *name;
1039 bfd_vma bitpos;
1040 bfd_vma bitsize;
1041 enum debug_visibility visibility;
1042 {
1043 struct pr_handle *info = (struct pr_handle *) p;
1044 char ab[20];
1045 char *t;
1046
1047 if (! substitute_type (info, name))
1048 return false;
1049
1050 if (! append_type (info, "; /* "))
1051 return false;
1052
1053 if (bitsize != 0)
1054 {
1055 print_vma (bitsize, ab, true, false);
1056 if (! append_type (info, "bitsize ")
1057 || ! append_type (info, ab)
1058 || ! append_type (info, ", "))
1059 return false;
1060 }
1061
1062 print_vma (bitpos, ab, true, false);
1063 if (! append_type (info, "bitpos ")
1064 || ! append_type (info, ab)
1065 || ! append_type (info, " */\n")
1066 || ! indent_type (info))
1067 return false;
1068
1069 t = pop_type (info);
1070 if (t == NULL)
1071 return false;
1072
1073 if (! pr_fix_visibility (info, visibility))
1074 return false;
1075
1076 return append_type (info, t);
1077 }
1078
1079 /* Finish a struct type. */
1080
1081 static boolean
1082 pr_end_struct_type (p)
1083 PTR p;
1084 {
1085 struct pr_handle *info = (struct pr_handle *) p;
1086 char *s;
1087
1088 assert (info->stack != NULL);
1089 assert (info->indent >= 2);
1090
1091 info->indent -= 2;
1092
1093 /* Change the trailing indentation to have a close brace. */
1094 s = info->stack->type + strlen (info->stack->type) - 2;
1095 assert (strcmp (s, " ") == 0);
1096
1097 *s++ = '}';
1098 *s = '\0';
1099
1100 return true;
1101 }
1102
1103 /* Start a class type. */
1104
1105 static boolean
1106 pr_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1107 PTR p;
1108 const char *tag;
1109 unsigned int id;
1110 boolean structp;
1111 unsigned int size;
1112 boolean vptr;
1113 boolean ownvptr;
1114 {
1115 struct pr_handle *info = (struct pr_handle *) p;
1116 char *tv = NULL;
1117
1118 info->indent += 2;
1119
1120 if (vptr && ! ownvptr)
1121 {
1122 tv = pop_type (info);
1123 if (tv == NULL)
1124 return false;
1125 }
1126
1127 if (! push_type (info, structp ? "class " : "union class "))
1128 return false;
1129 if (tag != NULL)
1130 {
1131 if (! append_type (info, tag))
1132 return false;
1133 }
1134 else
1135 {
1136 char idbuf[20];
1137
1138 sprintf (idbuf, "%%anon%u", id);
1139 if (! append_type (info, idbuf))
1140 return false;
1141 }
1142
1143 if (! append_type (info, " {"))
1144 return false;
1145 if (size != 0 || vptr || ownvptr || tag != NULL)
1146 {
1147 if (! append_type (info, " /*"))
1148 return false;
1149
1150 if (size != 0)
1151 {
1152 char ab[20];
1153
1154 sprintf (ab, "%u", size);
1155 if (! append_type (info, " size ")
1156 || ! append_type (info, ab))
1157 return false;
1158 }
1159
1160 if (vptr)
1161 {
1162 if (! append_type (info, " vtable "))
1163 return false;
1164 if (ownvptr)
1165 {
1166 if (! append_type (info, "self "))
1167 return false;
1168 }
1169 else
1170 {
1171 if (! append_type (info, tv)
1172 || ! append_type (info, " "))
1173 return false;
1174 }
1175 }
1176
1177 if (tag != NULL)
1178 {
1179 char ab[30];
1180
1181 sprintf (ab, " id %u", id);
1182 if (! append_type (info, ab))
1183 return false;
1184 }
1185
1186 if (! append_type (info, " */"))
1187 return false;
1188 }
1189
1190 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1191
1192 return (append_type (info, "\n")
1193 && indent_type (info));
1194 }
1195
1196 /* Add a static member to a class. */
1197
1198 static boolean
1199 pr_class_static_member (p, name, physname, visibility)
1200 PTR p;
1201 const char *name;
1202 const char *physname;
1203 enum debug_visibility visibility;
1204 {
1205 struct pr_handle *info = (struct pr_handle *) p;
1206 char *t;
1207
1208 if (! substitute_type (info, name))
1209 return false;
1210
1211 if (! prepend_type (info, "static ")
1212 || ! append_type (info, "; /* ")
1213 || ! append_type (info, physname)
1214 || ! append_type (info, " */\n")
1215 || ! indent_type (info))
1216 return false;
1217
1218 t = pop_type (info);
1219 if (t == NULL)
1220 return false;
1221
1222 if (! pr_fix_visibility (info, visibility))
1223 return false;
1224
1225 return append_type (info, t);
1226 }
1227
1228 /* Add a base class to a class. */
1229
1230 static boolean
1231 pr_class_baseclass (p, bitpos, virtual, visibility)
1232 PTR p;
1233 bfd_vma bitpos;
1234 boolean virtual;
1235 enum debug_visibility visibility;
1236 {
1237 struct pr_handle *info = (struct pr_handle *) p;
1238 char *t;
1239 const char *prefix;
1240 char ab[20];
1241 char *s, *l, *n;
1242
1243 assert (info->stack != NULL && info->stack->next != NULL);
1244
1245 if (! substitute_type (info, ""))
1246 return false;
1247
1248 t = pop_type (info);
1249 if (t == NULL)
1250 return false;
1251
1252 if (strncmp (t, "class ", sizeof "class " - 1) == 0)
1253 t += sizeof "class " - 1;
1254
1255 /* Push it back on to take advantage of the prepend_type and
1256 append_type routines. */
1257 if (! push_type (info, t))
1258 return false;
1259
1260 if (virtual)
1261 {
1262 if (! prepend_type (info, "virtual "))
1263 return false;
1264 }
1265
1266 switch (visibility)
1267 {
1268 case DEBUG_VISIBILITY_PUBLIC:
1269 prefix = "public ";
1270 break;
1271 case DEBUG_VISIBILITY_PROTECTED:
1272 prefix = "protected ";
1273 break;
1274 case DEBUG_VISIBILITY_PRIVATE:
1275 prefix = "private ";
1276 break;
1277 default:
1278 prefix = "/* unknown visibility */ ";
1279 break;
1280 }
1281
1282 if (! prepend_type (info, prefix))
1283 return false;
1284
1285 if (bitpos != 0)
1286 {
1287 print_vma (bitpos, ab, true, false);
1288 if (! append_type (info, " /* bitpos ")
1289 || ! append_type (info, ab)
1290 || ! append_type (info, " */"))
1291 return false;
1292 }
1293
1294 /* Now the top of the stack is something like "public A / * bitpos
1295 10 * /". The next element on the stack is something like "class
1296 xx { / * size 8 * /\n...". We want to substitute the top of the
1297 stack in before the {. */
1298 s = strchr (info->stack->next->type, '{');
1299 assert (s != NULL);
1300 --s;
1301
1302 /* If there is already a ':', then we already have a baseclass, and
1303 we must append this one after a comma. */
1304 for (l = info->stack->next->type; l != s; l++)
1305 if (*l == ':')
1306 break;
1307 if (! prepend_type (info, l == s ? " : " : ", "))
1308 return false;
1309
1310 t = pop_type (info);
1311 if (t == NULL)
1312 return false;
1313
1314 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1315 memcpy (n, info->stack->type, s - info->stack->type);
1316 strcpy (n + (s - info->stack->type), t);
1317 strcat (n, s);
1318
1319 free (info->stack->type);
1320 info->stack->type = n;
1321
1322 free (t);
1323
1324 return true;
1325 }
1326
1327 /* Start adding a method to a class. */
1328
1329 static boolean
1330 pr_class_start_method (p, name)
1331 PTR p;
1332 const char *name;
1333 {
1334 struct pr_handle *info = (struct pr_handle *) p;
1335
1336 assert (info->stack != NULL);
1337 info->stack->method = name;
1338 return true;
1339 }
1340
1341 /* Add a variant to a method. */
1342
1343 static boolean
1344 pr_class_method_variant (p, physname, visibility, constp, volatilep, voffset,
1345 context)
1346 PTR p;
1347 const char *physname;
1348 enum debug_visibility visibility;
1349 boolean constp;
1350 boolean volatilep;
1351 bfd_vma voffset;
1352 boolean context;
1353 {
1354 struct pr_handle *info = (struct pr_handle *) p;
1355 char *method_type;
1356 char *context_type;
1357
1358 assert (info->stack != NULL);
1359 assert (info->stack->next != NULL);
1360
1361 /* Put the const and volatile qualifiers on the type. */
1362 if (volatilep)
1363 {
1364 if (! append_type (info, " volatile"))
1365 return false;
1366 }
1367 if (constp)
1368 {
1369 if (! append_type (info, " const"))
1370 return false;
1371 }
1372
1373 /* Stick the name of the method into its type. */
1374 if (! substitute_type (info,
1375 (context
1376 ? info->stack->next->next->method
1377 : info->stack->next->method)))
1378 return false;
1379
1380 /* Get the type. */
1381 method_type = pop_type (info);
1382 if (method_type == NULL)
1383 return false;
1384
1385 /* Pull off the context type if there is one. */
1386 if (! context)
1387 context_type = NULL;
1388 else
1389 {
1390 context_type = pop_type (info);
1391 if (context_type == NULL)
1392 return false;
1393 }
1394
1395 /* Now the top of the stack is the class. */
1396
1397 if (! pr_fix_visibility (info, visibility))
1398 return false;
1399
1400 if (! append_type (info, method_type)
1401 || ! append_type (info, " /* ")
1402 || ! append_type (info, physname)
1403 || ! append_type (info, " "))
1404 return false;
1405 if (context || voffset != 0)
1406 {
1407 char ab[20];
1408
1409 if (context)
1410 {
1411 if (! append_type (info, "context ")
1412 || ! append_type (info, context_type)
1413 || ! append_type (info, " "))
1414 return false;
1415 }
1416 print_vma (voffset, ab, true, false);
1417 if (! append_type (info, "voffset ")
1418 || ! append_type (info, ab))
1419 return false;
1420 }
1421
1422 return (append_type (info, " */;\n")
1423 && indent_type (info));
1424 }
1425
1426 /* Add a static variant to a method. */
1427
1428 static boolean
1429 pr_class_static_method_variant (p, physname, visibility, constp, volatilep)
1430 PTR p;
1431 const char *physname;
1432 enum debug_visibility visibility;
1433 boolean constp;
1434 boolean volatilep;
1435 {
1436 struct pr_handle *info = (struct pr_handle *) p;
1437 char *method_type;
1438
1439 assert (info->stack != NULL);
1440 assert (info->stack->next != NULL);
1441 assert (info->stack->next->method != NULL);
1442
1443 /* Put the const and volatile qualifiers on the type. */
1444 if (volatilep)
1445 {
1446 if (! append_type (info, " volatile"))
1447 return false;
1448 }
1449 if (constp)
1450 {
1451 if (! append_type (info, " const"))
1452 return false;
1453 }
1454
1455 /* Mark it as static. */
1456 if (! prepend_type (info, "static "))
1457 return false;
1458
1459 /* Stick the name of the method into its type. */
1460 if (! substitute_type (info, info->stack->next->method))
1461 return false;
1462
1463 /* Get the type. */
1464 method_type = pop_type (info);
1465 if (method_type == NULL)
1466 return false;
1467
1468 /* Now the top of the stack is the class. */
1469
1470 if (! pr_fix_visibility (info, visibility))
1471 return false;
1472
1473 return (append_type (info, method_type)
1474 && append_type (info, " /* ")
1475 && append_type (info, physname)
1476 && append_type (info, " */;\n")
1477 && indent_type (info));
1478 }
1479
1480 /* Finish up a method. */
1481
1482 static boolean
1483 pr_class_end_method (p)
1484 PTR p;
1485 {
1486 struct pr_handle *info = (struct pr_handle *) p;
1487
1488 info->stack->method = NULL;
1489 return true;
1490 }
1491
1492 /* Finish up a class. */
1493
1494 static boolean
1495 pr_end_class_type (p)
1496 PTR p;
1497 {
1498 return pr_end_struct_type (p);
1499 }
1500
1501 /* Push a type on the stack using a typedef name. */
1502
1503 static boolean
1504 pr_typedef_type (p, name)
1505 PTR p;
1506 const char *name;
1507 {
1508 struct pr_handle *info = (struct pr_handle *) p;
1509
1510 return push_type (info, name);
1511 }
1512
1513 /* Push a type on the stack using a tag name. */
1514
1515 static boolean
1516 pr_tag_type (p, name, id, kind)
1517 PTR p;
1518 const char *name;
1519 unsigned int id;
1520 enum debug_type_kind kind;
1521 {
1522 struct pr_handle *info = (struct pr_handle *) p;
1523 const char *t, *tag;
1524 char idbuf[20];
1525
1526 switch (kind)
1527 {
1528 case DEBUG_KIND_STRUCT:
1529 t = "struct ";
1530 break;
1531 case DEBUG_KIND_UNION:
1532 t = "union ";
1533 break;
1534 case DEBUG_KIND_ENUM:
1535 t = "enum ";
1536 break;
1537 case DEBUG_KIND_CLASS:
1538 t = "class ";
1539 break;
1540 case DEBUG_KIND_UNION_CLASS:
1541 t = "union class ";
1542 break;
1543 default:
1544 abort ();
1545 return false;
1546 }
1547
1548 if (! push_type (info, t))
1549 return false;
1550 if (name != NULL)
1551 tag = name;
1552 else
1553 {
1554 sprintf (idbuf, "%%anon%u", id);
1555 tag = idbuf;
1556 }
1557
1558 if (! append_type (info, tag))
1559 return false;
1560 if (name != NULL && kind != DEBUG_KIND_ENUM)
1561 {
1562 sprintf (idbuf, " /* id %u */", id);
1563 if (! append_type (info, idbuf))
1564 return false;
1565 }
1566
1567 return true;
1568 }
1569
1570 /* Output a typedef. */
1571
1572 static boolean
1573 pr_typdef (p, name)
1574 PTR p;
1575 const char *name;
1576 {
1577 struct pr_handle *info = (struct pr_handle *) p;
1578 char *s;
1579
1580 if (! substitute_type (info, name))
1581 return false;
1582
1583 s = pop_type (info);
1584 if (s == NULL)
1585 return false;
1586
1587 indent (info);
1588 fprintf (info->f, "typedef %s;\n", s);
1589
1590 free (s);
1591
1592 return true;
1593 }
1594
1595 /* Output a tag. The tag should already be in the string on the
1596 stack, so all we have to do here is print it out. */
1597
1598 /*ARGSUSED*/
1599 static boolean
1600 pr_tag (p, name)
1601 PTR p;
1602 const char *name;
1603 {
1604 struct pr_handle *info = (struct pr_handle *) p;
1605 char *t;
1606
1607 t = pop_type (info);
1608 if (t == NULL)
1609 return false;
1610
1611 indent (info);
1612 fprintf (info->f, "%s;\n", t);
1613
1614 free (t);
1615
1616 return true;
1617 }
1618
1619 /* Output an integer constant. */
1620
1621 static boolean
1622 pr_int_constant (p, name, val)
1623 PTR p;
1624 const char *name;
1625 bfd_vma val;
1626 {
1627 struct pr_handle *info = (struct pr_handle *) info;
1628 char ab[20];
1629
1630 indent (info);
1631 print_vma (val, ab, false, false);
1632 fprintf (info->f, "const int %s = %s;\n", name, ab);
1633 return true;
1634 }
1635
1636 /* Output a floating point constant. */
1637
1638 static boolean
1639 pr_float_constant (p, name, val)
1640 PTR p;
1641 const char *name;
1642 double val;
1643 {
1644 struct pr_handle *info = (struct pr_handle *) info;
1645
1646 indent (info);
1647 fprintf (info->f, "const double %s = %g;\n", name, val);
1648 return true;
1649 }
1650
1651 /* Output a typed constant. */
1652
1653 static boolean
1654 pr_typed_constant (p, name, val)
1655 PTR p;
1656 const char *name;
1657 bfd_vma val;
1658 {
1659 struct pr_handle *info = (struct pr_handle *) p;
1660 char *t;
1661 char ab[20];
1662
1663 t = pop_type (info);
1664 if (t == NULL)
1665 return false;
1666
1667 indent (info);
1668 print_vma (val, ab, false, false);
1669 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1670
1671 free (t);
1672
1673 return true;
1674 }
1675
1676 /* Output a variable. */
1677
1678 static boolean
1679 pr_variable (p, name, kind, val)
1680 PTR p;
1681 const char *name;
1682 enum debug_var_kind kind;
1683 bfd_vma val;
1684 {
1685 struct pr_handle *info = (struct pr_handle *) p;
1686 char *t;
1687 char ab[20];
1688
1689 if (! substitute_type (info, name))
1690 return false;
1691
1692 t = pop_type (info);
1693 if (t == NULL)
1694 return false;
1695
1696 indent (info);
1697 switch (kind)
1698 {
1699 case DEBUG_STATIC:
1700 case DEBUG_LOCAL_STATIC:
1701 fprintf (info->f, "static ");
1702 break;
1703 case DEBUG_REGISTER:
1704 fprintf (info->f, "register ");
1705 break;
1706 default:
1707 break;
1708 }
1709 print_vma (val, ab, true, true);
1710 fprintf (info->f, "%s /* %s */;\n", t, ab);
1711
1712 free (t);
1713
1714 return true;
1715 }
1716
1717 /* Start outputting a function. */
1718
1719 static boolean
1720 pr_start_function (p, name, global)
1721 PTR p;
1722 const char *name;
1723 boolean global;
1724 {
1725 struct pr_handle *info = (struct pr_handle *) p;
1726 char *t;
1727
1728 if (! substitute_type (info, name))
1729 return false;
1730
1731 t = pop_type (info);
1732 if (t == NULL)
1733 return false;
1734
1735 indent (info);
1736 if (! global)
1737 fprintf (info->f, "static ");
1738 fprintf (info->f, "%s (", t);
1739
1740 info->parameter = 1;
1741
1742 return true;
1743 }
1744
1745 /* Output a function parameter. */
1746
1747 static boolean
1748 pr_function_parameter (p, name, kind, val)
1749 PTR p;
1750 const char *name;
1751 enum debug_parm_kind kind;
1752 bfd_vma val;
1753 {
1754 struct pr_handle *info = (struct pr_handle *) p;
1755 char *t;
1756 char ab[20];
1757
1758 if (kind == DEBUG_PARM_REFERENCE
1759 || kind == DEBUG_PARM_REF_REG)
1760 {
1761 if (! pr_reference_type (p))
1762 return false;
1763 }
1764
1765 if (! substitute_type (info, name))
1766 return false;
1767
1768 t = pop_type (info);
1769 if (t == NULL)
1770 return false;
1771
1772 if (info->parameter != 1)
1773 fprintf (info->f, ", ");
1774
1775 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1776 fprintf (info->f, "register ");
1777
1778 print_vma (val, ab, true, true);
1779 fprintf (info->f, "%s /* %s */", t, ab);
1780
1781 free (t);
1782
1783 ++info->parameter;
1784
1785 return true;
1786 }
1787
1788 /* Start writing out a block. */
1789
1790 static boolean
1791 pr_start_block (p, addr)
1792 PTR p;
1793 bfd_vma addr;
1794 {
1795 struct pr_handle *info = (struct pr_handle *) p;
1796 char ab[20];
1797
1798 if (info->parameter > 0)
1799 {
1800 fprintf (info->f, ")\n");
1801 info->parameter = 0;
1802 }
1803
1804 indent (info);
1805 print_vma (addr, ab, true, true);
1806 fprintf (info->f, "{ /* %s */\n", ab);
1807
1808 info->indent += 2;
1809
1810 return true;
1811 }
1812
1813 /* Write out line number information. */
1814
1815 static boolean
1816 pr_lineno (p, filename, lineno, addr)
1817 PTR p;
1818 const char *filename;
1819 unsigned long lineno;
1820 bfd_vma addr;
1821 {
1822 struct pr_handle *info = (struct pr_handle *) p;
1823 char ab[20];
1824
1825 indent (info);
1826 print_vma (addr, ab, true, true);
1827 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1828
1829 return true;
1830 }
1831
1832 /* Finish writing out a block. */
1833
1834 static boolean
1835 pr_end_block (p, addr)
1836 PTR p;
1837 bfd_vma addr;
1838 {
1839 struct pr_handle *info = (struct pr_handle *) p;
1840 char ab[20];
1841
1842 info->indent -= 2;
1843
1844 indent (info);
1845 print_vma (addr, ab, true, true);
1846 fprintf (info->f, "} /* %s */\n", ab);
1847
1848 return true;
1849 }
1850
1851 /* Finish writing out a function. */
1852
1853 /*ARGSUSED*/
1854 static boolean
1855 pr_end_function (p)
1856 PTR p;
1857 {
1858 return true;
1859 }
This page took 0.071585 seconds and 5 git commands to generate.