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