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"
42 /* See rust-lang.h. */
45 rust_last_path_segment (const char *path
)
47 const char *result
= strrchr (path
, ':');
54 /* See rust-lang.h. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
62 return std::string ();
64 return std::string (scope
, cp_find_first_component (scope
));
67 /* Return true if TYPE, which must be a struct type, represents a Rust
71 rust_enum_p (struct type
*type
)
73 /* is_dynamic_type will return true if any field has a dynamic
74 attribute -- but we only want to check the top level. */
75 return TYPE_HAS_VARIANT_PARTS (type
);
78 /* Return true if TYPE, which must be an already-resolved enum type,
82 rust_empty_enum_p (const struct type
*type
)
84 return type
->num_fields () == 0;
87 /* Given an already-resolved enum type and contents, find which
91 rust_enum_variant (struct type
*type
)
93 /* The active variant is simply the first non-artificial field. */
94 for (int i
= 0; i
< type
->num_fields (); ++i
)
95 if (!TYPE_FIELD_ARTIFICIAL (type
, i
))
98 /* Perhaps we could get here by trying to print an Ada variant
99 record in Rust mode. Unlikely, but an error is safer than an
101 error (_("Could not find active enum variant"));
104 /* See rust-lang.h. */
107 rust_tuple_type_p (struct type
*type
)
109 /* The current implementation is a bit of a hack, but there's
110 nothing else in the debuginfo to distinguish a tuple from a
112 return (type
->code () == TYPE_CODE_STRUCT
113 && type
->name () != NULL
114 && type
->name ()[0] == '(');
117 /* Return true if all non-static fields of a structlike type are in a
118 sequence like __0, __1, __2. */
121 rust_underscore_fields (struct type
*type
)
127 if (type
->code () != TYPE_CODE_STRUCT
)
129 for (i
= 0; i
< type
->num_fields (); ++i
)
131 if (!field_is_static (&type
->field (i
)))
135 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
136 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
144 /* See rust-lang.h. */
147 rust_tuple_struct_type_p (struct type
*type
)
149 /* This is just an approximation until DWARF can represent Rust more
150 precisely. We exclude zero-length structs because they may not
151 be tuple structs, and there's no way to tell. */
152 return type
->num_fields () > 0 && rust_underscore_fields (type
);
155 /* Return true if TYPE is a slice type, otherwise false. */
158 rust_slice_type_p (struct type
*type
)
160 return (type
->code () == TYPE_CODE_STRUCT
161 && type
->name () != NULL
162 && (strncmp (type
->name (), "&[", 2) == 0
163 || strcmp (type
->name (), "&str") == 0));
166 /* Return true if TYPE is a range type, otherwise false. */
169 rust_range_type_p (struct type
*type
)
173 if (type
->code () != TYPE_CODE_STRUCT
174 || type
->num_fields () > 2
175 || type
->name () == NULL
176 || strstr (type
->name (), "::Range") == NULL
)
179 if (type
->num_fields () == 0)
183 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
185 if (type
->num_fields () == 1)
189 else if (type
->num_fields () == 2)
191 /* First field had to be "start". */
195 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
198 /* Return true if TYPE is an inclusive range type, otherwise false.
199 This is only valid for types which are already known to be range
203 rust_inclusive_range_type_p (struct type
*type
)
205 return (strstr (type
->name (), "::RangeInclusive") != NULL
206 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
209 /* Return true if TYPE seems to be the type "u8", otherwise false. */
212 rust_u8_type_p (struct type
*type
)
214 return (type
->code () == TYPE_CODE_INT
215 && TYPE_UNSIGNED (type
)
216 && TYPE_LENGTH (type
) == 1);
219 /* Return true if TYPE is a Rust character type. */
222 rust_chartype_p (struct type
*type
)
224 return (type
->code () == TYPE_CODE_CHAR
225 && TYPE_LENGTH (type
) == 4
226 && TYPE_UNSIGNED (type
));
229 /* Return true if TYPE is a string type. */
232 rust_is_string_type_p (struct type
*type
)
234 LONGEST low_bound
, high_bound
;
236 type
= check_typedef (type
);
237 return ((type
->code () == TYPE_CODE_STRING
)
238 || (type
->code () == TYPE_CODE_PTR
239 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
240 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
241 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
243 || (type
->code () == TYPE_CODE_STRUCT
244 && !rust_enum_p (type
)
245 && rust_slice_type_p (type
)
246 && strcmp (type
->name (), "&str") == 0));
249 /* If VALUE represents a trait object pointer, return the underlying
250 pointer with the correct (i.e., runtime) type. Otherwise, return
253 static struct value
*
254 rust_get_trait_object_pointer (struct value
*value
)
256 struct type
*type
= check_typedef (value_type (value
));
258 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
261 /* Try to be a bit resilient if the ABI changes. */
262 int vtable_field
= 0;
263 for (int i
= 0; i
< 2; ++i
)
265 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
267 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
271 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
272 struct symbol
*symbol
= find_symbol_at_address (vtable
);
273 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
276 struct rust_vtable_symbol
*vtable_sym
277 = static_cast<struct rust_vtable_symbol
*> (symbol
);
278 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
279 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
284 /* language_defn::printstr implementation for Rust. */
287 rust_printstr (struct ui_file
*stream
, struct type
*type
,
288 const gdb_byte
*string
, unsigned int length
,
289 const char *user_encoding
, int force_ellipses
,
290 const struct value_print_options
*options
)
292 /* Rust always uses UTF-8, but let the caller override this if need
294 const char *encoding
= user_encoding
;
295 if (user_encoding
== NULL
|| !*user_encoding
)
297 /* In Rust strings, characters are "u8". */
298 if (rust_u8_type_p (type
))
302 /* This is probably some C string, so let's let C deal with
304 c_printstr (stream
, type
, string
, length
, user_encoding
,
305 force_ellipses
, options
);
310 /* This is not ideal as it doesn't use our character printer. */
311 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
317 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
319 const struct value_print_options
*options
);
321 /* Helper function to print a string slice. */
324 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
325 const struct value_print_options
*options
)
327 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
329 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
331 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
332 value_as_address (base
), value_as_long (len
), stream
,
336 /* rust_val_print helper for structs and untagged unions. */
339 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
340 const struct value_print_options
*options
)
344 struct type
*type
= check_typedef (value_type (val
));
346 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
348 /* If what we are printing here is actually a string within a
349 structure then VAL will be the original parent value, while TYPE
350 will be the type of the structure representing the string we want
352 However, RUST_VAL_PRINT_STR looks up the fields of the string
353 inside VAL, assuming that VAL is the string.
354 So, recreate VAL as a value representing just the string. */
355 val
= value_at_lazy (type
, value_address (val
));
356 rust_val_print_str (stream
, val
, options
);
360 bool is_tuple
= rust_tuple_type_p (type
);
361 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
362 struct value_print_options opts
;
366 if (type
->name () != NULL
)
367 fprintf_filtered (stream
, "%s", type
->name ());
369 if (type
->num_fields () == 0)
372 if (type
->name () != NULL
)
373 fputs_filtered (" ", stream
);
376 if (is_tuple
|| is_tuple_struct
)
377 fputs_filtered ("(", stream
);
379 fputs_filtered ("{", stream
);
385 for (i
= 0; i
< type
->num_fields (); ++i
)
387 if (field_is_static (&type
->field (i
)))
391 fputs_filtered (",", stream
);
393 if (options
->prettyformat
)
395 fputs_filtered ("\n", stream
);
396 print_spaces_filtered (2 + 2 * recurse
, stream
);
398 else if (!first_field
)
399 fputs_filtered (" ", stream
);
403 if (!is_tuple
&& !is_tuple_struct
)
405 fputs_styled (TYPE_FIELD_NAME (type
, i
),
406 variable_name_style
.style (), stream
);
407 fputs_filtered (": ", stream
);
410 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
414 if (options
->prettyformat
)
416 fputs_filtered ("\n", stream
);
417 print_spaces_filtered (2 * recurse
, stream
);
420 if (is_tuple
|| is_tuple_struct
)
421 fputs_filtered (")", stream
);
423 fputs_filtered ("}", stream
);
426 /* rust_val_print helper for discriminated unions (Rust enums). */
429 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
430 const struct value_print_options
*options
)
432 struct value_print_options opts
= *options
;
433 struct type
*type
= check_typedef (value_type (val
));
437 gdb_assert (rust_enum_p (type
));
438 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
439 TYPE_LENGTH (value_type (val
)));
440 type
= resolve_dynamic_type (type
, view
, value_address (val
));
442 if (rust_empty_enum_p (type
))
444 /* Print the enum type name here to be more clear. */
445 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
447 metadata_style
.style ().ptr (), nullptr);
451 int variant_fieldno
= rust_enum_variant (type
);
452 val
= value_field (val
, variant_fieldno
);
453 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
455 int nfields
= variant_type
->num_fields ();
457 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
459 fprintf_filtered (stream
, "%s", variant_type
->name ());
462 /* In case of a nullary variant like 'None', just output
467 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
469 fprintf_filtered (stream
, "(");
472 /* struct variant. */
473 fprintf_filtered (stream
, "{");
476 bool first_field
= true;
477 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
480 fputs_filtered (", ", stream
);
484 fprintf_filtered (stream
, "%ps: ",
485 styled_string (variable_name_style
.style (),
486 TYPE_FIELD_NAME (variant_type
, j
)));
488 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
493 fputs_filtered (")", stream
);
495 fputs_filtered ("}", stream
);
498 static const struct generic_val_print_decorations rust_decorations
=
500 /* Complex isn't used in Rust, but we provide C-ish values just in
512 /* la_value_print_inner implementation for Rust. */
514 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
516 const struct value_print_options
*options
)
518 struct value_print_options opts
= *options
;
521 if (opts
.prettyformat
== Val_prettyformat_default
)
522 opts
.prettyformat
= (opts
.prettyformat_structs
523 ? Val_prettyformat
: Val_no_prettyformat
);
525 struct type
*type
= check_typedef (value_type (val
));
526 switch (type
->code ())
530 LONGEST low_bound
, high_bound
;
532 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
533 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
534 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
537 /* We have a pointer to a byte string, so just print
539 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
540 CORE_ADDR addr
= value_as_address (val
);
541 struct gdbarch
*arch
= get_type_arch (type
);
543 if (opts
.addressprint
)
545 fputs_filtered (paddress (arch
, addr
), stream
);
546 fputs_filtered (" ", stream
);
549 fputs_filtered ("b", stream
);
550 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
551 high_bound
- low_bound
+ 1, stream
,
558 case TYPE_CODE_METHODPTR
:
559 case TYPE_CODE_MEMBERPTR
:
560 c_value_print_inner (val
, stream
, recurse
, &opts
);
564 /* Recognize the unit type. */
565 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
566 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
568 fputs_filtered ("()", stream
);
573 case TYPE_CODE_STRING
:
575 LONGEST low_bound
, high_bound
;
577 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
578 error (_("Could not determine the array bounds"));
580 /* If we see a plain TYPE_CODE_STRING, then we're printing a
581 byte string, hence the choice of "ASCII" as the
583 fputs_filtered ("b", stream
);
584 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
585 value_contents_for_printing (val
),
586 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
590 case TYPE_CODE_ARRAY
:
592 LONGEST low_bound
, high_bound
;
594 if (get_array_bounds (type
, &low_bound
, &high_bound
)
595 && high_bound
- low_bound
+ 1 == 0)
596 fputs_filtered ("[]", stream
);
602 case TYPE_CODE_UNION
:
603 /* Untagged unions are printed as if they are structs. Since
604 the field bit positions overlap in the debuginfo, the code
605 for printing a union is same as that for a struct, the only
606 difference is that the input type will have overlapping
608 val_print_struct (val
, stream
, recurse
, &opts
);
611 case TYPE_CODE_STRUCT
:
612 if (rust_enum_p (type
))
613 rust_print_enum (val
, stream
, recurse
, &opts
);
615 val_print_struct (val
, stream
, recurse
, &opts
);
620 /* Nothing special yet. */
621 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
628 rust_internal_print_type (struct type
*type
, const char *varstring
,
629 struct ui_file
*stream
, int show
, int level
,
630 const struct type_print_options
*flags
,
631 bool for_rust_enum
, print_offset_data
*podata
);
633 /* Print a struct or union typedef. */
635 rust_print_struct_def (struct type
*type
, const char *varstring
,
636 struct ui_file
*stream
, int show
, int level
,
637 const struct type_print_options
*flags
,
638 bool for_rust_enum
, print_offset_data
*podata
)
640 /* Print a tuple type simply. */
641 if (rust_tuple_type_p (type
))
643 fputs_filtered (type
->name (), stream
);
647 /* If we see a base class, delegate to C. */
648 if (TYPE_N_BASECLASSES (type
) > 0)
649 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
651 if (flags
->print_offsets
)
653 /* Temporarily bump the level so that the output lines up
658 /* Compute properties of TYPE here because, in the enum case, the
659 rest of the code ends up looking only at the variant part. */
660 const char *tagname
= type
->name ();
661 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
662 bool is_tuple
= rust_tuple_type_p (type
);
663 bool is_enum
= rust_enum_p (type
);
667 /* Already printing an outer enum, so nothing to print here. */
671 /* This code path is also used by unions and enums. */
674 fputs_filtered ("enum ", stream
);
675 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
676 if (prop
!= nullptr && prop
->kind
== PROP_TYPE
)
677 type
= prop
->data
.original_type
;
679 else if (type
->code () == TYPE_CODE_STRUCT
)
680 fputs_filtered ("struct ", stream
);
682 fputs_filtered ("union ", stream
);
685 fputs_filtered (tagname
, stream
);
688 if (type
->num_fields () == 0 && !is_tuple
)
690 if (for_rust_enum
&& !flags
->print_offsets
)
691 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
693 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
695 /* When printing offsets, we rearrange the fields into storage
696 order. This lets us show holes more clearly. We work using
697 field indices here because it simplifies calls to
698 print_offset_data::update below. */
699 std::vector
<int> fields
;
700 for (int i
= 0; i
< type
->num_fields (); ++i
)
702 if (field_is_static (&type
->field (i
)))
704 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
706 fields
.push_back (i
);
708 if (flags
->print_offsets
)
709 std::sort (fields
.begin (), fields
.end (),
712 return (TYPE_FIELD_BITPOS (type
, a
)
713 < TYPE_FIELD_BITPOS (type
, b
));
720 gdb_assert (!field_is_static (&type
->field (i
)));
721 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
723 if (flags
->print_offsets
)
724 podata
->update (type
, i
, stream
);
726 /* We'd like to print "pub" here as needed, but rustc
727 doesn't emit the debuginfo, and our types don't have
728 cplus_struct_type attached. */
730 /* For a tuple struct we print the type but nothing
732 if (!for_rust_enum
|| flags
->print_offsets
)
733 print_spaces_filtered (level
+ 2, stream
);
735 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
737 else if (!is_tuple_struct
)
738 fprintf_filtered (stream
, "%ps: ",
739 styled_string (variable_name_style
.style (),
740 TYPE_FIELD_NAME (type
, i
)));
742 rust_internal_print_type (type
->field (i
).type (), NULL
,
743 stream
, (is_enum
? show
: show
- 1),
744 level
+ 2, flags
, is_enum
, podata
);
745 if (!for_rust_enum
|| flags
->print_offsets
)
746 fputs_filtered (",\n", stream
);
747 /* Note that this check of "I" is ok because we only sorted the
748 fields by offset when print_offsets was set, so we won't take
749 this branch in that case. */
750 else if (i
+ 1 < type
->num_fields ())
751 fputs_filtered (", ", stream
);
754 if (flags
->print_offsets
)
756 /* Undo the temporary level increase we did above. */
758 podata
->finish (type
, level
, stream
);
759 print_spaces_filtered (print_offset_data::indentation
, stream
);
761 print_spaces_filtered (2, stream
);
763 if (!for_rust_enum
|| flags
->print_offsets
)
764 print_spaces_filtered (level
, stream
);
765 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
768 /* la_print_typedef implementation for Rust. */
771 rust_print_typedef (struct type
*type
,
772 struct symbol
*new_symbol
,
773 struct ui_file
*stream
)
775 type
= check_typedef (type
);
776 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
777 type_print (type
, "", stream
, 0);
778 fprintf_filtered (stream
, ";");
781 /* la_print_type implementation for Rust. */
784 rust_internal_print_type (struct type
*type
, const char *varstring
,
785 struct ui_file
*stream
, int show
, int level
,
786 const struct type_print_options
*flags
,
787 bool for_rust_enum
, print_offset_data
*podata
)
791 && type
->name () != NULL
)
793 /* Rust calls the unit type "void" in its debuginfo,
794 but we don't want to print it as that. */
795 if (type
->code () == TYPE_CODE_VOID
)
796 fputs_filtered ("()", stream
);
798 fputs_filtered (type
->name (), stream
);
802 type
= check_typedef (type
);
803 switch (type
->code ())
806 /* If we have an enum, we've already printed the type's
807 unqualified name, and there is nothing else to print
810 fputs_filtered ("()", stream
);
814 /* Delegate varargs to the C printer. */
815 if (TYPE_VARARGS (type
))
818 fputs_filtered ("fn ", stream
);
819 if (varstring
!= NULL
)
820 fputs_filtered (varstring
, stream
);
821 fputs_filtered ("(", stream
);
822 for (int i
= 0; i
< type
->num_fields (); ++i
)
826 fputs_filtered (", ", stream
);
827 rust_internal_print_type (type
->field (i
).type (), "", stream
,
828 -1, 0, flags
, false, podata
);
830 fputs_filtered (")", stream
);
831 /* If it returns unit, we can omit the return type. */
832 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
834 fputs_filtered (" -> ", stream
);
835 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
836 -1, 0, flags
, false, podata
);
840 case TYPE_CODE_ARRAY
:
842 LONGEST low_bound
, high_bound
;
844 fputs_filtered ("[", stream
);
845 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
846 stream
, show
- 1, level
, flags
, false,
849 if (TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCEXPR
850 || TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCLIST
)
851 fprintf_filtered (stream
, "; variable length");
852 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
853 fprintf_filtered (stream
, "; %s",
854 plongest (high_bound
- low_bound
+ 1));
855 fputs_filtered ("]", stream
);
859 case TYPE_CODE_UNION
:
860 case TYPE_CODE_STRUCT
:
861 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
862 for_rust_enum
, podata
);
869 fputs_filtered ("enum ", stream
);
870 if (type
->name () != NULL
)
872 fputs_filtered (type
->name (), stream
);
873 fputs_filtered (" ", stream
);
874 len
= strlen (type
->name ());
876 fputs_filtered ("{\n", stream
);
878 for (int i
= 0; i
< type
->num_fields (); ++i
)
880 const char *name
= TYPE_FIELD_NAME (type
, i
);
885 && strncmp (name
, type
->name (), len
) == 0
887 && name
[len
+ 1] == ':')
889 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
890 styled_string (variable_name_style
.style (),
894 fputs_filtered ("}", stream
);
900 if (type
->name () != nullptr)
901 fputs_filtered (type
->name (), stream
);
904 /* We currently can't distinguish between pointers and
906 fputs_filtered ("*mut ", stream
);
907 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
914 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
920 /* Like arch_composite_type, but uses TYPE to decide how to allocate
921 -- either on an obstack or on a gdbarch. */
924 rust_composite_type (struct type
*original
,
926 const char *field1
, struct type
*type1
,
927 const char *field2
, struct type
*type2
)
929 struct type
*result
= alloc_type_copy (original
);
930 int i
, nfields
, bitpos
;
938 result
->set_code (TYPE_CODE_STRUCT
);
939 result
->set_name (name
);
941 result
->set_num_fields (nfields
);
943 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
949 struct field
*field
= &result
->field (i
);
951 SET_FIELD_BITPOS (*field
, bitpos
);
952 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
954 FIELD_NAME (*field
) = field1
;
955 field
->set_type (type1
);
960 struct field
*field
= &result
->field (i
);
961 unsigned align
= type_align (type2
);
967 align
*= TARGET_CHAR_BIT
;
968 delta
= bitpos
% align
;
970 bitpos
+= align
- delta
;
972 SET_FIELD_BITPOS (*field
, bitpos
);
974 FIELD_NAME (*field
) = field2
;
975 field
->set_type (type2
);
981 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
982 TYPE_LENGTH (result
->field (i
- 1).type ()));
986 /* See rust-lang.h. */
989 rust_slice_type (const char *name
, struct type
*elt_type
,
990 struct type
*usize_type
)
994 elt_type
= lookup_pointer_type (elt_type
);
995 type
= rust_composite_type (elt_type
, name
,
996 "data_ptr", elt_type
,
997 "length", usize_type
);
1002 enum rust_primitive_types
1004 rust_primitive_bool
,
1005 rust_primitive_char
,
1014 rust_primitive_isize
,
1015 rust_primitive_usize
,
1018 rust_primitive_unit
,
1020 nr_rust_primitive_types
1025 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1027 static struct value
*
1028 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1031 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1033 struct value
*function
, *result
, *arg0
;
1034 struct type
*type
, *fn_type
;
1035 const struct block
*block
;
1036 struct block_symbol sym
;
1038 /* For an ordinary function call we can simply defer to the
1039 generic implementation. */
1040 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1041 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1043 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1045 method
= &exp
->elts
[*pos
+ 1].string
;
1046 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1048 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1049 type in order to look up the method. */
1050 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1052 if (noside
== EVAL_SKIP
)
1054 for (i
= 0; i
< num_args
; ++i
)
1055 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1059 std::vector
<struct value
*> args (num_args
+ 1);
1062 /* We don't yet implement real Deref semantics. */
1063 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1064 args
[0] = value_ind (args
[0]);
1066 type
= value_type (args
[0]);
1067 if ((type
->code () != TYPE_CODE_STRUCT
1068 && type
->code () != TYPE_CODE_UNION
1069 && type
->code () != TYPE_CODE_ENUM
)
1070 || rust_tuple_type_p (type
))
1071 error (_("Method calls only supported on struct or enum types"));
1072 if (type
->name () == NULL
)
1073 error (_("Method call on nameless type"));
1075 std::string name
= std::string (type
->name ()) + "::" + method
;
1077 block
= get_selected_block (0);
1078 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1079 if (sym
.symbol
== NULL
)
1080 error (_("Could not find function named '%s'"), name
.c_str ());
1082 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1083 if (fn_type
->num_fields () == 0)
1084 error (_("Function '%s' takes no arguments"), name
.c_str ());
1086 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1087 args
[0] = value_addr (args
[0]);
1089 function
= address_of_variable (sym
.symbol
, block
);
1091 for (i
= 0; i
< num_args
; ++i
)
1092 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1094 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1095 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1097 result
= call_function_by_hand (function
, NULL
, args
);
1101 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1103 static struct value
*
1104 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1106 enum range_type kind
;
1107 struct value
*low
= NULL
, *high
= NULL
;
1108 struct value
*addrval
, *result
;
1110 struct type
*range_type
;
1111 struct type
*index_type
;
1112 struct type
*temp_type
;
1115 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1118 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1119 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1120 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1121 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1122 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1123 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1124 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1126 if (noside
== EVAL_SKIP
)
1127 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1134 name
= "std::ops::RangeFull";
1138 index_type
= value_type (high
);
1140 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1147 index_type
= value_type (low
);
1148 name
= "std::ops::RangeFrom";
1152 if (!types_equal (value_type (low
), value_type (high
)))
1153 error (_("Range expression with different types"));
1154 index_type
= value_type (low
);
1155 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1159 /* If we don't have an index type, just allocate this on the
1160 arch. Here any type will do. */
1161 temp_type
= (index_type
== NULL
1162 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1164 /* It would be nicer to cache the range type. */
1165 range_type
= rust_composite_type (temp_type
, name
,
1166 low
== NULL
? NULL
: "start", index_type
,
1167 high
== NULL
? NULL
: "end", index_type
);
1169 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1170 return value_zero (range_type
, lval_memory
);
1172 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1173 addr
= value_as_long (addrval
);
1174 result
= value_at_lazy (range_type
, addr
);
1178 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1181 value_assign (start
, low
);
1186 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1189 value_assign (end
, high
);
1192 result
= value_at_lazy (range_type
, addr
);
1196 /* A helper function to compute the range and kind given a range
1197 value. TYPE is the type of the range value. RANGE is the range
1198 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1199 parameters might be filled in, or might not be, depending on the
1200 kind of range this is. KIND will always be set to the appropriate
1201 value describing the kind of range, and this can be used to
1202 determine whether LOW or HIGH are valid. */
1205 rust_compute_range (struct type
*type
, struct value
*range
,
1206 LONGEST
*low
, LONGEST
*high
,
1207 enum range_type
*kind
)
1213 *kind
= BOTH_BOUND_DEFAULT
;
1215 if (type
->num_fields () == 0)
1219 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1221 *kind
= HIGH_BOUND_DEFAULT
;
1222 *low
= value_as_long (value_field (range
, 0));
1225 if (type
->num_fields () > i
1226 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1228 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1229 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1230 *high
= value_as_long (value_field (range
, i
));
1232 if (rust_inclusive_range_type_p (type
))
1237 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1239 static struct value
*
1240 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1243 struct value
*lhs
, *rhs
, *result
;
1244 struct type
*rhstype
;
1245 LONGEST low
, high_bound
;
1246 /* Initialized to appease the compiler. */
1247 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1252 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1253 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1255 if (noside
== EVAL_SKIP
)
1258 rhstype
= check_typedef (value_type (rhs
));
1259 if (rust_range_type_p (rhstype
))
1262 error (_("Can't take slice of array without '&'"));
1263 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1267 low
= value_as_long (rhs
);
1269 struct type
*type
= check_typedef (value_type (lhs
));
1270 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1272 struct type
*base_type
= nullptr;
1273 if (type
->code () == TYPE_CODE_ARRAY
)
1274 base_type
= TYPE_TARGET_TYPE (type
);
1275 else if (rust_slice_type_p (type
))
1277 for (int i
= 0; i
< type
->num_fields (); ++i
)
1279 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1281 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1285 if (base_type
== nullptr)
1286 error (_("Could not find 'data_ptr' in slice type"));
1288 else if (type
->code () == TYPE_CODE_PTR
)
1289 base_type
= TYPE_TARGET_TYPE (type
);
1291 error (_("Cannot subscript non-array type"));
1293 struct type
*new_type
;
1296 if (rust_slice_type_p (type
))
1301 = language_lookup_primitive_type (exp
->language_defn
,
1304 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1308 new_type
= base_type
;
1310 return value_zero (new_type
, VALUE_LVAL (lhs
));
1317 if (type
->code () == TYPE_CODE_ARRAY
)
1320 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1321 error (_("Can't compute array bounds"));
1323 error (_("Found array with non-zero lower bound"));
1326 else if (rust_slice_type_p (type
))
1330 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1331 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1333 high_bound
= value_as_long (len
);
1335 else if (type
->code () == TYPE_CODE_PTR
)
1339 high_bound
= LONGEST_MAX
;
1342 error (_("Cannot subscript non-array type"));
1345 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1348 error (_("Index less than zero"));
1349 if (low
> high_bound
)
1350 error (_("Index greater than length"));
1352 result
= value_subscript (base
, low
);
1359 struct type
*usize
, *slice
;
1361 struct value
*addrval
, *tem
;
1363 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1366 error (_("High index less than zero"));
1368 error (_("Low index greater than high index"));
1369 if (high
> high_bound
)
1370 error (_("High index greater than length"));
1372 usize
= language_lookup_primitive_type (exp
->language_defn
,
1375 const char *new_name
= ((type
!= nullptr
1376 && rust_slice_type_p (type
))
1377 ? type
->name () : "&[*gdb*]");
1379 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1381 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1382 addr
= value_as_long (addrval
);
1383 tem
= value_at_lazy (slice
, addr
);
1385 value_assign (value_field (tem
, 0), value_addr (result
));
1386 value_assign (value_field (tem
, 1),
1387 value_from_longest (usize
, high
- low
));
1389 result
= value_at_lazy (slice
, addr
);
1392 result
= value_addr (result
);
1398 /* evaluate_exp implementation for Rust. */
1400 static struct value
*
1401 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1402 int *pos
, enum noside noside
)
1404 struct value
*result
;
1406 switch (exp
->elts
[*pos
].opcode
)
1410 if (noside
!= EVAL_NORMAL
)
1411 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1415 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1418 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1419 if (trait_ptr
!= NULL
)
1422 result
= value_ind (value
);
1427 case UNOP_COMPLEMENT
:
1429 struct value
*value
;
1432 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1433 if (noside
== EVAL_SKIP
)
1435 /* Preserving the type is enough. */
1438 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1439 result
= value_from_longest (value_type (value
),
1440 value_logical_not (value
));
1442 result
= value_complement (value
);
1446 case BINOP_SUBSCRIPT
:
1447 result
= rust_subscript (exp
, pos
, noside
, 0);
1451 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1457 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1458 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1461 struct value
*addrval
= NULL
;
1465 if (noside
== EVAL_NORMAL
)
1467 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1468 addr
= value_as_long (addrval
);
1469 result
= value_at_lazy (type
, addr
);
1472 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1477 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1478 if (noside
== EVAL_NORMAL
)
1480 /* This isn't quite right but will do for the time
1481 being, seeing that we can't implement the Copy
1483 value_assign (result
, init
);
1489 gdb_assert (arglen
% 2 == 0);
1490 for (i
= 0; i
< arglen
; i
+= 2)
1493 const char *fieldname
;
1494 struct value
*value
, *field
;
1496 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1498 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1500 fieldname
= &exp
->elts
[*pos
].string
;
1501 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1503 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1504 if (noside
== EVAL_NORMAL
)
1506 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1508 value_assign (field
, value
);
1512 if (noside
== EVAL_SKIP
)
1513 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1515 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1516 result
= allocate_value (type
);
1518 result
= value_at_lazy (type
, addr
);
1527 struct value
*ncopies
;
1529 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1530 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1531 copies
= value_as_long (ncopies
);
1533 error (_("Array with negative number of elements"));
1535 if (noside
== EVAL_NORMAL
)
1538 std::vector
<struct value
*> eltvec (copies
);
1540 for (i
= 0; i
< copies
; ++i
)
1542 result
= value_array (0, copies
- 1, eltvec
.data ());
1546 struct type
*arraytype
1547 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1548 result
= allocate_value (arraytype
);
1553 case STRUCTOP_ANONYMOUS
:
1555 /* Anonymous field access, i.e. foo.1. */
1557 int pc
, field_number
, nfields
;
1561 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1563 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1565 type
= value_type (lhs
);
1567 if (type
->code () == TYPE_CODE_STRUCT
)
1569 struct type
*outer_type
= NULL
;
1571 if (rust_enum_p (type
))
1573 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1574 TYPE_LENGTH (type
));
1575 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1577 if (rust_empty_enum_p (type
))
1578 error (_("Cannot access field %d of empty enum %s"),
1579 field_number
, type
->name ());
1581 int fieldno
= rust_enum_variant (type
);
1582 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1584 type
= value_type (lhs
);
1587 /* Tuples and tuple structs */
1588 nfields
= type
->num_fields ();
1590 if (field_number
>= nfields
|| field_number
< 0)
1592 if (outer_type
!= NULL
)
1593 error(_("Cannot access field %d of variant %s::%s, "
1594 "there are only %d fields"),
1595 field_number
, outer_type
->name (),
1596 rust_last_path_segment (type
->name ()),
1599 error(_("Cannot access field %d of %s, "
1600 "there are only %d fields"),
1601 field_number
, type
->name (), nfields
);
1604 /* Tuples are tuple structs too. */
1605 if (!rust_tuple_struct_type_p (type
))
1607 if (outer_type
!= NULL
)
1608 error(_("Variant %s::%s is not a tuple variant"),
1609 outer_type
->name (),
1610 rust_last_path_segment (type
->name ()));
1612 error(_("Attempting to access anonymous field %d "
1613 "of %s, which is not a tuple, tuple struct, or "
1614 "tuple-like variant"),
1615 field_number
, type
->name ());
1618 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1621 error(_("Anonymous field access is only allowed on tuples, \
1622 tuple structs, and tuple-like enum variants"));
1626 case STRUCTOP_STRUCT
:
1633 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1634 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1635 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1637 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1638 type
= value_type (lhs
);
1639 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1641 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1642 TYPE_LENGTH (type
));
1643 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1645 if (rust_empty_enum_p (type
))
1646 error (_("Cannot access field %s of empty enum %s"),
1647 field_name
, type
->name ());
1649 int fieldno
= rust_enum_variant (type
);
1650 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1652 struct type
*outer_type
= type
;
1653 type
= value_type (lhs
);
1654 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1655 error (_("Attempting to access named field %s of tuple "
1656 "variant %s::%s, which has only anonymous fields"),
1657 field_name
, outer_type
->name (),
1658 rust_last_path_segment (type
->name ()));
1662 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1665 catch (const gdb_exception_error
&except
)
1667 error (_("Could not find field %s of struct variant %s::%s"),
1668 field_name
, outer_type
->name (),
1669 rust_last_path_segment (type
->name ()));
1673 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1674 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1675 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1680 result
= rust_range (exp
, pos
, noside
);
1684 /* We might have &array[range], in which case we need to make a
1686 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1689 result
= rust_subscript (exp
, pos
, noside
, 1);
1694 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1701 /* operator_length implementation for Rust. */
1704 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1710 switch (exp
->elts
[pc
- 1].opcode
)
1713 /* We handle aggregate as a type and argument count. The first
1714 argument might be OP_OTHERS. After that the arguments
1715 alternate: first an OP_NAME, then an expression. */
1717 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1725 case STRUCTOP_ANONYMOUS
:
1736 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1744 /* op_name implementation for Rust. */
1747 rust_op_name (enum exp_opcode opcode
)
1752 return "OP_AGGREGATE";
1756 return op_name_standard (opcode
);
1760 /* dump_subexp_body implementation for Rust. */
1763 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1766 switch (exp
->elts
[elt
].opcode
)
1770 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1773 fprintf_filtered (stream
, "Type @");
1774 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1775 fprintf_filtered (stream
, " (");
1776 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1777 fprintf_filtered (stream
, "), length %d", length
);
1780 for (i
= 0; i
< length
; ++i
)
1781 elt
= dump_subexp (exp
, stream
, elt
);
1788 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1790 fprintf_filtered (stream
, "%s: %s",
1791 (exp
->elts
[elt
].opcode
== OP_STRING
1792 ? "string" : "name"),
1793 &exp
->elts
[elt
+ 2].string
);
1794 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1799 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1802 case STRUCTOP_ANONYMOUS
:
1806 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1808 fprintf_filtered (stream
, "Field number: %d", field_number
);
1809 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1818 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1825 /* print_subexp implementation for Rust. */
1828 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1829 enum precedence prec
)
1831 switch (exp
->elts
[*pos
].opcode
)
1835 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1838 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1839 fputs_filtered (" { ", stream
);
1842 for (i
= 0; i
< length
; ++i
)
1844 rust_print_subexp (exp
, pos
, stream
, prec
);
1845 fputs_filtered (", ", stream
);
1847 fputs_filtered (" }", stream
);
1853 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1855 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1856 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1862 fputs_filtered ("<<others>> (", stream
);
1864 rust_print_subexp (exp
, pos
, stream
, prec
);
1865 fputs_filtered (")", stream
);
1869 case STRUCTOP_ANONYMOUS
:
1871 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1874 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1875 fprintf_filtered (stream
, ".%d", tem
);
1881 fprintf_filtered (stream
, "[");
1882 rust_print_subexp (exp
, pos
, stream
, prec
);
1883 fprintf_filtered (stream
, "; ");
1884 rust_print_subexp (exp
, pos
, stream
, prec
);
1885 fprintf_filtered (stream
, "]");
1889 print_subexp_standard (exp
, pos
, stream
, prec
);
1894 /* operator_check implementation for Rust. */
1897 rust_operator_check (struct expression
*exp
, int pos
,
1898 int (*objfile_func
) (struct objfile
*objfile
,
1902 switch (exp
->elts
[pos
].opcode
)
1906 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1907 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1909 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1920 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1928 static const struct exp_descriptor exp_descriptor_rust
=
1931 rust_operator_length
,
1932 rust_operator_check
,
1934 rust_dump_subexp_body
,
1935 rust_evaluate_subexp
1938 static const char *rust_extensions
[] =
1943 /* Constant data representing the Rust language. */
1945 extern const struct language_data rust_language_data
=
1955 &exp_descriptor_rust
,
1956 rust_print_typedef
, /* Print a typedef using appropriate syntax */
1957 NULL
, /* name_of_this */
1958 false, /* la_store_sym_names_in_linkage_form_p */
1959 c_op_print_tab
, /* expression operators for printing */
1960 1, /* c-style arrays */
1961 0, /* String lower bound */
1962 &default_varobj_ops
,
1963 rust_is_string_type_p
,
1964 "{...}" /* la_struct_too_deep_ellipsis */
1967 /* Class representing the Rust language. */
1969 class rust_language
: public language_defn
1973 : language_defn (language_rust
, rust_language_data
)
1976 /* See language.h. */
1977 void language_arch_info (struct gdbarch
*gdbarch
,
1978 struct language_arch_info
*lai
) const override
1980 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1983 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1986 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1987 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1988 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1989 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1990 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1991 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1992 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1993 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1994 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1995 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1997 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1998 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1999 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
2001 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
2002 floatformats_ieee_single
);
2003 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
2004 floatformats_ieee_double
);
2006 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
2008 struct type
*tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
2009 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
2010 types
[rust_primitive_usize
]);
2012 lai
->primitive_type_vector
= types
;
2013 lai
->bool_type_default
= types
[rust_primitive_bool
];
2014 lai
->string_char_type
= types
[rust_primitive_u8
];
2017 /* See language.h. */
2018 bool sniff_from_mangled_name (const char *mangled
,
2019 char **demangled
) const override
2021 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2022 return *demangled
!= NULL
;
2025 /* See language.h. */
2027 char *demangle (const char *mangled
, int options
) const override
2029 return gdb_demangle (mangled
, options
);
2032 /* See language.h. */
2034 void print_type (struct type
*type
, const char *varstring
,
2035 struct ui_file
*stream
, int show
, int level
,
2036 const struct type_print_options
*flags
) const override
2038 print_offset_data podata
;
2039 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
2040 flags
, false, &podata
);
2043 /* See language.h. */
2045 gdb::unique_xmalloc_ptr
<char> watch_location_expression
2046 (struct type
*type
, CORE_ADDR addr
) const override
2048 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2049 std::string name
= type_to_string (type
);
2050 return gdb::unique_xmalloc_ptr
<char>
2051 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2055 /* See language.h. */
2057 void value_print_inner
2058 (struct value
*val
, struct ui_file
*stream
, int recurse
,
2059 const struct value_print_options
*options
) const override
2061 return rust_value_print_inner (val
, stream
, recurse
, options
);
2064 /* See language.h. */
2066 struct block_symbol lookup_symbol_nonlocal
2067 (const char *name
, const struct block
*block
,
2068 const domain_enum domain
) const override
2070 struct block_symbol result
= {};
2072 if (symbol_lookup_debug
)
2074 fprintf_unfiltered (gdb_stdlog
,
2075 "rust_lookup_symbol_non_local"
2076 " (%s, %s (scope %s), %s)\n",
2077 name
, host_address_to_string (block
),
2078 block_scope (block
), domain_name (domain
));
2081 /* Look up bare names in the block's scope. */
2082 std::string scopedname
;
2083 if (name
[cp_find_first_component (name
)] == '\0')
2085 const char *scope
= block_scope (block
);
2087 if (scope
[0] != '\0')
2089 scopedname
= std::string (scope
) + "::" + name
;
2090 name
= scopedname
.c_str ();
2098 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2099 if (result
.symbol
== NULL
)
2100 result
= lookup_global_symbol (name
, block
, domain
);
2105 /* See language.h. */
2107 int parser (struct parser_state
*ps
) const override
2109 return rust_parse (ps
);
2112 /* See language.h. */
2114 void emitchar (int ch
, struct type
*chtype
,
2115 struct ui_file
*stream
, int quoter
) const override
2117 if (!rust_chartype_p (chtype
))
2118 generic_emit_char (ch
, chtype
, stream
, quoter
,
2119 target_charset (get_type_arch (chtype
)));
2120 else if (ch
== '\\' || ch
== quoter
)
2121 fprintf_filtered (stream
, "\\%c", ch
);
2122 else if (ch
== '\n')
2123 fputs_filtered ("\\n", stream
);
2124 else if (ch
== '\r')
2125 fputs_filtered ("\\r", stream
);
2126 else if (ch
== '\t')
2127 fputs_filtered ("\\t", stream
);
2128 else if (ch
== '\0')
2129 fputs_filtered ("\\0", stream
);
2130 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
2131 fputc_filtered (ch
, stream
);
2133 fprintf_filtered (stream
, "\\x%02x", ch
);
2135 fprintf_filtered (stream
, "\\u{%06x}", ch
);
2138 /* See language.h. */
2140 void printchar (int ch
, struct type
*chtype
,
2141 struct ui_file
*stream
) const override
2143 fputs_filtered ("'", stream
);
2144 LA_EMIT_CHAR (ch
, chtype
, stream
, '\'');
2145 fputs_filtered ("'", stream
);
2148 /* See language.h. */
2150 void printstr (struct ui_file
*stream
, struct type
*elttype
,
2151 const gdb_byte
*string
, unsigned int length
,
2152 const char *encoding
, int force_ellipses
,
2153 const struct value_print_options
*options
) const override
2155 rust_printstr (stream
, elttype
, string
, length
, encoding
,
2156 force_ellipses
, options
);
2160 /* Single instance of the Rust language class. */
2162 static rust_language rust_language_defn
;