1 /* Language independent support for printing types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
3 2000 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "bfd.h" /* Binary File Description */
27 #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 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 || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
69 fprintf_filtered (stream
, " %s", SYMBOL_SOURCE_NAME (new));
74 fprintf_filtered (stream
, "TYPE ");
75 if (!TYPE_NAME (SYMBOL_TYPE (new)) ||
76 !STREQ (TYPE_NAME (SYMBOL_TYPE (new)), SYMBOL_NAME (new)))
77 fprintf_filtered (stream
, "%s = ", SYMBOL_SOURCE_NAME (new));
79 fprintf_filtered (stream
, "<builtin> = ");
80 type_print (type
, "", stream
, 0);
85 fprintf_filtered (stream
, "type ");
86 fprintf_filtered (stream
, "%s = ", SYMBOL_SOURCE_NAME (new));
87 type_print (type
, "", stream
, 0);
92 fprintf_filtered (stream
, "SYNMODE ");
93 if (!TYPE_NAME (SYMBOL_TYPE (new)) ||
94 !STREQ (TYPE_NAME (SYMBOL_TYPE (new)), SYMBOL_NAME (new)))
95 fprintf_filtered (stream
, "%s = ", SYMBOL_SOURCE_NAME (new));
97 fprintf_filtered (stream
, "<builtin> = ");
98 type_print (type
, "", stream
, 0);
102 error ("Language not supported.");
104 fprintf_filtered (stream
, ";\n");
107 /* Print a description of a type TYPE in the form of a declaration of a
108 variable named VARSTRING. (VARSTRING is demangled if necessary.)
109 Output goes to STREAM (via stdio).
110 If SHOW is positive, we show the contents of the outermost level
111 of structure even if there is a type name that could be used instead.
112 If SHOW is negative, we never show the details of elements' types. */
115 type_print (struct type
*type
, char *varstring
, struct ui_file
*stream
,
118 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0);
121 /* Print type of EXP, or last thing in value history if EXP == NULL.
122 show is passed to type_print. */
125 whatis_exp (char *exp
, int show
)
127 struct expression
*expr
;
128 register value_ptr val
;
129 register struct cleanup
*old_chain
= NULL
;
130 struct type
*real_type
= NULL
;
138 expr
= parse_expression (exp
);
139 old_chain
= make_cleanup (free_current_contents
, &expr
);
140 val
= evaluate_type (expr
);
143 val
= access_value_history (0);
145 type
= VALUE_TYPE (val
);
149 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) ||
150 (TYPE_CODE (type
) == TYPE_CODE_REF
))
152 (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
154 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
157 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
158 real_type
= lookup_pointer_type (real_type
);
160 real_type
= lookup_reference_type (real_type
);
163 else if (TYPE_CODE (type
) == TYPE_CODE_CLASS
)
164 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
167 printf_filtered ("type = ");
171 printf_filtered ("/* real type = ");
172 type_print (real_type
, "", gdb_stdout
, -1);
174 printf_filtered (" (incomplete object)");
175 printf_filtered (" */\n");
178 type_print (type
, "", gdb_stdout
, show
);
179 printf_filtered ("\n");
182 do_cleanups (old_chain
);
187 whatis_command (char *exp
, int from_tty
)
189 /* Most of the time users do not want to see all the fields
190 in a structure. If they do they can use the "ptype" command.
191 Hence the "-1" below. */
192 whatis_exp (exp
, -1);
195 /* Simple subroutine for ptype_command. */
198 ptype_eval (struct expression
*exp
)
200 if (exp
->elts
[0].opcode
== OP_TYPE
)
202 return (exp
->elts
[1].type
);
210 /* TYPENAME is either the name of a type, or an expression. */
214 ptype_command (char *typename
, int from_tty
)
216 register struct type
*type
;
217 struct expression
*expr
;
218 register struct cleanup
*old_chain
;
220 if (typename
== NULL
)
222 /* Print type of last thing in value history. */
223 whatis_exp (typename
, 1);
227 expr
= parse_expression (typename
);
228 old_chain
= make_cleanup (free_current_contents
, &expr
);
229 type
= ptype_eval (expr
);
232 /* User did "ptype <typename>" */
233 printf_filtered ("type = ");
234 type_print (type
, "", gdb_stdout
, 1);
235 printf_filtered ("\n");
236 do_cleanups (old_chain
);
240 /* User did "ptype <symbolname>" */
241 do_cleanups (old_chain
);
242 whatis_exp (typename
, 1);
247 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
248 Used to print data from type structures in a specified type. For example,
249 array bounds may be characters or booleans in some languages, and this
250 allows the ranges to be printed in their "natural" form rather than as
251 decimal integer values.
253 FIXME: This is here simply because only the type printing routines
254 currently use it, and it wasn't clear if it really belonged somewhere
255 else (like printcmd.c). There are a lot of other gdb routines that do
256 something similar, but they are generally concerned with printing values
257 that come from the inferior in target byte order and target size. */
260 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
265 CHECK_TYPEDEF (type
);
267 switch (TYPE_CODE (type
))
271 len
= TYPE_NFIELDS (type
);
272 for (i
= 0; i
< len
; i
++)
274 if (TYPE_FIELD_BITPOS (type
, i
) == val
)
281 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
285 print_longest (stream
, 'd', 0, val
);
290 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0, val
);
294 LA_PRINT_CHAR ((unsigned char) val
, stream
);
298 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
301 case TYPE_CODE_RANGE
:
302 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
305 case TYPE_CODE_UNDEF
:
307 case TYPE_CODE_ARRAY
:
308 case TYPE_CODE_STRUCT
:
309 case TYPE_CODE_UNION
:
314 case TYPE_CODE_STRING
:
315 case TYPE_CODE_ERROR
:
316 case TYPE_CODE_MEMBER
:
317 case TYPE_CODE_METHOD
:
319 error ("internal error: unhandled type in print_type_scalar");
323 error ("Invalid type code in symbol table.");
328 /* Dump details of a type specified either directly or indirectly.
329 Uses the same sort of type lookup mechanism as ptype_command()
330 and whatis_command(). */
333 maintenance_print_type (char *typename
, int from_tty
)
335 register value_ptr val
;
336 register struct type
*type
;
337 register struct cleanup
*old_chain
;
338 struct expression
*expr
;
340 if (typename
!= NULL
)
342 expr
= parse_expression (typename
);
343 old_chain
= make_cleanup (free_current_contents
, &expr
);
344 if (expr
->elts
[0].opcode
== OP_TYPE
)
346 /* The user expression names a type directly, just use that type. */
347 type
= expr
->elts
[1].type
;
351 /* The user expression may name a type indirectly by naming an
352 object of that type. Find that indirectly named type. */
353 val
= evaluate_type (expr
);
354 type
= VALUE_TYPE (val
);
358 recursive_dump_type (type
, 0);
360 do_cleanups (old_chain
);
366 _initialize_typeprint (void)
369 add_com ("ptype", class_vars
, ptype_command
,
370 "Print definition of type TYPE.\n\
371 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
372 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
373 The selected stack frame's lexical context is used to look up the name.");
375 add_com ("whatis", class_vars
, whatis_command
,
376 "Print data type of expression EXP.");
This page took 0.036894 seconds and 4 git commands to generate.