A single-file branch to help me keep things straight while adapting
[deliverable/binutils-gdb.git] / gdb / c-typeprint.c
CommitLineData
c906108c 1/* Support for printing C and C++ types for GDB, the GNU debugger.
b6ba6518
KB
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000
c906108c
SS
4 Free Software Foundation, Inc.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "obstack.h"
25#include "bfd.h" /* Binary File Description */
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "expression.h"
29#include "value.h"
30#include "gdbcore.h"
31#include "target.h"
32#include "command.h"
33#include "gdbcmd.h"
34#include "language.h"
35#include "demangle.h"
36#include "c-lang.h"
37#include "typeprint.h"
9e404285 38#include "cp-abi.h"
c906108c
SS
39
40#include "gdb_string.h"
41#include <errno.h>
c906108c
SS
42
43/* Flag indicating target was compiled by HP compiler */
44extern int hp_som_som_object_present;
45
d9fcf2fb
JM
46static void cp_type_print_method_args (struct type ** args, char *prefix,
47 char *varstring, int staticp,
48 struct ui_file *stream);
392a587b 49
d9fcf2fb 50static void c_type_print_args (struct type *, struct ui_file *);
c906108c 51
d9fcf2fb 52static void cp_type_print_derivation_info (struct ui_file *, struct type *);
c906108c 53
d9fcf2fb
JM
54void c_type_print_varspec_prefix (struct type *, struct ui_file *, int,
55 int);
c906108c 56
d9fcf2fb
JM
57static void c_type_print_cv_qualifier (struct type *, struct ui_file *,
58 int, int);
c5aa993b 59\f
c906108c
SS
60
61
c906108c
SS
62
63/* LEVEL is the depth to indent lines by. */
64
65void
fba45db2
KB
66c_print_type (struct type *type, char *varstring, struct ui_file *stream,
67 int show, int level)
c906108c
SS
68{
69 register enum type_code code;
70 int demangled_args;
71
72 if (show > 0)
73 CHECK_TYPEDEF (type);
74
75 c_type_print_base (type, stream, show, level);
76 code = TYPE_CODE (type);
77 if ((varstring != NULL && *varstring != '\0')
78 ||
c5aa993b
JM
79 /* Need a space if going to print stars or brackets;
80 but not if we will print just a type name. */
c906108c
SS
81 ((show > 0 || TYPE_NAME (type) == 0)
82 &&
83 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
84 || code == TYPE_CODE_METHOD
85 || code == TYPE_CODE_ARRAY
86 || code == TYPE_CODE_MEMBER
87 || code == TYPE_CODE_REF)))
88 fputs_filtered (" ", stream);
89 c_type_print_varspec_prefix (type, stream, show, 0);
90
91 if (varstring != NULL)
92 {
93 fputs_filtered (varstring, stream);
94
95 /* For demangled function names, we have the arglist as part of the name,
c5aa993b 96 so don't print an additional pair of ()'s */
c906108c 97
c5aa993b 98 demangled_args = strchr (varstring, '(') != NULL;
c906108c
SS
99 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
100 }
101}
c5aa993b 102
c906108c
SS
103/* If TYPE is a derived type, then print out derivation information.
104 Print only the actual base classes of this type, not the base classes
105 of the base classes. I.E. for the derivation hierarchy:
106
c5aa993b
JM
107 class A { int a; };
108 class B : public A {int b; };
109 class C : public B {int c; };
c906108c
SS
110
111 Print the type of class C as:
112
c5aa993b
JM
113 class C : public B {
114 int c;
115 }
c906108c
SS
116
117 Not as the following (like gdb used to), which is not legal C++ syntax for
118 derived types and may be confused with the multiple inheritance form:
119
c5aa993b
JM
120 class C : public B : public A {
121 int c;
122 }
c906108c
SS
123
124 In general, gdb should try to print the types as closely as possible to
125 the form that they appear in the source code.
126 Note that in case of protected derivation gcc will not say 'protected'
127 but 'private'. The HP's aCC compiler emits specific information for
128 derivation via protected inheritance, so gdb can print it out */
129
130static void
fba45db2 131cp_type_print_derivation_info (struct ui_file *stream, struct type *type)
c906108c
SS
132{
133 char *name;
134 int i;
135
136 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
137 {
138 fputs_filtered (i == 0 ? ": " : ", ", stream);
139 fprintf_filtered (stream, "%s%s ",
c5aa993b
JM
140 BASETYPE_VIA_PUBLIC (type, i) ? "public"
141 : (TYPE_FIELD_PROTECTED (type, i) ? "protected" : "private"),
142 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
c906108c
SS
143 name = type_name_no_tag (TYPE_BASECLASS (type, i));
144 fprintf_filtered (stream, "%s", name ? name : "(null)");
145 }
146 if (i > 0)
147 {
148 fputs_filtered (" ", stream);
149 }
150}
151/* Print the C++ method arguments ARGS to the file STREAM. */
c5aa993b 152
392a587b 153static void
fba45db2
KB
154cp_type_print_method_args (struct type **args, char *prefix, char *varstring,
155 int staticp, struct ui_file *stream)
c906108c
SS
156{
157 int i;
c5aa993b 158
c906108c
SS
159 fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI);
160 fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI);
161 fputs_filtered ("(", stream);
162 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
163 {
c5aa993b 164 i = !staticp; /* skip the class variable */
c906108c 165 while (1)
c5aa993b
JM
166 {
167 type_print (args[i++], "", stream, 0);
168 if (!args[i])
169 {
170 fprintf_filtered (stream, " ...");
171 break;
172 }
173 else if (args[i]->code != TYPE_CODE_VOID)
174 {
175 fprintf_filtered (stream, ", ");
176 }
177 else
178 break;
179 }
c906108c
SS
180 }
181 else if (current_language->la_language == language_cplus)
182 {
183 fprintf_filtered (stream, "void");
184 }
c5aa993b 185
c906108c
SS
186 fprintf_filtered (stream, ")");
187}
188
189
190/* Print any asterisks or open-parentheses needed before the
191 variable name (to describe its type).
192
193 On outermost call, pass 0 for PASSED_A_PTR.
194 On outermost call, SHOW > 0 means should ignore
195 any typename for TYPE and show its details.
196 SHOW is always zero on recursive calls. */
197
198void
fba45db2
KB
199c_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
200 int show, int passed_a_ptr)
c906108c
SS
201{
202 char *name;
203 if (type == 0)
204 return;
205
206 if (TYPE_NAME (type) && show <= 0)
207 return;
208
209 QUIT;
210
211 switch (TYPE_CODE (type))
212 {
213 case TYPE_CODE_PTR:
214 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
215 fprintf_filtered (stream, "*");
216 c_type_print_cv_qualifier (type, stream, 1, 0);
217 break;
218
219 case TYPE_CODE_MEMBER:
220 if (passed_a_ptr)
221 fprintf_filtered (stream, "(");
222 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
223 fprintf_filtered (stream, " ");
224 name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
225 if (name)
226 fputs_filtered (name, stream);
227 else
c5aa993b 228 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
c906108c
SS
229 fprintf_filtered (stream, "::");
230 break;
231
232 case TYPE_CODE_METHOD:
233 if (passed_a_ptr)
234 fprintf_filtered (stream, "(");
235 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
236 if (passed_a_ptr)
237 {
238 fprintf_filtered (stream, " ");
239 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
240 fprintf_filtered (stream, "::");
241 }
242 break;
243
244 case TYPE_CODE_REF:
245 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
246 fprintf_filtered (stream, "&");
247 c_type_print_cv_qualifier (type, stream, 1, 0);
248 break;
249
250 case TYPE_CODE_FUNC:
251 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
252 if (passed_a_ptr)
253 fprintf_filtered (stream, "(");
254 break;
255
256 case TYPE_CODE_ARRAY:
257 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
258 if (passed_a_ptr)
259 fprintf_filtered (stream, "(");
260 break;
261
262 case TYPE_CODE_UNDEF:
263 case TYPE_CODE_STRUCT:
264 case TYPE_CODE_UNION:
265 case TYPE_CODE_ENUM:
266 case TYPE_CODE_INT:
267 case TYPE_CODE_FLT:
268 case TYPE_CODE_VOID:
269 case TYPE_CODE_ERROR:
270 case TYPE_CODE_CHAR:
271 case TYPE_CODE_BOOL:
272 case TYPE_CODE_SET:
273 case TYPE_CODE_RANGE:
274 case TYPE_CODE_STRING:
275 case TYPE_CODE_BITSTRING:
276 case TYPE_CODE_COMPLEX:
277 case TYPE_CODE_TYPEDEF:
c4093a6a 278 case TYPE_CODE_TEMPLATE:
c906108c 279 /* These types need no prefix. They are listed here so that
c5aa993b 280 gcc -Wall will reveal any types that haven't been handled. */
c906108c 281 break;
c4093a6a
JM
282 default:
283 error ("type not handled in c_type_print_varspec_prefix()");
284 break;
c906108c
SS
285 }
286}
287
288/* Print out "const" and "volatile" attributes.
289 TYPE is a pointer to the type being printed out.
290 STREAM is the output destination.
291 NEED_SPACE = 1 indicates an initial white space is needed */
292
293static void
fba45db2
KB
294c_type_print_cv_qualifier (struct type *type, struct ui_file *stream,
295 int need_pre_space, int need_post_space)
c906108c
SS
296{
297 int flag = 0;
c5aa993b 298
c906108c
SS
299 if (TYPE_CONST (type))
300 {
301 if (need_pre_space)
c5aa993b 302 fprintf_filtered (stream, " ");
c906108c
SS
303 fprintf_filtered (stream, "const");
304 flag = 1;
305 }
c5aa993b 306
c906108c
SS
307 if (TYPE_VOLATILE (type))
308 {
309 if (flag || need_pre_space)
c5aa993b 310 fprintf_filtered (stream, " ");
c906108c
SS
311 fprintf_filtered (stream, "volatile");
312 flag = 1;
313 }
314
315 if (flag && need_post_space)
316 fprintf_filtered (stream, " ");
317}
318
319
320
321
322static void
fba45db2 323c_type_print_args (struct type *type, struct ui_file *stream)
c906108c
SS
324{
325 int i;
326 struct type **args;
327
328 fprintf_filtered (stream, "(");
329 args = TYPE_ARG_TYPES (type);
330 if (args != NULL)
331 {
332 if (args[1] == NULL)
333 {
334 fprintf_filtered (stream, "...");
335 }
336 else if ((args[1]->code == TYPE_CODE_VOID) &&
c5aa993b
JM
337 (current_language->la_language == language_cplus))
338 {
339 fprintf_filtered (stream, "void");
340 }
c906108c
SS
341 else
342 {
343 for (i = 1;
344 args[i] != NULL && args[i]->code != TYPE_CODE_VOID;
345 i++)
346 {
347 c_print_type (args[i], "", stream, -1, 0);
c5aa993b 348 if (args[i + 1] == NULL)
c906108c
SS
349 {
350 fprintf_filtered (stream, "...");
351 }
c5aa993b 352 else if (args[i + 1]->code != TYPE_CODE_VOID)
c906108c
SS
353 {
354 fprintf_filtered (stream, ",");
355 wrap_here (" ");
356 }
357 }
358 }
359 }
360 else if (current_language->la_language == language_cplus)
361 {
362 fprintf_filtered (stream, "void");
363 }
c5aa993b 364
c906108c
SS
365 fprintf_filtered (stream, ")");
366}
367
dfcd3bfb
JM
368
369/* Return true iff the j'th overloading of the i'th method of TYPE
370 is a type conversion operator, like `operator int () { ... }'.
371 When listing a class's methods, we don't print the return type of
372 such operators. */
373static int
374is_type_conversion_operator (struct type *type, int i, int j)
375{
376 /* I think the whole idea of recognizing type conversion operators
377 by their name is pretty terrible. But I don't think our present
378 data structure gives us any other way to tell. If you know of
379 some other way, feel free to rewrite this function. */
380 char *name = TYPE_FN_FIELDLIST_NAME (type, i);
381
382 if (strncmp (name, "operator", 8) != 0)
383 return 0;
384
385 name += 8;
386 if (! strchr (" \t\f\n\r", *name))
387 return 0;
388
389 while (strchr (" \t\f\n\r", *name))
390 name++;
391
392 if (strncmp (name, "new", 3) == 0)
393 name += 3;
394 else if (strncmp (name, "delete", 6) == 0)
395 name += 6;
396 else
397 return 0;
398
399 /* Is that really the end of the name? */
400 if (('a' <= *name && *name <= 'z')
401 || ('A' <= *name && *name <= 'Z')
402 || ('0' <= *name && *name <= '9')
403 || *name == '_')
404 /* No, so the identifier following "operator" must be a type name,
405 and this is a type conversion operator. */
406 return 1;
407
408 /* That was indeed the end of the name, so it was `operator new' or
409 `operator delete', neither of which are type conversion operators. */
410 return 0;
411}
412
413
414/* Given a C++ qualified identifier QID, strip off the qualifiers,
415 yielding the unqualified name. The return value is a pointer into
416 the original string.
417
418 It's a pity we don't have this information in some more structured
419 form. Even the author of this function feels that writing little
420 parsers like this everywhere is stupid. */
421static char *
422remove_qualifiers (char *qid)
423{
424 int quoted = 0; /* zero if we're not in quotes;
425 '"' if we're in a double-quoted string;
426 '\'' if we're in a single-quoted string. */
427 int depth = 0; /* number of unclosed parens we've seen */
428 char *parenstack = (char *) alloca (strlen (qid));
429 char *scan;
430 char *last = 0; /* The character after the rightmost
431 `::' token we've seen so far. */
432
433 for (scan = qid; *scan; scan++)
434 {
435 if (quoted)
436 {
437 if (*scan == quoted)
438 quoted = 0;
439 else if (*scan == '\\' && *(scan + 1))
440 scan++;
441 }
442 else if (scan[0] == ':' && scan[1] == ':')
443 {
444 /* If we're inside parenthesis (i.e., an argument list) or
445 angle brackets (i.e., a list of template arguments), then
446 we don't record the position of this :: token, since it's
447 not relevant to the top-level structure we're trying
448 to operate on. */
449 if (depth == 0)
450 {
451 last = scan + 2;
452 scan++;
453 }
454 }
455 else if (*scan == '"' || *scan == '\'')
456 quoted = *scan;
457 else if (*scan == '(')
458 parenstack[depth++] = ')';
459 else if (*scan == '[')
460 parenstack[depth++] = ']';
461 /* We're going to treat <> as a pair of matching characters,
462 since we're more likely to see those in template id's than
463 real less-than characters. What a crock. */
464 else if (*scan == '<')
465 parenstack[depth++] = '>';
466 else if (*scan == ')' || *scan == ']' || *scan == '>')
467 {
468 if (depth > 0 && parenstack[depth - 1] == *scan)
469 depth--;
470 else
471 {
472 /* We're going to do a little error recovery here. If we
473 don't find a match for *scan on the paren stack, but
474 there is something lower on the stack that does match, we
475 pop the stack to that point. */
476 int i;
477
478 for (i = depth - 1; i >= 0; i--)
479 if (parenstack[i] == *scan)
480 {
481 depth = i;
482 break;
483 }
484 }
485 }
486 }
487
488 if (last)
489 return last;
490 else
491 /* We didn't find any :: tokens at the top level, so declare the
492 whole thing an unqualified identifier. */
493 return qid;
494}
495
496
c906108c
SS
497/* Print any array sizes, function arguments or close parentheses
498 needed after the variable name (to describe its type).
499 Args work like c_type_print_varspec_prefix. */
500
501void
fba45db2
KB
502c_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
503 int show, int passed_a_ptr, int demangled_args)
c906108c
SS
504{
505 if (type == 0)
506 return;
507
508 if (TYPE_NAME (type) && show <= 0)
509 return;
510
511 QUIT;
512
513 switch (TYPE_CODE (type))
514 {
515 case TYPE_CODE_ARRAY:
516 if (passed_a_ptr)
517 fprintf_filtered (stream, ")");
c5aa993b 518
c906108c
SS
519 fprintf_filtered (stream, "[");
520 if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
c5aa993b 521 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
c906108c
SS
522 fprintf_filtered (stream, "%d",
523 (TYPE_LENGTH (type)
524 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
525 fprintf_filtered (stream, "]");
c5aa993b 526
c906108c
SS
527 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
528 break;
529
530 case TYPE_CODE_MEMBER:
531 if (passed_a_ptr)
532 fprintf_filtered (stream, ")");
533 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
534 break;
535
536 case TYPE_CODE_METHOD:
537 if (passed_a_ptr)
538 fprintf_filtered (stream, ")");
539 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
540 if (passed_a_ptr)
541 {
542 c_type_print_args (type, stream);
543 }
544 break;
545
546 case TYPE_CODE_PTR:
547 case TYPE_CODE_REF:
548 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
549 break;
550
551 case TYPE_CODE_FUNC:
552 if (passed_a_ptr)
553 fprintf_filtered (stream, ")");
554 if (!demangled_args)
c5aa993b
JM
555 {
556 int i, len = TYPE_NFIELDS (type);
c906108c 557 fprintf_filtered (stream, "(");
c5aa993b
JM
558 if ((len == 0) && (current_language->la_language == language_cplus))
559 {
560 fprintf_filtered (stream, "void");
561 }
562 else
563 for (i = 0; i < len; i++)
564 {
565 if (i > 0)
566 {
567 fputs_filtered (", ", stream);
568 wrap_here (" ");
569 }
570 c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
571 }
c906108c
SS
572 fprintf_filtered (stream, ")");
573 }
574 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
575 passed_a_ptr, 0);
576 break;
577
578 case TYPE_CODE_UNDEF:
579 case TYPE_CODE_STRUCT:
580 case TYPE_CODE_UNION:
581 case TYPE_CODE_ENUM:
582 case TYPE_CODE_INT:
583 case TYPE_CODE_FLT:
584 case TYPE_CODE_VOID:
585 case TYPE_CODE_ERROR:
586 case TYPE_CODE_CHAR:
587 case TYPE_CODE_BOOL:
588 case TYPE_CODE_SET:
589 case TYPE_CODE_RANGE:
590 case TYPE_CODE_STRING:
591 case TYPE_CODE_BITSTRING:
592 case TYPE_CODE_COMPLEX:
593 case TYPE_CODE_TYPEDEF:
c4093a6a 594 case TYPE_CODE_TEMPLATE:
c906108c 595 /* These types do not need a suffix. They are listed so that
c5aa993b 596 gcc -Wall will report types that may not have been considered. */
c906108c 597 break;
c4093a6a
JM
598 default:
599 error ("type not handled in c_type_print_varspec_suffix()");
600 break;
c906108c
SS
601 }
602}
603
604/* Print the name of the type (or the ultimate pointer target,
605 function value or array element), or the description of a
606 structure or union.
607
608 SHOW positive means print details about the type (e.g. enum values),
609 and print structure elements passing SHOW - 1 for show.
610 SHOW negative means just print the type name or struct tag if there is one.
611 If there is no name, print something sensible but concise like
612 "struct {...}".
613 SHOW zero means just print the type name or struct tag if there is one.
614 If there is no name, print something sensible but not as concise like
615 "struct {int x; int y;}".
616
617 LEVEL is the number of spaces to indent by.
618 We increase it for some recursive calls. */
619
620void
fba45db2
KB
621c_type_print_base (struct type *type, struct ui_file *stream, int show,
622 int level)
c906108c
SS
623{
624 register int i;
625 register int len;
626 register int lastval;
627 char *mangled_name;
628 char *demangled_name;
629 char *demangled_no_static;
c5aa993b
JM
630 enum
631 {
632 s_none, s_public, s_private, s_protected
633 }
634 section_type;
c906108c
SS
635 int need_access_label = 0;
636 int j, len2;
637
638 QUIT;
639
640 wrap_here (" ");
641 if (type == NULL)
642 {
643 fputs_filtered ("<type unknown>", stream);
644 return;
645 }
646
647 /* When SHOW is zero or less, and there is a valid type name, then always
648 just print the type name directly from the type. */
649 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
650 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
651 to expect things like "class5 *foo" rather than "struct class5 *foo". */
652
653 if (show <= 0
654 && TYPE_NAME (type) != NULL)
655 {
656 c_type_print_cv_qualifier (type, stream, 0, 1);
657 fputs_filtered (TYPE_NAME (type), stream);
658 return;
659 }
660
661 CHECK_TYPEDEF (type);
c5aa993b 662
c906108c
SS
663 switch (TYPE_CODE (type))
664 {
665 case TYPE_CODE_TYPEDEF:
666 case TYPE_CODE_ARRAY:
667 case TYPE_CODE_PTR:
668 case TYPE_CODE_MEMBER:
669 case TYPE_CODE_REF:
670 case TYPE_CODE_FUNC:
671 case TYPE_CODE_METHOD:
672 c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
673 break;
674
675 case TYPE_CODE_STRUCT:
676 c_type_print_cv_qualifier (type, stream, 0, 1);
677 /* Note TYPE_CODE_STRUCT and TYPE_CODE_CLASS have the same value,
678 * so we use another means for distinguishing them.
679 */
c5aa993b
JM
680 if (HAVE_CPLUS_STRUCT (type))
681 {
682 switch (TYPE_DECLARED_TYPE (type))
683 {
684 case DECLARED_TYPE_CLASS:
685 fprintf_filtered (stream, "class ");
686 break;
687 case DECLARED_TYPE_UNION:
688 fprintf_filtered (stream, "union ");
689 break;
690 case DECLARED_TYPE_STRUCT:
691 fprintf_filtered (stream, "struct ");
692 break;
693 default:
694 /* If there is a CPLUS_STRUCT, assume class if not
695 * otherwise specified in the declared_type field.
696 */
697 fprintf_filtered (stream, "class ");
698 break;
699 } /* switch */
700 }
701 else
702 {
703 /* If not CPLUS_STRUCT, then assume it's a C struct */
704 fprintf_filtered (stream, "struct ");
705 }
c906108c
SS
706 goto struct_union;
707
708 case TYPE_CODE_UNION:
709 c_type_print_cv_qualifier (type, stream, 0, 1);
710 fprintf_filtered (stream, "union ");
711
712 struct_union:
713
714 /* Print the tag if it exists.
715 * The HP aCC compiler emits
716 * a spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
717 * tag for unnamed struct/union/enum's, which we don't
718 * want to print.
719 */
720 if (TYPE_TAG_NAME (type) != NULL &&
c5aa993b 721 strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
c906108c
SS
722 {
723 fputs_filtered (TYPE_TAG_NAME (type), stream);
724 if (show > 0)
725 fputs_filtered (" ", stream);
726 }
727 wrap_here (" ");
728 if (show < 0)
729 {
730 /* If we just printed a tag name, no need to print anything else. */
731 if (TYPE_TAG_NAME (type) == NULL)
732 fprintf_filtered (stream, "{...}");
733 }
734 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
735 {
736 cp_type_print_derivation_info (stream, type);
c5aa993b 737
c906108c
SS
738 fprintf_filtered (stream, "{\n");
739 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
740 {
741 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
742 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
743 else
744 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
745 }
746
747 /* Start off with no specific section type, so we can print
748 one for the first field we find, and use that section type
749 thereafter until we find another type. */
750
751 section_type = s_none;
752
c5aa993b
JM
753 /* For a class, if all members are private, there's no need
754 for a "private:" label; similarly, for a struct or union
755 masquerading as a class, if all members are public, there's
756 no need for a "public:" label. */
757
758 if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_CLASS) ||
759 (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_TEMPLATE))
760 {
761 QUIT;
762 len = TYPE_NFIELDS (type);
763 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
764 if (!TYPE_FIELD_PRIVATE (type, i))
765 {
766 need_access_label = 1;
767 break;
768 }
769 QUIT;
770 if (!need_access_label)
771 {
772 len2 = TYPE_NFN_FIELDS (type);
773 for (j = 0; j < len2; j++)
774 {
775 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
776 for (i = 0; i < len; i++)
777 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i))
778 {
779 need_access_label = 1;
780 break;
781 }
782 if (need_access_label)
783 break;
784 }
785 }
786 }
787 else if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_STRUCT) ||
788 (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION))
789 {
790 QUIT;
791 len = TYPE_NFIELDS (type);
792 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
793 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
794 {
795 need_access_label = 1;
796 break;
797 }
798 QUIT;
799 if (!need_access_label)
800 {
801 len2 = TYPE_NFN_FIELDS (type);
802 for (j = 0; j < len2; j++)
803 {
804 QUIT;
805 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
806 for (i = 0; i < len; i++)
807 if (TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i) ||
808 TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type, j), i))
809 {
810 need_access_label = 1;
811 break;
812 }
813 if (need_access_label)
814 break;
815 }
816 }
817 }
c906108c
SS
818
819 /* If there is a base class for this type,
820 do not print the field that it occupies. */
821
822 len = TYPE_NFIELDS (type);
823 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
824 {
825 QUIT;
826 /* Don't print out virtual function table. */
c5aa993b
JM
827 /* HP ANSI C++ case */
828 if (TYPE_HAS_VTABLE (type) && (STREQN (TYPE_FIELD_NAME (type, i), "__vfp", 5)))
829 continue;
830 /* Other compilers */
c906108c
SS
831 if (STREQN (TYPE_FIELD_NAME (type, i), "_vptr", 5)
832 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
833 continue;
834
835 /* If this is a C++ class we can print the various C++ section
c5aa993b 836 labels. */
c906108c
SS
837
838 if (HAVE_CPLUS_STRUCT (type) && need_access_label)
839 {
840 if (TYPE_FIELD_PROTECTED (type, i))
841 {
842 if (section_type != s_protected)
843 {
844 section_type = s_protected;
845 fprintfi_filtered (level + 2, stream,
846 "protected:\n");
847 }
848 }
849 else if (TYPE_FIELD_PRIVATE (type, i))
850 {
851 if (section_type != s_private)
852 {
853 section_type = s_private;
854 fprintfi_filtered (level + 2, stream, "private:\n");
855 }
856 }
857 else
858 {
859 if (section_type != s_public)
860 {
861 section_type = s_public;
862 fprintfi_filtered (level + 2, stream, "public:\n");
863 }
864 }
865 }
866
867 print_spaces_filtered (level + 4, stream);
868 if (TYPE_FIELD_STATIC (type, i))
869 {
870 fprintf_filtered (stream, "static ");
871 }
872 c_print_type (TYPE_FIELD_TYPE (type, i),
873 TYPE_FIELD_NAME (type, i),
874 stream, show - 1, level + 4);
875 if (!TYPE_FIELD_STATIC (type, i)
876 && TYPE_FIELD_PACKED (type, i))
877 {
878 /* It is a bitfield. This code does not attempt
879 to look at the bitpos and reconstruct filler,
880 unnamed fields. This would lead to misleading
881 results if the compiler does not put out fields
882 for such things (I don't know what it does). */
883 fprintf_filtered (stream, " : %d",
884 TYPE_FIELD_BITSIZE (type, i));
885 }
886 fprintf_filtered (stream, ";\n");
887 }
888
889 /* If there are both fields and methods, put a space between. */
890 len = TYPE_NFN_FIELDS (type);
891 if (len && section_type != s_none)
c5aa993b 892 fprintf_filtered (stream, "\n");
c906108c
SS
893
894 /* C++: print out the methods */
895 for (i = 0; i < len; i++)
896 {
897 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
898 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
899 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
900 char *name = type_name_no_tag (type);
c5aa993b 901 int is_constructor = name && STREQ (method_name, name);
c906108c
SS
902 for (j = 0; j < len2; j++)
903 {
904 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c5aa993b 905 int is_full_physname_constructor =
9e404285
JB
906 is_constructor_name (physname)
907 || is_destructor_name (physname)
908 || method_name[0] == '~';
909
c906108c
SS
910
911 QUIT;
912 if (TYPE_FN_FIELD_PROTECTED (f, j))
913 {
914 if (section_type != s_protected)
915 {
916 section_type = s_protected;
917 fprintfi_filtered (level + 2, stream,
918 "protected:\n");
919 }
920 }
921 else if (TYPE_FN_FIELD_PRIVATE (f, j))
922 {
923 if (section_type != s_private)
924 {
925 section_type = s_private;
926 fprintfi_filtered (level + 2, stream, "private:\n");
927 }
928 }
929 else
930 {
931 if (section_type != s_public)
932 {
933 section_type = s_public;
934 fprintfi_filtered (level + 2, stream, "public:\n");
935 }
936 }
937
938 print_spaces_filtered (level + 4, stream);
939 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
940 fprintf_filtered (stream, "virtual ");
941 else if (TYPE_FN_FIELD_STATIC_P (f, j))
942 fprintf_filtered (stream, "static ");
943 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
944 {
945 /* Keep GDB from crashing here. */
946 fprintf_filtered (stream, "<undefined type> %s;\n",
c5aa993b 947 TYPE_FN_FIELD_PHYSNAME (f, j));
c906108c
SS
948 break;
949 }
c5aa993b
JM
950 else if (!is_constructor && /* constructors don't have declared types */
951 !is_full_physname_constructor && /* " " */
dfcd3bfb 952 !is_type_conversion_operator (type, i, j))
c906108c
SS
953 {
954 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
955 "", stream, -1);
956 fputs_filtered (" ", stream);
957 }
958 if (TYPE_FN_FIELD_STUB (f, j))
959 /* Build something we can demangle. */
960 mangled_name = gdb_mangle_name (type, i, j);
961 else
962 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
963
964 demangled_name =
965 cplus_demangle (mangled_name,
966 DMGL_ANSI | DMGL_PARAMS);
967 if (demangled_name == NULL)
968 {
969 /* in some cases (for instance with the HP demangling),
c5aa993b
JM
970 if a function has more than 10 arguments,
971 the demangling will fail.
972 Let's try to reconstruct the function signature from
973 the symbol information */
c906108c
SS
974 if (!TYPE_FN_FIELD_STUB (f, j))
975 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
976 method_name,
c5aa993b 977 TYPE_FN_FIELD_STATIC_P (f, j),
c906108c
SS
978 stream);
979 else
980 fprintf_filtered (stream, "<badly mangled name '%s'>",
981 mangled_name);
982 }
983 else
984 {
985 char *p;
dfcd3bfb
JM
986 char *demangled_no_class
987 = remove_qualifiers (demangled_name);
c5aa993b 988
dfcd3bfb 989 /* get rid of the `static' appended by the demangler */
c906108c
SS
990 p = strstr (demangled_no_class, " static");
991 if (p != NULL)
992 {
993 int length = p - demangled_no_class;
994 demangled_no_static = (char *) xmalloc (length + 1);
995 strncpy (demangled_no_static, demangled_no_class, length);
c5aa993b 996 *(demangled_no_static + length) = '\0';
c906108c 997 fputs_filtered (demangled_no_static, stream);
b8c9b27d 998 xfree (demangled_no_static);
c906108c
SS
999 }
1000 else
1001 fputs_filtered (demangled_no_class, stream);
b8c9b27d 1002 xfree (demangled_name);
c906108c
SS
1003 }
1004
1005 if (TYPE_FN_FIELD_STUB (f, j))
b8c9b27d 1006 xfree (mangled_name);
c906108c
SS
1007
1008 fprintf_filtered (stream, ";\n");
1009 }
1010 }
1011
c4093a6a
JM
1012 fprintfi_filtered (level, stream, "}");
1013
c5aa993b
JM
1014 if (TYPE_LOCALTYPE_PTR (type) && show >= 0)
1015 fprintfi_filtered (level, stream, " (Local at %s:%d)\n",
1016 TYPE_LOCALTYPE_FILE (type),
1017 TYPE_LOCALTYPE_LINE (type));
c906108c 1018 }
c5aa993b
JM
1019 if (TYPE_CODE (type) == TYPE_CODE_TEMPLATE)
1020 goto go_back;
c906108c
SS
1021 break;
1022
1023 case TYPE_CODE_ENUM:
1024 c_type_print_cv_qualifier (type, stream, 0, 1);
1025 /* HP C supports sized enums */
1026 if (hp_som_som_object_present)
c5aa993b
JM
1027 switch (TYPE_LENGTH (type))
1028 {
1029 case 1:
1030 fputs_filtered ("char ", stream);
1031 break;
1032 case 2:
1033 fputs_filtered ("short ", stream);
1034 break;
1035 default:
1036 break;
1037 }
1038 fprintf_filtered (stream, "enum ");
c906108c
SS
1039 /* Print the tag name if it exists.
1040 The aCC compiler emits a spurious
1041 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1042 tag for unnamed struct/union/enum's, which we don't
1043 want to print. */
1044 if (TYPE_TAG_NAME (type) != NULL &&
c5aa993b 1045 strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
c906108c
SS
1046 {
1047 fputs_filtered (TYPE_TAG_NAME (type), stream);
1048 if (show > 0)
1049 fputs_filtered (" ", stream);
1050 }
1051
1052 wrap_here (" ");
1053 if (show < 0)
1054 {
1055 /* If we just printed a tag name, no need to print anything else. */
1056 if (TYPE_TAG_NAME (type) == NULL)
1057 fprintf_filtered (stream, "{...}");
1058 }
1059 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1060 {
1061 fprintf_filtered (stream, "{");
1062 len = TYPE_NFIELDS (type);
1063 lastval = 0;
1064 for (i = 0; i < len; i++)
1065 {
1066 QUIT;
c5aa993b
JM
1067 if (i)
1068 fprintf_filtered (stream, ", ");
c906108c
SS
1069 wrap_here (" ");
1070 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1071 if (lastval != TYPE_FIELD_BITPOS (type, i))
1072 {
1073 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1074 lastval = TYPE_FIELD_BITPOS (type, i);
1075 }
1076 lastval++;
1077 }
1078 fprintf_filtered (stream, "}");
1079 }
1080 break;
1081
1082 case TYPE_CODE_VOID:
1083 fprintf_filtered (stream, "void");
1084 break;
1085
1086 case TYPE_CODE_UNDEF:
1087 fprintf_filtered (stream, "struct <unknown>");
1088 break;
1089
1090 case TYPE_CODE_ERROR:
1091 fprintf_filtered (stream, "<unknown type>");
1092 break;
1093
1094 case TYPE_CODE_RANGE:
1095 /* This should not occur */
1096 fprintf_filtered (stream, "<range type>");
1097 break;
1098
1099 case TYPE_CODE_TEMPLATE:
1100 /* Called on "ptype t" where "t" is a template.
1101 Prints the template header (with args), e.g.:
c5aa993b 1102 template <class T1, class T2> class "
c906108c
SS
1103 and then merges with the struct/union/class code to
1104 print the rest of the definition. */
1105 c_type_print_cv_qualifier (type, stream, 0, 1);
1106 fprintf_filtered (stream, "template <");
c5aa993b
JM
1107 for (i = 0; i < TYPE_NTEMPLATE_ARGS (type); i++)
1108 {
1109 struct template_arg templ_arg;
1110 templ_arg = TYPE_TEMPLATE_ARG (type, i);
1111 fprintf_filtered (stream, "class %s", templ_arg.name);
1112 if (i < TYPE_NTEMPLATE_ARGS (type) - 1)
1113 fprintf_filtered (stream, ", ");
1114 }
c906108c
SS
1115 fprintf_filtered (stream, "> class ");
1116 /* Yuck, factor this out to a subroutine so we can call
1117 it and return to the point marked with the "goback:" label... - RT */
c5aa993b
JM
1118 goto struct_union;
1119 go_back:
1120 if (TYPE_NINSTANTIATIONS (type) > 0)
1121 {
1122 fprintf_filtered (stream, "\ntemplate instantiations:\n");
1123 for (i = 0; i < TYPE_NINSTANTIATIONS (type); i++)
1124 {
1125 fprintf_filtered (stream, " ");
1126 c_type_print_base (TYPE_INSTANTIATION (type, i), stream, 0, level);
1127 if (i < TYPE_NINSTANTIATIONS (type) - 1)
1128 fprintf_filtered (stream, "\n");
1129 }
1130 }
c906108c 1131 break;
c5aa993b 1132
c906108c
SS
1133 default:
1134 /* Handle types not explicitly handled by the other cases,
c5aa993b
JM
1135 such as fundamental types. For these, just print whatever
1136 the type name is, as recorded in the type itself. If there
1137 is no type name, then complain. */
c906108c
SS
1138 if (TYPE_NAME (type) != NULL)
1139 {
c5aa993b 1140 c_type_print_cv_qualifier (type, stream, 0, 1);
c906108c
SS
1141 fputs_filtered (TYPE_NAME (type), stream);
1142 }
1143 else
1144 {
1145 /* At least for dump_symtab, it is important that this not be
1146 an error (). */
1147 fprintf_filtered (stream, "<invalid type code %d>",
1148 TYPE_CODE (type));
1149 }
1150 break;
1151 }
1152}
This page took 0.170343 seconds and 4 git commands to generate.