509b3ee3632d72aecd96fb263f19f10206699fef
[binutils-gdb.git] / gdb / typeprint.c
1 /* Language independent support for printing types for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1988-1989, 1991-1995, 1998-2001, 2003, 2006-2012
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdb_obstack.h"
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"
33 #include "cp-abi.h"
34 #include "typeprint.h"
35 #include "gdb_string.h"
36 #include "exceptions.h"
37 #include "valprint.h"
38 #include <errno.h>
39 #include <ctype.h>
40 #include "cli/cli-utils.h"
41
42 extern void _initialize_typeprint (void);
43
44 static void ptype_command (char *, int);
45
46 static void whatis_command (char *, int);
47
48 static void whatis_exp (char *, int);
49
50 const struct type_print_options type_print_raw_options =
51 {
52 1, /* raw */
53 1, /* print_methods */
54 1 /* print_typedefs */
55 };
56
57 /* The default flags for 'ptype' and 'whatis'. */
58
59 static struct type_print_options default_ptype_flags =
60 {
61 0, /* raw */
62 1, /* print_methods */
63 1 /* print_typedefs */
64 };
65
66 \f
67
68 /* Print a description of a type in the format of a
69 typedef for the current language.
70 NEW is the new name for a type TYPE. */
71
72 void
73 typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
74 {
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."));
85 }
86
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
95 type_print (struct type *type, const char *varstring, struct ui_file *stream,
96 int show)
97 {
98 LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
99 }
100
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;
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);
118 s = ui_file_xstrdup (stb, NULL);
119 }
120 if (except.reason < 0)
121 s = NULL;
122
123 do_cleanups (old_chain);
124
125 return s;
126 }
127
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
132 whatis_exp (char *exp, int show)
133 {
134 struct expression *expr;
135 struct value *val;
136 struct cleanup *old_chain = NULL;
137 struct type *real_type = NULL;
138 struct type *type;
139 int full = 0;
140 int top = -1;
141 int using_enc = 0;
142 struct value_print_options opts;
143 struct type_print_options flags = default_ptype_flags;
144
145 if (exp)
146 {
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
183 expr = parse_expression (exp);
184 old_chain = make_cleanup (free_current_contents, &expr);
185 val = evaluate_type (expr);
186 }
187 else
188 val = access_value_history (0);
189
190 type = value_type (val);
191
192 get_user_print_options (&opts);
193 if (opts.objectprint)
194 {
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))
198 real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
199 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
200 real_type = value_rtti_type (val, &full, &top, &using_enc);
201 }
202
203 printf_filtered ("type = ");
204
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 }
213
214 LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
215 printf_filtered ("\n");
216
217 if (exp)
218 do_cleanups (old_chain);
219 }
220
221 static void
222 whatis_command (char *exp, int from_tty)
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
230 /* TYPENAME is either the name of a type, or an expression. */
231
232 static void
233 ptype_command (char *typename, int from_tty)
234 {
235 whatis_exp (typename, 1);
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
248 that come from the inferior in target byte order and target size. */
249
250 void
251 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
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 {
265 if (TYPE_FIELD_ENUMVAL (type, i) == val)
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:
285 LA_PRINT_CHAR ((unsigned char) val, type, stream);
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:
307 case TYPE_CODE_MEMBERPTR:
308 case TYPE_CODE_METHODPTR:
309 case TYPE_CODE_METHOD:
310 case TYPE_CODE_REF:
311 case TYPE_CODE_NAMESPACE:
312 error (_("internal error: unhandled type in print_type_scalar"));
313 break;
314
315 default:
316 error (_("Invalid type code in symbol table."));
317 }
318 gdb_flush (stream);
319 }
320
321 /* Dump details of a type specified either directly or indirectly.
322 Uses the same sort of type lookup mechanism as ptype_command()
323 and whatis_command(). */
324
325 void
326 maintenance_print_type (char *typename, int from_tty)
327 {
328 struct value *val;
329 struct type *type;
330 struct cleanup *old_chain;
331 struct expression *expr;
332
333 if (typename != NULL)
334 {
335 expr = parse_expression (typename);
336 old_chain = make_cleanup (free_current_contents, &expr);
337 if (expr->elts[0].opcode == OP_TYPE)
338 {
339 /* The user expression names a type directly, just use that type. */
340 type = expr->elts[1].type;
341 }
342 else
343 {
344 /* The user expression may name a type indirectly by naming an
345 object of that type. Find that indirectly named type. */
346 val = evaluate_type (expr);
347 type = value_type (val);
348 }
349 if (type != NULL)
350 {
351 recursive_dump_type (type, 0);
352 }
353 do_cleanups (old_chain);
354 }
355 }
356 \f
357
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
408 void
409 _initialize_typeprint (void)
410 {
411 add_com ("ptype", class_vars, ptype_command, _("\
412 Print definition of type TYPE.\n\
413 Usage: ptype[/FLAGS] TYPE-NAME | EXPRESSION\n\
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\
416 The selected stack frame's lexical context is used to look up the name.\n\
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"));
425
426 add_com ("whatis", class_vars, whatis_command,
427 _("Print data type of expression EXP.\n\
428 Only one level of typedefs is unrolled. See also \"ptype\"."));
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);
451 }