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