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"
36 extern initialize_file_ftype _initialize_rust_language
;
38 /* Returns the last segment of a Rust path like foo::bar::baz. Will
39 not handle cases where the last segment contains generics. This
40 will return NULL if the last segment cannot be found. */
43 rust_last_path_segment (const char * path
)
45 const char *result
= strrchr (path
, ':');
52 /* Find the Rust crate for BLOCK. If no crate can be found, returns
53 NULL. Otherwise, returns a newly allocated string that the caller
54 is responsible for freeing. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
64 return xstrndup (scope
, cp_find_first_component (scope
));
67 /* Information about the discriminant/variant of an enum */
71 /* Name of field. Must be freed by caller. */
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 /* Utility function to get discriminant info for a given value. */
96 static struct disr_info
97 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
98 int embedded_offset
, CORE_ADDR address
,
99 const struct value
*val
)
102 struct disr_info ret
;
103 struct type
*disr_type
;
104 struct ui_file
*temp_file
;
105 struct value_print_options opts
;
106 struct cleanup
*cleanup
;
107 const char *name_segment
;
109 get_no_prettyformat_print_options (&opts
);
114 if (TYPE_NFIELDS (type
) == 0)
115 error (_("Encountered void enum value"));
117 /* If an enum has two values where one is empty and the other holds
118 a pointer that cannot be zero; then the Rust compiler optimizes
119 away the discriminant and instead uses a zero value in the
120 pointer field to indicate the empty variant. */
121 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
122 strlen (RUST_ENUM_PREFIX
)) == 0)
124 char *tail
, *token
, *name
, *saveptr
= NULL
;
125 unsigned long fieldno
;
126 struct type
*member_type
;
131 if (TYPE_NFIELDS (type
) != 1)
132 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
134 /* Optimized enums have only one field. */
135 member_type
= TYPE_FIELD_TYPE (type
, 0);
137 name
= xstrdup (TYPE_FIELD_NAME (type
, 0));
138 cleanup
= make_cleanup (xfree
, name
);
139 tail
= name
+ strlen (RUST_ENUM_PREFIX
);
141 /* The location of the value that doubles as a discriminant is
142 stored in the name of the field, as
143 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
144 where the fieldnos are the indices of the fields that should be
145 traversed in order to find the field (which may be several fields deep)
146 and the variantname is the name of the variant of the case when the
148 for (token
= strtok_r (tail
, "$", &saveptr
);
150 token
= strtok_r (NULL
, "$", &saveptr
))
152 if (sscanf (token
, "%lu", &fieldno
) != 1)
154 /* We have reached the enum name, which cannot start
158 if (fieldno
>= TYPE_NFIELDS (member_type
))
159 error (_("%s refers to field after end of member type"),
162 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
163 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
167 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
168 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
172 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
173 ret
.name
= concat (TYPE_NAME (type
), "::", token
, (char *) NULL
);
177 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
178 ret
.name
= concat (TYPE_NAME (type
), "::",
179 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))),
183 do_cleanups (cleanup
);
187 disr_type
= TYPE_FIELD_TYPE (type
, 0);
189 if (TYPE_NFIELDS (disr_type
) == 0)
191 /* This is a bounds check and should never be hit unless Rust
192 has changed its debuginfo format. */
193 error (_("Could not find enum discriminant field"));
196 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
197 error (_("Rust debug format has changed"));
199 temp_file
= mem_fileopen ();
200 cleanup
= make_cleanup_ui_file_delete (temp_file
);
201 /* The first value of the first field (or any field)
202 is the discriminant value. */
203 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
204 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
205 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
209 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
210 name_segment
= rust_last_path_segment (ret
.name
);
211 if (name_segment
!= NULL
)
213 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
215 /* Sadly, the discriminant value paths do not match the type
216 field name paths ('core::option::Option::Some' vs
217 'core::option::Some'). However, enum variant names are
218 unique in the last path segment and the generics are not
219 part of this path, so we can just compare those. This is
220 hackish and would be better fixed by improving rustc's
221 metadata for enums. */
222 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
224 if (field_type
!= NULL
225 && strcmp (name_segment
,
226 rust_last_path_segment (field_type
)) == 0)
234 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
236 /* Somehow the discriminant wasn't found. */
237 make_cleanup (xfree
, ret
.name
);
238 error (_("Could not find variant of %s with discriminant %s"),
239 TYPE_TAG_NAME (type
), ret
.name
);
242 do_cleanups (cleanup
);
246 /* See rust-lang.h. */
249 rust_tuple_type_p (struct type
*type
)
251 /* The current implementation is a bit of a hack, but there's
252 nothing else in the debuginfo to distinguish a tuple from a
254 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
255 && TYPE_TAG_NAME (type
) != NULL
256 && TYPE_TAG_NAME (type
)[0] == '(');
260 /* Return true if all non-static fields of a structlike type are in a
261 sequence like __0, __1, __2. OFFSET lets us skip fields. */
264 rust_underscore_fields (struct type
*type
, int offset
)
270 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
272 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
274 if (!field_is_static (&TYPE_FIELD (type
, i
)))
282 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
283 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
292 /* See rust-lang.h. */
295 rust_tuple_struct_type_p (struct type
*type
)
297 return rust_underscore_fields (type
, 0);
300 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
303 rust_tuple_variant_type_p (struct type
*type
)
305 /* First field is discriminant */
306 return rust_underscore_fields (type
, 1);
309 /* Return true if TYPE is a slice type, otherwise false. */
312 rust_slice_type_p (struct type
*type
)
314 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
315 && TYPE_TAG_NAME (type
) != NULL
316 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
319 /* Return true if TYPE is a range type, otherwise false. */
322 rust_range_type_p (struct type
*type
)
326 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
327 || TYPE_NFIELDS (type
) > 2
328 || TYPE_TAG_NAME (type
) == NULL
329 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
332 if (TYPE_NFIELDS (type
) == 0)
336 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
338 if (TYPE_NFIELDS (type
) == 1)
342 else if (TYPE_NFIELDS (type
) == 2)
344 /* First field had to be "start". */
348 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
351 /* Return true if TYPE seems to be the type "u8", otherwise false. */
354 rust_u8_type_p (struct type
*type
)
356 return (TYPE_CODE (type
) == TYPE_CODE_INT
357 && TYPE_UNSIGNED (type
)
358 && TYPE_LENGTH (type
) == 1);
361 /* Return true if TYPE is a Rust character type. */
364 rust_chartype_p (struct type
*type
)
366 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
367 && TYPE_LENGTH (type
) == 4
368 && TYPE_UNSIGNED (type
));
373 /* la_emitchar implementation for Rust. */
376 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
378 if (!rust_chartype_p (type
))
379 generic_emit_char (c
, type
, stream
, quoter
,
380 target_charset (get_type_arch (type
)));
381 else if (c
== '\\' || c
== quoter
)
382 fprintf_filtered (stream
, "\\%c", c
);
384 fputs_filtered ("\\n", stream
);
386 fputs_filtered ("\\r", stream
);
388 fputs_filtered ("\\t", stream
);
390 fputs_filtered ("\\0", stream
);
391 else if (c
>= 32 && c
<= 127 && isprint (c
))
392 fputc_filtered (c
, stream
);
394 fprintf_filtered (stream
, "\\x%02x", c
);
396 fprintf_filtered (stream
, "\\u{%06x}", c
);
399 /* la_printchar implementation for Rust. */
402 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
404 fputs_filtered ("'", stream
);
405 LA_EMIT_CHAR (c
, type
, stream
, '\'');
406 fputs_filtered ("'", stream
);
409 /* la_printstr implementation for Rust. */
412 rust_printstr (struct ui_file
*stream
, struct type
*type
,
413 const gdb_byte
*string
, unsigned int length
,
414 const char *user_encoding
, int force_ellipses
,
415 const struct value_print_options
*options
)
417 /* Rust always uses UTF-8, but let the caller override this if need
419 const char *encoding
= user_encoding
;
420 if (user_encoding
== NULL
|| !*user_encoding
)
422 /* In Rust strings, characters are "u8". */
423 if (rust_u8_type_p (type
))
427 /* This is probably some C string, so let's let C deal with
429 c_printstr (stream
, type
, string
, length
, user_encoding
,
430 force_ellipses
, options
);
435 /* This is not ideal as it doesn't use our character printer. */
436 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
442 static const struct generic_val_print_decorations rust_decorations
=
444 /* Complex isn't used in Rust, but we provide C-ish values just in
456 /* la_val_print implementation for Rust. */
459 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
460 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
461 const struct value
*val
,
462 const struct value_print_options
*options
)
464 type
= check_typedef (type
);
465 switch (TYPE_CODE (type
))
469 LONGEST low_bound
, high_bound
;
471 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
472 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
473 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
475 /* We have a pointer to a byte string, so just print
477 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
479 struct gdbarch
*arch
= get_type_arch (type
);
480 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
482 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
483 if (options
->addressprint
)
485 fputs_filtered (paddress (arch
, addr
), stream
);
486 fputs_filtered (" ", stream
);
489 fputs_filtered ("b", stream
);
490 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
491 high_bound
- low_bound
+ 1, stream
,
498 case TYPE_CODE_METHODPTR
:
499 case TYPE_CODE_MEMBERPTR
:
500 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
501 recurse
, val
, options
);
505 /* Recognize the unit type. */
506 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
507 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
509 fputs_filtered ("()", stream
);
514 case TYPE_CODE_STRING
:
516 struct gdbarch
*arch
= get_type_arch (type
);
517 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
518 LONGEST low_bound
, high_bound
;
520 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
521 error (_("Could not determine the array bounds"));
523 /* If we see a plain TYPE_CODE_STRING, then we're printing a
524 byte string, hence the choice of "ASCII" as the
526 fputs_filtered ("b", stream
);
527 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
528 valaddr
+ embedded_offset
* unit_size
,
529 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
533 case TYPE_CODE_ARRAY
:
535 LONGEST low_bound
, high_bound
;
537 if (get_array_bounds (type
, &low_bound
, &high_bound
)
538 && high_bound
- low_bound
+ 1 == 0)
539 fputs_filtered ("[]", stream
);
545 case TYPE_CODE_UNION
:
547 int j
, nfields
, first_field
, is_tuple
, start
;
548 struct type
*variant_type
;
549 struct disr_info disr
;
550 struct value_print_options opts
;
551 struct cleanup
*cleanup
;
556 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
558 cleanup
= make_cleanup (xfree
, disr
.name
);
560 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
562 fprintf_filtered (stream
, "%s", disr
.name
);
567 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
568 nfields
= TYPE_NFIELDS (variant_type
);
570 is_tuple
= (disr
.is_encoded
571 ? rust_tuple_struct_type_p (variant_type
)
572 : rust_tuple_variant_type_p (variant_type
));
573 start
= disr
.is_encoded
? 0 : 1;
577 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
579 fprintf_filtered (stream
, "%s(", disr
.name
);
582 /* struct variant. */
583 fprintf_filtered (stream
, "%s{", disr
.name
);
588 /* In case of a nullary variant like 'None', just output
590 fprintf_filtered (stream
, "%s", disr
.name
);
594 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
597 fputs_filtered (", ", stream
);
601 fprintf_filtered (stream
, "%s: ",
602 TYPE_FIELD_NAME (variant_type
, j
));
604 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
607 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
608 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
610 stream
, recurse
+ 1, val
, &opts
,
615 fputs_filtered (")", stream
);
617 fputs_filtered ("}", stream
);
620 do_cleanups (cleanup
);
624 case TYPE_CODE_STRUCT
:
628 int is_tuple
= rust_tuple_type_p (type
);
629 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
630 struct value_print_options opts
;
634 if (TYPE_TAG_NAME (type
) != NULL
)
635 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
637 if (TYPE_NFIELDS (type
) == 0)
640 if (TYPE_TAG_NAME (type
) != NULL
)
641 fputs_filtered (" ", stream
);
644 if (is_tuple
|| is_tuple_struct
)
645 fputs_filtered ("(", stream
);
647 fputs_filtered ("{", stream
);
653 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
655 if (field_is_static (&TYPE_FIELD (type
, i
)))
659 fputs_filtered (",", stream
);
661 if (options
->prettyformat
)
663 fputs_filtered ("\n", stream
);
664 print_spaces_filtered (2 + 2 * recurse
, stream
);
666 else if (!first_field
)
667 fputs_filtered (" ", stream
);
671 if (!is_tuple
&& !is_tuple_struct
)
673 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
674 fputs_filtered (": ", stream
);
677 val_print (TYPE_FIELD_TYPE (type
, i
),
679 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
681 stream
, recurse
+ 1, val
, &opts
,
685 if (options
->prettyformat
)
687 fputs_filtered ("\n", stream
);
688 print_spaces_filtered (2 * recurse
, stream
);
691 if (is_tuple
|| is_tuple_struct
)
692 fputs_filtered (")", stream
);
694 fputs_filtered ("}", stream
);
700 /* Nothing special yet. */
701 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
702 recurse
, val
, options
, &rust_decorations
);
708 /* la_print_typedef implementation for Rust. */
711 rust_print_typedef (struct type
*type
,
712 struct symbol
*new_symbol
,
713 struct ui_file
*stream
)
715 type
= check_typedef (type
);
716 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
717 type_print (type
, "", stream
, 0);
718 fprintf_filtered (stream
, ";\n");
721 /* la_print_type implementation for Rust. */
724 rust_print_type (struct type
*type
, const char *varstring
,
725 struct ui_file
*stream
, int show
, int level
,
726 const struct type_print_options
*flags
)
732 && TYPE_NAME (type
) != NULL
)
734 /* Rust calls the unit type "void" in its debuginfo,
735 but we don't want to print it as that. */
736 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
737 fputs_filtered ("()", stream
);
739 fputs_filtered (TYPE_NAME (type
), stream
);
743 type
= check_typedef (type
);
744 switch (TYPE_CODE (type
))
747 fputs_filtered ("()", stream
);
751 /* Delegate varargs to the C printer. */
752 if (TYPE_VARARGS (type
))
755 fputs_filtered ("fn ", stream
);
756 if (varstring
!= NULL
)
757 fputs_filtered (varstring
, stream
);
758 fputs_filtered ("(", stream
);
759 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
763 fputs_filtered (", ", stream
);
764 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
767 fputs_filtered (")", stream
);
768 /* If it returns unit, we can omit the return type. */
769 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
771 fputs_filtered (" -> ", stream
);
772 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
776 case TYPE_CODE_ARRAY
:
778 LONGEST low_bound
, high_bound
;
780 fputs_filtered ("[", stream
);
781 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
782 stream
, show
- 1, level
, flags
);
783 fputs_filtered ("; ", stream
);
785 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
786 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
787 fprintf_filtered (stream
, "variable length");
788 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
789 fprintf_filtered (stream
, "%s",
790 plongest (high_bound
- low_bound
+ 1));
791 fputs_filtered ("]", stream
);
795 case TYPE_CODE_STRUCT
:
799 /* Print a tuple type simply. */
800 if (rust_tuple_type_p (type
))
802 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
806 /* If we see a base class, delegate to C. */
807 if (TYPE_N_BASECLASSES (type
) > 0)
810 fputs_filtered ("struct ", stream
);
811 if (TYPE_TAG_NAME (type
) != NULL
)
812 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
814 is_tuple_struct
= rust_tuple_struct_type_p (type
);
816 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
818 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
820 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
825 if (field_is_static (&TYPE_FIELD (type
, i
)))
828 /* We'd like to print "pub" here as needed, but rustc
829 doesn't emit the debuginfo, and our types don't have
830 cplus_struct_type attached. */
832 /* For a tuple struct we print the type but nothing
834 print_spaces_filtered (level
+ 2, stream
);
835 if (!is_tuple_struct
)
836 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
838 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
839 stream
, show
- 1, level
+ 2,
841 fputs_filtered (",\n", stream
);
844 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
852 fputs_filtered ("enum ", stream
);
853 if (TYPE_TAG_NAME (type
) != NULL
)
855 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
856 fputs_filtered (" ", stream
);
857 len
= strlen (TYPE_TAG_NAME (type
));
859 fputs_filtered ("{\n", stream
);
861 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
863 const char *name
= TYPE_FIELD_NAME (type
, i
);
868 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
870 && name
[len
+ 1] == ':')
872 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
875 fputs_filtered ("}", stream
);
879 case TYPE_CODE_UNION
:
883 /* Skip the discriminant field. */
886 fputs_filtered ("enum ", stream
);
887 if (TYPE_TAG_NAME (type
) != NULL
)
889 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
890 fputs_filtered (" ", stream
);
891 len
= strlen (TYPE_TAG_NAME (type
));
893 fputs_filtered ("{\n", stream
);
895 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
896 strlen (RUST_ENUM_PREFIX
)) == 0)
898 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
899 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
901 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
902 /* There is no explicit discriminant field, skip nothing. */
907 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
909 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
911 = rust_last_path_segment (TYPE_NAME (variant_type
));
913 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
915 if (TYPE_NFIELDS (variant_type
) > skip_to
)
918 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
921 fputs_filtered (is_tuple
? "(" : "{", stream
);
922 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
927 fputs_filtered (", ", stream
);
930 fprintf_filtered (stream
, "%s: ",
931 TYPE_FIELD_NAME (variant_type
, j
));
933 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
934 stream
, show
- 1, level
+ 2,
937 fputs_filtered (is_tuple
? ")" : "}", stream
);
940 fputs_filtered (",\n", stream
);
943 fputs_filtered ("}", stream
);
949 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
955 /* Compute the alignment of the type T. */
958 rust_type_alignment (struct type
*t
)
960 t
= check_typedef (t
);
961 switch (TYPE_CODE (t
))
964 error (_("Could not compute alignment of type"));
973 return TYPE_LENGTH (t
);
975 case TYPE_CODE_ARRAY
:
976 case TYPE_CODE_COMPLEX
:
977 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
979 case TYPE_CODE_STRUCT
:
980 case TYPE_CODE_UNION
:
985 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
987 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
996 /* Like arch_composite_type, but uses TYPE to decide how to allocate
997 -- either on an obstack or on a gdbarch. */
1000 rust_composite_type (struct type
*original
,
1002 const char *field1
, struct type
*type1
,
1003 const char *field2
, struct type
*type2
)
1005 struct type
*result
= alloc_type_copy (original
);
1006 int i
, nfields
, bitpos
;
1014 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1015 TYPE_NAME (result
) = name
;
1016 TYPE_TAG_NAME (result
) = name
;
1018 TYPE_NFIELDS (result
) = nfields
;
1019 TYPE_FIELDS (result
)
1020 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1026 struct field
*field
= &TYPE_FIELD (result
, i
);
1028 SET_FIELD_BITPOS (*field
, bitpos
);
1029 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1031 FIELD_NAME (*field
) = field1
;
1032 FIELD_TYPE (*field
) = type1
;
1037 struct field
*field
= &TYPE_FIELD (result
, i
);
1038 int align
= rust_type_alignment (type2
);
1044 align
*= TARGET_CHAR_BIT
;
1045 delta
= bitpos
% align
;
1047 bitpos
+= align
- delta
;
1049 SET_FIELD_BITPOS (*field
, bitpos
);
1051 FIELD_NAME (*field
) = field2
;
1052 FIELD_TYPE (*field
) = type2
;
1057 TYPE_LENGTH (result
)
1058 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1059 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1063 /* See rust-lang.h. */
1066 rust_slice_type (const char *name
, struct type
*elt_type
,
1067 struct type
*usize_type
)
1071 elt_type
= lookup_pointer_type (elt_type
);
1072 type
= rust_composite_type (elt_type
, name
,
1073 "data_ptr", elt_type
,
1074 "length", usize_type
);
1079 enum rust_primitive_types
1081 rust_primitive_bool
,
1082 rust_primitive_char
,
1091 rust_primitive_isize
,
1092 rust_primitive_usize
,
1095 rust_primitive_unit
,
1097 nr_rust_primitive_types
1100 /* la_language_arch_info implementation for Rust. */
1103 rust_language_arch_info (struct gdbarch
*gdbarch
,
1104 struct language_arch_info
*lai
)
1106 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1108 struct type
**types
;
1109 unsigned int length
;
1111 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1114 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1115 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1116 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1117 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1118 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1119 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1120 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1121 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1122 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1123 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1125 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1126 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1127 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1129 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1130 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1132 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1134 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1135 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1136 types
[rust_primitive_usize
]);
1138 lai
->primitive_type_vector
= types
;
1139 lai
->bool_type_default
= types
[rust_primitive_bool
];
1140 lai
->string_char_type
= types
[rust_primitive_u8
];
1145 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1147 static struct value
*
1148 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1151 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1154 struct value
*function
, *result
, *arg0
;
1155 struct value
**args
;
1156 struct cleanup
*cleanup
;
1157 struct type
*type
, *fn_type
;
1158 const struct block
*block
;
1159 struct block_symbol sym
;
1161 /* For an ordinary function call we can simply defer to the
1162 generic implementation. */
1163 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1164 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1166 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1168 method
= &exp
->elts
[*pos
+ 1].string
;
1169 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1171 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1172 type in order to look up the method. */
1173 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1175 if (noside
== EVAL_SKIP
)
1177 for (i
= 0; i
< num_args
; ++i
)
1178 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1182 args
= XNEWVEC (struct value
*, num_args
+ 1);
1183 cleanup
= make_cleanup (xfree
, args
);
1186 /* We don't yet implement real Deref semantics. */
1187 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1188 args
[0] = value_ind (args
[0]);
1190 type
= value_type (args
[0]);
1191 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1192 && TYPE_CODE (type
) != TYPE_CODE_UNION
1193 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1194 || rust_tuple_type_p (type
))
1195 error (_("Method calls only supported on struct or enum types"));
1196 if (TYPE_TAG_NAME (type
) == NULL
)
1197 error (_("Method call on nameless type"));
1199 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1200 make_cleanup (xfree
, name
);
1202 block
= get_selected_block (0);
1203 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1204 if (sym
.symbol
== NULL
)
1205 error (_("Could not find function named '%s'"), name
);
1207 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1208 if (TYPE_NFIELDS (fn_type
) == 0)
1209 error (_("Function '%s' takes no arguments"), name
);
1211 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1212 args
[0] = value_addr (args
[0]);
1214 function
= address_of_variable (sym
.symbol
, block
);
1216 for (i
= 0; i
< num_args
; ++i
)
1217 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1219 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1220 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1222 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1223 do_cleanups (cleanup
);
1227 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1229 static struct value
*
1230 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1232 enum range_type kind
;
1233 struct value
*low
= NULL
, *high
= NULL
;
1234 struct value
*addrval
, *result
;
1236 struct type
*range_type
;
1237 struct type
*index_type
;
1238 struct type
*temp_type
;
1241 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1244 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1245 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1246 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1247 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1249 if (noside
== EVAL_SKIP
)
1250 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1257 name
= "std::ops::RangeFull";
1261 index_type
= value_type (high
);
1262 name
= "std::ops::RangeTo";
1269 index_type
= value_type (low
);
1270 name
= "std::ops::RangeFrom";
1274 if (!types_equal (value_type (low
), value_type (high
)))
1275 error (_("Range expression with different types"));
1276 index_type
= value_type (low
);
1277 name
= "std::ops::Range";
1281 /* If we don't have an index type, just allocate this on the
1282 arch. Here any type will do. */
1283 temp_type
= (index_type
== NULL
1284 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1286 /* It would be nicer to cache the range type. */
1287 range_type
= rust_composite_type (temp_type
, name
,
1288 low
== NULL
? NULL
: "start", index_type
,
1289 high
== NULL
? NULL
: "end", index_type
);
1291 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1292 return value_zero (range_type
, lval_memory
);
1294 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1295 addr
= value_as_long (addrval
);
1296 result
= value_at_lazy (range_type
, addr
);
1300 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1303 value_assign (start
, low
);
1308 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1311 value_assign (end
, high
);
1314 result
= value_at_lazy (range_type
, addr
);
1318 /* A helper function to compute the range and kind given a range
1319 value. TYPE is the type of the range value. RANGE is the range
1320 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1321 parameters might be filled in, or might not be, depending on the
1322 kind of range this is. KIND will always be set to the appropriate
1323 value describing the kind of range, and this can be used to
1324 determine whether LOW or HIGH are valid. */
1327 rust_compute_range (struct type
*type
, struct value
*range
,
1328 LONGEST
*low
, LONGEST
*high
,
1329 enum range_type
*kind
)
1335 *kind
= BOTH_BOUND_DEFAULT
;
1337 if (TYPE_NFIELDS (type
) == 0)
1341 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1343 *kind
= HIGH_BOUND_DEFAULT
;
1344 *low
= value_as_long (value_field (range
, 0));
1347 if (TYPE_NFIELDS (type
) > i
1348 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1350 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1351 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1352 *high
= value_as_long (value_field (range
, i
));
1356 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1358 static struct value
*
1359 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1362 struct value
*lhs
, *rhs
, *result
;
1363 struct type
*rhstype
;
1364 LONGEST low
, high_bound
;
1365 /* Initialized to appease the compiler. */
1366 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1371 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1372 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1374 if (noside
== EVAL_SKIP
)
1377 rhstype
= check_typedef (value_type (rhs
));
1378 if (rust_range_type_p (rhstype
))
1381 error (_("Can't take slice of array without '&'"));
1382 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1386 low
= value_as_long (rhs
);
1388 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1390 struct type
*type
= check_typedef (value_type (lhs
));
1392 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1398 struct type
*type
= check_typedef (value_type (lhs
));
1400 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1403 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1404 error (_("Can't compute array bounds"));
1406 error (_("Found array with non-zero lower bound"));
1409 else if (rust_slice_type_p (type
))
1413 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1414 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1416 high_bound
= value_as_long (len
);
1419 error (_("Cannot subscript non-array type"));
1422 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1425 error (_("Index less than zero"));
1426 if (low
> high_bound
)
1427 error (_("Index greater than length"));
1429 result
= value_subscript (base
, low
);
1436 struct type
*usize
, *slice
;
1438 struct value
*addrval
, *tem
;
1440 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1443 error (_("High index less than zero"));
1445 error (_("Low index greater than high index"));
1446 if (high
> high_bound
)
1447 error (_("High index greater than length"));
1449 usize
= language_lookup_primitive_type (exp
->language_defn
,
1452 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1455 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1456 addr
= value_as_long (addrval
);
1457 tem
= value_at_lazy (slice
, addr
);
1459 value_assign (value_field (tem
, 0), value_addr (result
));
1460 value_assign (value_field (tem
, 1),
1461 value_from_longest (usize
, high
- low
));
1463 result
= value_at_lazy (slice
, addr
);
1466 result
= value_addr (result
);
1472 /* evaluate_exp implementation for Rust. */
1474 static struct value
*
1475 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1476 int *pos
, enum noside noside
)
1478 struct value
*result
;
1480 switch (exp
->elts
[*pos
].opcode
)
1482 case UNOP_COMPLEMENT
:
1484 struct value
*value
;
1487 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1488 if (noside
== EVAL_SKIP
)
1490 /* Preserving the type is enough. */
1493 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1494 result
= value_from_longest (value_type (value
),
1495 value_logical_not (value
));
1497 result
= value_complement (value
);
1501 case BINOP_SUBSCRIPT
:
1502 result
= rust_subscript (exp
, pos
, noside
, 0);
1506 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1512 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1513 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1516 struct value
*addrval
= NULL
;
1520 if (noside
== EVAL_NORMAL
)
1522 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1523 addr
= value_as_long (addrval
);
1524 result
= value_at_lazy (type
, addr
);
1527 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1532 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1533 if (noside
== EVAL_NORMAL
)
1535 /* This isn't quite right but will do for the time
1536 being, seeing that we can't implement the Copy
1538 value_assign (result
, init
);
1544 gdb_assert (arglen
% 2 == 0);
1545 for (i
= 0; i
< arglen
; i
+= 2)
1548 const char *fieldname
;
1549 struct value
*value
, *field
;
1551 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1553 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1555 fieldname
= &exp
->elts
[*pos
].string
;
1556 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1558 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1559 if (noside
== EVAL_NORMAL
)
1561 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1563 value_assign (field
, value
);
1567 if (noside
== EVAL_SKIP
)
1568 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1570 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1571 result
= allocate_value (type
);
1573 result
= value_at_lazy (type
, addr
);
1582 struct value
*ncopies
;
1584 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1585 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1586 copies
= value_as_long (ncopies
);
1588 error (_("Array with negative number of elements"));
1590 if (noside
== EVAL_NORMAL
)
1594 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1595 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1597 for (i
= 0; i
< copies
; ++i
)
1599 result
= value_array (0, copies
- 1, eltvec
);
1601 do_cleanups (cleanup
);
1605 struct type
*arraytype
1606 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1607 result
= allocate_value (arraytype
);
1612 case STRUCTOP_ANONYMOUS
:
1614 /* Anonymous field access, i.e. foo.1. */
1616 int pc
, field_number
, nfields
;
1617 struct type
*type
, *variant_type
;
1618 struct disr_info disr
;
1621 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1623 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1625 type
= value_type (lhs
);
1626 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1628 struct cleanup
*cleanup
;
1630 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1631 value_embedded_offset (lhs
),
1632 value_address (lhs
), lhs
);
1634 cleanup
= make_cleanup (xfree
, disr
.name
);
1636 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1638 variant_type
= NULL
;
1643 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1644 nfields
= TYPE_NFIELDS (variant_type
);
1647 if (!disr
.is_encoded
)
1650 if (field_number
>= nfields
|| field_number
< 0)
1651 error(_("Cannot access field %d of variant %s, \
1652 there are only %d fields"),
1653 disr
.is_encoded
? field_number
: field_number
- 1,
1655 disr
.is_encoded
? nfields
: nfields
- 1);
1657 if (!(disr
.is_encoded
1658 ? rust_tuple_struct_type_p (variant_type
)
1659 : rust_tuple_variant_type_p (variant_type
)))
1660 error(_("Variant %s is not a tuple variant"), disr
.name
);
1662 result
= value_primitive_field (lhs
, 0, field_number
,
1664 do_cleanups (cleanup
);
1666 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1668 /* Tuples and tuple structs */
1669 nfields
= TYPE_NFIELDS(type
);
1671 if (field_number
>= nfields
|| field_number
< 0)
1672 error(_("Cannot access field %d of %s, there are only %d fields"),
1673 field_number
, TYPE_TAG_NAME (type
), nfields
);
1675 /* Tuples are tuple structs too. */
1676 if (!rust_tuple_struct_type_p (type
))
1677 error(_("Attempting to access anonymous field %d of %s, which is \
1678 not a tuple, tuple struct, or tuple-like variant"),
1679 field_number
, TYPE_TAG_NAME (type
));
1681 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1684 error(_("Anonymous field access is only allowed on tuples, \
1685 tuple structs, and tuple-like enum variants"));
1689 case STRUCTOP_STRUCT
:
1696 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1697 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1698 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1700 type
= value_type (lhs
);
1702 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1705 struct disr_info disr
;
1706 struct cleanup
* cleanup
;
1707 struct type
* variant_type
;
1710 field_name
= &exp
->elts
[pc
+ 2].string
;
1712 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1713 value_embedded_offset (lhs
),
1714 value_address (lhs
), lhs
);
1716 cleanup
= make_cleanup (xfree
, disr
.name
);
1718 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1719 error(_("Could not find field %s of struct variant %s"),
1720 field_name
, disr
.name
);
1722 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1724 if (variant_type
== NULL
1725 || rust_tuple_variant_type_p (variant_type
))
1726 error(_("Attempting to access named field %s of tuple variant %s, \
1727 which has only anonymous fields"),
1728 field_name
, disr
.name
);
1730 start
= disr
.is_encoded
? 0 : 1;
1731 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1733 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1735 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1740 if (i
== TYPE_NFIELDS (variant_type
))
1741 /* We didn't find it. */
1742 error(_("Could not find field %s of struct variant %s"),
1743 field_name
, disr
.name
);
1745 do_cleanups (cleanup
);
1750 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1756 result
= rust_range (exp
, pos
, noside
);
1760 /* We might have &array[range], in which case we need to make a
1762 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1765 result
= rust_subscript (exp
, pos
, noside
, 1);
1770 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1777 /* operator_length implementation for Rust. */
1780 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1786 switch (exp
->elts
[pc
- 1].opcode
)
1789 /* We handle aggregate as a type and argument count. The first
1790 argument might be OP_OTHERS. After that the arguments
1791 alternate: first an OP_NAME, then an expression. */
1793 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1801 case STRUCTOP_ANONYMOUS
:
1812 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1820 /* op_name implementation for Rust. */
1823 rust_op_name (enum exp_opcode opcode
)
1828 return "OP_AGGREGATE";
1832 return op_name_standard (opcode
);
1836 /* dump_subexp_body implementation for Rust. */
1839 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1842 switch (exp
->elts
[elt
].opcode
)
1846 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1849 fprintf_filtered (stream
, "Type @");
1850 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1851 fprintf_filtered (stream
, " (");
1852 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1853 fprintf_filtered (stream
, "), length %d", length
);
1856 for (i
= 0; i
< length
; ++i
)
1857 elt
= dump_subexp (exp
, stream
, elt
);
1864 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1866 fprintf_filtered (stream
, "%s: %s",
1867 (exp
->elts
[elt
].opcode
== OP_STRING
1868 ? "string" : "name"),
1869 &exp
->elts
[elt
+ 2].string
);
1870 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1875 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1878 case STRUCTOP_ANONYMOUS
:
1882 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1884 fprintf_filtered (stream
, "Field number: %d", field_number
);
1885 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1893 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1900 /* print_subexp implementation for Rust. */
1903 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1904 enum precedence prec
)
1906 switch (exp
->elts
[*pos
].opcode
)
1910 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1913 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1914 fputs_filtered (" { ", stream
);
1917 for (i
= 0; i
< length
; ++i
)
1919 rust_print_subexp (exp
, pos
, stream
, prec
);
1920 fputs_filtered (", ", stream
);
1922 fputs_filtered (" }", stream
);
1928 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1930 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1931 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1937 fputs_filtered ("<<others>> (", stream
);
1939 rust_print_subexp (exp
, pos
, stream
, prec
);
1940 fputs_filtered (")", stream
);
1944 case STRUCTOP_ANONYMOUS
:
1946 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1949 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1950 fprintf_filtered (stream
, ".%d", tem
);
1956 fprintf_filtered (stream
, "[");
1957 rust_print_subexp (exp
, pos
, stream
, prec
);
1958 fprintf_filtered (stream
, "; ");
1959 rust_print_subexp (exp
, pos
, stream
, prec
);
1960 fprintf_filtered (stream
, "]");
1964 print_subexp_standard (exp
, pos
, stream
, prec
);
1969 /* operator_check implementation for Rust. */
1972 rust_operator_check (struct expression
*exp
, int pos
,
1973 int (*objfile_func
) (struct objfile
*objfile
,
1977 switch (exp
->elts
[pos
].opcode
)
1981 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1982 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1984 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1995 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2003 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2005 static struct block_symbol
2006 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2008 const struct block
*block
,
2009 const domain_enum domain
)
2011 struct block_symbol result
= {NULL
, NULL
};
2013 if (symbol_lookup_debug
)
2015 fprintf_unfiltered (gdb_stdlog
,
2016 "rust_lookup_symbol_non_local"
2017 " (%s, %s (scope %s), %s)\n",
2018 name
, host_address_to_string (block
),
2019 block_scope (block
), domain_name (domain
));
2022 /* Look up bare names in the block's scope. */
2023 if (name
[cp_find_first_component (name
)] == '\0')
2025 const char *scope
= block_scope (block
);
2027 if (scope
[0] != '\0')
2029 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
2030 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
2032 result
= lookup_symbol_in_static_block (scopedname
, block
,
2034 if (result
.symbol
== NULL
)
2035 result
= lookup_global_symbol (scopedname
, block
, domain
);
2036 do_cleanups (cleanup
);
2044 /* la_sniff_from_mangled_name for Rust. */
2047 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2049 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2050 return *demangled
!= NULL
;
2055 static const struct exp_descriptor exp_descriptor_rust
=
2058 rust_operator_length
,
2059 rust_operator_check
,
2061 rust_dump_subexp_body
,
2062 rust_evaluate_subexp
2065 static const char *rust_extensions
[] =
2070 static const struct language_defn rust_language_defn
=
2080 &exp_descriptor_rust
,
2084 rust_printchar
, /* Print a character constant */
2085 rust_printstr
, /* Function to print string constant */
2086 rust_emitchar
, /* Print a single char */
2087 rust_print_type
, /* Print a type using appropriate syntax */
2088 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2089 rust_val_print
, /* Print a value using appropriate syntax */
2090 c_value_print
, /* Print a top-level value */
2091 default_read_var_value
, /* la_read_var_value */
2092 NULL
, /* Language specific skip_trampoline */
2093 NULL
, /* name_of_this */
2094 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2095 basic_lookup_transparent_type
,/* lookup_transparent_type */
2096 gdb_demangle
, /* Language specific symbol demangler */
2097 rust_sniff_from_mangled_name
,
2098 NULL
, /* Language specific
2099 class_name_from_physname */
2100 c_op_print_tab
, /* expression operators for printing */
2101 1, /* c-style arrays */
2102 0, /* String lower bound */
2103 default_word_break_characters
,
2104 default_make_symbol_completion_list
,
2105 rust_language_arch_info
,
2106 default_print_array_index
,
2107 default_pass_by_reference
,
2109 NULL
, /* la_get_symbol_name_cmp */
2110 iterate_over_symbols
,
2111 &default_varobj_ops
,
2118 _initialize_rust_language (void)
2120 add_language (&rust_language_defn
);