]> Git Repo - binutils.git/blob - gdb/ada-valprint.c
2003-04-28 Andrew Cagney <[email protected]>
[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              Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include <ctype.h>
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "demangle.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "annotate.h"
31 #include "ada-lang.h"
32 #include "c-lang.h"
33 #include "infcall.h"
34
35 /* Encapsulates arguments to ada_val_print. */
36 struct ada_val_print_args
37 {
38   struct type *type;
39   char *valaddr0;
40   int embedded_offset;
41   CORE_ADDR address;
42   struct ui_file *stream;
43   int format;
44   int deref_ref;
45   int recurse;
46   enum val_prettyprint pretty;
47 };
48
49 extern int inspect_it;
50 extern unsigned int repeat_count_threshold;
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 \f
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 array type, prints its lower bound on STREAM,
87    if non-standard (i.e., other than 1 for numbers, other than lower bound
88    of index type for enumerated type). Returns 1 if something printed, 
89    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       index_type = TYPE_TARGET_TYPE (index_type);
106     }
107   else
108     return 0;
109
110   switch (TYPE_CODE (index_type))
111     {
112     case TYPE_CODE_ENUM:
113       if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
114         return 0;
115       break;
116     case TYPE_CODE_UNDEF:
117       index_type = builtin_type_long;
118       /* FALL THROUGH */
119     default:
120       if (low_bound == 1)
121         return 0;
122       break;
123     }
124
125   ada_print_scalar (index_type, (LONGEST) low_bound, stream);
126   fprintf_filtered (stream, " => ");
127   return 1;
128 }
129
130 /*  Version of val_print_array_elements for GNAT-style packed arrays.
131     Prints elements of packed array of type TYPE at bit offset
132     BITOFFSET from VALADDR on STREAM.  Formats according to FORMAT and
133     separates with commas. RECURSE is the recursion (nesting) level.
134     If PRETTY, uses "prettier" format. TYPE must have been decoded (as
135     by ada_coerce_to_simple_array).  */
136
137 static void
138 val_print_packed_array_elements (struct type *type, char *valaddr,
139                                  int bitoffset, struct ui_file *stream,
140                                  int format, int recurse,
141                                  enum val_prettyprint pretty)
142 {
143   unsigned int i;
144   unsigned int things_printed = 0;
145   unsigned len;
146   struct type *elttype;
147   unsigned eltlen;
148   /* Position of the array element we are examining to see
149      whether it is repeated.  */
150   unsigned int rep1;
151   /* Number of repetitions we have detected so far.  */
152   unsigned int reps;
153   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
154   struct value *mark = value_mark ();
155
156   elttype = TYPE_TARGET_TYPE (type);
157   eltlen = TYPE_LENGTH (check_typedef (elttype));
158
159   {
160     LONGEST low, high;
161     if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
162       len = 1;
163     else
164       len = high - low + 1;
165   }
166
167   i = 0;
168   annotate_array_section_begin (i, elttype);
169
170   while (i < len && things_printed < print_max)
171     {
172       struct value *v0, *v1;
173       int i0;
174
175       if (i != 0)
176         {
177           if (prettyprint_arrays)
178             {
179               fprintf_filtered (stream, ",\n");
180               print_spaces_filtered (2 + 2 * recurse, stream);
181             }
182           else
183             {
184               fprintf_filtered (stream, ", ");
185             }
186         }
187       wrap_here (n_spaces (2 + 2 * recurse));
188
189       i0 = i;
190       v0 = ada_value_primitive_packed_val (NULL, valaddr,
191                                            (i0 * bitsize) / HOST_CHAR_BIT,
192                                            (i0 * bitsize) % HOST_CHAR_BIT,
193                                            bitsize, elttype);
194       while (1)
195         {
196           i += 1;
197           if (i >= len)
198             break;
199           v1 = ada_value_primitive_packed_val (NULL, valaddr,
200                                                (i * bitsize) / HOST_CHAR_BIT,
201                                                (i * bitsize) % HOST_CHAR_BIT,
202                                                bitsize, elttype);
203           if (memcmp (VALUE_CONTENTS (v0), VALUE_CONTENTS (v1), eltlen) != 0)
204             break;
205         }
206
207       if (i - i0 > repeat_count_threshold)
208         {
209           val_print (elttype, VALUE_CONTENTS (v0), 0, 0, stream, format,
210                      0, recurse + 1, pretty);
211           annotate_elt_rep (i - i0);
212           fprintf_filtered (stream, " <repeats %u times>", i - i0);
213           annotate_elt_rep_end ();
214
215         }
216       else
217         {
218           int j;
219           for (j = i0; j < i; j += 1)
220             {
221               if (j > i0)
222                 {
223                   if (prettyprint_arrays)
224                     {
225                       fprintf_filtered (stream, ",\n");
226                       print_spaces_filtered (2 + 2 * recurse, stream);
227                     }
228                   else
229                     {
230                       fprintf_filtered (stream, ", ");
231                     }
232                   wrap_here (n_spaces (2 + 2 * recurse));
233                 }
234               val_print (elttype, VALUE_CONTENTS (v0), 0, 0, stream, format,
235                          0, recurse + 1, pretty);
236               annotate_elt ();
237             }
238         }
239       things_printed += i - i0;
240     }
241   annotate_array_section_end ();
242   if (i < len)
243     {
244       fprintf_filtered (stream, "...");
245     }
246
247   value_free_to_mark (mark);
248 }
249
250 static struct type *
251 printable_val_type (struct type *type, char *valaddr)
252 {
253   return ada_to_fixed_type (ada_aligned_type (type), valaddr, 0, NULL);
254 }
255
256 /* Print the character C on STREAM as part of the contents of a literal
257    string whose delimiter is QUOTER.  TYPE_LEN is the length in bytes
258    (1 or 2) of the character. */
259
260 void
261 ada_emit_char (int c, struct ui_file *stream, int quoter, int type_len)
262 {
263   if (type_len != 2)
264     type_len = 1;
265
266   c &= (1 << (type_len * TARGET_CHAR_BIT)) - 1;
267
268   if (isascii (c) && isprint (c))
269     {
270       if (c == quoter && c == '"')
271         fprintf_filtered (stream, "[\"%c\"]", quoter);
272       else
273         fprintf_filtered (stream, "%c", c);
274     }
275   else
276     fprintf_filtered (stream, "[\"%0*x\"]", type_len * 2, c);
277 }
278
279 /* Character #I of STRING, given that TYPE_LEN is the size in bytes (1
280    or 2) of a character. */
281
282 static int
283 char_at (char *string, int i, int type_len)
284 {
285   if (type_len == 1)
286     return string[i];
287   else
288     return (int) extract_unsigned_integer (string + 2 * i, 2);
289 }
290
291 void
292 ada_printchar (int c, struct ui_file *stream)
293 {
294   fputs_filtered ("'", stream);
295   ada_emit_char (c, stream, '\'', 1);
296   fputs_filtered ("'", stream);
297 }
298
299 /* [From print_type_scalar in typeprint.c].   Print VAL on STREAM in a
300    form appropriate for TYPE. */
301
302 void
303 ada_print_scalar (struct type *type, LONGEST val, struct ui_file *stream)
304 {
305   unsigned int i;
306   unsigned len;
307
308   CHECK_TYPEDEF (type);
309
310   switch (TYPE_CODE (type))
311     {
312
313     case TYPE_CODE_ENUM:
314       len = TYPE_NFIELDS (type);
315       for (i = 0; i < len; i++)
316         {
317           if (TYPE_FIELD_BITPOS (type, i) == val)
318             {
319               break;
320             }
321         }
322       if (i < len)
323         {
324           fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
325         }
326       else
327         {
328           print_longest (stream, 'd', 0, val);
329         }
330       break;
331
332     case TYPE_CODE_INT:
333       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
334       break;
335
336     case TYPE_CODE_CHAR:
337       LA_PRINT_CHAR ((unsigned char) val, stream);
338       break;
339
340     case TYPE_CODE_BOOL:
341       fprintf_filtered (stream, val ? "true" : "false");
342       break;
343
344     case TYPE_CODE_RANGE:
345       ada_print_scalar (TYPE_TARGET_TYPE (type), val, stream);
346       return;
347
348     case TYPE_CODE_UNDEF:
349     case TYPE_CODE_PTR:
350     case TYPE_CODE_ARRAY:
351     case TYPE_CODE_STRUCT:
352     case TYPE_CODE_UNION:
353     case TYPE_CODE_FUNC:
354     case TYPE_CODE_FLT:
355     case TYPE_CODE_VOID:
356     case TYPE_CODE_SET:
357     case TYPE_CODE_STRING:
358     case TYPE_CODE_ERROR:
359     case TYPE_CODE_MEMBER:
360     case TYPE_CODE_METHOD:
361     case TYPE_CODE_REF:
362       warning ("internal error: unhandled type in ada_print_scalar");
363       break;
364
365     default:
366       error ("Invalid type code in symbol table.");
367     }
368   gdb_flush (stream);
369 }
370
371 /* Print the character string STRING, printing at most LENGTH characters.
372    Printing stops early if the number hits print_max; repeat counts
373    are printed as appropriate.  Print ellipses at the end if we
374    had to stop before printing LENGTH characters, or if
375    FORCE_ELLIPSES.   TYPE_LEN is the length (1 or 2) of the character type.
376  */
377
378 static void
379 printstr (struct ui_file *stream, char *string, unsigned int length,
380           int force_ellipses, int type_len)
381 {
382   unsigned int i;
383   unsigned int things_printed = 0;
384   int in_quotes = 0;
385   int need_comma = 0;
386
387   if (length == 0)
388     {
389       fputs_filtered ("\"\"", stream);
390       return;
391     }
392
393   for (i = 0; i < length && things_printed < print_max; i += 1)
394     {
395       /* Position of the character we are examining
396          to see whether it is repeated.  */
397       unsigned int rep1;
398       /* Number of repetitions we have detected so far.  */
399       unsigned int reps;
400
401       QUIT;
402
403       if (need_comma)
404         {
405           fputs_filtered (", ", stream);
406           need_comma = 0;
407         }
408
409       rep1 = i + 1;
410       reps = 1;
411       while (rep1 < length &&
412              char_at (string, rep1, type_len) == char_at (string, i,
413                                                           type_len))
414         {
415           rep1 += 1;
416           reps += 1;
417         }
418
419       if (reps > repeat_count_threshold)
420         {
421           if (in_quotes)
422             {
423               if (inspect_it)
424                 fputs_filtered ("\\\", ", stream);
425               else
426                 fputs_filtered ("\", ", stream);
427               in_quotes = 0;
428             }
429           fputs_filtered ("'", stream);
430           ada_emit_char (char_at (string, i, type_len), stream, '\'',
431                          type_len);
432           fputs_filtered ("'", stream);
433           fprintf_filtered (stream, " <repeats %u times>", reps);
434           i = rep1 - 1;
435           things_printed += repeat_count_threshold;
436           need_comma = 1;
437         }
438       else
439         {
440           if (!in_quotes)
441             {
442               if (inspect_it)
443                 fputs_filtered ("\\\"", stream);
444               else
445                 fputs_filtered ("\"", stream);
446               in_quotes = 1;
447             }
448           ada_emit_char (char_at (string, i, type_len), stream, '"',
449                          type_len);
450           things_printed += 1;
451         }
452     }
453
454   /* Terminate the quotes if necessary.  */
455   if (in_quotes)
456     {
457       if (inspect_it)
458         fputs_filtered ("\\\"", stream);
459       else
460         fputs_filtered ("\"", stream);
461     }
462
463   if (force_ellipses || i < length)
464     fputs_filtered ("...", stream);
465 }
466
467 void
468 ada_printstr (struct ui_file *stream, char *string, unsigned int length,
469               int force_ellipses, int width)
470 {
471   printstr (stream, string, length, force_ellipses, width);
472 }
473
474
475 /* Print data of type TYPE located at VALADDR (within GDB), which came from
476    the inferior at address ADDRESS, onto stdio stream STREAM according to
477    FORMAT (a letter as for the printf % codes or 0 for natural format).  
478    The data at VALADDR is in target byte order.
479
480    If the data is printed as a string, returns the number of string characters
481    printed.
482
483    If DEREF_REF is nonzero, then dereference references, otherwise just print
484    them like pointers.
485
486    RECURSE indicates the amount of indentation to supply before
487    continuation lines; this amount is roughly twice the value of RECURSE.
488
489    When PRETTY is non-zero, prints record fields on separate lines.
490    (For some reason, the current version of gdb instead uses a global
491    variable---prettyprint_arrays--- to causes a similar effect on
492    arrays.)  */
493
494 int
495 ada_val_print (struct type *type, char *valaddr0, int embedded_offset,
496                CORE_ADDR address, struct ui_file *stream, int format,
497                int deref_ref, int recurse, enum val_prettyprint pretty)
498 {
499   struct ada_val_print_args args;
500   args.type = type;
501   args.valaddr0 = valaddr0;
502   args.embedded_offset = embedded_offset;
503   args.address = address;
504   args.stream = stream;
505   args.format = format;
506   args.deref_ref = deref_ref;
507   args.recurse = recurse;
508   args.pretty = pretty;
509
510   return catch_errors (ada_val_print_stub, &args, NULL, RETURN_MASK_ALL);
511 }
512
513 /* Helper for ada_val_print; used as argument to catch_errors to
514    unmarshal the arguments to ada_val_print_1, which does the work. */
515 static int
516 ada_val_print_stub (void * args0)
517 {
518   struct ada_val_print_args *argsp = (struct ada_val_print_args *) args0;
519   return ada_val_print_1 (argsp->type, argsp->valaddr0,
520                           argsp->embedded_offset, argsp->address,
521                           argsp->stream, argsp->format, argsp->deref_ref,
522                           argsp->recurse, argsp->pretty);
523 }
524
525 /* See the comment on ada_val_print.  This function differs in that it
526  * does not catch evaluation errors (leaving that to ada_val_print). */
527
528 static int
529 ada_val_print_1 (struct type *type, char *valaddr0, int embedded_offset,
530                  CORE_ADDR address, struct ui_file *stream, int format,
531                  int deref_ref, int recurse, enum val_prettyprint pretty)
532 {
533   unsigned int len;
534   int i;
535   struct type *elttype;
536   unsigned int eltlen;
537   LONGEST val;
538   CORE_ADDR addr;
539   char *valaddr = valaddr0 + embedded_offset;
540
541   CHECK_TYPEDEF (type);
542
543   if (ada_is_array_descriptor (type) || ada_is_packed_array_type (type))
544     {
545       int retn;
546       struct value *mark = value_mark ();
547       struct value *val;
548       val = value_from_contents_and_address (type, valaddr, address);
549       val = ada_coerce_to_simple_array_ptr (val);
550       if (val == NULL)
551         {
552           fprintf_filtered (stream, "(null)");
553           retn = 0;
554         }
555       else
556         retn = ada_val_print_1 (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
557                                 VALUE_ADDRESS (val), stream, format,
558                                 deref_ref, recurse, pretty);
559       value_free_to_mark (mark);
560       return retn;
561     }
562
563   valaddr = ada_aligned_value_addr (type, valaddr);
564   embedded_offset -= valaddr - valaddr0 - embedded_offset;
565   type = printable_val_type (type, valaddr);
566
567   switch (TYPE_CODE (type))
568     {
569     default:
570       return c_val_print (type, valaddr0, embedded_offset, address, stream,
571                           format, deref_ref, recurse, pretty);
572
573     case TYPE_CODE_INT:
574     case TYPE_CODE_RANGE:
575       if (ada_is_fixed_point_type (type))
576         {
577           LONGEST v = unpack_long (type, valaddr);
578           int len = TYPE_LENGTH (type);
579
580           fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
581                             (double) ada_fixed_to_float (type, v));
582           return 0;
583         }
584       else if (ada_is_vax_floating_type (type))
585         {
586           struct value *val =
587             value_from_contents_and_address (type, valaddr, address);
588           struct value *func = ada_vax_float_print_function (type);
589           if (func != 0)
590             {
591               static struct type *parray_of_char = NULL;
592               struct value *printable_val;
593
594               if (parray_of_char == NULL)
595                 parray_of_char =
596                   make_pointer_type
597                   (create_array_type
598                    (NULL, builtin_type_char,
599                     create_range_type (NULL, builtin_type_int, 0, 32)), NULL);
600
601               printable_val =
602                 value_ind (value_cast (parray_of_char,
603                                        call_function_by_hand (func, 1,
604                                                               &val)));
605
606               fprintf_filtered (stream, "%s", VALUE_CONTENTS (printable_val));
607               return 0;
608             }
609           /* No special printing function.  Do as best we can. */
610         }
611       else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
612         {
613           struct type *target_type = TYPE_TARGET_TYPE (type);
614           if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
615             {
616               /* Obscure case of range type that has different length from
617                  its base type.  Perform a conversion, or we will get a
618                  nonsense value.  Actually, we could use the same
619                  code regardless of lengths; I'm just avoiding a cast. */
620               struct value *v = value_cast (target_type,
621                                             value_from_contents_and_address
622                                             (type, valaddr, 0));
623               return ada_val_print_1 (target_type, VALUE_CONTENTS (v), 0, 0,
624                                       stream, format, 0, recurse + 1, pretty);
625             }
626           else
627             return ada_val_print_1 (TYPE_TARGET_TYPE (type),
628                                     valaddr0, embedded_offset,
629                                     address, stream, format, deref_ref,
630                                     recurse, pretty);
631         }
632       else
633         {
634           format = format ? format : output_format;
635           if (format)
636             {
637               print_scalar_formatted (valaddr, type, format, 0, stream);
638             }
639           else
640             {
641               val_print_type_code_int (type, valaddr, stream);
642               if (ada_is_character_type (type))
643                 {
644                   fputs_filtered (" ", stream);
645                   ada_printchar ((unsigned char) unpack_long (type, valaddr),
646                                  stream);
647                 }
648             }
649           return 0;
650         }
651
652     case TYPE_CODE_ENUM:
653       if (format)
654         {
655           print_scalar_formatted (valaddr, type, format, 0, stream);
656           break;
657         }
658       len = TYPE_NFIELDS (type);
659       val = unpack_long (type, valaddr);
660       for (i = 0; i < len; i++)
661         {
662           QUIT;
663           if (val == TYPE_FIELD_BITPOS (type, i))
664             {
665               break;
666             }
667         }
668       if (i < len)
669         {
670           const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
671           if (name[0] == '\'')
672             fprintf_filtered (stream, "%ld %s", (long) val, name);
673           else
674             fputs_filtered (name, stream);
675         }
676       else
677         {
678           print_longest (stream, 'd', 0, val);
679         }
680       break;
681
682     case TYPE_CODE_UNION:
683     case TYPE_CODE_STRUCT:
684       if (ada_is_bogus_array_descriptor (type))
685         {
686           fprintf_filtered (stream, "(...?)");
687           return 0;
688         }
689       else
690         {
691           print_record (type, valaddr, stream, format, recurse, pretty);
692           return 0;
693         }
694
695     case TYPE_CODE_ARRAY:
696       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
697         {
698           elttype = TYPE_TARGET_TYPE (type);
699           eltlen = TYPE_LENGTH (elttype);
700           len = TYPE_LENGTH (type) / eltlen;
701
702           /* For an array of chars, print with string syntax.  */
703           if (ada_is_string_type (type) && (format == 0 || format == 's'))
704             {
705               if (prettyprint_arrays)
706                 {
707                   print_spaces_filtered (2 + 2 * recurse, stream);
708                 }
709               /* If requested, look for the first null char and only print
710                  elements up to it.  */
711               if (stop_print_at_null)
712                 {
713                   int temp_len;
714
715                   /* Look for a NULL char. */
716                   for (temp_len = 0;
717                        temp_len < len && temp_len < print_max
718                        && char_at (valaddr, temp_len, eltlen) != 0;
719                        temp_len += 1);
720                   len = temp_len;
721                 }
722
723               printstr (stream, valaddr, len, 0, eltlen);
724             }
725           else
726             {
727               len = 0;
728               fprintf_filtered (stream, "(");
729               print_optional_low_bound (stream, type);
730               if (TYPE_FIELD_BITSIZE (type, 0) > 0)
731                 val_print_packed_array_elements (type, valaddr, 0, stream,
732                                                  format, recurse, pretty);
733               else
734                 val_print_array_elements (type, valaddr, address, stream,
735                                           format, deref_ref, recurse,
736                                           pretty, 0);
737               fprintf_filtered (stream, ")");
738             }
739           gdb_flush (stream);
740           return len;
741         }
742
743     case TYPE_CODE_REF:
744       elttype = check_typedef (TYPE_TARGET_TYPE (type));
745       if (addressprint)
746         {
747           fprintf_filtered (stream, "@");
748           print_address_numeric
749             (extract_address (valaddr,
750                               TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
751           if (deref_ref)
752             fputs_filtered (": ", stream);
753         }
754       /* De-reference the reference */
755       if (deref_ref)
756         {
757           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
758             {
759               LONGEST deref_val_int = (LONGEST)
760                 unpack_pointer (lookup_pointer_type (builtin_type_void),
761                                 valaddr);
762               if (deref_val_int != 0)
763                 {
764                   struct value *deref_val =
765                     ada_value_ind (value_from_longest
766                                    (lookup_pointer_type (elttype),
767                                     deref_val_int));
768                   val_print (VALUE_TYPE (deref_val),
769                              VALUE_CONTENTS (deref_val), 0,
770                              VALUE_ADDRESS (deref_val), stream, format,
771                              deref_ref, recurse + 1, pretty);
772                 }
773               else
774                 fputs_filtered ("(null)", stream);
775             }
776           else
777             fputs_filtered ("???", stream);
778         }
779       break;
780     }
781   return 0;
782 }
783
784 static int
785 print_variant_part (struct type *type, int field_num, char *valaddr,
786                     struct ui_file *stream, int format, int recurse,
787                     enum val_prettyprint pretty, int comma_needed,
788                     struct type *outer_type, char *outer_valaddr)
789 {
790   struct type *var_type = TYPE_FIELD_TYPE (type, field_num);
791   int which = ada_which_variant_applies (var_type, outer_type, outer_valaddr);
792
793   if (which < 0)
794     return 0;
795   else
796     return print_field_values
797       (TYPE_FIELD_TYPE (var_type, which),
798        valaddr + TYPE_FIELD_BITPOS (type, field_num) / HOST_CHAR_BIT
799        + TYPE_FIELD_BITPOS (var_type, which) / HOST_CHAR_BIT,
800        stream, format, recurse, pretty,
801        comma_needed, outer_type, outer_valaddr);
802 }
803
804 int
805 ada_value_print (struct value *val0, struct ui_file *stream, int format,
806                  enum val_prettyprint pretty)
807 {
808   char *valaddr = VALUE_CONTENTS (val0);
809   CORE_ADDR address = VALUE_ADDRESS (val0) + VALUE_OFFSET (val0);
810   struct type *type =
811     ada_to_fixed_type (VALUE_TYPE (val0), valaddr, address, NULL);
812   struct value *val =
813     value_from_contents_and_address (type, valaddr, address);
814
815   /* If it is a pointer, indicate what it points to. */
816   if (TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_CODE (type) == TYPE_CODE_REF)
817     {
818       /* Hack:  remove (char *) for char strings.  Their
819          type is indicated by the quoted string anyway. */
820       if (TYPE_CODE (type) == TYPE_CODE_PTR &&
821           TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof (char) &&
822           TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
823           !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
824         {
825           /* Print nothing */
826         }
827       else
828         {
829           fprintf_filtered (stream, "(");
830           type_print (type, "", stream, -1);
831           fprintf_filtered (stream, ") ");
832         }
833     }
834   else if (ada_is_array_descriptor (type))
835     {
836       fprintf_filtered (stream, "(");
837       type_print (type, "", stream, -1);
838       fprintf_filtered (stream, ") ");
839     }
840   else if (ada_is_bogus_array_descriptor (type))
841     {
842       fprintf_filtered (stream, "(");
843       type_print (type, "", stream, -1);
844       fprintf_filtered (stream, ") (...?)");
845       return 0;
846     }
847   return (val_print (type, VALUE_CONTENTS (val), 0, address,
848                      stream, format, 1, 0, pretty));
849 }
850
851 static void
852 print_record (struct type *type, char *valaddr, struct ui_file *stream,
853               int format, int recurse, enum val_prettyprint pretty)
854 {
855   CHECK_TYPEDEF (type);
856
857   fprintf_filtered (stream, "(");
858
859   if (print_field_values (type, valaddr, stream, format, recurse, pretty,
860                           0, type, valaddr) != 0 && pretty)
861     {
862       fprintf_filtered (stream, "\n");
863       print_spaces_filtered (2 * recurse, stream);
864     }
865
866   fprintf_filtered (stream, ")");
867 }
868
869 /* Print out fields of value at VALADDR having structure type TYPE.
870   
871    TYPE, VALADDR, STREAM, FORMAT, RECURSE, and PRETTY have the
872    same meanings as in ada_print_value and ada_val_print.   
873
874    OUTER_TYPE and OUTER_VALADDR give type and address of enclosing record
875    (used to get discriminant values when printing variant parts).
876
877    COMMA_NEEDED is 1 if fields have been printed at the current recursion 
878    level, so that a comma is needed before any field printed by this
879    call. 
880
881    Returns 1 if COMMA_NEEDED or any fields were printed. */
882
883 static int
884 print_field_values (struct type *type, char *valaddr, struct ui_file *stream,
885                     int format, int recurse, enum val_prettyprint pretty,
886                     int comma_needed, struct type *outer_type,
887                     char *outer_valaddr)
888 {
889   int i, len;
890
891   len = TYPE_NFIELDS (type);
892
893   for (i = 0; i < len; i += 1)
894     {
895       if (ada_is_ignored_field (type, i))
896         continue;
897
898       if (ada_is_wrapper_field (type, i))
899         {
900           comma_needed =
901             print_field_values (TYPE_FIELD_TYPE (type, i),
902                                 valaddr
903                                 + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
904                                 stream, format, recurse, pretty,
905                                 comma_needed, type, valaddr);
906           continue;
907         }
908       else if (ada_is_variant_part (type, i))
909         {
910           comma_needed =
911             print_variant_part (type, i, valaddr,
912                                 stream, format, recurse, pretty, comma_needed,
913                                 outer_type, outer_valaddr);
914           continue;
915         }
916
917       if (comma_needed)
918         fprintf_filtered (stream, ", ");
919       comma_needed = 1;
920
921       if (pretty)
922         {
923           fprintf_filtered (stream, "\n");
924           print_spaces_filtered (2 + 2 * recurse, stream);
925         }
926       else
927         {
928           wrap_here (n_spaces (2 + 2 * recurse));
929         }
930       if (inspect_it)
931         {
932           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
933             fputs_filtered ("\"( ptr \"", stream);
934           else
935             fputs_filtered ("\"( nodef \"", stream);
936           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
937                                    language_cplus, DMGL_NO_OPTS);
938           fputs_filtered ("\" \"", stream);
939           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
940                                    language_cplus, DMGL_NO_OPTS);
941           fputs_filtered ("\") \"", stream);
942         }
943       else
944         {
945           annotate_field_begin (TYPE_FIELD_TYPE (type, i));
946           fprintf_filtered (stream, "%.*s",
947                             ada_name_prefix_len (TYPE_FIELD_NAME (type, i)),
948                             TYPE_FIELD_NAME (type, i));
949           annotate_field_name_end ();
950           fputs_filtered (" => ", stream);
951           annotate_field_value ();
952         }
953
954       if (TYPE_FIELD_PACKED (type, i))
955         {
956           struct value *v;
957
958           /* Bitfields require special handling, especially due to byte
959              order problems.  */
960           if (TYPE_CPLUS_SPECIFIC (type) != NULL
961               && TYPE_FIELD_IGNORE (type, i))
962             {
963               fputs_filtered ("<optimized out or zero length>", stream);
964             }
965           else
966             {
967               int bit_pos = TYPE_FIELD_BITPOS (type, i);
968               int bit_size = TYPE_FIELD_BITSIZE (type, i);
969
970               adjust_type_signedness (TYPE_FIELD_TYPE (type, i));
971               v = ada_value_primitive_packed_val (NULL, valaddr,
972                                                   bit_pos / HOST_CHAR_BIT,
973                                                   bit_pos % HOST_CHAR_BIT,
974                                                   bit_size,
975                                                   TYPE_FIELD_TYPE (type, i));
976               val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
977                          stream, format, 0, recurse + 1, pretty);
978             }
979         }
980       else
981         ada_val_print (TYPE_FIELD_TYPE (type, i),
982                        valaddr + TYPE_FIELD_BITPOS (type, i) / HOST_CHAR_BIT,
983                        0, 0, stream, format, 0, recurse + 1, pretty);
984       annotate_field_end ();
985     }
986
987   return comma_needed;
988 }
This page took 0.080813 seconds and 4 git commands to generate.