1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2021 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_NAME (type
, i
)) != 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_NAME (type
, 0), "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_NAME (type
, i
), "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_NAME (type
, i
), "vtable") == 0)
249 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "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
, NULL
, "data_ptr", NULL
,
307 struct value
*len
= value_struct_elt (&val
, NULL
, "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 fprintf_filtered (stream
, "%s", type
->name ());
348 if (type
->num_fields () == 0)
351 if (type
->name () != NULL
)
352 fputs_filtered (" ", stream
);
355 if (is_tuple
|| is_tuple_struct
)
356 fputs_filtered ("(", stream
);
358 fputs_filtered ("{", stream
);
364 for (i
= 0; i
< type
->num_fields (); ++i
)
366 if (field_is_static (&type
->field (i
)))
370 fputs_filtered (",", stream
);
372 if (options
->prettyformat
)
374 fputs_filtered ("\n", stream
);
375 print_spaces_filtered (2 + 2 * recurse
, stream
);
377 else if (!first_field
)
378 fputs_filtered (" ", stream
);
382 if (!is_tuple
&& !is_tuple_struct
)
384 fputs_styled (TYPE_FIELD_NAME (type
, i
),
385 variable_name_style
.style (), stream
);
386 fputs_filtered (": ", stream
);
389 value_print_inner (value_field (val
, i
), stream
, recurse
+ 1, &opts
);
392 if (options
->prettyformat
)
394 fputs_filtered ("\n", stream
);
395 print_spaces_filtered (2 * recurse
, stream
);
398 if (is_tuple
|| is_tuple_struct
)
399 fputs_filtered (")", stream
);
401 fputs_filtered ("}", stream
);
404 /* See rust-lang.h. */
407 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
409 const struct value_print_options
*options
) const
411 struct value_print_options opts
= *options
;
412 struct type
*type
= check_typedef (value_type (val
));
416 gdb_assert (rust_enum_p (type
));
417 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
418 TYPE_LENGTH (value_type (val
)));
419 type
= resolve_dynamic_type (type
, view
, value_address (val
));
421 if (rust_empty_enum_p (type
))
423 /* Print the enum type name here to be more clear. */
424 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
426 metadata_style
.style ().ptr (), nullptr);
430 int variant_fieldno
= rust_enum_variant (type
);
431 val
= value_field (val
, variant_fieldno
);
432 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
434 int nfields
= variant_type
->num_fields ();
436 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
438 fprintf_filtered (stream
, "%s", variant_type
->name ());
441 /* In case of a nullary variant like 'None', just output
446 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
448 fprintf_filtered (stream
, "(");
451 /* struct variant. */
452 fprintf_filtered (stream
, "{");
455 bool first_field
= true;
456 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
459 fputs_filtered (", ", stream
);
463 fprintf_filtered (stream
, "%ps: ",
464 styled_string (variable_name_style
.style (),
465 TYPE_FIELD_NAME (variant_type
, j
)));
467 value_print_inner (value_field (val
, j
), stream
, recurse
+ 1, &opts
);
471 fputs_filtered (")", stream
);
473 fputs_filtered ("}", stream
);
476 static const struct generic_val_print_decorations rust_decorations
=
478 /* Complex isn't used in Rust, but we provide C-ish values just in
490 /* See language.h. */
493 rust_language::value_print_inner
494 (struct value
*val
, struct ui_file
*stream
, int recurse
,
495 const struct value_print_options
*options
) const
497 struct value_print_options opts
= *options
;
500 if (opts
.prettyformat
== Val_prettyformat_default
)
501 opts
.prettyformat
= (opts
.prettyformat_structs
502 ? Val_prettyformat
: Val_no_prettyformat
);
504 struct type
*type
= check_typedef (value_type (val
));
505 switch (type
->code ())
509 LONGEST low_bound
, high_bound
;
511 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
512 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
513 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
516 /* We have a pointer to a byte string, so just print
518 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
519 CORE_ADDR addr
= value_as_address (val
);
520 struct gdbarch
*arch
= type
->arch ();
522 if (opts
.addressprint
)
524 fputs_filtered (paddress (arch
, addr
), stream
);
525 fputs_filtered (" ", stream
);
528 fputs_filtered ("b", stream
);
529 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
530 high_bound
- low_bound
+ 1, stream
,
538 /* Recognize the unit type. */
539 if (type
->is_unsigned () && TYPE_LENGTH (type
) == 0
540 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
542 fputs_filtered ("()", stream
);
547 case TYPE_CODE_STRING
:
549 LONGEST low_bound
, high_bound
;
551 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
552 error (_("Could not determine the array bounds"));
554 /* If we see a plain TYPE_CODE_STRING, then we're printing a
555 byte string, hence the choice of "ASCII" as the
557 fputs_filtered ("b", stream
);
558 printstr (stream
, TYPE_TARGET_TYPE (type
),
559 value_contents_for_printing (val
),
560 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
564 case TYPE_CODE_ARRAY
:
566 LONGEST low_bound
, high_bound
;
568 if (get_array_bounds (type
, &low_bound
, &high_bound
)
569 && high_bound
- low_bound
+ 1 == 0)
570 fputs_filtered ("[]", stream
);
576 case TYPE_CODE_UNION
:
577 /* Untagged unions are printed as if they are structs. Since
578 the field bit positions overlap in the debuginfo, the code
579 for printing a union is same as that for a struct, the only
580 difference is that the input type will have overlapping
582 val_print_struct (val
, stream
, recurse
, &opts
);
585 case TYPE_CODE_STRUCT
:
586 if (rust_enum_p (type
))
587 print_enum (val
, stream
, recurse
, &opts
);
589 val_print_struct (val
, stream
, recurse
, &opts
);
594 /* Nothing special yet. */
595 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
602 rust_internal_print_type (struct type
*type
, const char *varstring
,
603 struct ui_file
*stream
, int show
, int level
,
604 const struct type_print_options
*flags
,
605 bool for_rust_enum
, print_offset_data
*podata
);
607 /* Print a struct or union typedef. */
609 rust_print_struct_def (struct type
*type
, const char *varstring
,
610 struct ui_file
*stream
, int show
, int level
,
611 const struct type_print_options
*flags
,
612 bool for_rust_enum
, print_offset_data
*podata
)
614 /* Print a tuple type simply. */
615 if (rust_tuple_type_p (type
))
617 fputs_filtered (type
->name (), stream
);
621 /* If we see a base class, delegate to C. */
622 if (TYPE_N_BASECLASSES (type
) > 0)
623 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
625 if (flags
->print_offsets
)
627 /* Temporarily bump the level so that the output lines up
632 /* Compute properties of TYPE here because, in the enum case, the
633 rest of the code ends up looking only at the variant part. */
634 const char *tagname
= type
->name ();
635 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
636 bool is_tuple
= rust_tuple_type_p (type
);
637 bool is_enum
= rust_enum_p (type
);
641 /* Already printing an outer enum, so nothing to print here. */
645 /* This code path is also used by unions and enums. */
648 fputs_filtered ("enum ", stream
);
649 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
650 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
651 type
= prop
->original_type ();
653 else if (type
->code () == TYPE_CODE_STRUCT
)
654 fputs_filtered ("struct ", stream
);
656 fputs_filtered ("union ", stream
);
659 fputs_filtered (tagname
, stream
);
662 if (type
->num_fields () == 0 && !is_tuple
)
664 if (for_rust_enum
&& !flags
->print_offsets
)
665 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
667 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
669 /* When printing offsets, we rearrange the fields into storage
670 order. This lets us show holes more clearly. We work using
671 field indices here because it simplifies calls to
672 print_offset_data::update below. */
673 std::vector
<int> fields
;
674 for (int i
= 0; i
< type
->num_fields (); ++i
)
676 if (field_is_static (&type
->field (i
)))
678 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
680 fields
.push_back (i
);
682 if (flags
->print_offsets
)
683 std::sort (fields
.begin (), fields
.end (),
686 return (TYPE_FIELD_BITPOS (type
, a
)
687 < TYPE_FIELD_BITPOS (type
, b
));
694 gdb_assert (!field_is_static (&type
->field (i
)));
695 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
697 if (flags
->print_offsets
)
698 podata
->update (type
, i
, stream
);
700 /* We'd like to print "pub" here as needed, but rustc
701 doesn't emit the debuginfo, and our types don't have
702 cplus_struct_type attached. */
704 /* For a tuple struct we print the type but nothing
706 if (!for_rust_enum
|| flags
->print_offsets
)
707 print_spaces_filtered (level
+ 2, stream
);
709 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
711 else if (!is_tuple_struct
)
712 fprintf_filtered (stream
, "%ps: ",
713 styled_string (variable_name_style
.style (),
714 TYPE_FIELD_NAME (type
, i
)));
716 rust_internal_print_type (type
->field (i
).type (), NULL
,
717 stream
, (is_enum
? show
: show
- 1),
718 level
+ 2, flags
, is_enum
, podata
);
719 if (!for_rust_enum
|| flags
->print_offsets
)
720 fputs_filtered (",\n", stream
);
721 /* Note that this check of "I" is ok because we only sorted the
722 fields by offset when print_offsets was set, so we won't take
723 this branch in that case. */
724 else if (i
+ 1 < type
->num_fields ())
725 fputs_filtered (", ", stream
);
728 if (flags
->print_offsets
)
730 /* Undo the temporary level increase we did above. */
732 podata
->finish (type
, level
, stream
);
733 print_spaces_filtered (print_offset_data::indentation
, stream
);
735 print_spaces_filtered (2, stream
);
737 if (!for_rust_enum
|| flags
->print_offsets
)
738 print_spaces_filtered (level
, stream
);
739 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
742 /* la_print_type implementation for Rust. */
745 rust_internal_print_type (struct type
*type
, const char *varstring
,
746 struct ui_file
*stream
, int show
, int level
,
747 const struct type_print_options
*flags
,
748 bool for_rust_enum
, print_offset_data
*podata
)
752 && type
->name () != NULL
)
754 /* Rust calls the unit type "void" in its debuginfo,
755 but we don't want to print it as that. */
756 if (type
->code () == TYPE_CODE_VOID
)
757 fputs_filtered ("()", stream
);
759 fputs_filtered (type
->name (), stream
);
763 type
= check_typedef (type
);
764 switch (type
->code ())
767 /* If we have an enum, we've already printed the type's
768 unqualified name, and there is nothing else to print
771 fputs_filtered ("()", stream
);
775 /* Delegate varargs to the C printer. */
776 if (type
->has_varargs ())
779 fputs_filtered ("fn ", stream
);
780 if (varstring
!= NULL
)
781 fputs_filtered (varstring
, stream
);
782 fputs_filtered ("(", stream
);
783 for (int i
= 0; i
< type
->num_fields (); ++i
)
787 fputs_filtered (", ", stream
);
788 rust_internal_print_type (type
->field (i
).type (), "", stream
,
789 -1, 0, flags
, false, podata
);
791 fputs_filtered (")", stream
);
792 /* If it returns unit, we can omit the return type. */
793 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
795 fputs_filtered (" -> ", stream
);
796 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
797 -1, 0, flags
, false, podata
);
801 case TYPE_CODE_ARRAY
:
803 LONGEST low_bound
, high_bound
;
805 fputs_filtered ("[", stream
);
806 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
807 stream
, show
- 1, level
, flags
, false,
810 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
811 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
812 fprintf_filtered (stream
, "; variable length");
813 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
814 fprintf_filtered (stream
, "; %s",
815 plongest (high_bound
- low_bound
+ 1));
816 fputs_filtered ("]", stream
);
820 case TYPE_CODE_UNION
:
821 case TYPE_CODE_STRUCT
:
822 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
823 for_rust_enum
, podata
);
830 fputs_filtered ("enum ", stream
);
831 if (type
->name () != NULL
)
833 fputs_filtered (type
->name (), stream
);
834 fputs_filtered (" ", stream
);
835 len
= strlen (type
->name ());
837 fputs_filtered ("{\n", stream
);
839 for (int i
= 0; i
< type
->num_fields (); ++i
)
841 const char *name
= TYPE_FIELD_NAME (type
, i
);
846 && strncmp (name
, type
->name (), len
) == 0
848 && name
[len
+ 1] == ':')
850 fprintf_filtered (stream
, "%*s%ps,\n",
852 styled_string (variable_name_style
.style (),
856 fputs_filtered ("}", stream
);
862 if (type
->name () != nullptr)
863 fputs_filtered (type
->name (), stream
);
866 /* We currently can't distinguish between pointers and
868 fputs_filtered ("*mut ", stream
);
869 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
876 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
882 /* Like arch_composite_type, but uses TYPE to decide how to allocate
883 -- either on an obstack or on a gdbarch. */
886 rust_composite_type (struct type
*original
,
888 const char *field1
, struct type
*type1
,
889 const char *field2
, struct type
*type2
)
891 struct type
*result
= alloc_type_copy (original
);
892 int i
, nfields
, bitpos
;
900 result
->set_code (TYPE_CODE_STRUCT
);
901 result
->set_name (name
);
903 result
->set_num_fields (nfields
);
905 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
911 struct field
*field
= &result
->field (i
);
913 SET_FIELD_BITPOS (*field
, bitpos
);
914 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
916 FIELD_NAME (*field
) = field1
;
917 field
->set_type (type1
);
922 struct field
*field
= &result
->field (i
);
923 unsigned align
= type_align (type2
);
929 align
*= TARGET_CHAR_BIT
;
930 delta
= bitpos
% align
;
932 bitpos
+= align
- delta
;
934 SET_FIELD_BITPOS (*field
, bitpos
);
936 FIELD_NAME (*field
) = field2
;
937 field
->set_type (type2
);
943 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
944 TYPE_LENGTH (result
->field (i
- 1).type ()));
948 /* See rust-lang.h. */
951 rust_slice_type (const char *name
, struct type
*elt_type
,
952 struct type
*usize_type
)
956 elt_type
= lookup_pointer_type (elt_type
);
957 type
= rust_composite_type (elt_type
, name
,
958 "data_ptr", elt_type
,
959 "length", usize_type
);
966 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
968 static struct value
*
969 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
972 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
974 struct value
*function
, *result
, *arg0
;
975 struct type
*type
, *fn_type
;
976 const struct block
*block
;
977 struct block_symbol sym
;
979 /* For an ordinary function call we can simply defer to the
980 generic implementation. */
981 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
982 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
984 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
986 method
= &exp
->elts
[*pos
+ 1].string
;
987 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
989 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
990 type in order to look up the method. */
991 arg0
= evaluate_subexp (nullptr, exp
, pos
, noside
);
993 if (noside
== EVAL_SKIP
)
995 for (i
= 0; i
< num_args
; ++i
)
996 evaluate_subexp (nullptr, exp
, pos
, noside
);
1000 std::vector
<struct value
*> args (num_args
+ 1);
1003 /* We don't yet implement real Deref semantics. */
1004 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1005 args
[0] = value_ind (args
[0]);
1007 type
= value_type (args
[0]);
1008 if ((type
->code () != TYPE_CODE_STRUCT
1009 && type
->code () != TYPE_CODE_UNION
1010 && type
->code () != TYPE_CODE_ENUM
)
1011 || rust_tuple_type_p (type
))
1012 error (_("Method calls only supported on struct or enum types"));
1013 if (type
->name () == NULL
)
1014 error (_("Method call on nameless type"));
1016 std::string name
= std::string (type
->name ()) + "::" + method
;
1018 block
= get_selected_block (0);
1019 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1020 if (sym
.symbol
== NULL
)
1021 error (_("Could not find function named '%s'"), name
.c_str ());
1023 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1024 if (fn_type
->num_fields () == 0)
1025 error (_("Function '%s' takes no arguments"), name
.c_str ());
1027 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1028 args
[0] = value_addr (args
[0]);
1030 function
= address_of_variable (sym
.symbol
, block
);
1032 for (i
= 0; i
< num_args
; ++i
)
1033 args
[i
+ 1] = evaluate_subexp (nullptr, exp
, pos
, noside
);
1035 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1036 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1038 result
= call_function_by_hand (function
, NULL
, args
);
1042 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1045 rust_range (struct type
*expect_type
, struct expression
*exp
,
1046 enum noside noside
, enum range_flag kind
,
1047 struct value
*low
, struct value
*high
)
1049 struct value
*addrval
, *result
;
1051 struct type
*range_type
;
1052 struct type
*index_type
;
1053 struct type
*temp_type
;
1056 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1058 if (noside
== EVAL_SKIP
)
1059 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1066 name
= "std::ops::RangeFull";
1070 index_type
= value_type (high
);
1072 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1079 index_type
= value_type (low
);
1080 name
= "std::ops::RangeFrom";
1084 if (!types_equal (value_type (low
), value_type (high
)))
1085 error (_("Range expression with different types"));
1086 index_type
= value_type (low
);
1087 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1091 /* If we don't have an index type, just allocate this on the
1092 arch. Here any type will do. */
1093 temp_type
= (index_type
== NULL
1094 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1096 /* It would be nicer to cache the range type. */
1097 range_type
= rust_composite_type (temp_type
, name
,
1098 low
== NULL
? NULL
: "start", index_type
,
1099 high
== NULL
? NULL
: "end", index_type
);
1101 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1102 return value_zero (range_type
, lval_memory
);
1104 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1105 addr
= value_as_long (addrval
);
1106 result
= value_at_lazy (range_type
, addr
);
1110 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1113 value_assign (start
, low
);
1118 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1121 value_assign (end
, high
);
1124 result
= value_at_lazy (range_type
, addr
);
1128 /* A helper function to compute the range and kind given a range
1129 value. TYPE is the type of the range value. RANGE is the range
1130 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1131 parameters might be filled in, or might not be, depending on the
1132 kind of range this is. KIND will always be set to the appropriate
1133 value describing the kind of range, and this can be used to
1134 determine whether LOW or HIGH are valid. */
1137 rust_compute_range (struct type
*type
, struct value
*range
,
1138 LONGEST
*low
, LONGEST
*high
,
1145 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1147 if (type
->num_fields () == 0)
1151 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1153 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1154 *low
= value_as_long (value_field (range
, 0));
1157 if (type
->num_fields () > i
1158 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1160 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1161 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1162 *high
= value_as_long (value_field (range
, i
));
1164 if (rust_inclusive_range_type_p (type
))
1169 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1172 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1173 enum noside noside
, bool for_addr
,
1174 struct value
*lhs
, struct value
*rhs
)
1176 struct value
*result
;
1177 struct type
*rhstype
;
1178 LONGEST low
, high_bound
;
1179 /* Initialized to appease the compiler. */
1180 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1184 if (noside
== EVAL_SKIP
)
1187 rhstype
= check_typedef (value_type (rhs
));
1188 if (rust_range_type_p (rhstype
))
1191 error (_("Can't take slice of array without '&'"));
1192 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1196 low
= value_as_long (rhs
);
1198 struct type
*type
= check_typedef (value_type (lhs
));
1199 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1201 struct type
*base_type
= nullptr;
1202 if (type
->code () == TYPE_CODE_ARRAY
)
1203 base_type
= TYPE_TARGET_TYPE (type
);
1204 else if (rust_slice_type_p (type
))
1206 for (int i
= 0; i
< type
->num_fields (); ++i
)
1208 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1210 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1214 if (base_type
== nullptr)
1215 error (_("Could not find 'data_ptr' in slice type"));
1217 else if (type
->code () == TYPE_CODE_PTR
)
1218 base_type
= TYPE_TARGET_TYPE (type
);
1220 error (_("Cannot subscript non-array type"));
1222 struct type
*new_type
;
1225 if (rust_slice_type_p (type
))
1230 = language_lookup_primitive_type (exp
->language_defn
,
1233 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1237 new_type
= base_type
;
1239 return value_zero (new_type
, VALUE_LVAL (lhs
));
1246 if (type
->code () == TYPE_CODE_ARRAY
)
1249 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1250 error (_("Can't compute array bounds"));
1252 error (_("Found array with non-zero lower bound"));
1255 else if (rust_slice_type_p (type
))
1259 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1260 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1262 high_bound
= value_as_long (len
);
1264 else if (type
->code () == TYPE_CODE_PTR
)
1268 high_bound
= LONGEST_MAX
;
1271 error (_("Cannot subscript non-array type"));
1273 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1276 error (_("Index less than zero"));
1277 if (low
> high_bound
)
1278 error (_("Index greater than length"));
1280 result
= value_subscript (base
, low
);
1287 struct type
*usize
, *slice
;
1289 struct value
*addrval
, *tem
;
1291 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1294 error (_("High index less than zero"));
1296 error (_("Low index greater than high index"));
1297 if (high
> high_bound
)
1298 error (_("High index greater than length"));
1300 usize
= language_lookup_primitive_type (exp
->language_defn
,
1303 const char *new_name
= ((type
!= nullptr
1304 && rust_slice_type_p (type
))
1305 ? type
->name () : "&[*gdb*]");
1307 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1309 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1310 addr
= value_as_long (addrval
);
1311 tem
= value_at_lazy (slice
, addr
);
1313 value_assign (value_field (tem
, 0), value_addr (result
));
1314 value_assign (value_field (tem
, 1),
1315 value_from_longest (usize
, high
- low
));
1317 result
= value_at_lazy (slice
, addr
);
1320 result
= value_addr (result
);
1326 /* A helper function for UNOP_IND. */
1329 eval_op_rust_ind (struct type
*expect_type
, struct expression
*exp
,
1331 enum exp_opcode opcode
,
1332 struct value
*value
)
1334 gdb_assert (noside
== EVAL_NORMAL
);
1335 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1336 if (trait_ptr
!= NULL
)
1339 return value_ind (value
);
1342 /* A helper function for UNOP_COMPLEMENT. */
1345 eval_op_rust_complement (struct type
*expect_type
, struct expression
*exp
,
1347 enum exp_opcode opcode
,
1348 struct value
*value
)
1350 if (noside
== EVAL_SKIP
)
1352 /* Preserving the type is enough. */
1355 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1356 return value_from_longest (value_type (value
), value_logical_not (value
));
1357 return value_complement (value
);
1360 /* A helper function for OP_ARRAY. */
1363 eval_op_rust_array (struct type
*expect_type
, struct expression
*exp
,
1365 enum exp_opcode opcode
,
1366 struct value
*elt
, struct value
*ncopies
)
1368 int copies
= value_as_long (ncopies
);
1370 error (_("Array with negative number of elements"));
1372 if (noside
== EVAL_NORMAL
)
1375 std::vector
<struct value
*> eltvec (copies
);
1377 for (i
= 0; i
< copies
; ++i
)
1379 return value_array (0, copies
- 1, eltvec
.data ());
1383 struct type
*arraytype
1384 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1385 return allocate_value (arraytype
);
1389 /* A helper function for STRUCTOP_ANONYMOUS. */
1391 static struct value
*
1392 eval_op_rust_struct_anon (struct type
*expect_type
, struct expression
*exp
,
1394 int field_number
, struct value
*lhs
)
1396 struct type
*type
= value_type (lhs
);
1398 if (type
->code () == TYPE_CODE_STRUCT
)
1400 struct type
*outer_type
= NULL
;
1402 if (rust_enum_p (type
))
1404 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1405 TYPE_LENGTH (type
));
1406 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1408 if (rust_empty_enum_p (type
))
1409 error (_("Cannot access field %d of empty enum %s"),
1410 field_number
, type
->name ());
1412 int fieldno
= rust_enum_variant (type
);
1413 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1415 type
= value_type (lhs
);
1418 /* Tuples and tuple structs */
1419 int nfields
= type
->num_fields ();
1421 if (field_number
>= nfields
|| field_number
< 0)
1423 if (outer_type
!= NULL
)
1424 error(_("Cannot access field %d of variant %s::%s, "
1425 "there are only %d fields"),
1426 field_number
, outer_type
->name (),
1427 rust_last_path_segment (type
->name ()),
1430 error(_("Cannot access field %d of %s, "
1431 "there are only %d fields"),
1432 field_number
, type
->name (), nfields
);
1435 /* Tuples are tuple structs too. */
1436 if (!rust_tuple_struct_type_p (type
))
1438 if (outer_type
!= NULL
)
1439 error(_("Variant %s::%s is not a tuple variant"),
1440 outer_type
->name (),
1441 rust_last_path_segment (type
->name ()));
1443 error(_("Attempting to access anonymous field %d "
1444 "of %s, which is not a tuple, tuple struct, or "
1445 "tuple-like variant"),
1446 field_number
, type
->name ());
1449 return value_primitive_field (lhs
, 0, field_number
, type
);
1452 error(_("Anonymous field access is only allowed on tuples, \
1453 tuple structs, and tuple-like enum variants"));
1456 /* A helper function for STRUCTOP_STRUCT. */
1458 static struct value
*
1459 eval_op_rust_structop (struct type
*expect_type
, struct expression
*exp
,
1461 struct value
*lhs
, const char *field_name
)
1463 struct value
*result
;
1464 struct type
*type
= value_type (lhs
);
1465 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1467 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1468 TYPE_LENGTH (type
));
1469 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1471 if (rust_empty_enum_p (type
))
1472 error (_("Cannot access field %s of empty enum %s"),
1473 field_name
, type
->name ());
1475 int fieldno
= rust_enum_variant (type
);
1476 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1478 struct type
*outer_type
= type
;
1479 type
= value_type (lhs
);
1480 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1481 error (_("Attempting to access named field %s of tuple "
1482 "variant %s::%s, which has only anonymous fields"),
1483 field_name
, outer_type
->name (),
1484 rust_last_path_segment (type
->name ()));
1488 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1491 catch (const gdb_exception_error
&except
)
1493 error (_("Could not find field %s of struct variant %s::%s"),
1494 field_name
, outer_type
->name (),
1495 rust_last_path_segment (type
->name ()));
1499 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1500 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1501 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1505 /* evaluate_exp implementation for Rust. */
1507 static struct value
*
1508 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1509 int *pos
, enum noside noside
)
1511 struct value
*result
;
1512 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
1518 if (noside
!= EVAL_NORMAL
)
1519 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1523 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1525 result
= eval_op_rust_ind (expect_type
, exp
, noside
, op
, value
);
1530 case UNOP_COMPLEMENT
:
1532 struct value
*value
;
1535 value
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1536 result
= eval_op_rust_complement (expect_type
, exp
, noside
, op
, value
);
1540 case BINOP_SUBSCRIPT
:
1543 struct value
*lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1544 struct value
*rhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1545 result
= rust_subscript (expect_type
, exp
, noside
, false, lhs
, rhs
);
1550 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1556 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1557 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1560 struct value
*addrval
= NULL
;
1564 if (noside
== EVAL_NORMAL
)
1566 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1567 addr
= value_as_long (addrval
);
1568 result
= value_at_lazy (type
, addr
);
1571 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1576 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1577 if (noside
== EVAL_NORMAL
)
1579 /* This isn't quite right but will do for the time
1580 being, seeing that we can't implement the Copy
1582 value_assign (result
, init
);
1588 gdb_assert (arglen
% 2 == 0);
1589 for (i
= 0; i
< arglen
; i
+= 2)
1592 const char *fieldname
;
1593 struct value
*value
, *field
;
1595 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1597 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1599 fieldname
= &exp
->elts
[*pos
].string
;
1600 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1602 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1603 if (noside
== EVAL_NORMAL
)
1605 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1607 value_assign (field
, value
);
1611 if (noside
== EVAL_SKIP
)
1612 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1614 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1615 result
= allocate_value (type
);
1617 result
= value_at_lazy (type
, addr
);
1625 struct value
*ncopies
;
1627 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1628 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1629 return eval_op_rust_array (expect_type
, exp
, noside
, op
, elt
, ncopies
);
1633 case STRUCTOP_ANONYMOUS
:
1635 /* Anonymous field access, i.e. foo.1. */
1637 int pc
, field_number
;
1640 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1642 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1644 return eval_op_rust_struct_anon (expect_type
, exp
, noside
,
1649 case STRUCTOP_STRUCT
:
1655 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1656 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1657 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1659 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1660 return eval_op_rust_structop (expect_type
, exp
, noside
, lhs
,
1667 struct value
*low
= NULL
, *high
= NULL
;
1669 = (enum range_flag
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1672 if (!(kind
& RANGE_LOW_BOUND_DEFAULT
))
1673 low
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1674 if (!(kind
& RANGE_HIGH_BOUND_DEFAULT
))
1675 high
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1677 result
= rust_range (expect_type
, exp
, noside
, kind
, low
, high
);
1682 /* We might have &array[range], in which case we need to make a
1684 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1688 struct value
*lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1689 struct value
*rhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1691 result
= rust_subscript (expect_type
, exp
, noside
, true, lhs
, rhs
);
1696 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1703 /* operator_length implementation for Rust. */
1706 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1712 switch (exp
->elts
[pc
- 1].opcode
)
1715 /* We handle aggregate as a type and argument count. The first
1716 argument might be OP_OTHERS. After that the arguments
1717 alternate: first an OP_NAME, then an expression. */
1719 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1727 case STRUCTOP_ANONYMOUS
:
1738 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1746 /* dump_subexp_body implementation for Rust. */
1749 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1752 switch (exp
->elts
[elt
].opcode
)
1756 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1759 fprintf_filtered (stream
, "Type @");
1760 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1761 fprintf_filtered (stream
, " (");
1762 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1763 fprintf_filtered (stream
, "), length %d", length
);
1766 for (i
= 0; i
< length
; ++i
)
1767 elt
= dump_subexp (exp
, stream
, elt
);
1774 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1776 fprintf_filtered (stream
, "%s: %s",
1777 (exp
->elts
[elt
].opcode
== OP_STRING
1778 ? "string" : "name"),
1779 &exp
->elts
[elt
+ 2].string
);
1780 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1785 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1788 case STRUCTOP_ANONYMOUS
:
1792 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1794 fprintf_filtered (stream
, "Field number: %d", field_number
);
1795 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1804 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1811 /* print_subexp implementation for Rust. */
1814 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1815 enum precedence prec
)
1817 switch (exp
->elts
[*pos
].opcode
)
1821 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1824 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1825 fputs_filtered (" { ", stream
);
1828 for (i
= 0; i
< length
; ++i
)
1830 rust_print_subexp (exp
, pos
, stream
, prec
);
1831 fputs_filtered (", ", stream
);
1833 fputs_filtered (" }", stream
);
1839 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1841 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1842 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1848 fputs_filtered ("<<others>> (", stream
);
1850 rust_print_subexp (exp
, pos
, stream
, prec
);
1851 fputs_filtered (")", stream
);
1855 case STRUCTOP_ANONYMOUS
:
1857 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1860 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1861 fprintf_filtered (stream
, ".%d", tem
);
1867 fprintf_filtered (stream
, "[");
1868 rust_print_subexp (exp
, pos
, stream
, prec
);
1869 fprintf_filtered (stream
, "; ");
1870 rust_print_subexp (exp
, pos
, stream
, prec
);
1871 fprintf_filtered (stream
, "]");
1875 print_subexp_standard (exp
, pos
, stream
, prec
);
1880 /* operator_check implementation for Rust. */
1883 rust_operator_check (struct expression
*exp
, int pos
,
1884 int (*objfile_func
) (struct objfile
*objfile
,
1888 switch (exp
->elts
[pos
].opcode
)
1892 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1893 struct objfile
*objfile
= type
->objfile_owner ();
1895 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1906 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1914 const struct exp_descriptor
rust_language::exp_descriptor_tab
=
1917 rust_operator_length
,
1918 rust_operator_check
,
1919 rust_dump_subexp_body
,
1920 rust_evaluate_subexp
1923 /* See language.h. */
1926 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1927 struct language_arch_info
*lai
) const
1929 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1931 /* Helper function to allow shorter lines below. */
1932 auto add
= [&] (struct type
* t
) -> struct type
*
1934 lai
->add_primitive_type (t
);
1938 struct type
*bool_type
1939 = add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1940 add (arch_character_type (gdbarch
, 32, 1, "char"));
1941 add (arch_integer_type (gdbarch
, 8, 0, "i8"));
1942 struct type
*u8_type
1943 = add (arch_integer_type (gdbarch
, 8, 1, "u8"));
1944 add (arch_integer_type (gdbarch
, 16, 0, "i16"));
1945 add (arch_integer_type (gdbarch
, 16, 1, "u16"));
1946 add (arch_integer_type (gdbarch
, 32, 0, "i32"));
1947 add (arch_integer_type (gdbarch
, 32, 1, "u32"));
1948 add (arch_integer_type (gdbarch
, 64, 0, "i64"));
1949 add (arch_integer_type (gdbarch
, 64, 1, "u64"));
1951 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1952 add (arch_integer_type (gdbarch
, length
, 0, "isize"));
1953 struct type
*usize_type
1954 = add (arch_integer_type (gdbarch
, length
, 1, "usize"));
1956 add (arch_float_type (gdbarch
, 32, "f32", floatformats_ieee_single
));
1957 add (arch_float_type (gdbarch
, 64, "f64", floatformats_ieee_double
));
1958 add (arch_integer_type (gdbarch
, 0, 1, "()"));
1960 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1961 add (rust_slice_type ("&str", tem
, usize_type
));
1963 lai
->set_bool_type (bool_type
);
1964 lai
->set_string_char_type (u8_type
);
1967 /* See language.h. */
1970 rust_language::print_type (struct type
*type
, const char *varstring
,
1971 struct ui_file
*stream
, int show
, int level
,
1972 const struct type_print_options
*flags
) const
1974 print_offset_data podata
;
1975 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1976 flags
, false, &podata
);
1979 /* See language.h. */
1982 rust_language::emitchar (int ch
, struct type
*chtype
,
1983 struct ui_file
*stream
, int quoter
) const
1985 if (!rust_chartype_p (chtype
))
1986 generic_emit_char (ch
, chtype
, stream
, quoter
,
1987 target_charset (chtype
->arch ()));
1988 else if (ch
== '\\' || ch
== quoter
)
1989 fprintf_filtered (stream
, "\\%c", ch
);
1990 else if (ch
== '\n')
1991 fputs_filtered ("\\n", stream
);
1992 else if (ch
== '\r')
1993 fputs_filtered ("\\r", stream
);
1994 else if (ch
== '\t')
1995 fputs_filtered ("\\t", stream
);
1996 else if (ch
== '\0')
1997 fputs_filtered ("\\0", stream
);
1998 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1999 fputc_filtered (ch
, stream
);
2001 fprintf_filtered (stream
, "\\x%02x", ch
);
2003 fprintf_filtered (stream
, "\\u{%06x}", ch
);
2006 /* See language.h. */
2009 rust_language::is_string_type_p (struct type
*type
) const
2011 LONGEST low_bound
, high_bound
;
2013 type
= check_typedef (type
);
2014 return ((type
->code () == TYPE_CODE_STRING
)
2015 || (type
->code () == TYPE_CODE_PTR
2016 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
2017 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
2018 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
2020 || (type
->code () == TYPE_CODE_STRUCT
2021 && !rust_enum_p (type
)
2022 && rust_slice_type_p (type
)
2023 && strcmp (type
->name (), "&str") == 0));
2026 /* Single instance of the Rust language class. */
2028 static rust_language rust_language_defn
;