gdb/m2-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Modula 2 language support routines for GDB, the GNU debugger.

  2.    Copyright (C) 1992-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 "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "parser-defs.h"
  19. #include "language.h"
  20. #include "varobj.h"
  21. #include "m2-lang.h"
  22. #include "c-lang.h"
  23. #include "valprint.h"

  24. extern void _initialize_m2_language (void);
  25. static void m2_printchar (int, struct type *, struct ui_file *);
  26. static void m2_emit_char (int, struct type *, struct ui_file *, int);

  27. /* Print the character C on STREAM as part of the contents of a literal
  28.    string whose delimiter is QUOTER.  Note that that format for printing
  29.    characters and strings is language specific.
  30.    FIXME:  This is a copy of the same function from c-exp.y.  It should
  31.    be replaced with a true Modula version.  */

  32. static void
  33. m2_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
  34. {

  35.   c &= 0xFF;                        /* Avoid sign bit follies.  */

  36.   if (PRINT_LITERAL_FORM (c))
  37.     {
  38.       if (c == '\\' || c == quoter)
  39.         {
  40.           fputs_filtered ("\\", stream);
  41.         }
  42.       fprintf_filtered (stream, "%c", c);
  43.     }
  44.   else
  45.     {
  46.       switch (c)
  47.         {
  48.         case '\n':
  49.           fputs_filtered ("\\n", stream);
  50.           break;
  51.         case '\b':
  52.           fputs_filtered ("\\b", stream);
  53.           break;
  54.         case '\t':
  55.           fputs_filtered ("\\t", stream);
  56.           break;
  57.         case '\f':
  58.           fputs_filtered ("\\f", stream);
  59.           break;
  60.         case '\r':
  61.           fputs_filtered ("\\r", stream);
  62.           break;
  63.         case '\033':
  64.           fputs_filtered ("\\e", stream);
  65.           break;
  66.         case '\007':
  67.           fputs_filtered ("\\a", stream);
  68.           break;
  69.         default:
  70.           fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
  71.           break;
  72.         }
  73.     }
  74. }

  75. /* FIXME:  This is a copy of the same function from c-exp.y.  It should
  76.    be replaced with a true Modula version.  */

  77. static void
  78. m2_printchar (int c, struct type *type, struct ui_file *stream)
  79. {
  80.   fputs_filtered ("'", stream);
  81.   LA_EMIT_CHAR (c, type, stream, '\'');
  82.   fputs_filtered ("'", stream);
  83. }

  84. /* Print the character string STRING, printing at most LENGTH characters.
  85.    Printing stops early if the number hits print_max; repeat counts
  86.    are printed as appropriate.  Print ellipses at the end if we
  87.    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
  88.    FIXME:  This is a copy of the same function from c-exp.y.  It should
  89.    be replaced with a true Modula version.  */

  90. static void
  91. m2_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
  92.              unsigned int length, const char *encoding, int force_ellipses,
  93.              const struct value_print_options *options)
  94. {
  95.   unsigned int i;
  96.   unsigned int things_printed = 0;
  97.   int in_quotes = 0;
  98.   int need_comma = 0;

  99.   if (length == 0)
  100.     {
  101.       fputs_filtered ("\"\"", gdb_stdout);
  102.       return;
  103.     }

  104.   for (i = 0; i < length && things_printed < options->print_max; ++i)
  105.     {
  106.       /* Position of the character we are examining
  107.          to see whether it is repeated.  */
  108.       unsigned int rep1;
  109.       /* Number of repetitions we have detected so far.  */
  110.       unsigned int reps;

  111.       QUIT;

  112.       if (need_comma)
  113.         {
  114.           fputs_filtered (", ", stream);
  115.           need_comma = 0;
  116.         }

  117.       rep1 = i + 1;
  118.       reps = 1;
  119.       while (rep1 < length && string[rep1] == string[i])
  120.         {
  121.           ++rep1;
  122.           ++reps;
  123.         }

  124.       if (reps > options->repeat_count_threshold)
  125.         {
  126.           if (in_quotes)
  127.             {
  128.               fputs_filtered ("\", ", stream);
  129.               in_quotes = 0;
  130.             }
  131.           m2_printchar (string[i], type, stream);
  132.           fprintf_filtered (stream, " <repeats %u times>", reps);
  133.           i = rep1 - 1;
  134.           things_printed += options->repeat_count_threshold;
  135.           need_comma = 1;
  136.         }
  137.       else
  138.         {
  139.           if (!in_quotes)
  140.             {
  141.               fputs_filtered ("\"", stream);
  142.               in_quotes = 1;
  143.             }
  144.           LA_EMIT_CHAR (string[i], type, stream, '"');
  145.           ++things_printed;
  146.         }
  147.     }

  148.   /* Terminate the quotes if necessary.  */
  149.   if (in_quotes)
  150.     fputs_filtered ("\"", stream);

  151.   if (force_ellipses || i < length)
  152.     fputs_filtered ("...", stream);
  153. }

  154. static struct value *
  155. evaluate_subexp_modula2 (struct type *expect_type, struct expression *exp,
  156.                          int *pos, enum noside noside)
  157. {
  158.   enum exp_opcode op = exp->elts[*pos].opcode;
  159.   struct value *arg1;
  160.   struct value *arg2;
  161.   struct type *type;

  162.   switch (op)
  163.     {
  164.     case UNOP_HIGH:
  165.       (*pos)++;
  166.       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);

  167.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  168.         return arg1;
  169.       else
  170.         {
  171.           arg1 = coerce_ref (arg1);
  172.           type = check_typedef (value_type (arg1));

  173.           if (m2_is_unbounded_array (type))
  174.             {
  175.               struct value *temp = arg1;

  176.               type = TYPE_FIELD_TYPE (type, 1);
  177.               /* i18n: Do not translate the "_m2_high" part!  */
  178.               arg1 = value_struct_elt (&temp, NULL, "_m2_high", NULL,
  179.                                        _("unbounded structure "
  180.                                          "missing _m2_high field"));

  181.               if (value_type (arg1) != type)
  182.                 arg1 = value_cast (type, arg1);
  183.             }
  184.         }
  185.       return arg1;

  186.     case BINOP_SUBSCRIPT:
  187.       (*pos)++;
  188.       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  189.       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  190.       if (noside == EVAL_SKIP)
  191.         goto nosideret;
  192.       /* If the user attempts to subscript something that is not an
  193.          array or pointer type (like a plain int variable for example),
  194.          then report this as an error.  */

  195.       arg1 = coerce_ref (arg1);
  196.       type = check_typedef (value_type (arg1));

  197.       if (m2_is_unbounded_array (type))
  198.         {
  199.           struct value *temp = arg1;
  200.           type = TYPE_FIELD_TYPE (type, 0);
  201.           if (type == NULL || (TYPE_CODE (type) != TYPE_CODE_PTR))
  202.             {
  203.               warning (_("internal error: unbounded "
  204.                          "array structure is unknown"));
  205.               return evaluate_subexp_standard (expect_type, exp, pos, noside);
  206.             }
  207.           /* i18n: Do not translate the "_m2_contents" part!  */
  208.           arg1 = value_struct_elt (&temp, NULL, "_m2_contents", NULL,
  209.                                    _("unbounded structure "
  210.                                      "missing _m2_contents field"));

  211.           if (value_type (arg1) != type)
  212.             arg1 = value_cast (type, arg1);

  213.           check_typedef (value_type (arg1));
  214.           return value_ind (value_ptradd (arg1, value_as_long (arg2)));
  215.         }
  216.       else
  217.         if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
  218.           {
  219.             if (TYPE_NAME (type))
  220.               error (_("cannot subscript something of type `%s'"),
  221.                      TYPE_NAME (type));
  222.             else
  223.               error (_("cannot subscript requested type"));
  224.           }

  225.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  226.         return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
  227.       else
  228.         return value_subscript (arg1, value_as_long (arg2));

  229.     default:
  230.       return evaluate_subexp_standard (expect_type, exp, pos, noside);
  231.     }

  232. nosideret:
  233.   return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
  234. }


  235. /* Table of operators and their precedences for printing expressions.  */

  236. static const struct op_print m2_op_print_tab[] =
  237. {
  238.   {"+", BINOP_ADD, PREC_ADD, 0},
  239.   {"+", UNOP_PLUS, PREC_PREFIX, 0},
  240.   {"-", BINOP_SUB, PREC_ADD, 0},
  241.   {"-", UNOP_NEG, PREC_PREFIX, 0},
  242.   {"*", BINOP_MUL, PREC_MUL, 0},
  243.   {"/", BINOP_DIV, PREC_MUL, 0},
  244.   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
  245.   {"MOD", BINOP_REM, PREC_MUL, 0},
  246.   {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
  247.   {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
  248.   {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
  249.   {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
  250.   {"=", BINOP_EQUAL, PREC_EQUAL, 0},
  251.   {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  252.   {"<=", BINOP_LEQ, PREC_ORDER, 0},
  253.   {">=", BINOP_GEQ, PREC_ORDER, 0},
  254.   {">", BINOP_GTR, PREC_ORDER, 0},
  255.   {"<", BINOP_LESS, PREC_ORDER, 0},
  256.   {"^", UNOP_IND, PREC_PREFIX, 0},
  257.   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
  258.   {"CAP", UNOP_CAP, PREC_BUILTIN_FUNCTION, 0},
  259.   {"CHR", UNOP_CHR, PREC_BUILTIN_FUNCTION, 0},
  260.   {"ORD", UNOP_ORD, PREC_BUILTIN_FUNCTION, 0},
  261.   {"FLOAT", UNOP_FLOAT, PREC_BUILTIN_FUNCTION, 0},
  262.   {"HIGH", UNOP_HIGH, PREC_BUILTIN_FUNCTION, 0},
  263.   {"MAX", UNOP_MAX, PREC_BUILTIN_FUNCTION, 0},
  264.   {"MIN", UNOP_MIN, PREC_BUILTIN_FUNCTION, 0},
  265.   {"ODD", UNOP_ODD, PREC_BUILTIN_FUNCTION, 0},
  266.   {"TRUNC", UNOP_TRUNC, PREC_BUILTIN_FUNCTION, 0},
  267.   {NULL, 0, 0, 0}
  268. };

  269. /* The built-in types of Modula-2.  */

  270. enum m2_primitive_types {
  271.   m2_primitive_type_char,
  272.   m2_primitive_type_int,
  273.   m2_primitive_type_card,
  274.   m2_primitive_type_real,
  275.   m2_primitive_type_bool,
  276.   nr_m2_primitive_types
  277. };

  278. static void
  279. m2_language_arch_info (struct gdbarch *gdbarch,
  280.                        struct language_arch_info *lai)
  281. {
  282.   const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);

  283.   lai->string_char_type = builtin->builtin_char;
  284.   lai->primitive_type_vector
  285.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_m2_primitive_types + 1,
  286.                               struct type *);

  287.   lai->primitive_type_vector [m2_primitive_type_char]
  288.     = builtin->builtin_char;
  289.   lai->primitive_type_vector [m2_primitive_type_int]
  290.     = builtin->builtin_int;
  291.   lai->primitive_type_vector [m2_primitive_type_card]
  292.     = builtin->builtin_card;
  293.   lai->primitive_type_vector [m2_primitive_type_real]
  294.     = builtin->builtin_real;
  295.   lai->primitive_type_vector [m2_primitive_type_bool]
  296.     = builtin->builtin_bool;

  297.   lai->bool_type_symbol = "BOOLEAN";
  298.   lai->bool_type_default = builtin->builtin_bool;
  299. }

  300. const struct exp_descriptor exp_descriptor_modula2 =
  301. {
  302.   print_subexp_standard,
  303.   operator_length_standard,
  304.   operator_check_standard,
  305.   op_name_standard,
  306.   dump_subexp_body_standard,
  307.   evaluate_subexp_modula2
  308. };

  309. const struct language_defn m2_language_defn =
  310. {
  311.   "modula-2",
  312.   "Modula-2",
  313.   language_m2,
  314.   range_check_on,
  315.   case_sensitive_on,
  316.   array_row_major,
  317.   macro_expansion_no,
  318.   &exp_descriptor_modula2,
  319.   m2_parse,                        /* parser */
  320.   m2_error,                        /* parser error function */
  321.   null_post_parser,
  322.   m2_printchar,                        /* Print character constant */
  323.   m2_printstr,                        /* function to print string constant */
  324.   m2_emit_char,                        /* Function to print a single character */
  325.   m2_print_type,                /* Print a type using appropriate syntax */
  326.   m2_print_typedef,                /* Print a typedef using appropriate syntax */
  327.   m2_val_print,                        /* Print a value using appropriate syntax */
  328.   c_value_print,                /* Print a top-level value */
  329.   default_read_var_value,        /* la_read_var_value */
  330.   NULL,                                /* Language specific skip_trampoline */
  331.   NULL,                                /* name_of_this */
  332.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  333.   basic_lookup_transparent_type,/* lookup_transparent_type */
  334.   NULL,                                /* Language specific symbol demangler */
  335.   NULL,                                /* Language specific
  336.                                    class_name_from_physname */
  337.   m2_op_print_tab,                /* expression operators for printing */
  338.   0,                                /* arrays are first-class (not c-style) */
  339.   0,                                /* String lower bound */
  340.   default_word_break_characters,
  341.   default_make_symbol_completion_list,
  342.   m2_language_arch_info,
  343.   default_print_array_index,
  344.   default_pass_by_reference,
  345.   default_get_string,
  346.   NULL,                                /* la_get_symbol_name_cmp */
  347.   iterate_over_symbols,
  348.   &default_varobj_ops,
  349.   NULL,
  350.   NULL,
  351.   LANG_MAGIC
  352. };

  353. static void *
  354. build_m2_types (struct gdbarch *gdbarch)
  355. {
  356.   struct builtin_m2_type *builtin_m2_type
  357.     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type);

  358.   /* Modula-2 "pervasive" types.  NOTE:  these can be redefined!!! */
  359.   builtin_m2_type->builtin_int
  360.     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
  361.   builtin_m2_type->builtin_card
  362.     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
  363.   builtin_m2_type->builtin_real
  364.     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL", NULL);
  365.   builtin_m2_type->builtin_char
  366.     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR");
  367.   builtin_m2_type->builtin_bool
  368.     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");

  369.   return builtin_m2_type;
  370. }

  371. static struct gdbarch_data *m2_type_data;

  372. const struct builtin_m2_type *
  373. builtin_m2_type (struct gdbarch *gdbarch)
  374. {
  375.   return gdbarch_data (gdbarch, m2_type_data);
  376. }


  377. /* Initialization for Modula-2 */

  378. void
  379. _initialize_m2_language (void)
  380. {
  381.   m2_type_data = gdbarch_data_register_post_init (build_m2_types);

  382.   add_language (&m2_language_defn);
  383. }