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 | |
e86ca25f TT |
98 | && TYPE_NAME (type) != NULL |
99 | && TYPE_NAME (type)[0] == '('); | |
c44af4eb TT |
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 | |
e86ca25f TT |
146 | && TYPE_NAME (type) != NULL |
147 | && (strncmp (TYPE_NAME (type), "&[", 2) == 0 | |
148 | || strcmp (TYPE_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 | |
e86ca25f TT |
160 | || TYPE_NAME (type) == NULL |
161 | || strstr (TYPE_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 | { | |
e86ca25f TT |
190 | return (strstr (TYPE_NAME (type), "::RangeInclusive") != NULL |
191 | || strstr (TYPE_NAME (type), "::RangeToInclusive") != NULL); | |
6873858b TT |
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 | { | |
e86ca25f TT |
356 | if (TYPE_NAME (type) != NULL) |
357 | fprintf_filtered (stream, "%s", TYPE_NAME (type)); | |
b96645f1 MG |
358 | |
359 | if (TYPE_NFIELDS (type) == 0) | |
360 | return; | |
361 | ||
e86ca25f | 362 | if (TYPE_NAME (type) != NULL) |
b96645f1 MG |
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 | { | |
e86ca25f | 631 | fputs_filtered (TYPE_NAME (type), stream); |
b50f188d TT |
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. */ | |
e86ca25f | 641 | const char *tagname = TYPE_NAME (type); |
c9317f21 TT |
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); | |
e86ca25f | 822 | if (TYPE_NAME (type) != NULL) |
c44af4eb | 823 | { |
e86ca25f | 824 | fputs_filtered (TYPE_NAME (type), stream); |
c44af4eb | 825 | fputs_filtered (" ", stream); |
e86ca25f | 826 | len = strlen (TYPE_NAME (type)); |
c44af4eb TT |
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 | |
e86ca25f | 837 | && strncmp (name, TYPE_NAME (type), len) == 0 |
c44af4eb TT |
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; | |
c44af4eb TT |
885 | |
886 | TYPE_NFIELDS (result) = nfields; | |
887 | TYPE_FIELDS (result) | |
888 | = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)); | |
889 | ||
890 | i = 0; | |
891 | bitpos = 0; | |
892 | if (field1 != NULL) | |
893 | { | |
894 | struct field *field = &TYPE_FIELD (result, i); | |
895 | ||
896 | SET_FIELD_BITPOS (*field, bitpos); | |
897 | bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT; | |
898 | ||
899 | FIELD_NAME (*field) = field1; | |
900 | FIELD_TYPE (*field) = type1; | |
901 | ++i; | |
902 | } | |
903 | if (field2 != NULL) | |
904 | { | |
905 | struct field *field = &TYPE_FIELD (result, i); | |
2fff16dd | 906 | unsigned align = type_align (type2); |
c44af4eb TT |
907 | |
908 | if (align != 0) | |
909 | { | |
910 | int delta; | |
911 | ||
912 | align *= TARGET_CHAR_BIT; | |
913 | delta = bitpos % align; | |
914 | if (delta != 0) | |
915 | bitpos += align - delta; | |
916 | } | |
917 | SET_FIELD_BITPOS (*field, bitpos); | |
918 | ||
919 | FIELD_NAME (*field) = field2; | |
920 | FIELD_TYPE (*field) = type2; | |
921 | ++i; | |
922 | } | |
923 | ||
924 | if (i > 0) | |
925 | TYPE_LENGTH (result) | |
926 | = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT + | |
927 | TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1))); | |
928 | return result; | |
929 | } | |
930 | ||
931 | /* See rust-lang.h. */ | |
932 | ||
933 | struct type * | |
934 | rust_slice_type (const char *name, struct type *elt_type, | |
935 | struct type *usize_type) | |
936 | { | |
937 | struct type *type; | |
938 | ||
939 | elt_type = lookup_pointer_type (elt_type); | |
940 | type = rust_composite_type (elt_type, name, | |
941 | "data_ptr", elt_type, | |
942 | "length", usize_type); | |
943 | ||
944 | return type; | |
945 | } | |
946 | ||
947 | enum rust_primitive_types | |
948 | { | |
949 | rust_primitive_bool, | |
950 | rust_primitive_char, | |
951 | rust_primitive_i8, | |
952 | rust_primitive_u8, | |
953 | rust_primitive_i16, | |
954 | rust_primitive_u16, | |
955 | rust_primitive_i32, | |
956 | rust_primitive_u32, | |
957 | rust_primitive_i64, | |
958 | rust_primitive_u64, | |
959 | rust_primitive_isize, | |
960 | rust_primitive_usize, | |
961 | rust_primitive_f32, | |
962 | rust_primitive_f64, | |
963 | rust_primitive_unit, | |
964 | rust_primitive_str, | |
965 | nr_rust_primitive_types | |
966 | }; | |
967 | ||
968 | /* la_language_arch_info implementation for Rust. */ | |
969 | ||
970 | static void | |
971 | rust_language_arch_info (struct gdbarch *gdbarch, | |
972 | struct language_arch_info *lai) | |
973 | { | |
974 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
975 | struct type *tem; | |
976 | struct type **types; | |
977 | unsigned int length; | |
978 | ||
979 | types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1, | |
980 | struct type *); | |
981 | ||
982 | types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool"); | |
983 | types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char"); | |
984 | types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8"); | |
985 | types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8"); | |
986 | types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16"); | |
987 | types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16"); | |
988 | types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32"); | |
989 | types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32"); | |
990 | types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64"); | |
991 | types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64"); | |
992 | ||
993 | length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr); | |
994 | types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize"); | |
995 | types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize"); | |
996 | ||
49f190bc UW |
997 | types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32", |
998 | floatformats_ieee_single); | |
999 | types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64", | |
1000 | floatformats_ieee_double); | |
c44af4eb TT |
1001 | |
1002 | types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()"); | |
1003 | ||
1004 | tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL); | |
1005 | types[rust_primitive_str] = rust_slice_type ("&str", tem, | |
1006 | types[rust_primitive_usize]); | |
1007 | ||
1008 | lai->primitive_type_vector = types; | |
1009 | lai->bool_type_default = types[rust_primitive_bool]; | |
1010 | lai->string_char_type = types[rust_primitive_u8]; | |
1011 | } | |
1012 | ||
1013 | \f | |
1014 | ||
1015 | /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */ | |
1016 | ||
1017 | static struct value * | |
1018 | rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside) | |
1019 | { | |
1020 | int i; | |
1021 | int num_args = exp->elts[*pos + 1].longconst; | |
1022 | const char *method; | |
c44af4eb | 1023 | struct value *function, *result, *arg0; |
c44af4eb TT |
1024 | struct type *type, *fn_type; |
1025 | const struct block *block; | |
1026 | struct block_symbol sym; | |
1027 | ||
1028 | /* For an ordinary function call we can simply defer to the | |
1029 | generic implementation. */ | |
1030 | if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT) | |
1031 | return evaluate_subexp_standard (NULL, exp, pos, noside); | |
1032 | ||
1033 | /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */ | |
1034 | *pos += 4; | |
1035 | method = &exp->elts[*pos + 1].string; | |
1036 | *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1); | |
1037 | ||
1038 | /* Evaluate the argument to STRUCTOP_STRUCT, then find its | |
1039 | type in order to look up the method. */ | |
1040 | arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1041 | ||
1042 | if (noside == EVAL_SKIP) | |
1043 | { | |
1044 | for (i = 0; i < num_args; ++i) | |
1045 | evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1046 | return arg0; | |
1047 | } | |
1048 | ||
ab8b80a8 | 1049 | std::vector<struct value *> args (num_args + 1); |
c44af4eb TT |
1050 | args[0] = arg0; |
1051 | ||
1052 | /* We don't yet implement real Deref semantics. */ | |
1053 | while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR) | |
1054 | args[0] = value_ind (args[0]); | |
1055 | ||
1056 | type = value_type (args[0]); | |
1057 | if ((TYPE_CODE (type) != TYPE_CODE_STRUCT | |
1058 | && TYPE_CODE (type) != TYPE_CODE_UNION | |
1059 | && TYPE_CODE (type) != TYPE_CODE_ENUM) | |
1060 | || rust_tuple_type_p (type)) | |
1061 | error (_("Method calls only supported on struct or enum types")); | |
e86ca25f | 1062 | if (TYPE_NAME (type) == NULL) |
c44af4eb TT |
1063 | error (_("Method call on nameless type")); |
1064 | ||
e86ca25f | 1065 | std::string name = std::string (TYPE_NAME (type)) + "::" + method; |
c44af4eb TT |
1066 | |
1067 | block = get_selected_block (0); | |
ab8b80a8 | 1068 | sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL); |
c44af4eb | 1069 | if (sym.symbol == NULL) |
ab8b80a8 | 1070 | error (_("Could not find function named '%s'"), name.c_str ()); |
c44af4eb TT |
1071 | |
1072 | fn_type = SYMBOL_TYPE (sym.symbol); | |
1073 | if (TYPE_NFIELDS (fn_type) == 0) | |
ab8b80a8 | 1074 | error (_("Function '%s' takes no arguments"), name.c_str ()); |
c44af4eb TT |
1075 | |
1076 | if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR) | |
1077 | args[0] = value_addr (args[0]); | |
1078 | ||
1079 | function = address_of_variable (sym.symbol, block); | |
1080 | ||
1081 | for (i = 0; i < num_args; ++i) | |
1082 | args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1083 | ||
1084 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1085 | result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval); | |
1086 | else | |
7022349d | 1087 | result = call_function_by_hand (function, NULL, num_args + 1, args.data ()); |
c44af4eb TT |
1088 | return result; |
1089 | } | |
1090 | ||
01739a3b | 1091 | /* A helper for rust_evaluate_subexp that handles OP_RANGE. */ |
c44af4eb TT |
1092 | |
1093 | static struct value * | |
1094 | rust_range (struct expression *exp, int *pos, enum noside noside) | |
1095 | { | |
01739a3b | 1096 | enum range_type kind; |
c44af4eb TT |
1097 | struct value *low = NULL, *high = NULL; |
1098 | struct value *addrval, *result; | |
1099 | CORE_ADDR addr; | |
1100 | struct type *range_type; | |
1101 | struct type *index_type; | |
1102 | struct type *temp_type; | |
1103 | const char *name; | |
1104 | ||
01739a3b | 1105 | kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst); |
c44af4eb TT |
1106 | *pos += 3; |
1107 | ||
6873858b TT |
1108 | if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT |
1109 | || kind == NONE_BOUND_DEFAULT_EXCLUSIVE) | |
c44af4eb | 1110 | low = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
6873858b TT |
1111 | if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE |
1112 | || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE) | |
c44af4eb | 1113 | high = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
6873858b | 1114 | bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT); |
c44af4eb TT |
1115 | |
1116 | if (noside == EVAL_SKIP) | |
1117 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); | |
1118 | ||
1119 | if (low == NULL) | |
1120 | { | |
1121 | if (high == NULL) | |
1122 | { | |
1123 | index_type = NULL; | |
1124 | name = "std::ops::RangeFull"; | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | index_type = value_type (high); | |
6873858b TT |
1129 | name = (inclusive |
1130 | ? "std::ops::RangeToInclusive" : "std::ops::RangeTo"); | |
c44af4eb TT |
1131 | } |
1132 | } | |
1133 | else | |
1134 | { | |
1135 | if (high == NULL) | |
1136 | { | |
1137 | index_type = value_type (low); | |
1138 | name = "std::ops::RangeFrom"; | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | if (!types_equal (value_type (low), value_type (high))) | |
1143 | error (_("Range expression with different types")); | |
1144 | index_type = value_type (low); | |
6873858b | 1145 | name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range"; |
c44af4eb TT |
1146 | } |
1147 | } | |
1148 | ||
1149 | /* If we don't have an index type, just allocate this on the | |
1150 | arch. Here any type will do. */ | |
1151 | temp_type = (index_type == NULL | |
1152 | ? language_bool_type (exp->language_defn, exp->gdbarch) | |
1153 | : index_type); | |
1154 | /* It would be nicer to cache the range type. */ | |
1155 | range_type = rust_composite_type (temp_type, name, | |
1156 | low == NULL ? NULL : "start", index_type, | |
1157 | high == NULL ? NULL : "end", index_type); | |
1158 | ||
1159 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1160 | return value_zero (range_type, lval_memory); | |
1161 | ||
1162 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type)); | |
1163 | addr = value_as_long (addrval); | |
1164 | result = value_at_lazy (range_type, addr); | |
1165 | ||
1166 | if (low != NULL) | |
1167 | { | |
1168 | struct value *start = value_struct_elt (&result, NULL, "start", NULL, | |
1169 | "range"); | |
1170 | ||
1171 | value_assign (start, low); | |
1172 | } | |
1173 | ||
1174 | if (high != NULL) | |
1175 | { | |
1176 | struct value *end = value_struct_elt (&result, NULL, "end", NULL, | |
1177 | "range"); | |
1178 | ||
1179 | value_assign (end, high); | |
1180 | } | |
1181 | ||
1182 | result = value_at_lazy (range_type, addr); | |
1183 | return result; | |
1184 | } | |
1185 | ||
1186 | /* A helper function to compute the range and kind given a range | |
1187 | value. TYPE is the type of the range value. RANGE is the range | |
1188 | value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH | |
1189 | parameters might be filled in, or might not be, depending on the | |
1190 | kind of range this is. KIND will always be set to the appropriate | |
1191 | value describing the kind of range, and this can be used to | |
1192 | determine whether LOW or HIGH are valid. */ | |
1193 | ||
1194 | static void | |
1195 | rust_compute_range (struct type *type, struct value *range, | |
1196 | LONGEST *low, LONGEST *high, | |
01739a3b | 1197 | enum range_type *kind) |
c44af4eb TT |
1198 | { |
1199 | int i; | |
1200 | ||
1201 | *low = 0; | |
1202 | *high = 0; | |
1203 | *kind = BOTH_BOUND_DEFAULT; | |
1204 | ||
1205 | if (TYPE_NFIELDS (type) == 0) | |
1206 | return; | |
1207 | ||
1208 | i = 0; | |
1209 | if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0) | |
1210 | { | |
1211 | *kind = HIGH_BOUND_DEFAULT; | |
1212 | *low = value_as_long (value_field (range, 0)); | |
1213 | ++i; | |
1214 | } | |
1215 | if (TYPE_NFIELDS (type) > i | |
1216 | && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0) | |
1217 | { | |
1218 | *kind = (*kind == BOTH_BOUND_DEFAULT | |
1219 | ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT); | |
1220 | *high = value_as_long (value_field (range, i)); | |
6873858b TT |
1221 | |
1222 | if (rust_inclusive_range_type_p (type)) | |
1223 | ++*high; | |
c44af4eb TT |
1224 | } |
1225 | } | |
1226 | ||
1227 | /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */ | |
1228 | ||
1229 | static struct value * | |
1230 | rust_subscript (struct expression *exp, int *pos, enum noside noside, | |
1231 | int for_addr) | |
1232 | { | |
1233 | struct value *lhs, *rhs, *result; | |
1234 | struct type *rhstype; | |
45f4ed92 | 1235 | LONGEST low, high_bound; |
c44af4eb | 1236 | /* Initialized to appease the compiler. */ |
01739a3b | 1237 | enum range_type kind = BOTH_BOUND_DEFAULT; |
45f4ed92 | 1238 | LONGEST high = 0; |
c44af4eb TT |
1239 | int want_slice = 0; |
1240 | ||
1241 | ++*pos; | |
1242 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1243 | rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1244 | ||
1245 | if (noside == EVAL_SKIP) | |
1246 | return lhs; | |
1247 | ||
1248 | rhstype = check_typedef (value_type (rhs)); | |
1249 | if (rust_range_type_p (rhstype)) | |
1250 | { | |
1251 | if (!for_addr) | |
1252 | error (_("Can't take slice of array without '&'")); | |
1253 | rust_compute_range (rhstype, rhs, &low, &high, &kind); | |
1254 | want_slice = 1; | |
1255 | } | |
1256 | else | |
1257 | low = value_as_long (rhs); | |
1258 | ||
b3e3859b | 1259 | struct type *type = check_typedef (value_type (lhs)); |
c44af4eb TT |
1260 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1261 | { | |
b3e3859b TT |
1262 | struct type *base_type = nullptr; |
1263 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
1264 | base_type = TYPE_TARGET_TYPE (type); | |
1265 | else if (rust_slice_type_p (type)) | |
1266 | { | |
1267 | for (int i = 0; i < TYPE_NFIELDS (type); ++i) | |
1268 | { | |
1269 | if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0) | |
1270 | { | |
1271 | base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i)); | |
1272 | break; | |
1273 | } | |
1274 | } | |
1275 | if (base_type == nullptr) | |
1276 | error (_("Could not find 'data_ptr' in slice type")); | |
1277 | } | |
1278 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
1279 | base_type = TYPE_TARGET_TYPE (type); | |
1280 | else | |
1281 | error (_("Cannot subscript non-array type")); | |
1282 | ||
1283 | struct type *new_type; | |
1284 | if (want_slice) | |
1285 | { | |
1286 | if (rust_slice_type_p (type)) | |
1287 | new_type = type; | |
1288 | else | |
1289 | { | |
1290 | struct type *usize | |
1291 | = language_lookup_primitive_type (exp->language_defn, | |
1292 | exp->gdbarch, | |
1293 | "usize"); | |
1294 | new_type = rust_slice_type ("&[*gdb*]", base_type, usize); | |
1295 | } | |
1296 | } | |
1297 | else | |
1298 | new_type = base_type; | |
c44af4eb | 1299 | |
b3e3859b | 1300 | return value_zero (new_type, VALUE_LVAL (lhs)); |
c44af4eb TT |
1301 | } |
1302 | else | |
1303 | { | |
1304 | LONGEST low_bound; | |
1305 | struct value *base; | |
c44af4eb TT |
1306 | |
1307 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
1308 | { | |
1309 | base = lhs; | |
1310 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
1311 | error (_("Can't compute array bounds")); | |
1312 | if (low_bound != 0) | |
1313 | error (_("Found array with non-zero lower bound")); | |
1314 | ++high_bound; | |
1315 | } | |
1316 | else if (rust_slice_type_p (type)) | |
1317 | { | |
1318 | struct value *len; | |
1319 | ||
1320 | base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice"); | |
1321 | len = value_struct_elt (&lhs, NULL, "length", NULL, "slice"); | |
1322 | low_bound = 0; | |
1323 | high_bound = value_as_long (len); | |
1324 | } | |
42d94011 MG |
1325 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) |
1326 | { | |
1327 | base = lhs; | |
1328 | low_bound = 0; | |
1329 | high_bound = LONGEST_MAX; | |
1330 | } | |
c44af4eb TT |
1331 | else |
1332 | error (_("Cannot subscript non-array type")); | |
1333 | ||
1334 | if (want_slice | |
1335 | && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT)) | |
1336 | low = low_bound; | |
1337 | if (low < 0) | |
1338 | error (_("Index less than zero")); | |
1339 | if (low > high_bound) | |
1340 | error (_("Index greater than length")); | |
1341 | ||
1342 | result = value_subscript (base, low); | |
1343 | } | |
1344 | ||
1345 | if (for_addr) | |
1346 | { | |
1347 | if (want_slice) | |
1348 | { | |
1349 | struct type *usize, *slice; | |
1350 | CORE_ADDR addr; | |
1351 | struct value *addrval, *tem; | |
1352 | ||
1353 | if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT) | |
1354 | high = high_bound; | |
1355 | if (high < 0) | |
1356 | error (_("High index less than zero")); | |
1357 | if (low > high) | |
1358 | error (_("Low index greater than high index")); | |
1359 | if (high > high_bound) | |
1360 | error (_("High index greater than length")); | |
1361 | ||
1362 | usize = language_lookup_primitive_type (exp->language_defn, | |
1363 | exp->gdbarch, | |
1364 | "usize"); | |
45320ffa TT |
1365 | const char *new_name = ((type != nullptr |
1366 | && rust_slice_type_p (type)) | |
1367 | ? TYPE_NAME (type) : "&[*gdb*]"); | |
1368 | ||
1369 | slice = rust_slice_type (new_name, value_type (result), usize); | |
c44af4eb TT |
1370 | |
1371 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice)); | |
1372 | addr = value_as_long (addrval); | |
1373 | tem = value_at_lazy (slice, addr); | |
1374 | ||
1375 | value_assign (value_field (tem, 0), value_addr (result)); | |
1376 | value_assign (value_field (tem, 1), | |
1377 | value_from_longest (usize, high - low)); | |
1378 | ||
1379 | result = value_at_lazy (slice, addr); | |
1380 | } | |
1381 | else | |
1382 | result = value_addr (result); | |
1383 | } | |
1384 | ||
1385 | return result; | |
1386 | } | |
1387 | ||
1388 | /* evaluate_exp implementation for Rust. */ | |
1389 | ||
1390 | static struct value * | |
1391 | rust_evaluate_subexp (struct type *expect_type, struct expression *exp, | |
1392 | int *pos, enum noside noside) | |
1393 | { | |
1394 | struct value *result; | |
1395 | ||
1396 | switch (exp->elts[*pos].opcode) | |
1397 | { | |
71a3c369 TT |
1398 | case UNOP_IND: |
1399 | { | |
1400 | if (noside != EVAL_NORMAL) | |
1401 | result = evaluate_subexp_standard (expect_type, exp, pos, noside); | |
1402 | else | |
1403 | { | |
1404 | ++*pos; | |
1405 | struct value *value = evaluate_subexp (expect_type, exp, pos, | |
1406 | noside); | |
1407 | ||
1408 | struct value *trait_ptr = rust_get_trait_object_pointer (value); | |
1409 | if (trait_ptr != NULL) | |
1410 | value = trait_ptr; | |
1411 | ||
1412 | result = value_ind (value); | |
1413 | } | |
1414 | } | |
1415 | break; | |
1416 | ||
c44af4eb TT |
1417 | case UNOP_COMPLEMENT: |
1418 | { | |
1419 | struct value *value; | |
1420 | ||
1421 | ++*pos; | |
1422 | value = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1423 | if (noside == EVAL_SKIP) | |
1424 | { | |
1425 | /* Preserving the type is enough. */ | |
1426 | return value; | |
1427 | } | |
1428 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL) | |
1429 | result = value_from_longest (value_type (value), | |
1430 | value_logical_not (value)); | |
1431 | else | |
1432 | result = value_complement (value); | |
1433 | } | |
1434 | break; | |
1435 | ||
1436 | case BINOP_SUBSCRIPT: | |
1437 | result = rust_subscript (exp, pos, noside, 0); | |
1438 | break; | |
1439 | ||
1440 | case OP_FUNCALL: | |
1441 | result = rust_evaluate_funcall (exp, pos, noside); | |
1442 | break; | |
1443 | ||
1444 | case OP_AGGREGATE: | |
1445 | { | |
1446 | int pc = (*pos)++; | |
1447 | struct type *type = exp->elts[pc + 1].type; | |
1448 | int arglen = longest_to_int (exp->elts[pc + 2].longconst); | |
1449 | int i; | |
1450 | CORE_ADDR addr = 0; | |
1451 | struct value *addrval = NULL; | |
1452 | ||
1453 | *pos += 3; | |
1454 | ||
1455 | if (noside == EVAL_NORMAL) | |
1456 | { | |
1457 | addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type)); | |
1458 | addr = value_as_long (addrval); | |
1459 | result = value_at_lazy (type, addr); | |
1460 | } | |
1461 | ||
1462 | if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS) | |
1463 | { | |
1464 | struct value *init; | |
1465 | ||
1466 | ++*pos; | |
1467 | init = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1468 | if (noside == EVAL_NORMAL) | |
1469 | { | |
1470 | /* This isn't quite right but will do for the time | |
1471 | being, seeing that we can't implement the Copy | |
1472 | trait anyway. */ | |
1473 | value_assign (result, init); | |
1474 | } | |
1475 | ||
1476 | --arglen; | |
1477 | } | |
1478 | ||
1479 | gdb_assert (arglen % 2 == 0); | |
1480 | for (i = 0; i < arglen; i += 2) | |
1481 | { | |
1482 | int len; | |
1483 | const char *fieldname; | |
1484 | struct value *value, *field; | |
1485 | ||
1486 | gdb_assert (exp->elts[*pos].opcode == OP_NAME); | |
1487 | ++*pos; | |
1488 | len = longest_to_int (exp->elts[*pos].longconst); | |
1489 | ++*pos; | |
1490 | fieldname = &exp->elts[*pos].string; | |
1491 | *pos += 2 + BYTES_TO_EXP_ELEM (len + 1); | |
1492 | ||
1493 | value = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1494 | if (noside == EVAL_NORMAL) | |
1495 | { | |
1496 | field = value_struct_elt (&result, NULL, fieldname, NULL, | |
1497 | "structure"); | |
1498 | value_assign (field, value); | |
1499 | } | |
1500 | } | |
1501 | ||
1502 | if (noside == EVAL_SKIP) | |
1503 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, | |
1504 | 1); | |
1505 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1506 | result = allocate_value (type); | |
1507 | else | |
1508 | result = value_at_lazy (type, addr); | |
1509 | } | |
1510 | break; | |
1511 | ||
1512 | case OP_RUST_ARRAY: | |
1513 | { | |
1514 | int pc = (*pos)++; | |
1515 | int copies; | |
1516 | struct value *elt; | |
1517 | struct value *ncopies; | |
1518 | ||
1519 | elt = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1520 | ncopies = rust_evaluate_subexp (NULL, exp, pos, noside); | |
1521 | copies = value_as_long (ncopies); | |
1522 | if (copies < 0) | |
1523 | error (_("Array with negative number of elements")); | |
1524 | ||
1525 | if (noside == EVAL_NORMAL) | |
1526 | { | |
c44af4eb | 1527 | int i; |
ab8b80a8 | 1528 | std::vector<struct value *> eltvec (copies); |
c44af4eb TT |
1529 | |
1530 | for (i = 0; i < copies; ++i) | |
1531 | eltvec[i] = elt; | |
ab8b80a8 | 1532 | result = value_array (0, copies - 1, eltvec.data ()); |
c44af4eb TT |
1533 | } |
1534 | else | |
1535 | { | |
1536 | struct type *arraytype | |
1537 | = lookup_array_range_type (value_type (elt), 0, copies - 1); | |
1538 | result = allocate_value (arraytype); | |
1539 | } | |
1540 | } | |
1541 | break; | |
1542 | ||
1543 | case STRUCTOP_ANONYMOUS: | |
1544 | { | |
1545 | /* Anonymous field access, i.e. foo.1. */ | |
1546 | struct value *lhs; | |
1547 | int pc, field_number, nfields; | |
1548 | struct type *type, *variant_type; | |
c44af4eb TT |
1549 | |
1550 | pc = (*pos)++; | |
1551 | field_number = longest_to_int (exp->elts[pc + 1].longconst); | |
1552 | (*pos) += 2; | |
1553 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1554 | ||
1555 | type = value_type (lhs); | |
c9317f21 | 1556 | |
c9317f21 TT |
1557 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
1558 | { | |
1559 | struct type *outer_type = NULL; | |
1560 | ||
1561 | if (rust_enum_p (type)) | |
c44af4eb | 1562 | { |
c9317f21 TT |
1563 | const gdb_byte *valaddr = value_contents (lhs); |
1564 | struct field *variant_field = rust_enum_variant (type, valaddr); | |
1565 | ||
1566 | struct value *union_value = value_primitive_field (lhs, 0, 0, | |
1567 | type); | |
1568 | ||
1569 | int fieldno = (variant_field | |
1570 | - &TYPE_FIELD (value_type (union_value), 0)); | |
1571 | lhs = value_primitive_field (union_value, 0, fieldno, | |
1572 | value_type (union_value)); | |
1573 | outer_type = type; | |
1574 | type = value_type (lhs); | |
c44af4eb TT |
1575 | } |
1576 | ||
c44af4eb | 1577 | /* Tuples and tuple structs */ |
c9317f21 | 1578 | nfields = TYPE_NFIELDS (type); |
c44af4eb TT |
1579 | |
1580 | if (field_number >= nfields || field_number < 0) | |
c9317f21 TT |
1581 | { |
1582 | if (outer_type != NULL) | |
1583 | error(_("Cannot access field %d of variant %s::%s, " | |
1584 | "there are only %d fields"), | |
e86ca25f TT |
1585 | field_number, TYPE_NAME (outer_type), |
1586 | rust_last_path_segment (TYPE_NAME (type)), | |
c9317f21 TT |
1587 | nfields); |
1588 | else | |
1589 | error(_("Cannot access field %d of %s, " | |
1590 | "there are only %d fields"), | |
e86ca25f | 1591 | field_number, TYPE_NAME (type), nfields); |
c9317f21 | 1592 | } |
c44af4eb TT |
1593 | |
1594 | /* Tuples are tuple structs too. */ | |
1595 | if (!rust_tuple_struct_type_p (type)) | |
c9317f21 TT |
1596 | { |
1597 | if (outer_type != NULL) | |
1598 | error(_("Variant %s::%s is not a tuple variant"), | |
e86ca25f TT |
1599 | TYPE_NAME (outer_type), |
1600 | rust_last_path_segment (TYPE_NAME (type))); | |
c9317f21 TT |
1601 | else |
1602 | error(_("Attempting to access anonymous field %d " | |
1603 | "of %s, which is not a tuple, tuple struct, or " | |
1604 | "tuple-like variant"), | |
e86ca25f | 1605 | field_number, TYPE_NAME (type)); |
c9317f21 | 1606 | } |
c44af4eb TT |
1607 | |
1608 | result = value_primitive_field (lhs, 0, field_number, type); | |
1609 | } | |
1610 | else | |
1611 | error(_("Anonymous field access is only allowed on tuples, \ | |
1612 | tuple structs, and tuple-like enum variants")); | |
1613 | } | |
1614 | break; | |
1615 | ||
1616 | case STRUCTOP_STRUCT: | |
1617 | { | |
6830f270 | 1618 | struct value *lhs; |
c44af4eb TT |
1619 | struct type *type; |
1620 | int tem, pc; | |
1621 | ||
1622 | pc = (*pos)++; | |
1623 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
1624 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
1625 | lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1626 | ||
6830f270 | 1627 | const char *field_name = &exp->elts[pc + 2].string; |
c44af4eb | 1628 | type = value_type (lhs); |
c9317f21 | 1629 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type)) |
c44af4eb | 1630 | { |
c9317f21 TT |
1631 | const gdb_byte *valaddr = value_contents (lhs); |
1632 | struct field *variant_field = rust_enum_variant (type, valaddr); | |
1633 | ||
1634 | struct value *union_value = value_primitive_field (lhs, 0, 0, | |
1635 | type); | |
1636 | ||
1637 | int fieldno = (variant_field | |
1638 | - &TYPE_FIELD (value_type (union_value), 0)); | |
1639 | lhs = value_primitive_field (union_value, 0, fieldno, | |
1640 | value_type (union_value)); | |
1641 | ||
1642 | struct type *outer_type = type; | |
1643 | type = value_type (lhs); | |
1644 | if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type)) | |
1645 | error (_("Attempting to access named field foo of tuple " | |
1646 | "variant %s::%s, which has only anonymous fields"), | |
e86ca25f | 1647 | TYPE_NAME (outer_type), |
c9317f21 TT |
1648 | rust_last_path_segment (TYPE_NAME (type))); |
1649 | ||
1650 | TRY | |
c44af4eb | 1651 | { |
c9317f21 TT |
1652 | result = value_struct_elt (&lhs, NULL, field_name, |
1653 | NULL, "structure"); | |
c44af4eb | 1654 | } |
c9317f21 TT |
1655 | CATCH (except, RETURN_MASK_ERROR) |
1656 | { | |
1657 | error (_("Could not find field %s of struct variant %s::%s"), | |
e86ca25f | 1658 | field_name, TYPE_NAME (outer_type), |
c9317f21 TT |
1659 | rust_last_path_segment (TYPE_NAME (type))); |
1660 | } | |
1661 | END_CATCH | |
c44af4eb TT |
1662 | } |
1663 | else | |
c9317f21 TT |
1664 | result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure"); |
1665 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1666 | result = value_zero (value_type (result), VALUE_LVAL (result)); | |
c44af4eb TT |
1667 | } |
1668 | break; | |
1669 | ||
01739a3b | 1670 | case OP_RANGE: |
c44af4eb TT |
1671 | result = rust_range (exp, pos, noside); |
1672 | break; | |
1673 | ||
1674 | case UNOP_ADDR: | |
1675 | /* We might have &array[range], in which case we need to make a | |
1676 | slice. */ | |
1677 | if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT) | |
1678 | { | |
1679 | ++*pos; | |
1680 | result = rust_subscript (exp, pos, noside, 1); | |
1681 | break; | |
1682 | } | |
1683 | /* Fall through. */ | |
1684 | default: | |
1685 | result = evaluate_subexp_standard (expect_type, exp, pos, noside); | |
1686 | break; | |
1687 | } | |
1688 | ||
1689 | return result; | |
1690 | } | |
1691 | ||
1692 | /* operator_length implementation for Rust. */ | |
1693 | ||
1694 | static void | |
1695 | rust_operator_length (const struct expression *exp, int pc, int *oplenp, | |
1696 | int *argsp) | |
1697 | { | |
1698 | int oplen = 1; | |
1699 | int args = 0; | |
1700 | ||
1701 | switch (exp->elts[pc - 1].opcode) | |
1702 | { | |
1703 | case OP_AGGREGATE: | |
1704 | /* We handle aggregate as a type and argument count. The first | |
1705 | argument might be OP_OTHERS. After that the arguments | |
1706 | alternate: first an OP_NAME, then an expression. */ | |
1707 | oplen = 4; | |
1708 | args = longest_to_int (exp->elts[pc - 2].longconst); | |
1709 | break; | |
1710 | ||
1711 | case OP_OTHERS: | |
1712 | oplen = 1; | |
1713 | args = 1; | |
1714 | break; | |
1715 | ||
1716 | case STRUCTOP_ANONYMOUS: | |
1717 | oplen = 3; | |
1718 | args = 1; | |
1719 | break; | |
1720 | ||
1721 | case OP_RUST_ARRAY: | |
1722 | oplen = 1; | |
1723 | args = 2; | |
1724 | break; | |
1725 | ||
1726 | default: | |
1727 | operator_length_standard (exp, pc, oplenp, argsp); | |
1728 | return; | |
1729 | } | |
1730 | ||
1731 | *oplenp = oplen; | |
1732 | *argsp = args; | |
1733 | } | |
1734 | ||
1735 | /* op_name implementation for Rust. */ | |
1736 | ||
a121b7c1 | 1737 | static const char * |
c44af4eb TT |
1738 | rust_op_name (enum exp_opcode opcode) |
1739 | { | |
1740 | switch (opcode) | |
1741 | { | |
1742 | case OP_AGGREGATE: | |
1743 | return "OP_AGGREGATE"; | |
1744 | case OP_OTHERS: | |
1745 | return "OP_OTHERS"; | |
1746 | default: | |
1747 | return op_name_standard (opcode); | |
1748 | } | |
1749 | } | |
1750 | ||
1751 | /* dump_subexp_body implementation for Rust. */ | |
1752 | ||
1753 | static int | |
1754 | rust_dump_subexp_body (struct expression *exp, struct ui_file *stream, | |
1755 | int elt) | |
1756 | { | |
1757 | switch (exp->elts[elt].opcode) | |
1758 | { | |
1759 | case OP_AGGREGATE: | |
1760 | { | |
1761 | int length = longest_to_int (exp->elts[elt + 2].longconst); | |
1762 | int i; | |
1763 | ||
1764 | fprintf_filtered (stream, "Type @"); | |
1765 | gdb_print_host_address (exp->elts[elt + 1].type, stream); | |
1766 | fprintf_filtered (stream, " ("); | |
1767 | type_print (exp->elts[elt + 1].type, NULL, stream, 0); | |
1768 | fprintf_filtered (stream, "), length %d", length); | |
1769 | ||
1770 | elt += 4; | |
1771 | for (i = 0; i < length; ++i) | |
1772 | elt = dump_subexp (exp, stream, elt); | |
1773 | } | |
1774 | break; | |
1775 | ||
1776 | case OP_STRING: | |
1777 | case OP_NAME: | |
1778 | { | |
1779 | LONGEST len = exp->elts[elt + 1].longconst; | |
1780 | ||
1781 | fprintf_filtered (stream, "%s: %s", | |
1782 | (exp->elts[elt].opcode == OP_STRING | |
1783 | ? "string" : "name"), | |
1784 | &exp->elts[elt + 2].string); | |
1785 | elt += 4 + BYTES_TO_EXP_ELEM (len + 1); | |
1786 | } | |
1787 | break; | |
1788 | ||
1789 | case OP_OTHERS: | |
1790 | elt = dump_subexp (exp, stream, elt + 1); | |
1791 | break; | |
1792 | ||
1793 | case STRUCTOP_ANONYMOUS: | |
1794 | { | |
1795 | int field_number; | |
1796 | ||
68f2f2e3 | 1797 | field_number = longest_to_int (exp->elts[elt + 1].longconst); |
c44af4eb TT |
1798 | |
1799 | fprintf_filtered (stream, "Field number: %d", field_number); | |
68f2f2e3 | 1800 | elt = dump_subexp (exp, stream, elt + 3); |
c44af4eb TT |
1801 | } |
1802 | break; | |
1803 | ||
1804 | case OP_RUST_ARRAY: | |
68f2f2e3 | 1805 | ++elt; |
c44af4eb TT |
1806 | break; |
1807 | ||
1808 | default: | |
1809 | elt = dump_subexp_body_standard (exp, stream, elt); | |
1810 | break; | |
1811 | } | |
1812 | ||
1813 | return elt; | |
1814 | } | |
1815 | ||
1816 | /* print_subexp implementation for Rust. */ | |
1817 | ||
1818 | static void | |
1819 | rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream, | |
1820 | enum precedence prec) | |
1821 | { | |
1822 | switch (exp->elts[*pos].opcode) | |
1823 | { | |
1824 | case OP_AGGREGATE: | |
1825 | { | |
1826 | int length = longest_to_int (exp->elts[*pos + 2].longconst); | |
1827 | int i; | |
1828 | ||
1829 | type_print (exp->elts[*pos + 1].type, "", stream, 0); | |
1830 | fputs_filtered (" { ", stream); | |
1831 | ||
1832 | *pos += 4; | |
1833 | for (i = 0; i < length; ++i) | |
1834 | { | |
1835 | rust_print_subexp (exp, pos, stream, prec); | |
1836 | fputs_filtered (", ", stream); | |
1837 | } | |
1838 | fputs_filtered (" }", stream); | |
1839 | } | |
1840 | break; | |
1841 | ||
1842 | case OP_NAME: | |
1843 | { | |
1844 | LONGEST len = exp->elts[*pos + 1].longconst; | |
1845 | ||
1846 | fputs_filtered (&exp->elts[*pos + 2].string, stream); | |
1847 | *pos += 4 + BYTES_TO_EXP_ELEM (len + 1); | |
1848 | } | |
1849 | break; | |
1850 | ||
1851 | case OP_OTHERS: | |
1852 | { | |
1853 | fputs_filtered ("<<others>> (", stream); | |
1854 | ++*pos; | |
1855 | rust_print_subexp (exp, pos, stream, prec); | |
1856 | fputs_filtered (")", stream); | |
1857 | } | |
1858 | break; | |
1859 | ||
1860 | case STRUCTOP_ANONYMOUS: | |
1861 | { | |
1862 | int tem = longest_to_int (exp->elts[*pos + 1].longconst); | |
1863 | ||
1864 | (*pos) += 3; | |
1865 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
1866 | fprintf_filtered (stream, ".%d", tem); | |
1867 | } | |
256afbc2 | 1868 | break; |
c44af4eb TT |
1869 | |
1870 | case OP_RUST_ARRAY: | |
1871 | ++*pos; | |
1872 | fprintf_filtered (stream, "["); | |
1873 | rust_print_subexp (exp, pos, stream, prec); | |
1874 | fprintf_filtered (stream, "; "); | |
1875 | rust_print_subexp (exp, pos, stream, prec); | |
1876 | fprintf_filtered (stream, "]"); | |
1877 | break; | |
1878 | ||
1879 | default: | |
1880 | print_subexp_standard (exp, pos, stream, prec); | |
1881 | break; | |
1882 | } | |
1883 | } | |
1884 | ||
1885 | /* operator_check implementation for Rust. */ | |
1886 | ||
1887 | static int | |
1888 | rust_operator_check (struct expression *exp, int pos, | |
1889 | int (*objfile_func) (struct objfile *objfile, | |
1890 | void *data), | |
1891 | void *data) | |
1892 | { | |
1893 | switch (exp->elts[pos].opcode) | |
1894 | { | |
1895 | case OP_AGGREGATE: | |
1896 | { | |
1897 | struct type *type = exp->elts[pos + 1].type; | |
1898 | struct objfile *objfile = TYPE_OBJFILE (type); | |
1899 | ||
1900 | if (objfile != NULL && (*objfile_func) (objfile, data)) | |
1901 | return 1; | |
1902 | } | |
1903 | break; | |
1904 | ||
1905 | case OP_OTHERS: | |
1906 | case OP_NAME: | |
1907 | case OP_RUST_ARRAY: | |
1908 | break; | |
1909 | ||
1910 | default: | |
1911 | return operator_check_standard (exp, pos, objfile_func, data); | |
1912 | } | |
1913 | ||
1914 | return 0; | |
1915 | } | |
1916 | ||
1917 | \f | |
1918 | ||
1919 | /* Implementation of la_lookup_symbol_nonlocal for Rust. */ | |
1920 | ||
1921 | static struct block_symbol | |
1922 | rust_lookup_symbol_nonlocal (const struct language_defn *langdef, | |
1923 | const char *name, | |
1924 | const struct block *block, | |
1925 | const domain_enum domain) | |
1926 | { | |
1927 | struct block_symbol result = {NULL, NULL}; | |
1928 | ||
1929 | if (symbol_lookup_debug) | |
1930 | { | |
1931 | fprintf_unfiltered (gdb_stdlog, | |
1932 | "rust_lookup_symbol_non_local" | |
1933 | " (%s, %s (scope %s), %s)\n", | |
1934 | name, host_address_to_string (block), | |
1935 | block_scope (block), domain_name (domain)); | |
1936 | } | |
1937 | ||
1938 | /* Look up bare names in the block's scope. */ | |
fcfcc376 | 1939 | std::string scopedname; |
c44af4eb TT |
1940 | if (name[cp_find_first_component (name)] == '\0') |
1941 | { | |
1942 | const char *scope = block_scope (block); | |
1943 | ||
1944 | if (scope[0] != '\0') | |
1945 | { | |
fcfcc376 TT |
1946 | scopedname = std::string (scope) + "::" + name; |
1947 | name = scopedname.c_str (); | |
c44af4eb | 1948 | } |
fcfcc376 TT |
1949 | else |
1950 | name = NULL; | |
1951 | } | |
1952 | ||
1953 | if (name != NULL) | |
1954 | { | |
1955 | result = lookup_symbol_in_static_block (name, block, domain); | |
1956 | if (result.symbol == NULL) | |
1957 | result = lookup_global_symbol (name, block, domain); | |
c44af4eb TT |
1958 | } |
1959 | return result; | |
1960 | } | |
1961 | ||
1962 | \f | |
1963 | ||
8b302db8 TT |
1964 | /* la_sniff_from_mangled_name for Rust. */ |
1965 | ||
1966 | static int | |
1967 | rust_sniff_from_mangled_name (const char *mangled, char **demangled) | |
1968 | { | |
1969 | *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); | |
1970 | return *demangled != NULL; | |
1971 | } | |
1972 | ||
1973 | \f | |
1974 | ||
43cc5389 TT |
1975 | /* la_watch_location_expression for Rust. */ |
1976 | ||
1977 | static gdb::unique_xmalloc_ptr<char> | |
1978 | rust_watch_location_expression (struct type *type, CORE_ADDR addr) | |
1979 | { | |
1980 | type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type))); | |
1981 | std::string name = type_to_string (type); | |
1982 | return gdb::unique_xmalloc_ptr<char> | |
1983 | (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr), | |
1984 | name.c_str ())); | |
1985 | } | |
1986 | ||
1987 | \f | |
1988 | ||
c44af4eb TT |
1989 | static const struct exp_descriptor exp_descriptor_rust = |
1990 | { | |
1991 | rust_print_subexp, | |
1992 | rust_operator_length, | |
1993 | rust_operator_check, | |
1994 | rust_op_name, | |
1995 | rust_dump_subexp_body, | |
1996 | rust_evaluate_subexp | |
1997 | }; | |
1998 | ||
56618e20 TT |
1999 | static const char *rust_extensions[] = |
2000 | { | |
2001 | ".rs", NULL | |
2002 | }; | |
2003 | ||
47e77640 | 2004 | extern const struct language_defn rust_language_defn = |
c44af4eb TT |
2005 | { |
2006 | "rust", | |
2007 | "Rust", | |
2008 | language_rust, | |
2009 | range_check_on, | |
2010 | case_sensitive_on, | |
2011 | array_row_major, | |
2012 | macro_expansion_no, | |
56618e20 | 2013 | rust_extensions, |
c44af4eb TT |
2014 | &exp_descriptor_rust, |
2015 | rust_parse, | |
2016 | rustyyerror, | |
2017 | null_post_parser, | |
2018 | rust_printchar, /* Print a character constant */ | |
2019 | rust_printstr, /* Function to print string constant */ | |
2020 | rust_emitchar, /* Print a single char */ | |
2021 | rust_print_type, /* Print a type using appropriate syntax */ | |
2022 | rust_print_typedef, /* Print a typedef using appropriate syntax */ | |
2023 | rust_val_print, /* Print a value using appropriate syntax */ | |
2024 | c_value_print, /* Print a top-level value */ | |
2025 | default_read_var_value, /* la_read_var_value */ | |
2026 | NULL, /* Language specific skip_trampoline */ | |
2027 | NULL, /* name_of_this */ | |
59cc4834 | 2028 | false, /* la_store_sym_names_in_linkage_form_p */ |
c44af4eb TT |
2029 | rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
2030 | basic_lookup_transparent_type,/* lookup_transparent_type */ | |
2031 | gdb_demangle, /* Language specific symbol demangler */ | |
8b302db8 | 2032 | rust_sniff_from_mangled_name, |
c44af4eb TT |
2033 | NULL, /* Language specific |
2034 | class_name_from_physname */ | |
2035 | c_op_print_tab, /* expression operators for printing */ | |
2036 | 1, /* c-style arrays */ | |
2037 | 0, /* String lower bound */ | |
2038 | default_word_break_characters, | |
eb3ff9a5 | 2039 | default_collect_symbol_completion_matches, |
c44af4eb TT |
2040 | rust_language_arch_info, |
2041 | default_print_array_index, | |
2042 | default_pass_by_reference, | |
2043 | c_get_string, | |
43cc5389 | 2044 | rust_watch_location_expression, |
b5ec771e | 2045 | NULL, /* la_get_symbol_name_matcher */ |
c44af4eb | 2046 | iterate_over_symbols, |
5ffa0793 | 2047 | default_search_name_hash, |
c44af4eb TT |
2048 | &default_varobj_ops, |
2049 | NULL, | |
2050 | NULL, | |
2051 | LANG_MAGIC | |
2052 | }; |