1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2023 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"
32 #include "rust-lang.h"
33 #include "typeprint.h"
39 #include "cli/cli-style.h"
40 #include "parser-defs.h"
43 /* See rust-lang.h. */
46 rust_last_path_segment (const char *path
)
48 const char *result
= strrchr (path
, ':');
55 /* See rust-lang.h. */
58 rust_crate_for_block (const struct block
*block
)
60 const char *scope
= block
->scope ();
63 return std::string ();
65 return std::string (scope
, cp_find_first_component (scope
));
68 /* Return true if TYPE, which must be a struct type, represents a Rust
72 rust_enum_p (struct type
*type
)
74 /* is_dynamic_type will return true if any field has a dynamic
75 attribute -- but we only want to check the top level. */
76 return TYPE_HAS_VARIANT_PARTS (type
);
79 /* Return true if TYPE, which must be an already-resolved enum type,
83 rust_empty_enum_p (const struct type
*type
)
85 return type
->num_fields () == 0;
88 /* Given an already-resolved enum type and contents, find which
92 rust_enum_variant (struct type
*type
)
94 /* The active variant is simply the first non-artificial field. */
95 for (int i
= 0; i
< type
->num_fields (); ++i
)
96 if (!type
->field (i
).is_artificial ())
99 /* Perhaps we could get here by trying to print an Ada variant
100 record in Rust mode. Unlikely, but an error is safer than an
102 error (_("Could not find active enum variant"));
105 /* See rust-lang.h. */
108 rust_tuple_type_p (struct type
*type
)
110 /* The current implementation is a bit of a hack, but there's
111 nothing else in the debuginfo to distinguish a tuple from a
113 return (type
->code () == TYPE_CODE_STRUCT
114 && type
->name () != NULL
115 && type
->name ()[0] == '(');
118 /* Return true if all non-static fields of a structlike type are in a
119 sequence like __0, __1, __2. */
122 rust_underscore_fields (struct type
*type
)
128 if (type
->code () != TYPE_CODE_STRUCT
)
130 for (i
= 0; i
< type
->num_fields (); ++i
)
132 if (!type
->field (i
).is_static ())
136 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
137 if (strcmp (buf
, type
->field (i
).name ()) != 0)
145 /* See rust-lang.h. */
148 rust_tuple_struct_type_p (struct type
*type
)
150 /* This is just an approximation until DWARF can represent Rust more
151 precisely. We exclude zero-length structs because they may not
152 be tuple structs, and there's no way to tell. */
153 return type
->num_fields () > 0 && rust_underscore_fields (type
);
156 /* See rust-lang.h. */
159 rust_slice_type_p (const struct type
*type
)
161 if (type
->code () == TYPE_CODE_STRUCT
162 && type
->name () != NULL
163 && type
->num_fields () == 2)
165 /* The order of fields doesn't matter. While it would be nice
166 to check for artificiality here, the Rust compiler doesn't
167 emit this information. */
168 const char *n1
= type
->field (0).name ();
169 const char *n2
= type
->field (1).name ();
170 return ((streq (n1
, "data_ptr") && streq (n2
, "length"))
171 || (streq (n2
, "data_ptr") && streq (n1
, "length")));
176 /* Return true if TYPE is a range type, otherwise false. */
179 rust_range_type_p (struct type
*type
)
183 if (type
->code () != TYPE_CODE_STRUCT
184 || type
->num_fields () > 2
185 || type
->name () == NULL
186 || strstr (type
->name (), "::Range") == NULL
)
189 if (type
->num_fields () == 0)
193 if (strcmp (type
->field (0).name (), "start") == 0)
195 if (type
->num_fields () == 1)
199 else if (type
->num_fields () == 2)
201 /* First field had to be "start". */
205 return strcmp (type
->field (i
).name (), "end") == 0;
208 /* Return true if TYPE is an inclusive range type, otherwise false.
209 This is only valid for types which are already known to be range
213 rust_inclusive_range_type_p (struct type
*type
)
215 return (strstr (type
->name (), "::RangeInclusive") != NULL
216 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
219 /* Return true if TYPE seems to be the type "u8", otherwise false. */
222 rust_u8_type_p (struct type
*type
)
224 return (type
->code () == TYPE_CODE_INT
225 && type
->is_unsigned ()
226 && type
->length () == 1);
229 /* Return true if TYPE is a Rust character type. */
232 rust_chartype_p (struct type
*type
)
234 return (type
->code () == TYPE_CODE_CHAR
235 && type
->length () == 4
236 && type
->is_unsigned ());
239 /* If VALUE represents a trait object pointer, return the underlying
240 pointer with the correct (i.e., runtime) type. Otherwise, return
243 static struct value
*
244 rust_get_trait_object_pointer (struct value
*value
)
246 struct type
*type
= check_typedef (value
->type ());
248 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
251 /* Try to be a bit resilient if the ABI changes. */
252 int vtable_field
= 0;
253 for (int i
= 0; i
< 2; ++i
)
255 if (strcmp (type
->field (i
).name (), "vtable") == 0)
257 else if (strcmp (type
->field (i
).name (), "pointer") != 0)
261 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
262 struct symbol
*symbol
= find_symbol_at_address (vtable
);
263 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
266 struct rust_vtable_symbol
*vtable_sym
267 = static_cast<struct rust_vtable_symbol
*> (symbol
);
268 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
269 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
274 /* See language.h. */
277 rust_language::printstr (struct ui_file
*stream
, struct type
*type
,
278 const gdb_byte
*string
, unsigned int length
,
279 const char *user_encoding
, int force_ellipses
,
280 const struct value_print_options
*options
) const
282 /* Rust always uses UTF-8, but let the caller override this if need
284 const char *encoding
= user_encoding
;
285 if (user_encoding
== NULL
|| !*user_encoding
)
287 /* In Rust strings, characters are "u8". */
288 if (rust_u8_type_p (type
))
292 /* This is probably some C string, so let's let C deal with
294 language_defn::printstr (stream
, type
, string
, length
,
295 user_encoding
, force_ellipses
,
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 /* See rust-lang.h. */
325 rust_slice_to_array (struct value
*val
)
327 struct type
*type
= check_typedef (val
->type ());
328 /* This must have been checked by the caller. */
329 gdb_assert (rust_slice_type_p (type
));
331 struct value
*base
= value_struct_elt (&val
, {}, "data_ptr", NULL
,
333 struct value
*len
= value_struct_elt (&val
, {}, "length", NULL
, "slice");
334 LONGEST llen
= value_as_long (len
);
336 struct type
*elt_type
= base
->type ()->target_type ();
337 struct type
*array_type
= lookup_array_range_type (elt_type
, 0,
339 struct value
*array
= value::allocate_lazy (array_type
);
340 array
->set_lval (lval_memory
);
341 array
->set_address (value_as_address (base
));
346 /* Helper function to print a slice. */
349 rust_val_print_slice (struct value
*val
, struct ui_file
*stream
, int recurse
,
350 const struct value_print_options
*options
)
352 struct value
*base
= value_struct_elt (&val
, {}, "data_ptr", NULL
,
354 struct value
*len
= value_struct_elt (&val
, {}, "length", NULL
, "slice");
356 struct type
*type
= check_typedef (val
->type ());
357 if (strcmp (type
->name (), "&str") == 0)
358 val_print_string (base
->type ()->target_type (), "UTF-8",
359 value_as_address (base
), value_as_long (len
), stream
,
363 LONGEST llen
= value_as_long (len
);
365 type_print (val
->type (), "", stream
, -1);
366 gdb_printf (stream
, " ");
369 gdb_printf (stream
, "[]");
372 struct value
*array
= rust_slice_to_array (val
);
373 array
->fetch_lazy ();
374 generic_value_print (array
, stream
, recurse
, options
,
380 /* See rust-lang.h. */
383 rust_language::val_print_struct
384 (struct value
*val
, struct ui_file
*stream
, int recurse
,
385 const struct value_print_options
*options
) const
389 struct type
*type
= check_typedef (val
->type ());
391 if (rust_slice_type_p (type
))
393 rust_val_print_slice (val
, stream
, recurse
, options
);
397 bool is_tuple
= rust_tuple_type_p (type
);
398 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
399 struct value_print_options opts
;
403 if (type
->name () != NULL
)
404 gdb_printf (stream
, "%s", type
->name ());
406 if (type
->num_fields () == 0)
409 if (type
->name () != NULL
)
410 gdb_puts (" ", stream
);
413 if (is_tuple
|| is_tuple_struct
)
414 gdb_puts ("(", stream
);
416 gdb_puts ("{", stream
);
419 opts
.deref_ref
= false;
422 for (i
= 0; i
< type
->num_fields (); ++i
)
424 if (type
->field (i
).is_static ())
428 gdb_puts (",", stream
);
430 if (options
->prettyformat
)
432 gdb_puts ("\n", stream
);
433 print_spaces (2 + 2 * recurse
, stream
);
435 else if (!first_field
)
436 gdb_puts (" ", stream
);
440 if (!is_tuple
&& !is_tuple_struct
)
442 fputs_styled (type
->field (i
).name (),
443 variable_name_style
.style (), stream
);
444 gdb_puts (": ", stream
);
447 common_val_print (value_field (val
, i
), stream
, recurse
+ 1, &opts
,
451 if (options
->prettyformat
)
453 gdb_puts ("\n", stream
);
454 print_spaces (2 * recurse
, stream
);
457 if (is_tuple
|| is_tuple_struct
)
458 gdb_puts (")", stream
);
460 gdb_puts ("}", stream
);
463 /* See rust-lang.h. */
466 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
468 const struct value_print_options
*options
) const
470 struct value_print_options opts
= *options
;
471 struct type
*type
= check_typedef (val
->type ());
473 opts
.deref_ref
= false;
475 gdb_assert (rust_enum_p (type
));
476 gdb::array_view
<const gdb_byte
> view
477 (val
->contents_for_printing ().data (),
478 val
->type ()->length ());
479 type
= resolve_dynamic_type (type
, view
, val
->address ());
481 if (rust_empty_enum_p (type
))
483 /* Print the enum type name here to be more clear. */
484 gdb_printf (stream
, _("%s {%p[<No data fields>%p]}"),
486 metadata_style
.style ().ptr (), nullptr);
490 int variant_fieldno
= rust_enum_variant (type
);
491 val
= val
->primitive_field (0, variant_fieldno
, type
);
492 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
494 int nfields
= variant_type
->num_fields ();
496 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
498 gdb_printf (stream
, "%s", variant_type
->name ());
501 /* In case of a nullary variant like 'None', just output
506 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
508 gdb_printf (stream
, "(");
511 /* struct variant. */
512 gdb_printf (stream
, "{");
515 bool first_field
= true;
516 for (int j
= 0; j
< nfields
; j
++)
519 gdb_puts (", ", stream
);
523 gdb_printf (stream
, "%ps: ",
524 styled_string (variable_name_style
.style (),
525 variant_type
->field (j
).name ()));
527 common_val_print (value_field (val
, j
), stream
, recurse
+ 1, &opts
,
532 gdb_puts (")", stream
);
534 gdb_puts ("}", stream
);
537 /* See language.h. */
540 rust_language::value_print_inner
541 (struct value
*val
, struct ui_file
*stream
, int recurse
,
542 const struct value_print_options
*options
) const
544 struct value_print_options opts
= *options
;
545 opts
.deref_ref
= true;
547 if (opts
.prettyformat
== Val_prettyformat_default
)
548 opts
.prettyformat
= (opts
.prettyformat_structs
549 ? Val_prettyformat
: Val_no_prettyformat
);
551 struct type
*type
= check_typedef (val
->type ());
552 switch (type
->code ())
556 LONGEST low_bound
, high_bound
;
558 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
559 && rust_u8_type_p (type
->target_type ()->target_type ())
560 && get_array_bounds (type
->target_type (), &low_bound
,
563 /* We have a pointer to a byte string, so just print
565 struct type
*elttype
= check_typedef (type
->target_type ());
566 CORE_ADDR addr
= value_as_address (val
);
567 struct gdbarch
*arch
= type
->arch ();
569 if (opts
.addressprint
)
571 gdb_puts (paddress (arch
, addr
), stream
);
572 gdb_puts (" ", stream
);
575 gdb_puts ("b", stream
);
576 val_print_string (elttype
->target_type (), "ASCII", addr
,
577 high_bound
- low_bound
+ 1, stream
,
585 /* Recognize the unit type. */
586 if (type
->is_unsigned () && type
->length () == 0
587 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
589 gdb_puts ("()", stream
);
594 case TYPE_CODE_STRING
:
596 LONGEST low_bound
, high_bound
;
598 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
599 error (_("Could not determine the array bounds"));
601 /* If we see a plain TYPE_CODE_STRING, then we're printing a
602 byte string, hence the choice of "ASCII" as the
604 gdb_puts ("b", stream
);
605 printstr (stream
, type
->target_type (),
606 val
->contents_for_printing ().data (),
607 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
611 case TYPE_CODE_ARRAY
:
613 LONGEST low_bound
, high_bound
;
615 if (get_array_bounds (type
, &low_bound
, &high_bound
)
616 && high_bound
- low_bound
+ 1 == 0)
617 gdb_puts ("[]", stream
);
623 case TYPE_CODE_UNION
:
624 /* Untagged unions are printed as if they are structs. Since
625 the field bit positions overlap in the debuginfo, the code
626 for printing a union is same as that for a struct, the only
627 difference is that the input type will have overlapping
629 val_print_struct (val
, stream
, recurse
, &opts
);
632 case TYPE_CODE_STRUCT
:
633 if (rust_enum_p (type
))
634 print_enum (val
, stream
, recurse
, &opts
);
636 val_print_struct (val
, stream
, recurse
, &opts
);
641 /* Nothing special yet. */
642 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
646 /* See language.h. */
649 rust_language::value_print
650 (struct value
*val
, struct ui_file
*stream
,
651 const struct value_print_options
*options
) const
653 value_print_options opts
= *options
;
654 opts
.deref_ref
= true;
656 struct type
*type
= check_typedef (val
->type ());
657 if (type
->is_pointer_or_reference ())
659 gdb_printf (stream
, "(");
660 type_print (val
->type (), "", stream
, -1);
661 gdb_printf (stream
, ") ");
664 return common_val_print (val
, stream
, 0, &opts
, this);
670 rust_internal_print_type (struct type
*type
, const char *varstring
,
671 struct ui_file
*stream
, int show
, int level
,
672 const struct type_print_options
*flags
,
673 bool for_rust_enum
, print_offset_data
*podata
);
675 /* Print a struct or union typedef. */
677 rust_print_struct_def (struct type
*type
, const char *varstring
,
678 struct ui_file
*stream
, int show
, int level
,
679 const struct type_print_options
*flags
,
680 bool for_rust_enum
, print_offset_data
*podata
)
682 /* Print a tuple type simply. */
683 if (rust_tuple_type_p (type
))
685 gdb_puts (type
->name (), stream
);
689 /* If we see a base class, delegate to C. */
690 if (TYPE_N_BASECLASSES (type
) > 0)
691 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
, flags
);
693 if (flags
->print_offsets
)
695 /* Temporarily bump the level so that the output lines up
700 /* Compute properties of TYPE here because, in the enum case, the
701 rest of the code ends up looking only at the variant part. */
702 const char *tagname
= type
->name ();
703 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
704 bool is_tuple
= rust_tuple_type_p (type
);
705 bool is_enum
= rust_enum_p (type
);
709 /* Already printing an outer enum, so nothing to print here. */
713 /* This code path is also used by unions and enums. */
716 gdb_puts ("enum ", stream
);
717 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
718 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
719 type
= prop
->original_type ();
721 else if (type
->code () == TYPE_CODE_STRUCT
)
722 gdb_puts ("struct ", stream
);
724 gdb_puts ("union ", stream
);
727 gdb_puts (tagname
, stream
);
730 if (type
->num_fields () == 0 && !is_tuple
)
732 if (for_rust_enum
&& !flags
->print_offsets
)
733 gdb_puts (is_tuple_struct
? "(" : "{", stream
);
735 gdb_puts (is_tuple_struct
? " (\n" : " {\n", stream
);
737 /* When printing offsets, we rearrange the fields into storage
738 order. This lets us show holes more clearly. We work using
739 field indices here because it simplifies calls to
740 print_offset_data::update below. */
741 std::vector
<int> fields
;
742 for (int i
= 0; i
< type
->num_fields (); ++i
)
744 if (type
->field (i
).is_static ())
746 if (is_enum
&& type
->field (i
).is_artificial ())
748 fields
.push_back (i
);
750 if (flags
->print_offsets
)
751 std::sort (fields
.begin (), fields
.end (),
754 return (type
->field (a
).loc_bitpos ()
755 < type
->field (b
).loc_bitpos ());
762 gdb_assert (!type
->field (i
).is_static ());
763 gdb_assert (! (is_enum
&& type
->field (i
).is_artificial ()));
765 if (flags
->print_offsets
)
766 podata
->update (type
, i
, stream
);
768 /* We'd like to print "pub" here as needed, but rustc
769 doesn't emit the debuginfo, and our types don't have
770 cplus_struct_type attached. */
772 /* For a tuple struct we print the type but nothing
774 if (!for_rust_enum
|| flags
->print_offsets
)
775 print_spaces (level
+ 2, stream
);
777 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
779 else if (!is_tuple_struct
)
780 gdb_printf (stream
, "%ps: ",
781 styled_string (variable_name_style
.style (),
782 type
->field (i
).name ()));
784 rust_internal_print_type (type
->field (i
).type (), NULL
,
785 stream
, (is_enum
? show
: show
- 1),
786 level
+ 2, flags
, is_enum
, podata
);
787 if (!for_rust_enum
|| flags
->print_offsets
)
788 gdb_puts (",\n", stream
);
789 /* Note that this check of "I" is ok because we only sorted the
790 fields by offset when print_offsets was set, so we won't take
791 this branch in that case. */
792 else if (i
+ 1 < type
->num_fields ())
793 gdb_puts (", ", stream
);
796 if (flags
->print_offsets
)
798 /* Undo the temporary level increase we did above. */
800 podata
->finish (type
, level
, stream
);
801 print_spaces (print_offset_data::indentation
, stream
);
803 print_spaces (2, stream
);
805 if (!for_rust_enum
|| flags
->print_offsets
)
806 print_spaces (level
, stream
);
807 gdb_puts (is_tuple_struct
? ")" : "}", stream
);
810 /* la_print_type implementation for Rust. */
813 rust_internal_print_type (struct type
*type
, const char *varstring
,
814 struct ui_file
*stream
, int show
, int level
,
815 const struct type_print_options
*flags
,
816 bool for_rust_enum
, print_offset_data
*podata
)
820 && type
->name () != NULL
)
822 /* Rust calls the unit type "void" in its debuginfo,
823 but we don't want to print it as that. */
824 if (type
->code () == TYPE_CODE_VOID
)
825 gdb_puts ("()", stream
);
827 gdb_puts (type
->name (), stream
);
831 type
= check_typedef (type
);
832 switch (type
->code ())
835 /* If we have an enum, we've already printed the type's
836 unqualified name, and there is nothing else to print
839 gdb_puts ("()", stream
);
843 /* Delegate varargs to the C printer. */
844 if (type
->has_varargs ())
847 gdb_puts ("fn ", stream
);
848 if (varstring
!= NULL
)
849 gdb_puts (varstring
, stream
);
850 gdb_puts ("(", stream
);
851 for (int i
= 0; i
< type
->num_fields (); ++i
)
855 gdb_puts (", ", stream
);
856 rust_internal_print_type (type
->field (i
).type (), "", stream
,
857 -1, 0, flags
, false, podata
);
859 gdb_puts (")", stream
);
860 /* If it returns unit, we can omit the return type. */
861 if (type
->target_type ()->code () != TYPE_CODE_VOID
)
863 gdb_puts (" -> ", stream
);
864 rust_internal_print_type (type
->target_type (), "", stream
,
865 -1, 0, flags
, false, podata
);
869 case TYPE_CODE_ARRAY
:
871 LONGEST low_bound
, high_bound
;
873 gdb_puts ("[", stream
);
874 rust_internal_print_type (type
->target_type (), NULL
,
875 stream
, show
- 1, level
, flags
, false,
878 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
879 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
880 gdb_printf (stream
, "; variable length");
881 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
882 gdb_printf (stream
, "; %s",
883 plongest (high_bound
- low_bound
+ 1));
884 gdb_puts ("]", stream
);
888 case TYPE_CODE_UNION
:
889 case TYPE_CODE_STRUCT
:
890 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
891 for_rust_enum
, podata
);
898 gdb_puts ("enum ", stream
);
899 if (type
->name () != NULL
)
901 gdb_puts (type
->name (), stream
);
902 gdb_puts (" ", stream
);
903 len
= strlen (type
->name ());
905 gdb_puts ("{\n", stream
);
907 for (int i
= 0; i
< type
->num_fields (); ++i
)
909 const char *name
= type
->field (i
).name ();
914 && strncmp (name
, type
->name (), len
) == 0
916 && name
[len
+ 1] == ':')
918 gdb_printf (stream
, "%*s%ps,\n",
920 styled_string (variable_name_style
.style (),
924 gdb_puts ("}", stream
);
930 if (type
->name () != nullptr)
931 gdb_puts (type
->name (), stream
);
934 /* We currently can't distinguish between pointers and
936 gdb_puts ("*mut ", stream
);
937 type_print (type
->target_type (), "", stream
, 0);
944 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
,
951 /* Like arch_composite_type, but uses TYPE to decide how to allocate
952 -- either on an obstack or on a gdbarch. */
955 rust_composite_type (struct type
*original
,
957 const char *field1
, struct type
*type1
,
958 const char *field2
, struct type
*type2
)
960 struct type
*result
= type_allocator (original
).new_type ();
961 int i
, nfields
, bitpos
;
969 result
->set_code (TYPE_CODE_STRUCT
);
970 result
->set_name (name
);
972 result
->alloc_fields (nfields
);
978 struct field
*field
= &result
->field (i
);
980 field
->set_loc_bitpos (bitpos
);
981 bitpos
+= type1
->length () * TARGET_CHAR_BIT
;
983 field
->set_name (field1
);
984 field
->set_type (type1
);
989 struct field
*field
= &result
->field (i
);
990 unsigned align
= type_align (type2
);
996 align
*= TARGET_CHAR_BIT
;
997 delta
= bitpos
% align
;
999 bitpos
+= align
- delta
;
1001 field
->set_loc_bitpos (bitpos
);
1003 field
->set_name (field2
);
1004 field
->set_type (type2
);
1009 result
->set_length (result
->field (i
- 1).loc_bitpos () / TARGET_CHAR_BIT
1010 + result
->field (i
- 1).type ()->length ());
1014 /* See rust-lang.h. */
1017 rust_slice_type (const char *name
, struct type
*elt_type
,
1018 struct type
*usize_type
)
1022 elt_type
= lookup_pointer_type (elt_type
);
1023 type
= rust_composite_type (elt_type
, name
,
1024 "data_ptr", elt_type
,
1025 "length", usize_type
);
1032 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1035 rust_range (struct type
*expect_type
, struct expression
*exp
,
1036 enum noside noside
, enum range_flag kind
,
1037 struct value
*low
, struct value
*high
)
1039 struct value
*addrval
, *result
;
1041 struct type
*range_type
;
1042 struct type
*index_type
;
1043 struct type
*temp_type
;
1046 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1053 name
= "std::ops::RangeFull";
1057 index_type
= high
->type ();
1059 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1066 index_type
= low
->type ();
1067 name
= "std::ops::RangeFrom";
1071 if (!types_equal (low
->type (), high
->type ()))
1072 error (_("Range expression with different types"));
1073 index_type
= low
->type ();
1074 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1078 /* If we don't have an index type, just allocate this on the
1079 arch. Here any type will do. */
1080 temp_type
= (index_type
== NULL
1081 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1083 /* It would be nicer to cache the range type. */
1084 range_type
= rust_composite_type (temp_type
, name
,
1085 low
== NULL
? NULL
: "start", index_type
,
1086 high
== NULL
? NULL
: "end", index_type
);
1088 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1089 return value::zero (range_type
, lval_memory
);
1091 addrval
= value_allocate_space_in_inferior (range_type
->length ());
1092 addr
= value_as_long (addrval
);
1093 result
= value_at_lazy (range_type
, addr
);
1097 struct value
*start
= value_struct_elt (&result
, {}, "start", NULL
,
1100 value_assign (start
, low
);
1105 struct value
*end
= value_struct_elt (&result
, {}, "end", NULL
,
1108 value_assign (end
, high
);
1111 result
= value_at_lazy (range_type
, addr
);
1115 /* A helper function to compute the range and kind given a range
1116 value. TYPE is the type of the range value. RANGE is the range
1117 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1118 parameters might be filled in, or might not be, depending on the
1119 kind of range this is. KIND will always be set to the appropriate
1120 value describing the kind of range, and this can be used to
1121 determine whether LOW or HIGH are valid. */
1124 rust_compute_range (struct type
*type
, struct value
*range
,
1125 LONGEST
*low
, LONGEST
*high
,
1132 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1134 if (type
->num_fields () == 0)
1138 if (strcmp (type
->field (0).name (), "start") == 0)
1140 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1141 *low
= value_as_long (value_field (range
, 0));
1144 if (type
->num_fields () > i
1145 && strcmp (type
->field (i
).name (), "end") == 0)
1147 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1148 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1149 *high
= value_as_long (value_field (range
, i
));
1151 if (rust_inclusive_range_type_p (type
))
1156 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1159 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1160 enum noside noside
, bool for_addr
,
1161 struct value
*lhs
, struct value
*rhs
)
1163 struct value
*result
;
1164 struct type
*rhstype
;
1165 LONGEST low
, high_bound
;
1166 /* Initialized to appease the compiler. */
1167 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1171 rhstype
= check_typedef (rhs
->type ());
1172 if (rust_range_type_p (rhstype
))
1175 error (_("Can't take slice of array without '&'"));
1176 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1180 low
= value_as_long (rhs
);
1182 struct type
*type
= check_typedef (lhs
->type ());
1183 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1185 struct type
*base_type
= nullptr;
1186 if (type
->code () == TYPE_CODE_ARRAY
)
1187 base_type
= type
->target_type ();
1188 else if (rust_slice_type_p (type
))
1190 for (int i
= 0; i
< type
->num_fields (); ++i
)
1192 if (strcmp (type
->field (i
).name (), "data_ptr") == 0)
1194 base_type
= type
->field (i
).type ()->target_type ();
1198 if (base_type
== nullptr)
1199 error (_("Could not find 'data_ptr' in slice type"));
1201 else if (type
->code () == TYPE_CODE_PTR
)
1202 base_type
= type
->target_type ();
1204 error (_("Cannot subscript non-array type"));
1206 struct type
*new_type
;
1209 if (rust_slice_type_p (type
))
1214 = language_lookup_primitive_type (exp
->language_defn
,
1217 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1221 new_type
= base_type
;
1223 return value::zero (new_type
, lhs
->lval ());
1230 if (type
->code () == TYPE_CODE_ARRAY
)
1233 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1234 error (_("Can't compute array bounds"));
1236 error (_("Found array with non-zero lower bound"));
1239 else if (rust_slice_type_p (type
))
1243 base
= value_struct_elt (&lhs
, {}, "data_ptr", NULL
, "slice");
1244 len
= value_struct_elt (&lhs
, {}, "length", NULL
, "slice");
1246 high_bound
= value_as_long (len
);
1248 else if (type
->code () == TYPE_CODE_PTR
)
1252 high_bound
= LONGEST_MAX
;
1255 error (_("Cannot subscript non-array type"));
1257 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1260 error (_("Index less than zero"));
1261 if (low
> high_bound
)
1262 error (_("Index greater than length"));
1264 result
= value_subscript (base
, low
);
1271 struct type
*usize
, *slice
;
1273 struct value
*addrval
, *tem
;
1275 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1278 error (_("High index less than zero"));
1280 error (_("Low index greater than high index"));
1281 if (high
> high_bound
)
1282 error (_("High index greater than length"));
1284 usize
= language_lookup_primitive_type (exp
->language_defn
,
1287 const char *new_name
= ((type
!= nullptr
1288 && rust_slice_type_p (type
))
1289 ? type
->name () : "&[*gdb*]");
1291 slice
= rust_slice_type (new_name
, result
->type (), usize
);
1293 addrval
= value_allocate_space_in_inferior (slice
->length ());
1294 addr
= value_as_long (addrval
);
1295 tem
= value_at_lazy (slice
, addr
);
1297 value_assign (value_field (tem
, 0), value_addr (result
));
1298 value_assign (value_field (tem
, 1),
1299 value_from_longest (usize
, high
- low
));
1301 result
= value_at_lazy (slice
, addr
);
1304 result
= value_addr (result
);
1314 rust_unop_ind_operation::evaluate (struct type
*expect_type
,
1315 struct expression
*exp
,
1318 if (noside
!= EVAL_NORMAL
)
1319 return unop_ind_operation::evaluate (expect_type
, exp
, noside
);
1321 struct value
*value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1323 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1324 if (trait_ptr
!= NULL
)
1327 return value_ind (value
);
1330 } /* namespace expr */
1332 /* A helper function for UNOP_COMPLEMENT. */
1335 eval_op_rust_complement (struct type
*expect_type
, struct expression
*exp
,
1337 enum exp_opcode opcode
,
1338 struct value
*value
)
1340 if (value
->type ()->code () == TYPE_CODE_BOOL
)
1341 return value_from_longest (value
->type (), value_logical_not (value
));
1342 return value_complement (value
);
1345 /* A helper function for OP_ARRAY. */
1348 eval_op_rust_array (struct type
*expect_type
, struct expression
*exp
,
1350 enum exp_opcode opcode
,
1351 struct value
*elt
, struct value
*ncopies
)
1353 int copies
= value_as_long (ncopies
);
1355 error (_("Array with negative number of elements"));
1357 if (noside
== EVAL_NORMAL
)
1358 return value_array (0, std::vector
<value
*> (copies
, elt
));
1361 struct type
*arraytype
1362 = lookup_array_range_type (elt
->type (), 0, copies
- 1);
1363 return value::allocate (arraytype
);
1371 rust_struct_anon::evaluate (struct type
*expect_type
,
1372 struct expression
*exp
,
1375 value
*lhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
1376 int field_number
= std::get
<0> (m_storage
);
1378 struct type
*type
= lhs
->type ();
1380 if (type
->code () == TYPE_CODE_STRUCT
)
1382 struct type
*outer_type
= NULL
;
1384 if (rust_enum_p (type
))
1386 type
= resolve_dynamic_type (type
, lhs
->contents (),
1389 if (rust_empty_enum_p (type
))
1390 error (_("Cannot access field %d of empty enum %s"),
1391 field_number
, type
->name ());
1393 int fieldno
= rust_enum_variant (type
);
1394 lhs
= lhs
->primitive_field (0, fieldno
, type
);
1396 type
= lhs
->type ();
1399 /* Tuples and tuple structs */
1400 int nfields
= type
->num_fields ();
1402 if (field_number
>= nfields
|| field_number
< 0)
1404 if (outer_type
!= NULL
)
1405 error(_("Cannot access field %d of variant %s::%s, "
1406 "there are only %d fields"),
1407 field_number
, outer_type
->name (),
1408 rust_last_path_segment (type
->name ()),
1411 error(_("Cannot access field %d of %s, "
1412 "there are only %d fields"),
1413 field_number
, type
->name (), nfields
);
1416 /* Tuples are tuple structs too. */
1417 if (!rust_tuple_struct_type_p (type
))
1419 if (outer_type
!= NULL
)
1420 error(_("Variant %s::%s is not a tuple variant"),
1421 outer_type
->name (),
1422 rust_last_path_segment (type
->name ()));
1424 error(_("Attempting to access anonymous field %d "
1425 "of %s, which is not a tuple, tuple struct, or "
1426 "tuple-like variant"),
1427 field_number
, type
->name ());
1430 return lhs
->primitive_field (0, field_number
, type
);
1433 error(_("Anonymous field access is only allowed on tuples, \
1434 tuple structs, and tuple-like enum variants"));
1438 rust_structop::evaluate (struct type
*expect_type
,
1439 struct expression
*exp
,
1442 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1443 const char *field_name
= std::get
<1> (m_storage
).c_str ();
1445 struct value
*result
;
1446 struct type
*type
= lhs
->type ();
1447 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1449 type
= resolve_dynamic_type (type
, lhs
->contents (),
1452 if (rust_empty_enum_p (type
))
1453 error (_("Cannot access field %s of empty enum %s"),
1454 field_name
, type
->name ());
1456 int fieldno
= rust_enum_variant (type
);
1457 lhs
= lhs
->primitive_field (0, fieldno
, type
);
1459 struct type
*outer_type
= type
;
1460 type
= lhs
->type ();
1461 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1462 error (_("Attempting to access named field %s of tuple "
1463 "variant %s::%s, which has only anonymous fields"),
1464 field_name
, outer_type
->name (),
1465 rust_last_path_segment (type
->name ()));
1469 result
= value_struct_elt (&lhs
, {}, field_name
,
1472 catch (const gdb_exception_error
&except
)
1474 error (_("Could not find field %s of struct variant %s::%s"),
1475 field_name
, outer_type
->name (),
1476 rust_last_path_segment (type
->name ()));
1480 result
= value_struct_elt (&lhs
, {}, field_name
, NULL
, "structure");
1481 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1482 result
= value::zero (result
->type (), result
->lval ());
1487 rust_aggregate_operation::evaluate (struct type
*expect_type
,
1488 struct expression
*exp
,
1491 struct type
*type
= std::get
<0> (m_storage
);
1493 struct value
*addrval
= NULL
;
1496 if (noside
== EVAL_NORMAL
)
1498 addrval
= value_allocate_space_in_inferior (type
->length ());
1499 addr
= value_as_long (addrval
);
1500 result
= value_at_lazy (type
, addr
);
1503 if (std::get
<1> (m_storage
) != nullptr)
1505 struct value
*init
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1508 if (noside
== EVAL_NORMAL
)
1510 /* This isn't quite right but will do for the time
1511 being, seeing that we can't implement the Copy
1513 value_assign (result
, init
);
1517 for (const auto &item
: std::get
<2> (m_storage
))
1519 value
*val
= item
.second
->evaluate (nullptr, exp
, noside
);
1520 if (noside
== EVAL_NORMAL
)
1522 const char *fieldname
= item
.first
.c_str ();
1523 value
*field
= value_struct_elt (&result
, {}, fieldname
,
1524 nullptr, "structure");
1525 value_assign (field
, val
);
1529 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1530 result
= value::allocate (type
);
1532 result
= value_at_lazy (type
, addr
);
1538 rust_structop::evaluate_funcall (struct type
*expect_type
,
1539 struct expression
*exp
,
1541 const std::vector
<operation_up
> &ops
)
1543 std::vector
<struct value
*> args (ops
.size () + 1);
1545 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1546 type in order to look up the method. */
1547 args
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1548 /* We don't yet implement real Deref semantics. */
1549 while (args
[0]->type ()->code () == TYPE_CODE_PTR
)
1550 args
[0] = value_ind (args
[0]);
1552 struct type
*type
= args
[0]->type ();
1553 if ((type
->code () != TYPE_CODE_STRUCT
1554 && type
->code () != TYPE_CODE_UNION
1555 && type
->code () != TYPE_CODE_ENUM
)
1556 || rust_tuple_type_p (type
))
1557 error (_("Method calls only supported on struct or enum types"));
1558 if (type
->name () == NULL
)
1559 error (_("Method call on nameless type"));
1561 std::string name
= (std::string (type
->name ()) + "::"
1562 + std::get
<1> (m_storage
));
1564 const struct block
*block
= get_selected_block (0);
1565 struct block_symbol sym
= lookup_symbol (name
.c_str (), block
,
1567 if (sym
.symbol
== NULL
)
1568 error (_("Could not find function named '%s'"), name
.c_str ());
1570 struct type
*fn_type
= sym
.symbol
->type ();
1571 if (fn_type
->num_fields () == 0)
1572 error (_("Function '%s' takes no arguments"), name
.c_str ());
1574 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1575 args
[0] = value_addr (args
[0]);
1577 value
*function
= address_of_variable (sym
.symbol
, block
);
1579 for (int i
= 0; i
< ops
.size (); ++i
)
1580 args
[i
+ 1] = ops
[i
]->evaluate (nullptr, exp
, noside
);
1582 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1583 return value::zero (fn_type
->target_type (), not_lval
);
1584 return call_function_by_hand (function
, NULL
, args
);
1591 /* See language.h. */
1594 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1595 struct language_arch_info
*lai
) const
1597 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1599 /* Helper function to allow shorter lines below. */
1600 auto add
= [&] (struct type
* t
) -> struct type
*
1602 lai
->add_primitive_type (t
);
1606 type_allocator
alloc (gdbarch
);
1607 struct type
*bool_type
1608 = add (init_boolean_type (alloc
, 8, 1, "bool"));
1609 add (init_character_type (alloc
, 32, 1, "char"));
1610 add (init_integer_type (alloc
, 8, 0, "i8"));
1611 struct type
*u8_type
1612 = add (init_integer_type (alloc
, 8, 1, "u8"));
1613 add (init_integer_type (alloc
, 16, 0, "i16"));
1614 add (init_integer_type (alloc
, 16, 1, "u16"));
1615 add (init_integer_type (alloc
, 32, 0, "i32"));
1616 add (init_integer_type (alloc
, 32, 1, "u32"));
1617 add (init_integer_type (alloc
, 64, 0, "i64"));
1618 add (init_integer_type (alloc
, 64, 1, "u64"));
1619 add (init_integer_type (alloc
, 128, 0, "i128"));
1620 add (init_integer_type (alloc
, 128, 1, "u128"));
1622 unsigned int length
= 8 * builtin
->builtin_data_ptr
->length ();
1623 add (init_integer_type (alloc
, length
, 0, "isize"));
1624 struct type
*usize_type
1625 = add (init_integer_type (alloc
, length
, 1, "usize"));
1627 add (init_float_type (alloc
, 32, "f32", floatformats_ieee_single
));
1628 add (init_float_type (alloc
, 64, "f64", floatformats_ieee_double
));
1629 add (init_integer_type (alloc
, 0, 1, "()"));
1631 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1632 add (rust_slice_type ("&str", tem
, usize_type
));
1634 lai
->set_bool_type (bool_type
);
1635 lai
->set_string_char_type (u8_type
);
1638 /* See language.h. */
1641 rust_language::print_type (struct type
*type
, const char *varstring
,
1642 struct ui_file
*stream
, int show
, int level
,
1643 const struct type_print_options
*flags
) const
1645 print_offset_data
podata (flags
);
1646 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1647 flags
, false, &podata
);
1650 /* See language.h. */
1653 rust_language::emitchar (int ch
, struct type
*chtype
,
1654 struct ui_file
*stream
, int quoter
) const
1656 if (!rust_chartype_p (chtype
))
1657 generic_emit_char (ch
, chtype
, stream
, quoter
,
1658 target_charset (chtype
->arch ()));
1659 else if (ch
== '\\' || ch
== quoter
)
1660 gdb_printf (stream
, "\\%c", ch
);
1661 else if (ch
== '\n')
1662 gdb_puts ("\\n", stream
);
1663 else if (ch
== '\r')
1664 gdb_puts ("\\r", stream
);
1665 else if (ch
== '\t')
1666 gdb_puts ("\\t", stream
);
1667 else if (ch
== '\0')
1668 gdb_puts ("\\0", stream
);
1669 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1670 gdb_putc (ch
, stream
);
1672 gdb_printf (stream
, "\\x%02x", ch
);
1674 gdb_printf (stream
, "\\u{%06x}", ch
);
1677 /* See language.h. */
1680 rust_language::is_string_type_p (struct type
*type
) const
1682 LONGEST low_bound
, high_bound
;
1684 type
= check_typedef (type
);
1685 return ((type
->code () == TYPE_CODE_STRING
)
1686 || (type
->code () == TYPE_CODE_PTR
1687 && (type
->target_type ()->code () == TYPE_CODE_ARRAY
1688 && rust_u8_type_p (type
->target_type ()->target_type ())
1689 && get_array_bounds (type
->target_type (), &low_bound
,
1691 || (type
->code () == TYPE_CODE_STRUCT
1692 && !rust_enum_p (type
)
1693 && rust_slice_type_p (type
)
1694 && strcmp (type
->name (), "&str") == 0));
1697 /* See language.h. */
1700 rust_language::lookup_symbol_nonlocal
1701 (const char *name
, const struct block
*block
,
1702 const domain_enum domain
) const
1704 struct block_symbol result
= {};
1706 const char *scope
= block
== nullptr ? "" : block
->scope ();
1707 symbol_lookup_debug_printf
1708 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1709 name
, host_address_to_string (block
), scope
,
1710 domain_name (domain
));
1712 /* Look up bare names in the block's scope. */
1713 std::string scopedname
;
1714 if (name
[cp_find_first_component (name
)] == '\0')
1716 if (scope
[0] != '\0')
1718 scopedname
= std::string (scope
) + "::" + name
;
1719 name
= scopedname
.c_str ();
1727 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1728 if (result
.symbol
== NULL
)
1729 result
= lookup_global_symbol (name
, block
, domain
);
1734 /* Single instance of the Rust language class. */
1736 static rust_language rust_language_defn
;