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