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
);
896 fputs_filtered ("; ", stream
);
898 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
899 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
900 fprintf_filtered (stream
, "variable length");
901 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
902 fprintf_filtered (stream
, "%s",
903 plongest (high_bound
- low_bound
+ 1));
904 fputs_filtered ("]", stream
);
908 case TYPE_CODE_STRUCT
:
909 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
916 fputs_filtered ("enum ", stream
);
917 if (TYPE_TAG_NAME (type
) != NULL
)
919 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
920 fputs_filtered (" ", stream
);
921 len
= strlen (TYPE_TAG_NAME (type
));
923 fputs_filtered ("{\n", stream
);
925 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
927 const char *name
= TYPE_FIELD_NAME (type
, i
);
932 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
934 && name
[len
+ 1] == ':')
936 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
939 fputs_filtered ("}", stream
);
943 case TYPE_CODE_UNION
:
947 /* Skip the discriminant field. */
950 /* Unions and structs have the same syntax in Rust,
951 the only difference is that structs are declared with `struct`
952 and union with `union`. This difference is handled in the struct
954 if (rust_union_is_untagged (type
))
956 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
960 fputs_filtered ("enum ", stream
);
961 if (TYPE_TAG_NAME (type
) != NULL
)
963 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
964 fputs_filtered (" ", stream
);
966 fputs_filtered ("{\n", stream
);
968 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
969 strlen (RUST_ENUM_PREFIX
)) == 0)
971 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
972 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
974 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
975 /* There is no explicit discriminant field, skip nothing. */
979 else if (TYPE_NFIELDS (type
) == 1)
982 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
984 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
986 = rust_last_path_segment (TYPE_NAME (variant_type
));
988 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
990 if (TYPE_NFIELDS (variant_type
) > skip_to
)
993 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
994 ? rust_tuple_struct_type_p (variant_type
)
995 : rust_tuple_variant_type_p (variant_type
));
998 fputs_filtered (is_tuple
? "(" : "{", stream
);
999 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1004 fputs_filtered (", ", stream
);
1007 fprintf_filtered (stream
, "%s: ",
1008 TYPE_FIELD_NAME (variant_type
, j
));
1010 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1011 stream
, show
- 1, level
+ 2,
1014 fputs_filtered (is_tuple
? ")" : "}", stream
);
1017 fputs_filtered (",\n", stream
);
1020 fputs_filtered ("}", stream
);
1026 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1032 /* Compute the alignment of the type T. */
1035 rust_type_alignment (struct type
*t
)
1037 t
= check_typedef (t
);
1038 switch (TYPE_CODE (t
))
1041 error (_("Could not compute alignment of type"));
1044 case TYPE_CODE_ENUM
:
1048 case TYPE_CODE_CHAR
:
1049 case TYPE_CODE_BOOL
:
1050 return TYPE_LENGTH (t
);
1052 case TYPE_CODE_ARRAY
:
1053 case TYPE_CODE_COMPLEX
:
1054 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1056 case TYPE_CODE_STRUCT
:
1057 case TYPE_CODE_UNION
:
1062 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1064 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1073 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1074 -- either on an obstack or on a gdbarch. */
1076 static struct type
*
1077 rust_composite_type (struct type
*original
,
1079 const char *field1
, struct type
*type1
,
1080 const char *field2
, struct type
*type2
)
1082 struct type
*result
= alloc_type_copy (original
);
1083 int i
, nfields
, bitpos
;
1091 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1092 TYPE_NAME (result
) = name
;
1093 TYPE_TAG_NAME (result
) = name
;
1095 TYPE_NFIELDS (result
) = nfields
;
1096 TYPE_FIELDS (result
)
1097 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1103 struct field
*field
= &TYPE_FIELD (result
, i
);
1105 SET_FIELD_BITPOS (*field
, bitpos
);
1106 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1108 FIELD_NAME (*field
) = field1
;
1109 FIELD_TYPE (*field
) = type1
;
1114 struct field
*field
= &TYPE_FIELD (result
, i
);
1115 int align
= rust_type_alignment (type2
);
1121 align
*= TARGET_CHAR_BIT
;
1122 delta
= bitpos
% align
;
1124 bitpos
+= align
- delta
;
1126 SET_FIELD_BITPOS (*field
, bitpos
);
1128 FIELD_NAME (*field
) = field2
;
1129 FIELD_TYPE (*field
) = type2
;
1134 TYPE_LENGTH (result
)
1135 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1136 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1140 /* See rust-lang.h. */
1143 rust_slice_type (const char *name
, struct type
*elt_type
,
1144 struct type
*usize_type
)
1148 elt_type
= lookup_pointer_type (elt_type
);
1149 type
= rust_composite_type (elt_type
, name
,
1150 "data_ptr", elt_type
,
1151 "length", usize_type
);
1156 enum rust_primitive_types
1158 rust_primitive_bool
,
1159 rust_primitive_char
,
1168 rust_primitive_isize
,
1169 rust_primitive_usize
,
1172 rust_primitive_unit
,
1174 nr_rust_primitive_types
1177 /* la_language_arch_info implementation for Rust. */
1180 rust_language_arch_info (struct gdbarch
*gdbarch
,
1181 struct language_arch_info
*lai
)
1183 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1185 struct type
**types
;
1186 unsigned int length
;
1188 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1191 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1192 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1193 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1194 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1195 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1196 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1197 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1198 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1199 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1200 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1202 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1203 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1204 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1206 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1207 floatformats_ieee_single
);
1208 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1209 floatformats_ieee_double
);
1211 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1213 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1214 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1215 types
[rust_primitive_usize
]);
1217 lai
->primitive_type_vector
= types
;
1218 lai
->bool_type_default
= types
[rust_primitive_bool
];
1219 lai
->string_char_type
= types
[rust_primitive_u8
];
1224 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1226 static struct value
*
1227 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1230 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1232 struct value
*function
, *result
, *arg0
;
1233 struct type
*type
, *fn_type
;
1234 const struct block
*block
;
1235 struct block_symbol sym
;
1237 /* For an ordinary function call we can simply defer to the
1238 generic implementation. */
1239 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1240 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1242 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1244 method
= &exp
->elts
[*pos
+ 1].string
;
1245 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1247 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1248 type in order to look up the method. */
1249 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1251 if (noside
== EVAL_SKIP
)
1253 for (i
= 0; i
< num_args
; ++i
)
1254 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1258 std::vector
<struct value
*> args (num_args
+ 1);
1261 /* We don't yet implement real Deref semantics. */
1262 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1263 args
[0] = value_ind (args
[0]);
1265 type
= value_type (args
[0]);
1266 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1267 && TYPE_CODE (type
) != TYPE_CODE_UNION
1268 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1269 || rust_tuple_type_p (type
))
1270 error (_("Method calls only supported on struct or enum types"));
1271 if (TYPE_TAG_NAME (type
) == NULL
)
1272 error (_("Method call on nameless type"));
1274 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1276 block
= get_selected_block (0);
1277 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1278 if (sym
.symbol
== NULL
)
1279 error (_("Could not find function named '%s'"), name
.c_str ());
1281 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1282 if (TYPE_NFIELDS (fn_type
) == 0)
1283 error (_("Function '%s' takes no arguments"), name
.c_str ());
1285 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1286 args
[0] = value_addr (args
[0]);
1288 function
= address_of_variable (sym
.symbol
, block
);
1290 for (i
= 0; i
< num_args
; ++i
)
1291 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1293 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1294 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1296 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1300 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1302 static struct value
*
1303 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1305 enum range_type kind
;
1306 struct value
*low
= NULL
, *high
= NULL
;
1307 struct value
*addrval
, *result
;
1309 struct type
*range_type
;
1310 struct type
*index_type
;
1311 struct type
*temp_type
;
1314 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1317 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1318 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1319 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1320 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1322 if (noside
== EVAL_SKIP
)
1323 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1330 name
= "std::ops::RangeFull";
1334 index_type
= value_type (high
);
1335 name
= "std::ops::RangeTo";
1342 index_type
= value_type (low
);
1343 name
= "std::ops::RangeFrom";
1347 if (!types_equal (value_type (low
), value_type (high
)))
1348 error (_("Range expression with different types"));
1349 index_type
= value_type (low
);
1350 name
= "std::ops::Range";
1354 /* If we don't have an index type, just allocate this on the
1355 arch. Here any type will do. */
1356 temp_type
= (index_type
== NULL
1357 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1359 /* It would be nicer to cache the range type. */
1360 range_type
= rust_composite_type (temp_type
, name
,
1361 low
== NULL
? NULL
: "start", index_type
,
1362 high
== NULL
? NULL
: "end", index_type
);
1364 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1365 return value_zero (range_type
, lval_memory
);
1367 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1368 addr
= value_as_long (addrval
);
1369 result
= value_at_lazy (range_type
, addr
);
1373 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1376 value_assign (start
, low
);
1381 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1384 value_assign (end
, high
);
1387 result
= value_at_lazy (range_type
, addr
);
1391 /* A helper function to compute the range and kind given a range
1392 value. TYPE is the type of the range value. RANGE is the range
1393 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1394 parameters might be filled in, or might not be, depending on the
1395 kind of range this is. KIND will always be set to the appropriate
1396 value describing the kind of range, and this can be used to
1397 determine whether LOW or HIGH are valid. */
1400 rust_compute_range (struct type
*type
, struct value
*range
,
1401 LONGEST
*low
, LONGEST
*high
,
1402 enum range_type
*kind
)
1408 *kind
= BOTH_BOUND_DEFAULT
;
1410 if (TYPE_NFIELDS (type
) == 0)
1414 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1416 *kind
= HIGH_BOUND_DEFAULT
;
1417 *low
= value_as_long (value_field (range
, 0));
1420 if (TYPE_NFIELDS (type
) > i
1421 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1423 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1424 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1425 *high
= value_as_long (value_field (range
, i
));
1429 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1431 static struct value
*
1432 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1435 struct value
*lhs
, *rhs
, *result
;
1436 struct type
*rhstype
;
1437 LONGEST low
, high_bound
;
1438 /* Initialized to appease the compiler. */
1439 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1444 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1445 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1447 if (noside
== EVAL_SKIP
)
1450 rhstype
= check_typedef (value_type (rhs
));
1451 if (rust_range_type_p (rhstype
))
1454 error (_("Can't take slice of array without '&'"));
1455 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1459 low
= value_as_long (rhs
);
1461 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1463 struct type
*type
= check_typedef (value_type (lhs
));
1465 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1471 struct type
*type
= check_typedef (value_type (lhs
));
1473 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1476 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1477 error (_("Can't compute array bounds"));
1479 error (_("Found array with non-zero lower bound"));
1482 else if (rust_slice_type_p (type
))
1486 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1487 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1489 high_bound
= value_as_long (len
);
1491 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1495 high_bound
= LONGEST_MAX
;
1498 error (_("Cannot subscript non-array type"));
1501 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1504 error (_("Index less than zero"));
1505 if (low
> high_bound
)
1506 error (_("Index greater than length"));
1508 result
= value_subscript (base
, low
);
1515 struct type
*usize
, *slice
;
1517 struct value
*addrval
, *tem
;
1519 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1522 error (_("High index less than zero"));
1524 error (_("Low index greater than high index"));
1525 if (high
> high_bound
)
1526 error (_("High index greater than length"));
1528 usize
= language_lookup_primitive_type (exp
->language_defn
,
1531 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1534 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1535 addr
= value_as_long (addrval
);
1536 tem
= value_at_lazy (slice
, addr
);
1538 value_assign (value_field (tem
, 0), value_addr (result
));
1539 value_assign (value_field (tem
, 1),
1540 value_from_longest (usize
, high
- low
));
1542 result
= value_at_lazy (slice
, addr
);
1545 result
= value_addr (result
);
1551 /* evaluate_exp implementation for Rust. */
1553 static struct value
*
1554 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1555 int *pos
, enum noside noside
)
1557 struct value
*result
;
1559 switch (exp
->elts
[*pos
].opcode
)
1561 case UNOP_COMPLEMENT
:
1563 struct value
*value
;
1566 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1567 if (noside
== EVAL_SKIP
)
1569 /* Preserving the type is enough. */
1572 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1573 result
= value_from_longest (value_type (value
),
1574 value_logical_not (value
));
1576 result
= value_complement (value
);
1580 case BINOP_SUBSCRIPT
:
1581 result
= rust_subscript (exp
, pos
, noside
, 0);
1585 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1591 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1592 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1595 struct value
*addrval
= NULL
;
1599 if (noside
== EVAL_NORMAL
)
1601 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1602 addr
= value_as_long (addrval
);
1603 result
= value_at_lazy (type
, addr
);
1606 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1611 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1612 if (noside
== EVAL_NORMAL
)
1614 /* This isn't quite right but will do for the time
1615 being, seeing that we can't implement the Copy
1617 value_assign (result
, init
);
1623 gdb_assert (arglen
% 2 == 0);
1624 for (i
= 0; i
< arglen
; i
+= 2)
1627 const char *fieldname
;
1628 struct value
*value
, *field
;
1630 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1632 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1634 fieldname
= &exp
->elts
[*pos
].string
;
1635 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1637 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1638 if (noside
== EVAL_NORMAL
)
1640 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1642 value_assign (field
, value
);
1646 if (noside
== EVAL_SKIP
)
1647 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1649 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1650 result
= allocate_value (type
);
1652 result
= value_at_lazy (type
, addr
);
1661 struct value
*ncopies
;
1663 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1664 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1665 copies
= value_as_long (ncopies
);
1667 error (_("Array with negative number of elements"));
1669 if (noside
== EVAL_NORMAL
)
1673 std::vector
<struct value
*> eltvec (copies
);
1675 for (i
= 0; i
< copies
; ++i
)
1677 result
= value_array (0, copies
- 1, eltvec
.data ());
1681 struct type
*arraytype
1682 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1683 result
= allocate_value (arraytype
);
1688 case STRUCTOP_ANONYMOUS
:
1690 /* Anonymous field access, i.e. foo.1. */
1692 int pc
, field_number
, nfields
;
1693 struct type
*type
, *variant_type
;
1694 struct disr_info disr
;
1697 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1699 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1701 type
= value_type (lhs
);
1702 /* Untagged unions can't have anonymous field access since
1703 they can only have named fields. */
1704 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1705 && !rust_union_is_untagged (type
))
1707 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1708 value_embedded_offset (lhs
),
1709 value_address (lhs
), lhs
);
1711 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1713 variant_type
= NULL
;
1718 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1719 nfields
= TYPE_NFIELDS (variant_type
);
1722 if (!disr
.is_encoded
)
1725 if (field_number
>= nfields
|| field_number
< 0)
1726 error(_("Cannot access field %d of variant %s, \
1727 there are only %d fields"),
1728 disr
.is_encoded
? field_number
: field_number
- 1,
1730 disr
.is_encoded
? nfields
: nfields
- 1);
1732 if (!(disr
.is_encoded
1733 ? rust_tuple_struct_type_p (variant_type
)
1734 : rust_tuple_variant_type_p (variant_type
)))
1735 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1737 result
= value_primitive_field (lhs
, 0, field_number
,
1740 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1742 /* Tuples and tuple structs */
1743 nfields
= TYPE_NFIELDS(type
);
1745 if (field_number
>= nfields
|| field_number
< 0)
1746 error(_("Cannot access field %d of %s, there are only %d fields"),
1747 field_number
, TYPE_TAG_NAME (type
), nfields
);
1749 /* Tuples are tuple structs too. */
1750 if (!rust_tuple_struct_type_p (type
))
1751 error(_("Attempting to access anonymous field %d of %s, which is \
1752 not a tuple, tuple struct, or tuple-like variant"),
1753 field_number
, TYPE_TAG_NAME (type
));
1755 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1758 error(_("Anonymous field access is only allowed on tuples, \
1759 tuple structs, and tuple-like enum variants"));
1763 case STRUCTOP_STRUCT
:
1770 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1771 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1772 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
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
;
1783 field_name
= &exp
->elts
[pc
+ 2].string
;
1785 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1786 value_embedded_offset (lhs
),
1787 value_address (lhs
), lhs
);
1789 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1790 error(_("Could not find field %s of struct variant %s"),
1791 field_name
, disr
.name
.c_str ());
1793 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1795 if (variant_type
== NULL
1797 ? rust_tuple_struct_type_p (variant_type
)
1798 : rust_tuple_variant_type_p (variant_type
)))
1799 error(_("Attempting to access named field %s of tuple variant %s, \
1800 which has only anonymous fields"),
1801 field_name
, disr
.name
.c_str ());
1803 start
= disr
.is_encoded
? 0 : 1;
1804 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1806 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1808 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1813 if (i
== TYPE_NFIELDS (variant_type
))
1814 /* We didn't find it. */
1815 error(_("Could not find field %s of struct variant %s"),
1816 field_name
, disr
.name
.c_str ());
1820 /* Field access in structs and untagged unions works like C. */
1822 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1828 result
= rust_range (exp
, pos
, noside
);
1832 /* We might have &array[range], in which case we need to make a
1834 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1837 result
= rust_subscript (exp
, pos
, noside
, 1);
1842 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1849 /* operator_length implementation for Rust. */
1852 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1858 switch (exp
->elts
[pc
- 1].opcode
)
1861 /* We handle aggregate as a type and argument count. The first
1862 argument might be OP_OTHERS. After that the arguments
1863 alternate: first an OP_NAME, then an expression. */
1865 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1873 case STRUCTOP_ANONYMOUS
:
1884 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1892 /* op_name implementation for Rust. */
1895 rust_op_name (enum exp_opcode opcode
)
1900 return "OP_AGGREGATE";
1904 return op_name_standard (opcode
);
1908 /* dump_subexp_body implementation for Rust. */
1911 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1914 switch (exp
->elts
[elt
].opcode
)
1918 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1921 fprintf_filtered (stream
, "Type @");
1922 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1923 fprintf_filtered (stream
, " (");
1924 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1925 fprintf_filtered (stream
, "), length %d", length
);
1928 for (i
= 0; i
< length
; ++i
)
1929 elt
= dump_subexp (exp
, stream
, elt
);
1936 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1938 fprintf_filtered (stream
, "%s: %s",
1939 (exp
->elts
[elt
].opcode
== OP_STRING
1940 ? "string" : "name"),
1941 &exp
->elts
[elt
+ 2].string
);
1942 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1947 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1950 case STRUCTOP_ANONYMOUS
:
1954 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1956 fprintf_filtered (stream
, "Field number: %d", field_number
);
1957 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1965 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1972 /* print_subexp implementation for Rust. */
1975 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1976 enum precedence prec
)
1978 switch (exp
->elts
[*pos
].opcode
)
1982 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1985 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1986 fputs_filtered (" { ", stream
);
1989 for (i
= 0; i
< length
; ++i
)
1991 rust_print_subexp (exp
, pos
, stream
, prec
);
1992 fputs_filtered (", ", stream
);
1994 fputs_filtered (" }", stream
);
2000 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2002 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2003 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2009 fputs_filtered ("<<others>> (", stream
);
2011 rust_print_subexp (exp
, pos
, stream
, prec
);
2012 fputs_filtered (")", stream
);
2016 case STRUCTOP_ANONYMOUS
:
2018 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2021 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2022 fprintf_filtered (stream
, ".%d", tem
);
2028 fprintf_filtered (stream
, "[");
2029 rust_print_subexp (exp
, pos
, stream
, prec
);
2030 fprintf_filtered (stream
, "; ");
2031 rust_print_subexp (exp
, pos
, stream
, prec
);
2032 fprintf_filtered (stream
, "]");
2036 print_subexp_standard (exp
, pos
, stream
, prec
);
2041 /* operator_check implementation for Rust. */
2044 rust_operator_check (struct expression
*exp
, int pos
,
2045 int (*objfile_func
) (struct objfile
*objfile
,
2049 switch (exp
->elts
[pos
].opcode
)
2053 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2054 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2056 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2067 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2075 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2077 static struct block_symbol
2078 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2080 const struct block
*block
,
2081 const domain_enum domain
)
2083 struct block_symbol result
= {NULL
, NULL
};
2085 if (symbol_lookup_debug
)
2087 fprintf_unfiltered (gdb_stdlog
,
2088 "rust_lookup_symbol_non_local"
2089 " (%s, %s (scope %s), %s)\n",
2090 name
, host_address_to_string (block
),
2091 block_scope (block
), domain_name (domain
));
2094 /* Look up bare names in the block's scope. */
2095 if (name
[cp_find_first_component (name
)] == '\0')
2097 const char *scope
= block_scope (block
);
2099 if (scope
[0] != '\0')
2101 std::string scopedname
= std::string (scope
) + "::" + name
;
2103 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2105 if (result
.symbol
== NULL
)
2106 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2114 /* la_sniff_from_mangled_name for Rust. */
2117 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2119 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2120 return *demangled
!= NULL
;
2125 static const struct exp_descriptor exp_descriptor_rust
=
2128 rust_operator_length
,
2129 rust_operator_check
,
2131 rust_dump_subexp_body
,
2132 rust_evaluate_subexp
2135 static const char *rust_extensions
[] =
2140 static const struct language_defn rust_language_defn
=
2150 &exp_descriptor_rust
,
2154 rust_printchar
, /* Print a character constant */
2155 rust_printstr
, /* Function to print string constant */
2156 rust_emitchar
, /* Print a single char */
2157 rust_print_type
, /* Print a type using appropriate syntax */
2158 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2159 rust_val_print
, /* Print a value using appropriate syntax */
2160 c_value_print
, /* Print a top-level value */
2161 default_read_var_value
, /* la_read_var_value */
2162 NULL
, /* Language specific skip_trampoline */
2163 NULL
, /* name_of_this */
2164 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2165 basic_lookup_transparent_type
,/* lookup_transparent_type */
2166 gdb_demangle
, /* Language specific symbol demangler */
2167 rust_sniff_from_mangled_name
,
2168 NULL
, /* Language specific
2169 class_name_from_physname */
2170 c_op_print_tab
, /* expression operators for printing */
2171 1, /* c-style arrays */
2172 0, /* String lower bound */
2173 default_word_break_characters
,
2174 default_make_symbol_completion_list
,
2175 rust_language_arch_info
,
2176 default_print_array_index
,
2177 default_pass_by_reference
,
2179 NULL
, /* la_get_symbol_name_cmp */
2180 iterate_over_symbols
,
2181 &default_varobj_ops
,
2188 _initialize_rust_language (void)
2190 add_language (&rust_language_defn
);