1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
4 2000, 2001, 2003, 2006, 2007, 2008, 2009 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 extern void _initialize_typeprint (void);
42 static void ptype_command (char *, int);
44 static void whatis_command (char *, int);
46 static void whatis_exp (char *, int);
49 /* Print a description of a type in the format of a
50 typedef for the current language.
51 NEW is the new name for a type TYPE. */
54 typedef_print (struct type
*type
, struct symbol
*new, struct ui_file
*stream
)
56 LA_PRINT_TYPEDEF (type
, new, stream
);
59 /* The default way to print a typedef. */
62 default_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
63 struct ui_file
*stream
)
65 error (_("Language not supported."));
68 /* Print a description of a type TYPE in the form of a declaration of a
69 variable named VARSTRING. (VARSTRING is demangled if necessary.)
70 Output goes to STREAM (via stdio).
71 If SHOW is positive, we show the contents of the outermost level
72 of structure even if there is a type name that could be used instead.
73 If SHOW is negative, we never show the details of elements' types. */
76 type_print (struct type
*type
, char *varstring
, struct ui_file
*stream
,
79 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
82 /* Print TYPE to a string, returning it. The caller is responsible for
83 freeing the string. */
86 type_to_string (struct type
*type
)
91 struct cleanup
*old_chain
;
92 volatile struct gdb_exception except
;
94 stb
= mem_fileopen ();
95 old_chain
= make_cleanup_ui_file_delete (stb
);
97 TRY_CATCH (except
, RETURN_MASK_ALL
)
99 type_print (type
, "", stb
, -1);
100 s
= ui_file_xstrdup (stb
, &dummy
);
102 if (except
.reason
< 0)
105 do_cleanups (old_chain
);
110 /* Print type of EXP, or last thing in value history if EXP == NULL.
111 show is passed to type_print. */
114 whatis_exp (char *exp
, int show
)
116 struct expression
*expr
;
118 struct cleanup
*old_chain
= NULL
;
119 struct type
*real_type
= NULL
;
124 struct value_print_options opts
;
128 expr
= parse_expression (exp
);
129 old_chain
= make_cleanup (free_current_contents
, &expr
);
130 val
= evaluate_type (expr
);
133 val
= access_value_history (0);
135 type
= value_type (val
);
137 get_user_print_options (&opts
);
138 if (opts
.objectprint
)
140 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
141 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
142 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
144 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
147 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
148 real_type
= lookup_pointer_type (real_type
);
150 real_type
= lookup_reference_type (real_type
);
153 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
154 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
157 printf_filtered ("type = ");
161 printf_filtered ("/* real type = ");
162 type_print (real_type
, "", gdb_stdout
, -1);
164 printf_filtered (" (incomplete object)");
165 printf_filtered (" */\n");
168 type_print (type
, "", gdb_stdout
, show
);
169 printf_filtered ("\n");
172 do_cleanups (old_chain
);
176 whatis_command (char *exp
, int from_tty
)
178 /* Most of the time users do not want to see all the fields
179 in a structure. If they do they can use the "ptype" command.
180 Hence the "-1" below. */
181 whatis_exp (exp
, -1);
184 /* TYPENAME is either the name of a type, or an expression. */
187 ptype_command (char *typename
, int from_tty
)
189 whatis_exp (typename
, 1);
192 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
193 Used to print data from type structures in a specified type. For example,
194 array bounds may be characters or booleans in some languages, and this
195 allows the ranges to be printed in their "natural" form rather than as
196 decimal integer values.
198 FIXME: This is here simply because only the type printing routines
199 currently use it, and it wasn't clear if it really belonged somewhere
200 else (like printcmd.c). There are a lot of other gdb routines that do
201 something similar, but they are generally concerned with printing values
202 that come from the inferior in target byte order and target size. */
205 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
210 CHECK_TYPEDEF (type
);
212 switch (TYPE_CODE (type
))
216 len
= TYPE_NFIELDS (type
);
217 for (i
= 0; i
< len
; i
++)
219 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
226 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
230 print_longest (stream
, 'd', 0, val
);
235 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
239 LA_PRINT_CHAR ((unsigned char) val
, type
, stream
);
243 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
246 case TYPE_CODE_RANGE
:
247 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
250 case TYPE_CODE_UNDEF
:
252 case TYPE_CODE_ARRAY
:
253 case TYPE_CODE_STRUCT
:
254 case TYPE_CODE_UNION
:
259 case TYPE_CODE_STRING
:
260 case TYPE_CODE_ERROR
:
261 case TYPE_CODE_MEMBERPTR
:
262 case TYPE_CODE_METHODPTR
:
263 case TYPE_CODE_METHOD
:
265 case TYPE_CODE_NAMESPACE
:
266 error (_("internal error: unhandled type in print_type_scalar"));
270 error (_("Invalid type code in symbol table."));
275 /* Dump details of a type specified either directly or indirectly.
276 Uses the same sort of type lookup mechanism as ptype_command()
277 and whatis_command(). */
280 maintenance_print_type (char *typename
, int from_tty
)
284 struct cleanup
*old_chain
;
285 struct expression
*expr
;
287 if (typename
!= NULL
)
289 expr
= parse_expression (typename
);
290 old_chain
= make_cleanup (free_current_contents
, &expr
);
291 if (expr
->elts
[0].opcode
== OP_TYPE
)
293 /* The user expression names a type directly, just use that type. */
294 type
= expr
->elts
[1].type
;
298 /* The user expression may name a type indirectly by naming an
299 object of that type. Find that indirectly named type. */
300 val
= evaluate_type (expr
);
301 type
= value_type (val
);
305 recursive_dump_type (type
, 0);
307 do_cleanups (old_chain
);
313 _initialize_typeprint (void)
315 add_com ("ptype", class_vars
, ptype_command
, _("\
316 Print definition of type TYPE.\n\
317 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
318 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
319 The selected stack frame's lexical context is used to look up the name."));
321 add_com ("whatis", class_vars
, whatis_command
,
322 _("Print data type of expression EXP."));