gdb/m2-typeprint.c - gdb

Functions defined

Source code

  1. /* Support for printing Modula 2 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 "m2-lang.h"
  23. #include "target.h"
  24. #include "language.h"
  25. #include "demangle.h"
  26. #include "c-lang.h"
  27. #include "typeprint.h"
  28. #include "cp-abi.h"

  29. static void m2_print_bounds (struct type *type,
  30.                              struct ui_file *stream, int show, int level,
  31.                              int print_high);

  32. static void m2_typedef (struct type *, struct ui_file *, int, int,
  33.                         const struct type_print_options *);
  34. static void m2_array (struct type *, struct ui_file *, int, int,
  35.                       const struct type_print_options *);
  36. static void m2_pointer (struct type *, struct ui_file *, int, int,
  37.                         const struct type_print_options *);
  38. static void m2_ref (struct type *, struct ui_file *, int, int,
  39.                     const struct type_print_options *);
  40. static void m2_procedure (struct type *, struct ui_file *, int, int,
  41.                           const struct type_print_options *);
  42. static void m2_union (struct type *, struct ui_file *);
  43. static void m2_enum (struct type *, struct ui_file *, int, int);
  44. static void m2_range (struct type *, struct ui_file *, int, int,
  45.                       const struct type_print_options *);
  46. static void m2_type_name (struct type *type, struct ui_file *stream);
  47. static void m2_short_set (struct type *type, struct ui_file *stream,
  48.                           int show, int level);
  49. static int m2_long_set (struct type *type, struct ui_file *stream,
  50.                         int show, int level, const struct type_print_options *flags);
  51. static int m2_unbounded_array (struct type *type, struct ui_file *stream,
  52.                                int show, int level,
  53.                                const struct type_print_options *flags);
  54. static void m2_record_fields (struct type *type, struct ui_file *stream,
  55.                               int show, int level, const struct type_print_options *flags);
  56. static void m2_unknown (const char *s, struct type *type,
  57.                         struct ui_file *stream, int show, int level);

  58. int m2_is_long_set (struct type *type);
  59. int m2_is_long_set_of_type (struct type *type, struct type **of_type);
  60. int m2_is_unbounded_array (struct type *type);


  61. void
  62. m2_print_type (struct type *type, const char *varstring,
  63.                struct ui_file *stream,
  64.                int show, int level,
  65.                const struct type_print_options *flags)
  66. {
  67.   CHECK_TYPEDEF (type);

  68.   QUIT;

  69.   wrap_here ("    ");
  70.   if (type == NULL)
  71.     {
  72.       fputs_filtered (_("<type unknown>"), stream);
  73.       return;
  74.     }

  75.   switch (TYPE_CODE (type))
  76.     {
  77.     case TYPE_CODE_SET:
  78.       m2_short_set(type, stream, show, level);
  79.       break;

  80.     case TYPE_CODE_STRUCT:
  81.       if (m2_long_set (type, stream, show, level, flags)
  82.           || m2_unbounded_array (type, stream, show, level, flags))
  83.         break;
  84.       m2_record_fields (type, stream, show, level, flags);
  85.       break;

  86.     case TYPE_CODE_TYPEDEF:
  87.       m2_typedef (type, stream, show, level, flags);
  88.       break;

  89.     case TYPE_CODE_ARRAY:
  90.       m2_array (type, stream, show, level, flags);
  91.       break;

  92.     case TYPE_CODE_PTR:
  93.       m2_pointer (type, stream, show, level, flags);
  94.       break;

  95.     case TYPE_CODE_REF:
  96.       m2_ref (type, stream, show, level, flags);
  97.       break;

  98.     case TYPE_CODE_METHOD:
  99.       m2_unknown (_("method"), type, stream, show, level);
  100.       break;

  101.     case TYPE_CODE_FUNC:
  102.       m2_procedure (type, stream, show, level, flags);
  103.       break;

  104.     case TYPE_CODE_UNION:
  105.       m2_union (type, stream);
  106.       break;

  107.     case TYPE_CODE_ENUM:
  108.       m2_enum (type, stream, show, level);
  109.       break;

  110.     case TYPE_CODE_VOID:
  111.       break;

  112.     case TYPE_CODE_UNDEF:
  113.       /* i18n: Do not translate the "struct" part!  */
  114.       m2_unknown (_("undef"), type, stream, show, level);
  115.       break;

  116.     case TYPE_CODE_ERROR:
  117.       m2_unknown (_("error"), type, stream, show, level);
  118.       break;

  119.     case TYPE_CODE_RANGE:
  120.       m2_range (type, stream, show, level, flags);
  121.       break;

  122.     default:
  123.       m2_type_name (type, stream);
  124.       break;
  125.     }
  126. }

  127. /* Print a typedef using M2 syntax.  TYPE is the underlying type.
  128.    NEW_SYMBOL is the symbol naming the type.  STREAM is the stream on
  129.    which to print.  */

  130. void
  131. m2_print_typedef (struct type *type, struct symbol *new_symbol,
  132.                   struct ui_file *stream)
  133. {
  134.   CHECK_TYPEDEF (type);
  135.   fprintf_filtered (stream, "TYPE ");
  136.   if (!TYPE_NAME (SYMBOL_TYPE (new_symbol))
  137.       || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
  138.                  SYMBOL_LINKAGE_NAME (new_symbol)) != 0)
  139.     fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new_symbol));
  140.   else
  141.     fprintf_filtered (stream, "<builtin> = ");
  142.   type_print (type, "", stream, 0);
  143.   fprintf_filtered (stream, ";\n");
  144. }

  145. /* m2_type_name - if a, type, has a name then print it.  */

  146. void
  147. m2_type_name (struct type *type, struct ui_file *stream)
  148. {
  149.   if (TYPE_NAME (type) != NULL)
  150.     fputs_filtered (TYPE_NAME (type), stream);
  151. }

  152. /* m2_range - displays a Modula-2 subrange type.  */

  153. void
  154. m2_range (struct type *type, struct ui_file *stream, int show,
  155.           int level, const struct type_print_options *flags)
  156. {
  157.   if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
  158.     m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level,
  159.                    flags);
  160.   else
  161.     {
  162.       struct type *target = TYPE_TARGET_TYPE (type);

  163.       fprintf_filtered (stream, "[");
  164.       print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
  165.       fprintf_filtered (stream, "..");
  166.       print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
  167.       fprintf_filtered (stream, "]");
  168.     }
  169. }

  170. static void
  171. m2_typedef (struct type *type, struct ui_file *stream, int show,
  172.             int level, const struct type_print_options *flags)
  173. {
  174.   if (TYPE_NAME (type) != NULL)
  175.     {
  176.       fputs_filtered (TYPE_NAME (type), stream);
  177.       fputs_filtered (" = ", stream);
  178.     }
  179.   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  180. }

  181. /* m2_array - prints out a Modula-2 ARRAY ... OF type.  */

  182. static void m2_array (struct type *type, struct ui_file *stream,
  183.                       int show, int level, const struct type_print_options *flags)
  184. {
  185.   fprintf_filtered (stream, "ARRAY [");
  186.   if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
  187.       && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
  188.     {
  189.       if (TYPE_INDEX_TYPE (type) != 0)
  190.         {
  191.           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
  192.           fprintf_filtered (stream, "..");
  193.           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
  194.         }
  195.       else
  196.         fprintf_filtered (stream, "%d",
  197.                           (TYPE_LENGTH (type)
  198.                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
  199.     }
  200.   fprintf_filtered (stream, "] OF ");
  201.   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  202. }

  203. static void
  204. m2_pointer (struct type *type, struct ui_file *stream, int show,
  205.             int level, const struct type_print_options *flags)
  206. {
  207.   if (TYPE_CONST (type))
  208.     fprintf_filtered (stream, "[...] : ");
  209.   else
  210.     fprintf_filtered (stream, "POINTER TO ");

  211.   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  212. }

  213. static void
  214. m2_ref (struct type *type, struct ui_file *stream, int show,
  215.         int level, const struct type_print_options *flags)
  216. {
  217.   fprintf_filtered (stream, "VAR");
  218.   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  219. }

  220. static void
  221. m2_unknown (const char *s, struct type *type, struct ui_file *stream,
  222.             int show, int level)
  223. {
  224.   fprintf_filtered (stream, "%s %s", s, _("is unknown"));
  225. }

  226. static void m2_union (struct type *type, struct ui_file *stream)
  227. {
  228.   fprintf_filtered (stream, "union");
  229. }

  230. static void
  231. m2_procedure (struct type *type, struct ui_file *stream,
  232.               int show, int level, const struct type_print_options *flags)
  233. {
  234.   fprintf_filtered (stream, "PROCEDURE ");
  235.   m2_type_name (type, stream);
  236.   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
  237.     {
  238.       int i, len = TYPE_NFIELDS (type);

  239.       fprintf_filtered (stream, " (");
  240.       for (i = 0; i < len; i++)
  241.         {
  242.           if (i > 0)
  243.             {
  244.               fputs_filtered (", ", stream);
  245.               wrap_here ("    ");
  246.             }
  247.           m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, flags);
  248.         }
  249.       if (TYPE_TARGET_TYPE (type) != NULL)
  250.         {
  251.           fprintf_filtered (stream, " : ");
  252.           m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
  253.         }
  254.     }
  255. }

  256. static void
  257. m2_print_bounds (struct type *type,
  258.                  struct ui_file *stream, int show, int level,
  259.                  int print_high)
  260. {
  261.   struct type *target = TYPE_TARGET_TYPE (type);

  262.   if (TYPE_NFIELDS(type) == 0)
  263.     return;

  264.   if (print_high)
  265.     print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
  266.   else
  267.     print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
  268. }

  269. static void
  270. m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
  271. {
  272.   fprintf_filtered(stream, "SET [");
  273.   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
  274.                    show - 1, level, 0);

  275.   fprintf_filtered(stream, "..");
  276.   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
  277.                    show - 1, level, 1);
  278.   fprintf_filtered(stream, "]");
  279. }

  280. int
  281. m2_is_long_set (struct type *type)
  282. {
  283.   LONGEST previous_high = 0/* Unnecessary initialization
  284.                                  keeps gcc -Wall happy.  */
  285.   int len, i;
  286.   struct type *range;

  287.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  288.     {

  289.       /* check if all fields of the RECORD are consecutive sets.  */

  290.       len = TYPE_NFIELDS (type);
  291.       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
  292.         {
  293.           if (TYPE_FIELD_TYPE (type, i) == NULL)
  294.             return 0;
  295.           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
  296.             return 0;
  297.           if (TYPE_FIELD_NAME (type, i) != NULL
  298.               && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
  299.             return 0;
  300.           range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
  301.           if ((i > TYPE_N_BASECLASSES (type))
  302.               && previous_high + 1 != TYPE_LOW_BOUND (range))
  303.             return 0;
  304.           previous_high = TYPE_HIGH_BOUND (range);
  305.         }
  306.       return len>0;
  307.     }
  308.   return 0;
  309. }

  310. /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
  311.                             understands that CHARs might be signed.
  312.                             This should be integrated into gdbtypes.c
  313.                             inside get_discrete_bounds.  */

  314. static int
  315. m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
  316. {
  317.   CHECK_TYPEDEF (type);
  318.   switch (TYPE_CODE (type))
  319.     {
  320.     case TYPE_CODE_CHAR:
  321.       if (TYPE_LENGTH (type) < sizeof (LONGEST))
  322.         {
  323.           if (!TYPE_UNSIGNED (type))
  324.             {
  325.               *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
  326.               *highp = -*lowp - 1;
  327.               return 0;
  328.             }
  329.         }
  330.       /* fall through */
  331.     default:
  332.       return get_discrete_bounds (type, lowp, highp);
  333.     }
  334. }

  335. /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
  336.                             SET OF <oftype> of_type is assigned to the
  337.                             subtype.  */

  338. int
  339. m2_is_long_set_of_type (struct type *type, struct type **of_type)
  340. {
  341.   int len, i;
  342.   struct type *range;
  343.   struct type *target;
  344.   LONGEST l1, l2;
  345.   LONGEST h1, h2;

  346.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  347.     {
  348.       len = TYPE_NFIELDS (type);
  349.       i = TYPE_N_BASECLASSES (type);
  350.       if (len == 0)
  351.         return 0;
  352.       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
  353.       target = TYPE_TARGET_TYPE (range);

  354.       l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
  355.       h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
  356.       *of_type = target;
  357.       if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
  358.         return (l1 == l2 && h1 == h2);
  359.       error (_("long_set failed to find discrete bounds for its subtype"));
  360.       return 0;
  361.     }
  362.   error (_("expecting long_set"));
  363.   return 0;
  364. }

  365. static int
  366. m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
  367.              const struct type_print_options *flags)
  368. {
  369.   struct type *of_type;
  370.   int i;
  371.   int len = TYPE_NFIELDS (type);
  372.   LONGEST low;
  373.   LONGEST high;

  374.   if (m2_is_long_set (type))
  375.     {
  376.       if (TYPE_TAG_NAME (type) != NULL)
  377.         {
  378.           fputs_filtered (TYPE_TAG_NAME (type), stream);
  379.           if (show == 0)
  380.             return 1;
  381.         }
  382.       else if (TYPE_NAME (type) != NULL)
  383.         {
  384.           fputs_filtered (TYPE_NAME (type), stream);
  385.           if (show == 0)
  386.             return 1;
  387.         }

  388.       if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
  389.         fputs_filtered (" = ", stream);

  390.       if (get_long_set_bounds (type, &low, &high))
  391.         {
  392.           fprintf_filtered(stream, "SET OF ");
  393.           i = TYPE_N_BASECLASSES (type);
  394.           if (m2_is_long_set_of_type (type, &of_type))
  395.             m2_print_type (of_type, "", stream, show - 1, level, flags);
  396.           else
  397.             {
  398.               fprintf_filtered(stream, "[");
  399.               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
  400.                                stream, show - 1, level, 0);

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

  402.               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
  403.                                stream, show - 1, level, 1);
  404.               fprintf_filtered(stream, "]");
  405.             }
  406.         }
  407.       else
  408.         /* i18n: Do not translate the "SET OF" part!  */
  409.         fprintf_filtered(stream, _("SET OF <unknown>"));

  410.       return 1;
  411.     }
  412.   return 0;
  413. }

  414. /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
  415.                            as a Modula-2 unbounded ARRAY type.  */

  416. int
  417. m2_is_unbounded_array (struct type *type)
  418. {
  419.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  420.     {
  421.       /*
  422.        *  check if we have a structure with exactly two fields named
  423.        *  _m2_contents and _m2_high.  It also checks to see if the
  424.        *  type of _m2_contents is a pointer.  The TYPE_TARGET_TYPE
  425.        *  of the pointer determines the unbounded ARRAY OF type.
  426.        */
  427.       if (TYPE_NFIELDS (type) != 2)
  428.         return 0;
  429.       if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
  430.         return 0;
  431.       if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
  432.         return 0;
  433.       if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
  434.         return 0;
  435.       return 1;
  436.     }
  437.   return 0;
  438. }

  439. /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
  440.                         parameter type then display the type as an
  441.                         ARRAY OF type.  Returns TRUE if an unbounded
  442.                         array type was detected.  */

  443. static int
  444. m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
  445.                     int level, const struct type_print_options *flags)
  446. {
  447.   if (m2_is_unbounded_array (type))
  448.     {
  449.       if (show > 0)
  450.         {
  451.           fputs_filtered ("ARRAY OF ", stream);
  452.           m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
  453.                          "", stream, 0, level, flags);
  454.         }
  455.       return 1;
  456.     }
  457.   return 0;
  458. }

  459. void
  460. m2_record_fields (struct type *type, struct ui_file *stream, int show,
  461.                   int level, const struct type_print_options *flags)
  462. {
  463.   /* Print the tag if it exists.  */
  464.   if (TYPE_TAG_NAME (type) != NULL)
  465.     {
  466.       if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
  467.         {
  468.           fputs_filtered (TYPE_TAG_NAME (type), stream);
  469.           if (show > 0)
  470.             fprintf_filtered (stream, " = ");
  471.         }
  472.     }
  473.   wrap_here ("    ");
  474.   if (show < 0)
  475.     {
  476.       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  477.         fprintf_filtered (stream, "RECORD ... END ");
  478.       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
  479.         fprintf_filtered (stream, "CASE ... END ");
  480.     }
  481.   else if (show > 0)
  482.     {
  483.       int i;
  484.       int len = TYPE_NFIELDS (type);

  485.       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  486.         fprintf_filtered (stream, "RECORD\n");
  487.       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
  488.         /* i18n: Do not translate "CASE" and "OF".  */
  489.         fprintf_filtered (stream, _("CASE <variant> OF\n"));

  490.       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
  491.         {
  492.           QUIT;

  493.           print_spaces_filtered (level + 4, stream);
  494.           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  495.           fputs_filtered (" : ", stream);
  496.           m2_print_type (TYPE_FIELD_TYPE (type, i),
  497.                          "",
  498.                          stream, 0, level + 4, flags);
  499.           if (TYPE_FIELD_PACKED (type, i))
  500.             {
  501.               /* It is a bitfield.  This code does not attempt
  502.                  to look at the bitpos and reconstruct filler,
  503.                  unnamed fields.  This would lead to misleading
  504.                  results if the compiler does not put out fields
  505.                  for such things (I don't know what it does).  */
  506.               fprintf_filtered (stream, " : %d",
  507.                                 TYPE_FIELD_BITSIZE (type, i));
  508.             }
  509.           fprintf_filtered (stream, ";\n");
  510.         }

  511.       fprintfi_filtered (level, stream, "END ");
  512.     }
  513. }

  514. void
  515. m2_enum (struct type *type, struct ui_file *stream, int show, int level)
  516. {
  517.   LONGEST lastval;
  518.   int i, len;

  519.   if (show < 0)
  520.     {
  521.       /* If we just printed a tag name, no need to print anything else.  */
  522.       if (TYPE_TAG_NAME (type) == NULL)
  523.         fprintf_filtered (stream, "(...)");
  524.     }
  525.   else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
  526.     {
  527.       fprintf_filtered (stream, "(");
  528.       len = TYPE_NFIELDS (type);
  529.       lastval = 0;
  530.       for (i = 0; i < len; i++)
  531.         {
  532.           QUIT;
  533.           if (i > 0)
  534.             fprintf_filtered (stream, ", ");
  535.           wrap_here ("    ");
  536.           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  537.           if (lastval != TYPE_FIELD_ENUMVAL (type, i))
  538.             {
  539.               fprintf_filtered (stream, " = %s",
  540.                                 plongest (TYPE_FIELD_ENUMVAL (type, i)));
  541.               lastval = TYPE_FIELD_ENUMVAL (type, i);
  542.             }
  543.           lastval++;
  544.         }
  545.       fprintf_filtered (stream, ")");
  546.     }
  547. }