]> Git Repo - binutils.git/blob - gdb/c-valprint.c
comment change
[binutils.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "demangle.h"
26 #include "valprint.h"
27 #include "language.h"
28
29 /* BEGIN-FIXME */
30
31 extern int vtblprint;           /* Controls printing of vtbl's */
32 extern int demangle;            /* whether to print C++ syms raw or src-form */
33
34 extern void
35 cp_print_class_member PARAMS ((char *, struct type *, FILE *, char *));
36
37 extern void
38 cp_print_class_method PARAMS ((char *, struct type *, FILE *));
39
40 extern void
41 cp_print_value_fields PARAMS ((struct type *, char *, FILE *, int, int,
42                                enum val_prettyprint, struct type **));
43
44 extern int
45 cp_is_vtbl_ptr_type PARAMS ((struct type *));
46
47 extern int
48 cp_is_vtbl_member PARAMS ((struct type *));
49
50 /* END-FIXME */
51
52
53 /* BEGIN-FIXME:  Hooks into c-typeprint.c */
54
55 extern void
56 c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int));
57
58 extern void
59 cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
60                                    FILE *));
61 /* END-FIXME */
62
63
64 extern struct obstack dont_print_obstack;
65
66 \f
67 /* Print data of type TYPE located at VALADDR (within GDB), which came from
68    the inferior at address ADDRESS, onto stdio stream STREAM according to
69    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
70    target byte order.
71
72    If the data are a string pointer, returns the number of string characters
73    printed.
74
75    If DEREF_REF is nonzero, then dereference references, otherwise just print
76    them like pointers.
77
78    The PRETTY parameter controls prettyprinting.  */
79
80 int
81 c_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
82              pretty)
83      struct type *type;
84      char *valaddr;
85      CORE_ADDR address;
86      FILE *stream;
87      int format;
88      int deref_ref;
89      int recurse;
90      enum val_prettyprint pretty;
91 {
92   register unsigned int i = 0;          /* Number of characters printed */
93   unsigned len;
94   struct type *elttype;
95   unsigned eltlen;
96   LONGEST val;
97   unsigned char c;
98   CORE_ADDR addr;
99
100   switch (TYPE_CODE (type))
101     {
102     case TYPE_CODE_ARRAY:
103       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
104         {
105           elttype = TYPE_TARGET_TYPE (type);
106           eltlen = TYPE_LENGTH (elttype);
107           len = TYPE_LENGTH (type) / eltlen;
108           if (prettyprint_arrays)
109             {
110               print_spaces_filtered (2 + 2 * recurse, stream);
111             }
112           /* For an array of chars, print with string syntax.  */
113           if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
114               && (format == 0 || format == 's'))
115             {
116               LA_PRINT_STRING (stream, valaddr, len, 0);
117               i = len;
118             }
119           else
120             {
121               fprintf_filtered (stream, "{");
122               /* If this is a virtual function table, print the 0th
123                  entry specially, and the rest of the members normally.  */
124               if (cp_is_vtbl_ptr_type (elttype))
125                 {
126                   i = 1;
127                   fprintf_filtered (stream, "%d vtable entries", len - 1);
128                 }
129               else
130                 {
131                   i = 0;
132                 }
133               val_print_array_elements (type, valaddr, address, stream,
134                                         format, deref_ref, recurse, pretty, i);
135               fprintf_filtered (stream, "}");
136             }
137           break;
138         }
139       /* Array of unspecified length: treat like pointer to first elt.  */
140       addr = address;
141       goto print_unpacked_pointer;
142
143     case TYPE_CODE_PTR:
144       if (format && format != 's')
145         {
146           print_scalar_formatted (valaddr, type, format, 0, stream);
147           break;
148         }
149       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
150         {
151           cp_print_class_method (valaddr, type, stream);
152         }
153       else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
154         {
155           cp_print_class_member (valaddr,
156                                  TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
157                                  stream, "&");
158         }
159       else
160         {
161           addr = unpack_pointer (type, valaddr);
162         print_unpacked_pointer:
163           elttype = TYPE_TARGET_TYPE (type);
164
165           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
166             {
167               /* Try to print what function it points to.  */
168               print_address_demangle (addr, stream, demangle);
169               /* Return value is irrelevant except for string pointers.  */
170               return (0);
171             }
172
173           if (addressprint && format != 's')
174             {
175               fprintf_filtered (stream, "0x%x", addr);
176             }
177
178           /* For a pointer to char or unsigned char, also print the string
179              pointed to, unless pointer is null.  */
180           if (TYPE_LENGTH (elttype) == 1
181               && TYPE_CODE (elttype) == TYPE_CODE_INT
182               && (format == 0 || format == 's')
183               && addr != 0)
184             {
185               i = val_print_string (addr, 0, stream);
186             }
187           else if (cp_is_vtbl_member(type))
188             {
189               /* print vtbl's nicely */
190               CORE_ADDR vt_address = unpack_pointer (type, valaddr);
191
192               struct minimal_symbol *msymbol =
193                 lookup_minimal_symbol_by_pc (vt_address);
194               if ((msymbol != NULL) &&
195                   (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
196                 {
197                   fputs_filtered (" <", stream);
198                   fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
199                   fputs_filtered (">", stream);
200                 }
201               if (vtblprint)
202                 {
203                   value vt_val;
204
205                   vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address);
206                   val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
207                              VALUE_ADDRESS (vt_val), stream, format,
208                              deref_ref, recurse + 1, pretty);
209                   if (pretty)
210                     {
211                       fprintf_filtered (stream, "\n");
212                       print_spaces_filtered (2 + 2 * recurse, stream);
213                     }
214                 }
215               }
216
217           /* Return number of characters printed, plus one for the
218              terminating null if we have "reached the end".  */
219           return (i + (print_max && i != print_max));
220         }
221       break;
222
223     case TYPE_CODE_MEMBER:
224       error ("not implemented: member type in c_val_print");
225       break;
226
227     case TYPE_CODE_REF:
228       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
229         {
230           cp_print_class_member (valaddr,
231                                  TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
232                                  stream, "");
233           break;
234         }
235       if (addressprint)
236         {
237           fprintf_filtered (stream, "@0x%lx",
238                             unpack_long (builtin_type_int, valaddr));
239           if (deref_ref)
240             fputs_filtered (": ", stream);
241         }
242       /* De-reference the reference.  */
243       if (deref_ref)
244         {
245           if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
246             {
247               value deref_val =
248                 value_at
249                   (TYPE_TARGET_TYPE (type),
250                    unpack_pointer (lookup_pointer_type (builtin_type_void),
251                                    valaddr));
252               val_print (VALUE_TYPE (deref_val),
253                          VALUE_CONTENTS (deref_val),
254                          VALUE_ADDRESS (deref_val), stream, format,
255                          deref_ref, recurse + 1, pretty);
256             }
257           else
258             fputs_filtered ("???", stream);
259         }
260       break;
261
262     case TYPE_CODE_UNION:
263       if (recurse && !unionprint)
264         {
265           fprintf_filtered (stream, "{...}");
266           break;
267         }
268       /* Fall through.  */
269     case TYPE_CODE_STRUCT:
270       if (vtblprint && cp_is_vtbl_ptr_type(type))
271         {
272           /* Print the unmangled name if desired.  */
273           print_address_demangle(*((int *) (valaddr +   /* FIXME bytesex */
274               TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
275               stream, demangle);
276           break;
277         }
278       cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
279                              0);
280       break;
281
282     case TYPE_CODE_ENUM:
283       if (format)
284         {
285           print_scalar_formatted (valaddr, type, format, 0, stream);
286           break;
287         }
288       len = TYPE_NFIELDS (type);
289       val = unpack_long (type, valaddr);
290       for (i = 0; i < len; i++)
291         {
292           QUIT;
293           if (val == TYPE_FIELD_BITPOS (type, i))
294             {
295               break;
296             }
297         }
298       if (i < len)
299         {
300           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
301         }
302       else
303         {
304           print_longest (stream, 'd', 0, val);
305         }
306       break;
307
308     case TYPE_CODE_FUNC:
309       if (format)
310         {
311           print_scalar_formatted (valaddr, type, format, 0, stream);
312           break;
313         }
314       /* FIXME, we should consider, at least for ANSI C language, eliminating
315          the distinction made between FUNCs and POINTERs to FUNCs.  */
316       fprintf_filtered (stream, "{");
317       type_print (type, "", stream, -1);
318       fprintf_filtered (stream, "} ");
319       /* Try to print what function it points to, and its address.  */
320       print_address_demangle (address, stream, demangle);
321       break;
322
323     case TYPE_CODE_BOOL:
324       /* Do something at least vaguely reasonable, for example if the
325          language is set wrong.  */
326
327     case TYPE_CODE_INT:
328       format = format ? format : output_format;
329       if (format)
330         {
331           print_scalar_formatted (valaddr, type, format, 0, stream);
332         }
333       else
334         {
335           val_print_type_code_int (type, valaddr, stream);
336           /* C and C++ has no single byte int type, char is used instead.
337              Since we don't know whether the value is really intended to
338              be used as an integer or a character, print the character
339              equivalent as well. */
340           if (TYPE_LENGTH (type) == 1)
341             {
342               fputs_filtered (" ", stream);
343               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
344                              stream);
345             }
346         }
347       break;
348
349     case TYPE_CODE_CHAR:
350       format = format ? format : output_format;
351       if (format)
352         {
353           print_scalar_formatted (valaddr, type, format, 0, stream);
354         }
355       else
356         {
357           fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d",
358                             unpack_long (type, valaddr));
359           fputs_filtered (" ", stream);
360           LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream);
361         }
362       break;
363
364     case TYPE_CODE_FLT:
365       if (format)
366         {
367           print_scalar_formatted (valaddr, type, format, 0, stream);
368         }
369       else
370         {
371           print_floating (valaddr, type, stream);
372         }
373       break;
374
375     case TYPE_CODE_VOID:
376       fprintf_filtered (stream, "void");
377       break;
378
379     case TYPE_CODE_ERROR:
380       fprintf_filtered (stream, "<error type>");
381       break;
382
383     case TYPE_CODE_RANGE:
384       /* FIXME, we should not ever have to print one of these yet.  */
385       fprintf_filtered (stream, "<range type>");
386       break;
387
388     case TYPE_CODE_UNDEF:
389       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
390          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
391          and no complete type for struct foo in that file.  */
392       fprintf_filtered (stream, "<incomplete type>");
393       break;
394
395     default:
396       error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
397     }
398   fflush (stream);
399   return (0);
400 }
This page took 0.048805 seconds and 4 git commands to generate.