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