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