1 /* Language independent support for printing types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991-1993, 1998, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.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
PARAMS ((void));
45 ptype_command
PARAMS ((char *, int));
48 ptype_eval
PARAMS ((struct expression
*));
51 whatis_command
PARAMS ((char *, int));
54 whatis_exp
PARAMS ((char *, int));
56 /* Print a description of a type TYPE in the form of a declaration of a
57 variable named VARSTRING. (VARSTRING is demangled if necessary.)
58 Output goes to STREAM (via stdio).
59 If SHOW is positive, we show the contents of the outermost level
60 of structure even if there is a type name that could be used instead.
61 If SHOW is negative, we never show the details of elements' types. */
64 type_print (type
, varstring
, stream
, show
)
67 struct ui_file
*stream
;
70 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
73 /* Print type of EXP, or last thing in value history if EXP == NULL.
74 show is passed to type_print. */
77 whatis_exp (exp
, show
)
81 struct expression
*expr
;
82 register value_ptr val
;
83 register struct cleanup
*old_chain
= NULL
;
84 struct type
*real_type
= NULL
;
92 expr
= parse_expression (exp
);
93 old_chain
= make_cleanup (free_current_contents
, &expr
);
94 val
= evaluate_type (expr
);
97 val
= access_value_history (0);
99 type
= VALUE_TYPE (val
);
103 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) ||
104 (TYPE_CODE (type
) == TYPE_CODE_REF
))
106 (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
108 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
111 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
112 real_type
= lookup_pointer_type (real_type
);
114 real_type
= lookup_reference_type (real_type
);
117 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
118 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
121 printf_filtered ("type = ");
125 printf_filtered ("/* real type = ");
126 type_print (real_type
, "", gdb_stdout
, -1);
128 printf_filtered (" (incomplete object)");
129 printf_filtered (" */\n");
132 type_print (type
, "", gdb_stdout
, show
);
133 printf_filtered ("\n");
136 do_cleanups (old_chain
);
141 whatis_command (exp
, from_tty
)
145 /* Most of the time users do not want to see all the fields
146 in a structure. If they do they can use the "ptype" command.
147 Hence the "-1" below. */
148 whatis_exp (exp
, -1);
151 /* Simple subroutine for ptype_command. */
155 struct expression
*exp
;
157 if (exp
->elts
[0].opcode
== OP_TYPE
)
159 return (exp
->elts
[1].type
);
167 /* TYPENAME is either the name of a type, or an expression. */
171 ptype_command (typename
, from_tty
)
175 register struct type
*type
;
176 struct expression
*expr
;
177 register struct cleanup
*old_chain
;
179 if (typename
== NULL
)
181 /* Print type of last thing in value history. */
182 whatis_exp (typename
, 1);
186 expr
= parse_expression (typename
);
187 old_chain
= make_cleanup (free_current_contents
, &expr
);
188 type
= ptype_eval (expr
);
191 /* User did "ptype <typename>" */
192 printf_filtered ("type = ");
193 type_print (type
, "", gdb_stdout
, 1);
194 printf_filtered ("\n");
195 do_cleanups (old_chain
);
199 /* User did "ptype <symbolname>" */
200 do_cleanups (old_chain
);
201 whatis_exp (typename
, 1);
206 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
207 Used to print data from type structures in a specified type. For example,
208 array bounds may be characters or booleans in some languages, and this
209 allows the ranges to be printed in their "natural" form rather than as
210 decimal integer values.
212 FIXME: This is here simply because only the type printing routines
213 currently use it, and it wasn't clear if it really belonged somewhere
214 else (like printcmd.c). There are a lot of other gdb routines that do
215 something similar, but they are generally concerned with printing values
216 that come from the inferior in target byte order and target size. */
219 print_type_scalar (type
, val
, stream
)
222 struct ui_file
*stream
;
227 CHECK_TYPEDEF (type
);
229 switch (TYPE_CODE (type
))
233 len
= TYPE_NFIELDS (type
);
234 for (i
= 0; i
< len
; i
++)
236 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
243 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
247 print_longest (stream
, 'd', 0, val
);
252 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
256 LA_PRINT_CHAR ((unsigned char) val
, stream
);
260 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
263 case TYPE_CODE_RANGE
:
264 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
267 case TYPE_CODE_UNDEF
:
269 case TYPE_CODE_ARRAY
:
270 case TYPE_CODE_STRUCT
:
271 case TYPE_CODE_UNION
:
276 case TYPE_CODE_STRING
:
277 case TYPE_CODE_ERROR
:
278 case TYPE_CODE_MEMBER
:
279 case TYPE_CODE_METHOD
:
281 error ("internal error: unhandled type in print_type_scalar");
285 error ("Invalid type code in symbol table.");
290 /* Dump details of a type specified either directly or indirectly.
291 Uses the same sort of type lookup mechanism as ptype_command()
292 and whatis_command(). */
295 maintenance_print_type (typename
, from_tty
)
299 register value_ptr val
;
300 register struct type
*type
;
301 register struct cleanup
*old_chain
;
302 struct expression
*expr
;
304 if (typename
!= NULL
)
306 expr
= parse_expression (typename
);
307 old_chain
= make_cleanup (free_current_contents
, &expr
);
308 if (expr
->elts
[0].opcode
== OP_TYPE
)
310 /* The user expression names a type directly, just use that type. */
311 type
= expr
->elts
[1].type
;
315 /* The user expression may name a type indirectly by naming an
316 object of that type. Find that indirectly named type. */
317 val
= evaluate_type (expr
);
318 type
= VALUE_TYPE (val
);
322 recursive_dump_type (type
, 0);
324 do_cleanups (old_chain
);
330 _initialize_typeprint ()
333 add_com ("ptype", class_vars
, ptype_command
,
334 "Print definition of type TYPE.\n\
335 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
336 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
337 The selected stack frame's lexical context is used to look up the name.");
339 add_com ("whatis", class_vars
, whatis_command
,
340 "Print data type of expression EXP.");
This page took 0.035369 seconds and 4 git commands to generate.