gdb/ada-typeprint.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Support for printing Ada types for GDB, the GNU debugger.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdb_obstack.h"
  16. #include "bfd.h"                /* Binary File Description */
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "value.h"
  21. #include "gdbcore.h"
  22. #include "target.h"
  23. #include "command.h"
  24. #include "gdbcmd.h"
  25. #include "language.h"
  26. #include "demangle.h"
  27. #include "c-lang.h"
  28. #include "typeprint.h"
  29. #include "ada-lang.h"
  30. #include <ctype.h>

  31. static int print_selected_record_field_types (struct type *, struct type *,
  32.                                               int, int,
  33.                                               struct ui_file *, int, int,
  34.                                               const struct type_print_options *);

  35. static int print_record_field_types (struct type *, struct type *,
  36.                                      struct ui_file *, int, int,
  37.                                      const struct type_print_options *);



  38. static char *name_buffer;
  39. static int name_buffer_len;

  40. /* The (decoded) Ada name of TYPE.  This value persists until the
  41.    next call.  */

  42. static char *
  43. decoded_type_name (struct type *type)
  44. {
  45.   if (ada_type_name (type) == NULL)
  46.     return NULL;
  47.   else
  48.     {
  49.       const char *raw_name = ada_type_name (type);
  50.       char *s, *q;

  51.       if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
  52.         {
  53.           name_buffer_len = 16 + 2 * strlen (raw_name);
  54.           name_buffer = xrealloc (name_buffer, name_buffer_len);
  55.         }
  56.       strcpy (name_buffer, raw_name);

  57.       s = (char *) strstr (name_buffer, "___");
  58.       if (s != NULL)
  59.         *s = '\0';

  60.       s = name_buffer + strlen (name_buffer) - 1;
  61.       while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
  62.         s -= 1;

  63.       if (s == name_buffer)
  64.         return name_buffer;

  65.       if (!islower (s[1]))
  66.         return NULL;

  67.       for (s = q = name_buffer; *s != '\0'; q += 1)
  68.         {
  69.           if (s[0] == '_' && s[1] == '_')
  70.             {
  71.               *q = '.';
  72.               s += 2;
  73.             }
  74.           else
  75.             {
  76.               *q = *s;
  77.               s += 1;
  78.             }
  79.         }
  80.       *q = '\0';
  81.       return name_buffer;
  82.     }
  83. }

  84. /* Return nonzero if TYPE is a subrange type, and its bounds
  85.    are identical to the bounds of its subtype.  */

  86. static int
  87. type_is_full_subrange_of_target_type (struct type *type)
  88. {
  89.   struct type *subtype;

  90.   if (TYPE_CODE (type) != TYPE_CODE_RANGE)
  91.     return 0;

  92.   subtype = TYPE_TARGET_TYPE (type);
  93.   if (subtype == NULL)
  94.     return 0;

  95.   if (is_dynamic_type (type))
  96.     return 0;

  97.   if (ada_discrete_type_low_bound (type)
  98.       != ada_discrete_type_low_bound (subtype))
  99.     return 0;

  100.   if (ada_discrete_type_high_bound (type)
  101.       != ada_discrete_type_high_bound (subtype))
  102.     return 0;

  103.   return 1;
  104. }

  105. /* Print TYPE on STREAM, preferably as a range if BOUNDS_PREFERED_P
  106.    is nonzero.  */

  107. static void
  108. print_range (struct type *type, struct ui_file *stream,
  109.              int bounds_prefered_p)
  110. {
  111.   if (!bounds_prefered_p)
  112.     {
  113.       /* Try stripping all TYPE_CODE_RANGE layers whose bounds
  114.          are identical to the bounds of their subtype.  When
  115.          the bounds of both types match, it can allow us to
  116.          print a range using the name of its base type, which
  117.          is easier to read.  For instance, we would print...

  118.              array (character) of ...

  119.          ... instead of...

  120.              array ('["00"]' .. '["ff"]') of ...  */
  121.       while (type_is_full_subrange_of_target_type (type))
  122.         type = TYPE_TARGET_TYPE (type);
  123.     }

  124.   switch (TYPE_CODE (type))
  125.     {
  126.     case TYPE_CODE_RANGE:
  127.     case TYPE_CODE_ENUM:
  128.       {
  129.         struct type *target_type;
  130.         volatile struct gdb_exception e;
  131.         LONGEST lo = 0, hi = 0; /* init for gcc -Wall */

  132.         target_type = TYPE_TARGET_TYPE (type);
  133.         if (target_type == NULL)
  134.           target_type = type;

  135.         TRY_CATCH (e, RETURN_MASK_ERROR)
  136.           {
  137.             lo = ada_discrete_type_low_bound (type);
  138.             hi = ada_discrete_type_high_bound (type);
  139.           }
  140.         if (e.reason < 0)
  141.           {
  142.             /* This can happen when the range is dynamic.  Sometimes,
  143.                resolving dynamic property values requires us to have
  144.                access to an actual object, which is not available
  145.                when the user is using the "ptype" command on a type.
  146.                Print the range as an unbounded range.  */
  147.             fprintf_filtered (stream, "<>");
  148.           }
  149.         else
  150.           {
  151.             ada_print_scalar (target_type, lo, stream);
  152.             fprintf_filtered (stream, " .. ");
  153.             ada_print_scalar (target_type, hi, stream);
  154.           }
  155.       }
  156.       break;
  157.     default:
  158.       fprintf_filtered (stream, "%.*s",
  159.                         ada_name_prefix_len (TYPE_NAME (type)),
  160.                         TYPE_NAME (type));
  161.       break;
  162.     }
  163. }

  164. /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
  165.    set *N past the bound and its delimiter, if any.  */

  166. static void
  167. print_range_bound (struct type *type, char *bounds, int *n,
  168.                    struct ui_file *stream)
  169. {
  170.   LONGEST B;

  171.   if (ada_scan_number (bounds, *n, &B, n))
  172.     {
  173.       /* STABS decodes all range types which bounds are 0 .. -1 as
  174.          unsigned integers (ie. the type code is TYPE_CODE_INT, not
  175.          TYPE_CODE_RANGE).  Unfortunately, ada_print_scalar() relies
  176.          on the unsigned flag to determine whether the bound should
  177.          be printed as a signed or an unsigned value.  This causes
  178.          the upper bound of the 0 .. -1 range types to be printed as
  179.          a very large unsigned number instead of -1.
  180.          To workaround this stabs deficiency, we replace the TYPE by NULL
  181.          to indicate default output when we detect that the bound is negative,
  182.          and the type is a TYPE_CODE_INT.  The bound is negative when
  183.          'm' is the last character of the number scanned in BOUNDS.  */
  184.       if (bounds[*n - 1] == 'm' && TYPE_CODE (type) == TYPE_CODE_INT)
  185.         type = NULL;
  186.       ada_print_scalar (type, B, stream);
  187.       if (bounds[*n] == '_')
  188.         *n += 2;
  189.     }
  190.   else
  191.     {
  192.       int bound_len;
  193.       char *bound = bounds + *n;
  194.       char *pend;

  195.       pend = strstr (bound, "__");
  196.       if (pend == NULL)
  197.         *n += bound_len = strlen (bound);
  198.       else
  199.         {
  200.           bound_len = pend - bound;
  201.           *n += bound_len + 2;
  202.         }
  203.       fprintf_filtered (stream, "%.*s", bound_len, bound);
  204.     }
  205. }

  206. /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
  207.    the value (if found) of the bound indicated by SUFFIX ("___L" or
  208.    "___U") according to the ___XD conventions.  */

  209. static void
  210. print_dynamic_range_bound (struct type *type, const char *name, int name_len,
  211.                            const char *suffix, struct ui_file *stream)
  212. {
  213.   static char *name_buf = NULL;
  214.   static size_t name_buf_len = 0;
  215.   LONGEST B;
  216.   int OK;

  217.   GROW_VECT (name_buf, name_buf_len, name_len + strlen (suffix) + 1);
  218.   strncpy (name_buf, name, name_len);
  219.   strcpy (name_buf + name_len, suffix);

  220.   B = get_int_var_value (name_buf, &OK);
  221.   if (OK)
  222.     ada_print_scalar (type, B, stream);
  223.   else
  224.     fprintf_filtered (stream, "?");
  225. }

  226. /* Print RAW_TYPE as a range type, using any bound information
  227.    following the GNAT encoding (if available).

  228.    If BOUNDS_PREFERED_P is nonzero, force the printing of the range
  229.    using its bounds.  Otherwise, try printing the range without
  230.    printing the value of the bounds, if possible (this is only
  231.    considered a hint, not a guaranty).  */

  232. static void
  233. print_range_type (struct type *raw_type, struct ui_file *stream,
  234.                   int bounds_prefered_p)
  235. {
  236.   const char *name;
  237.   struct type *base_type;
  238.   const char *subtype_info;

  239.   gdb_assert (raw_type != NULL);
  240.   name = TYPE_NAME (raw_type);
  241.   gdb_assert (name != NULL);

  242.   if (TYPE_CODE (raw_type) == TYPE_CODE_RANGE)
  243.     base_type = TYPE_TARGET_TYPE (raw_type);
  244.   else
  245.     base_type = raw_type;

  246.   subtype_info = strstr (name, "___XD");
  247.   if (subtype_info == NULL)
  248.     print_range (raw_type, stream, bounds_prefered_p);
  249.   else
  250.     {
  251.       int prefix_len = subtype_info - name;
  252.       char *bounds_str;
  253.       int n;

  254.       subtype_info += 5;
  255.       bounds_str = strchr (subtype_info, '_');
  256.       n = 1;

  257.       if (*subtype_info == 'L')
  258.         {
  259.           print_range_bound (base_type, bounds_str, &n, stream);
  260.           subtype_info += 1;
  261.         }
  262.       else
  263.         print_dynamic_range_bound (base_type, name, prefix_len, "___L",
  264.                                    stream);

  265.       fprintf_filtered (stream, " .. ");

  266.       if (*subtype_info == 'U')
  267.         print_range_bound (base_type, bounds_str, &n, stream);
  268.       else
  269.         print_dynamic_range_bound (base_type, name, prefix_len, "___U",
  270.                                    stream);
  271.     }
  272. }

  273. /* Print enumerated type TYPE on STREAM.  */

  274. static void
  275. print_enum_type (struct type *type, struct ui_file *stream)
  276. {
  277.   int len = TYPE_NFIELDS (type);
  278.   int i;
  279.   LONGEST lastval;

  280.   fprintf_filtered (stream, "(");
  281.   wrap_here (" ");

  282.   lastval = 0;
  283.   for (i = 0; i < len; i++)
  284.     {
  285.       QUIT;
  286.       if (i)
  287.         fprintf_filtered (stream, ", ");
  288.       wrap_here ("    ");
  289.       fputs_filtered (ada_enum_name (TYPE_FIELD_NAME (type, i)), stream);
  290.       if (lastval != TYPE_FIELD_ENUMVAL (type, i))
  291.         {
  292.           fprintf_filtered (stream, " => %s",
  293.                             plongest (TYPE_FIELD_ENUMVAL (type, i)));
  294.           lastval = TYPE_FIELD_ENUMVAL (type, i);
  295.         }
  296.       lastval += 1;
  297.     }
  298.   fprintf_filtered (stream, ")");
  299. }

  300. /* Print representation of Ada fixed-point type TYPE on STREAM.  */

  301. static void
  302. print_fixed_point_type (struct type *type, struct ui_file *stream)
  303. {
  304.   DOUBLEST delta = ada_delta (type);
  305.   DOUBLEST small = ada_fixed_to_float (type, 1.0);

  306.   if (delta < 0.0)
  307.     fprintf_filtered (stream, "delta ??");
  308.   else
  309.     {
  310.       fprintf_filtered (stream, "delta %g", (double) delta);
  311.       if (delta != small)
  312.         fprintf_filtered (stream, " <'small = %g>", (double) small);
  313.     }
  314. }

  315. /* Print simple (constrained) array type TYPE on STREAM.  LEVEL is the
  316.    recursion (indentation) level, in case the element type itself has
  317.    nested structure, and SHOW is the number of levels of internal
  318.    structure to show (see ada_print_type).  */

  319. static void
  320. print_array_type (struct type *type, struct ui_file *stream, int show,
  321.                   int level, const struct type_print_options *flags)
  322. {
  323.   int bitsize;
  324.   int n_indices;

  325.   if (ada_is_constrained_packed_array_type (type))
  326.     type = ada_coerce_to_simple_array_type (type);

  327.   bitsize = 0;
  328.   fprintf_filtered (stream, "array (");

  329.   if (type == NULL)
  330.     {
  331.       fprintf_filtered (stream, _("<undecipherable array type>"));
  332.       return;
  333.     }

  334.   n_indices = -1;
  335.   if (ada_is_simple_array_type (type))
  336.     {
  337.       struct type *range_desc_type;
  338.       struct type *arr_type;

  339.       range_desc_type = ada_find_parallel_type (type, "___XA");
  340.       ada_fixup_array_indexes_type (range_desc_type);

  341.       bitsize = 0;
  342.       if (range_desc_type == NULL)
  343.         {
  344.           for (arr_type = type; TYPE_CODE (arr_type) == TYPE_CODE_ARRAY;
  345.                arr_type = TYPE_TARGET_TYPE (arr_type))
  346.             {
  347.               if (arr_type != type)
  348.                 fprintf_filtered (stream, ", ");
  349.               print_range (TYPE_INDEX_TYPE (arr_type), stream,
  350.                            0 /* bounds_prefered_p */);
  351.               if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
  352.                 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
  353.             }
  354.         }
  355.       else
  356.         {
  357.           int k;

  358.           n_indices = TYPE_NFIELDS (range_desc_type);
  359.           for (k = 0, arr_type = type;
  360.                k < n_indices;
  361.                k += 1, arr_type = TYPE_TARGET_TYPE (arr_type))
  362.             {
  363.               if (k > 0)
  364.                 fprintf_filtered (stream, ", ");
  365.               print_range_type (TYPE_FIELD_TYPE (range_desc_type, k),
  366.                                 stream, 0 /* bounds_prefered_p */);
  367.               if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
  368.                 bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
  369.             }
  370.         }
  371.     }
  372.   else
  373.     {
  374.       int i, i0;

  375.       for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
  376.         fprintf_filtered (stream, "%s<>", i == i0 ? "" : ", ");
  377.     }

  378.   fprintf_filtered (stream, ") of ");
  379.   wrap_here ("");
  380.   ada_print_type (ada_array_element_type (type, n_indices), "", stream,
  381.                   show == 0 ? 0 : show - 1, level + 1, flags);
  382.   if (bitsize > 0)
  383.     fprintf_filtered (stream, " <packed: %d-bit elements>", bitsize);
  384. }

  385. /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
  386.    STREAM, assuming that VAL_TYPE (if non-NULL) is the type of the
  387.    values.  Return non-zero if the field is an encoding of
  388.    discriminant values, as in a standard variant record, and 0 if the
  389.    field is not so encoded (as happens with single-component variants
  390.    in types annotated with pragma Unchecked_Variant).  */

  391. static int
  392. print_choices (struct type *type, int field_num, struct ui_file *stream,
  393.                struct type *val_type)
  394. {
  395.   int have_output;
  396.   int p;
  397.   const char *name = TYPE_FIELD_NAME (type, field_num);

  398.   have_output = 0;

  399.   /* Skip over leading 'V': NOTE soon to be obsolete.  */
  400.   if (name[0] == 'V')
  401.     {
  402.       if (!ada_scan_number (name, 1, NULL, &p))
  403.         goto Huh;
  404.     }
  405.   else
  406.     p = 0;

  407.   while (1)
  408.     {
  409.       switch (name[p])
  410.         {
  411.         default:
  412.           goto Huh;
  413.         case '_':
  414.         case '\0':
  415.           fprintf_filtered (stream, " =>");
  416.           return 1;
  417.         case 'S':
  418.         case 'R':
  419.         case 'O':
  420.           if (have_output)
  421.             fprintf_filtered (stream, " | ");
  422.           have_output = 1;
  423.           break;
  424.         }

  425.       switch (name[p])
  426.         {
  427.         case 'S':
  428.           {
  429.             LONGEST W;

  430.             if (!ada_scan_number (name, p + 1, &W, &p))
  431.               goto Huh;
  432.             ada_print_scalar (val_type, W, stream);
  433.             break;
  434.           }
  435.         case 'R':
  436.           {
  437.             LONGEST L, U;

  438.             if (!ada_scan_number (name, p + 1, &L, &p)
  439.                 || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
  440.               goto Huh;
  441.             ada_print_scalar (val_type, L, stream);
  442.             fprintf_filtered (stream, " .. ");
  443.             ada_print_scalar (val_type, U, stream);
  444.             break;
  445.           }
  446.         case 'O':
  447.           fprintf_filtered (stream, "others");
  448.           p += 1;
  449.           break;
  450.         }
  451.     }

  452. Huh:
  453.   fprintf_filtered (stream, "?? =>");
  454.   return 0;
  455. }

  456. /* Assuming that field FIELD_NUM of TYPE represents variants whose
  457.    discriminant is contained in OUTER_TYPE, print its components on STREAM.
  458.    LEVEL is the recursion (indentation) level, in case any of the fields
  459.    themselves have nested structure, and SHOW is the number of levels of
  460.    internal structure to show (see ada_print_type).  For this purpose,
  461.    fields nested in a variant part are taken to be at the same level as
  462.    the fields immediately outside the variant part.  */

  463. static void
  464. print_variant_clauses (struct type *type, int field_num,
  465.                        struct type *outer_type, struct ui_file *stream,
  466.                        int show, int level,
  467.                        const struct type_print_options *flags)
  468. {
  469.   int i;
  470.   struct type *var_type, *par_type;
  471.   struct type *discr_type;

  472.   var_type = TYPE_FIELD_TYPE (type, field_num);
  473.   discr_type = ada_variant_discrim_type (var_type, outer_type);

  474.   if (TYPE_CODE (var_type) == TYPE_CODE_PTR)
  475.     {
  476.       var_type = TYPE_TARGET_TYPE (var_type);
  477.       if (var_type == NULL || TYPE_CODE (var_type) != TYPE_CODE_UNION)
  478.         return;
  479.     }

  480.   par_type = ada_find_parallel_type (var_type, "___XVU");
  481.   if (par_type != NULL)
  482.     var_type = par_type;

  483.   for (i = 0; i < TYPE_NFIELDS (var_type); i += 1)
  484.     {
  485.       fprintf_filtered (stream, "\n%*swhen ", level + 4, "");
  486.       if (print_choices (var_type, i, stream, discr_type))
  487.         {
  488.           if (print_record_field_types (TYPE_FIELD_TYPE (var_type, i),
  489.                                         outer_type, stream, show, level + 4,
  490.                                         flags)
  491.               <= 0)
  492.             fprintf_filtered (stream, " null;");
  493.         }
  494.       else
  495.         print_selected_record_field_types (var_type, outer_type, i, i,
  496.                                            stream, show, level + 4, flags);
  497.     }
  498. }

  499. /* Assuming that field FIELD_NUM of TYPE is a variant part whose
  500.    discriminants are contained in OUTER_TYPE, print a description of it
  501.    on STREAM.  LEVEL is the recursion (indentation) level, in case any of
  502.    the fields themselves have nested structure, and SHOW is the number of
  503.    levels of internal structure to show (see ada_print_type).  For this
  504.    purpose, fields nested in a variant part are taken to be at the same
  505.    level as the fields immediately outside the variant part.  */

  506. static void
  507. print_variant_part (struct type *type, int field_num, struct type *outer_type,
  508.                     struct ui_file *stream, int show, int level,
  509.                     const struct type_print_options *flags)
  510. {
  511.   fprintf_filtered (stream, "\n%*scase %s is", level + 4, "",
  512.                     ada_variant_discrim_name
  513.                     (TYPE_FIELD_TYPE (type, field_num)));
  514.   print_variant_clauses (type, field_num, outer_type, stream, show,
  515.                          level + 4, flags);
  516.   fprintf_filtered (stream, "\n%*send case;", level + 4, "");
  517. }

  518. /* Print a description on STREAM of the fields FLD0 through FLD1 in
  519.    record or union type TYPE, whose discriminants are in OUTER_TYPE.
  520.    LEVEL is the recursion (indentation) level, in case any of the
  521.    fields themselves have nested structure, and SHOW is the number of
  522.    levels of internal structure to show (see ada_print_type).  Does
  523.    not print parent type information of TYPE.  Returns 0 if no fields
  524.    printed, -1 for an incomplete type, else > 0.  Prints each field
  525.    beginning on a new line, but does not put a new line at end.  */

  526. static int
  527. print_selected_record_field_types (struct type *type, struct type *outer_type,
  528.                                    int fld0, int fld1,
  529.                                    struct ui_file *stream, int show, int level,
  530.                                    const struct type_print_options *flags)
  531. {
  532.   int i, flds;

  533.   flds = 0;

  534.   if (fld0 > fld1 && TYPE_STUB (type))
  535.     return -1;

  536.   for (i = fld0; i <= fld1; i += 1)
  537.     {
  538.       QUIT;

  539.       if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
  540.         ;
  541.       else if (ada_is_wrapper_field (type, i))
  542.         flds += print_record_field_types (TYPE_FIELD_TYPE (type, i), type,
  543.                                           stream, show, level, flags);
  544.       else if (ada_is_variant_part (type, i))
  545.         {
  546.           print_variant_part (type, i, outer_type, stream, show, level, flags);
  547.           flds = 1;
  548.         }
  549.       else
  550.         {
  551.           flds += 1;
  552.           fprintf_filtered (stream, "\n%*s", level + 4, "");
  553.           ada_print_type (TYPE_FIELD_TYPE (type, i),
  554.                           TYPE_FIELD_NAME (type, i),
  555.                           stream, show - 1, level + 4, flags);
  556.           fprintf_filtered (stream, ";");
  557.         }
  558.     }

  559.   return flds;
  560. }

  561. /* Print a description on STREAM of all fields of record or union type
  562.    TYPE, as for print_selected_record_field_types, above.  */

  563. static int
  564. print_record_field_types (struct type *type, struct type *outer_type,
  565.                           struct ui_file *stream, int show, int level,
  566.                           const struct type_print_options *flags)
  567. {
  568.   return print_selected_record_field_types (type, outer_type,
  569.                                             0, TYPE_NFIELDS (type) - 1,
  570.                                             stream, show, level, flags);
  571. }


  572. /* Print record type TYPE on STREAM.  LEVEL is the recursion (indentation)
  573.    level, in case the element type itself has nested structure, and SHOW is
  574.    the number of levels of internal structure to show (see ada_print_type).  */

  575. static void
  576. print_record_type (struct type *type0, struct ui_file *stream, int show,
  577.                    int level, const struct type_print_options *flags)
  578. {
  579.   struct type *parent_type;
  580.   struct type *type;

  581.   type = ada_find_parallel_type (type0, "___XVE");
  582.   if (type == NULL)
  583.     type = type0;

  584.   parent_type = ada_parent_type (type);
  585.   if (ada_type_name (parent_type) != NULL)
  586.     {
  587.       const char *parent_name = decoded_type_name (parent_type);

  588.       /* If we fail to decode the parent type name, then use the parent
  589.          type name as is.  Not pretty, but should never happen except
  590.          when the debugging info is incomplete or incorrect.  This
  591.          prevents a crash trying to print a NULL pointer.  */
  592.       if (parent_name == NULL)
  593.         parent_name = ada_type_name (parent_type);
  594.       fprintf_filtered (stream, "new %s with record", parent_name);
  595.     }
  596.   else if (parent_type == NULL && ada_is_tagged_type (type, 0))
  597.     fprintf_filtered (stream, "tagged record");
  598.   else
  599.     fprintf_filtered (stream, "record");

  600.   if (show < 0)
  601.     fprintf_filtered (stream, " ... end record");
  602.   else
  603.     {
  604.       int flds;

  605.       flds = 0;
  606.       if (parent_type != NULL && ada_type_name (parent_type) == NULL)
  607.         flds += print_record_field_types (parent_type, parent_type,
  608.                                           stream, show, level, flags);
  609.       flds += print_record_field_types (type, type, stream, show, level,
  610.                                         flags);

  611.       if (flds > 0)
  612.         fprintf_filtered (stream, "\n%*send record", level, "");
  613.       else if (flds < 0)
  614.         fprintf_filtered (stream, _(" <incomplete type> end record"));
  615.       else
  616.         fprintf_filtered (stream, " null; end record");
  617.     }
  618. }

  619. /* Print the unchecked union type TYPE in something resembling Ada
  620.    format on STREAM.  LEVEL is the recursion (indentation) level
  621.    in case the element type itself has nested structure, and SHOW is the
  622.    number of levels of internal structure to show (see ada_print_type).  */
  623. static void
  624. print_unchecked_union_type (struct type *type, struct ui_file *stream,
  625.                             int show, int level,
  626.                             const struct type_print_options *flags)
  627. {
  628.   if (show < 0)
  629.     fprintf_filtered (stream, "record (?) is ... end record");
  630.   else if (TYPE_NFIELDS (type) == 0)
  631.     fprintf_filtered (stream, "record (?) is null; end record");
  632.   else
  633.     {
  634.       int i;

  635.       fprintf_filtered (stream, "record (?) is\n%*scase ? is", level + 4, "");

  636.       for (i = 0; i < TYPE_NFIELDS (type); i += 1)
  637.         {
  638.           fprintf_filtered (stream, "\n%*swhen ? =>\n%*s", level + 8, "",
  639.                             level + 12, "");
  640.           ada_print_type (TYPE_FIELD_TYPE (type, i),
  641.                           TYPE_FIELD_NAME (type, i),
  642.                           stream, show - 1, level + 12, flags);
  643.           fprintf_filtered (stream, ";");
  644.         }

  645.       fprintf_filtered (stream, "\n%*send case;\n%*send record",
  646.                         level + 4, "", level, "");
  647.     }
  648. }



  649. /* Print function or procedure type TYPE on STREAM.  Make it a header
  650.    for function or procedure NAME if NAME is not null.  */

  651. static void
  652. print_func_type (struct type *type, struct ui_file *stream, const char *name,
  653.                  const struct type_print_options *flags)
  654. {
  655.   int i, len = TYPE_NFIELDS (type);

  656.   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
  657.     fprintf_filtered (stream, "procedure");
  658.   else
  659.     fprintf_filtered (stream, "function");

  660.   if (name != NULL && name[0] != '\0')
  661.     fprintf_filtered (stream, " %s", name);

  662.   if (len > 0)
  663.     {
  664.       fprintf_filtered (stream, " (");
  665.       for (i = 0; i < len; i += 1)
  666.         {
  667.           if (i > 0)
  668.             {
  669.               fputs_filtered ("; ", stream);
  670.               wrap_here ("    ");
  671.             }
  672.           fprintf_filtered (stream, "a%d: ", i + 1);
  673.           ada_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
  674.                           flags);
  675.         }
  676.       fprintf_filtered (stream, ")");
  677.     }

  678.   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
  679.     {
  680.       fprintf_filtered (stream, " return ");
  681.       ada_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
  682.     }
  683. }


  684. /* Print a description of a type TYPE0.
  685.    Output goes to STREAM (via stdio).
  686.    If VARSTRING is a non-empty string, print as an Ada variable/field
  687.        declaration.
  688.    SHOW+1 is the maximum number of levels of internal type structure
  689.       to show (this applies to record types, enumerated types, and
  690.       array types).
  691.    SHOW is the number of levels of internal type structure to show
  692.       when there is a type name for the SHOWth deepest level (0th is
  693.       outer level).
  694.    When SHOW<0, no inner structure is shown.
  695.    LEVEL indicates level of recursion (for nested definitions).  */

  696. void
  697. ada_print_type (struct type *type0, const char *varstring,
  698.                 struct ui_file *stream, int show, int level,
  699.                 const struct type_print_options *flags)
  700. {
  701.   struct type *type = ada_check_typedef (ada_get_base_type (type0));
  702.   char *type_name = decoded_type_name (type0);
  703.   int is_var_decl = (varstring != NULL && varstring[0] != '\0');

  704.   if (type == NULL)
  705.     {
  706.       if (is_var_decl)
  707.         fprintf_filtered (stream, "%.*s: ",
  708.                           ada_name_prefix_len (varstring), varstring);
  709.       fprintf_filtered (stream, "<null type?>");
  710.       return;
  711.     }

  712.   if (show > 0)
  713.     type = ada_check_typedef (type);

  714.   if (is_var_decl && TYPE_CODE (type) != TYPE_CODE_FUNC)
  715.     fprintf_filtered (stream, "%.*s: ",
  716.                       ada_name_prefix_len (varstring), varstring);

  717.   if (type_name != NULL && show <= 0 && !ada_is_aligner_type (type))
  718.     {
  719.       fprintf_filtered (stream, "%.*s",
  720.                         ada_name_prefix_len (type_name), type_name);
  721.       return;
  722.     }

  723.   if (ada_is_aligner_type (type))
  724.     ada_print_type (ada_aligned_type (type), "", stream, show, level, flags);
  725.   else if (ada_is_constrained_packed_array_type (type)
  726.            && TYPE_CODE (type) != TYPE_CODE_PTR)
  727.     print_array_type (type, stream, show, level, flags);
  728.   else
  729.     switch (TYPE_CODE (type))
  730.       {
  731.       default:
  732.         fprintf_filtered (stream, "<");
  733.         c_print_type (type, "", stream, show, level, flags);
  734.         fprintf_filtered (stream, ">");
  735.         break;
  736.       case TYPE_CODE_PTR:
  737.       case TYPE_CODE_TYPEDEF:
  738.         fprintf_filtered (stream, "access ");
  739.         ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
  740.                         flags);
  741.         break;
  742.       case TYPE_CODE_REF:
  743.         fprintf_filtered (stream, "<ref> ");
  744.         ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
  745.                         flags);
  746.         break;
  747.       case TYPE_CODE_ARRAY:
  748.         print_array_type (type, stream, show, level, flags);
  749.         break;
  750.       case TYPE_CODE_BOOL:
  751.         fprintf_filtered (stream, "(false, true)");
  752.         break;
  753.       case TYPE_CODE_INT:
  754.         if (ada_is_fixed_point_type (type))
  755.           print_fixed_point_type (type, stream);
  756.         else
  757.           {
  758.             const char *name = ada_type_name (type);

  759.             if (!ada_is_range_type_name (name))
  760.               fprintf_filtered (stream, _("<%d-byte integer>"),
  761.                                 TYPE_LENGTH (type));
  762.             else
  763.               {
  764.                 fprintf_filtered (stream, "range ");
  765.                 print_range_type (type, stream, 1 /* bounds_prefered_p */);
  766.               }
  767.           }
  768.         break;
  769.       case TYPE_CODE_RANGE:
  770.         if (ada_is_fixed_point_type (type))
  771.           print_fixed_point_type (type, stream);
  772.         else if (ada_is_modular_type (type))
  773.           fprintf_filtered (stream, "mod %s",
  774.                             int_string (ada_modulus (type), 10, 0, 0, 1));
  775.         else
  776.           {
  777.             fprintf_filtered (stream, "range ");
  778.             print_range (type, stream, 1 /* bounds_prefered_p */);
  779.           }
  780.         break;
  781.       case TYPE_CODE_FLT:
  782.         fprintf_filtered (stream, _("<%d-byte float>"), TYPE_LENGTH (type));
  783.         break;
  784.       case TYPE_CODE_ENUM:
  785.         if (show < 0)
  786.           fprintf_filtered (stream, "(...)");
  787.         else
  788.           print_enum_type (type, stream);
  789.         break;
  790.       case TYPE_CODE_STRUCT:
  791.         if (ada_is_array_descriptor_type (type))
  792.           print_array_type (type, stream, show, level, flags);
  793.         else if (ada_is_bogus_array_descriptor (type))
  794.           fprintf_filtered (stream,
  795.                             _("array (?) of ? (<mal-formed descriptor>)"));
  796.         else
  797.           print_record_type (type, stream, show, level, flags);
  798.         break;
  799.       case TYPE_CODE_UNION:
  800.         print_unchecked_union_type (type, stream, show, level, flags);
  801.         break;
  802.       case TYPE_CODE_FUNC:
  803.         print_func_type (type, stream, varstring, flags);
  804.         break;
  805.       }
  806. }

  807. /* Implement the la_print_typedef language method for Ada.  */

  808. void
  809. ada_print_typedef (struct type *type, struct symbol *new_symbol,
  810.                    struct ui_file *stream)
  811. {
  812.   type = ada_check_typedef (type);
  813.   ada_print_type (type, "", stream, 0, 0, &type_print_raw_options);
  814.   fprintf_filtered (stream, "\n");
  815. }