]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/typeprint.c
Update FSF address.
[thirdparty/binutils-gdb.git] / gdb / typeprint.c
CommitLineData
a8a69e63 1/* Language independent support for printing types for GDB, the GNU debugger.
3d9b9577 2 Copyright 1986, 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
a8a69e63
FF
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
6c9638b4 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
a8a69e63
FF
19
20#include "defs.h"
21#include "obstack.h"
22#include "bfd.h" /* Binary File Description */
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "value.h"
27#include "gdbcore.h"
28#include "command.h"
29#include "gdbcmd.h"
30#include "target.h"
31#include "language.h"
32#include "demangle.h"
33
2b576293 34#include "gdb_string.h"
a8a69e63
FF
35#include <errno.h>
36
37static void
38ptype_command PARAMS ((char *, int));
39
40static struct type *
41ptype_eval PARAMS ((struct expression *));
42
43static void
44whatis_command PARAMS ((char *, int));
45
46static void
47whatis_exp PARAMS ((char *, int));
48
49/* Print a description of a type TYPE in the form of a declaration of a
50 variable named VARSTRING. (VARSTRING is demangled if necessary.)
51 Output goes to STREAM (via stdio).
52 If SHOW is positive, we show the contents of the outermost level
53 of structure even if there is a type name that could be used instead.
54 If SHOW is negative, we never show the details of elements' types. */
55
56void
57type_print (type, varstring, stream, show)
58 struct type *type;
59 char *varstring;
199b2450 60 GDB_FILE *stream;
a8a69e63
FF
61 int show;
62{
63 LA_PRINT_TYPE (type, varstring, stream, show, 0);
64}
65
66/* Print type of EXP, or last thing in value history if EXP == NULL.
67 show is passed to type_print. */
68
69static void
70whatis_exp (exp, show)
71 char *exp;
72 int show;
73{
74 struct expression *expr;
82a2edfb 75 register value_ptr val;
46c28185 76 register struct cleanup *old_chain = NULL;
a8a69e63
FF
77
78 if (exp)
79 {
80 expr = parse_expression (exp);
81 old_chain = make_cleanup (free_current_contents, &expr);
82 val = evaluate_type (expr);
83 }
84 else
85 val = access_value_history (0);
86
87 printf_filtered ("type = ");
199b2450 88 type_print (VALUE_TYPE (val), "", gdb_stdout, show);
a8a69e63
FF
89 printf_filtered ("\n");
90
91 if (exp)
92 do_cleanups (old_chain);
93}
94
95/* ARGSUSED */
96static void
97whatis_command (exp, from_tty)
98 char *exp;
99 int from_tty;
100{
101 /* Most of the time users do not want to see all the fields
102 in a structure. If they do they can use the "ptype" command.
103 Hence the "-1" below. */
104 whatis_exp (exp, -1);
105}
106
107/* Simple subroutine for ptype_command. */
108
109static struct type *
110ptype_eval (exp)
111 struct expression *exp;
112{
113 if (exp->elts[0].opcode == OP_TYPE)
114 {
115 return (exp->elts[1].type);
116 }
117 else
118 {
119 return (NULL);
120 }
121}
122
123/* TYPENAME is either the name of a type, or an expression. */
124
125/* ARGSUSED */
126static void
127ptype_command (typename, from_tty)
128 char *typename;
129 int from_tty;
130{
131 register struct type *type;
132 struct expression *expr;
133 register struct cleanup *old_chain;
134
135 if (typename == NULL)
136 {
137 /* Print type of last thing in value history. */
138 whatis_exp (typename, 1);
139 }
140 else
141 {
142 expr = parse_expression (typename);
143 old_chain = make_cleanup (free_current_contents, &expr);
144 type = ptype_eval (expr);
145 if (type != NULL)
146 {
147 /* User did "ptype <typename>" */
148 printf_filtered ("type = ");
199b2450 149 type_print (type, "", gdb_stdout, 1);
a8a69e63
FF
150 printf_filtered ("\n");
151 do_cleanups (old_chain);
152 }
153 else
154 {
155 /* User did "ptype <symbolname>" */
156 do_cleanups (old_chain);
157 whatis_exp (typename, 1);
158 }
159 }
160}
161
162/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
163 Used to print data from type structures in a specified type. For example,
164 array bounds may be characters or booleans in some languages, and this
165 allows the ranges to be printed in their "natural" form rather than as
166 decimal integer values.
167
168 FIXME: This is here simply because only the type printing routines
169 currently use it, and it wasn't clear if it really belonged somewhere
170 else (like printcmd.c). There are a lot of other gdb routines that do
171 something similar, but they are generally concerned with printing values
172 that come from the inferior in target byte order and target size. */
173
174void
175print_type_scalar (type, val, stream)
176 struct type *type;
177 LONGEST val;
199b2450 178 GDB_FILE *stream;
a8a69e63
FF
179{
180 unsigned int i;
181 unsigned len;
182
183 switch (TYPE_CODE (type))
184 {
185
186 case TYPE_CODE_ENUM:
187 len = TYPE_NFIELDS (type);
188 for (i = 0; i < len; i++)
189 {
190 if (TYPE_FIELD_BITPOS (type, i) == val)
191 {
192 break;
193 }
194 }
195 if (i < len)
196 {
197 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
198 }
199 else
200 {
7efb57c3 201 print_longest (stream, 'd', 0, val);
a8a69e63
FF
202 }
203 break;
204
205 case TYPE_CODE_INT:
7efb57c3 206 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
a8a69e63
FF
207 break;
208
209 case TYPE_CODE_CHAR:
210 LA_PRINT_CHAR ((unsigned char) val, stream);
211 break;
212
213 case TYPE_CODE_BOOL:
214 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
215 break;
216
217 case TYPE_CODE_UNDEF:
218 case TYPE_CODE_PTR:
219 case TYPE_CODE_ARRAY:
220 case TYPE_CODE_STRUCT:
221 case TYPE_CODE_UNION:
222 case TYPE_CODE_FUNC:
223 case TYPE_CODE_FLT:
224 case TYPE_CODE_VOID:
225 case TYPE_CODE_SET:
226 case TYPE_CODE_RANGE:
c4413e2c 227 case TYPE_CODE_STRING:
a8a69e63
FF
228 case TYPE_CODE_ERROR:
229 case TYPE_CODE_MEMBER:
230 case TYPE_CODE_METHOD:
231 case TYPE_CODE_REF:
232 error ("internal error: unhandled type in print_type_scalar");
233 break;
234
235 default:
236 error ("Invalid type code in symbol table.");
237 }
199b2450 238 gdb_flush (stream);
a8a69e63
FF
239}
240
241#if MAINTENANCE_CMDS
242
243/* Dump details of a type specified either directly or indirectly.
244 Uses the same sort of type lookup mechanism as ptype_command()
245 and whatis_command(). */
246
247void
248maintenance_print_type (typename, from_tty)
249 char *typename;
250 int from_tty;
251{
82a2edfb 252 register value_ptr val;
a8a69e63
FF
253 register struct type *type;
254 register struct cleanup *old_chain;
255 struct expression *expr;
256
257 if (typename != NULL)
258 {
259 expr = parse_expression (typename);
260 old_chain = make_cleanup (free_current_contents, &expr);
261 if (expr -> elts[0].opcode == OP_TYPE)
262 {
263 /* The user expression names a type directly, just use that type. */
264 type = expr -> elts[1].type;
265 }
266 else
267 {
268 /* The user expression may name a type indirectly by naming an
269 object of that type. Find that indirectly named type. */
270 val = evaluate_type (expr);
271 type = VALUE_TYPE (val);
272 }
273 if (type != NULL)
274 {
275 recursive_dump_type (type, 0);
276 }
277 do_cleanups (old_chain);
278 }
279}
280
281#endif /* MAINTENANCE_CMDS */
282
283\f
284void
285_initialize_typeprint ()
286{
287
288 add_com ("ptype", class_vars, ptype_command,
289 "Print definition of type TYPE.\n\
915e6cd9
JK
290Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
291or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
a8a69e63
FF
292The selected stack frame's lexical context is used to look up the name.");
293
294 add_com ("whatis", class_vars, whatis_command,
295 "Print data type of expression EXP.");
296
297}