Commit | Line | Data |
---|---|---|
c44af4eb TT |
1 | /* Rust language support routines for GDB, the GNU debugger. |
2 | ||
e2882c85 | 3 | Copyright (C) 2016-2018 Free Software Foundation, Inc. |
c44af4eb TT |
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 | ||
22 | #include <ctype.h> | |
23 | ||
24 | #include "block.h" | |
25 | #include "c-lang.h" | |
26 | #include "charset.h" | |
27 | #include "cp-support.h" | |
8b302db8 | 28 | #include "demangle.h" |
c44af4eb TT |
29 | #include "gdbarch.h" |
30 | #include "infcall.h" | |
31 | #include "objfiles.h" | |
71a3c369 | 32 | #include "psymtab.h" |
c44af4eb TT |
33 | #include "rust-lang.h" |
34 | #include "valprint.h" | |
35 | #include "varobj.h" | |
ab8b80a8 TT |
36 | #include <string> |
37 | #include <vector> | |
c44af4eb | 38 | |
c9317f21 | 39 | /* See rust-lang.h. */ |
c44af4eb | 40 | |
c9317f21 TT |
41 | const char * |
42 | rust_last_path_segment (const char *path) | |
c44af4eb TT |
43 | { |
44 | const char *result = strrchr (path, ':'); | |
45 | ||
46 | if (result == NULL) | |
c9317f21 | 47 | return path; |
c44af4eb TT |
48 | return result + 1; |
49 | } | |
50 | ||
03c85b11 | 51 | /* See rust-lang.h. */ |
c44af4eb | 52 | |
03c85b11 | 53 | std::string |
c44af4eb TT |
54 | rust_crate_for_block (const struct block *block) |
55 | { | |
56 | const char *scope = block_scope (block); | |
57 | ||
58 | if (scope[0] == '\0') | |
03c85b11 | 59 | return std::string (); |
c44af4eb | 60 | |
03c85b11 | 61 | return std::string (scope, cp_find_first_component (scope)); |
c44af4eb TT |
62 | } |
63 | ||
c9317f21 TT |
64 | /* Return true if TYPE, which must be a struct type, represents a Rust |
65 | enum. */ | |
c44af4eb | 66 | |
b96645f1 | 67 | static bool |
c9317f21 | 68 | rust_enum_p (const struct type *type) |
b96645f1 | 69 | { |
c9317f21 TT |
70 | return (TYPE_CODE (type) == TYPE_CODE_STRUCT |
71 | && TYPE_NFIELDS (type) == 1 | |
72 | && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type, 0))); | |
b96645f1 MG |
73 | } |
74 | ||
c9317f21 | 75 | /* Given an enum type and contents, find which variant is active. */ |
c44af4eb | 76 | |
c9317f21 TT |
77 | struct field * |
78 | rust_enum_variant (struct type *type, const gdb_byte *contents) | |
c44af4eb | 79 | { |
c9317f21 TT |
80 | /* In Rust the enum always fills the containing structure. */ |
81 | gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0); | |
c44af4eb | 82 | |
c9317f21 | 83 | struct type *union_type = TYPE_FIELD_TYPE (type, 0); |
c44af4eb | 84 | |
c9317f21 TT |
85 | int fieldno = value_union_variant (union_type, contents); |
86 | return &TYPE_FIELD (union_type, fieldno); | |
c44af4eb TT |
87 | } |
88 | ||
89 | /* See rust-lang.h. */ | |
90 | ||
65c40c95 | 91 | bool |
c44af4eb TT |
92 | rust_tuple_type_p (struct type *type) |
93 | { | |
94 | /* The current implementation is a bit of a hack, but there's | |
95 | nothing else in the debuginfo to distinguish a tuple from a | |
96 | struct. */ | |
97 | return (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
98 | && TYPE_TAG_NAME (type) != NULL | |
99 | && TYPE_TAG_NAME (type)[0] == '('); | |
100 | } | |
101 | ||
c44af4eb | 102 | /* Return true if all non-static fields of a structlike type are in a |
c9317f21 | 103 | sequence like __0, __1, __2. */ |
c44af4eb | 104 | |
65c40c95 | 105 | static bool |
c9317f21 | 106 | rust_underscore_fields (struct type *type) |
c44af4eb TT |
107 | { |
108 | int i, field_number; | |
109 | ||
110 | field_number = 0; | |
111 | ||
112 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT) | |
65c40c95 | 113 | return false; |
c44af4eb TT |
114 | for (i = 0; i < TYPE_NFIELDS (type); ++i) |
115 | { | |
116 | if (!field_is_static (&TYPE_FIELD (type, i))) | |
117 | { | |
c9317f21 | 118 | char buf[20]; |
c44af4eb | 119 | |
c9317f21 TT |
120 | xsnprintf (buf, sizeof (buf), "__%d", field_number); |
121 | if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0) | |
122 | return false; | |
123 | field_number++; | |
c44af4eb TT |
124 | } |
125 | } | |
65c40c95 | 126 | return true; |
c44af4eb TT |
127 | } |
128 | ||
129 | /* See rust-lang.h. */ | |
130 | ||
65c40c95 | 131 | bool |
c44af4eb TT |
132 | rust_tuple_struct_type_p (struct type *type) |
133 | { | |
12df5c00 TT |
134 | /* This is just an approximation until DWARF can represent Rust more |
135 | precisely. We exclude zero-length structs because they may not | |
136 | be tuple structs, and there's no way to tell. */ | |
c9317f21 | 137 | return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type); |
c44af4eb TT |
138 | } |
139 | ||
140 | /* Return true if TYPE is a slice type, otherwise false. */ | |
141 | ||
65c40c95 | 142 | static bool |
c44af4eb TT |
143 | rust_slice_type_p (struct type *type) |
144 | { | |
145 | return (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
146 | && TYPE_TAG_NAME (type) != NULL | |
01af5e0d TT |
147 | && (strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0 |
148 | || strcmp (TYPE_TAG_NAME (type), "&str") == 0)); | |
c44af4eb TT |
149 | } |
150 | ||
151 | /* Return true if TYPE is a range type, otherwise false. */ | |
152 | ||
65c40c95 | 153 | static bool |
c44af4eb TT |
154 | rust_range_type_p (struct type *type) |
155 | { | |
156 | int i; | |
157 | ||
158 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
159 | || TYPE_NFIELDS (type) > 2 | |
160 | || TYPE_TAG_NAME (type) == NULL | |
161 | || strstr (TYPE_TAG_NAME (type), "::Range") == NULL) | |
65c40c95 | 162 | return false; |
c44af4eb TT |
163 | |
164 | if (TYPE_NFIELDS (type) == 0) | |
65c40c95 | 165 | return true; |
c44af4eb TT |
166 | |
167 | i = 0; | |
168 | if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0) | |
169 | { | |
170 | if (TYPE_NFIELDS (type) == 1) | |
65c40c95 | 171 | return true; |
c44af4eb TT |
172 | i = 1; |
173 | } | |
174 | else if (TYPE_NFIELDS (type) == 2) | |
175 | { | |
176 | /* First field had to be "start". */ | |
65c40c95 | 177 | return false; |
c44af4eb TT |
178 | } |
179 | ||
180 | return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0; | |
181 | } | |
182 | ||
6873858b TT |
183 | /* Return true if TYPE is an inclusive range type, otherwise false. |
184 | This is only valid for types which are already known to be range | |
185 | types. */ | |
186 | ||
187 | static bool | |
188 | rust_inclusive_range_type_p (struct type *type) | |
189 | { | |
190 | return (strstr (TYPE_TAG_NAME (type), "::RangeInclusive") != NULL | |
191 | || strstr (TYPE_TAG_NAME (type), "::RangeToInclusive") != NULL); | |
192 | } | |
193 | ||
c44af4eb TT |
194 | /* Return true if TYPE seems to be the type "u8", otherwise false. */ |
195 | ||
65c40c95 | 196 | static bool |
c44af4eb TT |
197 | rust_u8_type_p (struct type *type) |
198 | { | |
199 | return (TYPE_CODE (type) == TYPE_CODE_INT | |
200 | && TYPE_UNSIGNED (type) | |
201 | && TYPE_LENGTH (type) == 1); | |
202 | } | |
203 | ||
204 | /* Return true if TYPE is a Rust character type. */ | |
205 | ||
65c40c95 | 206 | static bool |
c44af4eb TT |
207 | rust_chartype_p (struct type *type) |
208 | { | |
209 | return (TYPE_CODE (type) == TYPE_CODE_CHAR | |
210 | && TYPE_LENGTH (type) == 4 | |
211 | && TYPE_UNSIGNED (type)); | |
212 | } | |
213 | ||
71a3c369 TT |
214 | /* If VALUE represents a trait object pointer, return the underlying |
215 | pointer with the correct (i.e., runtime) type. Otherwise, return | |
216 | NULL. */ | |
217 | ||
218 | static struct value * | |
219 | rust_get_trait_object_pointer (struct value *value) | |
220 | { | |
221 | struct type *type = check_typedef (value_type (value)); | |
222 | ||
223 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2) | |
224 | return NULL; | |
225 | ||
226 | /* Try to be a bit resilient if the ABI changes. */ | |
227 | int vtable_field = 0; | |
228 | for (int i = 0; i < 2; ++i) | |
229 | { | |
230 | if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0) | |
231 | vtable_field = i; | |
232 | else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0) | |
233 | return NULL; | |
234 | } | |
235 | ||
236 | CORE_ADDR vtable = value_as_address (value_field (value, vtable_field)); | |
237 | struct symbol *symbol = find_symbol_at_address (vtable); | |
cf724bc9 | 238 | if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE) |
71a3c369 TT |
239 | return NULL; |
240 | ||
241 | struct rust_vtable_symbol *vtable_sym | |
242 | = static_cast<struct rust_vtable_symbol *> (symbol); | |
243 | struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type); | |
244 | return value_cast (pointer_type, value_field (value, 1 - vtable_field)); | |
245 | } | |
246 | ||
c44af4eb TT |
247 | \f |
248 | ||
249 | /* la_emitchar implementation for Rust. */ | |
250 | ||
251 | static void | |
252 | rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter) | |
253 | { | |
254 | if (!rust_chartype_p (type)) | |
255 | generic_emit_char (c, type, stream, quoter, | |
256 | target_charset (get_type_arch (type))); | |
257 | else if (c == '\\' || c == quoter) | |
258 | fprintf_filtered (stream, "\\%c", c); | |
259 | else if (c == '\n') | |
260 | fputs_filtered ("\\n", stream); | |
261 | else if (c == '\r') | |
262 | fputs_filtered ("\\r", stream); | |
263 | else if (c == '\t') | |
264 | fputs_filtered ("\\t", stream); | |
265 | else if (c == '\0') | |
266 | fputs_filtered ("\\0", stream); | |
267 | else if (c >= 32 && c <= 127 && isprint (c)) | |
268 | fputc_filtered (c, stream); | |
269 | else if (c <= 255) | |
270 | fprintf_filtered (stream, "\\x%02x", c); | |
271 | else | |
272 | fprintf_filtered (stream, "\\u{%06x}", c); | |
273 | } | |
274 | ||
275 | /* la_printchar implementation for Rust. */ | |
276 | ||
277 | static void | |
278 | rust_printchar (int c, struct type *type, struct ui_file *stream) | |
279 | { | |
280 | fputs_filtered ("'", stream); | |
281 | LA_EMIT_CHAR (c, type, stream, '\''); | |
282 | fputs_filtered ("'", stream); | |
283 | } | |
284 | ||
285 | /* la_printstr implementation for Rust. */ | |
286 | ||
287 | static void | |
288 | rust_printstr (struct ui_file *stream, struct type *type, | |
289 | const gdb_byte *string, unsigned int length, | |
290 | const char *user_encoding, int force_ellipses, | |
291 | const struct value_print_options *options) | |
292 | { | |
293 | /* Rust always uses UTF-8, but let the caller override this if need | |
294 | be. */ | |
295 | const char *encoding = user_encoding; | |
296 | if (user_encoding == NULL || !*user_encoding) | |
297 | { | |
298 | /* In Rust strings, characters are "u8". */ | |
299 | if (rust_u8_type_p (type)) | |
300 | encoding = "UTF-8"; | |
301 | else | |
302 | { | |
303 | /* This is probably some C string, so let's let C deal with | |
304 | it. */ | |
305 | c_printstr (stream, type, string, length, user_encoding, | |
306 | force_ellipses, options); | |
307 | return; | |
308 | } | |
309 | } | |
310 | ||
311 | /* This is not ideal as it doesn't use our character printer. */ | |
312 | generic_printstr (stream, type, string, length, encoding, force_ellipses, | |
313 | '"', 0, options); | |
314 | } | |
315 | ||
316 | \f | |
317 | ||
45320ffa TT |
318 | /* Helper function to print a string slice. */ |
319 | ||
320 | static void | |
321 | rust_val_print_str (struct ui_file *stream, struct value *val, | |
322 | const struct value_print_options *options) | |
323 | { | |
324 | struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL, | |
325 | "slice"); | |
326 | struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice"); | |
327 | ||
328 | val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8", | |
329 | value_as_address (base), value_as_long (len), stream, | |
330 | options); | |
331 | } | |
332 | ||
c9317f21 | 333 | /* rust_val_print helper for structs and untagged unions. */ |
b96645f1 MG |
334 | |
335 | static void | |
e8b24d9f YQ |
336 | val_print_struct (struct type *type, int embedded_offset, |
337 | CORE_ADDR address, struct ui_file *stream, | |
338 | int recurse, struct value *val, | |
20955dbf | 339 | const struct value_print_options *options) |
b96645f1 MG |
340 | { |
341 | int i; | |
342 | int first_field; | |
45320ffa TT |
343 | |
344 | if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0) | |
345 | { | |
346 | rust_val_print_str (stream, val, options); | |
347 | return; | |
348 | } | |
349 | ||
65c40c95 TT |
350 | bool is_tuple = rust_tuple_type_p (type); |
351 | bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type); | |
b96645f1 MG |
352 | struct value_print_options opts; |
353 | ||
354 | if (!is_tuple) | |
355 | { | |
356 | if (TYPE_TAG_NAME (type) != NULL) | |
357 | fprintf_filtered (stream, "%s", TYPE_TAG_NAME (type)); | |
358 | ||
359 | if (TYPE_NFIELDS (type) == 0) | |
360 | return; | |
361 | ||
362 | if (TYPE_TAG_NAME (type) != NULL) | |
363 | fputs_filtered (" ", stream); | |
364 | } | |
365 | ||
366 | if (is_tuple || is_tuple_struct) | |
367 | fputs_filtered ("(", stream); | |
368 | else | |
369 | fputs_filtered ("{", stream); | |
370 | ||
371 | opts = *options; | |
372 | opts.deref_ref = 0; | |
373 | ||
374 | first_field = 1; | |
375 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
376 | { | |
377 | if (field_is_static (&TYPE_FIELD (type, i))) | |
378 | continue; | |
379 | ||
380 | if (!first_field) | |
381 | fputs_filtered (",", stream); | |
382 | ||
383 | if (options->prettyformat) | |
384 | { | |
b50f188d TT |
385 | fputs_filtered ("\n", stream); |
386 | print_spaces_filtered (2 + 2 * recurse, stream); | |
b96645f1 MG |
387 | } |
388 | else if (!first_field) | |
389 | fputs_filtered (" ", stream); | |
390 | ||
391 | first_field = 0; | |
392 | ||
393 | if (!is_tuple && !is_tuple_struct) | |
394 | { | |
b50f188d TT |
395 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
396 | fputs_filtered (": ", stream); | |
b96645f1 MG |
397 | } |
398 | ||
399 | val_print (TYPE_FIELD_TYPE (type, i), | |
b50f188d TT |
400 | embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8, |
401 | address, | |
402 | stream, recurse + 1, val, &opts, | |
403 | current_language); | |
b96645f1 MG |
404 | } |
405 | ||
406 | if (options->prettyformat) | |
407 | { | |
408 | fputs_filtered ("\n", stream); | |
409 | print_spaces_filtered (2 * recurse, stream); | |
410 | } | |
411 | ||
412 | if (is_tuple || is_tuple_struct) | |
413 | fputs_filtered (")", stream); | |
414 | else | |
415 | fputs_filtered ("}", stream); | |
416 | } | |
417 | ||
c9317f21 TT |
418 | /* rust_val_print helper for discriminated unions (Rust enums). */ |
419 | ||
420 | static void | |
421 | rust_print_enum (struct type *type, int embedded_offset, | |
422 | CORE_ADDR address, struct ui_file *stream, | |
423 | int recurse, struct value *val, | |
424 | const struct value_print_options *options) | |
425 | { | |
426 | struct value_print_options opts = *options; | |
427 | ||
428 | opts.deref_ref = 0; | |
429 | ||
430 | const gdb_byte *valaddr = value_contents_for_printing (val); | |
431 | struct field *variant_field = rust_enum_variant (type, valaddr); | |
432 | embedded_offset += FIELD_BITPOS (*variant_field) / 8; | |
433 | struct type *variant_type = FIELD_TYPE (*variant_field); | |
434 | ||
435 | int nfields = TYPE_NFIELDS (variant_type); | |
436 | ||
437 | bool is_tuple = rust_tuple_struct_type_p (variant_type); | |
438 | ||
439 | fprintf_filtered (stream, "%s", TYPE_NAME (variant_type)); | |
440 | if (nfields == 0) | |
441 | { | |
442 | /* In case of a nullary variant like 'None', just output | |
443 | the name. */ | |
444 | return; | |
445 | } | |
446 | ||
447 | /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */ | |
448 | if (is_tuple) | |
449 | fprintf_filtered (stream, "("); | |
450 | else | |
451 | { | |
452 | /* struct variant. */ | |
453 | fprintf_filtered (stream, "{"); | |
454 | } | |
455 | ||
456 | bool first_field = true; | |
457 | for (int j = 0; j < TYPE_NFIELDS (variant_type); j++) | |
458 | { | |
459 | if (!first_field) | |
460 | fputs_filtered (", ", stream); | |
461 | first_field = false; | |
462 | ||
463 | if (!is_tuple) | |
464 | fprintf_filtered (stream, "%s: ", | |
465 | TYPE_FIELD_NAME (variant_type, j)); | |
466 | ||
467 | val_print (TYPE_FIELD_TYPE (variant_type, j), | |
468 | (embedded_offset | |
469 | + TYPE_FIELD_BITPOS (variant_type, j) / 8), | |
470 | address, | |
471 | stream, recurse + 1, val, &opts, | |
472 | current_language); | |
473 | } | |
474 | ||
475 | if (is_tuple) | |
476 | fputs_filtered (")", stream); | |
477 | else | |
478 | fputs_filtered ("}", stream); | |
479 | } | |
480 | ||
c44af4eb TT |
481 | static const struct generic_val_print_decorations rust_decorations = |
482 | { | |
483 | /* Complex isn't used in Rust, but we provide C-ish values just in | |
484 | case. */ | |
485 | "", | |
486 | " + ", | |
487 | " * I", | |
488 | "true", | |
489 | "false", | |
921d8f54 | 490 | "()", |
c44af4eb TT |
491 | "[", |
492 | "]" | |
493 | }; | |
494 | ||
495 | /* la_val_print implementation for Rust. */ | |
496 | ||
497 | static void | |
e8b24d9f | 498 | rust_val_print (struct type *type, int embedded_offset, |
c44af4eb | 499 | CORE_ADDR address, struct ui_file *stream, int recurse, |
e8b24d9f | 500 | struct value *val, |
c44af4eb TT |
501 | const struct value_print_options *options) |
502 | { | |
e8b24d9f YQ |
503 | const gdb_byte *valaddr = value_contents_for_printing (val); |
504 | ||
c44af4eb TT |
505 | type = check_typedef (type); |
506 | switch (TYPE_CODE (type)) | |
507 | { | |
508 | case TYPE_CODE_PTR: | |
509 | { | |
510 | LONGEST low_bound, high_bound; | |
511 | ||
512 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY | |
513 | && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type))) | |
514 | && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound, | |
515 | &high_bound)) { | |
516 | /* We have a pointer to a byte string, so just print | |
517 | that. */ | |
518 | struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
519 | CORE_ADDR addr; | |
520 | struct gdbarch *arch = get_type_arch (type); | |
521 | int unit_size = gdbarch_addressable_memory_unit_size (arch); | |
522 | ||
523 | addr = unpack_pointer (type, valaddr + embedded_offset * unit_size); | |
524 | if (options->addressprint) | |
525 | { | |
526 | fputs_filtered (paddress (arch, addr), stream); | |
527 | fputs_filtered (" ", stream); | |
528 | } | |
529 | ||
530 | fputs_filtered ("b", stream); | |
531 | val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr, | |
532 | high_bound - low_bound + 1, stream, | |
533 | options); | |
534 | break; | |
535 | } | |
536 | } | |
537 | /* Fall through. */ | |
538 | ||
539 | case TYPE_CODE_METHODPTR: | |
540 | case TYPE_CODE_MEMBERPTR: | |
e8b24d9f | 541 | c_val_print (type, embedded_offset, address, stream, |
c44af4eb TT |
542 | recurse, val, options); |
543 | break; | |
544 | ||
545 | case TYPE_CODE_INT: | |
546 | /* Recognize the unit type. */ | |
547 | if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0 | |
548 | && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0) | |
549 | { | |
550 | fputs_filtered ("()", stream); | |
551 | break; | |
552 | } | |
553 | goto generic_print; | |
554 | ||
555 | case TYPE_CODE_STRING: | |
556 | { | |
557 | struct gdbarch *arch = get_type_arch (type); | |
558 | int unit_size = gdbarch_addressable_memory_unit_size (arch); | |
559 | LONGEST low_bound, high_bound; | |
560 | ||
561 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
562 | error (_("Could not determine the array bounds")); | |
563 | ||
564 | /* If we see a plain TYPE_CODE_STRING, then we're printing a | |
565 | byte string, hence the choice of "ASCII" as the | |
566 | encoding. */ | |
567 | fputs_filtered ("b", stream); | |
568 | rust_printstr (stream, TYPE_TARGET_TYPE (type), | |
569 | valaddr + embedded_offset * unit_size, | |
570 | high_bound - low_bound + 1, "ASCII", 0, options); | |
571 | } | |
572 | break; | |
573 | ||
574 | case TYPE_CODE_ARRAY: | |
575 | { | |
576 | LONGEST low_bound, high_bound; | |
577 | ||
578 | if (get_array_bounds (type, &low_bound, &high_bound) | |
579 | && high_bound - low_bound + 1 == 0) | |
580 | fputs_filtered ("[]", stream); | |
581 | else | |
582 | goto generic_print; | |
583 | } | |
584 | break; | |
585 | ||
586 | case TYPE_CODE_UNION: | |
c9317f21 TT |
587 | /* Untagged unions are printed as if they are structs. Since |
588 | the field bit positions overlap in the debuginfo, the code | |
589 | for printing a union is same as that for a struct, the only | |
590 | difference is that the input type will have overlapping | |
591 | fields. */ | |
592 | val_print_struct (type, embedded_offset, address, stream, | |
593 | recurse, val, options); | |
c44af4eb TT |
594 | break; |
595 | ||
596 | case TYPE_CODE_STRUCT: | |
c9317f21 TT |
597 | if (rust_enum_p (type)) |
598 | rust_print_enum (type, embedded_offset, address, stream, | |
599 | recurse, val, options); | |
600 | else | |
601 | val_print_struct (type, embedded_offset, address, stream, | |
602 | recurse, val, options); | |
b96645f1 | 603 | break; |
c44af4eb | 604 | |
b96645f1 MG |
605 | default: |
606 | generic_print: | |
607 | /* Nothing special yet. */ | |
e8b24d9f | 608 | generic_val_print (type, embedded_offset, address, stream, |
b96645f1 MG |
609 | recurse, val, options, &rust_decorations); |
610 | } | |
611 | } | |
c44af4eb | 612 | |
b96645f1 | 613 | \f |
c44af4eb | 614 | |
b96645f1 | 615 | static void |
c9317f21 TT |
616 | rust_internal_print_type (struct type *type, const char *varstring, |
617 | struct ui_file *stream, int show, int level, | |
618 | const struct type_print_options *flags, | |
619 | bool for_rust_enum); | |
b96645f1 MG |
620 | |
621 | /* Print a struct or union typedef. */ | |
622 | static void | |
623 | rust_print_struct_def (struct type *type, const char *varstring, | |
b50f188d | 624 | struct ui_file *stream, int show, int level, |
c9317f21 TT |
625 | const struct type_print_options *flags, |
626 | bool for_rust_enum) | |
b96645f1 | 627 | { |
b50f188d TT |
628 | /* Print a tuple type simply. */ |
629 | if (rust_tuple_type_p (type)) | |
630 | { | |
631 | fputs_filtered (TYPE_TAG_NAME (type), stream); | |
632 | return; | |
633 | } | |
c44af4eb | 634 | |
b50f188d TT |
635 | /* If we see a base class, delegate to C. */ |
636 | if (TYPE_N_BASECLASSES (type) > 0) | |
637 | c_print_type (type, varstring, stream, show, level, flags); | |
c44af4eb | 638 | |
c9317f21 TT |
639 | /* Compute properties of TYPE here because, in the enum case, the |
640 | rest of the code ends up looking only at the variant part. */ | |
641 | const char *tagname = TYPE_TAG_NAME (type); | |
642 | bool is_tuple_struct = rust_tuple_struct_type_p (type); | |
643 | bool is_tuple = rust_tuple_type_p (type); | |
644 | bool is_enum = rust_enum_p (type); | |
b96645f1 | 645 | |
c9317f21 TT |
646 | int enum_discriminant_index = -1; |
647 | ||
648 | if (for_rust_enum) | |
649 | { | |
650 | /* Already printing an outer enum, so nothing to print here. */ | |
651 | } | |
652 | else | |
653 | { | |
654 | /* This code path is also used by unions and enums. */ | |
655 | if (is_enum) | |
656 | { | |
657 | fputs_filtered ("enum ", stream); | |
658 | type = TYPE_FIELD_TYPE (type, 0); | |
659 | ||
660 | struct dynamic_prop *discriminant_prop | |
661 | = get_dyn_prop (DYN_PROP_DISCRIMINATED, type); | |
662 | struct discriminant_info *info | |
663 | = (struct discriminant_info *) discriminant_prop->data.baton; | |
664 | enum_discriminant_index = info->discriminant_index; | |
665 | } | |
c9317f21 TT |
666 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
667 | fputs_filtered ("struct ", stream); | |
668 | else | |
669 | fputs_filtered ("union ", stream); | |
b96645f1 | 670 | |
c9317f21 TT |
671 | if (tagname != NULL) |
672 | fputs_filtered (tagname, stream); | |
673 | } | |
b96645f1 | 674 | |
c9317f21 | 675 | if (TYPE_NFIELDS (type) == 0 && !is_tuple) |
b50f188d | 676 | return; |
c9317f21 TT |
677 | if (for_rust_enum) |
678 | fputs_filtered (is_tuple_struct ? "(" : "{", stream); | |
679 | else | |
680 | fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream); | |
c44af4eb | 681 | |
c9317f21 | 682 | for (int i = 0; i < TYPE_NFIELDS (type); ++i) |
b50f188d | 683 | { |
b50f188d TT |
684 | QUIT; |
685 | if (field_is_static (&TYPE_FIELD (type, i))) | |
686 | continue; | |
687 | ||
688 | /* We'd like to print "pub" here as needed, but rustc | |
689 | doesn't emit the debuginfo, and our types don't have | |
690 | cplus_struct_type attached. */ | |
691 | ||
692 | /* For a tuple struct we print the type but nothing | |
693 | else. */ | |
c9317f21 TT |
694 | if (!for_rust_enum) |
695 | print_spaces_filtered (level + 2, stream); | |
696 | if (is_enum) | |
697 | { | |
698 | if (i == enum_discriminant_index) | |
699 | continue; | |
700 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
701 | } | |
c9317f21 | 702 | else if (!is_tuple_struct) |
b50f188d TT |
703 | fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i)); |
704 | ||
c9317f21 | 705 | rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL, |
53d7df28 TT |
706 | stream, (is_enum ? show : show - 1), |
707 | level + 2, flags, is_enum); | |
c9317f21 TT |
708 | if (!for_rust_enum) |
709 | fputs_filtered (",\n", stream); | |
710 | else if (i + 1 < TYPE_NFIELDS (type)) | |
711 | fputs_filtered (", ", stream); | |
b50f188d | 712 | } |
c44af4eb | 713 | |
c9317f21 TT |
714 | if (!for_rust_enum) |
715 | print_spaces_filtered (level, stream); | |
716 | fputs_filtered (is_tuple_struct ? ")" : "}", stream); | |
c44af4eb TT |
717 | } |
718 | ||
c44af4eb TT |
719 | /* la_print_typedef implementation for Rust. */ |
720 | ||
721 | static void | |
722 | rust_print_typedef (struct type *type, | |
723 | struct symbol *new_symbol, | |
724 | struct ui_file *stream) | |
725 | { | |
726 | type = check_typedef (type); | |
727 | fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol)); | |
728 | type_print (type, "", stream, 0); | |
729 | fprintf_filtered (stream, ";\n"); | |
730 | } | |
731 | ||
732 | /* la_print_type implementation for Rust. */ | |
733 | ||
734 | static void | |
c9317f21 TT |
735 | rust_internal_print_type (struct type *type, const char *varstring, |
736 | struct ui_file *stream, int show, int level, | |
737 | const struct type_print_options *flags, | |
738 | bool for_rust_enum) | |
c44af4eb TT |
739 | { |
740 | int i; | |
741 | ||
742 | QUIT; | |
743 | if (show <= 0 | |
744 | && TYPE_NAME (type) != NULL) | |
745 | { | |
921d8f54 MG |
746 | /* Rust calls the unit type "void" in its debuginfo, |
747 | but we don't want to print it as that. */ | |
748 | if (TYPE_CODE (type) == TYPE_CODE_VOID) | |
749 | fputs_filtered ("()", stream); | |
750 | else | |
751 | fputs_filtered (TYPE_NAME (type), stream); | |
c44af4eb TT |
752 | return; |
753 | } | |
754 | ||
755 | type = check_typedef (type); | |
756 | switch (TYPE_CODE (type)) | |
757 | { | |
921d8f54 | 758 | case TYPE_CODE_VOID: |
c9317f21 TT |
759 | /* If we have an enum, we've already printed the type's |
760 | unqualified name, and there is nothing else to print | |
761 | here. */ | |
762 | if (!for_rust_enum) | |
763 | fputs_filtered ("()", stream); | |
921d8f54 MG |
764 | break; |
765 | ||
c44af4eb TT |
766 | case TYPE_CODE_FUNC: |
767 | /* Delegate varargs to the C printer. */ | |
768 | if (TYPE_VARARGS (type)) | |
769 | goto c_printer; | |
770 | ||
771 | fputs_filtered ("fn ", stream); | |
772 | if (varstring != NULL) | |
773 | fputs_filtered (varstring, stream); | |
774 | fputs_filtered ("(", stream); | |
775 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
776 | { | |
777 | QUIT; | |
778 | if (i > 0) | |
779 | fputs_filtered (", ", stream); | |
c9317f21 TT |
780 | rust_internal_print_type (TYPE_FIELD_TYPE (type, i), "", stream, |
781 | -1, 0, flags, false); | |
c44af4eb | 782 | } |
921d8f54 MG |
783 | fputs_filtered (")", stream); |
784 | /* If it returns unit, we can omit the return type. */ | |
785 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID) | |
786 | { | |
787 | fputs_filtered (" -> ", stream); | |
c9317f21 TT |
788 | rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream, |
789 | -1, 0, flags, false); | |
921d8f54 | 790 | } |
c44af4eb TT |
791 | break; |
792 | ||
793 | case TYPE_CODE_ARRAY: | |
794 | { | |
795 | LONGEST low_bound, high_bound; | |
796 | ||
797 | fputs_filtered ("[", stream); | |
c9317f21 TT |
798 | rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL, |
799 | stream, show - 1, level, flags, false); | |
c44af4eb TT |
800 | |
801 | if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR | |
802 | || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST) | |
e6cf65f2 | 803 | fprintf_filtered (stream, "; variable length"); |
c44af4eb | 804 | else if (get_array_bounds (type, &low_bound, &high_bound)) |
e6cf65f2 | 805 | fprintf_filtered (stream, "; %s", |
c44af4eb TT |
806 | plongest (high_bound - low_bound + 1)); |
807 | fputs_filtered ("]", stream); | |
808 | } | |
809 | break; | |
810 | ||
c9317f21 | 811 | case TYPE_CODE_UNION: |
c44af4eb | 812 | case TYPE_CODE_STRUCT: |
c9317f21 TT |
813 | rust_print_struct_def (type, varstring, stream, show, level, flags, |
814 | for_rust_enum); | |
c44af4eb TT |
815 | break; |
816 | ||
817 | case TYPE_CODE_ENUM: | |
818 | { | |
819 | int i, len = 0; | |
820 | ||
821 | fputs_filtered ("enum ", stream); | |
822 | if (TYPE_TAG_NAME (type) != NULL) | |
823 | { | |
824 | fputs_filtered (TYPE_TAG_NAME (type), stream); | |
825 | fputs_filtered (" ", stream); | |
826 | len = strlen (TYPE_TAG_NAME (type)); | |
827 | } | |
828 | fputs_filtered ("{\n", stream); | |
829 | ||
830 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
831 | { | |
832 | const char *name = TYPE_FIELD_NAME (type, i); | |
833 | ||
834 | QUIT; | |
835 | ||
836 | if (len > 0 | |
837 | && strncmp (name, TYPE_TAG_NAME (type), len) == 0 | |
838 | && name[len] == ':' | |
839 | && name[len + 1] == ':') | |
840 | name += len + 2; | |
841 | fprintfi_filtered (level + 2, stream, "%s,\n", name); | |
842 | } | |
843 | ||
844 | fputs_filtered ("}", stream); | |
845 | } | |
846 | break; | |
847 | ||
c44af4eb TT |
848 | default: |
849 | c_printer: | |
850 | c_print_type (type, varstring, stream, show, level, flags); | |
851 | } | |
852 | } | |
853 | ||
c9317f21 TT |
854 | static void |
855 | rust_print_type (struct type *type, const char *varstring, | |
856 | struct ui_file *stream, int show, int level, | |
857 | const struct type_print_options *flags) | |
858 | { | |
859 | rust_internal_print_type (type, varstring, stream, show, level, | |
860 | flags, false); | |
861 | } | |
862 | ||
c44af4eb TT |
863 | \f |
864 | ||
c44af4eb TT |
865 | /* Like arch_composite_type, but uses TYPE to decide how to allocate |
866 | -- either on an obstack or on a gdbarch. */ | |
867 | ||
868 | static struct type * | |
869 | rust_composite_type (struct type *original, | |
870 | const char *name, | |
871 | const char *field1, struct type *type1, | |
872 | const char *field2, struct type *type2) | |
873 | { | |
874 | struct type *result = alloc_type_copy (original); | |
875 | int i, nfields, bitpos; | |
876 | ||
877 | nfields = 0; | |
878 | if (field1 != NULL) | |
879 | ++nfields; | |
880 | if (field2 != NULL) | |
881 | ++nfields; | |
882 | ||
883 | TYPE_CODE (result) = TYPE_CODE_STRUCT; | |
884 | TYPE_NAME (result) = name; | |
885 | TYPE_TAG_NAME (result) = name; | |
886 | ||
887 | TYPE_NFIELDS (result) = nfields; | |
888 | TYPE_FIELDS (result) | |
889 | = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)); | |
890 | ||
891 | i = 0; | |
892 | bitpos = 0; | |
893 | if (field1 != NULL) | |
894 | { | |
895 | struct field *field = &TYPE_FIELD (result, i); | |
896 | ||
897 | SET_FIELD_BITPOS (*field, bitpos); | |
898 | bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT; | |
899 | ||
900 | FIELD_NAME (*field) = field1; | |
901 | FIELD_TYPE (*field) = type1; | |
902 | ++i; | |
903 | } | |
904 | if (field2 != NULL) | |
905 | { | |
906 | struct field *field = &TYPE_FIELD (result, i); | |
2fff16dd | 907 | unsigned align = type_align (type2); |
c44af4eb TT |
908 | |
909 | if (align != 0) | |
910 | { | |
911 | int delta; | |
912 | ||
913 | align *= TARGET_CHAR_BIT; | |
914 | delta = bitpos % align; | |
915 | if (delta != 0) | |
916 | bitpos += align - delta; | |
917 | } | |
918 | SET_FIELD_BITPOS (*field, bitpos); | |
919 | ||
920 | FIELD_NAME (*field) = field2; | |
921 | FIELD_TYPE (*field) = type2; | |
922 | ++i; | |
923 | } | |
924 | ||
925 | if (i > 0) | |
926 | TYPE_LENGTH (result) | |
927 | = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT + | |
928 | TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1))); | |
929 | return result; | |
930 | } | |
931 | ||
932 | /* See rust-lang.h. */ | |
933 | ||
934 | struct type * | |
935 | rust_slice_type (const char *name, struct type *elt_type, | |
936 | struct type *usize_type) | |
937 | { | |
938 | struct type *type; | |
939 | ||
940 | elt_type = lookup_pointer_type (elt_type); | |
941 | type = rust_composite_type (elt_type, name, | |
942 | "data_ptr", elt_type, | |
943 | "length", usize_type); | |
944 | ||
945 | return type; | |
946 | } | |
947 | ||
948 | enum rust_primitive_types | |
949 | { | |
950 | rust_primitive_bool, | |
951 | rust_primitive_char, | |
952 | rust_primitive_i8, | |
953 | rust_primitive_u8, | |
954 | rust_primitive_i16, | |
955 | rust_primitive_u16, | |
956 | rust_primitive_i32, | |
957 | rust_primitive_u32, | |
958 | rust_primitive_i64, | |
959 | rust_primitive_u64, | |
960 | rust_primitive_isize, | |
961 | rust_primitive_usize, | |
962 | rust_primitive_f32, | |
963 | rust_primitive_f64, | |
964 | rust_primitive_unit, | |
965 | rust_primitive_str, | |
966 | nr_rust_primitive_types | |
967 | }; | |
968 | ||
969 | /* la_language_arch_info implementation for Rust. */ | |
970 | ||
971 | static void | |
972 | rust_language_arch_info (struct gdbarch *gdbarch, | |
973 | struct language_arch_info *lai) | |
974 | { | |
975 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
976 | struct type *tem; | |
977 | struct type **types; | |
978 | unsigned int length; | |
979 | ||
980 | types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1, | |
981 | struct type *); | |
982 | ||
983 | types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool"); | |
984 | types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char"); | |
985 | types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8"); | |
986 | types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8"); | |
987 | types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16"); | |
988 | types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16"); | |
989 | types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32"); | |
990 | types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32"); | |
991 | types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64"); | |
992 | types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64"); | |
993 | ||
994 | length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr); | |
995 | types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize"); | |
996 | types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize"); | |
997 | ||
49f190bc UW |
998 | types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32", |
999 | floatformats_ieee_single); | |
1000 | types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64", | |
1001 | floatformats_ieee_double); | |
c44af4eb TT |
1002 | |
1003 | types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()"); | |
1004 | ||
1005 | tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL); | |
1006 | types[rust_primitive_str] = rust_slice_type ("&str", tem, | |
1007 | types[rust_primitive_usize]); | |
1008 | ||
1009 | lai->primitive_type_vector = types; | |
1010 | lai->bool_type_default = types[rust_primitive_bool]; | |
1011 | lai->string_char_type = types[rust_primitive_u8]; | |
1012 | } | |
1013 | ||
1014 | \f | |
1015 | ||
1016 | /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */ | |
1017 | ||
1018 | static struct value * | |
1019 | rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside) | |
1020 | { | |
1021 | int i; | |
1022 | int num_args = exp->elts[*pos + 1].longconst; | |
1023 | const char *method; | |
c44af4eb | 1024 | struct value *function, *result, *arg0; |
c44af4eb TT |
1025 | struct type *type, *fn_type; |
1026 | const struct block *block; | |
1027 | struct block_symbol sym; | |
1028 | ||
1029 | /* For an ordinary function call we can simply defer to the | |
1030 | generic implementation. */ | |
1031 | if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT) | |
1032 | return evaluate_subexp_standard (NULL, exp, pos, noside); | |
1033 | ||
1034 | /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */ | |
1035 | *pos += 4; | |
1036 | method = &exp->elts[*pos + 1].string; | |
1037 | *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1); | |
1038 | ||
1039 | /* Evaluate the argument to STRUCTOP_STRUCT, then find its | |
1040 | type in order to look up the method. */ | |
1041 | arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1042 | ||
1043 | if (noside == EVAL_SKIP) | |
1044 | { | |
1045 | for (i = 0; i < num_args; ++i) | |
1046 | evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1047 | return arg0; | |
1048 | } | |
1049 | ||
ab8b80a8 | 1050 | std::vector<struct value *> args (num_args + 1); |
c44af4eb TT |
1051 | args[0] = arg0; |
1052 | ||
1053 | /* We don't yet implement real Deref semantics. */ | |
1054 | while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR) | |
1055 | args[0] = value_ind (args[0]); | |
1056 | ||
1057 | type = value_type (args[0]); | |
1058 | if ((TYPE_CODE (type) != TYPE_CODE_STRUCT | |
1059 | && TYPE_CODE (type) != TYPE_CODE_UNION | |
1060 | && TYPE_CODE (type) != TYPE_CODE_ENUM) | |
1061 | || rust_tuple_type_p (type)) | |
1062 | error (_("Method calls only supported on struct or enum types")); | |
1063 | if (TYPE_TAG_NAME (type) == NULL) | |
1064 | error (_("Method call on nameless type")); | |
1065 | ||
ab8b80a8 | 1066 | std::string name = std::string (TYPE_TAG_NAME (type)) + "::" + method; |
c44af4eb TT |
1067 | |
1068 | block = get_selected_block (0); | |
ab8b80a8 | 1069 | sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL); |
c44af4eb | 1070 | if (sym.symbol == NULL) |
ab8b80a8 | 1071 | error (_("Could not find function named '%s'"), name.c_str ()); |
c44af4eb TT |
1072 | |
1073 | fn_type = SYMBOL_TYPE (sym.symbol); | |
1074 | if (TYPE_NFIELDS (fn_type) == 0) | |
ab8b80a8 | 1075 | error (_("Function '%s' takes no arguments"), name.c_str ()); |
c44af4eb TT |
1076 | |
1077 | if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR) | |
1078 | args[0] = value_addr (args[0]); | |
1079 | ||
1080 | function = address_of_variable (sym.symbol, block); | |
1081 | ||
1082 | for (i = 0; i < num_args; ++i) | |
1083 | args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1084 | ||
1085 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1086 | result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval); | |
1087 | else | |
7022349d | 1088 | result = call_function_by_hand (function, NULL, num_args + 1, args.data ()); |
c44af4eb TT |
1089 | return result; |
1090 | } | |
1091 | ||
01739a3b | 1092 | /* A helper for rust_evaluate_subexp that handles OP_RANGE. */ |
c44af4eb TT |
1093 | |
1094 | static struct value * | |
1095 | rust_range (struct expression *exp, int *pos, enum noside noside) | |
1096 | { | |
01739a3b | 1097 | enum range_type kind; |
c44af4eb TT |
1098 | struct value *low = NULL, *high = NULL; |
1099 | struct value *addrval, *result; | |
1100 | CORE_ADDR addr; | |
1101 | struct type *range_type; | |
1102 | struct type *index_type; | |
1103 | struct type *temp_type; | |
1104 | const char *name; | |
1105 | ||
01739a3b | 1106 | kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst); |
c44af4eb TT |
1107 | *pos += 3; |
1108 | ||
6873858b TT |
1109 | if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT |
1110 | || kind == NONE_BOUND_DEFAULT_EXCLUSIVE) | |
c44af4eb | 1111 | low = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
6873858b TT |
1112 | if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE |
1113 | || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE) | |
c44af4eb | 1114 | high = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
6873858b | 1115 | bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT); |
c44af4eb TT |
1116 | |
1117 | if (noside == EVAL_SKIP) | |
1118 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); | |
1119 | ||
1120 | if (low == NULL) | |
1121 | { | |
1122 | if (high == NULL) | |
1123 | { | |
1124 | index_type = NULL; | |
1125 | name = "std::ops::RangeFull"; | |
1126 | } | |
1127 | else | |
1128 | { | |
1129 | index_type = value_type (high); | |
6873858b TT |
1130 | name = (inclusive |
1131 | ? "std::ops::RangeToInclusive" : "std::ops::RangeTo"); | |
c44af4eb TT |
1132 | } |
1133 | } | |
1134 | else | |
1135 | { | |
1136 | if (high == NULL) | |
1137 | { | |
1138 | index_type = value_type (low); | |
1139 | name = "std::ops::RangeFrom"; | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | if (!types_equal (value_type (low), value_type (high))) | |
1144 | error (_("Range expression with different types")); | |
1145 | index_type = value_type (low); | |
6873858b | 1146 | name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range"; |
c44af4eb TT |
1147 | } |
1148 | } | |
1149 | ||
1150 | /* If we don't have an index type, just allocate this on the | |
1151 | arch. Here any type will do. */ | |
1152 | temp_type = (index_type == NULL | |
1153 | ? language_bool_type (exp->language_defn, exp->gdbarch) | |
1154 | : index_type); | |
1155 | /* It would be nicer to cache the range type. */ | |
1156 | range_type = rust_composite_type (temp_type, name, | |
1157 | low == NULL ? NULL : "start", index_type, | |
1158 | high == NULL ? NULL : "end", index_type); | |
1159 | ||
1160 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1161 | return value_zero (range_type, lval_memory); | |
1162 | ||
1163 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type)); | |
1164 | addr = value_as_long (addrval); | |
1165 | result = value_at_lazy (range_type, addr); | |
1166 | ||
1167 | if (low != NULL) | |
1168 | { | |
1169 | struct value *start = value_struct_elt (&result, NULL, "start", NULL, | |
1170 | "range"); | |
1171 | ||
1172 | value_assign (start, low); | |
1173 | } | |
1174 | ||
1175 | if (high != NULL) | |
1176 | { | |
1177 | struct value *end = value_struct_elt (&result, NULL, "end", NULL, | |
1178 | "range"); | |
1179 | ||
1180 | value_assign (end, high); | |
1181 | } | |
1182 | ||
1183 | result = value_at_lazy (range_type, addr); | |
1184 | return result; | |
1185 | } | |
1186 | ||
1187 | /* A helper function to compute the range and kind given a range | |
1188 | value. TYPE is the type of the range value. RANGE is the range | |
1189 | value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH | |
1190 | parameters might be filled in, or might not be, depending on the | |
1191 | kind of range this is. KIND will always be set to the appropriate | |
1192 | value describing the kind of range, and this can be used to | |
1193 | determine whether LOW or HIGH are valid. */ | |
1194 | ||
1195 | static void | |
1196 | rust_compute_range (struct type *type, struct value *range, | |
1197 | LONGEST *low, LONGEST *high, | |
01739a3b | 1198 | enum range_type *kind) |
c44af4eb TT |
1199 | { |
1200 | int i; | |
1201 | ||
1202 | *low = 0; | |
1203 | *high = 0; | |
1204 | *kind = BOTH_BOUND_DEFAULT; | |
1205 | ||
1206 | if (TYPE_NFIELDS (type) == 0) | |
1207 | return; | |
1208 | ||
1209 | i = 0; | |
1210 | if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0) | |
1211 | { | |
1212 | *kind = HIGH_BOUND_DEFAULT; | |
1213 | *low = value_as_long (value_field (range, 0)); | |
1214 | ++i; | |
1215 | } | |
1216 | if (TYPE_NFIELDS (type) > i | |
1217 | && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0) | |
1218 | { | |
1219 | *kind = (*kind == BOTH_BOUND_DEFAULT | |
1220 | ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT); | |
1221 | *high = value_as_long (value_field (range, i)); | |
6873858b TT |
1222 | |
1223 | if (rust_inclusive_range_type_p (type)) | |
1224 | ++*high; | |
c44af4eb TT |
1225 | } |
1226 | } | |
1227 | ||
1228 | /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */ | |
1229 | ||
1230 | static struct value * | |
1231 | rust_subscript (struct expression *exp, int *pos, enum noside noside, | |
1232 | int for_addr) | |
1233 | { | |
1234 | struct value *lhs, *rhs, *result; | |
1235 | struct type *rhstype; | |
45f4ed92 | 1236 | LONGEST low, high_bound; |
c44af4eb | 1237 | /* Initialized to appease the compiler. */ |
01739a3b | 1238 | enum range_type kind = BOTH_BOUND_DEFAULT; |
45f4ed92 | 1239 | LONGEST high = 0; |
c44af4eb TT |
1240 | int want_slice = 0; |
1241 | ||
1242 | ++*pos; | |
1243 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1244 | rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1245 | ||
1246 | if (noside == EVAL_SKIP) | |
1247 | return lhs; | |
1248 | ||
1249 | rhstype = check_typedef (value_type (rhs)); | |
1250 | if (rust_range_type_p (rhstype)) | |
1251 | { | |
1252 | if (!for_addr) | |
1253 | error (_("Can't take slice of array without '&'")); | |
1254 | rust_compute_range (rhstype, rhs, &low, &high, &kind); | |
1255 | want_slice = 1; | |
1256 | } | |
1257 | else | |
1258 | low = value_as_long (rhs); | |
1259 | ||
b3e3859b | 1260 | struct type *type = check_typedef (value_type (lhs)); |
c44af4eb TT |
1261 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1262 | { | |
b3e3859b TT |
1263 | struct type *base_type = nullptr; |
1264 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
1265 | base_type = TYPE_TARGET_TYPE (type); | |
1266 | else if (rust_slice_type_p (type)) | |
1267 | { | |
1268 | for (int i = 0; i < TYPE_NFIELDS (type); ++i) | |
1269 | { | |
1270 | if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0) | |
1271 | { | |
1272 | base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i)); | |
1273 | break; | |
1274 | } | |
1275 | } | |
1276 | if (base_type == nullptr) | |
1277 | error (_("Could not find 'data_ptr' in slice type")); | |
1278 | } | |
1279 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
1280 | base_type = TYPE_TARGET_TYPE (type); | |
1281 | else | |
1282 | error (_("Cannot subscript non-array type")); | |
1283 | ||
1284 | struct type *new_type; | |
1285 | if (want_slice) | |
1286 | { | |
1287 | if (rust_slice_type_p (type)) | |
1288 | new_type = type; | |
1289 | else | |
1290 | { | |
1291 | struct type *usize | |
1292 | = language_lookup_primitive_type (exp->language_defn, | |
1293 | exp->gdbarch, | |
1294 | "usize"); | |
1295 | new_type = rust_slice_type ("&[*gdb*]", base_type, usize); | |
1296 | } | |
1297 | } | |
1298 | else | |
1299 | new_type = base_type; | |
c44af4eb | 1300 | |
b3e3859b | 1301 | return value_zero (new_type, VALUE_LVAL (lhs)); |
c44af4eb TT |
1302 | } |
1303 | else | |
1304 | { | |
1305 | LONGEST low_bound; | |
1306 | struct value *base; | |
c44af4eb TT |
1307 | |
1308 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
1309 | { | |
1310 | base = lhs; | |
1311 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
1312 | error (_("Can't compute array bounds")); | |
1313 | if (low_bound != 0) | |
1314 | error (_("Found array with non-zero lower bound")); | |
1315 | ++high_bound; | |
1316 | } | |
1317 | else if (rust_slice_type_p (type)) | |
1318 | { | |
1319 | struct value *len; | |
1320 | ||
1321 | base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice"); | |
1322 | len = value_struct_elt (&lhs, NULL, "length", NULL, "slice"); | |
1323 | low_bound = 0; | |
1324 | high_bound = value_as_long (len); | |
1325 | } | |
42d94011 MG |
1326 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) |
1327 | { | |
1328 | base = lhs; | |
1329 | low_bound = 0; | |
1330 | high_bound = LONGEST_MAX; | |
1331 | } | |
c44af4eb TT |
1332 | else |
1333 | error (_("Cannot subscript non-array type")); | |
1334 | ||
1335 | if (want_slice | |
1336 | && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT)) | |
1337 | low = low_bound; | |
1338 | if (low < 0) | |
1339 | error (_("Index less than zero")); | |
1340 | if (low > high_bound) | |
1341 | error (_("Index greater than length")); | |
1342 | ||
1343 | result = value_subscript (base, low); | |
1344 | } | |
1345 | ||
1346 | if (for_addr) | |
1347 | { | |
1348 | if (want_slice) | |
1349 | { | |
1350 | struct type *usize, *slice; | |
1351 | CORE_ADDR addr; | |
1352 | struct value *addrval, *tem; | |
1353 | ||
1354 | if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT) | |
1355 | high = high_bound; | |
1356 | if (high < 0) | |
1357 | error (_("High index less than zero")); | |
1358 | if (low > high) | |
1359 | error (_("Low index greater than high index")); | |
1360 | if (high > high_bound) | |
1361 | error (_("High index greater than length")); | |
1362 | ||
1363 | usize = language_lookup_primitive_type (exp->language_defn, | |
1364 | exp->gdbarch, | |
1365 | "usize"); | |
45320ffa TT |
1366 | const char *new_name = ((type != nullptr |
1367 | && rust_slice_type_p (type)) | |
1368 | ? TYPE_NAME (type) : "&[*gdb*]"); | |
1369 | ||
1370 | slice = rust_slice_type (new_name, value_type (result), usize); | |
c44af4eb TT |
1371 | |
1372 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice)); | |
1373 | addr = value_as_long (addrval); | |
1374 | tem = value_at_lazy (slice, addr); | |
1375 | ||
1376 | value_assign (value_field (tem, 0), value_addr (result)); | |
1377 | value_assign (value_field (tem, 1), | |
1378 | value_from_longest (usize, high - low)); | |
1379 | ||
1380 | result = value_at_lazy (slice, addr); | |
1381 | } | |
1382 | else | |
1383 | result = value_addr (result); | |
1384 | } | |
1385 | ||
1386 | return result; | |
1387 | } | |
1388 | ||
1389 | /* evaluate_exp implementation for Rust. */ | |
1390 | ||
1391 | static struct value * | |
1392 | rust_evaluate_subexp (struct type *expect_type, struct expression *exp, | |
1393 | int *pos, enum noside noside) | |
1394 | { | |
1395 | struct value *result; | |
1396 | ||
1397 | switch (exp->elts[*pos].opcode) | |
1398 | { | |
71a3c369 TT |
1399 | case UNOP_IND: |
1400 | { | |
1401 | if (noside != EVAL_NORMAL) | |
1402 | result = evaluate_subexp_standard (expect_type, exp, pos, noside); | |
1403 | else | |
1404 | { | |
1405 | ++*pos; | |
1406 | struct value *value = evaluate_subexp (expect_type, exp, pos, | |
1407 | noside); | |
1408 | ||
1409 | struct value *trait_ptr = rust_get_trait_object_pointer (value); | |
1410 | if (trait_ptr != NULL) | |
1411 | value = trait_ptr; | |
1412 | ||
1413 | result = value_ind (value); | |
1414 | } | |
1415 | } | |
1416 | break; | |
1417 | ||
c44af4eb TT |
1418 | case UNOP_COMPLEMENT: |
1419 | { | |
1420 | struct value *value; | |
1421 | ||
1422 | ++*pos; | |
1423 | value = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1424 | if (noside == EVAL_SKIP) | |
1425 | { | |
1426 | /* Preserving the type is enough. */ | |
1427 | return value; | |
1428 | } | |
1429 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL) | |
1430 | result = value_from_longest (value_type (value), | |
1431 | value_logical_not (value)); | |
1432 | else | |
1433 | result = value_complement (value); | |
1434 | } | |
1435 | break; | |
1436 | ||
1437 | case BINOP_SUBSCRIPT: | |
1438 | result = rust_subscript (exp, pos, noside, 0); | |
1439 | break; | |
1440 | ||
1441 | case OP_FUNCALL: | |
1442 | result = rust_evaluate_funcall (exp, pos, noside); | |
1443 | break; | |
1444 | ||
1445 | case OP_AGGREGATE: | |
1446 | { | |
1447 | int pc = (*pos)++; | |
1448 | struct type *type = exp->elts[pc + 1].type; | |
1449 | int arglen = longest_to_int (exp->elts[pc + 2].longconst); | |
1450 | int i; | |
1451 | CORE_ADDR addr = 0; | |
1452 | struct value *addrval = NULL; | |
1453 | ||
1454 | *pos += 3; | |
1455 | ||
1456 | if (noside == EVAL_NORMAL) | |
1457 | { | |
1458 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type)); | |
1459 | addr = value_as_long (addrval); | |
1460 | result = value_at_lazy (type, addr); | |
1461 | } | |
1462 | ||
1463 | if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS) | |
1464 | { | |
1465 | struct value *init; | |
1466 | ||
1467 | ++*pos; | |
1468 | init = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1469 | if (noside == EVAL_NORMAL) | |
1470 | { | |
1471 | /* This isn't quite right but will do for the time | |
1472 | being, seeing that we can't implement the Copy | |
1473 | trait anyway. */ | |
1474 | value_assign (result, init); | |
1475 | } | |
1476 | ||
1477 | --arglen; | |
1478 | } | |
1479 | ||
1480 | gdb_assert (arglen % 2 == 0); | |
1481 | for (i = 0; i < arglen; i += 2) | |
1482 | { | |
1483 | int len; | |
1484 | const char *fieldname; | |
1485 | struct value *value, *field; | |
1486 | ||
1487 | gdb_assert (exp->elts[*pos].opcode == OP_NAME); | |
1488 | ++*pos; | |
1489 | len = longest_to_int (exp->elts[*pos].longconst); | |
1490 | ++*pos; | |
1491 | fieldname = &exp->elts[*pos].string; | |
1492 | *pos += 2 + BYTES_TO_EXP_ELEM (len + 1); | |
1493 | ||
1494 | value = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1495 | if (noside == EVAL_NORMAL) | |
1496 | { | |
1497 | field = value_struct_elt (&result, NULL, fieldname, NULL, | |
1498 | "structure"); | |
1499 | value_assign (field, value); | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | if (noside == EVAL_SKIP) | |
1504 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, | |
1505 | 1); | |
1506 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1507 | result = allocate_value (type); | |
1508 | else | |
1509 | result = value_at_lazy (type, addr); | |
1510 | } | |
1511 | break; | |
1512 | ||
1513 | case OP_RUST_ARRAY: | |
1514 | { | |
1515 | int pc = (*pos)++; | |
1516 | int copies; | |
1517 | struct value *elt; | |
1518 | struct value *ncopies; | |
1519 | ||
1520 | elt = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1521 | ncopies = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1522 | copies = value_as_long (ncopies); | |
1523 | if (copies < 0) | |
1524 | error (_("Array with negative number of elements")); | |
1525 | ||
1526 | if (noside == EVAL_NORMAL) | |
1527 | { | |
c44af4eb | 1528 | int i; |
ab8b80a8 | 1529 | std::vector<struct value *> eltvec (copies); |
c44af4eb TT |
1530 | |
1531 | for (i = 0; i < copies; ++i) | |
1532 | eltvec[i] = elt; | |
ab8b80a8 | 1533 | result = value_array (0, copies - 1, eltvec.data ()); |
c44af4eb TT |
1534 | } |
1535 | else | |
1536 | { | |
1537 | struct type *arraytype | |
1538 | = lookup_array_range_type (value_type (elt), 0, copies - 1); | |
1539 | result = allocate_value (arraytype); | |
1540 | } | |
1541 | } | |
1542 | break; | |
1543 | ||
1544 | case STRUCTOP_ANONYMOUS: | |
1545 | { | |
1546 | /* Anonymous field access, i.e. foo.1. */ | |
1547 | struct value *lhs; | |
1548 | int pc, field_number, nfields; | |
1549 | struct type *type, *variant_type; | |
c44af4eb TT |
1550 | |
1551 | pc = (*pos)++; | |
1552 | field_number = longest_to_int (exp->elts[pc + 1].longconst); | |
1553 | (*pos) += 2; | |
1554 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1555 | ||
1556 | type = value_type (lhs); | |
c9317f21 | 1557 | |
c9317f21 TT |
1558 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
1559 | { | |
1560 | struct type *outer_type = NULL; | |
1561 | ||
1562 | if (rust_enum_p (type)) | |
c44af4eb | 1563 | { |
c9317f21 TT |
1564 | const gdb_byte *valaddr = value_contents (lhs); |
1565 | struct field *variant_field = rust_enum_variant (type, valaddr); | |
1566 | ||
1567 | struct value *union_value = value_primitive_field (lhs, 0, 0, | |
1568 | type); | |
1569 | ||
1570 | int fieldno = (variant_field | |
1571 | - &TYPE_FIELD (value_type (union_value), 0)); | |
1572 | lhs = value_primitive_field (union_value, 0, fieldno, | |
1573 | value_type (union_value)); | |
1574 | outer_type = type; | |
1575 | type = value_type (lhs); | |
c44af4eb TT |
1576 | } |
1577 | ||
c44af4eb | 1578 | /* Tuples and tuple structs */ |
c9317f21 | 1579 | nfields = TYPE_NFIELDS (type); |
c44af4eb TT |
1580 | |
1581 | if (field_number >= nfields || field_number < 0) | |
c9317f21 TT |
1582 | { |
1583 | if (outer_type != NULL) | |
1584 | error(_("Cannot access field %d of variant %s::%s, " | |
1585 | "there are only %d fields"), | |
1586 | field_number, TYPE_TAG_NAME (outer_type), | |
1587 | rust_last_path_segment (TYPE_TAG_NAME (type)), | |
1588 | nfields); | |
1589 | else | |
1590 | error(_("Cannot access field %d of %s, " | |
1591 | "there are only %d fields"), | |
1592 | field_number, TYPE_TAG_NAME (type), nfields); | |
1593 | } | |
c44af4eb TT |
1594 | |
1595 | /* Tuples are tuple structs too. */ | |
1596 | if (!rust_tuple_struct_type_p (type)) | |
c9317f21 TT |
1597 | { |
1598 | if (outer_type != NULL) | |
1599 | error(_("Variant %s::%s is not a tuple variant"), | |
1600 | TYPE_TAG_NAME (outer_type), | |
1601 | rust_last_path_segment (TYPE_TAG_NAME (type))); | |
1602 | else | |
1603 | error(_("Attempting to access anonymous field %d " | |
1604 | "of %s, which is not a tuple, tuple struct, or " | |
1605 | "tuple-like variant"), | |
1606 | field_number, TYPE_TAG_NAME (type)); | |
1607 | } | |
c44af4eb TT |
1608 | |
1609 | result = value_primitive_field (lhs, 0, field_number, type); | |
1610 | } | |
1611 | else | |
1612 | error(_("Anonymous field access is only allowed on tuples, \ | |
1613 | tuple structs, and tuple-like enum variants")); | |
1614 | } | |
1615 | break; | |
1616 | ||
1617 | case STRUCTOP_STRUCT: | |
1618 | { | |
6830f270 | 1619 | struct value *lhs; |
c44af4eb TT |
1620 | struct type *type; |
1621 | int tem, pc; | |
1622 | ||
1623 | pc = (*pos)++; | |
1624 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
1625 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
1626 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1627 | ||
6830f270 | 1628 | const char *field_name = &exp->elts[pc + 2].string; |
c44af4eb | 1629 | type = value_type (lhs); |
c9317f21 | 1630 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type)) |
c44af4eb | 1631 | { |
c9317f21 TT |
1632 | const gdb_byte *valaddr = value_contents (lhs); |
1633 | struct field *variant_field = rust_enum_variant (type, valaddr); | |
1634 | ||
1635 | struct value *union_value = value_primitive_field (lhs, 0, 0, | |
1636 | type); | |
1637 | ||
1638 | int fieldno = (variant_field | |
1639 | - &TYPE_FIELD (value_type (union_value), 0)); | |
1640 | lhs = value_primitive_field (union_value, 0, fieldno, | |
1641 | value_type (union_value)); | |
1642 | ||
1643 | struct type *outer_type = type; | |
1644 | type = value_type (lhs); | |
1645 | if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type)) | |
1646 | error (_("Attempting to access named field foo of tuple " | |
1647 | "variant %s::%s, which has only anonymous fields"), | |
1648 | TYPE_TAG_NAME (outer_type), | |
1649 | rust_last_path_segment (TYPE_NAME (type))); | |
1650 | ||
1651 | TRY | |
c44af4eb | 1652 | { |
c9317f21 TT |
1653 | result = value_struct_elt (&lhs, NULL, field_name, |
1654 | NULL, "structure"); | |
c44af4eb | 1655 | } |
c9317f21 TT |
1656 | CATCH (except, RETURN_MASK_ERROR) |
1657 | { | |
1658 | error (_("Could not find field %s of struct variant %s::%s"), | |
1659 | field_name, TYPE_TAG_NAME (outer_type), | |
1660 | rust_last_path_segment (TYPE_NAME (type))); | |
1661 | } | |
1662 | END_CATCH | |
c44af4eb TT |
1663 | } |
1664 | else | |
c9317f21 TT |
1665 | result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure"); |
1666 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1667 | result = value_zero (value_type (result), VALUE_LVAL (result)); | |
c44af4eb TT |
1668 | } |
1669 | break; | |
1670 | ||
01739a3b | 1671 | case OP_RANGE: |
c44af4eb TT |
1672 | result = rust_range (exp, pos, noside); |
1673 | break; | |
1674 | ||
1675 | case UNOP_ADDR: | |
1676 | /* We might have &array[range], in which case we need to make a | |
1677 | slice. */ | |
1678 | if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT) | |
1679 | { | |
1680 | ++*pos; | |
1681 | result = rust_subscript (exp, pos, noside, 1); | |
1682 | break; | |
1683 | } | |
1684 | /* Fall through. */ | |
1685 | default: | |
1686 | result = evaluate_subexp_standard (expect_type, exp, pos, noside); | |
1687 | break; | |
1688 | } | |
1689 | ||
1690 | return result; | |
1691 | } | |
1692 | ||
1693 | /* operator_length implementation for Rust. */ | |
1694 | ||
1695 | static void | |
1696 | rust_operator_length (const struct expression *exp, int pc, int *oplenp, | |
1697 | int *argsp) | |
1698 | { | |
1699 | int oplen = 1; | |
1700 | int args = 0; | |
1701 | ||
1702 | switch (exp->elts[pc - 1].opcode) | |
1703 | { | |
1704 | case OP_AGGREGATE: | |
1705 | /* We handle aggregate as a type and argument count. The first | |
1706 | argument might be OP_OTHERS. After that the arguments | |
1707 | alternate: first an OP_NAME, then an expression. */ | |
1708 | oplen = 4; | |
1709 | args = longest_to_int (exp->elts[pc - 2].longconst); | |
1710 | break; | |
1711 | ||
1712 | case OP_OTHERS: | |
1713 | oplen = 1; | |
1714 | args = 1; | |
1715 | break; | |
1716 | ||
1717 | case STRUCTOP_ANONYMOUS: | |
1718 | oplen = 3; | |
1719 | args = 1; | |
1720 | break; | |
1721 | ||
1722 | case OP_RUST_ARRAY: | |
1723 | oplen = 1; | |
1724 | args = 2; | |
1725 | break; | |
1726 | ||
1727 | default: | |
1728 | operator_length_standard (exp, pc, oplenp, argsp); | |
1729 | return; | |
1730 | } | |
1731 | ||
1732 | *oplenp = oplen; | |
1733 | *argsp = args; | |
1734 | } | |
1735 | ||
1736 | /* op_name implementation for Rust. */ | |
1737 | ||
a121b7c1 | 1738 | static const char * |
c44af4eb TT |
1739 | rust_op_name (enum exp_opcode opcode) |
1740 | { | |
1741 | switch (opcode) | |
1742 | { | |
1743 | case OP_AGGREGATE: | |
1744 | return "OP_AGGREGATE"; | |
1745 | case OP_OTHERS: | |
1746 | return "OP_OTHERS"; | |
1747 | default: | |
1748 | return op_name_standard (opcode); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | /* dump_subexp_body implementation for Rust. */ | |
1753 | ||
1754 | static int | |
1755 | rust_dump_subexp_body (struct expression *exp, struct ui_file *stream, | |
1756 | int elt) | |
1757 | { | |
1758 | switch (exp->elts[elt].opcode) | |
1759 | { | |
1760 | case OP_AGGREGATE: | |
1761 | { | |
1762 | int length = longest_to_int (exp->elts[elt + 2].longconst); | |
1763 | int i; | |
1764 | ||
1765 | fprintf_filtered (stream, "Type @"); | |
1766 | gdb_print_host_address (exp->elts[elt + 1].type, stream); | |
1767 | fprintf_filtered (stream, " ("); | |
1768 | type_print (exp->elts[elt + 1].type, NULL, stream, 0); | |
1769 | fprintf_filtered (stream, "), length %d", length); | |
1770 | ||
1771 | elt += 4; | |
1772 | for (i = 0; i < length; ++i) | |
1773 | elt = dump_subexp (exp, stream, elt); | |
1774 | } | |
1775 | break; | |
1776 | ||
1777 | case OP_STRING: | |
1778 | case OP_NAME: | |
1779 | { | |
1780 | LONGEST len = exp->elts[elt + 1].longconst; | |
1781 | ||
1782 | fprintf_filtered (stream, "%s: %s", | |
1783 | (exp->elts[elt].opcode == OP_STRING | |
1784 | ? "string" : "name"), | |
1785 | &exp->elts[elt + 2].string); | |
1786 | elt += 4 + BYTES_TO_EXP_ELEM (len + 1); | |
1787 | } | |
1788 | break; | |
1789 | ||
1790 | case OP_OTHERS: | |
1791 | elt = dump_subexp (exp, stream, elt + 1); | |
1792 | break; | |
1793 | ||
1794 | case STRUCTOP_ANONYMOUS: | |
1795 | { | |
1796 | int field_number; | |
1797 | ||
68f2f2e3 | 1798 | field_number = longest_to_int (exp->elts[elt + 1].longconst); |
c44af4eb TT |
1799 | |
1800 | fprintf_filtered (stream, "Field number: %d", field_number); | |
68f2f2e3 | 1801 | elt = dump_subexp (exp, stream, elt + 3); |
c44af4eb TT |
1802 | } |
1803 | break; | |
1804 | ||
1805 | case OP_RUST_ARRAY: | |
68f2f2e3 | 1806 | ++elt; |
c44af4eb TT |
1807 | break; |
1808 | ||
1809 | default: | |
1810 | elt = dump_subexp_body_standard (exp, stream, elt); | |
1811 | break; | |
1812 | } | |
1813 | ||
1814 | return elt; | |
1815 | } | |
1816 | ||
1817 | /* print_subexp implementation for Rust. */ | |
1818 | ||
1819 | static void | |
1820 | rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream, | |
1821 | enum precedence prec) | |
1822 | { | |
1823 | switch (exp->elts[*pos].opcode) | |
1824 | { | |
1825 | case OP_AGGREGATE: | |
1826 | { | |
1827 | int length = longest_to_int (exp->elts[*pos + 2].longconst); | |
1828 | int i; | |
1829 | ||
1830 | type_print (exp->elts[*pos + 1].type, "", stream, 0); | |
1831 | fputs_filtered (" { ", stream); | |
1832 | ||
1833 | *pos += 4; | |
1834 | for (i = 0; i < length; ++i) | |
1835 | { | |
1836 | rust_print_subexp (exp, pos, stream, prec); | |
1837 | fputs_filtered (", ", stream); | |
1838 | } | |
1839 | fputs_filtered (" }", stream); | |
1840 | } | |
1841 | break; | |
1842 | ||
1843 | case OP_NAME: | |
1844 | { | |
1845 | LONGEST len = exp->elts[*pos + 1].longconst; | |
1846 | ||
1847 | fputs_filtered (&exp->elts[*pos + 2].string, stream); | |
1848 | *pos += 4 + BYTES_TO_EXP_ELEM (len + 1); | |
1849 | } | |
1850 | break; | |
1851 | ||
1852 | case OP_OTHERS: | |
1853 | { | |
1854 | fputs_filtered ("<<others>> (", stream); | |
1855 | ++*pos; | |
1856 | rust_print_subexp (exp, pos, stream, prec); | |
1857 | fputs_filtered (")", stream); | |
1858 | } | |
1859 | break; | |
1860 | ||
1861 | case STRUCTOP_ANONYMOUS: | |
1862 | { | |
1863 | int tem = longest_to_int (exp->elts[*pos + 1].longconst); | |
1864 | ||
1865 | (*pos) += 3; | |
1866 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
1867 | fprintf_filtered (stream, ".%d", tem); | |
1868 | } | |
256afbc2 | 1869 | break; |
c44af4eb TT |
1870 | |
1871 | case OP_RUST_ARRAY: | |
1872 | ++*pos; | |
1873 | fprintf_filtered (stream, "["); | |
1874 | rust_print_subexp (exp, pos, stream, prec); | |
1875 | fprintf_filtered (stream, "; "); | |
1876 | rust_print_subexp (exp, pos, stream, prec); | |
1877 | fprintf_filtered (stream, "]"); | |
1878 | break; | |
1879 | ||
1880 | default: | |
1881 | print_subexp_standard (exp, pos, stream, prec); | |
1882 | break; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | /* operator_check implementation for Rust. */ | |
1887 | ||
1888 | static int | |
1889 | rust_operator_check (struct expression *exp, int pos, | |
1890 | int (*objfile_func) (struct objfile *objfile, | |
1891 | void *data), | |
1892 | void *data) | |
1893 | { | |
1894 | switch (exp->elts[pos].opcode) | |
1895 | { | |
1896 | case OP_AGGREGATE: | |
1897 | { | |
1898 | struct type *type = exp->elts[pos + 1].type; | |
1899 | struct objfile *objfile = TYPE_OBJFILE (type); | |
1900 | ||
1901 | if (objfile != NULL && (*objfile_func) (objfile, data)) | |
1902 | return 1; | |
1903 | } | |
1904 | break; | |
1905 | ||
1906 | case OP_OTHERS: | |
1907 | case OP_NAME: | |
1908 | case OP_RUST_ARRAY: | |
1909 | break; | |
1910 | ||
1911 | default: | |
1912 | return operator_check_standard (exp, pos, objfile_func, data); | |
1913 | } | |
1914 | ||
1915 | return 0; | |
1916 | } | |
1917 | ||
1918 | \f | |
1919 | ||
1920 | /* Implementation of la_lookup_symbol_nonlocal for Rust. */ | |
1921 | ||
1922 | static struct block_symbol | |
1923 | rust_lookup_symbol_nonlocal (const struct language_defn *langdef, | |
1924 | const char *name, | |
1925 | const struct block *block, | |
1926 | const domain_enum domain) | |
1927 | { | |
1928 | struct block_symbol result = {NULL, NULL}; | |
1929 | ||
1930 | if (symbol_lookup_debug) | |
1931 | { | |
1932 | fprintf_unfiltered (gdb_stdlog, | |
1933 | "rust_lookup_symbol_non_local" | |
1934 | " (%s, %s (scope %s), %s)\n", | |
1935 | name, host_address_to_string (block), | |
1936 | block_scope (block), domain_name (domain)); | |
1937 | } | |
1938 | ||
1939 | /* Look up bare names in the block's scope. */ | |
fcfcc376 | 1940 | std::string scopedname; |
c44af4eb TT |
1941 | if (name[cp_find_first_component (name)] == '\0') |
1942 | { | |
1943 | const char *scope = block_scope (block); | |
1944 | ||
1945 | if (scope[0] != '\0') | |
1946 | { | |
fcfcc376 TT |
1947 | scopedname = std::string (scope) + "::" + name; |
1948 | name = scopedname.c_str (); | |
c44af4eb | 1949 | } |
fcfcc376 TT |
1950 | else |
1951 | name = NULL; | |
1952 | } | |
1953 | ||
1954 | if (name != NULL) | |
1955 | { | |
1956 | result = lookup_symbol_in_static_block (name, block, domain); | |
1957 | if (result.symbol == NULL) | |
1958 | result = lookup_global_symbol (name, block, domain); | |
c44af4eb TT |
1959 | } |
1960 | return result; | |
1961 | } | |
1962 | ||
1963 | \f | |
1964 | ||
8b302db8 TT |
1965 | /* la_sniff_from_mangled_name for Rust. */ |
1966 | ||
1967 | static int | |
1968 | rust_sniff_from_mangled_name (const char *mangled, char **demangled) | |
1969 | { | |
1970 | *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); | |
1971 | return *demangled != NULL; | |
1972 | } | |
1973 | ||
1974 | \f | |
1975 | ||
43cc5389 TT |
1976 | /* la_watch_location_expression for Rust. */ |
1977 | ||
1978 | static gdb::unique_xmalloc_ptr<char> | |
1979 | rust_watch_location_expression (struct type *type, CORE_ADDR addr) | |
1980 | { | |
1981 | type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type))); | |
1982 | std::string name = type_to_string (type); | |
1983 | return gdb::unique_xmalloc_ptr<char> | |
1984 | (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr), | |
1985 | name.c_str ())); | |
1986 | } | |
1987 | ||
1988 | \f | |
1989 | ||
c44af4eb TT |
1990 | static const struct exp_descriptor exp_descriptor_rust = |
1991 | { | |
1992 | rust_print_subexp, | |
1993 | rust_operator_length, | |
1994 | rust_operator_check, | |
1995 | rust_op_name, | |
1996 | rust_dump_subexp_body, | |
1997 | rust_evaluate_subexp | |
1998 | }; | |
1999 | ||
56618e20 TT |
2000 | static const char *rust_extensions[] = |
2001 | { | |
2002 | ".rs", NULL | |
2003 | }; | |
2004 | ||
47e77640 | 2005 | extern const struct language_defn rust_language_defn = |
c44af4eb TT |
2006 | { |
2007 | "rust", | |
2008 | "Rust", | |
2009 | language_rust, | |
2010 | range_check_on, | |
2011 | case_sensitive_on, | |
2012 | array_row_major, | |
2013 | macro_expansion_no, | |
56618e20 | 2014 | rust_extensions, |
c44af4eb TT |
2015 | &exp_descriptor_rust, |
2016 | rust_parse, | |
2017 | rustyyerror, | |
2018 | null_post_parser, | |
2019 | rust_printchar, /* Print a character constant */ | |
2020 | rust_printstr, /* Function to print string constant */ | |
2021 | rust_emitchar, /* Print a single char */ | |
2022 | rust_print_type, /* Print a type using appropriate syntax */ | |
2023 | rust_print_typedef, /* Print a typedef using appropriate syntax */ | |
2024 | rust_val_print, /* Print a value using appropriate syntax */ | |
2025 | c_value_print, /* Print a top-level value */ | |
2026 | default_read_var_value, /* la_read_var_value */ | |
2027 | NULL, /* Language specific skip_trampoline */ | |
2028 | NULL, /* name_of_this */ | |
59cc4834 | 2029 | false, /* la_store_sym_names_in_linkage_form_p */ |
c44af4eb TT |
2030 | rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
2031 | basic_lookup_transparent_type,/* lookup_transparent_type */ | |
2032 | gdb_demangle, /* Language specific symbol demangler */ | |
8b302db8 | 2033 | rust_sniff_from_mangled_name, |
c44af4eb TT |
2034 | NULL, /* Language specific |
2035 | class_name_from_physname */ | |
2036 | c_op_print_tab, /* expression operators for printing */ | |
2037 | 1, /* c-style arrays */ | |
2038 | 0, /* String lower bound */ | |
2039 | default_word_break_characters, | |
eb3ff9a5 | 2040 | default_collect_symbol_completion_matches, |
c44af4eb TT |
2041 | rust_language_arch_info, |
2042 | default_print_array_index, | |
2043 | default_pass_by_reference, | |
2044 | c_get_string, | |
43cc5389 | 2045 | rust_watch_location_expression, |
b5ec771e | 2046 | NULL, /* la_get_symbol_name_matcher */ |
c44af4eb | 2047 | iterate_over_symbols, |
5ffa0793 | 2048 | default_search_name_hash, |
c44af4eb TT |
2049 | &default_varobj_ops, |
2050 | NULL, | |
2051 | NULL, | |
2052 | LANG_MAGIC | |
2053 | }; |