1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998,
4 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "gdb_obstack.h"
25 #include "bfd.h" /* Binary File Description */
28 #include "expression.h"
36 #include "typeprint.h"
37 #include "gdb_string.h"
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. */
44 extern void _initialize_typeprint (void);
46 static void ptype_command (char *, int);
48 static void whatis_command (char *, int);
50 static void whatis_exp (char *, int);
52 /* Print a description of a type in the format of a
53 typedef for the current language.
54 NEW is the new name for a type TYPE. */
57 typedef_print (struct type
*type
, struct symbol
*new, struct ui_file
*stream
)
60 switch (current_language
->la_language
)
65 fprintf_filtered (stream
, "typedef ");
66 type_print (type
, "", stream
, 0);
67 if (TYPE_NAME ((SYMBOL_TYPE (new))) == 0
68 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0)
69 fprintf_filtered (stream
, " %s", SYMBOL_PRINT_NAME (new));
74 fprintf_filtered (stream
, "TYPE ");
75 if (!TYPE_NAME (SYMBOL_TYPE (new))
76 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0)
77 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new));
79 fprintf_filtered (stream
, "<builtin> = ");
80 type_print (type
, "", stream
, 0);
85 fprintf_filtered (stream
, "type ");
86 fprintf_filtered (stream
, "%s = ", SYMBOL_PRINT_NAME (new));
87 type_print (type
, "", stream
, 0);
91 error (_("Language not supported."));
93 fprintf_filtered (stream
, ";\n");
96 /* Print a description of a type TYPE in the form of a declaration of a
97 variable named VARSTRING. (VARSTRING is demangled if necessary.)
98 Output goes to STREAM (via stdio).
99 If SHOW is positive, we show the contents of the outermost level
100 of structure even if there is a type name that could be used instead.
101 If SHOW is negative, we never show the details of elements' types. */
104 type_print (struct type
*type
, char *varstring
, struct ui_file
*stream
,
107 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
110 /* Print type of EXP, or last thing in value history if EXP == NULL.
111 show is passed to type_print. */
114 whatis_exp (char *exp
, int show
)
116 struct expression
*expr
;
118 struct cleanup
*old_chain
= NULL
;
119 struct type
*real_type
= NULL
;
127 expr
= parse_expression (exp
);
128 old_chain
= make_cleanup (free_current_contents
, &expr
);
129 val
= evaluate_type (expr
);
132 val
= access_value_history (0);
134 type
= value_type (val
);
138 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) ||
139 (TYPE_CODE (type
) == TYPE_CODE_REF
))
141 (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
143 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
146 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
147 real_type
= lookup_pointer_type (real_type
);
149 real_type
= lookup_reference_type (real_type
);
152 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
153 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
156 printf_filtered ("type = ");
160 printf_filtered ("/* real type = ");
161 type_print (real_type
, "", gdb_stdout
, -1);
163 printf_filtered (" (incomplete object)");
164 printf_filtered (" */\n");
167 type_print (type
, "", gdb_stdout
, show
);
168 printf_filtered ("\n");
171 do_cleanups (old_chain
);
175 whatis_command (char *exp
, int from_tty
)
177 /* Most of the time users do not want to see all the fields
178 in a structure. If they do they can use the "ptype" command.
179 Hence the "-1" below. */
180 whatis_exp (exp
, -1);
183 /* TYPENAME is either the name of a type, or an expression. */
186 ptype_command (char *typename
, int from_tty
)
188 whatis_exp (typename
, 1);
191 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
192 Used to print data from type structures in a specified type. For example,
193 array bounds may be characters or booleans in some languages, and this
194 allows the ranges to be printed in their "natural" form rather than as
195 decimal integer values.
197 FIXME: This is here simply because only the type printing routines
198 currently use it, and it wasn't clear if it really belonged somewhere
199 else (like printcmd.c). There are a lot of other gdb routines that do
200 something similar, but they are generally concerned with printing values
201 that come from the inferior in target byte order and target size. */
204 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
209 CHECK_TYPEDEF (type
);
211 switch (TYPE_CODE (type
))
215 len
= TYPE_NFIELDS (type
);
216 for (i
= 0; i
< len
; i
++)
218 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
225 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
229 print_longest (stream
, 'd', 0, val
);
234 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
238 LA_PRINT_CHAR ((unsigned char) val
, stream
);
242 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
245 case TYPE_CODE_RANGE
:
246 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
249 case TYPE_CODE_UNDEF
:
251 case TYPE_CODE_ARRAY
:
252 case TYPE_CODE_STRUCT
:
253 case TYPE_CODE_UNION
:
258 case TYPE_CODE_STRING
:
259 case TYPE_CODE_ERROR
:
260 case TYPE_CODE_MEMBER
:
261 case TYPE_CODE_METHOD
:
263 case TYPE_CODE_NAMESPACE
:
264 error (_("internal error: unhandled type in print_type_scalar"));
268 error (_("Invalid type code in symbol table."));
273 /* Dump details of a type specified either directly or indirectly.
274 Uses the same sort of type lookup mechanism as ptype_command()
275 and whatis_command(). */
278 maintenance_print_type (char *typename
, int from_tty
)
282 struct cleanup
*old_chain
;
283 struct expression
*expr
;
285 if (typename
!= NULL
)
287 expr
= parse_expression (typename
);
288 old_chain
= make_cleanup (free_current_contents
, &expr
);
289 if (expr
->elts
[0].opcode
== OP_TYPE
)
291 /* The user expression names a type directly, just use that type. */
292 type
= expr
->elts
[1].type
;
296 /* The user expression may name a type indirectly by naming an
297 object of that type. Find that indirectly named type. */
298 val
= evaluate_type (expr
);
299 type
= value_type (val
);
303 recursive_dump_type (type
, 0);
305 do_cleanups (old_chain
);
311 _initialize_typeprint (void)
314 add_com ("ptype", class_vars
, ptype_command
, _("\
315 Print definition of type TYPE.\n\
316 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
317 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
318 The selected stack frame's lexical context is used to look up the name."));
320 add_com ("whatis", class_vars
, whatis_command
,
321 _("Print data type of expression EXP."));