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
);
892 fputs_filtered ("{\n", stream
);
894 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
895 strlen (RUST_ENUM_PREFIX
)) == 0)
897 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
898 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
900 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
901 /* There is no explicit discriminant field, skip nothing. */
906 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
908 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
910 = rust_last_path_segment (TYPE_NAME (variant_type
));
912 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
914 if (TYPE_NFIELDS (variant_type
) > skip_to
)
917 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
920 fputs_filtered (is_tuple
? "(" : "{", stream
);
921 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
926 fputs_filtered (", ", stream
);
929 fprintf_filtered (stream
, "%s: ",
930 TYPE_FIELD_NAME (variant_type
, j
));
932 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
933 stream
, show
- 1, level
+ 2,
936 fputs_filtered (is_tuple
? ")" : "}", stream
);
939 fputs_filtered (",\n", stream
);
942 fputs_filtered ("}", stream
);
948 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
954 /* Compute the alignment of the type T. */
957 rust_type_alignment (struct type
*t
)
959 t
= check_typedef (t
);
960 switch (TYPE_CODE (t
))
963 error (_("Could not compute alignment of type"));
972 return TYPE_LENGTH (t
);
974 case TYPE_CODE_ARRAY
:
975 case TYPE_CODE_COMPLEX
:
976 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
978 case TYPE_CODE_STRUCT
:
979 case TYPE_CODE_UNION
:
984 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
986 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
995 /* Like arch_composite_type, but uses TYPE to decide how to allocate
996 -- either on an obstack or on a gdbarch. */
999 rust_composite_type (struct type
*original
,
1001 const char *field1
, struct type
*type1
,
1002 const char *field2
, struct type
*type2
)
1004 struct type
*result
= alloc_type_copy (original
);
1005 int i
, nfields
, bitpos
;
1013 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1014 TYPE_NAME (result
) = name
;
1015 TYPE_TAG_NAME (result
) = name
;
1017 TYPE_NFIELDS (result
) = nfields
;
1018 TYPE_FIELDS (result
)
1019 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1025 struct field
*field
= &TYPE_FIELD (result
, i
);
1027 SET_FIELD_BITPOS (*field
, bitpos
);
1028 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1030 FIELD_NAME (*field
) = field1
;
1031 FIELD_TYPE (*field
) = type1
;
1036 struct field
*field
= &TYPE_FIELD (result
, i
);
1037 int align
= rust_type_alignment (type2
);
1043 align
*= TARGET_CHAR_BIT
;
1044 delta
= bitpos
% align
;
1046 bitpos
+= align
- delta
;
1048 SET_FIELD_BITPOS (*field
, bitpos
);
1050 FIELD_NAME (*field
) = field2
;
1051 FIELD_TYPE (*field
) = type2
;
1056 TYPE_LENGTH (result
)
1057 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1058 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1062 /* See rust-lang.h. */
1065 rust_slice_type (const char *name
, struct type
*elt_type
,
1066 struct type
*usize_type
)
1070 elt_type
= lookup_pointer_type (elt_type
);
1071 type
= rust_composite_type (elt_type
, name
,
1072 "data_ptr", elt_type
,
1073 "length", usize_type
);
1078 enum rust_primitive_types
1080 rust_primitive_bool
,
1081 rust_primitive_char
,
1090 rust_primitive_isize
,
1091 rust_primitive_usize
,
1094 rust_primitive_unit
,
1096 nr_rust_primitive_types
1099 /* la_language_arch_info implementation for Rust. */
1102 rust_language_arch_info (struct gdbarch
*gdbarch
,
1103 struct language_arch_info
*lai
)
1105 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1107 struct type
**types
;
1108 unsigned int length
;
1110 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1113 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1114 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1115 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1116 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1117 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1118 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1119 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1120 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1121 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1122 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1124 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1125 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1126 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1128 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1129 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1131 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1133 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1134 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1135 types
[rust_primitive_usize
]);
1137 lai
->primitive_type_vector
= types
;
1138 lai
->bool_type_default
= types
[rust_primitive_bool
];
1139 lai
->string_char_type
= types
[rust_primitive_u8
];
1144 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1146 static struct value
*
1147 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1150 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1153 struct value
*function
, *result
, *arg0
;
1154 struct value
**args
;
1155 struct cleanup
*cleanup
;
1156 struct type
*type
, *fn_type
;
1157 const struct block
*block
;
1158 struct block_symbol sym
;
1160 /* For an ordinary function call we can simply defer to the
1161 generic implementation. */
1162 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1163 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1165 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1167 method
= &exp
->elts
[*pos
+ 1].string
;
1168 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1170 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1171 type in order to look up the method. */
1172 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1174 if (noside
== EVAL_SKIP
)
1176 for (i
= 0; i
< num_args
; ++i
)
1177 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1181 args
= XNEWVEC (struct value
*, num_args
+ 1);
1182 cleanup
= make_cleanup (xfree
, args
);
1185 /* We don't yet implement real Deref semantics. */
1186 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1187 args
[0] = value_ind (args
[0]);
1189 type
= value_type (args
[0]);
1190 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1191 && TYPE_CODE (type
) != TYPE_CODE_UNION
1192 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1193 || rust_tuple_type_p (type
))
1194 error (_("Method calls only supported on struct or enum types"));
1195 if (TYPE_TAG_NAME (type
) == NULL
)
1196 error (_("Method call on nameless type"));
1198 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1199 make_cleanup (xfree
, name
);
1201 block
= get_selected_block (0);
1202 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1203 if (sym
.symbol
== NULL
)
1204 error (_("Could not find function named '%s'"), name
);
1206 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1207 if (TYPE_NFIELDS (fn_type
) == 0)
1208 error (_("Function '%s' takes no arguments"), name
);
1210 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1211 args
[0] = value_addr (args
[0]);
1213 function
= address_of_variable (sym
.symbol
, block
);
1215 for (i
= 0; i
< num_args
; ++i
)
1216 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1218 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1219 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1221 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1222 do_cleanups (cleanup
);
1226 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1228 static struct value
*
1229 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1231 enum range_type kind
;
1232 struct value
*low
= NULL
, *high
= NULL
;
1233 struct value
*addrval
, *result
;
1235 struct type
*range_type
;
1236 struct type
*index_type
;
1237 struct type
*temp_type
;
1240 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1243 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1244 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1245 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1246 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1248 if (noside
== EVAL_SKIP
)
1249 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1256 name
= "std::ops::RangeFull";
1260 index_type
= value_type (high
);
1261 name
= "std::ops::RangeTo";
1268 index_type
= value_type (low
);
1269 name
= "std::ops::RangeFrom";
1273 if (!types_equal (value_type (low
), value_type (high
)))
1274 error (_("Range expression with different types"));
1275 index_type
= value_type (low
);
1276 name
= "std::ops::Range";
1280 /* If we don't have an index type, just allocate this on the
1281 arch. Here any type will do. */
1282 temp_type
= (index_type
== NULL
1283 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1285 /* It would be nicer to cache the range type. */
1286 range_type
= rust_composite_type (temp_type
, name
,
1287 low
== NULL
? NULL
: "start", index_type
,
1288 high
== NULL
? NULL
: "end", index_type
);
1290 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1291 return value_zero (range_type
, lval_memory
);
1293 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1294 addr
= value_as_long (addrval
);
1295 result
= value_at_lazy (range_type
, addr
);
1299 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1302 value_assign (start
, low
);
1307 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1310 value_assign (end
, high
);
1313 result
= value_at_lazy (range_type
, addr
);
1317 /* A helper function to compute the range and kind given a range
1318 value. TYPE is the type of the range value. RANGE is the range
1319 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1320 parameters might be filled in, or might not be, depending on the
1321 kind of range this is. KIND will always be set to the appropriate
1322 value describing the kind of range, and this can be used to
1323 determine whether LOW or HIGH are valid. */
1326 rust_compute_range (struct type
*type
, struct value
*range
,
1327 LONGEST
*low
, LONGEST
*high
,
1328 enum range_type
*kind
)
1334 *kind
= BOTH_BOUND_DEFAULT
;
1336 if (TYPE_NFIELDS (type
) == 0)
1340 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1342 *kind
= HIGH_BOUND_DEFAULT
;
1343 *low
= value_as_long (value_field (range
, 0));
1346 if (TYPE_NFIELDS (type
) > i
1347 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1349 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1350 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1351 *high
= value_as_long (value_field (range
, i
));
1355 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1357 static struct value
*
1358 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1361 struct value
*lhs
, *rhs
, *result
;
1362 struct type
*rhstype
;
1363 LONGEST low
, high_bound
;
1364 /* Initialized to appease the compiler. */
1365 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1370 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1371 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1373 if (noside
== EVAL_SKIP
)
1376 rhstype
= check_typedef (value_type (rhs
));
1377 if (rust_range_type_p (rhstype
))
1380 error (_("Can't take slice of array without '&'"));
1381 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1385 low
= value_as_long (rhs
);
1387 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1389 struct type
*type
= check_typedef (value_type (lhs
));
1391 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1397 struct type
*type
= check_typedef (value_type (lhs
));
1399 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1402 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1403 error (_("Can't compute array bounds"));
1405 error (_("Found array with non-zero lower bound"));
1408 else if (rust_slice_type_p (type
))
1412 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1413 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1415 high_bound
= value_as_long (len
);
1417 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1421 high_bound
= LONGEST_MAX
;
1424 error (_("Cannot subscript non-array type"));
1427 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1430 error (_("Index less than zero"));
1431 if (low
> high_bound
)
1432 error (_("Index greater than length"));
1434 result
= value_subscript (base
, low
);
1441 struct type
*usize
, *slice
;
1443 struct value
*addrval
, *tem
;
1445 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1448 error (_("High index less than zero"));
1450 error (_("Low index greater than high index"));
1451 if (high
> high_bound
)
1452 error (_("High index greater than length"));
1454 usize
= language_lookup_primitive_type (exp
->language_defn
,
1457 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1460 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1461 addr
= value_as_long (addrval
);
1462 tem
= value_at_lazy (slice
, addr
);
1464 value_assign (value_field (tem
, 0), value_addr (result
));
1465 value_assign (value_field (tem
, 1),
1466 value_from_longest (usize
, high
- low
));
1468 result
= value_at_lazy (slice
, addr
);
1471 result
= value_addr (result
);
1477 /* evaluate_exp implementation for Rust. */
1479 static struct value
*
1480 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1481 int *pos
, enum noside noside
)
1483 struct value
*result
;
1485 switch (exp
->elts
[*pos
].opcode
)
1487 case UNOP_COMPLEMENT
:
1489 struct value
*value
;
1492 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1493 if (noside
== EVAL_SKIP
)
1495 /* Preserving the type is enough. */
1498 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1499 result
= value_from_longest (value_type (value
),
1500 value_logical_not (value
));
1502 result
= value_complement (value
);
1506 case BINOP_SUBSCRIPT
:
1507 result
= rust_subscript (exp
, pos
, noside
, 0);
1511 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1517 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1518 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1521 struct value
*addrval
= NULL
;
1525 if (noside
== EVAL_NORMAL
)
1527 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1528 addr
= value_as_long (addrval
);
1529 result
= value_at_lazy (type
, addr
);
1532 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1537 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1538 if (noside
== EVAL_NORMAL
)
1540 /* This isn't quite right but will do for the time
1541 being, seeing that we can't implement the Copy
1543 value_assign (result
, init
);
1549 gdb_assert (arglen
% 2 == 0);
1550 for (i
= 0; i
< arglen
; i
+= 2)
1553 const char *fieldname
;
1554 struct value
*value
, *field
;
1556 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1558 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1560 fieldname
= &exp
->elts
[*pos
].string
;
1561 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1563 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1564 if (noside
== EVAL_NORMAL
)
1566 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1568 value_assign (field
, value
);
1572 if (noside
== EVAL_SKIP
)
1573 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1575 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1576 result
= allocate_value (type
);
1578 result
= value_at_lazy (type
, addr
);
1587 struct value
*ncopies
;
1589 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1590 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1591 copies
= value_as_long (ncopies
);
1593 error (_("Array with negative number of elements"));
1595 if (noside
== EVAL_NORMAL
)
1599 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1600 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1602 for (i
= 0; i
< copies
; ++i
)
1604 result
= value_array (0, copies
- 1, eltvec
);
1606 do_cleanups (cleanup
);
1610 struct type
*arraytype
1611 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1612 result
= allocate_value (arraytype
);
1617 case STRUCTOP_ANONYMOUS
:
1619 /* Anonymous field access, i.e. foo.1. */
1621 int pc
, field_number
, nfields
;
1622 struct type
*type
, *variant_type
;
1623 struct disr_info disr
;
1626 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1628 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1630 type
= value_type (lhs
);
1631 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1633 struct cleanup
*cleanup
;
1635 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1636 value_embedded_offset (lhs
),
1637 value_address (lhs
), lhs
);
1639 cleanup
= make_cleanup (xfree
, disr
.name
);
1641 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1643 variant_type
= NULL
;
1648 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1649 nfields
= TYPE_NFIELDS (variant_type
);
1652 if (!disr
.is_encoded
)
1655 if (field_number
>= nfields
|| field_number
< 0)
1656 error(_("Cannot access field %d of variant %s, \
1657 there are only %d fields"),
1658 disr
.is_encoded
? field_number
: field_number
- 1,
1660 disr
.is_encoded
? nfields
: nfields
- 1);
1662 if (!(disr
.is_encoded
1663 ? rust_tuple_struct_type_p (variant_type
)
1664 : rust_tuple_variant_type_p (variant_type
)))
1665 error(_("Variant %s is not a tuple variant"), disr
.name
);
1667 result
= value_primitive_field (lhs
, 0, field_number
,
1669 do_cleanups (cleanup
);
1671 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1673 /* Tuples and tuple structs */
1674 nfields
= TYPE_NFIELDS(type
);
1676 if (field_number
>= nfields
|| field_number
< 0)
1677 error(_("Cannot access field %d of %s, there are only %d fields"),
1678 field_number
, TYPE_TAG_NAME (type
), nfields
);
1680 /* Tuples are tuple structs too. */
1681 if (!rust_tuple_struct_type_p (type
))
1682 error(_("Attempting to access anonymous field %d of %s, which is \
1683 not a tuple, tuple struct, or tuple-like variant"),
1684 field_number
, TYPE_TAG_NAME (type
));
1686 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1689 error(_("Anonymous field access is only allowed on tuples, \
1690 tuple structs, and tuple-like enum variants"));
1694 case STRUCTOP_STRUCT
:
1701 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1702 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1703 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1705 type
= value_type (lhs
);
1707 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1710 struct disr_info disr
;
1711 struct cleanup
* cleanup
;
1712 struct type
* variant_type
;
1715 field_name
= &exp
->elts
[pc
+ 2].string
;
1717 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1718 value_embedded_offset (lhs
),
1719 value_address (lhs
), lhs
);
1721 cleanup
= make_cleanup (xfree
, disr
.name
);
1723 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1724 error(_("Could not find field %s of struct variant %s"),
1725 field_name
, disr
.name
);
1727 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1729 if (variant_type
== NULL
1730 || rust_tuple_variant_type_p (variant_type
))
1731 error(_("Attempting to access named field %s of tuple variant %s, \
1732 which has only anonymous fields"),
1733 field_name
, disr
.name
);
1735 start
= disr
.is_encoded
? 0 : 1;
1736 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1738 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1740 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1745 if (i
== TYPE_NFIELDS (variant_type
))
1746 /* We didn't find it. */
1747 error(_("Could not find field %s of struct variant %s"),
1748 field_name
, disr
.name
);
1750 do_cleanups (cleanup
);
1755 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1761 result
= rust_range (exp
, pos
, noside
);
1765 /* We might have &array[range], in which case we need to make a
1767 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1770 result
= rust_subscript (exp
, pos
, noside
, 1);
1775 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1782 /* operator_length implementation for Rust. */
1785 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1791 switch (exp
->elts
[pc
- 1].opcode
)
1794 /* We handle aggregate as a type and argument count. The first
1795 argument might be OP_OTHERS. After that the arguments
1796 alternate: first an OP_NAME, then an expression. */
1798 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1806 case STRUCTOP_ANONYMOUS
:
1817 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1825 /* op_name implementation for Rust. */
1828 rust_op_name (enum exp_opcode opcode
)
1833 return "OP_AGGREGATE";
1837 return op_name_standard (opcode
);
1841 /* dump_subexp_body implementation for Rust. */
1844 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1847 switch (exp
->elts
[elt
].opcode
)
1851 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1854 fprintf_filtered (stream
, "Type @");
1855 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1856 fprintf_filtered (stream
, " (");
1857 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1858 fprintf_filtered (stream
, "), length %d", length
);
1861 for (i
= 0; i
< length
; ++i
)
1862 elt
= dump_subexp (exp
, stream
, elt
);
1869 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1871 fprintf_filtered (stream
, "%s: %s",
1872 (exp
->elts
[elt
].opcode
== OP_STRING
1873 ? "string" : "name"),
1874 &exp
->elts
[elt
+ 2].string
);
1875 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1880 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1883 case STRUCTOP_ANONYMOUS
:
1887 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1889 fprintf_filtered (stream
, "Field number: %d", field_number
);
1890 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1898 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1905 /* print_subexp implementation for Rust. */
1908 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1909 enum precedence prec
)
1911 switch (exp
->elts
[*pos
].opcode
)
1915 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1918 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1919 fputs_filtered (" { ", stream
);
1922 for (i
= 0; i
< length
; ++i
)
1924 rust_print_subexp (exp
, pos
, stream
, prec
);
1925 fputs_filtered (", ", stream
);
1927 fputs_filtered (" }", stream
);
1933 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1935 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1936 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1942 fputs_filtered ("<<others>> (", stream
);
1944 rust_print_subexp (exp
, pos
, stream
, prec
);
1945 fputs_filtered (")", stream
);
1949 case STRUCTOP_ANONYMOUS
:
1951 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1954 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1955 fprintf_filtered (stream
, ".%d", tem
);
1961 fprintf_filtered (stream
, "[");
1962 rust_print_subexp (exp
, pos
, stream
, prec
);
1963 fprintf_filtered (stream
, "; ");
1964 rust_print_subexp (exp
, pos
, stream
, prec
);
1965 fprintf_filtered (stream
, "]");
1969 print_subexp_standard (exp
, pos
, stream
, prec
);
1974 /* operator_check implementation for Rust. */
1977 rust_operator_check (struct expression
*exp
, int pos
,
1978 int (*objfile_func
) (struct objfile
*objfile
,
1982 switch (exp
->elts
[pos
].opcode
)
1986 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1987 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1989 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2000 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2008 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2010 static struct block_symbol
2011 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2013 const struct block
*block
,
2014 const domain_enum domain
)
2016 struct block_symbol result
= {NULL
, NULL
};
2018 if (symbol_lookup_debug
)
2020 fprintf_unfiltered (gdb_stdlog
,
2021 "rust_lookup_symbol_non_local"
2022 " (%s, %s (scope %s), %s)\n",
2023 name
, host_address_to_string (block
),
2024 block_scope (block
), domain_name (domain
));
2027 /* Look up bare names in the block's scope. */
2028 if (name
[cp_find_first_component (name
)] == '\0')
2030 const char *scope
= block_scope (block
);
2032 if (scope
[0] != '\0')
2034 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
2035 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
2037 result
= lookup_symbol_in_static_block (scopedname
, block
,
2039 if (result
.symbol
== NULL
)
2040 result
= lookup_global_symbol (scopedname
, block
, domain
);
2041 do_cleanups (cleanup
);
2049 /* la_sniff_from_mangled_name for Rust. */
2052 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2054 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2055 return *demangled
!= NULL
;
2060 static const struct exp_descriptor exp_descriptor_rust
=
2063 rust_operator_length
,
2064 rust_operator_check
,
2066 rust_dump_subexp_body
,
2067 rust_evaluate_subexp
2070 static const char *rust_extensions
[] =
2075 static const struct language_defn rust_language_defn
=
2085 &exp_descriptor_rust
,
2089 rust_printchar
, /* Print a character constant */
2090 rust_printstr
, /* Function to print string constant */
2091 rust_emitchar
, /* Print a single char */
2092 rust_print_type
, /* Print a type using appropriate syntax */
2093 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2094 rust_val_print
, /* Print a value using appropriate syntax */
2095 c_value_print
, /* Print a top-level value */
2096 default_read_var_value
, /* la_read_var_value */
2097 NULL
, /* Language specific skip_trampoline */
2098 NULL
, /* name_of_this */
2099 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2100 basic_lookup_transparent_type
,/* lookup_transparent_type */
2101 gdb_demangle
, /* Language specific symbol demangler */
2102 rust_sniff_from_mangled_name
,
2103 NULL
, /* Language specific
2104 class_name_from_physname */
2105 c_op_print_tab
, /* expression operators for printing */
2106 1, /* c-style arrays */
2107 0, /* String lower bound */
2108 default_word_break_characters
,
2109 default_make_symbol_completion_list
,
2110 rust_language_arch_info
,
2111 default_print_array_index
,
2112 default_pass_by_reference
,
2114 NULL
, /* la_get_symbol_name_cmp */
2115 iterate_over_symbols
,
2116 &default_varobj_ops
,
2123 _initialize_rust_language (void)
2125 add_language (&rust_language_defn
);