* cli/cli-script.c (do_fclose_cleanup): Remove.
[deliverable/binutils-gdb.git] / gdb / typeprint.c
CommitLineData
c906108c 1/* Language independent support for printing types for GDB, the GNU debugger.
1bac305b 2
6aba47ca 3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
9b254dd1 4 2000, 2001, 2003, 2006, 2007, 2008 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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
04ea0df1 22#include "gdb_obstack.h"
c906108c
SS
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"
015a42b4 33#include "cp-abi.h"
b9362cc7 34#include "typeprint.h"
c906108c
SS
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
a14ed312 42extern void _initialize_typeprint (void);
392a587b 43
a14ed312 44static void ptype_command (char *, int);
c906108c 45
a14ed312 46static void whatis_command (char *, int);
c906108c 47
a14ed312 48static void whatis_exp (char *, int);
c906108c 49
5c6ce71d 50
a5238fbc
PM
51/* Print a description of a type in the format of a
52 typedef for the current language.
53 NEW is the new name for a type TYPE. */
54
55void
56typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
57{
5c6ce71d
TT
58 LA_PRINT_TYPEDEF (type, new, stream);
59}
60
61/* The default way to print a typedef. */
62
63void
64default_print_typedef (struct type *type, struct symbol *new_symbol,
65 struct ui_file *stream)
66{
67 error (_("Language not supported."));
a5238fbc
PM
68}
69
c906108c
SS
70/* Print a description of a type TYPE in the form of a declaration of a
71 variable named VARSTRING. (VARSTRING is demangled if necessary.)
72 Output goes to STREAM (via stdio).
73 If SHOW is positive, we show the contents of the outermost level
74 of structure even if there is a type name that could be used instead.
75 If SHOW is negative, we never show the details of elements' types. */
76
77void
fba45db2
KB
78type_print (struct type *type, char *varstring, struct ui_file *stream,
79 int show)
c906108c
SS
80{
81 LA_PRINT_TYPE (type, varstring, stream, show, 0);
82}
83
84/* Print type of EXP, or last thing in value history if EXP == NULL.
85 show is passed to type_print. */
86
87static void
fba45db2 88whatis_exp (char *exp, int show)
c906108c
SS
89{
90 struct expression *expr;
3d6d86c6 91 struct value *val;
52f0bd74 92 struct cleanup *old_chain = NULL;
c5aa993b 93 struct type *real_type = NULL;
070ad9f0 94 struct type *type;
c906108c
SS
95 int full = 0;
96 int top = -1;
97 int using_enc = 0;
98
99 if (exp)
100 {
101 expr = parse_expression (exp);
c13c43fd 102 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
103 val = evaluate_type (expr);
104 }
105 else
106 val = access_value_history (0);
107
df407dfe 108 type = value_type (val);
070ad9f0
DB
109
110 if (objectprint)
111 {
41808ebe
DE
112 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
113 || (TYPE_CODE (type) == TYPE_CODE_REF))
114 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
070ad9f0
DB
115 {
116 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
117 if (real_type)
118 {
119 if (TYPE_CODE (type) == TYPE_CODE_PTR)
120 real_type = lookup_pointer_type (real_type);
121 else
122 real_type = lookup_reference_type (real_type);
123 }
124 }
125 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
41808ebe 126 real_type = value_rtti_type (val, &full, &top, &using_enc);
070ad9f0 127 }
c906108c
SS
128
129 printf_filtered ("type = ");
130
070ad9f0
DB
131 if (real_type)
132 {
133 printf_filtered ("/* real type = ");
134 type_print (real_type, "", gdb_stdout, -1);
135 if (! full)
136 printf_filtered (" (incomplete object)");
137 printf_filtered (" */\n");
138 }
c906108c 139
070ad9f0 140 type_print (type, "", gdb_stdout, show);
c906108c
SS
141 printf_filtered ("\n");
142
143 if (exp)
144 do_cleanups (old_chain);
145}
146
c906108c 147static void
fba45db2 148whatis_command (char *exp, int from_tty)
c906108c
SS
149{
150 /* Most of the time users do not want to see all the fields
151 in a structure. If they do they can use the "ptype" command.
152 Hence the "-1" below. */
153 whatis_exp (exp, -1);
154}
155
c906108c
SS
156/* TYPENAME is either the name of a type, or an expression. */
157
c906108c 158static void
fba45db2 159ptype_command (char *typename, int from_tty)
c906108c 160{
d843c49c 161 whatis_exp (typename, 1);
c906108c
SS
162}
163
164/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
165 Used to print data from type structures in a specified type. For example,
166 array bounds may be characters or booleans in some languages, and this
167 allows the ranges to be printed in their "natural" form rather than as
168 decimal integer values.
169
170 FIXME: This is here simply because only the type printing routines
171 currently use it, and it wasn't clear if it really belonged somewhere
172 else (like printcmd.c). There are a lot of other gdb routines that do
173 something similar, but they are generally concerned with printing values
41808ebe 174 that come from the inferior in target byte order and target size. */
c906108c
SS
175
176void
fba45db2 177print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
c906108c
SS
178{
179 unsigned int i;
180 unsigned len;
181
182 CHECK_TYPEDEF (type);
183
184 switch (TYPE_CODE (type))
185 {
186
187 case TYPE_CODE_ENUM:
188 len = TYPE_NFIELDS (type);
189 for (i = 0; i < len; i++)
190 {
191 if (TYPE_FIELD_BITPOS (type, i) == val)
192 {
193 break;
194 }
195 }
196 if (i < len)
197 {
198 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
199 }
200 else
201 {
202 print_longest (stream, 'd', 0, val);
203 }
204 break;
205
206 case TYPE_CODE_INT:
207 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
208 break;
209
210 case TYPE_CODE_CHAR:
211 LA_PRINT_CHAR ((unsigned char) val, stream);
212 break;
213
214 case TYPE_CODE_BOOL:
215 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
216 break;
217
218 case TYPE_CODE_RANGE:
219 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
220 return;
221
222 case TYPE_CODE_UNDEF:
223 case TYPE_CODE_PTR:
224 case TYPE_CODE_ARRAY:
225 case TYPE_CODE_STRUCT:
226 case TYPE_CODE_UNION:
227 case TYPE_CODE_FUNC:
228 case TYPE_CODE_FLT:
229 case TYPE_CODE_VOID:
230 case TYPE_CODE_SET:
231 case TYPE_CODE_STRING:
232 case TYPE_CODE_ERROR:
0d5de010
DJ
233 case TYPE_CODE_MEMBERPTR:
234 case TYPE_CODE_METHODPTR:
c906108c
SS
235 case TYPE_CODE_METHOD:
236 case TYPE_CODE_REF:
5c4e30ca 237 case TYPE_CODE_NAMESPACE:
8a3fe4f8 238 error (_("internal error: unhandled type in print_type_scalar"));
c906108c
SS
239 break;
240
241 default:
8a3fe4f8 242 error (_("Invalid type code in symbol table."));
c906108c
SS
243 }
244 gdb_flush (stream);
245}
246
c906108c
SS
247/* Dump details of a type specified either directly or indirectly.
248 Uses the same sort of type lookup mechanism as ptype_command()
41808ebe 249 and whatis_command(). */
c906108c
SS
250
251void
fba45db2 252maintenance_print_type (char *typename, int from_tty)
c906108c 253{
3d6d86c6 254 struct value *val;
52f0bd74
AC
255 struct type *type;
256 struct cleanup *old_chain;
c906108c
SS
257 struct expression *expr;
258
259 if (typename != NULL)
c5aa993b
JM
260 {
261 expr = parse_expression (typename);
c13c43fd 262 old_chain = make_cleanup (free_current_contents, &expr);
c5aa993b
JM
263 if (expr->elts[0].opcode == OP_TYPE)
264 {
265 /* The user expression names a type directly, just use that type. */
266 type = expr->elts[1].type;
267 }
268 else
269 {
270 /* The user expression may name a type indirectly by naming an
271 object of that type. Find that indirectly named type. */
272 val = evaluate_type (expr);
df407dfe 273 type = value_type (val);
c5aa993b
JM
274 }
275 if (type != NULL)
276 {
277 recursive_dump_type (type, 0);
278 }
279 do_cleanups (old_chain);
280 }
c906108c 281}
c906108c 282\f
c5aa993b 283
c906108c 284void
fba45db2 285_initialize_typeprint (void)
c906108c 286{
1bedd215
AC
287 add_com ("ptype", class_vars, ptype_command, _("\
288Print definition of type TYPE.\n\
c906108c
SS
289Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
290or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
1bedd215 291The selected stack frame's lexical context is used to look up the name."));
c906108c
SS
292
293 add_com ("whatis", class_vars, whatis_command,
1bedd215 294 _("Print data type of expression EXP."));
c906108c 295}
This page took 1.511878 seconds and 4 git commands to generate.