* run.c (usage): Fix typos.
[deliverable/binutils-gdb.git] / gdb / typeprint.c
CommitLineData
c906108c 1/* Language independent support for printing types for GDB, the GNU debugger.
b6ba6518 2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
3d6d86c6 3 2000, 2001 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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. */
c906108c
SS
21
22#include "defs.h"
04ea0df1 23#include "gdb_obstack.h"
c906108c
SS
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"
015a42b4 34#include "cp-abi.h"
c906108c
SS
35
36#include "gdb_string.h"
37#include <errno.h>
38
39/* For real-type printing in whatis_exp() */
40extern int objectprint; /* Controls looking up an object's derived type
41 using what we find in its vtables. */
42
a14ed312 43extern void _initialize_typeprint (void);
392a587b 44
a14ed312 45static void ptype_command (char *, int);
c906108c 46
a14ed312 47static struct type *ptype_eval (struct expression *);
c906108c 48
a14ed312 49static void whatis_command (char *, int);
c906108c 50
a14ed312 51static void whatis_exp (char *, int);
c906108c 52
a5238fbc
PM
53/* Print a description of a type in the format of a
54 typedef for the current language.
55 NEW is the new name for a type TYPE. */
56
57void
58typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
59{
60 CHECK_TYPEDEF (type);
61 switch (current_language->la_language)
62 {
63#ifdef _LANG_c
64 case language_c:
65 case language_cplus:
66 fprintf_filtered (stream, "typedef ");
67 type_print (type, "", stream, 0);
68 if (TYPE_NAME ((SYMBOL_TYPE (new))) == 0
69 || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
70 fprintf_filtered (stream, " %s", SYMBOL_SOURCE_NAME (new));
71 break;
72#endif
73#ifdef _LANG_m2
74 case language_m2:
75 fprintf_filtered (stream, "TYPE ");
76 if (!TYPE_NAME (SYMBOL_TYPE (new)) ||
77 !STREQ (TYPE_NAME (SYMBOL_TYPE (new)), SYMBOL_NAME (new)))
78 fprintf_filtered (stream, "%s = ", SYMBOL_SOURCE_NAME (new));
79 else
80 fprintf_filtered (stream, "<builtin> = ");
81 type_print (type, "", stream, 0);
82 break;
83#endif
84#ifdef _LANG_pascal
85 case language_pascal:
86 fprintf_filtered (stream, "type ");
87 fprintf_filtered (stream, "%s = ", SYMBOL_SOURCE_NAME (new));
88 type_print (type, "", stream, 0);
89 break;
a5238fbc
PM
90#endif
91 default:
92 error ("Language not supported.");
93 }
94 fprintf_filtered (stream, ";\n");
95}
96
c906108c
SS
97/* Print a description of a type TYPE in the form of a declaration of a
98 variable named VARSTRING. (VARSTRING is demangled if necessary.)
99 Output goes to STREAM (via stdio).
100 If SHOW is positive, we show the contents of the outermost level
101 of structure even if there is a type name that could be used instead.
102 If SHOW is negative, we never show the details of elements' types. */
103
104void
fba45db2
KB
105type_print (struct type *type, char *varstring, struct ui_file *stream,
106 int show)
c906108c
SS
107{
108 LA_PRINT_TYPE (type, varstring, stream, show, 0);
109}
110
111/* Print type of EXP, or last thing in value history if EXP == NULL.
112 show is passed to type_print. */
113
114static void
fba45db2 115whatis_exp (char *exp, int show)
c906108c
SS
116{
117 struct expression *expr;
3d6d86c6 118 struct value *val;
c906108c 119 register struct cleanup *old_chain = NULL;
c5aa993b 120 struct type *real_type = NULL;
070ad9f0 121 struct type *type;
c906108c
SS
122 int full = 0;
123 int top = -1;
124 int using_enc = 0;
125
126 if (exp)
127 {
128 expr = parse_expression (exp);
c13c43fd 129 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
130 val = evaluate_type (expr);
131 }
132 else
133 val = access_value_history (0);
134
070ad9f0
DB
135 type = VALUE_TYPE (val);
136
137 if (objectprint)
138 {
139 if (((TYPE_CODE (type) == TYPE_CODE_PTR) ||
140 (TYPE_CODE (type) == TYPE_CODE_REF))
141 &&
142 (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
143 {
144 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
145 if (real_type)
146 {
147 if (TYPE_CODE (type) == TYPE_CODE_PTR)
148 real_type = lookup_pointer_type (real_type);
149 else
150 real_type = lookup_reference_type (real_type);
151 }
152 }
153 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
c906108c 154 real_type = value_rtti_type (val, &full, &top, &using_enc);
070ad9f0 155 }
c906108c
SS
156
157 printf_filtered ("type = ");
158
070ad9f0
DB
159 if (real_type)
160 {
161 printf_filtered ("/* real type = ");
162 type_print (real_type, "", gdb_stdout, -1);
163 if (! full)
164 printf_filtered (" (incomplete object)");
165 printf_filtered (" */\n");
166 }
c906108c 167
070ad9f0 168 type_print (type, "", gdb_stdout, show);
c906108c
SS
169 printf_filtered ("\n");
170
171 if (exp)
172 do_cleanups (old_chain);
173}
174
175/* ARGSUSED */
176static void
fba45db2 177whatis_command (char *exp, int from_tty)
c906108c
SS
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/* Simple subroutine for ptype_command. */
186
187static struct type *
fba45db2 188ptype_eval (struct expression *exp)
c906108c
SS
189{
190 if (exp->elts[0].opcode == OP_TYPE)
191 {
192 return (exp->elts[1].type);
193 }
194 else
195 {
196 return (NULL);
197 }
198}
199
200/* TYPENAME is either the name of a type, or an expression. */
201
202/* ARGSUSED */
203static void
fba45db2 204ptype_command (char *typename, int from_tty)
c906108c
SS
205{
206 register struct type *type;
207 struct expression *expr;
208 register struct cleanup *old_chain;
209
210 if (typename == NULL)
211 {
212 /* Print type of last thing in value history. */
213 whatis_exp (typename, 1);
214 }
215 else
216 {
217 expr = parse_expression (typename);
c13c43fd 218 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
219 type = ptype_eval (expr);
220 if (type != NULL)
221 {
222 /* User did "ptype <typename>" */
223 printf_filtered ("type = ");
224 type_print (type, "", gdb_stdout, 1);
225 printf_filtered ("\n");
226 do_cleanups (old_chain);
227 }
228 else
229 {
230 /* User did "ptype <symbolname>" */
231 do_cleanups (old_chain);
232 whatis_exp (typename, 1);
233 }
234 }
235}
236
237/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
238 Used to print data from type structures in a specified type. For example,
239 array bounds may be characters or booleans in some languages, and this
240 allows the ranges to be printed in their "natural" form rather than as
241 decimal integer values.
242
243 FIXME: This is here simply because only the type printing routines
244 currently use it, and it wasn't clear if it really belonged somewhere
245 else (like printcmd.c). There are a lot of other gdb routines that do
246 something similar, but they are generally concerned with printing values
247 that come from the inferior in target byte order and target size. */
248
249void
fba45db2 250print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
c906108c
SS
251{
252 unsigned int i;
253 unsigned len;
254
255 CHECK_TYPEDEF (type);
256
257 switch (TYPE_CODE (type))
258 {
259
260 case TYPE_CODE_ENUM:
261 len = TYPE_NFIELDS (type);
262 for (i = 0; i < len; i++)
263 {
264 if (TYPE_FIELD_BITPOS (type, i) == val)
265 {
266 break;
267 }
268 }
269 if (i < len)
270 {
271 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
272 }
273 else
274 {
275 print_longest (stream, 'd', 0, val);
276 }
277 break;
278
279 case TYPE_CODE_INT:
280 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
281 break;
282
283 case TYPE_CODE_CHAR:
284 LA_PRINT_CHAR ((unsigned char) val, stream);
285 break;
286
287 case TYPE_CODE_BOOL:
288 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
289 break;
290
291 case TYPE_CODE_RANGE:
292 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
293 return;
294
295 case TYPE_CODE_UNDEF:
296 case TYPE_CODE_PTR:
297 case TYPE_CODE_ARRAY:
298 case TYPE_CODE_STRUCT:
299 case TYPE_CODE_UNION:
300 case TYPE_CODE_FUNC:
301 case TYPE_CODE_FLT:
302 case TYPE_CODE_VOID:
303 case TYPE_CODE_SET:
304 case TYPE_CODE_STRING:
305 case TYPE_CODE_ERROR:
306 case TYPE_CODE_MEMBER:
307 case TYPE_CODE_METHOD:
308 case TYPE_CODE_REF:
309 error ("internal error: unhandled type in print_type_scalar");
310 break;
311
312 default:
313 error ("Invalid type code in symbol table.");
314 }
315 gdb_flush (stream);
316}
317
c906108c
SS
318/* Dump details of a type specified either directly or indirectly.
319 Uses the same sort of type lookup mechanism as ptype_command()
320 and whatis_command(). */
321
322void
fba45db2 323maintenance_print_type (char *typename, int from_tty)
c906108c 324{
3d6d86c6 325 struct value *val;
c906108c
SS
326 register struct type *type;
327 register struct cleanup *old_chain;
328 struct expression *expr;
329
330 if (typename != NULL)
c5aa993b
JM
331 {
332 expr = parse_expression (typename);
c13c43fd 333 old_chain = make_cleanup (free_current_contents, &expr);
c5aa993b
JM
334 if (expr->elts[0].opcode == OP_TYPE)
335 {
336 /* The user expression names a type directly, just use that type. */
337 type = expr->elts[1].type;
338 }
339 else
340 {
341 /* The user expression may name a type indirectly by naming an
342 object of that type. Find that indirectly named type. */
343 val = evaluate_type (expr);
344 type = VALUE_TYPE (val);
345 }
346 if (type != NULL)
347 {
348 recursive_dump_type (type, 0);
349 }
350 do_cleanups (old_chain);
351 }
c906108c 352}
c906108c 353\f
c5aa993b 354
c906108c 355void
fba45db2 356_initialize_typeprint (void)
c906108c
SS
357{
358
359 add_com ("ptype", class_vars, ptype_command,
360 "Print definition of type TYPE.\n\
361Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
362or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
363The selected stack frame's lexical context is used to look up the name.");
364
365 add_com ("whatis", class_vars, whatis_command,
366 "Print data type of expression EXP.");
367
368}
This page took 0.302683 seconds and 4 git commands to generate.