gdb
[external/binutils.git] / gdb / typeprint.c
1 /* Language independent support for printing types for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 1999,
4    2000, 2001, 2003, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_obstack.h"
23 #include "bfd.h"                /* Binary File Description */
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "gdbcore.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "target.h"
32 #include "language.h"
33 #include "cp-abi.h"
34 #include "typeprint.h"
35 #include "gdb_string.h"
36 #include <errno.h>
37
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.  */
41
42 extern void _initialize_typeprint (void);
43
44 static void ptype_command (char *, int);
45
46 static void whatis_command (char *, int);
47
48 static void whatis_exp (char *, int);
49
50
51 /* Print a description of a type in the format of a 
52    typedef for the current language.
53    NEW is the new name for a type TYPE. */
54
55 void
56 typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
57 {
58   LA_PRINT_TYPEDEF (type, new, stream);
59 }
60
61 /* The default way to print a typedef.  */
62
63 void
64 default_print_typedef (struct type *type, struct symbol *new_symbol,
65                        struct ui_file *stream)
66 {
67   error (_("Language not supported."));
68 }
69
70 /* Print a description of a type TYPE in the form of a declaration of a
71    variable named VARSTRING.  (VARSTRING is demangled if necessary.)
72    Output goes to STREAM (via stdio).
73    If SHOW is positive, we show the contents of the outermost level
74    of structure even if there is a type name that could be used instead.
75    If SHOW is negative, we never show the details of elements' types.  */
76
77 void
78 type_print (struct type *type, char *varstring, struct ui_file *stream,
79             int show)
80 {
81   LA_PRINT_TYPE (type, varstring, stream, show, 0);
82 }
83
84 /* Print type of EXP, or last thing in value history if EXP == NULL.
85    show is passed to type_print.  */
86
87 static void
88 whatis_exp (char *exp, int show)
89 {
90   struct expression *expr;
91   struct value *val;
92   struct cleanup *old_chain = NULL;
93   struct type *real_type = NULL;
94   struct type *type;
95   int full = 0;
96   int top = -1;
97   int using_enc = 0;
98
99   if (exp)
100     {
101       expr = parse_expression (exp);
102       old_chain = make_cleanup (free_current_contents, &expr);
103       val = evaluate_type (expr);
104     }
105   else
106     val = access_value_history (0);
107
108   type = value_type (val);
109
110   if (objectprint)
111     {
112       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
113            || (TYPE_CODE (type) == TYPE_CODE_REF))
114           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
115         {
116           real_type = value_rtti_target_type (val, &full, &top, &using_enc);
117           if (real_type)
118             {
119               if (TYPE_CODE (type) == TYPE_CODE_PTR)
120                 real_type = lookup_pointer_type (real_type);
121               else
122                 real_type = lookup_reference_type (real_type);
123             }
124         }
125       else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
126         real_type = value_rtti_type (val, &full, &top, &using_enc);
127     }
128
129   printf_filtered ("type = ");
130
131   if (real_type)
132     {
133       printf_filtered ("/* real type = ");
134       type_print (real_type, "", gdb_stdout, -1);
135       if (! full)
136         printf_filtered (" (incomplete object)");
137       printf_filtered (" */\n");    
138     }
139
140   type_print (type, "", gdb_stdout, show);
141   printf_filtered ("\n");
142
143   if (exp)
144     do_cleanups (old_chain);
145 }
146
147 static void
148 whatis_command (char *exp, int from_tty)
149 {
150   /* Most of the time users do not want to see all the fields
151      in a structure.  If they do they can use the "ptype" command.
152      Hence the "-1" below.  */
153   whatis_exp (exp, -1);
154 }
155
156 /* TYPENAME is either the name of a type, or an expression.  */
157
158 static void
159 ptype_command (char *typename, int from_tty)
160 {
161   whatis_exp (typename, 1);
162 }
163
164 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
165    Used to print data from type structures in a specified type.  For example,
166    array bounds may be characters or booleans in some languages, and this
167    allows the ranges to be printed in their "natural" form rather than as
168    decimal integer values.
169
170    FIXME:  This is here simply because only the type printing routines
171    currently use it, and it wasn't clear if it really belonged somewhere
172    else (like printcmd.c).  There are a lot of other gdb routines that do
173    something similar, but they are generally concerned with printing values
174    that come from the inferior in target byte order and target size.  */
175
176 void
177 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
178 {
179   unsigned int i;
180   unsigned len;
181
182   CHECK_TYPEDEF (type);
183
184   switch (TYPE_CODE (type))
185     {
186
187     case TYPE_CODE_ENUM:
188       len = TYPE_NFIELDS (type);
189       for (i = 0; i < len; i++)
190         {
191           if (TYPE_FIELD_BITPOS (type, i) == val)
192             {
193               break;
194             }
195         }
196       if (i < len)
197         {
198           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
199         }
200       else
201         {
202           print_longest (stream, 'd', 0, val);
203         }
204       break;
205
206     case TYPE_CODE_INT:
207       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
208       break;
209
210     case TYPE_CODE_CHAR:
211       LA_PRINT_CHAR ((unsigned char) val, stream);
212       break;
213
214     case TYPE_CODE_BOOL:
215       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
216       break;
217
218     case TYPE_CODE_RANGE:
219       print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
220       return;
221
222     case TYPE_CODE_UNDEF:
223     case TYPE_CODE_PTR:
224     case TYPE_CODE_ARRAY:
225     case TYPE_CODE_STRUCT:
226     case TYPE_CODE_UNION:
227     case TYPE_CODE_FUNC:
228     case TYPE_CODE_FLT:
229     case TYPE_CODE_VOID:
230     case TYPE_CODE_SET:
231     case TYPE_CODE_STRING:
232     case TYPE_CODE_ERROR:
233     case TYPE_CODE_MEMBERPTR:
234     case TYPE_CODE_METHODPTR:
235     case TYPE_CODE_METHOD:
236     case TYPE_CODE_REF:
237     case TYPE_CODE_NAMESPACE:
238       error (_("internal error: unhandled type in print_type_scalar"));
239       break;
240
241     default:
242       error (_("Invalid type code in symbol table."));
243     }
244   gdb_flush (stream);
245 }
246
247 /* Dump details of a type specified either directly or indirectly.
248    Uses the same sort of type lookup mechanism as ptype_command()
249    and whatis_command().  */
250
251 void
252 maintenance_print_type (char *typename, int from_tty)
253 {
254   struct value *val;
255   struct type *type;
256   struct cleanup *old_chain;
257   struct expression *expr;
258
259   if (typename != NULL)
260     {
261       expr = parse_expression (typename);
262       old_chain = make_cleanup (free_current_contents, &expr);
263       if (expr->elts[0].opcode == OP_TYPE)
264         {
265           /* The user expression names a type directly, just use that type. */
266           type = expr->elts[1].type;
267         }
268       else
269         {
270           /* The user expression may name a type indirectly by naming an
271              object of that type.  Find that indirectly named type. */
272           val = evaluate_type (expr);
273           type = value_type (val);
274         }
275       if (type != NULL)
276         {
277           recursive_dump_type (type, 0);
278         }
279       do_cleanups (old_chain);
280     }
281 }
282 \f
283
284 void
285 _initialize_typeprint (void)
286 {
287   add_com ("ptype", class_vars, ptype_command, _("\
288 Print definition of type TYPE.\n\
289 Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
290 or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
291 The selected stack frame's lexical context is used to look up the name."));
292
293   add_com ("whatis", class_vars, whatis_command,
294            _("Print data type of expression EXP."));
295 }