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