1 /* Modula 2 language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2002, 2003, 2004,
4 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
31 extern void _initialize_m2_language (void);
32 static void m2_printchar (int, struct type
*, struct ui_file
*);
33 static void m2_emit_char (int, struct type
*, struct ui_file
*, int);
35 /* Print the character C on STREAM as part of the contents of a literal
36 string whose delimiter is QUOTER. Note that that format for printing
37 characters and strings is language specific.
38 FIXME: This is a copy of the same function from c-exp.y. It should
39 be replaced with a true Modula version. */
42 m2_emit_char (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
45 c
&= 0xFF; /* Avoid sign bit follies */
47 if (PRINT_LITERAL_FORM (c
))
49 if (c
== '\\' || c
== quoter
)
51 fputs_filtered ("\\", stream
);
53 fprintf_filtered (stream
, "%c", c
);
60 fputs_filtered ("\\n", stream
);
63 fputs_filtered ("\\b", stream
);
66 fputs_filtered ("\\t", stream
);
69 fputs_filtered ("\\f", stream
);
72 fputs_filtered ("\\r", stream
);
75 fputs_filtered ("\\e", stream
);
78 fputs_filtered ("\\a", stream
);
81 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
87 /* FIXME: This is a copy of the same function from c-exp.y. It should
88 be replaced with a true Modula version. */
91 m2_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
93 fputs_filtered ("'", stream
);
94 LA_EMIT_CHAR (c
, type
, stream
, '\'');
95 fputs_filtered ("'", stream
);
98 /* Print the character string STRING, printing at most LENGTH characters.
99 Printing stops early if the number hits print_max; repeat counts
100 are printed as appropriate. Print ellipses at the end if we
101 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
102 FIXME: This is a copy of the same function from c-exp.y. It should
103 be replaced with a true Modula version. */
106 m2_printstr (struct ui_file
*stream
, struct type
*type
, const gdb_byte
*string
,
107 unsigned int length
, const char *encoding
, int force_ellipses
,
108 const struct value_print_options
*options
)
111 unsigned int things_printed
= 0;
114 int width
= TYPE_LENGTH (type
);
118 fputs_filtered ("\"\"", gdb_stdout
);
122 for (i
= 0; i
< length
&& things_printed
< options
->print_max
; ++i
)
124 /* Position of the character we are examining
125 to see whether it is repeated. */
127 /* Number of repetitions we have detected so far. */
134 fputs_filtered (", ", stream
);
140 while (rep1
< length
&& string
[rep1
] == string
[i
])
146 if (reps
> options
->repeat_count_threshold
)
150 if (options
->inspect_it
)
151 fputs_filtered ("\\\", ", stream
);
153 fputs_filtered ("\", ", stream
);
156 m2_printchar (string
[i
], type
, stream
);
157 fprintf_filtered (stream
, " <repeats %u times>", reps
);
159 things_printed
+= options
->repeat_count_threshold
;
166 if (options
->inspect_it
)
167 fputs_filtered ("\\\"", stream
);
169 fputs_filtered ("\"", stream
);
172 LA_EMIT_CHAR (string
[i
], type
, stream
, '"');
177 /* Terminate the quotes if necessary. */
180 if (options
->inspect_it
)
181 fputs_filtered ("\\\"", stream
);
183 fputs_filtered ("\"", stream
);
186 if (force_ellipses
|| i
< length
)
187 fputs_filtered ("...", stream
);
190 static struct value
*
191 evaluate_subexp_modula2 (struct type
*expect_type
, struct expression
*exp
,
192 int *pos
, enum noside noside
)
194 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
202 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
204 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
208 arg1
= coerce_ref (arg1
);
209 type
= check_typedef (value_type (arg1
));
211 if (m2_is_unbounded_array (type
))
213 struct value
*temp
= arg1
;
214 type
= TYPE_FIELD_TYPE (type
, 1);
215 /* i18n: Do not translate the "_m2_high" part! */
216 arg1
= value_struct_elt (&temp
, NULL
, "_m2_high", NULL
,
217 _("unbounded structure "
218 "missing _m2_high field"));
220 if (value_type (arg1
) != type
)
221 arg1
= value_cast (type
, arg1
);
226 case BINOP_SUBSCRIPT
:
228 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
229 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
230 if (noside
== EVAL_SKIP
)
232 /* If the user attempts to subscript something that is not an
233 array or pointer type (like a plain int variable for example),
234 then report this as an error. */
236 arg1
= coerce_ref (arg1
);
237 type
= check_typedef (value_type (arg1
));
239 if (m2_is_unbounded_array (type
))
241 struct value
*temp
= arg1
;
242 type
= TYPE_FIELD_TYPE (type
, 0);
243 if (type
== NULL
|| (TYPE_CODE (type
) != TYPE_CODE_PTR
)) {
244 warning (_("internal error: unbounded array structure is unknown"));
245 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
247 /* i18n: Do not translate the "_m2_contents" part! */
248 arg1
= value_struct_elt (&temp
, NULL
, "_m2_contents", NULL
,
249 _("unbounded structure "
250 "missing _m2_contents field"));
252 if (value_type (arg1
) != type
)
253 arg1
= value_cast (type
, arg1
);
255 type
= check_typedef (value_type (arg1
));
256 return value_ind (value_ptradd (arg1
, value_as_long (arg2
)));
259 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
261 if (TYPE_NAME (type
))
262 error (_("cannot subscript something of type `%s'"),
265 error (_("cannot subscript requested type"));
268 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
269 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
271 return value_subscript (arg1
, value_as_long (arg2
));
274 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
278 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
282 /* Table of operators and their precedences for printing expressions. */
284 static const struct op_print m2_op_print_tab
[] =
286 {"+", BINOP_ADD
, PREC_ADD
, 0},
287 {"+", UNOP_PLUS
, PREC_PREFIX
, 0},
288 {"-", BINOP_SUB
, PREC_ADD
, 0},
289 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
290 {"*", BINOP_MUL
, PREC_MUL
, 0},
291 {"/", BINOP_DIV
, PREC_MUL
, 0},
292 {"DIV", BINOP_INTDIV
, PREC_MUL
, 0},
293 {"MOD", BINOP_REM
, PREC_MUL
, 0},
294 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
295 {"OR", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
296 {"AND", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
297 {"NOT", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
298 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
299 {"<>", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
300 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
301 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
302 {">", BINOP_GTR
, PREC_ORDER
, 0},
303 {"<", BINOP_LESS
, PREC_ORDER
, 0},
304 {"^", UNOP_IND
, PREC_PREFIX
, 0},
305 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
306 {"CAP", UNOP_CAP
, PREC_BUILTIN_FUNCTION
, 0},
307 {"CHR", UNOP_CHR
, PREC_BUILTIN_FUNCTION
, 0},
308 {"ORD", UNOP_ORD
, PREC_BUILTIN_FUNCTION
, 0},
309 {"FLOAT", UNOP_FLOAT
, PREC_BUILTIN_FUNCTION
, 0},
310 {"HIGH", UNOP_HIGH
, PREC_BUILTIN_FUNCTION
, 0},
311 {"MAX", UNOP_MAX
, PREC_BUILTIN_FUNCTION
, 0},
312 {"MIN", UNOP_MIN
, PREC_BUILTIN_FUNCTION
, 0},
313 {"ODD", UNOP_ODD
, PREC_BUILTIN_FUNCTION
, 0},
314 {"TRUNC", UNOP_TRUNC
, PREC_BUILTIN_FUNCTION
, 0},
318 /* The built-in types of Modula-2. */
320 enum m2_primitive_types
{
321 m2_primitive_type_char
,
322 m2_primitive_type_int
,
323 m2_primitive_type_card
,
324 m2_primitive_type_real
,
325 m2_primitive_type_bool
,
326 nr_m2_primitive_types
330 m2_language_arch_info (struct gdbarch
*gdbarch
,
331 struct language_arch_info
*lai
)
333 const struct builtin_m2_type
*builtin
= builtin_m2_type (gdbarch
);
335 lai
->string_char_type
= builtin
->builtin_char
;
336 lai
->primitive_type_vector
337 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_m2_primitive_types
+ 1,
340 lai
->primitive_type_vector
[m2_primitive_type_char
]
341 = builtin
->builtin_char
;
342 lai
->primitive_type_vector
[m2_primitive_type_int
]
343 = builtin
->builtin_int
;
344 lai
->primitive_type_vector
[m2_primitive_type_card
]
345 = builtin
->builtin_card
;
346 lai
->primitive_type_vector
[m2_primitive_type_real
]
347 = builtin
->builtin_real
;
348 lai
->primitive_type_vector
[m2_primitive_type_bool
]
349 = builtin
->builtin_bool
;
351 lai
->bool_type_symbol
= "BOOLEAN";
352 lai
->bool_type_default
= builtin
->builtin_bool
;
355 const struct exp_descriptor exp_descriptor_modula2
=
357 print_subexp_standard
,
358 operator_length_standard
,
360 dump_subexp_body_standard
,
361 evaluate_subexp_modula2
364 const struct language_defn m2_language_defn
=
373 &exp_descriptor_modula2
,
374 m2_parse
, /* parser */
375 m2_error
, /* parser error function */
377 m2_printchar
, /* Print character constant */
378 m2_printstr
, /* function to print string constant */
379 m2_emit_char
, /* Function to print a single character */
380 m2_print_type
, /* Print a type using appropriate syntax */
381 m2_print_typedef
, /* Print a typedef using appropriate syntax */
382 m2_val_print
, /* Print a value using appropriate syntax */
383 c_value_print
, /* Print a top-level value */
384 NULL
, /* Language specific skip_trampoline */
385 NULL
, /* name_of_this */
386 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
387 basic_lookup_transparent_type
,/* lookup_transparent_type */
388 NULL
, /* Language specific symbol demangler */
389 NULL
, /* Language specific class_name_from_physname */
390 m2_op_print_tab
, /* expression operators for printing */
391 0, /* arrays are first-class (not c-style) */
392 0, /* String lower bound */
393 default_word_break_characters
,
394 default_make_symbol_completion_list
,
395 m2_language_arch_info
,
396 default_print_array_index
,
397 default_pass_by_reference
,
403 build_m2_types (struct gdbarch
*gdbarch
)
405 struct builtin_m2_type
*builtin_m2_type
406 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_m2_type
);
408 /* Modula-2 "pervasive" types. NOTE: these can be redefined!!! */
409 builtin_m2_type
->builtin_int
410 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 0, "INTEGER");
411 builtin_m2_type
->builtin_card
412 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 1, "CARDINAL");
413 builtin_m2_type
->builtin_real
414 = arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
), "REAL", NULL
);
415 builtin_m2_type
->builtin_char
416 = arch_character_type (gdbarch
, TARGET_CHAR_BIT
, 1, "CHAR");
417 builtin_m2_type
->builtin_bool
418 = arch_boolean_type (gdbarch
, gdbarch_int_bit (gdbarch
), 1, "BOOLEAN");
420 return builtin_m2_type
;
423 static struct gdbarch_data
*m2_type_data
;
425 const struct builtin_m2_type
*
426 builtin_m2_type (struct gdbarch
*gdbarch
)
428 return gdbarch_data (gdbarch
, m2_type_data
);
432 /* Initialization for Modula-2 */
435 _initialize_m2_language (void)
437 m2_type_data
= gdbarch_data_register_post_init (build_m2_types
);
439 add_language (&m2_language_defn
);