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