Fix nullptr in with_command_1
[external/binutils.git] / gdb / m2-lang.c
1 /* Modula 2 language support routines for GDB, the GNU debugger.
2
3    Copyright (C) 1992-2019 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 #include "gdbarch.h"
31
32 static void m2_printchar (int, struct type *, struct ui_file *);
33 static void m2_emit_char (int, struct type *, struct ui_file *, int);
34
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.  */
40
41 static void
42 m2_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
43 {
44
45   c &= 0xFF;                    /* Avoid sign bit follies.  */
46
47   if (PRINT_LITERAL_FORM (c))
48     {
49       if (c == '\\' || c == quoter)
50         {
51           fputs_filtered ("\\", stream);
52         }
53       fprintf_filtered (stream, "%c", c);
54     }
55   else
56     {
57       switch (c)
58         {
59         case '\n':
60           fputs_filtered ("\\n", stream);
61           break;
62         case '\b':
63           fputs_filtered ("\\b", stream);
64           break;
65         case '\t':
66           fputs_filtered ("\\t", stream);
67           break;
68         case '\f':
69           fputs_filtered ("\\f", stream);
70           break;
71         case '\r':
72           fputs_filtered ("\\r", stream);
73           break;
74         case '\033':
75           fputs_filtered ("\\e", stream);
76           break;
77         case '\007':
78           fputs_filtered ("\\a", stream);
79           break;
80         default:
81           fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
82           break;
83         }
84     }
85 }
86
87 /* FIXME:  This is a copy of the same function from c-exp.y.  It should
88    be replaced with a true Modula version.  */
89
90 static void
91 m2_printchar (int c, struct type *type, struct ui_file *stream)
92 {
93   fputs_filtered ("'", stream);
94   LA_EMIT_CHAR (c, type, stream, '\'');
95   fputs_filtered ("'", stream);
96 }
97
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.  */
104
105 static void
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)
109 {
110   unsigned int i;
111   unsigned int things_printed = 0;
112   int in_quotes = 0;
113   int need_comma = 0;
114
115   if (length == 0)
116     {
117       fputs_filtered ("\"\"", gdb_stdout);
118       return;
119     }
120
121   for (i = 0; i < length && things_printed < options->print_max; ++i)
122     {
123       /* Position of the character we are examining
124          to see whether it is repeated.  */
125       unsigned int rep1;
126       /* Number of repetitions we have detected so far.  */
127       unsigned int reps;
128
129       QUIT;
130
131       if (need_comma)
132         {
133           fputs_filtered (", ", stream);
134           need_comma = 0;
135         }
136
137       rep1 = i + 1;
138       reps = 1;
139       while (rep1 < length && string[rep1] == string[i])
140         {
141           ++rep1;
142           ++reps;
143         }
144
145       if (reps > options->repeat_count_threshold)
146         {
147           if (in_quotes)
148             {
149               fputs_filtered ("\", ", stream);
150               in_quotes = 0;
151             }
152           m2_printchar (string[i], type, stream);
153           fprintf_filtered (stream, " <repeats %u times>", reps);
154           i = rep1 - 1;
155           things_printed += options->repeat_count_threshold;
156           need_comma = 1;
157         }
158       else
159         {
160           if (!in_quotes)
161             {
162               fputs_filtered ("\"", stream);
163               in_quotes = 1;
164             }
165           LA_EMIT_CHAR (string[i], type, stream, '"');
166           ++things_printed;
167         }
168     }
169
170   /* Terminate the quotes if necessary.  */
171   if (in_quotes)
172     fputs_filtered ("\"", stream);
173
174   if (force_ellipses || i < length)
175     fputs_filtered ("...", stream);
176 }
177
178 /* Return true if TYPE is a string.  */
179
180 static bool
181 m2_is_string_type_p (struct type *type)
182 {
183   type = check_typedef (type);
184   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
185       && TYPE_LENGTH (type) > 0
186       && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
187     {
188       struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
189
190       if (TYPE_LENGTH (elttype) == 1
191           && (TYPE_CODE (elttype) == TYPE_CODE_INT
192               || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
193         return true;
194     }
195
196   return false;
197 }
198
199 static struct value *
200 evaluate_subexp_modula2 (struct type *expect_type, struct expression *exp,
201                          int *pos, enum noside noside)
202 {
203   enum exp_opcode op = exp->elts[*pos].opcode;
204   struct value *arg1;
205   struct value *arg2;
206   struct type *type;
207
208   switch (op)
209     {
210     case UNOP_HIGH:
211       (*pos)++;
212       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
213
214       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
215         return arg1;
216       else
217         {
218           arg1 = coerce_ref (arg1);
219           type = check_typedef (value_type (arg1));
220
221           if (m2_is_unbounded_array (type))
222             {
223               struct value *temp = arg1;
224
225               type = TYPE_FIELD_TYPE (type, 1);
226               /* i18n: Do not translate the "_m2_high" part!  */
227               arg1 = value_struct_elt (&temp, NULL, "_m2_high", NULL,
228                                        _("unbounded structure "
229                                          "missing _m2_high field"));
230           
231               if (value_type (arg1) != type)
232                 arg1 = value_cast (type, arg1);
233             }
234         }
235       return arg1;
236
237     case BINOP_SUBSCRIPT:
238       (*pos)++;
239       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
240       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
241       if (noside == EVAL_SKIP)
242         goto nosideret;
243       /* If the user attempts to subscript something that is not an
244          array or pointer type (like a plain int variable for example),
245          then report this as an error.  */
246
247       arg1 = coerce_ref (arg1);
248       type = check_typedef (value_type (arg1));
249
250       if (m2_is_unbounded_array (type))
251         {
252           struct value *temp = arg1;
253           type = TYPE_FIELD_TYPE (type, 0);
254           if (type == NULL || (TYPE_CODE (type) != TYPE_CODE_PTR))
255             {
256               warning (_("internal error: unbounded "
257                          "array structure is unknown"));
258               return evaluate_subexp_standard (expect_type, exp, pos, noside);
259             }
260           /* i18n: Do not translate the "_m2_contents" part!  */
261           arg1 = value_struct_elt (&temp, NULL, "_m2_contents", NULL,
262                                    _("unbounded structure "
263                                      "missing _m2_contents field"));
264           
265           if (value_type (arg1) != type)
266             arg1 = value_cast (type, arg1);
267
268           check_typedef (value_type (arg1));
269           return value_ind (value_ptradd (arg1, value_as_long (arg2)));
270         }
271       else
272         if (TYPE_CODE (type) != TYPE_CODE_ARRAY)
273           {
274             if (TYPE_NAME (type))
275               error (_("cannot subscript something of type `%s'"),
276                      TYPE_NAME (type));
277             else
278               error (_("cannot subscript requested type"));
279           }
280
281       if (noside == EVAL_AVOID_SIDE_EFFECTS)
282         return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
283       else
284         return value_subscript (arg1, value_as_long (arg2));
285
286     default:
287       return evaluate_subexp_standard (expect_type, exp, pos, noside);
288     }
289
290  nosideret:
291   return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
292 }
293 \f
294
295 /* Table of operators and their precedences for printing expressions.  */
296
297 static const struct op_print m2_op_print_tab[] =
298 {
299   {"+", BINOP_ADD, PREC_ADD, 0},
300   {"+", UNOP_PLUS, PREC_PREFIX, 0},
301   {"-", BINOP_SUB, PREC_ADD, 0},
302   {"-", UNOP_NEG, PREC_PREFIX, 0},
303   {"*", BINOP_MUL, PREC_MUL, 0},
304   {"/", BINOP_DIV, PREC_MUL, 0},
305   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
306   {"MOD", BINOP_REM, PREC_MUL, 0},
307   {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
308   {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
309   {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
310   {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
311   {"=", BINOP_EQUAL, PREC_EQUAL, 0},
312   {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
313   {"<=", BINOP_LEQ, PREC_ORDER, 0},
314   {">=", BINOP_GEQ, PREC_ORDER, 0},
315   {">", BINOP_GTR, PREC_ORDER, 0},
316   {"<", BINOP_LESS, PREC_ORDER, 0},
317   {"^", UNOP_IND, PREC_PREFIX, 0},
318   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
319   {"CAP", UNOP_CAP, PREC_BUILTIN_FUNCTION, 0},
320   {"CHR", UNOP_CHR, PREC_BUILTIN_FUNCTION, 0},
321   {"ORD", UNOP_ORD, PREC_BUILTIN_FUNCTION, 0},
322   {"FLOAT", UNOP_FLOAT, PREC_BUILTIN_FUNCTION, 0},
323   {"HIGH", UNOP_HIGH, PREC_BUILTIN_FUNCTION, 0},
324   {"MAX", UNOP_MAX, PREC_BUILTIN_FUNCTION, 0},
325   {"MIN", UNOP_MIN, PREC_BUILTIN_FUNCTION, 0},
326   {"ODD", UNOP_ODD, PREC_BUILTIN_FUNCTION, 0},
327   {"TRUNC", UNOP_TRUNC, PREC_BUILTIN_FUNCTION, 0},
328   {NULL, OP_NULL, PREC_BUILTIN_FUNCTION, 0}
329 };
330 \f
331 /* The built-in types of Modula-2.  */
332
333 enum m2_primitive_types {
334   m2_primitive_type_char,
335   m2_primitive_type_int,
336   m2_primitive_type_card,
337   m2_primitive_type_real,
338   m2_primitive_type_bool,
339   nr_m2_primitive_types
340 };
341
342 static void
343 m2_language_arch_info (struct gdbarch *gdbarch,
344                        struct language_arch_info *lai)
345 {
346   const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);
347
348   lai->string_char_type = builtin->builtin_char;
349   lai->primitive_type_vector
350     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_m2_primitive_types + 1,
351                               struct type *);
352
353   lai->primitive_type_vector [m2_primitive_type_char]
354     = builtin->builtin_char;
355   lai->primitive_type_vector [m2_primitive_type_int]
356     = builtin->builtin_int;
357   lai->primitive_type_vector [m2_primitive_type_card]
358     = builtin->builtin_card;
359   lai->primitive_type_vector [m2_primitive_type_real]
360     = builtin->builtin_real;
361   lai->primitive_type_vector [m2_primitive_type_bool]
362     = builtin->builtin_bool;
363
364   lai->bool_type_symbol = "BOOLEAN";
365   lai->bool_type_default = builtin->builtin_bool;
366 }
367
368 const struct exp_descriptor exp_descriptor_modula2 = 
369 {
370   print_subexp_standard,
371   operator_length_standard,
372   operator_check_standard,
373   op_name_standard,
374   dump_subexp_body_standard,
375   evaluate_subexp_modula2
376 };
377
378 extern const struct language_defn m2_language_defn =
379 {
380   "modula-2",
381   "Modula-2",
382   language_m2,
383   range_check_on,
384   case_sensitive_on,
385   array_row_major,
386   macro_expansion_no,
387   NULL,
388   &exp_descriptor_modula2,
389   m2_parse,                     /* parser */
390   null_post_parser,
391   m2_printchar,                 /* Print character constant */
392   m2_printstr,                  /* function to print string constant */
393   m2_emit_char,                 /* Function to print a single character */
394   m2_print_type,                /* Print a type using appropriate syntax */
395   m2_print_typedef,             /* Print a typedef using appropriate syntax */
396   m2_val_print,                 /* Print a value using appropriate syntax */
397   c_value_print,                /* Print a top-level value */
398   default_read_var_value,       /* la_read_var_value */
399   NULL,                         /* Language specific skip_trampoline */
400   NULL,                         /* name_of_this */
401   false,                        /* la_store_sym_names_in_linkage_form_p */
402   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
403   basic_lookup_transparent_type,/* lookup_transparent_type */
404   NULL,                         /* Language specific symbol demangler */
405   NULL,
406   NULL,                         /* Language specific
407                                    class_name_from_physname */
408   m2_op_print_tab,              /* expression operators for printing */
409   0,                            /* arrays are first-class (not c-style) */
410   0,                            /* String lower bound */
411   default_word_break_characters,
412   default_collect_symbol_completion_matches,
413   m2_language_arch_info,
414   default_print_array_index,
415   default_pass_by_reference,
416   default_get_string,
417   c_watch_location_expression,
418   NULL,                         /* la_get_symbol_name_matcher */
419   iterate_over_symbols,
420   default_search_name_hash,
421   &default_varobj_ops,
422   NULL,
423   NULL,
424   m2_is_string_type_p,
425   "{...}"                       /* la_struct_too_deep_ellipsis */
426 };
427
428 static void *
429 build_m2_types (struct gdbarch *gdbarch)
430 {
431   struct builtin_m2_type *builtin_m2_type
432     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type);
433
434   /* Modula-2 "pervasive" types.  NOTE:  these can be redefined!!! */
435   builtin_m2_type->builtin_int
436     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
437   builtin_m2_type->builtin_card
438     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
439   builtin_m2_type->builtin_real
440     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL",
441                        gdbarch_float_format (gdbarch));
442   builtin_m2_type->builtin_char
443     = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR");
444   builtin_m2_type->builtin_bool
445     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");
446
447   return builtin_m2_type;
448 }
449
450 static struct gdbarch_data *m2_type_data;
451
452 const struct builtin_m2_type *
453 builtin_m2_type (struct gdbarch *gdbarch)
454 {
455   return (const struct builtin_m2_type *) gdbarch_data (gdbarch, m2_type_data);
456 }
457
458
459 /* Initialization for Modula-2 */
460
461 void
462 _initialize_m2_language (void)
463 {
464   m2_type_data = gdbarch_data_register_post_init (build_m2_types);
465 }