1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
4 2000, 2001, 2003, 2006, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_obstack.h"
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
34 #include "typeprint.h"
35 #include "gdb_string.h"
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. */
42 extern void _initialize_typeprint (void);
44 static void ptype_command (char *, int);
46 static void whatis_command (char *, int);
48 static void whatis_exp (char *, int);
51 /* Print a description of a type in the format of a
52 typedef for the current language.
53 NEW is the new name for a type TYPE. */
56 typedef_print (struct type
*type
, struct symbol
*new, struct ui_file
*stream
)
58 LA_PRINT_TYPEDEF (type
, new, stream
);
61 /* The default way to print a typedef. */
64 default_print_typedef (struct type
*type
, struct symbol
*new_symbol
,
65 struct ui_file
*stream
)
67 error (_("Language not supported."));
70 /* Print a description of a type TYPE in the form of a declaration of a
71 variable named VARSTRING. (VARSTRING is demangled if necessary.)
72 Output goes to STREAM (via stdio).
73 If SHOW is positive, we show the contents of the outermost level
74 of structure even if there is a type name that could be used instead.
75 If SHOW is negative, we never show the details of elements' types. */
78 type_print (struct type
*type
, char *varstring
, struct ui_file
*stream
,
81 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
84 /* Print type of EXP, or last thing in value history if EXP == NULL.
85 show is passed to type_print. */
88 whatis_exp (char *exp
, int show
)
90 struct expression
*expr
;
92 struct cleanup
*old_chain
= NULL
;
93 struct type
*real_type
= NULL
;
101 expr
= parse_expression (exp
);
102 old_chain
= make_cleanup (free_current_contents
, &expr
);
103 val
= evaluate_type (expr
);
106 val
= access_value_history (0);
108 type
= value_type (val
);
112 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
)
113 || (TYPE_CODE (type
) == TYPE_CODE_REF
))
114 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
116 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
119 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
120 real_type
= lookup_pointer_type (real_type
);
122 real_type
= lookup_reference_type (real_type
);
125 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
126 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
129 printf_filtered ("type = ");
133 printf_filtered ("/* real type = ");
134 type_print (real_type
, "", gdb_stdout
, -1);
136 printf_filtered (" (incomplete object)");
137 printf_filtered (" */\n");
140 type_print (type
, "", gdb_stdout
, show
);
141 printf_filtered ("\n");
144 do_cleanups (old_chain
);
148 whatis_command (char *exp
, int from_tty
)
150 /* Most of the time users do not want to see all the fields
151 in a structure. If they do they can use the "ptype" command.
152 Hence the "-1" below. */
153 whatis_exp (exp
, -1);
156 /* TYPENAME is either the name of a type, or an expression. */
159 ptype_command (char *typename
, int from_tty
)
161 whatis_exp (typename
, 1);
164 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
165 Used to print data from type structures in a specified type. For example,
166 array bounds may be characters or booleans in some languages, and this
167 allows the ranges to be printed in their "natural" form rather than as
168 decimal integer values.
170 FIXME: This is here simply because only the type printing routines
171 currently use it, and it wasn't clear if it really belonged somewhere
172 else (like printcmd.c). There are a lot of other gdb routines that do
173 something similar, but they are generally concerned with printing values
174 that come from the inferior in target byte order and target size. */
177 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
182 CHECK_TYPEDEF (type
);
184 switch (TYPE_CODE (type
))
188 len
= TYPE_NFIELDS (type
);
189 for (i
= 0; i
< len
; i
++)
191 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
198 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
202 print_longest (stream
, 'd', 0, val
);
207 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
211 LA_PRINT_CHAR ((unsigned char) val
, stream
);
215 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
218 case TYPE_CODE_RANGE
:
219 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
222 case TYPE_CODE_UNDEF
:
224 case TYPE_CODE_ARRAY
:
225 case TYPE_CODE_STRUCT
:
226 case TYPE_CODE_UNION
:
231 case TYPE_CODE_STRING
:
232 case TYPE_CODE_ERROR
:
233 case TYPE_CODE_MEMBERPTR
:
234 case TYPE_CODE_METHODPTR
:
235 case TYPE_CODE_METHOD
:
237 case TYPE_CODE_NAMESPACE
:
238 error (_("internal error: unhandled type in print_type_scalar"));
242 error (_("Invalid type code in symbol table."));
247 /* Dump details of a type specified either directly or indirectly.
248 Uses the same sort of type lookup mechanism as ptype_command()
249 and whatis_command(). */
252 maintenance_print_type (char *typename
, int from_tty
)
256 struct cleanup
*old_chain
;
257 struct expression
*expr
;
259 if (typename
!= NULL
)
261 expr
= parse_expression (typename
);
262 old_chain
= make_cleanup (free_current_contents
, &expr
);
263 if (expr
->elts
[0].opcode
== OP_TYPE
)
265 /* The user expression names a type directly, just use that type. */
266 type
= expr
->elts
[1].type
;
270 /* The user expression may name a type indirectly by naming an
271 object of that type. Find that indirectly named type. */
272 val
= evaluate_type (expr
);
273 type
= value_type (val
);
277 recursive_dump_type (type
, 0);
279 do_cleanups (old_chain
);
285 _initialize_typeprint (void)
287 add_com ("ptype", class_vars
, ptype_command
, _("\
288 Print definition of type TYPE.\n\
289 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
290 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
291 The selected stack frame's lexical context is used to look up the name."));
293 add_com ("whatis", class_vars
, whatis_command
,
294 _("Print data type of expression EXP."));
This page took 0.034584 seconds and 4 git commands to generate.