Handle partially optimized out values similarly to unavailable values
[external/binutils.git] / gdb / p-valprint.c
1 /* Support for printing Pascal values for GDB, the GNU debugger.
2
3    Copyright (C) 2000-2014 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 /* This file is derived from c-valprint.c */
21
22 #include "defs.h"
23 #include "gdb_obstack.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "gdbcore.h"
31 #include "demangle.h"
32 #include "valprint.h"
33 #include "typeprint.h"
34 #include "language.h"
35 #include "target.h"
36 #include "annotate.h"
37 #include "p-lang.h"
38 #include "cp-abi.h"
39 #include "cp-support.h"
40 #include "exceptions.h"
41 #include "objfiles.h"
42 \f
43
44 /* Decorations for Pascal.  */
45
46 static const struct generic_val_print_decorations p_decorations =
47 {
48   "",
49   " + ",
50   " * I",
51   "true",
52   "false",
53   "void"
54 };
55
56 /* See val_print for a description of the various parameters of this
57    function; they are identical.  */
58
59 void
60 pascal_val_print (struct type *type, const gdb_byte *valaddr,
61                   int embedded_offset, CORE_ADDR address,
62                   struct ui_file *stream, int recurse,
63                   const struct value *original_value,
64                   const struct value_print_options *options)
65 {
66   struct gdbarch *gdbarch = get_type_arch (type);
67   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
68   unsigned int i = 0;   /* Number of characters printed */
69   unsigned len;
70   LONGEST low_bound, high_bound;
71   struct type *elttype;
72   unsigned eltlen;
73   int length_pos, length_size, string_pos;
74   struct type *char_type;
75   CORE_ADDR addr;
76   int want_space = 0;
77
78   CHECK_TYPEDEF (type);
79   switch (TYPE_CODE (type))
80     {
81     case TYPE_CODE_ARRAY:
82       if (get_array_bounds (type, &low_bound, &high_bound))
83         {
84           len = high_bound - low_bound + 1;
85           elttype = check_typedef (TYPE_TARGET_TYPE (type));
86           eltlen = TYPE_LENGTH (elttype);
87           if (options->prettyformat_arrays)
88             {
89               print_spaces_filtered (2 + 2 * recurse, stream);
90             }
91           /* If 's' format is used, try to print out as string.
92              If no format is given, print as string if element type
93              is of TYPE_CODE_CHAR and element size is 1,2 or 4.  */
94           if (options->format == 's'
95               || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
96                   && TYPE_CODE (elttype) == TYPE_CODE_CHAR
97                   && options->format == 0))
98             {
99               /* If requested, look for the first null char and only print
100                  elements up to it.  */
101               if (options->stop_print_at_null)
102                 {
103                   unsigned int temp_len;
104
105                   /* Look for a NULL char.  */
106                   for (temp_len = 0;
107                        extract_unsigned_integer (valaddr + embedded_offset +
108                                                  temp_len * eltlen, eltlen,
109                                                  byte_order)
110                        && temp_len < len && temp_len < options->print_max;
111                        temp_len++);
112                   len = temp_len;
113                 }
114
115               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
116                                valaddr + embedded_offset, len, NULL, 0,
117                                options);
118               i = len;
119             }
120           else
121             {
122               fprintf_filtered (stream, "{");
123               /* If this is a virtual function table, print the 0th
124                  entry specially, and the rest of the members normally.  */
125               if (pascal_object_is_vtbl_ptr_type (elttype))
126                 {
127                   i = 1;
128                   fprintf_filtered (stream, "%d vtable entries", len - 1);
129                 }
130               else
131                 {
132                   i = 0;
133                 }
134               val_print_array_elements (type, valaddr, embedded_offset,
135                                         address, stream, recurse,
136                                         original_value, options, i);
137               fprintf_filtered (stream, "}");
138             }
139           break;
140         }
141       /* Array of unspecified length: treat like pointer to first elt.  */
142       addr = address + embedded_offset;
143       goto print_unpacked_pointer;
144
145     case TYPE_CODE_PTR:
146       if (options->format && options->format != 's')
147         {
148           val_print_scalar_formatted (type, valaddr, embedded_offset,
149                                       original_value, options, 0, stream);
150           break;
151         }
152       if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
153         {
154           /* Print the unmangled name if desired.  */
155           /* Print vtable entry - we only get here if we ARE using
156              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.)  */
157           /* Extract the address, assume that it is unsigned.  */
158           addr = extract_unsigned_integer (valaddr + embedded_offset,
159                                            TYPE_LENGTH (type), byte_order);
160           print_address_demangle (options, gdbarch, addr, stream, demangle);
161           break;
162         }
163       check_typedef (TYPE_TARGET_TYPE (type));
164
165       addr = unpack_pointer (type, valaddr + embedded_offset);
166     print_unpacked_pointer:
167       elttype = check_typedef (TYPE_TARGET_TYPE (type));
168
169       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
170         {
171           /* Try to print what function it points to.  */
172           print_address_demangle (options, gdbarch, addr, stream, demangle);
173           return;
174         }
175
176       if (options->addressprint && options->format != 's')
177         {
178           fputs_filtered (paddress (gdbarch, addr), stream);
179           want_space = 1;
180         }
181
182       /* For a pointer to char or unsigned char, also print the string
183          pointed to, unless pointer is null.  */
184       if (((TYPE_LENGTH (elttype) == 1
185            && (TYPE_CODE (elttype) == TYPE_CODE_INT
186               || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
187           || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
188               && TYPE_CODE (elttype) == TYPE_CODE_CHAR))
189           && (options->format == 0 || options->format == 's')
190           && addr != 0)
191         {
192           if (want_space)
193             fputs_filtered (" ", stream);
194           /* No wide string yet.  */
195           i = val_print_string (elttype, NULL, addr, -1, stream, options);
196         }
197       /* Also for pointers to pascal strings.  */
198       /* Note: this is Free Pascal specific:
199          as GDB does not recognize stabs pascal strings
200          Pascal strings are mapped to records
201          with lowercase names PM.  */
202       if (is_pascal_string_type (elttype, &length_pos, &length_size,
203                                  &string_pos, &char_type, NULL)
204           && addr != 0)
205         {
206           ULONGEST string_length;
207           void *buffer;
208
209           if (want_space)
210             fputs_filtered (" ", stream);
211           buffer = xmalloc (length_size);
212           read_memory (addr + length_pos, buffer, length_size);
213           string_length = extract_unsigned_integer (buffer, length_size,
214                                                     byte_order);
215           xfree (buffer);
216           i = val_print_string (char_type, NULL,
217                                 addr + string_pos, string_length,
218                                 stream, options);
219         }
220       else if (pascal_object_is_vtbl_member (type))
221         {
222           /* Print vtbl's nicely.  */
223           CORE_ADDR vt_address = unpack_pointer (type,
224                                                  valaddr + embedded_offset);
225           struct bound_minimal_symbol msymbol =
226             lookup_minimal_symbol_by_pc (vt_address);
227
228           /* If 'symbol_print' is set, we did the work above.  */
229           if (!options->symbol_print
230               && (msymbol.minsym != NULL)
231               && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
232             {
233               if (want_space)
234                 fputs_filtered (" ", stream);
235               fputs_filtered ("<", stream);
236               fputs_filtered (MSYMBOL_PRINT_NAME (msymbol.minsym), stream);
237               fputs_filtered (">", stream);
238               want_space = 1;
239             }
240           if (vt_address && options->vtblprint)
241             {
242               struct value *vt_val;
243               struct symbol *wsym = (struct symbol *) NULL;
244               struct type *wtype;
245               struct block *block = (struct block *) NULL;
246               struct field_of_this_result is_this_fld;
247
248               if (want_space)
249                 fputs_filtered (" ", stream);
250
251               if (msymbol.minsym != NULL)
252                 wsym = lookup_symbol (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
253                                       block,
254                                       VAR_DOMAIN, &is_this_fld);
255
256               if (wsym)
257                 {
258                   wtype = SYMBOL_TYPE (wsym);
259                 }
260               else
261                 {
262                   wtype = TYPE_TARGET_TYPE (type);
263                 }
264               vt_val = value_at (wtype, vt_address);
265               common_val_print (vt_val, stream, recurse + 1, options,
266                                 current_language);
267               if (options->prettyformat)
268                 {
269                   fprintf_filtered (stream, "\n");
270                   print_spaces_filtered (2 + 2 * recurse, stream);
271                 }
272             }
273         }
274
275       return;
276
277     case TYPE_CODE_REF:
278     case TYPE_CODE_ENUM:
279     case TYPE_CODE_FLAGS:
280     case TYPE_CODE_FUNC:
281     case TYPE_CODE_RANGE:
282     case TYPE_CODE_INT:
283     case TYPE_CODE_FLT:
284     case TYPE_CODE_VOID:
285     case TYPE_CODE_ERROR:
286     case TYPE_CODE_UNDEF:
287     case TYPE_CODE_BOOL:
288     case TYPE_CODE_CHAR:
289       generic_val_print (type, valaddr, embedded_offset, address,
290                          stream, recurse, original_value, options,
291                          &p_decorations);
292       break;
293
294     case TYPE_CODE_UNION:
295       if (recurse && !options->unionprint)
296         {
297           fprintf_filtered (stream, "{...}");
298           break;
299         }
300       /* Fall through.  */
301     case TYPE_CODE_STRUCT:
302       if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
303         {
304           /* Print the unmangled name if desired.  */
305           /* Print vtable entry - we only get here if NOT using
306              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_PTR.)  */
307           /* Extract the address, assume that it is unsigned.  */
308           print_address_demangle
309             (options, gdbarch,
310              extract_unsigned_integer (valaddr + embedded_offset
311                                        + TYPE_FIELD_BITPOS (type,
312                                                             VTBL_FNADDR_OFFSET) / 8,
313                                        TYPE_LENGTH (TYPE_FIELD_TYPE (type,
314                                                                      VTBL_FNADDR_OFFSET)),
315                                        byte_order),
316              stream, demangle);
317         }
318       else
319         {
320           if (is_pascal_string_type (type, &length_pos, &length_size,
321                                      &string_pos, &char_type, NULL))
322             {
323               len = extract_unsigned_integer (valaddr + embedded_offset
324                                               + length_pos, length_size,
325                                               byte_order);
326               LA_PRINT_STRING (stream, char_type,
327                                valaddr + embedded_offset + string_pos,
328                                len, NULL, 0, options);
329             }
330           else
331             pascal_object_print_value_fields (type, valaddr, embedded_offset,
332                                               address, stream, recurse,
333                                               original_value, options,
334                                               NULL, 0);
335         }
336       break;
337
338     case TYPE_CODE_SET:
339       elttype = TYPE_INDEX_TYPE (type);
340       CHECK_TYPEDEF (elttype);
341       if (TYPE_STUB (elttype))
342         {
343           fprintf_filtered (stream, "<incomplete type>");
344           gdb_flush (stream);
345           break;
346         }
347       else
348         {
349           struct type *range = elttype;
350           LONGEST low_bound, high_bound;
351           int i;
352           int need_comma = 0;
353
354           fputs_filtered ("[", stream);
355
356           i = get_discrete_bounds (range, &low_bound, &high_bound);
357           if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
358             {
359               /* If we know the size of the set type, we can figure out the
360               maximum value.  */
361               i = 0;
362               high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
363               TYPE_HIGH_BOUND (range) = high_bound;
364             }
365         maybe_bad_bstring:
366           if (i < 0)
367             {
368               fputs_filtered ("<error value>", stream);
369               goto done;
370             }
371
372           for (i = low_bound; i <= high_bound; i++)
373             {
374               int element = value_bit_index (type,
375                                              valaddr + embedded_offset, i);
376
377               if (element < 0)
378                 {
379                   i = element;
380                   goto maybe_bad_bstring;
381                 }
382               if (element)
383                 {
384                   if (need_comma)
385                     fputs_filtered (", ", stream);
386                   print_type_scalar (range, i, stream);
387                   need_comma = 1;
388
389                   if (i + 1 <= high_bound
390                       && value_bit_index (type,
391                                           valaddr + embedded_offset, ++i))
392                     {
393                       int j = i;
394
395                       fputs_filtered ("..", stream);
396                       while (i + 1 <= high_bound
397                              && value_bit_index (type,
398                                                  valaddr + embedded_offset,
399                                                  ++i))
400                         j = i;
401                       print_type_scalar (range, j, stream);
402                     }
403                 }
404             }
405         done:
406           fputs_filtered ("]", stream);
407         }
408       break;
409
410     default:
411       error (_("Invalid pascal type code %d in symbol table."),
412              TYPE_CODE (type));
413     }
414   gdb_flush (stream);
415 }
416 \f
417 void
418 pascal_value_print (struct value *val, struct ui_file *stream,
419                     const struct value_print_options *options)
420 {
421   struct type *type = value_type (val);
422   struct value_print_options opts = *options;
423
424   opts.deref_ref = 1;
425
426   /* If it is a pointer, indicate what it points to.
427
428      Print type also if it is a reference.
429
430      Object pascal: if it is a member pointer, we will take care
431      of that when we print it.  */
432   if (TYPE_CODE (type) == TYPE_CODE_PTR
433       || TYPE_CODE (type) == TYPE_CODE_REF)
434     {
435       /* Hack:  remove (char *) for char strings.  Their
436          type is indicated by the quoted string anyway.  */
437       if (TYPE_CODE (type) == TYPE_CODE_PTR
438           && TYPE_NAME (type) == NULL
439           && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
440           && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
441         {
442           /* Print nothing.  */
443         }
444       else
445         {
446           fprintf_filtered (stream, "(");
447           type_print (type, "", stream, -1);
448           fprintf_filtered (stream, ") ");
449         }
450     }
451   common_val_print (val, stream, 0, &opts, current_language);
452 }
453
454
455 static void
456 show_pascal_static_field_print (struct ui_file *file, int from_tty,
457                                 struct cmd_list_element *c, const char *value)
458 {
459   fprintf_filtered (file, _("Printing of pascal static members is %s.\n"),
460                     value);
461 }
462
463 static struct obstack dont_print_vb_obstack;
464 static struct obstack dont_print_statmem_obstack;
465
466 static void pascal_object_print_static_field (struct value *,
467                                               struct ui_file *, int,
468                                               const struct value_print_options *);
469
470 static void pascal_object_print_value (struct type *, const gdb_byte *,
471                                        int,
472                                        CORE_ADDR, struct ui_file *, int,
473                                        const struct value *,
474                                        const struct value_print_options *,
475                                        struct type **);
476
477 /* It was changed to this after 2.4.5.  */
478 const char pascal_vtbl_ptr_name[] =
479 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
480
481 /* Return truth value for assertion that TYPE is of the type
482    "pointer to virtual function".  */
483
484 int
485 pascal_object_is_vtbl_ptr_type (struct type *type)
486 {
487   const char *typename = type_name_no_tag (type);
488
489   return (typename != NULL
490           && strcmp (typename, pascal_vtbl_ptr_name) == 0);
491 }
492
493 /* Return truth value for the assertion that TYPE is of the type
494    "pointer to virtual function table".  */
495
496 int
497 pascal_object_is_vtbl_member (struct type *type)
498 {
499   if (TYPE_CODE (type) == TYPE_CODE_PTR)
500     {
501       type = TYPE_TARGET_TYPE (type);
502       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
503         {
504           type = TYPE_TARGET_TYPE (type);
505           if (TYPE_CODE (type) == TYPE_CODE_STRUCT      /* If not using
506                                                            thunks.  */
507               || TYPE_CODE (type) == TYPE_CODE_PTR)     /* If using thunks.  */
508             {
509               /* Virtual functions tables are full of pointers
510                  to virtual functions.  */
511               return pascal_object_is_vtbl_ptr_type (type);
512             }
513         }
514     }
515   return 0;
516 }
517
518 /* Mutually recursive subroutines of pascal_object_print_value and
519    c_val_print to print out a structure's fields:
520    pascal_object_print_value_fields and pascal_object_print_value.
521
522    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
523    same meanings as in pascal_object_print_value and c_val_print.
524
525    DONT_PRINT is an array of baseclass types that we
526    should not print, or zero if called from top level.  */
527
528 void
529 pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr,
530                                   int offset,
531                                   CORE_ADDR address, struct ui_file *stream,
532                                   int recurse,
533                                   const struct value *val,
534                                   const struct value_print_options *options,
535                                   struct type **dont_print_vb,
536                                   int dont_print_statmem)
537 {
538   int i, len, n_baseclasses;
539   char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
540
541   CHECK_TYPEDEF (type);
542
543   fprintf_filtered (stream, "{");
544   len = TYPE_NFIELDS (type);
545   n_baseclasses = TYPE_N_BASECLASSES (type);
546
547   /* Print out baseclasses such that we don't print
548      duplicates of virtual baseclasses.  */
549   if (n_baseclasses > 0)
550     pascal_object_print_value (type, valaddr, offset, address,
551                                stream, recurse + 1, val,
552                                options, dont_print_vb);
553
554   if (!len && n_baseclasses == 1)
555     fprintf_filtered (stream, "<No data fields>");
556   else
557     {
558       struct obstack tmp_obstack = dont_print_statmem_obstack;
559       int fields_seen = 0;
560
561       if (dont_print_statmem == 0)
562         {
563           /* If we're at top level, carve out a completely fresh
564              chunk of the obstack and use that until this particular
565              invocation returns.  */
566           obstack_finish (&dont_print_statmem_obstack);
567         }
568
569       for (i = n_baseclasses; i < len; i++)
570         {
571           /* If requested, skip printing of static fields.  */
572           if (!options->pascal_static_field_print
573               && field_is_static (&TYPE_FIELD (type, i)))
574             continue;
575           if (fields_seen)
576             fprintf_filtered (stream, ", ");
577           else if (n_baseclasses > 0)
578             {
579               if (options->prettyformat)
580                 {
581                   fprintf_filtered (stream, "\n");
582                   print_spaces_filtered (2 + 2 * recurse, stream);
583                   fputs_filtered ("members of ", stream);
584                   fputs_filtered (type_name_no_tag (type), stream);
585                   fputs_filtered (": ", stream);
586                 }
587             }
588           fields_seen = 1;
589
590           if (options->prettyformat)
591             {
592               fprintf_filtered (stream, "\n");
593               print_spaces_filtered (2 + 2 * recurse, stream);
594             }
595           else
596             {
597               wrap_here (n_spaces (2 + 2 * recurse));
598             }
599
600           annotate_field_begin (TYPE_FIELD_TYPE (type, i));
601
602           if (field_is_static (&TYPE_FIELD (type, i)))
603             fputs_filtered ("static ", stream);
604           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
605                                    language_cplus,
606                                    DMGL_PARAMS | DMGL_ANSI);
607           annotate_field_name_end ();
608           fputs_filtered (" = ", stream);
609           annotate_field_value ();
610
611           if (!field_is_static (&TYPE_FIELD (type, i))
612               && TYPE_FIELD_PACKED (type, i))
613             {
614               struct value *v;
615
616               /* Bitfields require special handling, especially due to byte
617                  order problems.  */
618               if (TYPE_FIELD_IGNORE (type, i))
619                 {
620                   fputs_filtered ("<optimized out or zero length>", stream);
621                 }
622               else if (value_bits_synthetic_pointer (val,
623                                                      TYPE_FIELD_BITPOS (type,
624                                                                         i),
625                                                      TYPE_FIELD_BITSIZE (type,
626                                                                          i)))
627                 {
628                   fputs_filtered (_("<synthetic pointer>"), stream);
629                 }
630               else
631                 {
632                   struct value_print_options opts = *options;
633
634                   v = value_field_bitfield (type, i, valaddr, offset, val);
635
636                   opts.deref_ref = 0;
637                   common_val_print (v, stream, recurse + 1, &opts,
638                                     current_language);
639                 }
640             }
641           else
642             {
643               if (TYPE_FIELD_IGNORE (type, i))
644                 {
645                   fputs_filtered ("<optimized out or zero length>", stream);
646                 }
647               else if (field_is_static (&TYPE_FIELD (type, i)))
648                 {
649                   /* struct value *v = value_static_field (type, i);
650                      v4.17 specific.  */
651                   struct value *v;
652
653                   v = value_field_bitfield (type, i, valaddr, offset, val);
654
655                   if (v == NULL)
656                     val_print_optimized_out (NULL, stream);
657                   else
658                     pascal_object_print_static_field (v, stream, recurse + 1,
659                                                       options);
660                 }
661               else
662                 {
663                   struct value_print_options opts = *options;
664
665                   opts.deref_ref = 0;
666                   /* val_print (TYPE_FIELD_TYPE (type, i),
667                      valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
668                      address + TYPE_FIELD_BITPOS (type, i) / 8, 0,
669                      stream, format, 0, recurse + 1, pretty); */
670                   val_print (TYPE_FIELD_TYPE (type, i),
671                              valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
672                              address, stream, recurse + 1, val, &opts,
673                              current_language);
674                 }
675             }
676           annotate_field_end ();
677         }
678
679       if (dont_print_statmem == 0)
680         {
681           /* Free the space used to deal with the printing
682              of the members from top level.  */
683           obstack_free (&dont_print_statmem_obstack, last_dont_print);
684           dont_print_statmem_obstack = tmp_obstack;
685         }
686
687       if (options->prettyformat)
688         {
689           fprintf_filtered (stream, "\n");
690           print_spaces_filtered (2 * recurse, stream);
691         }
692     }
693   fprintf_filtered (stream, "}");
694 }
695
696 /* Special val_print routine to avoid printing multiple copies of virtual
697    baseclasses.  */
698
699 static void
700 pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
701                            int offset,
702                            CORE_ADDR address, struct ui_file *stream,
703                            int recurse,
704                            const struct value *val,
705                            const struct value_print_options *options,
706                            struct type **dont_print_vb)
707 {
708   struct type **last_dont_print
709     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
710   struct obstack tmp_obstack = dont_print_vb_obstack;
711   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
712
713   if (dont_print_vb == 0)
714     {
715       /* If we're at top level, carve out a completely fresh
716          chunk of the obstack and use that until this particular
717          invocation returns.  */
718       /* Bump up the high-water mark.  Now alpha is omega.  */
719       obstack_finish (&dont_print_vb_obstack);
720     }
721
722   for (i = 0; i < n_baseclasses; i++)
723     {
724       int boffset = 0;
725       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
726       const char *basename = type_name_no_tag (baseclass);
727       const gdb_byte *base_valaddr = NULL;
728       int thisoffset;
729       volatile struct gdb_exception ex;
730       int skip = 0;
731
732       if (BASETYPE_VIA_VIRTUAL (type, i))
733         {
734           struct type **first_dont_print
735             = (struct type **) obstack_base (&dont_print_vb_obstack);
736
737           int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
738             - first_dont_print;
739
740           while (--j >= 0)
741             if (baseclass == first_dont_print[j])
742               goto flush_it;
743
744           obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
745         }
746
747       thisoffset = offset;
748
749       TRY_CATCH (ex, RETURN_MASK_ERROR)
750         {
751           boffset = baseclass_offset (type, i, valaddr, offset, address, val);
752         }
753       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
754         skip = -1;
755       else if (ex.reason < 0)
756         skip = 1;
757       else
758         {
759           skip = 0;
760
761           /* The virtual base class pointer might have been clobbered by the
762              user program. Make sure that it still points to a valid memory
763              location.  */
764
765           if (boffset < 0 || boffset >= TYPE_LENGTH (type))
766             {
767               gdb_byte *buf;
768               struct cleanup *back_to;
769
770               buf = xmalloc (TYPE_LENGTH (baseclass));
771               back_to = make_cleanup (xfree, buf);
772
773               base_valaddr = buf;
774               if (target_read_memory (address + boffset, buf,
775                                       TYPE_LENGTH (baseclass)) != 0)
776                 skip = 1;
777               address = address + boffset;
778               thisoffset = 0;
779               boffset = 0;
780               do_cleanups (back_to);
781             }
782           else
783             base_valaddr = valaddr;
784         }
785
786       if (options->prettyformat)
787         {
788           fprintf_filtered (stream, "\n");
789           print_spaces_filtered (2 * recurse, stream);
790         }
791       fputs_filtered ("<", stream);
792       /* Not sure what the best notation is in the case where there is no
793          baseclass name.  */
794
795       fputs_filtered (basename ? basename : "", stream);
796       fputs_filtered ("> = ", stream);
797
798       if (skip < 0)
799         val_print_unavailable (stream);
800       else if (skip > 0)
801         val_print_invalid_address (stream);
802       else
803         pascal_object_print_value_fields (baseclass, base_valaddr,
804                                           thisoffset + boffset, address,
805                                           stream, recurse, val, options,
806                      (struct type **) obstack_base (&dont_print_vb_obstack),
807                                           0);
808       fputs_filtered (", ", stream);
809
810     flush_it:
811       ;
812     }
813
814   if (dont_print_vb == 0)
815     {
816       /* Free the space used to deal with the printing
817          of this type from top level.  */
818       obstack_free (&dont_print_vb_obstack, last_dont_print);
819       /* Reset watermark so that we can continue protecting
820          ourselves from whatever we were protecting ourselves.  */
821       dont_print_vb_obstack = tmp_obstack;
822     }
823 }
824
825 /* Print value of a static member.
826    To avoid infinite recursion when printing a class that contains
827    a static instance of the class, we keep the addresses of all printed
828    static member classes in an obstack and refuse to print them more
829    than once.
830
831    VAL contains the value to print, STREAM, RECURSE, and OPTIONS
832    have the same meanings as in c_val_print.  */
833
834 static void
835 pascal_object_print_static_field (struct value *val,
836                                   struct ui_file *stream,
837                                   int recurse,
838                                   const struct value_print_options *options)
839 {
840   struct type *type = value_type (val);
841   struct value_print_options opts;
842
843   if (value_entirely_optimized_out (val))
844     {
845       val_print_optimized_out (val, stream);
846       return;
847     }
848
849   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
850     {
851       CORE_ADDR *first_dont_print, addr;
852       int i;
853
854       first_dont_print
855         = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
856       i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
857         - first_dont_print;
858
859       while (--i >= 0)
860         {
861           if (value_address (val) == first_dont_print[i])
862             {
863               fputs_filtered ("\
864 <same as static member of an already seen type>",
865                               stream);
866               return;
867             }
868         }
869
870       addr = value_address (val);
871       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
872                     sizeof (CORE_ADDR));
873
874       CHECK_TYPEDEF (type);
875       pascal_object_print_value_fields (type,
876                                         value_contents_for_printing (val),
877                                         value_embedded_offset (val),
878                                         addr,
879                                         stream, recurse,
880                                         val, options, NULL, 1);
881       return;
882     }
883
884   opts = *options;
885   opts.deref_ref = 0;
886   common_val_print (val, stream, recurse, &opts, current_language);
887 }
888
889 /* -Wmissing-prototypes */
890 extern initialize_file_ftype _initialize_pascal_valprint;
891
892 void
893 _initialize_pascal_valprint (void)
894 {
895   add_setshow_boolean_cmd ("pascal_static-members", class_support,
896                            &user_print_options.pascal_static_field_print, _("\
897 Set printing of pascal static members."), _("\
898 Show printing of pascal static members."), NULL,
899                            NULL,
900                            show_pascal_static_field_print,
901                            &setprintlist, &showprintlist);
902 }