1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988-1989, 1991-1995, 1998-2001, 2003, 2006-2012
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdb_obstack.h"
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
34 #include "typeprint.h"
35 #include "gdb_string.h"
36 #include "exceptions.h"
40 #include "cli/cli-utils.h"
42 extern void _initialize_typeprint (void);
44 static void ptype_command (char *, int);
46 static void whatis_command (char *, int);
48 static void whatis_exp (char *, int);
50 const struct type_print_options type_print_raw_options =
53 1, /* print_methods */
54 1 /* print_typedefs */
57 /* The default flags for 'ptype' and 'whatis'. */
59 static struct type_print_options default_ptype_flags =
62 1, /* print_methods */
63 1 /* print_typedefs */
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. */
73 typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
75 LA_PRINT_TYPEDEF (type, new, stream);
78 /* The default way to print a typedef. */
81 default_print_typedef (struct type *type, struct symbol *new_symbol,
82 struct ui_file *stream)
84 error (_("Language not supported."));
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. */
95 type_print (struct type *type, const char *varstring, struct ui_file *stream,
98 LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
101 /* Print TYPE to a string, returning it. The caller is responsible for
102 freeing the string. */
105 type_to_string (struct type *type)
109 struct cleanup *old_chain;
110 volatile struct gdb_exception except;
112 stb = mem_fileopen ();
113 old_chain = make_cleanup_ui_file_delete (stb);
115 TRY_CATCH (except, RETURN_MASK_ALL)
117 type_print (type, "", stb, -1);
118 s = ui_file_xstrdup (stb, NULL);
120 if (except.reason < 0)
123 do_cleanups (old_chain);
128 /* Print type of EXP, or last thing in value history if EXP == NULL.
129 show is passed to type_print. */
132 whatis_exp (char *exp, int show)
134 struct expression *expr;
136 struct cleanup *old_chain = NULL;
137 struct type *real_type = NULL;
142 struct value_print_options opts;
143 struct type_print_options flags = default_ptype_flags;
151 for (++exp; *exp && !isspace (*exp); ++exp)
159 flags.print_methods = 0;
162 flags.print_methods = 1;
165 flags.print_typedefs = 0;
168 flags.print_typedefs = 1;
171 error (_("unrecognized flag '%c'"), *exp);
176 if (!*exp && !seen_one)
177 error (_("flag expected"));
179 error (_("expected space after format"));
180 exp = skip_spaces (exp);
183 expr = parse_expression (exp);
184 old_chain = make_cleanup (free_current_contents, &expr);
185 val = evaluate_type (expr);
188 val = access_value_history (0);
190 type = value_type (val);
192 get_user_print_options (&opts);
193 if (opts.objectprint)
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);
203 printf_filtered ("type = ");
207 printf_filtered ("/* real type = ");
208 type_print (real_type, "", gdb_stdout, -1);
210 printf_filtered (" (incomplete object)");
211 printf_filtered (" */\n");
214 LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
215 printf_filtered ("\n");
218 do_cleanups (old_chain);
222 whatis_command (char *exp, int from_tty)
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);
230 /* TYPENAME is either the name of a type, or an expression. */
233 ptype_command (char *typename, int from_tty)
235 whatis_exp (typename, 1);
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.
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. */
251 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
256 CHECK_TYPEDEF (type);
258 switch (TYPE_CODE (type))
262 len = TYPE_NFIELDS (type);
263 for (i = 0; i < len; i++)
265 if (TYPE_FIELD_ENUMVAL (type, i) == val)
272 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
276 print_longest (stream, 'd', 0, val);
281 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
285 LA_PRINT_CHAR ((unsigned char) val, type, stream);
289 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
292 case TYPE_CODE_RANGE:
293 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
296 case TYPE_CODE_UNDEF:
298 case TYPE_CODE_ARRAY:
299 case TYPE_CODE_STRUCT:
300 case TYPE_CODE_UNION:
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:
311 case TYPE_CODE_NAMESPACE:
312 error (_("internal error: unhandled type in print_type_scalar"));
316 error (_("Invalid type code in symbol table."));
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(). */
326 maintenance_print_type (char *typename, int from_tty)
330 struct cleanup *old_chain;
331 struct expression *expr;
333 if (typename != NULL)
335 expr = parse_expression (typename);
336 old_chain = make_cleanup (free_current_contents, &expr);
337 if (expr->elts[0].opcode == OP_TYPE)
339 /* The user expression names a type directly, just use that type. */
340 type = expr->elts[1].type;
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);
351 recursive_dump_type (type, 0);
353 do_cleanups (old_chain);
358 struct cmd_list_element *setprinttypelist;
360 struct cmd_list_element *showprinttypelist;
363 set_print_type (char *arg, int from_tty)
366 "\"set print type\" must be followed by the name of a subcommand.\n");
367 help_list (setprintlist, "set print type ", -1, gdb_stdout);
371 show_print_type (char *args, int from_tty)
373 cmd_show_list (showprinttypelist, from_tty, "");
376 static int print_methods = 1;
379 set_print_type_methods (char *args, int from_tty, struct cmd_list_element *c)
381 default_ptype_flags.print_methods = print_methods;
385 show_print_type_methods (struct ui_file *file, int from_tty,
386 struct cmd_list_element *c, const char *value)
388 fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
392 static int print_typedefs = 1;
395 set_print_type_typedefs (char *args, int from_tty, struct cmd_list_element *c)
397 default_ptype_flags.print_typedefs = print_typedefs;
401 show_print_type_typedefs (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c, const char *value)
404 fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
409 _initialize_typeprint (void)
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\
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"));
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\"."));
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);
437 add_setshow_boolean_cmd ("methods", no_class, &print_methods,
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,
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);