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
344 || strcmp (TYPE_TAG_NAME (type
), "&str") == 0));
347 /* Return true if TYPE is a range type, otherwise false. */
350 rust_range_type_p (struct type
*type
)
354 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
355 || TYPE_NFIELDS (type
) > 2
356 || TYPE_TAG_NAME (type
) == NULL
357 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
360 if (TYPE_NFIELDS (type
) == 0)
364 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
366 if (TYPE_NFIELDS (type
) == 1)
370 else if (TYPE_NFIELDS (type
) == 2)
372 /* First field had to be "start". */
376 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
379 /* Return true if TYPE seems to be the type "u8", otherwise false. */
382 rust_u8_type_p (struct type
*type
)
384 return (TYPE_CODE (type
) == TYPE_CODE_INT
385 && TYPE_UNSIGNED (type
)
386 && TYPE_LENGTH (type
) == 1);
389 /* Return true if TYPE is a Rust character type. */
392 rust_chartype_p (struct type
*type
)
394 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
395 && TYPE_LENGTH (type
) == 4
396 && TYPE_UNSIGNED (type
));
401 /* la_emitchar implementation for Rust. */
404 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
406 if (!rust_chartype_p (type
))
407 generic_emit_char (c
, type
, stream
, quoter
,
408 target_charset (get_type_arch (type
)));
409 else if (c
== '\\' || c
== quoter
)
410 fprintf_filtered (stream
, "\\%c", c
);
412 fputs_filtered ("\\n", stream
);
414 fputs_filtered ("\\r", stream
);
416 fputs_filtered ("\\t", stream
);
418 fputs_filtered ("\\0", stream
);
419 else if (c
>= 32 && c
<= 127 && isprint (c
))
420 fputc_filtered (c
, stream
);
422 fprintf_filtered (stream
, "\\x%02x", c
);
424 fprintf_filtered (stream
, "\\u{%06x}", c
);
427 /* la_printchar implementation for Rust. */
430 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
432 fputs_filtered ("'", stream
);
433 LA_EMIT_CHAR (c
, type
, stream
, '\'');
434 fputs_filtered ("'", stream
);
437 /* la_printstr implementation for Rust. */
440 rust_printstr (struct ui_file
*stream
, struct type
*type
,
441 const gdb_byte
*string
, unsigned int length
,
442 const char *user_encoding
, int force_ellipses
,
443 const struct value_print_options
*options
)
445 /* Rust always uses UTF-8, but let the caller override this if need
447 const char *encoding
= user_encoding
;
448 if (user_encoding
== NULL
|| !*user_encoding
)
450 /* In Rust strings, characters are "u8". */
451 if (rust_u8_type_p (type
))
455 /* This is probably some C string, so let's let C deal with
457 c_printstr (stream
, type
, string
, length
, user_encoding
,
458 force_ellipses
, options
);
463 /* This is not ideal as it doesn't use our character printer. */
464 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
470 /* Helper function to print a string slice. */
473 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
474 const struct value_print_options
*options
)
476 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
478 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
480 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
481 value_as_address (base
), value_as_long (len
), stream
,
485 /* rust_print_type branch for structs and untagged unions. */
488 val_print_struct (struct type
*type
, int embedded_offset
,
489 CORE_ADDR address
, struct ui_file
*stream
,
490 int recurse
, struct value
*val
,
491 const struct value_print_options
*options
)
496 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
498 rust_val_print_str (stream
, val
, options
);
502 bool is_tuple
= rust_tuple_type_p (type
);
503 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
504 struct value_print_options opts
;
508 if (TYPE_TAG_NAME (type
) != NULL
)
509 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
511 if (TYPE_NFIELDS (type
) == 0)
514 if (TYPE_TAG_NAME (type
) != NULL
)
515 fputs_filtered (" ", stream
);
518 if (is_tuple
|| is_tuple_struct
)
519 fputs_filtered ("(", stream
);
521 fputs_filtered ("{", stream
);
527 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
529 if (field_is_static (&TYPE_FIELD (type
, i
)))
533 fputs_filtered (",", stream
);
535 if (options
->prettyformat
)
537 fputs_filtered ("\n", stream
);
538 print_spaces_filtered (2 + 2 * recurse
, stream
);
540 else if (!first_field
)
541 fputs_filtered (" ", stream
);
545 if (!is_tuple
&& !is_tuple_struct
)
547 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
548 fputs_filtered (": ", stream
);
551 val_print (TYPE_FIELD_TYPE (type
, i
),
552 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
554 stream
, recurse
+ 1, val
, &opts
,
558 if (options
->prettyformat
)
560 fputs_filtered ("\n", stream
);
561 print_spaces_filtered (2 * recurse
, stream
);
564 if (is_tuple
|| is_tuple_struct
)
565 fputs_filtered (")", stream
);
567 fputs_filtered ("}", stream
);
570 static const struct generic_val_print_decorations rust_decorations
=
572 /* Complex isn't used in Rust, but we provide C-ish values just in
584 /* la_val_print implementation for Rust. */
587 rust_val_print (struct type
*type
, int embedded_offset
,
588 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
590 const struct value_print_options
*options
)
592 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
594 type
= check_typedef (type
);
595 switch (TYPE_CODE (type
))
599 LONGEST low_bound
, high_bound
;
601 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
602 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
603 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
605 /* We have a pointer to a byte string, so just print
607 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
609 struct gdbarch
*arch
= get_type_arch (type
);
610 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
612 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
613 if (options
->addressprint
)
615 fputs_filtered (paddress (arch
, addr
), stream
);
616 fputs_filtered (" ", stream
);
619 fputs_filtered ("b", stream
);
620 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
621 high_bound
- low_bound
+ 1, stream
,
628 case TYPE_CODE_METHODPTR
:
629 case TYPE_CODE_MEMBERPTR
:
630 c_val_print (type
, embedded_offset
, address
, stream
,
631 recurse
, val
, options
);
635 /* Recognize the unit type. */
636 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
637 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
639 fputs_filtered ("()", stream
);
644 case TYPE_CODE_STRING
:
646 struct gdbarch
*arch
= get_type_arch (type
);
647 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
648 LONGEST low_bound
, high_bound
;
650 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
651 error (_("Could not determine the array bounds"));
653 /* If we see a plain TYPE_CODE_STRING, then we're printing a
654 byte string, hence the choice of "ASCII" as the
656 fputs_filtered ("b", stream
);
657 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
658 valaddr
+ embedded_offset
* unit_size
,
659 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
663 case TYPE_CODE_ARRAY
:
665 LONGEST low_bound
, high_bound
;
667 if (get_array_bounds (type
, &low_bound
, &high_bound
)
668 && high_bound
- low_bound
+ 1 == 0)
669 fputs_filtered ("[]", stream
);
675 case TYPE_CODE_UNION
:
677 int j
, nfields
, first_field
, is_tuple
, start
;
678 struct type
*variant_type
;
679 struct disr_info disr
;
680 struct value_print_options opts
;
682 /* Untagged unions are printed as if they are structs.
683 Since the field bit positions overlap in the debuginfo,
684 the code for printing a union is same as that for a struct,
685 the only difference is that the input type will have overlapping
687 if (rust_union_is_untagged (type
))
689 val_print_struct (type
, embedded_offset
, address
, stream
,
690 recurse
, val
, options
);
697 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
700 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
702 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
707 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
708 nfields
= TYPE_NFIELDS (variant_type
);
710 is_tuple
= (disr
.is_encoded
711 ? rust_tuple_struct_type_p (variant_type
)
712 : rust_tuple_variant_type_p (variant_type
));
713 start
= disr
.is_encoded
? 0 : 1;
717 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
719 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
722 /* struct variant. */
723 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
728 /* In case of a nullary variant like 'None', just output
730 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
734 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
737 fputs_filtered (", ", stream
);
741 fprintf_filtered (stream
, "%s: ",
742 TYPE_FIELD_NAME (variant_type
, j
));
744 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
746 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
747 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
749 stream
, recurse
+ 1, val
, &opts
,
754 fputs_filtered (")", stream
);
756 fputs_filtered ("}", stream
);
760 case TYPE_CODE_STRUCT
:
761 val_print_struct (type
, embedded_offset
, address
, stream
,
762 recurse
, val
, options
);
767 /* Nothing special yet. */
768 generic_val_print (type
, embedded_offset
, address
, stream
,
769 recurse
, val
, options
, &rust_decorations
);
776 rust_print_type (struct type
*type
, const char *varstring
,
777 struct ui_file
*stream
, int show
, int level
,
778 const struct type_print_options
*flags
);
780 /* Print a struct or union typedef. */
782 rust_print_struct_def (struct type
*type
, const char *varstring
,
783 struct ui_file
*stream
, int show
, int level
,
784 const struct type_print_options
*flags
)
786 bool is_tuple_struct
;
789 /* Print a tuple type simply. */
790 if (rust_tuple_type_p (type
))
792 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
796 /* If we see a base class, delegate to C. */
797 if (TYPE_N_BASECLASSES (type
) > 0)
798 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
800 /* This code path is also used by unions. */
801 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
802 fputs_filtered ("struct ", stream
);
804 fputs_filtered ("union ", stream
);
806 if (TYPE_TAG_NAME (type
) != NULL
)
807 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
809 is_tuple_struct
= rust_tuple_struct_type_p (type
);
811 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
813 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
815 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
820 if (field_is_static (&TYPE_FIELD (type
, i
)))
823 /* We'd like to print "pub" here as needed, but rustc
824 doesn't emit the debuginfo, and our types don't have
825 cplus_struct_type attached. */
827 /* For a tuple struct we print the type but nothing
829 print_spaces_filtered (level
+ 2, stream
);
830 if (!is_tuple_struct
)
831 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
833 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
834 stream
, show
- 1, level
+ 2,
836 fputs_filtered (",\n", stream
);
839 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
842 /* la_print_typedef implementation for Rust. */
845 rust_print_typedef (struct type
*type
,
846 struct symbol
*new_symbol
,
847 struct ui_file
*stream
)
849 type
= check_typedef (type
);
850 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
851 type_print (type
, "", stream
, 0);
852 fprintf_filtered (stream
, ";\n");
855 /* la_print_type implementation for Rust. */
858 rust_print_type (struct type
*type
, const char *varstring
,
859 struct ui_file
*stream
, int show
, int level
,
860 const struct type_print_options
*flags
)
866 && TYPE_NAME (type
) != NULL
)
868 /* Rust calls the unit type "void" in its debuginfo,
869 but we don't want to print it as that. */
870 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
871 fputs_filtered ("()", stream
);
873 fputs_filtered (TYPE_NAME (type
), stream
);
877 type
= check_typedef (type
);
878 switch (TYPE_CODE (type
))
881 fputs_filtered ("()", stream
);
885 /* Delegate varargs to the C printer. */
886 if (TYPE_VARARGS (type
))
889 fputs_filtered ("fn ", stream
);
890 if (varstring
!= NULL
)
891 fputs_filtered (varstring
, stream
);
892 fputs_filtered ("(", stream
);
893 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
897 fputs_filtered (", ", stream
);
898 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
901 fputs_filtered (")", stream
);
902 /* If it returns unit, we can omit the return type. */
903 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
905 fputs_filtered (" -> ", stream
);
906 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
910 case TYPE_CODE_ARRAY
:
912 LONGEST low_bound
, high_bound
;
914 fputs_filtered ("[", stream
);
915 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
916 stream
, show
- 1, level
, flags
);
918 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
919 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
920 fprintf_filtered (stream
, "; variable length");
921 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
922 fprintf_filtered (stream
, "; %s",
923 plongest (high_bound
- low_bound
+ 1));
924 fputs_filtered ("]", stream
);
928 case TYPE_CODE_STRUCT
:
929 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
936 fputs_filtered ("enum ", stream
);
937 if (TYPE_TAG_NAME (type
) != NULL
)
939 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
940 fputs_filtered (" ", stream
);
941 len
= strlen (TYPE_TAG_NAME (type
));
943 fputs_filtered ("{\n", stream
);
945 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
947 const char *name
= TYPE_FIELD_NAME (type
, i
);
952 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
954 && name
[len
+ 1] == ':')
956 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
959 fputs_filtered ("}", stream
);
963 case TYPE_CODE_UNION
:
967 /* Skip the discriminant field. */
970 /* Unions and structs have the same syntax in Rust,
971 the only difference is that structs are declared with `struct`
972 and union with `union`. This difference is handled in the struct
974 if (rust_union_is_untagged (type
))
976 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
980 fputs_filtered ("enum ", stream
);
981 if (TYPE_TAG_NAME (type
) != NULL
)
983 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
984 fputs_filtered (" ", stream
);
986 fputs_filtered ("{\n", stream
);
988 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
989 strlen (RUST_ENUM_PREFIX
)) == 0)
991 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
992 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
994 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
995 /* There is no explicit discriminant field, skip nothing. */
999 else if (TYPE_NFIELDS (type
) == 1)
1002 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1004 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
1006 = rust_last_path_segment (TYPE_NAME (variant_type
));
1008 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
1010 if (TYPE_NFIELDS (variant_type
) > skip_to
)
1013 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
1014 ? rust_tuple_struct_type_p (variant_type
)
1015 : rust_tuple_variant_type_p (variant_type
));
1018 fputs_filtered (is_tuple
? "(" : "{", stream
);
1019 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1024 fputs_filtered (", ", stream
);
1027 fprintf_filtered (stream
, "%s: ",
1028 TYPE_FIELD_NAME (variant_type
, j
));
1030 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1031 stream
, show
- 1, level
+ 2,
1034 fputs_filtered (is_tuple
? ")" : "}", stream
);
1037 fputs_filtered (",\n", stream
);
1040 fputs_filtered ("}", stream
);
1046 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1052 /* Compute the alignment of the type T. */
1055 rust_type_alignment (struct type
*t
)
1057 t
= check_typedef (t
);
1058 switch (TYPE_CODE (t
))
1061 error (_("Could not compute alignment of type"));
1064 case TYPE_CODE_ENUM
:
1068 case TYPE_CODE_CHAR
:
1069 case TYPE_CODE_BOOL
:
1070 return TYPE_LENGTH (t
);
1072 case TYPE_CODE_ARRAY
:
1073 case TYPE_CODE_COMPLEX
:
1074 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1076 case TYPE_CODE_STRUCT
:
1077 case TYPE_CODE_UNION
:
1082 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1084 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1093 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1094 -- either on an obstack or on a gdbarch. */
1096 static struct type
*
1097 rust_composite_type (struct type
*original
,
1099 const char *field1
, struct type
*type1
,
1100 const char *field2
, struct type
*type2
)
1102 struct type
*result
= alloc_type_copy (original
);
1103 int i
, nfields
, bitpos
;
1111 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1112 TYPE_NAME (result
) = name
;
1113 TYPE_TAG_NAME (result
) = name
;
1115 TYPE_NFIELDS (result
) = nfields
;
1116 TYPE_FIELDS (result
)
1117 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1123 struct field
*field
= &TYPE_FIELD (result
, i
);
1125 SET_FIELD_BITPOS (*field
, bitpos
);
1126 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1128 FIELD_NAME (*field
) = field1
;
1129 FIELD_TYPE (*field
) = type1
;
1134 struct field
*field
= &TYPE_FIELD (result
, i
);
1135 int align
= rust_type_alignment (type2
);
1141 align
*= TARGET_CHAR_BIT
;
1142 delta
= bitpos
% align
;
1144 bitpos
+= align
- delta
;
1146 SET_FIELD_BITPOS (*field
, bitpos
);
1148 FIELD_NAME (*field
) = field2
;
1149 FIELD_TYPE (*field
) = type2
;
1154 TYPE_LENGTH (result
)
1155 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1156 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1160 /* See rust-lang.h. */
1163 rust_slice_type (const char *name
, struct type
*elt_type
,
1164 struct type
*usize_type
)
1168 elt_type
= lookup_pointer_type (elt_type
);
1169 type
= rust_composite_type (elt_type
, name
,
1170 "data_ptr", elt_type
,
1171 "length", usize_type
);
1176 enum rust_primitive_types
1178 rust_primitive_bool
,
1179 rust_primitive_char
,
1188 rust_primitive_isize
,
1189 rust_primitive_usize
,
1192 rust_primitive_unit
,
1194 nr_rust_primitive_types
1197 /* la_language_arch_info implementation for Rust. */
1200 rust_language_arch_info (struct gdbarch
*gdbarch
,
1201 struct language_arch_info
*lai
)
1203 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1205 struct type
**types
;
1206 unsigned int length
;
1208 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1211 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1212 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1213 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1214 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1215 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1216 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1217 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1218 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1219 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1220 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1222 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1223 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1224 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1226 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1227 floatformats_ieee_single
);
1228 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1229 floatformats_ieee_double
);
1231 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1233 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1234 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1235 types
[rust_primitive_usize
]);
1237 lai
->primitive_type_vector
= types
;
1238 lai
->bool_type_default
= types
[rust_primitive_bool
];
1239 lai
->string_char_type
= types
[rust_primitive_u8
];
1244 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1246 static struct value
*
1247 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1250 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1252 struct value
*function
, *result
, *arg0
;
1253 struct type
*type
, *fn_type
;
1254 const struct block
*block
;
1255 struct block_symbol sym
;
1257 /* For an ordinary function call we can simply defer to the
1258 generic implementation. */
1259 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1260 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1262 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1264 method
= &exp
->elts
[*pos
+ 1].string
;
1265 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1267 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1268 type in order to look up the method. */
1269 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1271 if (noside
== EVAL_SKIP
)
1273 for (i
= 0; i
< num_args
; ++i
)
1274 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1278 std::vector
<struct value
*> args (num_args
+ 1);
1281 /* We don't yet implement real Deref semantics. */
1282 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1283 args
[0] = value_ind (args
[0]);
1285 type
= value_type (args
[0]);
1286 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1287 && TYPE_CODE (type
) != TYPE_CODE_UNION
1288 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1289 || rust_tuple_type_p (type
))
1290 error (_("Method calls only supported on struct or enum types"));
1291 if (TYPE_TAG_NAME (type
) == NULL
)
1292 error (_("Method call on nameless type"));
1294 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1296 block
= get_selected_block (0);
1297 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1298 if (sym
.symbol
== NULL
)
1299 error (_("Could not find function named '%s'"), name
.c_str ());
1301 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1302 if (TYPE_NFIELDS (fn_type
) == 0)
1303 error (_("Function '%s' takes no arguments"), name
.c_str ());
1305 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1306 args
[0] = value_addr (args
[0]);
1308 function
= address_of_variable (sym
.symbol
, block
);
1310 for (i
= 0; i
< num_args
; ++i
)
1311 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1313 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1314 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1316 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1320 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1322 static struct value
*
1323 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1325 enum range_type kind
;
1326 struct value
*low
= NULL
, *high
= NULL
;
1327 struct value
*addrval
, *result
;
1329 struct type
*range_type
;
1330 struct type
*index_type
;
1331 struct type
*temp_type
;
1334 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1337 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1338 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1339 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1340 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1342 if (noside
== EVAL_SKIP
)
1343 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1350 name
= "std::ops::RangeFull";
1354 index_type
= value_type (high
);
1355 name
= "std::ops::RangeTo";
1362 index_type
= value_type (low
);
1363 name
= "std::ops::RangeFrom";
1367 if (!types_equal (value_type (low
), value_type (high
)))
1368 error (_("Range expression with different types"));
1369 index_type
= value_type (low
);
1370 name
= "std::ops::Range";
1374 /* If we don't have an index type, just allocate this on the
1375 arch. Here any type will do. */
1376 temp_type
= (index_type
== NULL
1377 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1379 /* It would be nicer to cache the range type. */
1380 range_type
= rust_composite_type (temp_type
, name
,
1381 low
== NULL
? NULL
: "start", index_type
,
1382 high
== NULL
? NULL
: "end", index_type
);
1384 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1385 return value_zero (range_type
, lval_memory
);
1387 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1388 addr
= value_as_long (addrval
);
1389 result
= value_at_lazy (range_type
, addr
);
1393 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1396 value_assign (start
, low
);
1401 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1404 value_assign (end
, high
);
1407 result
= value_at_lazy (range_type
, addr
);
1411 /* A helper function to compute the range and kind given a range
1412 value. TYPE is the type of the range value. RANGE is the range
1413 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1414 parameters might be filled in, or might not be, depending on the
1415 kind of range this is. KIND will always be set to the appropriate
1416 value describing the kind of range, and this can be used to
1417 determine whether LOW or HIGH are valid. */
1420 rust_compute_range (struct type
*type
, struct value
*range
,
1421 LONGEST
*low
, LONGEST
*high
,
1422 enum range_type
*kind
)
1428 *kind
= BOTH_BOUND_DEFAULT
;
1430 if (TYPE_NFIELDS (type
) == 0)
1434 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1436 *kind
= HIGH_BOUND_DEFAULT
;
1437 *low
= value_as_long (value_field (range
, 0));
1440 if (TYPE_NFIELDS (type
) > i
1441 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1443 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1444 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1445 *high
= value_as_long (value_field (range
, i
));
1449 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1451 static struct value
*
1452 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1455 struct value
*lhs
, *rhs
, *result
;
1456 struct type
*rhstype
;
1457 LONGEST low
, high_bound
;
1458 /* Initialized to appease the compiler. */
1459 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1464 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1465 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1467 if (noside
== EVAL_SKIP
)
1470 rhstype
= check_typedef (value_type (rhs
));
1471 if (rust_range_type_p (rhstype
))
1474 error (_("Can't take slice of array without '&'"));
1475 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1479 low
= value_as_long (rhs
);
1481 struct type
*type
= check_typedef (value_type (lhs
));
1482 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1484 struct type
*base_type
= nullptr;
1485 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1486 base_type
= TYPE_TARGET_TYPE (type
);
1487 else if (rust_slice_type_p (type
))
1489 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1491 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1493 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1497 if (base_type
== nullptr)
1498 error (_("Could not find 'data_ptr' in slice type"));
1500 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1501 base_type
= TYPE_TARGET_TYPE (type
);
1503 error (_("Cannot subscript non-array type"));
1505 struct type
*new_type
;
1508 if (rust_slice_type_p (type
))
1513 = language_lookup_primitive_type (exp
->language_defn
,
1516 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1520 new_type
= base_type
;
1522 return value_zero (new_type
, VALUE_LVAL (lhs
));
1529 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1532 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1533 error (_("Can't compute array bounds"));
1535 error (_("Found array with non-zero lower bound"));
1538 else if (rust_slice_type_p (type
))
1542 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1543 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1545 high_bound
= value_as_long (len
);
1547 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1551 high_bound
= LONGEST_MAX
;
1554 error (_("Cannot subscript non-array type"));
1557 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1560 error (_("Index less than zero"));
1561 if (low
> high_bound
)
1562 error (_("Index greater than length"));
1564 result
= value_subscript (base
, low
);
1571 struct type
*usize
, *slice
;
1573 struct value
*addrval
, *tem
;
1575 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1578 error (_("High index less than zero"));
1580 error (_("Low index greater than high index"));
1581 if (high
> high_bound
)
1582 error (_("High index greater than length"));
1584 usize
= language_lookup_primitive_type (exp
->language_defn
,
1587 const char *new_name
= ((type
!= nullptr
1588 && rust_slice_type_p (type
))
1589 ? TYPE_NAME (type
) : "&[*gdb*]");
1591 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1593 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1594 addr
= value_as_long (addrval
);
1595 tem
= value_at_lazy (slice
, addr
);
1597 value_assign (value_field (tem
, 0), value_addr (result
));
1598 value_assign (value_field (tem
, 1),
1599 value_from_longest (usize
, high
- low
));
1601 result
= value_at_lazy (slice
, addr
);
1604 result
= value_addr (result
);
1610 /* evaluate_exp implementation for Rust. */
1612 static struct value
*
1613 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1614 int *pos
, enum noside noside
)
1616 struct value
*result
;
1618 switch (exp
->elts
[*pos
].opcode
)
1620 case UNOP_COMPLEMENT
:
1622 struct value
*value
;
1625 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1626 if (noside
== EVAL_SKIP
)
1628 /* Preserving the type is enough. */
1631 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1632 result
= value_from_longest (value_type (value
),
1633 value_logical_not (value
));
1635 result
= value_complement (value
);
1639 case BINOP_SUBSCRIPT
:
1640 result
= rust_subscript (exp
, pos
, noside
, 0);
1644 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1650 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1651 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1654 struct value
*addrval
= NULL
;
1658 if (noside
== EVAL_NORMAL
)
1660 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1661 addr
= value_as_long (addrval
);
1662 result
= value_at_lazy (type
, addr
);
1665 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1670 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1671 if (noside
== EVAL_NORMAL
)
1673 /* This isn't quite right but will do for the time
1674 being, seeing that we can't implement the Copy
1676 value_assign (result
, init
);
1682 gdb_assert (arglen
% 2 == 0);
1683 for (i
= 0; i
< arglen
; i
+= 2)
1686 const char *fieldname
;
1687 struct value
*value
, *field
;
1689 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1691 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1693 fieldname
= &exp
->elts
[*pos
].string
;
1694 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1696 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1697 if (noside
== EVAL_NORMAL
)
1699 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1701 value_assign (field
, value
);
1705 if (noside
== EVAL_SKIP
)
1706 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1708 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1709 result
= allocate_value (type
);
1711 result
= value_at_lazy (type
, addr
);
1720 struct value
*ncopies
;
1722 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1723 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1724 copies
= value_as_long (ncopies
);
1726 error (_("Array with negative number of elements"));
1728 if (noside
== EVAL_NORMAL
)
1732 std::vector
<struct value
*> eltvec (copies
);
1734 for (i
= 0; i
< copies
; ++i
)
1736 result
= value_array (0, copies
- 1, eltvec
.data ());
1740 struct type
*arraytype
1741 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1742 result
= allocate_value (arraytype
);
1747 case STRUCTOP_ANONYMOUS
:
1749 /* Anonymous field access, i.e. foo.1. */
1751 int pc
, field_number
, nfields
;
1752 struct type
*type
, *variant_type
;
1753 struct disr_info disr
;
1756 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1758 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1760 type
= value_type (lhs
);
1761 /* Untagged unions can't have anonymous field access since
1762 they can only have named fields. */
1763 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1764 && !rust_union_is_untagged (type
))
1766 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1767 value_embedded_offset (lhs
),
1768 value_address (lhs
), lhs
);
1770 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1772 variant_type
= NULL
;
1777 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1778 nfields
= TYPE_NFIELDS (variant_type
);
1781 if (!disr
.is_encoded
)
1784 if (field_number
>= nfields
|| field_number
< 0)
1785 error(_("Cannot access field %d of variant %s, \
1786 there are only %d fields"),
1787 disr
.is_encoded
? field_number
: field_number
- 1,
1789 disr
.is_encoded
? nfields
: nfields
- 1);
1791 if (!(disr
.is_encoded
1792 ? rust_tuple_struct_type_p (variant_type
)
1793 : rust_tuple_variant_type_p (variant_type
)))
1794 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1796 result
= value_primitive_field (lhs
, 0, field_number
,
1799 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1801 /* Tuples and tuple structs */
1802 nfields
= TYPE_NFIELDS(type
);
1804 if (field_number
>= nfields
|| field_number
< 0)
1805 error(_("Cannot access field %d of %s, there are only %d fields"),
1806 field_number
, TYPE_TAG_NAME (type
), nfields
);
1808 /* Tuples are tuple structs too. */
1809 if (!rust_tuple_struct_type_p (type
))
1810 error(_("Attempting to access anonymous field %d of %s, which is \
1811 not a tuple, tuple struct, or tuple-like variant"),
1812 field_number
, TYPE_TAG_NAME (type
));
1814 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1817 error(_("Anonymous field access is only allowed on tuples, \
1818 tuple structs, and tuple-like enum variants"));
1822 case STRUCTOP_STRUCT
:
1829 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1830 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1831 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1833 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1834 type
= value_type (lhs
);
1835 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1836 && !rust_union_is_untagged (type
))
1839 struct disr_info disr
;
1840 struct type
*variant_type
;
1842 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1843 value_embedded_offset (lhs
),
1844 value_address (lhs
), lhs
);
1846 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1847 error(_("Could not find field %s of struct variant %s"),
1848 field_name
, disr
.name
.c_str ());
1850 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1852 if (variant_type
== NULL
1854 ? rust_tuple_struct_type_p (variant_type
)
1855 : rust_tuple_variant_type_p (variant_type
)))
1856 error(_("Attempting to access named field %s of tuple variant %s, \
1857 which has only anonymous fields"),
1858 field_name
, disr
.name
.c_str ());
1860 start
= disr
.is_encoded
? 0 : 1;
1861 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1863 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1865 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1870 if (i
== TYPE_NFIELDS (variant_type
))
1871 /* We didn't find it. */
1872 error(_("Could not find field %s of struct variant %s"),
1873 field_name
, disr
.name
.c_str ());
1877 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
,
1879 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1880 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1886 result
= rust_range (exp
, pos
, noside
);
1890 /* We might have &array[range], in which case we need to make a
1892 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1895 result
= rust_subscript (exp
, pos
, noside
, 1);
1900 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1907 /* operator_length implementation for Rust. */
1910 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1916 switch (exp
->elts
[pc
- 1].opcode
)
1919 /* We handle aggregate as a type and argument count. The first
1920 argument might be OP_OTHERS. After that the arguments
1921 alternate: first an OP_NAME, then an expression. */
1923 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1931 case STRUCTOP_ANONYMOUS
:
1942 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1950 /* op_name implementation for Rust. */
1953 rust_op_name (enum exp_opcode opcode
)
1958 return "OP_AGGREGATE";
1962 return op_name_standard (opcode
);
1966 /* dump_subexp_body implementation for Rust. */
1969 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1972 switch (exp
->elts
[elt
].opcode
)
1976 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1979 fprintf_filtered (stream
, "Type @");
1980 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1981 fprintf_filtered (stream
, " (");
1982 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1983 fprintf_filtered (stream
, "), length %d", length
);
1986 for (i
= 0; i
< length
; ++i
)
1987 elt
= dump_subexp (exp
, stream
, elt
);
1994 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1996 fprintf_filtered (stream
, "%s: %s",
1997 (exp
->elts
[elt
].opcode
== OP_STRING
1998 ? "string" : "name"),
1999 &exp
->elts
[elt
+ 2].string
);
2000 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2005 elt
= dump_subexp (exp
, stream
, elt
+ 1);
2008 case STRUCTOP_ANONYMOUS
:
2012 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
2014 fprintf_filtered (stream
, "Field number: %d", field_number
);
2015 elt
= dump_subexp (exp
, stream
, elt
+ 3);
2024 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
2031 /* print_subexp implementation for Rust. */
2034 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
2035 enum precedence prec
)
2037 switch (exp
->elts
[*pos
].opcode
)
2041 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
2044 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
2045 fputs_filtered (" { ", stream
);
2048 for (i
= 0; i
< length
; ++i
)
2050 rust_print_subexp (exp
, pos
, stream
, prec
);
2051 fputs_filtered (", ", stream
);
2053 fputs_filtered (" }", stream
);
2059 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2061 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2062 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2068 fputs_filtered ("<<others>> (", stream
);
2070 rust_print_subexp (exp
, pos
, stream
, prec
);
2071 fputs_filtered (")", stream
);
2075 case STRUCTOP_ANONYMOUS
:
2077 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2080 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2081 fprintf_filtered (stream
, ".%d", tem
);
2087 fprintf_filtered (stream
, "[");
2088 rust_print_subexp (exp
, pos
, stream
, prec
);
2089 fprintf_filtered (stream
, "; ");
2090 rust_print_subexp (exp
, pos
, stream
, prec
);
2091 fprintf_filtered (stream
, "]");
2095 print_subexp_standard (exp
, pos
, stream
, prec
);
2100 /* operator_check implementation for Rust. */
2103 rust_operator_check (struct expression
*exp
, int pos
,
2104 int (*objfile_func
) (struct objfile
*objfile
,
2108 switch (exp
->elts
[pos
].opcode
)
2112 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2113 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2115 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2126 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2134 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2136 static struct block_symbol
2137 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2139 const struct block
*block
,
2140 const domain_enum domain
)
2142 struct block_symbol result
= {NULL
, NULL
};
2144 if (symbol_lookup_debug
)
2146 fprintf_unfiltered (gdb_stdlog
,
2147 "rust_lookup_symbol_non_local"
2148 " (%s, %s (scope %s), %s)\n",
2149 name
, host_address_to_string (block
),
2150 block_scope (block
), domain_name (domain
));
2153 /* Look up bare names in the block's scope. */
2154 if (name
[cp_find_first_component (name
)] == '\0')
2156 const char *scope
= block_scope (block
);
2158 if (scope
[0] != '\0')
2160 std::string scopedname
= std::string (scope
) + "::" + name
;
2162 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2164 if (result
.symbol
== NULL
)
2165 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2173 /* la_sniff_from_mangled_name for Rust. */
2176 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2178 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2179 return *demangled
!= NULL
;
2184 /* la_watch_location_expression for Rust. */
2186 static gdb::unique_xmalloc_ptr
<char>
2187 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2189 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2190 std::string name
= type_to_string (type
);
2191 return gdb::unique_xmalloc_ptr
<char>
2192 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2198 static const struct exp_descriptor exp_descriptor_rust
=
2201 rust_operator_length
,
2202 rust_operator_check
,
2204 rust_dump_subexp_body
,
2205 rust_evaluate_subexp
2208 static const char *rust_extensions
[] =
2213 extern const struct language_defn rust_language_defn
=
2223 &exp_descriptor_rust
,
2227 rust_printchar
, /* Print a character constant */
2228 rust_printstr
, /* Function to print string constant */
2229 rust_emitchar
, /* Print a single char */
2230 rust_print_type
, /* Print a type using appropriate syntax */
2231 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2232 rust_val_print
, /* Print a value using appropriate syntax */
2233 c_value_print
, /* Print a top-level value */
2234 default_read_var_value
, /* la_read_var_value */
2235 NULL
, /* Language specific skip_trampoline */
2236 NULL
, /* name_of_this */
2237 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2238 basic_lookup_transparent_type
,/* lookup_transparent_type */
2239 gdb_demangle
, /* Language specific symbol demangler */
2240 rust_sniff_from_mangled_name
,
2241 NULL
, /* Language specific
2242 class_name_from_physname */
2243 c_op_print_tab
, /* expression operators for printing */
2244 1, /* c-style arrays */
2245 0, /* String lower bound */
2246 default_word_break_characters
,
2247 default_collect_symbol_completion_matches
,
2248 rust_language_arch_info
,
2249 default_print_array_index
,
2250 default_pass_by_reference
,
2252 rust_watch_location_expression
,
2253 NULL
, /* la_get_symbol_name_matcher */
2254 iterate_over_symbols
,
2255 default_search_name_hash
,
2256 &default_varobj_ops
,