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