1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2018 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"
34 #include "typeprint.h"
41 /* See rust-lang.h. */
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 /* Return true if TYPE, which must be a struct type, represents a Rust
70 rust_enum_p (const struct type *type)
72 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
73 && TYPE_NFIELDS (type) == 1
74 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type, 0)));
77 /* Given an enum type and contents, find which variant is active. */
80 rust_enum_variant (struct type *type, const gdb_byte *contents)
82 /* In Rust the enum always fills the containing structure. */
83 gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0);
85 struct type *union_type = TYPE_FIELD_TYPE (type, 0);
87 int fieldno = value_union_variant (union_type, contents);
88 return &TYPE_FIELD (union_type, fieldno);
91 /* See rust-lang.h. */
94 rust_tuple_type_p (struct type *type)
96 /* The current implementation is a bit of a hack, but there's
97 nothing else in the debuginfo to distinguish a tuple from a
99 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
100 && TYPE_NAME (type) != NULL
101 && TYPE_NAME (type)[0] == '(');
104 /* Return true if all non-static fields of a structlike type are in a
105 sequence like __0, __1, __2. */
108 rust_underscore_fields (struct type *type)
114 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
116 for (i = 0; i < TYPE_NFIELDS (type); ++i)
118 if (!field_is_static (&TYPE_FIELD (type, i)))
122 xsnprintf (buf, sizeof (buf), "__%d", field_number);
123 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
131 /* See rust-lang.h. */
134 rust_tuple_struct_type_p (struct type *type)
136 /* This is just an approximation until DWARF can represent Rust more
137 precisely. We exclude zero-length structs because they may not
138 be tuple structs, and there's no way to tell. */
139 return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type);
142 /* Return true if TYPE is a slice type, otherwise false. */
145 rust_slice_type_p (struct type *type)
147 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
148 && TYPE_NAME (type) != NULL
149 && (strncmp (TYPE_NAME (type), "&[", 2) == 0
150 || strcmp (TYPE_NAME (type), "&str") == 0));
153 /* Return true if TYPE is a range type, otherwise false. */
156 rust_range_type_p (struct type *type)
160 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
161 || TYPE_NFIELDS (type) > 2
162 || TYPE_NAME (type) == NULL
163 || strstr (TYPE_NAME (type), "::Range") == NULL)
166 if (TYPE_NFIELDS (type) == 0)
170 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
172 if (TYPE_NFIELDS (type) == 1)
176 else if (TYPE_NFIELDS (type) == 2)
178 /* First field had to be "start". */
182 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
185 /* Return true if TYPE is an inclusive range type, otherwise false.
186 This is only valid for types which are already known to be range
190 rust_inclusive_range_type_p (struct type *type)
192 return (strstr (TYPE_NAME (type), "::RangeInclusive") != NULL
193 || strstr (TYPE_NAME (type), "::RangeToInclusive") != NULL);
196 /* Return true if TYPE seems to be the type "u8", otherwise false. */
199 rust_u8_type_p (struct type *type)
201 return (TYPE_CODE (type) == TYPE_CODE_INT
202 && TYPE_UNSIGNED (type)
203 && TYPE_LENGTH (type) == 1);
206 /* Return true if TYPE is a Rust character type. */
209 rust_chartype_p (struct type *type)
211 return (TYPE_CODE (type) == TYPE_CODE_CHAR
212 && TYPE_LENGTH (type) == 4
213 && TYPE_UNSIGNED (type));
216 /* If VALUE represents a trait object pointer, return the underlying
217 pointer with the correct (i.e., runtime) type. Otherwise, return
220 static struct value *
221 rust_get_trait_object_pointer (struct value *value)
223 struct type *type = check_typedef (value_type (value));
225 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
228 /* Try to be a bit resilient if the ABI changes. */
229 int vtable_field = 0;
230 for (int i = 0; i < 2; ++i)
232 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
234 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
238 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
239 struct symbol *symbol = find_symbol_at_address (vtable);
240 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
243 struct rust_vtable_symbol *vtable_sym
244 = static_cast<struct rust_vtable_symbol *> (symbol);
245 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
246 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
251 /* la_emitchar implementation for Rust. */
254 rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
256 if (!rust_chartype_p (type))
257 generic_emit_char (c, type, stream, quoter,
258 target_charset (get_type_arch (type)));
259 else if (c == '\\' || c == quoter)
260 fprintf_filtered (stream, "\\%c", c);
262 fputs_filtered ("\\n", stream);
264 fputs_filtered ("\\r", stream);
266 fputs_filtered ("\\t", stream);
268 fputs_filtered ("\\0", stream);
269 else if (c >= 32 && c <= 127 && isprint (c))
270 fputc_filtered (c, stream);
272 fprintf_filtered (stream, "\\x%02x", c);
274 fprintf_filtered (stream, "\\u{%06x}", c);
277 /* la_printchar implementation for Rust. */
280 rust_printchar (int c, struct type *type, struct ui_file *stream)
282 fputs_filtered ("'", stream);
283 LA_EMIT_CHAR (c, type, stream, '\'');
284 fputs_filtered ("'", stream);
287 /* la_printstr implementation for Rust. */
290 rust_printstr (struct ui_file *stream, struct type *type,
291 const gdb_byte *string, unsigned int length,
292 const char *user_encoding, int force_ellipses,
293 const struct value_print_options *options)
295 /* Rust always uses UTF-8, but let the caller override this if need
297 const char *encoding = user_encoding;
298 if (user_encoding == NULL || !*user_encoding)
300 /* In Rust strings, characters are "u8". */
301 if (rust_u8_type_p (type))
305 /* This is probably some C string, so let's let C deal with
307 c_printstr (stream, type, string, length, user_encoding,
308 force_ellipses, options);
313 /* This is not ideal as it doesn't use our character printer. */
314 generic_printstr (stream, type, string, length, encoding, force_ellipses,
320 /* Helper function to print a string slice. */
323 rust_val_print_str (struct ui_file *stream, struct value *val,
324 const struct value_print_options *options)
326 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
328 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
330 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
331 value_as_address (base), value_as_long (len), stream,
335 /* rust_val_print helper for structs and untagged unions. */
338 val_print_struct (struct type *type, int embedded_offset,
339 CORE_ADDR address, struct ui_file *stream,
340 int recurse, struct value *val,
341 const struct value_print_options *options)
346 if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
348 rust_val_print_str (stream, val, options);
352 bool is_tuple = rust_tuple_type_p (type);
353 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
354 struct value_print_options opts;
358 if (TYPE_NAME (type) != NULL)
359 fprintf_filtered (stream, "%s", TYPE_NAME (type));
361 if (TYPE_NFIELDS (type) == 0)
364 if (TYPE_NAME (type) != NULL)
365 fputs_filtered (" ", stream);
368 if (is_tuple || is_tuple_struct)
369 fputs_filtered ("(", stream);
371 fputs_filtered ("{", stream);
377 for (i = 0; i < TYPE_NFIELDS (type); ++i)
379 if (field_is_static (&TYPE_FIELD (type, i)))
383 fputs_filtered (",", stream);
385 if (options->prettyformat)
387 fputs_filtered ("\n", stream);
388 print_spaces_filtered (2 + 2 * recurse, stream);
390 else if (!first_field)
391 fputs_filtered (" ", stream);
395 if (!is_tuple && !is_tuple_struct)
397 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
398 fputs_filtered (": ", stream);
401 val_print (TYPE_FIELD_TYPE (type, i),
402 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
404 stream, recurse + 1, val, &opts,
408 if (options->prettyformat)
410 fputs_filtered ("\n", stream);
411 print_spaces_filtered (2 * recurse, stream);
414 if (is_tuple || is_tuple_struct)
415 fputs_filtered (")", stream);
417 fputs_filtered ("}", stream);
420 /* rust_val_print helper for discriminated unions (Rust enums). */
423 rust_print_enum (struct type *type, int embedded_offset,
424 CORE_ADDR address, struct ui_file *stream,
425 int recurse, struct value *val,
426 const struct value_print_options *options)
428 struct value_print_options opts = *options;
432 const gdb_byte *valaddr = value_contents_for_printing (val);
433 struct field *variant_field = rust_enum_variant (type, valaddr);
434 embedded_offset += FIELD_BITPOS (*variant_field) / 8;
435 struct type *variant_type = FIELD_TYPE (*variant_field);
437 int nfields = TYPE_NFIELDS (variant_type);
439 bool is_tuple = rust_tuple_struct_type_p (variant_type);
441 fprintf_filtered (stream, "%s", TYPE_NAME (variant_type));
444 /* In case of a nullary variant like 'None', just output
449 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
451 fprintf_filtered (stream, "(");
454 /* struct variant. */
455 fprintf_filtered (stream, "{");
458 bool first_field = true;
459 for (int j = 0; j < TYPE_NFIELDS (variant_type); j++)
462 fputs_filtered (", ", stream);
466 fprintf_filtered (stream, "%s: ",
467 TYPE_FIELD_NAME (variant_type, j));
469 val_print (TYPE_FIELD_TYPE (variant_type, j),
471 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
473 stream, recurse + 1, val, &opts,
478 fputs_filtered (")", stream);
480 fputs_filtered ("}", stream);
483 static const struct generic_val_print_decorations rust_decorations =
485 /* Complex isn't used in Rust, but we provide C-ish values just in
497 /* la_val_print implementation for Rust. */
500 rust_val_print (struct type *type, int embedded_offset,
501 CORE_ADDR address, struct ui_file *stream, int recurse,
503 const struct value_print_options *options)
505 const gdb_byte *valaddr = value_contents_for_printing (val);
507 type = check_typedef (type);
508 switch (TYPE_CODE (type))
512 LONGEST low_bound, high_bound;
514 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
515 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
516 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
518 /* We have a pointer to a byte string, so just print
520 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
522 struct gdbarch *arch = get_type_arch (type);
523 int unit_size = gdbarch_addressable_memory_unit_size (arch);
525 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
526 if (options->addressprint)
528 fputs_filtered (paddress (arch, addr), stream);
529 fputs_filtered (" ", stream);
532 fputs_filtered ("b", stream);
533 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
534 high_bound - low_bound + 1, stream,
541 case TYPE_CODE_METHODPTR:
542 case TYPE_CODE_MEMBERPTR:
543 c_val_print (type, embedded_offset, address, stream,
544 recurse, val, options);
548 /* Recognize the unit type. */
549 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
550 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
552 fputs_filtered ("()", stream);
557 case TYPE_CODE_STRING:
559 struct gdbarch *arch = get_type_arch (type);
560 int unit_size = gdbarch_addressable_memory_unit_size (arch);
561 LONGEST low_bound, high_bound;
563 if (!get_array_bounds (type, &low_bound, &high_bound))
564 error (_("Could not determine the array bounds"));
566 /* If we see a plain TYPE_CODE_STRING, then we're printing a
567 byte string, hence the choice of "ASCII" as the
569 fputs_filtered ("b", stream);
570 rust_printstr (stream, TYPE_TARGET_TYPE (type),
571 valaddr + embedded_offset * unit_size,
572 high_bound - low_bound + 1, "ASCII", 0, options);
576 case TYPE_CODE_ARRAY:
578 LONGEST low_bound, high_bound;
580 if (get_array_bounds (type, &low_bound, &high_bound)
581 && high_bound - low_bound + 1 == 0)
582 fputs_filtered ("[]", stream);
588 case TYPE_CODE_UNION:
589 /* Untagged unions are printed as if they are structs. Since
590 the field bit positions overlap in the debuginfo, the code
591 for printing a union is same as that for a struct, the only
592 difference is that the input type will have overlapping
594 val_print_struct (type, embedded_offset, address, stream,
595 recurse, val, options);
598 case TYPE_CODE_STRUCT:
599 if (rust_enum_p (type))
600 rust_print_enum (type, embedded_offset, address, stream,
601 recurse, val, options);
603 val_print_struct (type, embedded_offset, address, stream,
604 recurse, val, options);
609 /* Nothing special yet. */
610 generic_val_print (type, embedded_offset, address, stream,
611 recurse, val, options, &rust_decorations);
618 rust_internal_print_type (struct type *type, const char *varstring,
619 struct ui_file *stream, int show, int level,
620 const struct type_print_options *flags,
621 bool for_rust_enum, print_offset_data *podata);
623 /* Print a struct or union typedef. */
625 rust_print_struct_def (struct type *type, const char *varstring,
626 struct ui_file *stream, int show, int level,
627 const struct type_print_options *flags,
628 bool for_rust_enum, print_offset_data *podata)
630 /* Print a tuple type simply. */
631 if (rust_tuple_type_p (type))
633 fputs_filtered (TYPE_NAME (type), stream);
637 /* If we see a base class, delegate to C. */
638 if (TYPE_N_BASECLASSES (type) > 0)
639 c_print_type (type, varstring, stream, show, level, flags);
641 if (flags->print_offsets)
643 /* Temporarily bump the level so that the output lines up
648 /* Compute properties of TYPE here because, in the enum case, the
649 rest of the code ends up looking only at the variant part. */
650 const char *tagname = TYPE_NAME (type);
651 bool is_tuple_struct = rust_tuple_struct_type_p (type);
652 bool is_tuple = rust_tuple_type_p (type);
653 bool is_enum = rust_enum_p (type);
655 int enum_discriminant_index = -1;
659 /* Already printing an outer enum, so nothing to print here. */
663 /* This code path is also used by unions and enums. */
666 fputs_filtered ("enum ", stream);
667 type = TYPE_FIELD_TYPE (type, 0);
669 struct dynamic_prop *discriminant_prop
670 = get_dyn_prop (DYN_PROP_DISCRIMINATED, type);
671 struct discriminant_info *info
672 = (struct discriminant_info *) discriminant_prop->data.baton;
673 enum_discriminant_index = info->discriminant_index;
675 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
676 fputs_filtered ("struct ", stream);
678 fputs_filtered ("union ", stream);
681 fputs_filtered (tagname, stream);
684 if (TYPE_NFIELDS (type) == 0 && !is_tuple)
686 if (for_rust_enum && !flags->print_offsets)
687 fputs_filtered (is_tuple_struct ? "(" : "{", stream);
689 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
691 /* When printing offsets, we rearrange the fields into storage
692 order. This lets us show holes more clearly. We work using
693 field indices here because it simplifies calls to
694 print_offset_data::update below. */
695 std::vector<int> fields;
696 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
698 if (field_is_static (&TYPE_FIELD (type, i)))
700 if (is_enum && i == enum_discriminant_index)
702 fields.push_back (i);
704 if (flags->print_offsets)
705 std::sort (fields.begin (), fields.end (),
708 return (TYPE_FIELD_BITPOS (type, a)
709 < TYPE_FIELD_BITPOS (type, b));
716 gdb_assert (!field_is_static (&TYPE_FIELD (type, i)));
717 gdb_assert (! (is_enum && i == enum_discriminant_index));
719 if (flags->print_offsets)
720 podata->update (type, i, stream);
722 /* We'd like to print "pub" here as needed, but rustc
723 doesn't emit the debuginfo, and our types don't have
724 cplus_struct_type attached. */
726 /* For a tuple struct we print the type but nothing
728 if (!for_rust_enum || flags->print_offsets)
729 print_spaces_filtered (level + 2, stream);
731 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
732 else if (!is_tuple_struct)
733 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
735 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL,
736 stream, (is_enum ? show : show - 1),
737 level + 2, flags, is_enum, podata);
738 if (!for_rust_enum || flags->print_offsets)
739 fputs_filtered (",\n", stream);
740 /* Note that this check of "I" is ok because we only sorted the
741 fields by offset when print_offsets was set, so we won't take
742 this branch in that case. */
743 else if (i + 1 < TYPE_NFIELDS (type))
744 fputs_filtered (", ", stream);
747 if (flags->print_offsets)
749 /* Undo the temporary level increase we did above. */
751 podata->finish (type, level, stream);
752 print_spaces_filtered (print_offset_data::indentation, stream);
754 print_spaces_filtered (2, stream);
756 if (!for_rust_enum || flags->print_offsets)
757 print_spaces_filtered (level, stream);
758 fputs_filtered (is_tuple_struct ? ")" : "}", stream);
761 /* la_print_typedef implementation for Rust. */
764 rust_print_typedef (struct type *type,
765 struct symbol *new_symbol,
766 struct ui_file *stream)
768 type = check_typedef (type);
769 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
770 type_print (type, "", stream, 0);
771 fprintf_filtered (stream, ";\n");
774 /* la_print_type implementation for Rust. */
777 rust_internal_print_type (struct type *type, const char *varstring,
778 struct ui_file *stream, int show, int level,
779 const struct type_print_options *flags,
780 bool for_rust_enum, print_offset_data *podata)
786 && TYPE_NAME (type) != NULL)
788 /* Rust calls the unit type "void" in its debuginfo,
789 but we don't want to print it as that. */
790 if (TYPE_CODE (type) == TYPE_CODE_VOID)
791 fputs_filtered ("()", stream);
793 fputs_filtered (TYPE_NAME (type), stream);
797 type = check_typedef (type);
798 switch (TYPE_CODE (type))
801 /* If we have an enum, we've already printed the type's
802 unqualified name, and there is nothing else to print
805 fputs_filtered ("()", stream);
809 /* Delegate varargs to the C printer. */
810 if (TYPE_VARARGS (type))
813 fputs_filtered ("fn ", stream);
814 if (varstring != NULL)
815 fputs_filtered (varstring, stream);
816 fputs_filtered ("(", stream);
817 for (i = 0; i < TYPE_NFIELDS (type); ++i)
821 fputs_filtered (", ", stream);
822 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), "", stream,
823 -1, 0, flags, false, podata);
825 fputs_filtered (")", stream);
826 /* If it returns unit, we can omit the return type. */
827 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
829 fputs_filtered (" -> ", stream);
830 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
831 -1, 0, flags, false, podata);
835 case TYPE_CODE_ARRAY:
837 LONGEST low_bound, high_bound;
839 fputs_filtered ("[", stream);
840 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
841 stream, show - 1, level, flags, false,
844 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
845 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
846 fprintf_filtered (stream, "; variable length");
847 else if (get_array_bounds (type, &low_bound, &high_bound))
848 fprintf_filtered (stream, "; %s",
849 plongest (high_bound - low_bound + 1));
850 fputs_filtered ("]", stream);
854 case TYPE_CODE_UNION:
855 case TYPE_CODE_STRUCT:
856 rust_print_struct_def (type, varstring, stream, show, level, flags,
857 for_rust_enum, podata);
864 fputs_filtered ("enum ", stream);
865 if (TYPE_NAME (type) != NULL)
867 fputs_filtered (TYPE_NAME (type), stream);
868 fputs_filtered (" ", stream);
869 len = strlen (TYPE_NAME (type));
871 fputs_filtered ("{\n", stream);
873 for (i = 0; i < TYPE_NFIELDS (type); ++i)
875 const char *name = TYPE_FIELD_NAME (type, i);
880 && strncmp (name, TYPE_NAME (type), len) == 0
882 && name[len + 1] == ':')
884 fprintfi_filtered (level + 2, stream, "%s,\n", name);
887 fputs_filtered ("}", stream);
893 c_print_type (type, varstring, stream, show, level, flags);
898 rust_print_type (struct type *type, const char *varstring,
899 struct ui_file *stream, int show, int level,
900 const struct type_print_options *flags)
902 print_offset_data podata;
903 rust_internal_print_type (type, varstring, stream, show, level,
904 flags, false, &podata);
909 /* Like arch_composite_type, but uses TYPE to decide how to allocate
910 -- either on an obstack or on a gdbarch. */
913 rust_composite_type (struct type *original,
915 const char *field1, struct type *type1,
916 const char *field2, struct type *type2)
918 struct type *result = alloc_type_copy (original);
919 int i, nfields, bitpos;
927 TYPE_CODE (result) = TYPE_CODE_STRUCT;
928 TYPE_NAME (result) = name;
930 TYPE_NFIELDS (result) = nfields;
932 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
938 struct field *field = &TYPE_FIELD (result, i);
940 SET_FIELD_BITPOS (*field, bitpos);
941 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
943 FIELD_NAME (*field) = field1;
944 FIELD_TYPE (*field) = type1;
949 struct field *field = &TYPE_FIELD (result, i);
950 unsigned align = type_align (type2);
956 align *= TARGET_CHAR_BIT;
957 delta = bitpos % align;
959 bitpos += align - delta;
961 SET_FIELD_BITPOS (*field, bitpos);
963 FIELD_NAME (*field) = field2;
964 FIELD_TYPE (*field) = type2;
970 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
971 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
975 /* See rust-lang.h. */
978 rust_slice_type (const char *name, struct type *elt_type,
979 struct type *usize_type)
983 elt_type = lookup_pointer_type (elt_type);
984 type = rust_composite_type (elt_type, name,
985 "data_ptr", elt_type,
986 "length", usize_type);
991 enum rust_primitive_types
1003 rust_primitive_isize,
1004 rust_primitive_usize,
1007 rust_primitive_unit,
1009 nr_rust_primitive_types
1012 /* la_language_arch_info implementation for Rust. */
1015 rust_language_arch_info (struct gdbarch *gdbarch,
1016 struct language_arch_info *lai)
1018 const struct builtin_type *builtin = builtin_type (gdbarch);
1020 struct type **types;
1021 unsigned int length;
1023 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1026 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1027 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1028 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1029 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1030 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1031 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1032 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1033 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1034 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1035 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1037 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1038 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1039 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1041 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1042 floatformats_ieee_single);
1043 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1044 floatformats_ieee_double);
1046 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1048 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1049 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1050 types[rust_primitive_usize]);
1052 lai->primitive_type_vector = types;
1053 lai->bool_type_default = types[rust_primitive_bool];
1054 lai->string_char_type = types[rust_primitive_u8];
1059 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1061 static struct value *
1062 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1065 int num_args = exp->elts[*pos + 1].longconst;
1067 struct value *function, *result, *arg0;
1068 struct type *type, *fn_type;
1069 const struct block *block;
1070 struct block_symbol sym;
1072 /* For an ordinary function call we can simply defer to the
1073 generic implementation. */
1074 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1075 return evaluate_subexp_standard (NULL, exp, pos, noside);
1077 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1079 method = &exp->elts[*pos + 1].string;
1080 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1082 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1083 type in order to look up the method. */
1084 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1086 if (noside == EVAL_SKIP)
1088 for (i = 0; i < num_args; ++i)
1089 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1093 std::vector<struct value *> args (num_args + 1);
1096 /* We don't yet implement real Deref semantics. */
1097 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1098 args[0] = value_ind (args[0]);
1100 type = value_type (args[0]);
1101 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1102 && TYPE_CODE (type) != TYPE_CODE_UNION
1103 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1104 || rust_tuple_type_p (type))
1105 error (_("Method calls only supported on struct or enum types"));
1106 if (TYPE_NAME (type) == NULL)
1107 error (_("Method call on nameless type"));
1109 std::string name = std::string (TYPE_NAME (type)) + "::" + method;
1111 block = get_selected_block (0);
1112 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1113 if (sym.symbol == NULL)
1114 error (_("Could not find function named '%s'"), name.c_str ());
1116 fn_type = SYMBOL_TYPE (sym.symbol);
1117 if (TYPE_NFIELDS (fn_type) == 0)
1118 error (_("Function '%s' takes no arguments"), name.c_str ());
1120 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1121 args[0] = value_addr (args[0]);
1123 function = address_of_variable (sym.symbol, block);
1125 for (i = 0; i < num_args; ++i)
1126 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1128 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1129 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1131 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
1135 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1137 static struct value *
1138 rust_range (struct expression *exp, int *pos, enum noside noside)
1140 enum range_type kind;
1141 struct value *low = NULL, *high = NULL;
1142 struct value *addrval, *result;
1144 struct type *range_type;
1145 struct type *index_type;
1146 struct type *temp_type;
1149 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1152 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
1153 || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1154 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1155 if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
1156 || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1157 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1158 bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
1160 if (noside == EVAL_SKIP)
1161 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1168 name = "std::ops::RangeFull";
1172 index_type = value_type (high);
1174 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1181 index_type = value_type (low);
1182 name = "std::ops::RangeFrom";
1186 if (!types_equal (value_type (low), value_type (high)))
1187 error (_("Range expression with different types"));
1188 index_type = value_type (low);
1189 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1193 /* If we don't have an index type, just allocate this on the
1194 arch. Here any type will do. */
1195 temp_type = (index_type == NULL
1196 ? language_bool_type (exp->language_defn, exp->gdbarch)
1198 /* It would be nicer to cache the range type. */
1199 range_type = rust_composite_type (temp_type, name,
1200 low == NULL ? NULL : "start", index_type,
1201 high == NULL ? NULL : "end", index_type);
1203 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1204 return value_zero (range_type, lval_memory);
1206 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1207 addr = value_as_long (addrval);
1208 result = value_at_lazy (range_type, addr);
1212 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1215 value_assign (start, low);
1220 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1223 value_assign (end, high);
1226 result = value_at_lazy (range_type, addr);
1230 /* A helper function to compute the range and kind given a range
1231 value. TYPE is the type of the range value. RANGE is the range
1232 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1233 parameters might be filled in, or might not be, depending on the
1234 kind of range this is. KIND will always be set to the appropriate
1235 value describing the kind of range, and this can be used to
1236 determine whether LOW or HIGH are valid. */
1239 rust_compute_range (struct type *type, struct value *range,
1240 LONGEST *low, LONGEST *high,
1241 enum range_type *kind)
1247 *kind = BOTH_BOUND_DEFAULT;
1249 if (TYPE_NFIELDS (type) == 0)
1253 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1255 *kind = HIGH_BOUND_DEFAULT;
1256 *low = value_as_long (value_field (range, 0));
1259 if (TYPE_NFIELDS (type) > i
1260 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1262 *kind = (*kind == BOTH_BOUND_DEFAULT
1263 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1264 *high = value_as_long (value_field (range, i));
1266 if (rust_inclusive_range_type_p (type))
1271 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1273 static struct value *
1274 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1277 struct value *lhs, *rhs, *result;
1278 struct type *rhstype;
1279 LONGEST low, high_bound;
1280 /* Initialized to appease the compiler. */
1281 enum range_type kind = BOTH_BOUND_DEFAULT;
1286 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1287 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1289 if (noside == EVAL_SKIP)
1292 rhstype = check_typedef (value_type (rhs));
1293 if (rust_range_type_p (rhstype))
1296 error (_("Can't take slice of array without '&'"));
1297 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1301 low = value_as_long (rhs);
1303 struct type *type = check_typedef (value_type (lhs));
1304 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1306 struct type *base_type = nullptr;
1307 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1308 base_type = TYPE_TARGET_TYPE (type);
1309 else if (rust_slice_type_p (type))
1311 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1313 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1315 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1319 if (base_type == nullptr)
1320 error (_("Could not find 'data_ptr' in slice type"));
1322 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1323 base_type = TYPE_TARGET_TYPE (type);
1325 error (_("Cannot subscript non-array type"));
1327 struct type *new_type;
1330 if (rust_slice_type_p (type))
1335 = language_lookup_primitive_type (exp->language_defn,
1338 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1342 new_type = base_type;
1344 return value_zero (new_type, VALUE_LVAL (lhs));
1351 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1354 if (!get_array_bounds (type, &low_bound, &high_bound))
1355 error (_("Can't compute array bounds"));
1357 error (_("Found array with non-zero lower bound"));
1360 else if (rust_slice_type_p (type))
1364 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1365 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1367 high_bound = value_as_long (len);
1369 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1373 high_bound = LONGEST_MAX;
1376 error (_("Cannot subscript non-array type"));
1379 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1382 error (_("Index less than zero"));
1383 if (low > high_bound)
1384 error (_("Index greater than length"));
1386 result = value_subscript (base, low);
1393 struct type *usize, *slice;
1395 struct value *addrval, *tem;
1397 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1400 error (_("High index less than zero"));
1402 error (_("Low index greater than high index"));
1403 if (high > high_bound)
1404 error (_("High index greater than length"));
1406 usize = language_lookup_primitive_type (exp->language_defn,
1409 const char *new_name = ((type != nullptr
1410 && rust_slice_type_p (type))
1411 ? TYPE_NAME (type) : "&[*gdb*]");
1413 slice = rust_slice_type (new_name, value_type (result), usize);
1415 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1416 addr = value_as_long (addrval);
1417 tem = value_at_lazy (slice, addr);
1419 value_assign (value_field (tem, 0), value_addr (result));
1420 value_assign (value_field (tem, 1),
1421 value_from_longest (usize, high - low));
1423 result = value_at_lazy (slice, addr);
1426 result = value_addr (result);
1432 /* evaluate_exp implementation for Rust. */
1434 static struct value *
1435 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1436 int *pos, enum noside noside)
1438 struct value *result;
1440 switch (exp->elts[*pos].opcode)
1444 if (noside != EVAL_NORMAL)
1445 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1449 struct value *value = evaluate_subexp (expect_type, exp, pos,
1452 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1453 if (trait_ptr != NULL)
1456 result = value_ind (value);
1461 case UNOP_COMPLEMENT:
1463 struct value *value;
1466 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1467 if (noside == EVAL_SKIP)
1469 /* Preserving the type is enough. */
1472 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1473 result = value_from_longest (value_type (value),
1474 value_logical_not (value));
1476 result = value_complement (value);
1480 case BINOP_SUBSCRIPT:
1481 result = rust_subscript (exp, pos, noside, 0);
1485 result = rust_evaluate_funcall (exp, pos, noside);
1491 struct type *type = exp->elts[pc + 1].type;
1492 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1495 struct value *addrval = NULL;
1499 if (noside == EVAL_NORMAL)
1501 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1502 addr = value_as_long (addrval);
1503 result = value_at_lazy (type, addr);
1506 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1511 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1512 if (noside == EVAL_NORMAL)
1514 /* This isn't quite right but will do for the time
1515 being, seeing that we can't implement the Copy
1517 value_assign (result, init);
1523 gdb_assert (arglen % 2 == 0);
1524 for (i = 0; i < arglen; i += 2)
1527 const char *fieldname;
1528 struct value *value, *field;
1530 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1532 len = longest_to_int (exp->elts[*pos].longconst);
1534 fieldname = &exp->elts[*pos].string;
1535 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1537 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1538 if (noside == EVAL_NORMAL)
1540 field = value_struct_elt (&result, NULL, fieldname, NULL,
1542 value_assign (field, value);
1546 if (noside == EVAL_SKIP)
1547 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1549 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1550 result = allocate_value (type);
1552 result = value_at_lazy (type, addr);
1561 struct value *ncopies;
1563 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1564 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1565 copies = value_as_long (ncopies);
1567 error (_("Array with negative number of elements"));
1569 if (noside == EVAL_NORMAL)
1572 std::vector<struct value *> eltvec (copies);
1574 for (i = 0; i < copies; ++i)
1576 result = value_array (0, copies - 1, eltvec.data ());
1580 struct type *arraytype
1581 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1582 result = allocate_value (arraytype);
1587 case STRUCTOP_ANONYMOUS:
1589 /* Anonymous field access, i.e. foo.1. */
1591 int pc, field_number, nfields;
1592 struct type *type, *variant_type;
1595 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1597 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1599 type = value_type (lhs);
1601 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1603 struct type *outer_type = NULL;
1605 if (rust_enum_p (type))
1607 const gdb_byte *valaddr = value_contents (lhs);
1608 struct field *variant_field = rust_enum_variant (type, valaddr);
1610 struct value *union_value = value_primitive_field (lhs, 0, 0,
1613 int fieldno = (variant_field
1614 - &TYPE_FIELD (value_type (union_value), 0));
1615 lhs = value_primitive_field (union_value, 0, fieldno,
1616 value_type (union_value));
1618 type = value_type (lhs);
1621 /* Tuples and tuple structs */
1622 nfields = TYPE_NFIELDS (type);
1624 if (field_number >= nfields || field_number < 0)
1626 if (outer_type != NULL)
1627 error(_("Cannot access field %d of variant %s::%s, "
1628 "there are only %d fields"),
1629 field_number, TYPE_NAME (outer_type),
1630 rust_last_path_segment (TYPE_NAME (type)),
1633 error(_("Cannot access field %d of %s, "
1634 "there are only %d fields"),
1635 field_number, TYPE_NAME (type), nfields);
1638 /* Tuples are tuple structs too. */
1639 if (!rust_tuple_struct_type_p (type))
1641 if (outer_type != NULL)
1642 error(_("Variant %s::%s is not a tuple variant"),
1643 TYPE_NAME (outer_type),
1644 rust_last_path_segment (TYPE_NAME (type)));
1646 error(_("Attempting to access anonymous field %d "
1647 "of %s, which is not a tuple, tuple struct, or "
1648 "tuple-like variant"),
1649 field_number, TYPE_NAME (type));
1652 result = value_primitive_field (lhs, 0, field_number, type);
1655 error(_("Anonymous field access is only allowed on tuples, \
1656 tuple structs, and tuple-like enum variants"));
1660 case STRUCTOP_STRUCT:
1667 tem = longest_to_int (exp->elts[pc + 1].longconst);
1668 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1669 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1671 const char *field_name = &exp->elts[pc + 2].string;
1672 type = value_type (lhs);
1673 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type))
1675 const gdb_byte *valaddr = value_contents (lhs);
1676 struct field *variant_field = rust_enum_variant (type, valaddr);
1678 struct value *union_value = value_primitive_field (lhs, 0, 0,
1681 int fieldno = (variant_field
1682 - &TYPE_FIELD (value_type (union_value), 0));
1683 lhs = value_primitive_field (union_value, 0, fieldno,
1684 value_type (union_value));
1686 struct type *outer_type = type;
1687 type = value_type (lhs);
1688 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1689 error (_("Attempting to access named field foo of tuple "
1690 "variant %s::%s, which has only anonymous fields"),
1691 TYPE_NAME (outer_type),
1692 rust_last_path_segment (TYPE_NAME (type)));
1696 result = value_struct_elt (&lhs, NULL, field_name,
1699 CATCH (except, RETURN_MASK_ERROR)
1701 error (_("Could not find field %s of struct variant %s::%s"),
1702 field_name, TYPE_NAME (outer_type),
1703 rust_last_path_segment (TYPE_NAME (type)));
1708 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1709 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1710 result = value_zero (value_type (result), VALUE_LVAL (result));
1715 result = rust_range (exp, pos, noside);
1719 /* We might have &array[range], in which case we need to make a
1721 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1724 result = rust_subscript (exp, pos, noside, 1);
1729 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1736 /* operator_length implementation for Rust. */
1739 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1745 switch (exp->elts[pc - 1].opcode)
1748 /* We handle aggregate as a type and argument count. The first
1749 argument might be OP_OTHERS. After that the arguments
1750 alternate: first an OP_NAME, then an expression. */
1752 args = longest_to_int (exp->elts[pc - 2].longconst);
1760 case STRUCTOP_ANONYMOUS:
1771 operator_length_standard (exp, pc, oplenp, argsp);
1779 /* op_name implementation for Rust. */
1782 rust_op_name (enum exp_opcode opcode)
1787 return "OP_AGGREGATE";
1791 return op_name_standard (opcode);
1795 /* dump_subexp_body implementation for Rust. */
1798 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1801 switch (exp->elts[elt].opcode)
1805 int length = longest_to_int (exp->elts[elt + 2].longconst);
1808 fprintf_filtered (stream, "Type @");
1809 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1810 fprintf_filtered (stream, " (");
1811 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1812 fprintf_filtered (stream, "), length %d", length);
1815 for (i = 0; i < length; ++i)
1816 elt = dump_subexp (exp, stream, elt);
1823 LONGEST len = exp->elts[elt + 1].longconst;
1825 fprintf_filtered (stream, "%s: %s",
1826 (exp->elts[elt].opcode == OP_STRING
1827 ? "string" : "name"),
1828 &exp->elts[elt + 2].string);
1829 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1834 elt = dump_subexp (exp, stream, elt + 1);
1837 case STRUCTOP_ANONYMOUS:
1841 field_number = longest_to_int (exp->elts[elt + 1].longconst);
1843 fprintf_filtered (stream, "Field number: %d", field_number);
1844 elt = dump_subexp (exp, stream, elt + 3);
1853 elt = dump_subexp_body_standard (exp, stream, elt);
1860 /* print_subexp implementation for Rust. */
1863 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1864 enum precedence prec)
1866 switch (exp->elts[*pos].opcode)
1870 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1873 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1874 fputs_filtered (" { ", stream);
1877 for (i = 0; i < length; ++i)
1879 rust_print_subexp (exp, pos, stream, prec);
1880 fputs_filtered (", ", stream);
1882 fputs_filtered (" }", stream);
1888 LONGEST len = exp->elts[*pos + 1].longconst;
1890 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1891 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1897 fputs_filtered ("<<others>> (", stream);
1899 rust_print_subexp (exp, pos, stream, prec);
1900 fputs_filtered (")", stream);
1904 case STRUCTOP_ANONYMOUS:
1906 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1909 print_subexp (exp, pos, stream, PREC_SUFFIX);
1910 fprintf_filtered (stream, ".%d", tem);
1916 fprintf_filtered (stream, "[");
1917 rust_print_subexp (exp, pos, stream, prec);
1918 fprintf_filtered (stream, "; ");
1919 rust_print_subexp (exp, pos, stream, prec);
1920 fprintf_filtered (stream, "]");
1924 print_subexp_standard (exp, pos, stream, prec);
1929 /* operator_check implementation for Rust. */
1932 rust_operator_check (struct expression *exp, int pos,
1933 int (*objfile_func) (struct objfile *objfile,
1937 switch (exp->elts[pos].opcode)
1941 struct type *type = exp->elts[pos + 1].type;
1942 struct objfile *objfile = TYPE_OBJFILE (type);
1944 if (objfile != NULL && (*objfile_func) (objfile, data))
1955 return operator_check_standard (exp, pos, objfile_func, data);
1963 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1965 static struct block_symbol
1966 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
1968 const struct block *block,
1969 const domain_enum domain)
1971 struct block_symbol result = {NULL, NULL};
1973 if (symbol_lookup_debug)
1975 fprintf_unfiltered (gdb_stdlog,
1976 "rust_lookup_symbol_non_local"
1977 " (%s, %s (scope %s), %s)\n",
1978 name, host_address_to_string (block),
1979 block_scope (block), domain_name (domain));
1982 /* Look up bare names in the block's scope. */
1983 std::string scopedname;
1984 if (name[cp_find_first_component (name)] == '\0')
1986 const char *scope = block_scope (block);
1988 if (scope[0] != '\0')
1990 scopedname = std::string (scope) + "::" + name;
1991 name = scopedname.c_str ();
1999 result = lookup_symbol_in_static_block (name, block, domain);
2000 if (result.symbol == NULL)
2001 result = lookup_global_symbol (name, block, domain);
2008 /* la_sniff_from_mangled_name for Rust. */
2011 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2013 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2014 return *demangled != NULL;
2019 /* la_watch_location_expression for Rust. */
2021 static gdb::unique_xmalloc_ptr<char>
2022 rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2024 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2025 std::string name = type_to_string (type);
2026 return gdb::unique_xmalloc_ptr<char>
2027 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2033 static const struct exp_descriptor exp_descriptor_rust =
2036 rust_operator_length,
2037 rust_operator_check,
2039 rust_dump_subexp_body,
2040 rust_evaluate_subexp
2043 static const char *rust_extensions[] =
2048 extern const struct language_defn rust_language_defn =
2058 &exp_descriptor_rust,
2061 rust_printchar, /* Print a character constant */
2062 rust_printstr, /* Function to print string constant */
2063 rust_emitchar, /* Print a single char */
2064 rust_print_type, /* Print a type using appropriate syntax */
2065 rust_print_typedef, /* Print a typedef using appropriate syntax */
2066 rust_val_print, /* Print a value using appropriate syntax */
2067 c_value_print, /* Print a top-level value */
2068 default_read_var_value, /* la_read_var_value */
2069 NULL, /* Language specific skip_trampoline */
2070 NULL, /* name_of_this */
2071 false, /* la_store_sym_names_in_linkage_form_p */
2072 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2073 basic_lookup_transparent_type,/* lookup_transparent_type */
2074 gdb_demangle, /* Language specific symbol demangler */
2075 rust_sniff_from_mangled_name,
2076 NULL, /* Language specific
2077 class_name_from_physname */
2078 c_op_print_tab, /* expression operators for printing */
2079 1, /* c-style arrays */
2080 0, /* String lower bound */
2081 default_word_break_characters,
2082 default_collect_symbol_completion_matches,
2083 rust_language_arch_info,
2084 default_print_array_index,
2085 default_pass_by_reference,
2087 rust_watch_location_expression,
2088 NULL, /* la_get_symbol_name_matcher */
2089 iterate_over_symbols,
2090 default_search_name_hash,
2091 &default_varobj_ops,