Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
1bac305b | 2 | |
c5a57081 | 3 | Copyright (C) 1986, 1988-1989, 1991-1995, 1998-2001, 2003, 2006-2012 |
4c38e0a4 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
04ea0df1 | 22 | #include "gdb_obstack.h" |
c906108c SS |
23 | #include "bfd.h" /* Binary File Description */ |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "value.h" | |
28 | #include "gdbcore.h" | |
29 | #include "command.h" | |
30 | #include "gdbcmd.h" | |
31 | #include "target.h" | |
32 | #include "language.h" | |
015a42b4 | 33 | #include "cp-abi.h" |
b9362cc7 | 34 | #include "typeprint.h" |
c906108c | 35 | #include "gdb_string.h" |
ae6a3a4c | 36 | #include "exceptions.h" |
79a45b7d | 37 | #include "valprint.h" |
c906108c | 38 | #include <errno.h> |
53342f27 TT |
39 | #include <ctype.h> |
40 | #include "cli/cli-utils.h" | |
c906108c | 41 | |
a14ed312 | 42 | extern void _initialize_typeprint (void); |
392a587b | 43 | |
a14ed312 | 44 | static void ptype_command (char *, int); |
c906108c | 45 | |
a14ed312 | 46 | static void whatis_command (char *, int); |
c906108c | 47 | |
a14ed312 | 48 | static void whatis_exp (char *, int); |
c906108c | 49 | |
79d43c61 TT |
50 | const struct type_print_options type_print_raw_options = |
51 | { | |
53342f27 TT |
52 | 1, /* raw */ |
53 | 1, /* print_methods */ | |
54 | 1 /* print_typedefs */ | |
79d43c61 TT |
55 | }; |
56 | ||
57 | /* The default flags for 'ptype' and 'whatis'. */ | |
58 | ||
59 | static struct type_print_options default_ptype_flags = | |
60 | { | |
53342f27 TT |
61 | 0, /* raw */ |
62 | 1, /* print_methods */ | |
63 | 1 /* print_typedefs */ | |
79d43c61 | 64 | }; |
5c6ce71d | 65 | |
53342f27 TT |
66 | \f |
67 | ||
a5238fbc PM |
68 | /* Print a description of a type in the format of a |
69 | typedef for the current language. | |
c378eb4e | 70 | NEW is the new name for a type TYPE. */ |
a5238fbc PM |
71 | |
72 | void | |
73 | typedef_print (struct type *type, struct symbol *new, struct ui_file *stream) | |
74 | { | |
5c6ce71d TT |
75 | LA_PRINT_TYPEDEF (type, new, stream); |
76 | } | |
77 | ||
78 | /* The default way to print a typedef. */ | |
79 | ||
80 | void | |
81 | default_print_typedef (struct type *type, struct symbol *new_symbol, | |
82 | struct ui_file *stream) | |
83 | { | |
84 | error (_("Language not supported.")); | |
a5238fbc PM |
85 | } |
86 | ||
c906108c SS |
87 | /* Print a description of a type TYPE in the form of a declaration of a |
88 | variable named VARSTRING. (VARSTRING is demangled if necessary.) | |
89 | Output goes to STREAM (via stdio). | |
90 | If SHOW is positive, we show the contents of the outermost level | |
91 | of structure even if there is a type name that could be used instead. | |
92 | If SHOW is negative, we never show the details of elements' types. */ | |
93 | ||
94 | void | |
0d5cff50 | 95 | type_print (struct type *type, const char *varstring, struct ui_file *stream, |
fba45db2 | 96 | int show) |
c906108c | 97 | { |
79d43c61 | 98 | LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags); |
c906108c SS |
99 | } |
100 | ||
ae6a3a4c TJB |
101 | /* Print TYPE to a string, returning it. The caller is responsible for |
102 | freeing the string. */ | |
103 | ||
104 | char * | |
105 | type_to_string (struct type *type) | |
106 | { | |
107 | char *s = NULL; | |
ae6a3a4c TJB |
108 | struct ui_file *stb; |
109 | struct cleanup *old_chain; | |
110 | volatile struct gdb_exception except; | |
111 | ||
112 | stb = mem_fileopen (); | |
113 | old_chain = make_cleanup_ui_file_delete (stb); | |
114 | ||
115 | TRY_CATCH (except, RETURN_MASK_ALL) | |
116 | { | |
117 | type_print (type, "", stb, -1); | |
759ef836 | 118 | s = ui_file_xstrdup (stb, NULL); |
ae6a3a4c TJB |
119 | } |
120 | if (except.reason < 0) | |
121 | s = NULL; | |
122 | ||
123 | do_cleanups (old_chain); | |
124 | ||
125 | return s; | |
126 | } | |
127 | ||
c906108c SS |
128 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
129 | show is passed to type_print. */ | |
130 | ||
131 | static void | |
fba45db2 | 132 | whatis_exp (char *exp, int show) |
c906108c SS |
133 | { |
134 | struct expression *expr; | |
3d6d86c6 | 135 | struct value *val; |
52f0bd74 | 136 | struct cleanup *old_chain = NULL; |
c5aa993b | 137 | struct type *real_type = NULL; |
070ad9f0 | 138 | struct type *type; |
c906108c SS |
139 | int full = 0; |
140 | int top = -1; | |
141 | int using_enc = 0; | |
79a45b7d | 142 | struct value_print_options opts; |
53342f27 | 143 | struct type_print_options flags = default_ptype_flags; |
c906108c SS |
144 | |
145 | if (exp) | |
146 | { | |
53342f27 TT |
147 | if (*exp == '/') |
148 | { | |
149 | int seen_one = 0; | |
150 | ||
151 | for (++exp; *exp && !isspace (*exp); ++exp) | |
152 | { | |
153 | switch (*exp) | |
154 | { | |
155 | case 'r': | |
156 | flags.raw = 1; | |
157 | break; | |
158 | case 'm': | |
159 | flags.print_methods = 0; | |
160 | break; | |
161 | case 'M': | |
162 | flags.print_methods = 1; | |
163 | break; | |
164 | case 't': | |
165 | flags.print_typedefs = 0; | |
166 | break; | |
167 | case 'T': | |
168 | flags.print_typedefs = 1; | |
169 | break; | |
170 | default: | |
171 | error (_("unrecognized flag '%c'"), *exp); | |
172 | } | |
173 | seen_one = 1; | |
174 | } | |
175 | ||
176 | if (!*exp && !seen_one) | |
177 | error (_("flag expected")); | |
178 | if (!isspace (*exp)) | |
179 | error (_("expected space after format")); | |
180 | exp = skip_spaces (exp); | |
181 | } | |
182 | ||
c906108c | 183 | expr = parse_expression (exp); |
c13c43fd | 184 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
185 | val = evaluate_type (expr); |
186 | } | |
187 | else | |
188 | val = access_value_history (0); | |
189 | ||
df407dfe | 190 | type = value_type (val); |
070ad9f0 | 191 | |
79a45b7d TT |
192 | get_user_print_options (&opts); |
193 | if (opts.objectprint) | |
070ad9f0 | 194 | { |
41808ebe DE |
195 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) |
196 | || (TYPE_CODE (type) == TYPE_CODE_REF)) | |
197 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
dfcee124 | 198 | real_type = value_rtti_indirect_type (val, &full, &top, &using_enc); |
070ad9f0 | 199 | else if (TYPE_CODE (type) == TYPE_CODE_CLASS) |
41808ebe | 200 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
070ad9f0 | 201 | } |
c906108c SS |
202 | |
203 | printf_filtered ("type = "); | |
204 | ||
070ad9f0 DB |
205 | if (real_type) |
206 | { | |
207 | printf_filtered ("/* real type = "); | |
208 | type_print (real_type, "", gdb_stdout, -1); | |
209 | if (! full) | |
210 | printf_filtered (" (incomplete object)"); | |
211 | printf_filtered (" */\n"); | |
212 | } | |
c906108c | 213 | |
53342f27 | 214 | LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags); |
c906108c SS |
215 | printf_filtered ("\n"); |
216 | ||
217 | if (exp) | |
218 | do_cleanups (old_chain); | |
219 | } | |
220 | ||
c906108c | 221 | static void |
fba45db2 | 222 | whatis_command (char *exp, int from_tty) |
c906108c SS |
223 | { |
224 | /* Most of the time users do not want to see all the fields | |
225 | in a structure. If they do they can use the "ptype" command. | |
226 | Hence the "-1" below. */ | |
227 | whatis_exp (exp, -1); | |
228 | } | |
229 | ||
c906108c SS |
230 | /* TYPENAME is either the name of a type, or an expression. */ |
231 | ||
c906108c | 232 | static void |
fba45db2 | 233 | ptype_command (char *typename, int from_tty) |
c906108c | 234 | { |
d843c49c | 235 | whatis_exp (typename, 1); |
c906108c SS |
236 | } |
237 | ||
238 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. | |
239 | Used to print data from type structures in a specified type. For example, | |
240 | array bounds may be characters or booleans in some languages, and this | |
241 | allows the ranges to be printed in their "natural" form rather than as | |
242 | decimal integer values. | |
243 | ||
244 | FIXME: This is here simply because only the type printing routines | |
245 | currently use it, and it wasn't clear if it really belonged somewhere | |
246 | else (like printcmd.c). There are a lot of other gdb routines that do | |
247 | something similar, but they are generally concerned with printing values | |
41808ebe | 248 | that come from the inferior in target byte order and target size. */ |
c906108c SS |
249 | |
250 | void | |
fba45db2 | 251 | print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) |
c906108c SS |
252 | { |
253 | unsigned int i; | |
254 | unsigned len; | |
255 | ||
256 | CHECK_TYPEDEF (type); | |
257 | ||
258 | switch (TYPE_CODE (type)) | |
259 | { | |
260 | ||
261 | case TYPE_CODE_ENUM: | |
262 | len = TYPE_NFIELDS (type); | |
263 | for (i = 0; i < len; i++) | |
264 | { | |
14e75d8e | 265 | if (TYPE_FIELD_ENUMVAL (type, i) == val) |
c906108c SS |
266 | { |
267 | break; | |
268 | } | |
269 | } | |
270 | if (i < len) | |
271 | { | |
272 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
273 | } | |
274 | else | |
275 | { | |
276 | print_longest (stream, 'd', 0, val); | |
277 | } | |
278 | break; | |
279 | ||
280 | case TYPE_CODE_INT: | |
281 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); | |
282 | break; | |
283 | ||
284 | case TYPE_CODE_CHAR: | |
6c7a06a3 | 285 | LA_PRINT_CHAR ((unsigned char) val, type, stream); |
c906108c SS |
286 | break; |
287 | ||
288 | case TYPE_CODE_BOOL: | |
289 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); | |
290 | break; | |
291 | ||
292 | case TYPE_CODE_RANGE: | |
293 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); | |
294 | return; | |
295 | ||
296 | case TYPE_CODE_UNDEF: | |
297 | case TYPE_CODE_PTR: | |
298 | case TYPE_CODE_ARRAY: | |
299 | case TYPE_CODE_STRUCT: | |
300 | case TYPE_CODE_UNION: | |
301 | case TYPE_CODE_FUNC: | |
302 | case TYPE_CODE_FLT: | |
303 | case TYPE_CODE_VOID: | |
304 | case TYPE_CODE_SET: | |
305 | case TYPE_CODE_STRING: | |
306 | case TYPE_CODE_ERROR: | |
0d5de010 DJ |
307 | case TYPE_CODE_MEMBERPTR: |
308 | case TYPE_CODE_METHODPTR: | |
c906108c SS |
309 | case TYPE_CODE_METHOD: |
310 | case TYPE_CODE_REF: | |
5c4e30ca | 311 | case TYPE_CODE_NAMESPACE: |
8a3fe4f8 | 312 | error (_("internal error: unhandled type in print_type_scalar")); |
c906108c SS |
313 | break; |
314 | ||
315 | default: | |
8a3fe4f8 | 316 | error (_("Invalid type code in symbol table.")); |
c906108c SS |
317 | } |
318 | gdb_flush (stream); | |
319 | } | |
320 | ||
c906108c SS |
321 | /* Dump details of a type specified either directly or indirectly. |
322 | Uses the same sort of type lookup mechanism as ptype_command() | |
41808ebe | 323 | and whatis_command(). */ |
c906108c SS |
324 | |
325 | void | |
fba45db2 | 326 | maintenance_print_type (char *typename, int from_tty) |
c906108c | 327 | { |
3d6d86c6 | 328 | struct value *val; |
52f0bd74 AC |
329 | struct type *type; |
330 | struct cleanup *old_chain; | |
c906108c SS |
331 | struct expression *expr; |
332 | ||
333 | if (typename != NULL) | |
c5aa993b JM |
334 | { |
335 | expr = parse_expression (typename); | |
c13c43fd | 336 | old_chain = make_cleanup (free_current_contents, &expr); |
c5aa993b JM |
337 | if (expr->elts[0].opcode == OP_TYPE) |
338 | { | |
c378eb4e | 339 | /* The user expression names a type directly, just use that type. */ |
c5aa993b JM |
340 | type = expr->elts[1].type; |
341 | } | |
342 | else | |
343 | { | |
344 | /* The user expression may name a type indirectly by naming an | |
c378eb4e | 345 | object of that type. Find that indirectly named type. */ |
c5aa993b | 346 | val = evaluate_type (expr); |
df407dfe | 347 | type = value_type (val); |
c5aa993b JM |
348 | } |
349 | if (type != NULL) | |
350 | { | |
351 | recursive_dump_type (type, 0); | |
352 | } | |
353 | do_cleanups (old_chain); | |
354 | } | |
c906108c | 355 | } |
c906108c | 356 | \f |
c5aa993b | 357 | |
53342f27 TT |
358 | struct cmd_list_element *setprinttypelist; |
359 | ||
360 | struct cmd_list_element *showprinttypelist; | |
361 | ||
362 | static void | |
363 | set_print_type (char *arg, int from_tty) | |
364 | { | |
365 | printf_unfiltered ( | |
366 | "\"set print type\" must be followed by the name of a subcommand.\n"); | |
367 | help_list (setprintlist, "set print type ", -1, gdb_stdout); | |
368 | } | |
369 | ||
370 | static void | |
371 | show_print_type (char *args, int from_tty) | |
372 | { | |
373 | cmd_show_list (showprinttypelist, from_tty, ""); | |
374 | } | |
375 | ||
376 | static int print_methods = 1; | |
377 | ||
378 | static void | |
379 | set_print_type_methods (char *args, int from_tty, struct cmd_list_element *c) | |
380 | { | |
381 | default_ptype_flags.print_methods = print_methods; | |
382 | } | |
383 | ||
384 | static void | |
385 | show_print_type_methods (struct ui_file *file, int from_tty, | |
386 | struct cmd_list_element *c, const char *value) | |
387 | { | |
388 | fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"), | |
389 | value); | |
390 | } | |
391 | ||
392 | static int print_typedefs = 1; | |
393 | ||
394 | static void | |
395 | set_print_type_typedefs (char *args, int from_tty, struct cmd_list_element *c) | |
396 | { | |
397 | default_ptype_flags.print_typedefs = print_typedefs; | |
398 | } | |
399 | ||
400 | static void | |
401 | show_print_type_typedefs (struct ui_file *file, int from_tty, | |
402 | struct cmd_list_element *c, const char *value) | |
403 | { | |
404 | fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"), | |
405 | value); | |
406 | } | |
407 | ||
c906108c | 408 | void |
fba45db2 | 409 | _initialize_typeprint (void) |
c906108c | 410 | { |
1bedd215 AC |
411 | add_com ("ptype", class_vars, ptype_command, _("\ |
412 | Print definition of type TYPE.\n\ | |
53342f27 | 413 | Usage: ptype[/FLAGS] TYPE-NAME | EXPRESSION\n\ |
c906108c SS |
414 | Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\ |
415 | or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\ | |
11081198 | 416 | The selected stack frame's lexical context is used to look up the name.\n\ |
53342f27 TT |
417 | Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\ |
418 | \n\ | |
419 | Available FLAGS are:\n\ | |
420 | /r print in \"raw\" form; do not substitute typedefs\n\ | |
421 | /m do not print methods defined in a class\n\ | |
422 | /M print methods defined in a class\n\ | |
423 | /t do not print typedefs defined in a class\n\ | |
424 | /T print typedefs defined in a class")); | |
c906108c SS |
425 | |
426 | add_com ("whatis", class_vars, whatis_command, | |
11081198 JK |
427 | _("Print data type of expression EXP.\n\ |
428 | Only one level of typedefs is unrolled. See also \"ptype\".")); | |
53342f27 TT |
429 | |
430 | add_prefix_cmd ("type", no_class, show_print_type, | |
431 | _("Generic command for showing type-printing settings."), | |
432 | &showprinttypelist, "show print type ", 0, &showprintlist); | |
433 | add_prefix_cmd ("type", no_class, set_print_type, | |
434 | _("Generic command for setting how types print."), | |
435 | &setprinttypelist, "show print type ", 0, &setprintlist); | |
436 | ||
437 | add_setshow_boolean_cmd ("methods", no_class, &print_methods, | |
438 | _("\ | |
439 | Set printing of methods defined in classes."), _("\ | |
440 | Show printing of methods defined in classes."), NULL, | |
441 | set_print_type_methods, | |
442 | show_print_type_methods, | |
443 | &setprinttypelist, &showprinttypelist); | |
444 | add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs, | |
445 | _("\ | |
446 | Set printing of typedefs defined in classes."), _("\ | |
447 | Show printing of typedefs defined in classes."), NULL, | |
448 | set_print_type_typedefs, | |
449 | show_print_type_typedefs, | |
450 | &setprinttypelist, &showprinttypelist); | |
c906108c | 451 | } |