GDB (xrefs)
/tmp/gdb-8.1/gdb/m2-lang.c
Go to the documentation of this file.
1 /* Modula 2 language support routines for GDB, the GNU debugger.
2 
3  Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "parser-defs.h"
25 #include "language.h"
26 #include "varobj.h"
27 #include "m2-lang.h"
28 #include "c-lang.h"
29 #include "valprint.h"
30 
31 static void m2_printchar (int, struct type *, struct ui_file *);
32 static void m2_emit_char (int, struct type *, struct ui_file *, int);
33 
34 /* Print the character C on STREAM as part of the contents of a literal
35  string whose delimiter is QUOTER. Note that that format for printing
36  characters and strings is language specific.
37  FIXME: This is a copy of the same function from c-exp.y. It should
38  be replaced with a true Modula version. */
39 
40 static void
41 m2_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
42 {
43 
44  c &= 0xFF; /* Avoid sign bit follies. */
45 
46  if (PRINT_LITERAL_FORM (c))
47  {
48  if (c == '\\' || c == quoter)
49  {
50  fputs_filtered ("\\", stream);
51  }
52  fprintf_filtered (stream, "%c", c);
53  }
54  else
55  {
56  switch (c)
57  {
58  case '\n':
59  fputs_filtered ("\\n", stream);
60  break;
61  case '\b':
62  fputs_filtered ("\\b", stream);
63  break;
64  case '\t':
65  fputs_filtered ("\\t", stream);
66  break;
67  case '\f':
68  fputs_filtered ("\\f", stream);
69  break;
70  case '\r':
71  fputs_filtered ("\\r", stream);
72  break;
73  case '\033':
74  fputs_filtered ("\\e", stream);
75  break;
76  case '\007':
77  fputs_filtered ("\\a", stream);
78  break;
79  default:
80  fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
81  break;
82  }
83  }
84 }
85 
86 /* FIXME: This is a copy of the same function from c-exp.y. It should
87  be replaced with a true Modula version. */
88 
89 static void
90 m2_printchar (int c, struct type *type, struct ui_file *stream)
91 {
92  fputs_filtered ("'", stream);
93  LA_EMIT_CHAR (c, type, stream, '\'');
94  fputs_filtered ("'", stream);
95 }
96 
97 /* Print the character string STRING, printing at most LENGTH characters.
98  Printing stops early if the number hits print_max; repeat counts
99  are printed as appropriate. Print ellipses at the end if we
100  had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
101  FIXME: This is a copy of the same function from c-exp.y. It should
102  be replaced with a true Modula version. */
103 
104 static void
105 m2_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
106  unsigned int length, const char *encoding, int force_ellipses,
107  const struct value_print_options *options)
108 {
109  unsigned int i;
110  unsigned int things_printed = 0;
111  int in_quotes = 0;
112  int need_comma = 0;
113 
114  if (length == 0)
115  {
116  fputs_filtered ("\"\"", gdb_stdout);
117  return;
118  }
119 
120  for (i = 0; i < length && things_printed < options->print_max; ++i)
121  {
122  /* Position of the character we are examining
123  to see whether it is repeated. */
124  unsigned int rep1;
125  /* Number of repetitions we have detected so far. */
126  unsigned int reps;
127 
128  QUIT;
129 
130  if (need_comma)
131  {
132  fputs_filtered (", ", stream);
133  need_comma = 0;
134  }
135 
136  rep1 = i + 1;
137  reps = 1;
138  while (rep1 < length && string[rep1] == string[i])
139  {
140  ++rep1;
141  ++reps;
142  }
143 
144  if (reps > options->repeat_count_threshold)
145  {
146  if (in_quotes)
147  {
148  fputs_filtered ("\", ", stream);
149  in_quotes = 0;
150  }
151  m2_printchar (string[i], type, stream);
152  fprintf_filtered (stream, " <repeats %u times>", reps);
153  i = rep1 - 1;
154  things_printed += options->repeat_count_threshold;
155  need_comma = 1;
156  }
157  else
158  {
159  if (!in_quotes)
160  {
161  fputs_filtered ("\"", stream);
162  in_quotes = 1;
163  }
164  LA_EMIT_CHAR (string[i], type, stream, '"');
165  ++things_printed;
166  }
167  }
168 
169  /* Terminate the quotes if necessary. */
170  if (in_quotes)
171  fputs_filtered ("\"", stream);
172 
173  if (force_ellipses || i < length)
174  fputs_filtered ("...", stream);
175 }
176 
177 static struct value *
178 evaluate_subexp_modula2 (struct type *expect_type, struct expression *exp,
179  int *pos, enum noside noside)
180 {
181  enum exp_opcode op = exp->elts[*pos].opcode;
182  struct value *arg1;
183  struct value *arg2;
184  struct type *type;
185 
186  switch (op)
187  {
188  case UNOP_HIGH:
189  (*pos)++;
190  arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
191 
193  return arg1;
194  else
195  {
196  arg1 = coerce_ref (arg1);
197  type = check_typedef (value_type (arg1));
198 
200  {
201  struct value *temp = arg1;
202 
203  type = TYPE_FIELD_TYPE (type, 1);
204  /* i18n: Do not translate the "_m2_high" part! */
205  arg1 = value_struct_elt (&temp, NULL, "_m2_high", NULL,
206  _("unbounded structure "
207  "missing _m2_high field"));
208 
209  if (value_type (arg1) != type)
210  arg1 = value_cast (type, arg1);
211  }
212  }
213  return arg1;
214 
215  case BINOP_SUBSCRIPT:
216  (*pos)++;
217  arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
218  arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
219  if (noside == EVAL_SKIP)
220  goto nosideret;
221  /* If the user attempts to subscript something that is not an
222  array or pointer type (like a plain int variable for example),
223  then report this as an error. */
224 
225  arg1 = coerce_ref (arg1);
226  type = check_typedef (value_type (arg1));
227 
229  {
230  struct value *temp = arg1;
231  type = TYPE_FIELD_TYPE (type, 0);
232  if (type == NULL || (TYPE_CODE (type) != TYPE_CODE_PTR))
233  {
234  warning (_("internal error: unbounded "
235  "array structure is unknown"));
236  return evaluate_subexp_standard (expect_type, exp, pos, noside);
237  }
238  /* i18n: Do not translate the "_m2_contents" part! */
239  arg1 = value_struct_elt (&temp, NULL, "_m2_contents", NULL,
240  _("unbounded structure "
241  "missing _m2_contents field"));
242 
243  if (value_type (arg1) != type)
244  arg1 = value_cast (type, arg1);
245 
246  check_typedef (value_type (arg1));
247  return value_ind (value_ptradd (arg1, value_as_long (arg2)));
248  }
249  else
250  if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
251  {
252  if (TYPE_NAME (type))
253  error (_("cannot subscript something of type `%s'"),
254  TYPE_NAME (type));
255  else
256  error (_("cannot subscript requested type"));
257  }
258 
260  return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
261  else
262  return value_subscript (arg1, value_as_long (arg2));
263 
264  default:
265  return evaluate_subexp_standard (expect_type, exp, pos, noside);
266  }
267 
268  nosideret:
270 }
271 
272 
273 /* Table of operators and their precedences for printing expressions. */
274 
275 static const struct op_print m2_op_print_tab[] =
276 {
277  {"+", BINOP_ADD, PREC_ADD, 0},
278  {"+", UNOP_PLUS, PREC_PREFIX, 0},
279  {"-", BINOP_SUB, PREC_ADD, 0},
280  {"-", UNOP_NEG, PREC_PREFIX, 0},
281  {"*", BINOP_MUL, PREC_MUL, 0},
282  {"/", BINOP_DIV, PREC_MUL, 0},
283  {"DIV", BINOP_INTDIV, PREC_MUL, 0},
284  {"MOD", BINOP_REM, PREC_MUL, 0},
285  {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
286  {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
287  {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
288  {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
289  {"=", BINOP_EQUAL, PREC_EQUAL, 0},
290  {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
291  {"<=", BINOP_LEQ, PREC_ORDER, 0},
292  {">=", BINOP_GEQ, PREC_ORDER, 0},
293  {">", BINOP_GTR, PREC_ORDER, 0},
294  {"<", BINOP_LESS, PREC_ORDER, 0},
295  {"^", UNOP_IND, PREC_PREFIX, 0},
296  {"@", BINOP_REPEAT, PREC_REPEAT, 0},
297  {"CAP", UNOP_CAP, PREC_BUILTIN_FUNCTION, 0},
298  {"CHR", UNOP_CHR, PREC_BUILTIN_FUNCTION, 0},
299  {"ORD", UNOP_ORD, PREC_BUILTIN_FUNCTION, 0},
300  {"FLOAT", UNOP_FLOAT, PREC_BUILTIN_FUNCTION, 0},
301  {"HIGH", UNOP_HIGH, PREC_BUILTIN_FUNCTION, 0},
302  {"MAX", UNOP_MAX, PREC_BUILTIN_FUNCTION, 0},
303  {"MIN", UNOP_MIN, PREC_BUILTIN_FUNCTION, 0},
304  {"ODD", UNOP_ODD, PREC_BUILTIN_FUNCTION, 0},
305  {"TRUNC", UNOP_TRUNC, PREC_BUILTIN_FUNCTION, 0},
306  {NULL, OP_NULL, PREC_BUILTIN_FUNCTION, 0}
307 };
308 
309 /* The built-in types of Modula-2. */
310 
318 };
319 
320 static void
322  struct language_arch_info *lai)
323 {
324  const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);
325 
326  lai->string_char_type = builtin->builtin_char;
329  struct type *);
330 
332  = builtin->builtin_char;
334  = builtin->builtin_int;
336  = builtin->builtin_card;
338  = builtin->builtin_real;
340  = builtin->builtin_bool;
341 
342  lai->bool_type_symbol = "BOOLEAN";
343  lai->bool_type_default = builtin->builtin_bool;
344 }
345 
347 {
354 };
355 
356 extern const struct language_defn m2_language_defn =
357 {
358  "modula-2",
359  "Modula-2",
360  language_m2,
365  NULL,
367  m2_parse, /* parser */
368  m2_yyerror, /* parser error function */
370  m2_printchar, /* Print character constant */
371  m2_printstr, /* function to print string constant */
372  m2_emit_char, /* Function to print a single character */
373  m2_print_type, /* Print a type using appropriate syntax */
374  m2_print_typedef, /* Print a typedef using appropriate syntax */
375  m2_val_print, /* Print a value using appropriate syntax */
376  c_value_print, /* Print a top-level value */
377  default_read_var_value, /* la_read_var_value */
378  NULL, /* Language specific skip_trampoline */
379  NULL, /* name_of_this */
380  basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
381  basic_lookup_transparent_type,/* lookup_transparent_type */
382  NULL, /* Language specific symbol demangler */
383  NULL,
384  NULL, /* Language specific
385  class_name_from_physname */
386  m2_op_print_tab, /* expression operators for printing */
387  0, /* arrays are first-class (not c-style) */
388  0, /* String lower bound */
396  NULL, /* la_get_symbol_name_matcher */
400  NULL,
401  NULL,
402  LANG_MAGIC
403 };
404 
405 static void *
407 {
410 
411  /* Modula-2 "pervasive" types. NOTE: these can be redefined!!! */
413  = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
415  = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
422  = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");
423 
424  return builtin_m2_type;
425 }
426 
427 static struct gdbarch_data *m2_type_data;
428 
429 const struct builtin_m2_type *
431 {
432  return (const struct builtin_m2_type *) gdbarch_data (gdbarch, m2_type_data);
433 }
434 
435 
436 /* Initialization for Modula-2 */
437 
438 void
440 {
442 }
int m2_parse(struct parser_state *par_state)
Definition: m2-exp.c:2842
struct value * value_zero(struct type *type, enum lval_type lv)
Definition: valops.c:847
union exp_element elts[1]
Definition: expression.h:84
unsigned int repeat_count_threshold
Definition: valprint.h:57
static const struct op_print m2_op_print_tab[]
Definition: m2-lang.c:275
void m2_print_type(struct type *, const char *, struct ui_file *, int, int, const struct type_print_options *)
Definition: m2-typeprint.c:72
void m2_print_typedef(struct type *, struct symbol *, struct ui_file *)
Definition: m2-typeprint.c:160
enum exp_opcode opcode
Definition: expression.h:64
struct value * value_subscript(struct value *array, LONGEST index)
Definition: valarith.c:142
const struct builtin_m2_type * builtin_m2_type(struct gdbarch *gdbarch)
Definition: m2-lang.c:430
noside
Definition: expression.h:123
m2_primitive_types
Definition: m2-lang.c:311
void _initialize_m2_language(void)
Definition: m2-lang.c:439
unsigned int default_search_name_hash(const char *string0)
Definition: dictionary.c:769
#define GDBARCH_OBSTACK_CALLOC(GDBARCH, NR, TYPE)
Definition: gdbarch.h:1708
LONGEST value_as_long(struct value *val)
Definition: value.c:2749
int gdbarch_int_bit(struct gdbarch *gdbarch)
Definition: gdbarch.c:1579
const char * default_word_break_characters(void)
Definition: language.c:679
void warning(const char *fmt,...)
Definition: errors.c:26
static void * build_m2_types(struct gdbarch *gdbarch)
Definition: m2-lang.c:406
struct value * evaluate_subexp_with_coercion(struct expression *exp, int *pos, enum noside noside)
Definition: eval.c:3048
#define TYPE_NAME(thistype)
Definition: gdbtypes.h:1224
static void m2_printstr(struct ui_file *stream, struct type *type, const gdb_byte *string, unsigned int length, const char *encoding, int force_ellipses, const struct value_print_options *options)
Definition: m2-lang.c:105
void * gdbarch_data(struct gdbarch *gdbarch, struct gdbarch_data *data)
Definition: gdbarch.c:5169
struct type * arch_character_type(struct gdbarch *gdbarch, int bit, int unsigned_p, const char *name)
Definition: gdbtypes.c:4979
struct type * builtin_char
Definition: m2-lang.h:49
int operator_check_standard(struct expression *exp, int pos, int(*objfile_func)(struct objfile *objfile, void *data), void *data)
Definition: parse.c:1723
struct value * default_read_var_value(struct symbol *var, const struct block *var_block, struct frame_info *frame)
Definition: findvar.c:587
struct value * coerce_ref(struct value *arg)
Definition: value.c:3755
static void m2_emit_char(int, struct type *, struct ui_file *, int)
Definition: m2-lang.c:41
struct value * value_ind(struct value *arg1)
Definition: valops.c:1542
#define _(String)
Definition: gdb_locale.h:35
struct type * string_char_type
Definition: language.h:122
struct type * arch_boolean_type(struct gdbarch *gdbarch, int bit, int unsigned_p, const char *name)
Definition: gdbtypes.c:4996
#define TYPE_FIELD_TYPE(thistype, n)
Definition: gdbtypes.h:1371
#define VALUE_LVAL(val)
Definition: value.h:414
void m2_yyerror(const char *)
#define GDBARCH_OBSTACK_ZALLOC(GDBARCH, TYPE)
Definition: gdbarch.h:1709
struct type * arch_integer_type(struct gdbarch *gdbarch, int bit, int unsigned_p, const char *name)
Definition: gdbtypes.c:4962
struct value * value_ptradd(struct value *arg1, LONGEST arg2)
Definition: valarith.c:80
static struct gdbarch_data * m2_type_data
Definition: m2-lang.c:427
struct value * value_struct_elt(struct value **argp, struct value **args, const char *name, int *static_memfuncp, const char *err)
Definition: valops.c:2133
#define LA_EMIT_CHAR(ch, type, stream, quoter)
Definition: language.h:532
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
void m2_val_print(struct type *, int, CORE_ADDR, struct ui_file *, int, struct value *, const struct value_print_options *)
Definition: m2-valprint.c:311
struct block_symbol basic_lookup_symbol_nonlocal(const struct language_defn *langdef, const char *name, const struct block *block, const domain_enum domain)
Definition: symtab.c:2417
struct type * bool_type_default
Definition: language.h:127
#define PRINT_LITERAL_FORM(c)
Definition: language.h:549
struct type * builtin_int
Definition: m2-lang.h:50
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
struct type * arch_float_type(struct gdbarch *gdbarch, int bit, const char *name, const struct floatformat **floatformats)
Definition: gdbtypes.c:5014
const struct language_defn m2_language_defn
static void m2_language_arch_info(struct gdbarch *gdbarch, struct language_arch_info *lai)
Definition: m2-lang.c:321
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
const char * bool_type_symbol
Definition: language.h:125
int default_pass_by_reference(struct type *type)
Definition: language.c:669
struct type * basic_lookup_transparent_type(const char *name)
Definition: symtab.c:2792
struct type * builtin_card
Definition: m2-lang.h:51
#define TARGET_CHAR_BIT
Definition: host-defs.h:29
Definition: gdbtypes.h:749
static const char * type
Definition: language.c:113
int dump_subexp_body_standard(struct expression *exp, struct ui_file *stream, int elt)
Definition: expprint.c:795
void null_post_parser(expression_up *exp, int void_context_p)
Definition: parse.c:1319
struct value * value_from_longest(struct type *type, LONGEST num)
Definition: value.c:3534
struct value * value_cast(struct type *type, struct value *arg2)
Definition: valops.c:351
void default_print_array_index(struct value *index_value, struct ui_file *stream, const struct value_print_options *options)
Definition: language.c:687
int m2_is_unbounded_array(struct type *type)
Definition: m2-typeprint.c:490
const struct exp_descriptor exp_descriptor_modula2
Definition: m2-lang.c:346
Definition: value.c:169
void default_collect_symbol_completion_matches(completion_tracker &tracker, complete_symbol_mode mode, symbol_name_match_type name_match_type, const char *text, const char *word, enum type_code code)
Definition: symtab.c:5190
bfd_byte gdb_byte
Definition: common-types.h:38
gdb::unique_xmalloc_ptr< char > c_watch_location_expression(struct type *type, CORE_ADDR addr)
Definition: c-lang.c:712
static void m2_printchar(int, struct type *, struct ui_file *)
Definition: m2-lang.c:90
static struct value * evaluate_subexp_modula2(struct type *expect_type, struct expression *exp, int *pos, enum noside noside)
Definition: m2-lang.c:178
#define TYPE_TARGET_TYPE(thistype)
Definition: gdbtypes.h:1226
void print_subexp_standard(struct expression *exp, int *pos, struct ui_file *stream, enum precedence prec)
Definition: expprint.c:58
void default_get_string(struct value *value, gdb_byte **buffer, int *length, struct type **char_type, const char **charset)
Definition: language.c:696
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
#define default_varobj_ops
Definition: varobj.h:236
int gdbarch_float_bit(struct gdbarch *gdbarch)
Definition: gdbarch.c:1680
exp_opcode
Definition: expression.h:42
struct value * evaluate_subexp_standard(struct type *expect_type, struct expression *exp, int *pos, enum noside noside)
Definition: eval.c:1240
void operator_length_standard(const struct expression *expr, int endpos, int *oplenp, int *argsp)
Definition: parse.c:837
const char * op_name_standard(enum exp_opcode opcode)
Definition: expprint.c:695
struct gdbarch * gdbarch
Definition: expression.h:82
struct type * value_type(const struct value *value)
Definition: value.c:1095
unsigned int print_max
Definition: valprint.h:53
const struct floatformat ** gdbarch_float_format(struct gdbarch *gdbarch)
Definition: gdbarch.c:1697
void iterate_over_symbols(const struct block *block, const lookup_name_info &name, const domain_enum domain, gdb::function_view< symbol_found_callback_ftype > callback)
Definition: symtab.c:2849
#define LANG_MAGIC
Definition: language.h:438
#define QUIT
Definition: defs.h:179
struct type ** primitive_type_vector
Definition: language.h:115
void c_value_print(struct value *, struct ui_file *, const struct value_print_options *)
Definition: c-valprint.c:571
struct type * builtin_bool
Definition: m2-lang.h:53
void error(const char *fmt,...)
Definition: errors.c:38
struct gdbarch_data * gdbarch_data_register_post_init(gdbarch_data_post_init_ftype *post_init)
Definition: gdbarch.c:5136
struct type * builtin_real
Definition: m2-lang.h:52
#define gdb_stdout
Definition: utils.h:340
struct type * builtin_int
Definition: gdbtypes.h:1503