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