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