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