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