1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2017 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"
32 #include "rust-lang.h"
38 /* Returns the last segment of a Rust path like foo::bar::baz. Will
39 not handle cases where the last segment contains generics. This
40 will return NULL if the last segment cannot be found. */
43 rust_last_path_segment (const char * path
)
45 const char *result
= strrchr (path
, ':');
52 /* See rust-lang.h. */
55 rust_crate_for_block (const struct block
*block
)
57 const char *scope
= block_scope (block
);
60 return std::string ();
62 return std::string (scope
, cp_find_first_component (scope
));
65 /* Information about the discriminant/variant of an enum */
71 /* Field number in union. Negative on error. For an encoded enum,
72 the "hidden" member will always be field 1, and the "real" member
73 will always be field 0. */
75 /* True if this is an encoded enum that has a single "real" member
76 and a single "hidden" member. */
77 unsigned int is_encoded
: 1;
80 /* The prefix of a specially-encoded enum. */
82 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
84 /* The number of the real field. */
86 #define RUST_ENCODED_ENUM_REAL 0
88 /* The number of the hidden field. */
90 #define RUST_ENCODED_ENUM_HIDDEN 1
92 /* Whether or not a TYPE_CODE_UNION value is an untagged union
93 as opposed to being a regular Rust enum. */
95 rust_union_is_untagged (struct type
*type
)
97 /* Unions must have at least one field. */
98 if (TYPE_NFIELDS (type
) == 0)
100 /* If the first field is named, but the name has the rust enum prefix,
102 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
103 strlen (RUST_ENUM_PREFIX
)) == 0)
105 /* Unions only have named fields. */
106 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
108 if (strlen (TYPE_FIELD_NAME (type
, i
)) == 0)
114 /* Utility function to get discriminant info for a given value. */
116 static struct disr_info
117 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
118 int embedded_offset
, CORE_ADDR address
,
122 struct disr_info ret
;
123 struct type
*disr_type
;
124 struct value_print_options opts
;
125 const char *name_segment
;
127 get_no_prettyformat_print_options (&opts
);
132 if (TYPE_NFIELDS (type
) == 0)
133 error (_("Encountered void enum value"));
135 /* If an enum has two values where one is empty and the other holds
136 a pointer that cannot be zero; then the Rust compiler optimizes
137 away the discriminant and instead uses a zero value in the
138 pointer field to indicate the empty variant. */
139 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
140 strlen (RUST_ENUM_PREFIX
)) == 0)
142 char *tail
, *token
, *saveptr
= NULL
;
143 unsigned long fieldno
;
144 struct type
*member_type
;
149 if (TYPE_NFIELDS (type
) != 1)
150 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
152 /* Optimized enums have only one field. */
153 member_type
= TYPE_FIELD_TYPE (type
, 0);
155 std::string
name (TYPE_FIELD_NAME (type
, 0));
156 tail
= &name
[0] + strlen (RUST_ENUM_PREFIX
);
158 /* The location of the value that doubles as a discriminant is
159 stored in the name of the field, as
160 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
161 where the fieldnos are the indices of the fields that should be
162 traversed in order to find the field (which may be several fields deep)
163 and the variantname is the name of the variant of the case when the
165 for (token
= strtok_r (tail
, "$", &saveptr
);
167 token
= strtok_r (NULL
, "$", &saveptr
))
169 if (sscanf (token
, "%lu", &fieldno
) != 1)
171 /* We have reached the enum name, which cannot start
175 if (fieldno
>= TYPE_NFIELDS (member_type
))
176 error (_("%s refers to field after end of member type"),
179 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
180 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
184 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
185 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
189 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
190 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + token
;
194 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
195 ret
.name
= (std::string (TYPE_NAME (type
)) + "::"
196 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))));
202 disr_type
= TYPE_FIELD_TYPE (type
, 0);
204 if (TYPE_NFIELDS (disr_type
) == 0)
206 /* This is a bounds check and should never be hit unless Rust
207 has changed its debuginfo format. */
208 error (_("Could not find enum discriminant field"));
210 else if (TYPE_NFIELDS (type
) == 1)
212 /* Sometimes univariant enums are encoded without a
213 discriminant. In that case, treating it as an encoded enum
214 with the first field being the actual type works. */
215 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
216 const char *last
= rust_last_path_segment (field_name
);
217 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + last
;
218 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
223 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
224 error (_("Rust debug format has changed"));
226 string_file temp_file
;
227 /* The first value of the first field (or any field)
228 is the discriminant value. */
229 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0),
230 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
231 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
235 ret
.name
= std::move (temp_file
.string ());
236 name_segment
= rust_last_path_segment (ret
.name
.c_str ());
237 if (name_segment
!= NULL
)
239 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
241 /* Sadly, the discriminant value paths do not match the type
242 field name paths ('core::option::Option::Some' vs
243 'core::option::Some'). However, enum variant names are
244 unique in the last path segment and the generics are not
245 part of this path, so we can just compare those. This is
246 hackish and would be better fixed by improving rustc's
247 metadata for enums. */
248 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
250 if (field_type
!= NULL
251 && strcmp (name_segment
,
252 rust_last_path_segment (field_type
)) == 0)
260 if (ret
.field_no
== -1 && !ret
.name
.empty ())
262 /* Somehow the discriminant wasn't found. */
263 error (_("Could not find variant of %s with discriminant %s"),
264 TYPE_TAG_NAME (type
), ret
.name
.c_str ());
270 /* See rust-lang.h. */
273 rust_tuple_type_p (struct type
*type
)
275 /* The current implementation is a bit of a hack, but there's
276 nothing else in the debuginfo to distinguish a tuple from a
278 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
279 && TYPE_TAG_NAME (type
) != NULL
280 && TYPE_TAG_NAME (type
)[0] == '(');
284 /* Return true if all non-static fields of a structlike type are in a
285 sequence like __0, __1, __2. OFFSET lets us skip fields. */
288 rust_underscore_fields (struct type
*type
, int offset
)
294 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
296 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
298 if (!field_is_static (&TYPE_FIELD (type
, i
)))
306 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
307 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
316 /* See rust-lang.h. */
319 rust_tuple_struct_type_p (struct type
*type
)
321 /* This is just an approximation until DWARF can represent Rust more
322 precisely. We exclude zero-length structs because they may not
323 be tuple structs, and there's no way to tell. */
324 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
327 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
330 rust_tuple_variant_type_p (struct type
*type
)
332 /* First field is discriminant */
333 return rust_underscore_fields (type
, 1);
336 /* Return true if TYPE is a slice type, otherwise false. */
339 rust_slice_type_p (struct type
*type
)
341 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
342 && TYPE_TAG_NAME (type
) != NULL
343 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
346 /* Return true if TYPE is a range type, otherwise false. */
349 rust_range_type_p (struct type
*type
)
353 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
354 || TYPE_NFIELDS (type
) > 2
355 || TYPE_TAG_NAME (type
) == NULL
356 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
359 if (TYPE_NFIELDS (type
) == 0)
363 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
365 if (TYPE_NFIELDS (type
) == 1)
369 else if (TYPE_NFIELDS (type
) == 2)
371 /* First field had to be "start". */
375 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
378 /* Return true if TYPE seems to be the type "u8", otherwise false. */
381 rust_u8_type_p (struct type
*type
)
383 return (TYPE_CODE (type
) == TYPE_CODE_INT
384 && TYPE_UNSIGNED (type
)
385 && TYPE_LENGTH (type
) == 1);
388 /* Return true if TYPE is a Rust character type. */
391 rust_chartype_p (struct type
*type
)
393 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
394 && TYPE_LENGTH (type
) == 4
395 && TYPE_UNSIGNED (type
));
400 /* la_emitchar implementation for Rust. */
403 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
405 if (!rust_chartype_p (type
))
406 generic_emit_char (c
, type
, stream
, quoter
,
407 target_charset (get_type_arch (type
)));
408 else if (c
== '\\' || c
== quoter
)
409 fprintf_filtered (stream
, "\\%c", c
);
411 fputs_filtered ("\\n", stream
);
413 fputs_filtered ("\\r", stream
);
415 fputs_filtered ("\\t", stream
);
417 fputs_filtered ("\\0", stream
);
418 else if (c
>= 32 && c
<= 127 && isprint (c
))
419 fputc_filtered (c
, stream
);
421 fprintf_filtered (stream
, "\\x%02x", c
);
423 fprintf_filtered (stream
, "\\u{%06x}", c
);
426 /* la_printchar implementation for Rust. */
429 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
431 fputs_filtered ("'", stream
);
432 LA_EMIT_CHAR (c
, type
, stream
, '\'');
433 fputs_filtered ("'", stream
);
436 /* la_printstr implementation for Rust. */
439 rust_printstr (struct ui_file
*stream
, struct type
*type
,
440 const gdb_byte
*string
, unsigned int length
,
441 const char *user_encoding
, int force_ellipses
,
442 const struct value_print_options
*options
)
444 /* Rust always uses UTF-8, but let the caller override this if need
446 const char *encoding
= user_encoding
;
447 if (user_encoding
== NULL
|| !*user_encoding
)
449 /* In Rust strings, characters are "u8". */
450 if (rust_u8_type_p (type
))
454 /* This is probably some C string, so let's let C deal with
456 c_printstr (stream
, type
, string
, length
, user_encoding
,
457 force_ellipses
, options
);
462 /* This is not ideal as it doesn't use our character printer. */
463 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
469 /* rust_print_type branch for structs and untagged unions. */
472 val_print_struct (struct type
*type
, int embedded_offset
,
473 CORE_ADDR address
, struct ui_file
*stream
,
474 int recurse
, struct value
*val
,
475 const struct value_print_options
*options
)
479 bool is_tuple
= rust_tuple_type_p (type
);
480 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
481 struct value_print_options opts
;
485 if (TYPE_TAG_NAME (type
) != NULL
)
486 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
488 if (TYPE_NFIELDS (type
) == 0)
491 if (TYPE_TAG_NAME (type
) != NULL
)
492 fputs_filtered (" ", stream
);
495 if (is_tuple
|| is_tuple_struct
)
496 fputs_filtered ("(", stream
);
498 fputs_filtered ("{", stream
);
504 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
506 if (field_is_static (&TYPE_FIELD (type
, i
)))
510 fputs_filtered (",", stream
);
512 if (options
->prettyformat
)
514 fputs_filtered ("\n", stream
);
515 print_spaces_filtered (2 + 2 * recurse
, stream
);
517 else if (!first_field
)
518 fputs_filtered (" ", stream
);
522 if (!is_tuple
&& !is_tuple_struct
)
524 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
525 fputs_filtered (": ", stream
);
528 val_print (TYPE_FIELD_TYPE (type
, i
),
529 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
531 stream
, recurse
+ 1, val
, &opts
,
535 if (options
->prettyformat
)
537 fputs_filtered ("\n", stream
);
538 print_spaces_filtered (2 * recurse
, stream
);
541 if (is_tuple
|| is_tuple_struct
)
542 fputs_filtered (")", stream
);
544 fputs_filtered ("}", stream
);
547 static const struct generic_val_print_decorations rust_decorations
=
549 /* Complex isn't used in Rust, but we provide C-ish values just in
561 /* la_val_print implementation for Rust. */
564 rust_val_print (struct type
*type
, int embedded_offset
,
565 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
567 const struct value_print_options
*options
)
569 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
571 type
= check_typedef (type
);
572 switch (TYPE_CODE (type
))
576 LONGEST low_bound
, high_bound
;
578 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
579 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
580 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
582 /* We have a pointer to a byte string, so just print
584 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
586 struct gdbarch
*arch
= get_type_arch (type
);
587 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
589 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
590 if (options
->addressprint
)
592 fputs_filtered (paddress (arch
, addr
), stream
);
593 fputs_filtered (" ", stream
);
596 fputs_filtered ("b", stream
);
597 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
598 high_bound
- low_bound
+ 1, stream
,
605 case TYPE_CODE_METHODPTR
:
606 case TYPE_CODE_MEMBERPTR
:
607 c_val_print (type
, embedded_offset
, address
, stream
,
608 recurse
, val
, options
);
612 /* Recognize the unit type. */
613 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
614 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
616 fputs_filtered ("()", stream
);
621 case TYPE_CODE_STRING
:
623 struct gdbarch
*arch
= get_type_arch (type
);
624 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
625 LONGEST low_bound
, high_bound
;
627 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
628 error (_("Could not determine the array bounds"));
630 /* If we see a plain TYPE_CODE_STRING, then we're printing a
631 byte string, hence the choice of "ASCII" as the
633 fputs_filtered ("b", stream
);
634 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
635 valaddr
+ embedded_offset
* unit_size
,
636 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
640 case TYPE_CODE_ARRAY
:
642 LONGEST low_bound
, high_bound
;
644 if (get_array_bounds (type
, &low_bound
, &high_bound
)
645 && high_bound
- low_bound
+ 1 == 0)
646 fputs_filtered ("[]", stream
);
652 case TYPE_CODE_UNION
:
654 int j
, nfields
, first_field
, is_tuple
, start
;
655 struct type
*variant_type
;
656 struct disr_info disr
;
657 struct value_print_options opts
;
659 /* Untagged unions are printed as if they are structs.
660 Since the field bit positions overlap in the debuginfo,
661 the code for printing a union is same as that for a struct,
662 the only difference is that the input type will have overlapping
664 if (rust_union_is_untagged (type
))
666 val_print_struct (type
, embedded_offset
, address
, stream
,
667 recurse
, val
, options
);
674 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
677 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
679 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
684 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
685 nfields
= TYPE_NFIELDS (variant_type
);
687 is_tuple
= (disr
.is_encoded
688 ? rust_tuple_struct_type_p (variant_type
)
689 : rust_tuple_variant_type_p (variant_type
));
690 start
= disr
.is_encoded
? 0 : 1;
694 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
696 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
699 /* struct variant. */
700 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
705 /* In case of a nullary variant like 'None', just output
707 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
711 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
714 fputs_filtered (", ", stream
);
718 fprintf_filtered (stream
, "%s: ",
719 TYPE_FIELD_NAME (variant_type
, j
));
721 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
723 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
724 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
726 stream
, recurse
+ 1, val
, &opts
,
731 fputs_filtered (")", stream
);
733 fputs_filtered ("}", stream
);
737 case TYPE_CODE_STRUCT
:
738 val_print_struct (type
, embedded_offset
, address
, stream
,
739 recurse
, val
, options
);
744 /* Nothing special yet. */
745 generic_val_print (type
, embedded_offset
, address
, stream
,
746 recurse
, val
, options
, &rust_decorations
);
753 rust_print_type (struct type
*type
, const char *varstring
,
754 struct ui_file
*stream
, int show
, int level
,
755 const struct type_print_options
*flags
);
757 /* Print a struct or union typedef. */
759 rust_print_struct_def (struct type
*type
, const char *varstring
,
760 struct ui_file
*stream
, int show
, int level
,
761 const struct type_print_options
*flags
)
763 bool is_tuple_struct
;
766 /* Print a tuple type simply. */
767 if (rust_tuple_type_p (type
))
769 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
773 /* If we see a base class, delegate to C. */
774 if (TYPE_N_BASECLASSES (type
) > 0)
775 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
777 /* This code path is also used by unions. */
778 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
779 fputs_filtered ("struct ", stream
);
781 fputs_filtered ("union ", stream
);
783 if (TYPE_TAG_NAME (type
) != NULL
)
784 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
786 is_tuple_struct
= rust_tuple_struct_type_p (type
);
788 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
790 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
792 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
797 if (field_is_static (&TYPE_FIELD (type
, i
)))
800 /* We'd like to print "pub" here as needed, but rustc
801 doesn't emit the debuginfo, and our types don't have
802 cplus_struct_type attached. */
804 /* For a tuple struct we print the type but nothing
806 print_spaces_filtered (level
+ 2, stream
);
807 if (!is_tuple_struct
)
808 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
810 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
811 stream
, show
- 1, level
+ 2,
813 fputs_filtered (",\n", stream
);
816 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
819 /* la_print_typedef implementation for Rust. */
822 rust_print_typedef (struct type
*type
,
823 struct symbol
*new_symbol
,
824 struct ui_file
*stream
)
826 type
= check_typedef (type
);
827 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
828 type_print (type
, "", stream
, 0);
829 fprintf_filtered (stream
, ";\n");
832 /* la_print_type implementation for Rust. */
835 rust_print_type (struct type
*type
, const char *varstring
,
836 struct ui_file
*stream
, int show
, int level
,
837 const struct type_print_options
*flags
)
843 && TYPE_NAME (type
) != NULL
)
845 /* Rust calls the unit type "void" in its debuginfo,
846 but we don't want to print it as that. */
847 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
848 fputs_filtered ("()", stream
);
850 fputs_filtered (TYPE_NAME (type
), stream
);
854 type
= check_typedef (type
);
855 switch (TYPE_CODE (type
))
858 fputs_filtered ("()", stream
);
862 /* Delegate varargs to the C printer. */
863 if (TYPE_VARARGS (type
))
866 fputs_filtered ("fn ", stream
);
867 if (varstring
!= NULL
)
868 fputs_filtered (varstring
, stream
);
869 fputs_filtered ("(", stream
);
870 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
874 fputs_filtered (", ", stream
);
875 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
878 fputs_filtered (")", stream
);
879 /* If it returns unit, we can omit the return type. */
880 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
882 fputs_filtered (" -> ", stream
);
883 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
887 case TYPE_CODE_ARRAY
:
889 LONGEST low_bound
, high_bound
;
891 fputs_filtered ("[", stream
);
892 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
893 stream
, show
- 1, level
, flags
);
895 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
896 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
897 fprintf_filtered (stream
, "; variable length");
898 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
899 fprintf_filtered (stream
, "; %s",
900 plongest (high_bound
- low_bound
+ 1));
901 fputs_filtered ("]", stream
);
905 case TYPE_CODE_STRUCT
:
906 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
913 fputs_filtered ("enum ", stream
);
914 if (TYPE_TAG_NAME (type
) != NULL
)
916 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
917 fputs_filtered (" ", stream
);
918 len
= strlen (TYPE_TAG_NAME (type
));
920 fputs_filtered ("{\n", stream
);
922 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
924 const char *name
= TYPE_FIELD_NAME (type
, i
);
929 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
931 && name
[len
+ 1] == ':')
933 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
936 fputs_filtered ("}", stream
);
940 case TYPE_CODE_UNION
:
944 /* Skip the discriminant field. */
947 /* Unions and structs have the same syntax in Rust,
948 the only difference is that structs are declared with `struct`
949 and union with `union`. This difference is handled in the struct
951 if (rust_union_is_untagged (type
))
953 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
957 fputs_filtered ("enum ", stream
);
958 if (TYPE_TAG_NAME (type
) != NULL
)
960 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
961 fputs_filtered (" ", stream
);
963 fputs_filtered ("{\n", stream
);
965 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
966 strlen (RUST_ENUM_PREFIX
)) == 0)
968 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
969 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
971 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
972 /* There is no explicit discriminant field, skip nothing. */
976 else if (TYPE_NFIELDS (type
) == 1)
979 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
981 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
983 = rust_last_path_segment (TYPE_NAME (variant_type
));
985 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
987 if (TYPE_NFIELDS (variant_type
) > skip_to
)
990 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
991 ? rust_tuple_struct_type_p (variant_type
)
992 : rust_tuple_variant_type_p (variant_type
));
995 fputs_filtered (is_tuple
? "(" : "{", stream
);
996 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1001 fputs_filtered (", ", stream
);
1004 fprintf_filtered (stream
, "%s: ",
1005 TYPE_FIELD_NAME (variant_type
, j
));
1007 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1008 stream
, show
- 1, level
+ 2,
1011 fputs_filtered (is_tuple
? ")" : "}", stream
);
1014 fputs_filtered (",\n", stream
);
1017 fputs_filtered ("}", stream
);
1023 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1029 /* Compute the alignment of the type T. */
1032 rust_type_alignment (struct type
*t
)
1034 t
= check_typedef (t
);
1035 switch (TYPE_CODE (t
))
1038 error (_("Could not compute alignment of type"));
1041 case TYPE_CODE_ENUM
:
1045 case TYPE_CODE_CHAR
:
1046 case TYPE_CODE_BOOL
:
1047 return TYPE_LENGTH (t
);
1049 case TYPE_CODE_ARRAY
:
1050 case TYPE_CODE_COMPLEX
:
1051 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1053 case TYPE_CODE_STRUCT
:
1054 case TYPE_CODE_UNION
:
1059 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1061 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1070 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1071 -- either on an obstack or on a gdbarch. */
1073 static struct type
*
1074 rust_composite_type (struct type
*original
,
1076 const char *field1
, struct type
*type1
,
1077 const char *field2
, struct type
*type2
)
1079 struct type
*result
= alloc_type_copy (original
);
1080 int i
, nfields
, bitpos
;
1088 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1089 TYPE_NAME (result
) = name
;
1090 TYPE_TAG_NAME (result
) = name
;
1092 TYPE_NFIELDS (result
) = nfields
;
1093 TYPE_FIELDS (result
)
1094 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1100 struct field
*field
= &TYPE_FIELD (result
, i
);
1102 SET_FIELD_BITPOS (*field
, bitpos
);
1103 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1105 FIELD_NAME (*field
) = field1
;
1106 FIELD_TYPE (*field
) = type1
;
1111 struct field
*field
= &TYPE_FIELD (result
, i
);
1112 int align
= rust_type_alignment (type2
);
1118 align
*= TARGET_CHAR_BIT
;
1119 delta
= bitpos
% align
;
1121 bitpos
+= align
- delta
;
1123 SET_FIELD_BITPOS (*field
, bitpos
);
1125 FIELD_NAME (*field
) = field2
;
1126 FIELD_TYPE (*field
) = type2
;
1131 TYPE_LENGTH (result
)
1132 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1133 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1137 /* See rust-lang.h. */
1140 rust_slice_type (const char *name
, struct type
*elt_type
,
1141 struct type
*usize_type
)
1145 elt_type
= lookup_pointer_type (elt_type
);
1146 type
= rust_composite_type (elt_type
, name
,
1147 "data_ptr", elt_type
,
1148 "length", usize_type
);
1153 enum rust_primitive_types
1155 rust_primitive_bool
,
1156 rust_primitive_char
,
1165 rust_primitive_isize
,
1166 rust_primitive_usize
,
1169 rust_primitive_unit
,
1171 nr_rust_primitive_types
1174 /* la_language_arch_info implementation for Rust. */
1177 rust_language_arch_info (struct gdbarch
*gdbarch
,
1178 struct language_arch_info
*lai
)
1180 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1182 struct type
**types
;
1183 unsigned int length
;
1185 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1188 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1189 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1190 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1191 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1192 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1193 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1194 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1195 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1196 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1197 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1199 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1200 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1201 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1203 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1204 floatformats_ieee_single
);
1205 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1206 floatformats_ieee_double
);
1208 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1210 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1211 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1212 types
[rust_primitive_usize
]);
1214 lai
->primitive_type_vector
= types
;
1215 lai
->bool_type_default
= types
[rust_primitive_bool
];
1216 lai
->string_char_type
= types
[rust_primitive_u8
];
1221 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1223 static struct value
*
1224 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1227 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1229 struct value
*function
, *result
, *arg0
;
1230 struct type
*type
, *fn_type
;
1231 const struct block
*block
;
1232 struct block_symbol sym
;
1234 /* For an ordinary function call we can simply defer to the
1235 generic implementation. */
1236 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1237 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1239 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1241 method
= &exp
->elts
[*pos
+ 1].string
;
1242 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1244 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1245 type in order to look up the method. */
1246 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1248 if (noside
== EVAL_SKIP
)
1250 for (i
= 0; i
< num_args
; ++i
)
1251 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1255 std::vector
<struct value
*> args (num_args
+ 1);
1258 /* We don't yet implement real Deref semantics. */
1259 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1260 args
[0] = value_ind (args
[0]);
1262 type
= value_type (args
[0]);
1263 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1264 && TYPE_CODE (type
) != TYPE_CODE_UNION
1265 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1266 || rust_tuple_type_p (type
))
1267 error (_("Method calls only supported on struct or enum types"));
1268 if (TYPE_TAG_NAME (type
) == NULL
)
1269 error (_("Method call on nameless type"));
1271 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1273 block
= get_selected_block (0);
1274 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1275 if (sym
.symbol
== NULL
)
1276 error (_("Could not find function named '%s'"), name
.c_str ());
1278 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1279 if (TYPE_NFIELDS (fn_type
) == 0)
1280 error (_("Function '%s' takes no arguments"), name
.c_str ());
1282 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1283 args
[0] = value_addr (args
[0]);
1285 function
= address_of_variable (sym
.symbol
, block
);
1287 for (i
= 0; i
< num_args
; ++i
)
1288 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1290 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1291 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1293 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1297 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1299 static struct value
*
1300 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1302 enum range_type kind
;
1303 struct value
*low
= NULL
, *high
= NULL
;
1304 struct value
*addrval
, *result
;
1306 struct type
*range_type
;
1307 struct type
*index_type
;
1308 struct type
*temp_type
;
1311 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1314 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1315 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1316 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1317 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1319 if (noside
== EVAL_SKIP
)
1320 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1327 name
= "std::ops::RangeFull";
1331 index_type
= value_type (high
);
1332 name
= "std::ops::RangeTo";
1339 index_type
= value_type (low
);
1340 name
= "std::ops::RangeFrom";
1344 if (!types_equal (value_type (low
), value_type (high
)))
1345 error (_("Range expression with different types"));
1346 index_type
= value_type (low
);
1347 name
= "std::ops::Range";
1351 /* If we don't have an index type, just allocate this on the
1352 arch. Here any type will do. */
1353 temp_type
= (index_type
== NULL
1354 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1356 /* It would be nicer to cache the range type. */
1357 range_type
= rust_composite_type (temp_type
, name
,
1358 low
== NULL
? NULL
: "start", index_type
,
1359 high
== NULL
? NULL
: "end", index_type
);
1361 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1362 return value_zero (range_type
, lval_memory
);
1364 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1365 addr
= value_as_long (addrval
);
1366 result
= value_at_lazy (range_type
, addr
);
1370 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1373 value_assign (start
, low
);
1378 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1381 value_assign (end
, high
);
1384 result
= value_at_lazy (range_type
, addr
);
1388 /* A helper function to compute the range and kind given a range
1389 value. TYPE is the type of the range value. RANGE is the range
1390 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1391 parameters might be filled in, or might not be, depending on the
1392 kind of range this is. KIND will always be set to the appropriate
1393 value describing the kind of range, and this can be used to
1394 determine whether LOW or HIGH are valid. */
1397 rust_compute_range (struct type
*type
, struct value
*range
,
1398 LONGEST
*low
, LONGEST
*high
,
1399 enum range_type
*kind
)
1405 *kind
= BOTH_BOUND_DEFAULT
;
1407 if (TYPE_NFIELDS (type
) == 0)
1411 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1413 *kind
= HIGH_BOUND_DEFAULT
;
1414 *low
= value_as_long (value_field (range
, 0));
1417 if (TYPE_NFIELDS (type
) > i
1418 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1420 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1421 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1422 *high
= value_as_long (value_field (range
, i
));
1426 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1428 static struct value
*
1429 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1432 struct value
*lhs
, *rhs
, *result
;
1433 struct type
*rhstype
;
1434 LONGEST low
, high_bound
;
1435 /* Initialized to appease the compiler. */
1436 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1441 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1442 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1444 if (noside
== EVAL_SKIP
)
1447 rhstype
= check_typedef (value_type (rhs
));
1448 if (rust_range_type_p (rhstype
))
1451 error (_("Can't take slice of array without '&'"));
1452 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1456 low
= value_as_long (rhs
);
1458 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1460 struct type
*type
= check_typedef (value_type (lhs
));
1462 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1468 struct type
*type
= check_typedef (value_type (lhs
));
1470 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1473 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1474 error (_("Can't compute array bounds"));
1476 error (_("Found array with non-zero lower bound"));
1479 else if (rust_slice_type_p (type
))
1483 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1484 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1486 high_bound
= value_as_long (len
);
1488 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1492 high_bound
= LONGEST_MAX
;
1495 error (_("Cannot subscript non-array type"));
1498 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1501 error (_("Index less than zero"));
1502 if (low
> high_bound
)
1503 error (_("Index greater than length"));
1505 result
= value_subscript (base
, low
);
1512 struct type
*usize
, *slice
;
1514 struct value
*addrval
, *tem
;
1516 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1519 error (_("High index less than zero"));
1521 error (_("Low index greater than high index"));
1522 if (high
> high_bound
)
1523 error (_("High index greater than length"));
1525 usize
= language_lookup_primitive_type (exp
->language_defn
,
1528 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1531 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1532 addr
= value_as_long (addrval
);
1533 tem
= value_at_lazy (slice
, addr
);
1535 value_assign (value_field (tem
, 0), value_addr (result
));
1536 value_assign (value_field (tem
, 1),
1537 value_from_longest (usize
, high
- low
));
1539 result
= value_at_lazy (slice
, addr
);
1542 result
= value_addr (result
);
1548 /* evaluate_exp implementation for Rust. */
1550 static struct value
*
1551 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1552 int *pos
, enum noside noside
)
1554 struct value
*result
;
1556 switch (exp
->elts
[*pos
].opcode
)
1558 case UNOP_COMPLEMENT
:
1560 struct value
*value
;
1563 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1564 if (noside
== EVAL_SKIP
)
1566 /* Preserving the type is enough. */
1569 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1570 result
= value_from_longest (value_type (value
),
1571 value_logical_not (value
));
1573 result
= value_complement (value
);
1577 case BINOP_SUBSCRIPT
:
1578 result
= rust_subscript (exp
, pos
, noside
, 0);
1582 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1588 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1589 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1592 struct value
*addrval
= NULL
;
1596 if (noside
== EVAL_NORMAL
)
1598 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1599 addr
= value_as_long (addrval
);
1600 result
= value_at_lazy (type
, addr
);
1603 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1608 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1609 if (noside
== EVAL_NORMAL
)
1611 /* This isn't quite right but will do for the time
1612 being, seeing that we can't implement the Copy
1614 value_assign (result
, init
);
1620 gdb_assert (arglen
% 2 == 0);
1621 for (i
= 0; i
< arglen
; i
+= 2)
1624 const char *fieldname
;
1625 struct value
*value
, *field
;
1627 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1629 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1631 fieldname
= &exp
->elts
[*pos
].string
;
1632 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1634 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1635 if (noside
== EVAL_NORMAL
)
1637 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1639 value_assign (field
, value
);
1643 if (noside
== EVAL_SKIP
)
1644 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1646 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1647 result
= allocate_value (type
);
1649 result
= value_at_lazy (type
, addr
);
1658 struct value
*ncopies
;
1660 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1661 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1662 copies
= value_as_long (ncopies
);
1664 error (_("Array with negative number of elements"));
1666 if (noside
== EVAL_NORMAL
)
1670 std::vector
<struct value
*> eltvec (copies
);
1672 for (i
= 0; i
< copies
; ++i
)
1674 result
= value_array (0, copies
- 1, eltvec
.data ());
1678 struct type
*arraytype
1679 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1680 result
= allocate_value (arraytype
);
1685 case STRUCTOP_ANONYMOUS
:
1687 /* Anonymous field access, i.e. foo.1. */
1689 int pc
, field_number
, nfields
;
1690 struct type
*type
, *variant_type
;
1691 struct disr_info disr
;
1694 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1696 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1698 type
= value_type (lhs
);
1699 /* Untagged unions can't have anonymous field access since
1700 they can only have named fields. */
1701 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1702 && !rust_union_is_untagged (type
))
1704 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1705 value_embedded_offset (lhs
),
1706 value_address (lhs
), lhs
);
1708 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1710 variant_type
= NULL
;
1715 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1716 nfields
= TYPE_NFIELDS (variant_type
);
1719 if (!disr
.is_encoded
)
1722 if (field_number
>= nfields
|| field_number
< 0)
1723 error(_("Cannot access field %d of variant %s, \
1724 there are only %d fields"),
1725 disr
.is_encoded
? field_number
: field_number
- 1,
1727 disr
.is_encoded
? nfields
: nfields
- 1);
1729 if (!(disr
.is_encoded
1730 ? rust_tuple_struct_type_p (variant_type
)
1731 : rust_tuple_variant_type_p (variant_type
)))
1732 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1734 result
= value_primitive_field (lhs
, 0, field_number
,
1737 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1739 /* Tuples and tuple structs */
1740 nfields
= TYPE_NFIELDS(type
);
1742 if (field_number
>= nfields
|| field_number
< 0)
1743 error(_("Cannot access field %d of %s, there are only %d fields"),
1744 field_number
, TYPE_TAG_NAME (type
), nfields
);
1746 /* Tuples are tuple structs too. */
1747 if (!rust_tuple_struct_type_p (type
))
1748 error(_("Attempting to access anonymous field %d of %s, which is \
1749 not a tuple, tuple struct, or tuple-like variant"),
1750 field_number
, TYPE_TAG_NAME (type
));
1752 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1755 error(_("Anonymous field access is only allowed on tuples, \
1756 tuple structs, and tuple-like enum variants"));
1760 case STRUCTOP_STRUCT
:
1767 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1768 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1769 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1771 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1772 type
= value_type (lhs
);
1773 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1774 && !rust_union_is_untagged (type
))
1777 struct disr_info disr
;
1778 struct type
*variant_type
;
1780 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1781 value_embedded_offset (lhs
),
1782 value_address (lhs
), lhs
);
1784 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1785 error(_("Could not find field %s of struct variant %s"),
1786 field_name
, disr
.name
.c_str ());
1788 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1790 if (variant_type
== NULL
1792 ? rust_tuple_struct_type_p (variant_type
)
1793 : rust_tuple_variant_type_p (variant_type
)))
1794 error(_("Attempting to access named field %s of tuple variant %s, \
1795 which has only anonymous fields"),
1796 field_name
, disr
.name
.c_str ());
1798 start
= disr
.is_encoded
? 0 : 1;
1799 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1801 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1803 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1808 if (i
== TYPE_NFIELDS (variant_type
))
1809 /* We didn't find it. */
1810 error(_("Could not find field %s of struct variant %s"),
1811 field_name
, disr
.name
.c_str ());
1815 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
,
1817 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1818 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1824 result
= rust_range (exp
, pos
, noside
);
1828 /* We might have &array[range], in which case we need to make a
1830 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1833 result
= rust_subscript (exp
, pos
, noside
, 1);
1838 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1845 /* operator_length implementation for Rust. */
1848 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1854 switch (exp
->elts
[pc
- 1].opcode
)
1857 /* We handle aggregate as a type and argument count. The first
1858 argument might be OP_OTHERS. After that the arguments
1859 alternate: first an OP_NAME, then an expression. */
1861 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1869 case STRUCTOP_ANONYMOUS
:
1880 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1888 /* op_name implementation for Rust. */
1891 rust_op_name (enum exp_opcode opcode
)
1896 return "OP_AGGREGATE";
1900 return op_name_standard (opcode
);
1904 /* dump_subexp_body implementation for Rust. */
1907 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1910 switch (exp
->elts
[elt
].opcode
)
1914 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1917 fprintf_filtered (stream
, "Type @");
1918 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1919 fprintf_filtered (stream
, " (");
1920 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1921 fprintf_filtered (stream
, "), length %d", length
);
1924 for (i
= 0; i
< length
; ++i
)
1925 elt
= dump_subexp (exp
, stream
, elt
);
1932 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1934 fprintf_filtered (stream
, "%s: %s",
1935 (exp
->elts
[elt
].opcode
== OP_STRING
1936 ? "string" : "name"),
1937 &exp
->elts
[elt
+ 2].string
);
1938 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1943 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1946 case STRUCTOP_ANONYMOUS
:
1950 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1952 fprintf_filtered (stream
, "Field number: %d", field_number
);
1953 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1962 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1969 /* print_subexp implementation for Rust. */
1972 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1973 enum precedence prec
)
1975 switch (exp
->elts
[*pos
].opcode
)
1979 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1982 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1983 fputs_filtered (" { ", stream
);
1986 for (i
= 0; i
< length
; ++i
)
1988 rust_print_subexp (exp
, pos
, stream
, prec
);
1989 fputs_filtered (", ", stream
);
1991 fputs_filtered (" }", stream
);
1997 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1999 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2000 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2006 fputs_filtered ("<<others>> (", stream
);
2008 rust_print_subexp (exp
, pos
, stream
, prec
);
2009 fputs_filtered (")", stream
);
2013 case STRUCTOP_ANONYMOUS
:
2015 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2018 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2019 fprintf_filtered (stream
, ".%d", tem
);
2025 fprintf_filtered (stream
, "[");
2026 rust_print_subexp (exp
, pos
, stream
, prec
);
2027 fprintf_filtered (stream
, "; ");
2028 rust_print_subexp (exp
, pos
, stream
, prec
);
2029 fprintf_filtered (stream
, "]");
2033 print_subexp_standard (exp
, pos
, stream
, prec
);
2038 /* operator_check implementation for Rust. */
2041 rust_operator_check (struct expression
*exp
, int pos
,
2042 int (*objfile_func
) (struct objfile
*objfile
,
2046 switch (exp
->elts
[pos
].opcode
)
2050 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2051 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2053 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2064 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2072 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2074 static struct block_symbol
2075 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2077 const struct block
*block
,
2078 const domain_enum domain
)
2080 struct block_symbol result
= {NULL
, NULL
};
2082 if (symbol_lookup_debug
)
2084 fprintf_unfiltered (gdb_stdlog
,
2085 "rust_lookup_symbol_non_local"
2086 " (%s, %s (scope %s), %s)\n",
2087 name
, host_address_to_string (block
),
2088 block_scope (block
), domain_name (domain
));
2091 /* Look up bare names in the block's scope. */
2092 if (name
[cp_find_first_component (name
)] == '\0')
2094 const char *scope
= block_scope (block
);
2096 if (scope
[0] != '\0')
2098 std::string scopedname
= std::string (scope
) + "::" + name
;
2100 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2102 if (result
.symbol
== NULL
)
2103 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2111 /* la_sniff_from_mangled_name for Rust. */
2114 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2116 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2117 return *demangled
!= NULL
;
2122 /* la_watch_location_expression for Rust. */
2124 static gdb::unique_xmalloc_ptr
<char>
2125 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2127 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2128 std::string name
= type_to_string (type
);
2129 return gdb::unique_xmalloc_ptr
<char>
2130 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2136 static const struct exp_descriptor exp_descriptor_rust
=
2139 rust_operator_length
,
2140 rust_operator_check
,
2142 rust_dump_subexp_body
,
2143 rust_evaluate_subexp
2146 static const char *rust_extensions
[] =
2151 extern const struct language_defn rust_language_defn
=
2161 &exp_descriptor_rust
,
2165 rust_printchar
, /* Print a character constant */
2166 rust_printstr
, /* Function to print string constant */
2167 rust_emitchar
, /* Print a single char */
2168 rust_print_type
, /* Print a type using appropriate syntax */
2169 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2170 rust_val_print
, /* Print a value using appropriate syntax */
2171 c_value_print
, /* Print a top-level value */
2172 default_read_var_value
, /* la_read_var_value */
2173 NULL
, /* Language specific skip_trampoline */
2174 NULL
, /* name_of_this */
2175 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2176 basic_lookup_transparent_type
,/* lookup_transparent_type */
2177 gdb_demangle
, /* Language specific symbol demangler */
2178 rust_sniff_from_mangled_name
,
2179 NULL
, /* Language specific
2180 class_name_from_physname */
2181 c_op_print_tab
, /* expression operators for printing */
2182 1, /* c-style arrays */
2183 0, /* String lower bound */
2184 default_word_break_characters
,
2185 default_collect_symbol_completion_matches
,
2186 rust_language_arch_info
,
2187 default_print_array_index
,
2188 default_pass_by_reference
,
2190 rust_watch_location_expression
,
2191 NULL
, /* la_get_symbol_name_cmp */
2192 iterate_over_symbols
,
2193 &default_varobj_ops
,