1 /* Modula 2 language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "expression.h"
24 #include "parser-defs.h"
31 static void m2_printchar (int, struct type *, struct ui_file *);
32 static void m2_emit_char (int, struct type *, struct ui_file *, int);
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. */
41 m2_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
44 c &= 0xFF; /* Avoid sign bit follies. */
46 if (PRINT_LITERAL_FORM (c))
48 if (c == '\\' || c == quoter)
50 fputs_filtered ("\\", stream);
52 fprintf_filtered (stream, "%c", c);
59 fputs_filtered ("\\n", stream);
62 fputs_filtered ("\\b", stream);
65 fputs_filtered ("\\t", stream);
68 fputs_filtered ("\\f", stream);
71 fputs_filtered ("\\r", stream);
74 fputs_filtered ("\\e", stream);
77 fputs_filtered ("\\a", stream);
80 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
86 /* FIXME: This is a copy of the same function from c-exp.y. It should
87 be replaced with a true Modula version. */
90 m2_printchar (int c, struct type *type, struct ui_file *stream)
92 fputs_filtered ("'", stream);
93 LA_EMIT_CHAR (c, type, stream, '\'');
94 fputs_filtered ("'", stream);
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. */
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)
110 unsigned int things_printed = 0;
116 fputs_filtered ("\"\"", gdb_stdout);
120 for (i = 0; i < length && things_printed < options->print_max; ++i)
122 /* Position of the character we are examining
123 to see whether it is repeated. */
125 /* Number of repetitions we have detected so far. */
132 fputs_filtered (", ", stream);
138 while (rep1 < length && string[rep1] == string[i])
144 if (reps > options->repeat_count_threshold)
148 fputs_filtered ("\", ", stream);
151 m2_printchar (string[i], type, stream);
152 fprintf_filtered (stream, " <repeats %u times>", reps);
154 things_printed += options->repeat_count_threshold;
161 fputs_filtered ("\"", stream);
164 LA_EMIT_CHAR (string[i], type, stream, '"');
169 /* Terminate the quotes if necessary. */
171 fputs_filtered ("\"", stream);
173 if (force_ellipses || i < length)
174 fputs_filtered ("...", stream);
177 static struct value *
178 evaluate_subexp_modula2 (struct type *expect_type, struct expression *exp,
179 int *pos, enum noside noside)
181 enum exp_opcode op = exp->elts[*pos].opcode;
190 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
192 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
196 arg1 = coerce_ref (arg1);
197 type = check_typedef (value_type (arg1));
199 if (m2_is_unbounded_array (type))
201 struct value *temp = arg1;
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"));
209 if (value_type (arg1) != type)
210 arg1 = value_cast (type, arg1);
215 case BINOP_SUBSCRIPT:
217 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
218 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
219 if (noside == EVAL_SKIP)
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. */
225 arg1 = coerce_ref (arg1);
226 type = check_typedef (value_type (arg1));
228 if (m2_is_unbounded_array (type))
230 struct value *temp = arg1;
231 type = TYPE_FIELD_TYPE (type, 0);
232 if (type == NULL || (TYPE_CODE (type) != TYPE_CODE_PTR))
234 warning (_("internal error: unbounded "
235 "array structure is unknown"));
236 return evaluate_subexp_standard (expect_type, exp, pos, noside);
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"));
243 if (value_type (arg1) != type)
244 arg1 = value_cast (type, arg1);
246 check_typedef (value_type (arg1));
247 return value_ind (value_ptradd (arg1, value_as_long (arg2)));
250 if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
252 if (TYPE_NAME (type))
253 error (_("cannot subscript something of type `%s'"),
256 error (_("cannot subscript requested type"));
259 if (noside == EVAL_AVOID_SIDE_EFFECTS)
260 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
262 return value_subscript (arg1, value_as_long (arg2));
265 return evaluate_subexp_standard (expect_type, exp, pos, noside);
269 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
273 /* Table of operators and their precedences for printing expressions. */
275 static const struct op_print m2_op_print_tab[] =
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}
309 /* The built-in types of Modula-2. */
311 enum m2_primitive_types {
312 m2_primitive_type_char,
313 m2_primitive_type_int,
314 m2_primitive_type_card,
315 m2_primitive_type_real,
316 m2_primitive_type_bool,
317 nr_m2_primitive_types
321 m2_language_arch_info (struct gdbarch *gdbarch,
322 struct language_arch_info *lai)
324 const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);
326 lai->string_char_type = builtin->builtin_char;
327 lai->primitive_type_vector
328 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_m2_primitive_types + 1,
331 lai->primitive_type_vector [m2_primitive_type_char]
332 = builtin->builtin_char;
333 lai->primitive_type_vector [m2_primitive_type_int]
334 = builtin->builtin_int;
335 lai->primitive_type_vector [m2_primitive_type_card]
336 = builtin->builtin_card;
337 lai->primitive_type_vector [m2_primitive_type_real]
338 = builtin->builtin_real;
339 lai->primitive_type_vector [m2_primitive_type_bool]
340 = builtin->builtin_bool;
342 lai->bool_type_symbol = "BOOLEAN";
343 lai->bool_type_default = builtin->builtin_bool;
346 const struct exp_descriptor exp_descriptor_modula2 =
348 print_subexp_standard,
349 operator_length_standard,
350 operator_check_standard,
352 dump_subexp_body_standard,
353 evaluate_subexp_modula2
356 extern const struct language_defn m2_language_defn =
366 &exp_descriptor_modula2,
367 m2_parse, /* parser */
369 m2_printchar, /* Print character constant */
370 m2_printstr, /* function to print string constant */
371 m2_emit_char, /* Function to print a single character */
372 m2_print_type, /* Print a type using appropriate syntax */
373 m2_print_typedef, /* Print a typedef using appropriate syntax */
374 m2_val_print, /* Print a value using appropriate syntax */
375 c_value_print, /* Print a top-level value */
376 default_read_var_value, /* la_read_var_value */
377 NULL, /* Language specific skip_trampoline */
378 NULL, /* name_of_this */
379 false, /* la_store_sym_names_in_linkage_form_p */
380 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
381 basic_lookup_transparent_type,/* lookup_transparent_type */
382 NULL, /* Language specific symbol demangler */
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 */
389 default_word_break_characters,
390 default_collect_symbol_completion_matches,
391 m2_language_arch_info,
392 default_print_array_index,
393 default_pass_by_reference,
395 c_watch_location_expression,
396 NULL, /* la_get_symbol_name_matcher */
397 iterate_over_symbols,
398 default_search_name_hash,
406 build_m2_types (struct gdbarch *gdbarch)
408 struct builtin_m2_type *builtin_m2_type
409 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type);
411 /* Modula-2 "pervasive" types. NOTE: these can be redefined!!! */
412 builtin_m2_type->builtin_int
413 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
414 builtin_m2_type->builtin_card
415 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
416 builtin_m2_type->builtin_real
417 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL",
418 gdbarch_float_format (gdbarch));
419 builtin_m2_type->builtin_char
420 = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR");
421 builtin_m2_type->builtin_bool
422 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");
424 return builtin_m2_type;
427 static struct gdbarch_data *m2_type_data;
429 const struct builtin_m2_type *
430 builtin_m2_type (struct gdbarch *gdbarch)
432 return (const struct builtin_m2_type *) gdbarch_data (gdbarch, m2_type_data);
436 /* Initialization for Modula-2 */
439 _initialize_m2_language (void)
441 m2_type_data = gdbarch_data_register_post_init (build_m2_types);