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