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