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