15796c8dcSSimon Schubert /* Language independent support for printing types for GDB, the GNU debugger.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdb_obstack.h"
225796c8dcSSimon Schubert #include "bfd.h" /* Binary File Description */
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "expression.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "command.h"
295796c8dcSSimon Schubert #include "gdbcmd.h"
305796c8dcSSimon Schubert #include "target.h"
315796c8dcSSimon Schubert #include "language.h"
325796c8dcSSimon Schubert #include "cp-abi.h"
335796c8dcSSimon Schubert #include "typeprint.h"
345796c8dcSSimon Schubert #include "gdb_string.h"
355796c8dcSSimon Schubert #include "exceptions.h"
365796c8dcSSimon Schubert #include "valprint.h"
375796c8dcSSimon Schubert #include <errno.h>
38*ef5ccd6cSJohn Marino #include <ctype.h>
39*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
40*ef5ccd6cSJohn Marino #include "python/python.h"
41*ef5ccd6cSJohn Marino #include "completer.h"
425796c8dcSSimon Schubert
435796c8dcSSimon Schubert extern void _initialize_typeprint (void);
445796c8dcSSimon Schubert
455796c8dcSSimon Schubert static void ptype_command (char *, int);
465796c8dcSSimon Schubert
475796c8dcSSimon Schubert static void whatis_command (char *, int);
485796c8dcSSimon Schubert
495796c8dcSSimon Schubert static void whatis_exp (char *, int);
505796c8dcSSimon Schubert
51*ef5ccd6cSJohn Marino const struct type_print_options type_print_raw_options =
52*ef5ccd6cSJohn Marino {
53*ef5ccd6cSJohn Marino 1, /* raw */
54*ef5ccd6cSJohn Marino 1, /* print_methods */
55*ef5ccd6cSJohn Marino 1, /* print_typedefs */
56*ef5ccd6cSJohn Marino NULL, /* local_typedefs */
57*ef5ccd6cSJohn Marino NULL, /* global_table */
58*ef5ccd6cSJohn Marino NULL /* global_printers */
59*ef5ccd6cSJohn Marino };
60*ef5ccd6cSJohn Marino
61*ef5ccd6cSJohn Marino /* The default flags for 'ptype' and 'whatis'. */
62*ef5ccd6cSJohn Marino
63*ef5ccd6cSJohn Marino static struct type_print_options default_ptype_flags =
64*ef5ccd6cSJohn Marino {
65*ef5ccd6cSJohn Marino 0, /* raw */
66*ef5ccd6cSJohn Marino 1, /* print_methods */
67*ef5ccd6cSJohn Marino 1, /* print_typedefs */
68*ef5ccd6cSJohn Marino NULL, /* local_typedefs */
69*ef5ccd6cSJohn Marino NULL, /* global_table */
70*ef5ccd6cSJohn Marino NULL /* global_printers */
71*ef5ccd6cSJohn Marino };
72*ef5ccd6cSJohn Marino
73*ef5ccd6cSJohn Marino
74*ef5ccd6cSJohn Marino
75*ef5ccd6cSJohn Marino /* A hash table holding typedef_field objects. This is more
76*ef5ccd6cSJohn Marino complicated than an ordinary hash because it must also track the
77*ef5ccd6cSJohn Marino lifetime of some -- but not all -- of the contained objects. */
78*ef5ccd6cSJohn Marino
79*ef5ccd6cSJohn Marino struct typedef_hash_table
80*ef5ccd6cSJohn Marino {
81*ef5ccd6cSJohn Marino /* The actual hash table. */
82*ef5ccd6cSJohn Marino htab_t table;
83*ef5ccd6cSJohn Marino
84*ef5ccd6cSJohn Marino /* Storage for typedef_field objects that must be synthesized. */
85*ef5ccd6cSJohn Marino struct obstack storage;
86*ef5ccd6cSJohn Marino };
87*ef5ccd6cSJohn Marino
88*ef5ccd6cSJohn Marino /* A hash function for a typedef_field. */
89*ef5ccd6cSJohn Marino
90*ef5ccd6cSJohn Marino static hashval_t
hash_typedef_field(const void * p)91*ef5ccd6cSJohn Marino hash_typedef_field (const void *p)
92*ef5ccd6cSJohn Marino {
93*ef5ccd6cSJohn Marino const struct typedef_field *tf = p;
94*ef5ccd6cSJohn Marino struct type *t = check_typedef (tf->type);
95*ef5ccd6cSJohn Marino
96*ef5ccd6cSJohn Marino return htab_hash_string (TYPE_SAFE_NAME (t));
97*ef5ccd6cSJohn Marino }
98*ef5ccd6cSJohn Marino
99*ef5ccd6cSJohn Marino /* An equality function for a typedef field. */
100*ef5ccd6cSJohn Marino
101*ef5ccd6cSJohn Marino static int
eq_typedef_field(const void * a,const void * b)102*ef5ccd6cSJohn Marino eq_typedef_field (const void *a, const void *b)
103*ef5ccd6cSJohn Marino {
104*ef5ccd6cSJohn Marino const struct typedef_field *tfa = a;
105*ef5ccd6cSJohn Marino const struct typedef_field *tfb = b;
106*ef5ccd6cSJohn Marino
107*ef5ccd6cSJohn Marino return types_equal (tfa->type, tfb->type);
108*ef5ccd6cSJohn Marino }
109*ef5ccd6cSJohn Marino
110*ef5ccd6cSJohn Marino /* Add typedefs from T to the hash table TABLE. */
111*ef5ccd6cSJohn Marino
112*ef5ccd6cSJohn Marino void
recursively_update_typedef_hash(struct typedef_hash_table * table,struct type * t)113*ef5ccd6cSJohn Marino recursively_update_typedef_hash (struct typedef_hash_table *table,
114*ef5ccd6cSJohn Marino struct type *t)
115*ef5ccd6cSJohn Marino {
116*ef5ccd6cSJohn Marino int i;
117*ef5ccd6cSJohn Marino
118*ef5ccd6cSJohn Marino if (table == NULL)
119*ef5ccd6cSJohn Marino return;
120*ef5ccd6cSJohn Marino
121*ef5ccd6cSJohn Marino for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
122*ef5ccd6cSJohn Marino {
123*ef5ccd6cSJohn Marino struct typedef_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
124*ef5ccd6cSJohn Marino void **slot;
125*ef5ccd6cSJohn Marino
126*ef5ccd6cSJohn Marino slot = htab_find_slot (table->table, tdef, INSERT);
127*ef5ccd6cSJohn Marino /* Only add a given typedef name once. Really this shouldn't
128*ef5ccd6cSJohn Marino happen; but it is safe enough to do the updates breadth-first
129*ef5ccd6cSJohn Marino and thus use the most specific typedef. */
130*ef5ccd6cSJohn Marino if (*slot == NULL)
131*ef5ccd6cSJohn Marino *slot = tdef;
132*ef5ccd6cSJohn Marino }
133*ef5ccd6cSJohn Marino
134*ef5ccd6cSJohn Marino /* Recurse into superclasses. */
135*ef5ccd6cSJohn Marino for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
136*ef5ccd6cSJohn Marino recursively_update_typedef_hash (table, TYPE_BASECLASS (t, i));
137*ef5ccd6cSJohn Marino }
138*ef5ccd6cSJohn Marino
139*ef5ccd6cSJohn Marino /* Add template parameters from T to the typedef hash TABLE. */
140*ef5ccd6cSJohn Marino
141*ef5ccd6cSJohn Marino void
add_template_parameters(struct typedef_hash_table * table,struct type * t)142*ef5ccd6cSJohn Marino add_template_parameters (struct typedef_hash_table *table, struct type *t)
143*ef5ccd6cSJohn Marino {
144*ef5ccd6cSJohn Marino int i;
145*ef5ccd6cSJohn Marino
146*ef5ccd6cSJohn Marino if (table == NULL)
147*ef5ccd6cSJohn Marino return;
148*ef5ccd6cSJohn Marino
149*ef5ccd6cSJohn Marino for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
150*ef5ccd6cSJohn Marino {
151*ef5ccd6cSJohn Marino struct typedef_field *tf;
152*ef5ccd6cSJohn Marino void **slot;
153*ef5ccd6cSJohn Marino
154*ef5ccd6cSJohn Marino /* We only want type-valued template parameters in the hash. */
155*ef5ccd6cSJohn Marino if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
156*ef5ccd6cSJohn Marino continue;
157*ef5ccd6cSJohn Marino
158*ef5ccd6cSJohn Marino tf = XOBNEW (&table->storage, struct typedef_field);
159*ef5ccd6cSJohn Marino tf->name = SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t, i));
160*ef5ccd6cSJohn Marino tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i));
161*ef5ccd6cSJohn Marino
162*ef5ccd6cSJohn Marino slot = htab_find_slot (table->table, tf, INSERT);
163*ef5ccd6cSJohn Marino if (*slot == NULL)
164*ef5ccd6cSJohn Marino *slot = tf;
165*ef5ccd6cSJohn Marino }
166*ef5ccd6cSJohn Marino }
167*ef5ccd6cSJohn Marino
168*ef5ccd6cSJohn Marino /* Create a new typedef-lookup hash table. */
169*ef5ccd6cSJohn Marino
170*ef5ccd6cSJohn Marino struct typedef_hash_table *
create_typedef_hash(void)171*ef5ccd6cSJohn Marino create_typedef_hash (void)
172*ef5ccd6cSJohn Marino {
173*ef5ccd6cSJohn Marino struct typedef_hash_table *result;
174*ef5ccd6cSJohn Marino
175*ef5ccd6cSJohn Marino result = XNEW (struct typedef_hash_table);
176*ef5ccd6cSJohn Marino result->table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
177*ef5ccd6cSJohn Marino NULL, xcalloc, xfree);
178*ef5ccd6cSJohn Marino obstack_init (&result->storage);
179*ef5ccd6cSJohn Marino
180*ef5ccd6cSJohn Marino return result;
181*ef5ccd6cSJohn Marino }
182*ef5ccd6cSJohn Marino
183*ef5ccd6cSJohn Marino /* Free a typedef field table. */
184*ef5ccd6cSJohn Marino
185*ef5ccd6cSJohn Marino void
free_typedef_hash(struct typedef_hash_table * table)186*ef5ccd6cSJohn Marino free_typedef_hash (struct typedef_hash_table *table)
187*ef5ccd6cSJohn Marino {
188*ef5ccd6cSJohn Marino if (table != NULL)
189*ef5ccd6cSJohn Marino {
190*ef5ccd6cSJohn Marino htab_delete (table->table);
191*ef5ccd6cSJohn Marino obstack_free (&table->storage, NULL);
192*ef5ccd6cSJohn Marino xfree (table);
193*ef5ccd6cSJohn Marino }
194*ef5ccd6cSJohn Marino }
195*ef5ccd6cSJohn Marino
196*ef5ccd6cSJohn Marino /* A cleanup for freeing a typedef_hash_table. */
197*ef5ccd6cSJohn Marino
198*ef5ccd6cSJohn Marino static void
do_free_typedef_hash(void * arg)199*ef5ccd6cSJohn Marino do_free_typedef_hash (void *arg)
200*ef5ccd6cSJohn Marino {
201*ef5ccd6cSJohn Marino free_typedef_hash (arg);
202*ef5ccd6cSJohn Marino }
203*ef5ccd6cSJohn Marino
204*ef5ccd6cSJohn Marino /* Return a new cleanup that frees TABLE. */
205*ef5ccd6cSJohn Marino
206*ef5ccd6cSJohn Marino struct cleanup *
make_cleanup_free_typedef_hash(struct typedef_hash_table * table)207*ef5ccd6cSJohn Marino make_cleanup_free_typedef_hash (struct typedef_hash_table *table)
208*ef5ccd6cSJohn Marino {
209*ef5ccd6cSJohn Marino return make_cleanup (do_free_typedef_hash, table);
210*ef5ccd6cSJohn Marino }
211*ef5ccd6cSJohn Marino
212*ef5ccd6cSJohn Marino /* Helper function for copy_typedef_hash. */
213*ef5ccd6cSJohn Marino
214*ef5ccd6cSJohn Marino static int
copy_typedef_hash_element(void ** slot,void * nt)215*ef5ccd6cSJohn Marino copy_typedef_hash_element (void **slot, void *nt)
216*ef5ccd6cSJohn Marino {
217*ef5ccd6cSJohn Marino htab_t new_table = nt;
218*ef5ccd6cSJohn Marino void **new_slot;
219*ef5ccd6cSJohn Marino
220*ef5ccd6cSJohn Marino new_slot = htab_find_slot (new_table, *slot, INSERT);
221*ef5ccd6cSJohn Marino if (*new_slot == NULL)
222*ef5ccd6cSJohn Marino *new_slot = *slot;
223*ef5ccd6cSJohn Marino
224*ef5ccd6cSJohn Marino return 1;
225*ef5ccd6cSJohn Marino }
226*ef5ccd6cSJohn Marino
227*ef5ccd6cSJohn Marino /* Copy a typedef hash. */
228*ef5ccd6cSJohn Marino
229*ef5ccd6cSJohn Marino struct typedef_hash_table *
copy_typedef_hash(struct typedef_hash_table * table)230*ef5ccd6cSJohn Marino copy_typedef_hash (struct typedef_hash_table *table)
231*ef5ccd6cSJohn Marino {
232*ef5ccd6cSJohn Marino struct typedef_hash_table *result;
233*ef5ccd6cSJohn Marino
234*ef5ccd6cSJohn Marino if (table == NULL)
235*ef5ccd6cSJohn Marino return NULL;
236*ef5ccd6cSJohn Marino
237*ef5ccd6cSJohn Marino result = create_typedef_hash ();
238*ef5ccd6cSJohn Marino htab_traverse_noresize (table->table, copy_typedef_hash_element,
239*ef5ccd6cSJohn Marino result->table);
240*ef5ccd6cSJohn Marino return result;
241*ef5ccd6cSJohn Marino }
242*ef5ccd6cSJohn Marino
243*ef5ccd6cSJohn Marino /* A cleanup to free the global typedef hash. */
244*ef5ccd6cSJohn Marino
245*ef5ccd6cSJohn Marino static void
do_free_global_table(void * arg)246*ef5ccd6cSJohn Marino do_free_global_table (void *arg)
247*ef5ccd6cSJohn Marino {
248*ef5ccd6cSJohn Marino struct type_print_options *flags = arg;
249*ef5ccd6cSJohn Marino
250*ef5ccd6cSJohn Marino free_typedef_hash (flags->global_typedefs);
251*ef5ccd6cSJohn Marino free_type_printers (flags->global_printers);
252*ef5ccd6cSJohn Marino }
253*ef5ccd6cSJohn Marino
254*ef5ccd6cSJohn Marino /* Create the global typedef hash. */
255*ef5ccd6cSJohn Marino
256*ef5ccd6cSJohn Marino static struct cleanup *
create_global_typedef_table(struct type_print_options * flags)257*ef5ccd6cSJohn Marino create_global_typedef_table (struct type_print_options *flags)
258*ef5ccd6cSJohn Marino {
259*ef5ccd6cSJohn Marino gdb_assert (flags->global_typedefs == NULL && flags->global_printers == NULL);
260*ef5ccd6cSJohn Marino flags->global_typedefs = create_typedef_hash ();
261*ef5ccd6cSJohn Marino flags->global_printers = start_type_printers ();
262*ef5ccd6cSJohn Marino return make_cleanup (do_free_global_table, flags);
263*ef5ccd6cSJohn Marino }
264*ef5ccd6cSJohn Marino
265*ef5ccd6cSJohn Marino /* Look up the type T in the global typedef hash. If it is found,
266*ef5ccd6cSJohn Marino return the typedef name. If it is not found, apply the
267*ef5ccd6cSJohn Marino type-printers, if any, given by start_type_printers and return the
268*ef5ccd6cSJohn Marino result. A NULL return means that the name was not found. */
269*ef5ccd6cSJohn Marino
270*ef5ccd6cSJohn Marino static const char *
find_global_typedef(const struct type_print_options * flags,struct type * t)271*ef5ccd6cSJohn Marino find_global_typedef (const struct type_print_options *flags,
272*ef5ccd6cSJohn Marino struct type *t)
273*ef5ccd6cSJohn Marino {
274*ef5ccd6cSJohn Marino char *applied;
275*ef5ccd6cSJohn Marino void **slot;
276*ef5ccd6cSJohn Marino struct typedef_field tf, *new_tf;
277*ef5ccd6cSJohn Marino
278*ef5ccd6cSJohn Marino if (flags->global_typedefs == NULL)
279*ef5ccd6cSJohn Marino return NULL;
280*ef5ccd6cSJohn Marino
281*ef5ccd6cSJohn Marino tf.name = NULL;
282*ef5ccd6cSJohn Marino tf.type = t;
283*ef5ccd6cSJohn Marino
284*ef5ccd6cSJohn Marino slot = htab_find_slot (flags->global_typedefs->table, &tf, INSERT);
285*ef5ccd6cSJohn Marino if (*slot != NULL)
286*ef5ccd6cSJohn Marino {
287*ef5ccd6cSJohn Marino new_tf = *slot;
288*ef5ccd6cSJohn Marino return new_tf->name;
289*ef5ccd6cSJohn Marino }
290*ef5ccd6cSJohn Marino
291*ef5ccd6cSJohn Marino /* Put an entry into the hash table now, in case apply_type_printers
292*ef5ccd6cSJohn Marino recurses. */
293*ef5ccd6cSJohn Marino new_tf = XOBNEW (&flags->global_typedefs->storage, struct typedef_field);
294*ef5ccd6cSJohn Marino new_tf->name = NULL;
295*ef5ccd6cSJohn Marino new_tf->type = t;
296*ef5ccd6cSJohn Marino
297*ef5ccd6cSJohn Marino *slot = new_tf;
298*ef5ccd6cSJohn Marino
299*ef5ccd6cSJohn Marino applied = apply_type_printers (flags->global_printers, t);
300*ef5ccd6cSJohn Marino
301*ef5ccd6cSJohn Marino if (applied != NULL)
302*ef5ccd6cSJohn Marino {
303*ef5ccd6cSJohn Marino new_tf->name = obstack_copy0 (&flags->global_typedefs->storage, applied,
304*ef5ccd6cSJohn Marino strlen (applied));
305*ef5ccd6cSJohn Marino xfree (applied);
306*ef5ccd6cSJohn Marino }
307*ef5ccd6cSJohn Marino
308*ef5ccd6cSJohn Marino return new_tf->name;
309*ef5ccd6cSJohn Marino }
310*ef5ccd6cSJohn Marino
311*ef5ccd6cSJohn Marino /* Look up the type T in the typedef hash table in with FLAGS. If T
312*ef5ccd6cSJohn Marino is in the table, return its short (class-relative) typedef name.
313*ef5ccd6cSJohn Marino Otherwise return NULL. If the table is NULL, this always returns
314*ef5ccd6cSJohn Marino NULL. */
315*ef5ccd6cSJohn Marino
316*ef5ccd6cSJohn Marino const char *
find_typedef_in_hash(const struct type_print_options * flags,struct type * t)317*ef5ccd6cSJohn Marino find_typedef_in_hash (const struct type_print_options *flags, struct type *t)
318*ef5ccd6cSJohn Marino {
319*ef5ccd6cSJohn Marino if (flags->local_typedefs != NULL)
320*ef5ccd6cSJohn Marino {
321*ef5ccd6cSJohn Marino struct typedef_field tf, *found;
322*ef5ccd6cSJohn Marino
323*ef5ccd6cSJohn Marino tf.name = NULL;
324*ef5ccd6cSJohn Marino tf.type = t;
325*ef5ccd6cSJohn Marino found = htab_find (flags->local_typedefs->table, &tf);
326*ef5ccd6cSJohn Marino
327*ef5ccd6cSJohn Marino if (found != NULL)
328*ef5ccd6cSJohn Marino return found->name;
329*ef5ccd6cSJohn Marino }
330*ef5ccd6cSJohn Marino
331*ef5ccd6cSJohn Marino return find_global_typedef (flags, t);
332*ef5ccd6cSJohn Marino }
333*ef5ccd6cSJohn Marino
334*ef5ccd6cSJohn Marino
3355796c8dcSSimon Schubert
3365796c8dcSSimon Schubert /* Print a description of a type in the format of a
3375796c8dcSSimon Schubert typedef for the current language.
3385796c8dcSSimon Schubert NEW is the new name for a type TYPE. */
3395796c8dcSSimon Schubert
3405796c8dcSSimon Schubert void
typedef_print(struct type * type,struct symbol * new,struct ui_file * stream)3415796c8dcSSimon Schubert typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
3425796c8dcSSimon Schubert {
3435796c8dcSSimon Schubert LA_PRINT_TYPEDEF (type, new, stream);
3445796c8dcSSimon Schubert }
3455796c8dcSSimon Schubert
3465796c8dcSSimon Schubert /* The default way to print a typedef. */
3475796c8dcSSimon Schubert
3485796c8dcSSimon Schubert void
default_print_typedef(struct type * type,struct symbol * new_symbol,struct ui_file * stream)3495796c8dcSSimon Schubert default_print_typedef (struct type *type, struct symbol *new_symbol,
3505796c8dcSSimon Schubert struct ui_file *stream)
3515796c8dcSSimon Schubert {
3525796c8dcSSimon Schubert error (_("Language not supported."));
3535796c8dcSSimon Schubert }
3545796c8dcSSimon Schubert
3555796c8dcSSimon Schubert /* Print a description of a type TYPE in the form of a declaration of a
3565796c8dcSSimon Schubert variable named VARSTRING. (VARSTRING is demangled if necessary.)
3575796c8dcSSimon Schubert Output goes to STREAM (via stdio).
3585796c8dcSSimon Schubert If SHOW is positive, we show the contents of the outermost level
3595796c8dcSSimon Schubert of structure even if there is a type name that could be used instead.
3605796c8dcSSimon Schubert If SHOW is negative, we never show the details of elements' types. */
3615796c8dcSSimon Schubert
3625796c8dcSSimon Schubert void
type_print(struct type * type,const char * varstring,struct ui_file * stream,int show)363*ef5ccd6cSJohn Marino type_print (struct type *type, const char *varstring, struct ui_file *stream,
3645796c8dcSSimon Schubert int show)
3655796c8dcSSimon Schubert {
366*ef5ccd6cSJohn Marino LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
3675796c8dcSSimon Schubert }
3685796c8dcSSimon Schubert
3695796c8dcSSimon Schubert /* Print TYPE to a string, returning it. The caller is responsible for
3705796c8dcSSimon Schubert freeing the string. */
3715796c8dcSSimon Schubert
3725796c8dcSSimon Schubert char *
type_to_string(struct type * type)3735796c8dcSSimon Schubert type_to_string (struct type *type)
3745796c8dcSSimon Schubert {
3755796c8dcSSimon Schubert char *s = NULL;
3765796c8dcSSimon Schubert struct ui_file *stb;
3775796c8dcSSimon Schubert struct cleanup *old_chain;
3785796c8dcSSimon Schubert volatile struct gdb_exception except;
3795796c8dcSSimon Schubert
3805796c8dcSSimon Schubert stb = mem_fileopen ();
3815796c8dcSSimon Schubert old_chain = make_cleanup_ui_file_delete (stb);
3825796c8dcSSimon Schubert
3835796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ALL)
3845796c8dcSSimon Schubert {
3855796c8dcSSimon Schubert type_print (type, "", stb, -1);
3865796c8dcSSimon Schubert s = ui_file_xstrdup (stb, NULL);
3875796c8dcSSimon Schubert }
3885796c8dcSSimon Schubert if (except.reason < 0)
3895796c8dcSSimon Schubert s = NULL;
3905796c8dcSSimon Schubert
3915796c8dcSSimon Schubert do_cleanups (old_chain);
3925796c8dcSSimon Schubert
3935796c8dcSSimon Schubert return s;
3945796c8dcSSimon Schubert }
3955796c8dcSSimon Schubert
3965796c8dcSSimon Schubert /* Print type of EXP, or last thing in value history if EXP == NULL.
3975796c8dcSSimon Schubert show is passed to type_print. */
3985796c8dcSSimon Schubert
3995796c8dcSSimon Schubert static void
whatis_exp(char * exp,int show)4005796c8dcSSimon Schubert whatis_exp (char *exp, int show)
4015796c8dcSSimon Schubert {
4025796c8dcSSimon Schubert struct expression *expr;
4035796c8dcSSimon Schubert struct value *val;
404*ef5ccd6cSJohn Marino struct cleanup *old_chain;
4055796c8dcSSimon Schubert struct type *real_type = NULL;
4065796c8dcSSimon Schubert struct type *type;
4075796c8dcSSimon Schubert int full = 0;
4085796c8dcSSimon Schubert int top = -1;
4095796c8dcSSimon Schubert int using_enc = 0;
4105796c8dcSSimon Schubert struct value_print_options opts;
411*ef5ccd6cSJohn Marino struct type_print_options flags = default_ptype_flags;
412*ef5ccd6cSJohn Marino
413*ef5ccd6cSJohn Marino old_chain = make_cleanup (null_cleanup, NULL);
4145796c8dcSSimon Schubert
4155796c8dcSSimon Schubert if (exp)
4165796c8dcSSimon Schubert {
417*ef5ccd6cSJohn Marino if (*exp == '/')
418*ef5ccd6cSJohn Marino {
419*ef5ccd6cSJohn Marino int seen_one = 0;
420*ef5ccd6cSJohn Marino
421*ef5ccd6cSJohn Marino for (++exp; *exp && !isspace (*exp); ++exp)
422*ef5ccd6cSJohn Marino {
423*ef5ccd6cSJohn Marino switch (*exp)
424*ef5ccd6cSJohn Marino {
425*ef5ccd6cSJohn Marino case 'r':
426*ef5ccd6cSJohn Marino flags.raw = 1;
427*ef5ccd6cSJohn Marino break;
428*ef5ccd6cSJohn Marino case 'm':
429*ef5ccd6cSJohn Marino flags.print_methods = 0;
430*ef5ccd6cSJohn Marino break;
431*ef5ccd6cSJohn Marino case 'M':
432*ef5ccd6cSJohn Marino flags.print_methods = 1;
433*ef5ccd6cSJohn Marino break;
434*ef5ccd6cSJohn Marino case 't':
435*ef5ccd6cSJohn Marino flags.print_typedefs = 0;
436*ef5ccd6cSJohn Marino break;
437*ef5ccd6cSJohn Marino case 'T':
438*ef5ccd6cSJohn Marino flags.print_typedefs = 1;
439*ef5ccd6cSJohn Marino break;
440*ef5ccd6cSJohn Marino default:
441*ef5ccd6cSJohn Marino error (_("unrecognized flag '%c'"), *exp);
442*ef5ccd6cSJohn Marino }
443*ef5ccd6cSJohn Marino seen_one = 1;
444*ef5ccd6cSJohn Marino }
445*ef5ccd6cSJohn Marino
446*ef5ccd6cSJohn Marino if (!*exp && !seen_one)
447*ef5ccd6cSJohn Marino error (_("flag expected"));
448*ef5ccd6cSJohn Marino if (!isspace (*exp))
449*ef5ccd6cSJohn Marino error (_("expected space after format"));
450*ef5ccd6cSJohn Marino exp = skip_spaces (exp);
451*ef5ccd6cSJohn Marino }
452*ef5ccd6cSJohn Marino
4535796c8dcSSimon Schubert expr = parse_expression (exp);
454*ef5ccd6cSJohn Marino make_cleanup (free_current_contents, &expr);
4555796c8dcSSimon Schubert val = evaluate_type (expr);
4565796c8dcSSimon Schubert }
4575796c8dcSSimon Schubert else
4585796c8dcSSimon Schubert val = access_value_history (0);
4595796c8dcSSimon Schubert
4605796c8dcSSimon Schubert type = value_type (val);
4615796c8dcSSimon Schubert
4625796c8dcSSimon Schubert get_user_print_options (&opts);
4635796c8dcSSimon Schubert if (opts.objectprint)
4645796c8dcSSimon Schubert {
4655796c8dcSSimon Schubert if (((TYPE_CODE (type) == TYPE_CODE_PTR)
4665796c8dcSSimon Schubert || (TYPE_CODE (type) == TYPE_CODE_REF))
4675796c8dcSSimon Schubert && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
468*ef5ccd6cSJohn Marino real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
4695796c8dcSSimon Schubert else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
4705796c8dcSSimon Schubert real_type = value_rtti_type (val, &full, &top, &using_enc);
4715796c8dcSSimon Schubert }
4725796c8dcSSimon Schubert
4735796c8dcSSimon Schubert printf_filtered ("type = ");
4745796c8dcSSimon Schubert
475*ef5ccd6cSJohn Marino if (!flags.raw)
476*ef5ccd6cSJohn Marino create_global_typedef_table (&flags);
477*ef5ccd6cSJohn Marino
4785796c8dcSSimon Schubert if (real_type)
4795796c8dcSSimon Schubert {
4805796c8dcSSimon Schubert printf_filtered ("/* real type = ");
4815796c8dcSSimon Schubert type_print (real_type, "", gdb_stdout, -1);
4825796c8dcSSimon Schubert if (! full)
4835796c8dcSSimon Schubert printf_filtered (" (incomplete object)");
4845796c8dcSSimon Schubert printf_filtered (" */\n");
4855796c8dcSSimon Schubert }
4865796c8dcSSimon Schubert
487*ef5ccd6cSJohn Marino LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
4885796c8dcSSimon Schubert printf_filtered ("\n");
4895796c8dcSSimon Schubert
4905796c8dcSSimon Schubert do_cleanups (old_chain);
4915796c8dcSSimon Schubert }
4925796c8dcSSimon Schubert
4935796c8dcSSimon Schubert static void
whatis_command(char * exp,int from_tty)4945796c8dcSSimon Schubert whatis_command (char *exp, int from_tty)
4955796c8dcSSimon Schubert {
4965796c8dcSSimon Schubert /* Most of the time users do not want to see all the fields
4975796c8dcSSimon Schubert in a structure. If they do they can use the "ptype" command.
4985796c8dcSSimon Schubert Hence the "-1" below. */
4995796c8dcSSimon Schubert whatis_exp (exp, -1);
5005796c8dcSSimon Schubert }
5015796c8dcSSimon Schubert
5025796c8dcSSimon Schubert /* TYPENAME is either the name of a type, or an expression. */
5035796c8dcSSimon Schubert
5045796c8dcSSimon Schubert static void
ptype_command(char * typename,int from_tty)5055796c8dcSSimon Schubert ptype_command (char *typename, int from_tty)
5065796c8dcSSimon Schubert {
5075796c8dcSSimon Schubert whatis_exp (typename, 1);
5085796c8dcSSimon Schubert }
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
5115796c8dcSSimon Schubert Used to print data from type structures in a specified type. For example,
5125796c8dcSSimon Schubert array bounds may be characters or booleans in some languages, and this
5135796c8dcSSimon Schubert allows the ranges to be printed in their "natural" form rather than as
5145796c8dcSSimon Schubert decimal integer values.
5155796c8dcSSimon Schubert
5165796c8dcSSimon Schubert FIXME: This is here simply because only the type printing routines
5175796c8dcSSimon Schubert currently use it, and it wasn't clear if it really belonged somewhere
5185796c8dcSSimon Schubert else (like printcmd.c). There are a lot of other gdb routines that do
5195796c8dcSSimon Schubert something similar, but they are generally concerned with printing values
5205796c8dcSSimon Schubert that come from the inferior in target byte order and target size. */
5215796c8dcSSimon Schubert
5225796c8dcSSimon Schubert void
print_type_scalar(struct type * type,LONGEST val,struct ui_file * stream)5235796c8dcSSimon Schubert print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
5245796c8dcSSimon Schubert {
5255796c8dcSSimon Schubert unsigned int i;
5265796c8dcSSimon Schubert unsigned len;
5275796c8dcSSimon Schubert
5285796c8dcSSimon Schubert CHECK_TYPEDEF (type);
5295796c8dcSSimon Schubert
5305796c8dcSSimon Schubert switch (TYPE_CODE (type))
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert
5335796c8dcSSimon Schubert case TYPE_CODE_ENUM:
5345796c8dcSSimon Schubert len = TYPE_NFIELDS (type);
5355796c8dcSSimon Schubert for (i = 0; i < len; i++)
5365796c8dcSSimon Schubert {
537*ef5ccd6cSJohn Marino if (TYPE_FIELD_ENUMVAL (type, i) == val)
5385796c8dcSSimon Schubert {
5395796c8dcSSimon Schubert break;
5405796c8dcSSimon Schubert }
5415796c8dcSSimon Schubert }
5425796c8dcSSimon Schubert if (i < len)
5435796c8dcSSimon Schubert {
5445796c8dcSSimon Schubert fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
5455796c8dcSSimon Schubert }
5465796c8dcSSimon Schubert else
5475796c8dcSSimon Schubert {
5485796c8dcSSimon Schubert print_longest (stream, 'd', 0, val);
5495796c8dcSSimon Schubert }
5505796c8dcSSimon Schubert break;
5515796c8dcSSimon Schubert
5525796c8dcSSimon Schubert case TYPE_CODE_INT:
5535796c8dcSSimon Schubert print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
5545796c8dcSSimon Schubert break;
5555796c8dcSSimon Schubert
5565796c8dcSSimon Schubert case TYPE_CODE_CHAR:
5575796c8dcSSimon Schubert LA_PRINT_CHAR ((unsigned char) val, type, stream);
5585796c8dcSSimon Schubert break;
5595796c8dcSSimon Schubert
5605796c8dcSSimon Schubert case TYPE_CODE_BOOL:
5615796c8dcSSimon Schubert fprintf_filtered (stream, val ? "TRUE" : "FALSE");
5625796c8dcSSimon Schubert break;
5635796c8dcSSimon Schubert
5645796c8dcSSimon Schubert case TYPE_CODE_RANGE:
5655796c8dcSSimon Schubert print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
5665796c8dcSSimon Schubert return;
5675796c8dcSSimon Schubert
5685796c8dcSSimon Schubert case TYPE_CODE_UNDEF:
5695796c8dcSSimon Schubert case TYPE_CODE_PTR:
5705796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
5715796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
5725796c8dcSSimon Schubert case TYPE_CODE_UNION:
5735796c8dcSSimon Schubert case TYPE_CODE_FUNC:
5745796c8dcSSimon Schubert case TYPE_CODE_FLT:
5755796c8dcSSimon Schubert case TYPE_CODE_VOID:
5765796c8dcSSimon Schubert case TYPE_CODE_SET:
5775796c8dcSSimon Schubert case TYPE_CODE_STRING:
5785796c8dcSSimon Schubert case TYPE_CODE_ERROR:
5795796c8dcSSimon Schubert case TYPE_CODE_MEMBERPTR:
5805796c8dcSSimon Schubert case TYPE_CODE_METHODPTR:
5815796c8dcSSimon Schubert case TYPE_CODE_METHOD:
5825796c8dcSSimon Schubert case TYPE_CODE_REF:
5835796c8dcSSimon Schubert case TYPE_CODE_NAMESPACE:
5845796c8dcSSimon Schubert error (_("internal error: unhandled type in print_type_scalar"));
5855796c8dcSSimon Schubert break;
5865796c8dcSSimon Schubert
5875796c8dcSSimon Schubert default:
5885796c8dcSSimon Schubert error (_("Invalid type code in symbol table."));
5895796c8dcSSimon Schubert }
5905796c8dcSSimon Schubert gdb_flush (stream);
5915796c8dcSSimon Schubert }
5925796c8dcSSimon Schubert
5935796c8dcSSimon Schubert /* Dump details of a type specified either directly or indirectly.
5945796c8dcSSimon Schubert Uses the same sort of type lookup mechanism as ptype_command()
5955796c8dcSSimon Schubert and whatis_command(). */
5965796c8dcSSimon Schubert
5975796c8dcSSimon Schubert void
maintenance_print_type(char * typename,int from_tty)5985796c8dcSSimon Schubert maintenance_print_type (char *typename, int from_tty)
5995796c8dcSSimon Schubert {
6005796c8dcSSimon Schubert struct value *val;
6015796c8dcSSimon Schubert struct type *type;
6025796c8dcSSimon Schubert struct cleanup *old_chain;
6035796c8dcSSimon Schubert struct expression *expr;
6045796c8dcSSimon Schubert
6055796c8dcSSimon Schubert if (typename != NULL)
6065796c8dcSSimon Schubert {
6075796c8dcSSimon Schubert expr = parse_expression (typename);
6085796c8dcSSimon Schubert old_chain = make_cleanup (free_current_contents, &expr);
6095796c8dcSSimon Schubert if (expr->elts[0].opcode == OP_TYPE)
6105796c8dcSSimon Schubert {
6115796c8dcSSimon Schubert /* The user expression names a type directly, just use that type. */
6125796c8dcSSimon Schubert type = expr->elts[1].type;
6135796c8dcSSimon Schubert }
6145796c8dcSSimon Schubert else
6155796c8dcSSimon Schubert {
6165796c8dcSSimon Schubert /* The user expression may name a type indirectly by naming an
6175796c8dcSSimon Schubert object of that type. Find that indirectly named type. */
6185796c8dcSSimon Schubert val = evaluate_type (expr);
6195796c8dcSSimon Schubert type = value_type (val);
6205796c8dcSSimon Schubert }
6215796c8dcSSimon Schubert if (type != NULL)
6225796c8dcSSimon Schubert {
6235796c8dcSSimon Schubert recursive_dump_type (type, 0);
6245796c8dcSSimon Schubert }
6255796c8dcSSimon Schubert do_cleanups (old_chain);
6265796c8dcSSimon Schubert }
6275796c8dcSSimon Schubert }
6285796c8dcSSimon Schubert
6295796c8dcSSimon Schubert
630*ef5ccd6cSJohn Marino struct cmd_list_element *setprinttypelist;
631*ef5ccd6cSJohn Marino
632*ef5ccd6cSJohn Marino struct cmd_list_element *showprinttypelist;
633*ef5ccd6cSJohn Marino
634*ef5ccd6cSJohn Marino static void
set_print_type(char * arg,int from_tty)635*ef5ccd6cSJohn Marino set_print_type (char *arg, int from_tty)
636*ef5ccd6cSJohn Marino {
637*ef5ccd6cSJohn Marino printf_unfiltered (
638*ef5ccd6cSJohn Marino "\"set print type\" must be followed by the name of a subcommand.\n");
639*ef5ccd6cSJohn Marino help_list (setprintlist, "set print type ", -1, gdb_stdout);
640*ef5ccd6cSJohn Marino }
641*ef5ccd6cSJohn Marino
642*ef5ccd6cSJohn Marino static void
show_print_type(char * args,int from_tty)643*ef5ccd6cSJohn Marino show_print_type (char *args, int from_tty)
644*ef5ccd6cSJohn Marino {
645*ef5ccd6cSJohn Marino cmd_show_list (showprinttypelist, from_tty, "");
646*ef5ccd6cSJohn Marino }
647*ef5ccd6cSJohn Marino
648*ef5ccd6cSJohn Marino static int print_methods = 1;
649*ef5ccd6cSJohn Marino
650*ef5ccd6cSJohn Marino static void
set_print_type_methods(char * args,int from_tty,struct cmd_list_element * c)651*ef5ccd6cSJohn Marino set_print_type_methods (char *args, int from_tty, struct cmd_list_element *c)
652*ef5ccd6cSJohn Marino {
653*ef5ccd6cSJohn Marino default_ptype_flags.print_methods = print_methods;
654*ef5ccd6cSJohn Marino }
655*ef5ccd6cSJohn Marino
656*ef5ccd6cSJohn Marino static void
show_print_type_methods(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)657*ef5ccd6cSJohn Marino show_print_type_methods (struct ui_file *file, int from_tty,
658*ef5ccd6cSJohn Marino struct cmd_list_element *c, const char *value)
659*ef5ccd6cSJohn Marino {
660*ef5ccd6cSJohn Marino fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
661*ef5ccd6cSJohn Marino value);
662*ef5ccd6cSJohn Marino }
663*ef5ccd6cSJohn Marino
664*ef5ccd6cSJohn Marino static int print_typedefs = 1;
665*ef5ccd6cSJohn Marino
666*ef5ccd6cSJohn Marino static void
set_print_type_typedefs(char * args,int from_tty,struct cmd_list_element * c)667*ef5ccd6cSJohn Marino set_print_type_typedefs (char *args, int from_tty, struct cmd_list_element *c)
668*ef5ccd6cSJohn Marino {
669*ef5ccd6cSJohn Marino default_ptype_flags.print_typedefs = print_typedefs;
670*ef5ccd6cSJohn Marino }
671*ef5ccd6cSJohn Marino
672*ef5ccd6cSJohn Marino static void
show_print_type_typedefs(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)673*ef5ccd6cSJohn Marino show_print_type_typedefs (struct ui_file *file, int from_tty,
674*ef5ccd6cSJohn Marino struct cmd_list_element *c, const char *value)
675*ef5ccd6cSJohn Marino {
676*ef5ccd6cSJohn Marino fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
677*ef5ccd6cSJohn Marino value);
678*ef5ccd6cSJohn Marino }
679*ef5ccd6cSJohn Marino
6805796c8dcSSimon Schubert void
_initialize_typeprint(void)6815796c8dcSSimon Schubert _initialize_typeprint (void)
6825796c8dcSSimon Schubert {
683*ef5ccd6cSJohn Marino struct cmd_list_element *c;
684*ef5ccd6cSJohn Marino
685*ef5ccd6cSJohn Marino c = add_com ("ptype", class_vars, ptype_command, _("\
6865796c8dcSSimon Schubert Print definition of type TYPE.\n\
687*ef5ccd6cSJohn Marino Usage: ptype[/FLAGS] TYPE-NAME | EXPRESSION\n\
6885796c8dcSSimon Schubert Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
6895796c8dcSSimon Schubert or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
690a45ae5f8SJohn Marino The selected stack frame's lexical context is used to look up the name.\n\
691*ef5ccd6cSJohn Marino Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
692*ef5ccd6cSJohn Marino \n\
693*ef5ccd6cSJohn Marino Available FLAGS are:\n\
694*ef5ccd6cSJohn Marino /r print in \"raw\" form; do not substitute typedefs\n\
695*ef5ccd6cSJohn Marino /m do not print methods defined in a class\n\
696*ef5ccd6cSJohn Marino /M print methods defined in a class\n\
697*ef5ccd6cSJohn Marino /t do not print typedefs defined in a class\n\
698*ef5ccd6cSJohn Marino /T print typedefs defined in a class"));
699*ef5ccd6cSJohn Marino set_cmd_completer (c, expression_completer);
7005796c8dcSSimon Schubert
701*ef5ccd6cSJohn Marino c = add_com ("whatis", class_vars, whatis_command,
702a45ae5f8SJohn Marino _("Print data type of expression EXP.\n\
703a45ae5f8SJohn Marino Only one level of typedefs is unrolled. See also \"ptype\"."));
704*ef5ccd6cSJohn Marino set_cmd_completer (c, expression_completer);
705*ef5ccd6cSJohn Marino
706*ef5ccd6cSJohn Marino add_prefix_cmd ("type", no_class, show_print_type,
707*ef5ccd6cSJohn Marino _("Generic command for showing type-printing settings."),
708*ef5ccd6cSJohn Marino &showprinttypelist, "show print type ", 0, &showprintlist);
709*ef5ccd6cSJohn Marino add_prefix_cmd ("type", no_class, set_print_type,
710*ef5ccd6cSJohn Marino _("Generic command for setting how types print."),
711*ef5ccd6cSJohn Marino &setprinttypelist, "show print type ", 0, &setprintlist);
712*ef5ccd6cSJohn Marino
713*ef5ccd6cSJohn Marino add_setshow_boolean_cmd ("methods", no_class, &print_methods,
714*ef5ccd6cSJohn Marino _("\
715*ef5ccd6cSJohn Marino Set printing of methods defined in classes."), _("\
716*ef5ccd6cSJohn Marino Show printing of methods defined in classes."), NULL,
717*ef5ccd6cSJohn Marino set_print_type_methods,
718*ef5ccd6cSJohn Marino show_print_type_methods,
719*ef5ccd6cSJohn Marino &setprinttypelist, &showprinttypelist);
720*ef5ccd6cSJohn Marino add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs,
721*ef5ccd6cSJohn Marino _("\
722*ef5ccd6cSJohn Marino Set printing of typedefs defined in classes."), _("\
723*ef5ccd6cSJohn Marino Show printing of typedefs defined in classes."), NULL,
724*ef5ccd6cSJohn Marino set_print_type_typedefs,
725*ef5ccd6cSJohn Marino show_print_type_typedefs,
726*ef5ccd6cSJohn Marino &setprinttypelist, &showprinttypelist);
7275796c8dcSSimon Schubert }
728