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