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 /* Return true if TYPE, which must be an enum type, has no
81 rust_empty_enum_p (const struct type *type)
83 gdb_assert (rust_enum_p (type));
84 /* In Rust the enum always fills the containing structure. */
85 gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0);
87 return TYPE_NFIELDS (TYPE_FIELD_TYPE (type, 0)) == 0;
90 /* Given an enum type and contents, find which variant is active. */
93 rust_enum_variant (struct type *type, const gdb_byte *contents)
95 /* In Rust the enum always fills the containing structure. */
96 gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0);
98 struct type *union_type = TYPE_FIELD_TYPE (type, 0);
100 int fieldno = value_union_variant (union_type, contents);
101 return &TYPE_FIELD (union_type, fieldno);
104 /* See rust-lang.h. */
107 rust_tuple_type_p (struct type *type)
109 /* The current implementation is a bit of a hack, but there's
110 nothing else in the debuginfo to distinguish a tuple from a
112 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
113 && TYPE_NAME (type) != NULL
114 && TYPE_NAME (type)[0] == '(');
117 /* Return true if all non-static fields of a structlike type are in a
118 sequence like __0, __1, __2. */
121 rust_underscore_fields (struct type *type)
127 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
129 for (i = 0; i < TYPE_NFIELDS (type); ++i)
131 if (!field_is_static (&TYPE_FIELD (type, i)))
135 xsnprintf (buf, sizeof (buf), "__%d", field_number);
136 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
144 /* See rust-lang.h. */
147 rust_tuple_struct_type_p (struct type *type)
149 /* This is just an approximation until DWARF can represent Rust more
150 precisely. We exclude zero-length structs because they may not
151 be tuple structs, and there's no way to tell. */
152 return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type);
155 /* Return true if TYPE is a slice type, otherwise false. */
158 rust_slice_type_p (struct type *type)
160 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
161 && TYPE_NAME (type) != NULL
162 && (strncmp (TYPE_NAME (type), "&[", 2) == 0
163 || strcmp (TYPE_NAME (type), "&str") == 0));
166 /* Return true if TYPE is a range type, otherwise false. */
169 rust_range_type_p (struct type *type)
173 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
174 || TYPE_NFIELDS (type) > 2
175 || TYPE_NAME (type) == NULL
176 || strstr (TYPE_NAME (type), "::Range") == NULL)
179 if (TYPE_NFIELDS (type) == 0)
183 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
185 if (TYPE_NFIELDS (type) == 1)
189 else if (TYPE_NFIELDS (type) == 2)
191 /* First field had to be "start". */
195 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
198 /* Return true if TYPE is an inclusive range type, otherwise false.
199 This is only valid for types which are already known to be range
203 rust_inclusive_range_type_p (struct type *type)
205 return (strstr (TYPE_NAME (type), "::RangeInclusive") != NULL
206 || strstr (TYPE_NAME (type), "::RangeToInclusive") != NULL);
209 /* Return true if TYPE seems to be the type "u8", otherwise false. */
212 rust_u8_type_p (struct type *type)
214 return (TYPE_CODE (type) == TYPE_CODE_INT
215 && TYPE_UNSIGNED (type)
216 && TYPE_LENGTH (type) == 1);
219 /* Return true if TYPE is a Rust character type. */
222 rust_chartype_p (struct type *type)
224 return (TYPE_CODE (type) == TYPE_CODE_CHAR
225 && TYPE_LENGTH (type) == 4
226 && TYPE_UNSIGNED (type));
229 /* If VALUE represents a trait object pointer, return the underlying
230 pointer with the correct (i.e., runtime) type. Otherwise, return
233 static struct value *
234 rust_get_trait_object_pointer (struct value *value)
236 struct type *type = check_typedef (value_type (value));
238 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
241 /* Try to be a bit resilient if the ABI changes. */
242 int vtable_field = 0;
243 for (int i = 0; i < 2; ++i)
245 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
247 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
251 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
252 struct symbol *symbol = find_symbol_at_address (vtable);
253 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
256 struct rust_vtable_symbol *vtable_sym
257 = static_cast<struct rust_vtable_symbol *> (symbol);
258 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
259 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
264 /* la_emitchar implementation for Rust. */
267 rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
269 if (!rust_chartype_p (type))
270 generic_emit_char (c, type, stream, quoter,
271 target_charset (get_type_arch (type)));
272 else if (c == '\\' || c == quoter)
273 fprintf_filtered (stream, "\\%c", c);
275 fputs_filtered ("\\n", stream);
277 fputs_filtered ("\\r", stream);
279 fputs_filtered ("\\t", stream);
281 fputs_filtered ("\\0", stream);
282 else if (c >= 32 && c <= 127 && isprint (c))
283 fputc_filtered (c, stream);
285 fprintf_filtered (stream, "\\x%02x", c);
287 fprintf_filtered (stream, "\\u{%06x}", c);
290 /* la_printchar implementation for Rust. */
293 rust_printchar (int c, struct type *type, struct ui_file *stream)
295 fputs_filtered ("'", stream);
296 LA_EMIT_CHAR (c, type, stream, '\'');
297 fputs_filtered ("'", stream);
300 /* la_printstr implementation for Rust. */
303 rust_printstr (struct ui_file *stream, struct type *type,
304 const gdb_byte *string, unsigned int length,
305 const char *user_encoding, int force_ellipses,
306 const struct value_print_options *options)
308 /* Rust always uses UTF-8, but let the caller override this if need
310 const char *encoding = user_encoding;
311 if (user_encoding == NULL || !*user_encoding)
313 /* In Rust strings, characters are "u8". */
314 if (rust_u8_type_p (type))
318 /* This is probably some C string, so let's let C deal with
320 c_printstr (stream, type, string, length, user_encoding,
321 force_ellipses, options);
326 /* This is not ideal as it doesn't use our character printer. */
327 generic_printstr (stream, type, string, length, encoding, force_ellipses,
333 /* Helper function to print a string slice. */
336 rust_val_print_str (struct ui_file *stream, struct value *val,
337 const struct value_print_options *options)
339 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
341 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
343 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
344 value_as_address (base), value_as_long (len), stream,
348 /* rust_val_print helper for structs and untagged unions. */
351 val_print_struct (struct type *type, int embedded_offset,
352 CORE_ADDR address, struct ui_file *stream,
353 int recurse, struct value *val,
354 const struct value_print_options *options)
359 if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
361 rust_val_print_str (stream, val, options);
365 bool is_tuple = rust_tuple_type_p (type);
366 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
367 struct value_print_options opts;
371 if (TYPE_NAME (type) != NULL)
372 fprintf_filtered (stream, "%s", TYPE_NAME (type));
374 if (TYPE_NFIELDS (type) == 0)
377 if (TYPE_NAME (type) != NULL)
378 fputs_filtered (" ", stream);
381 if (is_tuple || is_tuple_struct)
382 fputs_filtered ("(", stream);
384 fputs_filtered ("{", stream);
390 for (i = 0; i < TYPE_NFIELDS (type); ++i)
392 if (field_is_static (&TYPE_FIELD (type, i)))
396 fputs_filtered (",", stream);
398 if (options->prettyformat)
400 fputs_filtered ("\n", stream);
401 print_spaces_filtered (2 + 2 * recurse, stream);
403 else if (!first_field)
404 fputs_filtered (" ", stream);
408 if (!is_tuple && !is_tuple_struct)
410 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
411 fputs_filtered (": ", stream);
414 val_print (TYPE_FIELD_TYPE (type, i),
415 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
417 stream, recurse + 1, val, &opts,
421 if (options->prettyformat)
423 fputs_filtered ("\n", stream);
424 print_spaces_filtered (2 * recurse, stream);
427 if (is_tuple || is_tuple_struct)
428 fputs_filtered (")", stream);
430 fputs_filtered ("}", stream);
433 /* rust_val_print helper for discriminated unions (Rust enums). */
436 rust_print_enum (struct type *type, int embedded_offset,
437 CORE_ADDR address, struct ui_file *stream,
438 int recurse, struct value *val,
439 const struct value_print_options *options)
441 struct value_print_options opts = *options;
445 if (rust_empty_enum_p (type))
447 /* Print the enum type name here to be more clear. */
448 fprintf_filtered (stream, _("%s {<No data fields>}"), TYPE_NAME (type));
452 const gdb_byte *valaddr = value_contents_for_printing (val);
453 struct field *variant_field = rust_enum_variant (type, valaddr);
454 embedded_offset += FIELD_BITPOS (*variant_field) / 8;
455 struct type *variant_type = FIELD_TYPE (*variant_field);
457 int nfields = TYPE_NFIELDS (variant_type);
459 bool is_tuple = rust_tuple_struct_type_p (variant_type);
461 fprintf_filtered (stream, "%s", TYPE_NAME (variant_type));
464 /* In case of a nullary variant like 'None', just output
469 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
471 fprintf_filtered (stream, "(");
474 /* struct variant. */
475 fprintf_filtered (stream, "{");
478 bool first_field = true;
479 for (int j = 0; j < TYPE_NFIELDS (variant_type); j++)
482 fputs_filtered (", ", stream);
486 fprintf_filtered (stream, "%s: ",
487 TYPE_FIELD_NAME (variant_type, j));
489 val_print (TYPE_FIELD_TYPE (variant_type, j),
491 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
493 stream, recurse + 1, val, &opts,
498 fputs_filtered (")", stream);
500 fputs_filtered ("}", stream);
503 static const struct generic_val_print_decorations rust_decorations =
505 /* Complex isn't used in Rust, but we provide C-ish values just in
517 /* la_val_print implementation for Rust. */
520 rust_val_print (struct type *type, int embedded_offset,
521 CORE_ADDR address, struct ui_file *stream, int recurse,
523 const struct value_print_options *options)
525 const gdb_byte *valaddr = value_contents_for_printing (val);
527 type = check_typedef (type);
528 switch (TYPE_CODE (type))
532 LONGEST low_bound, high_bound;
534 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
535 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
536 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
538 /* We have a pointer to a byte string, so just print
540 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
542 struct gdbarch *arch = get_type_arch (type);
543 int unit_size = gdbarch_addressable_memory_unit_size (arch);
545 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
546 if (options->addressprint)
548 fputs_filtered (paddress (arch, addr), stream);
549 fputs_filtered (" ", stream);
552 fputs_filtered ("b", stream);
553 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
554 high_bound - low_bound + 1, stream,
561 case TYPE_CODE_METHODPTR:
562 case TYPE_CODE_MEMBERPTR:
563 c_val_print (type, embedded_offset, address, stream,
564 recurse, val, options);
568 /* Recognize the unit type. */
569 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
570 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
572 fputs_filtered ("()", stream);
577 case TYPE_CODE_STRING:
579 struct gdbarch *arch = get_type_arch (type);
580 int unit_size = gdbarch_addressable_memory_unit_size (arch);
581 LONGEST low_bound, high_bound;
583 if (!get_array_bounds (type, &low_bound, &high_bound))
584 error (_("Could not determine the array bounds"));
586 /* If we see a plain TYPE_CODE_STRING, then we're printing a
587 byte string, hence the choice of "ASCII" as the
589 fputs_filtered ("b", stream);
590 rust_printstr (stream, TYPE_TARGET_TYPE (type),
591 valaddr + embedded_offset * unit_size,
592 high_bound - low_bound + 1, "ASCII", 0, options);
596 case TYPE_CODE_ARRAY:
598 LONGEST low_bound, high_bound;
600 if (get_array_bounds (type, &low_bound, &high_bound)
601 && high_bound - low_bound + 1 == 0)
602 fputs_filtered ("[]", stream);
608 case TYPE_CODE_UNION:
609 /* Untagged unions are printed as if they are structs. Since
610 the field bit positions overlap in the debuginfo, the code
611 for printing a union is same as that for a struct, the only
612 difference is that the input type will have overlapping
614 val_print_struct (type, embedded_offset, address, stream,
615 recurse, val, options);
618 case TYPE_CODE_STRUCT:
619 if (rust_enum_p (type))
620 rust_print_enum (type, embedded_offset, address, stream,
621 recurse, val, options);
623 val_print_struct (type, embedded_offset, address, stream,
624 recurse, val, options);
629 /* Nothing special yet. */
630 generic_val_print (type, embedded_offset, address, stream,
631 recurse, val, options, &rust_decorations);
638 rust_internal_print_type (struct type *type, const char *varstring,
639 struct ui_file *stream, int show, int level,
640 const struct type_print_options *flags,
641 bool for_rust_enum, print_offset_data *podata);
643 /* Print a struct or union typedef. */
645 rust_print_struct_def (struct type *type, const char *varstring,
646 struct ui_file *stream, int show, int level,
647 const struct type_print_options *flags,
648 bool for_rust_enum, print_offset_data *podata)
650 /* Print a tuple type simply. */
651 if (rust_tuple_type_p (type))
653 fputs_filtered (TYPE_NAME (type), stream);
657 /* If we see a base class, delegate to C. */
658 if (TYPE_N_BASECLASSES (type) > 0)
659 c_print_type (type, varstring, stream, show, level, flags);
661 if (flags->print_offsets)
663 /* Temporarily bump the level so that the output lines up
668 /* Compute properties of TYPE here because, in the enum case, the
669 rest of the code ends up looking only at the variant part. */
670 const char *tagname = TYPE_NAME (type);
671 bool is_tuple_struct = rust_tuple_struct_type_p (type);
672 bool is_tuple = rust_tuple_type_p (type);
673 bool is_enum = rust_enum_p (type);
675 int enum_discriminant_index = -1;
679 /* Already printing an outer enum, so nothing to print here. */
683 /* This code path is also used by unions and enums. */
686 fputs_filtered ("enum ", stream);
688 if (rust_empty_enum_p (type))
692 fputs_filtered (tagname, stream);
693 fputs_filtered (" ", stream);
695 fputs_filtered ("{}", stream);
699 type = TYPE_FIELD_TYPE (type, 0);
701 struct dynamic_prop *discriminant_prop
702 = get_dyn_prop (DYN_PROP_DISCRIMINATED, type);
703 struct discriminant_info *info
704 = (struct discriminant_info *) discriminant_prop->data.baton;
705 enum_discriminant_index = info->discriminant_index;
707 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
708 fputs_filtered ("struct ", stream);
710 fputs_filtered ("union ", stream);
713 fputs_filtered (tagname, stream);
716 if (TYPE_NFIELDS (type) == 0 && !is_tuple)
718 if (for_rust_enum && !flags->print_offsets)
719 fputs_filtered (is_tuple_struct ? "(" : "{", stream);
721 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
723 /* When printing offsets, we rearrange the fields into storage
724 order. This lets us show holes more clearly. We work using
725 field indices here because it simplifies calls to
726 print_offset_data::update below. */
727 std::vector<int> fields;
728 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
730 if (field_is_static (&TYPE_FIELD (type, i)))
732 if (is_enum && i == enum_discriminant_index)
734 fields.push_back (i);
736 if (flags->print_offsets)
737 std::sort (fields.begin (), fields.end (),
740 return (TYPE_FIELD_BITPOS (type, a)
741 < TYPE_FIELD_BITPOS (type, b));
748 gdb_assert (!field_is_static (&TYPE_FIELD (type, i)));
749 gdb_assert (! (is_enum && i == enum_discriminant_index));
751 if (flags->print_offsets)
752 podata->update (type, i, stream);
754 /* We'd like to print "pub" here as needed, but rustc
755 doesn't emit the debuginfo, and our types don't have
756 cplus_struct_type attached. */
758 /* For a tuple struct we print the type but nothing
760 if (!for_rust_enum || flags->print_offsets)
761 print_spaces_filtered (level + 2, stream);
763 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
764 else if (!is_tuple_struct)
765 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
767 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL,
768 stream, (is_enum ? show : show - 1),
769 level + 2, flags, is_enum, podata);
770 if (!for_rust_enum || flags->print_offsets)
771 fputs_filtered (",\n", stream);
772 /* Note that this check of "I" is ok because we only sorted the
773 fields by offset when print_offsets was set, so we won't take
774 this branch in that case. */
775 else if (i + 1 < TYPE_NFIELDS (type))
776 fputs_filtered (", ", stream);
779 if (flags->print_offsets)
781 /* Undo the temporary level increase we did above. */
783 podata->finish (type, level, stream);
784 print_spaces_filtered (print_offset_data::indentation, stream);
786 print_spaces_filtered (2, stream);
788 if (!for_rust_enum || flags->print_offsets)
789 print_spaces_filtered (level, stream);
790 fputs_filtered (is_tuple_struct ? ")" : "}", stream);
793 /* la_print_typedef implementation for Rust. */
796 rust_print_typedef (struct type *type,
797 struct symbol *new_symbol,
798 struct ui_file *stream)
800 type = check_typedef (type);
801 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
802 type_print (type, "", stream, 0);
803 fprintf_filtered (stream, ";\n");
806 /* la_print_type implementation for Rust. */
809 rust_internal_print_type (struct type *type, const char *varstring,
810 struct ui_file *stream, int show, int level,
811 const struct type_print_options *flags,
812 bool for_rust_enum, print_offset_data *podata)
816 && TYPE_NAME (type) != NULL)
818 /* Rust calls the unit type "void" in its debuginfo,
819 but we don't want to print it as that. */
820 if (TYPE_CODE (type) == TYPE_CODE_VOID)
821 fputs_filtered ("()", stream);
823 fputs_filtered (TYPE_NAME (type), stream);
827 type = check_typedef (type);
828 switch (TYPE_CODE (type))
831 /* If we have an enum, we've already printed the type's
832 unqualified name, and there is nothing else to print
835 fputs_filtered ("()", stream);
839 /* Delegate varargs to the C printer. */
840 if (TYPE_VARARGS (type))
843 fputs_filtered ("fn ", stream);
844 if (varstring != NULL)
845 fputs_filtered (varstring, stream);
846 fputs_filtered ("(", stream);
847 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
851 fputs_filtered (", ", stream);
852 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), "", stream,
853 -1, 0, flags, false, podata);
855 fputs_filtered (")", stream);
856 /* If it returns unit, we can omit the return type. */
857 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
859 fputs_filtered (" -> ", stream);
860 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
861 -1, 0, flags, false, podata);
865 case TYPE_CODE_ARRAY:
867 LONGEST low_bound, high_bound;
869 fputs_filtered ("[", stream);
870 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
871 stream, show - 1, level, flags, false,
874 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
875 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
876 fprintf_filtered (stream, "; variable length");
877 else if (get_array_bounds (type, &low_bound, &high_bound))
878 fprintf_filtered (stream, "; %s",
879 plongest (high_bound - low_bound + 1));
880 fputs_filtered ("]", stream);
884 case TYPE_CODE_UNION:
885 case TYPE_CODE_STRUCT:
886 rust_print_struct_def (type, varstring, stream, show, level, flags,
887 for_rust_enum, podata);
894 fputs_filtered ("enum ", stream);
895 if (TYPE_NAME (type) != NULL)
897 fputs_filtered (TYPE_NAME (type), stream);
898 fputs_filtered (" ", stream);
899 len = strlen (TYPE_NAME (type));
901 fputs_filtered ("{\n", stream);
903 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
905 const char *name = TYPE_FIELD_NAME (type, i);
910 && strncmp (name, TYPE_NAME (type), len) == 0
912 && name[len + 1] == ':')
914 fprintfi_filtered (level + 2, stream, "%s,\n", name);
917 fputs_filtered ("}", stream);
923 c_print_type (type, varstring, stream, show, level, flags);
928 rust_print_type (struct type *type, const char *varstring,
929 struct ui_file *stream, int show, int level,
930 const struct type_print_options *flags)
932 print_offset_data podata;
933 rust_internal_print_type (type, varstring, stream, show, level,
934 flags, false, &podata);
939 /* Like arch_composite_type, but uses TYPE to decide how to allocate
940 -- either on an obstack or on a gdbarch. */
943 rust_composite_type (struct type *original,
945 const char *field1, struct type *type1,
946 const char *field2, struct type *type2)
948 struct type *result = alloc_type_copy (original);
949 int i, nfields, bitpos;
957 TYPE_CODE (result) = TYPE_CODE_STRUCT;
958 TYPE_NAME (result) = name;
960 TYPE_NFIELDS (result) = nfields;
962 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
968 struct field *field = &TYPE_FIELD (result, i);
970 SET_FIELD_BITPOS (*field, bitpos);
971 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
973 FIELD_NAME (*field) = field1;
974 FIELD_TYPE (*field) = type1;
979 struct field *field = &TYPE_FIELD (result, i);
980 unsigned align = type_align (type2);
986 align *= TARGET_CHAR_BIT;
987 delta = bitpos % align;
989 bitpos += align - delta;
991 SET_FIELD_BITPOS (*field, bitpos);
993 FIELD_NAME (*field) = field2;
994 FIELD_TYPE (*field) = type2;
1000 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1001 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1005 /* See rust-lang.h. */
1008 rust_slice_type (const char *name, struct type *elt_type,
1009 struct type *usize_type)
1013 elt_type = lookup_pointer_type (elt_type);
1014 type = rust_composite_type (elt_type, name,
1015 "data_ptr", elt_type,
1016 "length", usize_type);
1021 enum rust_primitive_types
1023 rust_primitive_bool,
1024 rust_primitive_char,
1033 rust_primitive_isize,
1034 rust_primitive_usize,
1037 rust_primitive_unit,
1039 nr_rust_primitive_types
1042 /* la_language_arch_info implementation for Rust. */
1045 rust_language_arch_info (struct gdbarch *gdbarch,
1046 struct language_arch_info *lai)
1048 const struct builtin_type *builtin = builtin_type (gdbarch);
1050 struct type **types;
1051 unsigned int length;
1053 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1056 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1057 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1058 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1059 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1060 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1061 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1062 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1063 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1064 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1065 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1067 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1068 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1069 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1071 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1072 floatformats_ieee_single);
1073 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1074 floatformats_ieee_double);
1076 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1078 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1079 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1080 types[rust_primitive_usize]);
1082 lai->primitive_type_vector = types;
1083 lai->bool_type_default = types[rust_primitive_bool];
1084 lai->string_char_type = types[rust_primitive_u8];
1089 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1091 static struct value *
1092 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1095 int num_args = exp->elts[*pos + 1].longconst;
1097 struct value *function, *result, *arg0;
1098 struct type *type, *fn_type;
1099 const struct block *block;
1100 struct block_symbol sym;
1102 /* For an ordinary function call we can simply defer to the
1103 generic implementation. */
1104 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1105 return evaluate_subexp_standard (NULL, exp, pos, noside);
1107 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1109 method = &exp->elts[*pos + 1].string;
1110 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1112 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1113 type in order to look up the method. */
1114 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1116 if (noside == EVAL_SKIP)
1118 for (i = 0; i < num_args; ++i)
1119 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1123 std::vector<struct value *> args (num_args + 1);
1126 /* We don't yet implement real Deref semantics. */
1127 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1128 args[0] = value_ind (args[0]);
1130 type = value_type (args[0]);
1131 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1132 && TYPE_CODE (type) != TYPE_CODE_UNION
1133 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1134 || rust_tuple_type_p (type))
1135 error (_("Method calls only supported on struct or enum types"));
1136 if (TYPE_NAME (type) == NULL)
1137 error (_("Method call on nameless type"));
1139 std::string name = std::string (TYPE_NAME (type)) + "::" + method;
1141 block = get_selected_block (0);
1142 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1143 if (sym.symbol == NULL)
1144 error (_("Could not find function named '%s'"), name.c_str ());
1146 fn_type = SYMBOL_TYPE (sym.symbol);
1147 if (TYPE_NFIELDS (fn_type) == 0)
1148 error (_("Function '%s' takes no arguments"), name.c_str ());
1150 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1151 args[0] = value_addr (args[0]);
1153 function = address_of_variable (sym.symbol, block);
1155 for (i = 0; i < num_args; ++i)
1156 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1158 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1159 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1161 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
1165 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1167 static struct value *
1168 rust_range (struct expression *exp, int *pos, enum noside noside)
1170 enum range_type kind;
1171 struct value *low = NULL, *high = NULL;
1172 struct value *addrval, *result;
1174 struct type *range_type;
1175 struct type *index_type;
1176 struct type *temp_type;
1179 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1182 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
1183 || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1184 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1185 if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
1186 || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1187 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1188 bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
1190 if (noside == EVAL_SKIP)
1191 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1198 name = "std::ops::RangeFull";
1202 index_type = value_type (high);
1204 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1211 index_type = value_type (low);
1212 name = "std::ops::RangeFrom";
1216 if (!types_equal (value_type (low), value_type (high)))
1217 error (_("Range expression with different types"));
1218 index_type = value_type (low);
1219 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1223 /* If we don't have an index type, just allocate this on the
1224 arch. Here any type will do. */
1225 temp_type = (index_type == NULL
1226 ? language_bool_type (exp->language_defn, exp->gdbarch)
1228 /* It would be nicer to cache the range type. */
1229 range_type = rust_composite_type (temp_type, name,
1230 low == NULL ? NULL : "start", index_type,
1231 high == NULL ? NULL : "end", index_type);
1233 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1234 return value_zero (range_type, lval_memory);
1236 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1237 addr = value_as_long (addrval);
1238 result = value_at_lazy (range_type, addr);
1242 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1245 value_assign (start, low);
1250 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1253 value_assign (end, high);
1256 result = value_at_lazy (range_type, addr);
1260 /* A helper function to compute the range and kind given a range
1261 value. TYPE is the type of the range value. RANGE is the range
1262 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1263 parameters might be filled in, or might not be, depending on the
1264 kind of range this is. KIND will always be set to the appropriate
1265 value describing the kind of range, and this can be used to
1266 determine whether LOW or HIGH are valid. */
1269 rust_compute_range (struct type *type, struct value *range,
1270 LONGEST *low, LONGEST *high,
1271 enum range_type *kind)
1277 *kind = BOTH_BOUND_DEFAULT;
1279 if (TYPE_NFIELDS (type) == 0)
1283 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1285 *kind = HIGH_BOUND_DEFAULT;
1286 *low = value_as_long (value_field (range, 0));
1289 if (TYPE_NFIELDS (type) > i
1290 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1292 *kind = (*kind == BOTH_BOUND_DEFAULT
1293 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1294 *high = value_as_long (value_field (range, i));
1296 if (rust_inclusive_range_type_p (type))
1301 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1303 static struct value *
1304 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1307 struct value *lhs, *rhs, *result;
1308 struct type *rhstype;
1309 LONGEST low, high_bound;
1310 /* Initialized to appease the compiler. */
1311 enum range_type kind = BOTH_BOUND_DEFAULT;
1316 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1317 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1319 if (noside == EVAL_SKIP)
1322 rhstype = check_typedef (value_type (rhs));
1323 if (rust_range_type_p (rhstype))
1326 error (_("Can't take slice of array without '&'"));
1327 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1331 low = value_as_long (rhs);
1333 struct type *type = check_typedef (value_type (lhs));
1334 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1336 struct type *base_type = nullptr;
1337 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1338 base_type = TYPE_TARGET_TYPE (type);
1339 else if (rust_slice_type_p (type))
1341 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1343 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1345 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1349 if (base_type == nullptr)
1350 error (_("Could not find 'data_ptr' in slice type"));
1352 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1353 base_type = TYPE_TARGET_TYPE (type);
1355 error (_("Cannot subscript non-array type"));
1357 struct type *new_type;
1360 if (rust_slice_type_p (type))
1365 = language_lookup_primitive_type (exp->language_defn,
1368 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1372 new_type = base_type;
1374 return value_zero (new_type, VALUE_LVAL (lhs));
1381 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1384 if (!get_array_bounds (type, &low_bound, &high_bound))
1385 error (_("Can't compute array bounds"));
1387 error (_("Found array with non-zero lower bound"));
1390 else if (rust_slice_type_p (type))
1394 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1395 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1397 high_bound = value_as_long (len);
1399 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1403 high_bound = LONGEST_MAX;
1406 error (_("Cannot subscript non-array type"));
1409 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1412 error (_("Index less than zero"));
1413 if (low > high_bound)
1414 error (_("Index greater than length"));
1416 result = value_subscript (base, low);
1423 struct type *usize, *slice;
1425 struct value *addrval, *tem;
1427 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1430 error (_("High index less than zero"));
1432 error (_("Low index greater than high index"));
1433 if (high > high_bound)
1434 error (_("High index greater than length"));
1436 usize = language_lookup_primitive_type (exp->language_defn,
1439 const char *new_name = ((type != nullptr
1440 && rust_slice_type_p (type))
1441 ? TYPE_NAME (type) : "&[*gdb*]");
1443 slice = rust_slice_type (new_name, value_type (result), usize);
1445 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1446 addr = value_as_long (addrval);
1447 tem = value_at_lazy (slice, addr);
1449 value_assign (value_field (tem, 0), value_addr (result));
1450 value_assign (value_field (tem, 1),
1451 value_from_longest (usize, high - low));
1453 result = value_at_lazy (slice, addr);
1456 result = value_addr (result);
1462 /* evaluate_exp implementation for Rust. */
1464 static struct value *
1465 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1466 int *pos, enum noside noside)
1468 struct value *result;
1470 switch (exp->elts[*pos].opcode)
1474 if (noside != EVAL_NORMAL)
1475 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1479 struct value *value = evaluate_subexp (expect_type, exp, pos,
1482 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1483 if (trait_ptr != NULL)
1486 result = value_ind (value);
1491 case UNOP_COMPLEMENT:
1493 struct value *value;
1496 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1497 if (noside == EVAL_SKIP)
1499 /* Preserving the type is enough. */
1502 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1503 result = value_from_longest (value_type (value),
1504 value_logical_not (value));
1506 result = value_complement (value);
1510 case BINOP_SUBSCRIPT:
1511 result = rust_subscript (exp, pos, noside, 0);
1515 result = rust_evaluate_funcall (exp, pos, noside);
1521 struct type *type = exp->elts[pc + 1].type;
1522 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1525 struct value *addrval = NULL;
1529 if (noside == EVAL_NORMAL)
1531 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1532 addr = value_as_long (addrval);
1533 result = value_at_lazy (type, addr);
1536 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1541 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1542 if (noside == EVAL_NORMAL)
1544 /* This isn't quite right but will do for the time
1545 being, seeing that we can't implement the Copy
1547 value_assign (result, init);
1553 gdb_assert (arglen % 2 == 0);
1554 for (i = 0; i < arglen; i += 2)
1557 const char *fieldname;
1558 struct value *value, *field;
1560 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1562 len = longest_to_int (exp->elts[*pos].longconst);
1564 fieldname = &exp->elts[*pos].string;
1565 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1567 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1568 if (noside == EVAL_NORMAL)
1570 field = value_struct_elt (&result, NULL, fieldname, NULL,
1572 value_assign (field, value);
1576 if (noside == EVAL_SKIP)
1577 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1579 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1580 result = allocate_value (type);
1582 result = value_at_lazy (type, addr);
1591 struct value *ncopies;
1593 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1594 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1595 copies = value_as_long (ncopies);
1597 error (_("Array with negative number of elements"));
1599 if (noside == EVAL_NORMAL)
1602 std::vector<struct value *> eltvec (copies);
1604 for (i = 0; i < copies; ++i)
1606 result = value_array (0, copies - 1, eltvec.data ());
1610 struct type *arraytype
1611 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1612 result = allocate_value (arraytype);
1617 case STRUCTOP_ANONYMOUS:
1619 /* Anonymous field access, i.e. foo.1. */
1621 int pc, field_number, nfields;
1625 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1627 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1629 type = value_type (lhs);
1631 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1633 struct type *outer_type = NULL;
1635 if (rust_enum_p (type))
1637 if (rust_empty_enum_p (type))
1638 error (_("Cannot access field %d of empty enum %s"),
1639 field_number, TYPE_NAME (type));
1641 const gdb_byte *valaddr = value_contents (lhs);
1642 struct field *variant_field = rust_enum_variant (type, valaddr);
1644 struct value *union_value = value_primitive_field (lhs, 0, 0,
1647 int fieldno = (variant_field
1648 - &TYPE_FIELD (value_type (union_value), 0));
1649 lhs = value_primitive_field (union_value, 0, fieldno,
1650 value_type (union_value));
1652 type = value_type (lhs);
1655 /* Tuples and tuple structs */
1656 nfields = TYPE_NFIELDS (type);
1658 if (field_number >= nfields || field_number < 0)
1660 if (outer_type != NULL)
1661 error(_("Cannot access field %d of variant %s::%s, "
1662 "there are only %d fields"),
1663 field_number, TYPE_NAME (outer_type),
1664 rust_last_path_segment (TYPE_NAME (type)),
1667 error(_("Cannot access field %d of %s, "
1668 "there are only %d fields"),
1669 field_number, TYPE_NAME (type), nfields);
1672 /* Tuples are tuple structs too. */
1673 if (!rust_tuple_struct_type_p (type))
1675 if (outer_type != NULL)
1676 error(_("Variant %s::%s is not a tuple variant"),
1677 TYPE_NAME (outer_type),
1678 rust_last_path_segment (TYPE_NAME (type)));
1680 error(_("Attempting to access anonymous field %d "
1681 "of %s, which is not a tuple, tuple struct, or "
1682 "tuple-like variant"),
1683 field_number, TYPE_NAME (type));
1686 result = value_primitive_field (lhs, 0, field_number, type);
1689 error(_("Anonymous field access is only allowed on tuples, \
1690 tuple structs, and tuple-like enum variants"));
1694 case STRUCTOP_STRUCT:
1701 tem = longest_to_int (exp->elts[pc + 1].longconst);
1702 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1703 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1705 const char *field_name = &exp->elts[pc + 2].string;
1706 type = value_type (lhs);
1707 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type))
1709 if (rust_empty_enum_p (type))
1710 error (_("Cannot access field %s of empty enum %s"),
1711 field_name, TYPE_NAME (type));
1713 const gdb_byte *valaddr = value_contents (lhs);
1714 struct field *variant_field = rust_enum_variant (type, valaddr);
1716 struct value *union_value = value_primitive_field (lhs, 0, 0,
1719 int fieldno = (variant_field
1720 - &TYPE_FIELD (value_type (union_value), 0));
1721 lhs = value_primitive_field (union_value, 0, fieldno,
1722 value_type (union_value));
1724 struct type *outer_type = type;
1725 type = value_type (lhs);
1726 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1727 error (_("Attempting to access named field %s of tuple "
1728 "variant %s::%s, which has only anonymous fields"),
1729 field_name, TYPE_NAME (outer_type),
1730 rust_last_path_segment (TYPE_NAME (type)));
1734 result = value_struct_elt (&lhs, NULL, field_name,
1737 CATCH (except, RETURN_MASK_ERROR)
1739 error (_("Could not find field %s of struct variant %s::%s"),
1740 field_name, TYPE_NAME (outer_type),
1741 rust_last_path_segment (TYPE_NAME (type)));
1746 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1747 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1748 result = value_zero (value_type (result), VALUE_LVAL (result));
1753 result = rust_range (exp, pos, noside);
1757 /* We might have &array[range], in which case we need to make a
1759 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1762 result = rust_subscript (exp, pos, noside, 1);
1767 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1774 /* operator_length implementation for Rust. */
1777 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1783 switch (exp->elts[pc - 1].opcode)
1786 /* We handle aggregate as a type and argument count. The first
1787 argument might be OP_OTHERS. After that the arguments
1788 alternate: first an OP_NAME, then an expression. */
1790 args = longest_to_int (exp->elts[pc - 2].longconst);
1798 case STRUCTOP_ANONYMOUS:
1809 operator_length_standard (exp, pc, oplenp, argsp);
1817 /* op_name implementation for Rust. */
1820 rust_op_name (enum exp_opcode opcode)
1825 return "OP_AGGREGATE";
1829 return op_name_standard (opcode);
1833 /* dump_subexp_body implementation for Rust. */
1836 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1839 switch (exp->elts[elt].opcode)
1843 int length = longest_to_int (exp->elts[elt + 2].longconst);
1846 fprintf_filtered (stream, "Type @");
1847 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1848 fprintf_filtered (stream, " (");
1849 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1850 fprintf_filtered (stream, "), length %d", length);
1853 for (i = 0; i < length; ++i)
1854 elt = dump_subexp (exp, stream, elt);
1861 LONGEST len = exp->elts[elt + 1].longconst;
1863 fprintf_filtered (stream, "%s: %s",
1864 (exp->elts[elt].opcode == OP_STRING
1865 ? "string" : "name"),
1866 &exp->elts[elt + 2].string);
1867 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1872 elt = dump_subexp (exp, stream, elt + 1);
1875 case STRUCTOP_ANONYMOUS:
1879 field_number = longest_to_int (exp->elts[elt + 1].longconst);
1881 fprintf_filtered (stream, "Field number: %d", field_number);
1882 elt = dump_subexp (exp, stream, elt + 3);
1891 elt = dump_subexp_body_standard (exp, stream, elt);
1898 /* print_subexp implementation for Rust. */
1901 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1902 enum precedence prec)
1904 switch (exp->elts[*pos].opcode)
1908 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1911 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1912 fputs_filtered (" { ", stream);
1915 for (i = 0; i < length; ++i)
1917 rust_print_subexp (exp, pos, stream, prec);
1918 fputs_filtered (", ", stream);
1920 fputs_filtered (" }", stream);
1926 LONGEST len = exp->elts[*pos + 1].longconst;
1928 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1929 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1935 fputs_filtered ("<<others>> (", stream);
1937 rust_print_subexp (exp, pos, stream, prec);
1938 fputs_filtered (")", stream);
1942 case STRUCTOP_ANONYMOUS:
1944 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1947 print_subexp (exp, pos, stream, PREC_SUFFIX);
1948 fprintf_filtered (stream, ".%d", tem);
1954 fprintf_filtered (stream, "[");
1955 rust_print_subexp (exp, pos, stream, prec);
1956 fprintf_filtered (stream, "; ");
1957 rust_print_subexp (exp, pos, stream, prec);
1958 fprintf_filtered (stream, "]");
1962 print_subexp_standard (exp, pos, stream, prec);
1967 /* operator_check implementation for Rust. */
1970 rust_operator_check (struct expression *exp, int pos,
1971 int (*objfile_func) (struct objfile *objfile,
1975 switch (exp->elts[pos].opcode)
1979 struct type *type = exp->elts[pos + 1].type;
1980 struct objfile *objfile = TYPE_OBJFILE (type);
1982 if (objfile != NULL && (*objfile_func) (objfile, data))
1993 return operator_check_standard (exp, pos, objfile_func, data);
2001 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2003 static struct block_symbol
2004 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2006 const struct block *block,
2007 const domain_enum domain)
2009 struct block_symbol result = {NULL, NULL};
2011 if (symbol_lookup_debug)
2013 fprintf_unfiltered (gdb_stdlog,
2014 "rust_lookup_symbol_non_local"
2015 " (%s, %s (scope %s), %s)\n",
2016 name, host_address_to_string (block),
2017 block_scope (block), domain_name (domain));
2020 /* Look up bare names in the block's scope. */
2021 std::string scopedname;
2022 if (name[cp_find_first_component (name)] == '\0')
2024 const char *scope = block_scope (block);
2026 if (scope[0] != '\0')
2028 scopedname = std::string (scope) + "::" + name;
2029 name = scopedname.c_str ();
2037 result = lookup_symbol_in_static_block (name, block, domain);
2038 if (result.symbol == NULL)
2039 result = lookup_global_symbol (name, block, domain);
2046 /* la_sniff_from_mangled_name for Rust. */
2049 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2051 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2052 return *demangled != NULL;
2057 /* la_watch_location_expression for Rust. */
2059 static gdb::unique_xmalloc_ptr<char>
2060 rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2062 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2063 std::string name = type_to_string (type);
2064 return gdb::unique_xmalloc_ptr<char>
2065 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2071 static const struct exp_descriptor exp_descriptor_rust =
2074 rust_operator_length,
2075 rust_operator_check,
2077 rust_dump_subexp_body,
2078 rust_evaluate_subexp
2081 static const char *rust_extensions[] =
2086 extern const struct language_defn rust_language_defn =
2096 &exp_descriptor_rust,
2099 rust_printchar, /* Print a character constant */
2100 rust_printstr, /* Function to print string constant */
2101 rust_emitchar, /* Print a single char */
2102 rust_print_type, /* Print a type using appropriate syntax */
2103 rust_print_typedef, /* Print a typedef using appropriate syntax */
2104 rust_val_print, /* Print a value using appropriate syntax */
2105 c_value_print, /* Print a top-level value */
2106 default_read_var_value, /* la_read_var_value */
2107 NULL, /* Language specific skip_trampoline */
2108 NULL, /* name_of_this */
2109 false, /* la_store_sym_names_in_linkage_form_p */
2110 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2111 basic_lookup_transparent_type,/* lookup_transparent_type */
2112 gdb_demangle, /* Language specific symbol demangler */
2113 rust_sniff_from_mangled_name,
2114 NULL, /* Language specific
2115 class_name_from_physname */
2116 c_op_print_tab, /* expression operators for printing */
2117 1, /* c-style arrays */
2118 0, /* String lower bound */
2119 default_word_break_characters,
2120 default_collect_symbol_completion_matches,
2121 rust_language_arch_info,
2122 default_print_array_index,
2123 default_pass_by_reference,
2125 rust_watch_location_expression,
2126 NULL, /* la_get_symbol_name_matcher */
2127 iterate_over_symbols,
2128 default_search_name_hash,
2129 &default_varobj_ops,