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