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