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 /* This is just an approximation until DWARF can represent Rust more
298 precisely. We exclude zero-length structs because they may not
299 be tuple structs, and there's no way to tell. */
300 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
303 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
306 rust_tuple_variant_type_p (struct type
*type
)
308 /* First field is discriminant */
309 return rust_underscore_fields (type
, 1);
312 /* Return true if TYPE is a slice type, otherwise false. */
315 rust_slice_type_p (struct type
*type
)
317 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
318 && TYPE_TAG_NAME (type
) != NULL
319 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
322 /* Return true if TYPE is a range type, otherwise false. */
325 rust_range_type_p (struct type
*type
)
329 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
330 || TYPE_NFIELDS (type
) > 2
331 || TYPE_TAG_NAME (type
) == NULL
332 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
335 if (TYPE_NFIELDS (type
) == 0)
339 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
341 if (TYPE_NFIELDS (type
) == 1)
345 else if (TYPE_NFIELDS (type
) == 2)
347 /* First field had to be "start". */
351 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
354 /* Return true if TYPE seems to be the type "u8", otherwise false. */
357 rust_u8_type_p (struct type
*type
)
359 return (TYPE_CODE (type
) == TYPE_CODE_INT
360 && TYPE_UNSIGNED (type
)
361 && TYPE_LENGTH (type
) == 1);
364 /* Return true if TYPE is a Rust character type. */
367 rust_chartype_p (struct type
*type
)
369 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
370 && TYPE_LENGTH (type
) == 4
371 && TYPE_UNSIGNED (type
));
376 /* la_emitchar implementation for Rust. */
379 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
381 if (!rust_chartype_p (type
))
382 generic_emit_char (c
, type
, stream
, quoter
,
383 target_charset (get_type_arch (type
)));
384 else if (c
== '\\' || c
== quoter
)
385 fprintf_filtered (stream
, "\\%c", c
);
387 fputs_filtered ("\\n", stream
);
389 fputs_filtered ("\\r", stream
);
391 fputs_filtered ("\\t", stream
);
393 fputs_filtered ("\\0", stream
);
394 else if (c
>= 32 && c
<= 127 && isprint (c
))
395 fputc_filtered (c
, stream
);
397 fprintf_filtered (stream
, "\\x%02x", c
);
399 fprintf_filtered (stream
, "\\u{%06x}", c
);
402 /* la_printchar implementation for Rust. */
405 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
407 fputs_filtered ("'", stream
);
408 LA_EMIT_CHAR (c
, type
, stream
, '\'');
409 fputs_filtered ("'", stream
);
412 /* la_printstr implementation for Rust. */
415 rust_printstr (struct ui_file
*stream
, struct type
*type
,
416 const gdb_byte
*string
, unsigned int length
,
417 const char *user_encoding
, int force_ellipses
,
418 const struct value_print_options
*options
)
420 /* Rust always uses UTF-8, but let the caller override this if need
422 const char *encoding
= user_encoding
;
423 if (user_encoding
== NULL
|| !*user_encoding
)
425 /* In Rust strings, characters are "u8". */
426 if (rust_u8_type_p (type
))
430 /* This is probably some C string, so let's let C deal with
432 c_printstr (stream
, type
, string
, length
, user_encoding
,
433 force_ellipses
, options
);
438 /* This is not ideal as it doesn't use our character printer. */
439 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
445 static const struct generic_val_print_decorations rust_decorations
=
447 /* Complex isn't used in Rust, but we provide C-ish values just in
459 /* la_val_print implementation for Rust. */
462 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
463 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
464 const struct value
*val
,
465 const struct value_print_options
*options
)
467 type
= check_typedef (type
);
468 switch (TYPE_CODE (type
))
472 LONGEST low_bound
, high_bound
;
474 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
475 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
476 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
478 /* We have a pointer to a byte string, so just print
480 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
482 struct gdbarch
*arch
= get_type_arch (type
);
483 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
485 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
486 if (options
->addressprint
)
488 fputs_filtered (paddress (arch
, addr
), stream
);
489 fputs_filtered (" ", stream
);
492 fputs_filtered ("b", stream
);
493 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
494 high_bound
- low_bound
+ 1, stream
,
501 case TYPE_CODE_METHODPTR
:
502 case TYPE_CODE_MEMBERPTR
:
503 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
504 recurse
, val
, options
);
508 /* Recognize the unit type. */
509 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
510 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
512 fputs_filtered ("()", stream
);
517 case TYPE_CODE_STRING
:
519 struct gdbarch
*arch
= get_type_arch (type
);
520 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
521 LONGEST low_bound
, high_bound
;
523 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
524 error (_("Could not determine the array bounds"));
526 /* If we see a plain TYPE_CODE_STRING, then we're printing a
527 byte string, hence the choice of "ASCII" as the
529 fputs_filtered ("b", stream
);
530 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
531 valaddr
+ embedded_offset
* unit_size
,
532 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
536 case TYPE_CODE_ARRAY
:
538 LONGEST low_bound
, high_bound
;
540 if (get_array_bounds (type
, &low_bound
, &high_bound
)
541 && high_bound
- low_bound
+ 1 == 0)
542 fputs_filtered ("[]", stream
);
548 case TYPE_CODE_UNION
:
550 int j
, nfields
, first_field
, is_tuple
, start
;
551 struct type
*variant_type
;
552 struct disr_info disr
;
553 struct value_print_options opts
;
554 struct cleanup
*cleanup
;
559 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
561 cleanup
= make_cleanup (xfree
, disr
.name
);
563 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
565 fprintf_filtered (stream
, "%s", disr
.name
);
570 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
571 nfields
= TYPE_NFIELDS (variant_type
);
573 is_tuple
= (disr
.is_encoded
574 ? rust_tuple_struct_type_p (variant_type
)
575 : rust_tuple_variant_type_p (variant_type
));
576 start
= disr
.is_encoded
? 0 : 1;
580 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
582 fprintf_filtered (stream
, "%s(", disr
.name
);
585 /* struct variant. */
586 fprintf_filtered (stream
, "%s{", disr
.name
);
591 /* In case of a nullary variant like 'None', just output
593 fprintf_filtered (stream
, "%s", disr
.name
);
597 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
600 fputs_filtered (", ", stream
);
604 fprintf_filtered (stream
, "%s: ",
605 TYPE_FIELD_NAME (variant_type
, j
));
607 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
610 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
611 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
613 stream
, recurse
+ 1, val
, &opts
,
618 fputs_filtered (")", stream
);
620 fputs_filtered ("}", stream
);
623 do_cleanups (cleanup
);
627 case TYPE_CODE_STRUCT
:
631 int is_tuple
= rust_tuple_type_p (type
);
632 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
633 struct value_print_options opts
;
637 if (TYPE_TAG_NAME (type
) != NULL
)
638 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
640 if (TYPE_NFIELDS (type
) == 0)
643 if (TYPE_TAG_NAME (type
) != NULL
)
644 fputs_filtered (" ", stream
);
647 if (is_tuple
|| is_tuple_struct
)
648 fputs_filtered ("(", stream
);
650 fputs_filtered ("{", stream
);
656 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
658 if (field_is_static (&TYPE_FIELD (type
, i
)))
662 fputs_filtered (",", stream
);
664 if (options
->prettyformat
)
666 fputs_filtered ("\n", stream
);
667 print_spaces_filtered (2 + 2 * recurse
, stream
);
669 else if (!first_field
)
670 fputs_filtered (" ", stream
);
674 if (!is_tuple
&& !is_tuple_struct
)
676 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
677 fputs_filtered (": ", stream
);
680 val_print (TYPE_FIELD_TYPE (type
, i
),
682 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
684 stream
, recurse
+ 1, val
, &opts
,
688 if (options
->prettyformat
)
690 fputs_filtered ("\n", stream
);
691 print_spaces_filtered (2 * recurse
, stream
);
694 if (is_tuple
|| is_tuple_struct
)
695 fputs_filtered (")", stream
);
697 fputs_filtered ("}", stream
);
703 /* Nothing special yet. */
704 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
705 recurse
, val
, options
, &rust_decorations
);
711 /* la_print_typedef implementation for Rust. */
714 rust_print_typedef (struct type
*type
,
715 struct symbol
*new_symbol
,
716 struct ui_file
*stream
)
718 type
= check_typedef (type
);
719 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
720 type_print (type
, "", stream
, 0);
721 fprintf_filtered (stream
, ";\n");
724 /* la_print_type implementation for Rust. */
727 rust_print_type (struct type
*type
, const char *varstring
,
728 struct ui_file
*stream
, int show
, int level
,
729 const struct type_print_options
*flags
)
735 && TYPE_NAME (type
) != NULL
)
737 /* Rust calls the unit type "void" in its debuginfo,
738 but we don't want to print it as that. */
739 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
740 fputs_filtered ("()", stream
);
742 fputs_filtered (TYPE_NAME (type
), stream
);
746 type
= check_typedef (type
);
747 switch (TYPE_CODE (type
))
750 fputs_filtered ("()", stream
);
754 /* Delegate varargs to the C printer. */
755 if (TYPE_VARARGS (type
))
758 fputs_filtered ("fn ", stream
);
759 if (varstring
!= NULL
)
760 fputs_filtered (varstring
, stream
);
761 fputs_filtered ("(", stream
);
762 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
766 fputs_filtered (", ", stream
);
767 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
770 fputs_filtered (")", stream
);
771 /* If it returns unit, we can omit the return type. */
772 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
774 fputs_filtered (" -> ", stream
);
775 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
779 case TYPE_CODE_ARRAY
:
781 LONGEST low_bound
, high_bound
;
783 fputs_filtered ("[", stream
);
784 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
785 stream
, show
- 1, level
, flags
);
786 fputs_filtered ("; ", stream
);
788 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
789 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
790 fprintf_filtered (stream
, "variable length");
791 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
792 fprintf_filtered (stream
, "%s",
793 plongest (high_bound
- low_bound
+ 1));
794 fputs_filtered ("]", stream
);
798 case TYPE_CODE_STRUCT
:
802 /* Print a tuple type simply. */
803 if (rust_tuple_type_p (type
))
805 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
809 /* If we see a base class, delegate to C. */
810 if (TYPE_N_BASECLASSES (type
) > 0)
813 fputs_filtered ("struct ", stream
);
814 if (TYPE_TAG_NAME (type
) != NULL
)
815 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
817 is_tuple_struct
= rust_tuple_struct_type_p (type
);
819 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
821 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
823 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
828 if (field_is_static (&TYPE_FIELD (type
, i
)))
831 /* We'd like to print "pub" here as needed, but rustc
832 doesn't emit the debuginfo, and our types don't have
833 cplus_struct_type attached. */
835 /* For a tuple struct we print the type but nothing
837 print_spaces_filtered (level
+ 2, stream
);
838 if (!is_tuple_struct
)
839 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
841 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
842 stream
, show
- 1, level
+ 2,
844 fputs_filtered (",\n", stream
);
847 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
855 fputs_filtered ("enum ", stream
);
856 if (TYPE_TAG_NAME (type
) != NULL
)
858 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
859 fputs_filtered (" ", stream
);
860 len
= strlen (TYPE_TAG_NAME (type
));
862 fputs_filtered ("{\n", stream
);
864 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
866 const char *name
= TYPE_FIELD_NAME (type
, i
);
871 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
873 && name
[len
+ 1] == ':')
875 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
878 fputs_filtered ("}", stream
);
882 case TYPE_CODE_UNION
:
886 /* Skip the discriminant field. */
889 fputs_filtered ("enum ", stream
);
890 if (TYPE_TAG_NAME (type
) != NULL
)
892 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
893 fputs_filtered (" ", stream
);
895 fputs_filtered ("{\n", stream
);
897 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
898 strlen (RUST_ENUM_PREFIX
)) == 0)
900 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
901 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
903 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
904 /* There is no explicit discriminant field, skip nothing. */
909 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
911 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
913 = rust_last_path_segment (TYPE_NAME (variant_type
));
915 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
917 if (TYPE_NFIELDS (variant_type
) > skip_to
)
920 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
923 fputs_filtered (is_tuple
? "(" : "{", stream
);
924 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
929 fputs_filtered (", ", stream
);
932 fprintf_filtered (stream
, "%s: ",
933 TYPE_FIELD_NAME (variant_type
, j
));
935 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
936 stream
, show
- 1, level
+ 2,
939 fputs_filtered (is_tuple
? ")" : "}", stream
);
942 fputs_filtered (",\n", stream
);
945 fputs_filtered ("}", stream
);
951 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
957 /* Compute the alignment of the type T. */
960 rust_type_alignment (struct type
*t
)
962 t
= check_typedef (t
);
963 switch (TYPE_CODE (t
))
966 error (_("Could not compute alignment of type"));
975 return TYPE_LENGTH (t
);
977 case TYPE_CODE_ARRAY
:
978 case TYPE_CODE_COMPLEX
:
979 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
981 case TYPE_CODE_STRUCT
:
982 case TYPE_CODE_UNION
:
987 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
989 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
998 /* Like arch_composite_type, but uses TYPE to decide how to allocate
999 -- either on an obstack or on a gdbarch. */
1001 static struct type
*
1002 rust_composite_type (struct type
*original
,
1004 const char *field1
, struct type
*type1
,
1005 const char *field2
, struct type
*type2
)
1007 struct type
*result
= alloc_type_copy (original
);
1008 int i
, nfields
, bitpos
;
1016 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1017 TYPE_NAME (result
) = name
;
1018 TYPE_TAG_NAME (result
) = name
;
1020 TYPE_NFIELDS (result
) = nfields
;
1021 TYPE_FIELDS (result
)
1022 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1028 struct field
*field
= &TYPE_FIELD (result
, i
);
1030 SET_FIELD_BITPOS (*field
, bitpos
);
1031 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1033 FIELD_NAME (*field
) = field1
;
1034 FIELD_TYPE (*field
) = type1
;
1039 struct field
*field
= &TYPE_FIELD (result
, i
);
1040 int align
= rust_type_alignment (type2
);
1046 align
*= TARGET_CHAR_BIT
;
1047 delta
= bitpos
% align
;
1049 bitpos
+= align
- delta
;
1051 SET_FIELD_BITPOS (*field
, bitpos
);
1053 FIELD_NAME (*field
) = field2
;
1054 FIELD_TYPE (*field
) = type2
;
1059 TYPE_LENGTH (result
)
1060 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1061 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1065 /* See rust-lang.h. */
1068 rust_slice_type (const char *name
, struct type
*elt_type
,
1069 struct type
*usize_type
)
1073 elt_type
= lookup_pointer_type (elt_type
);
1074 type
= rust_composite_type (elt_type
, name
,
1075 "data_ptr", elt_type
,
1076 "length", usize_type
);
1081 enum rust_primitive_types
1083 rust_primitive_bool
,
1084 rust_primitive_char
,
1093 rust_primitive_isize
,
1094 rust_primitive_usize
,
1097 rust_primitive_unit
,
1099 nr_rust_primitive_types
1102 /* la_language_arch_info implementation for Rust. */
1105 rust_language_arch_info (struct gdbarch
*gdbarch
,
1106 struct language_arch_info
*lai
)
1108 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1110 struct type
**types
;
1111 unsigned int length
;
1113 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1116 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1117 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1118 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1119 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1120 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1121 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1122 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1123 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1124 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1125 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1127 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1128 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1129 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1131 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1132 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1134 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1136 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1137 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1138 types
[rust_primitive_usize
]);
1140 lai
->primitive_type_vector
= types
;
1141 lai
->bool_type_default
= types
[rust_primitive_bool
];
1142 lai
->string_char_type
= types
[rust_primitive_u8
];
1147 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1149 static struct value
*
1150 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1153 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1156 struct value
*function
, *result
, *arg0
;
1157 struct value
**args
;
1158 struct cleanup
*cleanup
;
1159 struct type
*type
, *fn_type
;
1160 const struct block
*block
;
1161 struct block_symbol sym
;
1163 /* For an ordinary function call we can simply defer to the
1164 generic implementation. */
1165 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1166 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1168 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1170 method
= &exp
->elts
[*pos
+ 1].string
;
1171 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1173 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1174 type in order to look up the method. */
1175 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1177 if (noside
== EVAL_SKIP
)
1179 for (i
= 0; i
< num_args
; ++i
)
1180 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1184 args
= XNEWVEC (struct value
*, num_args
+ 1);
1185 cleanup
= make_cleanup (xfree
, args
);
1188 /* We don't yet implement real Deref semantics. */
1189 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1190 args
[0] = value_ind (args
[0]);
1192 type
= value_type (args
[0]);
1193 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1194 && TYPE_CODE (type
) != TYPE_CODE_UNION
1195 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1196 || rust_tuple_type_p (type
))
1197 error (_("Method calls only supported on struct or enum types"));
1198 if (TYPE_TAG_NAME (type
) == NULL
)
1199 error (_("Method call on nameless type"));
1201 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1202 make_cleanup (xfree
, name
);
1204 block
= get_selected_block (0);
1205 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1206 if (sym
.symbol
== NULL
)
1207 error (_("Could not find function named '%s'"), name
);
1209 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1210 if (TYPE_NFIELDS (fn_type
) == 0)
1211 error (_("Function '%s' takes no arguments"), name
);
1213 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1214 args
[0] = value_addr (args
[0]);
1216 function
= address_of_variable (sym
.symbol
, block
);
1218 for (i
= 0; i
< num_args
; ++i
)
1219 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1221 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1222 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1224 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1225 do_cleanups (cleanup
);
1229 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1231 static struct value
*
1232 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1234 enum range_type kind
;
1235 struct value
*low
= NULL
, *high
= NULL
;
1236 struct value
*addrval
, *result
;
1238 struct type
*range_type
;
1239 struct type
*index_type
;
1240 struct type
*temp_type
;
1243 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1246 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1247 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1248 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1249 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1251 if (noside
== EVAL_SKIP
)
1252 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1259 name
= "std::ops::RangeFull";
1263 index_type
= value_type (high
);
1264 name
= "std::ops::RangeTo";
1271 index_type
= value_type (low
);
1272 name
= "std::ops::RangeFrom";
1276 if (!types_equal (value_type (low
), value_type (high
)))
1277 error (_("Range expression with different types"));
1278 index_type
= value_type (low
);
1279 name
= "std::ops::Range";
1283 /* If we don't have an index type, just allocate this on the
1284 arch. Here any type will do. */
1285 temp_type
= (index_type
== NULL
1286 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1288 /* It would be nicer to cache the range type. */
1289 range_type
= rust_composite_type (temp_type
, name
,
1290 low
== NULL
? NULL
: "start", index_type
,
1291 high
== NULL
? NULL
: "end", index_type
);
1293 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1294 return value_zero (range_type
, lval_memory
);
1296 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1297 addr
= value_as_long (addrval
);
1298 result
= value_at_lazy (range_type
, addr
);
1302 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1305 value_assign (start
, low
);
1310 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1313 value_assign (end
, high
);
1316 result
= value_at_lazy (range_type
, addr
);
1320 /* A helper function to compute the range and kind given a range
1321 value. TYPE is the type of the range value. RANGE is the range
1322 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1323 parameters might be filled in, or might not be, depending on the
1324 kind of range this is. KIND will always be set to the appropriate
1325 value describing the kind of range, and this can be used to
1326 determine whether LOW or HIGH are valid. */
1329 rust_compute_range (struct type
*type
, struct value
*range
,
1330 LONGEST
*low
, LONGEST
*high
,
1331 enum range_type
*kind
)
1337 *kind
= BOTH_BOUND_DEFAULT
;
1339 if (TYPE_NFIELDS (type
) == 0)
1343 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1345 *kind
= HIGH_BOUND_DEFAULT
;
1346 *low
= value_as_long (value_field (range
, 0));
1349 if (TYPE_NFIELDS (type
) > i
1350 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1352 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1353 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1354 *high
= value_as_long (value_field (range
, i
));
1358 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1360 static struct value
*
1361 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1364 struct value
*lhs
, *rhs
, *result
;
1365 struct type
*rhstype
;
1366 LONGEST low
, high_bound
;
1367 /* Initialized to appease the compiler. */
1368 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1373 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1374 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1376 if (noside
== EVAL_SKIP
)
1379 rhstype
= check_typedef (value_type (rhs
));
1380 if (rust_range_type_p (rhstype
))
1383 error (_("Can't take slice of array without '&'"));
1384 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1388 low
= value_as_long (rhs
);
1390 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1392 struct type
*type
= check_typedef (value_type (lhs
));
1394 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1400 struct type
*type
= check_typedef (value_type (lhs
));
1402 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1405 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1406 error (_("Can't compute array bounds"));
1408 error (_("Found array with non-zero lower bound"));
1411 else if (rust_slice_type_p (type
))
1415 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1416 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1418 high_bound
= value_as_long (len
);
1420 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1424 high_bound
= LONGEST_MAX
;
1427 error (_("Cannot subscript non-array type"));
1430 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1433 error (_("Index less than zero"));
1434 if (low
> high_bound
)
1435 error (_("Index greater than length"));
1437 result
= value_subscript (base
, low
);
1444 struct type
*usize
, *slice
;
1446 struct value
*addrval
, *tem
;
1448 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1451 error (_("High index less than zero"));
1453 error (_("Low index greater than high index"));
1454 if (high
> high_bound
)
1455 error (_("High index greater than length"));
1457 usize
= language_lookup_primitive_type (exp
->language_defn
,
1460 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1463 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1464 addr
= value_as_long (addrval
);
1465 tem
= value_at_lazy (slice
, addr
);
1467 value_assign (value_field (tem
, 0), value_addr (result
));
1468 value_assign (value_field (tem
, 1),
1469 value_from_longest (usize
, high
- low
));
1471 result
= value_at_lazy (slice
, addr
);
1474 result
= value_addr (result
);
1480 /* evaluate_exp implementation for Rust. */
1482 static struct value
*
1483 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1484 int *pos
, enum noside noside
)
1486 struct value
*result
;
1488 switch (exp
->elts
[*pos
].opcode
)
1490 case UNOP_COMPLEMENT
:
1492 struct value
*value
;
1495 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1496 if (noside
== EVAL_SKIP
)
1498 /* Preserving the type is enough. */
1501 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1502 result
= value_from_longest (value_type (value
),
1503 value_logical_not (value
));
1505 result
= value_complement (value
);
1509 case BINOP_SUBSCRIPT
:
1510 result
= rust_subscript (exp
, pos
, noside
, 0);
1514 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1520 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1521 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1524 struct value
*addrval
= NULL
;
1528 if (noside
== EVAL_NORMAL
)
1530 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1531 addr
= value_as_long (addrval
);
1532 result
= value_at_lazy (type
, addr
);
1535 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1540 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1541 if (noside
== EVAL_NORMAL
)
1543 /* This isn't quite right but will do for the time
1544 being, seeing that we can't implement the Copy
1546 value_assign (result
, init
);
1552 gdb_assert (arglen
% 2 == 0);
1553 for (i
= 0; i
< arglen
; i
+= 2)
1556 const char *fieldname
;
1557 struct value
*value
, *field
;
1559 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1561 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1563 fieldname
= &exp
->elts
[*pos
].string
;
1564 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1566 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1567 if (noside
== EVAL_NORMAL
)
1569 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1571 value_assign (field
, value
);
1575 if (noside
== EVAL_SKIP
)
1576 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1578 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1579 result
= allocate_value (type
);
1581 result
= value_at_lazy (type
, addr
);
1590 struct value
*ncopies
;
1592 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1593 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1594 copies
= value_as_long (ncopies
);
1596 error (_("Array with negative number of elements"));
1598 if (noside
== EVAL_NORMAL
)
1602 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1603 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1605 for (i
= 0; i
< copies
; ++i
)
1607 result
= value_array (0, copies
- 1, eltvec
);
1609 do_cleanups (cleanup
);
1613 struct type
*arraytype
1614 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1615 result
= allocate_value (arraytype
);
1620 case STRUCTOP_ANONYMOUS
:
1622 /* Anonymous field access, i.e. foo.1. */
1624 int pc
, field_number
, nfields
;
1625 struct type
*type
, *variant_type
;
1626 struct disr_info disr
;
1629 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1631 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1633 type
= value_type (lhs
);
1634 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1636 struct cleanup
*cleanup
;
1638 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1639 value_embedded_offset (lhs
),
1640 value_address (lhs
), lhs
);
1642 cleanup
= make_cleanup (xfree
, disr
.name
);
1644 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1646 variant_type
= NULL
;
1651 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1652 nfields
= TYPE_NFIELDS (variant_type
);
1655 if (!disr
.is_encoded
)
1658 if (field_number
>= nfields
|| field_number
< 0)
1659 error(_("Cannot access field %d of variant %s, \
1660 there are only %d fields"),
1661 disr
.is_encoded
? field_number
: field_number
- 1,
1663 disr
.is_encoded
? nfields
: nfields
- 1);
1665 if (!(disr
.is_encoded
1666 ? rust_tuple_struct_type_p (variant_type
)
1667 : rust_tuple_variant_type_p (variant_type
)))
1668 error(_("Variant %s is not a tuple variant"), disr
.name
);
1670 result
= value_primitive_field (lhs
, 0, field_number
,
1672 do_cleanups (cleanup
);
1674 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1676 /* Tuples and tuple structs */
1677 nfields
= TYPE_NFIELDS(type
);
1679 if (field_number
>= nfields
|| field_number
< 0)
1680 error(_("Cannot access field %d of %s, there are only %d fields"),
1681 field_number
, TYPE_TAG_NAME (type
), nfields
);
1683 /* Tuples are tuple structs too. */
1684 if (!rust_tuple_struct_type_p (type
))
1685 error(_("Attempting to access anonymous field %d of %s, which is \
1686 not a tuple, tuple struct, or tuple-like variant"),
1687 field_number
, TYPE_TAG_NAME (type
));
1689 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1692 error(_("Anonymous field access is only allowed on tuples, \
1693 tuple structs, and tuple-like enum variants"));
1697 case STRUCTOP_STRUCT
:
1704 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1705 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1706 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1708 type
= value_type (lhs
);
1710 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1713 struct disr_info disr
;
1714 struct cleanup
* cleanup
;
1715 struct type
* variant_type
;
1718 field_name
= &exp
->elts
[pc
+ 2].string
;
1720 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1721 value_embedded_offset (lhs
),
1722 value_address (lhs
), lhs
);
1724 cleanup
= make_cleanup (xfree
, disr
.name
);
1726 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1727 error(_("Could not find field %s of struct variant %s"),
1728 field_name
, disr
.name
);
1730 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1732 if (variant_type
== NULL
1733 || rust_tuple_variant_type_p (variant_type
))
1734 error(_("Attempting to access named field %s of tuple variant %s, \
1735 which has only anonymous fields"),
1736 field_name
, disr
.name
);
1738 start
= disr
.is_encoded
? 0 : 1;
1739 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1741 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1743 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1748 if (i
== TYPE_NFIELDS (variant_type
))
1749 /* We didn't find it. */
1750 error(_("Could not find field %s of struct variant %s"),
1751 field_name
, disr
.name
);
1753 do_cleanups (cleanup
);
1758 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1764 result
= rust_range (exp
, pos
, noside
);
1768 /* We might have &array[range], in which case we need to make a
1770 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1773 result
= rust_subscript (exp
, pos
, noside
, 1);
1778 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1785 /* operator_length implementation for Rust. */
1788 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1794 switch (exp
->elts
[pc
- 1].opcode
)
1797 /* We handle aggregate as a type and argument count. The first
1798 argument might be OP_OTHERS. After that the arguments
1799 alternate: first an OP_NAME, then an expression. */
1801 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1809 case STRUCTOP_ANONYMOUS
:
1820 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1828 /* op_name implementation for Rust. */
1831 rust_op_name (enum exp_opcode opcode
)
1836 return "OP_AGGREGATE";
1840 return op_name_standard (opcode
);
1844 /* dump_subexp_body implementation for Rust. */
1847 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1850 switch (exp
->elts
[elt
].opcode
)
1854 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1857 fprintf_filtered (stream
, "Type @");
1858 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1859 fprintf_filtered (stream
, " (");
1860 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1861 fprintf_filtered (stream
, "), length %d", length
);
1864 for (i
= 0; i
< length
; ++i
)
1865 elt
= dump_subexp (exp
, stream
, elt
);
1872 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1874 fprintf_filtered (stream
, "%s: %s",
1875 (exp
->elts
[elt
].opcode
== OP_STRING
1876 ? "string" : "name"),
1877 &exp
->elts
[elt
+ 2].string
);
1878 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1883 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1886 case STRUCTOP_ANONYMOUS
:
1890 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1892 fprintf_filtered (stream
, "Field number: %d", field_number
);
1893 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1901 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1908 /* print_subexp implementation for Rust. */
1911 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1912 enum precedence prec
)
1914 switch (exp
->elts
[*pos
].opcode
)
1918 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1921 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1922 fputs_filtered (" { ", stream
);
1925 for (i
= 0; i
< length
; ++i
)
1927 rust_print_subexp (exp
, pos
, stream
, prec
);
1928 fputs_filtered (", ", stream
);
1930 fputs_filtered (" }", stream
);
1936 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1938 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1939 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1945 fputs_filtered ("<<others>> (", stream
);
1947 rust_print_subexp (exp
, pos
, stream
, prec
);
1948 fputs_filtered (")", stream
);
1952 case STRUCTOP_ANONYMOUS
:
1954 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1957 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1958 fprintf_filtered (stream
, ".%d", tem
);
1964 fprintf_filtered (stream
, "[");
1965 rust_print_subexp (exp
, pos
, stream
, prec
);
1966 fprintf_filtered (stream
, "; ");
1967 rust_print_subexp (exp
, pos
, stream
, prec
);
1968 fprintf_filtered (stream
, "]");
1972 print_subexp_standard (exp
, pos
, stream
, prec
);
1977 /* operator_check implementation for Rust. */
1980 rust_operator_check (struct expression
*exp
, int pos
,
1981 int (*objfile_func
) (struct objfile
*objfile
,
1985 switch (exp
->elts
[pos
].opcode
)
1989 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1990 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1992 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2003 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2011 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2013 static struct block_symbol
2014 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2016 const struct block
*block
,
2017 const domain_enum domain
)
2019 struct block_symbol result
= {NULL
, NULL
};
2021 if (symbol_lookup_debug
)
2023 fprintf_unfiltered (gdb_stdlog
,
2024 "rust_lookup_symbol_non_local"
2025 " (%s, %s (scope %s), %s)\n",
2026 name
, host_address_to_string (block
),
2027 block_scope (block
), domain_name (domain
));
2030 /* Look up bare names in the block's scope. */
2031 if (name
[cp_find_first_component (name
)] == '\0')
2033 const char *scope
= block_scope (block
);
2035 if (scope
[0] != '\0')
2037 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
2038 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
2040 result
= lookup_symbol_in_static_block (scopedname
, block
,
2042 if (result
.symbol
== NULL
)
2043 result
= lookup_global_symbol (scopedname
, block
, domain
);
2044 do_cleanups (cleanup
);
2052 /* la_sniff_from_mangled_name for Rust. */
2055 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2057 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2058 return *demangled
!= NULL
;
2063 static const struct exp_descriptor exp_descriptor_rust
=
2066 rust_operator_length
,
2067 rust_operator_check
,
2069 rust_dump_subexp_body
,
2070 rust_evaluate_subexp
2073 static const char *rust_extensions
[] =
2078 static const struct language_defn rust_language_defn
=
2088 &exp_descriptor_rust
,
2092 rust_printchar
, /* Print a character constant */
2093 rust_printstr
, /* Function to print string constant */
2094 rust_emitchar
, /* Print a single char */
2095 rust_print_type
, /* Print a type using appropriate syntax */
2096 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2097 rust_val_print
, /* Print a value using appropriate syntax */
2098 c_value_print
, /* Print a top-level value */
2099 default_read_var_value
, /* la_read_var_value */
2100 NULL
, /* Language specific skip_trampoline */
2101 NULL
, /* name_of_this */
2102 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2103 basic_lookup_transparent_type
,/* lookup_transparent_type */
2104 gdb_demangle
, /* Language specific symbol demangler */
2105 rust_sniff_from_mangled_name
,
2106 NULL
, /* Language specific
2107 class_name_from_physname */
2108 c_op_print_tab
, /* expression operators for printing */
2109 1, /* c-style arrays */
2110 0, /* String lower bound */
2111 default_word_break_characters
,
2112 default_make_symbol_completion_list
,
2113 rust_language_arch_info
,
2114 default_print_array_index
,
2115 default_pass_by_reference
,
2117 NULL
, /* la_get_symbol_name_cmp */
2118 iterate_over_symbols
,
2119 &default_varobj_ops
,
2126 _initialize_rust_language (void)
2128 add_language (&rust_language_defn
);