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