* Sync comment with code's reality.
[dragonfly.git] / contrib / gdb / gdb / typeprint.c
1 /* Language independent support for printing types for GDB, the GNU debugger.
2    Copyright 1986, 88, 89, 91, 92, 93, 1998 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "obstack.h"
22 #include "bfd.h"                /* Binary File Description */
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "target.h"
31 #include "language.h"
32 #include "demangle.h"
33
34 #include "gdb_string.h"
35 #include <errno.h>
36
37 /* For real-type printing in whatis_exp() */
38 extern int objectprint;         /* Controls looking up an object's derived type
39                                    using what we find in its vtables.  */
40
41 static void
42 ptype_command PARAMS ((char *, int));
43
44 static struct type *
45 ptype_eval PARAMS ((struct expression *));
46
47 static void
48 whatis_command PARAMS ((char *, int));
49
50 static void
51 whatis_exp PARAMS ((char *, int));
52
53 /* Print a description of a type TYPE in the form of a declaration of a
54    variable named VARSTRING.  (VARSTRING is demangled if necessary.)
55    Output goes to STREAM (via stdio).
56    If SHOW is positive, we show the contents of the outermost level
57    of structure even if there is a type name that could be used instead.
58    If SHOW is negative, we never show the details of elements' types.  */
59
60 void
61 type_print (type, varstring, stream, show)
62      struct type *type;
63      char *varstring;
64      GDB_FILE *stream;
65      int show;
66 {
67   LA_PRINT_TYPE (type, varstring, stream, show, 0);
68 }
69
70 /* Print type of EXP, or last thing in value history if EXP == NULL.
71    show is passed to type_print.  */
72
73 static void
74 whatis_exp (exp, show)
75      char *exp;
76      int show;
77 {
78   struct expression *expr;
79   register value_ptr val;
80   register struct cleanup *old_chain = NULL;
81   struct type * real_type = NULL;
82   int full = 0;
83   int top = -1;
84   int using_enc = 0;
85
86   if (exp)
87     {
88       expr = parse_expression (exp);
89       old_chain = make_cleanup ((make_cleanup_func) free_current_contents, 
90                                 &expr);
91       val = evaluate_type (expr);
92     }
93   else
94     val = access_value_history (0);
95
96   real_type = value_rtti_type (val, &full, &top, &using_enc);
97
98   printf_filtered ("type = ");
99
100   if (real_type && objectprint)
101     printf_filtered ("/* real type = %s%s */\n",
102                      TYPE_NAME (real_type),
103                      full ? "" : " (incomplete object)");
104   /* FIXME: maybe better to use type_print (real_type, "", gdb_stdout, -1); */
105
106   type_print (VALUE_TYPE (val), "", gdb_stdout, show);
107   printf_filtered ("\n");
108
109   if (exp)
110     do_cleanups (old_chain);
111 }
112
113 /* ARGSUSED */
114 static void
115 whatis_command (exp, from_tty)
116      char *exp;
117      int from_tty;
118 {
119   /* Most of the time users do not want to see all the fields
120      in a structure.  If they do they can use the "ptype" command.
121      Hence the "-1" below.  */
122   whatis_exp (exp, -1);
123 }
124
125 /* Simple subroutine for ptype_command.  */
126
127 static struct type *
128 ptype_eval (exp)
129      struct expression *exp;
130 {
131   if (exp->elts[0].opcode == OP_TYPE)
132     {
133       return (exp->elts[1].type);
134     }
135   else
136     {
137       return (NULL);
138     }
139 }
140
141 /* TYPENAME is either the name of a type, or an expression.  */
142
143 /* ARGSUSED */
144 static void
145 ptype_command (typename, from_tty)
146      char *typename;
147      int from_tty;
148 {
149   register struct type *type;
150   struct expression *expr;
151   register struct cleanup *old_chain;
152
153   if (typename == NULL)
154     {
155       /* Print type of last thing in value history. */
156       whatis_exp (typename, 1);
157     }
158   else
159     {
160       expr = parse_expression (typename);
161       old_chain = make_cleanup ((make_cleanup_func) free_current_contents, 
162                                 &expr);
163       type = ptype_eval (expr);
164       if (type != NULL)
165         {
166           /* User did "ptype <typename>" */
167           printf_filtered ("type = ");
168           type_print (type, "", gdb_stdout, 1);
169           printf_filtered ("\n");
170           do_cleanups (old_chain);
171         }
172       else
173         {
174           /* User did "ptype <symbolname>" */
175           do_cleanups (old_chain);
176           whatis_exp (typename, 1);
177         }
178     }
179 }
180
181 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
182    Used to print data from type structures in a specified type.  For example,
183    array bounds may be characters or booleans in some languages, and this
184    allows the ranges to be printed in their "natural" form rather than as
185    decimal integer values.
186
187    FIXME:  This is here simply because only the type printing routines
188    currently use it, and it wasn't clear if it really belonged somewhere
189    else (like printcmd.c).  There are a lot of other gdb routines that do
190    something similar, but they are generally concerned with printing values
191    that come from the inferior in target byte order and target size. */
192
193 void
194 print_type_scalar (type, val, stream)
195      struct type *type;
196      LONGEST val;
197      GDB_FILE *stream;
198 {
199   unsigned int i;
200   unsigned len;
201
202   CHECK_TYPEDEF (type);
203
204   switch (TYPE_CODE (type))
205     {
206
207     case TYPE_CODE_ENUM:
208       len = TYPE_NFIELDS (type);
209       for (i = 0; i < len; i++)
210         {
211           if (TYPE_FIELD_BITPOS (type, i) == val)
212             {
213               break;
214             }
215         }
216       if (i < len)
217         {
218           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
219         }
220       else
221         {
222           print_longest (stream, 'd', 0, val);
223         }
224       break;
225
226     case TYPE_CODE_INT:
227       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
228       break;
229
230     case TYPE_CODE_CHAR:
231       LA_PRINT_CHAR ((unsigned char) val, stream);
232       break;
233
234     case TYPE_CODE_BOOL:
235       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
236       break;
237
238     case TYPE_CODE_RANGE:
239       print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
240       return;
241
242     case TYPE_CODE_UNDEF:
243     case TYPE_CODE_PTR:
244     case TYPE_CODE_ARRAY:
245     case TYPE_CODE_STRUCT:
246     case TYPE_CODE_UNION:
247     case TYPE_CODE_FUNC:
248     case TYPE_CODE_FLT:
249     case TYPE_CODE_VOID:
250     case TYPE_CODE_SET:
251     case TYPE_CODE_STRING:
252     case TYPE_CODE_ERROR:
253     case TYPE_CODE_MEMBER:
254     case TYPE_CODE_METHOD:
255     case TYPE_CODE_REF:
256       error ("internal error: unhandled type in print_type_scalar");
257       break;
258
259     default:
260       error ("Invalid type code in symbol table.");
261     }
262   gdb_flush (stream);
263 }
264
265 #if MAINTENANCE_CMDS
266
267 /* Dump details of a type specified either directly or indirectly.
268    Uses the same sort of type lookup mechanism as ptype_command()
269    and whatis_command(). */
270
271 void
272 maintenance_print_type (typename, from_tty)
273      char *typename;
274      int from_tty;
275 {
276   register value_ptr val;
277   register struct type *type;
278   register struct cleanup *old_chain;
279   struct expression *expr;
280
281   if (typename != NULL)
282   {
283     expr = parse_expression (typename);
284     old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
285     if (expr -> elts[0].opcode == OP_TYPE)
286       {
287         /* The user expression names a type directly, just use that type. */
288         type = expr -> elts[1].type;
289       }
290     else
291       {
292         /* The user expression may name a type indirectly by naming an
293            object of that type.  Find that indirectly named type. */
294         val = evaluate_type (expr);
295         type = VALUE_TYPE (val);
296       }
297     if (type != NULL)
298       {
299         recursive_dump_type (type, 0);
300       }
301     do_cleanups (old_chain);
302   }
303 }
304
305 #endif  /* MAINTENANCE_CMDS */
306
307 \f
308 void
309 _initialize_typeprint ()
310 {
311
312   add_com ("ptype", class_vars, ptype_command,
313            "Print definition of type TYPE.\n\
314 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
315 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
316 The selected stack frame's lexical context is used to look up the name.");
317
318   add_com ("whatis", class_vars, whatis_command,
319            "Print data type of expression EXP.");
320
321 }