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