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