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