]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
1bac305b | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999, |
4c38e0a4 JB |
4 | 2000, 2001, 2003, 2006, 2007, 2008, 2009, 2010 |
5 | Free Software Foundation, Inc. | |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
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" |
b9362cc7 | 35 | #include "typeprint.h" |
c906108c | 36 | #include "gdb_string.h" |
ae6a3a4c | 37 | #include "exceptions.h" |
79a45b7d | 38 | #include "valprint.h" |
c906108c SS |
39 | #include <errno.h> |
40 | ||
a14ed312 | 41 | extern void _initialize_typeprint (void); |
392a587b | 42 | |
a14ed312 | 43 | static void ptype_command (char *, int); |
c906108c | 44 | |
a14ed312 | 45 | static void whatis_command (char *, int); |
c906108c | 46 | |
a14ed312 | 47 | static void whatis_exp (char *, int); |
c906108c | 48 | |
5c6ce71d | 49 | |
a5238fbc PM |
50 | /* Print a description of a type in the format of a |
51 | typedef for the current language. | |
52 | NEW is the new name for a type TYPE. */ | |
53 | ||
54 | void | |
55 | typedef_print (struct type *type, struct symbol *new, struct ui_file *stream) | |
56 | { | |
5c6ce71d TT |
57 | LA_PRINT_TYPEDEF (type, new, stream); |
58 | } | |
59 | ||
60 | /* The default way to print a typedef. */ | |
61 | ||
62 | void | |
63 | default_print_typedef (struct type *type, struct symbol *new_symbol, | |
64 | struct ui_file *stream) | |
65 | { | |
66 | error (_("Language not supported.")); | |
a5238fbc PM |
67 | } |
68 | ||
c906108c SS |
69 | /* Print a description of a type TYPE in the form of a declaration of a |
70 | variable named VARSTRING. (VARSTRING is demangled if necessary.) | |
71 | Output goes to STREAM (via stdio). | |
72 | If SHOW is positive, we show the contents of the outermost level | |
73 | of structure even if there is a type name that could be used instead. | |
74 | If SHOW is negative, we never show the details of elements' types. */ | |
75 | ||
76 | void | |
fba45db2 KB |
77 | type_print (struct type *type, char *varstring, struct ui_file *stream, |
78 | int show) | |
c906108c SS |
79 | { |
80 | LA_PRINT_TYPE (type, varstring, stream, show, 0); | |
81 | } | |
82 | ||
ae6a3a4c TJB |
83 | /* Print TYPE to a string, returning it. The caller is responsible for |
84 | freeing the string. */ | |
85 | ||
86 | char * | |
87 | type_to_string (struct type *type) | |
88 | { | |
89 | char *s = NULL; | |
ae6a3a4c TJB |
90 | struct ui_file *stb; |
91 | struct cleanup *old_chain; | |
92 | volatile struct gdb_exception except; | |
93 | ||
94 | stb = mem_fileopen (); | |
95 | old_chain = make_cleanup_ui_file_delete (stb); | |
96 | ||
97 | TRY_CATCH (except, RETURN_MASK_ALL) | |
98 | { | |
99 | type_print (type, "", stb, -1); | |
759ef836 | 100 | s = ui_file_xstrdup (stb, NULL); |
ae6a3a4c TJB |
101 | } |
102 | if (except.reason < 0) | |
103 | s = NULL; | |
104 | ||
105 | do_cleanups (old_chain); | |
106 | ||
107 | return s; | |
108 | } | |
109 | ||
c906108c SS |
110 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
111 | show is passed to type_print. */ | |
112 | ||
113 | static void | |
fba45db2 | 114 | whatis_exp (char *exp, int show) |
c906108c SS |
115 | { |
116 | struct expression *expr; | |
3d6d86c6 | 117 | struct value *val; |
52f0bd74 | 118 | struct cleanup *old_chain = NULL; |
c5aa993b | 119 | struct type *real_type = NULL; |
070ad9f0 | 120 | struct type *type; |
c906108c SS |
121 | int full = 0; |
122 | int top = -1; | |
123 | int using_enc = 0; | |
79a45b7d | 124 | struct value_print_options opts; |
c906108c SS |
125 | |
126 | if (exp) | |
127 | { | |
128 | expr = parse_expression (exp); | |
c13c43fd | 129 | old_chain = make_cleanup (free_current_contents, &expr); |
c906108c SS |
130 | val = evaluate_type (expr); |
131 | } | |
132 | else | |
133 | val = access_value_history (0); | |
134 | ||
df407dfe | 135 | type = value_type (val); |
070ad9f0 | 136 | |
79a45b7d TT |
137 | get_user_print_options (&opts); |
138 | if (opts.objectprint) | |
070ad9f0 | 139 | { |
41808ebe DE |
140 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) |
141 | || (TYPE_CODE (type) == TYPE_CODE_REF)) | |
142 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
070ad9f0 DB |
143 | { |
144 | real_type = value_rtti_target_type (val, &full, &top, &using_enc); | |
145 | if (real_type) | |
146 | { | |
147 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
148 | real_type = lookup_pointer_type (real_type); | |
149 | else | |
150 | real_type = lookup_reference_type (real_type); | |
151 | } | |
152 | } | |
153 | else if (TYPE_CODE (type) == TYPE_CODE_CLASS) | |
41808ebe | 154 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
070ad9f0 | 155 | } |
c906108c SS |
156 | |
157 | printf_filtered ("type = "); | |
158 | ||
070ad9f0 DB |
159 | if (real_type) |
160 | { | |
161 | printf_filtered ("/* real type = "); | |
162 | type_print (real_type, "", gdb_stdout, -1); | |
163 | if (! full) | |
164 | printf_filtered (" (incomplete object)"); | |
165 | printf_filtered (" */\n"); | |
166 | } | |
c906108c | 167 | |
070ad9f0 | 168 | type_print (type, "", gdb_stdout, show); |
c906108c SS |
169 | printf_filtered ("\n"); |
170 | ||
171 | if (exp) | |
172 | do_cleanups (old_chain); | |
173 | } | |
174 | ||
c906108c | 175 | static void |
fba45db2 | 176 | whatis_command (char *exp, int from_tty) |
c906108c SS |
177 | { |
178 | /* Most of the time users do not want to see all the fields | |
179 | in a structure. If they do they can use the "ptype" command. | |
180 | Hence the "-1" below. */ | |
181 | whatis_exp (exp, -1); | |
182 | } | |
183 | ||
c906108c SS |
184 | /* TYPENAME is either the name of a type, or an expression. */ |
185 | ||
c906108c | 186 | static void |
fba45db2 | 187 | ptype_command (char *typename, int from_tty) |
c906108c | 188 | { |
d843c49c | 189 | whatis_exp (typename, 1); |
c906108c SS |
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 | |
41808ebe | 202 | that come from the inferior in target byte order and target size. */ |
c906108c SS |
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: | |
6c7a06a3 | 239 | LA_PRINT_CHAR ((unsigned char) val, type, stream); |
c906108c SS |
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: | |
0d5de010 DJ |
261 | case TYPE_CODE_MEMBERPTR: |
262 | case TYPE_CODE_METHODPTR: | |
c906108c SS |
263 | case TYPE_CODE_METHOD: |
264 | case TYPE_CODE_REF: | |
5c4e30ca | 265 | case TYPE_CODE_NAMESPACE: |
8a3fe4f8 | 266 | error (_("internal error: unhandled type in print_type_scalar")); |
c906108c SS |
267 | break; |
268 | ||
269 | default: | |
8a3fe4f8 | 270 | error (_("Invalid type code in symbol table.")); |
c906108c SS |
271 | } |
272 | gdb_flush (stream); | |
273 | } | |
274 | ||
c906108c SS |
275 | /* Dump details of a type specified either directly or indirectly. |
276 | Uses the same sort of type lookup mechanism as ptype_command() | |
41808ebe | 277 | and whatis_command(). */ |
c906108c SS |
278 | |
279 | void | |
fba45db2 | 280 | maintenance_print_type (char *typename, int from_tty) |
c906108c | 281 | { |
3d6d86c6 | 282 | struct value *val; |
52f0bd74 AC |
283 | struct type *type; |
284 | struct cleanup *old_chain; | |
c906108c SS |
285 | struct expression *expr; |
286 | ||
287 | if (typename != NULL) | |
c5aa993b JM |
288 | { |
289 | expr = parse_expression (typename); | |
c13c43fd | 290 | old_chain = make_cleanup (free_current_contents, &expr); |
c5aa993b JM |
291 | if (expr->elts[0].opcode == OP_TYPE) |
292 | { | |
293 | /* The user expression names a type directly, just use that type. */ | |
294 | type = expr->elts[1].type; | |
295 | } | |
296 | else | |
297 | { | |
298 | /* The user expression may name a type indirectly by naming an | |
299 | object of that type. Find that indirectly named type. */ | |
300 | val = evaluate_type (expr); | |
df407dfe | 301 | type = value_type (val); |
c5aa993b JM |
302 | } |
303 | if (type != NULL) | |
304 | { | |
305 | recursive_dump_type (type, 0); | |
306 | } | |
307 | do_cleanups (old_chain); | |
308 | } | |
c906108c | 309 | } |
c906108c | 310 | \f |
c5aa993b | 311 | |
c906108c | 312 | void |
fba45db2 | 313 | _initialize_typeprint (void) |
c906108c | 314 | { |
1bedd215 AC |
315 | add_com ("ptype", class_vars, ptype_command, _("\ |
316 | Print definition of type TYPE.\n\ | |
c906108c SS |
317 | Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\ |
318 | or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\ | |
1bedd215 | 319 | The selected stack frame's lexical context is used to look up the name.")); |
c906108c SS |
320 | |
321 | add_com ("whatis", class_vars, whatis_command, | |
1bedd215 | 322 | _("Print data type of expression EXP.")); |
c906108c | 323 | } |