1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2022 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"
41 #include "parser-defs.h"
44 /* See rust-lang.h. */
47 rust_last_path_segment (const char *path
)
49 const char *result
= strrchr (path
, ':');
56 /* See rust-lang.h. */
59 rust_crate_for_block (const struct block
*block
)
61 const char *scope
= block_scope (block
);
64 return std::string ();
66 return std::string (scope
, cp_find_first_component (scope
));
69 /* Return true if TYPE, which must be a struct type, represents a Rust
73 rust_enum_p (struct type
*type
)
75 /* is_dynamic_type will return true if any field has a dynamic
76 attribute -- but we only want to check the top level. */
77 return TYPE_HAS_VARIANT_PARTS (type
);
80 /* Return true if TYPE, which must be an already-resolved enum type,
84 rust_empty_enum_p (const struct type
*type
)
86 return type
->num_fields () == 0;
89 /* Given an already-resolved enum type and contents, find which
93 rust_enum_variant (struct type
*type
)
95 /* The active variant is simply the first non-artificial field. */
96 for (int i
= 0; i
< type
->num_fields (); ++i
)
97 if (!TYPE_FIELD_ARTIFICIAL (type
, i
))
100 /* Perhaps we could get here by trying to print an Ada variant
101 record in Rust mode. Unlikely, but an error is safer than an
103 error (_("Could not find active enum variant"));
106 /* See rust-lang.h. */
109 rust_tuple_type_p (struct type
*type
)
111 /* The current implementation is a bit of a hack, but there's
112 nothing else in the debuginfo to distinguish a tuple from a
114 return (type
->code () == TYPE_CODE_STRUCT
115 && type
->name () != NULL
116 && type
->name ()[0] == '(');
119 /* Return true if all non-static fields of a structlike type are in a
120 sequence like __0, __1, __2. */
123 rust_underscore_fields (struct type
*type
)
129 if (type
->code () != TYPE_CODE_STRUCT
)
131 for (i
= 0; i
< type
->num_fields (); ++i
)
133 if (!field_is_static (&type
->field (i
)))
137 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
138 if (strcmp (buf
, type
->field (i
).name ()) != 0)
146 /* See rust-lang.h. */
149 rust_tuple_struct_type_p (struct type
*type
)
151 /* This is just an approximation until DWARF can represent Rust more
152 precisely. We exclude zero-length structs because they may not
153 be tuple structs, and there's no way to tell. */
154 return type
->num_fields () > 0 && rust_underscore_fields (type
);
157 /* Return true if TYPE is a slice type, otherwise false. */
160 rust_slice_type_p (struct type
*type
)
162 if (type
->code () == TYPE_CODE_STRUCT
163 && type
->name () != NULL
164 && type
->num_fields () == 2)
166 /* The order of fields doesn't matter. While it would be nice
167 to check for artificiality here, the Rust compiler doesn't
168 emit this information. */
169 const char *n1
= type
->field (0).name ();
170 const char *n2
= type
->field (1).name ();
171 return ((streq (n1
, "data_ptr") && streq (n2
, "length"))
172 || (streq (n2
, "data_ptr") && streq (n1
, "length")));
177 /* Return true if TYPE is a range type, otherwise false. */
180 rust_range_type_p (struct type
*type
)
184 if (type
->code () != TYPE_CODE_STRUCT
185 || type
->num_fields () > 2
186 || type
->name () == NULL
187 || strstr (type
->name (), "::Range") == NULL
)
190 if (type
->num_fields () == 0)
194 if (strcmp (type
->field (0).name (), "start") == 0)
196 if (type
->num_fields () == 1)
200 else if (type
->num_fields () == 2)
202 /* First field had to be "start". */
206 return strcmp (type
->field (i
).name (), "end") == 0;
209 /* Return true if TYPE is an inclusive range type, otherwise false.
210 This is only valid for types which are already known to be range
214 rust_inclusive_range_type_p (struct type
*type
)
216 return (strstr (type
->name (), "::RangeInclusive") != NULL
217 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
220 /* Return true if TYPE seems to be the type "u8", otherwise false. */
223 rust_u8_type_p (struct type
*type
)
225 return (type
->code () == TYPE_CODE_INT
226 && type
->is_unsigned ()
227 && type
->length () == 1);
230 /* Return true if TYPE is a Rust character type. */
233 rust_chartype_p (struct type
*type
)
235 return (type
->code () == TYPE_CODE_CHAR
236 && type
->length () == 4
237 && type
->is_unsigned ());
240 /* If VALUE represents a trait object pointer, return the underlying
241 pointer with the correct (i.e., runtime) type. Otherwise, return
244 static struct value
*
245 rust_get_trait_object_pointer (struct value
*value
)
247 struct type
*type
= check_typedef (value_type (value
));
249 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
252 /* Try to be a bit resilient if the ABI changes. */
253 int vtable_field
= 0;
254 for (int i
= 0; i
< 2; ++i
)
256 if (strcmp (type
->field (i
).name (), "vtable") == 0)
258 else if (strcmp (type
->field (i
).name (), "pointer") != 0)
262 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
263 struct symbol
*symbol
= find_symbol_at_address (vtable
);
264 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
267 struct rust_vtable_symbol
*vtable_sym
268 = static_cast<struct rust_vtable_symbol
*> (symbol
);
269 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
270 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
275 /* See language.h. */
278 rust_language::printstr (struct ui_file
*stream
, struct type
*type
,
279 const gdb_byte
*string
, unsigned int length
,
280 const char *user_encoding
, int force_ellipses
,
281 const struct value_print_options
*options
) const
283 /* Rust always uses UTF-8, but let the caller override this if need
285 const char *encoding
= user_encoding
;
286 if (user_encoding
== NULL
|| !*user_encoding
)
288 /* In Rust strings, characters are "u8". */
289 if (rust_u8_type_p (type
))
293 /* This is probably some C string, so let's let C deal with
295 c_printstr (stream
, type
, string
, length
, user_encoding
,
296 force_ellipses
, options
);
301 /* This is not ideal as it doesn't use our character printer. */
302 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
308 static const struct generic_val_print_decorations rust_decorations
=
310 /* Complex isn't used in Rust, but we provide C-ish values just in
322 /* Helper function to print a slice. */
325 rust_val_print_slice (struct value
*val
, struct ui_file
*stream
, int recurse
,
326 const struct value_print_options
*options
)
328 struct value
*base
= value_struct_elt (&val
, {}, "data_ptr", NULL
,
330 struct value
*len
= value_struct_elt (&val
, {}, "length", NULL
, "slice");
332 struct type
*type
= check_typedef (value_type (val
));
333 if (strcmp (type
->name (), "&str") == 0)
334 val_print_string (value_type (base
)->target_type (), "UTF-8",
335 value_as_address (base
), value_as_long (len
), stream
,
339 LONGEST llen
= value_as_long (len
);
341 type_print (value_type (val
), "", stream
, -1);
342 gdb_printf (stream
, " ");
345 gdb_printf (stream
, "[]");
348 struct type
*elt_type
= value_type (base
)->target_type ();
349 struct type
*array_type
= lookup_array_range_type (elt_type
, 0,
351 struct value
*array
= allocate_value_lazy (array_type
);
352 VALUE_LVAL (array
) = lval_memory
;
353 set_value_address (array
, value_as_address (base
));
354 value_fetch_lazy (array
);
355 generic_value_print (array
, stream
, recurse
, options
,
361 /* See rust-lang.h. */
364 rust_language::val_print_struct
365 (struct value
*val
, struct ui_file
*stream
, int recurse
,
366 const struct value_print_options
*options
) const
370 struct type
*type
= check_typedef (value_type (val
));
372 if (rust_slice_type_p (type
))
374 rust_val_print_slice (val
, stream
, recurse
, options
);
378 bool is_tuple
= rust_tuple_type_p (type
);
379 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
380 struct value_print_options opts
;
384 if (type
->name () != NULL
)
385 gdb_printf (stream
, "%s", type
->name ());
387 if (type
->num_fields () == 0)
390 if (type
->name () != NULL
)
391 gdb_puts (" ", stream
);
394 if (is_tuple
|| is_tuple_struct
)
395 gdb_puts ("(", stream
);
397 gdb_puts ("{", stream
);
403 for (i
= 0; i
< type
->num_fields (); ++i
)
405 if (field_is_static (&type
->field (i
)))
409 gdb_puts (",", stream
);
411 if (options
->prettyformat
)
413 gdb_puts ("\n", stream
);
414 print_spaces (2 + 2 * recurse
, stream
);
416 else if (!first_field
)
417 gdb_puts (" ", stream
);
421 if (!is_tuple
&& !is_tuple_struct
)
423 fputs_styled (type
->field (i
).name (),
424 variable_name_style
.style (), stream
);
425 gdb_puts (": ", stream
);
428 common_val_print (value_field (val
, i
), stream
, recurse
+ 1, &opts
,
432 if (options
->prettyformat
)
434 gdb_puts ("\n", stream
);
435 print_spaces (2 * recurse
, stream
);
438 if (is_tuple
|| is_tuple_struct
)
439 gdb_puts (")", stream
);
441 gdb_puts ("}", stream
);
444 /* See rust-lang.h. */
447 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
449 const struct value_print_options
*options
) const
451 struct value_print_options opts
= *options
;
452 struct type
*type
= check_typedef (value_type (val
));
456 gdb_assert (rust_enum_p (type
));
457 gdb::array_view
<const gdb_byte
> view
458 (value_contents_for_printing (val
).data (),
459 value_type (val
)->length ());
460 type
= resolve_dynamic_type (type
, view
, value_address (val
));
462 if (rust_empty_enum_p (type
))
464 /* Print the enum type name here to be more clear. */
465 gdb_printf (stream
, _("%s {%p[<No data fields>%p]}"),
467 metadata_style
.style ().ptr (), nullptr);
471 int variant_fieldno
= rust_enum_variant (type
);
472 val
= value_field (val
, variant_fieldno
);
473 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
475 int nfields
= variant_type
->num_fields ();
477 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
479 gdb_printf (stream
, "%s", variant_type
->name ());
482 /* In case of a nullary variant like 'None', just output
487 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
489 gdb_printf (stream
, "(");
492 /* struct variant. */
493 gdb_printf (stream
, "{");
496 bool first_field
= true;
497 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
500 gdb_puts (", ", stream
);
504 gdb_printf (stream
, "%ps: ",
505 styled_string (variable_name_style
.style (),
506 variant_type
->field (j
).name ()));
508 common_val_print (value_field (val
, j
), stream
, recurse
+ 1, &opts
,
513 gdb_puts (")", stream
);
515 gdb_puts ("}", stream
);
518 /* See language.h. */
521 rust_language::value_print_inner
522 (struct value
*val
, struct ui_file
*stream
, int recurse
,
523 const struct value_print_options
*options
) const
525 struct value_print_options opts
= *options
;
528 if (opts
.prettyformat
== Val_prettyformat_default
)
529 opts
.prettyformat
= (opts
.prettyformat_structs
530 ? Val_prettyformat
: Val_no_prettyformat
);
532 struct type
*type
= check_typedef (value_type (val
));
533 switch (type
->code ())
537 LONGEST low_bound
, high_bound
;
539 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
540 && rust_u8_type_p (type
->target_type ()->target_type ())
541 && get_array_bounds (type
->target_type (), &low_bound
,
544 /* We have a pointer to a byte string, so just print
546 struct type
*elttype
= check_typedef (type
->target_type ());
547 CORE_ADDR addr
= value_as_address (val
);
548 struct gdbarch
*arch
= type
->arch ();
550 if (opts
.addressprint
)
552 gdb_puts (paddress (arch
, addr
), stream
);
553 gdb_puts (" ", stream
);
556 gdb_puts ("b", stream
);
557 val_print_string (elttype
->target_type (), "ASCII", addr
,
558 high_bound
- low_bound
+ 1, stream
,
566 /* Recognize the unit type. */
567 if (type
->is_unsigned () && type
->length () == 0
568 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
570 gdb_puts ("()", stream
);
575 case TYPE_CODE_STRING
:
577 LONGEST low_bound
, high_bound
;
579 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
580 error (_("Could not determine the array bounds"));
582 /* If we see a plain TYPE_CODE_STRING, then we're printing a
583 byte string, hence the choice of "ASCII" as the
585 gdb_puts ("b", stream
);
586 printstr (stream
, type
->target_type (),
587 value_contents_for_printing (val
).data (),
588 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
592 case TYPE_CODE_ARRAY
:
594 LONGEST low_bound
, high_bound
;
596 if (get_array_bounds (type
, &low_bound
, &high_bound
)
597 && high_bound
- low_bound
+ 1 == 0)
598 gdb_puts ("[]", stream
);
604 case TYPE_CODE_UNION
:
605 /* Untagged unions are printed as if they are structs. Since
606 the field bit positions overlap in the debuginfo, the code
607 for printing a union is same as that for a struct, the only
608 difference is that the input type will have overlapping
610 val_print_struct (val
, stream
, recurse
, &opts
);
613 case TYPE_CODE_STRUCT
:
614 if (rust_enum_p (type
))
615 print_enum (val
, stream
, recurse
, &opts
);
617 val_print_struct (val
, stream
, recurse
, &opts
);
622 /* Nothing special yet. */
623 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
627 /* See language.h. */
630 rust_language::value_print
631 (struct value
*val
, struct ui_file
*stream
,
632 const struct value_print_options
*options
) const
634 value_print_options opts
= *options
;
635 opts
.deref_ref
= true;
637 struct type
*type
= check_typedef (value_type (val
));
638 if (type
->is_pointer_or_reference ())
640 gdb_printf (stream
, "(");
641 type_print (value_type (val
), "", stream
, -1);
642 gdb_printf (stream
, ") ");
645 return common_val_print (val
, stream
, 0, &opts
, this);
651 rust_internal_print_type (struct type
*type
, const char *varstring
,
652 struct ui_file
*stream
, int show
, int level
,
653 const struct type_print_options
*flags
,
654 bool for_rust_enum
, print_offset_data
*podata
);
656 /* Print a struct or union typedef. */
658 rust_print_struct_def (struct type
*type
, const char *varstring
,
659 struct ui_file
*stream
, int show
, int level
,
660 const struct type_print_options
*flags
,
661 bool for_rust_enum
, print_offset_data
*podata
)
663 /* Print a tuple type simply. */
664 if (rust_tuple_type_p (type
))
666 gdb_puts (type
->name (), stream
);
670 /* If we see a base class, delegate to C. */
671 if (TYPE_N_BASECLASSES (type
) > 0)
672 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
, flags
);
674 if (flags
->print_offsets
)
676 /* Temporarily bump the level so that the output lines up
681 /* Compute properties of TYPE here because, in the enum case, the
682 rest of the code ends up looking only at the variant part. */
683 const char *tagname
= type
->name ();
684 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
685 bool is_tuple
= rust_tuple_type_p (type
);
686 bool is_enum
= rust_enum_p (type
);
690 /* Already printing an outer enum, so nothing to print here. */
694 /* This code path is also used by unions and enums. */
697 gdb_puts ("enum ", stream
);
698 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
699 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
700 type
= prop
->original_type ();
702 else if (type
->code () == TYPE_CODE_STRUCT
)
703 gdb_puts ("struct ", stream
);
705 gdb_puts ("union ", stream
);
708 gdb_puts (tagname
, stream
);
711 if (type
->num_fields () == 0 && !is_tuple
)
713 if (for_rust_enum
&& !flags
->print_offsets
)
714 gdb_puts (is_tuple_struct
? "(" : "{", stream
);
716 gdb_puts (is_tuple_struct
? " (\n" : " {\n", stream
);
718 /* When printing offsets, we rearrange the fields into storage
719 order. This lets us show holes more clearly. We work using
720 field indices here because it simplifies calls to
721 print_offset_data::update below. */
722 std::vector
<int> fields
;
723 for (int i
= 0; i
< type
->num_fields (); ++i
)
725 if (field_is_static (&type
->field (i
)))
727 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
729 fields
.push_back (i
);
731 if (flags
->print_offsets
)
732 std::sort (fields
.begin (), fields
.end (),
735 return (type
->field (a
).loc_bitpos ()
736 < type
->field (b
).loc_bitpos ());
743 gdb_assert (!field_is_static (&type
->field (i
)));
744 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
746 if (flags
->print_offsets
)
747 podata
->update (type
, i
, stream
);
749 /* We'd like to print "pub" here as needed, but rustc
750 doesn't emit the debuginfo, and our types don't have
751 cplus_struct_type attached. */
753 /* For a tuple struct we print the type but nothing
755 if (!for_rust_enum
|| flags
->print_offsets
)
756 print_spaces (level
+ 2, stream
);
758 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
760 else if (!is_tuple_struct
)
761 gdb_printf (stream
, "%ps: ",
762 styled_string (variable_name_style
.style (),
763 type
->field (i
).name ()));
765 rust_internal_print_type (type
->field (i
).type (), NULL
,
766 stream
, (is_enum
? show
: show
- 1),
767 level
+ 2, flags
, is_enum
, podata
);
768 if (!for_rust_enum
|| flags
->print_offsets
)
769 gdb_puts (",\n", stream
);
770 /* Note that this check of "I" is ok because we only sorted the
771 fields by offset when print_offsets was set, so we won't take
772 this branch in that case. */
773 else if (i
+ 1 < type
->num_fields ())
774 gdb_puts (", ", stream
);
777 if (flags
->print_offsets
)
779 /* Undo the temporary level increase we did above. */
781 podata
->finish (type
, level
, stream
);
782 print_spaces (print_offset_data::indentation
, stream
);
784 print_spaces (2, stream
);
786 if (!for_rust_enum
|| flags
->print_offsets
)
787 print_spaces (level
, stream
);
788 gdb_puts (is_tuple_struct
? ")" : "}", 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 gdb_puts ("()", stream
);
808 gdb_puts (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 gdb_puts ("()", stream
);
824 /* Delegate varargs to the C printer. */
825 if (type
->has_varargs ())
828 gdb_puts ("fn ", stream
);
829 if (varstring
!= NULL
)
830 gdb_puts (varstring
, stream
);
831 gdb_puts ("(", stream
);
832 for (int i
= 0; i
< type
->num_fields (); ++i
)
836 gdb_puts (", ", stream
);
837 rust_internal_print_type (type
->field (i
).type (), "", stream
,
838 -1, 0, flags
, false, podata
);
840 gdb_puts (")", stream
);
841 /* If it returns unit, we can omit the return type. */
842 if (type
->target_type ()->code () != TYPE_CODE_VOID
)
844 gdb_puts (" -> ", stream
);
845 rust_internal_print_type (type
->target_type (), "", stream
,
846 -1, 0, flags
, false, podata
);
850 case TYPE_CODE_ARRAY
:
852 LONGEST low_bound
, high_bound
;
854 gdb_puts ("[", stream
);
855 rust_internal_print_type (type
->target_type (), NULL
,
856 stream
, show
- 1, level
, flags
, false,
859 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
860 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
861 gdb_printf (stream
, "; variable length");
862 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
863 gdb_printf (stream
, "; %s",
864 plongest (high_bound
- low_bound
+ 1));
865 gdb_puts ("]", 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 gdb_puts ("enum ", stream
);
880 if (type
->name () != NULL
)
882 gdb_puts (type
->name (), stream
);
883 gdb_puts (" ", stream
);
884 len
= strlen (type
->name ());
886 gdb_puts ("{\n", stream
);
888 for (int i
= 0; i
< type
->num_fields (); ++i
)
890 const char *name
= type
->field (i
).name ();
895 && strncmp (name
, type
->name (), len
) == 0
897 && name
[len
+ 1] == ':')
899 gdb_printf (stream
, "%*s%ps,\n",
901 styled_string (variable_name_style
.style (),
905 gdb_puts ("}", stream
);
911 if (type
->name () != nullptr)
912 gdb_puts (type
->name (), stream
);
915 /* We currently can't distinguish between pointers and
917 gdb_puts ("*mut ", stream
);
918 type_print (type
->target_type (), "", stream
, 0);
925 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
,
932 /* Like arch_composite_type, but uses TYPE to decide how to allocate
933 -- either on an obstack or on a gdbarch. */
936 rust_composite_type (struct type
*original
,
938 const char *field1
, struct type
*type1
,
939 const char *field2
, struct type
*type2
)
941 struct type
*result
= alloc_type_copy (original
);
942 int i
, nfields
, bitpos
;
950 result
->set_code (TYPE_CODE_STRUCT
);
951 result
->set_name (name
);
953 result
->set_num_fields (nfields
);
955 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
961 struct field
*field
= &result
->field (i
);
963 field
->set_loc_bitpos (bitpos
);
964 bitpos
+= type1
->length () * TARGET_CHAR_BIT
;
966 field
->set_name (field1
);
967 field
->set_type (type1
);
972 struct field
*field
= &result
->field (i
);
973 unsigned align
= type_align (type2
);
979 align
*= TARGET_CHAR_BIT
;
980 delta
= bitpos
% align
;
982 bitpos
+= align
- delta
;
984 field
->set_loc_bitpos (bitpos
);
986 field
->set_name (field2
);
987 field
->set_type (type2
);
992 result
->set_length (result
->field (i
- 1).loc_bitpos () / TARGET_CHAR_BIT
993 + result
->field (i
- 1).type ()->length ());
997 /* See rust-lang.h. */
1000 rust_slice_type (const char *name
, struct type
*elt_type
,
1001 struct type
*usize_type
)
1005 elt_type
= lookup_pointer_type (elt_type
);
1006 type
= rust_composite_type (elt_type
, name
,
1007 "data_ptr", elt_type
,
1008 "length", usize_type
);
1015 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1018 rust_range (struct type
*expect_type
, struct expression
*exp
,
1019 enum noside noside
, enum range_flag kind
,
1020 struct value
*low
, struct value
*high
)
1022 struct value
*addrval
, *result
;
1024 struct type
*range_type
;
1025 struct type
*index_type
;
1026 struct type
*temp_type
;
1029 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1036 name
= "std::ops::RangeFull";
1040 index_type
= value_type (high
);
1042 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1049 index_type
= value_type (low
);
1050 name
= "std::ops::RangeFrom";
1054 if (!types_equal (value_type (low
), value_type (high
)))
1055 error (_("Range expression with different types"));
1056 index_type
= value_type (low
);
1057 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1061 /* If we don't have an index type, just allocate this on the
1062 arch. Here any type will do. */
1063 temp_type
= (index_type
== NULL
1064 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1066 /* It would be nicer to cache the range type. */
1067 range_type
= rust_composite_type (temp_type
, name
,
1068 low
== NULL
? NULL
: "start", index_type
,
1069 high
== NULL
? NULL
: "end", index_type
);
1071 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1072 return value_zero (range_type
, lval_memory
);
1074 addrval
= value_allocate_space_in_inferior (range_type
->length ());
1075 addr
= value_as_long (addrval
);
1076 result
= value_at_lazy (range_type
, addr
);
1080 struct value
*start
= value_struct_elt (&result
, {}, "start", NULL
,
1083 value_assign (start
, low
);
1088 struct value
*end
= value_struct_elt (&result
, {}, "end", NULL
,
1091 value_assign (end
, high
);
1094 result
= value_at_lazy (range_type
, addr
);
1098 /* A helper function to compute the range and kind given a range
1099 value. TYPE is the type of the range value. RANGE is the range
1100 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1101 parameters might be filled in, or might not be, depending on the
1102 kind of range this is. KIND will always be set to the appropriate
1103 value describing the kind of range, and this can be used to
1104 determine whether LOW or HIGH are valid. */
1107 rust_compute_range (struct type
*type
, struct value
*range
,
1108 LONGEST
*low
, LONGEST
*high
,
1115 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1117 if (type
->num_fields () == 0)
1121 if (strcmp (type
->field (0).name (), "start") == 0)
1123 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1124 *low
= value_as_long (value_field (range
, 0));
1127 if (type
->num_fields () > i
1128 && strcmp (type
->field (i
).name (), "end") == 0)
1130 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1131 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1132 *high
= value_as_long (value_field (range
, i
));
1134 if (rust_inclusive_range_type_p (type
))
1139 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1142 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1143 enum noside noside
, bool for_addr
,
1144 struct value
*lhs
, struct value
*rhs
)
1146 struct value
*result
;
1147 struct type
*rhstype
;
1148 LONGEST low
, high_bound
;
1149 /* Initialized to appease the compiler. */
1150 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1154 rhstype
= check_typedef (value_type (rhs
));
1155 if (rust_range_type_p (rhstype
))
1158 error (_("Can't take slice of array without '&'"));
1159 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1163 low
= value_as_long (rhs
);
1165 struct type
*type
= check_typedef (value_type (lhs
));
1166 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1168 struct type
*base_type
= nullptr;
1169 if (type
->code () == TYPE_CODE_ARRAY
)
1170 base_type
= type
->target_type ();
1171 else if (rust_slice_type_p (type
))
1173 for (int i
= 0; i
< type
->num_fields (); ++i
)
1175 if (strcmp (type
->field (i
).name (), "data_ptr") == 0)
1177 base_type
= type
->field (i
).type ()->target_type ();
1181 if (base_type
== nullptr)
1182 error (_("Could not find 'data_ptr' in slice type"));
1184 else if (type
->code () == TYPE_CODE_PTR
)
1185 base_type
= type
->target_type ();
1187 error (_("Cannot subscript non-array type"));
1189 struct type
*new_type
;
1192 if (rust_slice_type_p (type
))
1197 = language_lookup_primitive_type (exp
->language_defn
,
1200 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1204 new_type
= base_type
;
1206 return value_zero (new_type
, VALUE_LVAL (lhs
));
1213 if (type
->code () == TYPE_CODE_ARRAY
)
1216 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1217 error (_("Can't compute array bounds"));
1219 error (_("Found array with non-zero lower bound"));
1222 else if (rust_slice_type_p (type
))
1226 base
= value_struct_elt (&lhs
, {}, "data_ptr", NULL
, "slice");
1227 len
= value_struct_elt (&lhs
, {}, "length", NULL
, "slice");
1229 high_bound
= value_as_long (len
);
1231 else if (type
->code () == TYPE_CODE_PTR
)
1235 high_bound
= LONGEST_MAX
;
1238 error (_("Cannot subscript non-array type"));
1240 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1243 error (_("Index less than zero"));
1244 if (low
> high_bound
)
1245 error (_("Index greater than length"));
1247 result
= value_subscript (base
, low
);
1254 struct type
*usize
, *slice
;
1256 struct value
*addrval
, *tem
;
1258 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1261 error (_("High index less than zero"));
1263 error (_("Low index greater than high index"));
1264 if (high
> high_bound
)
1265 error (_("High index greater than length"));
1267 usize
= language_lookup_primitive_type (exp
->language_defn
,
1270 const char *new_name
= ((type
!= nullptr
1271 && rust_slice_type_p (type
))
1272 ? type
->name () : "&[*gdb*]");
1274 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1276 addrval
= value_allocate_space_in_inferior (slice
->length ());
1277 addr
= value_as_long (addrval
);
1278 tem
= value_at_lazy (slice
, addr
);
1280 value_assign (value_field (tem
, 0), value_addr (result
));
1281 value_assign (value_field (tem
, 1),
1282 value_from_longest (usize
, high
- low
));
1284 result
= value_at_lazy (slice
, addr
);
1287 result
= value_addr (result
);
1297 rust_unop_ind_operation::evaluate (struct type
*expect_type
,
1298 struct expression
*exp
,
1301 if (noside
!= EVAL_NORMAL
)
1302 return unop_ind_operation::evaluate (expect_type
, exp
, noside
);
1304 struct value
*value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1306 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1307 if (trait_ptr
!= NULL
)
1310 return value_ind (value
);
1313 } /* namespace expr */
1315 /* A helper function for UNOP_COMPLEMENT. */
1318 eval_op_rust_complement (struct type
*expect_type
, struct expression
*exp
,
1320 enum exp_opcode opcode
,
1321 struct value
*value
)
1323 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1324 return value_from_longest (value_type (value
), value_logical_not (value
));
1325 return value_complement (value
);
1328 /* A helper function for OP_ARRAY. */
1331 eval_op_rust_array (struct type
*expect_type
, struct expression
*exp
,
1333 enum exp_opcode opcode
,
1334 struct value
*elt
, struct value
*ncopies
)
1336 int copies
= value_as_long (ncopies
);
1338 error (_("Array with negative number of elements"));
1340 if (noside
== EVAL_NORMAL
)
1343 std::vector
<struct value
*> eltvec (copies
);
1345 for (i
= 0; i
< copies
; ++i
)
1347 return value_array (0, copies
- 1, eltvec
.data ());
1351 struct type
*arraytype
1352 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1353 return allocate_value (arraytype
);
1361 rust_struct_anon::evaluate (struct type
*expect_type
,
1362 struct expression
*exp
,
1365 value
*lhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
1366 int field_number
= std::get
<0> (m_storage
);
1368 struct type
*type
= value_type (lhs
);
1370 if (type
->code () == TYPE_CODE_STRUCT
)
1372 struct type
*outer_type
= NULL
;
1374 if (rust_enum_p (type
))
1376 type
= resolve_dynamic_type (type
, value_contents (lhs
),
1377 value_address (lhs
));
1379 if (rust_empty_enum_p (type
))
1380 error (_("Cannot access field %d of empty enum %s"),
1381 field_number
, type
->name ());
1383 int fieldno
= rust_enum_variant (type
);
1384 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1386 type
= value_type (lhs
);
1389 /* Tuples and tuple structs */
1390 int nfields
= type
->num_fields ();
1392 if (field_number
>= nfields
|| field_number
< 0)
1394 if (outer_type
!= NULL
)
1395 error(_("Cannot access field %d of variant %s::%s, "
1396 "there are only %d fields"),
1397 field_number
, outer_type
->name (),
1398 rust_last_path_segment (type
->name ()),
1401 error(_("Cannot access field %d of %s, "
1402 "there are only %d fields"),
1403 field_number
, type
->name (), nfields
);
1406 /* Tuples are tuple structs too. */
1407 if (!rust_tuple_struct_type_p (type
))
1409 if (outer_type
!= NULL
)
1410 error(_("Variant %s::%s is not a tuple variant"),
1411 outer_type
->name (),
1412 rust_last_path_segment (type
->name ()));
1414 error(_("Attempting to access anonymous field %d "
1415 "of %s, which is not a tuple, tuple struct, or "
1416 "tuple-like variant"),
1417 field_number
, type
->name ());
1420 return value_primitive_field (lhs
, 0, field_number
, type
);
1423 error(_("Anonymous field access is only allowed on tuples, \
1424 tuple structs, and tuple-like enum variants"));
1428 rust_structop::evaluate (struct type
*expect_type
,
1429 struct expression
*exp
,
1432 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1433 const char *field_name
= std::get
<1> (m_storage
).c_str ();
1435 struct value
*result
;
1436 struct type
*type
= value_type (lhs
);
1437 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1439 type
= resolve_dynamic_type (type
, value_contents (lhs
),
1440 value_address (lhs
));
1442 if (rust_empty_enum_p (type
))
1443 error (_("Cannot access field %s of empty enum %s"),
1444 field_name
, type
->name ());
1446 int fieldno
= rust_enum_variant (type
);
1447 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1449 struct type
*outer_type
= type
;
1450 type
= value_type (lhs
);
1451 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1452 error (_("Attempting to access named field %s of tuple "
1453 "variant %s::%s, which has only anonymous fields"),
1454 field_name
, outer_type
->name (),
1455 rust_last_path_segment (type
->name ()));
1459 result
= value_struct_elt (&lhs
, {}, field_name
,
1462 catch (const gdb_exception_error
&except
)
1464 error (_("Could not find field %s of struct variant %s::%s"),
1465 field_name
, outer_type
->name (),
1466 rust_last_path_segment (type
->name ()));
1470 result
= value_struct_elt (&lhs
, {}, field_name
, NULL
, "structure");
1471 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1472 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1477 rust_aggregate_operation::evaluate (struct type
*expect_type
,
1478 struct expression
*exp
,
1481 struct type
*type
= std::get
<0> (m_storage
);
1483 struct value
*addrval
= NULL
;
1486 if (noside
== EVAL_NORMAL
)
1488 addrval
= value_allocate_space_in_inferior (type
->length ());
1489 addr
= value_as_long (addrval
);
1490 result
= value_at_lazy (type
, addr
);
1493 if (std::get
<1> (m_storage
) != nullptr)
1495 struct value
*init
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1498 if (noside
== EVAL_NORMAL
)
1500 /* This isn't quite right but will do for the time
1501 being, seeing that we can't implement the Copy
1503 value_assign (result
, init
);
1507 for (const auto &item
: std::get
<2> (m_storage
))
1509 value
*val
= item
.second
->evaluate (nullptr, exp
, noside
);
1510 if (noside
== EVAL_NORMAL
)
1512 const char *fieldname
= item
.first
.c_str ();
1513 value
*field
= value_struct_elt (&result
, {}, fieldname
,
1514 nullptr, "structure");
1515 value_assign (field
, val
);
1519 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1520 result
= allocate_value (type
);
1522 result
= value_at_lazy (type
, addr
);
1528 rust_structop::evaluate_funcall (struct type
*expect_type
,
1529 struct expression
*exp
,
1531 const std::vector
<operation_up
> &ops
)
1533 std::vector
<struct value
*> args (ops
.size () + 1);
1535 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1536 type in order to look up the method. */
1537 args
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1538 /* We don't yet implement real Deref semantics. */
1539 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1540 args
[0] = value_ind (args
[0]);
1542 struct type
*type
= value_type (args
[0]);
1543 if ((type
->code () != TYPE_CODE_STRUCT
1544 && type
->code () != TYPE_CODE_UNION
1545 && type
->code () != TYPE_CODE_ENUM
)
1546 || rust_tuple_type_p (type
))
1547 error (_("Method calls only supported on struct or enum types"));
1548 if (type
->name () == NULL
)
1549 error (_("Method call on nameless type"));
1551 std::string name
= (std::string (type
->name ()) + "::"
1552 + std::get
<1> (m_storage
));
1554 const struct block
*block
= get_selected_block (0);
1555 struct block_symbol sym
= lookup_symbol (name
.c_str (), block
,
1557 if (sym
.symbol
== NULL
)
1558 error (_("Could not find function named '%s'"), name
.c_str ());
1560 struct type
*fn_type
= sym
.symbol
->type ();
1561 if (fn_type
->num_fields () == 0)
1562 error (_("Function '%s' takes no arguments"), name
.c_str ());
1564 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1565 args
[0] = value_addr (args
[0]);
1567 value
*function
= address_of_variable (sym
.symbol
, block
);
1569 for (int i
= 0; i
< ops
.size (); ++i
)
1570 args
[i
+ 1] = ops
[i
]->evaluate (nullptr, exp
, noside
);
1572 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1573 return value_zero (fn_type
->target_type (), not_lval
);
1574 return call_function_by_hand (function
, NULL
, args
);
1581 /* See language.h. */
1584 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1585 struct language_arch_info
*lai
) const
1587 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1589 /* Helper function to allow shorter lines below. */
1590 auto add
= [&] (struct type
* t
) -> struct type
*
1592 lai
->add_primitive_type (t
);
1596 struct type
*bool_type
1597 = add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1598 add (arch_character_type (gdbarch
, 32, 1, "char"));
1599 add (arch_integer_type (gdbarch
, 8, 0, "i8"));
1600 struct type
*u8_type
1601 = add (arch_integer_type (gdbarch
, 8, 1, "u8"));
1602 add (arch_integer_type (gdbarch
, 16, 0, "i16"));
1603 add (arch_integer_type (gdbarch
, 16, 1, "u16"));
1604 add (arch_integer_type (gdbarch
, 32, 0, "i32"));
1605 add (arch_integer_type (gdbarch
, 32, 1, "u32"));
1606 add (arch_integer_type (gdbarch
, 64, 0, "i64"));
1607 add (arch_integer_type (gdbarch
, 64, 1, "u64"));
1609 unsigned int length
= 8 * builtin
->builtin_data_ptr
->length ();
1610 add (arch_integer_type (gdbarch
, length
, 0, "isize"));
1611 struct type
*usize_type
1612 = add (arch_integer_type (gdbarch
, length
, 1, "usize"));
1614 add (arch_float_type (gdbarch
, 32, "f32", floatformats_ieee_single
));
1615 add (arch_float_type (gdbarch
, 64, "f64", floatformats_ieee_double
));
1616 add (arch_integer_type (gdbarch
, 0, 1, "()"));
1618 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1619 add (rust_slice_type ("&str", tem
, usize_type
));
1621 lai
->set_bool_type (bool_type
);
1622 lai
->set_string_char_type (u8_type
);
1625 /* See language.h. */
1628 rust_language::print_type (struct type
*type
, const char *varstring
,
1629 struct ui_file
*stream
, int show
, int level
,
1630 const struct type_print_options
*flags
) const
1632 print_offset_data
podata (flags
);
1633 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1634 flags
, false, &podata
);
1637 /* See language.h. */
1640 rust_language::emitchar (int ch
, struct type
*chtype
,
1641 struct ui_file
*stream
, int quoter
) const
1643 if (!rust_chartype_p (chtype
))
1644 generic_emit_char (ch
, chtype
, stream
, quoter
,
1645 target_charset (chtype
->arch ()));
1646 else if (ch
== '\\' || ch
== quoter
)
1647 gdb_printf (stream
, "\\%c", ch
);
1648 else if (ch
== '\n')
1649 gdb_puts ("\\n", stream
);
1650 else if (ch
== '\r')
1651 gdb_puts ("\\r", stream
);
1652 else if (ch
== '\t')
1653 gdb_puts ("\\t", stream
);
1654 else if (ch
== '\0')
1655 gdb_puts ("\\0", stream
);
1656 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1657 gdb_putc (ch
, stream
);
1659 gdb_printf (stream
, "\\x%02x", ch
);
1661 gdb_printf (stream
, "\\u{%06x}", ch
);
1664 /* See language.h. */
1667 rust_language::is_string_type_p (struct type
*type
) const
1669 LONGEST low_bound
, high_bound
;
1671 type
= check_typedef (type
);
1672 return ((type
->code () == TYPE_CODE_STRING
)
1673 || (type
->code () == TYPE_CODE_PTR
1674 && (type
->target_type ()->code () == TYPE_CODE_ARRAY
1675 && rust_u8_type_p (type
->target_type ()->target_type ())
1676 && get_array_bounds (type
->target_type (), &low_bound
,
1678 || (type
->code () == TYPE_CODE_STRUCT
1679 && !rust_enum_p (type
)
1680 && rust_slice_type_p (type
)
1681 && strcmp (type
->name (), "&str") == 0));
1684 /* Single instance of the Rust language class. */
1686 static rust_language rust_language_defn
;