1 /* Language independent support for printing types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
38 ptype_command PARAMS ((char *, int));
41 ptype_eval PARAMS ((struct expression *));
44 whatis_command PARAMS ((char *, int));
47 whatis_exp PARAMS ((char *, int));
49 /* Print a description of a type TYPE in the form of a declaration of a
50 variable named VARSTRING. (VARSTRING is demangled if necessary.)
51 Output goes to STREAM (via stdio).
52 If SHOW is positive, we show the contents of the outermost level
53 of structure even if there is a type name that could be used instead.
54 If SHOW is negative, we never show the details of elements' types. */
57 type_print (type, varstring, stream, show)
63 LA_PRINT_TYPE (type, varstring, stream, show, 0);
66 /* Print type of EXP, or last thing in value history if EXP == NULL.
67 show is passed to type_print. */
70 whatis_exp (exp, show)
74 struct expression *expr;
76 register struct cleanup *old_chain;
80 expr = parse_expression (exp);
81 old_chain = make_cleanup (free_current_contents, &expr);
82 val = evaluate_type (expr);
85 val = access_value_history (0);
87 printf_filtered ("type = ");
88 type_print (VALUE_TYPE (val), "", stdout, show);
89 printf_filtered ("\n");
92 do_cleanups (old_chain);
97 whatis_command (exp, from_tty)
101 /* Most of the time users do not want to see all the fields
102 in a structure. If they do they can use the "ptype" command.
103 Hence the "-1" below. */
104 whatis_exp (exp, -1);
107 /* Simple subroutine for ptype_command. */
111 struct expression *exp;
113 if (exp->elts[0].opcode == OP_TYPE)
115 return (exp->elts[1].type);
123 /* TYPENAME is either the name of a type, or an expression. */
127 ptype_command (typename, from_tty)
131 register struct type *type;
132 struct expression *expr;
133 register struct cleanup *old_chain;
135 if (typename == NULL)
137 /* Print type of last thing in value history. */
138 whatis_exp (typename, 1);
142 expr = parse_expression (typename);
143 old_chain = make_cleanup (free_current_contents, &expr);
144 type = ptype_eval (expr);
147 /* User did "ptype <typename>" */
148 printf_filtered ("type = ");
149 type_print (type, "", stdout, 1);
150 printf_filtered ("\n");
151 do_cleanups (old_chain);
155 /* User did "ptype <symbolname>" */
156 do_cleanups (old_chain);
157 whatis_exp (typename, 1);
162 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
163 Used to print data from type structures in a specified type. For example,
164 array bounds may be characters or booleans in some languages, and this
165 allows the ranges to be printed in their "natural" form rather than as
166 decimal integer values.
168 FIXME: This is here simply because only the type printing routines
169 currently use it, and it wasn't clear if it really belonged somewhere
170 else (like printcmd.c). There are a lot of other gdb routines that do
171 something similar, but they are generally concerned with printing values
172 that come from the inferior in target byte order and target size. */
175 print_type_scalar (type, val, stream)
183 switch (TYPE_CODE (type))
187 len = TYPE_NFIELDS (type);
188 for (i = 0; i < len; i++)
190 if (TYPE_FIELD_BITPOS (type, i) == val)
197 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
202 fprintf_filtered (stream, "%lld", val);
204 fprintf_filtered (stream, "%ld", val);
211 fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%llu" : "%lld", val);
213 fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d", val);
218 LA_PRINT_CHAR ((unsigned char) val, stream);
222 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
225 case TYPE_CODE_UNDEF:
227 case TYPE_CODE_ARRAY:
228 case TYPE_CODE_STRUCT:
229 case TYPE_CODE_UNION:
234 case TYPE_CODE_RANGE:
235 case TYPE_CODE_STRING:
236 case TYPE_CODE_ERROR:
237 case TYPE_CODE_MEMBER:
238 case TYPE_CODE_METHOD:
240 error ("internal error: unhandled type in print_type_scalar");
244 error ("Invalid type code in symbol table.");
251 /* Dump details of a type specified either directly or indirectly.
252 Uses the same sort of type lookup mechanism as ptype_command()
253 and whatis_command(). */
256 maintenance_print_type (typename, from_tty)
261 register struct type *type;
262 register struct cleanup *old_chain;
263 struct expression *expr;
265 if (typename != NULL)
267 expr = parse_expression (typename);
268 old_chain = make_cleanup (free_current_contents, &expr);
269 if (expr -> elts[0].opcode == OP_TYPE)
271 /* The user expression names a type directly, just use that type. */
272 type = expr -> elts[1].type;
276 /* The user expression may name a type indirectly by naming an
277 object of that type. Find that indirectly named type. */
278 val = evaluate_type (expr);
279 type = VALUE_TYPE (val);
283 recursive_dump_type (type, 0);
285 do_cleanups (old_chain);
289 #endif /* MAINTENANCE_CMDS */
293 _initialize_typeprint ()
296 add_com ("ptype", class_vars, ptype_command,
297 "Print definition of type TYPE.\n\
298 Argument may be a type name defined by typedef, or \"struct STRUCTNAME\"\n\
299 or \"union UNIONNAME\" or \"enum ENUMNAME\".\n\
300 The selected stack frame's lexical context is used to look up the name.");
302 add_com ("whatis", class_vars, whatis_command,
303 "Print data type of expression EXP.");