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