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