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