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 struct cleanup
*cleanup
;
128 const char *name_segment
;
130 get_no_prettyformat_print_options (&opts
);
135 if (TYPE_NFIELDS (type
) == 0)
136 error (_("Encountered void enum value"));
138 /* If an enum has two values where one is empty and the other holds
139 a pointer that cannot be zero; then the Rust compiler optimizes
140 away the discriminant and instead uses a zero value in the
141 pointer field to indicate the empty variant. */
142 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
143 strlen (RUST_ENUM_PREFIX
)) == 0)
145 char *tail
, *token
, *saveptr
= NULL
;
146 unsigned long fieldno
;
147 struct type
*member_type
;
152 if (TYPE_NFIELDS (type
) != 1)
153 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
155 /* Optimized enums have only one field. */
156 member_type
= TYPE_FIELD_TYPE (type
, 0);
158 std::string
name (TYPE_FIELD_NAME (type
, 0));
159 tail
= &name
[0] + strlen (RUST_ENUM_PREFIX
);
161 /* The location of the value that doubles as a discriminant is
162 stored in the name of the field, as
163 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
164 where the fieldnos are the indices of the fields that should be
165 traversed in order to find the field (which may be several fields deep)
166 and the variantname is the name of the variant of the case when the
168 for (token
= strtok_r (tail
, "$", &saveptr
);
170 token
= strtok_r (NULL
, "$", &saveptr
))
172 if (sscanf (token
, "%lu", &fieldno
) != 1)
174 /* We have reached the enum name, which cannot start
178 if (fieldno
>= TYPE_NFIELDS (member_type
))
179 error (_("%s refers to field after end of member type"),
182 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
183 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
187 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
188 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
192 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
193 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + token
;
197 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
198 ret
.name
= (std::string (TYPE_NAME (type
)) + "::"
199 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))));
205 disr_type
= TYPE_FIELD_TYPE (type
, 0);
207 if (TYPE_NFIELDS (disr_type
) == 0)
209 /* This is a bounds check and should never be hit unless Rust
210 has changed its debuginfo format. */
211 error (_("Could not find enum discriminant field"));
213 else if (TYPE_NFIELDS (type
) == 1)
215 /* Sometimes univariant enums are encoded without a
216 discriminant. In that case, treating it as an encoded enum
217 with the first field being the actual type works. */
218 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
219 const char *last
= rust_last_path_segment (field_name
);
220 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + last
;
221 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
226 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
227 error (_("Rust debug format has changed"));
229 string_file temp_file
;
230 /* The first value of the first field (or any field)
231 is the discriminant value. */
232 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0),
233 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
234 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
238 ret
.name
= std::move (temp_file
.string ());
239 name_segment
= rust_last_path_segment (ret
.name
.c_str ());
240 if (name_segment
!= NULL
)
242 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
244 /* Sadly, the discriminant value paths do not match the type
245 field name paths ('core::option::Option::Some' vs
246 'core::option::Some'). However, enum variant names are
247 unique in the last path segment and the generics are not
248 part of this path, so we can just compare those. This is
249 hackish and would be better fixed by improving rustc's
250 metadata for enums. */
251 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
253 if (field_type
!= NULL
254 && strcmp (name_segment
,
255 rust_last_path_segment (field_type
)) == 0)
263 if (ret
.field_no
== -1 && !ret
.name
.empty ())
265 /* Somehow the discriminant wasn't found. */
266 error (_("Could not find variant of %s with discriminant %s"),
267 TYPE_TAG_NAME (type
), ret
.name
.c_str ());
273 /* See rust-lang.h. */
276 rust_tuple_type_p (struct type
*type
)
278 /* The current implementation is a bit of a hack, but there's
279 nothing else in the debuginfo to distinguish a tuple from a
281 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
282 && TYPE_TAG_NAME (type
) != NULL
283 && TYPE_TAG_NAME (type
)[0] == '(');
287 /* Return true if all non-static fields of a structlike type are in a
288 sequence like __0, __1, __2. OFFSET lets us skip fields. */
291 rust_underscore_fields (struct type
*type
, int offset
)
297 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
299 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
301 if (!field_is_static (&TYPE_FIELD (type
, i
)))
309 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
310 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
319 /* See rust-lang.h. */
322 rust_tuple_struct_type_p (struct type
*type
)
324 /* This is just an approximation until DWARF can represent Rust more
325 precisely. We exclude zero-length structs because they may not
326 be tuple structs, and there's no way to tell. */
327 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
330 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
333 rust_tuple_variant_type_p (struct type
*type
)
335 /* First field is discriminant */
336 return rust_underscore_fields (type
, 1);
339 /* Return true if TYPE is a slice type, otherwise false. */
342 rust_slice_type_p (struct type
*type
)
344 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
345 && TYPE_TAG_NAME (type
) != NULL
346 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
349 /* Return true if TYPE is a range type, otherwise false. */
352 rust_range_type_p (struct type
*type
)
356 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
357 || TYPE_NFIELDS (type
) > 2
358 || TYPE_TAG_NAME (type
) == NULL
359 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
362 if (TYPE_NFIELDS (type
) == 0)
366 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
368 if (TYPE_NFIELDS (type
) == 1)
372 else if (TYPE_NFIELDS (type
) == 2)
374 /* First field had to be "start". */
378 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
381 /* Return true if TYPE seems to be the type "u8", otherwise false. */
384 rust_u8_type_p (struct type
*type
)
386 return (TYPE_CODE (type
) == TYPE_CODE_INT
387 && TYPE_UNSIGNED (type
)
388 && TYPE_LENGTH (type
) == 1);
391 /* Return true if TYPE is a Rust character type. */
394 rust_chartype_p (struct type
*type
)
396 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
397 && TYPE_LENGTH (type
) == 4
398 && TYPE_UNSIGNED (type
));
403 /* la_emitchar implementation for Rust. */
406 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
408 if (!rust_chartype_p (type
))
409 generic_emit_char (c
, type
, stream
, quoter
,
410 target_charset (get_type_arch (type
)));
411 else if (c
== '\\' || c
== quoter
)
412 fprintf_filtered (stream
, "\\%c", c
);
414 fputs_filtered ("\\n", stream
);
416 fputs_filtered ("\\r", stream
);
418 fputs_filtered ("\\t", stream
);
420 fputs_filtered ("\\0", stream
);
421 else if (c
>= 32 && c
<= 127 && isprint (c
))
422 fputc_filtered (c
, stream
);
424 fprintf_filtered (stream
, "\\x%02x", c
);
426 fprintf_filtered (stream
, "\\u{%06x}", c
);
429 /* la_printchar implementation for Rust. */
432 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
434 fputs_filtered ("'", stream
);
435 LA_EMIT_CHAR (c
, type
, stream
, '\'');
436 fputs_filtered ("'", stream
);
439 /* la_printstr implementation for Rust. */
442 rust_printstr (struct ui_file
*stream
, struct type
*type
,
443 const gdb_byte
*string
, unsigned int length
,
444 const char *user_encoding
, int force_ellipses
,
445 const struct value_print_options
*options
)
447 /* Rust always uses UTF-8, but let the caller override this if need
449 const char *encoding
= user_encoding
;
450 if (user_encoding
== NULL
|| !*user_encoding
)
452 /* In Rust strings, characters are "u8". */
453 if (rust_u8_type_p (type
))
457 /* This is probably some C string, so let's let C deal with
459 c_printstr (stream
, type
, string
, length
, user_encoding
,
460 force_ellipses
, options
);
465 /* This is not ideal as it doesn't use our character printer. */
466 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
472 /* rust_print_type branch for structs and untagged unions. */
475 val_print_struct (struct type
*type
, int embedded_offset
,
476 CORE_ADDR address
, struct ui_file
*stream
,
477 int recurse
, struct value
*val
,
478 const struct value_print_options
*options
)
482 bool is_tuple
= rust_tuple_type_p (type
);
483 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
484 struct value_print_options opts
;
488 if (TYPE_TAG_NAME (type
) != NULL
)
489 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
491 if (TYPE_NFIELDS (type
) == 0)
494 if (TYPE_TAG_NAME (type
) != NULL
)
495 fputs_filtered (" ", stream
);
498 if (is_tuple
|| is_tuple_struct
)
499 fputs_filtered ("(", stream
);
501 fputs_filtered ("{", stream
);
507 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
509 if (field_is_static (&TYPE_FIELD (type
, i
)))
513 fputs_filtered (",", stream
);
515 if (options
->prettyformat
)
517 fputs_filtered ("\n", stream
);
518 print_spaces_filtered (2 + 2 * recurse
, stream
);
520 else if (!first_field
)
521 fputs_filtered (" ", stream
);
525 if (!is_tuple
&& !is_tuple_struct
)
527 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
528 fputs_filtered (": ", stream
);
531 val_print (TYPE_FIELD_TYPE (type
, i
),
532 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
534 stream
, recurse
+ 1, val
, &opts
,
538 if (options
->prettyformat
)
540 fputs_filtered ("\n", stream
);
541 print_spaces_filtered (2 * recurse
, stream
);
544 if (is_tuple
|| is_tuple_struct
)
545 fputs_filtered (")", stream
);
547 fputs_filtered ("}", stream
);
550 static const struct generic_val_print_decorations rust_decorations
=
552 /* Complex isn't used in Rust, but we provide C-ish values just in
564 /* la_val_print implementation for Rust. */
567 rust_val_print (struct type
*type
, int embedded_offset
,
568 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
570 const struct value_print_options
*options
)
572 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
574 type
= check_typedef (type
);
575 switch (TYPE_CODE (type
))
579 LONGEST low_bound
, high_bound
;
581 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
582 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
583 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
585 /* We have a pointer to a byte string, so just print
587 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
589 struct gdbarch
*arch
= get_type_arch (type
);
590 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
592 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
593 if (options
->addressprint
)
595 fputs_filtered (paddress (arch
, addr
), stream
);
596 fputs_filtered (" ", stream
);
599 fputs_filtered ("b", stream
);
600 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
601 high_bound
- low_bound
+ 1, stream
,
608 case TYPE_CODE_METHODPTR
:
609 case TYPE_CODE_MEMBERPTR
:
610 c_val_print (type
, embedded_offset
, address
, stream
,
611 recurse
, val
, options
);
615 /* Recognize the unit type. */
616 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
617 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
619 fputs_filtered ("()", stream
);
624 case TYPE_CODE_STRING
:
626 struct gdbarch
*arch
= get_type_arch (type
);
627 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
628 LONGEST low_bound
, high_bound
;
630 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
631 error (_("Could not determine the array bounds"));
633 /* If we see a plain TYPE_CODE_STRING, then we're printing a
634 byte string, hence the choice of "ASCII" as the
636 fputs_filtered ("b", stream
);
637 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
638 valaddr
+ embedded_offset
* unit_size
,
639 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
643 case TYPE_CODE_ARRAY
:
645 LONGEST low_bound
, high_bound
;
647 if (get_array_bounds (type
, &low_bound
, &high_bound
)
648 && high_bound
- low_bound
+ 1 == 0)
649 fputs_filtered ("[]", stream
);
655 case TYPE_CODE_UNION
:
657 int j
, nfields
, first_field
, is_tuple
, start
;
658 struct type
*variant_type
;
659 struct disr_info disr
;
660 struct value_print_options opts
;
662 /* Untagged unions are printed as if they are structs.
663 Since the field bit positions overlap in the debuginfo,
664 the code for printing a union is same as that for a struct,
665 the only difference is that the input type will have overlapping
667 if (rust_union_is_untagged (type
))
669 val_print_struct (type
, embedded_offset
, address
, stream
,
670 recurse
, val
, options
);
677 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
680 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
682 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
687 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
688 nfields
= TYPE_NFIELDS (variant_type
);
690 is_tuple
= (disr
.is_encoded
691 ? rust_tuple_struct_type_p (variant_type
)
692 : rust_tuple_variant_type_p (variant_type
));
693 start
= disr
.is_encoded
? 0 : 1;
697 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
699 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
702 /* struct variant. */
703 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
708 /* In case of a nullary variant like 'None', just output
710 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
714 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
717 fputs_filtered (", ", stream
);
721 fprintf_filtered (stream
, "%s: ",
722 TYPE_FIELD_NAME (variant_type
, j
));
724 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
726 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
727 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
729 stream
, recurse
+ 1, val
, &opts
,
734 fputs_filtered (")", stream
);
736 fputs_filtered ("}", stream
);
740 case TYPE_CODE_STRUCT
:
741 val_print_struct (type
, embedded_offset
, address
, stream
,
742 recurse
, val
, options
);
747 /* Nothing special yet. */
748 generic_val_print (type
, embedded_offset
, address
, stream
,
749 recurse
, val
, options
, &rust_decorations
);
756 rust_print_type (struct type
*type
, const char *varstring
,
757 struct ui_file
*stream
, int show
, int level
,
758 const struct type_print_options
*flags
);
760 /* Print a struct or union typedef. */
762 rust_print_struct_def (struct type
*type
, const char *varstring
,
763 struct ui_file
*stream
, int show
, int level
,
764 const struct type_print_options
*flags
)
766 bool is_tuple_struct
;
769 /* Print a tuple type simply. */
770 if (rust_tuple_type_p (type
))
772 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
776 /* If we see a base class, delegate to C. */
777 if (TYPE_N_BASECLASSES (type
) > 0)
778 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
780 /* This code path is also used by unions. */
781 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
782 fputs_filtered ("struct ", stream
);
784 fputs_filtered ("union ", stream
);
786 if (TYPE_TAG_NAME (type
) != NULL
)
787 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
789 is_tuple_struct
= rust_tuple_struct_type_p (type
);
791 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
793 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
795 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
800 if (field_is_static (&TYPE_FIELD (type
, i
)))
803 /* We'd like to print "pub" here as needed, but rustc
804 doesn't emit the debuginfo, and our types don't have
805 cplus_struct_type attached. */
807 /* For a tuple struct we print the type but nothing
809 print_spaces_filtered (level
+ 2, stream
);
810 if (!is_tuple_struct
)
811 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
813 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
814 stream
, show
- 1, level
+ 2,
816 fputs_filtered (",\n", stream
);
819 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
822 /* la_print_typedef implementation for Rust. */
825 rust_print_typedef (struct type
*type
,
826 struct symbol
*new_symbol
,
827 struct ui_file
*stream
)
829 type
= check_typedef (type
);
830 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
831 type_print (type
, "", stream
, 0);
832 fprintf_filtered (stream
, ";\n");
835 /* la_print_type implementation for Rust. */
838 rust_print_type (struct type
*type
, const char *varstring
,
839 struct ui_file
*stream
, int show
, int level
,
840 const struct type_print_options
*flags
)
846 && TYPE_NAME (type
) != NULL
)
848 /* Rust calls the unit type "void" in its debuginfo,
849 but we don't want to print it as that. */
850 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
851 fputs_filtered ("()", stream
);
853 fputs_filtered (TYPE_NAME (type
), stream
);
857 type
= check_typedef (type
);
858 switch (TYPE_CODE (type
))
861 fputs_filtered ("()", stream
);
865 /* Delegate varargs to the C printer. */
866 if (TYPE_VARARGS (type
))
869 fputs_filtered ("fn ", stream
);
870 if (varstring
!= NULL
)
871 fputs_filtered (varstring
, stream
);
872 fputs_filtered ("(", stream
);
873 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
877 fputs_filtered (", ", stream
);
878 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
881 fputs_filtered (")", stream
);
882 /* If it returns unit, we can omit the return type. */
883 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
885 fputs_filtered (" -> ", stream
);
886 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
890 case TYPE_CODE_ARRAY
:
892 LONGEST low_bound
, high_bound
;
894 fputs_filtered ("[", stream
);
895 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
896 stream
, show
- 1, level
, flags
);
897 fputs_filtered ("; ", stream
);
899 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
900 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
901 fprintf_filtered (stream
, "variable length");
902 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
903 fprintf_filtered (stream
, "%s",
904 plongest (high_bound
- low_bound
+ 1));
905 fputs_filtered ("]", stream
);
909 case TYPE_CODE_STRUCT
:
910 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
917 fputs_filtered ("enum ", stream
);
918 if (TYPE_TAG_NAME (type
) != NULL
)
920 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
921 fputs_filtered (" ", stream
);
922 len
= strlen (TYPE_TAG_NAME (type
));
924 fputs_filtered ("{\n", stream
);
926 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
928 const char *name
= TYPE_FIELD_NAME (type
, i
);
933 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
935 && name
[len
+ 1] == ':')
937 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
940 fputs_filtered ("}", stream
);
944 case TYPE_CODE_UNION
:
948 /* Skip the discriminant field. */
951 /* Unions and structs have the same syntax in Rust,
952 the only difference is that structs are declared with `struct`
953 and union with `union`. This difference is handled in the struct
955 if (rust_union_is_untagged (type
))
957 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
961 fputs_filtered ("enum ", stream
);
962 if (TYPE_TAG_NAME (type
) != NULL
)
964 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
965 fputs_filtered (" ", stream
);
967 fputs_filtered ("{\n", stream
);
969 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
970 strlen (RUST_ENUM_PREFIX
)) == 0)
972 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
973 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
975 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
976 /* There is no explicit discriminant field, skip nothing. */
980 else if (TYPE_NFIELDS (type
) == 1)
983 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
985 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
987 = rust_last_path_segment (TYPE_NAME (variant_type
));
989 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
991 if (TYPE_NFIELDS (variant_type
) > skip_to
)
994 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
995 ? rust_tuple_struct_type_p (variant_type
)
996 : rust_tuple_variant_type_p (variant_type
));
999 fputs_filtered (is_tuple
? "(" : "{", stream
);
1000 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1005 fputs_filtered (", ", stream
);
1008 fprintf_filtered (stream
, "%s: ",
1009 TYPE_FIELD_NAME (variant_type
, j
));
1011 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1012 stream
, show
- 1, level
+ 2,
1015 fputs_filtered (is_tuple
? ")" : "}", stream
);
1018 fputs_filtered (",\n", stream
);
1021 fputs_filtered ("}", stream
);
1027 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1033 /* Compute the alignment of the type T. */
1036 rust_type_alignment (struct type
*t
)
1038 t
= check_typedef (t
);
1039 switch (TYPE_CODE (t
))
1042 error (_("Could not compute alignment of type"));
1045 case TYPE_CODE_ENUM
:
1049 case TYPE_CODE_CHAR
:
1050 case TYPE_CODE_BOOL
:
1051 return TYPE_LENGTH (t
);
1053 case TYPE_CODE_ARRAY
:
1054 case TYPE_CODE_COMPLEX
:
1055 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1057 case TYPE_CODE_STRUCT
:
1058 case TYPE_CODE_UNION
:
1063 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1065 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1074 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1075 -- either on an obstack or on a gdbarch. */
1077 static struct type
*
1078 rust_composite_type (struct type
*original
,
1080 const char *field1
, struct type
*type1
,
1081 const char *field2
, struct type
*type2
)
1083 struct type
*result
= alloc_type_copy (original
);
1084 int i
, nfields
, bitpos
;
1092 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1093 TYPE_NAME (result
) = name
;
1094 TYPE_TAG_NAME (result
) = name
;
1096 TYPE_NFIELDS (result
) = nfields
;
1097 TYPE_FIELDS (result
)
1098 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1104 struct field
*field
= &TYPE_FIELD (result
, i
);
1106 SET_FIELD_BITPOS (*field
, bitpos
);
1107 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1109 FIELD_NAME (*field
) = field1
;
1110 FIELD_TYPE (*field
) = type1
;
1115 struct field
*field
= &TYPE_FIELD (result
, i
);
1116 int align
= rust_type_alignment (type2
);
1122 align
*= TARGET_CHAR_BIT
;
1123 delta
= bitpos
% align
;
1125 bitpos
+= align
- delta
;
1127 SET_FIELD_BITPOS (*field
, bitpos
);
1129 FIELD_NAME (*field
) = field2
;
1130 FIELD_TYPE (*field
) = type2
;
1135 TYPE_LENGTH (result
)
1136 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1137 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1141 /* See rust-lang.h. */
1144 rust_slice_type (const char *name
, struct type
*elt_type
,
1145 struct type
*usize_type
)
1149 elt_type
= lookup_pointer_type (elt_type
);
1150 type
= rust_composite_type (elt_type
, name
,
1151 "data_ptr", elt_type
,
1152 "length", usize_type
);
1157 enum rust_primitive_types
1159 rust_primitive_bool
,
1160 rust_primitive_char
,
1169 rust_primitive_isize
,
1170 rust_primitive_usize
,
1173 rust_primitive_unit
,
1175 nr_rust_primitive_types
1178 /* la_language_arch_info implementation for Rust. */
1181 rust_language_arch_info (struct gdbarch
*gdbarch
,
1182 struct language_arch_info
*lai
)
1184 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1186 struct type
**types
;
1187 unsigned int length
;
1189 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1192 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1193 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1194 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1195 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1196 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1197 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1198 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1199 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1200 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1201 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1203 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1204 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1205 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1207 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1208 floatformats_ieee_single
);
1209 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1210 floatformats_ieee_double
);
1212 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1214 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1215 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1216 types
[rust_primitive_usize
]);
1218 lai
->primitive_type_vector
= types
;
1219 lai
->bool_type_default
= types
[rust_primitive_bool
];
1220 lai
->string_char_type
= types
[rust_primitive_u8
];
1225 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1227 static struct value
*
1228 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1231 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1233 struct value
*function
, *result
, *arg0
;
1234 struct type
*type
, *fn_type
;
1235 const struct block
*block
;
1236 struct block_symbol sym
;
1238 /* For an ordinary function call we can simply defer to the
1239 generic implementation. */
1240 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1241 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1243 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1245 method
= &exp
->elts
[*pos
+ 1].string
;
1246 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1248 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1249 type in order to look up the method. */
1250 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1252 if (noside
== EVAL_SKIP
)
1254 for (i
= 0; i
< num_args
; ++i
)
1255 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1259 std::vector
<struct value
*> args (num_args
+ 1);
1262 /* We don't yet implement real Deref semantics. */
1263 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1264 args
[0] = value_ind (args
[0]);
1266 type
= value_type (args
[0]);
1267 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1268 && TYPE_CODE (type
) != TYPE_CODE_UNION
1269 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1270 || rust_tuple_type_p (type
))
1271 error (_("Method calls only supported on struct or enum types"));
1272 if (TYPE_TAG_NAME (type
) == NULL
)
1273 error (_("Method call on nameless type"));
1275 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1277 block
= get_selected_block (0);
1278 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1279 if (sym
.symbol
== NULL
)
1280 error (_("Could not find function named '%s'"), name
.c_str ());
1282 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1283 if (TYPE_NFIELDS (fn_type
) == 0)
1284 error (_("Function '%s' takes no arguments"), name
.c_str ());
1286 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1287 args
[0] = value_addr (args
[0]);
1289 function
= address_of_variable (sym
.symbol
, block
);
1291 for (i
= 0; i
< num_args
; ++i
)
1292 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1294 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1295 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1297 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1301 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1303 static struct value
*
1304 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1306 enum range_type kind
;
1307 struct value
*low
= NULL
, *high
= NULL
;
1308 struct value
*addrval
, *result
;
1310 struct type
*range_type
;
1311 struct type
*index_type
;
1312 struct type
*temp_type
;
1315 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1318 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1319 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1320 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1321 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1323 if (noside
== EVAL_SKIP
)
1324 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1331 name
= "std::ops::RangeFull";
1335 index_type
= value_type (high
);
1336 name
= "std::ops::RangeTo";
1343 index_type
= value_type (low
);
1344 name
= "std::ops::RangeFrom";
1348 if (!types_equal (value_type (low
), value_type (high
)))
1349 error (_("Range expression with different types"));
1350 index_type
= value_type (low
);
1351 name
= "std::ops::Range";
1355 /* If we don't have an index type, just allocate this on the
1356 arch. Here any type will do. */
1357 temp_type
= (index_type
== NULL
1358 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1360 /* It would be nicer to cache the range type. */
1361 range_type
= rust_composite_type (temp_type
, name
,
1362 low
== NULL
? NULL
: "start", index_type
,
1363 high
== NULL
? NULL
: "end", index_type
);
1365 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1366 return value_zero (range_type
, lval_memory
);
1368 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1369 addr
= value_as_long (addrval
);
1370 result
= value_at_lazy (range_type
, addr
);
1374 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1377 value_assign (start
, low
);
1382 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1385 value_assign (end
, high
);
1388 result
= value_at_lazy (range_type
, addr
);
1392 /* A helper function to compute the range and kind given a range
1393 value. TYPE is the type of the range value. RANGE is the range
1394 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1395 parameters might be filled in, or might not be, depending on the
1396 kind of range this is. KIND will always be set to the appropriate
1397 value describing the kind of range, and this can be used to
1398 determine whether LOW or HIGH are valid. */
1401 rust_compute_range (struct type
*type
, struct value
*range
,
1402 LONGEST
*low
, LONGEST
*high
,
1403 enum range_type
*kind
)
1409 *kind
= BOTH_BOUND_DEFAULT
;
1411 if (TYPE_NFIELDS (type
) == 0)
1415 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1417 *kind
= HIGH_BOUND_DEFAULT
;
1418 *low
= value_as_long (value_field (range
, 0));
1421 if (TYPE_NFIELDS (type
) > i
1422 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1424 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1425 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1426 *high
= value_as_long (value_field (range
, i
));
1430 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1432 static struct value
*
1433 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1436 struct value
*lhs
, *rhs
, *result
;
1437 struct type
*rhstype
;
1438 LONGEST low
, high_bound
;
1439 /* Initialized to appease the compiler. */
1440 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1445 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1446 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1448 if (noside
== EVAL_SKIP
)
1451 rhstype
= check_typedef (value_type (rhs
));
1452 if (rust_range_type_p (rhstype
))
1455 error (_("Can't take slice of array without '&'"));
1456 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1460 low
= value_as_long (rhs
);
1462 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1464 struct type
*type
= check_typedef (value_type (lhs
));
1466 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1472 struct type
*type
= check_typedef (value_type (lhs
));
1474 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1477 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1478 error (_("Can't compute array bounds"));
1480 error (_("Found array with non-zero lower bound"));
1483 else if (rust_slice_type_p (type
))
1487 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1488 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1490 high_bound
= value_as_long (len
);
1492 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1496 high_bound
= LONGEST_MAX
;
1499 error (_("Cannot subscript non-array type"));
1502 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1505 error (_("Index less than zero"));
1506 if (low
> high_bound
)
1507 error (_("Index greater than length"));
1509 result
= value_subscript (base
, low
);
1516 struct type
*usize
, *slice
;
1518 struct value
*addrval
, *tem
;
1520 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1523 error (_("High index less than zero"));
1525 error (_("Low index greater than high index"));
1526 if (high
> high_bound
)
1527 error (_("High index greater than length"));
1529 usize
= language_lookup_primitive_type (exp
->language_defn
,
1532 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1535 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1536 addr
= value_as_long (addrval
);
1537 tem
= value_at_lazy (slice
, addr
);
1539 value_assign (value_field (tem
, 0), value_addr (result
));
1540 value_assign (value_field (tem
, 1),
1541 value_from_longest (usize
, high
- low
));
1543 result
= value_at_lazy (slice
, addr
);
1546 result
= value_addr (result
);
1552 /* evaluate_exp implementation for Rust. */
1554 static struct value
*
1555 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1556 int *pos
, enum noside noside
)
1558 struct value
*result
;
1560 switch (exp
->elts
[*pos
].opcode
)
1562 case UNOP_COMPLEMENT
:
1564 struct value
*value
;
1567 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1568 if (noside
== EVAL_SKIP
)
1570 /* Preserving the type is enough. */
1573 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1574 result
= value_from_longest (value_type (value
),
1575 value_logical_not (value
));
1577 result
= value_complement (value
);
1581 case BINOP_SUBSCRIPT
:
1582 result
= rust_subscript (exp
, pos
, noside
, 0);
1586 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1592 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1593 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1596 struct value
*addrval
= NULL
;
1600 if (noside
== EVAL_NORMAL
)
1602 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1603 addr
= value_as_long (addrval
);
1604 result
= value_at_lazy (type
, addr
);
1607 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1612 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1613 if (noside
== EVAL_NORMAL
)
1615 /* This isn't quite right but will do for the time
1616 being, seeing that we can't implement the Copy
1618 value_assign (result
, init
);
1624 gdb_assert (arglen
% 2 == 0);
1625 for (i
= 0; i
< arglen
; i
+= 2)
1628 const char *fieldname
;
1629 struct value
*value
, *field
;
1631 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1633 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1635 fieldname
= &exp
->elts
[*pos
].string
;
1636 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1638 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1639 if (noside
== EVAL_NORMAL
)
1641 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1643 value_assign (field
, value
);
1647 if (noside
== EVAL_SKIP
)
1648 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1650 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1651 result
= allocate_value (type
);
1653 result
= value_at_lazy (type
, addr
);
1662 struct value
*ncopies
;
1664 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1665 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1666 copies
= value_as_long (ncopies
);
1668 error (_("Array with negative number of elements"));
1670 if (noside
== EVAL_NORMAL
)
1674 std::vector
<struct value
*> eltvec (copies
);
1676 for (i
= 0; i
< copies
; ++i
)
1678 result
= value_array (0, copies
- 1, eltvec
.data ());
1682 struct type
*arraytype
1683 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1684 result
= allocate_value (arraytype
);
1689 case STRUCTOP_ANONYMOUS
:
1691 /* Anonymous field access, i.e. foo.1. */
1693 int pc
, field_number
, nfields
;
1694 struct type
*type
, *variant_type
;
1695 struct disr_info disr
;
1698 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1700 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1702 type
= value_type (lhs
);
1703 /* Untagged unions can't have anonymous field access since
1704 they can only have named fields. */
1705 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1706 && !rust_union_is_untagged (type
))
1708 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1709 value_embedded_offset (lhs
),
1710 value_address (lhs
), lhs
);
1712 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1714 variant_type
= NULL
;
1719 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1720 nfields
= TYPE_NFIELDS (variant_type
);
1723 if (!disr
.is_encoded
)
1726 if (field_number
>= nfields
|| field_number
< 0)
1727 error(_("Cannot access field %d of variant %s, \
1728 there are only %d fields"),
1729 disr
.is_encoded
? field_number
: field_number
- 1,
1731 disr
.is_encoded
? nfields
: nfields
- 1);
1733 if (!(disr
.is_encoded
1734 ? rust_tuple_struct_type_p (variant_type
)
1735 : rust_tuple_variant_type_p (variant_type
)))
1736 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1738 result
= value_primitive_field (lhs
, 0, field_number
,
1741 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1743 /* Tuples and tuple structs */
1744 nfields
= TYPE_NFIELDS(type
);
1746 if (field_number
>= nfields
|| field_number
< 0)
1747 error(_("Cannot access field %d of %s, there are only %d fields"),
1748 field_number
, TYPE_TAG_NAME (type
), nfields
);
1750 /* Tuples are tuple structs too. */
1751 if (!rust_tuple_struct_type_p (type
))
1752 error(_("Attempting to access anonymous field %d of %s, which is \
1753 not a tuple, tuple struct, or tuple-like variant"),
1754 field_number
, TYPE_TAG_NAME (type
));
1756 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1759 error(_("Anonymous field access is only allowed on tuples, \
1760 tuple structs, and tuple-like enum variants"));
1764 case STRUCTOP_STRUCT
:
1771 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1772 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1773 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1775 type
= value_type (lhs
);
1776 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1777 && !rust_union_is_untagged (type
))
1780 struct disr_info disr
;
1781 struct type
* variant_type
;
1784 field_name
= &exp
->elts
[pc
+ 2].string
;
1786 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1787 value_embedded_offset (lhs
),
1788 value_address (lhs
), lhs
);
1790 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1791 error(_("Could not find field %s of struct variant %s"),
1792 field_name
, disr
.name
.c_str ());
1794 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1796 if (variant_type
== NULL
1798 ? rust_tuple_struct_type_p (variant_type
)
1799 : rust_tuple_variant_type_p (variant_type
)))
1800 error(_("Attempting to access named field %s of tuple variant %s, \
1801 which has only anonymous fields"),
1802 field_name
, disr
.name
.c_str ());
1804 start
= disr
.is_encoded
? 0 : 1;
1805 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1807 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1809 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1814 if (i
== TYPE_NFIELDS (variant_type
))
1815 /* We didn't find it. */
1816 error(_("Could not find field %s of struct variant %s"),
1817 field_name
, disr
.name
.c_str ());
1821 /* Field access in structs and untagged unions works like C. */
1823 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1829 result
= rust_range (exp
, pos
, noside
);
1833 /* We might have &array[range], in which case we need to make a
1835 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1838 result
= rust_subscript (exp
, pos
, noside
, 1);
1843 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1850 /* operator_length implementation for Rust. */
1853 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1859 switch (exp
->elts
[pc
- 1].opcode
)
1862 /* We handle aggregate as a type and argument count. The first
1863 argument might be OP_OTHERS. After that the arguments
1864 alternate: first an OP_NAME, then an expression. */
1866 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1874 case STRUCTOP_ANONYMOUS
:
1885 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1893 /* op_name implementation for Rust. */
1896 rust_op_name (enum exp_opcode opcode
)
1901 return "OP_AGGREGATE";
1905 return op_name_standard (opcode
);
1909 /* dump_subexp_body implementation for Rust. */
1912 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1915 switch (exp
->elts
[elt
].opcode
)
1919 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1922 fprintf_filtered (stream
, "Type @");
1923 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1924 fprintf_filtered (stream
, " (");
1925 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1926 fprintf_filtered (stream
, "), length %d", length
);
1929 for (i
= 0; i
< length
; ++i
)
1930 elt
= dump_subexp (exp
, stream
, elt
);
1937 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1939 fprintf_filtered (stream
, "%s: %s",
1940 (exp
->elts
[elt
].opcode
== OP_STRING
1941 ? "string" : "name"),
1942 &exp
->elts
[elt
+ 2].string
);
1943 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1948 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1951 case STRUCTOP_ANONYMOUS
:
1955 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1957 fprintf_filtered (stream
, "Field number: %d", field_number
);
1958 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1966 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1973 /* print_subexp implementation for Rust. */
1976 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1977 enum precedence prec
)
1979 switch (exp
->elts
[*pos
].opcode
)
1983 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1986 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1987 fputs_filtered (" { ", stream
);
1990 for (i
= 0; i
< length
; ++i
)
1992 rust_print_subexp (exp
, pos
, stream
, prec
);
1993 fputs_filtered (", ", stream
);
1995 fputs_filtered (" }", stream
);
2001 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2003 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2004 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2010 fputs_filtered ("<<others>> (", stream
);
2012 rust_print_subexp (exp
, pos
, stream
, prec
);
2013 fputs_filtered (")", stream
);
2017 case STRUCTOP_ANONYMOUS
:
2019 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2022 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2023 fprintf_filtered (stream
, ".%d", tem
);
2029 fprintf_filtered (stream
, "[");
2030 rust_print_subexp (exp
, pos
, stream
, prec
);
2031 fprintf_filtered (stream
, "; ");
2032 rust_print_subexp (exp
, pos
, stream
, prec
);
2033 fprintf_filtered (stream
, "]");
2037 print_subexp_standard (exp
, pos
, stream
, prec
);
2042 /* operator_check implementation for Rust. */
2045 rust_operator_check (struct expression
*exp
, int pos
,
2046 int (*objfile_func
) (struct objfile
*objfile
,
2050 switch (exp
->elts
[pos
].opcode
)
2054 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2055 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2057 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2068 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2076 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2078 static struct block_symbol
2079 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2081 const struct block
*block
,
2082 const domain_enum domain
)
2084 struct block_symbol result
= {NULL
, NULL
};
2086 if (symbol_lookup_debug
)
2088 fprintf_unfiltered (gdb_stdlog
,
2089 "rust_lookup_symbol_non_local"
2090 " (%s, %s (scope %s), %s)\n",
2091 name
, host_address_to_string (block
),
2092 block_scope (block
), domain_name (domain
));
2095 /* Look up bare names in the block's scope. */
2096 if (name
[cp_find_first_component (name
)] == '\0')
2098 const char *scope
= block_scope (block
);
2100 if (scope
[0] != '\0')
2102 std::string scopedname
= std::string (scope
) + "::" + name
;
2104 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2106 if (result
.symbol
== NULL
)
2107 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2115 /* la_sniff_from_mangled_name for Rust. */
2118 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2120 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2121 return *demangled
!= NULL
;
2126 static const struct exp_descriptor exp_descriptor_rust
=
2129 rust_operator_length
,
2130 rust_operator_check
,
2132 rust_dump_subexp_body
,
2133 rust_evaluate_subexp
2136 static const char *rust_extensions
[] =
2141 static const struct language_defn rust_language_defn
=
2151 &exp_descriptor_rust
,
2155 rust_printchar
, /* Print a character constant */
2156 rust_printstr
, /* Function to print string constant */
2157 rust_emitchar
, /* Print a single char */
2158 rust_print_type
, /* Print a type using appropriate syntax */
2159 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2160 rust_val_print
, /* Print a value using appropriate syntax */
2161 c_value_print
, /* Print a top-level value */
2162 default_read_var_value
, /* la_read_var_value */
2163 NULL
, /* Language specific skip_trampoline */
2164 NULL
, /* name_of_this */
2165 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2166 basic_lookup_transparent_type
,/* lookup_transparent_type */
2167 gdb_demangle
, /* Language specific symbol demangler */
2168 rust_sniff_from_mangled_name
,
2169 NULL
, /* Language specific
2170 class_name_from_physname */
2171 c_op_print_tab
, /* expression operators for printing */
2172 1, /* c-style arrays */
2173 0, /* String lower bound */
2174 default_word_break_characters
,
2175 default_make_symbol_completion_list
,
2176 rust_language_arch_info
,
2177 default_print_array_index
,
2178 default_pass_by_reference
,
2180 NULL
, /* la_get_symbol_name_cmp */
2181 iterate_over_symbols
,
2182 &default_varobj_ops
,
2189 _initialize_rust_language (void)
2191 add_language (&rust_language_defn
);