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 /* rust_print_type branch for structs and untagged unions. */
473 val_print_struct (struct type
*type
, int embedded_offset
,
474 CORE_ADDR address
, struct ui_file
*stream
,
475 int recurse
, struct value
*val
,
476 const struct value_print_options
*options
)
480 bool is_tuple
= rust_tuple_type_p (type
);
481 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
482 struct value_print_options opts
;
486 if (TYPE_TAG_NAME (type
) != NULL
)
487 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
489 if (TYPE_NFIELDS (type
) == 0)
492 if (TYPE_TAG_NAME (type
) != NULL
)
493 fputs_filtered (" ", stream
);
496 if (is_tuple
|| is_tuple_struct
)
497 fputs_filtered ("(", stream
);
499 fputs_filtered ("{", stream
);
505 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
507 if (field_is_static (&TYPE_FIELD (type
, i
)))
511 fputs_filtered (",", stream
);
513 if (options
->prettyformat
)
515 fputs_filtered ("\n", stream
);
516 print_spaces_filtered (2 + 2 * recurse
, stream
);
518 else if (!first_field
)
519 fputs_filtered (" ", stream
);
523 if (!is_tuple
&& !is_tuple_struct
)
525 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
526 fputs_filtered (": ", stream
);
529 val_print (TYPE_FIELD_TYPE (type
, i
),
530 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
532 stream
, recurse
+ 1, val
, &opts
,
536 if (options
->prettyformat
)
538 fputs_filtered ("\n", stream
);
539 print_spaces_filtered (2 * recurse
, stream
);
542 if (is_tuple
|| is_tuple_struct
)
543 fputs_filtered (")", stream
);
545 fputs_filtered ("}", stream
);
548 static const struct generic_val_print_decorations rust_decorations
=
550 /* Complex isn't used in Rust, but we provide C-ish values just in
562 /* la_val_print implementation for Rust. */
565 rust_val_print (struct type
*type
, int embedded_offset
,
566 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
568 const struct value_print_options
*options
)
570 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
572 type
= check_typedef (type
);
573 switch (TYPE_CODE (type
))
577 LONGEST low_bound
, high_bound
;
579 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
580 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
581 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
583 /* We have a pointer to a byte string, so just print
585 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
587 struct gdbarch
*arch
= get_type_arch (type
);
588 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
590 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
591 if (options
->addressprint
)
593 fputs_filtered (paddress (arch
, addr
), stream
);
594 fputs_filtered (" ", stream
);
597 fputs_filtered ("b", stream
);
598 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
599 high_bound
- low_bound
+ 1, stream
,
606 case TYPE_CODE_METHODPTR
:
607 case TYPE_CODE_MEMBERPTR
:
608 c_val_print (type
, embedded_offset
, address
, stream
,
609 recurse
, val
, options
);
613 /* Recognize the unit type. */
614 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
615 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
617 fputs_filtered ("()", stream
);
622 case TYPE_CODE_STRING
:
624 struct gdbarch
*arch
= get_type_arch (type
);
625 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
626 LONGEST low_bound
, high_bound
;
628 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
629 error (_("Could not determine the array bounds"));
631 /* If we see a plain TYPE_CODE_STRING, then we're printing a
632 byte string, hence the choice of "ASCII" as the
634 fputs_filtered ("b", stream
);
635 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
636 valaddr
+ embedded_offset
* unit_size
,
637 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
641 case TYPE_CODE_ARRAY
:
643 LONGEST low_bound
, high_bound
;
645 if (get_array_bounds (type
, &low_bound
, &high_bound
)
646 && high_bound
- low_bound
+ 1 == 0)
647 fputs_filtered ("[]", stream
);
653 case TYPE_CODE_UNION
:
655 int j
, nfields
, first_field
, is_tuple
, start
;
656 struct type
*variant_type
;
657 struct disr_info disr
;
658 struct value_print_options opts
;
660 /* Untagged unions are printed as if they are structs.
661 Since the field bit positions overlap in the debuginfo,
662 the code for printing a union is same as that for a struct,
663 the only difference is that the input type will have overlapping
665 if (rust_union_is_untagged (type
))
667 val_print_struct (type
, embedded_offset
, address
, stream
,
668 recurse
, val
, options
);
675 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
678 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
680 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
685 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
686 nfields
= TYPE_NFIELDS (variant_type
);
688 is_tuple
= (disr
.is_encoded
689 ? rust_tuple_struct_type_p (variant_type
)
690 : rust_tuple_variant_type_p (variant_type
));
691 start
= disr
.is_encoded
? 0 : 1;
695 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
697 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
700 /* struct variant. */
701 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
706 /* In case of a nullary variant like 'None', just output
708 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
712 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
715 fputs_filtered (", ", stream
);
719 fprintf_filtered (stream
, "%s: ",
720 TYPE_FIELD_NAME (variant_type
, j
));
722 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
724 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
725 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
727 stream
, recurse
+ 1, val
, &opts
,
732 fputs_filtered (")", stream
);
734 fputs_filtered ("}", stream
);
738 case TYPE_CODE_STRUCT
:
739 val_print_struct (type
, embedded_offset
, address
, stream
,
740 recurse
, val
, options
);
745 /* Nothing special yet. */
746 generic_val_print (type
, embedded_offset
, address
, stream
,
747 recurse
, val
, options
, &rust_decorations
);
754 rust_print_type (struct type
*type
, const char *varstring
,
755 struct ui_file
*stream
, int show
, int level
,
756 const struct type_print_options
*flags
);
758 /* Print a struct or union typedef. */
760 rust_print_struct_def (struct type
*type
, const char *varstring
,
761 struct ui_file
*stream
, int show
, int level
,
762 const struct type_print_options
*flags
)
764 bool is_tuple_struct
;
767 /* Print a tuple type simply. */
768 if (rust_tuple_type_p (type
))
770 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
774 /* If we see a base class, delegate to C. */
775 if (TYPE_N_BASECLASSES (type
) > 0)
776 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
778 /* This code path is also used by unions. */
779 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
780 fputs_filtered ("struct ", stream
);
782 fputs_filtered ("union ", stream
);
784 if (TYPE_TAG_NAME (type
) != NULL
)
785 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
787 is_tuple_struct
= rust_tuple_struct_type_p (type
);
789 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
791 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
793 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
798 if (field_is_static (&TYPE_FIELD (type
, i
)))
801 /* We'd like to print "pub" here as needed, but rustc
802 doesn't emit the debuginfo, and our types don't have
803 cplus_struct_type attached. */
805 /* For a tuple struct we print the type but nothing
807 print_spaces_filtered (level
+ 2, stream
);
808 if (!is_tuple_struct
)
809 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
811 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
812 stream
, show
- 1, level
+ 2,
814 fputs_filtered (",\n", stream
);
817 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
820 /* la_print_typedef implementation for Rust. */
823 rust_print_typedef (struct type
*type
,
824 struct symbol
*new_symbol
,
825 struct ui_file
*stream
)
827 type
= check_typedef (type
);
828 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
829 type_print (type
, "", stream
, 0);
830 fprintf_filtered (stream
, ";\n");
833 /* la_print_type implementation for Rust. */
836 rust_print_type (struct type
*type
, const char *varstring
,
837 struct ui_file
*stream
, int show
, int level
,
838 const struct type_print_options
*flags
)
844 && TYPE_NAME (type
) != NULL
)
846 /* Rust calls the unit type "void" in its debuginfo,
847 but we don't want to print it as that. */
848 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
849 fputs_filtered ("()", stream
);
851 fputs_filtered (TYPE_NAME (type
), stream
);
855 type
= check_typedef (type
);
856 switch (TYPE_CODE (type
))
859 fputs_filtered ("()", stream
);
863 /* Delegate varargs to the C printer. */
864 if (TYPE_VARARGS (type
))
867 fputs_filtered ("fn ", stream
);
868 if (varstring
!= NULL
)
869 fputs_filtered (varstring
, stream
);
870 fputs_filtered ("(", stream
);
871 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
875 fputs_filtered (", ", stream
);
876 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
879 fputs_filtered (")", stream
);
880 /* If it returns unit, we can omit the return type. */
881 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
883 fputs_filtered (" -> ", stream
);
884 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
888 case TYPE_CODE_ARRAY
:
890 LONGEST low_bound
, high_bound
;
892 fputs_filtered ("[", stream
);
893 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
894 stream
, show
- 1, level
, flags
);
896 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
897 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
898 fprintf_filtered (stream
, "; variable length");
899 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
900 fprintf_filtered (stream
, "; %s",
901 plongest (high_bound
- low_bound
+ 1));
902 fputs_filtered ("]", stream
);
906 case TYPE_CODE_STRUCT
:
907 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
914 fputs_filtered ("enum ", stream
);
915 if (TYPE_TAG_NAME (type
) != NULL
)
917 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
918 fputs_filtered (" ", stream
);
919 len
= strlen (TYPE_TAG_NAME (type
));
921 fputs_filtered ("{\n", stream
);
923 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
925 const char *name
= TYPE_FIELD_NAME (type
, i
);
930 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
932 && name
[len
+ 1] == ':')
934 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
937 fputs_filtered ("}", stream
);
941 case TYPE_CODE_UNION
:
945 /* Skip the discriminant field. */
948 /* Unions and structs have the same syntax in Rust,
949 the only difference is that structs are declared with `struct`
950 and union with `union`. This difference is handled in the struct
952 if (rust_union_is_untagged (type
))
954 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
958 fputs_filtered ("enum ", stream
);
959 if (TYPE_TAG_NAME (type
) != NULL
)
961 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
962 fputs_filtered (" ", stream
);
964 fputs_filtered ("{\n", stream
);
966 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
967 strlen (RUST_ENUM_PREFIX
)) == 0)
969 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
970 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
972 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
973 /* There is no explicit discriminant field, skip nothing. */
977 else if (TYPE_NFIELDS (type
) == 1)
980 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
982 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
984 = rust_last_path_segment (TYPE_NAME (variant_type
));
986 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
988 if (TYPE_NFIELDS (variant_type
) > skip_to
)
991 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
992 ? rust_tuple_struct_type_p (variant_type
)
993 : rust_tuple_variant_type_p (variant_type
));
996 fputs_filtered (is_tuple
? "(" : "{", stream
);
997 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1002 fputs_filtered (", ", stream
);
1005 fprintf_filtered (stream
, "%s: ",
1006 TYPE_FIELD_NAME (variant_type
, j
));
1008 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1009 stream
, show
- 1, level
+ 2,
1012 fputs_filtered (is_tuple
? ")" : "}", stream
);
1015 fputs_filtered (",\n", stream
);
1018 fputs_filtered ("}", stream
);
1024 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1030 /* Compute the alignment of the type T. */
1033 rust_type_alignment (struct type
*t
)
1035 t
= check_typedef (t
);
1036 switch (TYPE_CODE (t
))
1039 error (_("Could not compute alignment of type"));
1042 case TYPE_CODE_ENUM
:
1046 case TYPE_CODE_CHAR
:
1047 case TYPE_CODE_BOOL
:
1048 return TYPE_LENGTH (t
);
1050 case TYPE_CODE_ARRAY
:
1051 case TYPE_CODE_COMPLEX
:
1052 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1054 case TYPE_CODE_STRUCT
:
1055 case TYPE_CODE_UNION
:
1060 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1062 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1071 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1072 -- either on an obstack or on a gdbarch. */
1074 static struct type
*
1075 rust_composite_type (struct type
*original
,
1077 const char *field1
, struct type
*type1
,
1078 const char *field2
, struct type
*type2
)
1080 struct type
*result
= alloc_type_copy (original
);
1081 int i
, nfields
, bitpos
;
1089 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1090 TYPE_NAME (result
) = name
;
1091 TYPE_TAG_NAME (result
) = name
;
1093 TYPE_NFIELDS (result
) = nfields
;
1094 TYPE_FIELDS (result
)
1095 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1101 struct field
*field
= &TYPE_FIELD (result
, i
);
1103 SET_FIELD_BITPOS (*field
, bitpos
);
1104 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1106 FIELD_NAME (*field
) = field1
;
1107 FIELD_TYPE (*field
) = type1
;
1112 struct field
*field
= &TYPE_FIELD (result
, i
);
1113 int align
= rust_type_alignment (type2
);
1119 align
*= TARGET_CHAR_BIT
;
1120 delta
= bitpos
% align
;
1122 bitpos
+= align
- delta
;
1124 SET_FIELD_BITPOS (*field
, bitpos
);
1126 FIELD_NAME (*field
) = field2
;
1127 FIELD_TYPE (*field
) = type2
;
1132 TYPE_LENGTH (result
)
1133 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1134 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1138 /* See rust-lang.h. */
1141 rust_slice_type (const char *name
, struct type
*elt_type
,
1142 struct type
*usize_type
)
1146 elt_type
= lookup_pointer_type (elt_type
);
1147 type
= rust_composite_type (elt_type
, name
,
1148 "data_ptr", elt_type
,
1149 "length", usize_type
);
1154 enum rust_primitive_types
1156 rust_primitive_bool
,
1157 rust_primitive_char
,
1166 rust_primitive_isize
,
1167 rust_primitive_usize
,
1170 rust_primitive_unit
,
1172 nr_rust_primitive_types
1175 /* la_language_arch_info implementation for Rust. */
1178 rust_language_arch_info (struct gdbarch
*gdbarch
,
1179 struct language_arch_info
*lai
)
1181 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1183 struct type
**types
;
1184 unsigned int length
;
1186 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1189 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1190 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1191 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1192 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1193 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1194 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1195 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1196 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1197 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1198 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1200 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1201 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1202 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1204 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1205 floatformats_ieee_single
);
1206 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1207 floatformats_ieee_double
);
1209 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1211 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1212 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1213 types
[rust_primitive_usize
]);
1215 lai
->primitive_type_vector
= types
;
1216 lai
->bool_type_default
= types
[rust_primitive_bool
];
1217 lai
->string_char_type
= types
[rust_primitive_u8
];
1222 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1224 static struct value
*
1225 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1228 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1230 struct value
*function
, *result
, *arg0
;
1231 struct type
*type
, *fn_type
;
1232 const struct block
*block
;
1233 struct block_symbol sym
;
1235 /* For an ordinary function call we can simply defer to the
1236 generic implementation. */
1237 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1238 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1240 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1242 method
= &exp
->elts
[*pos
+ 1].string
;
1243 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1245 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1246 type in order to look up the method. */
1247 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1249 if (noside
== EVAL_SKIP
)
1251 for (i
= 0; i
< num_args
; ++i
)
1252 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1256 std::vector
<struct value
*> args (num_args
+ 1);
1259 /* We don't yet implement real Deref semantics. */
1260 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1261 args
[0] = value_ind (args
[0]);
1263 type
= value_type (args
[0]);
1264 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1265 && TYPE_CODE (type
) != TYPE_CODE_UNION
1266 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1267 || rust_tuple_type_p (type
))
1268 error (_("Method calls only supported on struct or enum types"));
1269 if (TYPE_TAG_NAME (type
) == NULL
)
1270 error (_("Method call on nameless type"));
1272 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1274 block
= get_selected_block (0);
1275 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1276 if (sym
.symbol
== NULL
)
1277 error (_("Could not find function named '%s'"), name
.c_str ());
1279 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1280 if (TYPE_NFIELDS (fn_type
) == 0)
1281 error (_("Function '%s' takes no arguments"), name
.c_str ());
1283 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1284 args
[0] = value_addr (args
[0]);
1286 function
= address_of_variable (sym
.symbol
, block
);
1288 for (i
= 0; i
< num_args
; ++i
)
1289 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1291 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1292 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1294 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1298 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1300 static struct value
*
1301 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1303 enum range_type kind
;
1304 struct value
*low
= NULL
, *high
= NULL
;
1305 struct value
*addrval
, *result
;
1307 struct type
*range_type
;
1308 struct type
*index_type
;
1309 struct type
*temp_type
;
1312 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1315 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1316 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1317 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1318 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1320 if (noside
== EVAL_SKIP
)
1321 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1328 name
= "std::ops::RangeFull";
1332 index_type
= value_type (high
);
1333 name
= "std::ops::RangeTo";
1340 index_type
= value_type (low
);
1341 name
= "std::ops::RangeFrom";
1345 if (!types_equal (value_type (low
), value_type (high
)))
1346 error (_("Range expression with different types"));
1347 index_type
= value_type (low
);
1348 name
= "std::ops::Range";
1352 /* If we don't have an index type, just allocate this on the
1353 arch. Here any type will do. */
1354 temp_type
= (index_type
== NULL
1355 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1357 /* It would be nicer to cache the range type. */
1358 range_type
= rust_composite_type (temp_type
, name
,
1359 low
== NULL
? NULL
: "start", index_type
,
1360 high
== NULL
? NULL
: "end", index_type
);
1362 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1363 return value_zero (range_type
, lval_memory
);
1365 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1366 addr
= value_as_long (addrval
);
1367 result
= value_at_lazy (range_type
, addr
);
1371 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1374 value_assign (start
, low
);
1379 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1382 value_assign (end
, high
);
1385 result
= value_at_lazy (range_type
, addr
);
1389 /* A helper function to compute the range and kind given a range
1390 value. TYPE is the type of the range value. RANGE is the range
1391 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1392 parameters might be filled in, or might not be, depending on the
1393 kind of range this is. KIND will always be set to the appropriate
1394 value describing the kind of range, and this can be used to
1395 determine whether LOW or HIGH are valid. */
1398 rust_compute_range (struct type
*type
, struct value
*range
,
1399 LONGEST
*low
, LONGEST
*high
,
1400 enum range_type
*kind
)
1406 *kind
= BOTH_BOUND_DEFAULT
;
1408 if (TYPE_NFIELDS (type
) == 0)
1412 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1414 *kind
= HIGH_BOUND_DEFAULT
;
1415 *low
= value_as_long (value_field (range
, 0));
1418 if (TYPE_NFIELDS (type
) > i
1419 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1421 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1422 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1423 *high
= value_as_long (value_field (range
, i
));
1427 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1429 static struct value
*
1430 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1433 struct value
*lhs
, *rhs
, *result
;
1434 struct type
*rhstype
;
1435 LONGEST low
, high_bound
;
1436 /* Initialized to appease the compiler. */
1437 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1442 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1443 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1445 if (noside
== EVAL_SKIP
)
1448 rhstype
= check_typedef (value_type (rhs
));
1449 if (rust_range_type_p (rhstype
))
1452 error (_("Can't take slice of array without '&'"));
1453 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1457 low
= value_as_long (rhs
);
1459 struct type
*type
= check_typedef (value_type (lhs
));
1460 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1462 struct type
*base_type
= nullptr;
1463 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1464 base_type
= TYPE_TARGET_TYPE (type
);
1465 else if (rust_slice_type_p (type
))
1467 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1469 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1471 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1475 if (base_type
== nullptr)
1476 error (_("Could not find 'data_ptr' in slice type"));
1478 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1479 base_type
= TYPE_TARGET_TYPE (type
);
1481 error (_("Cannot subscript non-array type"));
1483 struct type
*new_type
;
1486 if (rust_slice_type_p (type
))
1491 = language_lookup_primitive_type (exp
->language_defn
,
1494 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1498 new_type
= base_type
;
1500 return value_zero (new_type
, VALUE_LVAL (lhs
));
1507 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1510 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1511 error (_("Can't compute array bounds"));
1513 error (_("Found array with non-zero lower bound"));
1516 else if (rust_slice_type_p (type
))
1520 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1521 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1523 high_bound
= value_as_long (len
);
1525 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1529 high_bound
= LONGEST_MAX
;
1532 error (_("Cannot subscript non-array type"));
1535 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1538 error (_("Index less than zero"));
1539 if (low
> high_bound
)
1540 error (_("Index greater than length"));
1542 result
= value_subscript (base
, low
);
1549 struct type
*usize
, *slice
;
1551 struct value
*addrval
, *tem
;
1553 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1556 error (_("High index less than zero"));
1558 error (_("Low index greater than high index"));
1559 if (high
> high_bound
)
1560 error (_("High index greater than length"));
1562 usize
= language_lookup_primitive_type (exp
->language_defn
,
1565 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1568 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1569 addr
= value_as_long (addrval
);
1570 tem
= value_at_lazy (slice
, addr
);
1572 value_assign (value_field (tem
, 0), value_addr (result
));
1573 value_assign (value_field (tem
, 1),
1574 value_from_longest (usize
, high
- low
));
1576 result
= value_at_lazy (slice
, addr
);
1579 result
= value_addr (result
);
1585 /* evaluate_exp implementation for Rust. */
1587 static struct value
*
1588 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1589 int *pos
, enum noside noside
)
1591 struct value
*result
;
1593 switch (exp
->elts
[*pos
].opcode
)
1595 case UNOP_COMPLEMENT
:
1597 struct value
*value
;
1600 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1601 if (noside
== EVAL_SKIP
)
1603 /* Preserving the type is enough. */
1606 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1607 result
= value_from_longest (value_type (value
),
1608 value_logical_not (value
));
1610 result
= value_complement (value
);
1614 case BINOP_SUBSCRIPT
:
1615 result
= rust_subscript (exp
, pos
, noside
, 0);
1619 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1625 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1626 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1629 struct value
*addrval
= NULL
;
1633 if (noside
== EVAL_NORMAL
)
1635 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1636 addr
= value_as_long (addrval
);
1637 result
= value_at_lazy (type
, addr
);
1640 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1645 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1646 if (noside
== EVAL_NORMAL
)
1648 /* This isn't quite right but will do for the time
1649 being, seeing that we can't implement the Copy
1651 value_assign (result
, init
);
1657 gdb_assert (arglen
% 2 == 0);
1658 for (i
= 0; i
< arglen
; i
+= 2)
1661 const char *fieldname
;
1662 struct value
*value
, *field
;
1664 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1666 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1668 fieldname
= &exp
->elts
[*pos
].string
;
1669 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1671 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1672 if (noside
== EVAL_NORMAL
)
1674 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1676 value_assign (field
, value
);
1680 if (noside
== EVAL_SKIP
)
1681 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1683 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1684 result
= allocate_value (type
);
1686 result
= value_at_lazy (type
, addr
);
1695 struct value
*ncopies
;
1697 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1698 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1699 copies
= value_as_long (ncopies
);
1701 error (_("Array with negative number of elements"));
1703 if (noside
== EVAL_NORMAL
)
1707 std::vector
<struct value
*> eltvec (copies
);
1709 for (i
= 0; i
< copies
; ++i
)
1711 result
= value_array (0, copies
- 1, eltvec
.data ());
1715 struct type
*arraytype
1716 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1717 result
= allocate_value (arraytype
);
1722 case STRUCTOP_ANONYMOUS
:
1724 /* Anonymous field access, i.e. foo.1. */
1726 int pc
, field_number
, nfields
;
1727 struct type
*type
, *variant_type
;
1728 struct disr_info disr
;
1731 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1733 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1735 type
= value_type (lhs
);
1736 /* Untagged unions can't have anonymous field access since
1737 they can only have named fields. */
1738 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1739 && !rust_union_is_untagged (type
))
1741 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1742 value_embedded_offset (lhs
),
1743 value_address (lhs
), lhs
);
1745 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1747 variant_type
= NULL
;
1752 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1753 nfields
= TYPE_NFIELDS (variant_type
);
1756 if (!disr
.is_encoded
)
1759 if (field_number
>= nfields
|| field_number
< 0)
1760 error(_("Cannot access field %d of variant %s, \
1761 there are only %d fields"),
1762 disr
.is_encoded
? field_number
: field_number
- 1,
1764 disr
.is_encoded
? nfields
: nfields
- 1);
1766 if (!(disr
.is_encoded
1767 ? rust_tuple_struct_type_p (variant_type
)
1768 : rust_tuple_variant_type_p (variant_type
)))
1769 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1771 result
= value_primitive_field (lhs
, 0, field_number
,
1774 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1776 /* Tuples and tuple structs */
1777 nfields
= TYPE_NFIELDS(type
);
1779 if (field_number
>= nfields
|| field_number
< 0)
1780 error(_("Cannot access field %d of %s, there are only %d fields"),
1781 field_number
, TYPE_TAG_NAME (type
), nfields
);
1783 /* Tuples are tuple structs too. */
1784 if (!rust_tuple_struct_type_p (type
))
1785 error(_("Attempting to access anonymous field %d of %s, which is \
1786 not a tuple, tuple struct, or tuple-like variant"),
1787 field_number
, TYPE_TAG_NAME (type
));
1789 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1792 error(_("Anonymous field access is only allowed on tuples, \
1793 tuple structs, and tuple-like enum variants"));
1797 case STRUCTOP_STRUCT
:
1804 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1805 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1806 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1808 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1809 type
= value_type (lhs
);
1810 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1811 && !rust_union_is_untagged (type
))
1814 struct disr_info disr
;
1815 struct type
*variant_type
;
1817 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1818 value_embedded_offset (lhs
),
1819 value_address (lhs
), lhs
);
1821 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1822 error(_("Could not find field %s of struct variant %s"),
1823 field_name
, disr
.name
.c_str ());
1825 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1827 if (variant_type
== NULL
1829 ? rust_tuple_struct_type_p (variant_type
)
1830 : rust_tuple_variant_type_p (variant_type
)))
1831 error(_("Attempting to access named field %s of tuple variant %s, \
1832 which has only anonymous fields"),
1833 field_name
, disr
.name
.c_str ());
1835 start
= disr
.is_encoded
? 0 : 1;
1836 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1838 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1840 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1845 if (i
== TYPE_NFIELDS (variant_type
))
1846 /* We didn't find it. */
1847 error(_("Could not find field %s of struct variant %s"),
1848 field_name
, disr
.name
.c_str ());
1852 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
,
1854 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1855 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1861 result
= rust_range (exp
, pos
, noside
);
1865 /* We might have &array[range], in which case we need to make a
1867 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1870 result
= rust_subscript (exp
, pos
, noside
, 1);
1875 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1882 /* operator_length implementation for Rust. */
1885 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1891 switch (exp
->elts
[pc
- 1].opcode
)
1894 /* We handle aggregate as a type and argument count. The first
1895 argument might be OP_OTHERS. After that the arguments
1896 alternate: first an OP_NAME, then an expression. */
1898 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1906 case STRUCTOP_ANONYMOUS
:
1917 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1925 /* op_name implementation for Rust. */
1928 rust_op_name (enum exp_opcode opcode
)
1933 return "OP_AGGREGATE";
1937 return op_name_standard (opcode
);
1941 /* dump_subexp_body implementation for Rust. */
1944 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1947 switch (exp
->elts
[elt
].opcode
)
1951 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1954 fprintf_filtered (stream
, "Type @");
1955 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1956 fprintf_filtered (stream
, " (");
1957 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1958 fprintf_filtered (stream
, "), length %d", length
);
1961 for (i
= 0; i
< length
; ++i
)
1962 elt
= dump_subexp (exp
, stream
, elt
);
1969 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1971 fprintf_filtered (stream
, "%s: %s",
1972 (exp
->elts
[elt
].opcode
== OP_STRING
1973 ? "string" : "name"),
1974 &exp
->elts
[elt
+ 2].string
);
1975 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1980 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1983 case STRUCTOP_ANONYMOUS
:
1987 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1989 fprintf_filtered (stream
, "Field number: %d", field_number
);
1990 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1999 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
2006 /* print_subexp implementation for Rust. */
2009 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
2010 enum precedence prec
)
2012 switch (exp
->elts
[*pos
].opcode
)
2016 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
2019 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
2020 fputs_filtered (" { ", stream
);
2023 for (i
= 0; i
< length
; ++i
)
2025 rust_print_subexp (exp
, pos
, stream
, prec
);
2026 fputs_filtered (", ", stream
);
2028 fputs_filtered (" }", stream
);
2034 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2036 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2037 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2043 fputs_filtered ("<<others>> (", stream
);
2045 rust_print_subexp (exp
, pos
, stream
, prec
);
2046 fputs_filtered (")", stream
);
2050 case STRUCTOP_ANONYMOUS
:
2052 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2055 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2056 fprintf_filtered (stream
, ".%d", tem
);
2062 fprintf_filtered (stream
, "[");
2063 rust_print_subexp (exp
, pos
, stream
, prec
);
2064 fprintf_filtered (stream
, "; ");
2065 rust_print_subexp (exp
, pos
, stream
, prec
);
2066 fprintf_filtered (stream
, "]");
2070 print_subexp_standard (exp
, pos
, stream
, prec
);
2075 /* operator_check implementation for Rust. */
2078 rust_operator_check (struct expression
*exp
, int pos
,
2079 int (*objfile_func
) (struct objfile
*objfile
,
2083 switch (exp
->elts
[pos
].opcode
)
2087 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2088 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2090 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2101 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2109 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2111 static struct block_symbol
2112 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2114 const struct block
*block
,
2115 const domain_enum domain
)
2117 struct block_symbol result
= {NULL
, NULL
};
2119 if (symbol_lookup_debug
)
2121 fprintf_unfiltered (gdb_stdlog
,
2122 "rust_lookup_symbol_non_local"
2123 " (%s, %s (scope %s), %s)\n",
2124 name
, host_address_to_string (block
),
2125 block_scope (block
), domain_name (domain
));
2128 /* Look up bare names in the block's scope. */
2129 if (name
[cp_find_first_component (name
)] == '\0')
2131 const char *scope
= block_scope (block
);
2133 if (scope
[0] != '\0')
2135 std::string scopedname
= std::string (scope
) + "::" + name
;
2137 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2139 if (result
.symbol
== NULL
)
2140 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2148 /* la_sniff_from_mangled_name for Rust. */
2151 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2153 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2154 return *demangled
!= NULL
;
2159 /* la_watch_location_expression for Rust. */
2161 static gdb::unique_xmalloc_ptr
<char>
2162 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2164 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2165 std::string name
= type_to_string (type
);
2166 return gdb::unique_xmalloc_ptr
<char>
2167 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2173 static const struct exp_descriptor exp_descriptor_rust
=
2176 rust_operator_length
,
2177 rust_operator_check
,
2179 rust_dump_subexp_body
,
2180 rust_evaluate_subexp
2183 static const char *rust_extensions
[] =
2188 extern const struct language_defn rust_language_defn
=
2198 &exp_descriptor_rust
,
2202 rust_printchar
, /* Print a character constant */
2203 rust_printstr
, /* Function to print string constant */
2204 rust_emitchar
, /* Print a single char */
2205 rust_print_type
, /* Print a type using appropriate syntax */
2206 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2207 rust_val_print
, /* Print a value using appropriate syntax */
2208 c_value_print
, /* Print a top-level value */
2209 default_read_var_value
, /* la_read_var_value */
2210 NULL
, /* Language specific skip_trampoline */
2211 NULL
, /* name_of_this */
2212 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2213 basic_lookup_transparent_type
,/* lookup_transparent_type */
2214 gdb_demangle
, /* Language specific symbol demangler */
2215 rust_sniff_from_mangled_name
,
2216 NULL
, /* Language specific
2217 class_name_from_physname */
2218 c_op_print_tab
, /* expression operators for printing */
2219 1, /* c-style arrays */
2220 0, /* String lower bound */
2221 default_word_break_characters
,
2222 default_collect_symbol_completion_matches
,
2223 rust_language_arch_info
,
2224 default_print_array_index
,
2225 default_pass_by_reference
,
2227 rust_watch_location_expression
,
2228 NULL
, /* la_get_symbol_name_cmp */
2229 iterate_over_symbols
,
2230 &default_varobj_ops
,