Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Modula 2 language support routines for GDB, the GNU debugger. |
ce27fb25 | 2 | |
3666a048 | 3 | Copyright (C) 1992-2021 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
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" | |
a53b64ea | 26 | #include "varobj.h" |
c906108c SS |
27 | #include "m2-lang.h" |
28 | #include "c-lang.h" | |
745b8ca0 | 29 | #include "valprint.h" |
0d12e84c | 30 | #include "gdbarch.h" |
2bc9b40c | 31 | #include "m2-exp.h" |
c906108c | 32 | |
41bdced5 TT |
33 | /* A helper function for UNOP_HIGH. */ |
34 | ||
2bc9b40c | 35 | struct value * |
41bdced5 TT |
36 | eval_op_m2_high (struct type *expect_type, struct expression *exp, |
37 | enum noside noside, | |
38 | struct value *arg1) | |
39 | { | |
40 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
41 | return arg1; | |
42 | else | |
43 | { | |
44 | arg1 = coerce_ref (arg1); | |
45 | struct type *type = check_typedef (value_type (arg1)); | |
46 | ||
47 | if (m2_is_unbounded_array (type)) | |
48 | { | |
49 | struct value *temp = arg1; | |
50 | ||
51 | type = type->field (1).type (); | |
52 | /* i18n: Do not translate the "_m2_high" part! */ | |
53 | arg1 = value_struct_elt (&temp, NULL, "_m2_high", NULL, | |
54 | _("unbounded structure " | |
55 | "missing _m2_high field")); | |
56 | ||
57 | if (value_type (arg1) != type) | |
58 | arg1 = value_cast (type, arg1); | |
59 | } | |
60 | } | |
61 | return arg1; | |
62 | } | |
63 | ||
a49881f7 TT |
64 | /* A helper function for BINOP_SUBSCRIPT. */ |
65 | ||
2bc9b40c | 66 | struct value * |
a49881f7 TT |
67 | eval_op_m2_subscript (struct type *expect_type, struct expression *exp, |
68 | enum noside noside, | |
69 | struct value *arg1, struct value *arg2) | |
70 | { | |
71 | if (noside == EVAL_SKIP) | |
72 | return eval_skip_value (exp); | |
73 | /* If the user attempts to subscript something that is not an | |
74 | array or pointer type (like a plain int variable for example), | |
75 | then report this as an error. */ | |
76 | ||
77 | arg1 = coerce_ref (arg1); | |
78 | struct type *type = check_typedef (value_type (arg1)); | |
79 | ||
80 | if (m2_is_unbounded_array (type)) | |
81 | { | |
82 | struct value *temp = arg1; | |
83 | type = type->field (0).type (); | |
84 | if (type == NULL || (type->code () != TYPE_CODE_PTR)) | |
85 | error (_("internal error: unbounded " | |
86 | "array structure is unknown")); | |
87 | /* i18n: Do not translate the "_m2_contents" part! */ | |
88 | arg1 = value_struct_elt (&temp, NULL, "_m2_contents", NULL, | |
89 | _("unbounded structure " | |
90 | "missing _m2_contents field")); | |
91 | ||
92 | if (value_type (arg1) != type) | |
93 | arg1 = value_cast (type, arg1); | |
94 | ||
95 | check_typedef (value_type (arg1)); | |
96 | return value_ind (value_ptradd (arg1, value_as_long (arg2))); | |
97 | } | |
98 | else | |
99 | if (type->code () != TYPE_CODE_ARRAY) | |
100 | { | |
101 | if (type->name ()) | |
102 | error (_("cannot subscript something of type `%s'"), | |
103 | type->name ()); | |
104 | else | |
105 | error (_("cannot subscript requested type")); | |
106 | } | |
107 | ||
108 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
109 | return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1)); | |
110 | else | |
111 | return value_subscript (arg1, value_as_long (arg2)); | |
112 | } | |
113 | ||
c906108c | 114 | \f |
c5aa993b | 115 | |
790e2a12 | 116 | /* Single instance of the M2 language. */ |
d711ee67 | 117 | |
790e2a12 | 118 | static m2_language m2_language_defn; |
4ffc13fb | 119 | |
790e2a12 | 120 | /* See language.h. */ |
4ffc13fb | 121 | |
790e2a12 AB |
122 | void |
123 | m2_language::language_arch_info (struct gdbarch *gdbarch, | |
124 | struct language_arch_info *lai) const | |
125 | { | |
126 | const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch); | |
127 | ||
7bea47f0 AB |
128 | /* Helper function to allow shorter lines below. */ |
129 | auto add = [&] (struct type * t) | |
130 | { | |
131 | lai->add_primitive_type (t); | |
132 | }; | |
133 | ||
134 | add (builtin->builtin_char); | |
135 | add (builtin->builtin_int); | |
136 | add (builtin->builtin_card); | |
137 | add (builtin->builtin_real); | |
138 | add (builtin->builtin_bool); | |
139 | ||
140 | lai->set_string_char_type (builtin->builtin_char); | |
141 | lai->set_bool_type (builtin->builtin_bool, "BOOLEAN"); | |
790e2a12 | 142 | } |
4ffc13fb | 143 | |
790e2a12 | 144 | /* See languge.h. */ |
39e7ecca | 145 | |
790e2a12 AB |
146 | void |
147 | m2_language::printchar (int c, struct type *type, | |
148 | struct ui_file *stream) const | |
149 | { | |
150 | fputs_filtered ("'", stream); | |
151 | emitchar (c, type, stream, '\''); | |
152 | fputs_filtered ("'", stream); | |
153 | } | |
39e7ecca | 154 | |
790e2a12 | 155 | /* See language.h. */ |
39e7ecca | 156 | |
790e2a12 AB |
157 | void |
158 | m2_language::printstr (struct ui_file *stream, struct type *elttype, | |
159 | const gdb_byte *string, unsigned int length, | |
160 | const char *encoding, int force_ellipses, | |
161 | const struct value_print_options *options) const | |
162 | { | |
163 | unsigned int i; | |
164 | unsigned int things_printed = 0; | |
165 | int in_quotes = 0; | |
166 | int need_comma = 0; | |
67bd3fd5 | 167 | |
790e2a12 AB |
168 | if (length == 0) |
169 | { | |
170 | fputs_filtered ("\"\"", gdb_stdout); | |
171 | return; | |
172 | } | |
67bd3fd5 | 173 | |
790e2a12 AB |
174 | for (i = 0; i < length && things_printed < options->print_max; ++i) |
175 | { | |
176 | /* Position of the character we are examining | |
177 | to see whether it is repeated. */ | |
178 | unsigned int rep1; | |
179 | /* Number of repetitions we have detected so far. */ | |
180 | unsigned int reps; | |
22c12a6c | 181 | |
790e2a12 | 182 | QUIT; |
22c12a6c | 183 | |
790e2a12 AB |
184 | if (need_comma) |
185 | { | |
186 | fputs_filtered (", ", stream); | |
187 | need_comma = 0; | |
188 | } | |
efdf6a73 | 189 | |
790e2a12 AB |
190 | rep1 = i + 1; |
191 | reps = 1; | |
192 | while (rep1 < length && string[rep1] == string[i]) | |
193 | { | |
194 | ++rep1; | |
195 | ++reps; | |
196 | } | |
efdf6a73 | 197 | |
790e2a12 AB |
198 | if (reps > options->repeat_count_threshold) |
199 | { | |
200 | if (in_quotes) | |
201 | { | |
202 | fputs_filtered ("\", ", stream); | |
203 | in_quotes = 0; | |
204 | } | |
205 | printchar (string[i], elttype, stream); | |
206 | fprintf_filtered (stream, " <repeats %u times>", reps); | |
207 | i = rep1 - 1; | |
208 | things_printed += options->repeat_count_threshold; | |
209 | need_comma = 1; | |
210 | } | |
211 | else | |
212 | { | |
213 | if (!in_quotes) | |
214 | { | |
215 | fputs_filtered ("\"", stream); | |
216 | in_quotes = 1; | |
217 | } | |
218 | emitchar (string[i], elttype, stream, '"'); | |
219 | ++things_printed; | |
220 | } | |
221 | } | |
5aba6ebe | 222 | |
790e2a12 AB |
223 | /* Terminate the quotes if necessary. */ |
224 | if (in_quotes) | |
225 | fputs_filtered ("\"", stream); | |
5aba6ebe | 226 | |
790e2a12 AB |
227 | if (force_ellipses || i < length) |
228 | fputs_filtered ("...", stream); | |
229 | } | |
b7c6e27d | 230 | |
790e2a12 | 231 | /* See language.h. */ |
b7c6e27d | 232 | |
790e2a12 AB |
233 | void |
234 | m2_language::emitchar (int ch, struct type *chtype, | |
235 | struct ui_file *stream, int quoter) const | |
236 | { | |
237 | ch &= 0xFF; /* Avoid sign bit follies. */ | |
0874fd07 | 238 | |
790e2a12 AB |
239 | if (PRINT_LITERAL_FORM (ch)) |
240 | { | |
241 | if (ch == '\\' || ch == quoter) | |
242 | fputs_filtered ("\\", stream); | |
243 | fprintf_filtered (stream, "%c", ch); | |
244 | } | |
245 | else | |
246 | { | |
247 | switch (ch) | |
248 | { | |
249 | case '\n': | |
250 | fputs_filtered ("\\n", stream); | |
251 | break; | |
252 | case '\b': | |
253 | fputs_filtered ("\\b", stream); | |
254 | break; | |
255 | case '\t': | |
256 | fputs_filtered ("\\t", stream); | |
257 | break; | |
258 | case '\f': | |
259 | fputs_filtered ("\\f", stream); | |
260 | break; | |
261 | case '\r': | |
262 | fputs_filtered ("\\r", stream); | |
263 | break; | |
264 | case '\033': | |
265 | fputs_filtered ("\\e", stream); | |
266 | break; | |
267 | case '\007': | |
268 | fputs_filtered ("\\a", stream); | |
269 | break; | |
270 | default: | |
271 | fprintf_filtered (stream, "\\%.3o", (unsigned int) ch); | |
272 | break; | |
273 | } | |
274 | } | |
275 | } | |
0874fd07 | 276 | |
790e2a12 AB |
277 | /* Called during architecture gdbarch initialisation to create language |
278 | specific types. */ | |
0874fd07 | 279 | |
5760b90a UW |
280 | static void * |
281 | build_m2_types (struct gdbarch *gdbarch) | |
c906108c | 282 | { |
5760b90a UW |
283 | struct builtin_m2_type *builtin_m2_type |
284 | = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type); | |
285 | ||
c906108c | 286 | /* Modula-2 "pervasive" types. NOTE: these can be redefined!!! */ |
e9bb382b UW |
287 | builtin_m2_type->builtin_int |
288 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER"); | |
289 | builtin_m2_type->builtin_card | |
290 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL"); | |
291 | builtin_m2_type->builtin_real | |
49f190bc UW |
292 | = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL", |
293 | gdbarch_float_format (gdbarch)); | |
e9bb382b UW |
294 | builtin_m2_type->builtin_char |
295 | = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR"); | |
296 | builtin_m2_type->builtin_bool | |
297 | = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN"); | |
c906108c | 298 | |
5760b90a UW |
299 | return builtin_m2_type; |
300 | } | |
301 | ||
302 | static struct gdbarch_data *m2_type_data; | |
303 | ||
304 | const struct builtin_m2_type * | |
305 | builtin_m2_type (struct gdbarch *gdbarch) | |
306 | { | |
9a3c8263 | 307 | return (const struct builtin_m2_type *) gdbarch_data (gdbarch, m2_type_data); |
5760b90a UW |
308 | } |
309 | ||
310 | ||
311 | /* Initialization for Modula-2 */ | |
312 | ||
6c265988 | 313 | void _initialize_m2_language (); |
5760b90a | 314 | void |
6c265988 | 315 | _initialize_m2_language () |
5760b90a UW |
316 | { |
317 | m2_type_data = gdbarch_data_register_post_init (build_m2_types); | |
c906108c | 318 | } |