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