1130803Smarcel/* Support for printing Ada types for GDB, the GNU debugger.
2130803Smarcel   Copyright 1986, 1988, 1989, 1991, 1997, 2003 Free Software
3130803Smarcel   Foundation, Inc.
4130803Smarcel
5130803SmarcelThis file is part of GDB.
6130803Smarcel
7130803SmarcelThis program is free software; you can redistribute it and/or modify
8130803Smarcelit under the terms of the GNU General Public License as published by
9130803Smarcelthe Free Software Foundation; either version 2 of the License, or
10130803Smarcel(at your option) any later version.
11130803Smarcel
12130803SmarcelThis program is distributed in the hope that it will be useful,
13130803Smarcelbut WITHOUT ANY WARRANTY; without even the implied warranty of
14130803SmarcelMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803SmarcelGNU General Public License for more details.
16130803Smarcel
17130803SmarcelYou should have received a copy of the GNU General Public License
18130803Smarcelalong with this program; if not, write to the Free Software
19130803SmarcelFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20130803Smarcel
21130803Smarcel#include "defs.h"
22130803Smarcel#include "gdb_obstack.h"
23130803Smarcel#include "bfd.h"		/* Binary File Description */
24130803Smarcel#include "symtab.h"
25130803Smarcel#include "gdbtypes.h"
26130803Smarcel#include "expression.h"
27130803Smarcel#include "value.h"
28130803Smarcel#include "gdbcore.h"
29130803Smarcel#include "target.h"
30130803Smarcel#include "command.h"
31130803Smarcel#include "gdbcmd.h"
32130803Smarcel#include "language.h"
33130803Smarcel#include "demangle.h"
34130803Smarcel#include "c-lang.h"
35130803Smarcel#include "typeprint.h"
36130803Smarcel#include "ada-lang.h"
37130803Smarcel
38130803Smarcel#include <ctype.h>
39130803Smarcel#include "gdb_string.h"
40130803Smarcel#include <errno.h>
41130803Smarcel
42130803Smarcelstatic int print_record_field_types (struct type *, struct type *,
43130803Smarcel				     struct ui_file *, int, int);
44130803Smarcel
45130803Smarcelstatic void print_array_type (struct type *, struct ui_file *, int, int);
46130803Smarcel
47130803Smarcelstatic void print_choices (struct type *, int, struct ui_file *,
48130803Smarcel			   struct type *);
49130803Smarcel
50130803Smarcelstatic void print_range (struct type *, struct ui_file *);
51130803Smarcel
52130803Smarcelstatic void print_range_bound (struct type *, char *, int *,
53130803Smarcel			       struct ui_file *);
54130803Smarcel
55130803Smarcelstatic void
56130803Smarcelprint_dynamic_range_bound (struct type *, const char *, int,
57130803Smarcel			   const char *, struct ui_file *);
58130803Smarcel
59130803Smarcelstatic void print_range_type_named (char *, struct ui_file *);
60130803Smarcel
61130803Smarcel
62130803Smarcel
63130803Smarcelstatic char *name_buffer;
64130803Smarcelstatic int name_buffer_len;
65130803Smarcel
66130803Smarcel/* The (demangled) Ada name of TYPE. This value persists until the
67130803Smarcel   next call. */
68130803Smarcel
69130803Smarcelstatic char *
70130803Smarceldemangled_type_name (struct type *type)
71130803Smarcel{
72130803Smarcel  if (ada_type_name (type) == NULL)
73130803Smarcel    return NULL;
74130803Smarcel  else
75130803Smarcel    {
76130803Smarcel      char *raw_name = ada_type_name (type);
77130803Smarcel      char *s, *q;
78130803Smarcel
79130803Smarcel      if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
80130803Smarcel	{
81130803Smarcel	  name_buffer_len = 16 + 2 * strlen (raw_name);
82130803Smarcel	  name_buffer = xrealloc (name_buffer, name_buffer_len);
83130803Smarcel	}
84130803Smarcel      strcpy (name_buffer, raw_name);
85130803Smarcel
86130803Smarcel      s = (char *) strstr (name_buffer, "___");
87130803Smarcel      if (s != NULL)
88130803Smarcel	*s = '\0';
89130803Smarcel
90130803Smarcel      s = name_buffer + strlen (name_buffer) - 1;
91130803Smarcel      while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
92130803Smarcel	s -= 1;
93130803Smarcel
94130803Smarcel      if (s == name_buffer)
95130803Smarcel	return name_buffer;
96130803Smarcel
97130803Smarcel      if (!islower (s[1]))
98130803Smarcel	return NULL;
99130803Smarcel
100130803Smarcel      for (s = q = name_buffer; *s != '\0'; q += 1)
101130803Smarcel	{
102130803Smarcel	  if (s[0] == '_' && s[1] == '_')
103130803Smarcel	    {
104130803Smarcel	      *q = '.';
105130803Smarcel	      s += 2;
106130803Smarcel	    }
107130803Smarcel	  else
108130803Smarcel	    {
109130803Smarcel	      *q = *s;
110130803Smarcel	      s += 1;
111130803Smarcel	    }
112130803Smarcel	}
113130803Smarcel      *q = '\0';
114130803Smarcel      return name_buffer;
115130803Smarcel    }
116130803Smarcel}
117130803Smarcel
118130803Smarcel
119130803Smarcel/* Print a description of a type in the format of a
120130803Smarcel   typedef for the current language.
121130803Smarcel   NEW is the new name for a type TYPE. */
122130803Smarcel
123130803Smarcelvoid
124130803Smarcelada_typedef_print (struct type *type, struct symbol *new,
125130803Smarcel		   struct ui_file *stream)
126130803Smarcel{
127130803Smarcel  fprintf_filtered (stream, "type %.*s is ",
128130803Smarcel		    ada_name_prefix_len (SYMBOL_PRINT_NAME (new)),
129130803Smarcel		    SYMBOL_PRINT_NAME (new));
130130803Smarcel  type_print (type, "", stream, 1);
131130803Smarcel}
132130803Smarcel
133130803Smarcel/* Print range type TYPE on STREAM. */
134130803Smarcel
135130803Smarcelstatic void
136130803Smarcelprint_range (struct type *type, struct ui_file *stream)
137130803Smarcel{
138130803Smarcel  struct type *target_type;
139130803Smarcel  target_type = TYPE_TARGET_TYPE (type);
140130803Smarcel  if (target_type == NULL)
141130803Smarcel    target_type = type;
142130803Smarcel
143130803Smarcel  switch (TYPE_CODE (target_type))
144130803Smarcel    {
145130803Smarcel    case TYPE_CODE_RANGE:
146130803Smarcel    case TYPE_CODE_INT:
147130803Smarcel    case TYPE_CODE_BOOL:
148130803Smarcel    case TYPE_CODE_CHAR:
149130803Smarcel    case TYPE_CODE_ENUM:
150130803Smarcel      break;
151130803Smarcel    default:
152130803Smarcel      target_type = builtin_type_ada_int;
153130803Smarcel      break;
154130803Smarcel    }
155130803Smarcel
156130803Smarcel  if (TYPE_NFIELDS (type) < 2)
157130803Smarcel    {
158130803Smarcel      /* A range needs at least 2 bounds to be printed. If there are less
159130803Smarcel         than 2, just print the type name instead of the range itself.
160130803Smarcel         This check handles cases such as characters, for example.
161130803Smarcel
162130803Smarcel         Note that if the name is not defined, then we don't print anything.
163130803Smarcel       */
164130803Smarcel      fprintf_filtered (stream, "%.*s",
165130803Smarcel			ada_name_prefix_len (TYPE_NAME (type)),
166130803Smarcel			TYPE_NAME (type));
167130803Smarcel    }
168130803Smarcel  else
169130803Smarcel    {
170130803Smarcel      /* We extract the range type bounds respectively from the first element
171130803Smarcel         and the last element of the type->fields array */
172130803Smarcel      const LONGEST lower_bound = (LONGEST) TYPE_LOW_BOUND (type);
173130803Smarcel      const LONGEST upper_bound =
174130803Smarcel	(LONGEST) TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
175130803Smarcel
176130803Smarcel      ada_print_scalar (target_type, lower_bound, stream);
177130803Smarcel      fprintf_filtered (stream, " .. ");
178130803Smarcel      ada_print_scalar (target_type, upper_bound, stream);
179130803Smarcel    }
180130803Smarcel}
181130803Smarcel
182130803Smarcel/* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
183130803Smarcel   set *N past the bound and its delimiter, if any. */
184130803Smarcel
185130803Smarcelstatic void
186130803Smarcelprint_range_bound (struct type *type, char *bounds, int *n,
187130803Smarcel		   struct ui_file *stream)
188130803Smarcel{
189130803Smarcel  LONGEST B;
190130803Smarcel  if (ada_scan_number (bounds, *n, &B, n))
191130803Smarcel    {
192130803Smarcel      ada_print_scalar (type, B, stream);
193130803Smarcel      if (bounds[*n] == '_')
194130803Smarcel	*n += 2;
195130803Smarcel    }
196130803Smarcel  else
197130803Smarcel    {
198130803Smarcel      int bound_len;
199130803Smarcel      char *bound = bounds + *n;
200130803Smarcel      char *pend;
201130803Smarcel
202130803Smarcel      pend = strstr (bound, "__");
203130803Smarcel      if (pend == NULL)
204130803Smarcel	*n += bound_len = strlen (bound);
205130803Smarcel      else
206130803Smarcel	{
207130803Smarcel	  bound_len = pend - bound;
208130803Smarcel	  *n += bound_len + 2;
209130803Smarcel	}
210130803Smarcel      fprintf_filtered (stream, "%.*s", bound_len, bound);
211130803Smarcel    }
212130803Smarcel}
213130803Smarcel
214130803Smarcel/* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
215130803Smarcel   the value (if found) of the bound indicated by SUFFIX ("___L" or
216130803Smarcel   "___U") according to the ___XD conventions. */
217130803Smarcel
218130803Smarcelstatic void
219130803Smarcelprint_dynamic_range_bound (struct type *type, const char *name, int name_len,
220130803Smarcel			   const char *suffix, struct ui_file *stream)
221130803Smarcel{
222130803Smarcel  static char *name_buf = NULL;
223130803Smarcel  static size_t name_buf_len = 0;
224130803Smarcel  LONGEST B;
225130803Smarcel  int OK;
226130803Smarcel
227130803Smarcel  GROW_VECT (name_buf, name_buf_len, name_len + strlen (suffix) + 1);
228130803Smarcel  strncpy (name_buf, name, name_len);
229130803Smarcel  strcpy (name_buf + name_len, suffix);
230130803Smarcel
231130803Smarcel  B = get_int_var_value (name_buf, 0, &OK);
232130803Smarcel  if (OK)
233130803Smarcel    ada_print_scalar (type, B, stream);
234130803Smarcel  else
235130803Smarcel    fprintf_filtered (stream, "?");
236130803Smarcel}
237130803Smarcel
238130803Smarcel/* Print the range type named NAME. */
239130803Smarcel
240130803Smarcelstatic void
241130803Smarcelprint_range_type_named (char *name, struct ui_file *stream)
242130803Smarcel{
243130803Smarcel  struct type *raw_type = ada_find_any_type (name);
244130803Smarcel  struct type *base_type;
245130803Smarcel  LONGEST low, high;
246130803Smarcel  char *subtype_info;
247130803Smarcel
248130803Smarcel  if (raw_type == NULL)
249130803Smarcel    base_type = builtin_type_int;
250130803Smarcel  else if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
251130803Smarcel    base_type = TYPE_TARGET_TYPE (raw_type);
252130803Smarcel  else
253130803Smarcel    base_type = raw_type;
254130803Smarcel
255130803Smarcel  subtype_info = strstr (name, "___XD");
256130803Smarcel  if (subtype_info == NULL && raw_type == NULL)
257130803Smarcel    fprintf_filtered (stream, "? .. ?");
258130803Smarcel  else if (subtype_info == NULL)
259130803Smarcel    print_range (raw_type, stream);
260130803Smarcel  else
261130803Smarcel    {
262130803Smarcel      int prefix_len = subtype_info - name;
263130803Smarcel      char *bounds_str;
264130803Smarcel      int n;
265130803Smarcel
266130803Smarcel      subtype_info += 5;
267130803Smarcel      bounds_str = strchr (subtype_info, '_');
268130803Smarcel      n = 1;
269130803Smarcel
270130803Smarcel      if (*subtype_info == 'L')
271130803Smarcel	{
272130803Smarcel	  print_range_bound (raw_type, bounds_str, &n, stream);
273130803Smarcel	  subtype_info += 1;
274130803Smarcel	}
275130803Smarcel      else
276130803Smarcel	print_dynamic_range_bound (raw_type, name, prefix_len, "___L",
277130803Smarcel				   stream);
278130803Smarcel
279130803Smarcel      fprintf_filtered (stream, " .. ");
280130803Smarcel
281130803Smarcel      if (*subtype_info == 'U')
282130803Smarcel	print_range_bound (raw_type, bounds_str, &n, stream);
283130803Smarcel      else
284130803Smarcel	print_dynamic_range_bound (raw_type, name, prefix_len, "___U",
285130803Smarcel				   stream);
286130803Smarcel    }
287130803Smarcel}
288130803Smarcel
289130803Smarcel/* Print enumerated type TYPE on STREAM. */
290130803Smarcel
291130803Smarcelstatic void
292130803Smarcelprint_enum_type (struct type *type, struct ui_file *stream)
293130803Smarcel{
294130803Smarcel  int len = TYPE_NFIELDS (type);
295130803Smarcel  int i, lastval;
296130803Smarcel
297130803Smarcel  fprintf_filtered (stream, "(");
298130803Smarcel  wrap_here (" ");
299130803Smarcel
300130803Smarcel  lastval = 0;
301130803Smarcel  for (i = 0; i < len; i++)
302130803Smarcel    {
303130803Smarcel      QUIT;
304130803Smarcel      if (i)
305130803Smarcel	fprintf_filtered (stream, ", ");
306130803Smarcel      wrap_here ("    ");
307130803Smarcel      fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
308130803Smarcel      if (lastval != TYPE_FIELD_BITPOS (type, i))
309130803Smarcel	{
310130803Smarcel	  fprintf_filtered (stream, " => %d", TYPE_FIELD_BITPOS (type, i));
311130803Smarcel	  lastval = TYPE_FIELD_BITPOS (type, i);
312130803Smarcel	}
313130803Smarcel      lastval += 1;
314130803Smarcel    }
315130803Smarcel  fprintf_filtered (stream, ")");
316130803Smarcel}
317130803Smarcel
318130803Smarcel/* Print representation of Ada fixed-point type TYPE on STREAM. */
319130803Smarcel
320130803Smarcelstatic void
321130803Smarcelprint_fixed_point_type (struct type *type, struct ui_file *stream)
322130803Smarcel{
323130803Smarcel  DOUBLEST delta = ada_delta (type);
324130803Smarcel  DOUBLEST small = ada_fixed_to_float (type, 1.0);
325130803Smarcel
326130803Smarcel  if (delta < 0.0)
327130803Smarcel    fprintf_filtered (stream, "delta ??");
328130803Smarcel  else
329130803Smarcel    {
330130803Smarcel      fprintf_filtered (stream, "delta %g", (double) delta);
331130803Smarcel      if (delta != small)
332130803Smarcel	fprintf_filtered (stream, " <'small = %g>", (double) small);
333130803Smarcel    }
334130803Smarcel}
335130803Smarcel
336130803Smarcel/* Print representation of special VAX floating-point type TYPE on STREAM. */
337130803Smarcel
338130803Smarcelstatic void
339130803Smarcelprint_vax_floating_point_type (struct type *type, struct ui_file *stream)
340130803Smarcel{
341130803Smarcel  fprintf_filtered (stream, "<float format %c>",
342130803Smarcel		    ada_vax_float_type_suffix (type));
343130803Smarcel}
344130803Smarcel
345130803Smarcel/* Print simple (constrained) array type TYPE on STREAM.  LEVEL is the
346130803Smarcel   recursion (indentation) level, in case the element type itself has
347130803Smarcel   nested structure, and SHOW is the number of levels of internal
348130803Smarcel   structure to show (see ada_print_type). */
349130803Smarcel
350130803Smarcelstatic void
351130803Smarcelprint_array_type (struct type *type, struct ui_file *stream, int show,
352130803Smarcel		  int level)
353130803Smarcel{
354130803Smarcel  int bitsize;
355130803Smarcel  int n_indices;
356130803Smarcel
357130803Smarcel  bitsize = 0;
358130803Smarcel  fprintf_filtered (stream, "array (");
359130803Smarcel
360130803Smarcel  n_indices = -1;
361130803Smarcel  if (show < 0)
362130803Smarcel    fprintf_filtered (stream, "...");
363130803Smarcel  else
364130803Smarcel    {
365130803Smarcel      if (ada_is_packed_array_type (type))
366130803Smarcel	type = ada_coerce_to_simple_array_type (type);
367130803Smarcel      if (ada_is_simple_array (type))
368130803Smarcel	{
369130803Smarcel	  struct type *range_desc_type =
370130803Smarcel	    ada_find_parallel_type (type, "___XA");
371130803Smarcel	  struct type *arr_type;
372130803Smarcel
373130803Smarcel	  bitsize = 0;
374130803Smarcel	  if (range_desc_type == NULL)
375130803Smarcel	    {
376130803Smarcel	      for (arr_type = type; TYPE_CODE (arr_type) == TYPE_CODE_ARRAY;
377130803Smarcel		   arr_type = TYPE_TARGET_TYPE (arr_type))
378130803Smarcel		{
379130803Smarcel		  if (arr_type != type)
380130803Smarcel		    fprintf_filtered (stream, ", ");
381130803Smarcel		  print_range (TYPE_INDEX_TYPE (arr_type), stream);
382130803Smarcel		  if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
383130803Smarcel		    bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
384130803Smarcel		}
385130803Smarcel	    }
386130803Smarcel	  else
387130803Smarcel	    {
388130803Smarcel	      int k;
389130803Smarcel	      n_indices = TYPE_NFIELDS (range_desc_type);
390130803Smarcel	      for (k = 0, arr_type = type;
391130803Smarcel		   k < n_indices;
392130803Smarcel		   k += 1, arr_type = TYPE_TARGET_TYPE (arr_type))
393130803Smarcel		{
394130803Smarcel		  if (k > 0)
395130803Smarcel		    fprintf_filtered (stream, ", ");
396130803Smarcel		  print_range_type_named (TYPE_FIELD_NAME
397130803Smarcel					  (range_desc_type, k), stream);
398130803Smarcel		  if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
399130803Smarcel		    bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
400130803Smarcel		}
401130803Smarcel	    }
402130803Smarcel	}
403130803Smarcel      else
404130803Smarcel	{
405130803Smarcel	  int i, i0;
406130803Smarcel	  for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
407130803Smarcel	    fprintf_filtered (stream, "%s<>", i == i0 ? "" : ", ");
408130803Smarcel	}
409130803Smarcel    }
410130803Smarcel
411130803Smarcel  fprintf_filtered (stream, ") of ");
412130803Smarcel  wrap_here ("");
413130803Smarcel  ada_print_type (ada_array_element_type (type, n_indices), "", stream,
414130803Smarcel		  show == 0 ? 0 : show - 1, level + 1);
415130803Smarcel  if (bitsize > 0)
416130803Smarcel    fprintf_filtered (stream, " <packed: %d-bit elements>", bitsize);
417130803Smarcel}
418130803Smarcel
419130803Smarcel/* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
420130803Smarcel   STREAM, assuming the VAL_TYPE is the type of the values. */
421130803Smarcel
422130803Smarcelstatic void
423130803Smarcelprint_choices (struct type *type, int field_num, struct ui_file *stream,
424130803Smarcel	       struct type *val_type)
425130803Smarcel{
426130803Smarcel  int have_output;
427130803Smarcel  int p;
428130803Smarcel  const char *name = TYPE_FIELD_NAME (type, field_num);
429130803Smarcel
430130803Smarcel  have_output = 0;
431130803Smarcel
432130803Smarcel  /* Skip over leading 'V': NOTE soon to be obsolete. */
433130803Smarcel  if (name[0] == 'V')
434130803Smarcel    {
435130803Smarcel      if (!ada_scan_number (name, 1, NULL, &p))
436130803Smarcel	goto Huh;
437130803Smarcel    }
438130803Smarcel  else
439130803Smarcel    p = 0;
440130803Smarcel
441130803Smarcel  while (1)
442130803Smarcel    {
443130803Smarcel      switch (name[p])
444130803Smarcel	{
445130803Smarcel	default:
446130803Smarcel	  return;
447130803Smarcel	case 'S':
448130803Smarcel	case 'R':
449130803Smarcel	case 'O':
450130803Smarcel	  if (have_output)
451130803Smarcel	    fprintf_filtered (stream, " | ");
452130803Smarcel	  have_output = 1;
453130803Smarcel	  break;
454130803Smarcel	}
455130803Smarcel
456130803Smarcel      switch (name[p])
457130803Smarcel	{
458130803Smarcel	case 'S':
459130803Smarcel	  {
460130803Smarcel	    LONGEST W;
461130803Smarcel	    if (!ada_scan_number (name, p + 1, &W, &p))
462130803Smarcel	      goto Huh;
463130803Smarcel	    ada_print_scalar (val_type, W, stream);
464130803Smarcel	    break;
465130803Smarcel	  }
466130803Smarcel	case 'R':
467130803Smarcel	  {
468130803Smarcel	    LONGEST L, U;
469130803Smarcel	    if (!ada_scan_number (name, p + 1, &L, &p)
470130803Smarcel		|| name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
471130803Smarcel	      goto Huh;
472130803Smarcel	    ada_print_scalar (val_type, L, stream);
473130803Smarcel	    fprintf_filtered (stream, " .. ");
474130803Smarcel	    ada_print_scalar (val_type, U, stream);
475130803Smarcel	    break;
476130803Smarcel	  }
477130803Smarcel	case 'O':
478130803Smarcel	  fprintf_filtered (stream, "others");
479130803Smarcel	  p += 1;
480130803Smarcel	  break;
481130803Smarcel	}
482130803Smarcel    }
483130803Smarcel
484130803SmarcelHuh:
485130803Smarcel  fprintf_filtered (stream, "??");
486130803Smarcel
487130803Smarcel}
488130803Smarcel
489130803Smarcel/* Assuming that field FIELD_NUM of TYPE is a VARIANTS field whose
490130803Smarcel   discriminant is contained in OUTER_TYPE, print its variants on STREAM.
491130803Smarcel   LEVEL is the recursion
492130803Smarcel   (indentation) level, in case any of the fields themselves have
493130803Smarcel   nested structure, and SHOW is the number of levels of internal structure
494130803Smarcel   to show (see ada_print_type). For this purpose, fields nested in a
495130803Smarcel   variant part are taken to be at the same level as the fields
496130803Smarcel   immediately outside the variant part. */
497130803Smarcel
498130803Smarcelstatic void
499130803Smarcelprint_variant_clauses (struct type *type, int field_num,
500130803Smarcel		       struct type *outer_type, struct ui_file *stream,
501130803Smarcel		       int show, int level)
502130803Smarcel{
503130803Smarcel  int i;
504130803Smarcel  struct type *var_type;
505130803Smarcel  struct type *discr_type;
506130803Smarcel
507130803Smarcel  var_type = TYPE_FIELD_TYPE (type, field_num);
508130803Smarcel  discr_type = ada_variant_discrim_type (var_type, outer_type);
509130803Smarcel
510130803Smarcel  if (TYPE_CODE (var_type) == TYPE_CODE_PTR)
511130803Smarcel    {
512130803Smarcel      var_type = TYPE_TARGET_TYPE (var_type);
513130803Smarcel      if (TYPE_FLAGS (var_type) & TYPE_FLAG_STUB)
514130803Smarcel	{
515130803Smarcel	  var_type = ada_find_parallel_type (var_type, "___XVU");
516130803Smarcel	  if (var_type == NULL)
517130803Smarcel	    return;
518130803Smarcel	}
519130803Smarcel    }
520130803Smarcel
521130803Smarcel  for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
522130803Smarcel    {
523130803Smarcel      fprintf_filtered (stream, "\n%*swhen ", level + 4, "");
524130803Smarcel      print_choices (var_type, i, stream, discr_type);
525130803Smarcel      fprintf_filtered (stream, " =>");
526130803Smarcel      if (print_record_field_types (TYPE_FIELD_TYPE (var_type, i),
527130803Smarcel				    outer_type, stream, show, level + 4) <= 0)
528130803Smarcel	fprintf_filtered (stream, " null;");
529130803Smarcel    }
530130803Smarcel}
531130803Smarcel
532130803Smarcel/* Assuming that field FIELD_NUM of TYPE is a variant part whose
533130803Smarcel   discriminants are contained in OUTER_TYPE, print a description of it
534130803Smarcel   on STREAM.  LEVEL is the recursion (indentation) level, in case any of
535130803Smarcel   the fields themselves have nested structure, and SHOW is the number of
536130803Smarcel   levels of internal structure to show (see ada_print_type). For this
537130803Smarcel   purpose, fields nested in a variant part are taken to be at the same
538130803Smarcel   level as the fields immediately outside the variant part. */
539130803Smarcel
540130803Smarcelstatic void
541130803Smarcelprint_variant_part (struct type *type, int field_num, struct type *outer_type,
542130803Smarcel		    struct ui_file *stream, int show, int level)
543130803Smarcel{
544130803Smarcel  fprintf_filtered (stream, "\n%*scase %s is", level + 4, "",
545130803Smarcel		    ada_variant_discrim_name
546130803Smarcel		    (TYPE_FIELD_TYPE (type, field_num)));
547130803Smarcel  print_variant_clauses (type, field_num, outer_type, stream, show,
548130803Smarcel			 level + 4);
549130803Smarcel  fprintf_filtered (stream, "\n%*send case;", level + 4, "");
550130803Smarcel}
551130803Smarcel
552130803Smarcel/* Print a description on STREAM of the fields in record type TYPE, whose
553130803Smarcel   discriminants are in OUTER_TYPE.  LEVEL is the recursion (indentation)
554130803Smarcel   level, in case any of the fields themselves have nested structure,
555130803Smarcel   and SHOW is the number of levels of internal structure to show
556130803Smarcel   (see ada_print_type).  Does not print parent type information of TYPE.
557130803Smarcel   Returns 0 if no fields printed, -1 for an incomplete type, else > 0.
558130803Smarcel   Prints each field beginning on a new line, but does not put a new line at
559130803Smarcel   end. */
560130803Smarcel
561130803Smarcelstatic int
562130803Smarcelprint_record_field_types (struct type *type, struct type *outer_type,
563130803Smarcel			  struct ui_file *stream, int show, int level)
564130803Smarcel{
565130803Smarcel  int len, i, flds;
566130803Smarcel
567130803Smarcel  flds = 0;
568130803Smarcel  len = TYPE_NFIELDS (type);
569130803Smarcel
570130803Smarcel  if (len == 0 && (TYPE_FLAGS (type) & TYPE_FLAG_STUB) != 0)
571130803Smarcel    return -1;
572130803Smarcel
573130803Smarcel  for (i = 0; i < len; i += 1)
574130803Smarcel    {
575130803Smarcel      QUIT;
576130803Smarcel
577130803Smarcel      if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
578130803Smarcel	;
579130803Smarcel      else if (ada_is_wrapper_field (type, i))
580130803Smarcel	flds += print_record_field_types (TYPE_FIELD_TYPE (type, i), type,
581130803Smarcel					  stream, show, level);
582130803Smarcel      else if (ada_is_variant_part (type, i))
583130803Smarcel	{
584130803Smarcel	  print_variant_part (type, i, outer_type, stream, show, level);
585130803Smarcel	  flds = 1;
586130803Smarcel	}
587130803Smarcel      else
588130803Smarcel	{
589130803Smarcel	  flds += 1;
590130803Smarcel	  fprintf_filtered (stream, "\n%*s", level + 4, "");
591130803Smarcel	  ada_print_type (TYPE_FIELD_TYPE (type, i),
592130803Smarcel			  TYPE_FIELD_NAME (type, i),
593130803Smarcel			  stream, show - 1, level + 4);
594130803Smarcel	  fprintf_filtered (stream, ";");
595130803Smarcel	}
596130803Smarcel    }
597130803Smarcel
598130803Smarcel  return flds;
599130803Smarcel}
600130803Smarcel
601130803Smarcel/* Print record type TYPE on STREAM.  LEVEL is the recursion (indentation)
602130803Smarcel   level, in case the element type itself has nested structure, and SHOW is
603130803Smarcel   the number of levels of internal structure to show (see ada_print_type). */
604130803Smarcel
605130803Smarcelstatic void
606130803Smarcelprint_record_type (struct type *type0, struct ui_file *stream, int show,
607130803Smarcel		   int level)
608130803Smarcel{
609130803Smarcel  struct type *parent_type;
610130803Smarcel  struct type *type;
611130803Smarcel
612130803Smarcel  type = type0;
613130803Smarcel  if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
614130803Smarcel    {
615130803Smarcel      struct type *type1 = ada_find_parallel_type (type, "___XVE");
616130803Smarcel      if (type1 != NULL)
617130803Smarcel	type = type1;
618130803Smarcel    }
619130803Smarcel
620130803Smarcel  parent_type = ada_parent_type (type);
621130803Smarcel  if (ada_type_name (parent_type) != NULL)
622130803Smarcel    fprintf_filtered (stream, "new %s with ",
623130803Smarcel		      demangled_type_name (parent_type));
624130803Smarcel  else if (parent_type == NULL && ada_is_tagged_type (type))
625130803Smarcel    fprintf_filtered (stream, "tagged ");
626130803Smarcel
627130803Smarcel  fprintf_filtered (stream, "record");
628130803Smarcel
629130803Smarcel  if (show < 0)
630130803Smarcel    fprintf_filtered (stream, " ... end record");
631130803Smarcel  else
632130803Smarcel    {
633130803Smarcel      int flds;
634130803Smarcel
635130803Smarcel      flds = 0;
636130803Smarcel      if (parent_type != NULL && ada_type_name (parent_type) == NULL)
637130803Smarcel	flds += print_record_field_types (parent_type, parent_type,
638130803Smarcel					  stream, show, level);
639130803Smarcel      flds += print_record_field_types (type, type, stream, show, level);
640130803Smarcel
641130803Smarcel      if (flds > 0)
642130803Smarcel	fprintf_filtered (stream, "\n%*send record", level, "");
643130803Smarcel      else if (flds < 0)
644130803Smarcel	fprintf_filtered (stream, " <incomplete type> end record");
645130803Smarcel      else
646130803Smarcel	fprintf_filtered (stream, " null; end record");
647130803Smarcel    }
648130803Smarcel}
649130803Smarcel
650130803Smarcel/* Print the unchecked union type TYPE in something resembling Ada
651130803Smarcel   format on STREAM. LEVEL is the recursion (indentation) level
652130803Smarcel   in case the element type itself has nested structure, and SHOW is the
653130803Smarcel   number of levels of internal structure to show (see ada_print_type). */
654130803Smarcelstatic void
655130803Smarcelprint_unchecked_union_type (struct type *type, struct ui_file *stream,
656130803Smarcel			    int show, int level)
657130803Smarcel{
658130803Smarcel  fprintf_filtered (stream, "record (?) is");
659130803Smarcel
660130803Smarcel  if (show < 0)
661130803Smarcel    fprintf_filtered (stream, " ... end record");
662130803Smarcel  else if (TYPE_NFIELDS (type) == 0)
663130803Smarcel    fprintf_filtered (stream, " null; end record");
664130803Smarcel  else
665130803Smarcel    {
666130803Smarcel      int i;
667130803Smarcel
668130803Smarcel      fprintf_filtered (stream, "\n%*scase ? is", level + 4, "");
669130803Smarcel
670130803Smarcel      for (i = 0; i < TYPE_NFIELDS (type); i += 1)
671130803Smarcel	{
672130803Smarcel	  fprintf_filtered (stream, "\n%*swhen ? =>\n%*s", level + 8, "",
673130803Smarcel			    level + 12, "");
674130803Smarcel	  ada_print_type (TYPE_FIELD_TYPE (type, i),
675130803Smarcel			  TYPE_FIELD_NAME (type, i),
676130803Smarcel			  stream, show - 1, level + 12);
677130803Smarcel	  fprintf_filtered (stream, ";");
678130803Smarcel	}
679130803Smarcel
680130803Smarcel      fprintf_filtered (stream, "\n%*send case;\n%*send record",
681130803Smarcel			level + 4, "", level, "");
682130803Smarcel    }
683130803Smarcel}
684130803Smarcel
685130803Smarcel
686130803Smarcel
687130803Smarcel/* Print function or procedure type TYPE on STREAM.  Make it a header
688130803Smarcel   for function or procedure NAME if NAME is not null. */
689130803Smarcel
690130803Smarcelstatic void
691130803Smarcelprint_func_type (struct type *type, struct ui_file *stream, char *name)
692130803Smarcel{
693130803Smarcel  int i, len = TYPE_NFIELDS (type);
694130803Smarcel
695130803Smarcel  if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
696130803Smarcel    fprintf_filtered (stream, "procedure");
697130803Smarcel  else
698130803Smarcel    fprintf_filtered (stream, "function");
699130803Smarcel
700130803Smarcel  if (name != NULL && name[0] != '\0')
701130803Smarcel    fprintf_filtered (stream, " %s", name);
702130803Smarcel
703130803Smarcel  if (len > 0)
704130803Smarcel    {
705130803Smarcel      fprintf_filtered (stream, " (");
706130803Smarcel      for (i = 0; i < len; i += 1)
707130803Smarcel	{
708130803Smarcel	  if (i > 0)
709130803Smarcel	    {
710130803Smarcel	      fputs_filtered ("; ", stream);
711130803Smarcel	      wrap_here ("    ");
712130803Smarcel	    }
713130803Smarcel	  fprintf_filtered (stream, "a%d: ", i + 1);
714130803Smarcel	  ada_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
715130803Smarcel	}
716130803Smarcel      fprintf_filtered (stream, ")");
717130803Smarcel    }
718130803Smarcel
719130803Smarcel  if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
720130803Smarcel    {
721130803Smarcel      fprintf_filtered (stream, " return ");
722130803Smarcel      ada_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0);
723130803Smarcel    }
724130803Smarcel}
725130803Smarcel
726130803Smarcel
727130803Smarcel/* Print a description of a type TYPE0.
728130803Smarcel   Output goes to STREAM (via stdio).
729130803Smarcel   If VARSTRING is a non-empty string, print as an Ada variable/field
730130803Smarcel       declaration.
731130803Smarcel   SHOW+1 is the maximum number of levels of internal type structure
732130803Smarcel      to show (this applies to record types, enumerated types, and
733130803Smarcel      array types).
734130803Smarcel   SHOW is the number of levels of internal type structure to show
735130803Smarcel      when there is a type name for the SHOWth deepest level (0th is
736130803Smarcel      outer level).
737130803Smarcel   When SHOW<0, no inner structure is shown.
738130803Smarcel   LEVEL indicates level of recursion (for nested definitions). */
739130803Smarcel
740130803Smarcelvoid
741130803Smarcelada_print_type (struct type *type0, char *varstring, struct ui_file *stream,
742130803Smarcel		int show, int level)
743130803Smarcel{
744130803Smarcel  enum type_code code;
745130803Smarcel  int demangled_args;
746130803Smarcel  struct type *type = ada_completed_type (ada_get_base_type (type0));
747130803Smarcel  char *type_name = demangled_type_name (type);
748130803Smarcel  int is_var_decl = (varstring != NULL && varstring[0] != '\0');
749130803Smarcel
750130803Smarcel  if (type == NULL)
751130803Smarcel    {
752130803Smarcel      if (is_var_decl)
753130803Smarcel	fprintf_filtered (stream, "%.*s: ",
754130803Smarcel			  ada_name_prefix_len (varstring), varstring);
755130803Smarcel      fprintf_filtered (stream, "<null type?>");
756130803Smarcel      return;
757130803Smarcel    }
758130803Smarcel
759130803Smarcel  if (show > 0)
760130803Smarcel    CHECK_TYPEDEF (type);
761130803Smarcel
762130803Smarcel  if (is_var_decl && TYPE_CODE (type) != TYPE_CODE_FUNC)
763130803Smarcel    fprintf_filtered (stream, "%.*s: ",
764130803Smarcel		      ada_name_prefix_len (varstring), varstring);
765130803Smarcel
766130803Smarcel  if (type_name != NULL && show <= 0)
767130803Smarcel    {
768130803Smarcel      fprintf_filtered (stream, "%.*s",
769130803Smarcel			ada_name_prefix_len (type_name), type_name);
770130803Smarcel      return;
771130803Smarcel    }
772130803Smarcel
773130803Smarcel  if (ada_is_aligner_type (type))
774130803Smarcel    ada_print_type (ada_aligned_type (type), "", stream, show, level);
775130803Smarcel  else if (ada_is_packed_array_type (type))
776130803Smarcel    print_array_type (type, stream, show, level);
777130803Smarcel  else
778130803Smarcel    switch (TYPE_CODE (type))
779130803Smarcel      {
780130803Smarcel      default:
781130803Smarcel	fprintf_filtered (stream, "<");
782130803Smarcel	c_print_type (type, "", stream, show, level);
783130803Smarcel	fprintf_filtered (stream, ">");
784130803Smarcel	break;
785130803Smarcel      case TYPE_CODE_PTR:
786130803Smarcel	fprintf_filtered (stream, "access ");
787130803Smarcel	ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
788130803Smarcel	break;
789130803Smarcel      case TYPE_CODE_REF:
790130803Smarcel	fprintf_filtered (stream, "<ref> ");
791130803Smarcel	ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level);
792130803Smarcel	break;
793130803Smarcel      case TYPE_CODE_ARRAY:
794130803Smarcel	print_array_type (type, stream, show, level);
795130803Smarcel	break;
796130803Smarcel      case TYPE_CODE_INT:
797130803Smarcel	if (ada_is_fixed_point_type (type))
798130803Smarcel	  print_fixed_point_type (type, stream);
799130803Smarcel	else if (ada_is_vax_floating_type (type))
800130803Smarcel	  print_vax_floating_point_type (type, stream);
801130803Smarcel	else
802130803Smarcel	  {
803130803Smarcel	    char *name = ada_type_name (type);
804130803Smarcel	    if (!ada_is_range_type_name (name))
805130803Smarcel	      fprintf_filtered (stream, "<%d-byte integer>",
806130803Smarcel				TYPE_LENGTH (type));
807130803Smarcel	    else
808130803Smarcel	      {
809130803Smarcel		fprintf_filtered (stream, "range ");
810130803Smarcel		print_range_type_named (name, stream);
811130803Smarcel	      }
812130803Smarcel	  }
813130803Smarcel	break;
814130803Smarcel      case TYPE_CODE_RANGE:
815130803Smarcel	if (ada_is_fixed_point_type (type))
816130803Smarcel	  print_fixed_point_type (type, stream);
817130803Smarcel	else if (ada_is_vax_floating_type (type))
818130803Smarcel	  print_vax_floating_point_type (type, stream);
819130803Smarcel	else if (ada_is_modular_type (type))
820130803Smarcel	  fprintf_filtered (stream, "mod %ld", (long) ada_modulus (type));
821130803Smarcel	else
822130803Smarcel	  {
823130803Smarcel	    fprintf_filtered (stream, "range ");
824130803Smarcel	    print_range (type, stream);
825130803Smarcel	  }
826130803Smarcel	break;
827130803Smarcel      case TYPE_CODE_FLT:
828130803Smarcel	fprintf_filtered (stream, "<%d-byte float>", TYPE_LENGTH (type));
829130803Smarcel	break;
830130803Smarcel      case TYPE_CODE_ENUM:
831130803Smarcel	if (show < 0)
832130803Smarcel	  fprintf_filtered (stream, "(...)");
833130803Smarcel	else
834130803Smarcel	  print_enum_type (type, stream);
835130803Smarcel	break;
836130803Smarcel      case TYPE_CODE_STRUCT:
837130803Smarcel	if (ada_is_array_descriptor (type))
838130803Smarcel	  print_array_type (type, stream, show, level);
839130803Smarcel	else if (ada_is_bogus_array_descriptor (type))
840130803Smarcel	  fprintf_filtered (stream,
841130803Smarcel			    "array (?) of ? (<mal-formed descriptor>)");
842130803Smarcel	else
843130803Smarcel	  print_record_type (type, stream, show, level);
844130803Smarcel	break;
845130803Smarcel      case TYPE_CODE_UNION:
846130803Smarcel	print_unchecked_union_type (type, stream, show, level);
847130803Smarcel	break;
848130803Smarcel      case TYPE_CODE_FUNC:
849130803Smarcel	print_func_type (type, stream, varstring);
850130803Smarcel	break;
851130803Smarcel      }
852130803Smarcel}
853