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 (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_printchar implementation for Rust. */
287 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
289 fputs_filtered ("'", stream
);
290 LA_EMIT_CHAR (c
, type
, stream
, '\'');
291 fputs_filtered ("'", stream
);
294 /* la_printstr implementation for Rust. */
297 rust_printstr (struct ui_file
*stream
, struct type
*type
,
298 const gdb_byte
*string
, unsigned int length
,
299 const char *user_encoding
, int force_ellipses
,
300 const struct value_print_options
*options
)
302 /* Rust always uses UTF-8, but let the caller override this if need
304 const char *encoding
= user_encoding
;
305 if (user_encoding
== NULL
|| !*user_encoding
)
307 /* In Rust strings, characters are "u8". */
308 if (rust_u8_type_p (type
))
312 /* This is probably some C string, so let's let C deal with
314 c_printstr (stream
, type
, string
, length
, user_encoding
,
315 force_ellipses
, options
);
320 /* This is not ideal as it doesn't use our character printer. */
321 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
327 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
329 const struct value_print_options
*options
);
331 /* Helper function to print a string slice. */
334 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
335 const struct value_print_options
*options
)
337 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
339 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
341 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
342 value_as_address (base
), value_as_long (len
), stream
,
346 /* rust_val_print helper for structs and untagged unions. */
349 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
350 const struct value_print_options
*options
)
354 struct type
*type
= check_typedef (value_type (val
));
356 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
358 /* If what we are printing here is actually a string within a
359 structure then VAL will be the original parent value, while TYPE
360 will be the type of the structure representing the string we want
362 However, RUST_VAL_PRINT_STR looks up the fields of the string
363 inside VAL, assuming that VAL is the string.
364 So, recreate VAL as a value representing just the string. */
365 val
= value_at_lazy (type
, value_address (val
));
366 rust_val_print_str (stream
, val
, options
);
370 bool is_tuple
= rust_tuple_type_p (type
);
371 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
372 struct value_print_options opts
;
376 if (type
->name () != NULL
)
377 fprintf_filtered (stream
, "%s", type
->name ());
379 if (type
->num_fields () == 0)
382 if (type
->name () != NULL
)
383 fputs_filtered (" ", stream
);
386 if (is_tuple
|| is_tuple_struct
)
387 fputs_filtered ("(", stream
);
389 fputs_filtered ("{", stream
);
395 for (i
= 0; i
< type
->num_fields (); ++i
)
397 if (field_is_static (&type
->field (i
)))
401 fputs_filtered (",", stream
);
403 if (options
->prettyformat
)
405 fputs_filtered ("\n", stream
);
406 print_spaces_filtered (2 + 2 * recurse
, stream
);
408 else if (!first_field
)
409 fputs_filtered (" ", stream
);
413 if (!is_tuple
&& !is_tuple_struct
)
415 fputs_styled (TYPE_FIELD_NAME (type
, i
),
416 variable_name_style
.style (), stream
);
417 fputs_filtered (": ", stream
);
420 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
424 if (options
->prettyformat
)
426 fputs_filtered ("\n", stream
);
427 print_spaces_filtered (2 * recurse
, stream
);
430 if (is_tuple
|| is_tuple_struct
)
431 fputs_filtered (")", stream
);
433 fputs_filtered ("}", stream
);
436 /* rust_val_print helper for discriminated unions (Rust enums). */
439 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
440 const struct value_print_options
*options
)
442 struct value_print_options opts
= *options
;
443 struct type
*type
= check_typedef (value_type (val
));
447 gdb_assert (rust_enum_p (type
));
448 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
449 TYPE_LENGTH (value_type (val
)));
450 type
= resolve_dynamic_type (type
, view
, value_address (val
));
452 if (rust_empty_enum_p (type
))
454 /* Print the enum type name here to be more clear. */
455 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
457 metadata_style
.style ().ptr (), nullptr);
461 int variant_fieldno
= rust_enum_variant (type
);
462 val
= value_field (val
, variant_fieldno
);
463 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
465 int nfields
= variant_type
->num_fields ();
467 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
469 fprintf_filtered (stream
, "%s", variant_type
->name ());
472 /* In case of a nullary variant like 'None', just output
477 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
479 fprintf_filtered (stream
, "(");
482 /* struct variant. */
483 fprintf_filtered (stream
, "{");
486 bool first_field
= true;
487 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
490 fputs_filtered (", ", stream
);
494 fprintf_filtered (stream
, "%ps: ",
495 styled_string (variable_name_style
.style (),
496 TYPE_FIELD_NAME (variant_type
, j
)));
498 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
503 fputs_filtered (")", stream
);
505 fputs_filtered ("}", stream
);
508 static const struct generic_val_print_decorations rust_decorations
=
510 /* Complex isn't used in Rust, but we provide C-ish values just in
522 /* la_value_print_inner implementation for Rust. */
524 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
526 const struct value_print_options
*options
)
528 struct value_print_options opts
= *options
;
531 if (opts
.prettyformat
== Val_prettyformat_default
)
532 opts
.prettyformat
= (opts
.prettyformat_structs
533 ? Val_prettyformat
: Val_no_prettyformat
);
535 struct type
*type
= check_typedef (value_type (val
));
536 switch (type
->code ())
540 LONGEST low_bound
, high_bound
;
542 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
543 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
544 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
547 /* We have a pointer to a byte string, so just print
549 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
550 CORE_ADDR addr
= value_as_address (val
);
551 struct gdbarch
*arch
= get_type_arch (type
);
553 if (opts
.addressprint
)
555 fputs_filtered (paddress (arch
, addr
), stream
);
556 fputs_filtered (" ", stream
);
559 fputs_filtered ("b", stream
);
560 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
561 high_bound
- low_bound
+ 1, stream
,
568 case TYPE_CODE_METHODPTR
:
569 case TYPE_CODE_MEMBERPTR
:
570 c_value_print_inner (val
, stream
, recurse
, &opts
);
574 /* Recognize the unit type. */
575 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
576 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
578 fputs_filtered ("()", stream
);
583 case TYPE_CODE_STRING
:
585 LONGEST low_bound
, high_bound
;
587 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
588 error (_("Could not determine the array bounds"));
590 /* If we see a plain TYPE_CODE_STRING, then we're printing a
591 byte string, hence the choice of "ASCII" as the
593 fputs_filtered ("b", stream
);
594 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
595 value_contents_for_printing (val
),
596 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
600 case TYPE_CODE_ARRAY
:
602 LONGEST low_bound
, high_bound
;
604 if (get_array_bounds (type
, &low_bound
, &high_bound
)
605 && high_bound
- low_bound
+ 1 == 0)
606 fputs_filtered ("[]", stream
);
612 case TYPE_CODE_UNION
:
613 /* Untagged unions are printed as if they are structs. Since
614 the field bit positions overlap in the debuginfo, the code
615 for printing a union is same as that for a struct, the only
616 difference is that the input type will have overlapping
618 val_print_struct (val
, stream
, recurse
, &opts
);
621 case TYPE_CODE_STRUCT
:
622 if (rust_enum_p (type
))
623 rust_print_enum (val
, stream
, recurse
, &opts
);
625 val_print_struct (val
, stream
, recurse
, &opts
);
630 /* Nothing special yet. */
631 generic_value_print (val
, stream
, recurse
, &opts
, &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 (), 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 ();
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
);
677 /* Already printing an outer enum, so nothing to print here. */
681 /* This code path is also used by unions and enums. */
684 fputs_filtered ("enum ", stream
);
685 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
686 if (prop
!= nullptr && prop
->kind
== PROP_TYPE
)
687 type
= prop
->data
.original_type
;
689 else if (type
->code () == TYPE_CODE_STRUCT
)
690 fputs_filtered ("struct ", stream
);
692 fputs_filtered ("union ", stream
);
695 fputs_filtered (tagname
, stream
);
698 if (type
->num_fields () == 0 && !is_tuple
)
700 if (for_rust_enum
&& !flags
->print_offsets
)
701 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
703 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
705 /* When printing offsets, we rearrange the fields into storage
706 order. This lets us show holes more clearly. We work using
707 field indices here because it simplifies calls to
708 print_offset_data::update below. */
709 std::vector
<int> fields
;
710 for (int i
= 0; i
< type
->num_fields (); ++i
)
712 if (field_is_static (&type
->field (i
)))
714 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
716 fields
.push_back (i
);
718 if (flags
->print_offsets
)
719 std::sort (fields
.begin (), fields
.end (),
722 return (TYPE_FIELD_BITPOS (type
, a
)
723 < TYPE_FIELD_BITPOS (type
, b
));
730 gdb_assert (!field_is_static (&type
->field (i
)));
731 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
733 if (flags
->print_offsets
)
734 podata
->update (type
, i
, stream
);
736 /* We'd like to print "pub" here as needed, but rustc
737 doesn't emit the debuginfo, and our types don't have
738 cplus_struct_type attached. */
740 /* For a tuple struct we print the type but nothing
742 if (!for_rust_enum
|| flags
->print_offsets
)
743 print_spaces_filtered (level
+ 2, stream
);
745 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
747 else if (!is_tuple_struct
)
748 fprintf_filtered (stream
, "%ps: ",
749 styled_string (variable_name_style
.style (),
750 TYPE_FIELD_NAME (type
, i
)));
752 rust_internal_print_type (type
->field (i
).type (), NULL
,
753 stream
, (is_enum
? show
: show
- 1),
754 level
+ 2, flags
, is_enum
, podata
);
755 if (!for_rust_enum
|| flags
->print_offsets
)
756 fputs_filtered (",\n", stream
);
757 /* Note that this check of "I" is ok because we only sorted the
758 fields by offset when print_offsets was set, so we won't take
759 this branch in that case. */
760 else if (i
+ 1 < type
->num_fields ())
761 fputs_filtered (", ", stream
);
764 if (flags
->print_offsets
)
766 /* Undo the temporary level increase we did above. */
768 podata
->finish (type
, level
, stream
);
769 print_spaces_filtered (print_offset_data::indentation
, stream
);
771 print_spaces_filtered (2, stream
);
773 if (!for_rust_enum
|| flags
->print_offsets
)
774 print_spaces_filtered (level
, stream
);
775 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
778 /* la_print_typedef implementation for Rust. */
781 rust_print_typedef (struct type
*type
,
782 struct symbol
*new_symbol
,
783 struct ui_file
*stream
)
785 type
= check_typedef (type
);
786 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
787 type_print (type
, "", stream
, 0);
788 fprintf_filtered (stream
, ";");
791 /* la_print_type implementation for Rust. */
794 rust_internal_print_type (struct type
*type
, const char *varstring
,
795 struct ui_file
*stream
, int show
, int level
,
796 const struct type_print_options
*flags
,
797 bool for_rust_enum
, print_offset_data
*podata
)
801 && type
->name () != NULL
)
803 /* Rust calls the unit type "void" in its debuginfo,
804 but we don't want to print it as that. */
805 if (type
->code () == TYPE_CODE_VOID
)
806 fputs_filtered ("()", stream
);
808 fputs_filtered (type
->name (), stream
);
812 type
= check_typedef (type
);
813 switch (type
->code ())
816 /* If we have an enum, we've already printed the type's
817 unqualified name, and there is nothing else to print
820 fputs_filtered ("()", stream
);
824 /* Delegate varargs to the C printer. */
825 if (TYPE_VARARGS (type
))
828 fputs_filtered ("fn ", stream
);
829 if (varstring
!= NULL
)
830 fputs_filtered (varstring
, stream
);
831 fputs_filtered ("(", stream
);
832 for (int i
= 0; i
< type
->num_fields (); ++i
)
836 fputs_filtered (", ", stream
);
837 rust_internal_print_type (type
->field (i
).type (), "", stream
,
838 -1, 0, flags
, false, podata
);
840 fputs_filtered (")", stream
);
841 /* If it returns unit, we can omit the return type. */
842 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
844 fputs_filtered (" -> ", stream
);
845 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
846 -1, 0, flags
, false, podata
);
850 case TYPE_CODE_ARRAY
:
852 LONGEST low_bound
, high_bound
;
854 fputs_filtered ("[", stream
);
855 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
856 stream
, show
- 1, level
, flags
, false,
859 if (TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCEXPR
860 || TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCLIST
)
861 fprintf_filtered (stream
, "; variable length");
862 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
863 fprintf_filtered (stream
, "; %s",
864 plongest (high_bound
- low_bound
+ 1));
865 fputs_filtered ("]", stream
);
869 case TYPE_CODE_UNION
:
870 case TYPE_CODE_STRUCT
:
871 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
872 for_rust_enum
, podata
);
879 fputs_filtered ("enum ", stream
);
880 if (type
->name () != NULL
)
882 fputs_filtered (type
->name (), stream
);
883 fputs_filtered (" ", stream
);
884 len
= strlen (type
->name ());
886 fputs_filtered ("{\n", stream
);
888 for (int i
= 0; i
< type
->num_fields (); ++i
)
890 const char *name
= TYPE_FIELD_NAME (type
, i
);
895 && strncmp (name
, type
->name (), len
) == 0
897 && name
[len
+ 1] == ':')
899 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
900 styled_string (variable_name_style
.style (),
904 fputs_filtered ("}", stream
);
910 if (type
->name () != nullptr)
911 fputs_filtered (type
->name (), stream
);
914 /* We currently can't distinguish between pointers and
916 fputs_filtered ("*mut ", stream
);
917 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
924 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
930 /* Like arch_composite_type, but uses TYPE to decide how to allocate
931 -- either on an obstack or on a gdbarch. */
934 rust_composite_type (struct type
*original
,
936 const char *field1
, struct type
*type1
,
937 const char *field2
, struct type
*type2
)
939 struct type
*result
= alloc_type_copy (original
);
940 int i
, nfields
, bitpos
;
948 result
->set_code (TYPE_CODE_STRUCT
);
949 result
->set_name (name
);
951 result
->set_num_fields (nfields
);
953 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
959 struct field
*field
= &result
->field (i
);
961 SET_FIELD_BITPOS (*field
, bitpos
);
962 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
964 FIELD_NAME (*field
) = field1
;
965 field
->set_type (type1
);
970 struct field
*field
= &result
->field (i
);
971 unsigned align
= type_align (type2
);
977 align
*= TARGET_CHAR_BIT
;
978 delta
= bitpos
% align
;
980 bitpos
+= align
- delta
;
982 SET_FIELD_BITPOS (*field
, bitpos
);
984 FIELD_NAME (*field
) = field2
;
985 field
->set_type (type2
);
991 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
992 TYPE_LENGTH (result
->field (i
- 1).type ()));
996 /* See rust-lang.h. */
999 rust_slice_type (const char *name
, struct type
*elt_type
,
1000 struct type
*usize_type
)
1004 elt_type
= lookup_pointer_type (elt_type
);
1005 type
= rust_composite_type (elt_type
, name
,
1006 "data_ptr", elt_type
,
1007 "length", usize_type
);
1012 enum rust_primitive_types
1014 rust_primitive_bool
,
1015 rust_primitive_char
,
1024 rust_primitive_isize
,
1025 rust_primitive_usize
,
1028 rust_primitive_unit
,
1030 nr_rust_primitive_types
1035 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1037 static struct value
*
1038 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1041 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1043 struct value
*function
, *result
, *arg0
;
1044 struct type
*type
, *fn_type
;
1045 const struct block
*block
;
1046 struct block_symbol sym
;
1048 /* For an ordinary function call we can simply defer to the
1049 generic implementation. */
1050 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1051 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1053 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1055 method
= &exp
->elts
[*pos
+ 1].string
;
1056 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1058 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1059 type in order to look up the method. */
1060 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1062 if (noside
== EVAL_SKIP
)
1064 for (i
= 0; i
< num_args
; ++i
)
1065 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1069 std::vector
<struct value
*> args (num_args
+ 1);
1072 /* We don't yet implement real Deref semantics. */
1073 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1074 args
[0] = value_ind (args
[0]);
1076 type
= value_type (args
[0]);
1077 if ((type
->code () != TYPE_CODE_STRUCT
1078 && type
->code () != TYPE_CODE_UNION
1079 && type
->code () != TYPE_CODE_ENUM
)
1080 || rust_tuple_type_p (type
))
1081 error (_("Method calls only supported on struct or enum types"));
1082 if (type
->name () == NULL
)
1083 error (_("Method call on nameless type"));
1085 std::string name
= std::string (type
->name ()) + "::" + method
;
1087 block
= get_selected_block (0);
1088 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1089 if (sym
.symbol
== NULL
)
1090 error (_("Could not find function named '%s'"), name
.c_str ());
1092 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1093 if (fn_type
->num_fields () == 0)
1094 error (_("Function '%s' takes no arguments"), name
.c_str ());
1096 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1097 args
[0] = value_addr (args
[0]);
1099 function
= address_of_variable (sym
.symbol
, block
);
1101 for (i
= 0; i
< num_args
; ++i
)
1102 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1104 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1105 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1107 result
= call_function_by_hand (function
, NULL
, args
);
1111 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1113 static struct value
*
1114 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1116 enum range_type kind
;
1117 struct value
*low
= NULL
, *high
= NULL
;
1118 struct value
*addrval
, *result
;
1120 struct type
*range_type
;
1121 struct type
*index_type
;
1122 struct type
*temp_type
;
1125 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1128 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1129 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1130 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1131 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1132 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1133 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1134 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1136 if (noside
== EVAL_SKIP
)
1137 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1144 name
= "std::ops::RangeFull";
1148 index_type
= value_type (high
);
1150 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1157 index_type
= value_type (low
);
1158 name
= "std::ops::RangeFrom";
1162 if (!types_equal (value_type (low
), value_type (high
)))
1163 error (_("Range expression with different types"));
1164 index_type
= value_type (low
);
1165 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1169 /* If we don't have an index type, just allocate this on the
1170 arch. Here any type will do. */
1171 temp_type
= (index_type
== NULL
1172 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1174 /* It would be nicer to cache the range type. */
1175 range_type
= rust_composite_type (temp_type
, name
,
1176 low
== NULL
? NULL
: "start", index_type
,
1177 high
== NULL
? NULL
: "end", index_type
);
1179 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1180 return value_zero (range_type
, lval_memory
);
1182 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1183 addr
= value_as_long (addrval
);
1184 result
= value_at_lazy (range_type
, addr
);
1188 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1191 value_assign (start
, low
);
1196 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1199 value_assign (end
, high
);
1202 result
= value_at_lazy (range_type
, addr
);
1206 /* A helper function to compute the range and kind given a range
1207 value. TYPE is the type of the range value. RANGE is the range
1208 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1209 parameters might be filled in, or might not be, depending on the
1210 kind of range this is. KIND will always be set to the appropriate
1211 value describing the kind of range, and this can be used to
1212 determine whether LOW or HIGH are valid. */
1215 rust_compute_range (struct type
*type
, struct value
*range
,
1216 LONGEST
*low
, LONGEST
*high
,
1217 enum range_type
*kind
)
1223 *kind
= BOTH_BOUND_DEFAULT
;
1225 if (type
->num_fields () == 0)
1229 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1231 *kind
= HIGH_BOUND_DEFAULT
;
1232 *low
= value_as_long (value_field (range
, 0));
1235 if (type
->num_fields () > i
1236 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1238 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1239 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1240 *high
= value_as_long (value_field (range
, i
));
1242 if (rust_inclusive_range_type_p (type
))
1247 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1249 static struct value
*
1250 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1253 struct value
*lhs
, *rhs
, *result
;
1254 struct type
*rhstype
;
1255 LONGEST low
, high_bound
;
1256 /* Initialized to appease the compiler. */
1257 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1262 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1263 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1265 if (noside
== EVAL_SKIP
)
1268 rhstype
= check_typedef (value_type (rhs
));
1269 if (rust_range_type_p (rhstype
))
1272 error (_("Can't take slice of array without '&'"));
1273 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1277 low
= value_as_long (rhs
);
1279 struct type
*type
= check_typedef (value_type (lhs
));
1280 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1282 struct type
*base_type
= nullptr;
1283 if (type
->code () == TYPE_CODE_ARRAY
)
1284 base_type
= TYPE_TARGET_TYPE (type
);
1285 else if (rust_slice_type_p (type
))
1287 for (int i
= 0; i
< type
->num_fields (); ++i
)
1289 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1291 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1295 if (base_type
== nullptr)
1296 error (_("Could not find 'data_ptr' in slice type"));
1298 else if (type
->code () == TYPE_CODE_PTR
)
1299 base_type
= TYPE_TARGET_TYPE (type
);
1301 error (_("Cannot subscript non-array type"));
1303 struct type
*new_type
;
1306 if (rust_slice_type_p (type
))
1311 = language_lookup_primitive_type (exp
->language_defn
,
1314 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1318 new_type
= base_type
;
1320 return value_zero (new_type
, VALUE_LVAL (lhs
));
1327 if (type
->code () == TYPE_CODE_ARRAY
)
1330 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1331 error (_("Can't compute array bounds"));
1333 error (_("Found array with non-zero lower bound"));
1336 else if (rust_slice_type_p (type
))
1340 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1341 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1343 high_bound
= value_as_long (len
);
1345 else if (type
->code () == TYPE_CODE_PTR
)
1349 high_bound
= LONGEST_MAX
;
1352 error (_("Cannot subscript non-array type"));
1355 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1358 error (_("Index less than zero"));
1359 if (low
> high_bound
)
1360 error (_("Index greater than length"));
1362 result
= value_subscript (base
, low
);
1369 struct type
*usize
, *slice
;
1371 struct value
*addrval
, *tem
;
1373 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1376 error (_("High index less than zero"));
1378 error (_("Low index greater than high index"));
1379 if (high
> high_bound
)
1380 error (_("High index greater than length"));
1382 usize
= language_lookup_primitive_type (exp
->language_defn
,
1385 const char *new_name
= ((type
!= nullptr
1386 && rust_slice_type_p (type
))
1387 ? type
->name () : "&[*gdb*]");
1389 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1391 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1392 addr
= value_as_long (addrval
);
1393 tem
= value_at_lazy (slice
, addr
);
1395 value_assign (value_field (tem
, 0), value_addr (result
));
1396 value_assign (value_field (tem
, 1),
1397 value_from_longest (usize
, high
- low
));
1399 result
= value_at_lazy (slice
, addr
);
1402 result
= value_addr (result
);
1408 /* evaluate_exp implementation for Rust. */
1410 static struct value
*
1411 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1412 int *pos
, enum noside noside
)
1414 struct value
*result
;
1416 switch (exp
->elts
[*pos
].opcode
)
1420 if (noside
!= EVAL_NORMAL
)
1421 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1425 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1428 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1429 if (trait_ptr
!= NULL
)
1432 result
= value_ind (value
);
1437 case UNOP_COMPLEMENT
:
1439 struct value
*value
;
1442 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1443 if (noside
== EVAL_SKIP
)
1445 /* Preserving the type is enough. */
1448 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1449 result
= value_from_longest (value_type (value
),
1450 value_logical_not (value
));
1452 result
= value_complement (value
);
1456 case BINOP_SUBSCRIPT
:
1457 result
= rust_subscript (exp
, pos
, noside
, 0);
1461 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1467 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1468 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1471 struct value
*addrval
= NULL
;
1475 if (noside
== EVAL_NORMAL
)
1477 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1478 addr
= value_as_long (addrval
);
1479 result
= value_at_lazy (type
, addr
);
1482 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1487 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1488 if (noside
== EVAL_NORMAL
)
1490 /* This isn't quite right but will do for the time
1491 being, seeing that we can't implement the Copy
1493 value_assign (result
, init
);
1499 gdb_assert (arglen
% 2 == 0);
1500 for (i
= 0; i
< arglen
; i
+= 2)
1503 const char *fieldname
;
1504 struct value
*value
, *field
;
1506 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1508 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1510 fieldname
= &exp
->elts
[*pos
].string
;
1511 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1513 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1514 if (noside
== EVAL_NORMAL
)
1516 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1518 value_assign (field
, value
);
1522 if (noside
== EVAL_SKIP
)
1523 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1525 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1526 result
= allocate_value (type
);
1528 result
= value_at_lazy (type
, addr
);
1537 struct value
*ncopies
;
1539 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1540 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1541 copies
= value_as_long (ncopies
);
1543 error (_("Array with negative number of elements"));
1545 if (noside
== EVAL_NORMAL
)
1548 std::vector
<struct value
*> eltvec (copies
);
1550 for (i
= 0; i
< copies
; ++i
)
1552 result
= value_array (0, copies
- 1, eltvec
.data ());
1556 struct type
*arraytype
1557 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1558 result
= allocate_value (arraytype
);
1563 case STRUCTOP_ANONYMOUS
:
1565 /* Anonymous field access, i.e. foo.1. */
1567 int pc
, field_number
, nfields
;
1571 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1573 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1575 type
= value_type (lhs
);
1577 if (type
->code () == TYPE_CODE_STRUCT
)
1579 struct type
*outer_type
= NULL
;
1581 if (rust_enum_p (type
))
1583 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1584 TYPE_LENGTH (type
));
1585 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1587 if (rust_empty_enum_p (type
))
1588 error (_("Cannot access field %d of empty enum %s"),
1589 field_number
, type
->name ());
1591 int fieldno
= rust_enum_variant (type
);
1592 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1594 type
= value_type (lhs
);
1597 /* Tuples and tuple structs */
1598 nfields
= type
->num_fields ();
1600 if (field_number
>= nfields
|| field_number
< 0)
1602 if (outer_type
!= NULL
)
1603 error(_("Cannot access field %d of variant %s::%s, "
1604 "there are only %d fields"),
1605 field_number
, outer_type
->name (),
1606 rust_last_path_segment (type
->name ()),
1609 error(_("Cannot access field %d of %s, "
1610 "there are only %d fields"),
1611 field_number
, type
->name (), nfields
);
1614 /* Tuples are tuple structs too. */
1615 if (!rust_tuple_struct_type_p (type
))
1617 if (outer_type
!= NULL
)
1618 error(_("Variant %s::%s is not a tuple variant"),
1619 outer_type
->name (),
1620 rust_last_path_segment (type
->name ()));
1622 error(_("Attempting to access anonymous field %d "
1623 "of %s, which is not a tuple, tuple struct, or "
1624 "tuple-like variant"),
1625 field_number
, type
->name ());
1628 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1631 error(_("Anonymous field access is only allowed on tuples, \
1632 tuple structs, and tuple-like enum variants"));
1636 case STRUCTOP_STRUCT
:
1643 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1644 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1645 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1647 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1648 type
= value_type (lhs
);
1649 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1651 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1652 TYPE_LENGTH (type
));
1653 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1655 if (rust_empty_enum_p (type
))
1656 error (_("Cannot access field %s of empty enum %s"),
1657 field_name
, type
->name ());
1659 int fieldno
= rust_enum_variant (type
);
1660 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1662 struct type
*outer_type
= type
;
1663 type
= value_type (lhs
);
1664 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1665 error (_("Attempting to access named field %s of tuple "
1666 "variant %s::%s, which has only anonymous fields"),
1667 field_name
, outer_type
->name (),
1668 rust_last_path_segment (type
->name ()));
1672 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1675 catch (const gdb_exception_error
&except
)
1677 error (_("Could not find field %s of struct variant %s::%s"),
1678 field_name
, outer_type
->name (),
1679 rust_last_path_segment (type
->name ()));
1683 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1684 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1685 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1690 result
= rust_range (exp
, pos
, noside
);
1694 /* We might have &array[range], in which case we need to make a
1696 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1699 result
= rust_subscript (exp
, pos
, noside
, 1);
1704 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1711 /* operator_length implementation for Rust. */
1714 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1720 switch (exp
->elts
[pc
- 1].opcode
)
1723 /* We handle aggregate as a type and argument count. The first
1724 argument might be OP_OTHERS. After that the arguments
1725 alternate: first an OP_NAME, then an expression. */
1727 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1735 case STRUCTOP_ANONYMOUS
:
1746 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1754 /* op_name implementation for Rust. */
1757 rust_op_name (enum exp_opcode opcode
)
1762 return "OP_AGGREGATE";
1766 return op_name_standard (opcode
);
1770 /* dump_subexp_body implementation for Rust. */
1773 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1776 switch (exp
->elts
[elt
].opcode
)
1780 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1783 fprintf_filtered (stream
, "Type @");
1784 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1785 fprintf_filtered (stream
, " (");
1786 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1787 fprintf_filtered (stream
, "), length %d", length
);
1790 for (i
= 0; i
< length
; ++i
)
1791 elt
= dump_subexp (exp
, stream
, elt
);
1798 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1800 fprintf_filtered (stream
, "%s: %s",
1801 (exp
->elts
[elt
].opcode
== OP_STRING
1802 ? "string" : "name"),
1803 &exp
->elts
[elt
+ 2].string
);
1804 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1809 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1812 case STRUCTOP_ANONYMOUS
:
1816 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1818 fprintf_filtered (stream
, "Field number: %d", field_number
);
1819 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1828 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1835 /* print_subexp implementation for Rust. */
1838 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1839 enum precedence prec
)
1841 switch (exp
->elts
[*pos
].opcode
)
1845 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1848 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1849 fputs_filtered (" { ", stream
);
1852 for (i
= 0; i
< length
; ++i
)
1854 rust_print_subexp (exp
, pos
, stream
, prec
);
1855 fputs_filtered (", ", stream
);
1857 fputs_filtered (" }", stream
);
1863 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1865 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1866 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1872 fputs_filtered ("<<others>> (", stream
);
1874 rust_print_subexp (exp
, pos
, stream
, prec
);
1875 fputs_filtered (")", stream
);
1879 case STRUCTOP_ANONYMOUS
:
1881 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1884 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1885 fprintf_filtered (stream
, ".%d", tem
);
1891 fprintf_filtered (stream
, "[");
1892 rust_print_subexp (exp
, pos
, stream
, prec
);
1893 fprintf_filtered (stream
, "; ");
1894 rust_print_subexp (exp
, pos
, stream
, prec
);
1895 fprintf_filtered (stream
, "]");
1899 print_subexp_standard (exp
, pos
, stream
, prec
);
1904 /* operator_check implementation for Rust. */
1907 rust_operator_check (struct expression
*exp
, int pos
,
1908 int (*objfile_func
) (struct objfile
*objfile
,
1912 switch (exp
->elts
[pos
].opcode
)
1916 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1917 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1919 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1930 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1938 static const struct exp_descriptor exp_descriptor_rust
=
1941 rust_operator_length
,
1942 rust_operator_check
,
1944 rust_dump_subexp_body
,
1945 rust_evaluate_subexp
1948 static const char *rust_extensions
[] =
1953 /* Constant data representing the Rust language. */
1955 extern const struct language_data rust_language_data
=
1965 &exp_descriptor_rust
,
1966 rust_printchar
, /* Print a character constant */
1967 rust_printstr
, /* Function to print string constant */
1968 rust_print_typedef
, /* Print a typedef using appropriate syntax */
1969 NULL
, /* name_of_this */
1970 false, /* la_store_sym_names_in_linkage_form_p */
1971 c_op_print_tab
, /* expression operators for printing */
1972 1, /* c-style arrays */
1973 0, /* String lower bound */
1974 &default_varobj_ops
,
1975 rust_is_string_type_p
,
1976 "{...}" /* la_struct_too_deep_ellipsis */
1979 /* Class representing the Rust language. */
1981 class rust_language
: public language_defn
1985 : language_defn (language_rust
, rust_language_data
)
1988 /* See language.h. */
1989 void language_arch_info (struct gdbarch
*gdbarch
,
1990 struct language_arch_info
*lai
) const override
1992 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1995 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1998 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1999 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
2000 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
2001 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
2002 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
2003 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
2004 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
2005 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
2006 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
2007 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
2009 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
2010 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
2011 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
2013 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
2014 floatformats_ieee_single
);
2015 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
2016 floatformats_ieee_double
);
2018 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
2020 struct type
*tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
2021 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
2022 types
[rust_primitive_usize
]);
2024 lai
->primitive_type_vector
= types
;
2025 lai
->bool_type_default
= types
[rust_primitive_bool
];
2026 lai
->string_char_type
= types
[rust_primitive_u8
];
2029 /* See language.h. */
2030 bool sniff_from_mangled_name (const char *mangled
,
2031 char **demangled
) const override
2033 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2034 return *demangled
!= NULL
;
2037 /* See language.h. */
2039 char *demangle (const char *mangled
, int options
) const override
2041 return gdb_demangle (mangled
, options
);
2044 /* See language.h. */
2046 void print_type (struct type
*type
, const char *varstring
,
2047 struct ui_file
*stream
, int show
, int level
,
2048 const struct type_print_options
*flags
) const override
2050 print_offset_data podata
;
2051 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
2052 flags
, false, &podata
);
2055 /* See language.h. */
2057 gdb::unique_xmalloc_ptr
<char> watch_location_expression
2058 (struct type
*type
, CORE_ADDR addr
) const override
2060 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2061 std::string name
= type_to_string (type
);
2062 return gdb::unique_xmalloc_ptr
<char>
2063 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2067 /* See language.h. */
2069 void value_print_inner
2070 (struct value
*val
, struct ui_file
*stream
, int recurse
,
2071 const struct value_print_options
*options
) const override
2073 return rust_value_print_inner (val
, stream
, recurse
, options
);
2076 /* See language.h. */
2078 struct block_symbol lookup_symbol_nonlocal
2079 (const char *name
, const struct block
*block
,
2080 const domain_enum domain
) const override
2082 struct block_symbol result
= {};
2084 if (symbol_lookup_debug
)
2086 fprintf_unfiltered (gdb_stdlog
,
2087 "rust_lookup_symbol_non_local"
2088 " (%s, %s (scope %s), %s)\n",
2089 name
, host_address_to_string (block
),
2090 block_scope (block
), domain_name (domain
));
2093 /* Look up bare names in the block's scope. */
2094 std::string scopedname
;
2095 if (name
[cp_find_first_component (name
)] == '\0')
2097 const char *scope
= block_scope (block
);
2099 if (scope
[0] != '\0')
2101 scopedname
= std::string (scope
) + "::" + name
;
2102 name
= scopedname
.c_str ();
2110 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2111 if (result
.symbol
== NULL
)
2112 result
= lookup_global_symbol (name
, block
, domain
);
2117 /* See language.h. */
2119 int parser (struct parser_state
*ps
) const override
2121 return rust_parse (ps
);
2124 /* See language.h. */
2126 void emitchar (int ch
, struct type
*chtype
,
2127 struct ui_file
*stream
, int quoter
) const override
2129 if (!rust_chartype_p (chtype
))
2130 generic_emit_char (ch
, chtype
, stream
, quoter
,
2131 target_charset (get_type_arch (chtype
)));
2132 else if (ch
== '\\' || ch
== quoter
)
2133 fprintf_filtered (stream
, "\\%c", ch
);
2134 else if (ch
== '\n')
2135 fputs_filtered ("\\n", stream
);
2136 else if (ch
== '\r')
2137 fputs_filtered ("\\r", stream
);
2138 else if (ch
== '\t')
2139 fputs_filtered ("\\t", stream
);
2140 else if (ch
== '\0')
2141 fputs_filtered ("\\0", stream
);
2142 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
2143 fputc_filtered (ch
, stream
);
2145 fprintf_filtered (stream
, "\\x%02x", ch
);
2147 fprintf_filtered (stream
, "\\u{%06x}", ch
);
2151 /* Single instance of the Rust language class. */
2153 static rust_language rust_language_defn
;