]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
1bac305b AC |
2 | |
3 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, | |
4 | 1999, 2000, 2001, 2003 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
04ea0df1 | 24 | #include "gdb_obstack.h" |
c906108c SS |
25 | #include "bfd.h" /* Binary File Description */ |
26 | #include "symtab.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "expression.h" | |
29 | #include "value.h" | |
30 | #include "gdbcore.h" | |
31 | #include "command.h" | |
32 | #include "gdbcmd.h" | |
33 | #include "target.h" | |
34 | #include "language.h" | |
015a42b4 | 35 | #include "cp-abi.h" |
c906108c SS |
36 | |
37 | #include "gdb_string.h" | |
38 | #include <errno.h> | |
39 | ||
40 | /* For real-type printing in whatis_exp() */ | |
41 | extern int objectprint; /* Controls looking up an object's derived type | |
42 | using what we find in its vtables. */ | |
43 | ||
a14ed312 | 44 | extern void _initialize_typeprint (void); |
392a587b | 45 | |
a14ed312 | 46 | static void ptype_command (char *, int); |
c906108c | 47 | |
a14ed312 | 48 | static struct type *ptype_eval (struct expression *); |
c906108c | 49 | |
a14ed312 | 50 | static void whatis_command (char *, int); |
c906108c | 51 | |
a14ed312 | 52 | static void whatis_exp (char *, int); |
c906108c | 53 | |
a5238fbc PM |
54 | /* Print a description of a type in the format of a |
55 | typedef for the current language. | |
56 | NEW is the new name for a type TYPE. */ | |
57 | ||
58 | void | |
59 | typedef_print (struct type *type, struct symbol *new, struct ui_file *stream) | |
60 | { | |
61 | CHECK_TYPEDEF (type); | |
62 | switch (current_language->la_language) | |
63 | { | |
64 | #ifdef _LANG_c | |
65 | case language_c: | |
66 | case language_cplus: | |
67 | fprintf_filtered (stream, "typedef "); | |
68 | type_print (type, "", stream, 0); | |
69 | if (TYPE_NAME ((SYMBOL_TYPE (new))) == 0 | |
22abf04a | 70 | || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0) |
de5ad195 | 71 | fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new)); |
a5238fbc PM |
72 | break; |
73 | #endif | |
74 | #ifdef _LANG_m2 | |
75 | case language_m2: | |
76 | fprintf_filtered (stream, "TYPE "); | |
5cb316ef | 77 | if (!TYPE_NAME (SYMBOL_TYPE (new)) |
22abf04a | 78 | || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0) |
de5ad195 | 79 | fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new)); |
a5238fbc PM |
80 | else |
81 | fprintf_filtered (stream, "<builtin> = "); | |
82 | type_print (type, "", stream, 0); | |
83 | break; | |
84 | #endif | |
85 | #ifdef _LANG_pascal | |
86 | case language_pascal: | |
87 | fprintf_filtered (stream, "type "); | |
de5ad195 | 88 | fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new)); |
a5238fbc PM |
89 | type_print (type, "", stream, 0); |
90 | break; | |
a5238fbc PM |
91 | #endif |
92 | default: | |
93 | error ("Language not supported."); | |
94 | } | |
95 | fprintf_filtered (stream, ";\n"); | |
96 | } | |
97 | ||
c906108c SS |
98 | /* Print a description of a type TYPE in the form of a declaration of a |
99 | variable named VARSTRING. (VARSTRING is demangled if necessary.) | |
100 | Output goes to STREAM (via stdio). | |
101 | If SHOW is positive, we show the contents of the outermost level | |
102 | of structure even if there is a type name that could be used instead. | |
103 | If SHOW is negative, we never show the details of elements' types. */ | |
104 | ||
105 | void | |
fba45db2 KB |
106 | type_print (struct type *type, char *varstring, struct ui_file *stream, |
107 | int show) | |
c906108c SS |
108 | { |
109 | LA_PRINT_TYPE (type, varstring, stream, show, 0); | |
110 | } | |
111 | ||
112 | /* Print type of EXP, or last thing in value history if EXP == NULL. | |
113 | show is passed to type_print. */ | |
114 | ||
115 | static void | |
fba45db2 | 116 | whatis_exp (char *exp, int show) |
c906108c SS |
117 | { |
118 | struct expression *expr; | |
3d6d86c6 | 119 | struct value *val; |
c906108c | 120 | register struct cleanup *old_chain = NULL; |
c5aa993b | 121 | struct type *real_type = NULL; |
070ad9f0 | 122 | struct type *type; |
c906108c SS |
123 | int full = 0; |
124 | int top = -1; | |
125 | int using_enc = 0; | |
126 | ||
127 | if (exp) | |
128 | { | |
129 | expr = parse_expression (exp); | |
c13c43fd | 130 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
131 | val = evaluate_type (expr); |
132 | } | |
133 | else | |
134 | val = access_value_history (0); | |
135 | ||
070ad9f0 DB |
136 | type = VALUE_TYPE (val); |
137 | ||
138 | if (objectprint) | |
139 | { | |
140 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) || | |
141 | (TYPE_CODE (type) == TYPE_CODE_REF)) | |
142 | && | |
143 | (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
144 | { | |
145 | real_type = value_rtti_target_type (val, &full, &top, &using_enc); | |
146 | if (real_type) | |
147 | { | |
148 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
149 | real_type = lookup_pointer_type (real_type); | |
150 | else | |
151 | real_type = lookup_reference_type (real_type); | |
152 | } | |
153 | } | |
154 | else if (TYPE_CODE (type) == TYPE_CODE_CLASS) | |
c906108c | 155 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
070ad9f0 | 156 | } |
c906108c SS |
157 | |
158 | printf_filtered ("type = "); | |
159 | ||
070ad9f0 DB |
160 | if (real_type) |
161 | { | |
162 | printf_filtered ("/* real type = "); | |
163 | type_print (real_type, "", gdb_stdout, -1); | |
164 | if (! full) | |
165 | printf_filtered (" (incomplete object)"); | |
166 | printf_filtered (" */\n"); | |
167 | } | |
c906108c | 168 | |
070ad9f0 | 169 | type_print (type, "", gdb_stdout, show); |
c906108c SS |
170 | printf_filtered ("\n"); |
171 | ||
172 | if (exp) | |
173 | do_cleanups (old_chain); | |
174 | } | |
175 | ||
176 | /* ARGSUSED */ | |
177 | static void | |
fba45db2 | 178 | whatis_command (char *exp, int from_tty) |
c906108c SS |
179 | { |
180 | /* Most of the time users do not want to see all the fields | |
181 | in a structure. If they do they can use the "ptype" command. | |
182 | Hence the "-1" below. */ | |
183 | whatis_exp (exp, -1); | |
184 | } | |
185 | ||
186 | /* Simple subroutine for ptype_command. */ | |
187 | ||
188 | static struct type * | |
fba45db2 | 189 | ptype_eval (struct expression *exp) |
c906108c SS |
190 | { |
191 | if (exp->elts[0].opcode == OP_TYPE) | |
192 | { | |
193 | return (exp->elts[1].type); | |
194 | } | |
195 | else | |
196 | { | |
197 | return (NULL); | |
198 | } | |
199 | } | |
200 | ||
201 | /* TYPENAME is either the name of a type, or an expression. */ | |
202 | ||
203 | /* ARGSUSED */ | |
204 | static void | |
fba45db2 | 205 | ptype_command (char *typename, int from_tty) |
c906108c SS |
206 | { |
207 | register struct type *type; | |
208 | struct expression *expr; | |
209 | register struct cleanup *old_chain; | |
210 | ||
211 | if (typename == NULL) | |
212 | { | |
213 | /* Print type of last thing in value history. */ | |
214 | whatis_exp (typename, 1); | |
215 | } | |
216 | else | |
217 | { | |
218 | expr = parse_expression (typename); | |
c13c43fd | 219 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
220 | type = ptype_eval (expr); |
221 | if (type != NULL) | |
222 | { | |
223 | /* User did "ptype <typename>" */ | |
224 | printf_filtered ("type = "); | |
225 | type_print (type, "", gdb_stdout, 1); | |
226 | printf_filtered ("\n"); | |
227 | do_cleanups (old_chain); | |
228 | } | |
229 | else | |
230 | { | |
231 | /* User did "ptype <symbolname>" */ | |
232 | do_cleanups (old_chain); | |
233 | whatis_exp (typename, 1); | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. | |
239 | Used to print data from type structures in a specified type. For example, | |
240 | array bounds may be characters or booleans in some languages, and this | |
241 | allows the ranges to be printed in their "natural" form rather than as | |
242 | decimal integer values. | |
243 | ||
244 | FIXME: This is here simply because only the type printing routines | |
245 | currently use it, and it wasn't clear if it really belonged somewhere | |
246 | else (like printcmd.c). There are a lot of other gdb routines that do | |
247 | something similar, but they are generally concerned with printing values | |
248 | that come from the inferior in target byte order and target size. */ | |
249 | ||
250 | void | |
fba45db2 | 251 | print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) |
c906108c SS |
252 | { |
253 | unsigned int i; | |
254 | unsigned len; | |
255 | ||
256 | CHECK_TYPEDEF (type); | |
257 | ||
258 | switch (TYPE_CODE (type)) | |
259 | { | |
260 | ||
261 | case TYPE_CODE_ENUM: | |
262 | len = TYPE_NFIELDS (type); | |
263 | for (i = 0; i < len; i++) | |
264 | { | |
265 | if (TYPE_FIELD_BITPOS (type, i) == val) | |
266 | { | |
267 | break; | |
268 | } | |
269 | } | |
270 | if (i < len) | |
271 | { | |
272 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
273 | } | |
274 | else | |
275 | { | |
276 | print_longest (stream, 'd', 0, val); | |
277 | } | |
278 | break; | |
279 | ||
280 | case TYPE_CODE_INT: | |
281 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); | |
282 | break; | |
283 | ||
284 | case TYPE_CODE_CHAR: | |
285 | LA_PRINT_CHAR ((unsigned char) val, stream); | |
286 | break; | |
287 | ||
288 | case TYPE_CODE_BOOL: | |
289 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); | |
290 | break; | |
291 | ||
292 | case TYPE_CODE_RANGE: | |
293 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); | |
294 | return; | |
295 | ||
296 | case TYPE_CODE_UNDEF: | |
297 | case TYPE_CODE_PTR: | |
298 | case TYPE_CODE_ARRAY: | |
299 | case TYPE_CODE_STRUCT: | |
300 | case TYPE_CODE_UNION: | |
301 | case TYPE_CODE_FUNC: | |
302 | case TYPE_CODE_FLT: | |
303 | case TYPE_CODE_VOID: | |
304 | case TYPE_CODE_SET: | |
305 | case TYPE_CODE_STRING: | |
306 | case TYPE_CODE_ERROR: | |
307 | case TYPE_CODE_MEMBER: | |
308 | case TYPE_CODE_METHOD: | |
309 | case TYPE_CODE_REF: | |
310 | error ("internal error: unhandled type in print_type_scalar"); | |
311 | break; | |
312 | ||
313 | default: | |
314 | error ("Invalid type code in symbol table."); | |
315 | } | |
316 | gdb_flush (stream); | |
317 | } | |
318 | ||
c906108c SS |
319 | /* Dump details of a type specified either directly or indirectly. |
320 | Uses the same sort of type lookup mechanism as ptype_command() | |
321 | and whatis_command(). */ | |
322 | ||
323 | void | |
fba45db2 | 324 | maintenance_print_type (char *typename, int from_tty) |
c906108c | 325 | { |
3d6d86c6 | 326 | struct value *val; |
c906108c SS |
327 | register struct type *type; |
328 | register struct cleanup *old_chain; | |
329 | struct expression *expr; | |
330 | ||
331 | if (typename != NULL) | |
c5aa993b JM |
332 | { |
333 | expr = parse_expression (typename); | |
c13c43fd | 334 | old_chain = make_cleanup (free_current_contents, &expr); |
c5aa993b JM |
335 | if (expr->elts[0].opcode == OP_TYPE) |
336 | { | |
337 | /* The user expression names a type directly, just use that type. */ | |
338 | type = expr->elts[1].type; | |
339 | } | |
340 | else | |
341 | { | |
342 | /* The user expression may name a type indirectly by naming an | |
343 | object of that type. Find that indirectly named type. */ | |
344 | val = evaluate_type (expr); | |
345 | type = VALUE_TYPE (val); | |
346 | } | |
347 | if (type != NULL) | |
348 | { | |
349 | recursive_dump_type (type, 0); | |
350 | } | |
351 | do_cleanups (old_chain); | |
352 | } | |
c906108c | 353 | } |
c906108c | 354 | \f |
c5aa993b | 355 | |
c906108c | 356 | void |
fba45db2 | 357 | _initialize_typeprint (void) |
c906108c SS |
358 | { |
359 | ||
360 | add_com ("ptype", class_vars, ptype_command, | |
361 | "Print definition of type TYPE.\n\ | |
362 | Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\ | |
363 | or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\ | |
364 | The selected stack frame's lexical context is used to look up the name."); | |
365 | ||
366 | add_com ("whatis", class_vars, whatis_command, | |
367 | "Print data type of expression EXP."); | |
368 | ||
369 | } |