2000-07-17 matthew green <mrg@redhat.com>
[deliverable/binutils-gdb.git] / gdb / typeprint.c
1 /* Language independent support for printing types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991-1993, 1998, 2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "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 "demangle.h"
34
35 #include "gdb_string.h"
36 #include <errno.h>
37
38 /* For real-type printing in whatis_exp() */
39 extern int objectprint; /* Controls looking up an object's derived type
40 using what we find in its vtables. */
41
42 extern void _initialize_typeprint (void);
43
44 static void ptype_command (char *, int);
45
46 static struct type *ptype_eval (struct expression *);
47
48 static void whatis_command (char *, int);
49
50 static void whatis_exp (char *, int);
51
52 /* Print a description of a type TYPE in the form of a declaration of a
53 variable named VARSTRING. (VARSTRING is demangled if necessary.)
54 Output goes to STREAM (via stdio).
55 If SHOW is positive, we show the contents of the outermost level
56 of structure even if there is a type name that could be used instead.
57 If SHOW is negative, we never show the details of elements' types. */
58
59 void
60 type_print (type, varstring, stream, show)
61 struct type *type;
62 char *varstring;
63 struct ui_file *stream;
64 int show;
65 {
66 LA_PRINT_TYPE (type, varstring, stream, show, 0);
67 }
68
69 /* Print type of EXP, or last thing in value history if EXP == NULL.
70 show is passed to type_print. */
71
72 static void
73 whatis_exp (exp, show)
74 char *exp;
75 int show;
76 {
77 struct expression *expr;
78 register value_ptr val;
79 register struct cleanup *old_chain = NULL;
80 struct type *real_type = NULL;
81 struct type *type;
82 int full = 0;
83 int top = -1;
84 int using_enc = 0;
85
86 if (exp)
87 {
88 expr = parse_expression (exp);
89 old_chain = make_cleanup (free_current_contents, &expr);
90 val = evaluate_type (expr);
91 }
92 else
93 val = access_value_history (0);
94
95 type = VALUE_TYPE (val);
96
97 if (objectprint)
98 {
99 if (((TYPE_CODE (type) == TYPE_CODE_PTR) ||
100 (TYPE_CODE (type) == TYPE_CODE_REF))
101 &&
102 (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
103 {
104 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
105 if (real_type)
106 {
107 if (TYPE_CODE (type) == TYPE_CODE_PTR)
108 real_type = lookup_pointer_type (real_type);
109 else
110 real_type = lookup_reference_type (real_type);
111 }
112 }
113 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
114 real_type = value_rtti_type (val, &full, &top, &using_enc);
115 }
116
117 printf_filtered ("type = ");
118
119 if (real_type)
120 {
121 printf_filtered ("/* real type = ");
122 type_print (real_type, "", gdb_stdout, -1);
123 if (! full)
124 printf_filtered (" (incomplete object)");
125 printf_filtered (" */\n");
126 }
127
128 type_print (type, "", gdb_stdout, show);
129 printf_filtered ("\n");
130
131 if (exp)
132 do_cleanups (old_chain);
133 }
134
135 /* ARGSUSED */
136 static void
137 whatis_command (exp, from_tty)
138 char *exp;
139 int from_tty;
140 {
141 /* Most of the time users do not want to see all the fields
142 in a structure. If they do they can use the "ptype" command.
143 Hence the "-1" below. */
144 whatis_exp (exp, -1);
145 }
146
147 /* Simple subroutine for ptype_command. */
148
149 static struct type *
150 ptype_eval (exp)
151 struct expression *exp;
152 {
153 if (exp->elts[0].opcode == OP_TYPE)
154 {
155 return (exp->elts[1].type);
156 }
157 else
158 {
159 return (NULL);
160 }
161 }
162
163 /* TYPENAME is either the name of a type, or an expression. */
164
165 /* ARGSUSED */
166 static void
167 ptype_command (typename, from_tty)
168 char *typename;
169 int from_tty;
170 {
171 register struct type *type;
172 struct expression *expr;
173 register struct cleanup *old_chain;
174
175 if (typename == NULL)
176 {
177 /* Print type of last thing in value history. */
178 whatis_exp (typename, 1);
179 }
180 else
181 {
182 expr = parse_expression (typename);
183 old_chain = make_cleanup (free_current_contents, &expr);
184 type = ptype_eval (expr);
185 if (type != NULL)
186 {
187 /* User did "ptype <typename>" */
188 printf_filtered ("type = ");
189 type_print (type, "", gdb_stdout, 1);
190 printf_filtered ("\n");
191 do_cleanups (old_chain);
192 }
193 else
194 {
195 /* User did "ptype <symbolname>" */
196 do_cleanups (old_chain);
197 whatis_exp (typename, 1);
198 }
199 }
200 }
201
202 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
203 Used to print data from type structures in a specified type. For example,
204 array bounds may be characters or booleans in some languages, and this
205 allows the ranges to be printed in their "natural" form rather than as
206 decimal integer values.
207
208 FIXME: This is here simply because only the type printing routines
209 currently use it, and it wasn't clear if it really belonged somewhere
210 else (like printcmd.c). There are a lot of other gdb routines that do
211 something similar, but they are generally concerned with printing values
212 that come from the inferior in target byte order and target size. */
213
214 void
215 print_type_scalar (type, val, stream)
216 struct type *type;
217 LONGEST val;
218 struct ui_file *stream;
219 {
220 unsigned int i;
221 unsigned len;
222
223 CHECK_TYPEDEF (type);
224
225 switch (TYPE_CODE (type))
226 {
227
228 case TYPE_CODE_ENUM:
229 len = TYPE_NFIELDS (type);
230 for (i = 0; i < len; i++)
231 {
232 if (TYPE_FIELD_BITPOS (type, i) == val)
233 {
234 break;
235 }
236 }
237 if (i < len)
238 {
239 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
240 }
241 else
242 {
243 print_longest (stream, 'd', 0, val);
244 }
245 break;
246
247 case TYPE_CODE_INT:
248 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
249 break;
250
251 case TYPE_CODE_CHAR:
252 LA_PRINT_CHAR ((unsigned char) val, stream);
253 break;
254
255 case TYPE_CODE_BOOL:
256 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
257 break;
258
259 case TYPE_CODE_RANGE:
260 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
261 return;
262
263 case TYPE_CODE_UNDEF:
264 case TYPE_CODE_PTR:
265 case TYPE_CODE_ARRAY:
266 case TYPE_CODE_STRUCT:
267 case TYPE_CODE_UNION:
268 case TYPE_CODE_FUNC:
269 case TYPE_CODE_FLT:
270 case TYPE_CODE_VOID:
271 case TYPE_CODE_SET:
272 case TYPE_CODE_STRING:
273 case TYPE_CODE_ERROR:
274 case TYPE_CODE_MEMBER:
275 case TYPE_CODE_METHOD:
276 case TYPE_CODE_REF:
277 error ("internal error: unhandled type in print_type_scalar");
278 break;
279
280 default:
281 error ("Invalid type code in symbol table.");
282 }
283 gdb_flush (stream);
284 }
285
286 /* Dump details of a type specified either directly or indirectly.
287 Uses the same sort of type lookup mechanism as ptype_command()
288 and whatis_command(). */
289
290 void
291 maintenance_print_type (typename, from_tty)
292 char *typename;
293 int from_tty;
294 {
295 register value_ptr val;
296 register struct type *type;
297 register struct cleanup *old_chain;
298 struct expression *expr;
299
300 if (typename != NULL)
301 {
302 expr = parse_expression (typename);
303 old_chain = make_cleanup (free_current_contents, &expr);
304 if (expr->elts[0].opcode == OP_TYPE)
305 {
306 /* The user expression names a type directly, just use that type. */
307 type = expr->elts[1].type;
308 }
309 else
310 {
311 /* The user expression may name a type indirectly by naming an
312 object of that type. Find that indirectly named type. */
313 val = evaluate_type (expr);
314 type = VALUE_TYPE (val);
315 }
316 if (type != NULL)
317 {
318 recursive_dump_type (type, 0);
319 }
320 do_cleanups (old_chain);
321 }
322 }
323 \f
324
325 void
326 _initialize_typeprint ()
327 {
328
329 add_com ("ptype", class_vars, ptype_command,
330 "Print definition of type TYPE.\n\
331 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
332 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
333 The selected stack frame's lexical context is used to look up the name.");
334
335 add_com ("whatis", class_vars, whatis_command,
336 "Print data type of expression EXP.");
337
338 }
This page took 0.042907 seconds and 4 git commands to generate.