1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cp-support.h"
33 #include "rust-lang.h"
34 #include "typeprint.h"
40 #include "cli/cli-style.h"
41 #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 (block
);
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_ARTIFICIAL (type
, i
))
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 (!field_is_static (&type
->field (i
)))
136 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
137 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 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 /* Return true if TYPE is a slice type, otherwise false. */
159 rust_slice_type_p (struct type
*type
)
161 return (type
->code () == TYPE_CODE_STRUCT
162 && type
->name () != NULL
163 && (strncmp (type
->name (), "&[", 2) == 0
164 || strcmp (type
->name (), "&str") == 0));
167 /* Return true if TYPE is a range type, otherwise false. */
170 rust_range_type_p (struct type
*type
)
174 if (type
->code () != TYPE_CODE_STRUCT
175 || type
->num_fields () > 2
176 || type
->name () == NULL
177 || strstr (type
->name (), "::Range") == NULL
)
180 if (type
->num_fields () == 0)
184 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
186 if (type
->num_fields () == 1)
190 else if (type
->num_fields () == 2)
192 /* First field had to be "start". */
196 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
199 /* Return true if TYPE is an inclusive range type, otherwise false.
200 This is only valid for types which are already known to be range
204 rust_inclusive_range_type_p (struct type
*type
)
206 return (strstr (type
->name (), "::RangeInclusive") != NULL
207 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
210 /* Return true if TYPE seems to be the type "u8", otherwise false. */
213 rust_u8_type_p (struct type
*type
)
215 return (type
->code () == TYPE_CODE_INT
216 && type
->is_unsigned ()
217 && TYPE_LENGTH (type
) == 1);
220 /* Return true if TYPE is a Rust character type. */
223 rust_chartype_p (struct type
*type
)
225 return (type
->code () == TYPE_CODE_CHAR
226 && TYPE_LENGTH (type
) == 4
227 && type
->is_unsigned ());
230 /* If VALUE represents a trait object pointer, return the underlying
231 pointer with the correct (i.e., runtime) type. Otherwise, return
234 static struct value
*
235 rust_get_trait_object_pointer (struct value
*value
)
237 struct type
*type
= check_typedef (value_type (value
));
239 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
242 /* Try to be a bit resilient if the ABI changes. */
243 int vtable_field
= 0;
244 for (int i
= 0; i
< 2; ++i
)
246 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
248 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
252 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
253 struct symbol
*symbol
= find_symbol_at_address (vtable
);
254 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
257 struct rust_vtable_symbol
*vtable_sym
258 = static_cast<struct rust_vtable_symbol
*> (symbol
);
259 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
260 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
265 /* language_defn::printstr implementation for Rust. */
268 rust_printstr (struct ui_file
*stream
, struct type
*type
,
269 const gdb_byte
*string
, unsigned int length
,
270 const char *user_encoding
, int force_ellipses
,
271 const struct value_print_options
*options
)
273 /* Rust always uses UTF-8, but let the caller override this if need
275 const char *encoding
= user_encoding
;
276 if (user_encoding
== NULL
|| !*user_encoding
)
278 /* In Rust strings, characters are "u8". */
279 if (rust_u8_type_p (type
))
283 /* This is probably some C string, so let's let C deal with
285 c_printstr (stream
, type
, string
, length
, user_encoding
,
286 force_ellipses
, options
);
291 /* This is not ideal as it doesn't use our character printer. */
292 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
298 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
300 const struct value_print_options
*options
);
302 /* Helper function to print a string slice. */
305 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
306 const struct value_print_options
*options
)
308 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
310 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
312 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
313 value_as_address (base
), value_as_long (len
), stream
,
317 /* rust_val_print helper for structs and untagged unions. */
320 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
321 const struct value_print_options
*options
)
325 struct type
*type
= check_typedef (value_type (val
));
327 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
329 /* If what we are printing here is actually a string within a
330 structure then VAL will be the original parent value, while TYPE
331 will be the type of the structure representing the string we want
333 However, RUST_VAL_PRINT_STR looks up the fields of the string
334 inside VAL, assuming that VAL is the string.
335 So, recreate VAL as a value representing just the string. */
336 val
= value_at_lazy (type
, value_address (val
));
337 rust_val_print_str (stream
, val
, options
);
341 bool is_tuple
= rust_tuple_type_p (type
);
342 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
343 struct value_print_options opts
;
347 if (type
->name () != NULL
)
348 fprintf_filtered (stream
, "%s", type
->name ());
350 if (type
->num_fields () == 0)
353 if (type
->name () != NULL
)
354 fputs_filtered (" ", stream
);
357 if (is_tuple
|| is_tuple_struct
)
358 fputs_filtered ("(", stream
);
360 fputs_filtered ("{", stream
);
366 for (i
= 0; i
< type
->num_fields (); ++i
)
368 if (field_is_static (&type
->field (i
)))
372 fputs_filtered (",", stream
);
374 if (options
->prettyformat
)
376 fputs_filtered ("\n", stream
);
377 print_spaces_filtered (2 + 2 * recurse
, stream
);
379 else if (!first_field
)
380 fputs_filtered (" ", stream
);
384 if (!is_tuple
&& !is_tuple_struct
)
386 fputs_styled (TYPE_FIELD_NAME (type
, i
),
387 variable_name_style
.style (), stream
);
388 fputs_filtered (": ", stream
);
391 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
395 if (options
->prettyformat
)
397 fputs_filtered ("\n", stream
);
398 print_spaces_filtered (2 * recurse
, stream
);
401 if (is_tuple
|| is_tuple_struct
)
402 fputs_filtered (")", stream
);
404 fputs_filtered ("}", stream
);
407 /* rust_val_print helper for discriminated unions (Rust enums). */
410 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
411 const struct value_print_options
*options
)
413 struct value_print_options opts
= *options
;
414 struct type
*type
= check_typedef (value_type (val
));
418 gdb_assert (rust_enum_p (type
));
419 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
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 fprintf_filtered (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 fprintf_filtered (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 fprintf_filtered (stream
, "(");
453 /* struct variant. */
454 fprintf_filtered (stream
, "{");
457 bool first_field
= true;
458 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
461 fputs_filtered (", ", stream
);
465 fprintf_filtered (stream
, "%ps: ",
466 styled_string (variable_name_style
.style (),
467 TYPE_FIELD_NAME (variant_type
, j
)));
469 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
474 fputs_filtered (")", stream
);
476 fputs_filtered ("}", 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 /* la_value_print_inner implementation for Rust. */
495 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
497 const struct value_print_options
*options
)
499 struct value_print_options opts
= *options
;
502 if (opts
.prettyformat
== Val_prettyformat_default
)
503 opts
.prettyformat
= (opts
.prettyformat_structs
504 ? Val_prettyformat
: Val_no_prettyformat
);
506 struct type
*type
= check_typedef (value_type (val
));
507 switch (type
->code ())
511 LONGEST low_bound
, high_bound
;
513 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
514 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
515 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
518 /* We have a pointer to a byte string, so just print
520 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
521 CORE_ADDR addr
= value_as_address (val
);
522 struct gdbarch
*arch
= get_type_arch (type
);
524 if (opts
.addressprint
)
526 fputs_filtered (paddress (arch
, addr
), stream
);
527 fputs_filtered (" ", stream
);
530 fputs_filtered ("b", stream
);
531 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
532 high_bound
- low_bound
+ 1, stream
,
540 /* Recognize the unit type. */
541 if (type
->is_unsigned () && TYPE_LENGTH (type
) == 0
542 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
544 fputs_filtered ("()", stream
);
549 case TYPE_CODE_STRING
:
551 LONGEST low_bound
, high_bound
;
553 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
554 error (_("Could not determine the array bounds"));
556 /* If we see a plain TYPE_CODE_STRING, then we're printing a
557 byte string, hence the choice of "ASCII" as the
559 fputs_filtered ("b", stream
);
560 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
561 value_contents_for_printing (val
),
562 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
566 case TYPE_CODE_ARRAY
:
568 LONGEST low_bound
, high_bound
;
570 if (get_array_bounds (type
, &low_bound
, &high_bound
)
571 && high_bound
- low_bound
+ 1 == 0)
572 fputs_filtered ("[]", stream
);
578 case TYPE_CODE_UNION
:
579 /* Untagged unions are printed as if they are structs. Since
580 the field bit positions overlap in the debuginfo, the code
581 for printing a union is same as that for a struct, the only
582 difference is that the input type will have overlapping
584 val_print_struct (val
, stream
, recurse
, &opts
);
587 case TYPE_CODE_STRUCT
:
588 if (rust_enum_p (type
))
589 rust_print_enum (val
, stream
, recurse
, &opts
);
591 val_print_struct (val
, stream
, recurse
, &opts
);
596 /* Nothing special yet. */
597 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
604 rust_internal_print_type (struct type
*type
, const char *varstring
,
605 struct ui_file
*stream
, int show
, int level
,
606 const struct type_print_options
*flags
,
607 bool for_rust_enum
, print_offset_data
*podata
);
609 /* Print a struct or union typedef. */
611 rust_print_struct_def (struct type
*type
, const char *varstring
,
612 struct ui_file
*stream
, int show
, int level
,
613 const struct type_print_options
*flags
,
614 bool for_rust_enum
, print_offset_data
*podata
)
616 /* Print a tuple type simply. */
617 if (rust_tuple_type_p (type
))
619 fputs_filtered (type
->name (), stream
);
623 /* If we see a base class, delegate to C. */
624 if (TYPE_N_BASECLASSES (type
) > 0)
625 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
627 if (flags
->print_offsets
)
629 /* Temporarily bump the level so that the output lines up
634 /* Compute properties of TYPE here because, in the enum case, the
635 rest of the code ends up looking only at the variant part. */
636 const char *tagname
= type
->name ();
637 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
638 bool is_tuple
= rust_tuple_type_p (type
);
639 bool is_enum
= rust_enum_p (type
);
643 /* Already printing an outer enum, so nothing to print here. */
647 /* This code path is also used by unions and enums. */
650 fputs_filtered ("enum ", stream
);
651 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
652 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
653 type
= prop
->original_type ();
655 else if (type
->code () == TYPE_CODE_STRUCT
)
656 fputs_filtered ("struct ", stream
);
658 fputs_filtered ("union ", stream
);
661 fputs_filtered (tagname
, stream
);
664 if (type
->num_fields () == 0 && !is_tuple
)
666 if (for_rust_enum
&& !flags
->print_offsets
)
667 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
669 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
671 /* When printing offsets, we rearrange the fields into storage
672 order. This lets us show holes more clearly. We work using
673 field indices here because it simplifies calls to
674 print_offset_data::update below. */
675 std::vector
<int> fields
;
676 for (int i
= 0; i
< type
->num_fields (); ++i
)
678 if (field_is_static (&type
->field (i
)))
680 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
682 fields
.push_back (i
);
684 if (flags
->print_offsets
)
685 std::sort (fields
.begin (), fields
.end (),
688 return (TYPE_FIELD_BITPOS (type
, a
)
689 < TYPE_FIELD_BITPOS (type
, b
));
696 gdb_assert (!field_is_static (&type
->field (i
)));
697 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
699 if (flags
->print_offsets
)
700 podata
->update (type
, i
, stream
);
702 /* We'd like to print "pub" here as needed, but rustc
703 doesn't emit the debuginfo, and our types don't have
704 cplus_struct_type attached. */
706 /* For a tuple struct we print the type but nothing
708 if (!for_rust_enum
|| flags
->print_offsets
)
709 print_spaces_filtered (level
+ 2, stream
);
711 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
713 else if (!is_tuple_struct
)
714 fprintf_filtered (stream
, "%ps: ",
715 styled_string (variable_name_style
.style (),
716 TYPE_FIELD_NAME (type
, i
)));
718 rust_internal_print_type (type
->field (i
).type (), NULL
,
719 stream
, (is_enum
? show
: show
- 1),
720 level
+ 2, flags
, is_enum
, podata
);
721 if (!for_rust_enum
|| flags
->print_offsets
)
722 fputs_filtered (",\n", stream
);
723 /* Note that this check of "I" is ok because we only sorted the
724 fields by offset when print_offsets was set, so we won't take
725 this branch in that case. */
726 else if (i
+ 1 < type
->num_fields ())
727 fputs_filtered (", ", stream
);
730 if (flags
->print_offsets
)
732 /* Undo the temporary level increase we did above. */
734 podata
->finish (type
, level
, stream
);
735 print_spaces_filtered (print_offset_data::indentation
, stream
);
737 print_spaces_filtered (2, stream
);
739 if (!for_rust_enum
|| flags
->print_offsets
)
740 print_spaces_filtered (level
, stream
);
741 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
744 /* la_print_type implementation for Rust. */
747 rust_internal_print_type (struct type
*type
, const char *varstring
,
748 struct ui_file
*stream
, int show
, int level
,
749 const struct type_print_options
*flags
,
750 bool for_rust_enum
, print_offset_data
*podata
)
754 && type
->name () != NULL
)
756 /* Rust calls the unit type "void" in its debuginfo,
757 but we don't want to print it as that. */
758 if (type
->code () == TYPE_CODE_VOID
)
759 fputs_filtered ("()", stream
);
761 fputs_filtered (type
->name (), stream
);
765 type
= check_typedef (type
);
766 switch (type
->code ())
769 /* If we have an enum, we've already printed the type's
770 unqualified name, and there is nothing else to print
773 fputs_filtered ("()", stream
);
777 /* Delegate varargs to the C printer. */
778 if (type
->has_varargs ())
781 fputs_filtered ("fn ", stream
);
782 if (varstring
!= NULL
)
783 fputs_filtered (varstring
, stream
);
784 fputs_filtered ("(", stream
);
785 for (int i
= 0; i
< type
->num_fields (); ++i
)
789 fputs_filtered (", ", stream
);
790 rust_internal_print_type (type
->field (i
).type (), "", stream
,
791 -1, 0, flags
, false, podata
);
793 fputs_filtered (")", stream
);
794 /* If it returns unit, we can omit the return type. */
795 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
797 fputs_filtered (" -> ", stream
);
798 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
799 -1, 0, flags
, false, podata
);
803 case TYPE_CODE_ARRAY
:
805 LONGEST low_bound
, high_bound
;
807 fputs_filtered ("[", stream
);
808 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
809 stream
, show
- 1, level
, flags
, false,
812 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
813 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
814 fprintf_filtered (stream
, "; variable length");
815 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
816 fprintf_filtered (stream
, "; %s",
817 plongest (high_bound
- low_bound
+ 1));
818 fputs_filtered ("]", stream
);
822 case TYPE_CODE_UNION
:
823 case TYPE_CODE_STRUCT
:
824 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
825 for_rust_enum
, podata
);
832 fputs_filtered ("enum ", stream
);
833 if (type
->name () != NULL
)
835 fputs_filtered (type
->name (), stream
);
836 fputs_filtered (" ", stream
);
837 len
= strlen (type
->name ());
839 fputs_filtered ("{\n", stream
);
841 for (int i
= 0; i
< type
->num_fields (); ++i
)
843 const char *name
= TYPE_FIELD_NAME (type
, i
);
848 && strncmp (name
, type
->name (), len
) == 0
850 && name
[len
+ 1] == ':')
852 fprintf_filtered (stream
, "%*s%ps,\n",
854 styled_string (variable_name_style
.style (),
858 fputs_filtered ("}", stream
);
864 if (type
->name () != nullptr)
865 fputs_filtered (type
->name (), stream
);
868 /* We currently can't distinguish between pointers and
870 fputs_filtered ("*mut ", stream
);
871 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
878 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
884 /* Like arch_composite_type, but uses TYPE to decide how to allocate
885 -- either on an obstack or on a gdbarch. */
888 rust_composite_type (struct type
*original
,
890 const char *field1
, struct type
*type1
,
891 const char *field2
, struct type
*type2
)
893 struct type
*result
= alloc_type_copy (original
);
894 int i
, nfields
, bitpos
;
902 result
->set_code (TYPE_CODE_STRUCT
);
903 result
->set_name (name
);
905 result
->set_num_fields (nfields
);
907 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
913 struct field
*field
= &result
->field (i
);
915 SET_FIELD_BITPOS (*field
, bitpos
);
916 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
918 FIELD_NAME (*field
) = field1
;
919 field
->set_type (type1
);
924 struct field
*field
= &result
->field (i
);
925 unsigned align
= type_align (type2
);
931 align
*= TARGET_CHAR_BIT
;
932 delta
= bitpos
% align
;
934 bitpos
+= align
- delta
;
936 SET_FIELD_BITPOS (*field
, bitpos
);
938 FIELD_NAME (*field
) = field2
;
939 field
->set_type (type2
);
945 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
946 TYPE_LENGTH (result
->field (i
- 1).type ()));
950 /* See rust-lang.h. */
953 rust_slice_type (const char *name
, struct type
*elt_type
,
954 struct type
*usize_type
)
958 elt_type
= lookup_pointer_type (elt_type
);
959 type
= rust_composite_type (elt_type
, name
,
960 "data_ptr", elt_type
,
961 "length", usize_type
);
968 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
970 static struct value
*
971 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
974 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
976 struct value
*function
, *result
, *arg0
;
977 struct type
*type
, *fn_type
;
978 const struct block
*block
;
979 struct block_symbol sym
;
981 /* For an ordinary function call we can simply defer to the
982 generic implementation. */
983 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
984 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
986 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
988 method
= &exp
->elts
[*pos
+ 1].string
;
989 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
991 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
992 type in order to look up the method. */
993 arg0
= evaluate_subexp (nullptr, exp
, pos
, noside
);
995 if (noside
== EVAL_SKIP
)
997 for (i
= 0; i
< num_args
; ++i
)
998 evaluate_subexp (nullptr, exp
, pos
, noside
);
1002 std::vector
<struct value
*> args (num_args
+ 1);
1005 /* We don't yet implement real Deref semantics. */
1006 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1007 args
[0] = value_ind (args
[0]);
1009 type
= value_type (args
[0]);
1010 if ((type
->code () != TYPE_CODE_STRUCT
1011 && type
->code () != TYPE_CODE_UNION
1012 && type
->code () != TYPE_CODE_ENUM
)
1013 || rust_tuple_type_p (type
))
1014 error (_("Method calls only supported on struct or enum types"));
1015 if (type
->name () == NULL
)
1016 error (_("Method call on nameless type"));
1018 std::string name
= std::string (type
->name ()) + "::" + method
;
1020 block
= get_selected_block (0);
1021 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1022 if (sym
.symbol
== NULL
)
1023 error (_("Could not find function named '%s'"), name
.c_str ());
1025 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1026 if (fn_type
->num_fields () == 0)
1027 error (_("Function '%s' takes no arguments"), name
.c_str ());
1029 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1030 args
[0] = value_addr (args
[0]);
1032 function
= address_of_variable (sym
.symbol
, block
);
1034 for (i
= 0; i
< num_args
; ++i
)
1035 args
[i
+ 1] = evaluate_subexp (nullptr, exp
, pos
, noside
);
1037 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1038 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1040 result
= call_function_by_hand (function
, NULL
, args
);
1044 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1046 static struct value
*
1047 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1049 struct value
*low
= NULL
, *high
= NULL
;
1050 struct value
*addrval
, *result
;
1052 struct type
*range_type
;
1053 struct type
*index_type
;
1054 struct type
*temp_type
;
1058 = (enum range_flag
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1061 if (!(kind
& RANGE_LOW_BOUND_DEFAULT
))
1062 low
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1063 if (!(kind
& RANGE_HIGH_BOUND_DEFAULT
))
1064 high
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1065 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1067 if (noside
== EVAL_SKIP
)
1068 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1075 name
= "std::ops::RangeFull";
1079 index_type
= value_type (high
);
1081 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1088 index_type
= value_type (low
);
1089 name
= "std::ops::RangeFrom";
1093 if (!types_equal (value_type (low
), value_type (high
)))
1094 error (_("Range expression with different types"));
1095 index_type
= value_type (low
);
1096 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1100 /* If we don't have an index type, just allocate this on the
1101 arch. Here any type will do. */
1102 temp_type
= (index_type
== NULL
1103 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1105 /* It would be nicer to cache the range type. */
1106 range_type
= rust_composite_type (temp_type
, name
,
1107 low
== NULL
? NULL
: "start", index_type
,
1108 high
== NULL
? NULL
: "end", index_type
);
1110 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1111 return value_zero (range_type
, lval_memory
);
1113 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1114 addr
= value_as_long (addrval
);
1115 result
= value_at_lazy (range_type
, addr
);
1119 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1122 value_assign (start
, low
);
1127 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1130 value_assign (end
, high
);
1133 result
= value_at_lazy (range_type
, addr
);
1137 /* A helper function to compute the range and kind given a range
1138 value. TYPE is the type of the range value. RANGE is the range
1139 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1140 parameters might be filled in, or might not be, depending on the
1141 kind of range this is. KIND will always be set to the appropriate
1142 value describing the kind of range, and this can be used to
1143 determine whether LOW or HIGH are valid. */
1146 rust_compute_range (struct type
*type
, struct value
*range
,
1147 LONGEST
*low
, LONGEST
*high
,
1154 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1156 if (type
->num_fields () == 0)
1160 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1162 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1163 *low
= value_as_long (value_field (range
, 0));
1166 if (type
->num_fields () > i
1167 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1169 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1170 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1171 *high
= value_as_long (value_field (range
, i
));
1173 if (rust_inclusive_range_type_p (type
))
1178 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1180 static struct value
*
1181 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1184 struct value
*lhs
, *rhs
, *result
;
1185 struct type
*rhstype
;
1186 LONGEST low
, high_bound
;
1187 /* Initialized to appease the compiler. */
1188 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1193 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1194 rhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1196 if (noside
== EVAL_SKIP
)
1199 rhstype
= check_typedef (value_type (rhs
));
1200 if (rust_range_type_p (rhstype
))
1203 error (_("Can't take slice of array without '&'"));
1204 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1208 low
= value_as_long (rhs
);
1210 struct type
*type
= check_typedef (value_type (lhs
));
1211 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1213 struct type
*base_type
= nullptr;
1214 if (type
->code () == TYPE_CODE_ARRAY
)
1215 base_type
= TYPE_TARGET_TYPE (type
);
1216 else if (rust_slice_type_p (type
))
1218 for (int i
= 0; i
< type
->num_fields (); ++i
)
1220 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1222 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1226 if (base_type
== nullptr)
1227 error (_("Could not find 'data_ptr' in slice type"));
1229 else if (type
->code () == TYPE_CODE_PTR
)
1230 base_type
= TYPE_TARGET_TYPE (type
);
1232 error (_("Cannot subscript non-array type"));
1234 struct type
*new_type
;
1237 if (rust_slice_type_p (type
))
1242 = language_lookup_primitive_type (exp
->language_defn
,
1245 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1249 new_type
= base_type
;
1251 return value_zero (new_type
, VALUE_LVAL (lhs
));
1258 if (type
->code () == TYPE_CODE_ARRAY
)
1261 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1262 error (_("Can't compute array bounds"));
1264 error (_("Found array with non-zero lower bound"));
1267 else if (rust_slice_type_p (type
))
1271 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1272 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1274 high_bound
= value_as_long (len
);
1276 else if (type
->code () == TYPE_CODE_PTR
)
1280 high_bound
= LONGEST_MAX
;
1283 error (_("Cannot subscript non-array type"));
1285 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1288 error (_("Index less than zero"));
1289 if (low
> high_bound
)
1290 error (_("Index greater than length"));
1292 result
= value_subscript (base
, low
);
1299 struct type
*usize
, *slice
;
1301 struct value
*addrval
, *tem
;
1303 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1306 error (_("High index less than zero"));
1308 error (_("Low index greater than high index"));
1309 if (high
> high_bound
)
1310 error (_("High index greater than length"));
1312 usize
= language_lookup_primitive_type (exp
->language_defn
,
1315 const char *new_name
= ((type
!= nullptr
1316 && rust_slice_type_p (type
))
1317 ? type
->name () : "&[*gdb*]");
1319 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1321 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1322 addr
= value_as_long (addrval
);
1323 tem
= value_at_lazy (slice
, addr
);
1325 value_assign (value_field (tem
, 0), value_addr (result
));
1326 value_assign (value_field (tem
, 1),
1327 value_from_longest (usize
, high
- low
));
1329 result
= value_at_lazy (slice
, addr
);
1332 result
= value_addr (result
);
1338 /* evaluate_exp implementation for Rust. */
1340 static struct value
*
1341 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1342 int *pos
, enum noside noside
)
1344 struct value
*result
;
1346 switch (exp
->elts
[*pos
].opcode
)
1350 if (noside
!= EVAL_NORMAL
)
1351 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1355 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1358 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1359 if (trait_ptr
!= NULL
)
1362 result
= value_ind (value
);
1367 case UNOP_COMPLEMENT
:
1369 struct value
*value
;
1372 value
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1373 if (noside
== EVAL_SKIP
)
1375 /* Preserving the type is enough. */
1378 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1379 result
= value_from_longest (value_type (value
),
1380 value_logical_not (value
));
1382 result
= value_complement (value
);
1386 case BINOP_SUBSCRIPT
:
1387 result
= rust_subscript (exp
, pos
, noside
, 0);
1391 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1397 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1398 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1401 struct value
*addrval
= NULL
;
1405 if (noside
== EVAL_NORMAL
)
1407 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1408 addr
= value_as_long (addrval
);
1409 result
= value_at_lazy (type
, addr
);
1412 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1417 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1418 if (noside
== EVAL_NORMAL
)
1420 /* This isn't quite right but will do for the time
1421 being, seeing that we can't implement the Copy
1423 value_assign (result
, init
);
1429 gdb_assert (arglen
% 2 == 0);
1430 for (i
= 0; i
< arglen
; i
+= 2)
1433 const char *fieldname
;
1434 struct value
*value
, *field
;
1436 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1438 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1440 fieldname
= &exp
->elts
[*pos
].string
;
1441 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1443 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1444 if (noside
== EVAL_NORMAL
)
1446 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1448 value_assign (field
, value
);
1452 if (noside
== EVAL_SKIP
)
1453 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1455 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1456 result
= allocate_value (type
);
1458 result
= value_at_lazy (type
, addr
);
1467 struct value
*ncopies
;
1469 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1470 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1471 copies
= value_as_long (ncopies
);
1473 error (_("Array with negative number of elements"));
1475 if (noside
== EVAL_NORMAL
)
1478 std::vector
<struct value
*> eltvec (copies
);
1480 for (i
= 0; i
< copies
; ++i
)
1482 result
= value_array (0, copies
- 1, eltvec
.data ());
1486 struct type
*arraytype
1487 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1488 result
= allocate_value (arraytype
);
1493 case STRUCTOP_ANONYMOUS
:
1495 /* Anonymous field access, i.e. foo.1. */
1497 int pc
, field_number
, nfields
;
1501 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1503 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1505 type
= value_type (lhs
);
1507 if (type
->code () == TYPE_CODE_STRUCT
)
1509 struct type
*outer_type
= NULL
;
1511 if (rust_enum_p (type
))
1513 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1514 TYPE_LENGTH (type
));
1515 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1517 if (rust_empty_enum_p (type
))
1518 error (_("Cannot access field %d of empty enum %s"),
1519 field_number
, type
->name ());
1521 int fieldno
= rust_enum_variant (type
);
1522 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1524 type
= value_type (lhs
);
1527 /* Tuples and tuple structs */
1528 nfields
= type
->num_fields ();
1530 if (field_number
>= nfields
|| field_number
< 0)
1532 if (outer_type
!= NULL
)
1533 error(_("Cannot access field %d of variant %s::%s, "
1534 "there are only %d fields"),
1535 field_number
, outer_type
->name (),
1536 rust_last_path_segment (type
->name ()),
1539 error(_("Cannot access field %d of %s, "
1540 "there are only %d fields"),
1541 field_number
, type
->name (), nfields
);
1544 /* Tuples are tuple structs too. */
1545 if (!rust_tuple_struct_type_p (type
))
1547 if (outer_type
!= NULL
)
1548 error(_("Variant %s::%s is not a tuple variant"),
1549 outer_type
->name (),
1550 rust_last_path_segment (type
->name ()));
1552 error(_("Attempting to access anonymous field %d "
1553 "of %s, which is not a tuple, tuple struct, or "
1554 "tuple-like variant"),
1555 field_number
, type
->name ());
1558 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1561 error(_("Anonymous field access is only allowed on tuples, \
1562 tuple structs, and tuple-like enum variants"));
1566 case STRUCTOP_STRUCT
:
1573 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1574 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1575 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1577 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1578 type
= value_type (lhs
);
1579 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1581 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1582 TYPE_LENGTH (type
));
1583 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1585 if (rust_empty_enum_p (type
))
1586 error (_("Cannot access field %s of empty enum %s"),
1587 field_name
, type
->name ());
1589 int fieldno
= rust_enum_variant (type
);
1590 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1592 struct type
*outer_type
= type
;
1593 type
= value_type (lhs
);
1594 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1595 error (_("Attempting to access named field %s of tuple "
1596 "variant %s::%s, which has only anonymous fields"),
1597 field_name
, outer_type
->name (),
1598 rust_last_path_segment (type
->name ()));
1602 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1605 catch (const gdb_exception_error
&except
)
1607 error (_("Could not find field %s of struct variant %s::%s"),
1608 field_name
, outer_type
->name (),
1609 rust_last_path_segment (type
->name ()));
1613 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1614 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1615 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1620 result
= rust_range (exp
, pos
, noside
);
1624 /* We might have &array[range], in which case we need to make a
1626 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1629 result
= rust_subscript (exp
, pos
, noside
, 1);
1634 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1641 /* operator_length implementation for Rust. */
1644 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1650 switch (exp
->elts
[pc
- 1].opcode
)
1653 /* We handle aggregate as a type and argument count. The first
1654 argument might be OP_OTHERS. After that the arguments
1655 alternate: first an OP_NAME, then an expression. */
1657 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1665 case STRUCTOP_ANONYMOUS
:
1676 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1684 /* dump_subexp_body implementation for Rust. */
1687 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1690 switch (exp
->elts
[elt
].opcode
)
1694 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1697 fprintf_filtered (stream
, "Type @");
1698 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1699 fprintf_filtered (stream
, " (");
1700 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1701 fprintf_filtered (stream
, "), length %d", length
);
1704 for (i
= 0; i
< length
; ++i
)
1705 elt
= dump_subexp (exp
, stream
, elt
);
1712 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1714 fprintf_filtered (stream
, "%s: %s",
1715 (exp
->elts
[elt
].opcode
== OP_STRING
1716 ? "string" : "name"),
1717 &exp
->elts
[elt
+ 2].string
);
1718 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1723 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1726 case STRUCTOP_ANONYMOUS
:
1730 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1732 fprintf_filtered (stream
, "Field number: %d", field_number
);
1733 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1742 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1749 /* print_subexp implementation for Rust. */
1752 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1753 enum precedence prec
)
1755 switch (exp
->elts
[*pos
].opcode
)
1759 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1762 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1763 fputs_filtered (" { ", stream
);
1766 for (i
= 0; i
< length
; ++i
)
1768 rust_print_subexp (exp
, pos
, stream
, prec
);
1769 fputs_filtered (", ", stream
);
1771 fputs_filtered (" }", stream
);
1777 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1779 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1780 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1786 fputs_filtered ("<<others>> (", stream
);
1788 rust_print_subexp (exp
, pos
, stream
, prec
);
1789 fputs_filtered (")", stream
);
1793 case STRUCTOP_ANONYMOUS
:
1795 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1798 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1799 fprintf_filtered (stream
, ".%d", tem
);
1805 fprintf_filtered (stream
, "[");
1806 rust_print_subexp (exp
, pos
, stream
, prec
);
1807 fprintf_filtered (stream
, "; ");
1808 rust_print_subexp (exp
, pos
, stream
, prec
);
1809 fprintf_filtered (stream
, "]");
1813 print_subexp_standard (exp
, pos
, stream
, prec
);
1818 /* operator_check implementation for Rust. */
1821 rust_operator_check (struct expression
*exp
, int pos
,
1822 int (*objfile_func
) (struct objfile
*objfile
,
1826 switch (exp
->elts
[pos
].opcode
)
1830 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1831 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1833 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1844 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1852 static const struct exp_descriptor exp_descriptor_rust
=
1855 rust_operator_length
,
1856 rust_operator_check
,
1857 rust_dump_subexp_body
,
1858 rust_evaluate_subexp
1861 /* Class representing the Rust language. */
1863 class rust_language
: public language_defn
1867 : language_defn (language_rust
)
1870 /* See language.h. */
1872 const char *name () const override
1875 /* See language.h. */
1877 const char *natural_name () const override
1880 /* See language.h. */
1882 const std::vector
<const char *> &filename_extensions () const override
1884 static const std::vector
<const char *> extensions
= { ".rs" };
1888 /* See language.h. */
1889 void language_arch_info (struct gdbarch
*gdbarch
,
1890 struct language_arch_info
*lai
) const override
1892 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1894 /* Helper function to allow shorter lines below. */
1895 auto add
= [&] (struct type
* t
) -> struct type
*
1897 lai
->add_primitive_type (t
);
1901 struct type
*bool_type
1902 = add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1903 add (arch_character_type (gdbarch
, 32, 1, "char"));
1904 add (arch_integer_type (gdbarch
, 8, 0, "i8"));
1905 struct type
*u8_type
1906 = add (arch_integer_type (gdbarch
, 8, 1, "u8"));
1907 add (arch_integer_type (gdbarch
, 16, 0, "i16"));
1908 add (arch_integer_type (gdbarch
, 16, 1, "u16"));
1909 add (arch_integer_type (gdbarch
, 32, 0, "i32"));
1910 add (arch_integer_type (gdbarch
, 32, 1, "u32"));
1911 add (arch_integer_type (gdbarch
, 64, 0, "i64"));
1912 add (arch_integer_type (gdbarch
, 64, 1, "u64"));
1914 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1915 add (arch_integer_type (gdbarch
, length
, 0, "isize"));
1916 struct type
*usize_type
1917 = add (arch_integer_type (gdbarch
, length
, 1, "usize"));
1919 add (arch_float_type (gdbarch
, 32, "f32", floatformats_ieee_single
));
1920 add (arch_float_type (gdbarch
, 64, "f64", floatformats_ieee_double
));
1921 add (arch_integer_type (gdbarch
, 0, 1, "()"));
1923 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1924 add (rust_slice_type ("&str", tem
, usize_type
));
1926 lai
->set_bool_type (bool_type
);
1927 lai
->set_string_char_type (u8_type
);
1930 /* See language.h. */
1931 bool sniff_from_mangled_name (const char *mangled
,
1932 char **demangled
) const override
1934 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1935 return *demangled
!= NULL
;
1938 /* See language.h. */
1940 char *demangle_symbol (const char *mangled
, int options
) const override
1942 return gdb_demangle (mangled
, options
);
1945 /* See language.h. */
1947 void print_type (struct type
*type
, const char *varstring
,
1948 struct ui_file
*stream
, int show
, int level
,
1949 const struct type_print_options
*flags
) const override
1951 print_offset_data podata
;
1952 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1953 flags
, false, &podata
);
1956 /* See language.h. */
1958 gdb::unique_xmalloc_ptr
<char> watch_location_expression
1959 (struct type
*type
, CORE_ADDR addr
) const override
1961 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
1962 std::string name
= type_to_string (type
);
1963 return gdb::unique_xmalloc_ptr
<char>
1964 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
1968 /* See language.h. */
1970 void value_print_inner
1971 (struct value
*val
, struct ui_file
*stream
, int recurse
,
1972 const struct value_print_options
*options
) const override
1974 return rust_value_print_inner (val
, stream
, recurse
, options
);
1977 /* See language.h. */
1979 struct block_symbol lookup_symbol_nonlocal
1980 (const char *name
, const struct block
*block
,
1981 const domain_enum domain
) const override
1983 struct block_symbol result
= {};
1985 if (symbol_lookup_debug
)
1987 fprintf_unfiltered (gdb_stdlog
,
1988 "rust_lookup_symbol_non_local"
1989 " (%s, %s (scope %s), %s)\n",
1990 name
, host_address_to_string (block
),
1991 block_scope (block
), domain_name (domain
));
1994 /* Look up bare names in the block's scope. */
1995 std::string scopedname
;
1996 if (name
[cp_find_first_component (name
)] == '\0')
1998 const char *scope
= block_scope (block
);
2000 if (scope
[0] != '\0')
2002 scopedname
= std::string (scope
) + "::" + name
;
2003 name
= scopedname
.c_str ();
2011 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2012 if (result
.symbol
== NULL
)
2013 result
= lookup_global_symbol (name
, block
, domain
);
2018 /* See language.h. */
2020 int parser (struct parser_state
*ps
) const override
2022 return rust_parse (ps
);
2025 /* See language.h. */
2027 void emitchar (int ch
, struct type
*chtype
,
2028 struct ui_file
*stream
, int quoter
) const override
2030 if (!rust_chartype_p (chtype
))
2031 generic_emit_char (ch
, chtype
, stream
, quoter
,
2032 target_charset (get_type_arch (chtype
)));
2033 else if (ch
== '\\' || ch
== quoter
)
2034 fprintf_filtered (stream
, "\\%c", ch
);
2035 else if (ch
== '\n')
2036 fputs_filtered ("\\n", stream
);
2037 else if (ch
== '\r')
2038 fputs_filtered ("\\r", stream
);
2039 else if (ch
== '\t')
2040 fputs_filtered ("\\t", stream
);
2041 else if (ch
== '\0')
2042 fputs_filtered ("\\0", stream
);
2043 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
2044 fputc_filtered (ch
, stream
);
2046 fprintf_filtered (stream
, "\\x%02x", ch
);
2048 fprintf_filtered (stream
, "\\u{%06x}", ch
);
2051 /* See language.h. */
2053 void printchar (int ch
, struct type
*chtype
,
2054 struct ui_file
*stream
) const override
2056 fputs_filtered ("'", stream
);
2057 emitchar (ch
, chtype
, stream
, '\'');
2058 fputs_filtered ("'", stream
);
2061 /* See language.h. */
2063 void printstr (struct ui_file
*stream
, struct type
*elttype
,
2064 const gdb_byte
*string
, unsigned int length
,
2065 const char *encoding
, int force_ellipses
,
2066 const struct value_print_options
*options
) const override
2068 rust_printstr (stream
, elttype
, string
, length
, encoding
,
2069 force_ellipses
, options
);
2072 /* See language.h. */
2074 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
2075 struct ui_file
*stream
) const override
2077 type
= check_typedef (type
);
2078 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
2079 type_print (type
, "", stream
, 0);
2080 fprintf_filtered (stream
, ";");
2083 /* See language.h. */
2085 bool is_string_type_p (struct type
*type
) const override
2087 LONGEST low_bound
, high_bound
;
2089 type
= check_typedef (type
);
2090 return ((type
->code () == TYPE_CODE_STRING
)
2091 || (type
->code () == TYPE_CODE_PTR
2092 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
2093 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
2094 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
2096 || (type
->code () == TYPE_CODE_STRUCT
2097 && !rust_enum_p (type
)
2098 && rust_slice_type_p (type
)
2099 && strcmp (type
->name (), "&str") == 0));
2102 /* See language.h. */
2104 bool range_checking_on_by_default () const override
2107 /* See language.h. */
2109 const struct exp_descriptor
*expression_ops () const override
2110 { return &exp_descriptor_rust
; }
2112 /* See language.h. */
2114 const struct op_print
*opcode_print_table () const override
2115 { return c_op_print_tab
; }
2118 /* Single instance of the Rust language class. */
2120 static rust_language rust_language_defn
;