]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/typeprint.c
Move 2006 ChangeLog entries to ChangeLog-2006.
[thirdparty/binutils-gdb.git] / gdb / typeprint.c
CommitLineData
c906108c 1/* Language independent support for printing types for GDB, the GNU debugger.
1bac305b 2
197e01b6 3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998,
0d5de010 4 1999, 2000, 2001, 2003, 2006 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
04ea0df1 24#include "gdb_obstack.h"
c906108c
SS
25#include "bfd.h" /* Binary File Description */
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "expression.h"
29#include "value.h"
30#include "gdbcore.h"
31#include "command.h"
32#include "gdbcmd.h"
33#include "target.h"
34#include "language.h"
015a42b4 35#include "cp-abi.h"
b9362cc7 36#include "typeprint.h"
c906108c
SS
37#include "gdb_string.h"
38#include <errno.h>
39
40/* For real-type printing in whatis_exp() */
41extern int objectprint; /* Controls looking up an object's derived type
42 using what we find in its vtables. */
43
a14ed312 44extern void _initialize_typeprint (void);
392a587b 45
a14ed312 46static void ptype_command (char *, int);
c906108c 47
a14ed312 48static void whatis_command (char *, int);
c906108c 49
a14ed312 50static void whatis_exp (char *, int);
c906108c 51
a5238fbc
PM
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
56void
57typedef_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
22abf04a 68 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0)
de5ad195 69 fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new));
a5238fbc
PM
70 break;
71#endif
72#ifdef _LANG_m2
73 case language_m2:
74 fprintf_filtered (stream, "TYPE ");
5cb316ef 75 if (!TYPE_NAME (SYMBOL_TYPE (new))
22abf04a 76 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0)
de5ad195 77 fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new));
a5238fbc
PM
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 ");
de5ad195 86 fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new));
a5238fbc
PM
87 type_print (type, "", stream, 0);
88 break;
a5238fbc
PM
89#endif
90 default:
8a3fe4f8 91 error (_("Language not supported."));
a5238fbc
PM
92 }
93 fprintf_filtered (stream, ";\n");
94}
95
c906108c
SS
96/* Print a description of a type TYPE in the form of a declaration of a
97 variable named VARSTRING. (VARSTRING is demangled if necessary.)
98 Output goes to STREAM (via stdio).
99 If SHOW is positive, we show the contents of the outermost level
100 of structure even if there is a type name that could be used instead.
101 If SHOW is negative, we never show the details of elements' types. */
102
103void
fba45db2
KB
104type_print (struct type *type, char *varstring, struct ui_file *stream,
105 int show)
c906108c
SS
106{
107 LA_PRINT_TYPE (type, varstring, stream, show, 0);
108}
109
110/* Print type of EXP, or last thing in value history if EXP == NULL.
111 show is passed to type_print. */
112
113static void
fba45db2 114whatis_exp (char *exp, int show)
c906108c
SS
115{
116 struct expression *expr;
3d6d86c6 117 struct value *val;
52f0bd74 118 struct cleanup *old_chain = NULL;
c5aa993b 119 struct type *real_type = NULL;
070ad9f0 120 struct type *type;
c906108c
SS
121 int full = 0;
122 int top = -1;
123 int using_enc = 0;
124
125 if (exp)
126 {
127 expr = parse_expression (exp);
c13c43fd 128 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
129 val = evaluate_type (expr);
130 }
131 else
132 val = access_value_history (0);
133
df407dfe 134 type = value_type (val);
070ad9f0
DB
135
136 if (objectprint)
137 {
138 if (((TYPE_CODE (type) == TYPE_CODE_PTR) ||
139 (TYPE_CODE (type) == TYPE_CODE_REF))
140 &&
141 (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
142 {
143 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
144 if (real_type)
145 {
146 if (TYPE_CODE (type) == TYPE_CODE_PTR)
147 real_type = lookup_pointer_type (real_type);
148 else
149 real_type = lookup_reference_type (real_type);
150 }
151 }
152 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
c906108c 153 real_type = value_rtti_type (val, &full, &top, &using_enc);
070ad9f0 154 }
c906108c
SS
155
156 printf_filtered ("type = ");
157
070ad9f0
DB
158 if (real_type)
159 {
160 printf_filtered ("/* real type = ");
161 type_print (real_type, "", gdb_stdout, -1);
162 if (! full)
163 printf_filtered (" (incomplete object)");
164 printf_filtered (" */\n");
165 }
c906108c 166
070ad9f0 167 type_print (type, "", gdb_stdout, show);
c906108c
SS
168 printf_filtered ("\n");
169
170 if (exp)
171 do_cleanups (old_chain);
172}
173
c906108c 174static void
fba45db2 175whatis_command (char *exp, int from_tty)
c906108c
SS
176{
177 /* Most of the time users do not want to see all the fields
178 in a structure. If they do they can use the "ptype" command.
179 Hence the "-1" below. */
180 whatis_exp (exp, -1);
181}
182
c906108c
SS
183/* TYPENAME is either the name of a type, or an expression. */
184
c906108c 185static void
fba45db2 186ptype_command (char *typename, int from_tty)
c906108c 187{
d843c49c 188 whatis_exp (typename, 1);
c906108c
SS
189}
190
191/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
192 Used to print data from type structures in a specified type. For example,
193 array bounds may be characters or booleans in some languages, and this
194 allows the ranges to be printed in their "natural" form rather than as
195 decimal integer values.
196
197 FIXME: This is here simply because only the type printing routines
198 currently use it, and it wasn't clear if it really belonged somewhere
199 else (like printcmd.c). There are a lot of other gdb routines that do
200 something similar, but they are generally concerned with printing values
201 that come from the inferior in target byte order and target size. */
202
203void
fba45db2 204print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
c906108c
SS
205{
206 unsigned int i;
207 unsigned len;
208
209 CHECK_TYPEDEF (type);
210
211 switch (TYPE_CODE (type))
212 {
213
214 case TYPE_CODE_ENUM:
215 len = TYPE_NFIELDS (type);
216 for (i = 0; i < len; i++)
217 {
218 if (TYPE_FIELD_BITPOS (type, i) == val)
219 {
220 break;
221 }
222 }
223 if (i < len)
224 {
225 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
226 }
227 else
228 {
229 print_longest (stream, 'd', 0, val);
230 }
231 break;
232
233 case TYPE_CODE_INT:
234 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
235 break;
236
237 case TYPE_CODE_CHAR:
238 LA_PRINT_CHAR ((unsigned char) val, stream);
239 break;
240
241 case TYPE_CODE_BOOL:
242 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
243 break;
244
245 case TYPE_CODE_RANGE:
246 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
247 return;
248
249 case TYPE_CODE_UNDEF:
250 case TYPE_CODE_PTR:
251 case TYPE_CODE_ARRAY:
252 case TYPE_CODE_STRUCT:
253 case TYPE_CODE_UNION:
254 case TYPE_CODE_FUNC:
255 case TYPE_CODE_FLT:
256 case TYPE_CODE_VOID:
257 case TYPE_CODE_SET:
258 case TYPE_CODE_STRING:
259 case TYPE_CODE_ERROR:
0d5de010
DJ
260 case TYPE_CODE_MEMBERPTR:
261 case TYPE_CODE_METHODPTR:
c906108c
SS
262 case TYPE_CODE_METHOD:
263 case TYPE_CODE_REF:
5c4e30ca 264 case TYPE_CODE_NAMESPACE:
8a3fe4f8 265 error (_("internal error: unhandled type in print_type_scalar"));
c906108c
SS
266 break;
267
268 default:
8a3fe4f8 269 error (_("Invalid type code in symbol table."));
c906108c
SS
270 }
271 gdb_flush (stream);
272}
273
c906108c
SS
274/* Dump details of a type specified either directly or indirectly.
275 Uses the same sort of type lookup mechanism as ptype_command()
276 and whatis_command(). */
277
278void
fba45db2 279maintenance_print_type (char *typename, int from_tty)
c906108c 280{
3d6d86c6 281 struct value *val;
52f0bd74
AC
282 struct type *type;
283 struct cleanup *old_chain;
c906108c
SS
284 struct expression *expr;
285
286 if (typename != NULL)
c5aa993b
JM
287 {
288 expr = parse_expression (typename);
c13c43fd 289 old_chain = make_cleanup (free_current_contents, &expr);
c5aa993b
JM
290 if (expr->elts[0].opcode == OP_TYPE)
291 {
292 /* The user expression names a type directly, just use that type. */
293 type = expr->elts[1].type;
294 }
295 else
296 {
297 /* The user expression may name a type indirectly by naming an
298 object of that type. Find that indirectly named type. */
299 val = evaluate_type (expr);
df407dfe 300 type = value_type (val);
c5aa993b
JM
301 }
302 if (type != NULL)
303 {
304 recursive_dump_type (type, 0);
305 }
306 do_cleanups (old_chain);
307 }
c906108c 308}
c906108c 309\f
c5aa993b 310
c906108c 311void
fba45db2 312_initialize_typeprint (void)
c906108c
SS
313{
314
1bedd215
AC
315 add_com ("ptype", class_vars, ptype_command, _("\
316Print definition of type TYPE.\n\
c906108c
SS
317Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
318or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
1bedd215 319The selected stack frame's lexical context is used to look up the name."));
c906108c
SS
320
321 add_com ("whatis", class_vars, whatis_command,
1bedd215 322 _("Print data type of expression EXP."));
c906108c
SS
323
324}