1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cp-support.h"
32 #include "rust-lang.h"
38 extern initialize_file_ftype _initialize_rust_language
;
40 /* Returns the last segment of a Rust path like foo::bar::baz. Will
41 not handle cases where the last segment contains generics. This
42 will return NULL if the last segment cannot be found. */
45 rust_last_path_segment (const char * path
)
47 const char *result
= strrchr (path
, ':');
54 /* Find the Rust crate for BLOCK. If no crate can be found, returns
55 NULL. Otherwise, returns a newly allocated string that the caller
56 is responsible for freeing. */
59 rust_crate_for_block (const struct block
*block
)
61 const char *scope
= block_scope (block
);
66 return xstrndup (scope
, cp_find_first_component (scope
));
69 /* Information about the discriminant/variant of an enum */
73 /* Name of field. Must be freed by caller. */
75 /* Field number in union. Negative on error. For an encoded enum,
76 the "hidden" member will always be field 1, and the "real" member
77 will always be field 0. */
79 /* True if this is an encoded enum that has a single "real" member
80 and a single "hidden" member. */
81 unsigned int is_encoded
: 1;
84 /* The prefix of a specially-encoded enum. */
86 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
88 /* The number of the real field. */
90 #define RUST_ENCODED_ENUM_REAL 0
92 /* The number of the hidden field. */
94 #define RUST_ENCODED_ENUM_HIDDEN 1
96 /* Utility function to get discriminant info for a given value. */
98 static struct disr_info
99 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
100 int embedded_offset
, CORE_ADDR address
,
101 const struct value
*val
)
104 struct disr_info ret
;
105 struct type
*disr_type
;
106 struct ui_file
*temp_file
;
107 struct value_print_options opts
;
108 struct cleanup
*cleanup
;
109 const char *name_segment
;
111 get_no_prettyformat_print_options (&opts
);
116 if (TYPE_NFIELDS (type
) == 0)
117 error (_("Encountered void enum value"));
119 /* If an enum has two values where one is empty and the other holds
120 a pointer that cannot be zero; then the Rust compiler optimizes
121 away the discriminant and instead uses a zero value in the
122 pointer field to indicate the empty variant. */
123 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
124 strlen (RUST_ENUM_PREFIX
)) == 0)
126 char *tail
, *token
, *name
, *saveptr
= NULL
;
127 unsigned long fieldno
;
128 struct type
*member_type
;
133 if (TYPE_NFIELDS (type
) != 1)
134 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
136 /* Optimized enums have only one field. */
137 member_type
= TYPE_FIELD_TYPE (type
, 0);
139 name
= xstrdup (TYPE_FIELD_NAME (type
, 0));
140 cleanup
= make_cleanup (xfree
, name
);
141 tail
= name
+ strlen (RUST_ENUM_PREFIX
);
143 /* The location of the value that doubles as a discriminant is
144 stored in the name of the field, as
145 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
146 where the fieldnos are the indices of the fields that should be
147 traversed in order to find the field (which may be several fields deep)
148 and the variantname is the name of the variant of the case when the
150 for (token
= strtok_r (tail
, "$", &saveptr
);
152 token
= strtok_r (NULL
, "$", &saveptr
))
154 if (sscanf (token
, "%lu", &fieldno
) != 1)
156 /* We have reached the enum name, which cannot start
160 if (fieldno
>= TYPE_NFIELDS (member_type
))
161 error (_("%s refers to field after end of member type"),
164 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
165 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
169 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
170 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
174 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
175 ret
.name
= concat (TYPE_NAME (type
), "::", token
, (char *) NULL
);
179 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
180 ret
.name
= concat (TYPE_NAME (type
), "::",
181 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))),
185 do_cleanups (cleanup
);
189 disr_type
= TYPE_FIELD_TYPE (type
, 0);
191 if (TYPE_NFIELDS (disr_type
) == 0)
193 /* This is a bounds check and should never be hit unless Rust
194 has changed its debuginfo format. */
195 error (_("Could not find enum discriminant field"));
198 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
199 error (_("Rust debug format has changed"));
201 temp_file
= mem_fileopen ();
202 cleanup
= make_cleanup_ui_file_delete (temp_file
);
203 /* The first value of the first field (or any field)
204 is the discriminant value. */
205 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
206 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
207 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
211 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
212 name_segment
= rust_last_path_segment (ret
.name
);
213 if (name_segment
!= NULL
)
215 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
217 /* Sadly, the discriminant value paths do not match the type
218 field name paths ('core::option::Option::Some' vs
219 'core::option::Some'). However, enum variant names are
220 unique in the last path segment and the generics are not
221 part of this path, so we can just compare those. This is
222 hackish and would be better fixed by improving rustc's
223 metadata for enums. */
224 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
226 if (field_type
!= NULL
227 && strcmp (name_segment
,
228 rust_last_path_segment (field_type
)) == 0)
236 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
238 /* Somehow the discriminant wasn't found. */
239 make_cleanup (xfree
, ret
.name
);
240 error (_("Could not find variant of %s with discriminant %s"),
241 TYPE_TAG_NAME (type
), ret
.name
);
244 do_cleanups (cleanup
);
248 /* See rust-lang.h. */
251 rust_tuple_type_p (struct type
*type
)
253 /* The current implementation is a bit of a hack, but there's
254 nothing else in the debuginfo to distinguish a tuple from a
256 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
257 && TYPE_TAG_NAME (type
) != NULL
258 && TYPE_TAG_NAME (type
)[0] == '(');
262 /* Return true if all non-static fields of a structlike type are in a
263 sequence like __0, __1, __2. OFFSET lets us skip fields. */
266 rust_underscore_fields (struct type
*type
, int offset
)
272 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
274 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
276 if (!field_is_static (&TYPE_FIELD (type
, i
)))
284 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
285 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
294 /* See rust-lang.h. */
297 rust_tuple_struct_type_p (struct type
*type
)
299 /* This is just an approximation until DWARF can represent Rust more
300 precisely. We exclude zero-length structs because they may not
301 be tuple structs, and there's no way to tell. */
302 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
305 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
308 rust_tuple_variant_type_p (struct type
*type
)
310 /* First field is discriminant */
311 return rust_underscore_fields (type
, 1);
314 /* Return true if TYPE is a slice type, otherwise false. */
317 rust_slice_type_p (struct type
*type
)
319 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
320 && TYPE_TAG_NAME (type
) != NULL
321 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
324 /* Return true if TYPE is a range type, otherwise false. */
327 rust_range_type_p (struct type
*type
)
331 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
332 || TYPE_NFIELDS (type
) > 2
333 || TYPE_TAG_NAME (type
) == NULL
334 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
337 if (TYPE_NFIELDS (type
) == 0)
341 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
343 if (TYPE_NFIELDS (type
) == 1)
347 else if (TYPE_NFIELDS (type
) == 2)
349 /* First field had to be "start". */
353 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
356 /* Return true if TYPE seems to be the type "u8", otherwise false. */
359 rust_u8_type_p (struct type
*type
)
361 return (TYPE_CODE (type
) == TYPE_CODE_INT
362 && TYPE_UNSIGNED (type
)
363 && TYPE_LENGTH (type
) == 1);
366 /* Return true if TYPE is a Rust character type. */
369 rust_chartype_p (struct type
*type
)
371 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
372 && TYPE_LENGTH (type
) == 4
373 && TYPE_UNSIGNED (type
));
378 /* la_emitchar implementation for Rust. */
381 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
383 if (!rust_chartype_p (type
))
384 generic_emit_char (c
, type
, stream
, quoter
,
385 target_charset (get_type_arch (type
)));
386 else if (c
== '\\' || c
== quoter
)
387 fprintf_filtered (stream
, "\\%c", c
);
389 fputs_filtered ("\\n", stream
);
391 fputs_filtered ("\\r", stream
);
393 fputs_filtered ("\\t", stream
);
395 fputs_filtered ("\\0", stream
);
396 else if (c
>= 32 && c
<= 127 && isprint (c
))
397 fputc_filtered (c
, stream
);
399 fprintf_filtered (stream
, "\\x%02x", c
);
401 fprintf_filtered (stream
, "\\u{%06x}", c
);
404 /* la_printchar implementation for Rust. */
407 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
409 fputs_filtered ("'", stream
);
410 LA_EMIT_CHAR (c
, type
, stream
, '\'');
411 fputs_filtered ("'", stream
);
414 /* la_printstr implementation for Rust. */
417 rust_printstr (struct ui_file
*stream
, struct type
*type
,
418 const gdb_byte
*string
, unsigned int length
,
419 const char *user_encoding
, int force_ellipses
,
420 const struct value_print_options
*options
)
422 /* Rust always uses UTF-8, but let the caller override this if need
424 const char *encoding
= user_encoding
;
425 if (user_encoding
== NULL
|| !*user_encoding
)
427 /* In Rust strings, characters are "u8". */
428 if (rust_u8_type_p (type
))
432 /* This is probably some C string, so let's let C deal with
434 c_printstr (stream
, type
, string
, length
, user_encoding
,
435 force_ellipses
, options
);
440 /* This is not ideal as it doesn't use our character printer. */
441 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
447 static const struct generic_val_print_decorations rust_decorations
=
449 /* Complex isn't used in Rust, but we provide C-ish values just in
461 /* la_val_print implementation for Rust. */
464 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
465 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
466 const struct value
*val
,
467 const struct value_print_options
*options
)
469 type
= check_typedef (type
);
470 switch (TYPE_CODE (type
))
474 LONGEST low_bound
, high_bound
;
476 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
477 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
478 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
480 /* We have a pointer to a byte string, so just print
482 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
484 struct gdbarch
*arch
= get_type_arch (type
);
485 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
487 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
488 if (options
->addressprint
)
490 fputs_filtered (paddress (arch
, addr
), stream
);
491 fputs_filtered (" ", stream
);
494 fputs_filtered ("b", stream
);
495 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
496 high_bound
- low_bound
+ 1, stream
,
503 case TYPE_CODE_METHODPTR
:
504 case TYPE_CODE_MEMBERPTR
:
505 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
506 recurse
, val
, options
);
510 /* Recognize the unit type. */
511 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
512 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
514 fputs_filtered ("()", stream
);
519 case TYPE_CODE_STRING
:
521 struct gdbarch
*arch
= get_type_arch (type
);
522 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
523 LONGEST low_bound
, high_bound
;
525 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
526 error (_("Could not determine the array bounds"));
528 /* If we see a plain TYPE_CODE_STRING, then we're printing a
529 byte string, hence the choice of "ASCII" as the
531 fputs_filtered ("b", stream
);
532 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
533 valaddr
+ embedded_offset
* unit_size
,
534 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
538 case TYPE_CODE_ARRAY
:
540 LONGEST low_bound
, high_bound
;
542 if (get_array_bounds (type
, &low_bound
, &high_bound
)
543 && high_bound
- low_bound
+ 1 == 0)
544 fputs_filtered ("[]", stream
);
550 case TYPE_CODE_UNION
:
552 int j
, nfields
, first_field
, is_tuple
, start
;
553 struct type
*variant_type
;
554 struct disr_info disr
;
555 struct value_print_options opts
;
556 struct cleanup
*cleanup
;
561 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
563 cleanup
= make_cleanup (xfree
, disr
.name
);
565 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
567 fprintf_filtered (stream
, "%s", disr
.name
);
572 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
573 nfields
= TYPE_NFIELDS (variant_type
);
575 is_tuple
= (disr
.is_encoded
576 ? rust_tuple_struct_type_p (variant_type
)
577 : rust_tuple_variant_type_p (variant_type
));
578 start
= disr
.is_encoded
? 0 : 1;
582 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
584 fprintf_filtered (stream
, "%s(", disr
.name
);
587 /* struct variant. */
588 fprintf_filtered (stream
, "%s{", disr
.name
);
593 /* In case of a nullary variant like 'None', just output
595 fprintf_filtered (stream
, "%s", disr
.name
);
599 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
602 fputs_filtered (", ", stream
);
606 fprintf_filtered (stream
, "%s: ",
607 TYPE_FIELD_NAME (variant_type
, j
));
609 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
612 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
613 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
615 stream
, recurse
+ 1, val
, &opts
,
620 fputs_filtered (")", stream
);
622 fputs_filtered ("}", stream
);
625 do_cleanups (cleanup
);
629 case TYPE_CODE_STRUCT
:
633 int is_tuple
= rust_tuple_type_p (type
);
634 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
635 struct value_print_options opts
;
639 if (TYPE_TAG_NAME (type
) != NULL
)
640 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
642 if (TYPE_NFIELDS (type
) == 0)
645 if (TYPE_TAG_NAME (type
) != NULL
)
646 fputs_filtered (" ", stream
);
649 if (is_tuple
|| is_tuple_struct
)
650 fputs_filtered ("(", stream
);
652 fputs_filtered ("{", stream
);
658 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
660 if (field_is_static (&TYPE_FIELD (type
, i
)))
664 fputs_filtered (",", stream
);
666 if (options
->prettyformat
)
668 fputs_filtered ("\n", stream
);
669 print_spaces_filtered (2 + 2 * recurse
, stream
);
671 else if (!first_field
)
672 fputs_filtered (" ", stream
);
676 if (!is_tuple
&& !is_tuple_struct
)
678 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
679 fputs_filtered (": ", stream
);
682 val_print (TYPE_FIELD_TYPE (type
, i
),
684 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
686 stream
, recurse
+ 1, val
, &opts
,
690 if (options
->prettyformat
)
692 fputs_filtered ("\n", stream
);
693 print_spaces_filtered (2 * recurse
, stream
);
696 if (is_tuple
|| is_tuple_struct
)
697 fputs_filtered (")", stream
);
699 fputs_filtered ("}", stream
);
705 /* Nothing special yet. */
706 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
707 recurse
, val
, options
, &rust_decorations
);
713 /* la_print_typedef implementation for Rust. */
716 rust_print_typedef (struct type
*type
,
717 struct symbol
*new_symbol
,
718 struct ui_file
*stream
)
720 type
= check_typedef (type
);
721 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
722 type_print (type
, "", stream
, 0);
723 fprintf_filtered (stream
, ";\n");
726 /* la_print_type implementation for Rust. */
729 rust_print_type (struct type
*type
, const char *varstring
,
730 struct ui_file
*stream
, int show
, int level
,
731 const struct type_print_options
*flags
)
737 && TYPE_NAME (type
) != NULL
)
739 /* Rust calls the unit type "void" in its debuginfo,
740 but we don't want to print it as that. */
741 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
742 fputs_filtered ("()", stream
);
744 fputs_filtered (TYPE_NAME (type
), stream
);
748 type
= check_typedef (type
);
749 switch (TYPE_CODE (type
))
752 fputs_filtered ("()", stream
);
756 /* Delegate varargs to the C printer. */
757 if (TYPE_VARARGS (type
))
760 fputs_filtered ("fn ", stream
);
761 if (varstring
!= NULL
)
762 fputs_filtered (varstring
, stream
);
763 fputs_filtered ("(", stream
);
764 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
768 fputs_filtered (", ", stream
);
769 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
772 fputs_filtered (")", stream
);
773 /* If it returns unit, we can omit the return type. */
774 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
776 fputs_filtered (" -> ", stream
);
777 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
781 case TYPE_CODE_ARRAY
:
783 LONGEST low_bound
, high_bound
;
785 fputs_filtered ("[", stream
);
786 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
787 stream
, show
- 1, level
, flags
);
788 fputs_filtered ("; ", stream
);
790 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
791 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
792 fprintf_filtered (stream
, "variable length");
793 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
794 fprintf_filtered (stream
, "%s",
795 plongest (high_bound
- low_bound
+ 1));
796 fputs_filtered ("]", stream
);
800 case TYPE_CODE_STRUCT
:
804 /* Print a tuple type simply. */
805 if (rust_tuple_type_p (type
))
807 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
811 /* If we see a base class, delegate to C. */
812 if (TYPE_N_BASECLASSES (type
) > 0)
815 fputs_filtered ("struct ", stream
);
816 if (TYPE_TAG_NAME (type
) != NULL
)
817 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
819 is_tuple_struct
= rust_tuple_struct_type_p (type
);
821 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
823 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
825 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
830 if (field_is_static (&TYPE_FIELD (type
, i
)))
833 /* We'd like to print "pub" here as needed, but rustc
834 doesn't emit the debuginfo, and our types don't have
835 cplus_struct_type attached. */
837 /* For a tuple struct we print the type but nothing
839 print_spaces_filtered (level
+ 2, stream
);
840 if (!is_tuple_struct
)
841 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
843 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
844 stream
, show
- 1, level
+ 2,
846 fputs_filtered (",\n", stream
);
849 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
857 fputs_filtered ("enum ", stream
);
858 if (TYPE_TAG_NAME (type
) != NULL
)
860 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
861 fputs_filtered (" ", stream
);
862 len
= strlen (TYPE_TAG_NAME (type
));
864 fputs_filtered ("{\n", stream
);
866 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
868 const char *name
= TYPE_FIELD_NAME (type
, i
);
873 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
875 && name
[len
+ 1] == ':')
877 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
880 fputs_filtered ("}", stream
);
884 case TYPE_CODE_UNION
:
888 /* Skip the discriminant field. */
891 fputs_filtered ("enum ", stream
);
892 if (TYPE_TAG_NAME (type
) != NULL
)
894 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
895 fputs_filtered (" ", stream
);
897 fputs_filtered ("{\n", stream
);
899 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
900 strlen (RUST_ENUM_PREFIX
)) == 0)
902 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
903 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
905 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
906 /* There is no explicit discriminant field, skip nothing. */
911 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
913 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
915 = rust_last_path_segment (TYPE_NAME (variant_type
));
917 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
919 if (TYPE_NFIELDS (variant_type
) > skip_to
)
922 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
925 fputs_filtered (is_tuple
? "(" : "{", stream
);
926 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
931 fputs_filtered (", ", stream
);
934 fprintf_filtered (stream
, "%s: ",
935 TYPE_FIELD_NAME (variant_type
, j
));
937 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
938 stream
, show
- 1, level
+ 2,
941 fputs_filtered (is_tuple
? ")" : "}", stream
);
944 fputs_filtered (",\n", stream
);
947 fputs_filtered ("}", stream
);
953 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
959 /* Compute the alignment of the type T. */
962 rust_type_alignment (struct type
*t
)
964 t
= check_typedef (t
);
965 switch (TYPE_CODE (t
))
968 error (_("Could not compute alignment of type"));
977 return TYPE_LENGTH (t
);
979 case TYPE_CODE_ARRAY
:
980 case TYPE_CODE_COMPLEX
:
981 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
983 case TYPE_CODE_STRUCT
:
984 case TYPE_CODE_UNION
:
989 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
991 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1000 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1001 -- either on an obstack or on a gdbarch. */
1003 static struct type
*
1004 rust_composite_type (struct type
*original
,
1006 const char *field1
, struct type
*type1
,
1007 const char *field2
, struct type
*type2
)
1009 struct type
*result
= alloc_type_copy (original
);
1010 int i
, nfields
, bitpos
;
1018 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1019 TYPE_NAME (result
) = name
;
1020 TYPE_TAG_NAME (result
) = name
;
1022 TYPE_NFIELDS (result
) = nfields
;
1023 TYPE_FIELDS (result
)
1024 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1030 struct field
*field
= &TYPE_FIELD (result
, i
);
1032 SET_FIELD_BITPOS (*field
, bitpos
);
1033 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1035 FIELD_NAME (*field
) = field1
;
1036 FIELD_TYPE (*field
) = type1
;
1041 struct field
*field
= &TYPE_FIELD (result
, i
);
1042 int align
= rust_type_alignment (type2
);
1048 align
*= TARGET_CHAR_BIT
;
1049 delta
= bitpos
% align
;
1051 bitpos
+= align
- delta
;
1053 SET_FIELD_BITPOS (*field
, bitpos
);
1055 FIELD_NAME (*field
) = field2
;
1056 FIELD_TYPE (*field
) = type2
;
1061 TYPE_LENGTH (result
)
1062 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1063 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1067 /* See rust-lang.h. */
1070 rust_slice_type (const char *name
, struct type
*elt_type
,
1071 struct type
*usize_type
)
1075 elt_type
= lookup_pointer_type (elt_type
);
1076 type
= rust_composite_type (elt_type
, name
,
1077 "data_ptr", elt_type
,
1078 "length", usize_type
);
1083 enum rust_primitive_types
1085 rust_primitive_bool
,
1086 rust_primitive_char
,
1095 rust_primitive_isize
,
1096 rust_primitive_usize
,
1099 rust_primitive_unit
,
1101 nr_rust_primitive_types
1104 /* la_language_arch_info implementation for Rust. */
1107 rust_language_arch_info (struct gdbarch
*gdbarch
,
1108 struct language_arch_info
*lai
)
1110 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1112 struct type
**types
;
1113 unsigned int length
;
1115 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1118 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1119 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1120 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1121 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1122 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1123 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1124 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1125 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1126 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1127 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1129 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1130 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1131 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1133 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1134 floatformats_ieee_single
);
1135 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1136 floatformats_ieee_double
);
1138 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1140 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1141 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1142 types
[rust_primitive_usize
]);
1144 lai
->primitive_type_vector
= types
;
1145 lai
->bool_type_default
= types
[rust_primitive_bool
];
1146 lai
->string_char_type
= types
[rust_primitive_u8
];
1151 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1153 static struct value
*
1154 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1157 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1159 struct value
*function
, *result
, *arg0
;
1160 struct type
*type
, *fn_type
;
1161 const struct block
*block
;
1162 struct block_symbol sym
;
1164 /* For an ordinary function call we can simply defer to the
1165 generic implementation. */
1166 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1167 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1169 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1171 method
= &exp
->elts
[*pos
+ 1].string
;
1172 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1174 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1175 type in order to look up the method. */
1176 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1178 if (noside
== EVAL_SKIP
)
1180 for (i
= 0; i
< num_args
; ++i
)
1181 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1185 std::vector
<struct value
*> args (num_args
+ 1);
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 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1203 block
= get_selected_block (0);
1204 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1205 if (sym
.symbol
== NULL
)
1206 error (_("Could not find function named '%s'"), name
.c_str ());
1208 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1209 if (TYPE_NFIELDS (fn_type
) == 0)
1210 error (_("Function '%s' takes no arguments"), name
.c_str ());
1212 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1213 args
[0] = value_addr (args
[0]);
1215 function
= address_of_variable (sym
.symbol
, block
);
1217 for (i
= 0; i
< num_args
; ++i
)
1218 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1220 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1221 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1223 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1227 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1229 static struct value
*
1230 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1232 enum range_type kind
;
1233 struct value
*low
= NULL
, *high
= NULL
;
1234 struct value
*addrval
, *result
;
1236 struct type
*range_type
;
1237 struct type
*index_type
;
1238 struct type
*temp_type
;
1241 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1244 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1245 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1246 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1247 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1249 if (noside
== EVAL_SKIP
)
1250 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1257 name
= "std::ops::RangeFull";
1261 index_type
= value_type (high
);
1262 name
= "std::ops::RangeTo";
1269 index_type
= value_type (low
);
1270 name
= "std::ops::RangeFrom";
1274 if (!types_equal (value_type (low
), value_type (high
)))
1275 error (_("Range expression with different types"));
1276 index_type
= value_type (low
);
1277 name
= "std::ops::Range";
1281 /* If we don't have an index type, just allocate this on the
1282 arch. Here any type will do. */
1283 temp_type
= (index_type
== NULL
1284 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1286 /* It would be nicer to cache the range type. */
1287 range_type
= rust_composite_type (temp_type
, name
,
1288 low
== NULL
? NULL
: "start", index_type
,
1289 high
== NULL
? NULL
: "end", index_type
);
1291 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1292 return value_zero (range_type
, lval_memory
);
1294 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1295 addr
= value_as_long (addrval
);
1296 result
= value_at_lazy (range_type
, addr
);
1300 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1303 value_assign (start
, low
);
1308 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1311 value_assign (end
, high
);
1314 result
= value_at_lazy (range_type
, addr
);
1318 /* A helper function to compute the range and kind given a range
1319 value. TYPE is the type of the range value. RANGE is the range
1320 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1321 parameters might be filled in, or might not be, depending on the
1322 kind of range this is. KIND will always be set to the appropriate
1323 value describing the kind of range, and this can be used to
1324 determine whether LOW or HIGH are valid. */
1327 rust_compute_range (struct type
*type
, struct value
*range
,
1328 LONGEST
*low
, LONGEST
*high
,
1329 enum range_type
*kind
)
1335 *kind
= BOTH_BOUND_DEFAULT
;
1337 if (TYPE_NFIELDS (type
) == 0)
1341 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1343 *kind
= HIGH_BOUND_DEFAULT
;
1344 *low
= value_as_long (value_field (range
, 0));
1347 if (TYPE_NFIELDS (type
) > i
1348 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1350 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1351 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1352 *high
= value_as_long (value_field (range
, i
));
1356 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1358 static struct value
*
1359 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1362 struct value
*lhs
, *rhs
, *result
;
1363 struct type
*rhstype
;
1364 LONGEST low
, high_bound
;
1365 /* Initialized to appease the compiler. */
1366 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1371 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1372 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1374 if (noside
== EVAL_SKIP
)
1377 rhstype
= check_typedef (value_type (rhs
));
1378 if (rust_range_type_p (rhstype
))
1381 error (_("Can't take slice of array without '&'"));
1382 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1386 low
= value_as_long (rhs
);
1388 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1390 struct type
*type
= check_typedef (value_type (lhs
));
1392 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1398 struct type
*type
= check_typedef (value_type (lhs
));
1400 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1403 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1404 error (_("Can't compute array bounds"));
1406 error (_("Found array with non-zero lower bound"));
1409 else if (rust_slice_type_p (type
))
1413 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1414 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1416 high_bound
= value_as_long (len
);
1418 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1422 high_bound
= LONGEST_MAX
;
1425 error (_("Cannot subscript non-array type"));
1428 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1431 error (_("Index less than zero"));
1432 if (low
> high_bound
)
1433 error (_("Index greater than length"));
1435 result
= value_subscript (base
, low
);
1442 struct type
*usize
, *slice
;
1444 struct value
*addrval
, *tem
;
1446 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1449 error (_("High index less than zero"));
1451 error (_("Low index greater than high index"));
1452 if (high
> high_bound
)
1453 error (_("High index greater than length"));
1455 usize
= language_lookup_primitive_type (exp
->language_defn
,
1458 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1461 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1462 addr
= value_as_long (addrval
);
1463 tem
= value_at_lazy (slice
, addr
);
1465 value_assign (value_field (tem
, 0), value_addr (result
));
1466 value_assign (value_field (tem
, 1),
1467 value_from_longest (usize
, high
- low
));
1469 result
= value_at_lazy (slice
, addr
);
1472 result
= value_addr (result
);
1478 /* evaluate_exp implementation for Rust. */
1480 static struct value
*
1481 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1482 int *pos
, enum noside noside
)
1484 struct value
*result
;
1486 switch (exp
->elts
[*pos
].opcode
)
1488 case UNOP_COMPLEMENT
:
1490 struct value
*value
;
1493 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1494 if (noside
== EVAL_SKIP
)
1496 /* Preserving the type is enough. */
1499 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1500 result
= value_from_longest (value_type (value
),
1501 value_logical_not (value
));
1503 result
= value_complement (value
);
1507 case BINOP_SUBSCRIPT
:
1508 result
= rust_subscript (exp
, pos
, noside
, 0);
1512 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1518 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1519 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1522 struct value
*addrval
= NULL
;
1526 if (noside
== EVAL_NORMAL
)
1528 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1529 addr
= value_as_long (addrval
);
1530 result
= value_at_lazy (type
, addr
);
1533 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1538 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1539 if (noside
== EVAL_NORMAL
)
1541 /* This isn't quite right but will do for the time
1542 being, seeing that we can't implement the Copy
1544 value_assign (result
, init
);
1550 gdb_assert (arglen
% 2 == 0);
1551 for (i
= 0; i
< arglen
; i
+= 2)
1554 const char *fieldname
;
1555 struct value
*value
, *field
;
1557 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1559 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1561 fieldname
= &exp
->elts
[*pos
].string
;
1562 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1564 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1565 if (noside
== EVAL_NORMAL
)
1567 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1569 value_assign (field
, value
);
1573 if (noside
== EVAL_SKIP
)
1574 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1576 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1577 result
= allocate_value (type
);
1579 result
= value_at_lazy (type
, addr
);
1588 struct value
*ncopies
;
1590 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1591 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1592 copies
= value_as_long (ncopies
);
1594 error (_("Array with negative number of elements"));
1596 if (noside
== EVAL_NORMAL
)
1600 std::vector
<struct value
*> eltvec (copies
);
1602 for (i
= 0; i
< copies
; ++i
)
1604 result
= value_array (0, copies
- 1, eltvec
.data ());
1608 struct type
*arraytype
1609 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1610 result
= allocate_value (arraytype
);
1615 case STRUCTOP_ANONYMOUS
:
1617 /* Anonymous field access, i.e. foo.1. */
1619 int pc
, field_number
, nfields
;
1620 struct type
*type
, *variant_type
;
1621 struct disr_info disr
;
1624 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1626 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1628 type
= value_type (lhs
);
1629 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1631 struct cleanup
*cleanup
;
1633 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1634 value_embedded_offset (lhs
),
1635 value_address (lhs
), lhs
);
1637 cleanup
= make_cleanup (xfree
, disr
.name
);
1639 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1641 variant_type
= NULL
;
1646 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1647 nfields
= TYPE_NFIELDS (variant_type
);
1650 if (!disr
.is_encoded
)
1653 if (field_number
>= nfields
|| field_number
< 0)
1654 error(_("Cannot access field %d of variant %s, \
1655 there are only %d fields"),
1656 disr
.is_encoded
? field_number
: field_number
- 1,
1658 disr
.is_encoded
? nfields
: nfields
- 1);
1660 if (!(disr
.is_encoded
1661 ? rust_tuple_struct_type_p (variant_type
)
1662 : rust_tuple_variant_type_p (variant_type
)))
1663 error(_("Variant %s is not a tuple variant"), disr
.name
);
1665 result
= value_primitive_field (lhs
, 0, field_number
,
1667 do_cleanups (cleanup
);
1669 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1671 /* Tuples and tuple structs */
1672 nfields
= TYPE_NFIELDS(type
);
1674 if (field_number
>= nfields
|| field_number
< 0)
1675 error(_("Cannot access field %d of %s, there are only %d fields"),
1676 field_number
, TYPE_TAG_NAME (type
), nfields
);
1678 /* Tuples are tuple structs too. */
1679 if (!rust_tuple_struct_type_p (type
))
1680 error(_("Attempting to access anonymous field %d of %s, which is \
1681 not a tuple, tuple struct, or tuple-like variant"),
1682 field_number
, TYPE_TAG_NAME (type
));
1684 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1687 error(_("Anonymous field access is only allowed on tuples, \
1688 tuple structs, and tuple-like enum variants"));
1692 case STRUCTOP_STRUCT
:
1699 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1700 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1701 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1703 type
= value_type (lhs
);
1705 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1708 struct disr_info disr
;
1709 struct cleanup
* cleanup
;
1710 struct type
* variant_type
;
1713 field_name
= &exp
->elts
[pc
+ 2].string
;
1715 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1716 value_embedded_offset (lhs
),
1717 value_address (lhs
), lhs
);
1719 cleanup
= make_cleanup (xfree
, disr
.name
);
1721 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1722 error(_("Could not find field %s of struct variant %s"),
1723 field_name
, disr
.name
);
1725 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1727 if (variant_type
== NULL
1728 || rust_tuple_variant_type_p (variant_type
))
1729 error(_("Attempting to access named field %s of tuple variant %s, \
1730 which has only anonymous fields"),
1731 field_name
, disr
.name
);
1733 start
= disr
.is_encoded
? 0 : 1;
1734 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1736 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1738 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1743 if (i
== TYPE_NFIELDS (variant_type
))
1744 /* We didn't find it. */
1745 error(_("Could not find field %s of struct variant %s"),
1746 field_name
, disr
.name
);
1748 do_cleanups (cleanup
);
1753 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1759 result
= rust_range (exp
, pos
, noside
);
1763 /* We might have &array[range], in which case we need to make a
1765 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1768 result
= rust_subscript (exp
, pos
, noside
, 1);
1773 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1780 /* operator_length implementation for Rust. */
1783 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1789 switch (exp
->elts
[pc
- 1].opcode
)
1792 /* We handle aggregate as a type and argument count. The first
1793 argument might be OP_OTHERS. After that the arguments
1794 alternate: first an OP_NAME, then an expression. */
1796 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1804 case STRUCTOP_ANONYMOUS
:
1815 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1823 /* op_name implementation for Rust. */
1826 rust_op_name (enum exp_opcode opcode
)
1831 return "OP_AGGREGATE";
1835 return op_name_standard (opcode
);
1839 /* dump_subexp_body implementation for Rust. */
1842 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1845 switch (exp
->elts
[elt
].opcode
)
1849 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1852 fprintf_filtered (stream
, "Type @");
1853 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1854 fprintf_filtered (stream
, " (");
1855 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1856 fprintf_filtered (stream
, "), length %d", length
);
1859 for (i
= 0; i
< length
; ++i
)
1860 elt
= dump_subexp (exp
, stream
, elt
);
1867 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1869 fprintf_filtered (stream
, "%s: %s",
1870 (exp
->elts
[elt
].opcode
== OP_STRING
1871 ? "string" : "name"),
1872 &exp
->elts
[elt
+ 2].string
);
1873 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1878 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1881 case STRUCTOP_ANONYMOUS
:
1885 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1887 fprintf_filtered (stream
, "Field number: %d", field_number
);
1888 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1896 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1903 /* print_subexp implementation for Rust. */
1906 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1907 enum precedence prec
)
1909 switch (exp
->elts
[*pos
].opcode
)
1913 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1916 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1917 fputs_filtered (" { ", stream
);
1920 for (i
= 0; i
< length
; ++i
)
1922 rust_print_subexp (exp
, pos
, stream
, prec
);
1923 fputs_filtered (", ", stream
);
1925 fputs_filtered (" }", stream
);
1931 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1933 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1934 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1940 fputs_filtered ("<<others>> (", stream
);
1942 rust_print_subexp (exp
, pos
, stream
, prec
);
1943 fputs_filtered (")", stream
);
1947 case STRUCTOP_ANONYMOUS
:
1949 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1952 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1953 fprintf_filtered (stream
, ".%d", tem
);
1959 fprintf_filtered (stream
, "[");
1960 rust_print_subexp (exp
, pos
, stream
, prec
);
1961 fprintf_filtered (stream
, "; ");
1962 rust_print_subexp (exp
, pos
, stream
, prec
);
1963 fprintf_filtered (stream
, "]");
1967 print_subexp_standard (exp
, pos
, stream
, prec
);
1972 /* operator_check implementation for Rust. */
1975 rust_operator_check (struct expression
*exp
, int pos
,
1976 int (*objfile_func
) (struct objfile
*objfile
,
1980 switch (exp
->elts
[pos
].opcode
)
1984 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1985 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1987 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1998 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2006 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2008 static struct block_symbol
2009 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2011 const struct block
*block
,
2012 const domain_enum domain
)
2014 struct block_symbol result
= {NULL
, NULL
};
2016 if (symbol_lookup_debug
)
2018 fprintf_unfiltered (gdb_stdlog
,
2019 "rust_lookup_symbol_non_local"
2020 " (%s, %s (scope %s), %s)\n",
2021 name
, host_address_to_string (block
),
2022 block_scope (block
), domain_name (domain
));
2025 /* Look up bare names in the block's scope. */
2026 if (name
[cp_find_first_component (name
)] == '\0')
2028 const char *scope
= block_scope (block
);
2030 if (scope
[0] != '\0')
2032 std::string scopedname
= std::string (scope
) + "::" + name
;
2034 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2036 if (result
.symbol
== NULL
)
2037 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2045 /* la_sniff_from_mangled_name for Rust. */
2048 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2050 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2051 return *demangled
!= NULL
;
2056 static const struct exp_descriptor exp_descriptor_rust
=
2059 rust_operator_length
,
2060 rust_operator_check
,
2062 rust_dump_subexp_body
,
2063 rust_evaluate_subexp
2066 static const char *rust_extensions
[] =
2071 static const struct language_defn rust_language_defn
=
2081 &exp_descriptor_rust
,
2085 rust_printchar
, /* Print a character constant */
2086 rust_printstr
, /* Function to print string constant */
2087 rust_emitchar
, /* Print a single char */
2088 rust_print_type
, /* Print a type using appropriate syntax */
2089 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2090 rust_val_print
, /* Print a value using appropriate syntax */
2091 c_value_print
, /* Print a top-level value */
2092 default_read_var_value
, /* la_read_var_value */
2093 NULL
, /* Language specific skip_trampoline */
2094 NULL
, /* name_of_this */
2095 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2096 basic_lookup_transparent_type
,/* lookup_transparent_type */
2097 gdb_demangle
, /* Language specific symbol demangler */
2098 rust_sniff_from_mangled_name
,
2099 NULL
, /* Language specific
2100 class_name_from_physname */
2101 c_op_print_tab
, /* expression operators for printing */
2102 1, /* c-style arrays */
2103 0, /* String lower bound */
2104 default_word_break_characters
,
2105 default_make_symbol_completion_list
,
2106 rust_language_arch_info
,
2107 default_print_array_index
,
2108 default_pass_by_reference
,
2110 NULL
, /* la_get_symbol_name_cmp */
2111 iterate_over_symbols
,
2112 &default_varobj_ops
,
2119 _initialize_rust_language (void)
2121 add_language (&rust_language_defn
);