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 (const struct type
*type
)
73 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
74 && TYPE_NFIELDS (type
) == 1
75 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type
, 0)));
78 /* Return true if TYPE, which must be an enum type, has no
82 rust_empty_enum_p (const struct type
*type
)
84 gdb_assert (rust_enum_p (type
));
85 /* In Rust the enum always fills the containing structure. */
86 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
88 return TYPE_NFIELDS (TYPE_FIELD_TYPE (type
, 0)) == 0;
91 /* Given an enum type and contents, find which variant is active. */
94 rust_enum_variant (struct type
*type
, const gdb_byte
*contents
)
96 /* In Rust the enum always fills the containing structure. */
97 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
99 struct type
*union_type
= TYPE_FIELD_TYPE (type
, 0);
101 int fieldno
= value_union_variant (union_type
, contents
);
102 return &TYPE_FIELD (union_type
, fieldno
);
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
) == TYPE_CODE_STRUCT
114 && TYPE_NAME (type
) != NULL
115 && TYPE_NAME (type
)[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
) != TYPE_CODE_STRUCT
)
130 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
132 if (!field_is_static (&TYPE_FIELD (type
, 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_NFIELDS (type
) > 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
) == TYPE_CODE_STRUCT
162 && TYPE_NAME (type
) != NULL
163 && (strncmp (TYPE_NAME (type
), "&[", 2) == 0
164 || strcmp (TYPE_NAME (type
), "&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
) != TYPE_CODE_STRUCT
175 || TYPE_NFIELDS (type
) > 2
176 || TYPE_NAME (type
) == NULL
177 || strstr (TYPE_NAME (type
), "::Range") == NULL
)
180 if (TYPE_NFIELDS (type
) == 0)
184 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
186 if (TYPE_NFIELDS (type
) == 1)
190 else if (TYPE_NFIELDS (type
) == 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 (type
), "::RangeInclusive") != NULL
207 || strstr (TYPE_NAME (type
), "::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
) == TYPE_CODE_INT
216 && TYPE_UNSIGNED (type
)
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
) == TYPE_CODE_CHAR
226 && TYPE_LENGTH (type
) == 4
227 && TYPE_UNSIGNED (type
));
230 /* Return true if TYPE is a string type. */
233 rust_is_string_type_p (struct type
*type
)
235 LONGEST low_bound
, high_bound
;
237 type
= check_typedef (type
);
238 return ((TYPE_CODE (type
) == TYPE_CODE_STRING
)
239 || (TYPE_CODE (type
) == TYPE_CODE_PTR
240 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
241 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
242 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
244 || (TYPE_CODE (type
) == TYPE_CODE_STRUCT
245 && !rust_enum_p (type
)
246 && rust_slice_type_p (type
)
247 && strcmp (TYPE_NAME (type
), "&str") == 0));
250 /* If VALUE represents a trait object pointer, return the underlying
251 pointer with the correct (i.e., runtime) type. Otherwise, return
254 static struct value
*
255 rust_get_trait_object_pointer (struct value
*value
)
257 struct type
*type
= check_typedef (value_type (value
));
259 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
262 /* Try to be a bit resilient if the ABI changes. */
263 int vtable_field
= 0;
264 for (int i
= 0; i
< 2; ++i
)
266 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
268 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
272 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
273 struct symbol
*symbol
= find_symbol_at_address (vtable
);
274 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
277 struct rust_vtable_symbol
*vtable_sym
278 = static_cast<struct rust_vtable_symbol
*> (symbol
);
279 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
280 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
285 /* la_emitchar implementation for Rust. */
288 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
290 if (!rust_chartype_p (type
))
291 generic_emit_char (c
, type
, stream
, quoter
,
292 target_charset (get_type_arch (type
)));
293 else if (c
== '\\' || c
== quoter
)
294 fprintf_filtered (stream
, "\\%c", c
);
296 fputs_filtered ("\\n", stream
);
298 fputs_filtered ("\\r", stream
);
300 fputs_filtered ("\\t", stream
);
302 fputs_filtered ("\\0", stream
);
303 else if (c
>= 32 && c
<= 127 && isprint (c
))
304 fputc_filtered (c
, stream
);
306 fprintf_filtered (stream
, "\\x%02x", c
);
308 fprintf_filtered (stream
, "\\u{%06x}", c
);
311 /* la_printchar implementation for Rust. */
314 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
316 fputs_filtered ("'", stream
);
317 LA_EMIT_CHAR (c
, type
, stream
, '\'');
318 fputs_filtered ("'", stream
);
321 /* la_printstr implementation for Rust. */
324 rust_printstr (struct ui_file
*stream
, struct type
*type
,
325 const gdb_byte
*string
, unsigned int length
,
326 const char *user_encoding
, int force_ellipses
,
327 const struct value_print_options
*options
)
329 /* Rust always uses UTF-8, but let the caller override this if need
331 const char *encoding
= user_encoding
;
332 if (user_encoding
== NULL
|| !*user_encoding
)
334 /* In Rust strings, characters are "u8". */
335 if (rust_u8_type_p (type
))
339 /* This is probably some C string, so let's let C deal with
341 c_printstr (stream
, type
, string
, length
, user_encoding
,
342 force_ellipses
, options
);
347 /* This is not ideal as it doesn't use our character printer. */
348 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
354 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
356 const struct value_print_options
*options
);
358 /* Helper function to print a string slice. */
361 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
362 const struct value_print_options
*options
)
364 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
366 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
368 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
369 value_as_address (base
), value_as_long (len
), stream
,
373 /* rust_val_print helper for structs and untagged unions. */
376 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
377 const struct value_print_options
*options
)
381 struct type
*type
= check_typedef (value_type (val
));
383 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
385 /* If what we are printing here is actually a string within a
386 structure then VAL will be the original parent value, while TYPE
387 will be the type of the structure representing the string we want
389 However, RUST_VAL_PRINT_STR looks up the fields of the string
390 inside VAL, assuming that VAL is the string.
391 So, recreate VAL as a value representing just the string. */
392 val
= value_at_lazy (type
, value_address (val
));
393 rust_val_print_str (stream
, val
, options
);
397 bool is_tuple
= rust_tuple_type_p (type
);
398 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
399 struct value_print_options opts
;
403 if (TYPE_NAME (type
) != NULL
)
404 fprintf_filtered (stream
, "%s", TYPE_NAME (type
));
406 if (TYPE_NFIELDS (type
) == 0)
409 if (TYPE_NAME (type
) != NULL
)
410 fputs_filtered (" ", stream
);
413 if (is_tuple
|| is_tuple_struct
)
414 fputs_filtered ("(", stream
);
416 fputs_filtered ("{", stream
);
422 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
424 if (field_is_static (&TYPE_FIELD (type
, i
)))
428 fputs_filtered (",", stream
);
430 if (options
->prettyformat
)
432 fputs_filtered ("\n", stream
);
433 print_spaces_filtered (2 + 2 * recurse
, stream
);
435 else if (!first_field
)
436 fputs_filtered (" ", stream
);
440 if (!is_tuple
&& !is_tuple_struct
)
442 fputs_styled (TYPE_FIELD_NAME (type
, i
),
443 variable_name_style
.style (), stream
);
444 fputs_filtered (": ", stream
);
447 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
451 if (options
->prettyformat
)
453 fputs_filtered ("\n", stream
);
454 print_spaces_filtered (2 * recurse
, stream
);
457 if (is_tuple
|| is_tuple_struct
)
458 fputs_filtered (")", stream
);
460 fputs_filtered ("}", stream
);
463 /* rust_val_print helper for discriminated unions (Rust enums). */
466 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
467 const struct value_print_options
*options
)
469 struct value_print_options opts
= *options
;
470 struct type
*type
= check_typedef (value_type (val
));
474 if (rust_empty_enum_p (type
))
476 /* Print the enum type name here to be more clear. */
477 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
479 metadata_style
.style ().ptr (), nullptr);
483 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
484 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
485 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
487 int nfields
= TYPE_NFIELDS (variant_type
);
489 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
491 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
494 /* In case of a nullary variant like 'None', just output
499 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
501 fprintf_filtered (stream
, "(");
504 /* struct variant. */
505 fprintf_filtered (stream
, "{");
508 struct value
*union_value
= value_field (val
, 0);
509 int fieldno
= (variant_field
- &TYPE_FIELD (value_type (union_value
), 0));
510 val
= value_field (union_value
, fieldno
);
512 bool first_field
= true;
513 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
516 fputs_filtered (", ", stream
);
520 fprintf_filtered (stream
, "%ps: ",
521 styled_string (variable_name_style
.style (),
522 TYPE_FIELD_NAME (variant_type
, j
)));
524 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
529 fputs_filtered (")", stream
);
531 fputs_filtered ("}", stream
);
534 static const struct generic_val_print_decorations rust_decorations
=
536 /* Complex isn't used in Rust, but we provide C-ish values just in
548 /* la_value_print_inner implementation for Rust. */
550 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
552 const struct value_print_options
*options
)
554 struct value_print_options opts
= *options
;
557 if (opts
.prettyformat
== Val_prettyformat_default
)
558 opts
.prettyformat
= (opts
.prettyformat_structs
559 ? Val_prettyformat
: Val_no_prettyformat
);
561 struct type
*type
= check_typedef (value_type (val
));
562 switch (TYPE_CODE (type
))
566 LONGEST low_bound
, high_bound
;
568 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
569 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
570 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
573 /* We have a pointer to a byte string, so just print
575 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
576 CORE_ADDR addr
= value_as_address (val
);
577 struct gdbarch
*arch
= get_type_arch (type
);
579 if (opts
.addressprint
)
581 fputs_filtered (paddress (arch
, addr
), stream
);
582 fputs_filtered (" ", stream
);
585 fputs_filtered ("b", stream
);
586 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
587 high_bound
- low_bound
+ 1, stream
,
594 case TYPE_CODE_METHODPTR
:
595 case TYPE_CODE_MEMBERPTR
:
596 c_value_print_inner (val
, stream
, recurse
, &opts
);
600 /* Recognize the unit type. */
601 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
602 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
604 fputs_filtered ("()", stream
);
609 case TYPE_CODE_STRING
:
611 LONGEST low_bound
, high_bound
;
613 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
614 error (_("Could not determine the array bounds"));
616 /* If we see a plain TYPE_CODE_STRING, then we're printing a
617 byte string, hence the choice of "ASCII" as the
619 fputs_filtered ("b", stream
);
620 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
621 value_contents_for_printing (val
),
622 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
626 case TYPE_CODE_ARRAY
:
628 LONGEST low_bound
, high_bound
;
630 if (get_array_bounds (type
, &low_bound
, &high_bound
)
631 && high_bound
- low_bound
+ 1 == 0)
632 fputs_filtered ("[]", stream
);
638 case TYPE_CODE_UNION
:
639 /* Untagged unions are printed as if they are structs. Since
640 the field bit positions overlap in the debuginfo, the code
641 for printing a union is same as that for a struct, the only
642 difference is that the input type will have overlapping
644 val_print_struct (val
, stream
, recurse
, &opts
);
647 case TYPE_CODE_STRUCT
:
648 if (rust_enum_p (type
))
649 rust_print_enum (val
, stream
, recurse
, &opts
);
651 val_print_struct (val
, stream
, recurse
, &opts
);
656 /* Nothing special yet. */
657 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
664 rust_internal_print_type (struct type
*type
, const char *varstring
,
665 struct ui_file
*stream
, int show
, int level
,
666 const struct type_print_options
*flags
,
667 bool for_rust_enum
, print_offset_data
*podata
);
669 /* Print a struct or union typedef. */
671 rust_print_struct_def (struct type
*type
, const char *varstring
,
672 struct ui_file
*stream
, int show
, int level
,
673 const struct type_print_options
*flags
,
674 bool for_rust_enum
, print_offset_data
*podata
)
676 /* Print a tuple type simply. */
677 if (rust_tuple_type_p (type
))
679 fputs_filtered (TYPE_NAME (type
), stream
);
683 /* If we see a base class, delegate to C. */
684 if (TYPE_N_BASECLASSES (type
) > 0)
685 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
687 if (flags
->print_offsets
)
689 /* Temporarily bump the level so that the output lines up
694 /* Compute properties of TYPE here because, in the enum case, the
695 rest of the code ends up looking only at the variant part. */
696 const char *tagname
= TYPE_NAME (type
);
697 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
698 bool is_tuple
= rust_tuple_type_p (type
);
699 bool is_enum
= rust_enum_p (type
);
701 int enum_discriminant_index
= -1;
705 /* Already printing an outer enum, so nothing to print here. */
709 /* This code path is also used by unions and enums. */
712 fputs_filtered ("enum ", stream
);
714 if (rust_empty_enum_p (type
))
718 fputs_filtered (tagname
, stream
);
719 fputs_filtered (" ", stream
);
721 fputs_filtered ("{}", stream
);
725 type
= TYPE_FIELD_TYPE (type
, 0);
727 struct dynamic_prop
*discriminant_prop
728 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
729 struct discriminant_info
*info
730 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
731 enum_discriminant_index
= info
->discriminant_index
;
733 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
734 fputs_filtered ("struct ", stream
);
736 fputs_filtered ("union ", stream
);
739 fputs_filtered (tagname
, stream
);
742 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
744 if (for_rust_enum
&& !flags
->print_offsets
)
745 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
747 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
749 /* When printing offsets, we rearrange the fields into storage
750 order. This lets us show holes more clearly. We work using
751 field indices here because it simplifies calls to
752 print_offset_data::update below. */
753 std::vector
<int> fields
;
754 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
756 if (field_is_static (&TYPE_FIELD (type
, i
)))
758 if (is_enum
&& i
== enum_discriminant_index
)
760 fields
.push_back (i
);
762 if (flags
->print_offsets
)
763 std::sort (fields
.begin (), fields
.end (),
766 return (TYPE_FIELD_BITPOS (type
, a
)
767 < TYPE_FIELD_BITPOS (type
, b
));
774 gdb_assert (!field_is_static (&TYPE_FIELD (type
, i
)));
775 gdb_assert (! (is_enum
&& i
== enum_discriminant_index
));
777 if (flags
->print_offsets
)
778 podata
->update (type
, i
, stream
);
780 /* We'd like to print "pub" here as needed, but rustc
781 doesn't emit the debuginfo, and our types don't have
782 cplus_struct_type attached. */
784 /* For a tuple struct we print the type but nothing
786 if (!for_rust_enum
|| flags
->print_offsets
)
787 print_spaces_filtered (level
+ 2, stream
);
789 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
791 else if (!is_tuple_struct
)
792 fprintf_filtered (stream
, "%ps: ",
793 styled_string (variable_name_style
.style (),
794 TYPE_FIELD_NAME (type
, i
)));
796 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
797 stream
, (is_enum
? show
: show
- 1),
798 level
+ 2, flags
, is_enum
, podata
);
799 if (!for_rust_enum
|| flags
->print_offsets
)
800 fputs_filtered (",\n", stream
);
801 /* Note that this check of "I" is ok because we only sorted the
802 fields by offset when print_offsets was set, so we won't take
803 this branch in that case. */
804 else if (i
+ 1 < TYPE_NFIELDS (type
))
805 fputs_filtered (", ", stream
);
808 if (flags
->print_offsets
)
810 /* Undo the temporary level increase we did above. */
812 podata
->finish (type
, level
, stream
);
813 print_spaces_filtered (print_offset_data::indentation
, stream
);
815 print_spaces_filtered (2, stream
);
817 if (!for_rust_enum
|| flags
->print_offsets
)
818 print_spaces_filtered (level
, stream
);
819 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
822 /* la_print_typedef implementation for Rust. */
825 rust_print_typedef (struct type
*type
,
826 struct symbol
*new_symbol
,
827 struct ui_file
*stream
)
829 type
= check_typedef (type
);
830 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
831 type_print (type
, "", stream
, 0);
832 fprintf_filtered (stream
, ";");
835 /* la_print_type implementation for Rust. */
838 rust_internal_print_type (struct type
*type
, const char *varstring
,
839 struct ui_file
*stream
, int show
, int level
,
840 const struct type_print_options
*flags
,
841 bool for_rust_enum
, print_offset_data
*podata
)
845 && TYPE_NAME (type
) != NULL
)
847 /* Rust calls the unit type "void" in its debuginfo,
848 but we don't want to print it as that. */
849 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
850 fputs_filtered ("()", stream
);
852 fputs_filtered (TYPE_NAME (type
), stream
);
856 type
= check_typedef (type
);
857 switch (TYPE_CODE (type
))
860 /* If we have an enum, we've already printed the type's
861 unqualified name, and there is nothing else to print
864 fputs_filtered ("()", stream
);
868 /* Delegate varargs to the C printer. */
869 if (TYPE_VARARGS (type
))
872 fputs_filtered ("fn ", stream
);
873 if (varstring
!= NULL
)
874 fputs_filtered (varstring
, stream
);
875 fputs_filtered ("(", stream
);
876 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
880 fputs_filtered (", ", stream
);
881 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
882 -1, 0, flags
, false, podata
);
884 fputs_filtered (")", stream
);
885 /* If it returns unit, we can omit the return type. */
886 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
888 fputs_filtered (" -> ", stream
);
889 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
890 -1, 0, flags
, false, podata
);
894 case TYPE_CODE_ARRAY
:
896 LONGEST low_bound
, high_bound
;
898 fputs_filtered ("[", stream
);
899 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
900 stream
, show
- 1, level
, flags
, false,
903 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
904 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
905 fprintf_filtered (stream
, "; variable length");
906 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
907 fprintf_filtered (stream
, "; %s",
908 plongest (high_bound
- low_bound
+ 1));
909 fputs_filtered ("]", stream
);
913 case TYPE_CODE_UNION
:
914 case TYPE_CODE_STRUCT
:
915 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
916 for_rust_enum
, podata
);
923 fputs_filtered ("enum ", stream
);
924 if (TYPE_NAME (type
) != NULL
)
926 fputs_filtered (TYPE_NAME (type
), stream
);
927 fputs_filtered (" ", stream
);
928 len
= strlen (TYPE_NAME (type
));
930 fputs_filtered ("{\n", stream
);
932 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
934 const char *name
= TYPE_FIELD_NAME (type
, i
);
939 && strncmp (name
, TYPE_NAME (type
), len
) == 0
941 && name
[len
+ 1] == ':')
943 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
944 styled_string (variable_name_style
.style (),
948 fputs_filtered ("}", stream
);
954 if (TYPE_NAME (type
) != nullptr)
955 fputs_filtered (TYPE_NAME (type
), stream
);
958 /* We currently can't distinguish between pointers and
960 fputs_filtered ("*mut ", stream
);
961 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
968 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
973 rust_print_type (struct type
*type
, const char *varstring
,
974 struct ui_file
*stream
, int show
, int level
,
975 const struct type_print_options
*flags
)
977 print_offset_data podata
;
978 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
979 flags
, false, &podata
);
984 /* Like arch_composite_type, but uses TYPE to decide how to allocate
985 -- either on an obstack or on a gdbarch. */
988 rust_composite_type (struct type
*original
,
990 const char *field1
, struct type
*type1
,
991 const char *field2
, struct type
*type2
)
993 struct type
*result
= alloc_type_copy (original
);
994 int i
, nfields
, bitpos
;
1002 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1003 TYPE_NAME (result
) = name
;
1005 TYPE_NFIELDS (result
) = nfields
;
1006 TYPE_FIELDS (result
)
1007 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1013 struct field
*field
= &TYPE_FIELD (result
, i
);
1015 SET_FIELD_BITPOS (*field
, bitpos
);
1016 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1018 FIELD_NAME (*field
) = field1
;
1019 FIELD_TYPE (*field
) = type1
;
1024 struct field
*field
= &TYPE_FIELD (result
, i
);
1025 unsigned align
= type_align (type2
);
1031 align
*= TARGET_CHAR_BIT
;
1032 delta
= bitpos
% align
;
1034 bitpos
+= align
- delta
;
1036 SET_FIELD_BITPOS (*field
, bitpos
);
1038 FIELD_NAME (*field
) = field2
;
1039 FIELD_TYPE (*field
) = type2
;
1044 TYPE_LENGTH (result
)
1045 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1046 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1050 /* See rust-lang.h. */
1053 rust_slice_type (const char *name
, struct type
*elt_type
,
1054 struct type
*usize_type
)
1058 elt_type
= lookup_pointer_type (elt_type
);
1059 type
= rust_composite_type (elt_type
, name
,
1060 "data_ptr", elt_type
,
1061 "length", usize_type
);
1066 enum rust_primitive_types
1068 rust_primitive_bool
,
1069 rust_primitive_char
,
1078 rust_primitive_isize
,
1079 rust_primitive_usize
,
1082 rust_primitive_unit
,
1084 nr_rust_primitive_types
1087 /* la_language_arch_info implementation for Rust. */
1090 rust_language_arch_info (struct gdbarch
*gdbarch
,
1091 struct language_arch_info
*lai
)
1093 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1095 struct type
**types
;
1096 unsigned int length
;
1098 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1101 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1102 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1103 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1104 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1105 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1106 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1107 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1108 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1109 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1110 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1112 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1113 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1114 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1116 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1117 floatformats_ieee_single
);
1118 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1119 floatformats_ieee_double
);
1121 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1123 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1124 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1125 types
[rust_primitive_usize
]);
1127 lai
->primitive_type_vector
= types
;
1128 lai
->bool_type_default
= types
[rust_primitive_bool
];
1129 lai
->string_char_type
= types
[rust_primitive_u8
];
1134 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1136 static struct value
*
1137 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1140 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1142 struct value
*function
, *result
, *arg0
;
1143 struct type
*type
, *fn_type
;
1144 const struct block
*block
;
1145 struct block_symbol sym
;
1147 /* For an ordinary function call we can simply defer to the
1148 generic implementation. */
1149 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1150 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1152 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1154 method
= &exp
->elts
[*pos
+ 1].string
;
1155 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1157 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1158 type in order to look up the method. */
1159 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1161 if (noside
== EVAL_SKIP
)
1163 for (i
= 0; i
< num_args
; ++i
)
1164 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1168 std::vector
<struct value
*> args (num_args
+ 1);
1171 /* We don't yet implement real Deref semantics. */
1172 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1173 args
[0] = value_ind (args
[0]);
1175 type
= value_type (args
[0]);
1176 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1177 && TYPE_CODE (type
) != TYPE_CODE_UNION
1178 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1179 || rust_tuple_type_p (type
))
1180 error (_("Method calls only supported on struct or enum types"));
1181 if (TYPE_NAME (type
) == NULL
)
1182 error (_("Method call on nameless type"));
1184 std::string name
= std::string (TYPE_NAME (type
)) + "::" + method
;
1186 block
= get_selected_block (0);
1187 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1188 if (sym
.symbol
== NULL
)
1189 error (_("Could not find function named '%s'"), name
.c_str ());
1191 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1192 if (TYPE_NFIELDS (fn_type
) == 0)
1193 error (_("Function '%s' takes no arguments"), name
.c_str ());
1195 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1196 args
[0] = value_addr (args
[0]);
1198 function
= address_of_variable (sym
.symbol
, block
);
1200 for (i
= 0; i
< num_args
; ++i
)
1201 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1203 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1204 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1206 result
= call_function_by_hand (function
, NULL
, args
);
1210 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1212 static struct value
*
1213 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1215 enum range_type kind
;
1216 struct value
*low
= NULL
, *high
= NULL
;
1217 struct value
*addrval
, *result
;
1219 struct type
*range_type
;
1220 struct type
*index_type
;
1221 struct type
*temp_type
;
1224 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1227 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1228 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1229 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1230 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1231 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1232 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1233 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1235 if (noside
== EVAL_SKIP
)
1236 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1243 name
= "std::ops::RangeFull";
1247 index_type
= value_type (high
);
1249 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1256 index_type
= value_type (low
);
1257 name
= "std::ops::RangeFrom";
1261 if (!types_equal (value_type (low
), value_type (high
)))
1262 error (_("Range expression with different types"));
1263 index_type
= value_type (low
);
1264 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1268 /* If we don't have an index type, just allocate this on the
1269 arch. Here any type will do. */
1270 temp_type
= (index_type
== NULL
1271 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1273 /* It would be nicer to cache the range type. */
1274 range_type
= rust_composite_type (temp_type
, name
,
1275 low
== NULL
? NULL
: "start", index_type
,
1276 high
== NULL
? NULL
: "end", index_type
);
1278 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1279 return value_zero (range_type
, lval_memory
);
1281 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1282 addr
= value_as_long (addrval
);
1283 result
= value_at_lazy (range_type
, addr
);
1287 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1290 value_assign (start
, low
);
1295 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1298 value_assign (end
, high
);
1301 result
= value_at_lazy (range_type
, addr
);
1305 /* A helper function to compute the range and kind given a range
1306 value. TYPE is the type of the range value. RANGE is the range
1307 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1308 parameters might be filled in, or might not be, depending on the
1309 kind of range this is. KIND will always be set to the appropriate
1310 value describing the kind of range, and this can be used to
1311 determine whether LOW or HIGH are valid. */
1314 rust_compute_range (struct type
*type
, struct value
*range
,
1315 LONGEST
*low
, LONGEST
*high
,
1316 enum range_type
*kind
)
1322 *kind
= BOTH_BOUND_DEFAULT
;
1324 if (TYPE_NFIELDS (type
) == 0)
1328 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1330 *kind
= HIGH_BOUND_DEFAULT
;
1331 *low
= value_as_long (value_field (range
, 0));
1334 if (TYPE_NFIELDS (type
) > i
1335 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1337 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1338 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1339 *high
= value_as_long (value_field (range
, i
));
1341 if (rust_inclusive_range_type_p (type
))
1346 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1348 static struct value
*
1349 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1352 struct value
*lhs
, *rhs
, *result
;
1353 struct type
*rhstype
;
1354 LONGEST low
, high_bound
;
1355 /* Initialized to appease the compiler. */
1356 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1361 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1362 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1364 if (noside
== EVAL_SKIP
)
1367 rhstype
= check_typedef (value_type (rhs
));
1368 if (rust_range_type_p (rhstype
))
1371 error (_("Can't take slice of array without '&'"));
1372 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1376 low
= value_as_long (rhs
);
1378 struct type
*type
= check_typedef (value_type (lhs
));
1379 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1381 struct type
*base_type
= nullptr;
1382 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1383 base_type
= TYPE_TARGET_TYPE (type
);
1384 else if (rust_slice_type_p (type
))
1386 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1388 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1390 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1394 if (base_type
== nullptr)
1395 error (_("Could not find 'data_ptr' in slice type"));
1397 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1398 base_type
= TYPE_TARGET_TYPE (type
);
1400 error (_("Cannot subscript non-array type"));
1402 struct type
*new_type
;
1405 if (rust_slice_type_p (type
))
1410 = language_lookup_primitive_type (exp
->language_defn
,
1413 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1417 new_type
= base_type
;
1419 return value_zero (new_type
, VALUE_LVAL (lhs
));
1426 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1429 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1430 error (_("Can't compute array bounds"));
1432 error (_("Found array with non-zero lower bound"));
1435 else if (rust_slice_type_p (type
))
1439 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1440 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1442 high_bound
= value_as_long (len
);
1444 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1448 high_bound
= LONGEST_MAX
;
1451 error (_("Cannot subscript non-array type"));
1454 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1457 error (_("Index less than zero"));
1458 if (low
> high_bound
)
1459 error (_("Index greater than length"));
1461 result
= value_subscript (base
, low
);
1468 struct type
*usize
, *slice
;
1470 struct value
*addrval
, *tem
;
1472 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1475 error (_("High index less than zero"));
1477 error (_("Low index greater than high index"));
1478 if (high
> high_bound
)
1479 error (_("High index greater than length"));
1481 usize
= language_lookup_primitive_type (exp
->language_defn
,
1484 const char *new_name
= ((type
!= nullptr
1485 && rust_slice_type_p (type
))
1486 ? TYPE_NAME (type
) : "&[*gdb*]");
1488 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1490 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1491 addr
= value_as_long (addrval
);
1492 tem
= value_at_lazy (slice
, addr
);
1494 value_assign (value_field (tem
, 0), value_addr (result
));
1495 value_assign (value_field (tem
, 1),
1496 value_from_longest (usize
, high
- low
));
1498 result
= value_at_lazy (slice
, addr
);
1501 result
= value_addr (result
);
1507 /* evaluate_exp implementation for Rust. */
1509 static struct value
*
1510 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1511 int *pos
, enum noside noside
)
1513 struct value
*result
;
1515 switch (exp
->elts
[*pos
].opcode
)
1519 if (noside
!= EVAL_NORMAL
)
1520 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1524 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1527 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1528 if (trait_ptr
!= NULL
)
1531 result
= value_ind (value
);
1536 case UNOP_COMPLEMENT
:
1538 struct value
*value
;
1541 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1542 if (noside
== EVAL_SKIP
)
1544 /* Preserving the type is enough. */
1547 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1548 result
= value_from_longest (value_type (value
),
1549 value_logical_not (value
));
1551 result
= value_complement (value
);
1555 case BINOP_SUBSCRIPT
:
1556 result
= rust_subscript (exp
, pos
, noside
, 0);
1560 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1566 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1567 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1570 struct value
*addrval
= NULL
;
1574 if (noside
== EVAL_NORMAL
)
1576 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1577 addr
= value_as_long (addrval
);
1578 result
= value_at_lazy (type
, addr
);
1581 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1586 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1587 if (noside
== EVAL_NORMAL
)
1589 /* This isn't quite right but will do for the time
1590 being, seeing that we can't implement the Copy
1592 value_assign (result
, init
);
1598 gdb_assert (arglen
% 2 == 0);
1599 for (i
= 0; i
< arglen
; i
+= 2)
1602 const char *fieldname
;
1603 struct value
*value
, *field
;
1605 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1607 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1609 fieldname
= &exp
->elts
[*pos
].string
;
1610 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1612 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1613 if (noside
== EVAL_NORMAL
)
1615 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1617 value_assign (field
, value
);
1621 if (noside
== EVAL_SKIP
)
1622 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1624 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1625 result
= allocate_value (type
);
1627 result
= value_at_lazy (type
, addr
);
1636 struct value
*ncopies
;
1638 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1639 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1640 copies
= value_as_long (ncopies
);
1642 error (_("Array with negative number of elements"));
1644 if (noside
== EVAL_NORMAL
)
1647 std::vector
<struct value
*> eltvec (copies
);
1649 for (i
= 0; i
< copies
; ++i
)
1651 result
= value_array (0, copies
- 1, eltvec
.data ());
1655 struct type
*arraytype
1656 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1657 result
= allocate_value (arraytype
);
1662 case STRUCTOP_ANONYMOUS
:
1664 /* Anonymous field access, i.e. foo.1. */
1666 int pc
, field_number
, nfields
;
1670 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1672 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1674 type
= value_type (lhs
);
1676 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1678 struct type
*outer_type
= NULL
;
1680 if (rust_enum_p (type
))
1682 if (rust_empty_enum_p (type
))
1683 error (_("Cannot access field %d of empty enum %s"),
1684 field_number
, TYPE_NAME (type
));
1686 const gdb_byte
*valaddr
= value_contents (lhs
);
1687 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1689 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1692 int fieldno
= (variant_field
1693 - &TYPE_FIELD (value_type (union_value
), 0));
1694 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1695 value_type (union_value
));
1697 type
= value_type (lhs
);
1700 /* Tuples and tuple structs */
1701 nfields
= TYPE_NFIELDS (type
);
1703 if (field_number
>= nfields
|| field_number
< 0)
1705 if (outer_type
!= NULL
)
1706 error(_("Cannot access field %d of variant %s::%s, "
1707 "there are only %d fields"),
1708 field_number
, TYPE_NAME (outer_type
),
1709 rust_last_path_segment (TYPE_NAME (type
)),
1712 error(_("Cannot access field %d of %s, "
1713 "there are only %d fields"),
1714 field_number
, TYPE_NAME (type
), nfields
);
1717 /* Tuples are tuple structs too. */
1718 if (!rust_tuple_struct_type_p (type
))
1720 if (outer_type
!= NULL
)
1721 error(_("Variant %s::%s is not a tuple variant"),
1722 TYPE_NAME (outer_type
),
1723 rust_last_path_segment (TYPE_NAME (type
)));
1725 error(_("Attempting to access anonymous field %d "
1726 "of %s, which is not a tuple, tuple struct, or "
1727 "tuple-like variant"),
1728 field_number
, TYPE_NAME (type
));
1731 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1734 error(_("Anonymous field access is only allowed on tuples, \
1735 tuple structs, and tuple-like enum variants"));
1739 case STRUCTOP_STRUCT
:
1746 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1747 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1748 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1750 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1751 type
= value_type (lhs
);
1752 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1754 if (rust_empty_enum_p (type
))
1755 error (_("Cannot access field %s of empty enum %s"),
1756 field_name
, TYPE_NAME (type
));
1758 const gdb_byte
*valaddr
= value_contents (lhs
);
1759 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1761 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1764 int fieldno
= (variant_field
1765 - &TYPE_FIELD (value_type (union_value
), 0));
1766 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1767 value_type (union_value
));
1769 struct type
*outer_type
= type
;
1770 type
= value_type (lhs
);
1771 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1772 error (_("Attempting to access named field %s of tuple "
1773 "variant %s::%s, which has only anonymous fields"),
1774 field_name
, TYPE_NAME (outer_type
),
1775 rust_last_path_segment (TYPE_NAME (type
)));
1779 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1782 catch (const gdb_exception_error
&except
)
1784 error (_("Could not find field %s of struct variant %s::%s"),
1785 field_name
, TYPE_NAME (outer_type
),
1786 rust_last_path_segment (TYPE_NAME (type
)));
1790 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1791 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1792 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1797 result
= rust_range (exp
, pos
, noside
);
1801 /* We might have &array[range], in which case we need to make a
1803 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1806 result
= rust_subscript (exp
, pos
, noside
, 1);
1811 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1818 /* operator_length implementation for Rust. */
1821 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1827 switch (exp
->elts
[pc
- 1].opcode
)
1830 /* We handle aggregate as a type and argument count. The first
1831 argument might be OP_OTHERS. After that the arguments
1832 alternate: first an OP_NAME, then an expression. */
1834 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1842 case STRUCTOP_ANONYMOUS
:
1853 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1861 /* op_name implementation for Rust. */
1864 rust_op_name (enum exp_opcode opcode
)
1869 return "OP_AGGREGATE";
1873 return op_name_standard (opcode
);
1877 /* dump_subexp_body implementation for Rust. */
1880 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1883 switch (exp
->elts
[elt
].opcode
)
1887 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1890 fprintf_filtered (stream
, "Type @");
1891 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1892 fprintf_filtered (stream
, " (");
1893 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1894 fprintf_filtered (stream
, "), length %d", length
);
1897 for (i
= 0; i
< length
; ++i
)
1898 elt
= dump_subexp (exp
, stream
, elt
);
1905 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1907 fprintf_filtered (stream
, "%s: %s",
1908 (exp
->elts
[elt
].opcode
== OP_STRING
1909 ? "string" : "name"),
1910 &exp
->elts
[elt
+ 2].string
);
1911 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1916 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1919 case STRUCTOP_ANONYMOUS
:
1923 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1925 fprintf_filtered (stream
, "Field number: %d", field_number
);
1926 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1935 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1942 /* print_subexp implementation for Rust. */
1945 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1946 enum precedence prec
)
1948 switch (exp
->elts
[*pos
].opcode
)
1952 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1955 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1956 fputs_filtered (" { ", stream
);
1959 for (i
= 0; i
< length
; ++i
)
1961 rust_print_subexp (exp
, pos
, stream
, prec
);
1962 fputs_filtered (", ", stream
);
1964 fputs_filtered (" }", stream
);
1970 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1972 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1973 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1979 fputs_filtered ("<<others>> (", stream
);
1981 rust_print_subexp (exp
, pos
, stream
, prec
);
1982 fputs_filtered (")", stream
);
1986 case STRUCTOP_ANONYMOUS
:
1988 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1991 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1992 fprintf_filtered (stream
, ".%d", tem
);
1998 fprintf_filtered (stream
, "[");
1999 rust_print_subexp (exp
, pos
, stream
, prec
);
2000 fprintf_filtered (stream
, "; ");
2001 rust_print_subexp (exp
, pos
, stream
, prec
);
2002 fprintf_filtered (stream
, "]");
2006 print_subexp_standard (exp
, pos
, stream
, prec
);
2011 /* operator_check implementation for Rust. */
2014 rust_operator_check (struct expression
*exp
, int pos
,
2015 int (*objfile_func
) (struct objfile
*objfile
,
2019 switch (exp
->elts
[pos
].opcode
)
2023 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2024 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2026 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2037 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2045 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2047 static struct block_symbol
2048 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2050 const struct block
*block
,
2051 const domain_enum domain
)
2053 struct block_symbol result
= {};
2055 if (symbol_lookup_debug
)
2057 fprintf_unfiltered (gdb_stdlog
,
2058 "rust_lookup_symbol_non_local"
2059 " (%s, %s (scope %s), %s)\n",
2060 name
, host_address_to_string (block
),
2061 block_scope (block
), domain_name (domain
));
2064 /* Look up bare names in the block's scope. */
2065 std::string scopedname
;
2066 if (name
[cp_find_first_component (name
)] == '\0')
2068 const char *scope
= block_scope (block
);
2070 if (scope
[0] != '\0')
2072 scopedname
= std::string (scope
) + "::" + name
;
2073 name
= scopedname
.c_str ();
2081 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2082 if (result
.symbol
== NULL
)
2083 result
= lookup_global_symbol (name
, block
, domain
);
2090 /* la_sniff_from_mangled_name for Rust. */
2093 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2095 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2096 return *demangled
!= NULL
;
2101 /* la_watch_location_expression for Rust. */
2103 static gdb::unique_xmalloc_ptr
<char>
2104 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2106 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2107 std::string name
= type_to_string (type
);
2108 return gdb::unique_xmalloc_ptr
<char>
2109 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2115 static const struct exp_descriptor exp_descriptor_rust
=
2118 rust_operator_length
,
2119 rust_operator_check
,
2121 rust_dump_subexp_body
,
2122 rust_evaluate_subexp
2125 static const char *rust_extensions
[] =
2130 extern const struct language_defn rust_language_defn
=
2140 &exp_descriptor_rust
,
2143 rust_printchar
, /* Print a character constant */
2144 rust_printstr
, /* Function to print string constant */
2145 rust_emitchar
, /* Print a single char */
2146 rust_print_type
, /* Print a type using appropriate syntax */
2147 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2148 rust_value_print_inner
, /* la_value_print_inner */
2149 c_value_print
, /* Print a top-level value */
2150 default_read_var_value
, /* la_read_var_value */
2151 NULL
, /* Language specific skip_trampoline */
2152 NULL
, /* name_of_this */
2153 false, /* la_store_sym_names_in_linkage_form_p */
2154 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2155 basic_lookup_transparent_type
,/* lookup_transparent_type */
2156 gdb_demangle
, /* Language specific symbol demangler */
2157 rust_sniff_from_mangled_name
,
2158 NULL
, /* Language specific
2159 class_name_from_physname */
2160 c_op_print_tab
, /* expression operators for printing */
2161 1, /* c-style arrays */
2162 0, /* String lower bound */
2163 default_word_break_characters
,
2164 default_collect_symbol_completion_matches
,
2165 rust_language_arch_info
,
2166 default_print_array_index
,
2167 default_pass_by_reference
,
2168 rust_watch_location_expression
,
2169 NULL
, /* la_get_symbol_name_matcher */
2170 iterate_over_symbols
,
2171 default_search_name_hash
,
2172 &default_varobj_ops
,
2175 rust_is_string_type_p
,
2176 "{...}" /* la_struct_too_deep_ellipsis */