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