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