dwarf2read: Replace copy_string usages with savestring
[deliverable/binutils-gdb.git] / gdb / c-typeprint.c
CommitLineData
c906108c 1/* Support for printing C and C++ types for GDB, the GNU debugger.
61baf725 2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19#include "defs.h"
04ea0df1 20#include "gdb_obstack.h"
aff410f1 21#include "bfd.h" /* Binary File Description. */
c906108c
SS
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "expression.h"
25#include "value.h"
26#include "gdbcore.h"
27#include "target.h"
c906108c
SS
28#include "language.h"
29#include "demangle.h"
30#include "c-lang.h"
31#include "typeprint.h"
015a42b4 32#include "cp-abi.h"
bd69fc68 33#include "cp-support.h"
c906108c 34
aff410f1
MS
35static void c_type_print_varspec_prefix (struct type *,
36 struct ui_file *,
79d43c61
TT
37 int, int, int,
38 const struct type_print_options *);
c906108c 39
aff410f1
MS
40/* Print "const", "volatile", or address space modifiers. */
41static void c_type_print_modifier (struct type *,
42 struct ui_file *,
47663de5 43 int, int);
c5aa993b 44\f
bd69fc68
TT
45
46/* A callback function for cp_canonicalize_string_full that uses
47 find_typedef_in_hash. */
48
49static const char *
50find_typedef_for_canonicalize (struct type *t, void *data)
51{
9a3c8263 52 return find_typedef_in_hash ((const struct type_print_options *) data, t);
bd69fc68
TT
53}
54
55/* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
56 canonicalize NAME using the local typedefs first. */
57
58static void
59print_name_maybe_canonical (const char *name,
60 const struct type_print_options *flags,
61 struct ui_file *stream)
62{
2f408ecb 63 std::string s;
bd69fc68
TT
64
65 if (!flags->raw)
66 s = cp_canonicalize_string_full (name,
67 find_typedef_for_canonicalize,
68 (void *) flags);
69
2f408ecb 70 fputs_filtered (!s.empty () ? s.c_str () : name, stream);
bd69fc68
TT
71}
72
73\f
74
c906108c
SS
75/* LEVEL is the depth to indent lines by. */
76
77void
aff410f1
MS
78c_print_type (struct type *type,
79 const char *varstring,
80 struct ui_file *stream,
79d43c61
TT
81 int show, int level,
82 const struct type_print_options *flags)
c906108c 83{
52f0bd74 84 enum type_code code;
c906108c 85 int demangled_args;
9750e763 86 int need_post_space;
bd69fc68 87 const char *local_name;
c906108c
SS
88
89 if (show > 0)
f168693b 90 type = check_typedef (type);
c906108c 91
bd69fc68
TT
92 local_name = find_typedef_in_hash (flags, type);
93 if (local_name != NULL)
94 {
95 fputs_filtered (local_name, stream);
96 if (varstring != NULL && *varstring != '\0')
97 fputs_filtered (" ", stream);
98 }
99 else
100 {
101 c_type_print_base (type, stream, show, level, flags);
102 code = TYPE_CODE (type);
103 if ((varstring != NULL && *varstring != '\0')
104 /* Need a space if going to print stars or brackets;
105 but not if we will print just a type name. */
106 || ((show > 0 || TYPE_NAME (type) == 0)
107 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
108 || code == TYPE_CODE_METHOD
109 || (code == TYPE_CODE_ARRAY
110 && !TYPE_VECTOR (type))
111 || code == TYPE_CODE_MEMBERPTR
112 || code == TYPE_CODE_METHODPTR
e1cb3213 113 || TYPE_IS_REFERENCE (type))))
bd69fc68
TT
114 fputs_filtered (" ", stream);
115 need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
116 c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
117 flags);
118 }
c906108c
SS
119
120 if (varstring != NULL)
121 {
122 fputs_filtered (varstring, stream);
123
aff410f1 124 /* For demangled function names, we have the arglist as part of
0963b4bd 125 the name, so don't print an additional pair of ()'s. */
bd69fc68
TT
126 if (local_name == NULL)
127 {
128 demangled_args = strchr (varstring, '(') != NULL;
129 c_type_print_varspec_suffix (type, stream, show,
130 0, demangled_args,
131 flags);
132 }
c906108c
SS
133 }
134}
c5aa993b 135
5c6ce71d
TT
136/* Print a typedef using C syntax. TYPE is the underlying type.
137 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
138 which to print. */
139
140void
aff410f1
MS
141c_print_typedef (struct type *type,
142 struct symbol *new_symbol,
5c6ce71d
TT
143 struct ui_file *stream)
144{
f168693b 145 type = check_typedef (type);
5c6ce71d
TT
146 fprintf_filtered (stream, "typedef ");
147 type_print (type, "", stream, 0);
148 if (TYPE_NAME ((SYMBOL_TYPE (new_symbol))) == 0
149 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
b1d61bc9
PM
150 SYMBOL_LINKAGE_NAME (new_symbol)) != 0
151 || TYPE_CODE (SYMBOL_TYPE (new_symbol)) == TYPE_CODE_TYPEDEF)
5c6ce71d
TT
152 fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new_symbol));
153 fprintf_filtered (stream, ";\n");
154}
155
c906108c 156/* If TYPE is a derived type, then print out derivation information.
aff410f1
MS
157 Print only the actual base classes of this type, not the base
158 classes of the base classes. I.e. for the derivation hierarchy:
c906108c 159
c5aa993b
JM
160 class A { int a; };
161 class B : public A {int b; };
162 class C : public B {int c; };
c906108c
SS
163
164 Print the type of class C as:
165
c5aa993b
JM
166 class C : public B {
167 int c;
168 }
c906108c 169
aff410f1
MS
170 Not as the following (like gdb used to), which is not legal C++
171 syntax for derived types and may be confused with the multiple
172 inheritance form:
c906108c 173
c5aa993b
JM
174 class C : public B : public A {
175 int c;
176 }
c906108c 177
aff410f1 178 In general, gdb should try to print the types as closely as
62a49610 179 possible to the form that they appear in the source code. */
c906108c
SS
180
181static void
aff410f1 182cp_type_print_derivation_info (struct ui_file *stream,
bd69fc68
TT
183 struct type *type,
184 const struct type_print_options *flags)
c906108c 185{
0d5cff50 186 const char *name;
c906108c
SS
187 int i;
188
189 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
190 {
bd69fc68 191 wrap_here (" ");
c906108c
SS
192 fputs_filtered (i == 0 ? ": " : ", ", stream);
193 fprintf_filtered (stream, "%s%s ",
aff410f1
MS
194 BASETYPE_VIA_PUBLIC (type, i)
195 ? "public" : (TYPE_FIELD_PROTECTED (type, i)
196 ? "protected" : "private"),
c5aa993b 197 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
c906108c 198 name = type_name_no_tag (TYPE_BASECLASS (type, i));
bd69fc68
TT
199 if (name)
200 print_name_maybe_canonical (name, flags, stream);
201 else
202 fprintf_filtered (stream, "(null)");
c906108c
SS
203 }
204 if (i > 0)
205 {
206 fputs_filtered (" ", stream);
207 }
208}
ad2f7632 209
c906108c 210/* Print the C++ method arguments ARGS to the file STREAM. */
c5aa993b 211
392a587b 212static void
0d5cff50
DE
213cp_type_print_method_args (struct type *mtype, const char *prefix,
214 const char *varstring, int staticp,
6c8702eb
TT
215 struct ui_file *stream,
216 const struct type_print_options *flags)
c906108c 217{
ad2f7632
DJ
218 struct field *args = TYPE_FIELDS (mtype);
219 int nargs = TYPE_NFIELDS (mtype);
220 int varargs = TYPE_VARARGS (mtype);
c906108c 221 int i;
c5aa993b 222
aff410f1
MS
223 fprintf_symbol_filtered (stream, prefix,
224 language_cplus, DMGL_ANSI);
225 fprintf_symbol_filtered (stream, varstring,
226 language_cplus, DMGL_ANSI);
c906108c 227 fputs_filtered ("(", stream);
ad2f7632 228
5f4d1085
KS
229 /* Skip the class variable. We keep this here to accommodate older
230 compilers and debug formats which may not support artificial
231 parameters. */
ad2f7632
DJ
232 i = staticp ? 0 : 1;
233 if (nargs > i)
c906108c 234 {
ad2f7632 235 while (i < nargs)
c5aa993b 236 {
5f4d1085
KS
237 struct field arg = args[i++];
238
239 /* Skip any artificial arguments. */
240 if (FIELD_ARTIFICIAL (arg))
241 continue;
242
243 c_print_type (arg.type, "", stream, 0, 0, flags);
ad2f7632
DJ
244
245 if (i == nargs && varargs)
246 fprintf_filtered (stream, ", ...");
247 else if (i < nargs)
bd69fc68
TT
248 {
249 fprintf_filtered (stream, ", ");
250 wrap_here (" ");
251 }
c5aa993b 252 }
c906108c 253 }
ad2f7632
DJ
254 else if (varargs)
255 fprintf_filtered (stream, "...");
c906108c 256 else if (current_language->la_language == language_cplus)
ad2f7632 257 fprintf_filtered (stream, "void");
c5aa993b 258
c906108c 259 fprintf_filtered (stream, ")");
94af9270
KS
260
261 /* For non-static methods, read qualifiers from the type of
262 THIS. */
263 if (!staticp)
264 {
265 struct type *domain;
266
267 gdb_assert (nargs > 0);
268 gdb_assert (TYPE_CODE (args[0].type) == TYPE_CODE_PTR);
269 domain = TYPE_TARGET_TYPE (args[0].type);
270
271 if (TYPE_CONST (domain))
272 fprintf_filtered (stream, " const");
273
274 if (TYPE_VOLATILE (domain))
275 fprintf_filtered (stream, " volatile");
06d66ee9
TT
276
277 if (TYPE_RESTRICT (domain))
278 fprintf_filtered (stream, " restrict");
a2c2acaf
MW
279
280 if (TYPE_ATOMIC (domain))
281 fprintf_filtered (stream, " _Atomic");
94af9270 282 }
c906108c
SS
283}
284
285
286/* Print any asterisks or open-parentheses needed before the
287 variable name (to describe its type).
288
289 On outermost call, pass 0 for PASSED_A_PTR.
290 On outermost call, SHOW > 0 means should ignore
291 any typename for TYPE and show its details.
9750e763
KB
292 SHOW is always zero on recursive calls.
293
294 NEED_POST_SPACE is non-zero when a space will be be needed
295 between a trailing qualifier and a field, variable, or function
296 name. */
c906108c 297
a737a51b 298static void
aff410f1
MS
299c_type_print_varspec_prefix (struct type *type,
300 struct ui_file *stream,
301 int show, int passed_a_ptr,
79d43c61
TT
302 int need_post_space,
303 const struct type_print_options *flags)
c906108c 304{
0d5cff50 305 const char *name;
c5504eaf 306
c906108c
SS
307 if (type == 0)
308 return;
309
310 if (TYPE_NAME (type) && show <= 0)
311 return;
312
313 QUIT;
314
315 switch (TYPE_CODE (type))
316 {
317 case TYPE_CODE_PTR:
aff410f1 318 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 319 stream, show, 1, 1, flags);
c906108c 320 fprintf_filtered (stream, "*");
9750e763 321 c_type_print_modifier (type, stream, 1, need_post_space);
c906108c
SS
322 break;
323
0d5de010 324 case TYPE_CODE_MEMBERPTR:
aff410f1 325 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 326 stream, show, 0, 0, flags);
4bfb94b8 327 name = type_name_no_tag (TYPE_SELF_TYPE (type));
c906108c 328 if (name)
bd69fc68 329 print_name_maybe_canonical (name, flags, stream);
c906108c 330 else
4bfb94b8 331 c_type_print_base (TYPE_SELF_TYPE (type),
79d43c61 332 stream, -1, passed_a_ptr, flags);
0d5de010 333 fprintf_filtered (stream, "::*");
c906108c
SS
334 break;
335
0d5de010 336 case TYPE_CODE_METHODPTR:
aff410f1 337 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 338 stream, show, 0, 0, flags);
0d5de010 339 fprintf_filtered (stream, "(");
4bfb94b8 340 name = type_name_no_tag (TYPE_SELF_TYPE (type));
0d5de010 341 if (name)
bd69fc68 342 print_name_maybe_canonical (name, flags, stream);
0d5de010 343 else
4bfb94b8 344 c_type_print_base (TYPE_SELF_TYPE (type),
79d43c61 345 stream, -1, passed_a_ptr, flags);
0d5de010 346 fprintf_filtered (stream, "::*");
c906108c
SS
347 break;
348
349 case TYPE_CODE_REF:
e1cb3213 350 case TYPE_CODE_RVALUE_REF:
aff410f1 351 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 352 stream, show, 1, 0, flags);
e1cb3213 353 fprintf_filtered (stream, TYPE_CODE(type) == TYPE_CODE_REF ? "&" : "&&");
9750e763 354 c_type_print_modifier (type, stream, 1, need_post_space);
c906108c
SS
355 break;
356
0d5de010 357 case TYPE_CODE_METHOD:
c906108c 358 case TYPE_CODE_FUNC:
aff410f1 359 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 360 stream, show, 0, 0, flags);
c906108c
SS
361 if (passed_a_ptr)
362 fprintf_filtered (stream, "(");
363 break;
364
365 case TYPE_CODE_ARRAY:
aff410f1 366 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 367 stream, show, 0, 0, flags);
c906108c
SS
368 if (passed_a_ptr)
369 fprintf_filtered (stream, "(");
370 break;
371
248f8055 372 case TYPE_CODE_TYPEDEF:
aff410f1 373 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
79d43c61 374 stream, show, passed_a_ptr, 0, flags);
248f8055
DJ
375 break;
376
c906108c
SS
377 case TYPE_CODE_UNDEF:
378 case TYPE_CODE_STRUCT:
379 case TYPE_CODE_UNION:
380 case TYPE_CODE_ENUM:
81516450 381 case TYPE_CODE_FLAGS:
c906108c
SS
382 case TYPE_CODE_INT:
383 case TYPE_CODE_FLT:
384 case TYPE_CODE_VOID:
385 case TYPE_CODE_ERROR:
386 case TYPE_CODE_CHAR:
387 case TYPE_CODE_BOOL:
388 case TYPE_CODE_SET:
389 case TYPE_CODE_RANGE:
390 case TYPE_CODE_STRING:
c906108c 391 case TYPE_CODE_COMPLEX:
5c4e30ca 392 case TYPE_CODE_NAMESPACE:
7678ef8f 393 case TYPE_CODE_DECFLOAT:
c906108c 394 /* These types need no prefix. They are listed here so that
c5aa993b 395 gcc -Wall will reveal any types that haven't been handled. */
c906108c 396 break;
c4093a6a 397 default:
3d263c1d 398 error (_("type not handled in c_type_print_varspec_prefix()"));
c4093a6a 399 break;
c906108c
SS
400 }
401}
402
64b00020
DE
403/* Print out "const" and "volatile" attributes,
404 and address space id if present.
c906108c
SS
405 TYPE is a pointer to the type being printed out.
406 STREAM is the output destination.
a737a51b
DE
407 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
408 NEED_POST_SPACE = 1 indicates a final white space is needed. */
c906108c
SS
409
410static void
47663de5
MS
411c_type_print_modifier (struct type *type, struct ui_file *stream,
412 int need_pre_space, int need_post_space)
c906108c 413{
47663de5 414 int did_print_modifier = 0;
321432c0 415 const char *address_space_id;
c5aa993b 416
7f0b5c30
JB
417 /* We don't print `const' qualifiers for references --- since all
418 operators affect the thing referenced, not the reference itself,
419 every reference is `const'. */
e1cb3213 420 if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
c906108c
SS
421 {
422 if (need_pre_space)
c5aa993b 423 fprintf_filtered (stream, " ");
c906108c 424 fprintf_filtered (stream, "const");
47663de5 425 did_print_modifier = 1;
c906108c 426 }
c5aa993b 427
c906108c
SS
428 if (TYPE_VOLATILE (type))
429 {
47663de5 430 if (did_print_modifier || need_pre_space)
c5aa993b 431 fprintf_filtered (stream, " ");
c906108c 432 fprintf_filtered (stream, "volatile");
47663de5 433 did_print_modifier = 1;
c906108c
SS
434 }
435
06d66ee9
TT
436 if (TYPE_RESTRICT (type))
437 {
438 if (did_print_modifier || need_pre_space)
439 fprintf_filtered (stream, " ");
440 fprintf_filtered (stream, "restrict");
441 did_print_modifier = 1;
442 }
443
a2c2acaf
MW
444 if (TYPE_ATOMIC (type))
445 {
446 if (did_print_modifier || need_pre_space)
447 fprintf_filtered (stream, " ");
448 fprintf_filtered (stream, "_Atomic");
449 did_print_modifier = 1;
450 }
451
50810684
UW
452 address_space_id = address_space_int_to_name (get_type_arch (type),
453 TYPE_INSTANCE_FLAGS (type));
47663de5
MS
454 if (address_space_id)
455 {
456 if (did_print_modifier || need_pre_space)
457 fprintf_filtered (stream, " ");
458 fprintf_filtered (stream, "@%s", address_space_id);
459 did_print_modifier = 1;
460 }
461
462 if (did_print_modifier && need_post_space)
c906108c
SS
463 fprintf_filtered (stream, " ");
464}
465
466
0d5de010
DJ
467/* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
468 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
3167638f
JK
469 in non-static methods, are displayed if LINKAGE_NAME is zero. If
470 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
471 parameter types get removed their possible const and volatile qualifiers to
472 match demangled linkage name parameters part of such function type.
473 LANGUAGE is the language in which TYPE was defined. This is a necessary
9c37b5ae 474 evil since this code is used by the C and C++. */
c906108c 475
94af9270
KS
476void
477c_type_print_args (struct type *type, struct ui_file *stream,
79d43c61
TT
478 int linkage_name, enum language language,
479 const struct type_print_options *flags)
c906108c 480{
df54f8eb 481 int i;
0d5de010 482 int printed_any = 0;
c906108c
SS
483
484 fprintf_filtered (stream, "(");
ad2f7632 485
0d5de010
DJ
486 for (i = 0; i < TYPE_NFIELDS (type); i++)
487 {
bc9a5551
JK
488 struct type *param_type;
489
3167638f 490 if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
94af9270
KS
491 continue;
492
0d5de010 493 if (printed_any)
c906108c 494 {
0d5de010
DJ
495 fprintf_filtered (stream, ", ");
496 wrap_here (" ");
c906108c 497 }
0d5de010 498
bc9a5551
JK
499 param_type = TYPE_FIELD_TYPE (type, i);
500
3167638f 501 if (language == language_cplus && linkage_name)
bc9a5551
JK
502 {
503 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
504 - Parameter declarations that differ only in the presence or
505 absence of const and/or volatile are equivalent.
506
507 And the const/volatile qualifiers are not present in the mangled
508 names as produced by GCC. */
509
510 param_type = make_cv_type (0, 0, param_type, NULL);
511 }
512
9c37b5ae 513 c_print_type (param_type, "", stream, -1, 0, flags);
0d5de010 514 printed_any = 1;
c906108c 515 }
0d5de010
DJ
516
517 if (printed_any && TYPE_VARARGS (type))
c906108c 518 {
0d5de010
DJ
519 /* Print out a trailing ellipsis for varargs functions. Ignore
520 TYPE_VARARGS if the function has no named arguments; that
521 represents unprototyped (K&R style) C functions. */
522 if (printed_any && TYPE_VARARGS (type))
523 {
524 fprintf_filtered (stream, ", ");
525 wrap_here (" ");
526 fprintf_filtered (stream, "...");
527 }
c906108c 528 }
0d5de010 529 else if (!printed_any
9c37b5ae 530 && (TYPE_PROTOTYPED (type) || language == language_cplus))
0d5de010 531 fprintf_filtered (stream, "void");
c5aa993b 532
c906108c
SS
533 fprintf_filtered (stream, ")");
534}
535
dfcd3bfb
JM
536/* Return true iff the j'th overloading of the i'th method of TYPE
537 is a type conversion operator, like `operator int () { ... }'.
538 When listing a class's methods, we don't print the return type of
539 such operators. */
a737a51b 540
dfcd3bfb
JM
541static int
542is_type_conversion_operator (struct type *type, int i, int j)
543{
544 /* I think the whole idea of recognizing type conversion operators
545 by their name is pretty terrible. But I don't think our present
546 data structure gives us any other way to tell. If you know of
547 some other way, feel free to rewrite this function. */
0d5cff50 548 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
dfcd3bfb 549
8090b426 550 if (!startswith (name, CP_OPERATOR_STR))
dfcd3bfb
JM
551 return 0;
552
553 name += 8;
554 if (! strchr (" \t\f\n\r", *name))
555 return 0;
556
557 while (strchr (" \t\f\n\r", *name))
558 name++;
559
b0129042
DJ
560 if (!('a' <= *name && *name <= 'z')
561 && !('A' <= *name && *name <= 'Z')
562 && *name != '_')
563 /* If this doesn't look like the start of an identifier, then it
564 isn't a type conversion operator. */
565 return 0;
61012eef 566 else if (startswith (name, "new"))
dfcd3bfb 567 name += 3;
61012eef 568 else if (startswith (name, "delete"))
dfcd3bfb
JM
569 name += 6;
570 else
39c22d1a
JM
571 /* If it doesn't look like new or delete, it's a type conversion
572 operator. */
573 return 1;
dfcd3bfb
JM
574
575 /* Is that really the end of the name? */
576 if (('a' <= *name && *name <= 'z')
577 || ('A' <= *name && *name <= 'Z')
578 || ('0' <= *name && *name <= '9')
579 || *name == '_')
580 /* No, so the identifier following "operator" must be a type name,
581 and this is a type conversion operator. */
582 return 1;
583
584 /* That was indeed the end of the name, so it was `operator new' or
aff410f1
MS
585 `operator delete', neither of which are type conversion
586 operators. */
dfcd3bfb
JM
587 return 0;
588}
589
dfcd3bfb
JM
590/* Given a C++ qualified identifier QID, strip off the qualifiers,
591 yielding the unqualified name. The return value is a pointer into
592 the original string.
593
594 It's a pity we don't have this information in some more structured
595 form. Even the author of this function feels that writing little
596 parsers like this everywhere is stupid. */
a737a51b 597
dfcd3bfb
JM
598static char *
599remove_qualifiers (char *qid)
600{
0963b4bd 601 int quoted = 0; /* Zero if we're not in quotes;
aff410f1
MS
602 '"' if we're in a double-quoted string;
603 '\'' if we're in a single-quoted string. */
0963b4bd 604 int depth = 0; /* Number of unclosed parens we've seen. */
dfcd3bfb
JM
605 char *parenstack = (char *) alloca (strlen (qid));
606 char *scan;
aff410f1
MS
607 char *last = 0; /* The character after the rightmost
608 `::' token we've seen so far. */
dfcd3bfb
JM
609
610 for (scan = qid; *scan; scan++)
611 {
612 if (quoted)
613 {
614 if (*scan == quoted)
615 quoted = 0;
616 else if (*scan == '\\' && *(scan + 1))
617 scan++;
618 }
619 else if (scan[0] == ':' && scan[1] == ':')
620 {
621 /* If we're inside parenthesis (i.e., an argument list) or
622 angle brackets (i.e., a list of template arguments), then
623 we don't record the position of this :: token, since it's
aff410f1
MS
624 not relevant to the top-level structure we're trying to
625 operate on. */
dfcd3bfb
JM
626 if (depth == 0)
627 {
628 last = scan + 2;
629 scan++;
630 }
631 }
632 else if (*scan == '"' || *scan == '\'')
633 quoted = *scan;
634 else if (*scan == '(')
635 parenstack[depth++] = ')';
636 else if (*scan == '[')
637 parenstack[depth++] = ']';
638 /* We're going to treat <> as a pair of matching characters,
639 since we're more likely to see those in template id's than
640 real less-than characters. What a crock. */
641 else if (*scan == '<')
642 parenstack[depth++] = '>';
643 else if (*scan == ')' || *scan == ']' || *scan == '>')
644 {
645 if (depth > 0 && parenstack[depth - 1] == *scan)
646 depth--;
647 else
648 {
aff410f1
MS
649 /* We're going to do a little error recovery here. If
650 we don't find a match for *scan on the paren stack,
651 but there is something lower on the stack that does
652 match, we pop the stack to that point. */
dfcd3bfb
JM
653 int i;
654
655 for (i = depth - 1; i >= 0; i--)
656 if (parenstack[i] == *scan)
657 {
658 depth = i;
659 break;
660 }
661 }
662 }
663 }
664
665 if (last)
666 return last;
667 else
668 /* We didn't find any :: tokens at the top level, so declare the
669 whole thing an unqualified identifier. */
670 return qid;
671}
672
c906108c
SS
673/* Print any array sizes, function arguments or close parentheses
674 needed after the variable name (to describe its type).
675 Args work like c_type_print_varspec_prefix. */
676
677void
aff410f1
MS
678c_type_print_varspec_suffix (struct type *type,
679 struct ui_file *stream,
680 int show, int passed_a_ptr,
79d43c61
TT
681 int demangled_args,
682 const struct type_print_options *flags)
c906108c
SS
683{
684 if (type == 0)
685 return;
686
687 if (TYPE_NAME (type) && show <= 0)
688 return;
689
690 QUIT;
691
692 switch (TYPE_CODE (type))
693 {
694 case TYPE_CODE_ARRAY:
dbc98a8b
KW
695 {
696 LONGEST low_bound, high_bound;
42056501 697 int is_vector = TYPE_VECTOR (type);
c5aa993b 698
dbc98a8b
KW
699 if (passed_a_ptr)
700 fprintf_filtered (stream, ")");
c5aa993b 701
42056501 702 fprintf_filtered (stream, (is_vector ?
2f27adfe 703 " __attribute__ ((vector_size(" : "["));
1d42e4c4
SA
704 /* Bounds are not yet resolved, print a bounds placeholder instead. */
705 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
706 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
707 fprintf_filtered (stream, "variable length");
708 else if (get_array_bounds (type, &low_bound, &high_bound))
318102b9
SP
709 fprintf_filtered (stream, "%s",
710 plongest (high_bound - low_bound + 1));
42056501 711 fprintf_filtered (stream, (is_vector ? ")))" : "]"));
dbc98a8b 712
aff410f1 713 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 714 show, 0, 0, flags);
dbc98a8b 715 }
c906108c
SS
716 break;
717
0d5de010 718 case TYPE_CODE_MEMBERPTR:
aff410f1 719 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 720 show, 0, 0, flags);
c906108c
SS
721 break;
722
0d5de010
DJ
723 case TYPE_CODE_METHODPTR:
724 fprintf_filtered (stream, ")");
aff410f1 725 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 726 show, 0, 0, flags);
c906108c
SS
727 break;
728
729 case TYPE_CODE_PTR:
730 case TYPE_CODE_REF:
e1cb3213 731 case TYPE_CODE_RVALUE_REF:
aff410f1 732 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 733 show, 1, 0, flags);
c906108c
SS
734 break;
735
0d5de010 736 case TYPE_CODE_METHOD:
c906108c
SS
737 case TYPE_CODE_FUNC:
738 if (passed_a_ptr)
739 fprintf_filtered (stream, ")");
740 if (!demangled_args)
79d43c61
TT
741 c_type_print_args (type, stream, 0, current_language->la_language,
742 flags);
aff410f1 743 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 744 show, passed_a_ptr, 0, flags);
248f8055
DJ
745 break;
746
747 case TYPE_CODE_TYPEDEF:
aff410f1 748 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
79d43c61 749 show, passed_a_ptr, 0, flags);
c906108c
SS
750 break;
751
752 case TYPE_CODE_UNDEF:
753 case TYPE_CODE_STRUCT:
754 case TYPE_CODE_UNION:
81516450 755 case TYPE_CODE_FLAGS:
c906108c
SS
756 case TYPE_CODE_ENUM:
757 case TYPE_CODE_INT:
758 case TYPE_CODE_FLT:
759 case TYPE_CODE_VOID:
760 case TYPE_CODE_ERROR:
761 case TYPE_CODE_CHAR:
762 case TYPE_CODE_BOOL:
763 case TYPE_CODE_SET:
764 case TYPE_CODE_RANGE:
765 case TYPE_CODE_STRING:
c906108c 766 case TYPE_CODE_COMPLEX:
5c4e30ca 767 case TYPE_CODE_NAMESPACE:
7678ef8f 768 case TYPE_CODE_DECFLOAT:
c906108c 769 /* These types do not need a suffix. They are listed so that
aff410f1
MS
770 gcc -Wall will report types that may not have been
771 considered. */
c906108c 772 break;
c4093a6a 773 default:
3d263c1d 774 error (_("type not handled in c_type_print_varspec_suffix()"));
c4093a6a 775 break;
c906108c
SS
776 }
777}
778
bd69fc68
TT
779/* A helper for c_type_print_base that displays template
780 parameters and their bindings, if needed.
781
782 TABLE is the local bindings table to use. If NULL, no printing is
783 done. Note that, at this point, TABLE won't have any useful
784 information in it -- but it is also used as a flag to
785 print_name_maybe_canonical to activate searching the global typedef
786 table.
787
788 TYPE is the type whose template arguments are being displayed.
789
790 STREAM is the stream on which to print. */
791
792static void
793c_type_print_template_args (const struct type_print_options *flags,
794 struct type *type, struct ui_file *stream)
795{
796 int first = 1, i;
797
798 if (flags->raw)
799 return;
800
801 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
802 {
803 struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
804
805 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
806 continue;
807
808 if (first)
809 {
810 wrap_here (" ");
811 fprintf_filtered (stream, _("[with %s = "),
812 SYMBOL_LINKAGE_NAME (sym));
813 first = 0;
814 }
815 else
816 {
817 fputs_filtered (", ", stream);
818 wrap_here (" ");
819 fprintf_filtered (stream, "%s = ", SYMBOL_LINKAGE_NAME (sym));
820 }
821
822 c_print_type (SYMBOL_TYPE (sym), "", stream, -1, 0, flags);
823 }
824
825 if (!first)
826 fputs_filtered (_("] "), stream);
827}
828
c906108c 829/* Print the name of the type (or the ultimate pointer target,
aff410f1
MS
830 function value or array element), or the description of a structure
831 or union.
832
833 SHOW positive means print details about the type (e.g. enum
834 values), and print structure elements passing SHOW - 1 for show.
835
836 SHOW negative means just print the type name or struct tag if there
837 is one. If there is no name, print something sensible but concise
838 like "struct {...}".
839
840 SHOW zero means just print the type name or struct tag if there is
841 one. If there is no name, print something sensible but not as
842 concise like "struct {int x; int y;}".
c906108c
SS
843
844 LEVEL is the number of spaces to indent by.
845 We increase it for some recursive calls. */
846
847void
aff410f1 848c_type_print_base (struct type *type, struct ui_file *stream,
79d43c61 849 int show, int level, const struct type_print_options *flags)
c906108c 850{
b02dede2
DJ
851 int i;
852 int len, real_len;
c5aa993b
JM
853 enum
854 {
855 s_none, s_public, s_private, s_protected
856 }
857 section_type;
c906108c
SS
858 int need_access_label = 0;
859 int j, len2;
860
861 QUIT;
862
c906108c
SS
863 if (type == NULL)
864 {
3d263c1d 865 fputs_filtered (_("<type unknown>"), stream);
c906108c
SS
866 return;
867 }
868
aff410f1
MS
869 /* When SHOW is zero or less, and there is a valid type name, then
870 always just print the type name directly from the type. */
871 /* If we have "typedef struct foo {. . .} bar;" do we want to print
872 it as "struct foo" or as "bar"? Pick the latter, because C++
873 folk tend to expect things like "class5 *foo" rather than "struct
874 class5 *foo". */
c906108c
SS
875
876 if (show <= 0
877 && TYPE_NAME (type) != NULL)
878 {
47663de5 879 c_type_print_modifier (type, stream, 0, 1);
bd69fc68 880 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
c906108c
SS
881 return;
882 }
883
f168693b 884 type = check_typedef (type);
c5aa993b 885
c906108c
SS
886 switch (TYPE_CODE (type))
887 {
888 case TYPE_CODE_TYPEDEF:
aff410f1
MS
889 /* If we get here, the typedef doesn't have a name, and we
890 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
8c540a24
DE
891 gdb_assert (TYPE_NAME (type) == NULL);
892 gdb_assert (TYPE_TARGET_TYPE (type) == NULL);
893 fprintf_filtered (stream, _("<unnamed typedef>"));
894 break;
895
7022349d
PA
896 case TYPE_CODE_FUNC:
897 case TYPE_CODE_METHOD:
898 if (TYPE_TARGET_TYPE (type) == NULL)
899 type_print_unknown_return_type (stream);
900 else
901 c_type_print_base (TYPE_TARGET_TYPE (type),
902 stream, show, level, flags);
903 break;
c906108c
SS
904 case TYPE_CODE_ARRAY:
905 case TYPE_CODE_PTR:
0d5de010 906 case TYPE_CODE_MEMBERPTR:
c906108c 907 case TYPE_CODE_REF:
e1cb3213 908 case TYPE_CODE_RVALUE_REF:
0d5de010 909 case TYPE_CODE_METHODPTR:
aff410f1 910 c_type_print_base (TYPE_TARGET_TYPE (type),
79d43c61 911 stream, show, level, flags);
c906108c
SS
912 break;
913
914 case TYPE_CODE_STRUCT:
1c5b7826 915 case TYPE_CODE_UNION:
bd69fc68
TT
916 {
917 struct type_print_options local_flags = *flags;
918 struct type_print_options semi_local_flags = *flags;
919 struct cleanup *local_cleanups = make_cleanup (null_cleanup, NULL);
920
921 local_flags.local_typedefs = NULL;
922 semi_local_flags.local_typedefs = NULL;
923
924 if (!flags->raw)
925 {
926 if (flags->local_typedefs)
927 local_flags.local_typedefs
928 = copy_typedef_hash (flags->local_typedefs);
929 else
930 local_flags.local_typedefs = create_typedef_hash ();
931
932 make_cleanup_free_typedef_hash (local_flags.local_typedefs);
933 }
934
935 c_type_print_modifier (type, stream, 0, 1);
936 if (TYPE_CODE (type) == TYPE_CODE_UNION)
937 fprintf_filtered (stream, "union ");
938 else if (TYPE_DECLARED_CLASS (type))
939 fprintf_filtered (stream, "class ");
940 else
941 fprintf_filtered (stream, "struct ");
942
943 /* Print the tag if it exists. The HP aCC compiler emits a
944 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
945 enum}" tag for unnamed struct/union/enum's, which we don't
946 want to print. */
947 if (TYPE_TAG_NAME (type) != NULL
61012eef 948 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
bd69fc68
TT
949 {
950 /* When printing the tag name, we are still effectively
951 printing in the outer context, hence the use of FLAGS
952 here. */
953 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
954 if (show > 0)
955 fputs_filtered (" ", stream);
956 }
957
958 if (show < 0)
959 {
960 /* If we just printed a tag name, no need to print anything
961 else. */
962 if (TYPE_TAG_NAME (type) == NULL)
963 fprintf_filtered (stream, "{...}");
964 }
965 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
966 {
967 struct type *basetype;
968 int vptr_fieldno;
969
970 c_type_print_template_args (&local_flags, type, stream);
971
972 /* Add in template parameters when printing derivation info. */
973 add_template_parameters (local_flags.local_typedefs, type);
974 cp_type_print_derivation_info (stream, type, &local_flags);
975
976 /* This holds just the global typedefs and the template
977 parameters. */
978 semi_local_flags.local_typedefs
979 = copy_typedef_hash (local_flags.local_typedefs);
980 if (semi_local_flags.local_typedefs)
981 make_cleanup_free_typedef_hash (semi_local_flags.local_typedefs);
982
983 /* Now add in the local typedefs. */
984 recursively_update_typedef_hash (local_flags.local_typedefs, type);
985
986 fprintf_filtered (stream, "{\n");
987 if (TYPE_NFIELDS (type) == 0 && TYPE_NFN_FIELDS (type) == 0
988 && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
989 {
990 if (TYPE_STUB (type))
991 fprintfi_filtered (level + 4, stream,
992 _("<incomplete type>\n"));
993 else
994 fprintfi_filtered (level + 4, stream,
995 _("<no data fields>\n"));
996 }
997
998 /* Start off with no specific section type, so we can print
999 one for the first field we find, and use that section type
1000 thereafter until we find another type. */
1001
1002 section_type = s_none;
1003
1004 /* For a class, if all members are private, there's no need
1005 for a "private:" label; similarly, for a struct or union
1006 masquerading as a class, if all members are public, there's
1007 no need for a "public:" label. */
1008
1009 if (TYPE_DECLARED_CLASS (type))
1010 {
1011 QUIT;
1012 len = TYPE_NFIELDS (type);
1013 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1014 if (!TYPE_FIELD_PRIVATE (type, i))
c5aa993b 1015 {
bd69fc68
TT
1016 need_access_label = 1;
1017 break;
c5aa993b 1018 }
bd69fc68
TT
1019 QUIT;
1020 if (!need_access_label)
c5aa993b 1021 {
bd69fc68
TT
1022 len2 = TYPE_NFN_FIELDS (type);
1023 for (j = 0; j < len2; j++)
1024 {
1025 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1026 for (i = 0; i < len; i++)
1027 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1028 j), i))
1029 {
1030 need_access_label = 1;
1031 break;
1032 }
1033 if (need_access_label)
1034 break;
1035 }
c5aa993b 1036 }
bd69fc68
TT
1037 }
1038 else
1039 {
1040 QUIT;
1041 len = TYPE_NFIELDS (type);
1042 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1043 if (TYPE_FIELD_PRIVATE (type, i)
1044 || TYPE_FIELD_PROTECTED (type, i))
c5aa993b 1045 {
bd69fc68
TT
1046 need_access_label = 1;
1047 break;
c5aa993b 1048 }
bd69fc68
TT
1049 QUIT;
1050 if (!need_access_label)
1051 {
1052 len2 = TYPE_NFN_FIELDS (type);
1053 for (j = 0; j < len2; j++)
1054 {
1055 QUIT;
1056 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1057 for (i = 0; i < len; i++)
1058 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
1059 j), i)
1060 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1061 j),
1062 i))
1063 {
1064 need_access_label = 1;
1065 break;
1066 }
1067 if (need_access_label)
1068 break;
1069 }
1070 }
1071 }
c906108c 1072
bd69fc68
TT
1073 /* If there is a base class for this type,
1074 do not print the field that it occupies. */
c906108c 1075
bd69fc68
TT
1076 len = TYPE_NFIELDS (type);
1077 vptr_fieldno = get_vptr_fieldno (type, &basetype);
1078 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1079 {
1080 QUIT;
d48cc9dd 1081
bd69fc68
TT
1082 /* If we have a virtual table pointer, omit it. Even if
1083 virtual table pointers are not specifically marked in
1084 the debug info, they should be artificial. */
1085 if ((i == vptr_fieldno && type == basetype)
1086 || TYPE_FIELD_ARTIFICIAL (type, i))
1087 continue;
c906108c 1088
bd69fc68
TT
1089 if (need_access_label)
1090 {
1091 if (TYPE_FIELD_PROTECTED (type, i))
1092 {
1093 if (section_type != s_protected)
1094 {
1095 section_type = s_protected;
1096 fprintfi_filtered (level + 2, stream,
1097 "protected:\n");
1098 }
1099 }
1100 else if (TYPE_FIELD_PRIVATE (type, i))
1101 {
1102 if (section_type != s_private)
1103 {
1104 section_type = s_private;
1105 fprintfi_filtered (level + 2, stream,
1106 "private:\n");
1107 }
1108 }
1109 else
1110 {
1111 if (section_type != s_public)
1112 {
1113 section_type = s_public;
1114 fprintfi_filtered (level + 2, stream,
1115 "public:\n");
1116 }
1117 }
1118 }
c906108c 1119
bd69fc68
TT
1120 print_spaces_filtered (level + 4, stream);
1121 if (field_is_static (&TYPE_FIELD (type, i)))
1122 fprintf_filtered (stream, "static ");
1123 c_print_type (TYPE_FIELD_TYPE (type, i),
1124 TYPE_FIELD_NAME (type, i),
1125 stream, show - 1, level + 4,
1126 &local_flags);
1127 if (!field_is_static (&TYPE_FIELD (type, i))
1128 && TYPE_FIELD_PACKED (type, i))
1129 {
1130 /* It is a bitfield. This code does not attempt
1131 to look at the bitpos and reconstruct filler,
1132 unnamed fields. This would lead to misleading
1133 results if the compiler does not put out fields
1134 for such things (I don't know what it does). */
1135 fprintf_filtered (stream, " : %d",
1136 TYPE_FIELD_BITSIZE (type, i));
1137 }
1138 fprintf_filtered (stream, ";\n");
1139 }
c906108c 1140
b02dede2 1141 /* If there are both fields and methods, put a blank line
aff410f1
MS
1142 between them. Make sure to count only method that we
1143 will display; artificial methods will be hidden. */
c906108c 1144 len = TYPE_NFN_FIELDS (type);
53342f27
TT
1145 if (!flags->print_methods)
1146 len = 0;
b02dede2
DJ
1147 real_len = 0;
1148 for (i = 0; i < len; i++)
1149 {
1150 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1151 int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1152 int j;
c5504eaf 1153
b02dede2
DJ
1154 for (j = 0; j < len2; j++)
1155 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1156 real_len++;
1157 }
1158 if (real_len > 0 && section_type != s_none)
c5aa993b 1159 fprintf_filtered (stream, "\n");
c906108c 1160
0963b4bd 1161 /* C++: print out the methods. */
c906108c
SS
1162 for (i = 0; i < len; i++)
1163 {
1164 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1165 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
0d5cff50
DE
1166 const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1167 const char *name = type_name_no_tag (type);
aff410f1
MS
1168 int is_constructor = name && strcmp (method_name,
1169 name) == 0;
c5504eaf 1170
c906108c
SS
1171 for (j = 0; j < len2; j++)
1172 {
1d06ead6
TT
1173 const char *mangled_name;
1174 char *demangled_name;
1175 struct cleanup *inner_cleanup;
1176 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
c5aa993b 1177 int is_full_physname_constructor =
7d27a96d
TT
1178 TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1179 || is_constructor_name (physname)
c5504eaf
MS
1180 || is_destructor_name (physname)
1181 || method_name[0] == '~';
015a42b4 1182
b02dede2
DJ
1183 /* Do not print out artificial methods. */
1184 if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1185 continue;
c906108c 1186
1d06ead6
TT
1187 inner_cleanup = make_cleanup (null_cleanup, NULL);
1188
c906108c
SS
1189 QUIT;
1190 if (TYPE_FN_FIELD_PROTECTED (f, j))
1191 {
1192 if (section_type != s_protected)
1193 {
1194 section_type = s_protected;
1195 fprintfi_filtered (level + 2, stream,
1196 "protected:\n");
1197 }
1198 }
1199 else if (TYPE_FN_FIELD_PRIVATE (f, j))
1200 {
1201 if (section_type != s_private)
1202 {
1203 section_type = s_private;
aff410f1
MS
1204 fprintfi_filtered (level + 2, stream,
1205 "private:\n");
c906108c
SS
1206 }
1207 }
1208 else
1209 {
1210 if (section_type != s_public)
1211 {
1212 section_type = s_public;
aff410f1
MS
1213 fprintfi_filtered (level + 2, stream,
1214 "public:\n");
c906108c
SS
1215 }
1216 }
1217
1218 print_spaces_filtered (level + 4, stream);
1219 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1220 fprintf_filtered (stream, "virtual ");
1221 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1222 fprintf_filtered (stream, "static ");
1223 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1224 {
1225 /* Keep GDB from crashing here. */
aff410f1
MS
1226 fprintf_filtered (stream,
1227 _("<undefined type> %s;\n"),
c5aa993b 1228 TYPE_FN_FIELD_PHYSNAME (f, j));
c906108c
SS
1229 break;
1230 }
aff410f1
MS
1231 else if (!is_constructor /* Constructors don't
1232 have declared
1233 types. */
1234 && !is_full_physname_constructor /* " " */
5aafa1cc 1235 && !is_type_conversion_operator (type, i, j))
c906108c 1236 {
6c8702eb 1237 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
bd69fc68
TT
1238 "", stream, -1, 0,
1239 &local_flags);
c906108c
SS
1240 fputs_filtered (" ", stream);
1241 }
1242 if (TYPE_FN_FIELD_STUB (f, j))
1d06ead6
TT
1243 {
1244 char *tem;
1245
1246 /* Build something we can demangle. */
1247 tem = gdb_mangle_name (type, i, j);
1248 make_cleanup (xfree, tem);
1249 mangled_name = tem;
1250 }
c906108c
SS
1251 else
1252 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1253
1254 demangled_name =
8de20a37
TT
1255 gdb_demangle (mangled_name,
1256 DMGL_ANSI | DMGL_PARAMS);
c906108c
SS
1257 if (demangled_name == NULL)
1258 {
aff410f1 1259 /* In some cases (for instance with the HP
bd69fc68
TT
1260 demangling), if a function has more than 10
1261 arguments, the demangling will fail.
1262 Let's try to reconstruct the function
1263 signature from the symbol information. */
c906108c 1264 if (!TYPE_FN_FIELD_STUB (f, j))
ad2f7632
DJ
1265 {
1266 int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1267 struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
c5504eaf 1268
ad2f7632
DJ
1269 cp_type_print_method_args (mtype,
1270 "",
1271 method_name,
1272 staticp,
bd69fc68 1273 stream, &local_flags);
ad2f7632 1274 }
c906108c 1275 else
aff410f1
MS
1276 fprintf_filtered (stream,
1277 _("<badly mangled name '%s'>"),
c906108c
SS
1278 mangled_name);
1279 }
1280 else
1281 {
1282 char *p;
dfcd3bfb
JM
1283 char *demangled_no_class
1284 = remove_qualifiers (demangled_name);
c5aa993b 1285
aff410f1
MS
1286 /* Get rid of the `static' appended by the
1287 demangler. */
c906108c
SS
1288 p = strstr (demangled_no_class, " static");
1289 if (p != NULL)
1290 {
1291 int length = p - demangled_no_class;
1d06ead6 1292 char *demangled_no_static;
c5504eaf 1293
aff410f1
MS
1294 demangled_no_static
1295 = (char *) xmalloc (length + 1);
1296 strncpy (demangled_no_static,
1297 demangled_no_class, length);
c5aa993b 1298 *(demangled_no_static + length) = '\0';
c906108c 1299 fputs_filtered (demangled_no_static, stream);
b8c9b27d 1300 xfree (demangled_no_static);
c906108c
SS
1301 }
1302 else
1303 fputs_filtered (demangled_no_class, stream);
b8c9b27d 1304 xfree (demangled_name);
c906108c
SS
1305 }
1306
1d06ead6 1307 do_cleanups (inner_cleanup);
c906108c
SS
1308
1309 fprintf_filtered (stream, ";\n");
1310 }
1311 }
1312
98751a41
JK
1313 /* Print typedefs defined in this class. */
1314
53342f27 1315 if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
98751a41
JK
1316 {
1317 if (TYPE_NFIELDS (type) != 0 || TYPE_NFN_FIELDS (type) != 0)
1318 fprintf_filtered (stream, "\n");
1319
a579cd9a
MW
1320 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1321 {
1322 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1323
1324 /* Dereference the typedef declaration itself. */
1325 gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
1326 target = TYPE_TARGET_TYPE (target);
1327
1328 print_spaces_filtered (level + 4, stream);
1329 fprintf_filtered (stream, "typedef ");
1330
1331 /* We want to print typedefs with substitutions
1332 from the template parameters or globally-known
1333 typedefs but not local typedefs. */
1334 c_print_type (target,
1335 TYPE_TYPEDEF_FIELD_NAME (type, i),
1336 stream, show - 1, level + 4,
1337 &semi_local_flags);
1338 fprintf_filtered (stream, ";\n");
1339 }
1340 }
98751a41 1341
bd69fc68 1342 fprintfi_filtered (level, stream, "}");
bd69fc68 1343 }
c4093a6a 1344
bd69fc68
TT
1345 do_cleanups (local_cleanups);
1346 }
c906108c
SS
1347 break;
1348
1349 case TYPE_CODE_ENUM:
47663de5 1350 c_type_print_modifier (type, stream, 0, 1);
c5aa993b 1351 fprintf_filtered (stream, "enum ");
3d567982
TT
1352 if (TYPE_DECLARED_CLASS (type))
1353 fprintf_filtered (stream, "class ");
c906108c
SS
1354 /* Print the tag name if it exists.
1355 The aCC compiler emits a spurious
1356 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1357 tag for unnamed struct/union/enum's, which we don't
aff410f1 1358 want to print. */
5aafa1cc 1359 if (TYPE_TAG_NAME (type) != NULL
61012eef 1360 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
c906108c 1361 {
bd69fc68 1362 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
c906108c
SS
1363 if (show > 0)
1364 fputs_filtered (" ", stream);
1365 }
1366
1367 wrap_here (" ");
1368 if (show < 0)
1369 {
aff410f1
MS
1370 /* If we just printed a tag name, no need to print anything
1371 else. */
c906108c
SS
1372 if (TYPE_TAG_NAME (type) == NULL)
1373 fprintf_filtered (stream, "{...}");
1374 }
1375 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1376 {
14e75d8e
JK
1377 LONGEST lastval = 0;
1378
3d567982
TT
1379 /* We can't handle this case perfectly, as DWARF does not
1380 tell us whether or not the underlying type was specified
1381 in the source (and other debug formats don't provide this
1382 at all). We choose to print the underlying type, if it
1383 has a name, when in C++ on the theory that it's better to
1384 print too much than too little; but conversely not to
1385 print something egregiously outside the current
1386 language's syntax. */
1387 if (current_language->la_language == language_cplus
1388 && TYPE_TARGET_TYPE (type) != NULL)
1389 {
1390 struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
1391
1392 if (TYPE_NAME (underlying) != NULL)
1393 fprintf_filtered (stream, ": %s ", TYPE_NAME (underlying));
1394 }
1395
c906108c
SS
1396 fprintf_filtered (stream, "{");
1397 len = TYPE_NFIELDS (type);
c906108c
SS
1398 for (i = 0; i < len; i++)
1399 {
1400 QUIT;
c5aa993b
JM
1401 if (i)
1402 fprintf_filtered (stream, ", ");
c906108c
SS
1403 wrap_here (" ");
1404 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
14e75d8e 1405 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
c906108c 1406 {
14e75d8e
JK
1407 fprintf_filtered (stream, " = %s",
1408 plongest (TYPE_FIELD_ENUMVAL (type, i)));
1409 lastval = TYPE_FIELD_ENUMVAL (type, i);
c906108c
SS
1410 }
1411 lastval++;
1412 }
1413 fprintf_filtered (stream, "}");
1414 }
1415 break;
1416
81516450
DE
1417 case TYPE_CODE_FLAGS:
1418 {
1419 struct type_print_options local_flags = *flags;
1420
1421 local_flags.local_typedefs = NULL;
1422
1423 c_type_print_modifier (type, stream, 0, 1);
1424 fprintf_filtered (stream, "flag ");
1425 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
1426 if (show > 0)
1427 {
1428 fputs_filtered (" ", stream);
1429 fprintf_filtered (stream, "{\n");
1430 if (TYPE_NFIELDS (type) == 0)
1431 {
1432 if (TYPE_STUB (type))
1433 fprintfi_filtered (level + 4, stream,
1434 _("<incomplete type>\n"));
1435 else
1436 fprintfi_filtered (level + 4, stream,
1437 _("<no data fields>\n"));
1438 }
1439 len = TYPE_NFIELDS (type);
1440 for (i = 0; i < len; i++)
1441 {
1442 QUIT;
1443 print_spaces_filtered (level + 4, stream);
1444 /* We pass "show" here and not "show - 1" to get enum types
1445 printed. There's no other way to see them. */
1446 c_print_type (TYPE_FIELD_TYPE (type, i),
1447 TYPE_FIELD_NAME (type, i),
1448 stream, show, level + 4,
1449 &local_flags);
6b850546
DT
1450 fprintf_filtered (stream, " @%s",
1451 plongest (TYPE_FIELD_BITPOS (type, i)));
81516450
DE
1452 if (TYPE_FIELD_BITSIZE (type, i) > 1)
1453 {
6b850546
DT
1454 fprintf_filtered (stream, "-%s",
1455 plongest (TYPE_FIELD_BITPOS (type, i)
1456 + TYPE_FIELD_BITSIZE (type, i)
1457 - 1));
81516450
DE
1458 }
1459 fprintf_filtered (stream, ";\n");
1460 }
1461 fprintfi_filtered (level, stream, "}");
1462 }
1463 }
1464 break;
1465
c906108c
SS
1466 case TYPE_CODE_VOID:
1467 fprintf_filtered (stream, "void");
1468 break;
1469
1470 case TYPE_CODE_UNDEF:
3d263c1d 1471 fprintf_filtered (stream, _("struct <unknown>"));
c906108c
SS
1472 break;
1473
1474 case TYPE_CODE_ERROR:
b00fdb78 1475 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
c906108c
SS
1476 break;
1477
1478 case TYPE_CODE_RANGE:
0963b4bd 1479 /* This should not occur. */
3d263c1d 1480 fprintf_filtered (stream, _("<range type>"));
c906108c
SS
1481 break;
1482
5c4e30ca
DC
1483 case TYPE_CODE_NAMESPACE:
1484 fputs_filtered ("namespace ", stream);
1485 fputs_filtered (TYPE_TAG_NAME (type), stream);
1486 break;
1487
c906108c 1488 default:
aff410f1
MS
1489 /* Handle types not explicitly handled by the other cases, such
1490 as fundamental types. For these, just print whatever the
1491 type name is, as recorded in the type itself. If there is no
1492 type name, then complain. */
c906108c
SS
1493 if (TYPE_NAME (type) != NULL)
1494 {
47663de5 1495 c_type_print_modifier (type, stream, 0, 1);
bd69fc68 1496 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
c906108c
SS
1497 }
1498 else
1499 {
aff410f1
MS
1500 /* At least for dump_symtab, it is important that this not
1501 be an error (). */
3d263c1d 1502 fprintf_filtered (stream, _("<invalid type code %d>"),
c906108c
SS
1503 TYPE_CODE (type));
1504 }
1505 break;
1506 }
1507}
This page took 1.308395 seconds and 4 git commands to generate.