]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/typeprint.c
* dwarf2read.c (lookup_signatured_type): Return NULL instead of 0.
[thirdparty/binutils-gdb.git] / gdb / typeprint.c
CommitLineData
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
SS
38#include <errno.h>
39
a14ed312 40extern void _initialize_typeprint (void);
392a587b 41
a14ed312 42static void ptype_command (char *, int);
c906108c 43
a14ed312 44static void whatis_command (char *, int);
c906108c 45
a14ed312 46static void whatis_exp (char *, int);
c906108c 47
5c6ce71d 48
a5238fbc
PM
49/* Print a description of a type in the format of a
50 typedef for the current language.
c378eb4e 51 NEW is the new name for a type TYPE. */
a5238fbc
PM
52
53void
54typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
55{
5c6ce71d
TT
56 LA_PRINT_TYPEDEF (type, new, stream);
57}
58
59/* The default way to print a typedef. */
60
61void
62default_print_typedef (struct type *type, struct symbol *new_symbol,
63 struct ui_file *stream)
64{
65 error (_("Language not supported."));
a5238fbc
PM
66}
67
c906108c
SS
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. */
74
75void
0d5cff50 76type_print (struct type *type, const char *varstring, struct ui_file *stream,
fba45db2 77 int show)
c906108c
SS
78{
79 LA_PRINT_TYPE (type, varstring, stream, show, 0);
80}
81
ae6a3a4c
TJB
82/* Print TYPE to a string, returning it. The caller is responsible for
83 freeing the string. */
84
85char *
86type_to_string (struct type *type)
87{
88 char *s = NULL;
ae6a3a4c
TJB
89 struct ui_file *stb;
90 struct cleanup *old_chain;
91 volatile struct gdb_exception except;
92
93 stb = mem_fileopen ();
94 old_chain = make_cleanup_ui_file_delete (stb);
95
96 TRY_CATCH (except, RETURN_MASK_ALL)
97 {
98 type_print (type, "", stb, -1);
759ef836 99 s = ui_file_xstrdup (stb, NULL);
ae6a3a4c
TJB
100 }
101 if (except.reason < 0)
102 s = NULL;
103
104 do_cleanups (old_chain);
105
106 return s;
107}
108
c906108c
SS
109/* Print type of EXP, or last thing in value history if EXP == NULL.
110 show is passed to type_print. */
111
112static void
fba45db2 113whatis_exp (char *exp, int show)
c906108c
SS
114{
115 struct expression *expr;
3d6d86c6 116 struct value *val;
52f0bd74 117 struct cleanup *old_chain = NULL;
c5aa993b 118 struct type *real_type = NULL;
070ad9f0 119 struct type *type;
c906108c
SS
120 int full = 0;
121 int top = -1;
122 int using_enc = 0;
79a45b7d 123 struct value_print_options opts;
c906108c
SS
124
125 if (exp)
126 {
127 expr = parse_expression (exp);
c13c43fd 128 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
129 val = evaluate_type (expr);
130 }
131 else
132 val = access_value_history (0);
133
df407dfe 134 type = value_type (val);
070ad9f0 135
79a45b7d
TT
136 get_user_print_options (&opts);
137 if (opts.objectprint)
070ad9f0 138 {
41808ebe
DE
139 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
140 || (TYPE_CODE (type) == TYPE_CODE_REF))
141 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
dfcee124 142 real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
070ad9f0 143 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
41808ebe 144 real_type = value_rtti_type (val, &full, &top, &using_enc);
070ad9f0 145 }
c906108c
SS
146
147 printf_filtered ("type = ");
148
070ad9f0
DB
149 if (real_type)
150 {
151 printf_filtered ("/* real type = ");
152 type_print (real_type, "", gdb_stdout, -1);
153 if (! full)
154 printf_filtered (" (incomplete object)");
155 printf_filtered (" */\n");
156 }
c906108c 157
070ad9f0 158 type_print (type, "", gdb_stdout, show);
c906108c
SS
159 printf_filtered ("\n");
160
161 if (exp)
162 do_cleanups (old_chain);
163}
164
c906108c 165static void
fba45db2 166whatis_command (char *exp, int from_tty)
c906108c
SS
167{
168 /* Most of the time users do not want to see all the fields
169 in a structure. If they do they can use the "ptype" command.
170 Hence the "-1" below. */
171 whatis_exp (exp, -1);
172}
173
c906108c
SS
174/* TYPENAME is either the name of a type, or an expression. */
175
c906108c 176static void
fba45db2 177ptype_command (char *typename, int from_tty)
c906108c 178{
d843c49c 179 whatis_exp (typename, 1);
c906108c
SS
180}
181
182/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
183 Used to print data from type structures in a specified type. For example,
184 array bounds may be characters or booleans in some languages, and this
185 allows the ranges to be printed in their "natural" form rather than as
186 decimal integer values.
187
188 FIXME: This is here simply because only the type printing routines
189 currently use it, and it wasn't clear if it really belonged somewhere
190 else (like printcmd.c). There are a lot of other gdb routines that do
191 something similar, but they are generally concerned with printing values
41808ebe 192 that come from the inferior in target byte order and target size. */
c906108c
SS
193
194void
fba45db2 195print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
c906108c
SS
196{
197 unsigned int i;
198 unsigned len;
199
200 CHECK_TYPEDEF (type);
201
202 switch (TYPE_CODE (type))
203 {
204
205 case TYPE_CODE_ENUM:
206 len = TYPE_NFIELDS (type);
207 for (i = 0; i < len; i++)
208 {
209 if (TYPE_FIELD_BITPOS (type, i) == val)
210 {
211 break;
212 }
213 }
214 if (i < len)
215 {
216 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
217 }
218 else
219 {
220 print_longest (stream, 'd', 0, val);
221 }
222 break;
223
224 case TYPE_CODE_INT:
225 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
226 break;
227
228 case TYPE_CODE_CHAR:
6c7a06a3 229 LA_PRINT_CHAR ((unsigned char) val, type, stream);
c906108c
SS
230 break;
231
232 case TYPE_CODE_BOOL:
233 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
234 break;
235
236 case TYPE_CODE_RANGE:
237 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
238 return;
239
240 case TYPE_CODE_UNDEF:
241 case TYPE_CODE_PTR:
242 case TYPE_CODE_ARRAY:
243 case TYPE_CODE_STRUCT:
244 case TYPE_CODE_UNION:
245 case TYPE_CODE_FUNC:
246 case TYPE_CODE_FLT:
247 case TYPE_CODE_VOID:
248 case TYPE_CODE_SET:
249 case TYPE_CODE_STRING:
250 case TYPE_CODE_ERROR:
0d5de010
DJ
251 case TYPE_CODE_MEMBERPTR:
252 case TYPE_CODE_METHODPTR:
c906108c
SS
253 case TYPE_CODE_METHOD:
254 case TYPE_CODE_REF:
5c4e30ca 255 case TYPE_CODE_NAMESPACE:
8a3fe4f8 256 error (_("internal error: unhandled type in print_type_scalar"));
c906108c
SS
257 break;
258
259 default:
8a3fe4f8 260 error (_("Invalid type code in symbol table."));
c906108c
SS
261 }
262 gdb_flush (stream);
263}
264
c906108c
SS
265/* Dump details of a type specified either directly or indirectly.
266 Uses the same sort of type lookup mechanism as ptype_command()
41808ebe 267 and whatis_command(). */
c906108c
SS
268
269void
fba45db2 270maintenance_print_type (char *typename, int from_tty)
c906108c 271{
3d6d86c6 272 struct value *val;
52f0bd74
AC
273 struct type *type;
274 struct cleanup *old_chain;
c906108c
SS
275 struct expression *expr;
276
277 if (typename != NULL)
c5aa993b
JM
278 {
279 expr = parse_expression (typename);
c13c43fd 280 old_chain = make_cleanup (free_current_contents, &expr);
c5aa993b
JM
281 if (expr->elts[0].opcode == OP_TYPE)
282 {
c378eb4e 283 /* The user expression names a type directly, just use that type. */
c5aa993b
JM
284 type = expr->elts[1].type;
285 }
286 else
287 {
288 /* The user expression may name a type indirectly by naming an
c378eb4e 289 object of that type. Find that indirectly named type. */
c5aa993b 290 val = evaluate_type (expr);
df407dfe 291 type = value_type (val);
c5aa993b
JM
292 }
293 if (type != NULL)
294 {
295 recursive_dump_type (type, 0);
296 }
297 do_cleanups (old_chain);
298 }
c906108c 299}
c906108c 300\f
c5aa993b 301
c906108c 302void
fba45db2 303_initialize_typeprint (void)
c906108c 304{
1bedd215
AC
305 add_com ("ptype", class_vars, ptype_command, _("\
306Print definition of type TYPE.\n\
c906108c
SS
307Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
308or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
11081198
JK
309The selected stack frame's lexical context is used to look up the name.\n\
310Contrary to \"whatis\", \"ptype\" always unrolls any typedefs."));
c906108c
SS
311
312 add_com ("whatis", class_vars, whatis_command,
11081198
JK
313 _("Print data type of expression EXP.\n\
314Only one level of typedefs is unrolled. See also \"ptype\"."));
c906108c 315}