* arsup.c: Remove ARGSUSED.
[deliverable/binutils-gdb.git] / binutils / prdbg.c
CommitLineData
252b5132 1/* prdbg.c -- Print out generic debugging information.
8c2bc687 2 Copyright 1995, 1996 Free Software Foundation, Inc.
252b5132
RH
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
36struct 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
50struct 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
62static void indent PARAMS ((struct pr_handle *));
63static boolean push_type PARAMS ((struct pr_handle *, const char *));
64static boolean prepend_type PARAMS ((struct pr_handle *, const char *));
65static boolean append_type PARAMS ((struct pr_handle *, const char *));
66static boolean substitute_type PARAMS ((struct pr_handle *, const char *));
67static boolean indent_type PARAMS ((struct pr_handle *));
68static char *pop_type PARAMS ((struct pr_handle *));
69static void print_vma PARAMS ((bfd_vma, char *, boolean, boolean));
70static boolean pr_fix_visibility
71 PARAMS ((struct pr_handle *, enum debug_visibility));
72
73static boolean pr_start_compilation_unit PARAMS ((PTR, const char *));
74static boolean pr_start_source PARAMS ((PTR, const char *));
75static boolean pr_empty_type PARAMS ((PTR));
76static boolean pr_void_type PARAMS ((PTR));
77static boolean pr_int_type PARAMS ((PTR, unsigned int, boolean));
78static boolean pr_float_type PARAMS ((PTR, unsigned int));
79static boolean pr_complex_type PARAMS ((PTR, unsigned int));
80static boolean pr_bool_type PARAMS ((PTR, unsigned int));
81static boolean pr_enum_type
82 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
83static boolean pr_pointer_type PARAMS ((PTR));
84static boolean pr_function_type PARAMS ((PTR, int, boolean));
85static boolean pr_reference_type PARAMS ((PTR));
86static boolean pr_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
87static boolean pr_array_type
88 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
89static boolean pr_set_type PARAMS ((PTR, boolean));
90static boolean pr_offset_type PARAMS ((PTR));
91static boolean pr_method_type PARAMS ((PTR, boolean, int, boolean));
92static boolean pr_const_type PARAMS ((PTR));
93static boolean pr_volatile_type PARAMS ((PTR));
94static boolean pr_start_struct_type
95 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int));
96static boolean pr_struct_field
97 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
98static boolean pr_end_struct_type PARAMS ((PTR));
99static boolean pr_start_class_type
100 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean,
101 boolean));
102static boolean pr_class_static_member
103 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
104static boolean pr_class_baseclass
105 PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
106static boolean pr_class_start_method PARAMS ((PTR, const char *));
107static boolean pr_class_method_variant
108 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
109 bfd_vma, boolean));
110static boolean pr_class_static_method_variant
111 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
112static boolean pr_class_end_method PARAMS ((PTR));
113static boolean pr_end_class_type PARAMS ((PTR));
114static boolean pr_typedef_type PARAMS ((PTR, const char *));
115static boolean pr_tag_type
116 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
117static boolean pr_typdef PARAMS ((PTR, const char *));
118static boolean pr_tag PARAMS ((PTR, const char *));
119static boolean pr_int_constant PARAMS ((PTR, const char *, bfd_vma));
120static boolean pr_float_constant PARAMS ((PTR, const char *, double));
121static boolean pr_typed_constant PARAMS ((PTR, const char *, bfd_vma));
122static boolean pr_variable
123 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
124static boolean pr_start_function PARAMS ((PTR, const char *, boolean));
125static boolean pr_function_parameter
126 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
127static boolean pr_start_block PARAMS ((PTR, bfd_vma));
128static boolean pr_end_block PARAMS ((PTR, bfd_vma));
129static boolean pr_end_function PARAMS ((PTR));
130static boolean pr_lineno PARAMS ((PTR, const char *, unsigned long, bfd_vma));
131
132static 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
182boolean
183print_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
199static void
200indent (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
211static boolean
212push_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
235static boolean
236prepend_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
254static boolean
255append_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
278static boolean
279substitute_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
322static boolean
323indent_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
339static char *
340pop_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
358static void
359print_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
384static boolean
385pr_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
400static boolean
401pr_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
416static boolean
417pr_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
427static boolean
428pr_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
438static boolean
439pr_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
453static boolean
454pr_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
472static boolean
473pr_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
487static boolean
488pr_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
502static boolean
503pr_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
563static boolean
564pr_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
580static boolean
581pr_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
657static boolean
658pr_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
670static boolean
671pr_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
252b5132
RH
696static boolean
697pr_array_type (p, lower, upper, stringp)
698 PTR p;
699 bfd_signed_vma lower;
700 bfd_signed_vma upper;
701 boolean stringp;
702{
703 struct pr_handle *info = (struct pr_handle *) p;
704 char *range_type;
705 char abl[20], abu[20], ab[50];
706
707 range_type = pop_type (info);
708 if (range_type == NULL)
709 return false;
710
711 if (lower == 0)
712 {
713 if (upper == -1)
714 sprintf (ab, "|[]");
715 else
716 {
717 print_vma (upper + 1, abu, false, false);
718 sprintf (ab, "|[%s]", abu);
719 }
720 }
721 else
722 {
723 print_vma (lower, abl, false, false);
724 print_vma (upper, abu, false, false);
725 sprintf (ab, "|[%s:%s]", abl, abu);
726 }
727
728 if (! substitute_type (info, ab))
729 return false;
730
731 if (strcmp (range_type, "int") != 0)
732 {
733 if (! append_type (info, ":")
734 || ! append_type (info, range_type))
735 return false;
736 }
737
738 if (stringp)
739 {
740 if (! append_type (info, " /* string */"))
741 return false;
742 }
743
744 return true;
745}
746
747/* Make a set type. */
748
252b5132
RH
749static boolean
750pr_set_type (p, bitstringp)
751 PTR p;
752 boolean bitstringp;
753{
754 struct pr_handle *info = (struct pr_handle *) p;
755
756 if (! substitute_type (info, ""))
757 return false;
758
759 if (! prepend_type (info, "set { ")
760 || ! append_type (info, " }"))
761 return false;
762
763 if (bitstringp)
764 {
765 if (! append_type (info, "/* bitstring */"))
766 return false;
767 }
768
769 return true;
770}
771
772/* Make an offset type. */
773
774static boolean
775pr_offset_type (p)
776 PTR p;
777{
778 struct pr_handle *info = (struct pr_handle *) p;
779 char *t;
780
781 if (! substitute_type (info, ""))
782 return false;
783
784 t = pop_type (info);
785 if (t == NULL)
786 return false;
787
788 return (substitute_type (info, "")
789 && prepend_type (info, " ")
790 && prepend_type (info, t)
791 && append_type (info, "::|"));
792}
793
794/* Make a method type. */
795
796static boolean
797pr_method_type (p, domain, argcount, varargs)
798 PTR p;
799 boolean domain;
800 int argcount;
801 boolean varargs;
802{
803 struct pr_handle *info = (struct pr_handle *) p;
804 unsigned int len;
805 char *domain_type;
806 char **arg_types;
807 char *s;
808
809 len = 10;
810
811 if (! domain)
812 domain_type = NULL;
813 else
814 {
815 if (! substitute_type (info, ""))
816 return false;
817 domain_type = pop_type (info);
818 if (domain_type == NULL)
819 return false;
820 if (strncmp (domain_type, "class ", sizeof "class " - 1) == 0
821 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
822 domain_type += sizeof "class " - 1;
823 else if (strncmp (domain_type, "union class ",
824 sizeof "union class ") == 0
825 && (strchr (domain_type + sizeof "union class " - 1, ' ')
826 == NULL))
827 domain_type += sizeof "union class " - 1;
828 len += strlen (domain_type);
829 }
830
831 if (argcount <= 0)
832 {
833 arg_types = NULL;
834 len += 15;
835 }
836 else
837 {
838 int i;
839
840 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
841 for (i = argcount - 1; i >= 0; i--)
842 {
843 if (! substitute_type (info, ""))
844 return false;
845 arg_types[i] = pop_type (info);
846 if (arg_types[i] == NULL)
847 return false;
848 len += strlen (arg_types[i]) + 2;
849 }
850 if (varargs)
851 len += 5;
852 }
853
854 /* Now the return type is on the top of the stack. */
855
856 s = (char *) xmalloc (len);
857 if (! domain)
858 *s = '\0';
859 else
860 strcpy (s, domain_type);
861 strcat (s, "::| (");
862
863 if (argcount < 0)
864 strcat (s, "/* unknown */");
865 else
866 {
867 int i;
868
869 for (i = 0; i < argcount; i++)
870 {
871 if (i > 0)
872 strcat (s, ", ");
873 strcat (s, arg_types[i]);
874 }
875 if (varargs)
876 {
877 if (i > 0)
878 strcat (s, ", ");
879 strcat (s, "...");
880 }
881 if (argcount > 0)
882 free (arg_types);
883 }
884
885 strcat (s, ")");
886
887 if (! substitute_type (info, s))
888 return false;
889
890 free (s);
891
892 return true;
893}
894
895/* Make a const qualified type. */
896
897static boolean
898pr_const_type (p)
899 PTR p;
900{
901 struct pr_handle *info = (struct pr_handle *) p;
902
903 return substitute_type (info, "const |");
904}
905
906/* Make a volatile qualified type. */
907
908static boolean
909pr_volatile_type (p)
910 PTR p;
911{
912 struct pr_handle *info = (struct pr_handle *) p;
913
914 return substitute_type (info, "volatile |");
915}
916
917/* Start accumulating a struct type. */
918
919static boolean
920pr_start_struct_type (p, tag, id, structp, size)
921 PTR p;
922 const char *tag;
923 unsigned int id;
924 boolean structp;
925 unsigned int size;
926{
927 struct pr_handle *info = (struct pr_handle *) p;
928
929 info->indent += 2;
930
931 if (! push_type (info, structp ? "struct " : "union "))
932 return false;
933 if (tag != NULL)
934 {
935 if (! append_type (info, tag))
936 return false;
937 }
938 else
939 {
940 char idbuf[20];
941
942 sprintf (idbuf, "%%anon%u", id);
943 if (! append_type (info, idbuf))
944 return false;
945 }
946
947 if (! append_type (info, " {"))
948 return false;
949 if (size != 0 || tag != NULL)
950 {
951 char ab[30];
952
953 if (! append_type (info, " /*"))
954 return false;
955
956 if (size != 0)
957 {
958 sprintf (ab, " size %u", size);
959 if (! append_type (info, ab))
960 return false;
961 }
962 if (tag != NULL)
963 {
964 sprintf (ab, " id %u", id);
965 if (! append_type (info, ab))
966 return false;
967 }
968 if (! append_type (info, " */"))
969 return false;
970 }
971 if (! append_type (info, "\n"))
972 return false;
973
974 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
975
976 return indent_type (info);
977}
978
979/* Output the visibility of a field in a struct. */
980
981static boolean
982pr_fix_visibility (info, visibility)
983 struct pr_handle *info;
984 enum debug_visibility visibility;
985{
b4c96d0d 986 const char *s = NULL;
252b5132
RH
987 char *t;
988 unsigned int len;
989
990 assert (info->stack != NULL);
991
992 if (info->stack->visibility == visibility)
993 return true;
994
995 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
996
997 switch (visibility)
998 {
999 case DEBUG_VISIBILITY_PUBLIC:
1000 s = "public";
1001 break;
1002 case DEBUG_VISIBILITY_PRIVATE:
1003 s = "private";
1004 break;
1005 case DEBUG_VISIBILITY_PROTECTED:
1006 s = "protected";
1007 break;
1008 case DEBUG_VISIBILITY_IGNORE:
1009 s = "/* ignore */";
1010 break;
1011 default:
1012 abort ();
1013 return false;
1014 }
1015
1016 /* Trim off a trailing space in the struct string, to make the
1017 output look a bit better, then stick on the visibility string. */
1018
1019 t = info->stack->type;
1020 len = strlen (t);
1021 assert (t[len - 1] == ' ');
1022 t[len - 1] = '\0';
1023
1024 if (! append_type (info, s)
1025 || ! append_type (info, ":\n")
1026 || ! indent_type (info))
1027 return false;
1028
1029 info->stack->visibility = visibility;
1030
1031 return true;
1032}
1033
1034/* Add a field to a struct type. */
1035
1036static boolean
1037pr_struct_field (p, name, bitpos, bitsize, visibility)
1038 PTR p;
1039 const char *name;
1040 bfd_vma bitpos;
1041 bfd_vma bitsize;
1042 enum debug_visibility visibility;
1043{
1044 struct pr_handle *info = (struct pr_handle *) p;
1045 char ab[20];
1046 char *t;
1047
1048 if (! substitute_type (info, name))
1049 return false;
1050
1051 if (! append_type (info, "; /* "))
1052 return false;
1053
1054 if (bitsize != 0)
1055 {
1056 print_vma (bitsize, ab, true, false);
1057 if (! append_type (info, "bitsize ")
1058 || ! append_type (info, ab)
1059 || ! append_type (info, ", "))
1060 return false;
1061 }
1062
1063 print_vma (bitpos, ab, true, false);
1064 if (! append_type (info, "bitpos ")
1065 || ! append_type (info, ab)
1066 || ! append_type (info, " */\n")
1067 || ! indent_type (info))
1068 return false;
1069
1070 t = pop_type (info);
1071 if (t == NULL)
1072 return false;
1073
1074 if (! pr_fix_visibility (info, visibility))
1075 return false;
1076
1077 return append_type (info, t);
1078}
1079
1080/* Finish a struct type. */
1081
1082static boolean
1083pr_end_struct_type (p)
1084 PTR p;
1085{
1086 struct pr_handle *info = (struct pr_handle *) p;
1087 char *s;
1088
1089 assert (info->stack != NULL);
1090 assert (info->indent >= 2);
1091
1092 info->indent -= 2;
1093
1094 /* Change the trailing indentation to have a close brace. */
1095 s = info->stack->type + strlen (info->stack->type) - 2;
1096 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1097
1098 *s++ = '}';
1099 *s = '\0';
1100
1101 return true;
1102}
1103
1104/* Start a class type. */
1105
1106static boolean
1107pr_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1108 PTR p;
1109 const char *tag;
1110 unsigned int id;
1111 boolean structp;
1112 unsigned int size;
1113 boolean vptr;
1114 boolean ownvptr;
1115{
1116 struct pr_handle *info = (struct pr_handle *) p;
1117 char *tv = NULL;
1118
1119 info->indent += 2;
1120
1121 if (vptr && ! ownvptr)
1122 {
1123 tv = pop_type (info);
1124 if (tv == NULL)
1125 return false;
1126 }
1127
1128 if (! push_type (info, structp ? "class " : "union class "))
1129 return false;
1130 if (tag != NULL)
1131 {
1132 if (! append_type (info, tag))
1133 return false;
1134 }
1135 else
1136 {
1137 char idbuf[20];
1138
1139 sprintf (idbuf, "%%anon%u", id);
1140 if (! append_type (info, idbuf))
1141 return false;
1142 }
1143
1144 if (! append_type (info, " {"))
1145 return false;
1146 if (size != 0 || vptr || ownvptr || tag != NULL)
1147 {
1148 if (! append_type (info, " /*"))
1149 return false;
1150
1151 if (size != 0)
1152 {
1153 char ab[20];
1154
1155 sprintf (ab, "%u", size);
1156 if (! append_type (info, " size ")
1157 || ! append_type (info, ab))
1158 return false;
1159 }
1160
1161 if (vptr)
1162 {
1163 if (! append_type (info, " vtable "))
1164 return false;
1165 if (ownvptr)
1166 {
1167 if (! append_type (info, "self "))
1168 return false;
1169 }
1170 else
1171 {
1172 if (! append_type (info, tv)
1173 || ! append_type (info, " "))
1174 return false;
1175 }
1176 }
1177
1178 if (tag != NULL)
1179 {
1180 char ab[30];
1181
1182 sprintf (ab, " id %u", id);
1183 if (! append_type (info, ab))
1184 return false;
1185 }
1186
1187 if (! append_type (info, " */"))
1188 return false;
1189 }
1190
1191 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1192
1193 return (append_type (info, "\n")
1194 && indent_type (info));
1195}
1196
1197/* Add a static member to a class. */
1198
1199static boolean
1200pr_class_static_member (p, name, physname, visibility)
1201 PTR p;
1202 const char *name;
1203 const char *physname;
1204 enum debug_visibility visibility;
1205{
1206 struct pr_handle *info = (struct pr_handle *) p;
1207 char *t;
1208
1209 if (! substitute_type (info, name))
1210 return false;
1211
1212 if (! prepend_type (info, "static ")
1213 || ! append_type (info, "; /* ")
1214 || ! append_type (info, physname)
1215 || ! append_type (info, " */\n")
1216 || ! indent_type (info))
1217 return false;
1218
1219 t = pop_type (info);
1220 if (t == NULL)
1221 return false;
1222
1223 if (! pr_fix_visibility (info, visibility))
1224 return false;
1225
1226 return append_type (info, t);
1227}
1228
1229/* Add a base class to a class. */
1230
1231static boolean
1232pr_class_baseclass (p, bitpos, virtual, visibility)
1233 PTR p;
1234 bfd_vma bitpos;
1235 boolean virtual;
1236 enum debug_visibility visibility;
1237{
1238 struct pr_handle *info = (struct pr_handle *) p;
1239 char *t;
1240 const char *prefix;
1241 char ab[20];
1242 char *s, *l, *n;
1243
1244 assert (info->stack != NULL && info->stack->next != NULL);
1245
1246 if (! substitute_type (info, ""))
1247 return false;
1248
1249 t = pop_type (info);
1250 if (t == NULL)
1251 return false;
1252
1253 if (strncmp (t, "class ", sizeof "class " - 1) == 0)
1254 t += sizeof "class " - 1;
1255
1256 /* Push it back on to take advantage of the prepend_type and
1257 append_type routines. */
1258 if (! push_type (info, t))
1259 return false;
1260
1261 if (virtual)
1262 {
1263 if (! prepend_type (info, "virtual "))
1264 return false;
1265 }
1266
1267 switch (visibility)
1268 {
1269 case DEBUG_VISIBILITY_PUBLIC:
1270 prefix = "public ";
1271 break;
1272 case DEBUG_VISIBILITY_PROTECTED:
1273 prefix = "protected ";
1274 break;
1275 case DEBUG_VISIBILITY_PRIVATE:
1276 prefix = "private ";
1277 break;
1278 default:
1279 prefix = "/* unknown visibility */ ";
1280 break;
1281 }
1282
1283 if (! prepend_type (info, prefix))
1284 return false;
1285
1286 if (bitpos != 0)
1287 {
1288 print_vma (bitpos, ab, true, false);
1289 if (! append_type (info, " /* bitpos ")
1290 || ! append_type (info, ab)
1291 || ! append_type (info, " */"))
1292 return false;
1293 }
1294
1295 /* Now the top of the stack is something like "public A / * bitpos
1296 10 * /". The next element on the stack is something like "class
1297 xx { / * size 8 * /\n...". We want to substitute the top of the
1298 stack in before the {. */
1299 s = strchr (info->stack->next->type, '{');
1300 assert (s != NULL);
1301 --s;
1302
1303 /* If there is already a ':', then we already have a baseclass, and
1304 we must append this one after a comma. */
1305 for (l = info->stack->next->type; l != s; l++)
1306 if (*l == ':')
1307 break;
1308 if (! prepend_type (info, l == s ? " : " : ", "))
1309 return false;
1310
1311 t = pop_type (info);
1312 if (t == NULL)
1313 return false;
1314
1315 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1316 memcpy (n, info->stack->type, s - info->stack->type);
1317 strcpy (n + (s - info->stack->type), t);
1318 strcat (n, s);
1319
1320 free (info->stack->type);
1321 info->stack->type = n;
1322
1323 free (t);
1324
1325 return true;
1326}
1327
1328/* Start adding a method to a class. */
1329
1330static boolean
1331pr_class_start_method (p, name)
1332 PTR p;
1333 const char *name;
1334{
1335 struct pr_handle *info = (struct pr_handle *) p;
1336
1337 assert (info->stack != NULL);
1338 info->stack->method = name;
1339 return true;
1340}
1341
1342/* Add a variant to a method. */
1343
1344static boolean
1345pr_class_method_variant (p, physname, visibility, constp, volatilep, voffset,
1346 context)
1347 PTR p;
1348 const char *physname;
1349 enum debug_visibility visibility;
1350 boolean constp;
1351 boolean volatilep;
1352 bfd_vma voffset;
1353 boolean context;
1354{
1355 struct pr_handle *info = (struct pr_handle *) p;
1356 char *method_type;
1357 char *context_type;
1358
1359 assert (info->stack != NULL);
1360 assert (info->stack->next != NULL);
1361
1362 /* Put the const and volatile qualifiers on the type. */
1363 if (volatilep)
1364 {
1365 if (! append_type (info, " volatile"))
1366 return false;
1367 }
1368 if (constp)
1369 {
1370 if (! append_type (info, " const"))
1371 return false;
1372 }
1373
1374 /* Stick the name of the method into its type. */
1375 if (! substitute_type (info,
1376 (context
1377 ? info->stack->next->next->method
1378 : info->stack->next->method)))
1379 return false;
1380
1381 /* Get the type. */
1382 method_type = pop_type (info);
1383 if (method_type == NULL)
1384 return false;
1385
1386 /* Pull off the context type if there is one. */
1387 if (! context)
1388 context_type = NULL;
1389 else
1390 {
1391 context_type = pop_type (info);
1392 if (context_type == NULL)
1393 return false;
1394 }
1395
1396 /* Now the top of the stack is the class. */
1397
1398 if (! pr_fix_visibility (info, visibility))
1399 return false;
1400
1401 if (! append_type (info, method_type)
1402 || ! append_type (info, " /* ")
1403 || ! append_type (info, physname)
1404 || ! append_type (info, " "))
1405 return false;
1406 if (context || voffset != 0)
1407 {
1408 char ab[20];
1409
1410 if (context)
1411 {
1412 if (! append_type (info, "context ")
1413 || ! append_type (info, context_type)
1414 || ! append_type (info, " "))
1415 return false;
1416 }
1417 print_vma (voffset, ab, true, false);
1418 if (! append_type (info, "voffset ")
1419 || ! append_type (info, ab))
1420 return false;
1421 }
1422
1423 return (append_type (info, " */;\n")
1424 && indent_type (info));
1425}
1426
1427/* Add a static variant to a method. */
1428
1429static boolean
1430pr_class_static_method_variant (p, physname, visibility, constp, volatilep)
1431 PTR p;
1432 const char *physname;
1433 enum debug_visibility visibility;
1434 boolean constp;
1435 boolean volatilep;
1436{
1437 struct pr_handle *info = (struct pr_handle *) p;
1438 char *method_type;
1439
1440 assert (info->stack != NULL);
1441 assert (info->stack->next != NULL);
1442 assert (info->stack->next->method != NULL);
1443
1444 /* Put the const and volatile qualifiers on the type. */
1445 if (volatilep)
1446 {
1447 if (! append_type (info, " volatile"))
1448 return false;
1449 }
1450 if (constp)
1451 {
1452 if (! append_type (info, " const"))
1453 return false;
1454 }
1455
1456 /* Mark it as static. */
1457 if (! prepend_type (info, "static "))
1458 return false;
1459
1460 /* Stick the name of the method into its type. */
1461 if (! substitute_type (info, info->stack->next->method))
1462 return false;
1463
1464 /* Get the type. */
1465 method_type = pop_type (info);
1466 if (method_type == NULL)
1467 return false;
1468
1469 /* Now the top of the stack is the class. */
1470
1471 if (! pr_fix_visibility (info, visibility))
1472 return false;
1473
1474 return (append_type (info, method_type)
1475 && append_type (info, " /* ")
1476 && append_type (info, physname)
1477 && append_type (info, " */;\n")
1478 && indent_type (info));
1479}
1480
1481/* Finish up a method. */
1482
1483static boolean
1484pr_class_end_method (p)
1485 PTR p;
1486{
1487 struct pr_handle *info = (struct pr_handle *) p;
1488
1489 info->stack->method = NULL;
1490 return true;
1491}
1492
1493/* Finish up a class. */
1494
1495static boolean
1496pr_end_class_type (p)
1497 PTR p;
1498{
1499 return pr_end_struct_type (p);
1500}
1501
1502/* Push a type on the stack using a typedef name. */
1503
1504static boolean
1505pr_typedef_type (p, name)
1506 PTR p;
1507 const char *name;
1508{
1509 struct pr_handle *info = (struct pr_handle *) p;
1510
1511 return push_type (info, name);
1512}
1513
1514/* Push a type on the stack using a tag name. */
1515
1516static boolean
1517pr_tag_type (p, name, id, kind)
1518 PTR p;
1519 const char *name;
1520 unsigned int id;
1521 enum debug_type_kind kind;
1522{
1523 struct pr_handle *info = (struct pr_handle *) p;
1524 const char *t, *tag;
1525 char idbuf[20];
1526
1527 switch (kind)
1528 {
1529 case DEBUG_KIND_STRUCT:
1530 t = "struct ";
1531 break;
1532 case DEBUG_KIND_UNION:
1533 t = "union ";
1534 break;
1535 case DEBUG_KIND_ENUM:
1536 t = "enum ";
1537 break;
1538 case DEBUG_KIND_CLASS:
1539 t = "class ";
1540 break;
1541 case DEBUG_KIND_UNION_CLASS:
1542 t = "union class ";
1543 break;
1544 default:
1545 abort ();
1546 return false;
1547 }
1548
1549 if (! push_type (info, t))
1550 return false;
1551 if (name != NULL)
1552 tag = name;
1553 else
1554 {
1555 sprintf (idbuf, "%%anon%u", id);
1556 tag = idbuf;
1557 }
1558
1559 if (! append_type (info, tag))
1560 return false;
1561 if (name != NULL && kind != DEBUG_KIND_ENUM)
1562 {
1563 sprintf (idbuf, " /* id %u */", id);
1564 if (! append_type (info, idbuf))
1565 return false;
1566 }
1567
1568 return true;
1569}
1570
1571/* Output a typedef. */
1572
1573static boolean
1574pr_typdef (p, name)
1575 PTR p;
1576 const char *name;
1577{
1578 struct pr_handle *info = (struct pr_handle *) p;
1579 char *s;
1580
1581 if (! substitute_type (info, name))
1582 return false;
1583
1584 s = pop_type (info);
1585 if (s == NULL)
1586 return false;
1587
1588 indent (info);
1589 fprintf (info->f, "typedef %s;\n", s);
1590
1591 free (s);
1592
1593 return true;
1594}
1595
1596/* Output a tag. The tag should already be in the string on the
1597 stack, so all we have to do here is print it out. */
1598
252b5132
RH
1599static boolean
1600pr_tag (p, name)
1601 PTR p;
b4c96d0d 1602 const char *name ATTRIBUTE_UNUSED;
252b5132
RH
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
1621static boolean
1622pr_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 *) p;
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
1638static boolean
1639pr_float_constant (p, name, val)
1640 PTR p;
1641 const char *name;
1642 double val;
1643{
1644 struct pr_handle *info = (struct pr_handle *) p;
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
1653static boolean
1654pr_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
1678static boolean
1679pr_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
1719static boolean
1720pr_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
1747static boolean
1748pr_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
1790static boolean
1791pr_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
1815static boolean
1816pr_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
1834static boolean
1835pr_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
252b5132
RH
1853static boolean
1854pr_end_function (p)
b4c96d0d 1855 PTR p ATTRIBUTE_UNUSED;
252b5132
RH
1856{
1857 return true;
1858}
This page took 0.208027 seconds and 4 git commands to generate.