1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
27 #include "cp-support.h"
33 #include "rust-lang.h"
39 /* Returns the last segment of a Rust path like foo::bar::baz. Will
40 not handle cases where the last segment contains generics. This
41 will return NULL if the last segment cannot be found. */
44 rust_last_path_segment (const char * path)
46 const char *result = strrchr (path, ':');
53 /* See rust-lang.h. */
56 rust_crate_for_block (const struct block *block)
58 const char *scope = block_scope (block);
61 return std::string ();
63 return std::string (scope, cp_find_first_component (scope));
66 /* Information about the discriminant/variant of an enum */
72 /* Field number in union. Negative on error. For an encoded enum,
73 the "hidden" member will always be field 1, and the "real" member
74 will always be field 0. */
76 /* True if this is an encoded enum that has a single "real" member
77 and a single "hidden" member. */
78 unsigned int is_encoded : 1;
81 /* The prefix of a specially-encoded enum. */
83 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
85 /* The number of the real field. */
87 #define RUST_ENCODED_ENUM_REAL 0
89 /* The number of the hidden field. */
91 #define RUST_ENCODED_ENUM_HIDDEN 1
93 /* Whether or not a TYPE_CODE_UNION value is an untagged union
94 as opposed to being a regular Rust enum. */
96 rust_union_is_untagged (struct type *type)
98 /* Unions must have at least one field. */
99 if (TYPE_NFIELDS (type) == 0)
101 /* If the first field is named, but the name has the rust enum prefix,
103 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
104 strlen (RUST_ENUM_PREFIX)) == 0)
106 /* Unions only have named fields. */
107 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
109 if (strlen (TYPE_FIELD_NAME (type, i)) == 0)
115 /* Utility function to get discriminant info for a given value. */
117 static struct disr_info
118 rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
119 int embedded_offset, CORE_ADDR address,
123 struct disr_info ret;
124 struct type *disr_type;
125 struct value_print_options opts;
126 const char *name_segment;
128 get_no_prettyformat_print_options (&opts);
133 if (TYPE_NFIELDS (type) == 0)
134 error (_("Encountered void enum value"));
136 /* If an enum has two values where one is empty and the other holds
137 a pointer that cannot be zero; then the Rust compiler optimizes
138 away the discriminant and instead uses a zero value in the
139 pointer field to indicate the empty variant. */
140 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
141 strlen (RUST_ENUM_PREFIX)) == 0)
143 char *tail, *token, *saveptr = NULL;
144 unsigned long fieldno;
145 struct type *member_type;
150 if (TYPE_NFIELDS (type) != 1)
151 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX);
153 /* Optimized enums have only one field. */
154 member_type = TYPE_FIELD_TYPE (type, 0);
156 std::string name (TYPE_FIELD_NAME (type, 0));
157 tail = &name[0] + strlen (RUST_ENUM_PREFIX);
159 /* The location of the value that doubles as a discriminant is
160 stored in the name of the field, as
161 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
162 where the fieldnos are the indices of the fields that should be
163 traversed in order to find the field (which may be several fields deep)
164 and the variantname is the name of the variant of the case when the
166 for (token = strtok_r (tail, "$", &saveptr);
168 token = strtok_r (NULL, "$", &saveptr))
170 if (sscanf (token, "%lu", &fieldno) != 1)
172 /* We have reached the enum name, which cannot start
176 if (fieldno >= TYPE_NFIELDS (member_type))
177 error (_("%s refers to field after end of member type"),
180 embedded_offset += TYPE_FIELD_BITPOS (member_type, fieldno) / 8;
181 member_type = TYPE_FIELD_TYPE (member_type, fieldno);
185 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
186 value = unpack_long (member_type, valaddr + embedded_offset);
190 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
191 ret.name = std::string (TYPE_NAME (type)) + "::" + token;
195 ret.field_no = RUST_ENCODED_ENUM_REAL;
196 ret.name = (std::string (TYPE_NAME (type)) + "::"
197 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))));
203 disr_type = TYPE_FIELD_TYPE (type, 0);
205 if (TYPE_NFIELDS (disr_type) == 0)
207 /* This is a bounds check and should never be hit unless Rust
208 has changed its debuginfo format. */
209 error (_("Could not find enum discriminant field"));
211 else if (TYPE_NFIELDS (type) == 1)
213 /* Sometimes univariant enums are encoded without a
214 discriminant. In that case, treating it as an encoded enum
215 with the first field being the actual type works. */
216 const char *field_name = TYPE_NAME (TYPE_FIELD_TYPE (type, 0));
217 const char *last = rust_last_path_segment (field_name);
218 ret.name = std::string (TYPE_NAME (type)) + "::" + last;
219 ret.field_no = RUST_ENCODED_ENUM_REAL;
224 if (strcmp (TYPE_FIELD_NAME (disr_type, 0), "RUST$ENUM$DISR") != 0)
225 error (_("Rust debug format has changed"));
227 string_file temp_file;
228 /* The first value of the first field (or any field)
229 is the discriminant value. */
230 c_val_print (TYPE_FIELD_TYPE (disr_type, 0),
231 (embedded_offset + TYPE_FIELD_BITPOS (type, 0) / 8
232 + TYPE_FIELD_BITPOS (disr_type, 0) / 8),
236 ret.name = std::move (temp_file.string ());
237 name_segment = rust_last_path_segment (ret.name.c_str ());
238 if (name_segment != NULL)
240 for (i = 0; i < TYPE_NFIELDS (type); ++i)
242 /* Sadly, the discriminant value paths do not match the type
243 field name paths ('core::option::Option::Some' vs
244 'core::option::Some'). However, enum variant names are
245 unique in the last path segment and the generics are not
246 part of this path, so we can just compare those. This is
247 hackish and would be better fixed by improving rustc's
248 metadata for enums. */
249 const char *field_type = TYPE_NAME (TYPE_FIELD_TYPE (type, i));
251 if (field_type != NULL
252 && strcmp (name_segment,
253 rust_last_path_segment (field_type)) == 0)
261 if (ret.field_no == -1 && !ret.name.empty ())
263 /* Somehow the discriminant wasn't found. */
264 error (_("Could not find variant of %s with discriminant %s"),
265 TYPE_TAG_NAME (type), ret.name.c_str ());
271 /* See rust-lang.h. */
274 rust_tuple_type_p (struct type *type)
276 /* The current implementation is a bit of a hack, but there's
277 nothing else in the debuginfo to distinguish a tuple from a
279 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
280 && TYPE_TAG_NAME (type) != NULL
281 && TYPE_TAG_NAME (type)[0] == '(');
285 /* Return true if all non-static fields of a structlike type are in a
286 sequence like __0, __1, __2. OFFSET lets us skip fields. */
289 rust_underscore_fields (struct type *type, int offset)
295 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
297 for (i = 0; i < TYPE_NFIELDS (type); ++i)
299 if (!field_is_static (&TYPE_FIELD (type, i)))
307 xsnprintf (buf, sizeof (buf), "__%d", field_number);
308 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
317 /* See rust-lang.h. */
320 rust_tuple_struct_type_p (struct type *type)
322 /* This is just an approximation until DWARF can represent Rust more
323 precisely. We exclude zero-length structs because they may not
324 be tuple structs, and there's no way to tell. */
325 return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type, 0);
328 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
331 rust_tuple_variant_type_p (struct type *type)
333 /* First field is discriminant */
334 return rust_underscore_fields (type, 1);
337 /* Return true if TYPE is a slice type, otherwise false. */
340 rust_slice_type_p (struct type *type)
342 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
343 && TYPE_TAG_NAME (type) != NULL
344 && (strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0
345 || strcmp (TYPE_TAG_NAME (type), "&str") == 0));
348 /* Return true if TYPE is a range type, otherwise false. */
351 rust_range_type_p (struct type *type)
355 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
356 || TYPE_NFIELDS (type) > 2
357 || TYPE_TAG_NAME (type) == NULL
358 || strstr (TYPE_TAG_NAME (type), "::Range") == NULL)
361 if (TYPE_NFIELDS (type) == 0)
365 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
367 if (TYPE_NFIELDS (type) == 1)
371 else if (TYPE_NFIELDS (type) == 2)
373 /* First field had to be "start". */
377 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
380 /* Return true if TYPE seems to be the type "u8", otherwise false. */
383 rust_u8_type_p (struct type *type)
385 return (TYPE_CODE (type) == TYPE_CODE_INT
386 && TYPE_UNSIGNED (type)
387 && TYPE_LENGTH (type) == 1);
390 /* Return true if TYPE is a Rust character type. */
393 rust_chartype_p (struct type *type)
395 return (TYPE_CODE (type) == TYPE_CODE_CHAR
396 && TYPE_LENGTH (type) == 4
397 && TYPE_UNSIGNED (type));
400 /* If VALUE represents a trait object pointer, return the underlying
401 pointer with the correct (i.e., runtime) type. Otherwise, return
404 static struct value *
405 rust_get_trait_object_pointer (struct value *value)
407 struct type *type = check_typedef (value_type (value));
409 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
412 /* Try to be a bit resilient if the ABI changes. */
413 int vtable_field = 0;
414 for (int i = 0; i < 2; ++i)
416 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
418 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
422 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
423 struct symbol *symbol = find_symbol_at_address (vtable);
424 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
427 struct rust_vtable_symbol *vtable_sym
428 = static_cast<struct rust_vtable_symbol *> (symbol);
429 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
430 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
435 /* la_emitchar implementation for Rust. */
438 rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
440 if (!rust_chartype_p (type))
441 generic_emit_char (c, type, stream, quoter,
442 target_charset (get_type_arch (type)));
443 else if (c == '\\' || c == quoter)
444 fprintf_filtered (stream, "\\%c", c);
446 fputs_filtered ("\\n", stream);
448 fputs_filtered ("\\r", stream);
450 fputs_filtered ("\\t", stream);
452 fputs_filtered ("\\0", stream);
453 else if (c >= 32 && c <= 127 && isprint (c))
454 fputc_filtered (c, stream);
456 fprintf_filtered (stream, "\\x%02x", c);
458 fprintf_filtered (stream, "\\u{%06x}", c);
461 /* la_printchar implementation for Rust. */
464 rust_printchar (int c, struct type *type, struct ui_file *stream)
466 fputs_filtered ("'", stream);
467 LA_EMIT_CHAR (c, type, stream, '\'');
468 fputs_filtered ("'", stream);
471 /* la_printstr implementation for Rust. */
474 rust_printstr (struct ui_file *stream, struct type *type,
475 const gdb_byte *string, unsigned int length,
476 const char *user_encoding, int force_ellipses,
477 const struct value_print_options *options)
479 /* Rust always uses UTF-8, but let the caller override this if need
481 const char *encoding = user_encoding;
482 if (user_encoding == NULL || !*user_encoding)
484 /* In Rust strings, characters are "u8". */
485 if (rust_u8_type_p (type))
489 /* This is probably some C string, so let's let C deal with
491 c_printstr (stream, type, string, length, user_encoding,
492 force_ellipses, options);
497 /* This is not ideal as it doesn't use our character printer. */
498 generic_printstr (stream, type, string, length, encoding, force_ellipses,
504 /* Helper function to print a string slice. */
507 rust_val_print_str (struct ui_file *stream, struct value *val,
508 const struct value_print_options *options)
510 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
512 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
514 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
515 value_as_address (base), value_as_long (len), stream,
519 /* rust_print_type branch for structs and untagged unions. */
522 val_print_struct (struct type *type, int embedded_offset,
523 CORE_ADDR address, struct ui_file *stream,
524 int recurse, struct value *val,
525 const struct value_print_options *options)
530 if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
532 rust_val_print_str (stream, val, options);
536 bool is_tuple = rust_tuple_type_p (type);
537 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
538 struct value_print_options opts;
542 if (TYPE_TAG_NAME (type) != NULL)
543 fprintf_filtered (stream, "%s", TYPE_TAG_NAME (type));
545 if (TYPE_NFIELDS (type) == 0)
548 if (TYPE_TAG_NAME (type) != NULL)
549 fputs_filtered (" ", stream);
552 if (is_tuple || is_tuple_struct)
553 fputs_filtered ("(", stream);
555 fputs_filtered ("{", stream);
561 for (i = 0; i < TYPE_NFIELDS (type); ++i)
563 if (field_is_static (&TYPE_FIELD (type, i)))
567 fputs_filtered (",", stream);
569 if (options->prettyformat)
571 fputs_filtered ("\n", stream);
572 print_spaces_filtered (2 + 2 * recurse, stream);
574 else if (!first_field)
575 fputs_filtered (" ", stream);
579 if (!is_tuple && !is_tuple_struct)
581 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
582 fputs_filtered (": ", stream);
585 val_print (TYPE_FIELD_TYPE (type, i),
586 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
588 stream, recurse + 1, val, &opts,
592 if (options->prettyformat)
594 fputs_filtered ("\n", stream);
595 print_spaces_filtered (2 * recurse, stream);
598 if (is_tuple || is_tuple_struct)
599 fputs_filtered (")", stream);
601 fputs_filtered ("}", stream);
604 static const struct generic_val_print_decorations rust_decorations =
606 /* Complex isn't used in Rust, but we provide C-ish values just in
618 /* la_val_print implementation for Rust. */
621 rust_val_print (struct type *type, int embedded_offset,
622 CORE_ADDR address, struct ui_file *stream, int recurse,
624 const struct value_print_options *options)
626 const gdb_byte *valaddr = value_contents_for_printing (val);
628 type = check_typedef (type);
629 switch (TYPE_CODE (type))
633 LONGEST low_bound, high_bound;
635 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
636 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
637 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
639 /* We have a pointer to a byte string, so just print
641 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
643 struct gdbarch *arch = get_type_arch (type);
644 int unit_size = gdbarch_addressable_memory_unit_size (arch);
646 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
647 if (options->addressprint)
649 fputs_filtered (paddress (arch, addr), stream);
650 fputs_filtered (" ", stream);
653 fputs_filtered ("b", stream);
654 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
655 high_bound - low_bound + 1, stream,
662 case TYPE_CODE_METHODPTR:
663 case TYPE_CODE_MEMBERPTR:
664 c_val_print (type, embedded_offset, address, stream,
665 recurse, val, options);
669 /* Recognize the unit type. */
670 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
671 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
673 fputs_filtered ("()", stream);
678 case TYPE_CODE_STRING:
680 struct gdbarch *arch = get_type_arch (type);
681 int unit_size = gdbarch_addressable_memory_unit_size (arch);
682 LONGEST low_bound, high_bound;
684 if (!get_array_bounds (type, &low_bound, &high_bound))
685 error (_("Could not determine the array bounds"));
687 /* If we see a plain TYPE_CODE_STRING, then we're printing a
688 byte string, hence the choice of "ASCII" as the
690 fputs_filtered ("b", stream);
691 rust_printstr (stream, TYPE_TARGET_TYPE (type),
692 valaddr + embedded_offset * unit_size,
693 high_bound - low_bound + 1, "ASCII", 0, options);
697 case TYPE_CODE_ARRAY:
699 LONGEST low_bound, high_bound;
701 if (get_array_bounds (type, &low_bound, &high_bound)
702 && high_bound - low_bound + 1 == 0)
703 fputs_filtered ("[]", stream);
709 case TYPE_CODE_UNION:
711 int j, nfields, first_field, is_tuple, start;
712 struct type *variant_type;
713 struct disr_info disr;
714 struct value_print_options opts;
716 /* Untagged unions are printed as if they are structs.
717 Since the field bit positions overlap in the debuginfo,
718 the code for printing a union is same as that for a struct,
719 the only difference is that the input type will have overlapping
721 if (rust_union_is_untagged (type))
723 val_print_struct (type, embedded_offset, address, stream,
724 recurse, val, options);
731 disr = rust_get_disr_info (type, valaddr, embedded_offset, address,
734 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
736 fprintf_filtered (stream, "%s", disr.name.c_str ());
741 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
742 nfields = TYPE_NFIELDS (variant_type);
744 is_tuple = (disr.is_encoded
745 ? rust_tuple_struct_type_p (variant_type)
746 : rust_tuple_variant_type_p (variant_type));
747 start = disr.is_encoded ? 0 : 1;
751 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
753 fprintf_filtered (stream, "%s(", disr.name.c_str ());
756 /* struct variant. */
757 fprintf_filtered (stream, "%s{", disr.name.c_str ());
762 /* In case of a nullary variant like 'None', just output
764 fprintf_filtered (stream, "%s", disr.name.c_str ());
768 for (j = start; j < TYPE_NFIELDS (variant_type); j++)
771 fputs_filtered (", ", stream);
775 fprintf_filtered (stream, "%s: ",
776 TYPE_FIELD_NAME (variant_type, j));
778 val_print (TYPE_FIELD_TYPE (variant_type, j),
780 + TYPE_FIELD_BITPOS (type, disr.field_no) / 8
781 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
783 stream, recurse + 1, val, &opts,
788 fputs_filtered (")", stream);
790 fputs_filtered ("}", stream);
794 case TYPE_CODE_STRUCT:
795 val_print_struct (type, embedded_offset, address, stream,
796 recurse, val, options);
801 /* Nothing special yet. */
802 generic_val_print (type, embedded_offset, address, stream,
803 recurse, val, options, &rust_decorations);
810 rust_print_type (struct type *type, const char *varstring,
811 struct ui_file *stream, int show, int level,
812 const struct type_print_options *flags);
814 /* Print a struct or union typedef. */
816 rust_print_struct_def (struct type *type, const char *varstring,
817 struct ui_file *stream, int show, int level,
818 const struct type_print_options *flags)
820 bool is_tuple_struct;
823 /* Print a tuple type simply. */
824 if (rust_tuple_type_p (type))
826 fputs_filtered (TYPE_TAG_NAME (type), stream);
830 /* If we see a base class, delegate to C. */
831 if (TYPE_N_BASECLASSES (type) > 0)
832 c_print_type (type, varstring, stream, show, level, flags);
834 /* This code path is also used by unions. */
835 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
836 fputs_filtered ("struct ", stream);
838 fputs_filtered ("union ", stream);
840 if (TYPE_TAG_NAME (type) != NULL)
841 fputs_filtered (TYPE_TAG_NAME (type), stream);
843 is_tuple_struct = rust_tuple_struct_type_p (type);
845 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
847 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
849 for (i = 0; i < TYPE_NFIELDS (type); ++i)
854 if (field_is_static (&TYPE_FIELD (type, i)))
857 /* We'd like to print "pub" here as needed, but rustc
858 doesn't emit the debuginfo, and our types don't have
859 cplus_struct_type attached. */
861 /* For a tuple struct we print the type but nothing
863 print_spaces_filtered (level + 2, stream);
864 if (!is_tuple_struct)
865 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
867 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
868 stream, show - 1, level + 2,
870 fputs_filtered (",\n", stream);
873 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
876 /* la_print_typedef implementation for Rust. */
879 rust_print_typedef (struct type *type,
880 struct symbol *new_symbol,
881 struct ui_file *stream)
883 type = check_typedef (type);
884 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
885 type_print (type, "", stream, 0);
886 fprintf_filtered (stream, ";\n");
889 /* la_print_type implementation for Rust. */
892 rust_print_type (struct type *type, const char *varstring,
893 struct ui_file *stream, int show, int level,
894 const struct type_print_options *flags)
900 && TYPE_NAME (type) != NULL)
902 /* Rust calls the unit type "void" in its debuginfo,
903 but we don't want to print it as that. */
904 if (TYPE_CODE (type) == TYPE_CODE_VOID)
905 fputs_filtered ("()", stream);
907 fputs_filtered (TYPE_NAME (type), stream);
911 type = check_typedef (type);
912 switch (TYPE_CODE (type))
915 fputs_filtered ("()", stream);
919 /* Delegate varargs to the C printer. */
920 if (TYPE_VARARGS (type))
923 fputs_filtered ("fn ", stream);
924 if (varstring != NULL)
925 fputs_filtered (varstring, stream);
926 fputs_filtered ("(", stream);
927 for (i = 0; i < TYPE_NFIELDS (type); ++i)
931 fputs_filtered (", ", stream);
932 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
935 fputs_filtered (")", stream);
936 /* If it returns unit, we can omit the return type. */
937 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
939 fputs_filtered (" -> ", stream);
940 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
944 case TYPE_CODE_ARRAY:
946 LONGEST low_bound, high_bound;
948 fputs_filtered ("[", stream);
949 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
950 stream, show - 1, level, flags);
952 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
953 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
954 fprintf_filtered (stream, "; variable length");
955 else if (get_array_bounds (type, &low_bound, &high_bound))
956 fprintf_filtered (stream, "; %s",
957 plongest (high_bound - low_bound + 1));
958 fputs_filtered ("]", stream);
962 case TYPE_CODE_STRUCT:
963 rust_print_struct_def (type, varstring, stream, show, level, flags);
970 fputs_filtered ("enum ", stream);
971 if (TYPE_TAG_NAME (type) != NULL)
973 fputs_filtered (TYPE_TAG_NAME (type), stream);
974 fputs_filtered (" ", stream);
975 len = strlen (TYPE_TAG_NAME (type));
977 fputs_filtered ("{\n", stream);
979 for (i = 0; i < TYPE_NFIELDS (type); ++i)
981 const char *name = TYPE_FIELD_NAME (type, i);
986 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
988 && name[len + 1] == ':')
990 fprintfi_filtered (level + 2, stream, "%s,\n", name);
993 fputs_filtered ("}", stream);
997 case TYPE_CODE_UNION:
1001 /* Skip the discriminant field. */
1004 /* Unions and structs have the same syntax in Rust,
1005 the only difference is that structs are declared with `struct`
1006 and union with `union`. This difference is handled in the struct
1008 if (rust_union_is_untagged (type))
1010 rust_print_struct_def (type, varstring, stream, show, level, flags);
1014 fputs_filtered ("enum ", stream);
1015 if (TYPE_TAG_NAME (type) != NULL)
1017 fputs_filtered (TYPE_TAG_NAME (type), stream);
1018 fputs_filtered (" ", stream);
1020 fputs_filtered ("{\n", stream);
1022 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
1023 strlen (RUST_ENUM_PREFIX)) == 0)
1025 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
1026 if (zero_field != NULL && strlen (zero_field) > 1)
1028 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
1029 /* There is no explicit discriminant field, skip nothing. */
1033 else if (TYPE_NFIELDS (type) == 1)
1036 for (i = 0; i < TYPE_NFIELDS (type); ++i)
1038 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
1040 = rust_last_path_segment (TYPE_NAME (variant_type));
1042 fprintfi_filtered (level + 2, stream, "%s", name);
1044 if (TYPE_NFIELDS (variant_type) > skip_to)
1047 bool is_tuple = (TYPE_NFIELDS (type) == 1
1048 ? rust_tuple_struct_type_p (variant_type)
1049 : rust_tuple_variant_type_p (variant_type));
1052 fputs_filtered (is_tuple ? "(" : "{", stream);
1053 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
1058 fputs_filtered (", ", stream);
1061 fprintf_filtered (stream, "%s: ",
1062 TYPE_FIELD_NAME (variant_type, j));
1064 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
1065 stream, show - 1, level + 2,
1068 fputs_filtered (is_tuple ? ")" : "}", stream);
1071 fputs_filtered (",\n", stream);
1074 fputs_filtered ("}", stream);
1080 c_print_type (type, varstring, stream, show, level, flags);
1086 /* Compute the alignment of the type T. */
1089 rust_type_alignment (struct type *t)
1091 t = check_typedef (t);
1092 switch (TYPE_CODE (t))
1095 error (_("Could not compute alignment of type"));
1098 case TYPE_CODE_ENUM:
1102 case TYPE_CODE_CHAR:
1103 case TYPE_CODE_BOOL:
1104 return TYPE_LENGTH (t);
1106 case TYPE_CODE_ARRAY:
1107 case TYPE_CODE_COMPLEX:
1108 return rust_type_alignment (TYPE_TARGET_TYPE (t));
1110 case TYPE_CODE_STRUCT:
1111 case TYPE_CODE_UNION:
1116 for (i = 0; i < TYPE_NFIELDS (t); ++i)
1118 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
1127 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1128 -- either on an obstack or on a gdbarch. */
1130 static struct type *
1131 rust_composite_type (struct type *original,
1133 const char *field1, struct type *type1,
1134 const char *field2, struct type *type2)
1136 struct type *result = alloc_type_copy (original);
1137 int i, nfields, bitpos;
1145 TYPE_CODE (result) = TYPE_CODE_STRUCT;
1146 TYPE_NAME (result) = name;
1147 TYPE_TAG_NAME (result) = name;
1149 TYPE_NFIELDS (result) = nfields;
1150 TYPE_FIELDS (result)
1151 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1157 struct field *field = &TYPE_FIELD (result, i);
1159 SET_FIELD_BITPOS (*field, bitpos);
1160 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1162 FIELD_NAME (*field) = field1;
1163 FIELD_TYPE (*field) = type1;
1168 struct field *field = &TYPE_FIELD (result, i);
1169 int align = rust_type_alignment (type2);
1175 align *= TARGET_CHAR_BIT;
1176 delta = bitpos % align;
1178 bitpos += align - delta;
1180 SET_FIELD_BITPOS (*field, bitpos);
1182 FIELD_NAME (*field) = field2;
1183 FIELD_TYPE (*field) = type2;
1188 TYPE_LENGTH (result)
1189 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1190 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1194 /* See rust-lang.h. */
1197 rust_slice_type (const char *name, struct type *elt_type,
1198 struct type *usize_type)
1202 elt_type = lookup_pointer_type (elt_type);
1203 type = rust_composite_type (elt_type, name,
1204 "data_ptr", elt_type,
1205 "length", usize_type);
1210 enum rust_primitive_types
1212 rust_primitive_bool,
1213 rust_primitive_char,
1222 rust_primitive_isize,
1223 rust_primitive_usize,
1226 rust_primitive_unit,
1228 nr_rust_primitive_types
1231 /* la_language_arch_info implementation for Rust. */
1234 rust_language_arch_info (struct gdbarch *gdbarch,
1235 struct language_arch_info *lai)
1237 const struct builtin_type *builtin = builtin_type (gdbarch);
1239 struct type **types;
1240 unsigned int length;
1242 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1245 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1246 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1247 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1248 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1249 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1250 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1251 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1252 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1253 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1254 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1256 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1257 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1258 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1260 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1261 floatformats_ieee_single);
1262 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1263 floatformats_ieee_double);
1265 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1267 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1268 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1269 types[rust_primitive_usize]);
1271 lai->primitive_type_vector = types;
1272 lai->bool_type_default = types[rust_primitive_bool];
1273 lai->string_char_type = types[rust_primitive_u8];
1278 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1280 static struct value *
1281 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1284 int num_args = exp->elts[*pos + 1].longconst;
1286 struct value *function, *result, *arg0;
1287 struct type *type, *fn_type;
1288 const struct block *block;
1289 struct block_symbol sym;
1291 /* For an ordinary function call we can simply defer to the
1292 generic implementation. */
1293 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1294 return evaluate_subexp_standard (NULL, exp, pos, noside);
1296 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1298 method = &exp->elts[*pos + 1].string;
1299 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1301 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1302 type in order to look up the method. */
1303 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1305 if (noside == EVAL_SKIP)
1307 for (i = 0; i < num_args; ++i)
1308 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1312 std::vector<struct value *> args (num_args + 1);
1315 /* We don't yet implement real Deref semantics. */
1316 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1317 args[0] = value_ind (args[0]);
1319 type = value_type (args[0]);
1320 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1321 && TYPE_CODE (type) != TYPE_CODE_UNION
1322 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1323 || rust_tuple_type_p (type))
1324 error (_("Method calls only supported on struct or enum types"));
1325 if (TYPE_TAG_NAME (type) == NULL)
1326 error (_("Method call on nameless type"));
1328 std::string name = std::string (TYPE_TAG_NAME (type)) + "::" + method;
1330 block = get_selected_block (0);
1331 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1332 if (sym.symbol == NULL)
1333 error (_("Could not find function named '%s'"), name.c_str ());
1335 fn_type = SYMBOL_TYPE (sym.symbol);
1336 if (TYPE_NFIELDS (fn_type) == 0)
1337 error (_("Function '%s' takes no arguments"), name.c_str ());
1339 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1340 args[0] = value_addr (args[0]);
1342 function = address_of_variable (sym.symbol, block);
1344 for (i = 0; i < num_args; ++i)
1345 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1347 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1348 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1350 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
1354 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1356 static struct value *
1357 rust_range (struct expression *exp, int *pos, enum noside noside)
1359 enum range_type kind;
1360 struct value *low = NULL, *high = NULL;
1361 struct value *addrval, *result;
1363 struct type *range_type;
1364 struct type *index_type;
1365 struct type *temp_type;
1368 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1371 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1372 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1373 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1374 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1376 if (noside == EVAL_SKIP)
1377 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1384 name = "std::ops::RangeFull";
1388 index_type = value_type (high);
1389 name = "std::ops::RangeTo";
1396 index_type = value_type (low);
1397 name = "std::ops::RangeFrom";
1401 if (!types_equal (value_type (low), value_type (high)))
1402 error (_("Range expression with different types"));
1403 index_type = value_type (low);
1404 name = "std::ops::Range";
1408 /* If we don't have an index type, just allocate this on the
1409 arch. Here any type will do. */
1410 temp_type = (index_type == NULL
1411 ? language_bool_type (exp->language_defn, exp->gdbarch)
1413 /* It would be nicer to cache the range type. */
1414 range_type = rust_composite_type (temp_type, name,
1415 low == NULL ? NULL : "start", index_type,
1416 high == NULL ? NULL : "end", index_type);
1418 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1419 return value_zero (range_type, lval_memory);
1421 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1422 addr = value_as_long (addrval);
1423 result = value_at_lazy (range_type, addr);
1427 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1430 value_assign (start, low);
1435 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1438 value_assign (end, high);
1441 result = value_at_lazy (range_type, addr);
1445 /* A helper function to compute the range and kind given a range
1446 value. TYPE is the type of the range value. RANGE is the range
1447 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1448 parameters might be filled in, or might not be, depending on the
1449 kind of range this is. KIND will always be set to the appropriate
1450 value describing the kind of range, and this can be used to
1451 determine whether LOW or HIGH are valid. */
1454 rust_compute_range (struct type *type, struct value *range,
1455 LONGEST *low, LONGEST *high,
1456 enum range_type *kind)
1462 *kind = BOTH_BOUND_DEFAULT;
1464 if (TYPE_NFIELDS (type) == 0)
1468 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1470 *kind = HIGH_BOUND_DEFAULT;
1471 *low = value_as_long (value_field (range, 0));
1474 if (TYPE_NFIELDS (type) > i
1475 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1477 *kind = (*kind == BOTH_BOUND_DEFAULT
1478 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1479 *high = value_as_long (value_field (range, i));
1483 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1485 static struct value *
1486 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1489 struct value *lhs, *rhs, *result;
1490 struct type *rhstype;
1491 LONGEST low, high_bound;
1492 /* Initialized to appease the compiler. */
1493 enum range_type kind = BOTH_BOUND_DEFAULT;
1498 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1499 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1501 if (noside == EVAL_SKIP)
1504 rhstype = check_typedef (value_type (rhs));
1505 if (rust_range_type_p (rhstype))
1508 error (_("Can't take slice of array without '&'"));
1509 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1513 low = value_as_long (rhs);
1515 struct type *type = check_typedef (value_type (lhs));
1516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1518 struct type *base_type = nullptr;
1519 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1520 base_type = TYPE_TARGET_TYPE (type);
1521 else if (rust_slice_type_p (type))
1523 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1525 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1527 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1531 if (base_type == nullptr)
1532 error (_("Could not find 'data_ptr' in slice type"));
1534 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1535 base_type = TYPE_TARGET_TYPE (type);
1537 error (_("Cannot subscript non-array type"));
1539 struct type *new_type;
1542 if (rust_slice_type_p (type))
1547 = language_lookup_primitive_type (exp->language_defn,
1550 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1554 new_type = base_type;
1556 return value_zero (new_type, VALUE_LVAL (lhs));
1563 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1566 if (!get_array_bounds (type, &low_bound, &high_bound))
1567 error (_("Can't compute array bounds"));
1569 error (_("Found array with non-zero lower bound"));
1572 else if (rust_slice_type_p (type))
1576 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1577 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1579 high_bound = value_as_long (len);
1581 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1585 high_bound = LONGEST_MAX;
1588 error (_("Cannot subscript non-array type"));
1591 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1594 error (_("Index less than zero"));
1595 if (low > high_bound)
1596 error (_("Index greater than length"));
1598 result = value_subscript (base, low);
1605 struct type *usize, *slice;
1607 struct value *addrval, *tem;
1609 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1612 error (_("High index less than zero"));
1614 error (_("Low index greater than high index"));
1615 if (high > high_bound)
1616 error (_("High index greater than length"));
1618 usize = language_lookup_primitive_type (exp->language_defn,
1621 const char *new_name = ((type != nullptr
1622 && rust_slice_type_p (type))
1623 ? TYPE_NAME (type) : "&[*gdb*]");
1625 slice = rust_slice_type (new_name, value_type (result), usize);
1627 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1628 addr = value_as_long (addrval);
1629 tem = value_at_lazy (slice, addr);
1631 value_assign (value_field (tem, 0), value_addr (result));
1632 value_assign (value_field (tem, 1),
1633 value_from_longest (usize, high - low));
1635 result = value_at_lazy (slice, addr);
1638 result = value_addr (result);
1644 /* evaluate_exp implementation for Rust. */
1646 static struct value *
1647 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1648 int *pos, enum noside noside)
1650 struct value *result;
1652 switch (exp->elts[*pos].opcode)
1656 if (noside != EVAL_NORMAL)
1657 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1661 struct value *value = evaluate_subexp (expect_type, exp, pos,
1664 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1665 if (trait_ptr != NULL)
1668 result = value_ind (value);
1673 case UNOP_COMPLEMENT:
1675 struct value *value;
1678 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1679 if (noside == EVAL_SKIP)
1681 /* Preserving the type is enough. */
1684 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1685 result = value_from_longest (value_type (value),
1686 value_logical_not (value));
1688 result = value_complement (value);
1692 case BINOP_SUBSCRIPT:
1693 result = rust_subscript (exp, pos, noside, 0);
1697 result = rust_evaluate_funcall (exp, pos, noside);
1703 struct type *type = exp->elts[pc + 1].type;
1704 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1707 struct value *addrval = NULL;
1711 if (noside == EVAL_NORMAL)
1713 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1714 addr = value_as_long (addrval);
1715 result = value_at_lazy (type, addr);
1718 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1723 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1724 if (noside == EVAL_NORMAL)
1726 /* This isn't quite right but will do for the time
1727 being, seeing that we can't implement the Copy
1729 value_assign (result, init);
1735 gdb_assert (arglen % 2 == 0);
1736 for (i = 0; i < arglen; i += 2)
1739 const char *fieldname;
1740 struct value *value, *field;
1742 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1744 len = longest_to_int (exp->elts[*pos].longconst);
1746 fieldname = &exp->elts[*pos].string;
1747 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1749 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1750 if (noside == EVAL_NORMAL)
1752 field = value_struct_elt (&result, NULL, fieldname, NULL,
1754 value_assign (field, value);
1758 if (noside == EVAL_SKIP)
1759 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1761 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1762 result = allocate_value (type);
1764 result = value_at_lazy (type, addr);
1773 struct value *ncopies;
1775 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1776 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1777 copies = value_as_long (ncopies);
1779 error (_("Array with negative number of elements"));
1781 if (noside == EVAL_NORMAL)
1785 std::vector<struct value *> eltvec (copies);
1787 for (i = 0; i < copies; ++i)
1789 result = value_array (0, copies - 1, eltvec.data ());
1793 struct type *arraytype
1794 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1795 result = allocate_value (arraytype);
1800 case STRUCTOP_ANONYMOUS:
1802 /* Anonymous field access, i.e. foo.1. */
1804 int pc, field_number, nfields;
1805 struct type *type, *variant_type;
1806 struct disr_info disr;
1809 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1811 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1813 type = value_type (lhs);
1814 /* Untagged unions can't have anonymous field access since
1815 they can only have named fields. */
1816 if (TYPE_CODE (type) == TYPE_CODE_UNION
1817 && !rust_union_is_untagged (type))
1819 disr = rust_get_disr_info (type, value_contents (lhs),
1820 value_embedded_offset (lhs),
1821 value_address (lhs), lhs);
1823 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1825 variant_type = NULL;
1830 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1831 nfields = TYPE_NFIELDS (variant_type);
1834 if (!disr.is_encoded)
1837 if (field_number >= nfields || field_number < 0)
1838 error(_("Cannot access field %d of variant %s, \
1839 there are only %d fields"),
1840 disr.is_encoded ? field_number : field_number - 1,
1842 disr.is_encoded ? nfields : nfields - 1);
1844 if (!(disr.is_encoded
1845 ? rust_tuple_struct_type_p (variant_type)
1846 : rust_tuple_variant_type_p (variant_type)))
1847 error(_("Variant %s is not a tuple variant"), disr.name.c_str ());
1849 result = value_primitive_field (lhs, 0, field_number,
1852 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1854 /* Tuples and tuple structs */
1855 nfields = TYPE_NFIELDS(type);
1857 if (field_number >= nfields || field_number < 0)
1858 error(_("Cannot access field %d of %s, there are only %d fields"),
1859 field_number, TYPE_TAG_NAME (type), nfields);
1861 /* Tuples are tuple structs too. */
1862 if (!rust_tuple_struct_type_p (type))
1863 error(_("Attempting to access anonymous field %d of %s, which is \
1864 not a tuple, tuple struct, or tuple-like variant"),
1865 field_number, TYPE_TAG_NAME (type));
1867 result = value_primitive_field (lhs, 0, field_number, type);
1870 error(_("Anonymous field access is only allowed on tuples, \
1871 tuple structs, and tuple-like enum variants"));
1875 case STRUCTOP_STRUCT:
1882 tem = longest_to_int (exp->elts[pc + 1].longconst);
1883 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1884 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1886 const char *field_name = &exp->elts[pc + 2].string;
1887 type = value_type (lhs);
1888 if (TYPE_CODE (type) == TYPE_CODE_UNION
1889 && !rust_union_is_untagged (type))
1892 struct disr_info disr;
1893 struct type *variant_type;
1895 disr = rust_get_disr_info (type, value_contents (lhs),
1896 value_embedded_offset (lhs),
1897 value_address (lhs), lhs);
1899 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1900 error(_("Could not find field %s of struct variant %s"),
1901 field_name, disr.name.c_str ());
1903 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1905 if (variant_type == NULL
1907 ? rust_tuple_struct_type_p (variant_type)
1908 : rust_tuple_variant_type_p (variant_type)))
1909 error(_("Attempting to access named field %s of tuple variant %s, \
1910 which has only anonymous fields"),
1911 field_name, disr.name.c_str ());
1913 start = disr.is_encoded ? 0 : 1;
1914 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1916 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1918 result = value_primitive_field (lhs, 0, i, variant_type);
1923 if (i == TYPE_NFIELDS (variant_type))
1924 /* We didn't find it. */
1925 error(_("Could not find field %s of struct variant %s"),
1926 field_name, disr.name.c_str ());
1930 result = value_struct_elt (&lhs, NULL, field_name, NULL,
1932 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1933 result = value_zero (value_type (result), VALUE_LVAL (result));
1939 result = rust_range (exp, pos, noside);
1943 /* We might have &array[range], in which case we need to make a
1945 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1948 result = rust_subscript (exp, pos, noside, 1);
1953 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1960 /* operator_length implementation for Rust. */
1963 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1969 switch (exp->elts[pc - 1].opcode)
1972 /* We handle aggregate as a type and argument count. The first
1973 argument might be OP_OTHERS. After that the arguments
1974 alternate: first an OP_NAME, then an expression. */
1976 args = longest_to_int (exp->elts[pc - 2].longconst);
1984 case STRUCTOP_ANONYMOUS:
1995 operator_length_standard (exp, pc, oplenp, argsp);
2003 /* op_name implementation for Rust. */
2006 rust_op_name (enum exp_opcode opcode)
2011 return "OP_AGGREGATE";
2015 return op_name_standard (opcode);
2019 /* dump_subexp_body implementation for Rust. */
2022 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
2025 switch (exp->elts[elt].opcode)
2029 int length = longest_to_int (exp->elts[elt + 2].longconst);
2032 fprintf_filtered (stream, "Type @");
2033 gdb_print_host_address (exp->elts[elt + 1].type, stream);
2034 fprintf_filtered (stream, " (");
2035 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
2036 fprintf_filtered (stream, "), length %d", length);
2039 for (i = 0; i < length; ++i)
2040 elt = dump_subexp (exp, stream, elt);
2047 LONGEST len = exp->elts[elt + 1].longconst;
2049 fprintf_filtered (stream, "%s: %s",
2050 (exp->elts[elt].opcode == OP_STRING
2051 ? "string" : "name"),
2052 &exp->elts[elt + 2].string);
2053 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
2058 elt = dump_subexp (exp, stream, elt + 1);
2061 case STRUCTOP_ANONYMOUS:
2065 field_number = longest_to_int (exp->elts[elt + 1].longconst);
2067 fprintf_filtered (stream, "Field number: %d", field_number);
2068 elt = dump_subexp (exp, stream, elt + 3);
2077 elt = dump_subexp_body_standard (exp, stream, elt);
2084 /* print_subexp implementation for Rust. */
2087 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
2088 enum precedence prec)
2090 switch (exp->elts[*pos].opcode)
2094 int length = longest_to_int (exp->elts[*pos + 2].longconst);
2097 type_print (exp->elts[*pos + 1].type, "", stream, 0);
2098 fputs_filtered (" { ", stream);
2101 for (i = 0; i < length; ++i)
2103 rust_print_subexp (exp, pos, stream, prec);
2104 fputs_filtered (", ", stream);
2106 fputs_filtered (" }", stream);
2112 LONGEST len = exp->elts[*pos + 1].longconst;
2114 fputs_filtered (&exp->elts[*pos + 2].string, stream);
2115 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
2121 fputs_filtered ("<<others>> (", stream);
2123 rust_print_subexp (exp, pos, stream, prec);
2124 fputs_filtered (")", stream);
2128 case STRUCTOP_ANONYMOUS:
2130 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
2133 print_subexp (exp, pos, stream, PREC_SUFFIX);
2134 fprintf_filtered (stream, ".%d", tem);
2140 fprintf_filtered (stream, "[");
2141 rust_print_subexp (exp, pos, stream, prec);
2142 fprintf_filtered (stream, "; ");
2143 rust_print_subexp (exp, pos, stream, prec);
2144 fprintf_filtered (stream, "]");
2148 print_subexp_standard (exp, pos, stream, prec);
2153 /* operator_check implementation for Rust. */
2156 rust_operator_check (struct expression *exp, int pos,
2157 int (*objfile_func) (struct objfile *objfile,
2161 switch (exp->elts[pos].opcode)
2165 struct type *type = exp->elts[pos + 1].type;
2166 struct objfile *objfile = TYPE_OBJFILE (type);
2168 if (objfile != NULL && (*objfile_func) (objfile, data))
2179 return operator_check_standard (exp, pos, objfile_func, data);
2187 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2189 static struct block_symbol
2190 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2192 const struct block *block,
2193 const domain_enum domain)
2195 struct block_symbol result = {NULL, NULL};
2197 if (symbol_lookup_debug)
2199 fprintf_unfiltered (gdb_stdlog,
2200 "rust_lookup_symbol_non_local"
2201 " (%s, %s (scope %s), %s)\n",
2202 name, host_address_to_string (block),
2203 block_scope (block), domain_name (domain));
2206 /* Look up bare names in the block's scope. */
2207 if (name[cp_find_first_component (name)] == '\0')
2209 const char *scope = block_scope (block);
2211 if (scope[0] != '\0')
2213 std::string scopedname = std::string (scope) + "::" + name;
2215 result = lookup_symbol_in_static_block (scopedname.c_str (), block,
2217 if (result.symbol == NULL)
2218 result = lookup_global_symbol (scopedname.c_str (), block, domain);
2226 /* la_sniff_from_mangled_name for Rust. */
2229 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2231 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2232 return *demangled != NULL;
2237 /* la_watch_location_expression for Rust. */
2239 static gdb::unique_xmalloc_ptr<char>
2240 rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2242 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2243 std::string name = type_to_string (type);
2244 return gdb::unique_xmalloc_ptr<char>
2245 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2251 static const struct exp_descriptor exp_descriptor_rust =
2254 rust_operator_length,
2255 rust_operator_check,
2257 rust_dump_subexp_body,
2258 rust_evaluate_subexp
2261 static const char *rust_extensions[] =
2266 extern const struct language_defn rust_language_defn =
2276 &exp_descriptor_rust,
2280 rust_printchar, /* Print a character constant */
2281 rust_printstr, /* Function to print string constant */
2282 rust_emitchar, /* Print a single char */
2283 rust_print_type, /* Print a type using appropriate syntax */
2284 rust_print_typedef, /* Print a typedef using appropriate syntax */
2285 rust_val_print, /* Print a value using appropriate syntax */
2286 c_value_print, /* Print a top-level value */
2287 default_read_var_value, /* la_read_var_value */
2288 NULL, /* Language specific skip_trampoline */
2289 NULL, /* name_of_this */
2290 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2291 basic_lookup_transparent_type,/* lookup_transparent_type */
2292 gdb_demangle, /* Language specific symbol demangler */
2293 rust_sniff_from_mangled_name,
2294 NULL, /* Language specific
2295 class_name_from_physname */
2296 c_op_print_tab, /* expression operators for printing */
2297 1, /* c-style arrays */
2298 0, /* String lower bound */
2299 default_word_break_characters,
2300 default_collect_symbol_completion_matches,
2301 rust_language_arch_info,
2302 default_print_array_index,
2303 default_pass_by_reference,
2305 rust_watch_location_expression,
2306 NULL, /* la_get_symbol_name_matcher */
2307 iterate_over_symbols,
2308 default_search_name_hash,
2309 &default_varobj_ops,