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