]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/prdbg.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / prdbg.c
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4 Tags style generation written by Salvador E. Tropea <set@computer.org>.
5
6 This file is part of GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* This file prints out the generic debugging information, by
24 supplying a set of routines to debug_write. */
25
26 #include "sysdep.h"
27 #include <assert.h>
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "demangle.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 /* The following are used only by the tags code (tg_). */
47 /* Name of the file we are using. */
48 char *filename;
49 /* The BFD. */
50 bfd *abfd;
51 /* The symbols table for this BFD. */
52 asymbol **syms;
53 /* Pointer to a function to demangle symbols. */
54 char *(*demangler) (bfd *, const char *, int);
55 };
56
57 /* The type stack. */
58
59 struct pr_stack
60 {
61 /* Next element on the stack. */
62 struct pr_stack *next;
63 /* This element. */
64 char *type;
65 /* Current visibility of fields if this is a class. */
66 enum debug_visibility visibility;
67 /* Name of the current method we are handling. */
68 const char *method;
69 /* The following are used only by the tags code (tg_). */
70 /* Type for the container (struct, union, class, union class). */
71 const char *flavor;
72 /* A comma separated list of parent classes. */
73 char *parents;
74 /* How many parents contains parents. */
75 int num_parents;
76 };
77
78 static void indent (struct pr_handle *);
79 static bool push_type (struct pr_handle *, const char *);
80 static bool prepend_type (struct pr_handle *, const char *);
81 static bool append_type (struct pr_handle *, const char *);
82 static bool substitute_type (struct pr_handle *, const char *);
83 static bool indent_type (struct pr_handle *);
84 static char *pop_type (struct pr_handle *);
85 static void print_vma (bfd_vma, char *, bool, bool);
86 static bool pr_fix_visibility (struct pr_handle *, enum debug_visibility);
87 static bool pr_start_compilation_unit (void *, const char *);
88 static bool pr_start_source (void *, const char *);
89 static bool pr_empty_type (void *);
90 static bool pr_void_type (void *);
91 static bool pr_int_type (void *, unsigned int, bool);
92 static bool pr_float_type (void *, unsigned int);
93 static bool pr_complex_type (void *, unsigned int);
94 static bool pr_bool_type (void *, unsigned int);
95 static bool pr_enum_type
96 (void *, const char *, const char **, bfd_signed_vma *);
97 static bool pr_pointer_type (void *);
98 static bool pr_function_type (void *, int, bool);
99 static bool pr_reference_type (void *);
100 static bool pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
101 static bool pr_array_type (void *, bfd_signed_vma, bfd_signed_vma, bool);
102 static bool pr_set_type (void *, bool);
103 static bool pr_offset_type (void *);
104 static bool pr_method_type (void *, bool, int, bool);
105 static bool pr_const_type (void *);
106 static bool pr_volatile_type (void *);
107 static bool pr_start_struct_type
108 (void *, const char *, unsigned int, bool, unsigned int);
109 static bool pr_struct_field
110 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
111 static bool pr_end_struct_type (void *);
112 static bool pr_start_class_type
113 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
114 static bool pr_class_static_member
115 (void *, const char *, const char *, enum debug_visibility);
116 static bool pr_class_baseclass
117 (void *, bfd_vma, bool, enum debug_visibility);
118 static bool pr_class_start_method (void *, const char *);
119 static bool pr_class_method_variant
120 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
121 static bool pr_class_static_method_variant
122 (void *, const char *, enum debug_visibility, bool, bool);
123 static bool pr_class_end_method (void *);
124 static bool pr_end_class_type (void *);
125 static bool pr_typedef_type (void *, const char *);
126 static bool pr_tag_type
127 (void *, const char *, unsigned int, enum debug_type_kind);
128 static bool pr_typdef (void *, const char *);
129 static bool pr_tag (void *, const char *);
130 static bool pr_int_constant (void *, const char *, bfd_vma);
131 static bool pr_float_constant (void *, const char *, double);
132 static bool pr_typed_constant (void *, const char *, bfd_vma);
133 static bool pr_variable (void *, const char *, enum debug_var_kind, bfd_vma);
134 static bool pr_start_function (void *, const char *, bool);
135 static bool pr_function_parameter
136 (void *, const char *, enum debug_parm_kind, bfd_vma);
137 static bool pr_start_block (void *, bfd_vma);
138 static bool pr_end_block (void *, bfd_vma);
139 static bool pr_end_function (void *);
140 static bool pr_lineno (void *, const char *, unsigned long, bfd_vma);
141 static bool append_parent (struct pr_handle *, const char *);
142 /* Only used by tg_ code. */
143 static bool tg_fix_visibility
144 (struct pr_handle *, enum debug_visibility);
145 static void find_address_in_section (bfd *, asection *, void *);
146 static void translate_addresses (bfd *, char *, FILE *, asymbol **);
147 static const char *visibility_name (enum debug_visibility);
148 /* Tags style replacements. */
149 static bool tg_start_compilation_unit (void *, const char *);
150 static bool tg_start_source (void *, const char *);
151 static bool tg_enum_type
152 (void *, const char *, const char **, bfd_signed_vma *);
153 static bool tg_start_struct_type
154 (void *, const char *, unsigned int, bool, unsigned int);
155 static bool pr_struct_field
156 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
157 static bool tg_struct_field
158 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
159 static bool tg_struct_field
160 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
161 static bool tg_end_struct_type (void *);
162 static bool tg_start_class_type
163 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
164 static bool tg_class_static_member
165 (void *, const char *, const char *, enum debug_visibility);
166 static bool tg_class_baseclass (void *, bfd_vma, bool, enum debug_visibility);
167 static bool tg_class_method_variant
168 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
169 static bool tg_class_static_method_variant
170 (void *, const char *, enum debug_visibility, bool, bool);
171 static bool tg_end_class_type (void *);
172 static bool tg_tag_type
173 (void *, const char *, unsigned int, enum debug_type_kind);
174 static bool tg_typdef (void *, const char *);
175 static bool tg_tag (void *, const char *);
176 static bool tg_int_constant (void *, const char *, bfd_vma);
177 static bool tg_float_constant (void *, const char *, double);
178 static bool tg_typed_constant (void *, const char *, bfd_vma);
179 static bool tg_variable (void *, const char *, enum debug_var_kind, bfd_vma);
180 static bool tg_start_function (void *, const char *, bool);
181 static bool tg_function_parameter
182 (void *, const char *, enum debug_parm_kind, bfd_vma);
183 static bool tg_start_block (void *, bfd_vma);
184 static bool tg_end_block (void *, bfd_vma);
185 static bool tg_lineno (void *, const char *, unsigned long, bfd_vma);
186 \f
187 static const struct debug_write_fns pr_fns =
188 {
189 pr_start_compilation_unit,
190 pr_start_source,
191 pr_empty_type,
192 pr_void_type,
193 pr_int_type,
194 pr_float_type,
195 pr_complex_type,
196 pr_bool_type,
197 pr_enum_type,
198 pr_pointer_type,
199 pr_function_type,
200 pr_reference_type,
201 pr_range_type,
202 pr_array_type,
203 pr_set_type,
204 pr_offset_type,
205 pr_method_type,
206 pr_const_type,
207 pr_volatile_type,
208 pr_start_struct_type,
209 pr_struct_field,
210 pr_end_struct_type,
211 pr_start_class_type,
212 pr_class_static_member,
213 pr_class_baseclass,
214 pr_class_start_method,
215 pr_class_method_variant,
216 pr_class_static_method_variant,
217 pr_class_end_method,
218 pr_end_class_type,
219 pr_typedef_type,
220 pr_tag_type,
221 pr_typdef,
222 pr_tag,
223 pr_int_constant,
224 pr_float_constant,
225 pr_typed_constant,
226 pr_variable,
227 pr_start_function,
228 pr_function_parameter,
229 pr_start_block,
230 pr_end_block,
231 pr_end_function,
232 pr_lineno
233 };
234 \f
235 static const struct debug_write_fns tg_fns =
236 {
237 tg_start_compilation_unit,
238 tg_start_source,
239 pr_empty_type, /* Same, push_type. */
240 pr_void_type, /* Same, push_type. */
241 pr_int_type, /* Same, push_type. */
242 pr_float_type, /* Same, push_type. */
243 pr_complex_type, /* Same, push_type. */
244 pr_bool_type, /* Same, push_type. */
245 tg_enum_type,
246 pr_pointer_type, /* Same, changes to pointer. */
247 pr_function_type, /* Same, push_type. */
248 pr_reference_type, /* Same, changes to reference. */
249 pr_range_type, /* FIXME: What's that?. */
250 pr_array_type, /* Same, push_type. */
251 pr_set_type, /* FIXME: What's that?. */
252 pr_offset_type, /* FIXME: What's that?. */
253 pr_method_type, /* Same. */
254 pr_const_type, /* Same, changes to const. */
255 pr_volatile_type, /* Same, changes to volatile. */
256 tg_start_struct_type,
257 tg_struct_field,
258 tg_end_struct_type,
259 tg_start_class_type,
260 tg_class_static_member,
261 tg_class_baseclass,
262 pr_class_start_method, /* Same, remembers that's a method. */
263 tg_class_method_variant,
264 tg_class_static_method_variant,
265 pr_class_end_method, /* Same, forgets that's a method. */
266 tg_end_class_type,
267 pr_typedef_type, /* Same, just push type. */
268 tg_tag_type,
269 tg_typdef,
270 tg_tag,
271 tg_int_constant, /* Untested. */
272 tg_float_constant, /* Untested. */
273 tg_typed_constant, /* Untested. */
274 tg_variable,
275 tg_start_function,
276 tg_function_parameter,
277 tg_start_block,
278 tg_end_block,
279 pr_end_function, /* Same, does nothing. */
280 tg_lineno
281 };
282
283 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
284 \f
285 /* Print out the generic debugging information recorded in dhandle. */
286
287 bool
288 print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
289 char * (*demangler) (struct bfd *, const char *, int),
290 bool as_tags)
291 {
292 struct pr_handle info;
293
294 info.f = f;
295 info.indent = 0;
296 info.stack = NULL;
297 info.parameter = 0;
298 info.filename = NULL;
299 info.abfd = abfd;
300 info.syms = syms;
301 info.demangler = demangler;
302
303 if (as_tags)
304 {
305 fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
306 fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
307 fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
308 fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
309 }
310
311 return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
312 : debug_write (dhandle, &pr_fns, (void *) & info);
313 }
314 \f
315 /* Indent to the current indentation level. */
316
317 static void
318 indent (struct pr_handle *info)
319 {
320 unsigned int i;
321
322 for (i = 0; i < info->indent; i++)
323 putc (' ', info->f);
324 }
325
326 /* Push a type on the type stack. */
327
328 static bool
329 push_type (struct pr_handle *info, const char *type)
330 {
331 struct pr_stack *n;
332
333 if (type == NULL)
334 return false;
335
336 n = (struct pr_stack *) xmalloc (sizeof *n);
337 memset (n, 0, sizeof *n);
338
339 n->type = xstrdup (type);
340 n->visibility = DEBUG_VISIBILITY_IGNORE;
341 n->method = NULL;
342 n->next = info->stack;
343 info->stack = n;
344
345 return true;
346 }
347
348 /* Prepend a string onto the type on the top of the type stack. */
349
350 static bool
351 prepend_type (struct pr_handle *info, const char *s)
352 {
353 char *n;
354
355 assert (info->stack != NULL);
356
357 n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
358 sprintf (n, "%s%s", s, info->stack->type);
359 free (info->stack->type);
360 info->stack->type = n;
361
362 return true;
363 }
364
365 /* Append a string to the type on the top of the type stack. */
366
367 static bool
368 append_type (struct pr_handle *info, const char *s)
369 {
370 unsigned int len;
371
372 if (s == NULL)
373 return false;
374
375 assert (info->stack != NULL);
376
377 len = strlen (info->stack->type);
378 info->stack->type = (char *) xrealloc (info->stack->type,
379 len + strlen (s) + 1);
380 strcpy (info->stack->type + len, s);
381
382 return true;
383 }
384
385 /* Append a string to the parents on the top of the type stack. */
386
387 static bool
388 append_parent (struct pr_handle *info, const char *s)
389 {
390 unsigned int len;
391
392 if (s == NULL)
393 return false;
394
395 assert (info->stack != NULL);
396
397 len = info->stack->parents ? strlen (info->stack->parents) : 0;
398 info->stack->parents = (char *) xrealloc (info->stack->parents,
399 len + strlen (s) + 1);
400 strcpy (info->stack->parents + len, s);
401
402 return true;
403 }
404
405 /* We use an underscore to indicate where the name should go in a type
406 string. This function substitutes a string for the underscore. If
407 there is no underscore, the name follows the type. */
408
409 static bool
410 substitute_type (struct pr_handle *info, const char *s)
411 {
412 char *u;
413
414 assert (info->stack != NULL);
415
416 u = strchr (info->stack->type, '|');
417 if (u != NULL)
418 {
419 char *n;
420
421 n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
422
423 memcpy (n, info->stack->type, u - info->stack->type);
424 strcpy (n + (u - info->stack->type), s);
425 strcat (n, u + 1);
426
427 free (info->stack->type);
428 info->stack->type = n;
429
430 return true;
431 }
432
433 if (strchr (s, '|') != NULL
434 && (strchr (info->stack->type, '{') != NULL
435 || strchr (info->stack->type, '(') != NULL))
436 {
437 if (! prepend_type (info, "(")
438 || ! append_type (info, ")"))
439 return false;
440 }
441
442 if (*s == '\0')
443 return true;
444
445 return (append_type (info, " ")
446 && append_type (info, s));
447 }
448
449 /* Indent the type at the top of the stack by appending spaces. */
450
451 static bool
452 indent_type (struct pr_handle *info)
453 {
454 unsigned int i;
455
456 for (i = 0; i < info->indent; i++)
457 {
458 if (! append_type (info, " "))
459 return false;
460 }
461
462 return true;
463 }
464
465 /* Pop a type from the type stack. */
466
467 static char *
468 pop_type (struct pr_handle *info)
469 {
470 struct pr_stack *o;
471 char *ret;
472
473 assert (info->stack != NULL);
474
475 o = info->stack;
476 info->stack = o->next;
477 ret = o->type;
478 free (o);
479
480 return ret;
481 }
482
483 /* Print a VMA value into a string. */
484
485 static void
486 print_vma (bfd_vma vma, char *buf, bool unsignedp, bool hexp)
487 {
488 if (sizeof (vma) <= sizeof (unsigned long))
489 {
490 if (hexp)
491 sprintf (buf, "0x%lx", (unsigned long) vma);
492 else if (unsignedp)
493 sprintf (buf, "%lu", (unsigned long) vma);
494 else
495 sprintf (buf, "%ld", (long) vma);
496 }
497 #if BFD_HOST_64BIT_LONG_LONG
498 else if (sizeof (vma) <= sizeof (unsigned long long))
499 {
500 #ifndef __MSVCRT__
501 if (hexp)
502 sprintf (buf, "0x%llx", (unsigned long long) vma);
503 else if (unsignedp)
504 sprintf (buf, "%llu", (unsigned long long) vma);
505 else
506 sprintf (buf, "%lld", (long long) vma);
507 #else
508 if (hexp)
509 sprintf (buf, "0x%I64x", (unsigned long long) vma);
510 else if (unsignedp)
511 sprintf (buf, "%I64u", (unsigned long long) vma);
512 else
513 sprintf (buf, "%I64d", (long long) vma);
514 #endif
515 }
516 #endif
517 else
518 {
519 buf[0] = '0';
520 buf[1] = 'x';
521 sprintf_vma (buf + 2, vma);
522 }
523 }
524 \f
525 /* Start a new compilation unit. */
526
527 static bool
528 pr_start_compilation_unit (void *p, const char *filename)
529 {
530 struct pr_handle *info = (struct pr_handle *) p;
531
532 assert (info->indent == 0);
533
534 fprintf (info->f, "%s:\n", filename);
535
536 return true;
537 }
538
539 /* Start a source file within a compilation unit. */
540
541 static bool
542 pr_start_source (void *p, const char *filename)
543 {
544 struct pr_handle *info = (struct pr_handle *) p;
545
546 assert (info->indent == 0);
547
548 fprintf (info->f, " %s:\n", filename);
549
550 return true;
551 }
552
553 /* Push an empty type onto the type stack. */
554
555 static bool
556 pr_empty_type (void *p)
557 {
558 struct pr_handle *info = (struct pr_handle *) p;
559
560 return push_type (info, "<undefined>");
561 }
562
563 /* Push a void type onto the type stack. */
564
565 static bool
566 pr_void_type (void *p)
567 {
568 struct pr_handle *info = (struct pr_handle *) p;
569
570 return push_type (info, "void");
571 }
572
573 /* Push an integer type onto the type stack. */
574
575 static bool
576 pr_int_type (void *p, unsigned int size, bool unsignedp)
577 {
578 struct pr_handle *info = (struct pr_handle *) p;
579 char ab[40];
580
581 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
582 return push_type (info, ab);
583 }
584
585 /* Push a floating type onto the type stack. */
586
587 static bool
588 pr_float_type (void *p, unsigned int size)
589 {
590 struct pr_handle *info = (struct pr_handle *) p;
591 char ab[40];
592
593 if (size == 4)
594 return push_type (info, "float");
595 else if (size == 8)
596 return push_type (info, "double");
597
598 sprintf (ab, "float%d", size * 8);
599 return push_type (info, ab);
600 }
601
602 /* Push a complex type onto the type stack. */
603
604 static bool
605 pr_complex_type (void *p, unsigned int size)
606 {
607 struct pr_handle *info = (struct pr_handle *) p;
608
609 if (! pr_float_type (p, size))
610 return false;
611
612 return prepend_type (info, "complex ");
613 }
614
615 /* Push a bool type onto the type stack. */
616
617 static bool
618 pr_bool_type (void *p, unsigned int size)
619 {
620 struct pr_handle *info = (struct pr_handle *) p;
621 char ab[40];
622
623 sprintf (ab, "bool%d", size * 8);
624
625 return push_type (info, ab);
626 }
627
628 /* Push an enum type onto the type stack. */
629
630 static bool
631 pr_enum_type (void *p, const char *tag, const char **names,
632 bfd_signed_vma *values)
633 {
634 struct pr_handle *info = (struct pr_handle *) p;
635 unsigned int i;
636 bfd_signed_vma val;
637
638 if (! push_type (info, "enum "))
639 return false;
640 if (tag != NULL)
641 {
642 if (! append_type (info, tag)
643 || ! append_type (info, " "))
644 return false;
645 }
646 if (! append_type (info, "{ "))
647 return false;
648
649 if (names == NULL)
650 {
651 if (! append_type (info, "/* undefined */"))
652 return false;
653 }
654 else
655 {
656 val = 0;
657 for (i = 0; names[i] != NULL; i++)
658 {
659 if (i > 0)
660 {
661 if (! append_type (info, ", "))
662 return false;
663 }
664
665 if (! append_type (info, names[i]))
666 return false;
667
668 if (values[i] != val)
669 {
670 char ab[22];
671
672 print_vma (values[i], ab, false, false);
673 if (! append_type (info, " = ")
674 || ! append_type (info, ab))
675 return false;
676 val = values[i];
677 }
678
679 ++val;
680 }
681 }
682
683 return append_type (info, " }");
684 }
685
686 /* Turn the top type on the stack into a pointer. */
687
688 static bool
689 pr_pointer_type (void *p)
690 {
691 struct pr_handle *info = (struct pr_handle *) p;
692 char *s;
693
694 assert (info->stack != NULL);
695
696 s = strchr (info->stack->type, '|');
697 if (s != NULL && s[1] == '[')
698 return substitute_type (info, "(*|)");
699 return substitute_type (info, "*|");
700 }
701
702 /* Turn the top type on the stack into a function returning that type. */
703
704 static bool
705 pr_function_type (void *p, int argcount, bool varargs)
706 {
707 struct pr_handle *info = (struct pr_handle *) p;
708 char **arg_types;
709 unsigned int len;
710 char *s;
711
712 assert (info->stack != NULL);
713
714 len = 10;
715
716 if (argcount <= 0)
717 {
718 arg_types = NULL;
719 len += 15;
720 }
721 else
722 {
723 int i;
724
725 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
726 for (i = argcount - 1; i >= 0; i--)
727 {
728 if (! substitute_type (info, ""))
729 {
730 free (arg_types);
731 return false;
732 }
733 arg_types[i] = pop_type (info);
734 if (arg_types[i] == NULL)
735 {
736 free (arg_types);
737 return false;
738 }
739 len += strlen (arg_types[i]) + 2;
740 }
741 if (varargs)
742 len += 5;
743 }
744
745 /* Now the return type is on the top of the stack. */
746
747 s = (char *) xmalloc (len);
748 strcpy (s, "(|) (");
749
750 if (argcount < 0)
751 strcat (s, "/* unknown */");
752 else
753 {
754 int i;
755
756 for (i = 0; i < argcount; i++)
757 {
758 if (i > 0)
759 strcat (s, ", ");
760 strcat (s, arg_types[i]);
761 }
762 if (varargs)
763 {
764 if (i > 0)
765 strcat (s, ", ");
766 strcat (s, "...");
767 }
768 if (argcount > 0)
769 free (arg_types);
770 }
771
772 strcat (s, ")");
773
774 if (! substitute_type (info, s))
775 return false;
776
777 free (s);
778
779 return true;
780 }
781
782 /* Turn the top type on the stack into a reference to that type. */
783
784 static bool
785 pr_reference_type (void *p)
786 {
787 struct pr_handle *info = (struct pr_handle *) p;
788
789 assert (info->stack != NULL);
790
791 return substitute_type (info, "&|");
792 }
793
794 /* Make a range type. */
795
796 static bool
797 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
798 {
799 struct pr_handle *info = (struct pr_handle *) p;
800 char abl[22], abu[22];
801
802 assert (info->stack != NULL);
803
804 if (! substitute_type (info, ""))
805 return false;
806
807 print_vma (lower, abl, false, false);
808 print_vma (upper, abu, false, false);
809
810 return (prepend_type (info, "range (")
811 && append_type (info, "):")
812 && append_type (info, abl)
813 && append_type (info, ":")
814 && append_type (info, abu));
815 }
816
817 /* Make an array type. */
818
819 static bool
820 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
821 bool stringp)
822 {
823 struct pr_handle *info = (struct pr_handle *) p;
824 char *range_type;
825 char abl[22], abu[22], ab[50];
826
827 range_type = pop_type (info);
828 if (range_type == NULL)
829 return false;
830
831 if (lower == 0)
832 {
833 if (upper == -1)
834 sprintf (ab, "|[]");
835 else
836 {
837 print_vma (upper + 1, abu, false, false);
838 sprintf (ab, "|[%s]", abu);
839 }
840 }
841 else
842 {
843 print_vma (lower, abl, false, false);
844 print_vma (upper, abu, false, false);
845 sprintf (ab, "|[%s:%s]", abl, abu);
846 }
847
848 if (! substitute_type (info, ab))
849 return false;
850
851 if (strcmp (range_type, "int") != 0)
852 {
853 if (! append_type (info, ":")
854 || ! append_type (info, range_type))
855 return false;
856 }
857
858 if (stringp)
859 {
860 if (! append_type (info, " /* string */"))
861 return false;
862 }
863
864 return true;
865 }
866
867 /* Make a set type. */
868
869 static bool
870 pr_set_type (void *p, bool bitstringp)
871 {
872 struct pr_handle *info = (struct pr_handle *) p;
873
874 if (! substitute_type (info, ""))
875 return false;
876
877 if (! prepend_type (info, "set { ")
878 || ! append_type (info, " }"))
879 return false;
880
881 if (bitstringp)
882 {
883 if (! append_type (info, "/* bitstring */"))
884 return false;
885 }
886
887 return true;
888 }
889
890 /* Make an offset type. */
891
892 static bool
893 pr_offset_type (void *p)
894 {
895 struct pr_handle *info = (struct pr_handle *) p;
896 char *t;
897
898 if (! substitute_type (info, ""))
899 return false;
900
901 t = pop_type (info);
902 if (t == NULL)
903 return false;
904
905 return (substitute_type (info, "")
906 && prepend_type (info, " ")
907 && prepend_type (info, t)
908 && append_type (info, "::|"));
909 }
910
911 /* Make a method type. */
912
913 static bool
914 pr_method_type (void *p, bool domain, int argcount, bool varargs)
915 {
916 struct pr_handle *info = (struct pr_handle *) p;
917 unsigned int len;
918 char *domain_type;
919 char **arg_types;
920 char *s;
921
922 len = 10;
923
924 if (! domain)
925 domain_type = NULL;
926 else
927 {
928 if (! substitute_type (info, ""))
929 return false;
930 domain_type = pop_type (info);
931 if (domain_type == NULL)
932 return false;
933 if (startswith (domain_type, "class ")
934 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
935 domain_type += sizeof "class " - 1;
936 else if (startswith (domain_type, "union class ")
937 && (strchr (domain_type + sizeof "union class " - 1, ' ')
938 == NULL))
939 domain_type += sizeof "union class " - 1;
940 len += strlen (domain_type);
941 }
942
943 if (argcount <= 0)
944 {
945 arg_types = NULL;
946 len += 15;
947 }
948 else
949 {
950 int i;
951
952 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
953 for (i = argcount - 1; i >= 0; i--)
954 {
955 if (! substitute_type (info, ""))
956 {
957 free (arg_types);
958 return false;
959 }
960 arg_types[i] = pop_type (info);
961 if (arg_types[i] == NULL)
962 {
963 free (arg_types);
964 return false;
965 }
966 len += strlen (arg_types[i]) + 2;
967 }
968 if (varargs)
969 len += 5;
970 }
971
972 /* Now the return type is on the top of the stack. */
973
974 s = (char *) xmalloc (len);
975 if (! domain)
976 *s = '\0';
977 else
978 strcpy (s, domain_type);
979 strcat (s, "::| (");
980
981 if (argcount < 0)
982 strcat (s, "/* unknown */");
983 else
984 {
985 int i;
986
987 for (i = 0; i < argcount; i++)
988 {
989 if (i > 0)
990 strcat (s, ", ");
991 strcat (s, arg_types[i]);
992 }
993 if (varargs)
994 {
995 if (i > 0)
996 strcat (s, ", ");
997 strcat (s, "...");
998 }
999 if (argcount > 0)
1000 free (arg_types);
1001 }
1002
1003 strcat (s, ")");
1004
1005 if (! substitute_type (info, s))
1006 return false;
1007
1008 free (s);
1009
1010 return true;
1011 }
1012
1013 /* Make a const qualified type. */
1014
1015 static bool
1016 pr_const_type (void *p)
1017 {
1018 struct pr_handle *info = (struct pr_handle *) p;
1019
1020 return substitute_type (info, "const |");
1021 }
1022
1023 /* Make a volatile qualified type. */
1024
1025 static bool
1026 pr_volatile_type (void *p)
1027 {
1028 struct pr_handle *info = (struct pr_handle *) p;
1029
1030 return substitute_type (info, "volatile |");
1031 }
1032
1033 /* Start accumulating a struct type. */
1034
1035 static bool
1036 pr_start_struct_type (void *p, const char *tag, unsigned int id,
1037 bool structp, unsigned int size)
1038 {
1039 struct pr_handle *info = (struct pr_handle *) p;
1040
1041 info->indent += 2;
1042
1043 if (! push_type (info, structp ? "struct " : "union "))
1044 return false;
1045 if (tag != NULL)
1046 {
1047 if (! append_type (info, tag))
1048 return false;
1049 }
1050 else
1051 {
1052 char idbuf[20];
1053
1054 sprintf (idbuf, "%%anon%u", id);
1055 if (! append_type (info, idbuf))
1056 return false;
1057 }
1058
1059 if (! append_type (info, " {"))
1060 return false;
1061 if (size != 0 || tag != NULL)
1062 {
1063 char ab[30];
1064
1065 if (! append_type (info, " /*"))
1066 return false;
1067
1068 if (size != 0)
1069 {
1070 sprintf (ab, " size %u", size);
1071 if (! append_type (info, ab))
1072 return false;
1073 }
1074 if (tag != NULL)
1075 {
1076 sprintf (ab, " id %u", id);
1077 if (! append_type (info, ab))
1078 return false;
1079 }
1080 if (! append_type (info, " */"))
1081 return false;
1082 }
1083 if (! append_type (info, "\n"))
1084 return false;
1085
1086 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1087
1088 return indent_type (info);
1089 }
1090
1091 /* Output the visibility of a field in a struct. */
1092
1093 static bool
1094 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1095 {
1096 const char *s = NULL;
1097 char *t;
1098 unsigned int len;
1099
1100 assert (info->stack != NULL);
1101
1102 if (info->stack->visibility == visibility)
1103 return true;
1104
1105 switch (visibility)
1106 {
1107 case DEBUG_VISIBILITY_PUBLIC:
1108 s = "public";
1109 break;
1110 case DEBUG_VISIBILITY_PRIVATE:
1111 s = "private";
1112 break;
1113 case DEBUG_VISIBILITY_PROTECTED:
1114 s = "protected";
1115 break;
1116 case DEBUG_VISIBILITY_IGNORE:
1117 s = "/* ignore */";
1118 break;
1119 default:
1120 abort ();
1121 return false;
1122 }
1123
1124 /* Trim off a trailing space in the struct string, to make the
1125 output look a bit better, then stick on the visibility string. */
1126
1127 t = info->stack->type;
1128 len = strlen (t);
1129 assert (t[len - 1] == ' ');
1130 t[len - 1] = '\0';
1131
1132 if (! append_type (info, s)
1133 || ! append_type (info, ":\n")
1134 || ! indent_type (info))
1135 return false;
1136
1137 info->stack->visibility = visibility;
1138
1139 return true;
1140 }
1141
1142 /* Add a field to a struct type. */
1143
1144 static bool
1145 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1146 enum debug_visibility visibility)
1147 {
1148 struct pr_handle *info = (struct pr_handle *) p;
1149 char ab[22];
1150 char *t;
1151
1152 if (! substitute_type (info, name))
1153 return false;
1154
1155 if (! append_type (info, "; /* "))
1156 return false;
1157
1158 if (bitsize != 0)
1159 {
1160 print_vma (bitsize, ab, true, false);
1161 if (! append_type (info, "bitsize ")
1162 || ! append_type (info, ab)
1163 || ! append_type (info, ", "))
1164 return false;
1165 }
1166
1167 print_vma (bitpos, ab, true, false);
1168 if (! append_type (info, "bitpos ")
1169 || ! append_type (info, ab)
1170 || ! append_type (info, " */\n")
1171 || ! indent_type (info))
1172 return false;
1173
1174 t = pop_type (info);
1175 if (t == NULL)
1176 return false;
1177
1178 if (! pr_fix_visibility (info, visibility))
1179 return false;
1180
1181 return append_type (info, t);
1182 }
1183
1184 /* Finish a struct type. */
1185
1186 static bool
1187 pr_end_struct_type (void *p)
1188 {
1189 struct pr_handle *info = (struct pr_handle *) p;
1190 char *s;
1191
1192 assert (info->stack != NULL);
1193 assert (info->indent >= 2);
1194
1195 info->indent -= 2;
1196
1197 /* Change the trailing indentation to have a close brace. */
1198 s = info->stack->type + strlen (info->stack->type) - 2;
1199 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1200
1201 *s++ = '}';
1202 *s = '\0';
1203
1204 return true;
1205 }
1206
1207 /* Start a class type. */
1208
1209 static bool
1210 pr_start_class_type (void *p, const char *tag, unsigned int id,
1211 bool structp, unsigned int size,
1212 bool vptr, bool ownvptr)
1213 {
1214 struct pr_handle *info = (struct pr_handle *) p;
1215 char *tv = NULL;
1216
1217 info->indent += 2;
1218
1219 if (vptr && ! ownvptr)
1220 {
1221 tv = pop_type (info);
1222 if (tv == NULL)
1223 return false;
1224 }
1225
1226 if (! push_type (info, structp ? "class " : "union class "))
1227 return false;
1228 if (tag != NULL)
1229 {
1230 if (! append_type (info, tag))
1231 return false;
1232 }
1233 else
1234 {
1235 char idbuf[20];
1236
1237 sprintf (idbuf, "%%anon%u", id);
1238 if (! append_type (info, idbuf))
1239 return false;
1240 }
1241
1242 if (! append_type (info, " {"))
1243 return false;
1244 if (size != 0 || vptr || ownvptr || tag != NULL)
1245 {
1246 if (! append_type (info, " /*"))
1247 return false;
1248
1249 if (size != 0)
1250 {
1251 char ab[20];
1252
1253 sprintf (ab, "%u", size);
1254 if (! append_type (info, " size ")
1255 || ! append_type (info, ab))
1256 return false;
1257 }
1258
1259 if (vptr)
1260 {
1261 if (! append_type (info, " vtable "))
1262 return false;
1263 if (ownvptr)
1264 {
1265 if (! append_type (info, "self "))
1266 return false;
1267 }
1268 else
1269 {
1270 if (! append_type (info, tv)
1271 || ! append_type (info, " "))
1272 return false;
1273 }
1274 }
1275
1276 if (tag != NULL)
1277 {
1278 char ab[30];
1279
1280 sprintf (ab, " id %u", id);
1281 if (! append_type (info, ab))
1282 return false;
1283 }
1284
1285 if (! append_type (info, " */"))
1286 return false;
1287 }
1288
1289 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1290
1291 return (append_type (info, "\n")
1292 && indent_type (info));
1293 }
1294
1295 /* Add a static member to a class. */
1296
1297 static bool
1298 pr_class_static_member (void *p, const char *name, const char *physname,
1299 enum debug_visibility visibility)
1300 {
1301 struct pr_handle *info = (struct pr_handle *) p;
1302 char *t;
1303
1304 if (! substitute_type (info, name))
1305 return false;
1306
1307 if (! prepend_type (info, "static ")
1308 || ! append_type (info, "; /* ")
1309 || ! append_type (info, physname)
1310 || ! append_type (info, " */\n")
1311 || ! indent_type (info))
1312 return false;
1313
1314 t = pop_type (info);
1315 if (t == NULL)
1316 return false;
1317
1318 if (! pr_fix_visibility (info, visibility))
1319 return false;
1320
1321 return append_type (info, t);
1322 }
1323
1324 /* Add a base class to a class. */
1325
1326 static bool
1327 pr_class_baseclass (void *p, bfd_vma bitpos, bool is_virtual,
1328 enum debug_visibility visibility)
1329 {
1330 struct pr_handle *info = (struct pr_handle *) p;
1331 char *t;
1332 const char *prefix;
1333 char ab[22];
1334 char *s, *l, *n;
1335
1336 assert (info->stack != NULL && info->stack->next != NULL);
1337
1338 if (! substitute_type (info, ""))
1339 return false;
1340
1341 t = pop_type (info);
1342 if (t == NULL)
1343 return false;
1344
1345 if (startswith (t, "class "))
1346 t += sizeof "class " - 1;
1347
1348 /* Push it back on to take advantage of the prepend_type and
1349 append_type routines. */
1350 if (! push_type (info, t))
1351 return false;
1352
1353 if (is_virtual)
1354 {
1355 if (! prepend_type (info, "virtual "))
1356 return false;
1357 }
1358
1359 switch (visibility)
1360 {
1361 case DEBUG_VISIBILITY_PUBLIC:
1362 prefix = "public ";
1363 break;
1364 case DEBUG_VISIBILITY_PROTECTED:
1365 prefix = "protected ";
1366 break;
1367 case DEBUG_VISIBILITY_PRIVATE:
1368 prefix = "private ";
1369 break;
1370 default:
1371 prefix = "/* unknown visibility */ ";
1372 break;
1373 }
1374
1375 if (! prepend_type (info, prefix))
1376 return false;
1377
1378 if (bitpos != 0)
1379 {
1380 print_vma (bitpos, ab, true, false);
1381 if (! append_type (info, " /* bitpos ")
1382 || ! append_type (info, ab)
1383 || ! append_type (info, " */"))
1384 return false;
1385 }
1386
1387 /* Now the top of the stack is something like "public A / * bitpos
1388 10 * /". The next element on the stack is something like "class
1389 xx { / * size 8 * /\n...". We want to substitute the top of the
1390 stack in before the {. */
1391 s = strchr (info->stack->next->type, '{');
1392 assert (s != NULL);
1393 --s;
1394
1395 /* If there is already a ':', then we already have a baseclass, and
1396 we must append this one after a comma. */
1397 for (l = info->stack->next->type; l != s; l++)
1398 if (*l == ':')
1399 break;
1400 if (! prepend_type (info, l == s ? " : " : ", "))
1401 return false;
1402
1403 t = pop_type (info);
1404 if (t == NULL)
1405 return false;
1406
1407 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1408 memcpy (n, info->stack->type, s - info->stack->type);
1409 strcpy (n + (s - info->stack->type), t);
1410 strcat (n, s);
1411
1412 free (info->stack->type);
1413 info->stack->type = n;
1414
1415 free (t);
1416
1417 return true;
1418 }
1419
1420 /* Start adding a method to a class. */
1421
1422 static bool
1423 pr_class_start_method (void *p, const char *name)
1424 {
1425 struct pr_handle *info = (struct pr_handle *) p;
1426
1427 assert (info->stack != NULL);
1428 info->stack->method = name;
1429 return true;
1430 }
1431
1432 /* Add a variant to a method. */
1433
1434 static bool
1435 pr_class_method_variant (void *p, const char *physname,
1436 enum debug_visibility visibility,
1437 bool constp, bool volatilep,
1438 bfd_vma voffset, bool context)
1439 {
1440 struct pr_handle *info = (struct pr_handle *) p;
1441 char *method_type;
1442 char *context_type;
1443
1444 assert (info->stack != NULL);
1445 assert (info->stack->next != NULL);
1446
1447 /* Put the const and volatile qualifiers on the type. */
1448 if (volatilep)
1449 {
1450 if (! append_type (info, " volatile"))
1451 return false;
1452 }
1453 if (constp)
1454 {
1455 if (! append_type (info, " const"))
1456 return false;
1457 }
1458
1459 /* Stick the name of the method into its type. */
1460 if (! substitute_type (info,
1461 (context
1462 ? info->stack->next->next->method
1463 : info->stack->next->method)))
1464 return false;
1465
1466 /* Get the type. */
1467 method_type = pop_type (info);
1468 if (method_type == NULL)
1469 return false;
1470
1471 /* Pull off the context type if there is one. */
1472 if (! context)
1473 context_type = NULL;
1474 else
1475 {
1476 context_type = pop_type (info);
1477 if (context_type == NULL)
1478 return false;
1479 }
1480
1481 /* Now the top of the stack is the class. */
1482
1483 if (! pr_fix_visibility (info, visibility))
1484 return false;
1485
1486 if (! append_type (info, method_type)
1487 || ! append_type (info, " /* ")
1488 || ! append_type (info, physname)
1489 || ! append_type (info, " "))
1490 return false;
1491 if (context || voffset != 0)
1492 {
1493 char ab[22];
1494
1495 if (context)
1496 {
1497 if (! append_type (info, "context ")
1498 || ! append_type (info, context_type)
1499 || ! append_type (info, " "))
1500 return false;
1501 }
1502 print_vma (voffset, ab, true, false);
1503 if (! append_type (info, "voffset ")
1504 || ! append_type (info, ab))
1505 return false;
1506 }
1507
1508 return (append_type (info, " */;\n")
1509 && indent_type (info));
1510 }
1511
1512 /* Add a static variant to a method. */
1513
1514 static bool
1515 pr_class_static_method_variant (void *p, const char *physname,
1516 enum debug_visibility visibility,
1517 bool constp, bool volatilep)
1518 {
1519 struct pr_handle *info = (struct pr_handle *) p;
1520 char *method_type;
1521
1522 assert (info->stack != NULL);
1523 assert (info->stack->next != NULL);
1524 assert (info->stack->next->method != NULL);
1525
1526 /* Put the const and volatile qualifiers on the type. */
1527 if (volatilep)
1528 {
1529 if (! append_type (info, " volatile"))
1530 return false;
1531 }
1532 if (constp)
1533 {
1534 if (! append_type (info, " const"))
1535 return false;
1536 }
1537
1538 /* Mark it as static. */
1539 if (! prepend_type (info, "static "))
1540 return false;
1541
1542 /* Stick the name of the method into its type. */
1543 if (! substitute_type (info, info->stack->next->method))
1544 return false;
1545
1546 /* Get the type. */
1547 method_type = pop_type (info);
1548 if (method_type == NULL)
1549 return false;
1550
1551 /* Now the top of the stack is the class. */
1552
1553 if (! pr_fix_visibility (info, visibility))
1554 return false;
1555
1556 return (append_type (info, method_type)
1557 && append_type (info, " /* ")
1558 && append_type (info, physname)
1559 && append_type (info, " */;\n")
1560 && indent_type (info));
1561 }
1562
1563 /* Finish up a method. */
1564
1565 static bool
1566 pr_class_end_method (void *p)
1567 {
1568 struct pr_handle *info = (struct pr_handle *) p;
1569
1570 info->stack->method = NULL;
1571 return true;
1572 }
1573
1574 /* Finish up a class. */
1575
1576 static bool
1577 pr_end_class_type (void *p)
1578 {
1579 return pr_end_struct_type (p);
1580 }
1581
1582 /* Push a type on the stack using a typedef name. */
1583
1584 static bool
1585 pr_typedef_type (void *p, const char *name)
1586 {
1587 struct pr_handle *info = (struct pr_handle *) p;
1588
1589 return push_type (info, name);
1590 }
1591
1592 /* Push a type on the stack using a tag name. */
1593
1594 static bool
1595 pr_tag_type (void *p, const char *name, unsigned int id,
1596 enum debug_type_kind kind)
1597 {
1598 struct pr_handle *info = (struct pr_handle *) p;
1599 const char *t, *tag;
1600 char idbuf[22];
1601
1602 switch (kind)
1603 {
1604 case DEBUG_KIND_STRUCT:
1605 t = "struct ";
1606 break;
1607 case DEBUG_KIND_UNION:
1608 t = "union ";
1609 break;
1610 case DEBUG_KIND_ENUM:
1611 t = "enum ";
1612 break;
1613 case DEBUG_KIND_CLASS:
1614 t = "class ";
1615 break;
1616 case DEBUG_KIND_UNION_CLASS:
1617 t = "union class ";
1618 break;
1619 default:
1620 /* PR 25625: Corrupt input can trigger this case. */
1621 return false;
1622 }
1623
1624 if (! push_type (info, t))
1625 return false;
1626 if (name != NULL)
1627 tag = name;
1628 else
1629 {
1630 sprintf (idbuf, "%%anon%u", id);
1631 tag = idbuf;
1632 }
1633
1634 if (! append_type (info, tag))
1635 return false;
1636 if (name != NULL && kind != DEBUG_KIND_ENUM)
1637 {
1638 sprintf (idbuf, " /* id %u */", id);
1639 if (! append_type (info, idbuf))
1640 return false;
1641 }
1642
1643 return true;
1644 }
1645
1646 /* Output a typedef. */
1647
1648 static bool
1649 pr_typdef (void *p, const char *name)
1650 {
1651 struct pr_handle *info = (struct pr_handle *) p;
1652 char *s;
1653
1654 if (! substitute_type (info, name))
1655 return false;
1656
1657 s = pop_type (info);
1658 if (s == NULL)
1659 return false;
1660
1661 indent (info);
1662 fprintf (info->f, "typedef %s;\n", s);
1663
1664 free (s);
1665
1666 return true;
1667 }
1668
1669 /* Output a tag. The tag should already be in the string on the
1670 stack, so all we have to do here is print it out. */
1671
1672 static bool
1673 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1674 {
1675 struct pr_handle *info = (struct pr_handle *) p;
1676 char *t;
1677
1678 t = pop_type (info);
1679 if (t == NULL)
1680 return false;
1681
1682 indent (info);
1683 fprintf (info->f, "%s;\n", t);
1684
1685 free (t);
1686
1687 return true;
1688 }
1689
1690 /* Output an integer constant. */
1691
1692 static bool
1693 pr_int_constant (void *p, const char *name, bfd_vma val)
1694 {
1695 struct pr_handle *info = (struct pr_handle *) p;
1696 char ab[22];
1697
1698 indent (info);
1699 print_vma (val, ab, false, false);
1700 fprintf (info->f, "const int %s = %s;\n", name, ab);
1701 return true;
1702 }
1703
1704 /* Output a floating point constant. */
1705
1706 static bool
1707 pr_float_constant (void *p, const char *name, double val)
1708 {
1709 struct pr_handle *info = (struct pr_handle *) p;
1710
1711 indent (info);
1712 fprintf (info->f, "const double %s = %g;\n", name, val);
1713 return true;
1714 }
1715
1716 /* Output a typed constant. */
1717
1718 static bool
1719 pr_typed_constant (void *p, const char *name, bfd_vma val)
1720 {
1721 struct pr_handle *info = (struct pr_handle *) p;
1722 char *t;
1723 char ab[22];
1724
1725 t = pop_type (info);
1726 if (t == NULL)
1727 return false;
1728
1729 indent (info);
1730 print_vma (val, ab, false, false);
1731 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1732
1733 free (t);
1734
1735 return true;
1736 }
1737
1738 /* Output a variable. */
1739
1740 static bool
1741 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1742 bfd_vma val)
1743 {
1744 struct pr_handle *info = (struct pr_handle *) p;
1745 char *t;
1746 char ab[22];
1747
1748 if (! substitute_type (info, name))
1749 return false;
1750
1751 t = pop_type (info);
1752 if (t == NULL)
1753 return false;
1754
1755 indent (info);
1756 switch (kind)
1757 {
1758 case DEBUG_STATIC:
1759 case DEBUG_LOCAL_STATIC:
1760 fprintf (info->f, "static ");
1761 break;
1762 case DEBUG_REGISTER:
1763 fprintf (info->f, "register ");
1764 break;
1765 default:
1766 break;
1767 }
1768 print_vma (val, ab, true, true);
1769 fprintf (info->f, "%s /* %s */;\n", t, ab);
1770
1771 free (t);
1772
1773 return true;
1774 }
1775
1776 /* Start outputting a function. */
1777
1778 static bool
1779 pr_start_function (void *p, const char *name, bool global)
1780 {
1781 struct pr_handle *info = (struct pr_handle *) p;
1782 char *t;
1783
1784 if (! substitute_type (info, name))
1785 return false;
1786
1787 t = pop_type (info);
1788 if (t == NULL)
1789 return false;
1790
1791 indent (info);
1792 if (! global)
1793 fprintf (info->f, "static ");
1794 fprintf (info->f, "%s (", t);
1795
1796 info->parameter = 1;
1797
1798 return true;
1799 }
1800
1801 /* Output a function parameter. */
1802
1803 static bool
1804 pr_function_parameter (void *p, const char *name,
1805 enum debug_parm_kind kind, bfd_vma val)
1806 {
1807 struct pr_handle *info = (struct pr_handle *) p;
1808 char *t;
1809 char ab[22];
1810
1811 if (kind == DEBUG_PARM_REFERENCE
1812 || kind == DEBUG_PARM_REF_REG)
1813 {
1814 if (! pr_reference_type (p))
1815 return false;
1816 }
1817
1818 if (! substitute_type (info, name))
1819 return false;
1820
1821 t = pop_type (info);
1822 if (t == NULL)
1823 return false;
1824
1825 if (info->parameter != 1)
1826 fprintf (info->f, ", ");
1827
1828 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1829 fprintf (info->f, "register ");
1830
1831 print_vma (val, ab, true, true);
1832 fprintf (info->f, "%s /* %s */", t, ab);
1833
1834 free (t);
1835
1836 ++info->parameter;
1837
1838 return true;
1839 }
1840
1841 /* Start writing out a block. */
1842
1843 static bool
1844 pr_start_block (void *p, bfd_vma addr)
1845 {
1846 struct pr_handle *info = (struct pr_handle *) p;
1847 char ab[22];
1848
1849 if (info->parameter > 0)
1850 {
1851 fprintf (info->f, ")\n");
1852 info->parameter = 0;
1853 }
1854
1855 indent (info);
1856 print_vma (addr, ab, true, true);
1857 fprintf (info->f, "{ /* %s */\n", ab);
1858
1859 info->indent += 2;
1860
1861 return true;
1862 }
1863
1864 /* Write out line number information. */
1865
1866 static bool
1867 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1868 {
1869 struct pr_handle *info = (struct pr_handle *) p;
1870 char ab[22];
1871
1872 indent (info);
1873 print_vma (addr, ab, true, true);
1874 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1875
1876 return true;
1877 }
1878
1879 /* Finish writing out a block. */
1880
1881 static bool
1882 pr_end_block (void *p, bfd_vma addr)
1883 {
1884 struct pr_handle *info = (struct pr_handle *) p;
1885 char ab[22];
1886
1887 info->indent -= 2;
1888
1889 indent (info);
1890 print_vma (addr, ab, true, true);
1891 fprintf (info->f, "} /* %s */\n", ab);
1892
1893 return true;
1894 }
1895
1896 /* Finish writing out a function. */
1897
1898 static bool
1899 pr_end_function (void *p ATTRIBUTE_UNUSED)
1900 {
1901 return true;
1902 }
1903 \f
1904 /* Tags style generation functions start here. */
1905
1906 /* Variables for address to line translation. */
1907 static bfd_vma pc;
1908 static const char *filename;
1909 static const char *functionname;
1910 static unsigned int line;
1911 static bool found;
1912
1913 /* Look for an address in a section. This is called via
1914 bfd_map_over_sections. */
1915
1916 static void
1917 find_address_in_section (bfd *abfd, asection *section, void *data)
1918 {
1919 bfd_vma vma;
1920 bfd_size_type size;
1921 asymbol **syms = (asymbol **) data;
1922
1923 if (found)
1924 return;
1925
1926 if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
1927 return;
1928
1929 vma = bfd_section_vma (section);
1930 if (pc < vma)
1931 return;
1932
1933 size = bfd_section_size (section);
1934 if (pc >= vma + size)
1935 return;
1936
1937 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1938 &filename, &functionname, &line);
1939 }
1940
1941 static void
1942 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1943 {
1944 pc = bfd_scan_vma (addr_hex, NULL, 16);
1945 found = false;
1946 bfd_map_over_sections (abfd, find_address_in_section, syms);
1947
1948 if (! found)
1949 fprintf (f, "??");
1950 else
1951 fprintf (f, "%u", line);
1952 }
1953
1954 /* Start a new compilation unit. */
1955
1956 static bool
1957 tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
1958 {
1959 struct pr_handle *info = (struct pr_handle *) p;
1960
1961 free (info->filename);
1962 /* Should it be relative? best way to do it here?. */
1963 info->filename = strdup (fname);
1964
1965 return true;
1966 }
1967
1968 /* Start a source file within a compilation unit. */
1969
1970 static bool
1971 tg_start_source (void *p, const char *fname)
1972 {
1973 struct pr_handle *info = (struct pr_handle *) p;
1974
1975 free (info->filename);
1976 /* Should it be relative? best way to do it here?. */
1977 info->filename = strdup (fname);
1978
1979 return true;
1980 }
1981
1982 /* Push an enum type onto the type stack. */
1983
1984 static bool
1985 tg_enum_type (void *p, const char *tag, const char **names,
1986 bfd_signed_vma *values)
1987 {
1988 struct pr_handle *info = (struct pr_handle *) p;
1989 unsigned int i;
1990 const char *name;
1991 char ab[22];
1992
1993 if (! pr_enum_type (p, tag, names, values))
1994 return false;
1995
1996 name = tag ? tag : "unknown";
1997 /* Generate an entry for the enum. */
1998 if (tag)
1999 fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
2000 info->filename, info->stack->type);
2001
2002 /* Generate entries for the values. */
2003 if (names != NULL)
2004 {
2005 for (i = 0; names[i] != NULL; i++)
2006 {
2007 print_vma (values[i], ab, false, false);
2008 fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
2009 names[i], info->filename, name, ab);
2010 }
2011 }
2012
2013 return true;
2014 }
2015
2016 /* Start accumulating a struct type. */
2017
2018 static bool
2019 tg_start_struct_type (void *p, const char *tag, unsigned int id,
2020 bool structp,
2021 unsigned int size ATTRIBUTE_UNUSED)
2022 {
2023 struct pr_handle *info = (struct pr_handle *) p;
2024 const char *name;
2025 char idbuf[20];
2026
2027 if (tag != NULL)
2028 name = tag;
2029 else
2030 {
2031 name = idbuf;
2032 sprintf (idbuf, "%%anon%u", id);
2033 }
2034
2035 if (! push_type (info, name))
2036 return false;
2037
2038 info->stack->flavor = structp ? "struct" : "union";
2039
2040 fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2041 info->stack->flavor[0]);
2042
2043 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2044
2045 return indent_type (info);
2046 }
2047
2048 /* Output the visibility of a field in a struct. */
2049
2050 static bool
2051 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2052 {
2053 assert (info->stack != NULL);
2054
2055 if (info->stack->visibility == visibility)
2056 return true;
2057
2058 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2059
2060 info->stack->visibility = visibility;
2061
2062 return true;
2063 }
2064
2065 /* Add a field to a struct type. */
2066
2067 static bool
2068 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2069 bfd_vma bitsize ATTRIBUTE_UNUSED,
2070 enum debug_visibility visibility)
2071 {
2072 struct pr_handle *info = (struct pr_handle *) p;
2073 char *t;
2074
2075 t = pop_type (info);
2076 if (t == NULL)
2077 return false;
2078
2079 if (! tg_fix_visibility (info, visibility))
2080 return false;
2081
2082 /* It happens, a bug? */
2083 if (! name[0])
2084 return true;
2085
2086 fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2087 name, info->filename, t, info->stack->flavor, info->stack->type,
2088 visibility_name (visibility));
2089
2090 return true;
2091 }
2092
2093 /* Finish a struct type. */
2094
2095 static bool
2096 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2097 {
2098 assert (((struct pr_handle *) p)->stack != NULL);
2099
2100 return true;
2101 }
2102
2103 /* Start a class type. */
2104
2105 static bool
2106 tg_start_class_type (void *p, const char *tag, unsigned int id,
2107 bool structp, unsigned int size,
2108 bool vptr, bool ownvptr)
2109 {
2110 struct pr_handle *info = (struct pr_handle *) p;
2111 char *tv = NULL;
2112 const char *name;
2113 char idbuf[20];
2114
2115 info->indent += 2;
2116
2117 if (vptr && ! ownvptr)
2118 {
2119 tv = pop_type (info);
2120 if (tv == NULL)
2121 return false;
2122 }
2123
2124 if (tag != NULL)
2125 name = tag;
2126 else
2127 {
2128 sprintf (idbuf, "%%anon%u", id);
2129 name = idbuf;
2130 }
2131
2132 if (! push_type (info, name))
2133 return false;
2134
2135 info->stack->flavor = structp ? "class" : "union class";
2136 info->stack->parents = NULL;
2137 info->stack->num_parents = 0;
2138
2139 if (size != 0 || vptr || ownvptr || tag != NULL)
2140 {
2141 if (vptr)
2142 {
2143 if (! append_type (info, " vtable "))
2144 return false;
2145 if (ownvptr)
2146 {
2147 if (! append_type (info, "self "))
2148 return false;
2149 }
2150 else
2151 {
2152 if (! append_type (info, tv)
2153 || ! append_type (info, " "))
2154 return false;
2155 }
2156 }
2157 }
2158
2159 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2160
2161 return true;
2162 }
2163
2164 /* Add a static member to a class. */
2165
2166 static bool
2167 tg_class_static_member (void *p, const char *name,
2168 const char *physname ATTRIBUTE_UNUSED,
2169 enum debug_visibility visibility)
2170 {
2171 struct pr_handle *info = (struct pr_handle *) p;
2172 char *t;
2173 int len_var, len_class;
2174 char *full_name;
2175
2176 len_var = strlen (name);
2177 len_class = strlen (info->stack->next->type);
2178 full_name = (char *) xmalloc (len_var + len_class + 3);
2179 if (! full_name)
2180 return false;
2181 sprintf (full_name, "%s::%s", info->stack->next->type, name);
2182
2183 if (! substitute_type (info, full_name))
2184 {
2185 free (full_name);
2186 return false;
2187 }
2188
2189 if (! prepend_type (info, "static "))
2190 {
2191 free (full_name);
2192 return false;
2193 }
2194
2195 t = pop_type (info);
2196 if (t == NULL)
2197 {
2198 free (full_name);
2199 return false;
2200 }
2201
2202 if (! tg_fix_visibility (info, visibility))
2203 {
2204 free (t);
2205 free (full_name);
2206 return false;
2207 }
2208
2209 fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2210 name, info->filename, t, info->stack->type,
2211 visibility_name (visibility));
2212 free (t);
2213 free (full_name);
2214
2215 return true;
2216 }
2217
2218 /* Add a base class to a class. */
2219
2220 static bool
2221 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2222 bool is_virtual, enum debug_visibility visibility)
2223 {
2224 struct pr_handle *info = (struct pr_handle *) p;
2225 char *t;
2226 const char *prefix;
2227
2228 assert (info->stack != NULL && info->stack->next != NULL);
2229
2230 t = pop_type (info);
2231 if (t == NULL)
2232 return false;
2233
2234 if (startswith (t, "class "))
2235 t += sizeof "class " - 1;
2236
2237 /* Push it back on to take advantage of the prepend_type and
2238 append_type routines. */
2239 if (! push_type (info, t))
2240 return false;
2241
2242 if (is_virtual)
2243 {
2244 if (! prepend_type (info, "virtual "))
2245 return false;
2246 }
2247
2248 switch (visibility)
2249 {
2250 case DEBUG_VISIBILITY_PUBLIC:
2251 prefix = "public ";
2252 break;
2253 case DEBUG_VISIBILITY_PROTECTED:
2254 prefix = "protected ";
2255 break;
2256 case DEBUG_VISIBILITY_PRIVATE:
2257 prefix = "private ";
2258 break;
2259 default:
2260 prefix = "/* unknown visibility */ ";
2261 break;
2262 }
2263
2264 if (! prepend_type (info, prefix))
2265 return false;
2266
2267 t = pop_type (info);
2268 if (t == NULL)
2269 return false;
2270
2271 if (info->stack->num_parents && ! append_parent (info, ", "))
2272 return false;
2273
2274 if (! append_parent (info, t))
2275 return false;
2276 info->stack->num_parents++;
2277
2278 free (t);
2279
2280 return true;
2281 }
2282
2283 /* Add a variant to a method. */
2284
2285 static bool
2286 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2287 enum debug_visibility visibility,
2288 bool constp, bool volatilep,
2289 bfd_vma voffset ATTRIBUTE_UNUSED,
2290 bool context)
2291 {
2292 struct pr_handle *info = (struct pr_handle *) p;
2293 char *method_type;
2294 char *context_type;
2295 char *method_name;
2296
2297 assert (info->stack != NULL);
2298 assert (info->stack->next != NULL);
2299
2300 /* Put the const and volatile qualifiers on the type. */
2301 if (volatilep)
2302 {
2303 if (! append_type (info, " volatile"))
2304 return false;
2305 }
2306 if (constp)
2307 {
2308 if (! append_type (info, " const"))
2309 return false;
2310 }
2311
2312 method_name = strdup (context ? info->stack->next->next->method
2313 : info->stack->next->method);
2314
2315 /* Stick the name of the method into its type. */
2316 if (! substitute_type (info, method_name))
2317 {
2318 free (method_name);
2319 return false;
2320 }
2321
2322 /* Get the type. */
2323 method_type = pop_type (info);
2324 if (method_type == NULL)
2325 {
2326 free (method_name);
2327 return false;
2328 }
2329
2330 /* Pull off the context type if there is one. */
2331 if (! context)
2332 context_type = NULL;
2333 else
2334 {
2335 context_type = pop_type (info);
2336 if (context_type == NULL)
2337 {
2338 free (method_type);
2339 free (method_name);
2340 return false;
2341 }
2342 }
2343
2344 /* Now the top of the stack is the class. */
2345 if (! tg_fix_visibility (info, visibility))
2346 {
2347 free (method_type);
2348 free (method_name);
2349 free (context_type);
2350 return false;
2351 }
2352
2353 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2354 method_name, info->filename, method_type, info->stack->type);
2355 free (method_type);
2356 free (method_name);
2357 free (context_type);
2358
2359 return true;
2360 }
2361
2362 /* Add a static variant to a method. */
2363
2364 static bool
2365 tg_class_static_method_variant (void *p,
2366 const char *physname ATTRIBUTE_UNUSED,
2367 enum debug_visibility visibility,
2368 bool constp, bool volatilep)
2369 {
2370 struct pr_handle *info = (struct pr_handle *) p;
2371 char *method_type;
2372 char *method_name;
2373
2374 assert (info->stack != NULL);
2375 assert (info->stack->next != NULL);
2376 assert (info->stack->next->method != NULL);
2377
2378 /* Put the const and volatile qualifiers on the type. */
2379 if (volatilep)
2380 {
2381 if (! append_type (info, " volatile"))
2382 return false;
2383 }
2384 if (constp)
2385 {
2386 if (! append_type (info, " const"))
2387 return false;
2388 }
2389
2390 /* Mark it as static. */
2391 if (! prepend_type (info, "static "))
2392 return false;
2393
2394 method_name = strdup (info->stack->next->method);
2395 /* Stick the name of the method into its type. */
2396 if (! substitute_type (info, info->stack->next->method))
2397 {
2398 free (method_name);
2399 return false;
2400 }
2401
2402 /* Get the type. */
2403 method_type = pop_type (info);
2404 if (method_type == NULL)
2405 {
2406 free (method_name);
2407 return false;
2408 }
2409
2410 /* Now the top of the stack is the class. */
2411 if (! tg_fix_visibility (info, visibility))
2412 {
2413 free (method_type);
2414 free (method_name);
2415 return false;
2416 }
2417
2418 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2419 method_name, info->filename, method_type, info->stack->type,
2420 visibility_name (visibility));
2421 free (method_type);
2422 free (method_name);
2423
2424 return true;
2425 }
2426
2427 /* Finish up a class. */
2428
2429 static bool
2430 tg_end_class_type (void *p)
2431 {
2432 struct pr_handle *info = (struct pr_handle *) p;
2433
2434 fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2435 info->filename, info->stack->flavor);
2436 if (info->stack->num_parents)
2437 {
2438 fprintf (info->f, "\tinherits:%s", info->stack->parents);
2439 free (info->stack->parents);
2440 }
2441 fputc ('\n', info->f);
2442
2443 return tg_end_struct_type (p);
2444 }
2445
2446 /* Push a type on the stack using a tag name. */
2447
2448 static bool
2449 tg_tag_type (void *p, const char *name, unsigned int id,
2450 enum debug_type_kind kind)
2451 {
2452 struct pr_handle *info = (struct pr_handle *) p;
2453 const char *t, *tag;
2454 char idbuf[20];
2455
2456 switch (kind)
2457 {
2458 case DEBUG_KIND_STRUCT:
2459 t = "struct ";
2460 break;
2461 case DEBUG_KIND_UNION:
2462 t = "union ";
2463 break;
2464 case DEBUG_KIND_ENUM:
2465 t = "enum ";
2466 break;
2467 case DEBUG_KIND_CLASS:
2468 t = "class ";
2469 break;
2470 case DEBUG_KIND_UNION_CLASS:
2471 t = "union class ";
2472 break;
2473 default:
2474 abort ();
2475 return false;
2476 }
2477
2478 if (! push_type (info, t))
2479 return false;
2480 if (name != NULL)
2481 tag = name;
2482 else
2483 {
2484 sprintf (idbuf, "%%anon%u", id);
2485 tag = idbuf;
2486 }
2487
2488 if (! append_type (info, tag))
2489 return false;
2490
2491 return true;
2492 }
2493
2494 /* Output a typedef. */
2495
2496 static bool
2497 tg_typdef (void *p, const char *name)
2498 {
2499 struct pr_handle *info = (struct pr_handle *) p;
2500 char *s;
2501
2502 s = pop_type (info);
2503 if (s == NULL)
2504 return false;
2505
2506 fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2507 info->filename, s);
2508
2509 free (s);
2510
2511 return true;
2512 }
2513
2514 /* Output a tag. The tag should already be in the string on the
2515 stack, so all we have to do here is print it out. */
2516
2517 static bool
2518 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2519 {
2520 struct pr_handle *info = (struct pr_handle *) p;
2521 char *t;
2522
2523 t = pop_type (info);
2524 if (t == NULL)
2525 return false;
2526 free (t);
2527
2528 return true;
2529 }
2530
2531 /* Output an integer constant. */
2532
2533 static bool
2534 tg_int_constant (void *p, const char *name, bfd_vma val)
2535 {
2536 struct pr_handle *info = (struct pr_handle *) p;
2537 char ab[22];
2538
2539 indent (info);
2540 print_vma (val, ab, false, false);
2541 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2542 name, info->filename, ab);
2543 return true;
2544 }
2545
2546 /* Output a floating point constant. */
2547
2548 static bool
2549 tg_float_constant (void *p, const char *name, double val)
2550 {
2551 struct pr_handle *info = (struct pr_handle *) p;
2552
2553 indent (info);
2554 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2555 name, info->filename, val);
2556 return true;
2557 }
2558
2559 /* Output a typed constant. */
2560
2561 static bool
2562 tg_typed_constant (void *p, const char *name, bfd_vma val)
2563 {
2564 struct pr_handle *info = (struct pr_handle *) p;
2565 char *t;
2566 char ab[22];
2567
2568 t = pop_type (info);
2569 if (t == NULL)
2570 return false;
2571
2572 indent (info);
2573 print_vma (val, ab, false, false);
2574 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2575 name, info->filename, t, ab);
2576
2577 free (t);
2578
2579 return true;
2580 }
2581
2582 /* Output a variable. */
2583
2584 static bool
2585 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2586 bfd_vma val ATTRIBUTE_UNUSED)
2587 {
2588 struct pr_handle *info = (struct pr_handle *) p;
2589 char *t, *dname, *from_class;
2590
2591 t = pop_type (info);
2592 if (t == NULL)
2593 return false;
2594
2595 dname = NULL;
2596 if (info->demangler)
2597 dname = info->demangler (info->abfd, name, demangle_flags);
2598
2599 from_class = NULL;
2600 if (dname != NULL)
2601 {
2602 char *sep;
2603 sep = strstr (dname, "::");
2604 if (sep)
2605 {
2606 *sep = 0;
2607 name = sep + 2;
2608 from_class = dname;
2609 }
2610 else
2611 /* Obscure types as vts and type_info nodes. */
2612 name = dname;
2613 }
2614
2615 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2616
2617 switch (kind)
2618 {
2619 case DEBUG_STATIC:
2620 case DEBUG_LOCAL_STATIC:
2621 fprintf (info->f, "\tfile:");
2622 break;
2623 case DEBUG_REGISTER:
2624 fprintf (info->f, "\tregister:");
2625 break;
2626 default:
2627 break;
2628 }
2629
2630 if (from_class)
2631 fprintf (info->f, "\tclass:%s", from_class);
2632
2633 if (dname)
2634 free (dname);
2635
2636 fprintf (info->f, "\n");
2637
2638 free (t);
2639
2640 return true;
2641 }
2642
2643 /* Start outputting a function. */
2644
2645 static bool
2646 tg_start_function (void *p, const char *name, bool global)
2647 {
2648 struct pr_handle *info = (struct pr_handle *) p;
2649 char *dname;
2650
2651 if (! global)
2652 info->stack->flavor = "static";
2653 else
2654 info->stack->flavor = NULL;
2655
2656 dname = NULL;
2657 if (info->demangler)
2658 dname = info->demangler (info->abfd, name, demangle_flags);
2659
2660 if (! substitute_type (info, dname ? dname : name))
2661 return false;
2662
2663 info->stack->method = NULL;
2664 if (dname != NULL)
2665 {
2666 char *sep;
2667 sep = strstr (dname, "::");
2668 if (sep)
2669 {
2670 info->stack->method = dname;
2671 *sep = 0;
2672 name = sep + 2;
2673 }
2674 else
2675 {
2676 info->stack->method = "";
2677 name = dname;
2678 }
2679 sep = strchr (name, '(');
2680 if (sep)
2681 *sep = 0;
2682 /* Obscure functions as type_info function. */
2683 }
2684
2685 info->stack->parents = strdup (name);
2686
2687 if (! info->stack->method && ! append_type (info, "("))
2688 return false;
2689
2690 info->parameter = 1;
2691
2692 return true;
2693 }
2694
2695 /* Output a function parameter. */
2696
2697 static bool
2698 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2699 bfd_vma val ATTRIBUTE_UNUSED)
2700 {
2701 struct pr_handle *info = (struct pr_handle *) p;
2702 char *t;
2703
2704 if (kind == DEBUG_PARM_REFERENCE
2705 || kind == DEBUG_PARM_REF_REG)
2706 {
2707 if (! pr_reference_type (p))
2708 return false;
2709 }
2710
2711 if (! substitute_type (info, name))
2712 return false;
2713
2714 t = pop_type (info);
2715 if (t == NULL)
2716 return false;
2717
2718 if (! info->stack->method)
2719 {
2720 if (info->parameter != 1 && ! append_type (info, ", "))
2721 return false;
2722
2723 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2724 if (! append_type (info, "register "))
2725 return false;
2726
2727 if (! append_type (info, t))
2728 return false;
2729 }
2730
2731 free (t);
2732
2733 ++info->parameter;
2734
2735 return true;
2736 }
2737
2738 /* Start writing out a block. */
2739
2740 static bool
2741 tg_start_block (void *p, bfd_vma addr)
2742 {
2743 struct pr_handle *info = (struct pr_handle *) p;
2744 char ab[22], kind, *partof;
2745 char *t;
2746 bool local;
2747
2748 if (info->parameter > 0)
2749 {
2750 info->parameter = 0;
2751
2752 /* Delayed name. */
2753 fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2754 free (info->stack->parents);
2755
2756 print_vma (addr, ab, true, true);
2757 translate_addresses (info->abfd, ab, info->f, info->syms);
2758 local = info->stack->flavor != NULL;
2759 if (info->stack->method && *info->stack->method)
2760 {
2761 kind = 'm';
2762 partof = (char *) info->stack->method;
2763 }
2764 else
2765 {
2766 kind = 'f';
2767 partof = NULL;
2768 if (! info->stack->method && ! append_type (info, ")"))
2769 return false;
2770 }
2771 t = pop_type (info);
2772 if (t == NULL)
2773 return false;
2774 fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2775 if (local)
2776 fputs ("\tfile:", info->f);
2777 if (partof)
2778 {
2779 fprintf (info->f, "\tclass:%s", partof);
2780 free (partof);
2781 }
2782 fputc ('\n', info->f);
2783 }
2784
2785 return true;
2786 }
2787
2788 /* Write out line number information. */
2789
2790 static bool
2791 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2792 unsigned long lineno ATTRIBUTE_UNUSED,
2793 bfd_vma addr ATTRIBUTE_UNUSED)
2794 {
2795 return true;
2796 }
2797
2798 /* Finish writing out a block. */
2799
2800 static bool
2801 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2802 {
2803 return true;
2804 }
2805
2806 /* Convert the visibility value into a human readable name. */
2807
2808 static const char *
2809 visibility_name (enum debug_visibility visibility)
2810 {
2811 const char *s;
2812
2813 switch (visibility)
2814 {
2815 case DEBUG_VISIBILITY_PUBLIC:
2816 s = "public";
2817 break;
2818 case DEBUG_VISIBILITY_PRIVATE:
2819 s = "private";
2820 break;
2821 case DEBUG_VISIBILITY_PROTECTED:
2822 s = "protected";
2823 break;
2824 case DEBUG_VISIBILITY_IGNORE:
2825 s = "/* ignore */";
2826 break;
2827 default:
2828 abort ();
2829 return false;
2830 }
2831 return s;
2832 }