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