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 extern initialize_file_ftype _initialize_rust_language
;
40 /* Returns the last segment of a Rust path like foo::bar::baz. Will
41 not handle cases where the last segment contains generics. This
42 will return NULL if the last segment cannot be found. */
45 rust_last_path_segment (const char * path
)
47 const char *result
= strrchr (path
, ':');
54 /* See rust-lang.h. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
62 return std::string ();
64 return std::string (scope
, cp_find_first_component (scope
));
67 /* Information about the discriminant/variant of an enum */
73 /* Field number in union. Negative on error. For an encoded enum,
74 the "hidden" member will always be field 1, and the "real" member
75 will always be field 0. */
77 /* True if this is an encoded enum that has a single "real" member
78 and a single "hidden" member. */
79 unsigned int is_encoded
: 1;
82 /* The prefix of a specially-encoded enum. */
84 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
86 /* The number of the real field. */
88 #define RUST_ENCODED_ENUM_REAL 0
90 /* The number of the hidden field. */
92 #define RUST_ENCODED_ENUM_HIDDEN 1
94 /* Whether or not a TYPE_CODE_UNION value is an untagged union
95 as opposed to being a regular Rust enum. */
97 rust_union_is_untagged (struct type
*type
)
99 /* Unions must have at least one field. */
100 if (TYPE_NFIELDS (type
) == 0)
102 /* If the first field is named, but the name has the rust enum prefix,
104 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
105 strlen (RUST_ENUM_PREFIX
)) == 0)
107 /* Unions only have named fields. */
108 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
110 if (strlen (TYPE_FIELD_NAME (type
, i
)) == 0)
116 /* Utility function to get discriminant info for a given value. */
118 static struct disr_info
119 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
120 int embedded_offset
, CORE_ADDR address
,
124 struct disr_info ret
;
125 struct type
*disr_type
;
126 struct value_print_options opts
;
127 const char *name_segment
;
129 get_no_prettyformat_print_options (&opts
);
134 if (TYPE_NFIELDS (type
) == 0)
135 error (_("Encountered void enum value"));
137 /* If an enum has two values where one is empty and the other holds
138 a pointer that cannot be zero; then the Rust compiler optimizes
139 away the discriminant and instead uses a zero value in the
140 pointer field to indicate the empty variant. */
141 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
142 strlen (RUST_ENUM_PREFIX
)) == 0)
144 char *tail
, *token
, *saveptr
= NULL
;
145 unsigned long fieldno
;
146 struct type
*member_type
;
151 if (TYPE_NFIELDS (type
) != 1)
152 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
154 /* Optimized enums have only one field. */
155 member_type
= TYPE_FIELD_TYPE (type
, 0);
157 std::string
name (TYPE_FIELD_NAME (type
, 0));
158 tail
= &name
[0] + strlen (RUST_ENUM_PREFIX
);
160 /* The location of the value that doubles as a discriminant is
161 stored in the name of the field, as
162 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
163 where the fieldnos are the indices of the fields that should be
164 traversed in order to find the field (which may be several fields deep)
165 and the variantname is the name of the variant of the case when the
167 for (token
= strtok_r (tail
, "$", &saveptr
);
169 token
= strtok_r (NULL
, "$", &saveptr
))
171 if (sscanf (token
, "%lu", &fieldno
) != 1)
173 /* We have reached the enum name, which cannot start
177 if (fieldno
>= TYPE_NFIELDS (member_type
))
178 error (_("%s refers to field after end of member type"),
181 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
182 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
186 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
187 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
191 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
192 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + token
;
196 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
197 ret
.name
= (std::string (TYPE_NAME (type
)) + "::"
198 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))));
204 disr_type
= TYPE_FIELD_TYPE (type
, 0);
206 if (TYPE_NFIELDS (disr_type
) == 0)
208 /* This is a bounds check and should never be hit unless Rust
209 has changed its debuginfo format. */
210 error (_("Could not find enum discriminant field"));
212 else if (TYPE_NFIELDS (type
) == 1)
214 /* Sometimes univariant enums are encoded without a
215 discriminant. In that case, treating it as an encoded enum
216 with the first field being the actual type works. */
217 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
218 const char *last
= rust_last_path_segment (field_name
);
219 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + last
;
220 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
225 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
226 error (_("Rust debug format has changed"));
228 string_file temp_file
;
229 /* The first value of the first field (or any field)
230 is the discriminant value. */
231 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0),
232 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
233 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
237 ret
.name
= std::move (temp_file
.string ());
238 name_segment
= rust_last_path_segment (ret
.name
.c_str ());
239 if (name_segment
!= NULL
)
241 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
243 /* Sadly, the discriminant value paths do not match the type
244 field name paths ('core::option::Option::Some' vs
245 'core::option::Some'). However, enum variant names are
246 unique in the last path segment and the generics are not
247 part of this path, so we can just compare those. This is
248 hackish and would be better fixed by improving rustc's
249 metadata for enums. */
250 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
252 if (field_type
!= NULL
253 && strcmp (name_segment
,
254 rust_last_path_segment (field_type
)) == 0)
262 if (ret
.field_no
== -1 && !ret
.name
.empty ())
264 /* Somehow the discriminant wasn't found. */
265 error (_("Could not find variant of %s with discriminant %s"),
266 TYPE_TAG_NAME (type
), ret
.name
.c_str ());
272 /* See rust-lang.h. */
275 rust_tuple_type_p (struct type
*type
)
277 /* The current implementation is a bit of a hack, but there's
278 nothing else in the debuginfo to distinguish a tuple from a
280 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
281 && TYPE_TAG_NAME (type
) != NULL
282 && TYPE_TAG_NAME (type
)[0] == '(');
286 /* Return true if all non-static fields of a structlike type are in a
287 sequence like __0, __1, __2. OFFSET lets us skip fields. */
290 rust_underscore_fields (struct type
*type
, int offset
)
296 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
298 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
300 if (!field_is_static (&TYPE_FIELD (type
, i
)))
308 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
309 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
318 /* See rust-lang.h. */
321 rust_tuple_struct_type_p (struct type
*type
)
323 /* This is just an approximation until DWARF can represent Rust more
324 precisely. We exclude zero-length structs because they may not
325 be tuple structs, and there's no way to tell. */
326 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
329 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
332 rust_tuple_variant_type_p (struct type
*type
)
334 /* First field is discriminant */
335 return rust_underscore_fields (type
, 1);
338 /* Return true if TYPE is a slice type, otherwise false. */
341 rust_slice_type_p (struct type
*type
)
343 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
344 && TYPE_TAG_NAME (type
) != NULL
345 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
348 /* Return true if TYPE is a range type, otherwise false. */
351 rust_range_type_p (struct type
*type
)
355 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
356 || TYPE_NFIELDS (type
) > 2
357 || TYPE_TAG_NAME (type
) == NULL
358 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
361 if (TYPE_NFIELDS (type
) == 0)
365 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
367 if (TYPE_NFIELDS (type
) == 1)
371 else if (TYPE_NFIELDS (type
) == 2)
373 /* First field had to be "start". */
377 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
380 /* Return true if TYPE seems to be the type "u8", otherwise false. */
383 rust_u8_type_p (struct type
*type
)
385 return (TYPE_CODE (type
) == TYPE_CODE_INT
386 && TYPE_UNSIGNED (type
)
387 && TYPE_LENGTH (type
) == 1);
390 /* Return true if TYPE is a Rust character type. */
393 rust_chartype_p (struct type
*type
)
395 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
396 && TYPE_LENGTH (type
) == 4
397 && TYPE_UNSIGNED (type
));
402 /* la_emitchar implementation for Rust. */
405 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
407 if (!rust_chartype_p (type
))
408 generic_emit_char (c
, type
, stream
, quoter
,
409 target_charset (get_type_arch (type
)));
410 else if (c
== '\\' || c
== quoter
)
411 fprintf_filtered (stream
, "\\%c", c
);
413 fputs_filtered ("\\n", stream
);
415 fputs_filtered ("\\r", stream
);
417 fputs_filtered ("\\t", stream
);
419 fputs_filtered ("\\0", stream
);
420 else if (c
>= 32 && c
<= 127 && isprint (c
))
421 fputc_filtered (c
, stream
);
423 fprintf_filtered (stream
, "\\x%02x", c
);
425 fprintf_filtered (stream
, "\\u{%06x}", c
);
428 /* la_printchar implementation for Rust. */
431 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
433 fputs_filtered ("'", stream
);
434 LA_EMIT_CHAR (c
, type
, stream
, '\'');
435 fputs_filtered ("'", stream
);
438 /* la_printstr implementation for Rust. */
441 rust_printstr (struct ui_file
*stream
, struct type
*type
,
442 const gdb_byte
*string
, unsigned int length
,
443 const char *user_encoding
, int force_ellipses
,
444 const struct value_print_options
*options
)
446 /* Rust always uses UTF-8, but let the caller override this if need
448 const char *encoding
= user_encoding
;
449 if (user_encoding
== NULL
|| !*user_encoding
)
451 /* In Rust strings, characters are "u8". */
452 if (rust_u8_type_p (type
))
456 /* This is probably some C string, so let's let C deal with
458 c_printstr (stream
, type
, string
, length
, user_encoding
,
459 force_ellipses
, options
);
464 /* This is not ideal as it doesn't use our character printer. */
465 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
471 /* rust_print_type branch for structs and untagged unions. */
474 val_print_struct (struct type
*type
, int embedded_offset
,
475 CORE_ADDR address
, struct ui_file
*stream
,
476 int recurse
, struct value
*val
,
477 const struct value_print_options
*options
)
481 bool is_tuple
= rust_tuple_type_p (type
);
482 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
483 struct value_print_options opts
;
487 if (TYPE_TAG_NAME (type
) != NULL
)
488 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
490 if (TYPE_NFIELDS (type
) == 0)
493 if (TYPE_TAG_NAME (type
) != NULL
)
494 fputs_filtered (" ", stream
);
497 if (is_tuple
|| is_tuple_struct
)
498 fputs_filtered ("(", stream
);
500 fputs_filtered ("{", stream
);
506 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
508 if (field_is_static (&TYPE_FIELD (type
, i
)))
512 fputs_filtered (",", stream
);
514 if (options
->prettyformat
)
516 fputs_filtered ("\n", stream
);
517 print_spaces_filtered (2 + 2 * recurse
, stream
);
519 else if (!first_field
)
520 fputs_filtered (" ", stream
);
524 if (!is_tuple
&& !is_tuple_struct
)
526 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
527 fputs_filtered (": ", stream
);
530 val_print (TYPE_FIELD_TYPE (type
, i
),
531 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
533 stream
, recurse
+ 1, val
, &opts
,
537 if (options
->prettyformat
)
539 fputs_filtered ("\n", stream
);
540 print_spaces_filtered (2 * recurse
, stream
);
543 if (is_tuple
|| is_tuple_struct
)
544 fputs_filtered (")", stream
);
546 fputs_filtered ("}", stream
);
549 static const struct generic_val_print_decorations rust_decorations
=
551 /* Complex isn't used in Rust, but we provide C-ish values just in
563 /* la_val_print implementation for Rust. */
566 rust_val_print (struct type
*type
, int embedded_offset
,
567 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
569 const struct value_print_options
*options
)
571 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
573 type
= check_typedef (type
);
574 switch (TYPE_CODE (type
))
578 LONGEST low_bound
, high_bound
;
580 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
581 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
582 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
584 /* We have a pointer to a byte string, so just print
586 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
588 struct gdbarch
*arch
= get_type_arch (type
);
589 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
591 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
592 if (options
->addressprint
)
594 fputs_filtered (paddress (arch
, addr
), stream
);
595 fputs_filtered (" ", stream
);
598 fputs_filtered ("b", stream
);
599 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
600 high_bound
- low_bound
+ 1, stream
,
607 case TYPE_CODE_METHODPTR
:
608 case TYPE_CODE_MEMBERPTR
:
609 c_val_print (type
, embedded_offset
, address
, stream
,
610 recurse
, val
, options
);
614 /* Recognize the unit type. */
615 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
616 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
618 fputs_filtered ("()", stream
);
623 case TYPE_CODE_STRING
:
625 struct gdbarch
*arch
= get_type_arch (type
);
626 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
627 LONGEST low_bound
, high_bound
;
629 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
630 error (_("Could not determine the array bounds"));
632 /* If we see a plain TYPE_CODE_STRING, then we're printing a
633 byte string, hence the choice of "ASCII" as the
635 fputs_filtered ("b", stream
);
636 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
637 valaddr
+ embedded_offset
* unit_size
,
638 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
642 case TYPE_CODE_ARRAY
:
644 LONGEST low_bound
, high_bound
;
646 if (get_array_bounds (type
, &low_bound
, &high_bound
)
647 && high_bound
- low_bound
+ 1 == 0)
648 fputs_filtered ("[]", stream
);
654 case TYPE_CODE_UNION
:
656 int j
, nfields
, first_field
, is_tuple
, start
;
657 struct type
*variant_type
;
658 struct disr_info disr
;
659 struct value_print_options opts
;
661 /* Untagged unions are printed as if they are structs.
662 Since the field bit positions overlap in the debuginfo,
663 the code for printing a union is same as that for a struct,
664 the only difference is that the input type will have overlapping
666 if (rust_union_is_untagged (type
))
668 val_print_struct (type
, embedded_offset
, address
, stream
,
669 recurse
, val
, options
);
676 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
679 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
681 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
686 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
687 nfields
= TYPE_NFIELDS (variant_type
);
689 is_tuple
= (disr
.is_encoded
690 ? rust_tuple_struct_type_p (variant_type
)
691 : rust_tuple_variant_type_p (variant_type
));
692 start
= disr
.is_encoded
? 0 : 1;
696 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
698 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
701 /* struct variant. */
702 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
707 /* In case of a nullary variant like 'None', just output
709 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
713 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
716 fputs_filtered (", ", stream
);
720 fprintf_filtered (stream
, "%s: ",
721 TYPE_FIELD_NAME (variant_type
, j
));
723 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
725 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
726 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
728 stream
, recurse
+ 1, val
, &opts
,
733 fputs_filtered (")", stream
);
735 fputs_filtered ("}", stream
);
739 case TYPE_CODE_STRUCT
:
740 val_print_struct (type
, embedded_offset
, address
, stream
,
741 recurse
, val
, options
);
746 /* Nothing special yet. */
747 generic_val_print (type
, embedded_offset
, address
, stream
,
748 recurse
, val
, options
, &rust_decorations
);
755 rust_print_type (struct type
*type
, const char *varstring
,
756 struct ui_file
*stream
, int show
, int level
,
757 const struct type_print_options
*flags
);
759 /* Print a struct or union typedef. */
761 rust_print_struct_def (struct type
*type
, const char *varstring
,
762 struct ui_file
*stream
, int show
, int level
,
763 const struct type_print_options
*flags
)
765 bool is_tuple_struct
;
768 /* Print a tuple type simply. */
769 if (rust_tuple_type_p (type
))
771 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
775 /* If we see a base class, delegate to C. */
776 if (TYPE_N_BASECLASSES (type
) > 0)
777 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
779 /* This code path is also used by unions. */
780 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
781 fputs_filtered ("struct ", stream
);
783 fputs_filtered ("union ", stream
);
785 if (TYPE_TAG_NAME (type
) != NULL
)
786 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
788 is_tuple_struct
= rust_tuple_struct_type_p (type
);
790 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
792 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
794 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
799 if (field_is_static (&TYPE_FIELD (type
, i
)))
802 /* We'd like to print "pub" here as needed, but rustc
803 doesn't emit the debuginfo, and our types don't have
804 cplus_struct_type attached. */
806 /* For a tuple struct we print the type but nothing
808 print_spaces_filtered (level
+ 2, stream
);
809 if (!is_tuple_struct
)
810 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
812 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
813 stream
, show
- 1, level
+ 2,
815 fputs_filtered (",\n", stream
);
818 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
821 /* la_print_typedef implementation for Rust. */
824 rust_print_typedef (struct type
*type
,
825 struct symbol
*new_symbol
,
826 struct ui_file
*stream
)
828 type
= check_typedef (type
);
829 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
830 type_print (type
, "", stream
, 0);
831 fprintf_filtered (stream
, ";\n");
834 /* la_print_type implementation for Rust. */
837 rust_print_type (struct type
*type
, const char *varstring
,
838 struct ui_file
*stream
, int show
, int level
,
839 const struct type_print_options
*flags
)
845 && TYPE_NAME (type
) != NULL
)
847 /* Rust calls the unit type "void" in its debuginfo,
848 but we don't want to print it as that. */
849 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
850 fputs_filtered ("()", stream
);
852 fputs_filtered (TYPE_NAME (type
), stream
);
856 type
= check_typedef (type
);
857 switch (TYPE_CODE (type
))
860 fputs_filtered ("()", stream
);
864 /* Delegate varargs to the C printer. */
865 if (TYPE_VARARGS (type
))
868 fputs_filtered ("fn ", stream
);
869 if (varstring
!= NULL
)
870 fputs_filtered (varstring
, stream
);
871 fputs_filtered ("(", stream
);
872 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
876 fputs_filtered (", ", stream
);
877 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
880 fputs_filtered (")", stream
);
881 /* If it returns unit, we can omit the return type. */
882 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
884 fputs_filtered (" -> ", stream
);
885 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
889 case TYPE_CODE_ARRAY
:
891 LONGEST low_bound
, high_bound
;
893 fputs_filtered ("[", stream
);
894 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
895 stream
, show
- 1, level
, flags
);
897 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
898 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
899 fprintf_filtered (stream
, "; variable length");
900 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
901 fprintf_filtered (stream
, "; %s",
902 plongest (high_bound
- low_bound
+ 1));
903 fputs_filtered ("]", stream
);
907 case TYPE_CODE_STRUCT
:
908 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
915 fputs_filtered ("enum ", stream
);
916 if (TYPE_TAG_NAME (type
) != NULL
)
918 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
919 fputs_filtered (" ", stream
);
920 len
= strlen (TYPE_TAG_NAME (type
));
922 fputs_filtered ("{\n", stream
);
924 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
926 const char *name
= TYPE_FIELD_NAME (type
, i
);
931 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
933 && name
[len
+ 1] == ':')
935 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
938 fputs_filtered ("}", stream
);
942 case TYPE_CODE_UNION
:
946 /* Skip the discriminant field. */
949 /* Unions and structs have the same syntax in Rust,
950 the only difference is that structs are declared with `struct`
951 and union with `union`. This difference is handled in the struct
953 if (rust_union_is_untagged (type
))
955 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
959 fputs_filtered ("enum ", stream
);
960 if (TYPE_TAG_NAME (type
) != NULL
)
962 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
963 fputs_filtered (" ", stream
);
965 fputs_filtered ("{\n", stream
);
967 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
968 strlen (RUST_ENUM_PREFIX
)) == 0)
970 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
971 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
973 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
974 /* There is no explicit discriminant field, skip nothing. */
978 else if (TYPE_NFIELDS (type
) == 1)
981 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
983 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
985 = rust_last_path_segment (TYPE_NAME (variant_type
));
987 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
989 if (TYPE_NFIELDS (variant_type
) > skip_to
)
992 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
993 ? rust_tuple_struct_type_p (variant_type
)
994 : rust_tuple_variant_type_p (variant_type
));
997 fputs_filtered (is_tuple
? "(" : "{", stream
);
998 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1003 fputs_filtered (", ", stream
);
1006 fprintf_filtered (stream
, "%s: ",
1007 TYPE_FIELD_NAME (variant_type
, j
));
1009 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1010 stream
, show
- 1, level
+ 2,
1013 fputs_filtered (is_tuple
? ")" : "}", stream
);
1016 fputs_filtered (",\n", stream
);
1019 fputs_filtered ("}", stream
);
1025 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1031 /* Compute the alignment of the type T. */
1034 rust_type_alignment (struct type
*t
)
1036 t
= check_typedef (t
);
1037 switch (TYPE_CODE (t
))
1040 error (_("Could not compute alignment of type"));
1043 case TYPE_CODE_ENUM
:
1047 case TYPE_CODE_CHAR
:
1048 case TYPE_CODE_BOOL
:
1049 return TYPE_LENGTH (t
);
1051 case TYPE_CODE_ARRAY
:
1052 case TYPE_CODE_COMPLEX
:
1053 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1055 case TYPE_CODE_STRUCT
:
1056 case TYPE_CODE_UNION
:
1061 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1063 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1072 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1073 -- either on an obstack or on a gdbarch. */
1075 static struct type
*
1076 rust_composite_type (struct type
*original
,
1078 const char *field1
, struct type
*type1
,
1079 const char *field2
, struct type
*type2
)
1081 struct type
*result
= alloc_type_copy (original
);
1082 int i
, nfields
, bitpos
;
1090 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1091 TYPE_NAME (result
) = name
;
1092 TYPE_TAG_NAME (result
) = name
;
1094 TYPE_NFIELDS (result
) = nfields
;
1095 TYPE_FIELDS (result
)
1096 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1102 struct field
*field
= &TYPE_FIELD (result
, i
);
1104 SET_FIELD_BITPOS (*field
, bitpos
);
1105 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1107 FIELD_NAME (*field
) = field1
;
1108 FIELD_TYPE (*field
) = type1
;
1113 struct field
*field
= &TYPE_FIELD (result
, i
);
1114 int align
= rust_type_alignment (type2
);
1120 align
*= TARGET_CHAR_BIT
;
1121 delta
= bitpos
% align
;
1123 bitpos
+= align
- delta
;
1125 SET_FIELD_BITPOS (*field
, bitpos
);
1127 FIELD_NAME (*field
) = field2
;
1128 FIELD_TYPE (*field
) = type2
;
1133 TYPE_LENGTH (result
)
1134 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1135 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1139 /* See rust-lang.h. */
1142 rust_slice_type (const char *name
, struct type
*elt_type
,
1143 struct type
*usize_type
)
1147 elt_type
= lookup_pointer_type (elt_type
);
1148 type
= rust_composite_type (elt_type
, name
,
1149 "data_ptr", elt_type
,
1150 "length", usize_type
);
1155 enum rust_primitive_types
1157 rust_primitive_bool
,
1158 rust_primitive_char
,
1167 rust_primitive_isize
,
1168 rust_primitive_usize
,
1171 rust_primitive_unit
,
1173 nr_rust_primitive_types
1176 /* la_language_arch_info implementation for Rust. */
1179 rust_language_arch_info (struct gdbarch
*gdbarch
,
1180 struct language_arch_info
*lai
)
1182 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1184 struct type
**types
;
1185 unsigned int length
;
1187 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1190 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1191 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1192 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1193 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1194 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1195 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1196 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1197 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1198 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1199 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1201 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1202 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1203 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1205 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1206 floatformats_ieee_single
);
1207 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1208 floatformats_ieee_double
);
1210 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1212 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1213 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1214 types
[rust_primitive_usize
]);
1216 lai
->primitive_type_vector
= types
;
1217 lai
->bool_type_default
= types
[rust_primitive_bool
];
1218 lai
->string_char_type
= types
[rust_primitive_u8
];
1223 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1225 static struct value
*
1226 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1229 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1231 struct value
*function
, *result
, *arg0
;
1232 struct type
*type
, *fn_type
;
1233 const struct block
*block
;
1234 struct block_symbol sym
;
1236 /* For an ordinary function call we can simply defer to the
1237 generic implementation. */
1238 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1239 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1241 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1243 method
= &exp
->elts
[*pos
+ 1].string
;
1244 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1246 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1247 type in order to look up the method. */
1248 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1250 if (noside
== EVAL_SKIP
)
1252 for (i
= 0; i
< num_args
; ++i
)
1253 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1257 std::vector
<struct value
*> args (num_args
+ 1);
1260 /* We don't yet implement real Deref semantics. */
1261 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1262 args
[0] = value_ind (args
[0]);
1264 type
= value_type (args
[0]);
1265 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1266 && TYPE_CODE (type
) != TYPE_CODE_UNION
1267 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1268 || rust_tuple_type_p (type
))
1269 error (_("Method calls only supported on struct or enum types"));
1270 if (TYPE_TAG_NAME (type
) == NULL
)
1271 error (_("Method call on nameless type"));
1273 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1275 block
= get_selected_block (0);
1276 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1277 if (sym
.symbol
== NULL
)
1278 error (_("Could not find function named '%s'"), name
.c_str ());
1280 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1281 if (TYPE_NFIELDS (fn_type
) == 0)
1282 error (_("Function '%s' takes no arguments"), name
.c_str ());
1284 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1285 args
[0] = value_addr (args
[0]);
1287 function
= address_of_variable (sym
.symbol
, block
);
1289 for (i
= 0; i
< num_args
; ++i
)
1290 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1292 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1293 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1295 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1299 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1301 static struct value
*
1302 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1304 enum range_type kind
;
1305 struct value
*low
= NULL
, *high
= NULL
;
1306 struct value
*addrval
, *result
;
1308 struct type
*range_type
;
1309 struct type
*index_type
;
1310 struct type
*temp_type
;
1313 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1316 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1317 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1318 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1319 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1321 if (noside
== EVAL_SKIP
)
1322 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1329 name
= "std::ops::RangeFull";
1333 index_type
= value_type (high
);
1334 name
= "std::ops::RangeTo";
1341 index_type
= value_type (low
);
1342 name
= "std::ops::RangeFrom";
1346 if (!types_equal (value_type (low
), value_type (high
)))
1347 error (_("Range expression with different types"));
1348 index_type
= value_type (low
);
1349 name
= "std::ops::Range";
1353 /* If we don't have an index type, just allocate this on the
1354 arch. Here any type will do. */
1355 temp_type
= (index_type
== NULL
1356 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1358 /* It would be nicer to cache the range type. */
1359 range_type
= rust_composite_type (temp_type
, name
,
1360 low
== NULL
? NULL
: "start", index_type
,
1361 high
== NULL
? NULL
: "end", index_type
);
1363 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1364 return value_zero (range_type
, lval_memory
);
1366 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1367 addr
= value_as_long (addrval
);
1368 result
= value_at_lazy (range_type
, addr
);
1372 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1375 value_assign (start
, low
);
1380 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1383 value_assign (end
, high
);
1386 result
= value_at_lazy (range_type
, addr
);
1390 /* A helper function to compute the range and kind given a range
1391 value. TYPE is the type of the range value. RANGE is the range
1392 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1393 parameters might be filled in, or might not be, depending on the
1394 kind of range this is. KIND will always be set to the appropriate
1395 value describing the kind of range, and this can be used to
1396 determine whether LOW or HIGH are valid. */
1399 rust_compute_range (struct type
*type
, struct value
*range
,
1400 LONGEST
*low
, LONGEST
*high
,
1401 enum range_type
*kind
)
1407 *kind
= BOTH_BOUND_DEFAULT
;
1409 if (TYPE_NFIELDS (type
) == 0)
1413 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1415 *kind
= HIGH_BOUND_DEFAULT
;
1416 *low
= value_as_long (value_field (range
, 0));
1419 if (TYPE_NFIELDS (type
) > i
1420 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1422 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1423 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1424 *high
= value_as_long (value_field (range
, i
));
1428 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1430 static struct value
*
1431 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1434 struct value
*lhs
, *rhs
, *result
;
1435 struct type
*rhstype
;
1436 LONGEST low
, high_bound
;
1437 /* Initialized to appease the compiler. */
1438 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1443 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1444 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1446 if (noside
== EVAL_SKIP
)
1449 rhstype
= check_typedef (value_type (rhs
));
1450 if (rust_range_type_p (rhstype
))
1453 error (_("Can't take slice of array without '&'"));
1454 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1458 low
= value_as_long (rhs
);
1460 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1462 struct type
*type
= check_typedef (value_type (lhs
));
1464 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1470 struct type
*type
= check_typedef (value_type (lhs
));
1472 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1475 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1476 error (_("Can't compute array bounds"));
1478 error (_("Found array with non-zero lower bound"));
1481 else if (rust_slice_type_p (type
))
1485 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1486 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1488 high_bound
= value_as_long (len
);
1490 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1494 high_bound
= LONGEST_MAX
;
1497 error (_("Cannot subscript non-array type"));
1500 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1503 error (_("Index less than zero"));
1504 if (low
> high_bound
)
1505 error (_("Index greater than length"));
1507 result
= value_subscript (base
, low
);
1514 struct type
*usize
, *slice
;
1516 struct value
*addrval
, *tem
;
1518 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1521 error (_("High index less than zero"));
1523 error (_("Low index greater than high index"));
1524 if (high
> high_bound
)
1525 error (_("High index greater than length"));
1527 usize
= language_lookup_primitive_type (exp
->language_defn
,
1530 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1533 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1534 addr
= value_as_long (addrval
);
1535 tem
= value_at_lazy (slice
, addr
);
1537 value_assign (value_field (tem
, 0), value_addr (result
));
1538 value_assign (value_field (tem
, 1),
1539 value_from_longest (usize
, high
- low
));
1541 result
= value_at_lazy (slice
, addr
);
1544 result
= value_addr (result
);
1550 /* evaluate_exp implementation for Rust. */
1552 static struct value
*
1553 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1554 int *pos
, enum noside noside
)
1556 struct value
*result
;
1558 switch (exp
->elts
[*pos
].opcode
)
1560 case UNOP_COMPLEMENT
:
1562 struct value
*value
;
1565 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1566 if (noside
== EVAL_SKIP
)
1568 /* Preserving the type is enough. */
1571 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1572 result
= value_from_longest (value_type (value
),
1573 value_logical_not (value
));
1575 result
= value_complement (value
);
1579 case BINOP_SUBSCRIPT
:
1580 result
= rust_subscript (exp
, pos
, noside
, 0);
1584 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1590 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1591 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1594 struct value
*addrval
= NULL
;
1598 if (noside
== EVAL_NORMAL
)
1600 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1601 addr
= value_as_long (addrval
);
1602 result
= value_at_lazy (type
, addr
);
1605 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1610 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1611 if (noside
== EVAL_NORMAL
)
1613 /* This isn't quite right but will do for the time
1614 being, seeing that we can't implement the Copy
1616 value_assign (result
, init
);
1622 gdb_assert (arglen
% 2 == 0);
1623 for (i
= 0; i
< arglen
; i
+= 2)
1626 const char *fieldname
;
1627 struct value
*value
, *field
;
1629 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1631 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1633 fieldname
= &exp
->elts
[*pos
].string
;
1634 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1636 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1637 if (noside
== EVAL_NORMAL
)
1639 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1641 value_assign (field
, value
);
1645 if (noside
== EVAL_SKIP
)
1646 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1648 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1649 result
= allocate_value (type
);
1651 result
= value_at_lazy (type
, addr
);
1660 struct value
*ncopies
;
1662 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1663 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1664 copies
= value_as_long (ncopies
);
1666 error (_("Array with negative number of elements"));
1668 if (noside
== EVAL_NORMAL
)
1672 std::vector
<struct value
*> eltvec (copies
);
1674 for (i
= 0; i
< copies
; ++i
)
1676 result
= value_array (0, copies
- 1, eltvec
.data ());
1680 struct type
*arraytype
1681 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1682 result
= allocate_value (arraytype
);
1687 case STRUCTOP_ANONYMOUS
:
1689 /* Anonymous field access, i.e. foo.1. */
1691 int pc
, field_number
, nfields
;
1692 struct type
*type
, *variant_type
;
1693 struct disr_info disr
;
1696 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1698 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1700 type
= value_type (lhs
);
1701 /* Untagged unions can't have anonymous field access since
1702 they can only have named fields. */
1703 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1704 && !rust_union_is_untagged (type
))
1706 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1707 value_embedded_offset (lhs
),
1708 value_address (lhs
), lhs
);
1710 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1712 variant_type
= NULL
;
1717 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1718 nfields
= TYPE_NFIELDS (variant_type
);
1721 if (!disr
.is_encoded
)
1724 if (field_number
>= nfields
|| field_number
< 0)
1725 error(_("Cannot access field %d of variant %s, \
1726 there are only %d fields"),
1727 disr
.is_encoded
? field_number
: field_number
- 1,
1729 disr
.is_encoded
? nfields
: nfields
- 1);
1731 if (!(disr
.is_encoded
1732 ? rust_tuple_struct_type_p (variant_type
)
1733 : rust_tuple_variant_type_p (variant_type
)))
1734 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1736 result
= value_primitive_field (lhs
, 0, field_number
,
1739 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1741 /* Tuples and tuple structs */
1742 nfields
= TYPE_NFIELDS(type
);
1744 if (field_number
>= nfields
|| field_number
< 0)
1745 error(_("Cannot access field %d of %s, there are only %d fields"),
1746 field_number
, TYPE_TAG_NAME (type
), nfields
);
1748 /* Tuples are tuple structs too. */
1749 if (!rust_tuple_struct_type_p (type
))
1750 error(_("Attempting to access anonymous field %d of %s, which is \
1751 not a tuple, tuple struct, or tuple-like variant"),
1752 field_number
, TYPE_TAG_NAME (type
));
1754 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1757 error(_("Anonymous field access is only allowed on tuples, \
1758 tuple structs, and tuple-like enum variants"));
1762 case STRUCTOP_STRUCT
:
1769 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1770 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1771 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1773 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1774 type
= value_type (lhs
);
1775 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1776 && !rust_union_is_untagged (type
))
1779 struct disr_info disr
;
1780 struct type
*variant_type
;
1782 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1783 value_embedded_offset (lhs
),
1784 value_address (lhs
), lhs
);
1786 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1787 error(_("Could not find field %s of struct variant %s"),
1788 field_name
, disr
.name
.c_str ());
1790 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1792 if (variant_type
== NULL
1794 ? rust_tuple_struct_type_p (variant_type
)
1795 : rust_tuple_variant_type_p (variant_type
)))
1796 error(_("Attempting to access named field %s of tuple variant %s, \
1797 which has only anonymous fields"),
1798 field_name
, disr
.name
.c_str ());
1800 start
= disr
.is_encoded
? 0 : 1;
1801 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1803 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1805 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1810 if (i
== TYPE_NFIELDS (variant_type
))
1811 /* We didn't find it. */
1812 error(_("Could not find field %s of struct variant %s"),
1813 field_name
, disr
.name
.c_str ());
1817 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
,
1819 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1820 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1826 result
= rust_range (exp
, pos
, noside
);
1830 /* We might have &array[range], in which case we need to make a
1832 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1835 result
= rust_subscript (exp
, pos
, noside
, 1);
1840 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1847 /* operator_length implementation for Rust. */
1850 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1856 switch (exp
->elts
[pc
- 1].opcode
)
1859 /* We handle aggregate as a type and argument count. The first
1860 argument might be OP_OTHERS. After that the arguments
1861 alternate: first an OP_NAME, then an expression. */
1863 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1871 case STRUCTOP_ANONYMOUS
:
1882 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1890 /* op_name implementation for Rust. */
1893 rust_op_name (enum exp_opcode opcode
)
1898 return "OP_AGGREGATE";
1902 return op_name_standard (opcode
);
1906 /* dump_subexp_body implementation for Rust. */
1909 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1912 switch (exp
->elts
[elt
].opcode
)
1916 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1919 fprintf_filtered (stream
, "Type @");
1920 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1921 fprintf_filtered (stream
, " (");
1922 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1923 fprintf_filtered (stream
, "), length %d", length
);
1926 for (i
= 0; i
< length
; ++i
)
1927 elt
= dump_subexp (exp
, stream
, elt
);
1934 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1936 fprintf_filtered (stream
, "%s: %s",
1937 (exp
->elts
[elt
].opcode
== OP_STRING
1938 ? "string" : "name"),
1939 &exp
->elts
[elt
+ 2].string
);
1940 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1945 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1948 case STRUCTOP_ANONYMOUS
:
1952 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1954 fprintf_filtered (stream
, "Field number: %d", field_number
);
1955 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1964 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1971 /* print_subexp implementation for Rust. */
1974 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1975 enum precedence prec
)
1977 switch (exp
->elts
[*pos
].opcode
)
1981 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1984 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1985 fputs_filtered (" { ", stream
);
1988 for (i
= 0; i
< length
; ++i
)
1990 rust_print_subexp (exp
, pos
, stream
, prec
);
1991 fputs_filtered (", ", stream
);
1993 fputs_filtered (" }", stream
);
1999 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2001 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2002 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2008 fputs_filtered ("<<others>> (", stream
);
2010 rust_print_subexp (exp
, pos
, stream
, prec
);
2011 fputs_filtered (")", stream
);
2015 case STRUCTOP_ANONYMOUS
:
2017 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2020 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2021 fprintf_filtered (stream
, ".%d", tem
);
2027 fprintf_filtered (stream
, "[");
2028 rust_print_subexp (exp
, pos
, stream
, prec
);
2029 fprintf_filtered (stream
, "; ");
2030 rust_print_subexp (exp
, pos
, stream
, prec
);
2031 fprintf_filtered (stream
, "]");
2035 print_subexp_standard (exp
, pos
, stream
, prec
);
2040 /* operator_check implementation for Rust. */
2043 rust_operator_check (struct expression
*exp
, int pos
,
2044 int (*objfile_func
) (struct objfile
*objfile
,
2048 switch (exp
->elts
[pos
].opcode
)
2052 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2053 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2055 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2066 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2074 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2076 static struct block_symbol
2077 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2079 const struct block
*block
,
2080 const domain_enum domain
)
2082 struct block_symbol result
= {NULL
, NULL
};
2084 if (symbol_lookup_debug
)
2086 fprintf_unfiltered (gdb_stdlog
,
2087 "rust_lookup_symbol_non_local"
2088 " (%s, %s (scope %s), %s)\n",
2089 name
, host_address_to_string (block
),
2090 block_scope (block
), domain_name (domain
));
2093 /* Look up bare names in the block's scope. */
2094 if (name
[cp_find_first_component (name
)] == '\0')
2096 const char *scope
= block_scope (block
);
2098 if (scope
[0] != '\0')
2100 std::string scopedname
= std::string (scope
) + "::" + name
;
2102 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2104 if (result
.symbol
== NULL
)
2105 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2113 /* la_sniff_from_mangled_name for Rust. */
2116 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2118 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2119 return *demangled
!= NULL
;
2124 /* la_watch_location_expression for Rust. */
2126 static gdb::unique_xmalloc_ptr
<char>
2127 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2129 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2130 std::string name
= type_to_string (type
);
2131 return gdb::unique_xmalloc_ptr
<char>
2132 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2138 static const struct exp_descriptor exp_descriptor_rust
=
2141 rust_operator_length
,
2142 rust_operator_check
,
2144 rust_dump_subexp_body
,
2145 rust_evaluate_subexp
2148 static const char *rust_extensions
[] =
2153 static const struct language_defn rust_language_defn
=
2163 &exp_descriptor_rust
,
2167 rust_printchar
, /* Print a character constant */
2168 rust_printstr
, /* Function to print string constant */
2169 rust_emitchar
, /* Print a single char */
2170 rust_print_type
, /* Print a type using appropriate syntax */
2171 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2172 rust_val_print
, /* Print a value using appropriate syntax */
2173 c_value_print
, /* Print a top-level value */
2174 default_read_var_value
, /* la_read_var_value */
2175 NULL
, /* Language specific skip_trampoline */
2176 NULL
, /* name_of_this */
2177 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2178 basic_lookup_transparent_type
,/* lookup_transparent_type */
2179 gdb_demangle
, /* Language specific symbol demangler */
2180 rust_sniff_from_mangled_name
,
2181 NULL
, /* Language specific
2182 class_name_from_physname */
2183 c_op_print_tab
, /* expression operators for printing */
2184 1, /* c-style arrays */
2185 0, /* String lower bound */
2186 default_word_break_characters
,
2187 default_collect_symbol_completion_matches
,
2188 rust_language_arch_info
,
2189 default_print_array_index
,
2190 default_pass_by_reference
,
2192 rust_watch_location_expression
,
2193 NULL
, /* la_get_symbol_name_cmp */
2194 iterate_over_symbols
,
2195 &default_varobj_ops
,
2202 _initialize_rust_language (void)
2204 add_language (&rust_language_defn
);