1 /* Support for printing C++ values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    2000, 2001, 2002, 2003
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., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22 
23 #include "defs.h"
24 #include "gdb_obstack.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "demangle.h"
32 #include "annotate.h"
33 #include "gdb_string.h"
34 #include "c-lang.h"
35 #include "target.h"
36 #include "cp-abi.h"
37 #include "valprint.h"
38 
39 /* Indication of presence of HP-compiled object files */
40 extern int hp_som_som_object_present;	/* defined in symtab.c */
41 
42 
43 int vtblprint;			/* Controls printing of vtbl's */
44 int objectprint;		/* Controls looking up an object's derived type
45 				   using what we find in its vtables.  */
46 int static_field_print;		/* Controls printing of static fields. */
47 
48 static struct obstack dont_print_vb_obstack;
49 static struct obstack dont_print_statmem_obstack;
50 
51 extern void _initialize_cp_valprint (void);
52 
53 static void cp_print_static_field (struct type *, struct value *,
54 				   struct ui_file *, int, int,
55 				   enum val_prettyprint);
56 
57 static void cp_print_value (struct type *, struct type *, char *, int,
58 			    CORE_ADDR, struct ui_file *, int, int,
59 			    enum val_prettyprint, struct type **);
60 
61 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
62 						  struct value *,
63 						  struct ui_file *, int,
64 						  int,
65 						  enum val_prettyprint);
66 
67 
68 void
cp_print_class_method(char * valaddr,struct type * type,struct ui_file * stream)69 cp_print_class_method (char *valaddr,
70 		       struct type *type,
71 		       struct ui_file *stream)
72 {
73   struct type *domain;
74   struct fn_field *f = NULL;
75   int j = 0;
76   int len2;
77   int offset;
78   char *kind = "";
79   CORE_ADDR addr;
80   struct symbol *sym;
81   unsigned len;
82   unsigned int i;
83   struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
84 
85   domain = TYPE_DOMAIN_TYPE (target_type);
86   if (domain == (struct type *) NULL)
87     {
88       fprintf_filtered (stream, "<unknown>");
89       return;
90     }
91   addr = unpack_pointer (type, valaddr);
92   if (METHOD_PTR_IS_VIRTUAL (addr))
93     {
94       offset = METHOD_PTR_TO_VOFFSET (addr);
95       len = TYPE_NFN_FIELDS (domain);
96       for (i = 0; i < len; i++)
97 	{
98 	  f = TYPE_FN_FIELDLIST1 (domain, i);
99 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
100 
101 	  check_stub_method_group (domain, i);
102 	  for (j = 0; j < len2; j++)
103 	    {
104 	      if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
105 		{
106 		  kind = "virtual ";
107 		  goto common;
108 		}
109 	    }
110 	}
111     }
112   else
113     {
114       sym = find_pc_function (addr);
115       if (sym == 0)
116 	{
117 	  /* 1997-08-01 Currently unsupported with HP aCC */
118 	  if (hp_som_som_object_present)
119 	    {
120 	      fputs_filtered ("?? <not supported with HP aCC>", stream);
121 	      return;
122 	    }
123 	  error ("invalid pointer to member function");
124 	}
125       len = TYPE_NFN_FIELDS (domain);
126       for (i = 0; i < len; i++)
127 	{
128 	  f = TYPE_FN_FIELDLIST1 (domain, i);
129 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
130 
131 	  check_stub_method_group (domain, i);
132 	  for (j = 0; j < len2; j++)
133 	    {
134 	      if (strcmp (DEPRECATED_SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))
135 		  == 0)
136 		goto common;
137 	    }
138 	}
139     }
140  common:
141   if (i < len)
142     {
143       char *demangled_name;
144 
145       fprintf_filtered (stream, "&");
146       fputs_filtered (kind, stream);
147       demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
148 				       DMGL_ANSI | DMGL_PARAMS);
149       if (demangled_name == NULL)
150 	fprintf_filtered (stream, "<badly mangled name %s>",
151 			  TYPE_FN_FIELD_PHYSNAME (f, j));
152       else
153 	{
154 	  fputs_filtered (demangled_name, stream);
155 	  xfree (demangled_name);
156 	}
157     }
158   else
159     {
160       fprintf_filtered (stream, "(");
161       type_print (type, "", stream, -1);
162       fprintf_filtered (stream, ") %d", (int) addr >> 3);
163     }
164 }
165 
166 /* GCC versions after 2.4.5 use this.  */
167 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
168 
169 /* HP aCC uses different names.  */
170 const char hpacc_vtbl_ptr_name[] = "__vfp";
171 const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
172 
173 /* Return truth value for assertion that TYPE is of the type
174    "pointer to virtual function".  */
175 
176 int
cp_is_vtbl_ptr_type(struct type * type)177 cp_is_vtbl_ptr_type (struct type *type)
178 {
179   char *typename = type_name_no_tag (type);
180 
181   return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
182 }
183 
184 /* Return truth value for the assertion that TYPE is of the type
185    "pointer to virtual function table".  */
186 
187 int
cp_is_vtbl_member(struct type * type)188 cp_is_vtbl_member (struct type *type)
189 {
190   /* With older versions of g++, the vtbl field pointed to an array
191      of structures.  Nowadays it points directly to the structure. */
192   if (TYPE_CODE (type) == TYPE_CODE_PTR)
193     {
194       type = TYPE_TARGET_TYPE (type);
195       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
196 	{
197 	  type = TYPE_TARGET_TYPE (type);
198 	  if (TYPE_CODE (type) == TYPE_CODE_STRUCT	/* if not using thunks */
199 	      || TYPE_CODE (type) == TYPE_CODE_PTR)	/* if using thunks */
200 	    {
201 	      /* Virtual functions tables are full of pointers
202 	         to virtual functions. */
203 	      return cp_is_vtbl_ptr_type (type);
204 	    }
205 	}
206       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)  /* if not using thunks */
207 	{
208 	  return cp_is_vtbl_ptr_type (type);
209 	}
210       else if (TYPE_CODE (type) == TYPE_CODE_PTR)     /* if using thunks */
211 	{
212 	  /* The type name of the thunk pointer is NULL when using dwarf2.
213 	     We could test for a pointer to a function, but there is
214 	     no type info for the virtual table either, so it wont help.  */
215 	  return cp_is_vtbl_ptr_type (type);
216 	}
217     }
218   return 0;
219 }
220 
221 /* Mutually recursive subroutines of cp_print_value and c_val_print to
222    print out a structure's fields: cp_print_value_fields and cp_print_value.
223 
224    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
225    same meanings as in cp_print_value and c_val_print.
226 
227    2nd argument REAL_TYPE is used to carry over the type of the derived
228    class across the recursion to base classes.
229 
230    DONT_PRINT is an array of baseclass types that we
231    should not print, or zero if called from top level.  */
232 
233 void
cp_print_value_fields(struct type * type,struct type * real_type,char * valaddr,int offset,CORE_ADDR address,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,struct type ** dont_print_vb,int dont_print_statmem)234 cp_print_value_fields (struct type *type, struct type *real_type, char *valaddr,
235 		       int offset, CORE_ADDR address, struct ui_file *stream,
236 		       int format, int recurse, enum val_prettyprint pretty,
237 		       struct type **dont_print_vb, int dont_print_statmem)
238 {
239   int i, len, n_baseclasses;
240   struct obstack tmp_obstack;
241   char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
242   int fields_seen = 0;
243 
244   CHECK_TYPEDEF (type);
245 
246   fprintf_filtered (stream, "{");
247   len = TYPE_NFIELDS (type);
248   n_baseclasses = TYPE_N_BASECLASSES (type);
249 
250   /* First, print out baseclasses such that we don't print
251      duplicates of virtual baseclasses.  */
252 
253   if (n_baseclasses > 0)
254     cp_print_value (type, real_type, valaddr, offset, address, stream,
255 		    format, recurse + 1, pretty, dont_print_vb);
256 
257   /* Second, print out data fields */
258 
259   /* If there are no data fields, or if the only field is the
260    * vtbl pointer, skip this part */
261   if ((len == n_baseclasses)
262       || ((len - n_baseclasses == 1)
263 	  && TYPE_HAS_VTABLE (type)
264 	  && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
265 		      hpacc_vtbl_ptr_name, 5) == 0)
266       || !len)
267     fprintf_filtered (stream, "<No data fields>");
268   else
269     {
270       if (dont_print_statmem == 0)
271 	{
272 	  /* If we're at top level, carve out a completely fresh
273 	     chunk of the obstack and use that until this particular
274 	     invocation returns.  */
275 	  tmp_obstack = dont_print_statmem_obstack;
276 	  obstack_finish (&dont_print_statmem_obstack);
277 	}
278 
279       for (i = n_baseclasses; i < len; i++)
280 	{
281 	  /* If requested, skip printing of static fields.  */
282 	  if (!static_field_print && TYPE_FIELD_STATIC (type, i))
283 	    continue;
284 
285 	  /* If a vtable pointer appears, we'll print it out later */
286 	  if (TYPE_HAS_VTABLE (type)
287 	      && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
288 			  5) == 0)
289 	    continue;
290 
291 	  if (fields_seen)
292 	    fprintf_filtered (stream, ", ");
293 	  else if (n_baseclasses > 0)
294 	    {
295 	      if (pretty)
296 		{
297 		  fprintf_filtered (stream, "\n");
298 		  print_spaces_filtered (2 + 2 * recurse, stream);
299 		  fputs_filtered ("members of ", stream);
300 		  fputs_filtered (type_name_no_tag (type), stream);
301 		  fputs_filtered (": ", stream);
302 		}
303 	    }
304 	  fields_seen = 1;
305 
306 	  if (pretty)
307 	    {
308 	      fprintf_filtered (stream, "\n");
309 	      print_spaces_filtered (2 + 2 * recurse, stream);
310 	    }
311 	  else
312 	    {
313 	      wrap_here (n_spaces (2 + 2 * recurse));
314 	    }
315 	  if (inspect_it)
316 	    {
317 	      if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
318 		fputs_filtered ("\"( ptr \"", stream);
319 	      else
320 		fputs_filtered ("\"( nodef \"", stream);
321 	      if (TYPE_FIELD_STATIC (type, i))
322 		fputs_filtered ("static ", stream);
323 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
324 				       language_cplus,
325 				       DMGL_PARAMS | DMGL_ANSI);
326 	      fputs_filtered ("\" \"", stream);
327 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
328 				       language_cplus,
329 				       DMGL_PARAMS | DMGL_ANSI);
330 	      fputs_filtered ("\") \"", stream);
331 	    }
332 	  else
333 	    {
334 	      annotate_field_begin (TYPE_FIELD_TYPE (type, i));
335 
336 	      if (TYPE_FIELD_STATIC (type, i))
337 		fputs_filtered ("static ", stream);
338 	      fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
339 				       language_cplus,
340 				       DMGL_PARAMS | DMGL_ANSI);
341 	      annotate_field_name_end ();
342 	      /* do not print leading '=' in case of anonymous unions */
343 	      if (strcmp (TYPE_FIELD_NAME (type, i), ""))
344 		fputs_filtered (" = ", stream);
345 	      annotate_field_value ();
346 	    }
347 
348 	  if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
349 	    {
350 	      struct value *v;
351 
352 	      /* Bitfields require special handling, especially due to byte
353 	         order problems.  */
354 	      if (TYPE_FIELD_IGNORE (type, i))
355 		{
356 		  fputs_filtered ("<optimized out or zero length>", stream);
357 		}
358 	      else
359 		{
360 		  v = value_from_longest
361 		    (TYPE_FIELD_TYPE (type, i),
362 		     unpack_field_as_long (type, valaddr + offset, i));
363 
364 		  common_val_print (v, stream, format, 0, recurse + 1, pretty);
365 		}
366 	    }
367 	  else
368 	    {
369 	      if (TYPE_FIELD_IGNORE (type, i))
370 		{
371 		  fputs_filtered ("<optimized out or zero length>", stream);
372 		}
373 	      else if (TYPE_FIELD_STATIC (type, i))
374 		{
375 		  struct value *v = value_static_field (type, i);
376 		  if (v == NULL)
377 		    fputs_filtered ("<optimized out>", stream);
378 		  else
379 		    cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
380 					   stream, format, recurse + 1,
381 					   pretty);
382 		}
383 	      else
384 		{
385 		  val_print (TYPE_FIELD_TYPE (type, i),
386 			     valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
387 			     address + TYPE_FIELD_BITPOS (type, i) / 8,
388 			     stream, format, 0, recurse + 1, pretty);
389 		}
390 	    }
391 	  annotate_field_end ();
392 	}
393 
394       if (dont_print_statmem == 0)
395 	{
396 	  /* Free the space used to deal with the printing
397 	     of the members from top level.  */
398 	  obstack_free (&dont_print_statmem_obstack, last_dont_print);
399 	  dont_print_statmem_obstack = tmp_obstack;
400 	}
401 
402       if (pretty)
403 	{
404 	  fprintf_filtered (stream, "\n");
405 	  print_spaces_filtered (2 * recurse, stream);
406 	}
407     }				/* if there are data fields */
408   /* Now print out the virtual table pointer if there is one */
409   if (TYPE_HAS_VTABLE (type)
410       && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
411 		  hpacc_vtbl_ptr_name, 5) == 0)
412     {
413       struct value *v;
414       /* First get the virtual table pointer and print it out */
415 
416 #if 0
417       fputs_filtered ("__vfp = ", stream);
418 #endif
419 
420       fputs_filtered (", Virtual table at ", stream);
421 
422       /* pai: FIXME 32x64 problem? */
423       /* Not sure what the best notation is in the case where there is no
424          baseclass name.  */
425       v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
426 			      *(unsigned long *) (valaddr + offset));
427 
428       common_val_print (v, stream, format, 0, recurse + 1, pretty);
429       fields_seen = 1;
430 
431       if (vtblprint)
432 	{
433 	  /* Print out function pointers in vtable. */
434 
435 	  /* FIXME: then-clause is for non-RRBC layout of virtual
436 	   * table.  The RRBC case in the else-clause is yet to be
437 	   * implemented.  The if (1) below should be changed to a
438 	   * test for whether the executable we have was compiled
439 	   * with a version of HP aCC that doesn't have RRBC
440 	   * support. */
441 
442 	  if (1)
443 	    {
444 	      /* no RRBC support; function pointers embedded directly
445                  in vtable */
446 
447 	      int vfuncs = count_virtual_fns (real_type);
448 
449 	      fputs_filtered (" {", stream);
450 
451 	      /* FIXME : doesn't work at present */
452 #if 0
453 	      fprintf_filtered (stream, "%d entr%s: ", vfuncs,
454 				vfuncs == 1 ? "y" : "ies");
455 #else
456 	      fputs_filtered ("not implemented", stream);
457 
458 
459 #endif
460 
461 	      /* recursive function that prints all virtual function entries */
462 #if 0
463 	      cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
464 						    stream, format, recurse,
465 						    pretty);
466 #endif
467 	      fputs_filtered ("}", stream);
468 	    }			/* non-RRBC case */
469 	  else
470 	    {
471 	      /* FIXME -- see comments above */
472 	      /* RRBC support present; function pointers are found
473 	       * by indirection through the class segment entries. */
474 
475 
476 	    }			/* RRBC case */
477 	}			/* if vtblprint */
478 
479       if (pretty)
480 	{
481 	  fprintf_filtered (stream, "\n");
482 	  print_spaces_filtered (2 * recurse, stream);
483 	}
484 
485     }				/* if vtable exists */
486 
487   fprintf_filtered (stream, "}");
488 }
489 
490 /* Special val_print routine to avoid printing multiple copies of virtual
491    baseclasses.  */
492 
493 static void
cp_print_value(struct type * type,struct type * real_type,char * valaddr,int offset,CORE_ADDR address,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty,struct type ** dont_print_vb)494 cp_print_value (struct type *type, struct type *real_type, char *valaddr,
495 		int offset, CORE_ADDR address, struct ui_file *stream,
496 		int format, int recurse, enum val_prettyprint pretty,
497 		struct type **dont_print_vb)
498 {
499   struct obstack tmp_obstack;
500   struct type **last_dont_print
501     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
502   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
503   int thisoffset;
504   struct type *thistype;
505 
506   if (dont_print_vb == 0)
507     {
508       /* If we're at top level, carve out a completely fresh
509          chunk of the obstack and use that until this particular
510          invocation returns.  */
511       tmp_obstack = dont_print_vb_obstack;
512       /* Bump up the high-water mark.  Now alpha is omega.  */
513       obstack_finish (&dont_print_vb_obstack);
514     }
515 
516   for (i = 0; i < n_baseclasses; i++)
517     {
518       int boffset;
519       int skip;
520       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
521       char *basename = TYPE_NAME (baseclass);
522       char *base_valaddr;
523 
524       if (BASETYPE_VIA_VIRTUAL (type, i))
525 	{
526 	  struct type **first_dont_print
527 	    = (struct type **) obstack_base (&dont_print_vb_obstack);
528 
529 	  int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
530 	    - first_dont_print;
531 
532 	  while (--j >= 0)
533 	    if (baseclass == first_dont_print[j])
534 	      goto flush_it;
535 
536 	  obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
537 	}
538 
539       thisoffset = offset;
540       thistype = real_type;
541       if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
542 	{
543 	  /* Assume HP/Taligent runtime convention */
544 	  find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
545 				valaddr, offset, &boffset, &skip);
546 	  if (skip >= 0)
547 	    error ("Virtual base class offset not found from vtable while"
548 		   " printing");
549 	  base_valaddr = valaddr;
550 	}
551       else
552 	{
553 	  boffset = baseclass_offset (type, i,
554 				      valaddr + offset,
555 				      address);
556 	  skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
557 
558 	  if (BASETYPE_VIA_VIRTUAL (type, i))
559 	    {
560 	      /* The virtual base class pointer might have been
561 	         clobbered by the user program. Make sure that it
562 	         still points to a valid memory location.  */
563 
564 	      if (boffset != -1
565 		  && ((boffset + offset) < 0
566 		      || (boffset + offset) >= TYPE_LENGTH (type)))
567 		{
568 		  /* FIXME (alloca): unsafe if baseclass is really really large. */
569 		  base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
570 		  if (target_read_memory (address + boffset, base_valaddr,
571 					  TYPE_LENGTH (baseclass)) != 0)
572 		    skip = 1;
573 		  address = address + boffset;
574 		  thisoffset = 0;
575 		  boffset = 0;
576 		  thistype = baseclass;
577 		}
578 	      else
579 		base_valaddr = valaddr;
580 	    }
581 	  else
582 	    base_valaddr = valaddr;
583 	}
584 
585       /* now do the printing */
586       if (pretty)
587 	{
588 	  fprintf_filtered (stream, "\n");
589 	  print_spaces_filtered (2 * recurse, stream);
590 	}
591       fputs_filtered ("<", stream);
592       /* Not sure what the best notation is in the case where there is no
593          baseclass name.  */
594       fputs_filtered (basename ? basename : "", stream);
595       fputs_filtered ("> = ", stream);
596 
597 
598       if (skip >= 1)
599 	fprintf_filtered (stream, "<invalid address>");
600       else
601 	cp_print_value_fields (baseclass, thistype, base_valaddr,
602 			       thisoffset + boffset, address + boffset,
603 			       stream, format,
604 			       recurse, pretty,
605 			       ((struct type **)
606 				obstack_base (&dont_print_vb_obstack)),
607 			       0);
608       fputs_filtered (", ", stream);
609 
610     flush_it:
611       ;
612     }
613 
614   if (dont_print_vb == 0)
615     {
616       /* Free the space used to deal with the printing
617          of this type from top level.  */
618       obstack_free (&dont_print_vb_obstack, last_dont_print);
619       /* Reset watermark so that we can continue protecting
620          ourselves from whatever we were protecting ourselves.  */
621       dont_print_vb_obstack = tmp_obstack;
622     }
623 }
624 
625 /* Print value of a static member.
626    To avoid infinite recursion when printing a class that contains
627    a static instance of the class, we keep the addresses of all printed
628    static member classes in an obstack and refuse to print them more
629    than once.
630 
631    VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
632    have the same meanings as in c_val_print.  */
633 
634 static void
cp_print_static_field(struct type * type,struct value * val,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)635 cp_print_static_field (struct type *type,
636 		       struct value *val,
637 		       struct ui_file *stream,
638 		       int format,
639 		       int recurse,
640 		       enum val_prettyprint pretty)
641 {
642   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
643     {
644       CORE_ADDR *first_dont_print;
645       int i;
646 
647       first_dont_print
648 	= (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
649       i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
650 	- first_dont_print;
651 
652       while (--i >= 0)
653 	{
654 	  if (VALUE_ADDRESS (val) == first_dont_print[i])
655 	    {
656 	      fputs_filtered ("<same as static member of an already"
657 			      " seen type>",
658 			      stream);
659 	      return;
660 	    }
661 	}
662 
663       obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
664 		    sizeof (CORE_ADDR));
665 
666       CHECK_TYPEDEF (type);
667       cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val),
668 			     VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
669 			     stream, format, recurse, pretty, NULL, 1);
670       return;
671     }
672   val_print (type, VALUE_CONTENTS_ALL (val),
673 	     VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
674 	     stream, format, 0, recurse, pretty);
675 }
676 
677 void
cp_print_class_member(char * valaddr,struct type * domain,struct ui_file * stream,char * prefix)678 cp_print_class_member (char *valaddr, struct type *domain,
679 		       struct ui_file *stream, char *prefix)
680 {
681 
682   /* VAL is a byte offset into the structure type DOMAIN.
683      Find the name of the field for that offset and
684      print it.  */
685   int extra = 0;
686   int bits = 0;
687   unsigned int i;
688   unsigned len = TYPE_NFIELDS (domain);
689 
690   /* @@ Make VAL into bit offset */
691 
692   /* Note: HP aCC generates offsets that are the real byte offsets added
693      to a constant bias 0x20000000 (1 << 29).  This constant bias gets
694      shifted out in the code below -- joyous happenstance! */
695 
696   /* Note: HP cfront uses a constant bias of 1; if we support this
697      compiler ever, we will have to adjust the computation below */
698 
699   LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
700   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
701     {
702       int bitpos = TYPE_FIELD_BITPOS (domain, i);
703       QUIT;
704       if (val == bitpos)
705 	break;
706       if (val < bitpos && i != 0)
707 	{
708 	  /* Somehow pointing into a field.  */
709 	  i -= 1;
710 	  extra = (val - TYPE_FIELD_BITPOS (domain, i));
711 	  if (extra & 0x7)
712 	    bits = 1;
713 	  else
714 	    extra >>= 3;
715 	  break;
716 	}
717     }
718   if (i < len)
719     {
720       char *name;
721       fputs_filtered (prefix, stream);
722       name = type_name_no_tag (domain);
723       if (name)
724 	fputs_filtered (name, stream);
725       else
726 	c_type_print_base (domain, stream, 0, 0);
727       fprintf_filtered (stream, "::");
728       fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
729       if (extra)
730 	fprintf_filtered (stream, " + %d bytes", extra);
731       if (bits)
732 	fprintf_filtered (stream, " (offset in bits)");
733     }
734   else
735     fprintf_filtered (stream, "%ld", (long) (val >> 3));
736 }
737 
738 
739 /* This function prints out virtual table entries for a class; it
740  * recurses on the base classes to find all virtual functions
741  * available in a class.
742  *
743  * pai/1997-05-21 Note: As the name suggests, it's currently
744  * implemented for HP aCC runtime only. g++ objects are handled
745  * differently and I have made no attempt to fold that logic in
746  * here. The runtime layout is different for the two cases.  Also,
747  * this currently has only the code for non-RRBC layouts generated by
748  * the HP aCC compiler; RRBC code is stubbed out and will have to be
749  * added later. */
750 
751 
752 static void
cp_print_hpacc_virtual_table_entries(struct type * type,int * vfuncs,struct value * v,struct ui_file * stream,int format,int recurse,enum val_prettyprint pretty)753 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
754 				      struct value *v, struct ui_file *stream,
755 				      int format, int recurse,
756 				      enum val_prettyprint pretty)
757 {
758   int fn, oi;
759 
760   /* pai: FIXME this function doesn't work. It should handle a given
761    * virtual function only once (latest redefinition in class hierarchy)
762    */
763 
764   /* Recursion on other classes that can share the same vtable */
765   struct type *pbc = primary_base_class (type);
766   if (pbc)
767     cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format,
768 					  recurse, pretty);
769 
770   /* Now deal with vfuncs declared in this class */
771   for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
772     for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
773       if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
774 	{
775 	  char *vf_name;
776 	  const char *field_physname;
777 
778 	  /* virtual function offset */
779 	  int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi)
780 		    - 1);
781 
782 	  /* Get the address of the vfunction entry */
783 	  struct value *vf = value_copy (v);
784 	  if (VALUE_LAZY (vf))
785 	    (void) value_fetch_lazy (vf);
786 	  /* adjust by offset */
787 	  vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx);
788 	  vf = value_ind (vf);	/* get the entry */
789 	  VALUE_TYPE (vf) = VALUE_TYPE (v);	/* make it a pointer */
790 
791 	  /* print out the entry */
792 	  common_val_print (vf, stream, format, 0, recurse + 1, pretty);
793 	  field_physname
794 	    = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
795 	  /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
796 	  vf_name = cplus_demangle (field_physname, DMGL_ARM);
797 	  fprintf_filtered (stream, " %s", vf_name);
798 	  if (--(*vfuncs) > 0)
799 	    fputs_filtered (", ", stream);
800 	}
801 }
802 
803 
804 
805 void
_initialize_cp_valprint(void)806 _initialize_cp_valprint (void)
807 {
808   add_show_from_set
809     (add_set_cmd ("static-members", class_support, var_boolean,
810 		  (char *) &static_field_print,
811 		  "Set printing of C++ static members.",
812 		  &setprintlist),
813      &showprintlist);
814   /* Turn on printing of static fields.  */
815   static_field_print = 1;
816 
817   add_show_from_set
818     (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
819 		  "Set printing of C++ virtual function tables.",
820 		  &setprintlist),
821      &showprintlist);
822 
823   add_show_from_set
824     (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
825 	      "Set printing of object's derived type based on vtable info.",
826 		  &setprintlist),
827      &showprintlist);
828 
829   /* Give people the defaults which they are used to.  */
830   objectprint = 0;
831   vtblprint = 0;
832   obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
833   obstack_specify_allocation (&dont_print_statmem_obstack,
834 			      32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),
835 			      xmalloc, xfree);
836 }
837