GDB (API)
/home/stan/gdb/src/gdb/m2-lang.c
Go to the documentation of this file.
00001 /* Modula 2 language support routines for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1992-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "expression.h"
00024 #include "parser-defs.h"
00025 #include "language.h"
00026 #include "m2-lang.h"
00027 #include "c-lang.h"
00028 #include "valprint.h"
00029 
00030 extern void _initialize_m2_language (void);
00031 static void m2_printchar (int, struct type *, struct ui_file *);
00032 static void m2_emit_char (int, struct type *, struct ui_file *, int);
00033 
00034 /* Print the character C on STREAM as part of the contents of a literal
00035    string whose delimiter is QUOTER.  Note that that format for printing
00036    characters and strings is language specific.
00037    FIXME:  This is a copy of the same function from c-exp.y.  It should
00038    be replaced with a true Modula version.  */
00039 
00040 static void
00041 m2_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
00042 {
00043 
00044   c &= 0xFF;                    /* Avoid sign bit follies.  */
00045 
00046   if (PRINT_LITERAL_FORM (c))
00047     {
00048       if (c == '\\' || c == quoter)
00049         {
00050           fputs_filtered ("\\", stream);
00051         }
00052       fprintf_filtered (stream, "%c", c);
00053     }
00054   else
00055     {
00056       switch (c)
00057         {
00058         case '\n':
00059           fputs_filtered ("\\n", stream);
00060           break;
00061         case '\b':
00062           fputs_filtered ("\\b", stream);
00063           break;
00064         case '\t':
00065           fputs_filtered ("\\t", stream);
00066           break;
00067         case '\f':
00068           fputs_filtered ("\\f", stream);
00069           break;
00070         case '\r':
00071           fputs_filtered ("\\r", stream);
00072           break;
00073         case '\033':
00074           fputs_filtered ("\\e", stream);
00075           break;
00076         case '\007':
00077           fputs_filtered ("\\a", stream);
00078           break;
00079         default:
00080           fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
00081           break;
00082         }
00083     }
00084 }
00085 
00086 /* FIXME:  This is a copy of the same function from c-exp.y.  It should
00087    be replaced with a true Modula version.  */
00088 
00089 static void
00090 m2_printchar (int c, struct type *type, struct ui_file *stream)
00091 {
00092   fputs_filtered ("'", stream);
00093   LA_EMIT_CHAR (c, type, stream, '\'');
00094   fputs_filtered ("'", stream);
00095 }
00096 
00097 /* Print the character string STRING, printing at most LENGTH characters.
00098    Printing stops early if the number hits print_max; repeat counts
00099    are printed as appropriate.  Print ellipses at the end if we
00100    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
00101    FIXME:  This is a copy of the same function from c-exp.y.  It should
00102    be replaced with a true Modula version.  */
00103 
00104 static void
00105 m2_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
00106              unsigned int length, const char *encoding, int force_ellipses,
00107              const struct value_print_options *options)
00108 {
00109   unsigned int i;
00110   unsigned int things_printed = 0;
00111   int in_quotes = 0;
00112   int need_comma = 0;
00113 
00114   if (length == 0)
00115     {
00116       fputs_filtered ("\"\"", gdb_stdout);
00117       return;
00118     }
00119 
00120   for (i = 0; i < length && things_printed < options->print_max; ++i)
00121     {
00122       /* Position of the character we are examining
00123          to see whether it is repeated.  */
00124       unsigned int rep1;
00125       /* Number of repetitions we have detected so far.  */
00126       unsigned int reps;
00127 
00128       QUIT;
00129 
00130       if (need_comma)
00131         {
00132           fputs_filtered (", ", stream);
00133           need_comma = 0;
00134         }
00135 
00136       rep1 = i + 1;
00137       reps = 1;
00138       while (rep1 < length && string[rep1] == string[i])
00139         {
00140           ++rep1;
00141           ++reps;
00142         }
00143 
00144       if (reps > options->repeat_count_threshold)
00145         {
00146           if (in_quotes)
00147             {
00148               fputs_filtered ("\", ", stream);
00149               in_quotes = 0;
00150             }
00151           m2_printchar (string[i], type, stream);
00152           fprintf_filtered (stream, " <repeats %u times>", reps);
00153           i = rep1 - 1;
00154           things_printed += options->repeat_count_threshold;
00155           need_comma = 1;
00156         }
00157       else
00158         {
00159           if (!in_quotes)
00160             {
00161               fputs_filtered ("\"", stream);
00162               in_quotes = 1;
00163             }
00164           LA_EMIT_CHAR (string[i], type, stream, '"');
00165           ++things_printed;
00166         }
00167     }
00168 
00169   /* Terminate the quotes if necessary.  */
00170   if (in_quotes)
00171     fputs_filtered ("\"", stream);
00172 
00173   if (force_ellipses || i < length)
00174     fputs_filtered ("...", stream);
00175 }
00176 
00177 static struct value *
00178 evaluate_subexp_modula2 (struct type *expect_type, struct expression *exp,
00179                          int *pos, enum noside noside)
00180 {
00181   enum exp_opcode op = exp->elts[*pos].opcode;
00182   struct value *arg1;
00183   struct value *arg2;
00184   struct type *type;
00185 
00186   switch (op)
00187     {
00188     case UNOP_HIGH:
00189       (*pos)++;
00190       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
00191 
00192       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
00193         return arg1;
00194       else
00195         {
00196           arg1 = coerce_ref (arg1);
00197           type = check_typedef (value_type (arg1));
00198 
00199           if (m2_is_unbounded_array (type))
00200             {
00201               struct value *temp = arg1;
00202 
00203               type = TYPE_FIELD_TYPE (type, 1);
00204               /* i18n: Do not translate the "_m2_high" part!  */
00205               arg1 = value_struct_elt (&temp, NULL, "_m2_high", NULL,
00206                                        _("unbounded structure "
00207                                          "missing _m2_high field"));
00208           
00209               if (value_type (arg1) != type)
00210                 arg1 = value_cast (type, arg1);
00211             }
00212         }
00213       return arg1;
00214 
00215     case BINOP_SUBSCRIPT:
00216       (*pos)++;
00217       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
00218       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
00219       if (noside == EVAL_SKIP)
00220         goto nosideret;
00221       /* If the user attempts to subscript something that is not an
00222          array or pointer type (like a plain int variable for example),
00223          then report this as an error.  */
00224 
00225       arg1 = coerce_ref (arg1);
00226       type = check_typedef (value_type (arg1));
00227 
00228       if (m2_is_unbounded_array (type))
00229         {
00230           struct value *temp = arg1;
00231           type = TYPE_FIELD_TYPE (type, 0);
00232           if (type == NULL || (TYPE_CODE (type) != TYPE_CODE_PTR))
00233             {
00234               warning (_("internal error: unbounded "
00235                          "array structure is unknown"));
00236               return evaluate_subexp_standard (expect_type, exp, pos, noside);
00237             }
00238           /* i18n: Do not translate the "_m2_contents" part!  */
00239           arg1 = value_struct_elt (&temp, NULL, "_m2_contents", NULL,
00240                                    _("unbounded structure "
00241                                      "missing _m2_contents field"));
00242           
00243           if (value_type (arg1) != type)
00244             arg1 = value_cast (type, arg1);
00245 
00246           check_typedef (value_type (arg1));
00247           return value_ind (value_ptradd (arg1, value_as_long (arg2)));
00248         }
00249       else
00250         if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
00251           {
00252             if (TYPE_NAME (type))
00253               error (_("cannot subscript something of type `%s'"),
00254                      TYPE_NAME (type));
00255             else
00256               error (_("cannot subscript requested type"));
00257           }
00258 
00259       if (noside == EVAL_AVOID_SIDE_EFFECTS)
00260         return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
00261       else
00262         return value_subscript (arg1, value_as_long (arg2));
00263 
00264     default:
00265       return evaluate_subexp_standard (expect_type, exp, pos, noside);
00266     }
00267 
00268  nosideret:
00269   return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
00270 }
00271 
00272 
00273 /* Table of operators and their precedences for printing expressions.  */
00274 
00275 static const struct op_print m2_op_print_tab[] =
00276 {
00277   {"+", BINOP_ADD, PREC_ADD, 0},
00278   {"+", UNOP_PLUS, PREC_PREFIX, 0},
00279   {"-", BINOP_SUB, PREC_ADD, 0},
00280   {"-", UNOP_NEG, PREC_PREFIX, 0},
00281   {"*", BINOP_MUL, PREC_MUL, 0},
00282   {"/", BINOP_DIV, PREC_MUL, 0},
00283   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
00284   {"MOD", BINOP_REM, PREC_MUL, 0},
00285   {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
00286   {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
00287   {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
00288   {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
00289   {"=", BINOP_EQUAL, PREC_EQUAL, 0},
00290   {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
00291   {"<=", BINOP_LEQ, PREC_ORDER, 0},
00292   {">=", BINOP_GEQ, PREC_ORDER, 0},
00293   {">", BINOP_GTR, PREC_ORDER, 0},
00294   {"<", BINOP_LESS, PREC_ORDER, 0},
00295   {"^", UNOP_IND, PREC_PREFIX, 0},
00296   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
00297   {"CAP", UNOP_CAP, PREC_BUILTIN_FUNCTION, 0},
00298   {"CHR", UNOP_CHR, PREC_BUILTIN_FUNCTION, 0},
00299   {"ORD", UNOP_ORD, PREC_BUILTIN_FUNCTION, 0},
00300   {"FLOAT", UNOP_FLOAT, PREC_BUILTIN_FUNCTION, 0},
00301   {"HIGH", UNOP_HIGH, PREC_BUILTIN_FUNCTION, 0},
00302   {"MAX", UNOP_MAX, PREC_BUILTIN_FUNCTION, 0},
00303   {"MIN", UNOP_MIN, PREC_BUILTIN_FUNCTION, 0},
00304   {"ODD", UNOP_ODD, PREC_BUILTIN_FUNCTION, 0},
00305   {"TRUNC", UNOP_TRUNC, PREC_BUILTIN_FUNCTION, 0},
00306   {NULL, 0, 0, 0}
00307 };
00308 
00309 /* The built-in types of Modula-2.  */
00310 
00311 enum m2_primitive_types {
00312   m2_primitive_type_char,
00313   m2_primitive_type_int,
00314   m2_primitive_type_card,
00315   m2_primitive_type_real,
00316   m2_primitive_type_bool,
00317   nr_m2_primitive_types
00318 };
00319 
00320 static void
00321 m2_language_arch_info (struct gdbarch *gdbarch,
00322                        struct language_arch_info *lai)
00323 {
00324   const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);
00325 
00326   lai->string_char_type = builtin->builtin_char;
00327   lai->primitive_type_vector
00328     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_m2_primitive_types + 1,
00329                               struct type *);
00330 
00331   lai->primitive_type_vector [m2_primitive_type_char]
00332     = builtin->builtin_char;
00333   lai->primitive_type_vector [m2_primitive_type_int]
00334     = builtin->builtin_int;
00335   lai->primitive_type_vector [m2_primitive_type_card]
00336     = builtin->builtin_card;
00337   lai->primitive_type_vector [m2_primitive_type_real]
00338     = builtin->builtin_real;
00339   lai->primitive_type_vector [m2_primitive_type_bool]
00340     = builtin->builtin_bool;
00341 
00342   lai->bool_type_symbol = "BOOLEAN";
00343   lai->bool_type_default = builtin->builtin_bool;
00344 }
00345 
00346 const struct exp_descriptor exp_descriptor_modula2 = 
00347 {
00348   print_subexp_standard,
00349   operator_length_standard,
00350   operator_check_standard,
00351   op_name_standard,
00352   dump_subexp_body_standard,
00353   evaluate_subexp_modula2
00354 };
00355 
00356 const struct language_defn m2_language_defn =
00357 {
00358   "modula-2",
00359   language_m2,
00360   range_check_on,
00361   case_sensitive_on,
00362   array_row_major,
00363   macro_expansion_no,
00364   &exp_descriptor_modula2,
00365   m2_parse,                     /* parser */
00366   m2_error,                     /* parser error function */
00367   null_post_parser,
00368   m2_printchar,                 /* Print character constant */
00369   m2_printstr,                  /* function to print string constant */
00370   m2_emit_char,                 /* Function to print a single character */
00371   m2_print_type,                /* Print a type using appropriate syntax */
00372   m2_print_typedef,             /* Print a typedef using appropriate syntax */
00373   m2_val_print,                 /* Print a value using appropriate syntax */
00374   c_value_print,                /* Print a top-level value */
00375   default_read_var_value,       /* la_read_var_value */
00376   NULL,                         /* Language specific skip_trampoline */
00377   NULL,                         /* name_of_this */
00378   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
00379   basic_lookup_transparent_type,/* lookup_transparent_type */
00380   NULL,                         /* Language specific symbol demangler */
00381   NULL,                         /* Language specific
00382                                    class_name_from_physname */
00383   m2_op_print_tab,              /* expression operators for printing */
00384   0,                            /* arrays are first-class (not c-style) */
00385   0,                            /* String lower bound */
00386   default_word_break_characters,
00387   default_make_symbol_completion_list,
00388   m2_language_arch_info,
00389   default_print_array_index,
00390   default_pass_by_reference,
00391   default_get_string,
00392   NULL,                         /* la_get_symbol_name_cmp */
00393   iterate_over_symbols,
00394   LANG_MAGIC
00395 };
00396 
00397 static void *
00398 build_m2_types (struct gdbarch *gdbarch)
00399 {
00400   struct builtin_m2_type *builtin_m2_type
00401     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type);
00402 
00403   /* Modula-2 "pervasive" types.  NOTE:  these can be redefined!!! */
00404   builtin_m2_type->builtin_int
00405     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
00406   builtin_m2_type->builtin_card
00407     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
00408   builtin_m2_type->builtin_real
00409     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL", NULL);
00410   builtin_m2_type->builtin_char
00411     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR");
00412   builtin_m2_type->builtin_bool
00413     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");
00414 
00415   return builtin_m2_type;
00416 }
00417 
00418 static struct gdbarch_data *m2_type_data;
00419 
00420 const struct builtin_m2_type *
00421 builtin_m2_type (struct gdbarch *gdbarch)
00422 {
00423   return gdbarch_data (gdbarch, m2_type_data);
00424 }
00425 
00426 
00427 /* Initialization for Modula-2 */
00428 
00429 void
00430 _initialize_m2_language (void)
00431 {
00432   m2_type_data = gdbarch_data_register_post_init (build_m2_types);
00433 
00434   add_language (&m2_language_defn);
00435 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines