1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016 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 /* Find the Rust crate for BLOCK. If no crate can be found, returns
55 NULL. Otherwise, returns a newly allocated string that the caller
56 is responsible for freeing. */
59 rust_crate_for_block (const struct block
*block
)
61 const char *scope
= block_scope (block
);
66 return xstrndup (scope
, cp_find_first_component (scope
));
69 /* Information about the discriminant/variant of an enum */
73 /* Name of field. Must be freed by caller. */
75 /* Field number in union. Negative on error. For an encoded enum,
76 the "hidden" member will always be field 1, and the "real" member
77 will always be field 0. */
79 /* True if this is an encoded enum that has a single "real" member
80 and a single "hidden" member. */
81 unsigned int is_encoded
: 1;
84 /* The prefix of a specially-encoded enum. */
86 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
88 /* The number of the real field. */
90 #define RUST_ENCODED_ENUM_REAL 0
92 /* The number of the hidden field. */
94 #define RUST_ENCODED_ENUM_HIDDEN 1
96 /* Utility function to get discriminant info for a given value. */
98 static struct disr_info
99 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
100 int embedded_offset
, CORE_ADDR address
,
101 const struct value
*val
)
104 struct disr_info ret
;
105 struct type
*disr_type
;
106 struct ui_file
*temp_file
;
107 struct value_print_options opts
;
108 struct cleanup
*cleanup
;
109 const char *name_segment
;
111 get_no_prettyformat_print_options (&opts
);
116 if (TYPE_NFIELDS (type
) == 0)
117 error (_("Encountered void enum value"));
119 /* If an enum has two values where one is empty and the other holds
120 a pointer that cannot be zero; then the Rust compiler optimizes
121 away the discriminant and instead uses a zero value in the
122 pointer field to indicate the empty variant. */
123 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
124 strlen (RUST_ENUM_PREFIX
)) == 0)
126 char *tail
, *token
, *name
, *saveptr
= NULL
;
127 unsigned long fieldno
;
128 struct type
*member_type
;
133 if (TYPE_NFIELDS (type
) != 1)
134 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
136 /* Optimized enums have only one field. */
137 member_type
= TYPE_FIELD_TYPE (type
, 0);
139 name
= xstrdup (TYPE_FIELD_NAME (type
, 0));
140 cleanup
= make_cleanup (xfree
, name
);
141 tail
= name
+ strlen (RUST_ENUM_PREFIX
);
143 /* The location of the value that doubles as a discriminant is
144 stored in the name of the field, as
145 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
146 where the fieldnos are the indices of the fields that should be
147 traversed in order to find the field (which may be several fields deep)
148 and the variantname is the name of the variant of the case when the
150 for (token
= strtok_r (tail
, "$", &saveptr
);
152 token
= strtok_r (NULL
, "$", &saveptr
))
154 if (sscanf (token
, "%lu", &fieldno
) != 1)
156 /* We have reached the enum name, which cannot start
160 if (fieldno
>= TYPE_NFIELDS (member_type
))
161 error (_("%s refers to field after end of member type"),
164 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
165 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
169 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
170 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
174 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
175 ret
.name
= concat (TYPE_NAME (type
), "::", token
, (char *) NULL
);
179 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
180 ret
.name
= concat (TYPE_NAME (type
), "::",
181 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))),
185 do_cleanups (cleanup
);
189 disr_type
= TYPE_FIELD_TYPE (type
, 0);
191 if (TYPE_NFIELDS (disr_type
) == 0)
193 /* This is a bounds check and should never be hit unless Rust
194 has changed its debuginfo format. */
195 error (_("Could not find enum discriminant field"));
197 else if (TYPE_NFIELDS (type
) == 1)
199 /* Sometimes univariant enums are encoded without a
200 discriminant. In that case, treating it as an encoded enum
201 with the first field being the actual type works. */
202 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
203 const char *last
= rust_last_path_segment (field_name
);
204 ret
.name
= concat (TYPE_NAME (type
), "::", last
, (char *) NULL
);
205 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
210 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
211 error (_("Rust debug format has changed"));
213 temp_file
= mem_fileopen ();
214 cleanup
= make_cleanup_ui_file_delete (temp_file
);
215 /* The first value of the first field (or any field)
216 is the discriminant value. */
217 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
218 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
219 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
223 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
224 name_segment
= rust_last_path_segment (ret
.name
);
225 if (name_segment
!= NULL
)
227 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
229 /* Sadly, the discriminant value paths do not match the type
230 field name paths ('core::option::Option::Some' vs
231 'core::option::Some'). However, enum variant names are
232 unique in the last path segment and the generics are not
233 part of this path, so we can just compare those. This is
234 hackish and would be better fixed by improving rustc's
235 metadata for enums. */
236 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
238 if (field_type
!= NULL
239 && strcmp (name_segment
,
240 rust_last_path_segment (field_type
)) == 0)
248 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
250 /* Somehow the discriminant wasn't found. */
251 make_cleanup (xfree
, ret
.name
);
252 error (_("Could not find variant of %s with discriminant %s"),
253 TYPE_TAG_NAME (type
), ret
.name
);
256 do_cleanups (cleanup
);
260 /* See rust-lang.h. */
263 rust_tuple_type_p (struct type
*type
)
265 /* The current implementation is a bit of a hack, but there's
266 nothing else in the debuginfo to distinguish a tuple from a
268 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
269 && TYPE_TAG_NAME (type
) != NULL
270 && TYPE_TAG_NAME (type
)[0] == '(');
274 /* Return true if all non-static fields of a structlike type are in a
275 sequence like __0, __1, __2. OFFSET lets us skip fields. */
278 rust_underscore_fields (struct type
*type
, int offset
)
284 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
286 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
288 if (!field_is_static (&TYPE_FIELD (type
, i
)))
296 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
297 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
306 /* See rust-lang.h. */
309 rust_tuple_struct_type_p (struct type
*type
)
311 /* This is just an approximation until DWARF can represent Rust more
312 precisely. We exclude zero-length structs because they may not
313 be tuple structs, and there's no way to tell. */
314 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
317 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
320 rust_tuple_variant_type_p (struct type
*type
)
322 /* First field is discriminant */
323 return rust_underscore_fields (type
, 1);
326 /* Return true if TYPE is a slice type, otherwise false. */
329 rust_slice_type_p (struct type
*type
)
331 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
332 && TYPE_TAG_NAME (type
) != NULL
333 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
336 /* Return true if TYPE is a range type, otherwise false. */
339 rust_range_type_p (struct type
*type
)
343 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
344 || TYPE_NFIELDS (type
) > 2
345 || TYPE_TAG_NAME (type
) == NULL
346 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
349 if (TYPE_NFIELDS (type
) == 0)
353 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
355 if (TYPE_NFIELDS (type
) == 1)
359 else if (TYPE_NFIELDS (type
) == 2)
361 /* First field had to be "start". */
365 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
368 /* Return true if TYPE seems to be the type "u8", otherwise false. */
371 rust_u8_type_p (struct type
*type
)
373 return (TYPE_CODE (type
) == TYPE_CODE_INT
374 && TYPE_UNSIGNED (type
)
375 && TYPE_LENGTH (type
) == 1);
378 /* Return true if TYPE is a Rust character type. */
381 rust_chartype_p (struct type
*type
)
383 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
384 && TYPE_LENGTH (type
) == 4
385 && TYPE_UNSIGNED (type
));
390 /* la_emitchar implementation for Rust. */
393 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
395 if (!rust_chartype_p (type
))
396 generic_emit_char (c
, type
, stream
, quoter
,
397 target_charset (get_type_arch (type
)));
398 else if (c
== '\\' || c
== quoter
)
399 fprintf_filtered (stream
, "\\%c", c
);
401 fputs_filtered ("\\n", stream
);
403 fputs_filtered ("\\r", stream
);
405 fputs_filtered ("\\t", stream
);
407 fputs_filtered ("\\0", stream
);
408 else if (c
>= 32 && c
<= 127 && isprint (c
))
409 fputc_filtered (c
, stream
);
411 fprintf_filtered (stream
, "\\x%02x", c
);
413 fprintf_filtered (stream
, "\\u{%06x}", c
);
416 /* la_printchar implementation for Rust. */
419 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
421 fputs_filtered ("'", stream
);
422 LA_EMIT_CHAR (c
, type
, stream
, '\'');
423 fputs_filtered ("'", stream
);
426 /* la_printstr implementation for Rust. */
429 rust_printstr (struct ui_file
*stream
, struct type
*type
,
430 const gdb_byte
*string
, unsigned int length
,
431 const char *user_encoding
, int force_ellipses
,
432 const struct value_print_options
*options
)
434 /* Rust always uses UTF-8, but let the caller override this if need
436 const char *encoding
= user_encoding
;
437 if (user_encoding
== NULL
|| !*user_encoding
)
439 /* In Rust strings, characters are "u8". */
440 if (rust_u8_type_p (type
))
444 /* This is probably some C string, so let's let C deal with
446 c_printstr (stream
, type
, string
, length
, user_encoding
,
447 force_ellipses
, options
);
452 /* This is not ideal as it doesn't use our character printer. */
453 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
459 static const struct generic_val_print_decorations rust_decorations
=
461 /* Complex isn't used in Rust, but we provide C-ish values just in
473 /* la_val_print implementation for Rust. */
476 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
477 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
478 const struct value
*val
,
479 const struct value_print_options
*options
)
481 type
= check_typedef (type
);
482 switch (TYPE_CODE (type
))
486 LONGEST low_bound
, high_bound
;
488 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
489 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
490 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
492 /* We have a pointer to a byte string, so just print
494 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
496 struct gdbarch
*arch
= get_type_arch (type
);
497 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
499 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
500 if (options
->addressprint
)
502 fputs_filtered (paddress (arch
, addr
), stream
);
503 fputs_filtered (" ", stream
);
506 fputs_filtered ("b", stream
);
507 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
508 high_bound
- low_bound
+ 1, stream
,
515 case TYPE_CODE_METHODPTR
:
516 case TYPE_CODE_MEMBERPTR
:
517 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
518 recurse
, val
, options
);
522 /* Recognize the unit type. */
523 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
524 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
526 fputs_filtered ("()", stream
);
531 case TYPE_CODE_STRING
:
533 struct gdbarch
*arch
= get_type_arch (type
);
534 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
535 LONGEST low_bound
, high_bound
;
537 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
538 error (_("Could not determine the array bounds"));
540 /* If we see a plain TYPE_CODE_STRING, then we're printing a
541 byte string, hence the choice of "ASCII" as the
543 fputs_filtered ("b", stream
);
544 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
545 valaddr
+ embedded_offset
* unit_size
,
546 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
550 case TYPE_CODE_ARRAY
:
552 LONGEST low_bound
, high_bound
;
554 if (get_array_bounds (type
, &low_bound
, &high_bound
)
555 && high_bound
- low_bound
+ 1 == 0)
556 fputs_filtered ("[]", stream
);
562 case TYPE_CODE_UNION
:
564 int j
, nfields
, first_field
, is_tuple
, start
;
565 struct type
*variant_type
;
566 struct disr_info disr
;
567 struct value_print_options opts
;
568 struct cleanup
*cleanup
;
573 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
575 cleanup
= make_cleanup (xfree
, disr
.name
);
577 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
579 fprintf_filtered (stream
, "%s", disr
.name
);
584 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
585 nfields
= TYPE_NFIELDS (variant_type
);
587 is_tuple
= (disr
.is_encoded
588 ? rust_tuple_struct_type_p (variant_type
)
589 : rust_tuple_variant_type_p (variant_type
));
590 start
= disr
.is_encoded
? 0 : 1;
594 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
596 fprintf_filtered (stream
, "%s(", disr
.name
);
599 /* struct variant. */
600 fprintf_filtered (stream
, "%s{", disr
.name
);
605 /* In case of a nullary variant like 'None', just output
607 fprintf_filtered (stream
, "%s", disr
.name
);
611 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
614 fputs_filtered (", ", stream
);
618 fprintf_filtered (stream
, "%s: ",
619 TYPE_FIELD_NAME (variant_type
, j
));
621 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
624 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
625 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
627 stream
, recurse
+ 1, val
, &opts
,
632 fputs_filtered (")", stream
);
634 fputs_filtered ("}", stream
);
637 do_cleanups (cleanup
);
641 case TYPE_CODE_STRUCT
:
645 int is_tuple
= rust_tuple_type_p (type
);
646 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
647 struct value_print_options opts
;
651 if (TYPE_TAG_NAME (type
) != NULL
)
652 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
654 if (TYPE_NFIELDS (type
) == 0)
657 if (TYPE_TAG_NAME (type
) != NULL
)
658 fputs_filtered (" ", stream
);
661 if (is_tuple
|| is_tuple_struct
)
662 fputs_filtered ("(", stream
);
664 fputs_filtered ("{", stream
);
670 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
672 if (field_is_static (&TYPE_FIELD (type
, i
)))
676 fputs_filtered (",", stream
);
678 if (options
->prettyformat
)
680 fputs_filtered ("\n", stream
);
681 print_spaces_filtered (2 + 2 * recurse
, stream
);
683 else if (!first_field
)
684 fputs_filtered (" ", stream
);
688 if (!is_tuple
&& !is_tuple_struct
)
690 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
691 fputs_filtered (": ", stream
);
694 val_print (TYPE_FIELD_TYPE (type
, i
),
696 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
698 stream
, recurse
+ 1, val
, &opts
,
702 if (options
->prettyformat
)
704 fputs_filtered ("\n", stream
);
705 print_spaces_filtered (2 * recurse
, stream
);
708 if (is_tuple
|| is_tuple_struct
)
709 fputs_filtered (")", stream
);
711 fputs_filtered ("}", stream
);
717 /* Nothing special yet. */
718 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
719 recurse
, val
, options
, &rust_decorations
);
725 /* la_print_typedef implementation for Rust. */
728 rust_print_typedef (struct type
*type
,
729 struct symbol
*new_symbol
,
730 struct ui_file
*stream
)
732 type
= check_typedef (type
);
733 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
734 type_print (type
, "", stream
, 0);
735 fprintf_filtered (stream
, ";\n");
738 /* la_print_type implementation for Rust. */
741 rust_print_type (struct type
*type
, const char *varstring
,
742 struct ui_file
*stream
, int show
, int level
,
743 const struct type_print_options
*flags
)
749 && TYPE_NAME (type
) != NULL
)
751 /* Rust calls the unit type "void" in its debuginfo,
752 but we don't want to print it as that. */
753 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
754 fputs_filtered ("()", stream
);
756 fputs_filtered (TYPE_NAME (type
), stream
);
760 type
= check_typedef (type
);
761 switch (TYPE_CODE (type
))
764 fputs_filtered ("()", stream
);
768 /* Delegate varargs to the C printer. */
769 if (TYPE_VARARGS (type
))
772 fputs_filtered ("fn ", stream
);
773 if (varstring
!= NULL
)
774 fputs_filtered (varstring
, stream
);
775 fputs_filtered ("(", stream
);
776 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
780 fputs_filtered (", ", stream
);
781 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
784 fputs_filtered (")", stream
);
785 /* If it returns unit, we can omit the return type. */
786 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
788 fputs_filtered (" -> ", stream
);
789 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
793 case TYPE_CODE_ARRAY
:
795 LONGEST low_bound
, high_bound
;
797 fputs_filtered ("[", stream
);
798 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
799 stream
, show
- 1, level
, flags
);
800 fputs_filtered ("; ", stream
);
802 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
803 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
804 fprintf_filtered (stream
, "variable length");
805 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
806 fprintf_filtered (stream
, "%s",
807 plongest (high_bound
- low_bound
+ 1));
808 fputs_filtered ("]", stream
);
812 case TYPE_CODE_STRUCT
:
816 /* Print a tuple type simply. */
817 if (rust_tuple_type_p (type
))
819 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
823 /* If we see a base class, delegate to C. */
824 if (TYPE_N_BASECLASSES (type
) > 0)
827 fputs_filtered ("struct ", stream
);
828 if (TYPE_TAG_NAME (type
) != NULL
)
829 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
831 is_tuple_struct
= rust_tuple_struct_type_p (type
);
833 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
835 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
837 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
842 if (field_is_static (&TYPE_FIELD (type
, i
)))
845 /* We'd like to print "pub" here as needed, but rustc
846 doesn't emit the debuginfo, and our types don't have
847 cplus_struct_type attached. */
849 /* For a tuple struct we print the type but nothing
851 print_spaces_filtered (level
+ 2, stream
);
852 if (!is_tuple_struct
)
853 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
855 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
856 stream
, show
- 1, level
+ 2,
858 fputs_filtered (",\n", stream
);
861 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
869 fputs_filtered ("enum ", stream
);
870 if (TYPE_TAG_NAME (type
) != NULL
)
872 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
873 fputs_filtered (" ", stream
);
874 len
= strlen (TYPE_TAG_NAME (type
));
876 fputs_filtered ("{\n", stream
);
878 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
880 const char *name
= TYPE_FIELD_NAME (type
, i
);
885 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
887 && name
[len
+ 1] == ':')
889 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
892 fputs_filtered ("}", stream
);
896 case TYPE_CODE_UNION
:
900 /* Skip the discriminant field. */
903 fputs_filtered ("enum ", stream
);
904 if (TYPE_TAG_NAME (type
) != NULL
)
906 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
907 fputs_filtered (" ", stream
);
909 fputs_filtered ("{\n", stream
);
911 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
912 strlen (RUST_ENUM_PREFIX
)) == 0)
914 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
915 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
917 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
918 /* There is no explicit discriminant field, skip nothing. */
923 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
925 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
927 = rust_last_path_segment (TYPE_NAME (variant_type
));
929 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
931 if (TYPE_NFIELDS (variant_type
) > skip_to
)
934 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
937 fputs_filtered (is_tuple
? "(" : "{", stream
);
938 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
943 fputs_filtered (", ", stream
);
946 fprintf_filtered (stream
, "%s: ",
947 TYPE_FIELD_NAME (variant_type
, j
));
949 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
950 stream
, show
- 1, level
+ 2,
953 fputs_filtered (is_tuple
? ")" : "}", stream
);
956 fputs_filtered (",\n", stream
);
959 fputs_filtered ("}", stream
);
965 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
971 /* Compute the alignment of the type T. */
974 rust_type_alignment (struct type
*t
)
976 t
= check_typedef (t
);
977 switch (TYPE_CODE (t
))
980 error (_("Could not compute alignment of type"));
989 return TYPE_LENGTH (t
);
991 case TYPE_CODE_ARRAY
:
992 case TYPE_CODE_COMPLEX
:
993 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
995 case TYPE_CODE_STRUCT
:
996 case TYPE_CODE_UNION
:
1001 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1003 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1012 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1013 -- either on an obstack or on a gdbarch. */
1015 static struct type
*
1016 rust_composite_type (struct type
*original
,
1018 const char *field1
, struct type
*type1
,
1019 const char *field2
, struct type
*type2
)
1021 struct type
*result
= alloc_type_copy (original
);
1022 int i
, nfields
, bitpos
;
1030 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1031 TYPE_NAME (result
) = name
;
1032 TYPE_TAG_NAME (result
) = name
;
1034 TYPE_NFIELDS (result
) = nfields
;
1035 TYPE_FIELDS (result
)
1036 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1042 struct field
*field
= &TYPE_FIELD (result
, i
);
1044 SET_FIELD_BITPOS (*field
, bitpos
);
1045 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1047 FIELD_NAME (*field
) = field1
;
1048 FIELD_TYPE (*field
) = type1
;
1053 struct field
*field
= &TYPE_FIELD (result
, i
);
1054 int align
= rust_type_alignment (type2
);
1060 align
*= TARGET_CHAR_BIT
;
1061 delta
= bitpos
% align
;
1063 bitpos
+= align
- delta
;
1065 SET_FIELD_BITPOS (*field
, bitpos
);
1067 FIELD_NAME (*field
) = field2
;
1068 FIELD_TYPE (*field
) = type2
;
1073 TYPE_LENGTH (result
)
1074 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1075 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1079 /* See rust-lang.h. */
1082 rust_slice_type (const char *name
, struct type
*elt_type
,
1083 struct type
*usize_type
)
1087 elt_type
= lookup_pointer_type (elt_type
);
1088 type
= rust_composite_type (elt_type
, name
,
1089 "data_ptr", elt_type
,
1090 "length", usize_type
);
1095 enum rust_primitive_types
1097 rust_primitive_bool
,
1098 rust_primitive_char
,
1107 rust_primitive_isize
,
1108 rust_primitive_usize
,
1111 rust_primitive_unit
,
1113 nr_rust_primitive_types
1116 /* la_language_arch_info implementation for Rust. */
1119 rust_language_arch_info (struct gdbarch
*gdbarch
,
1120 struct language_arch_info
*lai
)
1122 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1124 struct type
**types
;
1125 unsigned int length
;
1127 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1130 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1131 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1132 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1133 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1134 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1135 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1136 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1137 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1138 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1139 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1141 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1142 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1143 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1145 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1146 floatformats_ieee_single
);
1147 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1148 floatformats_ieee_double
);
1150 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1152 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1153 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1154 types
[rust_primitive_usize
]);
1156 lai
->primitive_type_vector
= types
;
1157 lai
->bool_type_default
= types
[rust_primitive_bool
];
1158 lai
->string_char_type
= types
[rust_primitive_u8
];
1163 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1165 static struct value
*
1166 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1169 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1171 struct value
*function
, *result
, *arg0
;
1172 struct type
*type
, *fn_type
;
1173 const struct block
*block
;
1174 struct block_symbol sym
;
1176 /* For an ordinary function call we can simply defer to the
1177 generic implementation. */
1178 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1179 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1181 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1183 method
= &exp
->elts
[*pos
+ 1].string
;
1184 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1186 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1187 type in order to look up the method. */
1188 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1190 if (noside
== EVAL_SKIP
)
1192 for (i
= 0; i
< num_args
; ++i
)
1193 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1197 std::vector
<struct value
*> args (num_args
+ 1);
1200 /* We don't yet implement real Deref semantics. */
1201 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1202 args
[0] = value_ind (args
[0]);
1204 type
= value_type (args
[0]);
1205 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1206 && TYPE_CODE (type
) != TYPE_CODE_UNION
1207 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1208 || rust_tuple_type_p (type
))
1209 error (_("Method calls only supported on struct or enum types"));
1210 if (TYPE_TAG_NAME (type
) == NULL
)
1211 error (_("Method call on nameless type"));
1213 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1215 block
= get_selected_block (0);
1216 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1217 if (sym
.symbol
== NULL
)
1218 error (_("Could not find function named '%s'"), name
.c_str ());
1220 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1221 if (TYPE_NFIELDS (fn_type
) == 0)
1222 error (_("Function '%s' takes no arguments"), name
.c_str ());
1224 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1225 args
[0] = value_addr (args
[0]);
1227 function
= address_of_variable (sym
.symbol
, block
);
1229 for (i
= 0; i
< num_args
; ++i
)
1230 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1232 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1233 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1235 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1239 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1241 static struct value
*
1242 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1244 enum range_type kind
;
1245 struct value
*low
= NULL
, *high
= NULL
;
1246 struct value
*addrval
, *result
;
1248 struct type
*range_type
;
1249 struct type
*index_type
;
1250 struct type
*temp_type
;
1253 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1256 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1257 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1258 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1259 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1261 if (noside
== EVAL_SKIP
)
1262 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1269 name
= "std::ops::RangeFull";
1273 index_type
= value_type (high
);
1274 name
= "std::ops::RangeTo";
1281 index_type
= value_type (low
);
1282 name
= "std::ops::RangeFrom";
1286 if (!types_equal (value_type (low
), value_type (high
)))
1287 error (_("Range expression with different types"));
1288 index_type
= value_type (low
);
1289 name
= "std::ops::Range";
1293 /* If we don't have an index type, just allocate this on the
1294 arch. Here any type will do. */
1295 temp_type
= (index_type
== NULL
1296 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1298 /* It would be nicer to cache the range type. */
1299 range_type
= rust_composite_type (temp_type
, name
,
1300 low
== NULL
? NULL
: "start", index_type
,
1301 high
== NULL
? NULL
: "end", index_type
);
1303 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1304 return value_zero (range_type
, lval_memory
);
1306 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1307 addr
= value_as_long (addrval
);
1308 result
= value_at_lazy (range_type
, addr
);
1312 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1315 value_assign (start
, low
);
1320 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1323 value_assign (end
, high
);
1326 result
= value_at_lazy (range_type
, addr
);
1330 /* A helper function to compute the range and kind given a range
1331 value. TYPE is the type of the range value. RANGE is the range
1332 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1333 parameters might be filled in, or might not be, depending on the
1334 kind of range this is. KIND will always be set to the appropriate
1335 value describing the kind of range, and this can be used to
1336 determine whether LOW or HIGH are valid. */
1339 rust_compute_range (struct type
*type
, struct value
*range
,
1340 LONGEST
*low
, LONGEST
*high
,
1341 enum range_type
*kind
)
1347 *kind
= BOTH_BOUND_DEFAULT
;
1349 if (TYPE_NFIELDS (type
) == 0)
1353 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1355 *kind
= HIGH_BOUND_DEFAULT
;
1356 *low
= value_as_long (value_field (range
, 0));
1359 if (TYPE_NFIELDS (type
) > i
1360 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1362 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1363 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1364 *high
= value_as_long (value_field (range
, i
));
1368 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1370 static struct value
*
1371 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1374 struct value
*lhs
, *rhs
, *result
;
1375 struct type
*rhstype
;
1376 LONGEST low
, high_bound
;
1377 /* Initialized to appease the compiler. */
1378 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1383 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1384 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1386 if (noside
== EVAL_SKIP
)
1389 rhstype
= check_typedef (value_type (rhs
));
1390 if (rust_range_type_p (rhstype
))
1393 error (_("Can't take slice of array without '&'"));
1394 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1398 low
= value_as_long (rhs
);
1400 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1402 struct type
*type
= check_typedef (value_type (lhs
));
1404 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1410 struct type
*type
= check_typedef (value_type (lhs
));
1412 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1415 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1416 error (_("Can't compute array bounds"));
1418 error (_("Found array with non-zero lower bound"));
1421 else if (rust_slice_type_p (type
))
1425 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1426 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1428 high_bound
= value_as_long (len
);
1430 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1434 high_bound
= LONGEST_MAX
;
1437 error (_("Cannot subscript non-array type"));
1440 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1443 error (_("Index less than zero"));
1444 if (low
> high_bound
)
1445 error (_("Index greater than length"));
1447 result
= value_subscript (base
, low
);
1454 struct type
*usize
, *slice
;
1456 struct value
*addrval
, *tem
;
1458 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1461 error (_("High index less than zero"));
1463 error (_("Low index greater than high index"));
1464 if (high
> high_bound
)
1465 error (_("High index greater than length"));
1467 usize
= language_lookup_primitive_type (exp
->language_defn
,
1470 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1473 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1474 addr
= value_as_long (addrval
);
1475 tem
= value_at_lazy (slice
, addr
);
1477 value_assign (value_field (tem
, 0), value_addr (result
));
1478 value_assign (value_field (tem
, 1),
1479 value_from_longest (usize
, high
- low
));
1481 result
= value_at_lazy (slice
, addr
);
1484 result
= value_addr (result
);
1490 /* evaluate_exp implementation for Rust. */
1492 static struct value
*
1493 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1494 int *pos
, enum noside noside
)
1496 struct value
*result
;
1498 switch (exp
->elts
[*pos
].opcode
)
1500 case UNOP_COMPLEMENT
:
1502 struct value
*value
;
1505 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1506 if (noside
== EVAL_SKIP
)
1508 /* Preserving the type is enough. */
1511 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1512 result
= value_from_longest (value_type (value
),
1513 value_logical_not (value
));
1515 result
= value_complement (value
);
1519 case BINOP_SUBSCRIPT
:
1520 result
= rust_subscript (exp
, pos
, noside
, 0);
1524 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1530 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1531 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1534 struct value
*addrval
= NULL
;
1538 if (noside
== EVAL_NORMAL
)
1540 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1541 addr
= value_as_long (addrval
);
1542 result
= value_at_lazy (type
, addr
);
1545 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1550 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1551 if (noside
== EVAL_NORMAL
)
1553 /* This isn't quite right but will do for the time
1554 being, seeing that we can't implement the Copy
1556 value_assign (result
, init
);
1562 gdb_assert (arglen
% 2 == 0);
1563 for (i
= 0; i
< arglen
; i
+= 2)
1566 const char *fieldname
;
1567 struct value
*value
, *field
;
1569 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1571 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1573 fieldname
= &exp
->elts
[*pos
].string
;
1574 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1576 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1577 if (noside
== EVAL_NORMAL
)
1579 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1581 value_assign (field
, value
);
1585 if (noside
== EVAL_SKIP
)
1586 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1588 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1589 result
= allocate_value (type
);
1591 result
= value_at_lazy (type
, addr
);
1600 struct value
*ncopies
;
1602 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1603 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1604 copies
= value_as_long (ncopies
);
1606 error (_("Array with negative number of elements"));
1608 if (noside
== EVAL_NORMAL
)
1612 std::vector
<struct value
*> eltvec (copies
);
1614 for (i
= 0; i
< copies
; ++i
)
1616 result
= value_array (0, copies
- 1, eltvec
.data ());
1620 struct type
*arraytype
1621 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1622 result
= allocate_value (arraytype
);
1627 case STRUCTOP_ANONYMOUS
:
1629 /* Anonymous field access, i.e. foo.1. */
1631 int pc
, field_number
, nfields
;
1632 struct type
*type
, *variant_type
;
1633 struct disr_info disr
;
1636 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1638 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1640 type
= value_type (lhs
);
1641 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1643 struct cleanup
*cleanup
;
1645 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1646 value_embedded_offset (lhs
),
1647 value_address (lhs
), lhs
);
1649 cleanup
= make_cleanup (xfree
, disr
.name
);
1651 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1653 variant_type
= NULL
;
1658 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1659 nfields
= TYPE_NFIELDS (variant_type
);
1662 if (!disr
.is_encoded
)
1665 if (field_number
>= nfields
|| field_number
< 0)
1666 error(_("Cannot access field %d of variant %s, \
1667 there are only %d fields"),
1668 disr
.is_encoded
? field_number
: field_number
- 1,
1670 disr
.is_encoded
? nfields
: nfields
- 1);
1672 if (!(disr
.is_encoded
1673 ? rust_tuple_struct_type_p (variant_type
)
1674 : rust_tuple_variant_type_p (variant_type
)))
1675 error(_("Variant %s is not a tuple variant"), disr
.name
);
1677 result
= value_primitive_field (lhs
, 0, field_number
,
1679 do_cleanups (cleanup
);
1681 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1683 /* Tuples and tuple structs */
1684 nfields
= TYPE_NFIELDS(type
);
1686 if (field_number
>= nfields
|| field_number
< 0)
1687 error(_("Cannot access field %d of %s, there are only %d fields"),
1688 field_number
, TYPE_TAG_NAME (type
), nfields
);
1690 /* Tuples are tuple structs too. */
1691 if (!rust_tuple_struct_type_p (type
))
1692 error(_("Attempting to access anonymous field %d of %s, which is \
1693 not a tuple, tuple struct, or tuple-like variant"),
1694 field_number
, TYPE_TAG_NAME (type
));
1696 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1699 error(_("Anonymous field access is only allowed on tuples, \
1700 tuple structs, and tuple-like enum variants"));
1704 case STRUCTOP_STRUCT
:
1711 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1712 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1713 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1715 type
= value_type (lhs
);
1717 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1720 struct disr_info disr
;
1721 struct cleanup
* cleanup
;
1722 struct type
* variant_type
;
1725 field_name
= &exp
->elts
[pc
+ 2].string
;
1727 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1728 value_embedded_offset (lhs
),
1729 value_address (lhs
), lhs
);
1731 cleanup
= make_cleanup (xfree
, disr
.name
);
1733 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1734 error(_("Could not find field %s of struct variant %s"),
1735 field_name
, disr
.name
);
1737 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1739 if (variant_type
== NULL
1741 ? rust_tuple_struct_type_p (variant_type
)
1742 : rust_tuple_variant_type_p (variant_type
)))
1743 error(_("Attempting to access named field %s of tuple variant %s, \
1744 which has only anonymous fields"),
1745 field_name
, disr
.name
);
1747 start
= disr
.is_encoded
? 0 : 1;
1748 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1750 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1752 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1757 if (i
== TYPE_NFIELDS (variant_type
))
1758 /* We didn't find it. */
1759 error(_("Could not find field %s of struct variant %s"),
1760 field_name
, disr
.name
);
1762 do_cleanups (cleanup
);
1767 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1773 result
= rust_range (exp
, pos
, noside
);
1777 /* We might have &array[range], in which case we need to make a
1779 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1782 result
= rust_subscript (exp
, pos
, noside
, 1);
1787 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1794 /* operator_length implementation for Rust. */
1797 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1803 switch (exp
->elts
[pc
- 1].opcode
)
1806 /* We handle aggregate as a type and argument count. The first
1807 argument might be OP_OTHERS. After that the arguments
1808 alternate: first an OP_NAME, then an expression. */
1810 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1818 case STRUCTOP_ANONYMOUS
:
1829 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1837 /* op_name implementation for Rust. */
1840 rust_op_name (enum exp_opcode opcode
)
1845 return "OP_AGGREGATE";
1849 return op_name_standard (opcode
);
1853 /* dump_subexp_body implementation for Rust. */
1856 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1859 switch (exp
->elts
[elt
].opcode
)
1863 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1866 fprintf_filtered (stream
, "Type @");
1867 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1868 fprintf_filtered (stream
, " (");
1869 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1870 fprintf_filtered (stream
, "), length %d", length
);
1873 for (i
= 0; i
< length
; ++i
)
1874 elt
= dump_subexp (exp
, stream
, elt
);
1881 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1883 fprintf_filtered (stream
, "%s: %s",
1884 (exp
->elts
[elt
].opcode
== OP_STRING
1885 ? "string" : "name"),
1886 &exp
->elts
[elt
+ 2].string
);
1887 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1892 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1895 case STRUCTOP_ANONYMOUS
:
1899 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1901 fprintf_filtered (stream
, "Field number: %d", field_number
);
1902 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1910 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1917 /* print_subexp implementation for Rust. */
1920 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1921 enum precedence prec
)
1923 switch (exp
->elts
[*pos
].opcode
)
1927 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1930 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1931 fputs_filtered (" { ", stream
);
1934 for (i
= 0; i
< length
; ++i
)
1936 rust_print_subexp (exp
, pos
, stream
, prec
);
1937 fputs_filtered (", ", stream
);
1939 fputs_filtered (" }", stream
);
1945 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1947 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1948 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1954 fputs_filtered ("<<others>> (", stream
);
1956 rust_print_subexp (exp
, pos
, stream
, prec
);
1957 fputs_filtered (")", stream
);
1961 case STRUCTOP_ANONYMOUS
:
1963 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1966 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1967 fprintf_filtered (stream
, ".%d", tem
);
1973 fprintf_filtered (stream
, "[");
1974 rust_print_subexp (exp
, pos
, stream
, prec
);
1975 fprintf_filtered (stream
, "; ");
1976 rust_print_subexp (exp
, pos
, stream
, prec
);
1977 fprintf_filtered (stream
, "]");
1981 print_subexp_standard (exp
, pos
, stream
, prec
);
1986 /* operator_check implementation for Rust. */
1989 rust_operator_check (struct expression
*exp
, int pos
,
1990 int (*objfile_func
) (struct objfile
*objfile
,
1994 switch (exp
->elts
[pos
].opcode
)
1998 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1999 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2001 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2012 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2020 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2022 static struct block_symbol
2023 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2025 const struct block
*block
,
2026 const domain_enum domain
)
2028 struct block_symbol result
= {NULL
, NULL
};
2030 if (symbol_lookup_debug
)
2032 fprintf_unfiltered (gdb_stdlog
,
2033 "rust_lookup_symbol_non_local"
2034 " (%s, %s (scope %s), %s)\n",
2035 name
, host_address_to_string (block
),
2036 block_scope (block
), domain_name (domain
));
2039 /* Look up bare names in the block's scope. */
2040 if (name
[cp_find_first_component (name
)] == '\0')
2042 const char *scope
= block_scope (block
);
2044 if (scope
[0] != '\0')
2046 std::string scopedname
= std::string (scope
) + "::" + name
;
2048 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2050 if (result
.symbol
== NULL
)
2051 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2059 /* la_sniff_from_mangled_name for Rust. */
2062 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2064 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2065 return *demangled
!= NULL
;
2070 static const struct exp_descriptor exp_descriptor_rust
=
2073 rust_operator_length
,
2074 rust_operator_check
,
2076 rust_dump_subexp_body
,
2077 rust_evaluate_subexp
2080 static const char *rust_extensions
[] =
2085 static const struct language_defn rust_language_defn
=
2095 &exp_descriptor_rust
,
2099 rust_printchar
, /* Print a character constant */
2100 rust_printstr
, /* Function to print string constant */
2101 rust_emitchar
, /* Print a single char */
2102 rust_print_type
, /* Print a type using appropriate syntax */
2103 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2104 rust_val_print
, /* Print a value using appropriate syntax */
2105 c_value_print
, /* Print a top-level value */
2106 default_read_var_value
, /* la_read_var_value */
2107 NULL
, /* Language specific skip_trampoline */
2108 NULL
, /* name_of_this */
2109 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2110 basic_lookup_transparent_type
,/* lookup_transparent_type */
2111 gdb_demangle
, /* Language specific symbol demangler */
2112 rust_sniff_from_mangled_name
,
2113 NULL
, /* Language specific
2114 class_name_from_physname */
2115 c_op_print_tab
, /* expression operators for printing */
2116 1, /* c-style arrays */
2117 0, /* String lower bound */
2118 default_word_break_characters
,
2119 default_make_symbol_completion_list
,
2120 rust_language_arch_info
,
2121 default_print_array_index
,
2122 default_pass_by_reference
,
2124 NULL
, /* la_get_symbol_name_cmp */
2125 iterate_over_symbols
,
2126 &default_varobj_ops
,
2133 _initialize_rust_language (void)
2135 add_language (&rust_language_defn
);