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 return (type
->code () == TYPE_CODE_STRUCT
163 && type
->name () != NULL
164 && (strncmp (type
->name (), "&[", 2) == 0
165 || strcmp (type
->name (), "&str") == 0));
168 /* Return true if TYPE is a range type, otherwise false. */
171 rust_range_type_p (struct type
*type
)
175 if (type
->code () != TYPE_CODE_STRUCT
176 || type
->num_fields () > 2
177 || type
->name () == NULL
178 || strstr (type
->name (), "::Range") == NULL
)
181 if (type
->num_fields () == 0)
185 if (strcmp (type
->field (0).name (), "start") == 0)
187 if (type
->num_fields () == 1)
191 else if (type
->num_fields () == 2)
193 /* First field had to be "start". */
197 return strcmp (type
->field (i
).name (), "end") == 0;
200 /* Return true if TYPE is an inclusive range type, otherwise false.
201 This is only valid for types which are already known to be range
205 rust_inclusive_range_type_p (struct type
*type
)
207 return (strstr (type
->name (), "::RangeInclusive") != NULL
208 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
211 /* Return true if TYPE seems to be the type "u8", otherwise false. */
214 rust_u8_type_p (struct type
*type
)
216 return (type
->code () == TYPE_CODE_INT
217 && type
->is_unsigned ()
218 && TYPE_LENGTH (type
) == 1);
221 /* Return true if TYPE is a Rust character type. */
224 rust_chartype_p (struct type
*type
)
226 return (type
->code () == TYPE_CODE_CHAR
227 && TYPE_LENGTH (type
) == 4
228 && type
->is_unsigned ());
231 /* If VALUE represents a trait object pointer, return the underlying
232 pointer with the correct (i.e., runtime) type. Otherwise, return
235 static struct value
*
236 rust_get_trait_object_pointer (struct value
*value
)
238 struct type
*type
= check_typedef (value_type (value
));
240 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
243 /* Try to be a bit resilient if the ABI changes. */
244 int vtable_field
= 0;
245 for (int i
= 0; i
< 2; ++i
)
247 if (strcmp (type
->field (i
).name (), "vtable") == 0)
249 else if (strcmp (type
->field (i
).name (), "pointer") != 0)
253 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
254 struct symbol
*symbol
= find_symbol_at_address (vtable
);
255 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
258 struct rust_vtable_symbol
*vtable_sym
259 = static_cast<struct rust_vtable_symbol
*> (symbol
);
260 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
261 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
266 /* See language.h. */
269 rust_language::printstr (struct ui_file
*stream
, struct type
*type
,
270 const gdb_byte
*string
, unsigned int length
,
271 const char *user_encoding
, int force_ellipses
,
272 const struct value_print_options
*options
) const
274 /* Rust always uses UTF-8, but let the caller override this if need
276 const char *encoding
= user_encoding
;
277 if (user_encoding
== NULL
|| !*user_encoding
)
279 /* In Rust strings, characters are "u8". */
280 if (rust_u8_type_p (type
))
284 /* This is probably some C string, so let's let C deal with
286 c_printstr (stream
, type
, string
, length
, user_encoding
,
287 force_ellipses
, options
);
292 /* This is not ideal as it doesn't use our character printer. */
293 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
299 /* Helper function to print a string slice. */
302 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
303 const struct value_print_options
*options
)
305 struct value
*base
= value_struct_elt (&val
, {}, "data_ptr", NULL
,
307 struct value
*len
= value_struct_elt (&val
, {}, "length", NULL
, "slice");
309 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
310 value_as_address (base
), value_as_long (len
), stream
,
314 /* See rust-lang.h. */
317 rust_language::val_print_struct
318 (struct value
*val
, struct ui_file
*stream
, int recurse
,
319 const struct value_print_options
*options
) const
323 struct type
*type
= check_typedef (value_type (val
));
325 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
327 /* If what we are printing here is actually a string within a
328 structure then VAL will be the original parent value, while TYPE
329 will be the type of the structure representing the string we want
331 However, RUST_VAL_PRINT_STR looks up the fields of the string
332 inside VAL, assuming that VAL is the string.
333 So, recreate VAL as a value representing just the string. */
334 val
= value_at_lazy (type
, value_address (val
));
335 rust_val_print_str (stream
, val
, options
);
339 bool is_tuple
= rust_tuple_type_p (type
);
340 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
341 struct value_print_options opts
;
345 if (type
->name () != NULL
)
346 gdb_printf (stream
, "%s", type
->name ());
348 if (type
->num_fields () == 0)
351 if (type
->name () != NULL
)
352 gdb_puts (" ", stream
);
355 if (is_tuple
|| is_tuple_struct
)
356 gdb_puts ("(", stream
);
358 gdb_puts ("{", stream
);
364 for (i
= 0; i
< type
->num_fields (); ++i
)
366 if (field_is_static (&type
->field (i
)))
370 gdb_puts (",", stream
);
372 if (options
->prettyformat
)
374 gdb_puts ("\n", stream
);
375 print_spaces (2 + 2 * recurse
, stream
);
377 else if (!first_field
)
378 gdb_puts (" ", stream
);
382 if (!is_tuple
&& !is_tuple_struct
)
384 fputs_styled (type
->field (i
).name (),
385 variable_name_style
.style (), stream
);
386 gdb_puts (": ", stream
);
389 common_val_print (value_field (val
, i
), stream
, recurse
+ 1, &opts
,
393 if (options
->prettyformat
)
395 gdb_puts ("\n", stream
);
396 print_spaces (2 * recurse
, stream
);
399 if (is_tuple
|| is_tuple_struct
)
400 gdb_puts (")", stream
);
402 gdb_puts ("}", stream
);
405 /* See rust-lang.h. */
408 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
410 const struct value_print_options
*options
) const
412 struct value_print_options opts
= *options
;
413 struct type
*type
= check_typedef (value_type (val
));
417 gdb_assert (rust_enum_p (type
));
418 gdb::array_view
<const gdb_byte
> view
419 (value_contents_for_printing (val
).data (),
420 TYPE_LENGTH (value_type (val
)));
421 type
= resolve_dynamic_type (type
, view
, value_address (val
));
423 if (rust_empty_enum_p (type
))
425 /* Print the enum type name here to be more clear. */
426 gdb_printf (stream
, _("%s {%p[<No data fields>%p]}"),
428 metadata_style
.style ().ptr (), nullptr);
432 int variant_fieldno
= rust_enum_variant (type
);
433 val
= value_field (val
, variant_fieldno
);
434 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
436 int nfields
= variant_type
->num_fields ();
438 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
440 gdb_printf (stream
, "%s", variant_type
->name ());
443 /* In case of a nullary variant like 'None', just output
448 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
450 gdb_printf (stream
, "(");
453 /* struct variant. */
454 gdb_printf (stream
, "{");
457 bool first_field
= true;
458 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
461 gdb_puts (", ", stream
);
465 gdb_printf (stream
, "%ps: ",
466 styled_string (variable_name_style
.style (),
467 variant_type
->field (j
).name ()));
469 common_val_print (value_field (val
, j
), stream
, recurse
+ 1, &opts
,
474 gdb_puts (")", stream
);
476 gdb_puts ("}", stream
);
479 static const struct generic_val_print_decorations rust_decorations
=
481 /* Complex isn't used in Rust, but we provide C-ish values just in
493 /* See language.h. */
496 rust_language::value_print_inner
497 (struct value
*val
, struct ui_file
*stream
, int recurse
,
498 const struct value_print_options
*options
) const
500 struct value_print_options opts
= *options
;
503 if (opts
.prettyformat
== Val_prettyformat_default
)
504 opts
.prettyformat
= (opts
.prettyformat_structs
505 ? Val_prettyformat
: Val_no_prettyformat
);
507 struct type
*type
= check_typedef (value_type (val
));
508 switch (type
->code ())
512 LONGEST low_bound
, high_bound
;
514 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
515 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
516 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
519 /* We have a pointer to a byte string, so just print
521 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
522 CORE_ADDR addr
= value_as_address (val
);
523 struct gdbarch
*arch
= type
->arch ();
525 if (opts
.addressprint
)
527 gdb_puts (paddress (arch
, addr
), stream
);
528 gdb_puts (" ", stream
);
531 gdb_puts ("b", stream
);
532 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
533 high_bound
- low_bound
+ 1, stream
,
541 /* Recognize the unit type. */
542 if (type
->is_unsigned () && TYPE_LENGTH (type
) == 0
543 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
545 gdb_puts ("()", stream
);
550 case TYPE_CODE_STRING
:
552 LONGEST low_bound
, high_bound
;
554 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
555 error (_("Could not determine the array bounds"));
557 /* If we see a plain TYPE_CODE_STRING, then we're printing a
558 byte string, hence the choice of "ASCII" as the
560 gdb_puts ("b", stream
);
561 printstr (stream
, TYPE_TARGET_TYPE (type
),
562 value_contents_for_printing (val
).data (),
563 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
567 case TYPE_CODE_ARRAY
:
569 LONGEST low_bound
, high_bound
;
571 if (get_array_bounds (type
, &low_bound
, &high_bound
)
572 && high_bound
- low_bound
+ 1 == 0)
573 gdb_puts ("[]", stream
);
579 case TYPE_CODE_UNION
:
580 /* Untagged unions are printed as if they are structs. Since
581 the field bit positions overlap in the debuginfo, the code
582 for printing a union is same as that for a struct, the only
583 difference is that the input type will have overlapping
585 val_print_struct (val
, stream
, recurse
, &opts
);
588 case TYPE_CODE_STRUCT
:
589 if (rust_enum_p (type
))
590 print_enum (val
, stream
, recurse
, &opts
);
592 val_print_struct (val
, stream
, recurse
, &opts
);
597 /* Nothing special yet. */
598 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
605 rust_internal_print_type (struct type
*type
, const char *varstring
,
606 struct ui_file
*stream
, int show
, int level
,
607 const struct type_print_options
*flags
,
608 bool for_rust_enum
, print_offset_data
*podata
);
610 /* Print a struct or union typedef. */
612 rust_print_struct_def (struct type
*type
, const char *varstring
,
613 struct ui_file
*stream
, int show
, int level
,
614 const struct type_print_options
*flags
,
615 bool for_rust_enum
, print_offset_data
*podata
)
617 /* Print a tuple type simply. */
618 if (rust_tuple_type_p (type
))
620 gdb_puts (type
->name (), stream
);
624 /* If we see a base class, delegate to C. */
625 if (TYPE_N_BASECLASSES (type
) > 0)
626 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
628 if (flags
->print_offsets
)
630 /* Temporarily bump the level so that the output lines up
635 /* Compute properties of TYPE here because, in the enum case, the
636 rest of the code ends up looking only at the variant part. */
637 const char *tagname
= type
->name ();
638 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
639 bool is_tuple
= rust_tuple_type_p (type
);
640 bool is_enum
= rust_enum_p (type
);
644 /* Already printing an outer enum, so nothing to print here. */
648 /* This code path is also used by unions and enums. */
651 gdb_puts ("enum ", stream
);
652 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
653 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
654 type
= prop
->original_type ();
656 else if (type
->code () == TYPE_CODE_STRUCT
)
657 gdb_puts ("struct ", stream
);
659 gdb_puts ("union ", stream
);
662 gdb_puts (tagname
, stream
);
665 if (type
->num_fields () == 0 && !is_tuple
)
667 if (for_rust_enum
&& !flags
->print_offsets
)
668 gdb_puts (is_tuple_struct
? "(" : "{", stream
);
670 gdb_puts (is_tuple_struct
? " (\n" : " {\n", stream
);
672 /* When printing offsets, we rearrange the fields into storage
673 order. This lets us show holes more clearly. We work using
674 field indices here because it simplifies calls to
675 print_offset_data::update below. */
676 std::vector
<int> fields
;
677 for (int i
= 0; i
< type
->num_fields (); ++i
)
679 if (field_is_static (&type
->field (i
)))
681 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
683 fields
.push_back (i
);
685 if (flags
->print_offsets
)
686 std::sort (fields
.begin (), fields
.end (),
689 return (type
->field (a
).loc_bitpos ()
690 < type
->field (b
).loc_bitpos ());
697 gdb_assert (!field_is_static (&type
->field (i
)));
698 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
700 if (flags
->print_offsets
)
701 podata
->update (type
, i
, stream
);
703 /* We'd like to print "pub" here as needed, but rustc
704 doesn't emit the debuginfo, and our types don't have
705 cplus_struct_type attached. */
707 /* For a tuple struct we print the type but nothing
709 if (!for_rust_enum
|| flags
->print_offsets
)
710 print_spaces (level
+ 2, stream
);
712 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
714 else if (!is_tuple_struct
)
715 gdb_printf (stream
, "%ps: ",
716 styled_string (variable_name_style
.style (),
717 type
->field (i
).name ()));
719 rust_internal_print_type (type
->field (i
).type (), NULL
,
720 stream
, (is_enum
? show
: show
- 1),
721 level
+ 2, flags
, is_enum
, podata
);
722 if (!for_rust_enum
|| flags
->print_offsets
)
723 gdb_puts (",\n", stream
);
724 /* Note that this check of "I" is ok because we only sorted the
725 fields by offset when print_offsets was set, so we won't take
726 this branch in that case. */
727 else if (i
+ 1 < type
->num_fields ())
728 gdb_puts (", ", stream
);
731 if (flags
->print_offsets
)
733 /* Undo the temporary level increase we did above. */
735 podata
->finish (type
, level
, stream
);
736 print_spaces (print_offset_data::indentation
, stream
);
738 print_spaces (2, stream
);
740 if (!for_rust_enum
|| flags
->print_offsets
)
741 print_spaces (level
, stream
);
742 gdb_puts (is_tuple_struct
? ")" : "}", stream
);
745 /* la_print_type implementation for Rust. */
748 rust_internal_print_type (struct type
*type
, const char *varstring
,
749 struct ui_file
*stream
, int show
, int level
,
750 const struct type_print_options
*flags
,
751 bool for_rust_enum
, print_offset_data
*podata
)
755 && type
->name () != NULL
)
757 /* Rust calls the unit type "void" in its debuginfo,
758 but we don't want to print it as that. */
759 if (type
->code () == TYPE_CODE_VOID
)
760 gdb_puts ("()", stream
);
762 gdb_puts (type
->name (), stream
);
766 type
= check_typedef (type
);
767 switch (type
->code ())
770 /* If we have an enum, we've already printed the type's
771 unqualified name, and there is nothing else to print
774 gdb_puts ("()", stream
);
778 /* Delegate varargs to the C printer. */
779 if (type
->has_varargs ())
782 gdb_puts ("fn ", stream
);
783 if (varstring
!= NULL
)
784 gdb_puts (varstring
, stream
);
785 gdb_puts ("(", stream
);
786 for (int i
= 0; i
< type
->num_fields (); ++i
)
790 gdb_puts (", ", stream
);
791 rust_internal_print_type (type
->field (i
).type (), "", stream
,
792 -1, 0, flags
, false, podata
);
794 gdb_puts (")", stream
);
795 /* If it returns unit, we can omit the return type. */
796 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
798 gdb_puts (" -> ", stream
);
799 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
800 -1, 0, flags
, false, podata
);
804 case TYPE_CODE_ARRAY
:
806 LONGEST low_bound
, high_bound
;
808 gdb_puts ("[", stream
);
809 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
810 stream
, show
- 1, level
, flags
, false,
813 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
814 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
815 gdb_printf (stream
, "; variable length");
816 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
817 gdb_printf (stream
, "; %s",
818 plongest (high_bound
- low_bound
+ 1));
819 gdb_puts ("]", stream
);
823 case TYPE_CODE_UNION
:
824 case TYPE_CODE_STRUCT
:
825 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
826 for_rust_enum
, podata
);
833 gdb_puts ("enum ", stream
);
834 if (type
->name () != NULL
)
836 gdb_puts (type
->name (), stream
);
837 gdb_puts (" ", stream
);
838 len
= strlen (type
->name ());
840 gdb_puts ("{\n", stream
);
842 for (int i
= 0; i
< type
->num_fields (); ++i
)
844 const char *name
= type
->field (i
).name ();
849 && strncmp (name
, type
->name (), len
) == 0
851 && name
[len
+ 1] == ':')
853 gdb_printf (stream
, "%*s%ps,\n",
855 styled_string (variable_name_style
.style (),
859 gdb_puts ("}", stream
);
865 if (type
->name () != nullptr)
866 gdb_puts (type
->name (), stream
);
869 /* We currently can't distinguish between pointers and
871 gdb_puts ("*mut ", stream
);
872 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
879 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
885 /* Like arch_composite_type, but uses TYPE to decide how to allocate
886 -- either on an obstack or on a gdbarch. */
889 rust_composite_type (struct type
*original
,
891 const char *field1
, struct type
*type1
,
892 const char *field2
, struct type
*type2
)
894 struct type
*result
= alloc_type_copy (original
);
895 int i
, nfields
, bitpos
;
903 result
->set_code (TYPE_CODE_STRUCT
);
904 result
->set_name (name
);
906 result
->set_num_fields (nfields
);
908 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
914 struct field
*field
= &result
->field (i
);
916 field
->set_loc_bitpos (bitpos
);
917 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
919 field
->set_name (field1
);
920 field
->set_type (type1
);
925 struct field
*field
= &result
->field (i
);
926 unsigned align
= type_align (type2
);
932 align
*= TARGET_CHAR_BIT
;
933 delta
= bitpos
% align
;
935 bitpos
+= align
- delta
;
937 field
->set_loc_bitpos (bitpos
);
939 field
->set_name (field2
);
940 field
->set_type (type2
);
946 = (result
->field (i
- 1).loc_bitpos () / TARGET_CHAR_BIT
+
947 TYPE_LENGTH (result
->field (i
- 1).type ()));
951 /* See rust-lang.h. */
954 rust_slice_type (const char *name
, struct type
*elt_type
,
955 struct type
*usize_type
)
959 elt_type
= lookup_pointer_type (elt_type
);
960 type
= rust_composite_type (elt_type
, name
,
961 "data_ptr", elt_type
,
962 "length", usize_type
);
969 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
972 rust_range (struct type
*expect_type
, struct expression
*exp
,
973 enum noside noside
, enum range_flag kind
,
974 struct value
*low
, struct value
*high
)
976 struct value
*addrval
, *result
;
978 struct type
*range_type
;
979 struct type
*index_type
;
980 struct type
*temp_type
;
983 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
990 name
= "std::ops::RangeFull";
994 index_type
= value_type (high
);
996 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1003 index_type
= value_type (low
);
1004 name
= "std::ops::RangeFrom";
1008 if (!types_equal (value_type (low
), value_type (high
)))
1009 error (_("Range expression with different types"));
1010 index_type
= value_type (low
);
1011 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1015 /* If we don't have an index type, just allocate this on the
1016 arch. Here any type will do. */
1017 temp_type
= (index_type
== NULL
1018 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1020 /* It would be nicer to cache the range type. */
1021 range_type
= rust_composite_type (temp_type
, name
,
1022 low
== NULL
? NULL
: "start", index_type
,
1023 high
== NULL
? NULL
: "end", index_type
);
1025 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1026 return value_zero (range_type
, lval_memory
);
1028 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1029 addr
= value_as_long (addrval
);
1030 result
= value_at_lazy (range_type
, addr
);
1034 struct value
*start
= value_struct_elt (&result
, {}, "start", NULL
,
1037 value_assign (start
, low
);
1042 struct value
*end
= value_struct_elt (&result
, {}, "end", NULL
,
1045 value_assign (end
, high
);
1048 result
= value_at_lazy (range_type
, addr
);
1052 /* A helper function to compute the range and kind given a range
1053 value. TYPE is the type of the range value. RANGE is the range
1054 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1055 parameters might be filled in, or might not be, depending on the
1056 kind of range this is. KIND will always be set to the appropriate
1057 value describing the kind of range, and this can be used to
1058 determine whether LOW or HIGH are valid. */
1061 rust_compute_range (struct type
*type
, struct value
*range
,
1062 LONGEST
*low
, LONGEST
*high
,
1069 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1071 if (type
->num_fields () == 0)
1075 if (strcmp (type
->field (0).name (), "start") == 0)
1077 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1078 *low
= value_as_long (value_field (range
, 0));
1081 if (type
->num_fields () > i
1082 && strcmp (type
->field (i
).name (), "end") == 0)
1084 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1085 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1086 *high
= value_as_long (value_field (range
, i
));
1088 if (rust_inclusive_range_type_p (type
))
1093 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1096 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1097 enum noside noside
, bool for_addr
,
1098 struct value
*lhs
, struct value
*rhs
)
1100 struct value
*result
;
1101 struct type
*rhstype
;
1102 LONGEST low
, high_bound
;
1103 /* Initialized to appease the compiler. */
1104 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1108 rhstype
= check_typedef (value_type (rhs
));
1109 if (rust_range_type_p (rhstype
))
1112 error (_("Can't take slice of array without '&'"));
1113 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1117 low
= value_as_long (rhs
);
1119 struct type
*type
= check_typedef (value_type (lhs
));
1120 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1122 struct type
*base_type
= nullptr;
1123 if (type
->code () == TYPE_CODE_ARRAY
)
1124 base_type
= TYPE_TARGET_TYPE (type
);
1125 else if (rust_slice_type_p (type
))
1127 for (int i
= 0; i
< type
->num_fields (); ++i
)
1129 if (strcmp (type
->field (i
).name (), "data_ptr") == 0)
1131 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1135 if (base_type
== nullptr)
1136 error (_("Could not find 'data_ptr' in slice type"));
1138 else if (type
->code () == TYPE_CODE_PTR
)
1139 base_type
= TYPE_TARGET_TYPE (type
);
1141 error (_("Cannot subscript non-array type"));
1143 struct type
*new_type
;
1146 if (rust_slice_type_p (type
))
1151 = language_lookup_primitive_type (exp
->language_defn
,
1154 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1158 new_type
= base_type
;
1160 return value_zero (new_type
, VALUE_LVAL (lhs
));
1167 if (type
->code () == TYPE_CODE_ARRAY
)
1170 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1171 error (_("Can't compute array bounds"));
1173 error (_("Found array with non-zero lower bound"));
1176 else if (rust_slice_type_p (type
))
1180 base
= value_struct_elt (&lhs
, {}, "data_ptr", NULL
, "slice");
1181 len
= value_struct_elt (&lhs
, {}, "length", NULL
, "slice");
1183 high_bound
= value_as_long (len
);
1185 else if (type
->code () == TYPE_CODE_PTR
)
1189 high_bound
= LONGEST_MAX
;
1192 error (_("Cannot subscript non-array type"));
1194 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1197 error (_("Index less than zero"));
1198 if (low
> high_bound
)
1199 error (_("Index greater than length"));
1201 result
= value_subscript (base
, low
);
1208 struct type
*usize
, *slice
;
1210 struct value
*addrval
, *tem
;
1212 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1215 error (_("High index less than zero"));
1217 error (_("Low index greater than high index"));
1218 if (high
> high_bound
)
1219 error (_("High index greater than length"));
1221 usize
= language_lookup_primitive_type (exp
->language_defn
,
1224 const char *new_name
= ((type
!= nullptr
1225 && rust_slice_type_p (type
))
1226 ? type
->name () : "&[*gdb*]");
1228 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1230 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1231 addr
= value_as_long (addrval
);
1232 tem
= value_at_lazy (slice
, addr
);
1234 value_assign (value_field (tem
, 0), value_addr (result
));
1235 value_assign (value_field (tem
, 1),
1236 value_from_longest (usize
, high
- low
));
1238 result
= value_at_lazy (slice
, addr
);
1241 result
= value_addr (result
);
1251 rust_unop_ind_operation::evaluate (struct type
*expect_type
,
1252 struct expression
*exp
,
1255 if (noside
!= EVAL_NORMAL
)
1256 return unop_ind_operation::evaluate (expect_type
, exp
, noside
);
1258 struct value
*value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1260 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1261 if (trait_ptr
!= NULL
)
1264 return value_ind (value
);
1267 } /* namespace expr */
1269 /* A helper function for UNOP_COMPLEMENT. */
1272 eval_op_rust_complement (struct type
*expect_type
, struct expression
*exp
,
1274 enum exp_opcode opcode
,
1275 struct value
*value
)
1277 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1278 return value_from_longest (value_type (value
), value_logical_not (value
));
1279 return value_complement (value
);
1282 /* A helper function for OP_ARRAY. */
1285 eval_op_rust_array (struct type
*expect_type
, struct expression
*exp
,
1287 enum exp_opcode opcode
,
1288 struct value
*elt
, struct value
*ncopies
)
1290 int copies
= value_as_long (ncopies
);
1292 error (_("Array with negative number of elements"));
1294 if (noside
== EVAL_NORMAL
)
1297 std::vector
<struct value
*> eltvec (copies
);
1299 for (i
= 0; i
< copies
; ++i
)
1301 return value_array (0, copies
- 1, eltvec
.data ());
1305 struct type
*arraytype
1306 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1307 return allocate_value (arraytype
);
1315 rust_struct_anon::evaluate (struct type
*expect_type
,
1316 struct expression
*exp
,
1319 value
*lhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
1320 int field_number
= std::get
<0> (m_storage
);
1322 struct type
*type
= value_type (lhs
);
1324 if (type
->code () == TYPE_CODE_STRUCT
)
1326 struct type
*outer_type
= NULL
;
1328 if (rust_enum_p (type
))
1330 type
= resolve_dynamic_type (type
, value_contents (lhs
),
1331 value_address (lhs
));
1333 if (rust_empty_enum_p (type
))
1334 error (_("Cannot access field %d of empty enum %s"),
1335 field_number
, type
->name ());
1337 int fieldno
= rust_enum_variant (type
);
1338 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1340 type
= value_type (lhs
);
1343 /* Tuples and tuple structs */
1344 int nfields
= type
->num_fields ();
1346 if (field_number
>= nfields
|| field_number
< 0)
1348 if (outer_type
!= NULL
)
1349 error(_("Cannot access field %d of variant %s::%s, "
1350 "there are only %d fields"),
1351 field_number
, outer_type
->name (),
1352 rust_last_path_segment (type
->name ()),
1355 error(_("Cannot access field %d of %s, "
1356 "there are only %d fields"),
1357 field_number
, type
->name (), nfields
);
1360 /* Tuples are tuple structs too. */
1361 if (!rust_tuple_struct_type_p (type
))
1363 if (outer_type
!= NULL
)
1364 error(_("Variant %s::%s is not a tuple variant"),
1365 outer_type
->name (),
1366 rust_last_path_segment (type
->name ()));
1368 error(_("Attempting to access anonymous field %d "
1369 "of %s, which is not a tuple, tuple struct, or "
1370 "tuple-like variant"),
1371 field_number
, type
->name ());
1374 return value_primitive_field (lhs
, 0, field_number
, type
);
1377 error(_("Anonymous field access is only allowed on tuples, \
1378 tuple structs, and tuple-like enum variants"));
1382 rust_structop::evaluate (struct type
*expect_type
,
1383 struct expression
*exp
,
1386 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1387 const char *field_name
= std::get
<1> (m_storage
).c_str ();
1389 struct value
*result
;
1390 struct type
*type
= value_type (lhs
);
1391 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1393 type
= resolve_dynamic_type (type
, value_contents (lhs
),
1394 value_address (lhs
));
1396 if (rust_empty_enum_p (type
))
1397 error (_("Cannot access field %s of empty enum %s"),
1398 field_name
, type
->name ());
1400 int fieldno
= rust_enum_variant (type
);
1401 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1403 struct type
*outer_type
= type
;
1404 type
= value_type (lhs
);
1405 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1406 error (_("Attempting to access named field %s of tuple "
1407 "variant %s::%s, which has only anonymous fields"),
1408 field_name
, outer_type
->name (),
1409 rust_last_path_segment (type
->name ()));
1413 result
= value_struct_elt (&lhs
, {}, field_name
,
1416 catch (const gdb_exception_error
&except
)
1418 error (_("Could not find field %s of struct variant %s::%s"),
1419 field_name
, outer_type
->name (),
1420 rust_last_path_segment (type
->name ()));
1424 result
= value_struct_elt (&lhs
, {}, field_name
, NULL
, "structure");
1425 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1426 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1431 rust_aggregate_operation::evaluate (struct type
*expect_type
,
1432 struct expression
*exp
,
1435 struct type
*type
= std::get
<0> (m_storage
);
1437 struct value
*addrval
= NULL
;
1440 if (noside
== EVAL_NORMAL
)
1442 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1443 addr
= value_as_long (addrval
);
1444 result
= value_at_lazy (type
, addr
);
1447 if (std::get
<1> (m_storage
) != nullptr)
1449 struct value
*init
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1452 if (noside
== EVAL_NORMAL
)
1454 /* This isn't quite right but will do for the time
1455 being, seeing that we can't implement the Copy
1457 value_assign (result
, init
);
1461 for (const auto &item
: std::get
<2> (m_storage
))
1463 value
*val
= item
.second
->evaluate (nullptr, exp
, noside
);
1464 if (noside
== EVAL_NORMAL
)
1466 const char *fieldname
= item
.first
.c_str ();
1467 value
*field
= value_struct_elt (&result
, {}, fieldname
,
1468 nullptr, "structure");
1469 value_assign (field
, val
);
1473 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1474 result
= allocate_value (type
);
1476 result
= value_at_lazy (type
, addr
);
1482 rust_structop::evaluate_funcall (struct type
*expect_type
,
1483 struct expression
*exp
,
1485 const std::vector
<operation_up
> &ops
)
1487 std::vector
<struct value
*> args (ops
.size () + 1);
1489 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1490 type in order to look up the method. */
1491 args
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1492 /* We don't yet implement real Deref semantics. */
1493 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1494 args
[0] = value_ind (args
[0]);
1496 struct type
*type
= value_type (args
[0]);
1497 if ((type
->code () != TYPE_CODE_STRUCT
1498 && type
->code () != TYPE_CODE_UNION
1499 && type
->code () != TYPE_CODE_ENUM
)
1500 || rust_tuple_type_p (type
))
1501 error (_("Method calls only supported on struct or enum types"));
1502 if (type
->name () == NULL
)
1503 error (_("Method call on nameless type"));
1505 std::string name
= (std::string (type
->name ()) + "::"
1506 + std::get
<1> (m_storage
));
1508 const struct block
*block
= get_selected_block (0);
1509 struct block_symbol sym
= lookup_symbol (name
.c_str (), block
,
1511 if (sym
.symbol
== NULL
)
1512 error (_("Could not find function named '%s'"), name
.c_str ());
1514 struct type
*fn_type
= sym
.symbol
->type ();
1515 if (fn_type
->num_fields () == 0)
1516 error (_("Function '%s' takes no arguments"), name
.c_str ());
1518 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1519 args
[0] = value_addr (args
[0]);
1521 value
*function
= address_of_variable (sym
.symbol
, block
);
1523 for (int i
= 0; i
< ops
.size (); ++i
)
1524 args
[i
+ 1] = ops
[i
]->evaluate (nullptr, exp
, noside
);
1526 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1527 return value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1528 return call_function_by_hand (function
, NULL
, args
);
1535 /* See language.h. */
1538 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1539 struct language_arch_info
*lai
) const
1541 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1543 /* Helper function to allow shorter lines below. */
1544 auto add
= [&] (struct type
* t
) -> struct type
*
1546 lai
->add_primitive_type (t
);
1550 struct type
*bool_type
1551 = add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1552 add (arch_character_type (gdbarch
, 32, 1, "char"));
1553 add (arch_integer_type (gdbarch
, 8, 0, "i8"));
1554 struct type
*u8_type
1555 = add (arch_integer_type (gdbarch
, 8, 1, "u8"));
1556 add (arch_integer_type (gdbarch
, 16, 0, "i16"));
1557 add (arch_integer_type (gdbarch
, 16, 1, "u16"));
1558 add (arch_integer_type (gdbarch
, 32, 0, "i32"));
1559 add (arch_integer_type (gdbarch
, 32, 1, "u32"));
1560 add (arch_integer_type (gdbarch
, 64, 0, "i64"));
1561 add (arch_integer_type (gdbarch
, 64, 1, "u64"));
1563 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1564 add (arch_integer_type (gdbarch
, length
, 0, "isize"));
1565 struct type
*usize_type
1566 = add (arch_integer_type (gdbarch
, length
, 1, "usize"));
1568 add (arch_float_type (gdbarch
, 32, "f32", floatformats_ieee_single
));
1569 add (arch_float_type (gdbarch
, 64, "f64", floatformats_ieee_double
));
1570 add (arch_integer_type (gdbarch
, 0, 1, "()"));
1572 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1573 add (rust_slice_type ("&str", tem
, usize_type
));
1575 lai
->set_bool_type (bool_type
);
1576 lai
->set_string_char_type (u8_type
);
1579 /* See language.h. */
1582 rust_language::print_type (struct type
*type
, const char *varstring
,
1583 struct ui_file
*stream
, int show
, int level
,
1584 const struct type_print_options
*flags
) const
1586 print_offset_data
podata (flags
);
1587 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1588 flags
, false, &podata
);
1591 /* See language.h. */
1594 rust_language::emitchar (int ch
, struct type
*chtype
,
1595 struct ui_file
*stream
, int quoter
) const
1597 if (!rust_chartype_p (chtype
))
1598 generic_emit_char (ch
, chtype
, stream
, quoter
,
1599 target_charset (chtype
->arch ()));
1600 else if (ch
== '\\' || ch
== quoter
)
1601 gdb_printf (stream
, "\\%c", ch
);
1602 else if (ch
== '\n')
1603 gdb_puts ("\\n", stream
);
1604 else if (ch
== '\r')
1605 gdb_puts ("\\r", stream
);
1606 else if (ch
== '\t')
1607 gdb_puts ("\\t", stream
);
1608 else if (ch
== '\0')
1609 gdb_puts ("\\0", stream
);
1610 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1611 gdb_putc (ch
, stream
);
1613 gdb_printf (stream
, "\\x%02x", ch
);
1615 gdb_printf (stream
, "\\u{%06x}", ch
);
1618 /* See language.h. */
1621 rust_language::is_string_type_p (struct type
*type
) const
1623 LONGEST low_bound
, high_bound
;
1625 type
= check_typedef (type
);
1626 return ((type
->code () == TYPE_CODE_STRING
)
1627 || (type
->code () == TYPE_CODE_PTR
1628 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
1629 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
1630 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
1632 || (type
->code () == TYPE_CODE_STRUCT
1633 && !rust_enum_p (type
)
1634 && rust_slice_type_p (type
)
1635 && strcmp (type
->name (), "&str") == 0));
1638 /* Single instance of the Rust language class. */
1640 static rust_language rust_language_defn
;