1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2020 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"
40 #include "cli/cli-style.h"
42 /* See rust-lang.h. */
45 rust_last_path_segment (const char *path
)
47 const char *result
= strrchr (path
, ':');
54 /* See rust-lang.h. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
62 return std::string ();
64 return std::string (scope
, cp_find_first_component (scope
));
67 /* Return true if TYPE, which must be a struct type, represents a Rust
71 rust_enum_p (struct type
*type
)
73 /* is_dynamic_type will return true if any field has a dynamic
74 attribute -- but we only want to check the top level. */
75 return TYPE_HAS_VARIANT_PARTS (type
);
78 /* Return true if TYPE, which must be an already-resolved enum type,
82 rust_empty_enum_p (const struct type
*type
)
84 return type
->num_fields () == 0;
87 /* Given an already-resolved enum type and contents, find which
91 rust_enum_variant (struct type
*type
)
93 /* The active variant is simply the first non-artificial field. */
94 for (int i
= 0; i
< type
->num_fields (); ++i
)
95 if (!TYPE_FIELD_ARTIFICIAL (type
, i
))
98 /* Perhaps we could get here by trying to print an Ada variant
99 record in Rust mode. Unlikely, but an error is safer than an
101 error (_("Could not find active enum variant"));
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_CODE_STRUCT
113 && type
->name () != NULL
114 && type
->name ()[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_CODE_STRUCT
)
129 for (i
= 0; i
< type
->num_fields (); ++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
->num_fields () > 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_CODE_STRUCT
161 && type
->name () != NULL
162 && (strncmp (type
->name (), "&[", 2) == 0
163 || strcmp (type
->name (), "&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_CODE_STRUCT
174 || type
->num_fields () > 2
175 || type
->name () == NULL
176 || strstr (type
->name (), "::Range") == NULL
)
179 if (type
->num_fields () == 0)
183 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
185 if (type
->num_fields () == 1)
189 else if (type
->num_fields () == 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 (), "::RangeInclusive") != NULL
206 || strstr (type
->name (), "::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_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_CODE_CHAR
225 && TYPE_LENGTH (type
) == 4
226 && TYPE_UNSIGNED (type
));
229 /* Return true if TYPE is a string type. */
232 rust_is_string_type_p (struct type
*type
)
234 LONGEST low_bound
, high_bound
;
236 type
= check_typedef (type
);
237 return ((type
->code () == TYPE_CODE_STRING
)
238 || (type
->code () == TYPE_CODE_PTR
239 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
240 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
241 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
243 || (type
->code () == TYPE_CODE_STRUCT
244 && !rust_enum_p (type
)
245 && rust_slice_type_p (type
)
246 && strcmp (type
->name (), "&str") == 0));
249 /* If VALUE represents a trait object pointer, return the underlying
250 pointer with the correct (i.e., runtime) type. Otherwise, return
253 static struct value
*
254 rust_get_trait_object_pointer (struct value
*value
)
256 struct type
*type
= check_typedef (value_type (value
));
258 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
261 /* Try to be a bit resilient if the ABI changes. */
262 int vtable_field
= 0;
263 for (int i
= 0; i
< 2; ++i
)
265 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
267 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
271 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
272 struct symbol
*symbol
= find_symbol_at_address (vtable
);
273 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
276 struct rust_vtable_symbol
*vtable_sym
277 = static_cast<struct rust_vtable_symbol
*> (symbol
);
278 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
279 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
284 /* la_emitchar implementation for Rust. */
287 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
289 if (!rust_chartype_p (type
))
290 generic_emit_char (c
, type
, stream
, quoter
,
291 target_charset (get_type_arch (type
)));
292 else if (c
== '\\' || c
== quoter
)
293 fprintf_filtered (stream
, "\\%c", c
);
295 fputs_filtered ("\\n", stream
);
297 fputs_filtered ("\\r", stream
);
299 fputs_filtered ("\\t", stream
);
301 fputs_filtered ("\\0", stream
);
302 else if (c
>= 32 && c
<= 127 && isprint (c
))
303 fputc_filtered (c
, stream
);
305 fprintf_filtered (stream
, "\\x%02x", c
);
307 fprintf_filtered (stream
, "\\u{%06x}", c
);
310 /* la_printchar implementation for Rust. */
313 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
315 fputs_filtered ("'", stream
);
316 LA_EMIT_CHAR (c
, type
, stream
, '\'');
317 fputs_filtered ("'", stream
);
320 /* la_printstr implementation for Rust. */
323 rust_printstr (struct ui_file
*stream
, struct type
*type
,
324 const gdb_byte
*string
, unsigned int length
,
325 const char *user_encoding
, int force_ellipses
,
326 const struct value_print_options
*options
)
328 /* Rust always uses UTF-8, but let the caller override this if need
330 const char *encoding
= user_encoding
;
331 if (user_encoding
== NULL
|| !*user_encoding
)
333 /* In Rust strings, characters are "u8". */
334 if (rust_u8_type_p (type
))
338 /* This is probably some C string, so let's let C deal with
340 c_printstr (stream
, type
, string
, length
, user_encoding
,
341 force_ellipses
, options
);
346 /* This is not ideal as it doesn't use our character printer. */
347 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
353 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
355 const struct value_print_options
*options
);
357 /* Helper function to print a string slice. */
360 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
361 const struct value_print_options
*options
)
363 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
365 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
367 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
368 value_as_address (base
), value_as_long (len
), stream
,
372 /* rust_val_print helper for structs and untagged unions. */
375 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
376 const struct value_print_options
*options
)
380 struct type
*type
= check_typedef (value_type (val
));
382 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
384 /* If what we are printing here is actually a string within a
385 structure then VAL will be the original parent value, while TYPE
386 will be the type of the structure representing the string we want
388 However, RUST_VAL_PRINT_STR looks up the fields of the string
389 inside VAL, assuming that VAL is the string.
390 So, recreate VAL as a value representing just the string. */
391 val
= value_at_lazy (type
, value_address (val
));
392 rust_val_print_str (stream
, val
, options
);
396 bool is_tuple
= rust_tuple_type_p (type
);
397 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
398 struct value_print_options opts
;
402 if (type
->name () != NULL
)
403 fprintf_filtered (stream
, "%s", type
->name ());
405 if (type
->num_fields () == 0)
408 if (type
->name () != NULL
)
409 fputs_filtered (" ", stream
);
412 if (is_tuple
|| is_tuple_struct
)
413 fputs_filtered ("(", stream
);
415 fputs_filtered ("{", stream
);
421 for (i
= 0; i
< type
->num_fields (); ++i
)
423 if (field_is_static (&TYPE_FIELD (type
, i
)))
427 fputs_filtered (",", stream
);
429 if (options
->prettyformat
)
431 fputs_filtered ("\n", stream
);
432 print_spaces_filtered (2 + 2 * recurse
, stream
);
434 else if (!first_field
)
435 fputs_filtered (" ", stream
);
439 if (!is_tuple
&& !is_tuple_struct
)
441 fputs_styled (TYPE_FIELD_NAME (type
, i
),
442 variable_name_style
.style (), stream
);
443 fputs_filtered (": ", stream
);
446 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
450 if (options
->prettyformat
)
452 fputs_filtered ("\n", stream
);
453 print_spaces_filtered (2 * recurse
, stream
);
456 if (is_tuple
|| is_tuple_struct
)
457 fputs_filtered (")", stream
);
459 fputs_filtered ("}", stream
);
462 /* rust_val_print helper for discriminated unions (Rust enums). */
465 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
466 const struct value_print_options
*options
)
468 struct value_print_options opts
= *options
;
469 struct type
*type
= check_typedef (value_type (val
));
473 gdb_assert (rust_enum_p (type
));
474 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
475 TYPE_LENGTH (value_type (val
)));
476 type
= resolve_dynamic_type (type
, view
, value_address (val
));
478 if (rust_empty_enum_p (type
))
480 /* Print the enum type name here to be more clear. */
481 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
483 metadata_style
.style ().ptr (), nullptr);
487 int variant_fieldno
= rust_enum_variant (type
);
488 val
= value_field (val
, variant_fieldno
);
489 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, variant_fieldno
);
491 int nfields
= variant_type
->num_fields ();
493 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
495 fprintf_filtered (stream
, "%s", variant_type
->name ());
498 /* In case of a nullary variant like 'None', just output
503 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
505 fprintf_filtered (stream
, "(");
508 /* struct variant. */
509 fprintf_filtered (stream
, "{");
512 bool first_field
= true;
513 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
516 fputs_filtered (", ", stream
);
520 fprintf_filtered (stream
, "%ps: ",
521 styled_string (variable_name_style
.style (),
522 TYPE_FIELD_NAME (variant_type
, j
)));
524 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
529 fputs_filtered (")", stream
);
531 fputs_filtered ("}", stream
);
534 static const struct generic_val_print_decorations rust_decorations
=
536 /* Complex isn't used in Rust, but we provide C-ish values just in
548 /* la_value_print_inner implementation for Rust. */
550 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
552 const struct value_print_options
*options
)
554 struct value_print_options opts
= *options
;
557 if (opts
.prettyformat
== Val_prettyformat_default
)
558 opts
.prettyformat
= (opts
.prettyformat_structs
559 ? Val_prettyformat
: Val_no_prettyformat
);
561 struct type
*type
= check_typedef (value_type (val
));
562 switch (type
->code ())
566 LONGEST low_bound
, high_bound
;
568 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
569 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
570 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
573 /* We have a pointer to a byte string, so just print
575 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
576 CORE_ADDR addr
= value_as_address (val
);
577 struct gdbarch
*arch
= get_type_arch (type
);
579 if (opts
.addressprint
)
581 fputs_filtered (paddress (arch
, addr
), stream
);
582 fputs_filtered (" ", stream
);
585 fputs_filtered ("b", stream
);
586 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
587 high_bound
- low_bound
+ 1, stream
,
594 case TYPE_CODE_METHODPTR
:
595 case TYPE_CODE_MEMBERPTR
:
596 c_value_print_inner (val
, stream
, recurse
, &opts
);
600 /* Recognize the unit type. */
601 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
602 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
604 fputs_filtered ("()", stream
);
609 case TYPE_CODE_STRING
:
611 LONGEST low_bound
, high_bound
;
613 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
614 error (_("Could not determine the array bounds"));
616 /* If we see a plain TYPE_CODE_STRING, then we're printing a
617 byte string, hence the choice of "ASCII" as the
619 fputs_filtered ("b", stream
);
620 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
621 value_contents_for_printing (val
),
622 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
626 case TYPE_CODE_ARRAY
:
628 LONGEST low_bound
, high_bound
;
630 if (get_array_bounds (type
, &low_bound
, &high_bound
)
631 && high_bound
- low_bound
+ 1 == 0)
632 fputs_filtered ("[]", stream
);
638 case TYPE_CODE_UNION
:
639 /* Untagged unions are printed as if they are structs. Since
640 the field bit positions overlap in the debuginfo, the code
641 for printing a union is same as that for a struct, the only
642 difference is that the input type will have overlapping
644 val_print_struct (val
, stream
, recurse
, &opts
);
647 case TYPE_CODE_STRUCT
:
648 if (rust_enum_p (type
))
649 rust_print_enum (val
, stream
, recurse
, &opts
);
651 val_print_struct (val
, stream
, recurse
, &opts
);
656 /* Nothing special yet. */
657 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
664 rust_internal_print_type (struct type
*type
, const char *varstring
,
665 struct ui_file
*stream
, int show
, int level
,
666 const struct type_print_options
*flags
,
667 bool for_rust_enum
, print_offset_data
*podata
);
669 /* Print a struct or union typedef. */
671 rust_print_struct_def (struct type
*type
, const char *varstring
,
672 struct ui_file
*stream
, int show
, int level
,
673 const struct type_print_options
*flags
,
674 bool for_rust_enum
, print_offset_data
*podata
)
676 /* Print a tuple type simply. */
677 if (rust_tuple_type_p (type
))
679 fputs_filtered (type
->name (), stream
);
683 /* If we see a base class, delegate to C. */
684 if (TYPE_N_BASECLASSES (type
) > 0)
685 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
687 if (flags
->print_offsets
)
689 /* Temporarily bump the level so that the output lines up
694 /* Compute properties of TYPE here because, in the enum case, the
695 rest of the code ends up looking only at the variant part. */
696 const char *tagname
= type
->name ();
697 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
698 bool is_tuple
= rust_tuple_type_p (type
);
699 bool is_enum
= rust_enum_p (type
);
703 /* Already printing an outer enum, so nothing to print here. */
707 /* This code path is also used by unions and enums. */
710 fputs_filtered ("enum ", stream
);
711 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
712 if (prop
!= nullptr && prop
->kind
== PROP_TYPE
)
713 type
= prop
->data
.original_type
;
715 else if (type
->code () == TYPE_CODE_STRUCT
)
716 fputs_filtered ("struct ", stream
);
718 fputs_filtered ("union ", stream
);
721 fputs_filtered (tagname
, stream
);
724 if (type
->num_fields () == 0 && !is_tuple
)
726 if (for_rust_enum
&& !flags
->print_offsets
)
727 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
729 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
731 /* When printing offsets, we rearrange the fields into storage
732 order. This lets us show holes more clearly. We work using
733 field indices here because it simplifies calls to
734 print_offset_data::update below. */
735 std::vector
<int> fields
;
736 for (int i
= 0; i
< type
->num_fields (); ++i
)
738 if (field_is_static (&TYPE_FIELD (type
, i
)))
740 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
742 fields
.push_back (i
);
744 if (flags
->print_offsets
)
745 std::sort (fields
.begin (), fields
.end (),
748 return (TYPE_FIELD_BITPOS (type
, a
)
749 < TYPE_FIELD_BITPOS (type
, b
));
756 gdb_assert (!field_is_static (&TYPE_FIELD (type
, i
)));
757 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
759 if (flags
->print_offsets
)
760 podata
->update (type
, i
, stream
);
762 /* We'd like to print "pub" here as needed, but rustc
763 doesn't emit the debuginfo, and our types don't have
764 cplus_struct_type attached. */
766 /* For a tuple struct we print the type but nothing
768 if (!for_rust_enum
|| flags
->print_offsets
)
769 print_spaces_filtered (level
+ 2, stream
);
771 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
773 else if (!is_tuple_struct
)
774 fprintf_filtered (stream
, "%ps: ",
775 styled_string (variable_name_style
.style (),
776 TYPE_FIELD_NAME (type
, i
)));
778 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
779 stream
, (is_enum
? show
: show
- 1),
780 level
+ 2, flags
, is_enum
, podata
);
781 if (!for_rust_enum
|| flags
->print_offsets
)
782 fputs_filtered (",\n", stream
);
783 /* Note that this check of "I" is ok because we only sorted the
784 fields by offset when print_offsets was set, so we won't take
785 this branch in that case. */
786 else if (i
+ 1 < type
->num_fields ())
787 fputs_filtered (", ", stream
);
790 if (flags
->print_offsets
)
792 /* Undo the temporary level increase we did above. */
794 podata
->finish (type
, level
, stream
);
795 print_spaces_filtered (print_offset_data::indentation
, stream
);
797 print_spaces_filtered (2, stream
);
799 if (!for_rust_enum
|| flags
->print_offsets
)
800 print_spaces_filtered (level
, stream
);
801 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
804 /* la_print_typedef implementation for Rust. */
807 rust_print_typedef (struct type
*type
,
808 struct symbol
*new_symbol
,
809 struct ui_file
*stream
)
811 type
= check_typedef (type
);
812 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
813 type_print (type
, "", stream
, 0);
814 fprintf_filtered (stream
, ";");
817 /* la_print_type implementation for Rust. */
820 rust_internal_print_type (struct type
*type
, const char *varstring
,
821 struct ui_file
*stream
, int show
, int level
,
822 const struct type_print_options
*flags
,
823 bool for_rust_enum
, print_offset_data
*podata
)
827 && type
->name () != NULL
)
829 /* Rust calls the unit type "void" in its debuginfo,
830 but we don't want to print it as that. */
831 if (type
->code () == TYPE_CODE_VOID
)
832 fputs_filtered ("()", stream
);
834 fputs_filtered (type
->name (), stream
);
838 type
= check_typedef (type
);
839 switch (type
->code ())
842 /* If we have an enum, we've already printed the type's
843 unqualified name, and there is nothing else to print
846 fputs_filtered ("()", stream
);
850 /* Delegate varargs to the C printer. */
851 if (TYPE_VARARGS (type
))
854 fputs_filtered ("fn ", stream
);
855 if (varstring
!= NULL
)
856 fputs_filtered (varstring
, stream
);
857 fputs_filtered ("(", stream
);
858 for (int i
= 0; i
< type
->num_fields (); ++i
)
862 fputs_filtered (", ", stream
);
863 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
864 -1, 0, flags
, false, podata
);
866 fputs_filtered (")", stream
);
867 /* If it returns unit, we can omit the return type. */
868 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
870 fputs_filtered (" -> ", stream
);
871 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
872 -1, 0, flags
, false, podata
);
876 case TYPE_CODE_ARRAY
:
878 LONGEST low_bound
, high_bound
;
880 fputs_filtered ("[", stream
);
881 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
882 stream
, show
- 1, level
, flags
, false,
885 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
886 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
887 fprintf_filtered (stream
, "; variable length");
888 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
889 fprintf_filtered (stream
, "; %s",
890 plongest (high_bound
- low_bound
+ 1));
891 fputs_filtered ("]", stream
);
895 case TYPE_CODE_UNION
:
896 case TYPE_CODE_STRUCT
:
897 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
898 for_rust_enum
, podata
);
905 fputs_filtered ("enum ", stream
);
906 if (type
->name () != NULL
)
908 fputs_filtered (type
->name (), stream
);
909 fputs_filtered (" ", stream
);
910 len
= strlen (type
->name ());
912 fputs_filtered ("{\n", stream
);
914 for (int i
= 0; i
< type
->num_fields (); ++i
)
916 const char *name
= TYPE_FIELD_NAME (type
, i
);
921 && strncmp (name
, type
->name (), len
) == 0
923 && name
[len
+ 1] == ':')
925 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
926 styled_string (variable_name_style
.style (),
930 fputs_filtered ("}", stream
);
936 if (type
->name () != nullptr)
937 fputs_filtered (type
->name (), stream
);
940 /* We currently can't distinguish between pointers and
942 fputs_filtered ("*mut ", stream
);
943 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
950 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
955 rust_print_type (struct type
*type
, const char *varstring
,
956 struct ui_file
*stream
, int show
, int level
,
957 const struct type_print_options
*flags
)
959 print_offset_data podata
;
960 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
961 flags
, false, &podata
);
966 /* Like arch_composite_type, but uses TYPE to decide how to allocate
967 -- either on an obstack or on a gdbarch. */
970 rust_composite_type (struct type
*original
,
972 const char *field1
, struct type
*type1
,
973 const char *field2
, struct type
*type2
)
975 struct type
*result
= alloc_type_copy (original
);
976 int i
, nfields
, bitpos
;
984 result
->set_code (TYPE_CODE_STRUCT
);
985 result
->set_name (name
);
987 result
->set_num_fields (nfields
);
989 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
995 struct field
*field
= &TYPE_FIELD (result
, i
);
997 SET_FIELD_BITPOS (*field
, bitpos
);
998 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1000 FIELD_NAME (*field
) = field1
;
1001 FIELD_TYPE (*field
) = type1
;
1006 struct field
*field
= &TYPE_FIELD (result
, i
);
1007 unsigned align
= type_align (type2
);
1013 align
*= TARGET_CHAR_BIT
;
1014 delta
= bitpos
% align
;
1016 bitpos
+= align
- delta
;
1018 SET_FIELD_BITPOS (*field
, bitpos
);
1020 FIELD_NAME (*field
) = field2
;
1021 FIELD_TYPE (*field
) = type2
;
1026 TYPE_LENGTH (result
)
1027 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1028 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1032 /* See rust-lang.h. */
1035 rust_slice_type (const char *name
, struct type
*elt_type
,
1036 struct type
*usize_type
)
1040 elt_type
= lookup_pointer_type (elt_type
);
1041 type
= rust_composite_type (elt_type
, name
,
1042 "data_ptr", elt_type
,
1043 "length", usize_type
);
1048 enum rust_primitive_types
1050 rust_primitive_bool
,
1051 rust_primitive_char
,
1060 rust_primitive_isize
,
1061 rust_primitive_usize
,
1064 rust_primitive_unit
,
1066 nr_rust_primitive_types
1069 /* la_language_arch_info implementation for Rust. */
1072 rust_language_arch_info (struct gdbarch
*gdbarch
,
1073 struct language_arch_info
*lai
)
1075 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1077 struct type
**types
;
1078 unsigned int length
;
1080 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1083 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1084 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1085 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1086 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1087 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1088 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1089 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1090 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1091 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1092 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1094 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1095 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1096 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1098 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1099 floatformats_ieee_single
);
1100 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1101 floatformats_ieee_double
);
1103 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1105 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1106 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1107 types
[rust_primitive_usize
]);
1109 lai
->primitive_type_vector
= types
;
1110 lai
->bool_type_default
= types
[rust_primitive_bool
];
1111 lai
->string_char_type
= types
[rust_primitive_u8
];
1116 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1118 static struct value
*
1119 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1122 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1124 struct value
*function
, *result
, *arg0
;
1125 struct type
*type
, *fn_type
;
1126 const struct block
*block
;
1127 struct block_symbol sym
;
1129 /* For an ordinary function call we can simply defer to the
1130 generic implementation. */
1131 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1132 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1134 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1136 method
= &exp
->elts
[*pos
+ 1].string
;
1137 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1139 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1140 type in order to look up the method. */
1141 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1143 if (noside
== EVAL_SKIP
)
1145 for (i
= 0; i
< num_args
; ++i
)
1146 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1150 std::vector
<struct value
*> args (num_args
+ 1);
1153 /* We don't yet implement real Deref semantics. */
1154 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1155 args
[0] = value_ind (args
[0]);
1157 type
= value_type (args
[0]);
1158 if ((type
->code () != TYPE_CODE_STRUCT
1159 && type
->code () != TYPE_CODE_UNION
1160 && type
->code () != TYPE_CODE_ENUM
)
1161 || rust_tuple_type_p (type
))
1162 error (_("Method calls only supported on struct or enum types"));
1163 if (type
->name () == NULL
)
1164 error (_("Method call on nameless type"));
1166 std::string name
= std::string (type
->name ()) + "::" + method
;
1168 block
= get_selected_block (0);
1169 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1170 if (sym
.symbol
== NULL
)
1171 error (_("Could not find function named '%s'"), name
.c_str ());
1173 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1174 if (fn_type
->num_fields () == 0)
1175 error (_("Function '%s' takes no arguments"), name
.c_str ());
1177 if (TYPE_FIELD_TYPE (fn_type
, 0)->code () == TYPE_CODE_PTR
)
1178 args
[0] = value_addr (args
[0]);
1180 function
= address_of_variable (sym
.symbol
, block
);
1182 for (i
= 0; i
< num_args
; ++i
)
1183 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1185 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1186 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1188 result
= call_function_by_hand (function
, NULL
, args
);
1192 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1194 static struct value
*
1195 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1197 enum range_type kind
;
1198 struct value
*low
= NULL
, *high
= NULL
;
1199 struct value
*addrval
, *result
;
1201 struct type
*range_type
;
1202 struct type
*index_type
;
1203 struct type
*temp_type
;
1206 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1209 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1210 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1211 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1212 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1213 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1214 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1215 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1217 if (noside
== EVAL_SKIP
)
1218 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1225 name
= "std::ops::RangeFull";
1229 index_type
= value_type (high
);
1231 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1238 index_type
= value_type (low
);
1239 name
= "std::ops::RangeFrom";
1243 if (!types_equal (value_type (low
), value_type (high
)))
1244 error (_("Range expression with different types"));
1245 index_type
= value_type (low
);
1246 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1250 /* If we don't have an index type, just allocate this on the
1251 arch. Here any type will do. */
1252 temp_type
= (index_type
== NULL
1253 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1255 /* It would be nicer to cache the range type. */
1256 range_type
= rust_composite_type (temp_type
, name
,
1257 low
== NULL
? NULL
: "start", index_type
,
1258 high
== NULL
? NULL
: "end", index_type
);
1260 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1261 return value_zero (range_type
, lval_memory
);
1263 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1264 addr
= value_as_long (addrval
);
1265 result
= value_at_lazy (range_type
, addr
);
1269 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1272 value_assign (start
, low
);
1277 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1280 value_assign (end
, high
);
1283 result
= value_at_lazy (range_type
, addr
);
1287 /* A helper function to compute the range and kind given a range
1288 value. TYPE is the type of the range value. RANGE is the range
1289 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1290 parameters might be filled in, or might not be, depending on the
1291 kind of range this is. KIND will always be set to the appropriate
1292 value describing the kind of range, and this can be used to
1293 determine whether LOW or HIGH are valid. */
1296 rust_compute_range (struct type
*type
, struct value
*range
,
1297 LONGEST
*low
, LONGEST
*high
,
1298 enum range_type
*kind
)
1304 *kind
= BOTH_BOUND_DEFAULT
;
1306 if (type
->num_fields () == 0)
1310 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1312 *kind
= HIGH_BOUND_DEFAULT
;
1313 *low
= value_as_long (value_field (range
, 0));
1316 if (type
->num_fields () > i
1317 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1319 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1320 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1321 *high
= value_as_long (value_field (range
, i
));
1323 if (rust_inclusive_range_type_p (type
))
1328 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1330 static struct value
*
1331 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1334 struct value
*lhs
, *rhs
, *result
;
1335 struct type
*rhstype
;
1336 LONGEST low
, high_bound
;
1337 /* Initialized to appease the compiler. */
1338 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1343 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1344 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1346 if (noside
== EVAL_SKIP
)
1349 rhstype
= check_typedef (value_type (rhs
));
1350 if (rust_range_type_p (rhstype
))
1353 error (_("Can't take slice of array without '&'"));
1354 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1358 low
= value_as_long (rhs
);
1360 struct type
*type
= check_typedef (value_type (lhs
));
1361 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1363 struct type
*base_type
= nullptr;
1364 if (type
->code () == TYPE_CODE_ARRAY
)
1365 base_type
= TYPE_TARGET_TYPE (type
);
1366 else if (rust_slice_type_p (type
))
1368 for (int i
= 0; i
< type
->num_fields (); ++i
)
1370 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1372 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1376 if (base_type
== nullptr)
1377 error (_("Could not find 'data_ptr' in slice type"));
1379 else if (type
->code () == TYPE_CODE_PTR
)
1380 base_type
= TYPE_TARGET_TYPE (type
);
1382 error (_("Cannot subscript non-array type"));
1384 struct type
*new_type
;
1387 if (rust_slice_type_p (type
))
1392 = language_lookup_primitive_type (exp
->language_defn
,
1395 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1399 new_type
= base_type
;
1401 return value_zero (new_type
, VALUE_LVAL (lhs
));
1408 if (type
->code () == TYPE_CODE_ARRAY
)
1411 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1412 error (_("Can't compute array bounds"));
1414 error (_("Found array with non-zero lower bound"));
1417 else if (rust_slice_type_p (type
))
1421 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1422 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1424 high_bound
= value_as_long (len
);
1426 else if (type
->code () == TYPE_CODE_PTR
)
1430 high_bound
= LONGEST_MAX
;
1433 error (_("Cannot subscript non-array type"));
1436 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1439 error (_("Index less than zero"));
1440 if (low
> high_bound
)
1441 error (_("Index greater than length"));
1443 result
= value_subscript (base
, low
);
1450 struct type
*usize
, *slice
;
1452 struct value
*addrval
, *tem
;
1454 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1457 error (_("High index less than zero"));
1459 error (_("Low index greater than high index"));
1460 if (high
> high_bound
)
1461 error (_("High index greater than length"));
1463 usize
= language_lookup_primitive_type (exp
->language_defn
,
1466 const char *new_name
= ((type
!= nullptr
1467 && rust_slice_type_p (type
))
1468 ? type
->name () : "&[*gdb*]");
1470 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1472 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1473 addr
= value_as_long (addrval
);
1474 tem
= value_at_lazy (slice
, addr
);
1476 value_assign (value_field (tem
, 0), value_addr (result
));
1477 value_assign (value_field (tem
, 1),
1478 value_from_longest (usize
, high
- low
));
1480 result
= value_at_lazy (slice
, addr
);
1483 result
= value_addr (result
);
1489 /* evaluate_exp implementation for Rust. */
1491 static struct value
*
1492 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1493 int *pos
, enum noside noside
)
1495 struct value
*result
;
1497 switch (exp
->elts
[*pos
].opcode
)
1501 if (noside
!= EVAL_NORMAL
)
1502 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1506 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1509 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1510 if (trait_ptr
!= NULL
)
1513 result
= value_ind (value
);
1518 case UNOP_COMPLEMENT
:
1520 struct value
*value
;
1523 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1524 if (noside
== EVAL_SKIP
)
1526 /* Preserving the type is enough. */
1529 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1530 result
= value_from_longest (value_type (value
),
1531 value_logical_not (value
));
1533 result
= value_complement (value
);
1537 case BINOP_SUBSCRIPT
:
1538 result
= rust_subscript (exp
, pos
, noside
, 0);
1542 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1548 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1549 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1552 struct value
*addrval
= NULL
;
1556 if (noside
== EVAL_NORMAL
)
1558 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1559 addr
= value_as_long (addrval
);
1560 result
= value_at_lazy (type
, addr
);
1563 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1568 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1569 if (noside
== EVAL_NORMAL
)
1571 /* This isn't quite right but will do for the time
1572 being, seeing that we can't implement the Copy
1574 value_assign (result
, init
);
1580 gdb_assert (arglen
% 2 == 0);
1581 for (i
= 0; i
< arglen
; i
+= 2)
1584 const char *fieldname
;
1585 struct value
*value
, *field
;
1587 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1589 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1591 fieldname
= &exp
->elts
[*pos
].string
;
1592 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1594 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1595 if (noside
== EVAL_NORMAL
)
1597 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1599 value_assign (field
, value
);
1603 if (noside
== EVAL_SKIP
)
1604 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1606 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1607 result
= allocate_value (type
);
1609 result
= value_at_lazy (type
, addr
);
1618 struct value
*ncopies
;
1620 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1621 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1622 copies
= value_as_long (ncopies
);
1624 error (_("Array with negative number of elements"));
1626 if (noside
== EVAL_NORMAL
)
1629 std::vector
<struct value
*> eltvec (copies
);
1631 for (i
= 0; i
< copies
; ++i
)
1633 result
= value_array (0, copies
- 1, eltvec
.data ());
1637 struct type
*arraytype
1638 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1639 result
= allocate_value (arraytype
);
1644 case STRUCTOP_ANONYMOUS
:
1646 /* Anonymous field access, i.e. foo.1. */
1648 int pc
, field_number
, nfields
;
1652 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1654 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1656 type
= value_type (lhs
);
1658 if (type
->code () == TYPE_CODE_STRUCT
)
1660 struct type
*outer_type
= NULL
;
1662 if (rust_enum_p (type
))
1664 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1665 TYPE_LENGTH (type
));
1666 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1668 if (rust_empty_enum_p (type
))
1669 error (_("Cannot access field %d of empty enum %s"),
1670 field_number
, type
->name ());
1672 int fieldno
= rust_enum_variant (type
);
1673 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1675 type
= value_type (lhs
);
1678 /* Tuples and tuple structs */
1679 nfields
= type
->num_fields ();
1681 if (field_number
>= nfields
|| field_number
< 0)
1683 if (outer_type
!= NULL
)
1684 error(_("Cannot access field %d of variant %s::%s, "
1685 "there are only %d fields"),
1686 field_number
, outer_type
->name (),
1687 rust_last_path_segment (type
->name ()),
1690 error(_("Cannot access field %d of %s, "
1691 "there are only %d fields"),
1692 field_number
, type
->name (), nfields
);
1695 /* Tuples are tuple structs too. */
1696 if (!rust_tuple_struct_type_p (type
))
1698 if (outer_type
!= NULL
)
1699 error(_("Variant %s::%s is not a tuple variant"),
1700 outer_type
->name (),
1701 rust_last_path_segment (type
->name ()));
1703 error(_("Attempting to access anonymous field %d "
1704 "of %s, which is not a tuple, tuple struct, or "
1705 "tuple-like variant"),
1706 field_number
, type
->name ());
1709 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1712 error(_("Anonymous field access is only allowed on tuples, \
1713 tuple structs, and tuple-like enum variants"));
1717 case STRUCTOP_STRUCT
:
1724 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1725 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1726 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1728 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1729 type
= value_type (lhs
);
1730 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1732 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1733 TYPE_LENGTH (type
));
1734 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1736 if (rust_empty_enum_p (type
))
1737 error (_("Cannot access field %s of empty enum %s"),
1738 field_name
, type
->name ());
1740 int fieldno
= rust_enum_variant (type
);
1741 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1743 struct type
*outer_type
= type
;
1744 type
= value_type (lhs
);
1745 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1746 error (_("Attempting to access named field %s of tuple "
1747 "variant %s::%s, which has only anonymous fields"),
1748 field_name
, outer_type
->name (),
1749 rust_last_path_segment (type
->name ()));
1753 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1756 catch (const gdb_exception_error
&except
)
1758 error (_("Could not find field %s of struct variant %s::%s"),
1759 field_name
, outer_type
->name (),
1760 rust_last_path_segment (type
->name ()));
1764 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1765 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1766 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1771 result
= rust_range (exp
, pos
, noside
);
1775 /* We might have &array[range], in which case we need to make a
1777 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1780 result
= rust_subscript (exp
, pos
, noside
, 1);
1785 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1792 /* operator_length implementation for Rust. */
1795 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1801 switch (exp
->elts
[pc
- 1].opcode
)
1804 /* We handle aggregate as a type and argument count. The first
1805 argument might be OP_OTHERS. After that the arguments
1806 alternate: first an OP_NAME, then an expression. */
1808 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1816 case STRUCTOP_ANONYMOUS
:
1827 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1835 /* op_name implementation for Rust. */
1838 rust_op_name (enum exp_opcode opcode
)
1843 return "OP_AGGREGATE";
1847 return op_name_standard (opcode
);
1851 /* dump_subexp_body implementation for Rust. */
1854 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1857 switch (exp
->elts
[elt
].opcode
)
1861 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1864 fprintf_filtered (stream
, "Type @");
1865 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1866 fprintf_filtered (stream
, " (");
1867 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1868 fprintf_filtered (stream
, "), length %d", length
);
1871 for (i
= 0; i
< length
; ++i
)
1872 elt
= dump_subexp (exp
, stream
, elt
);
1879 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1881 fprintf_filtered (stream
, "%s: %s",
1882 (exp
->elts
[elt
].opcode
== OP_STRING
1883 ? "string" : "name"),
1884 &exp
->elts
[elt
+ 2].string
);
1885 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1890 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1893 case STRUCTOP_ANONYMOUS
:
1897 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1899 fprintf_filtered (stream
, "Field number: %d", field_number
);
1900 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1909 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1916 /* print_subexp implementation for Rust. */
1919 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1920 enum precedence prec
)
1922 switch (exp
->elts
[*pos
].opcode
)
1926 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1929 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1930 fputs_filtered (" { ", stream
);
1933 for (i
= 0; i
< length
; ++i
)
1935 rust_print_subexp (exp
, pos
, stream
, prec
);
1936 fputs_filtered (", ", stream
);
1938 fputs_filtered (" }", stream
);
1944 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1946 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1947 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1953 fputs_filtered ("<<others>> (", stream
);
1955 rust_print_subexp (exp
, pos
, stream
, prec
);
1956 fputs_filtered (")", stream
);
1960 case STRUCTOP_ANONYMOUS
:
1962 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1965 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1966 fprintf_filtered (stream
, ".%d", tem
);
1972 fprintf_filtered (stream
, "[");
1973 rust_print_subexp (exp
, pos
, stream
, prec
);
1974 fprintf_filtered (stream
, "; ");
1975 rust_print_subexp (exp
, pos
, stream
, prec
);
1976 fprintf_filtered (stream
, "]");
1980 print_subexp_standard (exp
, pos
, stream
, prec
);
1985 /* operator_check implementation for Rust. */
1988 rust_operator_check (struct expression
*exp
, int pos
,
1989 int (*objfile_func
) (struct objfile
*objfile
,
1993 switch (exp
->elts
[pos
].opcode
)
1997 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1998 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2000 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2011 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2019 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2021 static struct block_symbol
2022 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2024 const struct block
*block
,
2025 const domain_enum domain
)
2027 struct block_symbol result
= {};
2029 if (symbol_lookup_debug
)
2031 fprintf_unfiltered (gdb_stdlog
,
2032 "rust_lookup_symbol_non_local"
2033 " (%s, %s (scope %s), %s)\n",
2034 name
, host_address_to_string (block
),
2035 block_scope (block
), domain_name (domain
));
2038 /* Look up bare names in the block's scope. */
2039 std::string scopedname
;
2040 if (name
[cp_find_first_component (name
)] == '\0')
2042 const char *scope
= block_scope (block
);
2044 if (scope
[0] != '\0')
2046 scopedname
= std::string (scope
) + "::" + name
;
2047 name
= scopedname
.c_str ();
2055 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2056 if (result
.symbol
== NULL
)
2057 result
= lookup_global_symbol (name
, block
, domain
);
2064 /* la_sniff_from_mangled_name for Rust. */
2067 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2069 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2070 return *demangled
!= NULL
;
2075 /* la_watch_location_expression for Rust. */
2077 static gdb::unique_xmalloc_ptr
<char>
2078 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2080 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2081 std::string name
= type_to_string (type
);
2082 return gdb::unique_xmalloc_ptr
<char>
2083 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2089 static const struct exp_descriptor exp_descriptor_rust
=
2092 rust_operator_length
,
2093 rust_operator_check
,
2095 rust_dump_subexp_body
,
2096 rust_evaluate_subexp
2099 static const char *rust_extensions
[] =
2104 extern const struct language_defn rust_language_defn
=
2114 &exp_descriptor_rust
,
2117 rust_printchar
, /* Print a character constant */
2118 rust_printstr
, /* Function to print string constant */
2119 rust_emitchar
, /* Print a single char */
2120 rust_print_type
, /* Print a type using appropriate syntax */
2121 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2122 rust_value_print_inner
, /* la_value_print_inner */
2123 c_value_print
, /* Print a top-level value */
2124 default_read_var_value
, /* la_read_var_value */
2125 NULL
, /* Language specific skip_trampoline */
2126 NULL
, /* name_of_this */
2127 false, /* la_store_sym_names_in_linkage_form_p */
2128 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2129 basic_lookup_transparent_type
,/* lookup_transparent_type */
2130 gdb_demangle
, /* Language specific symbol demangler */
2131 rust_sniff_from_mangled_name
,
2132 NULL
, /* Language specific
2133 class_name_from_physname */
2134 c_op_print_tab
, /* expression operators for printing */
2135 1, /* c-style arrays */
2136 0, /* String lower bound */
2137 default_word_break_characters
,
2138 default_collect_symbol_completion_matches
,
2139 rust_language_arch_info
,
2140 default_print_array_index
,
2141 default_pass_by_reference
,
2142 rust_watch_location_expression
,
2143 NULL
, /* la_get_symbol_name_matcher */
2144 iterate_over_symbols
,
2145 default_search_name_hash
,
2146 &default_varobj_ops
,
2149 rust_is_string_type_p
,
2150 "{...}" /* la_struct_too_deep_ellipsis */