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 *name
, *tail
, *token
;
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 while ((token
= strsep (&tail
, "$")) != NULL
)
150 if (sscanf (token
, "%lu", &fieldno
) != 1)
152 /* We have reached the enum name,
153 which cannot start with a digit. */
156 if (fieldno
>= TYPE_NFIELDS (member_type
))
157 error (_("%s refers to field after end of member type"),
160 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
161 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
164 if (token
>= name
+ strlen (TYPE_FIELD_NAME (type
, 0)))
165 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
166 value
= unpack_long (member_type
,
167 valaddr
+ embedded_offset
);
173 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
174 ret
.name
= concat (TYPE_NAME (type
), "::", token
, (char *) NULL
);
178 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
179 ret
.name
= concat (TYPE_NAME (type
), "::",
180 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))),
184 do_cleanups (cleanup
);
188 disr_type
= TYPE_FIELD_TYPE (type
, 0);
190 if (TYPE_NFIELDS (disr_type
) == 0)
192 /* This is a bounds check and should never be hit unless Rust
193 has changed its debuginfo format. */
194 error (_("Could not find enum discriminant field"));
197 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
198 error (_("Rust debug format has changed"));
200 temp_file
= mem_fileopen ();
201 cleanup
= make_cleanup_ui_file_delete (temp_file
);
202 /* The first value of the first field (or any field)
203 is the discriminant value. */
204 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
205 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
206 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
210 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
211 name_segment
= rust_last_path_segment (ret
.name
);
212 if (name_segment
!= NULL
)
214 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
216 /* Sadly, the discriminant value paths do not match the type
217 field name paths ('core::option::Option::Some' vs
218 'core::option::Some'). However, enum variant names are
219 unique in the last path segment and the generics are not
220 part of this path, so we can just compare those. This is
221 hackish and would be better fixed by improving rustc's
222 metadata for enums. */
223 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
225 if (field_type
!= NULL
226 && strcmp (name_segment
,
227 rust_last_path_segment (field_type
)) == 0)
235 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
237 /* Somehow the discriminant wasn't found. */
238 make_cleanup (xfree
, ret
.name
);
239 error (_("Could not find variant of %s with discriminant %s"),
240 TYPE_TAG_NAME (type
), ret
.name
);
243 do_cleanups (cleanup
);
247 /* See rust-lang.h. */
250 rust_tuple_type_p (struct type
*type
)
252 /* The current implementation is a bit of a hack, but there's
253 nothing else in the debuginfo to distinguish a tuple from a
255 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
256 && TYPE_TAG_NAME (type
) != NULL
257 && TYPE_TAG_NAME (type
)[0] == '(');
261 /* Return true if all non-static fields of a structlike type are in a
262 sequence like __0, __1, __2. OFFSET lets us skip fields. */
265 rust_underscore_fields (struct type
*type
, int offset
)
271 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
273 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
275 if (!field_is_static (&TYPE_FIELD (type
, i
)))
283 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
284 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
293 /* See rust-lang.h. */
296 rust_tuple_struct_type_p (struct type
*type
)
298 return rust_underscore_fields (type
, 0);
301 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
304 rust_tuple_variant_type_p (struct type
*type
)
306 /* First field is discriminant */
307 return rust_underscore_fields (type
, 1);
310 /* Return true if TYPE is a slice type, otherwise false. */
313 rust_slice_type_p (struct type
*type
)
315 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
316 && TYPE_TAG_NAME (type
) != NULL
317 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
320 /* Return true if TYPE is a range type, otherwise false. */
323 rust_range_type_p (struct type
*type
)
327 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
328 || TYPE_NFIELDS (type
) > 2
329 || TYPE_TAG_NAME (type
) == NULL
330 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
333 if (TYPE_NFIELDS (type
) == 0)
337 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
339 if (TYPE_NFIELDS (type
) == 1)
343 else if (TYPE_NFIELDS (type
) == 2)
345 /* First field had to be "start". */
349 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
352 /* Return true if TYPE seems to be the type "u8", otherwise false. */
355 rust_u8_type_p (struct type
*type
)
357 return (TYPE_CODE (type
) == TYPE_CODE_INT
358 && TYPE_UNSIGNED (type
)
359 && TYPE_LENGTH (type
) == 1);
362 /* Return true if TYPE is a Rust character type. */
365 rust_chartype_p (struct type
*type
)
367 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
368 && TYPE_LENGTH (type
) == 4
369 && TYPE_UNSIGNED (type
));
374 /* la_emitchar implementation for Rust. */
377 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
379 if (!rust_chartype_p (type
))
380 generic_emit_char (c
, type
, stream
, quoter
,
381 target_charset (get_type_arch (type
)));
382 else if (c
== '\\' || c
== quoter
)
383 fprintf_filtered (stream
, "\\%c", c
);
385 fputs_filtered ("\\n", stream
);
387 fputs_filtered ("\\r", stream
);
389 fputs_filtered ("\\t", stream
);
391 fputs_filtered ("\\0", stream
);
392 else if (c
>= 32 && c
<= 127 && isprint (c
))
393 fputc_filtered (c
, stream
);
395 fprintf_filtered (stream
, "\\x%02x", c
);
397 fprintf_filtered (stream
, "\\u{%06x}", c
);
400 /* la_printchar implementation for Rust. */
403 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
405 fputs_filtered ("'", stream
);
406 LA_EMIT_CHAR (c
, type
, stream
, '\'');
407 fputs_filtered ("'", stream
);
410 /* la_printstr implementation for Rust. */
413 rust_printstr (struct ui_file
*stream
, struct type
*type
,
414 const gdb_byte
*string
, unsigned int length
,
415 const char *user_encoding
, int force_ellipses
,
416 const struct value_print_options
*options
)
418 /* Rust always uses UTF-8, but let the caller override this if need
420 const char *encoding
= user_encoding
;
421 if (user_encoding
== NULL
|| !*user_encoding
)
423 /* In Rust strings, characters are "u8". */
424 if (rust_u8_type_p (type
))
428 /* This is probably some C string, so let's let C deal with
430 c_printstr (stream
, type
, string
, length
, user_encoding
,
431 force_ellipses
, options
);
436 /* This is not ideal as it doesn't use our character printer. */
437 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
443 static const struct generic_val_print_decorations rust_decorations
=
445 /* Complex isn't used in Rust, but we provide C-ish values just in
457 /* la_val_print implementation for Rust. */
460 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
461 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
462 const struct value
*val
,
463 const struct value_print_options
*options
)
465 type
= check_typedef (type
);
466 switch (TYPE_CODE (type
))
470 LONGEST low_bound
, high_bound
;
472 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
473 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
474 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
476 /* We have a pointer to a byte string, so just print
478 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
480 struct gdbarch
*arch
= get_type_arch (type
);
481 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
483 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
484 if (options
->addressprint
)
486 fputs_filtered (paddress (arch
, addr
), stream
);
487 fputs_filtered (" ", stream
);
490 fputs_filtered ("b", stream
);
491 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
492 high_bound
- low_bound
+ 1, stream
,
499 case TYPE_CODE_METHODPTR
:
500 case TYPE_CODE_MEMBERPTR
:
501 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
502 recurse
, val
, options
);
506 /* Recognize the unit type. */
507 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
508 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
510 fputs_filtered ("()", stream
);
515 case TYPE_CODE_STRING
:
517 struct gdbarch
*arch
= get_type_arch (type
);
518 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
519 LONGEST low_bound
, high_bound
;
521 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
522 error (_("Could not determine the array bounds"));
524 /* If we see a plain TYPE_CODE_STRING, then we're printing a
525 byte string, hence the choice of "ASCII" as the
527 fputs_filtered ("b", stream
);
528 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
529 valaddr
+ embedded_offset
* unit_size
,
530 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
534 case TYPE_CODE_ARRAY
:
536 LONGEST low_bound
, high_bound
;
538 if (get_array_bounds (type
, &low_bound
, &high_bound
)
539 && high_bound
- low_bound
+ 1 == 0)
540 fputs_filtered ("[]", stream
);
546 case TYPE_CODE_UNION
:
548 int j
, nfields
, first_field
, is_tuple
, start
;
549 struct type
*variant_type
;
550 struct disr_info disr
;
551 struct value_print_options opts
;
552 struct cleanup
*cleanup
;
557 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
559 cleanup
= make_cleanup (xfree
, disr
.name
);
561 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
563 fprintf_filtered (stream
, "%s", disr
.name
);
568 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
569 nfields
= TYPE_NFIELDS (variant_type
);
571 is_tuple
= (disr
.is_encoded
572 ? rust_tuple_struct_type_p (variant_type
)
573 : rust_tuple_variant_type_p (variant_type
));
574 start
= disr
.is_encoded
? 0 : 1;
578 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
580 fprintf_filtered (stream
, "%s(", disr
.name
);
583 /* struct variant. */
584 fprintf_filtered (stream
, "%s{", disr
.name
);
589 /* In case of a nullary variant like 'None', just output
591 fprintf_filtered (stream
, "%s", disr
.name
);
595 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
598 fputs_filtered (", ", stream
);
602 fprintf_filtered (stream
, "%s: ",
603 TYPE_FIELD_NAME (variant_type
, j
));
605 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
608 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
609 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
611 stream
, recurse
+ 1, val
, &opts
,
616 fputs_filtered (")", stream
);
618 fputs_filtered ("}", stream
);
621 do_cleanups (cleanup
);
625 case TYPE_CODE_STRUCT
:
629 int is_tuple
= rust_tuple_type_p (type
);
630 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
631 struct value_print_options opts
;
635 if (TYPE_TAG_NAME (type
) != NULL
)
636 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
638 if (TYPE_NFIELDS (type
) == 0)
641 if (TYPE_TAG_NAME (type
) != NULL
)
642 fputs_filtered (" ", stream
);
645 if (is_tuple
|| is_tuple_struct
)
646 fputs_filtered ("(", stream
);
648 fputs_filtered ("{", stream
);
654 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
656 if (field_is_static (&TYPE_FIELD (type
, i
)))
660 fputs_filtered (",", stream
);
662 if (options
->prettyformat
)
664 fputs_filtered ("\n", stream
);
665 print_spaces_filtered (2 + 2 * recurse
, stream
);
667 else if (!first_field
)
668 fputs_filtered (" ", stream
);
672 if (!is_tuple
&& !is_tuple_struct
)
674 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
675 fputs_filtered (": ", stream
);
678 val_print (TYPE_FIELD_TYPE (type
, i
),
680 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
682 stream
, recurse
+ 1, val
, &opts
,
686 if (options
->prettyformat
)
688 fputs_filtered ("\n", stream
);
689 print_spaces_filtered (2 * recurse
, stream
);
692 if (is_tuple
|| is_tuple_struct
)
693 fputs_filtered (")", stream
);
695 fputs_filtered ("}", stream
);
701 /* Nothing special yet. */
702 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
703 recurse
, val
, options
, &rust_decorations
);
709 /* la_print_typedef implementation for Rust. */
712 rust_print_typedef (struct type
*type
,
713 struct symbol
*new_symbol
,
714 struct ui_file
*stream
)
716 type
= check_typedef (type
);
717 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
718 type_print (type
, "", stream
, 0);
719 fprintf_filtered (stream
, ";\n");
722 /* la_print_type implementation for Rust. */
725 rust_print_type (struct type
*type
, const char *varstring
,
726 struct ui_file
*stream
, int show
, int level
,
727 const struct type_print_options
*flags
)
733 && TYPE_NAME (type
) != NULL
)
735 fputs_filtered (TYPE_NAME (type
), stream
);
739 type
= check_typedef (type
);
740 switch (TYPE_CODE (type
))
743 /* Delegate varargs to the C printer. */
744 if (TYPE_VARARGS (type
))
747 fputs_filtered ("fn ", stream
);
748 if (varstring
!= NULL
)
749 fputs_filtered (varstring
, stream
);
750 fputs_filtered ("(", stream
);
751 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
755 fputs_filtered (", ", stream
);
756 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
759 fputs_filtered (") -> ", stream
);
760 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
763 case TYPE_CODE_ARRAY
:
765 LONGEST low_bound
, high_bound
;
767 fputs_filtered ("[", stream
);
768 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
769 stream
, show
- 1, level
, flags
);
770 fputs_filtered ("; ", stream
);
772 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
773 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
774 fprintf_filtered (stream
, "variable length");
775 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
776 fprintf_filtered (stream
, "%s",
777 plongest (high_bound
- low_bound
+ 1));
778 fputs_filtered ("]", stream
);
782 case TYPE_CODE_STRUCT
:
786 /* Print a tuple type simply. */
787 if (rust_tuple_type_p (type
))
789 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
793 /* If we see a base class, delegate to C. */
794 if (TYPE_N_BASECLASSES (type
) > 0)
797 fputs_filtered ("struct ", stream
);
798 if (TYPE_TAG_NAME (type
) != NULL
)
799 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
801 is_tuple_struct
= rust_tuple_struct_type_p (type
);
803 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
805 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
807 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
812 if (field_is_static (&TYPE_FIELD (type
, i
)))
815 /* We'd like to print "pub" here as needed, but rustc
816 doesn't emit the debuginfo, and our types don't have
817 cplus_struct_type attached. */
819 /* For a tuple struct we print the type but nothing
821 print_spaces_filtered (level
+ 2, stream
);
822 if (!is_tuple_struct
)
823 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
825 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
826 stream
, show
- 1, level
+ 2,
828 fputs_filtered (",\n", stream
);
831 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
839 fputs_filtered ("enum ", stream
);
840 if (TYPE_TAG_NAME (type
) != NULL
)
842 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
843 fputs_filtered (" ", stream
);
844 len
= strlen (TYPE_TAG_NAME (type
));
846 fputs_filtered ("{\n", stream
);
848 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
850 const char *name
= TYPE_FIELD_NAME (type
, i
);
855 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
857 && name
[len
+ 1] == ':')
859 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
862 fputs_filtered ("}", stream
);
866 case TYPE_CODE_UNION
:
870 /* Skip the discriminant field. */
873 fputs_filtered ("enum ", stream
);
874 if (TYPE_TAG_NAME (type
) != NULL
)
876 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
877 fputs_filtered (" ", stream
);
878 len
= strlen (TYPE_TAG_NAME (type
));
880 fputs_filtered ("{\n", stream
);
882 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
883 strlen (RUST_ENUM_PREFIX
)) == 0) {
884 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
885 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
887 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+1);
888 /* There is no explicit discriminant field, skip nothing. */
893 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
895 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
897 = rust_last_path_segment (TYPE_NAME (variant_type
));
899 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
901 if (TYPE_NFIELDS (variant_type
) > skip_to
)
904 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
907 fputs_filtered (is_tuple
? "(" : "{", stream
);
908 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
913 fputs_filtered (", ", stream
);
916 fprintf_filtered (stream
, "%s: ",
917 TYPE_FIELD_NAME (variant_type
, j
));
919 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
920 stream
, show
- 1, level
+ 2,
923 fputs_filtered (is_tuple
? ")" : "}", stream
);
926 fputs_filtered (",\n", stream
);
929 fputs_filtered ("}", stream
);
935 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
941 /* Compute the alignment of the type T. */
944 rust_type_alignment (struct type
*t
)
946 t
= check_typedef (t
);
947 switch (TYPE_CODE (t
))
950 error (_("Could not compute alignment of type"));
959 return TYPE_LENGTH (t
);
961 case TYPE_CODE_ARRAY
:
962 case TYPE_CODE_COMPLEX
:
963 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
965 case TYPE_CODE_STRUCT
:
966 case TYPE_CODE_UNION
:
971 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
973 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
982 /* Like arch_composite_type, but uses TYPE to decide how to allocate
983 -- either on an obstack or on a gdbarch. */
986 rust_composite_type (struct type
*original
,
988 const char *field1
, struct type
*type1
,
989 const char *field2
, struct type
*type2
)
991 struct type
*result
= alloc_type_copy (original
);
992 int i
, nfields
, bitpos
;
1000 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1001 TYPE_NAME (result
) = name
;
1002 TYPE_TAG_NAME (result
) = name
;
1004 TYPE_NFIELDS (result
) = nfields
;
1005 TYPE_FIELDS (result
)
1006 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1012 struct field
*field
= &TYPE_FIELD (result
, i
);
1014 SET_FIELD_BITPOS (*field
, bitpos
);
1015 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1017 FIELD_NAME (*field
) = field1
;
1018 FIELD_TYPE (*field
) = type1
;
1023 struct field
*field
= &TYPE_FIELD (result
, i
);
1024 int align
= rust_type_alignment (type2
);
1030 align
*= TARGET_CHAR_BIT
;
1031 delta
= bitpos
% align
;
1033 bitpos
+= align
- delta
;
1035 SET_FIELD_BITPOS (*field
, bitpos
);
1037 FIELD_NAME (*field
) = field2
;
1038 FIELD_TYPE (*field
) = type2
;
1043 TYPE_LENGTH (result
)
1044 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1045 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1049 /* See rust-lang.h. */
1052 rust_slice_type (const char *name
, struct type
*elt_type
,
1053 struct type
*usize_type
)
1057 elt_type
= lookup_pointer_type (elt_type
);
1058 type
= rust_composite_type (elt_type
, name
,
1059 "data_ptr", elt_type
,
1060 "length", usize_type
);
1065 enum rust_primitive_types
1067 rust_primitive_bool
,
1068 rust_primitive_char
,
1077 rust_primitive_isize
,
1078 rust_primitive_usize
,
1081 rust_primitive_unit
,
1083 nr_rust_primitive_types
1086 /* la_language_arch_info implementation for Rust. */
1089 rust_language_arch_info (struct gdbarch
*gdbarch
,
1090 struct language_arch_info
*lai
)
1092 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1094 struct type
**types
;
1095 unsigned int length
;
1097 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1100 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1101 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1102 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1103 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1104 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1105 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1106 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1107 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1108 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1109 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1111 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1112 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1113 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1115 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1116 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1118 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1120 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1121 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1122 types
[rust_primitive_usize
]);
1124 lai
->primitive_type_vector
= types
;
1125 lai
->bool_type_default
= types
[rust_primitive_bool
];
1126 lai
->string_char_type
= types
[rust_primitive_u8
];
1131 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1133 static struct value
*
1134 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1137 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1140 struct value
*function
, *result
, *arg0
;
1141 struct value
**args
;
1142 struct cleanup
*cleanup
;
1143 struct type
*type
, *fn_type
;
1144 const struct block
*block
;
1145 struct block_symbol sym
;
1147 /* For an ordinary function call we can simply defer to the
1148 generic implementation. */
1149 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1150 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1152 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1154 method
= &exp
->elts
[*pos
+ 1].string
;
1155 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1157 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1158 type in order to look up the method. */
1159 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1161 if (noside
== EVAL_SKIP
)
1163 for (i
= 0; i
< num_args
; ++i
)
1164 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1168 args
= XNEWVEC (struct value
*, num_args
+ 1);
1169 cleanup
= make_cleanup (xfree
, args
);
1172 /* We don't yet implement real Deref semantics. */
1173 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1174 args
[0] = value_ind (args
[0]);
1176 type
= value_type (args
[0]);
1177 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1178 && TYPE_CODE (type
) != TYPE_CODE_UNION
1179 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1180 || rust_tuple_type_p (type
))
1181 error (_("Method calls only supported on struct or enum types"));
1182 if (TYPE_TAG_NAME (type
) == NULL
)
1183 error (_("Method call on nameless type"));
1185 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1186 make_cleanup (xfree
, name
);
1188 block
= get_selected_block (0);
1189 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1190 if (sym
.symbol
== NULL
)
1191 error (_("Could not find function named '%s'"), name
);
1193 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1194 if (TYPE_NFIELDS (fn_type
) == 0)
1195 error (_("Function '%s' takes no arguments"), name
);
1197 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1198 args
[0] = value_addr (args
[0]);
1200 function
= address_of_variable (sym
.symbol
, block
);
1202 for (i
= 0; i
< num_args
; ++i
)
1203 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1205 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1206 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1208 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1209 do_cleanups (cleanup
);
1213 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1215 static struct value
*
1216 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1218 enum range_type kind
;
1219 struct value
*low
= NULL
, *high
= NULL
;
1220 struct value
*addrval
, *result
;
1222 struct type
*range_type
;
1223 struct type
*index_type
;
1224 struct type
*temp_type
;
1227 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1230 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1231 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1232 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1233 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1235 if (noside
== EVAL_SKIP
)
1236 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1243 name
= "std::ops::RangeFull";
1247 index_type
= value_type (high
);
1248 name
= "std::ops::RangeTo";
1255 index_type
= value_type (low
);
1256 name
= "std::ops::RangeFrom";
1260 if (!types_equal (value_type (low
), value_type (high
)))
1261 error (_("Range expression with different types"));
1262 index_type
= value_type (low
);
1263 name
= "std::ops::Range";
1267 /* If we don't have an index type, just allocate this on the
1268 arch. Here any type will do. */
1269 temp_type
= (index_type
== NULL
1270 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1272 /* It would be nicer to cache the range type. */
1273 range_type
= rust_composite_type (temp_type
, name
,
1274 low
== NULL
? NULL
: "start", index_type
,
1275 high
== NULL
? NULL
: "end", index_type
);
1277 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1278 return value_zero (range_type
, lval_memory
);
1280 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1281 addr
= value_as_long (addrval
);
1282 result
= value_at_lazy (range_type
, addr
);
1286 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1289 value_assign (start
, low
);
1294 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1297 value_assign (end
, high
);
1300 result
= value_at_lazy (range_type
, addr
);
1304 /* A helper function to compute the range and kind given a range
1305 value. TYPE is the type of the range value. RANGE is the range
1306 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1307 parameters might be filled in, or might not be, depending on the
1308 kind of range this is. KIND will always be set to the appropriate
1309 value describing the kind of range, and this can be used to
1310 determine whether LOW or HIGH are valid. */
1313 rust_compute_range (struct type
*type
, struct value
*range
,
1314 LONGEST
*low
, LONGEST
*high
,
1315 enum range_type
*kind
)
1321 *kind
= BOTH_BOUND_DEFAULT
;
1323 if (TYPE_NFIELDS (type
) == 0)
1327 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1329 *kind
= HIGH_BOUND_DEFAULT
;
1330 *low
= value_as_long (value_field (range
, 0));
1333 if (TYPE_NFIELDS (type
) > i
1334 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1336 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1337 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1338 *high
= value_as_long (value_field (range
, i
));
1342 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1344 static struct value
*
1345 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1348 struct value
*lhs
, *rhs
, *result
;
1349 struct type
*rhstype
;
1350 LONGEST low
, high_bound
;
1351 /* Initialized to appease the compiler. */
1352 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1357 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1358 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1360 if (noside
== EVAL_SKIP
)
1363 rhstype
= check_typedef (value_type (rhs
));
1364 if (rust_range_type_p (rhstype
))
1367 error (_("Can't take slice of array without '&'"));
1368 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1372 low
= value_as_long (rhs
);
1374 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1376 struct type
*type
= check_typedef (value_type (lhs
));
1378 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1384 struct type
*type
= check_typedef (value_type (lhs
));
1386 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1389 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1390 error (_("Can't compute array bounds"));
1392 error (_("Found array with non-zero lower bound"));
1395 else if (rust_slice_type_p (type
))
1399 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1400 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1402 high_bound
= value_as_long (len
);
1405 error (_("Cannot subscript non-array type"));
1408 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1411 error (_("Index less than zero"));
1412 if (low
> high_bound
)
1413 error (_("Index greater than length"));
1415 result
= value_subscript (base
, low
);
1422 struct type
*usize
, *slice
;
1424 struct value
*addrval
, *tem
;
1426 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1429 error (_("High index less than zero"));
1431 error (_("Low index greater than high index"));
1432 if (high
> high_bound
)
1433 error (_("High index greater than length"));
1435 usize
= language_lookup_primitive_type (exp
->language_defn
,
1438 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1441 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1442 addr
= value_as_long (addrval
);
1443 tem
= value_at_lazy (slice
, addr
);
1445 value_assign (value_field (tem
, 0), value_addr (result
));
1446 value_assign (value_field (tem
, 1),
1447 value_from_longest (usize
, high
- low
));
1449 result
= value_at_lazy (slice
, addr
);
1452 result
= value_addr (result
);
1458 /* evaluate_exp implementation for Rust. */
1460 static struct value
*
1461 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1462 int *pos
, enum noside noside
)
1464 struct value
*result
;
1466 switch (exp
->elts
[*pos
].opcode
)
1468 case UNOP_COMPLEMENT
:
1470 struct value
*value
;
1473 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1474 if (noside
== EVAL_SKIP
)
1476 /* Preserving the type is enough. */
1479 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1480 result
= value_from_longest (value_type (value
),
1481 value_logical_not (value
));
1483 result
= value_complement (value
);
1487 case BINOP_SUBSCRIPT
:
1488 result
= rust_subscript (exp
, pos
, noside
, 0);
1492 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1498 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1499 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1502 struct value
*addrval
= NULL
;
1506 if (noside
== EVAL_NORMAL
)
1508 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1509 addr
= value_as_long (addrval
);
1510 result
= value_at_lazy (type
, addr
);
1513 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1518 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1519 if (noside
== EVAL_NORMAL
)
1521 /* This isn't quite right but will do for the time
1522 being, seeing that we can't implement the Copy
1524 value_assign (result
, init
);
1530 gdb_assert (arglen
% 2 == 0);
1531 for (i
= 0; i
< arglen
; i
+= 2)
1534 const char *fieldname
;
1535 struct value
*value
, *field
;
1537 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1539 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1541 fieldname
= &exp
->elts
[*pos
].string
;
1542 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1544 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1545 if (noside
== EVAL_NORMAL
)
1547 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1549 value_assign (field
, value
);
1553 if (noside
== EVAL_SKIP
)
1554 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1556 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1557 result
= allocate_value (type
);
1559 result
= value_at_lazy (type
, addr
);
1568 struct value
*ncopies
;
1570 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1571 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1572 copies
= value_as_long (ncopies
);
1574 error (_("Array with negative number of elements"));
1576 if (noside
== EVAL_NORMAL
)
1580 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1581 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1583 for (i
= 0; i
< copies
; ++i
)
1585 result
= value_array (0, copies
- 1, eltvec
);
1587 do_cleanups (cleanup
);
1591 struct type
*arraytype
1592 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1593 result
= allocate_value (arraytype
);
1598 case STRUCTOP_ANONYMOUS
:
1600 /* Anonymous field access, i.e. foo.1. */
1602 int pc
, field_number
, nfields
;
1603 struct type
*type
, *variant_type
;
1604 struct disr_info disr
;
1607 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1609 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1611 type
= value_type (lhs
);
1612 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1614 struct cleanup
*cleanup
;
1616 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1617 value_embedded_offset (lhs
),
1618 value_address (lhs
), lhs
);
1620 cleanup
= make_cleanup (xfree
, disr
.name
);
1622 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1624 variant_type
= NULL
;
1629 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1630 nfields
= TYPE_NFIELDS (variant_type
);
1633 if (!disr
.is_encoded
)
1636 if (field_number
>= nfields
|| field_number
< 0)
1637 error(_("Cannot access field %d of variant %s, \
1638 there are only %d fields"),
1639 disr
.is_encoded
? field_number
: field_number
- 1,
1641 disr
.is_encoded
? nfields
: nfields
- 1);
1643 if (!(disr
.is_encoded
1644 ? rust_tuple_struct_type_p (variant_type
)
1645 : rust_tuple_variant_type_p (variant_type
)))
1646 error(_("Variant %s is not a tuple variant"), disr
.name
);
1648 result
= value_primitive_field (lhs
, 0, field_number
,
1650 do_cleanups (cleanup
);
1652 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1654 /* Tuples and tuple structs */
1655 nfields
= TYPE_NFIELDS(type
);
1657 if (field_number
>= nfields
|| field_number
< 0)
1658 error(_("Cannot access field %d of %s, there are only %d fields"),
1659 field_number
, TYPE_TAG_NAME (type
), nfields
);
1661 /* Tuples are tuple structs too. */
1662 if (!rust_tuple_struct_type_p (type
))
1663 error(_("Attempting to access anonymous field %d of %s, which is \
1664 not a tuple, tuple struct, or tuple-like variant"),
1665 field_number
, TYPE_TAG_NAME (type
));
1667 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1670 error(_("Anonymous field access is only allowed on tuples, \
1671 tuple structs, and tuple-like enum variants"));
1675 case STRUCTOP_STRUCT
:
1682 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1683 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1684 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1686 type
= value_type (lhs
);
1688 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1691 struct disr_info disr
;
1692 struct cleanup
* cleanup
;
1693 struct type
* variant_type
;
1696 field_name
= &exp
->elts
[pc
+ 2].string
;
1698 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1699 value_embedded_offset (lhs
),
1700 value_address (lhs
), lhs
);
1702 cleanup
= make_cleanup (xfree
, disr
.name
);
1704 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1705 error(_("Could not find field %s of struct variant %s"),
1706 field_name
, disr
.name
);
1708 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1710 if (variant_type
== NULL
1711 || rust_tuple_variant_type_p (variant_type
))
1712 error(_("Attempting to access named field %s of tuple variant %s, \
1713 which has only anonymous fields"),
1714 field_name
, disr
.name
);
1716 start
= disr
.is_encoded
? 0 : 1;
1717 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1719 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1721 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1726 if (i
== TYPE_NFIELDS (variant_type
))
1727 /* We didn't find it. */
1728 error(_("Could not find field %s of struct variant %s"),
1729 field_name
, disr
.name
);
1731 do_cleanups (cleanup
);
1736 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1742 result
= rust_range (exp
, pos
, noside
);
1746 /* We might have &array[range], in which case we need to make a
1748 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1751 result
= rust_subscript (exp
, pos
, noside
, 1);
1756 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1763 /* operator_length implementation for Rust. */
1766 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1772 switch (exp
->elts
[pc
- 1].opcode
)
1775 /* We handle aggregate as a type and argument count. The first
1776 argument might be OP_OTHERS. After that the arguments
1777 alternate: first an OP_NAME, then an expression. */
1779 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1787 case STRUCTOP_ANONYMOUS
:
1798 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1806 /* op_name implementation for Rust. */
1809 rust_op_name (enum exp_opcode opcode
)
1814 return "OP_AGGREGATE";
1818 return op_name_standard (opcode
);
1822 /* dump_subexp_body implementation for Rust. */
1825 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1828 switch (exp
->elts
[elt
].opcode
)
1832 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1835 fprintf_filtered (stream
, "Type @");
1836 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1837 fprintf_filtered (stream
, " (");
1838 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1839 fprintf_filtered (stream
, "), length %d", length
);
1842 for (i
= 0; i
< length
; ++i
)
1843 elt
= dump_subexp (exp
, stream
, elt
);
1850 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1852 fprintf_filtered (stream
, "%s: %s",
1853 (exp
->elts
[elt
].opcode
== OP_STRING
1854 ? "string" : "name"),
1855 &exp
->elts
[elt
+ 2].string
);
1856 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1861 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1864 case STRUCTOP_ANONYMOUS
:
1868 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1870 fprintf_filtered (stream
, "Field number: %d", field_number
);
1871 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1879 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1886 /* print_subexp implementation for Rust. */
1889 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1890 enum precedence prec
)
1892 switch (exp
->elts
[*pos
].opcode
)
1896 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1899 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1900 fputs_filtered (" { ", stream
);
1903 for (i
= 0; i
< length
; ++i
)
1905 rust_print_subexp (exp
, pos
, stream
, prec
);
1906 fputs_filtered (", ", stream
);
1908 fputs_filtered (" }", stream
);
1914 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1916 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1917 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1923 fputs_filtered ("<<others>> (", stream
);
1925 rust_print_subexp (exp
, pos
, stream
, prec
);
1926 fputs_filtered (")", stream
);
1930 case STRUCTOP_ANONYMOUS
:
1932 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1935 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1936 fprintf_filtered (stream
, ".%d", tem
);
1942 fprintf_filtered (stream
, "[");
1943 rust_print_subexp (exp
, pos
, stream
, prec
);
1944 fprintf_filtered (stream
, "; ");
1945 rust_print_subexp (exp
, pos
, stream
, prec
);
1946 fprintf_filtered (stream
, "]");
1950 print_subexp_standard (exp
, pos
, stream
, prec
);
1955 /* operator_check implementation for Rust. */
1958 rust_operator_check (struct expression
*exp
, int pos
,
1959 int (*objfile_func
) (struct objfile
*objfile
,
1963 switch (exp
->elts
[pos
].opcode
)
1967 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1968 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1970 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1981 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1989 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1991 static struct block_symbol
1992 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1994 const struct block
*block
,
1995 const domain_enum domain
)
1997 struct block_symbol result
= {NULL
, NULL
};
1999 if (symbol_lookup_debug
)
2001 fprintf_unfiltered (gdb_stdlog
,
2002 "rust_lookup_symbol_non_local"
2003 " (%s, %s (scope %s), %s)\n",
2004 name
, host_address_to_string (block
),
2005 block_scope (block
), domain_name (domain
));
2008 /* Look up bare names in the block's scope. */
2009 if (name
[cp_find_first_component (name
)] == '\0')
2011 const char *scope
= block_scope (block
);
2013 if (scope
[0] != '\0')
2015 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
2016 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
2018 result
= lookup_symbol_in_static_block (scopedname
, block
,
2020 if (result
.symbol
== NULL
)
2021 result
= lookup_global_symbol (scopedname
, block
, domain
);
2022 do_cleanups (cleanup
);
2030 /* la_sniff_from_mangled_name for Rust. */
2033 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2035 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2036 return *demangled
!= NULL
;
2041 static const struct exp_descriptor exp_descriptor_rust
=
2044 rust_operator_length
,
2045 rust_operator_check
,
2047 rust_dump_subexp_body
,
2048 rust_evaluate_subexp
2051 static const char *rust_extensions
[] =
2056 static const struct language_defn rust_language_defn
=
2066 &exp_descriptor_rust
,
2070 rust_printchar
, /* Print a character constant */
2071 rust_printstr
, /* Function to print string constant */
2072 rust_emitchar
, /* Print a single char */
2073 rust_print_type
, /* Print a type using appropriate syntax */
2074 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2075 rust_val_print
, /* Print a value using appropriate syntax */
2076 c_value_print
, /* Print a top-level value */
2077 default_read_var_value
, /* la_read_var_value */
2078 NULL
, /* Language specific skip_trampoline */
2079 NULL
, /* name_of_this */
2080 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2081 basic_lookup_transparent_type
,/* lookup_transparent_type */
2082 gdb_demangle
, /* Language specific symbol demangler */
2083 rust_sniff_from_mangled_name
,
2084 NULL
, /* Language specific
2085 class_name_from_physname */
2086 c_op_print_tab
, /* expression operators for printing */
2087 1, /* c-style arrays */
2088 0, /* String lower bound */
2089 default_word_break_characters
,
2090 default_make_symbol_completion_list
,
2091 rust_language_arch_info
,
2092 default_print_array_index
,
2093 default_pass_by_reference
,
2095 NULL
, /* la_get_symbol_name_cmp */
2096 iterate_over_symbols
,
2097 &default_varobj_ops
,
2104 _initialize_rust_language (void)
2106 add_language (&rust_language_defn
);