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