7781fada9a257e950e3cc454a3effd4ec97809a5
[platform/upstream/binutils.git] / gdb / ada-valprint.c
1 /* Support for printing Ada values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1997, 2001,
4    2002, 2003, 2004, 2005, 2006 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.  */
22
23 #include <ctype.h>
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "value.h"
30 #include "demangle.h"
31 #include "valprint.h"
32 #include "language.h"
33 #include "annotate.h"
34 #include "ada-lang.h"
35 #include "c-lang.h"
36 #include "infcall.h"
37 #include "exceptions.h"
38
39 /* Encapsulates arguments to ada_val_print.  */
40 struct ada_val_print_args
41 {
42   struct type *type;
43   const gdb_byte *valaddr0;
44   int embedded_offset;
45   CORE_ADDR address;
46   struct ui_file *stream;
47   int format;
48   int deref_ref;
49   int recurse;
50   enum val_prettyprint pretty;
51 };
52
53 static void print_record (struct type *, const gdb_byte *, struct ui_file *,
54                           int, int, enum val_prettyprint);
55
56 static int print_field_values (struct type *, const gdb_byte *,
57                                struct ui_file *, int, int,
58                                enum val_prettyprint, int, struct type *,
59                                const gdb_byte *);
60
61 static void adjust_type_signedness (struct type *);
62
63 static int ada_val_print_stub (void *args0);
64
65 static int ada_val_print_1 (struct type *, const gdb_byte *, int, CORE_ADDR,
66                             struct ui_file *, int, int, int,
67                             enum val_prettyprint);
68 \f
69
70 /* Make TYPE unsigned if its range of values includes no negatives.  */
71 static void
72 adjust_type_signedness (struct type *type)
73 {
74   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_RANGE
75       && TYPE_LOW_BOUND (type) >= 0)
76     TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
77 }
78
79 /* Assuming TYPE is a simple, non-empty array type, prints its lower bound 
80    on STREAM, if non-standard (i.e., other than 1 for numbers, other
81    than lower bound of index type for enumerated type).  Returns 1 
82    if something printed, otherwise 0.  */
83
84 static int
85 print_optional_low_bound (struct ui_file *stream, struct type *type)
86 {
87   struct type *index_type;
88   long low_bound;
89
90   if (print_array_indexes_p ())
91     return 0;
92
93   if (!get_array_low_bound (type, &low_bound))
94     return 0;
95
96   index_type = TYPE_INDEX_TYPE (type);
97
98   if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
99     {
100       /* We need to know what the base type is, in order to do the
101          appropriate check below.  Otherwise, if this is a subrange
102          of an enumerated type, where the underlying value of the
103          first element is typically 0, we might test the low bound
104          against the wrong value.  */
105       index_type = TYPE_TARGET_TYPE (index_type);
106     }
107
108   switch (TYPE_CODE (index_type))
109     {
110     case TYPE_CODE_ENUM:
111       if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
112         return 0;
113       break;
114     case TYPE_CODE_UNDEF:
115       index_type = builtin_type_long;
116       /* FALL THROUGH */
117     default:
118       if (low_bound == 1)
119         return 0;
120       break;
121     }
122
123   ada_print_scalar (index_type, (LONGEST) low_bound, stream);
124   fprintf_filtered (stream, " => ");
125   return 1;
126 }
127
128 /*  Version of val_print_array_elements for GNAT-style packed arrays.
129     Prints elements of packed array of type TYPE at bit offset
130     BITOFFSET from VALADDR on STREAM.  Formats according to FORMAT and
131     separates with commas.  RECURSE is the recursion (nesting) level.
132     If PRETTY, uses "prettier" format.  TYPE must have been decoded (as
133     by ada_coerce_to_simple_array).  */
134
135 static void
136 val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
137                                  int bitoffset, struct ui_file *stream,
138                                  int format, int recurse,
139                                  enum val_prettyprint pretty)
140 {
141   unsigned int i;
142   unsigned int things_printed = 0;
143   unsigned len;
144   struct type *elttype, *index_type;
145   unsigned eltlen;
146   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
147   struct value *mark = value_mark ();
148   LONGEST low = 0;
149
150   elttype = TYPE_TARGET_TYPE (type);
151   eltlen = TYPE_LENGTH (check_typedef (elttype));
152   index_type = TYPE_INDEX_TYPE (type);
153
154   {
155     LONGEST high;
156     if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
157       len = 1;
158     else
159       len = high - low + 1;
160   }
161
162   i = 0;
163   annotate_array_section_begin (i, elttype);
164
165   while (i < len && things_printed < print_max)
166     {
167       struct value *v0, *v1;
168       int i0;
169
170       if (i != 0)
171         {
172           if (prettyprint_arrays)
173             {
174               fprintf_filtered (stream, ",\n");
175               print_spaces_filtered (2 + 2 * recurse, stream);
176             }
177           else
178             {
179               fprintf_filtered (stream, ", ");
180             }
181         }
182       wrap_here (n_spaces (2 + 2 * recurse));
183       maybe_print_array_index (index_type, i + low, stream, format, pretty);
184
185       i0 = i;
186       v0 = ada_value_primitive_packed_val (NULL, valaddr,
187                                            (i0 * bitsize) / HOST_CHAR_BIT,
188                                            (i0 * bitsize) % HOST_CHAR_BIT,
189                                            bitsize, elttype);
190       while (1)
191         {
192           i += 1;
193           if (i >= len)
194             break;
195           v1 = ada_value_primitive_packed_val (NULL, valaddr,
196                                                (i * bitsize) / HOST_CHAR_BIT,
197                                                (i * bitsize) % HOST_CHAR_BIT,
198                                                bitsize, elttype);
199           if (memcmp (value_contents (v0), value_contents (v1), eltlen) != 0)
200             break;
201         }
202
203       if (i - i0 > repeat_count_threshold)
204         {
205           val_print (elttype, value_contents (v0), 0, 0, stream, format,
206                      0, recurse + 1, pretty);
207           annotate_elt_rep (i - i0);
208           fprintf_filtered (stream, _(" <repeats %u times>"), i - i0);
209           annotate_elt_rep_end ();
210
211         }
212       else
213         {
214           int j;
215           for (j = i0; j < i; j += 1)
216             {
217               if (j > i0)
218                 {
219                   if (prettyprint_arrays)
220                     {
221                       fprintf_filtered (stream, ",\n");
222                       print_spaces_filtered (2 + 2 * recurse, stream);
223                     }
224                   else
225                     {
226                       fprintf_filtered (stream, ", ");
227                     }
228                   wrap_here (n_spaces (2 + 2 * recurse));
229                   maybe_print_array_index (index_type, j + low,
230                                            stream, format, pretty);
231                 }
232               val_print (elttype, value_contents (v0), 0, 0, stream, format,
233                          0, recurse + 1, pretty);
234               annotate_elt ();
235             }
236         }
237       things_printed += i - i0;
238     }
239   annotate_array_section_end ();
240   if (i < len)
241     {
242       fprintf_filtered (stream, "...");
243     }
244
245   value_free_to_mark (mark);
246 }
247
248 static struct type *
249 printable_val_type (struct type *type, const gdb_byte *valaddr)
250 {
251   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL);
252 }
253
254 /* Print the character C on STREAM as part of the contents of a literal
255    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
256    (1 or 2) of the character.  */
257
258 void
259 ada_emit_char (int c, struct ui_file *stream, int quoter, int type_len)
260 {
261   if (type_len != 2)
262     type_len = 1;
263
264   c &= (1 << (type_len * TARGET_CHAR_BIT)) - 1;
265
266   if (isascii (c) && isprint (c))
267     {
268       if (c == quoter && c == '"')
269         fprintf_filtered (stream, "\"\"");
270       else
271         fprintf_filtered (stream, "%c", c);
272     }
273   else
274     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
275 }
276
277 /* Character #I of STRING, given that TYPE_LEN is the size in bytes (1
278    or 2) of a character.  */
279
280 static int
281 char_at (const gdb_byte *string, int i, int type_len)
282 {
283   if (type_len == 1)
284     return string[i];
285   else
286     return (int) extract_unsigned_integer (string + 2 * i, 2);
287 }
288
289 /* Wrapper around memcpy to make it legal argument to ui_file_put */
290 static void
291 ui_memcpy (void *dest, const char *buffer, long len)
292 {
293   memcpy (dest, buffer, (size_t) len);
294   ((char *) dest)[len] = '\0';
295 }
296
297 /* Print a floating-point value of type TYPE, pointed to in GDB by
298    VALADDR, on STREAM.  Use Ada formatting conventions: there must be
299    a decimal point, and at least one digit before and after the
300    point.  We use GNAT format for NaNs and infinities.  */
301 static void
302 ada_print_floating (const gdb_byte *valaddr, struct type *type,
303                     struct ui_file *stream)
304 {
305   char buffer[64];
306   char *s, *result;
307   int len;
308   struct ui_file *tmp_stream = mem_fileopen ();
309   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_stream);
310
311   print_floating (valaddr, type, tmp_stream);
312   ui_file_put (tmp_stream, ui_memcpy, buffer);
313   do_cleanups (cleanups);
314
315   result = buffer;
316   len = strlen (result);
317
318   /* Modify for Ada rules.  */
319   
320   s = strstr (result, "inf");
321   if (s == NULL)
322     s = strstr (result, "Inf");
323   if (s == NULL)
324     s = strstr (result, "INF");
325   if (s != NULL)
326     strcpy (s, "Inf");
327
328   if (s == NULL)
329     {
330       s = strstr (result, "nan");
331       if (s == NULL)
332         s = strstr (result, "NaN");
333       if (s == NULL)
334         s = strstr (result, "Nan");
335       if (s != NULL)
336         {
337           s[0] = s[2] = 'N';
338           if (result[0] == '-')
339             result += 1;
340         }
341     }
342
343   if (s == NULL && strchr (result, '.') == NULL)
344     {
345       s = strchr (result, 'e');
346       if (s == NULL)
347         fprintf_filtered (stream, "%s.0", result);
348       else
349         fprintf_filtered (stream, "%.*s.0%s", (int) (s-result), result, s);
350       return;
351     }
352   fprintf_filtered (stream, "%s", result);
353 }
354
355 void
356 ada_printchar (int c, struct ui_file *stream)
357 {
358   fputs_filtered ("'", stream);
359   ada_emit_char (c, stream, '\'', 1);
360   fputs_filtered ("'", stream);
361 }
362
363 /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
364    form appropriate for TYPE.  */
365
366 void
367 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
368 {
369   unsigned int i;
370   unsigned len;
371
372   type = ada_check_typedef (type);
373
374   switch (TYPE_CODE (type))
375     {
376
377     case TYPE_CODE_ENUM:
378       len = TYPE_NFIELDS (type);
379       for (i = 0; i < len; i++)
380         {
381           if (TYPE_FIELD_BITPOS (type, i) == val)
382             {
383               break;
384             }
385         }
386       if (i < len)
387         {
388           fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
389         }
390       else
391         {
392           print_longest (stream, 'd', 0, val);
393         }
394       break;
395
396     case TYPE_CODE_INT:
397       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
398       break;
399
400     case TYPE_CODE_CHAR:
401       LA_PRINT_CHAR ((unsigned char) val, stream);
402       break;
403
404     case TYPE_CODE_BOOL:
405       fprintf_filtered (stream, val ? "true" : "false");
406       break;
407
408     case TYPE_CODE_RANGE:
409       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
410       return;
411
412     case TYPE_CODE_UNDEF:
413     case TYPE_CODE_PTR:
414     case TYPE_CODE_ARRAY:
415     case TYPE_CODE_STRUCT:
416     case TYPE_CODE_UNION:
417     case TYPE_CODE_FUNC:
418     case TYPE_CODE_FLT:
419     case TYPE_CODE_VOID:
420     case TYPE_CODE_SET:
421     case TYPE_CODE_STRING:
422     case TYPE_CODE_ERROR:
423     case TYPE_CODE_MEMBER:
424     case TYPE_CODE_METHOD:
425     case TYPE_CODE_REF:
426       warning (_("internal error: unhandled type in ada_print_scalar"));
427       break;
428
429     default:
430       error (_("Invalid type code in symbol table."));
431     }
432   gdb_flush (stream);
433 }
434
435 /* Print the character string STRING, printing at most LENGTH characters.
436    Printing stops early if the number hits print_max; repeat counts
437    are printed as appropriate.  Print ellipses at the end if we
438    had to stop before printing LENGTH characters, or if
439    FORCE_ELLIPSES.   TYPE_LEN is the length (1 or 2) of the character type.
440  */
441
442 static void
443 printstr (struct ui_file *stream, const gdb_byte *string,
444           unsigned int length, int force_ellipses, int type_len)
445 {
446   unsigned int i;
447   unsigned int things_printed = 0;
448   int in_quotes = 0;
449   int need_comma = 0;
450
451   if (length == 0)
452     {
453       fputs_filtered ("\"\"", stream);
454       return;
455     }
456
457   for (i = 0; i < length && things_printed < print_max; i += 1)
458     {
459       /* Position of the character we are examining
460          to see whether it is repeated.  */
461       unsigned int rep1;
462       /* Number of repetitions we have detected so far.  */
463       unsigned int reps;
464
465       QUIT;
466
467       if (need_comma)
468         {
469           fputs_filtered (", ", stream);
470           need_comma = 0;
471         }
472
473       rep1 = i + 1;
474       reps = 1;
475       while (rep1 < length
476              && char_at (string, rep1, type_len) == char_at (string, i,
477                                                              type_len))
478         {
479           rep1 += 1;
480           reps += 1;
481         }
482
483       if (reps > repeat_count_threshold)
484         {
485           if (in_quotes)
486             {
487               if (inspect_it)
488                 fputs_filtered ("\\\", ", stream);
489               else
490                 fputs_filtered ("\", ", stream);
491               in_quotes = 0;
492             }
493           fputs_filtered ("'", stream);
494           ada_emit_char (char_at (string, i, type_len), stream, '\'',
495                          type_len);
496           fputs_filtered ("'", stream);
497           fprintf_filtered (stream, _(" <repeats %u times>"), reps);
498           i = rep1 - 1;
499           things_printed += repeat_count_threshold;
500           need_comma = 1;
501         }
502       else
503         {
504           if (!in_quotes)
505             {
506               if (inspect_it)
507                 fputs_filtered ("\\\"", stream);
508               else
509                 fputs_filtered ("\"", stream);
510               in_quotes = 1;
511             }
512           ada_emit_char (char_at (string, i, type_len), stream, '"',
513                          type_len);
514           things_printed += 1;
515         }
516     }
517
518   /* Terminate the quotes if necessary.  */
519   if (in_quotes)
520     {
521       if (inspect_it)
522         fputs_filtered ("\\\"", stream);
523       else
524         fputs_filtered ("\"", stream);
525     }
526
527   if (force_ellipses || i < length)
528     fputs_filtered ("...", stream);
529 }
530
531 void
532 ada_printstr (struct ui_file *stream, const gdb_byte *string,
533               unsigned int length, int width, int force_ellipses)
534 {
535   printstr (stream, string, length, force_ellipses, width);
536 }
537
538
539 /* Print data of type TYPE located at VALADDR (within GDB), which came from
540    the inferior at address ADDRESS, onto stdio stream STREAM according to
541    FORMAT (a letter as for the printf % codes or 0 for natural format).
542    The data at VALADDR is in target byte order.
543
544    If the data is printed as a string, returns the number of string characters
545    printed.
546
547    If DEREF_REF is nonzero, then dereference references, otherwise just print
548    them like pointers.
549
550    RECURSE indicates the amount of indentation to supply before
551    continuation lines; this amount is roughly twice the value of RECURSE.
552
553    When PRETTY is non-zero, prints record fields on separate lines.
554    (For some reason, the current version of gdb instead uses a global
555    variable---prettyprint_arrays--- to causes a similar effect on
556    arrays.)  */
557
558 int
559 ada_val_print (struct type *type, const gdb_byte *valaddr0,
560                int embedded_offset, CORE_ADDR address,
561                struct ui_file *stream, int format, int deref_ref,
562                int recurse, enum val_prettyprint pretty)
563 {
564   struct ada_val_print_args args;
565   args.type = type;
566   args.valaddr0 = valaddr0;
567   args.embedded_offset = embedded_offset;
568   args.address = address;
569   args.stream = stream;
570   args.format = format;
571   args.deref_ref = deref_ref;
572   args.recurse = recurse;
573   args.pretty = pretty;
574
575   return catch_errors (ada_val_print_stub, &args, NULL, RETURN_MASK_ALL);
576 }
577
578 /* Helper for ada_val_print; used as argument to catch_errors to
579    unmarshal the arguments to ada_val_print_1, which does the work.  */
580 static int
581 ada_val_print_stub (void *args0)
582 {
583   struct ada_val_print_args *argsp = (struct ada_val_print_args *) args0;
584   return ada_val_print_1 (argsp->type, argsp->valaddr0,
585                           argsp->embedded_offset, argsp->address,
586                           argsp->stream, argsp->format, argsp->deref_ref,
587                           argsp->recurse, argsp->pretty);
588 }
589
590 /* See the comment on ada_val_print.  This function differs in that it
591  * does not catch evaluation errors (leaving that to ada_val_print).  */
592
593 static int
594 ada_val_print_1 (struct type *type, const gdb_byte *valaddr0,
595                  int embedded_offset, CORE_ADDR address,
596                  struct ui_file *stream, int format,
597                  int deref_ref, int recurse, enum val_prettyprint pretty)
598 {
599   unsigned int len;
600   int i;
601   struct type *elttype;
602   unsigned int eltlen;
603   LONGEST val;
604   const gdb_byte *valaddr = valaddr0 + embedded_offset;
605
606   type = ada_check_typedef (type);
607
608   if (ada_is_array_descriptor_type (type) || ada_is_packed_array_type (type))
609     {
610       int retn;
611       struct value *mark = value_mark ();
612       struct value *val;
613       val = value_from_contents_and_address (type, valaddr, address);
614       val = ada_coerce_to_simple_array_ptr (val);
615       if (val == NULL)
616         {
617           fprintf_filtered (stream, "(null)");
618           retn = 0;
619         }
620       else
621         retn = ada_val_print_1 (value_type (val), value_contents (val), 0,
622                                 VALUE_ADDRESS (val), stream, format,
623                                 deref_ref, recurse, pretty);
624       value_free_to_mark (mark);
625       return retn;
626     }
627
628   valaddr = ada_aligned_value_addr (type, valaddr);
629   embedded_offset -= valaddr - valaddr0 - embedded_offset;
630   type = printable_val_type (type, valaddr);
631
632   switch (TYPE_CODE (type))
633     {
634     default:
635       return c_val_print (type, valaddr0, embedded_offset, address, stream,
636                           format, deref_ref, recurse, pretty);
637
638     case TYPE_CODE_PTR:
639       {
640         int ret = c_val_print (type, valaddr0, embedded_offset, address, 
641                                stream, format, deref_ref, recurse, pretty);
642         if (ada_is_tag_type (type))
643           {
644             struct value *val = 
645               value_from_contents_and_address (type, valaddr, address);
646             const char *name = ada_tag_name (val);
647             if (name != NULL) 
648               fprintf_filtered (stream, " (%s)", name);
649             return 0;
650         }
651         return ret;
652       }
653
654     case TYPE_CODE_INT:
655     case TYPE_CODE_RANGE:
656       if (ada_is_fixed_point_type (type))
657         {
658           LONGEST v = unpack_long (type, valaddr);
659           int len = TYPE_LENGTH (type);
660
661           fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
662                             (double) ada_fixed_to_float (type, v));
663           return 0;
664         }
665       else if (ada_is_vax_floating_type (type))
666         {
667           struct value *val =
668             value_from_contents_and_address (type, valaddr, address);
669           struct value *func = ada_vax_float_print_function (type);
670           if (func != 0)
671             {
672               static struct type *parray_of_char = NULL;
673               struct value *printable_val;
674
675               if (parray_of_char == NULL)
676                 parray_of_char =
677                   make_pointer_type
678                   (create_array_type
679                    (NULL, builtin_type_char,
680                     create_range_type (NULL, builtin_type_int, 0, 32)), NULL);
681
682               printable_val =
683                 value_ind (value_cast (parray_of_char,
684                                        call_function_by_hand (func, 1,
685                                                               &val)));
686
687               fprintf_filtered (stream, "%s", value_contents (printable_val));
688               return 0;
689             }
690           /* No special printing function.  Do as best we can.  */
691         }
692       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
693         {
694           struct type *target_type = TYPE_TARGET_TYPE (type);
695           if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
696             {
697               /* Obscure case of range type that has different length from
698                  its base type.  Perform a conversion, or we will get a
699                  nonsense value.  Actually, we could use the same
700                  code regardless of lengths; I'm just avoiding a cast.  */
701               struct value *v = value_cast (target_type,
702                                             value_from_contents_and_address
703                                             (type, valaddr, 0));
704               return ada_val_print_1 (target_type, value_contents (v), 0, 0,
705                                       stream, format, 0, recurse + 1, pretty);
706             }
707           else
708             return ada_val_print_1 (TYPE_TARGET_TYPE (type),
709                                     valaddr0, embedded_offset,
710                                     address, stream, format, deref_ref,
711                                     recurse, pretty);
712         }
713       else
714         {
715           format = format ? format : output_format;
716           if (format)
717             {
718               print_scalar_formatted (valaddr, type, format, 0, stream);
719             }
720           else if (ada_is_system_address_type (type))
721             {
722               /* FIXME: We want to print System.Address variables using
723                  the same format as for any access type.  But for some
724                  reason GNAT encodes the System.Address type as an int,
725                  so we have to work-around this deficiency by handling
726                  System.Address values as a special case.  */
727               fprintf_filtered (stream, "(");
728               type_print (type, "", stream, -1);
729               fprintf_filtered (stream, ") ");
730               deprecated_print_address_numeric 
731                 (extract_typed_address (valaddr, builtin_type_void_data_ptr),
732                  1, stream);
733             }
734           else
735             {
736               val_print_type_code_int (type, valaddr, stream);
737               if (ada_is_character_type (type))
738                 {
739                   fputs_filtered (" ", stream);
740                   ada_printchar ((unsigned char) unpack_long (type, valaddr),
741                                  stream);
742                 }
743             }
744           return 0;
745         }
746
747     case TYPE_CODE_ENUM:
748       if (format)
749         {
750           print_scalar_formatted (valaddr, type, format, 0, stream);
751           break;
752         }
753       len = TYPE_NFIELDS (type);
754       val = unpack_long (type, valaddr);
755       for (i = 0; i < len; i++)
756         {
757           QUIT;
758           if (val == TYPE_FIELD_BITPOS (type, i))
759             {
760               break;
761             }
762         }
763       if (i < len)
764         {
765           const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
766           if (name[0] == '\'')
767             fprintf_filtered (stream, "%ld %s", (long) val, name);
768           else
769             fputs_filtered (name, stream);
770         }
771       else
772         {
773           print_longest (stream, 'd', 0, val);
774         }
775       break;
776
777     case TYPE_CODE_FLAGS:
778       if (format)
779           print_scalar_formatted (valaddr, type, format, 0, stream);
780       else
781         val_print_type_code_flags (type, valaddr, stream);
782       break;
783
784     case TYPE_CODE_FLT:
785       if (format)
786         return c_val_print (type, valaddr0, embedded_offset, address, stream,
787                             format, deref_ref, recurse, pretty);
788       else
789         ada_print_floating (valaddr0 + embedded_offset, type, stream);
790       break;
791
792     case TYPE_CODE_UNION:
793     case TYPE_CODE_STRUCT:
794       if (ada_is_bogus_array_descriptor (type))
795         {
796           fprintf_filtered (stream, "(...?)");
797           return 0;
798         }
799       else
800         {
801           print_record (type, valaddr, stream, format, recurse, pretty);
802           return 0;
803         }
804
805     case TYPE_CODE_ARRAY:
806       elttype = TYPE_TARGET_TYPE (type);
807       if (elttype == NULL)
808         eltlen = 0;
809       else
810         eltlen = TYPE_LENGTH (elttype);
811       /* FIXME: This doesn't deal with non-empty arrays of
812          0-length items (not a typical case!) */
813       if (eltlen == 0)
814         len = 0;
815       else
816         len = TYPE_LENGTH (type) / eltlen;
817
818           /* For an array of chars, print with string syntax.  */
819       if (ada_is_string_type (type) && (format == 0 || format == 's'))
820         {
821           if (prettyprint_arrays)
822             {
823               print_spaces_filtered (2 + 2 * recurse, stream);
824             }
825           /* If requested, look for the first null char and only print
826              elements up to it.  */
827           if (stop_print_at_null)
828             {
829               int temp_len;
830
831               /* Look for a NULL char.  */
832               for (temp_len = 0;
833                    temp_len < len && temp_len < print_max
834                      && char_at (valaddr, temp_len, eltlen) != 0;
835                    temp_len += 1);
836               len = temp_len;
837             }
838
839           printstr (stream, valaddr, len, 0, eltlen);
840         }
841       else
842         {
843           len = 0;
844           fprintf_filtered (stream, "(");
845           print_optional_low_bound (stream, type);
846           if (TYPE_FIELD_BITSIZE (type, 0) > 0)
847             val_print_packed_array_elements (type, valaddr, 0, stream,
848                                              format, recurse, pretty);
849           else
850             val_print_array_elements (type, valaddr, address, stream,
851                                       format, deref_ref, recurse,
852                                       pretty, 0);
853           fprintf_filtered (stream, ")");
854         }
855       gdb_flush (stream);
856       return len;
857
858     case TYPE_CODE_REF:
859       elttype = check_typedef (TYPE_TARGET_TYPE (type));
860       /* De-reference the reference */
861       if (deref_ref)
862         {
863           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
864             {
865               LONGEST deref_val_int = (LONGEST)
866                 unpack_pointer (lookup_pointer_type (builtin_type_void),
867                                 valaddr);
868               if (deref_val_int != 0)
869                 {
870                   struct value *deref_val =
871                     ada_value_ind (value_from_longest
872                                    (lookup_pointer_type (elttype),
873                                     deref_val_int));
874                   val_print (value_type (deref_val),
875                              value_contents (deref_val), 0,
876                              VALUE_ADDRESS (deref_val), stream, format,
877                              deref_ref, recurse + 1, pretty);
878                 }
879               else
880                 fputs_filtered ("(null)", stream);
881             }
882           else
883             fputs_filtered ("???", stream);
884         }
885       break;
886     }
887   gdb_flush (stream);
888   return 0;
889 }
890
891 static int
892 print_variant_part (struct type *type, int field_num, const gdb_byte *valaddr,
893                     struct ui_file *stream, int format, int recurse,
894                     enum val_prettyprint pretty, int comma_needed,
895                     struct type *outer_type, const gdb_byte *outer_valaddr)
896 {
897   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
898   int which = ada_which_variant_applies (var_type, outer_type, outer_valaddr);
899
900   if (which < 0)
901     return 0;
902   else
903     return print_field_values
904       (TYPE_FIELD_TYPE (var_type, which),
905        valaddr + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
906        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
907        stream, format, recurse, pretty,
908        comma_needed, outer_type, outer_valaddr);
909 }
910
911 int
912 ada_value_print (struct value *val0, struct ui_file *stream, int format,
913                  enum val_prettyprint pretty)
914 {
915   const gdb_byte *valaddr = value_contents (val0);
916   CORE_ADDR address = VALUE_ADDRESS (val0) + value_offset (val0);
917   struct type *type =
918     ada_to_fixed_type (value_type (val0), valaddr, address, NULL);
919   struct value *val =
920     value_from_contents_and_address (type, valaddr, address);
921
922   /* If it is a pointer, indicate what it points to.  */
923   if (TYPE_CODE (type) == TYPE_CODE_PTR)
924     {
925       /* Hack:  don't print (char *) for char strings.  Their
926          type is indicated by the quoted string anyway.  */
927       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) != sizeof (char)
928           || TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_INT 
929           || TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
930         {
931           fprintf_filtered (stream, "(");
932           type_print (type, "", stream, -1);
933           fprintf_filtered (stream, ") ");
934         }
935     }
936   else if (ada_is_array_descriptor_type (type))
937     {
938       fprintf_filtered (stream, "(");
939       type_print (type, "", stream, -1);
940       fprintf_filtered (stream, ") ");
941     }
942   else if (ada_is_bogus_array_descriptor (type))
943     {
944       fprintf_filtered (stream, "(");
945       type_print (type, "", stream, -1);
946       fprintf_filtered (stream, ") (...?)");
947       return 0;
948     }
949
950   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
951       && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == 0
952       && TYPE_CODE (TYPE_INDEX_TYPE (type)) == TYPE_CODE_RANGE)
953     {
954       /* This is an array of zero-length elements, that is an array
955          of null records.  This array needs to be printed by hand,
956          as the standard routine to print arrays relies on the size of
957          the array elements to be nonzero.  This is because it computes
958          the number of elements in the array by dividing the array size
959          by the array element size.  */
960       fprintf_filtered (stream, "(%d .. %d => ())",
961                         TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
962                         TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)));
963       return 0;
964     }
965   
966   return (val_print (type, value_contents (val), 0, address,
967                      stream, format, 1, 0, pretty));
968 }
969
970 static void
971 print_record (struct type *type, const gdb_byte *valaddr,
972               struct ui_file *stream, int format, int recurse,
973               enum val_prettyprint pretty)
974 {
975   type = ada_check_typedef (type);
976
977   fprintf_filtered (stream, "(");
978
979   if (print_field_values (type, valaddr, stream, format, recurse, pretty,
980                           0, type, valaddr) != 0 && pretty)
981     {
982       fprintf_filtered (stream, "\n");
983       print_spaces_filtered (2 * recurse, stream);
984     }
985
986   fprintf_filtered (stream, ")");
987 }
988
989 /* Print out fields of value at VALADDR having structure type TYPE.
990
991    TYPE, VALADDR, STREAM, FORMAT, RECURSE, and PRETTY have the
992    same meanings as in ada_print_value and ada_val_print.
993
994    OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
995    (used to get discriminant values when printing variant parts).
996
997    COMMA_NEEDED is 1 if fields have been printed at the current recursion
998    level, so that a comma is needed before any field printed by this
999    call.
1000
1001    Returns 1 if COMMA_NEEDED or any fields were printed.  */
1002
1003 static int
1004 print_field_values (struct type *type, const gdb_byte *valaddr,
1005                     struct ui_file *stream, int format, int recurse,
1006                     enum val_prettyprint pretty, int comma_needed,
1007                     struct type *outer_type, const gdb_byte *outer_valaddr)
1008 {
1009   int i, len;
1010
1011   len = TYPE_NFIELDS (type);
1012
1013   for (i = 0; i < len; i += 1)
1014     {
1015       if (ada_is_ignored_field (type, i))
1016         continue;
1017
1018       if (ada_is_wrapper_field (type, i))
1019         {
1020           comma_needed =
1021             print_field_values (TYPE_FIELD_TYPE (type, i),
1022                                 valaddr
1023                                 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1024                                 stream, format, recurse, pretty,
1025                                 comma_needed, type, valaddr);
1026           continue;
1027         }
1028       else if (ada_is_variant_part (type, i))
1029         {
1030           comma_needed =
1031             print_variant_part (type, i, valaddr,
1032                                 stream, format, recurse, pretty, comma_needed,
1033                                 outer_type, outer_valaddr);
1034           continue;
1035         }
1036
1037       if (comma_needed)
1038         fprintf_filtered (stream, ", ");
1039       comma_needed = 1;
1040
1041       if (pretty)
1042         {
1043           fprintf_filtered (stream, "\n");
1044           print_spaces_filtered (2 + 2 * recurse, stream);
1045         }
1046       else
1047         {
1048           wrap_here (n_spaces (2 + 2 * recurse));
1049         }
1050       if (inspect_it)
1051         {
1052           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
1053             fputs_filtered ("\"( ptr \"", stream);
1054           else
1055             fputs_filtered ("\"( nodef \"", stream);
1056           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1057                                    language_cplus, DMGL_NO_OPTS);
1058           fputs_filtered ("\" \"", stream);
1059           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
1060                                    language_cplus, DMGL_NO_OPTS);
1061           fputs_filtered ("\") \"", stream);
1062         }
1063       else
1064         {
1065           annotate_field_begin (TYPE_FIELD_TYPE (type, i));
1066           fprintf_filtered (stream, "%.*s",
1067                             ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
1068                             TYPE_FIELD_NAME (type, i));
1069           annotate_field_name_end ();
1070           fputs_filtered (" => ", stream);
1071           annotate_field_value ();
1072         }
1073
1074       if (TYPE_FIELD_PACKED (type, i))
1075         {
1076           struct value *v;
1077
1078           /* Bitfields require special handling, especially due to byte
1079              order problems.  */
1080           if (TYPE_CPLUS_SPECIFIC (type) != NULL
1081               && TYPE_FIELD_IGNORE (type, i))
1082             {
1083               fputs_filtered (_("<optimized out or zero length>"), stream);
1084             }
1085           else
1086             {
1087               int bit_pos = TYPE_FIELD_BITPOS (type, i);
1088               int bit_size = TYPE_FIELD_BITSIZE (type, i);
1089
1090               adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
1091               v = ada_value_primitive_packed_val (NULL, valaddr,
1092                                                   bit_pos / HOST_CHAR_BIT,
1093                                                   bit_pos % HOST_CHAR_BIT,
1094                                                   bit_size,
1095                                                   TYPE_FIELD_TYPE (type, i));
1096               val_print (TYPE_FIELD_TYPE (type, i), value_contents (v), 0, 0,
1097                          stream, format, 0, recurse + 1, pretty);
1098             }
1099         }
1100       else
1101         ada_val_print (TYPE_FIELD_TYPE (type, i),
1102                        valaddr + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
1103                        0, 0, stream, format, 0, recurse + 1, pretty);
1104       annotate_field_end ();
1105     }
1106
1107   return comma_needed;
1108 }