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 (void);
44 static void ptype_command (char *, int);
46 static struct type
*ptype_eval (struct expression
*);
48 static void whatis_command (char *, int);
50 static void whatis_exp (char *, int);
52 /* Print a description of a type TYPE in the form of a declaration of a
53 variable named VARSTRING. (VARSTRING is demangled if necessary.)
54 Output goes to STREAM (via stdio).
55 If SHOW is positive, we show the contents of the outermost level
56 of structure even if there is a type name that could be used instead.
57 If SHOW is negative, we never show the details of elements' types. */
60 type_print (type
, varstring
, stream
, show
)
63 struct ui_file
*stream
;
66 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
69 /* Print type of EXP, or last thing in value history if EXP == NULL.
70 show is passed to type_print. */
73 whatis_exp (exp
, show
)
77 struct expression
*expr
;
78 register value_ptr val
;
79 register struct cleanup
*old_chain
= NULL
;
80 struct type
*real_type
= NULL
;
88 expr
= parse_expression (exp
);
89 old_chain
= make_cleanup (free_current_contents
, &expr
);
90 val
= evaluate_type (expr
);
93 val
= access_value_history (0);
95 type
= VALUE_TYPE (val
);
99 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) ||
100 (TYPE_CODE (type
) == TYPE_CODE_REF
))
102 (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
104 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
107 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
108 real_type
= lookup_pointer_type (real_type
);
110 real_type
= lookup_reference_type (real_type
);
113 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
114 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
117 printf_filtered ("type = ");
121 printf_filtered ("/* real type = ");
122 type_print (real_type
, "", gdb_stdout
, -1);
124 printf_filtered (" (incomplete object)");
125 printf_filtered (" */\n");
128 type_print (type
, "", gdb_stdout
, show
);
129 printf_filtered ("\n");
132 do_cleanups (old_chain
);
137 whatis_command (exp
, from_tty
)
141 /* Most of the time users do not want to see all the fields
142 in a structure. If they do they can use the "ptype" command.
143 Hence the "-1" below. */
144 whatis_exp (exp
, -1);
147 /* Simple subroutine for ptype_command. */
151 struct expression
*exp
;
153 if (exp
->elts
[0].opcode
== OP_TYPE
)
155 return (exp
->elts
[1].type
);
163 /* TYPENAME is either the name of a type, or an expression. */
167 ptype_command (typename
, from_tty
)
171 register struct type
*type
;
172 struct expression
*expr
;
173 register struct cleanup
*old_chain
;
175 if (typename
== NULL
)
177 /* Print type of last thing in value history. */
178 whatis_exp (typename
, 1);
182 expr
= parse_expression (typename
);
183 old_chain
= make_cleanup (free_current_contents
, &expr
);
184 type
= ptype_eval (expr
);
187 /* User did "ptype <typename>" */
188 printf_filtered ("type = ");
189 type_print (type
, "", gdb_stdout
, 1);
190 printf_filtered ("\n");
191 do_cleanups (old_chain
);
195 /* User did "ptype <symbolname>" */
196 do_cleanups (old_chain
);
197 whatis_exp (typename
, 1);
202 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
203 Used to print data from type structures in a specified type. For example,
204 array bounds may be characters or booleans in some languages, and this
205 allows the ranges to be printed in their "natural" form rather than as
206 decimal integer values.
208 FIXME: This is here simply because only the type printing routines
209 currently use it, and it wasn't clear if it really belonged somewhere
210 else (like printcmd.c). There are a lot of other gdb routines that do
211 something similar, but they are generally concerned with printing values
212 that come from the inferior in target byte order and target size. */
215 print_type_scalar (type
, val
, stream
)
218 struct ui_file
*stream
;
223 CHECK_TYPEDEF (type
);
225 switch (TYPE_CODE (type
))
229 len
= TYPE_NFIELDS (type
);
230 for (i
= 0; i
< len
; i
++)
232 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
239 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
243 print_longest (stream
, 'd', 0, val
);
248 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
252 LA_PRINT_CHAR ((unsigned char) val
, stream
);
256 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
259 case TYPE_CODE_RANGE
:
260 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
263 case TYPE_CODE_UNDEF
:
265 case TYPE_CODE_ARRAY
:
266 case TYPE_CODE_STRUCT
:
267 case TYPE_CODE_UNION
:
272 case TYPE_CODE_STRING
:
273 case TYPE_CODE_ERROR
:
274 case TYPE_CODE_MEMBER
:
275 case TYPE_CODE_METHOD
:
277 error ("internal error: unhandled type in print_type_scalar");
281 error ("Invalid type code in symbol table.");
286 /* Dump details of a type specified either directly or indirectly.
287 Uses the same sort of type lookup mechanism as ptype_command()
288 and whatis_command(). */
291 maintenance_print_type (typename
, from_tty
)
295 register value_ptr val
;
296 register struct type
*type
;
297 register struct cleanup
*old_chain
;
298 struct expression
*expr
;
300 if (typename
!= NULL
)
302 expr
= parse_expression (typename
);
303 old_chain
= make_cleanup (free_current_contents
, &expr
);
304 if (expr
->elts
[0].opcode
== OP_TYPE
)
306 /* The user expression names a type directly, just use that type. */
307 type
= expr
->elts
[1].type
;
311 /* The user expression may name a type indirectly by naming an
312 object of that type. Find that indirectly named type. */
313 val
= evaluate_type (expr
);
314 type
= VALUE_TYPE (val
);
318 recursive_dump_type (type
, 0);
320 do_cleanups (old_chain
);
326 _initialize_typeprint ()
329 add_com ("ptype", class_vars
, ptype_command
,
330 "Print definition of type TYPE.\n\
331 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
332 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
333 The selected stack frame's lexical context is used to look up the name.");
335 add_com ("whatis", class_vars
, whatis_command
,
336 "Print data type of expression EXP.");
This page took 0.035987 seconds and 4 git commands to generate.